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

fix: Put null helpers back #1002

Merged
merged 6 commits into from
Jun 29, 2023
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
78 changes: 78 additions & 0 deletions plugin/nulls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package plugin

import (
"github.com/apache/arrow/go/v13/arrow"
"github.com/apache/arrow/go/v13/arrow/array"
"github.com/apache/arrow/go/v13/arrow/memory"
)

// TODO(v4): use in v4
//
// nolint:unused
func stripNullsFromLists(records []arrow.Record) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding unused code? Will there be a follow-up where it gets used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I was just migrating CH when I noticed this missing.

I can comment out the relevant section in CH tests, but it will still be required once we bring the vast testing back

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave this here as a reminder, but can we rather add it back when it's needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

for i := range records {
cols := records[i].Columns()
for c, col := range cols {
if col.DataType().ID() != arrow.LIST {
continue
}

list := col.(*array.List)
bldr := array.NewListBuilder(memory.DefaultAllocator, list.DataType().(*arrow.ListType).Elem())
for j := 0; j < list.Len(); j++ {
if list.IsNull(j) {
bldr.AppendNull()
continue
}
bldr.Append(true)
vBldr := bldr.ValueBuilder()
from, to := list.ValueOffsets(j)
slc := array.NewSlice(list.ListValues(), from, to)
for k := 0; k < int(to-from); k++ {
if slc.IsNull(k) {
continue
}
err := vBldr.AppendValueFromString(slc.ValueStr(k))
if err != nil {
panic(err)
}
}
}
cols[c] = bldr.NewArray()
}
records[i] = array.NewRecord(records[i].Schema(), cols, records[i].NumRows())
}
}

type AllowNullFunc func(arrow.DataType) bool

// TODO(v4): use in v4
//
// nolint:unused
func (f AllowNullFunc) replaceNullsByEmpty(records []arrow.Record) {
if f == nil {
return
}
for i := range records {
cols := records[i].Columns()
for c, col := range records[i].Columns() {
if col.NullN() == 0 || f(col.DataType()) {
continue
}

builder := array.NewBuilder(memory.DefaultAllocator, records[i].Column(c).DataType())
for j := 0; j < col.Len(); j++ {
if col.IsNull(j) {
builder.AppendEmptyValue()
continue
}

if err := builder.AppendValueFromString(col.ValueStr(j)); err != nil {
panic(err)
}
}
cols[c] = builder.NewArray()
}
records[i] = array.NewRecord(records[i].Schema(), cols, records[i].NumRows())
}
}
13 changes: 7 additions & 6 deletions plugin/testing_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/apache/arrow/go/v13/arrow"
"github.com/cloudquery/plugin-sdk/v4/schema"
)

Expand All @@ -16,7 +17,7 @@ type WriterTestSuite struct {
// Destinations that have problems representing some data types should provide a custom implementation here.
// If this param is empty, the default is to allow all data types to be nullable.
// When the value returned by this func is `true` the comparison is made with the empty value instead of null.
// allowNull AllowNullFunc
allowNull AllowNullFunc

// IgnoreNullsInLists allows stripping null values from lists before comparison.
// Destination setups that don't support nulls in lists should set this to true.
Expand Down Expand Up @@ -56,11 +57,11 @@ type WriterTestSuiteTests struct {

type NewPluginFunc func() *Plugin

// func WithTestSourceAllowNull(allowNull func(arrow.DataType) bool) func(o *WriterTestSuite) {
// return func(o *WriterTestSuite) {
// o.allowNull = allowNull
// }
// }
func WithTestSourceAllowNull(allowNull func(arrow.DataType) bool) func(o *WriterTestSuite) {
return func(o *WriterTestSuite) {
o.allowNull = allowNull
}
}

func WithTestIgnoreNullsInLists() func(o *WriterTestSuite) {
return func(o *WriterTestSuite) {
Expand Down
Loading