2828
2929import org .apache .arrow .memory .BufferAllocator ;
3030import org .apache .arrow .memory .RootAllocator ;
31+ import org .apache .arrow .vector .complex .DenseUnionVector ;
3132import org .apache .arrow .vector .complex .FixedSizeListVector ;
3233import org .apache .arrow .vector .complex .ListVector ;
3334import org .apache .arrow .vector .complex .MapVector ;
@@ -49,7 +50,7 @@ public class TestSplitAndTransfer {
4950 public void init () {
5051 allocator = new RootAllocator (Long .MAX_VALUE );
5152 }
52-
53+
5354 @ After
5455 public void terminate () throws Exception {
5556 allocator .close ();
@@ -65,21 +66,130 @@ private void populateVarcharVector(final VarCharVector vector, int valueCount, S
6566 }
6667 vector .setValueCount (valueCount );
6768 }
68-
69+
70+ private void populateIntVector (final IntVector vector , int valueCount ) {
71+ for (int i = 0 ; i < valueCount ; i ++) {
72+ vector .set (i , i );
73+ }
74+ vector .setValueCount (valueCount );
75+ }
76+
77+ private void populateDenseUnionVector (final DenseUnionVector vector , int valueCount ) {
78+ VarCharVector varCharVector = vector .addOrGet ("varchar" , FieldType .nullable (new ArrowType .Utf8 ()),
79+ VarCharVector .class );
80+ BigIntVector intVector = vector .addOrGet ("int" ,
81+ FieldType .nullable (new ArrowType .Int (64 , true )), BigIntVector .class );
82+
83+ for (int i = 0 ; i < valueCount ; i ++) {
84+ vector .setTypeId (i , (byte ) (i % 2 ));
85+ if (i % 2 == 0 ) {
86+ final String s = String .format ("%010d" , i );
87+ varCharVector .setSafe (i / 2 , s .getBytes (StandardCharsets .UTF_8 ));
88+ } else {
89+ intVector .setSafe (i / 2 , i );
90+ }
91+ }
92+ vector .setValueCount (valueCount );
93+ }
94+
95+ @ Test
96+ public void testWithEmptyVector () {
97+ // MapVector use TransferImpl from ListVector
98+ ListVector listVector = ListVector .empty ("" , allocator );
99+ TransferPair transferPair = listVector .getTransferPair (allocator );
100+ transferPair .splitAndTransfer (0 , 0 );
101+ assertEquals (0 , transferPair .getTo ().getValueCount ());
102+ // BaseFixedWidthVector
103+ IntVector intVector = new IntVector ("" , allocator );
104+ transferPair = intVector .getTransferPair (allocator );
105+ transferPair .splitAndTransfer (0 , 0 );
106+ assertEquals (0 , transferPair .getTo ().getValueCount ());
107+ // BaseVariableWidthVector
108+ VarCharVector varCharVector = new VarCharVector ("" , allocator );
109+ transferPair = varCharVector .getTransferPair (allocator );
110+ transferPair .splitAndTransfer (0 , 0 );
111+ assertEquals (0 , transferPair .getTo ().getValueCount ());
112+ // BaseLargeVariableWidthVector
113+ LargeVarCharVector largeVarCharVector = new LargeVarCharVector ("" , allocator );
114+ transferPair = largeVarCharVector .getTransferPair (allocator );
115+ transferPair .splitAndTransfer (0 , 0 );
116+ assertEquals (0 , transferPair .getTo ().getValueCount ());
117+ // StructVector
118+ StructVector structVector = StructVector .empty ("" , allocator );
119+ transferPair = structVector .getTransferPair (allocator );
120+ transferPair .splitAndTransfer (0 , 0 );
121+ assertEquals (0 , transferPair .getTo ().getValueCount ());
122+ // FixedSizeListVector
123+ FixedSizeListVector fixedSizeListVector = FixedSizeListVector .empty ("" , 0 , allocator );
124+ transferPair = fixedSizeListVector .getTransferPair (allocator );
125+ transferPair .splitAndTransfer (0 , 0 );
126+ assertEquals (0 , transferPair .getTo ().getValueCount ());
127+ // FixedSizeBinaryVector
128+ FixedSizeBinaryVector fixedSizeBinaryVector = new FixedSizeBinaryVector ("" , allocator , 4 );
129+ transferPair = fixedSizeBinaryVector .getTransferPair (allocator );
130+ transferPair .splitAndTransfer (0 , 0 );
131+ assertEquals (0 , transferPair .getTo ().getValueCount ());
132+ // UnionVector
133+ UnionVector unionVector = UnionVector .empty ("" , allocator );
134+ transferPair = unionVector .getTransferPair (allocator );
135+ transferPair .splitAndTransfer (0 , 0 );
136+ assertEquals (0 , transferPair .getTo ().getValueCount ());
137+ // DenseUnionVector
138+ DenseUnionVector duv = DenseUnionVector .empty ("" , allocator );
139+ transferPair = duv .getTransferPair (allocator );
140+ transferPair .splitAndTransfer (0 , 0 );
141+ assertEquals (0 , transferPair .getTo ().getValueCount ());
142+
143+ // non empty from vector
144+
145+ // BaseFixedWidthVector
146+ IntVector fromIntVector = new IntVector ("" , allocator );
147+ fromIntVector .allocateNew (100 );
148+ populateIntVector (fromIntVector , 100 );
149+ transferPair = fromIntVector .getTransferPair (allocator );
150+ IntVector toIntVector = (IntVector ) transferPair .getTo ();
151+ transferPair .splitAndTransfer (0 , 0 );
152+ assertEquals (0 , toIntVector .getValueCount ());
153+
154+ transferPair .splitAndTransfer (50 , 0 );
155+ assertEquals (0 , toIntVector .getValueCount ());
156+
157+ transferPair .splitAndTransfer (100 , 0 );
158+ assertEquals (0 , toIntVector .getValueCount ());
159+ fromIntVector .clear ();
160+ toIntVector .clear ();
161+
162+ // DenseUnionVector
163+ DenseUnionVector fromDuv = DenseUnionVector .empty ("" , allocator );
164+ populateDenseUnionVector (fromDuv , 100 );
165+ transferPair = fromDuv .getTransferPair (allocator );
166+ DenseUnionVector toDUV = (DenseUnionVector ) transferPair .getTo ();
167+ transferPair .splitAndTransfer (0 , 0 );
168+ assertEquals (0 , toDUV .getValueCount ());
169+
170+ transferPair .splitAndTransfer (50 , 0 );
171+ assertEquals (0 , toDUV .getValueCount ());
172+
173+ transferPair .splitAndTransfer (100 , 0 );
174+ assertEquals (0 , toDUV .getValueCount ());
175+ fromDuv .clear ();
176+ toDUV .clear ();
177+ }
178+
69179 @ Test /* VarCharVector */
70180 public void test () throws Exception {
71181 try (final VarCharVector varCharVector = new VarCharVector ("myvector" , allocator )) {
72182 varCharVector .allocateNew (10000 , 1000 );
73183
74184 final int valueCount = 500 ;
75185 final String [] compareArray = new String [valueCount ];
76-
186+
77187 populateVarcharVector (varCharVector , valueCount , compareArray );
78-
188+
79189 final TransferPair tp = varCharVector .getTransferPair (allocator );
80190 final VarCharVector newVarCharVector = (VarCharVector ) tp .getTo ();
81191 final int [][] startLengths = {{0 , 201 }, {201 , 0 }, {201 , 200 }, {401 , 99 }};
82-
192+
83193 for (final int [] startLength : startLengths ) {
84194 final int start = startLength [0 ];
85195 final int length = startLength [1 ];
@@ -429,5 +539,4 @@ public void testMapVectorZeroStartIndexAndLength() {
429539 newMapVector .clear ();
430540 }
431541 }
432-
433542}
0 commit comments