@@ -6,73 +6,75 @@ import (
66 "github.com/apache/arrow/go/v13/arrow/memory"
77)
88
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 {
9+ func stripNullsFromLists (record arrow.Record ) arrow.Record {
10+ cols := record .Columns ()
11+ for c , col := range cols {
12+ list , ok := col .(array.ListLike )
13+ if ! ok {
14+ continue
15+ }
16+ if _ , ok := list .(* array.Map ); ok {
17+ // maps also correspond to array.ListLike
18+ continue
19+ }
20+
21+ bldr := array .NewListBuilder (memory .DefaultAllocator , list .DataType ().(arrow.ListLikeType ).Elem ())
22+ for j := 0 ; j < list .Len (); j ++ {
23+ if list .IsNull (j ) {
24+ bldr .AppendNull ()
1725 continue
1826 }
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 ()
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 ) {
2533 continue
2634 }
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- }
35+ err := vBldr .AppendValueFromString (slc .ValueStr (k ))
36+ if err != nil {
37+ panic (err )
3938 }
4039 }
41- cols [c ] = bldr .NewArray ()
4240 }
43- records [ i ] = array . NewRecord ( records [ i ]. Schema (), cols , records [ i ]. NumRows () )
41+ cols [ c ] = bldr . NewArray ( )
4442 }
43+ return array .NewRecord (record .Schema (), cols , record .NumRows ())
4544}
4645
4746type AllowNullFunc func (arrow.DataType ) bool
4847
49- // TODO(v4): use in v4
50- //
51- // nolint:unused
52- func (f AllowNullFunc ) replaceNullsByEmpty (records []arrow.Record ) {
53- if f == nil {
54- return
48+ func (s * WriterTestSuite ) replaceNullsByEmpty (record arrow.Record ) arrow.Record {
49+ if s .allowNull == nil {
50+ return record
5551 }
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 ()) {
52+
53+ cols := record .Columns ()
54+ for c , col := range cols {
55+ if col .NullN () == 0 || s .allowNull (col .DataType ()) {
56+ continue
57+ }
58+
59+ builder := array .NewBuilder (memory .DefaultAllocator , col .DataType ())
60+ for j := 0 ; j < col .Len (); j ++ {
61+ if col .IsNull (j ) {
62+ builder .AppendEmptyValue ()
6063 continue
6164 }
6265
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- }
66+ if err := builder .AppendValueFromString (col .ValueStr (j )); err != nil {
67+ panic (err )
7368 }
74- cols [c ] = builder .NewArray ()
7569 }
76- records [i ] = array .NewRecord (records [i ].Schema (), cols , records [i ].NumRows ())
70+ cols [c ] = builder .NewArray ()
71+ }
72+ return array .NewRecord (record .Schema (), cols , record .NumRows ())
73+ }
74+
75+ func (s * WriterTestSuite ) handleNulls (record arrow.Record ) arrow.Record {
76+ if s .ignoreNullsInLists {
77+ record = stripNullsFromLists (record )
7778 }
79+ return s .replaceNullsByEmpty (record )
7880}
0 commit comments