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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.adapter
.jdbc
;
20 import static org
.junit
.Assert
.assertArrayEquals
;
21 import static org
.junit
.Assert
.assertEquals
;
22 import static org
.junit
.Assert
.assertNotNull
;
23 import static org
.junit
.Assert
.assertTrue
;
25 import java
.math
.BigDecimal
;
26 import java
.nio
.charset
.Charset
;
27 import java
.sql
.ResultSetMetaData
;
28 import java
.sql
.SQLException
;
29 import java
.util
.List
;
32 import org
.apache
.arrow
.vector
.BaseValueVector
;
33 import org
.apache
.arrow
.vector
.BigIntVector
;
34 import org
.apache
.arrow
.vector
.BitVector
;
35 import org
.apache
.arrow
.vector
.DateDayVector
;
36 import org
.apache
.arrow
.vector
.DecimalVector
;
37 import org
.apache
.arrow
.vector
.Float4Vector
;
38 import org
.apache
.arrow
.vector
.Float8Vector
;
39 import org
.apache
.arrow
.vector
.IntVector
;
40 import org
.apache
.arrow
.vector
.NullVector
;
41 import org
.apache
.arrow
.vector
.SmallIntVector
;
42 import org
.apache
.arrow
.vector
.TimeMilliVector
;
43 import org
.apache
.arrow
.vector
.TimeStampVector
;
44 import org
.apache
.arrow
.vector
.TinyIntVector
;
45 import org
.apache
.arrow
.vector
.VarBinaryVector
;
46 import org
.apache
.arrow
.vector
.VarCharVector
;
47 import org
.apache
.arrow
.vector
.VectorSchemaRoot
;
48 import org
.apache
.arrow
.vector
.types
.pojo
.Field
;
49 import org
.apache
.arrow
.vector
.types
.pojo
.Schema
;
52 * This is a Helper class which has functionalities to read and assert the values from the given FieldVector object.
54 public class JdbcToArrowTestHelper
{
56 public static void assertIntVectorValues(IntVector intVector
, int rowCount
, Integer
[] values
) {
57 assertEquals(rowCount
, intVector
.getValueCount());
59 for (int j
= 0; j
< intVector
.getValueCount(); j
++) {
60 if (values
[j
] == null) {
61 assertTrue(intVector
.isNull(j
));
63 assertEquals(values
[j
].intValue(), intVector
.get(j
));
68 public static void assertBooleanVectorValues(BitVector bitVector
, int rowCount
, Boolean
[] values
) {
69 assertEquals(rowCount
, bitVector
.getValueCount());
71 for (int j
= 0; j
< bitVector
.getValueCount(); j
++) {
72 if (values
[j
] == null) {
73 assertTrue(bitVector
.isNull(j
));
75 assertEquals(values
[j
].booleanValue(), bitVector
.get(j
) == 1);
80 public static void assertBitVectorValues(BitVector bitVector
, int rowCount
, Integer
[] values
) {
81 assertEquals(rowCount
, bitVector
.getValueCount());
83 for (int j
= 0; j
< bitVector
.getValueCount(); j
++) {
84 if (values
[j
] == null) {
85 assertTrue(bitVector
.isNull(j
));
87 assertEquals(values
[j
].intValue(), bitVector
.get(j
));
92 public static void assertTinyIntVectorValues(TinyIntVector tinyIntVector
, int rowCount
, Integer
[] values
) {
93 assertEquals(rowCount
, tinyIntVector
.getValueCount());
95 for (int j
= 0; j
< tinyIntVector
.getValueCount(); j
++) {
96 if (values
[j
] == null) {
97 assertTrue(tinyIntVector
.isNull(j
));
99 assertEquals(values
[j
].intValue(), tinyIntVector
.get(j
));
104 public static void assertSmallIntVectorValues(SmallIntVector smallIntVector
, int rowCount
, Integer
[] values
) {
105 assertEquals(rowCount
, smallIntVector
.getValueCount());
107 for (int j
= 0; j
< smallIntVector
.getValueCount(); j
++) {
108 if (values
[j
] == null) {
109 assertTrue(smallIntVector
.isNull(j
));
111 assertEquals(values
[j
].intValue(), smallIntVector
.get(j
));
116 public static void assertBigIntVectorValues(BigIntVector bigIntVector
, int rowCount
, Long
[] values
) {
117 assertEquals(rowCount
, bigIntVector
.getValueCount());
119 for (int j
= 0; j
< bigIntVector
.getValueCount(); j
++) {
120 if (values
[j
] == null) {
121 assertTrue(bigIntVector
.isNull(j
));
123 assertEquals(values
[j
].longValue(), bigIntVector
.get(j
));
128 public static void assertDecimalVectorValues(DecimalVector decimalVector
, int rowCount
, BigDecimal
[] values
) {
129 assertEquals(rowCount
, decimalVector
.getValueCount());
131 for (int j
= 0; j
< decimalVector
.getValueCount(); j
++) {
132 if (values
[j
] == null) {
133 assertTrue(decimalVector
.isNull(j
));
135 assertEquals(values
[j
].doubleValue(), decimalVector
.getObject(j
).doubleValue(), 0);
140 public static void assertFloat8VectorValues(Float8Vector float8Vector
, int rowCount
, Double
[] values
) {
141 assertEquals(rowCount
, float8Vector
.getValueCount());
143 for (int j
= 0; j
< float8Vector
.getValueCount(); j
++) {
144 if (values
[j
] == null) {
145 assertTrue(float8Vector
.isNull(j
));
147 assertEquals(values
[j
], float8Vector
.get(j
), 0.01);
152 public static void assertFloat4VectorValues(Float4Vector float4Vector
, int rowCount
, Float
[] values
) {
153 assertEquals(rowCount
, float4Vector
.getValueCount());
155 for (int j
= 0; j
< float4Vector
.getValueCount(); j
++) {
156 if (values
[j
] == null) {
157 assertTrue(float4Vector
.isNull(j
));
159 assertEquals(values
[j
], float4Vector
.get(j
), 0.01);
164 public static void assertTimeVectorValues(TimeMilliVector timeMilliVector
, int rowCount
, Long
[] values
) {
165 assertEquals(rowCount
, timeMilliVector
.getValueCount());
167 for (int j
= 0; j
< timeMilliVector
.getValueCount(); j
++) {
168 if (values
[j
] == null) {
169 assertTrue(timeMilliVector
.isNull(j
));
171 assertEquals(values
[j
].longValue(), timeMilliVector
.get(j
));
176 public static void assertDateVectorValues(DateDayVector dateDayVector
, int rowCount
, Integer
[] values
) {
177 assertEquals(rowCount
, dateDayVector
.getValueCount());
179 for (int j
= 0; j
< dateDayVector
.getValueCount(); j
++) {
180 if (values
[j
] == null) {
181 assertTrue(dateDayVector
.isNull(j
));
183 assertEquals(values
[j
].longValue(), dateDayVector
.get(j
));
188 public static void assertTimeStampVectorValues(TimeStampVector timeStampVector
, int rowCount
, Long
[] values
) {
189 assertEquals(rowCount
, timeStampVector
.getValueCount());
191 for (int j
= 0; j
< timeStampVector
.getValueCount(); j
++) {
192 if (values
[j
] == null) {
193 assertTrue(timeStampVector
.isNull(j
));
195 assertEquals(values
[j
].longValue(), timeStampVector
.get(j
));
200 public static void assertVarBinaryVectorValues(VarBinaryVector varBinaryVector
, int rowCount
, byte[][] values
) {
201 assertEquals(rowCount
, varBinaryVector
.getValueCount());
203 for (int j
= 0; j
< varBinaryVector
.getValueCount(); j
++) {
204 if (values
[j
] == null) {
205 assertTrue(varBinaryVector
.isNull(j
));
207 assertArrayEquals(values
[j
], varBinaryVector
.get(j
));
212 public static void assertVarcharVectorValues(VarCharVector varCharVector
, int rowCount
, byte[][] values
) {
213 assertEquals(rowCount
, varCharVector
.getValueCount());
215 for (int j
= 0; j
< varCharVector
.getValueCount(); j
++) {
216 if (values
[j
] == null) {
217 assertTrue(varCharVector
.isNull(j
));
219 assertArrayEquals(values
[j
], varCharVector
.get(j
));
224 public static void assertNullVectorValues(NullVector vector
, int rowCount
) {
225 assertEquals(rowCount
, vector
.getValueCount());
228 public static void assertNullValues(BaseValueVector vector
, int rowCount
) {
229 assertEquals(rowCount
, vector
.getValueCount());
231 for (int j
= 0; j
< vector
.getValueCount(); j
++) {
232 assertTrue(vector
.isNull(j
));
236 public static void assertFieldMetadataIsEmpty(VectorSchemaRoot schema
) {
237 assertNotNull(schema
);
238 assertNotNull(schema
.getSchema());
239 assertNotNull(schema
.getSchema().getFields());
241 for (Field field
: schema
.getSchema().getFields()) {
242 assertNotNull(field
.getMetadata());
243 assertEquals(0, field
.getMetadata().size());
247 public static void assertFieldMetadataMatchesResultSetMetadata(ResultSetMetaData rsmd
, Schema schema
)
248 throws SQLException
{
249 assertNotNull(schema
);
250 assertNotNull(schema
.getFields());
253 List
<Field
> fields
= schema
.getFields();
255 assertEquals(rsmd
.getColumnCount(), fields
.size());
257 // Vector columns are created in the same order as ResultSet columns.
258 for (int i
= 1; i
<= rsmd
.getColumnCount(); ++i
) {
259 Map
<String
, String
> metadata
= fields
.get(i
- 1).getMetadata();
261 assertNotNull(metadata
);
262 assertEquals(4, metadata
.size());
264 assertEquals(rsmd
.getCatalogName(i
), metadata
.get(Constants
.SQL_CATALOG_NAME_KEY
));
265 assertEquals(rsmd
.getTableName(i
), metadata
.get(Constants
.SQL_TABLE_NAME_KEY
));
266 assertEquals(rsmd
.getColumnLabel(i
), metadata
.get(Constants
.SQL_COLUMN_NAME_KEY
));
267 assertEquals(rsmd
.getColumnTypeName(i
), metadata
.get(Constants
.SQL_TYPE_KEY
));
271 public static byte[] hexStringToByteArray(String s
) {
272 int len
= s
.length();
273 byte[] data
= new byte[len
/ 2];
274 for (int i
= 0; i
< len
; i
+= 2) {
275 data
[i
/ 2] = (byte) ((Character
.digit(s
.charAt(i
), 16) << 4) +
276 Character
.digit(s
.charAt(i
+ 1), 16));
281 public static Integer
[] getIntValues(String
[] values
, String dataType
) {
282 String
[] dataArr
= getValues(values
, dataType
);
283 Integer
[] valueArr
= new Integer
[dataArr
.length
];
285 for (String data
: dataArr
) {
286 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : Integer
.parseInt(data
);
291 public static Boolean
[] getBooleanValues(String
[] values
, String dataType
) {
292 String
[] dataArr
= getValues(values
, dataType
);
293 Boolean
[] valueArr
= new Boolean
[dataArr
.length
];
295 for (String data
: dataArr
) {
296 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : data
.trim().equals("1");
301 public static BigDecimal
[] getDecimalValues(String
[] values
, String dataType
) {
302 String
[] dataArr
= getValues(values
, dataType
);
303 BigDecimal
[] valueArr
= new BigDecimal
[dataArr
.length
];
305 for (String data
: dataArr
) {
306 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : new BigDecimal(data
);
311 public static Double
[] getDoubleValues(String
[] values
, String dataType
) {
312 String
[] dataArr
= getValues(values
, dataType
);
313 Double
[] valueArr
= new Double
[dataArr
.length
];
315 for (String data
: dataArr
) {
316 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : Double
.parseDouble(data
);
321 public static Float
[] getFloatValues(String
[] values
, String dataType
) {
322 String
[] dataArr
= getValues(values
, dataType
);
323 Float
[] valueArr
= new Float
[dataArr
.length
];
325 for (String data
: dataArr
) {
326 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : Float
.parseFloat(data
);
331 public static Long
[] getLongValues(String
[] values
, String dataType
) {
332 String
[] dataArr
= getValues(values
, dataType
);
333 Long
[] valueArr
= new Long
[dataArr
.length
];
335 for (String data
: dataArr
) {
336 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : Long
.parseLong(data
);
341 public static byte[][] getCharArray(String
[] values
, String dataType
) {
342 String
[] dataArr
= getValues(values
, dataType
);
343 byte[][] valueArr
= new byte[dataArr
.length
][];
345 for (String data
: dataArr
) {
346 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : data
.trim().getBytes();
351 public static byte[][] getCharArrayWithCharSet(String
[] values
, String dataType
, Charset charSet
) {
352 String
[] dataArr
= getValues(values
, dataType
);
353 byte[][] valueArr
= new byte[dataArr
.length
][];
355 for (String data
: dataArr
) {
356 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : data
.trim().getBytes(charSet
);
361 public static byte[][] getBinaryValues(String
[] values
, String dataType
) {
362 String
[] dataArr
= getValues(values
, dataType
);
363 byte[][] valueArr
= new byte[dataArr
.length
][];
365 for (String data
: dataArr
) {
366 valueArr
[i
++] = "null".equals(data
.trim()) ?
null : hexStringToByteArray(data
.trim());
371 public static String
[] getValues(String
[] values
, String dataType
) {
373 for (String val
: values
) {
374 if (val
.startsWith(dataType
)) {
375 value
= val
.split("=")[1];
379 return value
.split(",");