]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowDataTypesTest.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / adapter / jdbc / src / test / java / org / apache / arrow / adapter / jdbc / h2 / JdbcToArrowDataTypesTest.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.adapter.jdbc.h2;
19
20import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertBigIntVectorValues;
21import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertBitVectorValues;
22import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertBooleanVectorValues;
23import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertDateVectorValues;
24import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertDecimalVectorValues;
25import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertFloat4VectorValues;
26import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertFloat8VectorValues;
27import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertIntVectorValues;
28import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertNullVectorValues;
29import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertSmallIntVectorValues;
30import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertTimeStampVectorValues;
31import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertTimeVectorValues;
32import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertTinyIntVectorValues;
33import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertVarBinaryVectorValues;
34import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertVarcharVectorValues;
35
36import java.io.IOException;
37import java.sql.ResultSetMetaData;
38import java.sql.SQLException;
39import java.util.Arrays;
40import java.util.Calendar;
41import java.util.Collection;
42
43import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest;
44import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
45import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder;
46import org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper;
47import org.apache.arrow.adapter.jdbc.JdbcToArrowUtils;
48import org.apache.arrow.adapter.jdbc.Table;
49import org.apache.arrow.memory.RootAllocator;
50import org.apache.arrow.vector.BigIntVector;
51import org.apache.arrow.vector.BitVector;
52import org.apache.arrow.vector.DateDayVector;
53import org.apache.arrow.vector.DecimalVector;
54import org.apache.arrow.vector.Float4Vector;
55import org.apache.arrow.vector.Float8Vector;
56import org.apache.arrow.vector.IntVector;
57import org.apache.arrow.vector.NullVector;
58import org.apache.arrow.vector.SmallIntVector;
59import org.apache.arrow.vector.TimeMilliVector;
60import org.apache.arrow.vector.TimeStampVector;
61import org.apache.arrow.vector.TinyIntVector;
62import org.apache.arrow.vector.VarBinaryVector;
63import org.apache.arrow.vector.VarCharVector;
64import org.apache.arrow.vector.VectorSchemaRoot;
65import org.apache.arrow.vector.types.pojo.Schema;
66import org.junit.Test;
67import org.junit.runner.RunWith;
68import org.junit.runners.Parameterized;
69import org.junit.runners.Parameterized.Parameters;
70
71/**
72 * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with various data types
73 * for H2 database using multiple test data files.
74 */
75@RunWith(Parameterized.class)
76public class JdbcToArrowDataTypesTest extends AbstractJdbcToArrowTest {
77
78 private static final String BIGINT = "big_int";
79 private static final String BINARY = "binary";
80 private static final String BIT = "bit";
81 private static final String BLOB = "blob";
82 private static final String BOOL = "bool";
83 private static final String CHAR = "char";
84 private static final String CLOB = "clob";
85 private static final String DATE = "date";
86 private static final String DECIMAL = "decimal";
87 private static final String DOUBLE = "double";
88 private static final String INT = "int";
89 private static final String REAL = "real";
90 private static final String SMALLINT = "small_int";
91 private static final String TIME = "time";
92 private static final String TIMESTAMP = "timestamp";
93 private static final String TINYINT = "tiny_int";
94 private static final String VARCHAR = "varchar";
95 private static final String NULL = "null";
96
97 private static final String[] testFiles = {
98 "h2/test1_bigint_h2.yml",
99 "h2/test1_binary_h2.yml",
100 "h2/test1_bit_h2.yml",
101 "h2/test1_blob_h2.yml",
102 "h2/test1_bool_h2.yml",
103 "h2/test1_char_h2.yml",
104 "h2/test1_clob_h2.yml",
105 "h2/test1_date_h2.yml",
106 "h2/test1_decimal_h2.yml",
107 "h2/test1_double_h2.yml",
108 "h2/test1_int_h2.yml",
109 "h2/test1_real_h2.yml",
110 "h2/test1_smallint_h2.yml",
111 "h2/test1_time_h2.yml",
112 "h2/test1_timestamp_h2.yml",
113 "h2/test1_tinyint_h2.yml",
114 "h2/test1_varchar_h2.yml",
115 "h2/test1_null_h2.yml"
116 };
117
118 /**
119 * Constructor which populates the table object for each test iteration.
120 *
121 * @param table Table object
122 */
123 public JdbcToArrowDataTypesTest(Table table) {
124 this.table = table;
125 }
126
127 /**
128 * Get the test data as a collection of Table objects for each test iteration.
129 *
130 * @return Collection of Table objects
131 * @throws SQLException on error
132 * @throws ClassNotFoundException on error
133 * @throws IOException on error
134 */
135 @Parameters
136 public static Collection<Object[]> getTestData() throws SQLException, ClassNotFoundException, IOException {
137 return Arrays.asList(prepareTestData(testFiles, JdbcToArrowDataTypesTest.class));
138 }
139
140 /**
141 * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes.
142 */
143 @Test
144 public void testJdbcToArrowValues() throws SQLException, IOException {
145 testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE),
146 Calendar.getInstance()));
147 testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)));
148 testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()),
149 new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()));
150 testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery())));
151 testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()),
152 new RootAllocator(Integer.MAX_VALUE)));
153 testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()));
154 testDataSets(sqlToArrow(
155 conn.createStatement().executeQuery(table.getQuery()),
156 new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
157 testDataSets(sqlToArrow(
158 conn,
159 table.getQuery(),
160 new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
161 }
162
163 @Test
164 public void testJdbcSchemaMetadata() throws SQLException {
165 JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true).build();
166 ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
167 Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config);
168 JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema);
169 }
170
171 /**
172 * This method calls the assert methods for various DataSets.
173 *
174 * @param root VectorSchemaRoot for test
175 */
176 public void testDataSets(VectorSchemaRoot root) {
177 JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root);
178
179 switch (table.getType()) {
180 case BIGINT:
181 assertBigIntVectorValues((BigIntVector) root.getVector(table.getVector()), table.getValues().length,
182 table.getLongValues());
183 break;
184 case BINARY:
185 case BLOB:
186 assertVarBinaryVectorValues((VarBinaryVector) root.getVector(table.getVector()), table.getValues().length,
187 table.getBinaryValues());
188 break;
189 case BIT:
190 assertBitVectorValues((BitVector) root.getVector(table.getVector()), table.getValues().length,
191 table.getIntValues());
192 break;
193 case BOOL:
194 assertBooleanVectorValues((BitVector) root.getVector(table.getVector()), table.getValues().length,
195 table.getBoolValues());
196 break;
197 case CHAR:
198 case VARCHAR:
199 case CLOB:
200 assertVarcharVectorValues((VarCharVector) root.getVector(table.getVector()), table.getValues().length,
201 table.getCharValues());
202 break;
203 case DATE:
204 assertDateVectorValues((DateDayVector) root.getVector(table.getVector()), table.getValues().length,
205 table.getIntValues());
206 break;
207 case TIME:
208 assertTimeVectorValues((TimeMilliVector) root.getVector(table.getVector()), table.getValues().length,
209 table.getLongValues());
210 break;
211 case TIMESTAMP:
212 assertTimeStampVectorValues((TimeStampVector) root.getVector(table.getVector()), table.getValues().length,
213 table.getLongValues());
214 break;
215 case DECIMAL:
216 assertDecimalVectorValues((DecimalVector) root.getVector(table.getVector()), table.getValues().length,
217 table.getBigDecimalValues());
218 break;
219 case DOUBLE:
220 assertFloat8VectorValues((Float8Vector) root.getVector(table.getVector()), table.getValues().length,
221 table.getDoubleValues());
222 break;
223 case INT:
224 assertIntVectorValues((IntVector) root.getVector(table.getVector()), table.getValues().length,
225 table.getIntValues());
226 break;
227 case SMALLINT:
228 assertSmallIntVectorValues((SmallIntVector) root.getVector(table.getVector()), table.getValues().length,
229 table.getIntValues());
230 break;
231 case TINYINT:
232 assertTinyIntVectorValues((TinyIntVector) root.getVector(table.getVector()), table.getValues().length,
233 table.getIntValues());
234 break;
235 case REAL:
236 assertFloat4VectorValues((Float4Vector) root.getVector(table.getVector()), table.getValues().length,
237 table.getFloatValues());
238 break;
239 case NULL:
240 assertNullVectorValues((NullVector) root.getVector(table.getVector()), table.getRowCount());
241 break;
242 default:
243 // do nothing
244 break;
245 }
246 }
247}
248