Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
JdbcTest.java
Go to the documentation of this file.
1 // Copyright (c) 2012 Cloudera, Inc. All rights reserved.
2 
3 package com.cloudera.impala.service;
4 
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;
10 
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;
18 import java.util.Map;
19 
20 import org.junit.AfterClass;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 
25 
33 public class JdbcTest {
34  private static Connection con_;
35 
36  @BeforeClass
37  public static void setUp() throws Exception {
38  ImpalaJdbcClient client = ImpalaJdbcClient.createClientUsingHiveJdbcDriver();
39  client.connect();
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);
45  }
46 
47  @AfterClass
48  public static void cleanUp() throws Exception {
49  con_.close();
50  assertTrue("Connection should be closed", con_.isClosed());
51 
52  Exception expectedException = null;
53  try {
54  con_.createStatement();
55  } catch (Exception e) {
56  expectedException = e;
57  }
58 
59  assertNotNull("createStatement() on closed connection should throw exception",
60  expectedException);
61  }
62 
63  @Test
64  public void testMetaDataGetTables() throws SQLException {
65  // map from tablename search pattern to actual table name.
66  Map<String, String> tests = new HashMap<String, String>();
67  tests.put("alltypes", "alltypes");
68  tests.put("%all_ypes", "alltypes");
69 
70  String[][] tblTypes = {null, {"TABLE"}};
71 
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());
77 
78  // TABLE_NAME is the 3rd column.
79  String resultTableName = rs.getString("TABLE_NAME");
80  assertEquals(rs.getString(3), resultTableName);
81 
82  assertEquals("Table mismatch", tests.get(tblNamePattern), resultTableName);
83  String tableType = rs.getString("TABLE_TYPE");
84  assertEquals("table", tableType.toLowerCase());
85  assertFalse(rs.next());
86  rs.close();
87  }
88  }
89 
90  for (String[] tblType: tblTypes) {
91  ResultSet rs = con_.getMetaData().getTables(null, null, null, tblType);
92  // Should return at least one value.
93  assertTrue(rs.next());
94  rs.close();
95 
96  rs = con_.getMetaData().getTables(null, null, null, tblType);
97  assertTrue(rs.next());
98  rs.close();
99  }
100  }
101 
102  @Test
103  public void testMetaDataGetCatalogs() throws SQLException {
104  // Hive/Impala does not have catalogs.
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());
110  }
111 
112  @Test
113  public void testMetaDataGetSchemas() throws SQLException {
114  // There is only one schema: "default".
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());
123  rs.close();
124  }
125 
126  @Test
127  public void testMetaDataGetTableTypes() throws SQLException {
128  // There is only one table type: "table".
129  ResultSet rs = con_.getMetaData().getTableTypes();
130  assertTrue(rs.next());
131  assertEquals(rs.getString(1).toLowerCase(), "table");
132  assertFalse(rs.next());
133  rs.close();
134  }
135 
136  @Test
137  public void testMetaDataGetColumns() throws SQLException {
138  // It should return alltypessmall.string_col.
139  ResultSet rs = con_.getMetaData().getColumns(null,
140  "functional", "alltypessmall", "s%rin%");
141 
142  // validate the metadata for the getColumns result set
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());
152  rs.close();
153 
154  // validate bool_col
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());
159  rs.close();
160 
161  // validate tinyint_col
162  rs = con_.getMetaData().getColumns(null, "functional", "alltypessmall",
163  "tinyint_col");
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());
170  rs.close();
171 
172  // validate smallint_col
173  rs = con_.getMetaData().getColumns(null, "functional", "alltypessmall",
174  "smallint_col");
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());
181  rs.close();
182 
183  // validate int_col
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());
191  rs.close();
192 
193  // validate bigint_col
194  rs = con_.getMetaData().getColumns(null, "functional", "alltypessmall",
195  "bigint_col");
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());
202  rs.close();
203 
204  // validate float_col
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());
212  rs.close();
213 
214  // validate double_col
215  rs = con_.getMetaData().getColumns(null, "functional", "alltypessmall",
216  "double_col");
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());
223  rs.close();
224 
225  // validate timestamp_col
226  rs = con_.getMetaData().getColumns(null, "functional", "alltypessmall",
227  "timestamp_col");
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"));
232  // Use getString() to check the value is null (and not 0).
233  assertEquals(null, rs.getString("NUM_PREC_RADIX"));
234  assertFalse(rs.next());
235  rs.close();
236 
237  // validate null column name returns all columns.
238  rs = con_.getMetaData().getColumns(null, "functional", "alltypessmall", null);
239  int numCols = 0;
240  while (rs.next()) {
241  ++numCols;
242  }
243  assertEquals(13, numCols);
244  rs.close();
245 
246  // validate DECIMAL columns
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());
279  rs.close();
280 
281  // validate CHAR/VARCHAR columns
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());
293  rs.close();
294  }
295 
296  @Test
297  public void testDecimalGetColumnTypes() throws SQLException {
298  // Table has 5 decimal columns
299  ResultSet rs = con_.createStatement().executeQuery(
300  "select * from functional.decimal_tbl");
301 
302  assertEquals(rs.getMetaData().getColumnType(1), Types.DECIMAL);
303  assertEquals(rs.getMetaData().getPrecision(1), 9);
304  assertEquals(rs.getMetaData().getScale(1), 0);
305 
306  assertEquals(rs.getMetaData().getColumnType(2), Types.DECIMAL);
307  assertEquals(rs.getMetaData().getPrecision(2), 10);
308  assertEquals(rs.getMetaData().getScale(2), 0);
309 
310  assertEquals(rs.getMetaData().getColumnType(3), Types.DECIMAL);
311  assertEquals(rs.getMetaData().getPrecision(3), 20);
312  assertEquals(rs.getMetaData().getScale(3), 10);
313 
314  assertEquals(rs.getMetaData().getColumnType(4), Types.DECIMAL);
315  assertEquals(rs.getMetaData().getPrecision(4), 38);
316  assertEquals(rs.getMetaData().getScale(4), 38);
317 
318  assertEquals(rs.getMetaData().getColumnType(5), Types.DECIMAL);
319  assertEquals(rs.getMetaData().getPrecision(5), 10);
320  assertEquals(rs.getMetaData().getScale(5), 5);
321 
322  assertEquals(rs.getMetaData().getColumnType(6), Types.DECIMAL);
323  assertEquals(rs.getMetaData().getPrecision(6), 9);
324  assertEquals(rs.getMetaData().getScale(6), 0);
325 
326  rs.close();
327  }
328 
332  @Test
333  public void testMetaDataGetColumnsMetaData() throws SQLException {
334  ResultSet rs = con_.getMetaData().getColumns(null, "functional", "alltypes", null);
335 
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));
343  }
344 
345  @Test
346  public void testMetaDataGetFunctions() throws SQLException {
347  // Look up the 'substring' function.
348  // We support 2 overloaded version of it.
349  ResultSet rs = con_.getMetaData().getFunctions(
350  null, null, "substring");
351  int numFound = 0;
352  while (rs.next()) {
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("));
359  ++numFound;
360  }
361  assertEquals(numFound, 2);
362  rs.close();
363 
364  // substring is not in default db
365  rs = con_.getMetaData().getFunctions(null, "default", "substring");
366  assertFalse(rs.next());
367  rs.close();
368  }
369 
370  @Test
371  public void testUtilityFunctions() throws SQLException {
372  ResultSet rs = con_.createStatement().executeQuery("select user()");
373  try {
374  // We expect exactly one result row with a NULL inside the first column.
375  // The user() function returns NULL because we currently cannot set the user
376  // when establishing the Jdbc connection.
377  assertTrue(rs.next());
378  assertNull(rs.getString(1));
379  assertFalse(rs.next());
380  } finally {
381  rs.close();
382  }
383  }
384 
385  @Test
386  public void testSelectNull() throws SQLException {
387  // Regression test for IMPALA-914.
388  ResultSet rs = con_.createStatement().executeQuery("select NULL");
389  // Expect the column to be of type BOOLEAN to be compatible with Hive.
390  assertEquals(rs.getMetaData().getColumnType(1), Types.BOOLEAN);
391  try {
392  // We expect exactly one result row with a NULL inside the first column.
393  assertTrue(rs.next());
394  assertNull(rs.getString(1));
395  assertFalse(rs.next());
396  } finally {
397  rs.close();
398  }
399  }
400 }
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)