Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(execute): build arrow columns with null values #638

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 86 additions & 7 deletions execute/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ func (b *ColListTableBuilder) SetNil(i, j int) error {
return fmt.Errorf("set nil: column does not exist, index out of bounds: %d", j)
}
if i < 0 || i > b.cols[j].Len() {
return fmt.Errorf("set nil: row does not exist, index out of bounds: %d", i)
return fmt.Errorf("set nil: row does not exist, index out of bounds: %d", i)
}

b.cols[j].SetNil(i, true)
Expand Down Expand Up @@ -1613,7 +1613,22 @@ func (c *boolColumnBuilder) Clear() {
c.data = c.data[0:0]
}
func (c *boolColumnBuilder) Copy() column {
data := arrow.NewBool(c.data, c.alloc.Allocator)
var data *array.Boolean
if len(c.nils) > 0 {
b := arrow.NewBoolBuilder(c.alloc.Allocator)
b.Reserve(len(c.data))
for i, v := range c.data {
if c.nils[i] {
b.UnsafeAppendBoolToBitmap(false)
continue
}
b.UnsafeAppend(v)
}
data = b.NewBooleanArray()
b.Release()
} else {
data = arrow.NewBool(c.data, c.alloc.Allocator)
}
col := &boolColumn{
ColMeta: c.ColMeta,
data: data,
Expand Down Expand Up @@ -1683,7 +1698,22 @@ func (c *intColumnBuilder) Clear() {
c.data = c.data[0:0]
}
func (c *intColumnBuilder) Copy() column {
data := arrow.NewInt(c.data, c.alloc.Allocator)
var data *array.Int64
if len(c.nils) > 0 {
b := arrow.NewIntBuilder(c.alloc.Allocator)
b.Reserve(len(c.data))
for i, v := range c.data {
if c.nils[i] {
b.UnsafeAppendBoolToBitmap(false)
continue
}
b.UnsafeAppend(v)
}
data = b.NewInt64Array()
b.Release()
} else {
data = arrow.NewInt(c.data, c.alloc.Allocator)
}
col := &intColumn{
ColMeta: c.ColMeta,
data: data,
Expand Down Expand Up @@ -1750,7 +1780,22 @@ func (c *uintColumnBuilder) Clear() {
c.data = c.data[0:0]
}
func (c *uintColumnBuilder) Copy() column {
data := arrow.NewUint(c.data, c.alloc.Allocator)
var data *array.Uint64
if len(c.nils) > 0 {
b := arrow.NewUintBuilder(c.alloc.Allocator)
b.Reserve(len(c.data))
for i, v := range c.data {
if c.nils[i] {
b.UnsafeAppendBoolToBitmap(false)
continue
}
b.UnsafeAppend(v)
}
data = b.NewUint64Array()
b.Release()
} else {
data = arrow.NewUint(c.data, c.alloc.Allocator)
}
col := &uintColumn{
ColMeta: c.ColMeta,
data: data,
Expand Down Expand Up @@ -1817,7 +1862,22 @@ func (c *floatColumnBuilder) Clear() {
c.data = c.data[0:0]
}
func (c *floatColumnBuilder) Copy() column {
data := arrow.NewFloat(c.data, c.alloc.Allocator)
var data *array.Float64
if len(c.nils) > 0 {
b := arrow.NewFloatBuilder(c.alloc.Allocator)
b.Reserve(len(c.data))
for i, v := range c.data {
if c.nils[i] {
b.UnsafeAppendBoolToBitmap(false)
continue
}
b.UnsafeAppend(v)
}
data = b.NewFloat64Array()
b.Release()
} else {
data = arrow.NewFloat(c.data, c.alloc.Allocator)
}
col := &floatColumn{
ColMeta: c.ColMeta,
data: data,
Expand Down Expand Up @@ -1884,7 +1944,22 @@ func (c *stringColumnBuilder) Clear() {
c.data = c.data[0:0]
}
func (c *stringColumnBuilder) Copy() column {
data := arrow.NewString(c.data, c.alloc.Allocator)
var data *array.Binary
if len(c.nils) > 0 {
b := arrow.NewStringBuilder(c.alloc.Allocator)
b.Reserve(len(c.data))
for i, v := range c.data {
if c.nils[i] {
b.UnsafeAppendBoolToBitmap(false)
continue
}
b.AppendString(v)
}
data = b.NewBinaryArray()
b.Release()
} else {
data = arrow.NewString(c.data, c.alloc.Allocator)
}
col := &stringColumn{
ColMeta: c.ColMeta,
data: data,
Expand Down Expand Up @@ -1953,7 +2028,11 @@ func (c *timeColumnBuilder) Clear() {
func (c *timeColumnBuilder) Copy() column {
b := arrow.NewIntBuilder(c.alloc.Allocator)
b.Reserve(len(c.data))
for _, v := range c.data {
for i, v := range c.data {
if c.nils[i] {
b.UnsafeAppendBoolToBitmap(false)
continue
}
b.UnsafeAppend(int64(v))
}
col := &timeColumn{
Expand Down
79 changes: 79 additions & 0 deletions execute/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/execute/executetest"
"github.com/influxdata/flux/memory"
)

func TestTablesEqual(t *testing.T) {
Expand Down Expand Up @@ -172,3 +173,81 @@ func TestTablesEqual(t *testing.T) {
})
}
}

func TestColListTable_AppendNil(t *testing.T) {
key := execute.NewGroupKey(nil, nil)
tb := execute.NewColListTableBuilder(key, &memory.Allocator{})

// Add a column for the value.
idx, _ := tb.AddCol(flux.ColMeta{
Label: execute.DefaultValueColLabel,
Type: flux.TFloat,
})

// Add one normal value and add one nil value.
_ = tb.AppendFloat(idx, 1.0)
_ = tb.AppendNil(idx)

// Build the table and then verify the arrow table.
tbl, err := tb.Table()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if err := tbl.DoArrow(func(cr flux.ArrowColReader) error {
vs := cr.Floats(idx)
if got, want := vs.Len(), 2; got != want {
t.Errorf("unexpected length -want/+got\n\t- %d\n\t+ %d", want, got)
return nil
}

if vs.IsNull(0) {
t.Error("first value should not be null")
}
if !vs.IsNull(1) {
t.Error("second value should be null")
}
return nil
}); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}

func TestColListTable_SetNil(t *testing.T) {
key := execute.NewGroupKey(nil, nil)
tb := execute.NewColListTableBuilder(key, &memory.Allocator{})

// Add a column for the value.
idx, _ := tb.AddCol(flux.ColMeta{
Label: execute.DefaultValueColLabel,
Type: flux.TFloat,
})

// Grow by two values and then set the second to nil.
_ = tb.GrowFloats(idx, 2)
_ = tb.SetNil(1, idx)

// Build the table and then verify the arrow table.
tbl, err := tb.Table()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if err := tbl.DoArrow(func(cr flux.ArrowColReader) error {
vs := cr.Floats(idx)
if got, want := vs.Len(), 2; got != want {
t.Errorf("unexpected length -want/+got\n\t- %d\n\t+ %d", want, got)
return nil
}

if vs.IsNull(0) {
t.Error("first value should not be null")
}
if !vs.IsNull(1) {
t.Error("second value should be null")
}
return nil
}); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}