3 package com.cloudera.impala.service;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertFalse;
7 import static org.junit.Assert.assertNotNull;
8 import static org.junit.Assert.assertNull;
9 import static org.junit.Assert.assertTrue;
11 import java.sql.Connection;
12 import java.sql.ResultSet;
13 import java.sql.ResultSetMetaData;
14 import java.sql.SQLException;
15 import java.sql.Statement;
16 import java.sql.Types;
17 import java.util.HashMap;
20 import org.junit.AfterClass;
21 import org.junit.BeforeClass;
34 private static Connection
con_;
37 public static void setUp() throws Exception {
38 ImpalaJdbcClient client = ImpalaJdbcClient.createClientUsingHiveJdbcDriver();
40 con_ = client.getConnection();
41 assertNotNull(
"Connection is null", con_);
42 assertFalse(
"Connection should not be closed", con_.isClosed());
43 Statement stmt = con_.createStatement();
44 assertNotNull(
"Statement is null", stmt);
48 public static void cleanUp() throws Exception {
50 assertTrue(
"Connection should be closed",
con_.isClosed());
52 Exception expectedException = null;
54 con_.createStatement();
55 }
catch (Exception e) {
56 expectedException = e;
59 assertNotNull(
"createStatement() on closed connection should throw exception",
66 Map<String, String> tests =
new HashMap<String, String>();
67 tests.put(
"alltypes",
"alltypes");
68 tests.put(
"%all_ypes",
"alltypes");
70 String[][] tblTypes = {null, {
"TABLE"}};
72 for (String tblNamePattern: tests.keySet()) {
73 for (String[] tblType: tblTypes) {
74 ResultSet rs = con_.getMetaData().getTables(
"",
"functional",
75 tblNamePattern, tblType);
76 assertTrue(rs.next());
79 String resultTableName = rs.getString(
"TABLE_NAME");
80 assertEquals(rs.getString(3), resultTableName);
82 assertEquals(
"Table mismatch", tests.get(tblNamePattern), resultTableName);
83 String tableType = rs.getString(
"TABLE_TYPE");
84 assertEquals(
"table", tableType.toLowerCase());
85 assertFalse(rs.next());
90 for (String[] tblType: tblTypes) {
91 ResultSet rs = con_.getMetaData().getTables(null, null, null, tblType);
93 assertTrue(rs.next());
96 rs = con_.getMetaData().getTables(null, null, null, tblType);
97 assertTrue(rs.next());
105 ResultSet rs = con_.getMetaData().getCatalogs();
106 ResultSetMetaData resMeta = rs.getMetaData();
107 assertEquals(1, resMeta.getColumnCount());
108 assertEquals(
"TABLE_CAT", resMeta.getColumnName(1));
109 assertFalse(rs.next());
115 ResultSet rs = con_.getMetaData().getSchemas(
"",
"d_f%");
116 ResultSetMetaData resMeta = rs.getMetaData();
117 assertEquals(2, resMeta.getColumnCount());
118 assertEquals(
"TABLE_SCHEM", resMeta.getColumnName(1));
119 assertEquals(
"TABLE_CATALOG", resMeta.getColumnName(2));
120 assertTrue(rs.next());
121 assertEquals(rs.getString(1).toLowerCase(),
"default");
122 assertFalse(rs.next());
129 ResultSet rs = con_.getMetaData().getTableTypes();
130 assertTrue(rs.next());
131 assertEquals(rs.getString(1).toLowerCase(),
"table");
132 assertFalse(rs.next());
139 ResultSet rs = con_.getMetaData().getColumns(null,
140 "functional",
"alltypessmall",
"s%rin%");
143 ResultSetMetaData rsmd = rs.getMetaData();
144 assertEquals(
"TABLE_CAT", rsmd.getColumnName(1));
145 assertTrue(rs.next());
146 String columnname = rs.getString(
"COLUMN_NAME");
147 int ordinalPos = rs.getInt(
"ORDINAL_POSITION");
148 assertEquals(
"Incorrect column name",
"string_col", columnname);
149 assertEquals(
"Incorrect ordinal position", 12, ordinalPos);
150 assertEquals(
"Incorrect type", Types.VARCHAR, rs.getInt(
"DATA_TYPE"));
151 assertFalse(rs.next());
155 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
"bool_col");
156 assertTrue(rs.next());
157 assertEquals(
"Incorrect type", Types.BOOLEAN, rs.getInt(
"DATA_TYPE"));
158 assertFalse(rs.next());
162 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
164 assertTrue(rs.next());
165 assertEquals(
"Incorrect type", Types.TINYINT, rs.getInt(
"DATA_TYPE"));
166 assertEquals(3, rs.getInt(
"COLUMN_SIZE"));
167 assertEquals(0, rs.getInt(
"DECIMAL_DIGITS"));
168 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
169 assertFalse(rs.next());
173 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
175 assertTrue(rs.next());
176 assertEquals(
"Incorrect type", Types.SMALLINT, rs.getInt(
"DATA_TYPE"));
177 assertEquals(5, rs.getInt(
"COLUMN_SIZE"));
178 assertEquals(0, rs.getInt(
"DECIMAL_DIGITS"));
179 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
180 assertFalse(rs.next());
184 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
"int_col");
185 assertTrue(rs.next());
186 assertEquals(
"Incorrect type", Types.INTEGER, rs.getInt(
"DATA_TYPE"));
187 assertEquals(10, rs.getInt(
"COLUMN_SIZE"));
188 assertEquals(0, rs.getInt(
"DECIMAL_DIGITS"));
189 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
190 assertFalse(rs.next());
194 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
196 assertTrue(rs.next());
197 assertEquals(
"Incorrect type", Types.BIGINT, rs.getInt(
"DATA_TYPE"));
198 assertEquals(19, rs.getInt(
"COLUMN_SIZE"));
199 assertEquals(0, rs.getInt(
"DECIMAL_DIGITS"));
200 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
201 assertFalse(rs.next());
205 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
"float_col");
206 assertTrue(rs.next());
207 assertEquals(
"Incorrect type", Types.FLOAT, rs.getInt(
"DATA_TYPE"));
208 assertEquals(7, rs.getInt(
"COLUMN_SIZE"));
209 assertEquals(7, rs.getInt(
"DECIMAL_DIGITS"));
210 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
211 assertFalse(rs.next());
215 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
217 assertTrue(rs.next());
218 assertEquals(
"Incorrect type", Types.DOUBLE, rs.getInt(
"DATA_TYPE"));
219 assertEquals(15, rs.getInt(
"COLUMN_SIZE"));
220 assertEquals(15, rs.getInt(
"DECIMAL_DIGITS"));
221 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
222 assertFalse(rs.next());
226 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall",
228 assertTrue(rs.next());
229 assertEquals(
"Incorrect type", Types.TIMESTAMP, rs.getInt(
"DATA_TYPE"));
230 assertEquals(29, rs.getInt(
"COLUMN_SIZE"));
231 assertEquals(9, rs.getInt(
"DECIMAL_DIGITS"));
233 assertEquals(null, rs.getString(
"NUM_PREC_RADIX"));
234 assertFalse(rs.next());
238 rs = con_.getMetaData().getColumns(null,
"functional",
"alltypessmall", null);
243 assertEquals(13, numCols);
247 rs = con_.getMetaData().getColumns(null,
"functional",
"decimal_tbl", null);
248 assertTrue(rs.next());
249 assertEquals(
"Incorrect type", Types.DECIMAL, rs.getInt(
"DATA_TYPE"));
250 assertEquals(9, rs.getInt(
"COLUMN_SIZE"));
251 assertEquals(0, rs.getInt(
"DECIMAL_DIGITS"));
252 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
253 assertTrue(rs.next());
254 assertEquals(
"Incorrect type", Types.DECIMAL, rs.getInt(
"DATA_TYPE"));
255 assertEquals(10, rs.getInt(
"COLUMN_SIZE"));
256 assertEquals(0, rs.getInt(
"DECIMAL_DIGITS"));
257 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
258 assertTrue(rs.next());
259 assertEquals(
"Incorrect type", Types.DECIMAL, rs.getInt(
"DATA_TYPE"));
260 assertEquals(20, rs.getInt(
"COLUMN_SIZE"));
261 assertEquals(10, rs.getInt(
"DECIMAL_DIGITS"));
262 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
263 assertTrue(rs.next());
264 assertEquals(
"Incorrect type", Types.DECIMAL, rs.getInt(
"DATA_TYPE"));
265 assertEquals(38, rs.getInt(
"COLUMN_SIZE"));
266 assertEquals(38, rs.getInt(
"DECIMAL_DIGITS"));
267 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
268 assertTrue(rs.next());
269 assertEquals(
"Incorrect type", Types.DECIMAL, rs.getInt(
"DATA_TYPE"));
270 assertEquals(10, rs.getInt(
"COLUMN_SIZE"));
271 assertEquals(5, rs.getInt(
"DECIMAL_DIGITS"));
272 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
273 assertTrue(rs.next());
274 assertEquals(
"Incorrect type", Types.DECIMAL, rs.getInt(
"DATA_TYPE"));
275 assertEquals(9, rs.getInt(
"COLUMN_SIZE"));
276 assertEquals(0, rs.getInt(
"DECIMAL_DIGITS"));
277 assertEquals(10, rs.getInt(
"NUM_PREC_RADIX"));
278 assertFalse(rs.next());
282 rs = con_.getMetaData().getColumns(null,
"functional",
"chars_tiny", null);
283 assertTrue(rs.next());
284 assertEquals(
"Incorrect type", Types.CHAR, rs.getInt(
"DATA_TYPE"));
285 assertEquals(5, rs.getInt(
"COLUMN_SIZE"));
286 assertTrue(rs.next());
287 assertEquals(
"Incorrect type", Types.CHAR, rs.getInt(
"DATA_TYPE"));
288 assertEquals(140, rs.getInt(
"COLUMN_SIZE"));
289 assertTrue(rs.next());
290 assertEquals(
"Incorrect type", Types.VARCHAR, rs.getInt(
"DATA_TYPE"));
291 assertEquals(32, rs.getInt(
"COLUMN_SIZE"));
292 assertFalse(rs.next());
299 ResultSet rs = con_.createStatement().executeQuery(
300 "select * from functional.decimal_tbl");
302 assertEquals(rs.getMetaData().getColumnType(1), Types.DECIMAL);
303 assertEquals(rs.getMetaData().getPrecision(1), 9);
304 assertEquals(rs.getMetaData().getScale(1), 0);
306 assertEquals(rs.getMetaData().getColumnType(2), Types.DECIMAL);
307 assertEquals(rs.getMetaData().getPrecision(2), 10);
308 assertEquals(rs.getMetaData().getScale(2), 0);
310 assertEquals(rs.getMetaData().getColumnType(3), Types.DECIMAL);
311 assertEquals(rs.getMetaData().getPrecision(3), 20);
312 assertEquals(rs.getMetaData().getScale(3), 10);
314 assertEquals(rs.getMetaData().getColumnType(4), Types.DECIMAL);
315 assertEquals(rs.getMetaData().getPrecision(4), 38);
316 assertEquals(rs.getMetaData().getScale(4), 38);
318 assertEquals(rs.getMetaData().getColumnType(5), Types.DECIMAL);
319 assertEquals(rs.getMetaData().getPrecision(5), 10);
320 assertEquals(rs.getMetaData().getScale(5), 5);
322 assertEquals(rs.getMetaData().getColumnType(6), Types.DECIMAL);
323 assertEquals(rs.getMetaData().getPrecision(6), 9);
324 assertEquals(rs.getMetaData().getScale(6), 0);
334 ResultSet rs = con_.getMetaData().getColumns(null,
"functional",
"alltypes", null);
336 ResultSetMetaData rsmd = rs.getMetaData();
337 assertEquals(
"TABLE_CAT", rsmd.getColumnName(1));
338 assertEquals(Types.VARCHAR, rsmd.getColumnType(1));
339 assertEquals(Integer.MAX_VALUE, rsmd.getColumnDisplaySize(1));
340 assertEquals(
"ORDINAL_POSITION", rsmd.getColumnName(17));
341 assertEquals(Types.INTEGER, rsmd.getColumnType(17));
342 assertEquals(11, rsmd.getColumnDisplaySize(17));
349 ResultSet rs = con_.getMetaData().getFunctions(
350 null, null,
"substring");
353 String funcName = rs.getString(
"FUNCTION_NAME");
354 assertEquals(
"Incorrect function name",
"substring", funcName.toLowerCase());
355 String dbName = rs.getString(
"FUNCTION_SCHEM");
356 assertEquals(
"Incorrect function name",
"_impala_builtins", dbName.toLowerCase());
357 String fnSignature = rs.getString(
"SPECIFIC_NAME");
358 assertTrue(fnSignature.startsWith(
"substring("));
361 assertEquals(numFound, 2);
365 rs = con_.getMetaData().getFunctions(null,
"default",
"substring");
366 assertFalse(rs.next());
372 ResultSet rs = con_.createStatement().executeQuery(
"select user()");
377 assertTrue(rs.next());
378 assertNull(rs.getString(1));
379 assertFalse(rs.next());
388 ResultSet rs = con_.createStatement().executeQuery(
"select NULL");
390 assertEquals(rs.getMetaData().getColumnType(1), Types.BOOLEAN);
393 assertTrue(rs.next());
394 assertNull(rs.getString(1));
395 assertFalse(rs.next());
void testMetaDataGetTables()
void testMetaDataGetTableTypes()
void testMetaDataGetColumns()
void testMetaDataGetSchemas()
void testDecimalGetColumnTypes()
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
void testMetaDataGetCatalogs()
void testMetaDataGetFunctions()
void testMetaDataGetColumnsMetaData()
void testUtilityFunctions()