]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | package org.apache.arrow.vector.validate; | |
19 | ||
20 | import static org.apache.arrow.vector.testing.ValueVectorDataPopulator.setVector; | |
21 | import static org.apache.arrow.vector.util.ValueVectorUtility.validateFull; | |
22 | import static org.junit.Assert.assertTrue; | |
23 | import static org.junit.jupiter.api.Assertions.assertEquals; | |
24 | import static org.junit.jupiter.api.Assertions.assertThrows; | |
25 | ||
26 | import java.util.Arrays; | |
27 | ||
28 | import org.apache.arrow.memory.ArrowBuf; | |
29 | import org.apache.arrow.memory.BufferAllocator; | |
30 | import org.apache.arrow.memory.RootAllocator; | |
31 | import org.apache.arrow.vector.Float4Vector; | |
32 | import org.apache.arrow.vector.IntVector; | |
33 | import org.apache.arrow.vector.LargeVarCharVector; | |
34 | import org.apache.arrow.vector.ValueVector; | |
35 | import org.apache.arrow.vector.VarCharVector; | |
36 | import org.apache.arrow.vector.complex.DenseUnionVector; | |
37 | import org.apache.arrow.vector.complex.LargeListVector; | |
38 | import org.apache.arrow.vector.complex.ListVector; | |
39 | import org.apache.arrow.vector.complex.StructVector; | |
40 | import org.apache.arrow.vector.complex.UnionVector; | |
41 | import org.apache.arrow.vector.holders.NullableFloat4Holder; | |
42 | import org.apache.arrow.vector.holders.NullableFloat8Holder; | |
43 | import org.apache.arrow.vector.testing.ValueVectorDataPopulator; | |
44 | import org.apache.arrow.vector.types.Types; | |
45 | import org.apache.arrow.vector.types.pojo.ArrowType; | |
46 | import org.apache.arrow.vector.types.pojo.Field; | |
47 | import org.apache.arrow.vector.types.pojo.FieldType; | |
48 | import org.junit.After; | |
49 | import org.junit.Before; | |
50 | import org.junit.Test; | |
51 | ||
52 | public class TestValidateVectorFull { | |
53 | ||
54 | private BufferAllocator allocator; | |
55 | ||
56 | @Before | |
57 | public void init() { | |
58 | allocator = new RootAllocator(Long.MAX_VALUE); | |
59 | } | |
60 | ||
61 | @After | |
62 | public void terminate() throws Exception { | |
63 | allocator.close(); | |
64 | } | |
65 | ||
66 | @Test | |
67 | public void testBaseVariableWidthVector() { | |
68 | try (final VarCharVector vector = new VarCharVector("v", allocator)) { | |
69 | validateFull(vector); | |
70 | setVector(vector, "aaa", "bbb", "ccc"); | |
71 | validateFull(vector); | |
72 | ||
73 | ArrowBuf offsetBuf = vector.getOffsetBuffer(); | |
74 | offsetBuf.setInt(0, 100); | |
75 | offsetBuf.setInt(4, 50); | |
76 | ||
77 | ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, | |
78 | () -> validateFull(vector)); | |
79 | assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); | |
80 | } | |
81 | } | |
82 | ||
83 | @Test | |
84 | public void testBaseLargeVariableWidthVector() { | |
85 | try (final LargeVarCharVector vector = new LargeVarCharVector("v", allocator)) { | |
86 | validateFull(vector); | |
87 | setVector(vector, "aaa", "bbb", null, "ccc"); | |
88 | validateFull(vector); | |
89 | ||
90 | ArrowBuf offsetBuf = vector.getOffsetBuffer(); | |
91 | offsetBuf.setLong(0, 100); | |
92 | offsetBuf.setLong(8, 50); | |
93 | ||
94 | ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, | |
95 | () -> validateFull(vector)); | |
96 | assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the large offset buffer are decreasing")); | |
97 | } | |
98 | } | |
99 | ||
100 | @Test | |
101 | public void testListVector() { | |
102 | try (final ListVector vector = ListVector.empty("v", allocator)) { | |
103 | validateFull(vector); | |
104 | setVector(vector, Arrays.asList(1, 2, 3), Arrays.asList(4, 5), Arrays.asList(6, 7, 8, 9)); | |
105 | validateFull(vector); | |
106 | ||
107 | ArrowBuf offsetBuf = vector.getOffsetBuffer(); | |
108 | offsetBuf.setInt(0, 100); | |
109 | offsetBuf.setInt(8, 50); | |
110 | ||
111 | ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, | |
112 | () -> validateFull(vector)); | |
113 | assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); | |
114 | } | |
115 | } | |
116 | ||
117 | @Test | |
118 | public void testLargeListVector() { | |
119 | try (final LargeListVector vector = LargeListVector.empty("v", allocator)) { | |
120 | validateFull(vector); | |
121 | setVector(vector, Arrays.asList(1, 2, 3), Arrays.asList(4, 5), Arrays.asList(6, 7, 8, 9)); | |
122 | validateFull(vector); | |
123 | ||
124 | ArrowBuf offsetBuf = vector.getOffsetBuffer(); | |
125 | offsetBuf.setLong(0, 100); | |
126 | offsetBuf.setLong(16, 50); | |
127 | ||
128 | ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, | |
129 | () -> validateFull(vector)); | |
130 | assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the large offset buffer are decreasing")); | |
131 | } | |
132 | } | |
133 | ||
134 | @Test | |
135 | public void testStructVectorRangeEquals() { | |
136 | try (final StructVector vector = StructVector.empty("struct", allocator)) { | |
137 | IntVector intVector = | |
138 | vector.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); | |
139 | VarCharVector strVector = | |
140 | vector.addOrGet("f1", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); | |
141 | ||
142 | validateFull(vector); | |
143 | validateFull(intVector); | |
144 | validateFull(strVector); | |
145 | ||
146 | ValueVectorDataPopulator.setVector(intVector, 1, 2, 3, 4, 5); | |
147 | ValueVectorDataPopulator.setVector(strVector, "a", "b", "c", "d", "e"); | |
148 | vector.setValueCount(5); | |
149 | ||
150 | validateFull(vector); | |
151 | validateFull(intVector); | |
152 | validateFull(strVector); | |
153 | ||
154 | ArrowBuf offsetBuf = strVector.getOffsetBuffer(); | |
155 | offsetBuf.setInt(0, 100); | |
156 | offsetBuf.setInt(8, 50); | |
157 | ||
158 | ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, | |
159 | () -> validateFull(strVector)); | |
160 | assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); | |
161 | ||
162 | e = assertThrows(ValidateUtil.ValidateException.class, | |
163 | () -> validateFull(vector)); | |
164 | assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); | |
165 | } | |
166 | } | |
167 | ||
168 | @Test | |
169 | public void testUnionVector() { | |
170 | try (final UnionVector vector = UnionVector.empty("union", allocator)) { | |
171 | validateFull(vector); | |
172 | ||
173 | final NullableFloat4Holder float4Holder = new NullableFloat4Holder(); | |
174 | float4Holder.value = 1.01f; | |
175 | float4Holder.isSet = 1; | |
176 | ||
177 | final NullableFloat8Holder float8Holder = new NullableFloat8Holder(); | |
178 | float8Holder.value = 2.02f; | |
179 | float8Holder.isSet = 1; | |
180 | ||
181 | vector.setType(0, Types.MinorType.FLOAT4); | |
182 | vector.setSafe(0, float4Holder); | |
183 | vector.setType(1, Types.MinorType.FLOAT8); | |
184 | vector.setSafe(1, float8Holder); | |
185 | vector.setValueCount(2); | |
186 | ||
187 | validateFull(vector); | |
188 | ||
189 | // negative type id | |
190 | vector.getTypeBuffer().setByte(0, -1); | |
191 | ||
192 | ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, | |
193 | () -> validateFull(vector)); | |
194 | assertTrue(e.getMessage().contains("The type id at position 0 is negative")); | |
195 | } | |
196 | } | |
197 | ||
198 | @Test | |
199 | public void testDenseUnionVector() { | |
200 | try (final DenseUnionVector vector = DenseUnionVector.empty("union", allocator)) { | |
201 | validateFull(vector); | |
202 | ||
203 | final NullableFloat4Holder float4Holder = new NullableFloat4Holder(); | |
204 | float4Holder.value = 1.01f; | |
205 | float4Holder.isSet = 1; | |
206 | ||
207 | final NullableFloat8Holder float8Holder = new NullableFloat8Holder(); | |
208 | float8Holder.value = 2.02f; | |
209 | float8Holder.isSet = 1; | |
210 | ||
211 | byte float4TypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); | |
212 | byte float8TypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT8.getType())); | |
213 | ||
214 | vector.setTypeId(0, float4TypeId); | |
215 | vector.setSafe(0, float4Holder); | |
216 | vector.setTypeId(1, float8TypeId); | |
217 | vector.setSafe(1, float8Holder); | |
218 | vector.setValueCount(2); | |
219 | ||
220 | validateFull(vector); | |
221 | ||
222 | ValueVector subVector = vector.getVectorByType(float4TypeId); | |
223 | assertTrue(subVector instanceof Float4Vector); | |
224 | assertEquals(1, subVector.getValueCount()); | |
225 | ||
226 | // shrink sub-vector | |
227 | subVector.setValueCount(0); | |
228 | ||
229 | ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, | |
230 | () -> validateFull(vector)); | |
231 | assertTrue(e.getMessage().contains("Dense union vector offset exceeds sub-vector boundary")); | |
232 | } | |
233 | } | |
234 | } |