]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorSchemaRootAppender.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / vector / src / test / java / org / apache / arrow / vector / util / TestVectorSchemaRootAppender.java
CommitLineData
1d09f67e
TL
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.arrow.vector.util;
19
20import static junit.framework.TestCase.assertEquals;
21import static org.apache.arrow.vector.util.TestVectorAppender.assertVectorsEqual;
22import static org.junit.jupiter.api.Assertions.assertThrows;
23
24import org.apache.arrow.memory.BufferAllocator;
25import org.apache.arrow.memory.RootAllocator;
26import org.apache.arrow.vector.BigIntVector;
27import org.apache.arrow.vector.IntVector;
28import org.apache.arrow.vector.VarCharVector;
29import org.apache.arrow.vector.VectorSchemaRoot;
30import org.apache.arrow.vector.testing.ValueVectorDataPopulator;
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34
35/**
36 * Test cases for {@link VectorSchemaRootAppender}.
37 */
38public class TestVectorSchemaRootAppender {
39
40 private BufferAllocator allocator;
41
42 @Before
43 public void prepare() {
44 allocator = new RootAllocator(1024 * 1024);
45 }
46
47 @After
48 public void shutdown() {
49 allocator.close();
50 }
51
52 @Test
53 public void testVectorScehmaRootAppend() {
54 final int length1 = 5;
55 final int length2 = 3;
56 final int length3 = 2;
57
58 try (IntVector targetChild1 = new IntVector("t1", allocator);
59 VarCharVector targetChild2 = new VarCharVector("t2", allocator);
60 BigIntVector targetChild3 = new BigIntVector("t3", allocator);
61
62 IntVector deltaChildOne1 = new IntVector("do1", allocator);
63 VarCharVector deltaChildOne2 = new VarCharVector("do2", allocator);
64 BigIntVector deltaChildOne3 = new BigIntVector("do3", allocator);
65
66 IntVector deltaChildTwo1 = new IntVector("dt1", allocator);
67 VarCharVector deltaChildTwo2 = new VarCharVector("dt2", allocator);
68 BigIntVector deltaChildTwo3 = new BigIntVector("dt3", allocator)) {
69
70 ValueVectorDataPopulator.setVector(targetChild1, 0, 1, null, 3, 4);
71 ValueVectorDataPopulator.setVector(targetChild2, "zero", "one", null, "three", "four");
72 ValueVectorDataPopulator.setVector(targetChild3, 0L, 10L, null, 30L, 40L);
73 VectorSchemaRoot root1 = VectorSchemaRoot.of(targetChild1, targetChild2, targetChild3);
74 root1.setRowCount(length1);
75
76 ValueVectorDataPopulator.setVector(deltaChildOne1, 5, 6, 7);
77 ValueVectorDataPopulator.setVector(deltaChildOne2, "five", "six", "seven");
78 ValueVectorDataPopulator.setVector(deltaChildOne3, 50L, 60L, 70L);
79 VectorSchemaRoot root2 = VectorSchemaRoot.of(deltaChildOne1, deltaChildOne2, deltaChildOne3);
80 root2.setRowCount(length2);
81
82 ValueVectorDataPopulator.setVector(deltaChildTwo1, null, 9);
83 ValueVectorDataPopulator.setVector(deltaChildTwo2, null, "nine");
84 ValueVectorDataPopulator.setVector(deltaChildTwo3, null, 90L);
85 VectorSchemaRoot root3 = VectorSchemaRoot.of(deltaChildTwo1, deltaChildTwo2, deltaChildTwo3);
86 root3.setRowCount(length3);
87
88 VectorSchemaRootAppender.append(root1, root2, root3);
89 assertEquals(length1 + length2 + length3, root1.getRowCount());
90 assertEquals(3, root1.getFieldVectors().size());
91
92 try (IntVector expected1 = new IntVector("", allocator);
93 VarCharVector expected2 = new VarCharVector("", allocator);
94 BigIntVector expected3 = new BigIntVector("", allocator)) {
95
96 ValueVectorDataPopulator.setVector(expected1, 0, 1, null, 3, 4, 5, 6, 7, null, 9);
97 ValueVectorDataPopulator.setVector(
98 expected2, "zero", "one", null, "three", "four", "five", "six", "seven", null, "nine");
99 ValueVectorDataPopulator.setVector(expected3, 0L, 10L, null, 30L, 40L, 50L, 60L, 70L, null, 90L);
100
101 assertVectorsEqual(expected1, root1.getVector(0));
102 assertVectorsEqual(expected2, root1.getVector(1));
103 assertVectorsEqual(expected3, root1.getVector(2));
104 }
105 }
106 }
107
108 @Test
109 public void testRootWithDifferentChildCounts() {
110 try (IntVector targetChild1 = new IntVector("t1", allocator);
111 VarCharVector targetChild2 = new VarCharVector("t2", allocator);
112 BigIntVector targetChild3 = new BigIntVector("t3", allocator);
113
114 IntVector deltaChild1 = new IntVector("d1", allocator);
115 VarCharVector deltaChild2 = new VarCharVector("d2", allocator)) {
116
117 ValueVectorDataPopulator.setVector(targetChild1, 0, 1, null, 3, 4);
118 ValueVectorDataPopulator.setVector(targetChild2, "zero", "one", null, "three", "four");
119 ValueVectorDataPopulator.setVector(targetChild3, 0L, 10L, null, 30L, 40L);
120 VectorSchemaRoot root1 = VectorSchemaRoot.of(targetChild1, targetChild2, targetChild3);
121 root1.setRowCount(5);
122
123 ValueVectorDataPopulator.setVector(deltaChild1, 5, 6, 7);
124 ValueVectorDataPopulator.setVector(deltaChild2, "five", "six", "seven");
125 VectorSchemaRoot root2 = VectorSchemaRoot.of(deltaChild1, deltaChild2);
126 root2.setRowCount(3);
127
128 IllegalArgumentException exp = assertThrows(IllegalArgumentException.class,
129 () -> VectorSchemaRootAppender.append(root1, root2));
130
131 assertEquals("Vector schema roots have different numbers of child vectors.", exp.getMessage());
132 }
133 }
134
135 @Test
136 public void testRootWithDifferentChildTypes() {
137 try (IntVector targetChild1 = new IntVector("t1", allocator);
138 VarCharVector targetChild2 = new VarCharVector("t2", allocator);
139
140 IntVector deltaChild1 = new IntVector("d1", allocator);
141 VarCharVector deltaChild2 = new VarCharVector("d2", allocator)) {
142
143 ValueVectorDataPopulator.setVector(targetChild1, 0, 1, null, 3, 4);
144 ValueVectorDataPopulator.setVector(targetChild2, "zero", "one", null, "three", "four");
145 VectorSchemaRoot root1 = VectorSchemaRoot.of(targetChild1, targetChild2);
146 root1.setRowCount(5);
147
148 ValueVectorDataPopulator.setVector(deltaChild1, 5, 6, 7);
149 ValueVectorDataPopulator.setVector(deltaChild2, "five", "six", "seven");
150
151 // note that the child vectors are in reverse order
152 VectorSchemaRoot root2 = VectorSchemaRoot.of(deltaChild2, deltaChild1);
153 root2.setRowCount(3);
154
155 IllegalArgumentException exp = assertThrows(IllegalArgumentException.class,
156 () -> VectorSchemaRootAppender.append(root1, root2));
157
158 assertEquals("Vector schema roots have different schemas.", exp.getMessage());
159 }
160 }
161}