|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/apache/arrow/go/v13/arrow" |
| 5 | + "github.com/apache/arrow/go/v13/arrow/array" |
| 6 | + "github.com/apache/arrow/go/v13/arrow/memory" |
| 7 | +) |
| 8 | + |
| 9 | +// TODO(v4): use in v4 |
| 10 | +// |
| 11 | +// nolint:unused |
| 12 | +func stripNullsFromLists(records []arrow.Record) { |
| 13 | + for i := range records { |
| 14 | + cols := records[i].Columns() |
| 15 | + for c, col := range cols { |
| 16 | + if col.DataType().ID() != arrow.LIST { |
| 17 | + continue |
| 18 | + } |
| 19 | + |
| 20 | + list := col.(*array.List) |
| 21 | + bldr := array.NewListBuilder(memory.DefaultAllocator, list.DataType().(*arrow.ListType).Elem()) |
| 22 | + for j := 0; j < list.Len(); j++ { |
| 23 | + if list.IsNull(j) { |
| 24 | + bldr.AppendNull() |
| 25 | + continue |
| 26 | + } |
| 27 | + bldr.Append(true) |
| 28 | + vBldr := bldr.ValueBuilder() |
| 29 | + from, to := list.ValueOffsets(j) |
| 30 | + slc := array.NewSlice(list.ListValues(), from, to) |
| 31 | + for k := 0; k < int(to-from); k++ { |
| 32 | + if slc.IsNull(k) { |
| 33 | + continue |
| 34 | + } |
| 35 | + err := vBldr.AppendValueFromString(slc.ValueStr(k)) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + cols[c] = bldr.NewArray() |
| 42 | + } |
| 43 | + records[i] = array.NewRecord(records[i].Schema(), cols, records[i].NumRows()) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +type AllowNullFunc func(arrow.DataType) bool |
| 48 | + |
| 49 | +// TODO(v4): use in v4 |
| 50 | +// |
| 51 | +// nolint:unused |
| 52 | +func (f AllowNullFunc) replaceNullsByEmpty(records []arrow.Record) { |
| 53 | + if f == nil { |
| 54 | + return |
| 55 | + } |
| 56 | + for i := range records { |
| 57 | + cols := records[i].Columns() |
| 58 | + for c, col := range records[i].Columns() { |
| 59 | + if col.NullN() == 0 || f(col.DataType()) { |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + builder := array.NewBuilder(memory.DefaultAllocator, records[i].Column(c).DataType()) |
| 64 | + for j := 0; j < col.Len(); j++ { |
| 65 | + if col.IsNull(j) { |
| 66 | + builder.AppendEmptyValue() |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + if err := builder.AppendValueFromString(col.ValueStr(j)); err != nil { |
| 71 | + panic(err) |
| 72 | + } |
| 73 | + } |
| 74 | + cols[c] = builder.NewArray() |
| 75 | + } |
| 76 | + records[i] = array.NewRecord(records[i].Schema(), cols, records[i].NumRows()) |
| 77 | + } |
| 78 | +} |
0 commit comments