Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CatalogTest.java
Go to the documentation of this file.
1 // Copyright (c) 2012 Cloudera, Inc. All rights reserved.
2 
3 package com.cloudera.impala.catalog;
4 
5 import static com.cloudera.impala.thrift.ImpalaInternalServiceConstants.DEFAULT_PARTITION_ID;
6 import static org.junit.Assert.assertEquals;
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.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Set;
15 
16 import org.apache.hadoop.hive.metastore.TableType;
17 import org.apache.hadoop.hive.metastore.api.ColumnStatisticsData;
18 import org.junit.Test;
19 
26 import com.google.common.collect.Lists;
27 import com.google.common.collect.Sets;
28 
29 public class CatalogTest {
31  CatalogServiceTestCatalog.create();
32 
33  private void checkTableCols(Db db, String tblName, int numClusteringCols,
34  String[] colNames, Type[] colTypes) throws TableLoadingException {
35  Table tbl = db.getTable(tblName);
36  assertEquals(tbl.getName(), tblName);
37  assertEquals(tbl.getNumClusteringCols(), numClusteringCols);
38  List<Column> cols = tbl.getColumns();
39  assertEquals(colNames.length, colTypes.length);
40  assertEquals(cols.size(), colNames.length);
41  Iterator<Column> it = cols.iterator();
42  int i = 0;
43  while (it.hasNext()) {
44  Column col = it.next();
45  assertEquals(col.getName(), colNames[i]);
46  assertTrue(col.getType().equals(colTypes[i]));
47  ++i;
48  }
49  }
50 
51  private void checkHBaseTableCols(Db db, String hiveTableName, String hbaseTableName,
52  String[] hiveColNames, String[] colFamilies, String[] colQualifiers,
53  Type[] colTypes) throws TableLoadingException{
54  checkTableCols(db, hiveTableName, 1, hiveColNames, colTypes);
55  HBaseTable tbl = (HBaseTable) db.getTable(hiveTableName);
56  assertEquals(tbl.getHBaseTableName(), hbaseTableName);
57  List<Column> cols = tbl.getColumns();
58  assertEquals(colFamilies.length, colTypes.length);
59  assertEquals(colQualifiers.length, colTypes.length);
60  Iterator<Column> it = cols.iterator();
61  int i = 0;
62  while (it.hasNext()) {
63  HBaseColumn col = (HBaseColumn)it.next();
64  assertEquals(col.getColumnFamily(), colFamilies[i]);
65  assertEquals(col.getColumnQualifier(), colQualifiers[i]);
66  ++i;
67  }
68  }
69 
70  @Test
71  public void TestColSchema() throws CatalogException {
72  Db functionalDb = catalog_.getDb("functional");
73  assertNotNull(functionalDb);
74  assertEquals(functionalDb.getName(), "functional");
75  assertNotNull(catalog_.getOrLoadTable("functional", "alltypes"));
76  assertNotNull(catalog_.getOrLoadTable("functional", "alltypes_view"));
77  assertNotNull(catalog_.getOrLoadTable("functional", "alltypes_view_sub"));
78  assertNotNull(catalog_.getOrLoadTable("functional", "alltypessmall"));
79  assertNotNull(catalog_.getOrLoadTable("functional", "alltypeserror"));
80  assertNotNull(catalog_.getOrLoadTable("functional", "alltypeserrornonulls"));
81  assertNotNull(catalog_.getOrLoadTable("functional", "alltypesagg"));
82  assertNotNull(catalog_.getOrLoadTable("functional", "alltypesaggnonulls"));
83  assertNotNull(catalog_.getOrLoadTable("functional", "alltypesnopart"));
84  assertNotNull(catalog_.getOrLoadTable("functional", "alltypesinsert"));
85  assertNotNull(catalog_.getOrLoadTable("functional", "complex_view"));
86  assertNotNull(catalog_.getOrLoadTable("functional", "testtbl"));
87  assertNotNull(catalog_.getOrLoadTable("functional", "dimtbl"));
88  assertNotNull(catalog_.getOrLoadTable("functional", "jointbl"));
89  assertNotNull(catalog_.getOrLoadTable("functional", "liketbl"));
90  assertNotNull(catalog_.getOrLoadTable("functional", "greptiny"));
91  assertNotNull(catalog_.getOrLoadTable("functional", "rankingssmall"));
92  assertNotNull(catalog_.getOrLoadTable("functional", "uservisitssmall"));
93  assertNotNull(catalog_.getOrLoadTable("functional", "view_view"));
94  // IMP-163 - table with string partition column does not load if there are partitions
95  assertNotNull(catalog_.getOrLoadTable("functional", "StringPartitionKey"));
96  // Test non-existent table
97  assertNull(catalog_.getOrLoadTable("functional", "nonexistenttable"));
98 
99  // functional_seq contains the same tables as functional
100  Db testDb = catalog_.getDb("functional_seq");
101  assertNotNull(testDb);
102  assertEquals(testDb.getName(), "functional_seq");
103  assertNotNull(catalog_.getOrLoadTable("functional_seq", "alltypes"));
104  assertNotNull(catalog_.getOrLoadTable("functional_seq", "testtbl"));
105 
106  Db hbaseDb = catalog_.getDb("functional_hbase");
107  assertNotNull(hbaseDb);
108  assertEquals(hbaseDb.getName(), "functional_hbase");
109  // Loading succeeds for an HBase table that has binary columns and an implicit key
110  // column mapping
111  assertNotNull(catalog_.getOrLoadTable(hbaseDb.getName(), "alltypessmallbinary"));
112  assertNotNull(catalog_.getOrLoadTable(hbaseDb.getName(), "alltypessmall"));
113  assertNotNull(catalog_.getOrLoadTable(hbaseDb.getName(), "hbasealltypeserror"));
114  assertNotNull(catalog_.getOrLoadTable(hbaseDb.getName(),
115  "hbasealltypeserrornonulls"));
116  assertNotNull(catalog_.getOrLoadTable(hbaseDb.getName(), "alltypesagg"));
117  assertNotNull(catalog_.getOrLoadTable(hbaseDb.getName(), "stringids"));
118 
119  checkTableCols(functionalDb, "alltypes", 2,
120  new String[]
121  {"year", "month", "id", "bool_col", "tinyint_col", "smallint_col",
122  "int_col", "bigint_col", "float_col", "double_col", "date_string_col",
123  "string_col", "timestamp_col"},
124  new Type[]
129  Type.TIMESTAMP});
130  checkTableCols(functionalDb, "testtbl", 0,
131  new String[] {"id", "name", "zip"},
132  new Type[]
133  {Type.BIGINT, Type.STRING, Type.INT});
134  checkTableCols(testDb, "testtbl", 0,
135  new String[] {"id", "name", "zip"},
136  new Type[]
137  {Type.BIGINT, Type.STRING, Type.INT});
138  checkTableCols(functionalDb, "liketbl", 0,
139  new String[] {
140  "str_col", "match_like_col", "no_match_like_col", "match_regex_col",
141  "no_match_regex_col"},
142  new Type[]
144  Type.STRING, Type.STRING});
145  checkTableCols(functionalDb, "dimtbl", 0,
146  new String[] {"id", "name", "zip"},
147  new Type[]
148  {Type.BIGINT, Type.STRING, Type.INT});
149  checkTableCols(functionalDb, "jointbl", 0,
150  new String[] {"test_id", "test_name", "test_zip", "alltypes_id"},
151  new Type[]
153  Type.INT});
154 
155  checkHBaseTableCols(hbaseDb, "alltypessmall", "functional_hbase.alltypessmall",
156  new String[]
157  {"id", "bigint_col", "bool_col", "date_string_col", "double_col", "float_col",
158  "int_col", "month", "smallint_col", "string_col", "timestamp_col",
159  "tinyint_col", "year"},
160  new String[]
161  {":key", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d"},
162  new String[]
163  {null, "bigint_col", "bool_col", "date_string_col", "double_col", "float_col",
164  "int_col", "month", "smallint_col", "string_col", "timestamp_col",
165  "tinyint_col", "year"},
166  new Type[]
171  Type.TINYINT, Type.INT});
172 
173  checkHBaseTableCols(hbaseDb, "hbasealltypeserror",
174  "functional_hbase.hbasealltypeserror",
175  new String[]
176  {"id", "bigint_col", "bool_col","date_string_col", "double_col", "float_col",
177  "int_col", "smallint_col", "string_col","timestamp_col", "tinyint_col"},
178  new String[]
179  {":key", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d"},
180  new String[]
181  {null, "bigint_col", "bool_col","date_string_col", "double_col", "float_col",
182  "int_col", "smallint_col", "string_col","timestamp_col", "tinyint_col"},
183  new Type[]
187  Type.TIMESTAMP, Type.TINYINT});
188 
189  checkHBaseTableCols(hbaseDb, "hbasealltypeserrornonulls",
190  "functional_hbase.hbasealltypeserrornonulls",
191  new String[]
192  {"id", "bigint_col", "bool_col", "date_string_col", "double_col", "float_col",
193  "int_col", "smallint_col", "string_col","timestamp_col", "tinyint_col"},
194  new String[]
195  {":key", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d"},
196  new String[]
197  {null, "bigint_col", "bool_col", "date_string_col", "double_col", "float_col",
198  "int_col", "smallint_col", "string_col","timestamp_col", "tinyint_col"},
199  new Type[]
203  Type.TIMESTAMP, Type.TINYINT});
204 
205  checkHBaseTableCols(hbaseDb, "alltypesagg", "functional_hbase.alltypesagg",
206  new String[]
207  {"id", "bigint_col", "bool_col", "date_string_col", "day", "double_col",
208  "float_col", "int_col", "month", "smallint_col", "string_col",
209  "timestamp_col", "tinyint_col", "year"},
210  new String[]
211  {":key", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d"},
212  new String[]
213  {null, "bigint_col", "bool_col", "date_string_col", "day", "double_col",
214  "float_col", "int_col", "month", "smallint_col", "string_col",
215  "timestamp_col", "tinyint_col", "year"},
216  new Type[]
221  Type.TINYINT, Type.INT});
222 
223  checkHBaseTableCols(hbaseDb, "stringids", "functional_hbase.alltypesagg",
224  new String[]
225  {"id", "bigint_col", "bool_col", "date_string_col", "day", "double_col",
226  "float_col", "int_col", "month", "smallint_col", "string_col",
227  "timestamp_col", "tinyint_col", "year"},
228  new String[]
229  {":key", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d"},
230  new String[]
231  {null, "bigint_col", "bool_col", "date_string_col", "day", "double_col",
232  "float_col", "int_col", "month", "smallint_col", "string_col",
233  "timestamp_col", "tinyint_col", "year"},
234  new Type[]
239  Type.TINYINT, Type.INT});
240 
241  checkTableCols(functionalDb, "greptiny", 0,
242  new String[]
243  {"field"},
244  new Type[]
245  {Type.STRING});
246 
247  checkTableCols(functionalDb, "rankingssmall", 0,
248  new String[]
249  {"pagerank", "pageurl", "avgduration"},
250  new Type[]
251  {Type.INT, Type.STRING, Type.INT});
252 
253  checkTableCols(functionalDb, "uservisitssmall", 0,
254  new String[]
255  {"sourceip", "desturl", "visitdate", "adrevenue", "useragent",
256  "ccode", "lcode", "skeyword", "avgtimeonsite"},
257  new Type[]
260  Type.STRING, Type.STRING, Type.INT});
261 
262  // case-insensitive lookup
263  assertEquals(catalog_.getOrLoadTable("functional", "alltypes"),
264  catalog_.getOrLoadTable("functional", "AllTypes"));
265  }
266 
267  @Test
268  public void TestPartitions() throws CatalogException {
269  HdfsTable table =
270  (HdfsTable) catalog_.getOrLoadTable("functional", "AllTypes");
271  List<HdfsPartition> partitions = table.getPartitions();
272 
273  // check that partition keys cover the date range 1/1/2009-12/31/2010
274  // and that we have one file per partition, plus the default partition
275  assertEquals(25, partitions.size());
276  Set<Long> months = Sets.newHashSet();
277  for (HdfsPartition p: partitions) {
278  if (p.getId() == DEFAULT_PARTITION_ID) {
279  continue;
280  }
281 
282  assertEquals(2, p.getPartitionValues().size());
283 
284  LiteralExpr key1Expr = p.getPartitionValues().get(0);
285  assertTrue(key1Expr instanceof NumericLiteral);
286  long key1 = ((NumericLiteral) key1Expr).getLongValue();
287  assertTrue(key1 == 2009 || key1 == 2010);
288 
289  LiteralExpr key2Expr = p.getPartitionValues().get(1);
290  assertTrue(key2Expr instanceof NumericLiteral);
291  long key2 = ((NumericLiteral) key2Expr).getLongValue();
292  assertTrue(key2 >= 1 && key2 <= 12);
293 
294  months.add(key1 * 100 + key2);
295 
296  assertEquals(p.getFileDescriptors().size(), 1);
297  }
298  assertEquals(months.size(), 24);
299  }
300 
301  // TODO: All Hive-stats related tests are temporarily disabled because of an unknown,
302  // sporadic issue causing stats of some columns to be absent in Jenkins runs.
303  // Investigate this issue further.
304  //@Test
305  public void testStats() throws TableLoadingException {
306  // make sure the stats for functional.alltypesagg look correct
307  HdfsTable table =
308  (HdfsTable) catalog_.getDb("functional").getTable("AllTypesAgg");
309 
310  Column idCol = table.getColumn("id");
311  assertEquals(idCol.getStats().getAvgSerializedSize() -
312  PrimitiveType.INT.getSlotSize(),
313  PrimitiveType.INT.getSlotSize(), 0.0001);
314  assertEquals(idCol.getStats().getMaxSize(), PrimitiveType.INT.getSlotSize());
315  assertTrue(!idCol.getStats().hasNulls());
316 
317  Column boolCol = table.getColumn("bool_col");
318  assertEquals(boolCol.getStats().getAvgSerializedSize() -
319  PrimitiveType.BOOLEAN.getSlotSize(),
320  PrimitiveType.BOOLEAN.getSlotSize(), 0.0001);
321  assertEquals(boolCol.getStats().getMaxSize(), PrimitiveType.BOOLEAN.getSlotSize());
322  assertTrue(!boolCol.getStats().hasNulls());
323 
324  Column tinyintCol = table.getColumn("tinyint_col");
325  assertEquals(tinyintCol.getStats().getAvgSerializedSize() -
326  PrimitiveType.TINYINT.getSlotSize(),
327  PrimitiveType.TINYINT.getSlotSize(), 0.0001);
328  assertEquals(tinyintCol.getStats().getMaxSize(),
329  PrimitiveType.TINYINT.getSlotSize());
330  assertTrue(tinyintCol.getStats().hasNulls());
331 
332  Column smallintCol = table.getColumn("smallint_col");
333  assertEquals(smallintCol.getStats().getAvgSerializedSize() -
334  PrimitiveType.SMALLINT.getSlotSize(),
335  PrimitiveType.SMALLINT.getSlotSize(), 0.0001);
336  assertEquals(smallintCol.getStats().getMaxSize(),
337  PrimitiveType.SMALLINT.getSlotSize());
338  assertTrue(smallintCol.getStats().hasNulls());
339 
340  Column intCol = table.getColumn("int_col");
341  assertEquals(intCol.getStats().getAvgSerializedSize() -
342  PrimitiveType.INT.getSlotSize(),
343  PrimitiveType.INT.getSlotSize(), 0.0001);
344  assertEquals(intCol.getStats().getMaxSize(), PrimitiveType.INT.getSlotSize());
345  assertTrue(intCol.getStats().hasNulls());
346 
347  Column bigintCol = table.getColumn("bigint_col");
348  assertEquals(bigintCol.getStats().getAvgSerializedSize() -
349  PrimitiveType.BIGINT.getSlotSize(),
350  PrimitiveType.BIGINT.getSlotSize(), 0.0001);
351  assertEquals(bigintCol.getStats().getMaxSize(), PrimitiveType.BIGINT.getSlotSize());
352  assertTrue(bigintCol.getStats().hasNulls());
353 
354  Column floatCol = table.getColumn("float_col");
355  assertEquals(floatCol.getStats().getAvgSerializedSize() -
356  PrimitiveType.FLOAT.getSlotSize(),
357  PrimitiveType.FLOAT.getSlotSize(), 0.0001);
358  assertEquals(floatCol.getStats().getMaxSize(), PrimitiveType.FLOAT.getSlotSize());
359  assertTrue(floatCol.getStats().hasNulls());
360 
361  Column doubleCol = table.getColumn("double_col");
362  assertEquals(doubleCol.getStats().getAvgSerializedSize() -
363  PrimitiveType.DOUBLE.getSlotSize(),
364  PrimitiveType.DOUBLE.getSlotSize(), 0.0001);
365  assertEquals(doubleCol.getStats().getMaxSize(), PrimitiveType.DOUBLE.getSlotSize());
366  assertTrue(doubleCol.getStats().hasNulls());
367 
368  Column timestampCol = table.getColumn("timestamp_col");
369  assertEquals(timestampCol.getStats().getAvgSerializedSize() -
370  PrimitiveType.TIMESTAMP.getSlotSize(),
371  PrimitiveType.TIMESTAMP.getSlotSize(), 0.0001);
372  assertEquals(timestampCol.getStats().getMaxSize(),
373  PrimitiveType.TIMESTAMP.getSlotSize());
374  // this does not have nulls, it's not clear why this passes
375  // TODO: investigate and re-enable
376  //assertTrue(timestampCol.getStats().hasNulls());
377 
378  Column stringCol = table.getColumn("string_col");
379  assertTrue(stringCol.getStats().getAvgSerializedSize() >=
380  PrimitiveType.STRING.getSlotSize());
381  assertTrue(stringCol.getStats().getAvgSerializedSize() > 0);
382  assertTrue(stringCol.getStats().getMaxSize() > 0);
383  assertTrue(!stringCol.getStats().hasNulls());
384  }
385 
391  // TODO: All Hive-stats related tests are temporarily disabled because of an unknown,
392  // sporadic issue causing stats of some columns to be absent in Jenkins runs.
393  // Investigate this issue further.
394  //@Test
395  public void testColStatsColTypeMismatch() throws Exception {
396  // First load a table that has column stats.
397  //catalog_.refreshTable("functional", "alltypesagg", false);
398  HdfsTable table = (HdfsTable) catalog_.getOrLoadTable("functional", "alltypesagg");
399 
400  // Now attempt to update a column's stats with mismatched stats data and ensure
401  // we get the expected results.
402  MetaStoreClient client = catalog_.getMetaStoreClient();
403  try {
404  // Load some string stats data and use it to update the stats of different
405  // typed columns.
406  ColumnStatisticsData stringColStatsData = client.getHiveClient()
407  .getTableColumnStatistics("functional", "alltypesagg",
408  Lists.newArrayList("string_col")).get(0).getStatsData();
409 
410  assertTrue(!table.getColumn("int_col").updateStats(stringColStatsData));
411  assertStatsUnknown(table.getColumn("int_col"));
412 
413  assertTrue(!table.getColumn("double_col").updateStats(stringColStatsData));
414  assertStatsUnknown(table.getColumn("double_col"));
415 
416  assertTrue(!table.getColumn("bool_col").updateStats(stringColStatsData));
417  assertStatsUnknown(table.getColumn("bool_col"));
418 
419  // Do the same thing, but apply bigint stats to a string column.
420  ColumnStatisticsData bigIntCol = client.getHiveClient()
421  .getTableColumnStatistics("functional", "alltypes",
422  Lists.newArrayList("bigint_col")).get(0).getStatsData();
423  assertTrue(!table.getColumn("string_col").updateStats(bigIntCol));
424  assertStatsUnknown(table.getColumn("string_col"));
425 
426  // Now try to apply a matching column stats data and ensure it succeeds.
427  assertTrue(table.getColumn("string_col").updateStats(stringColStatsData));
428  assertEquals(1178, table.getColumn("string_col").getStats().getNumDistinctValues());
429  } finally {
430  // Make sure to invalidate the metadata so the next test isn't using bad col stats
431  //catalog_.refreshTable("functional", "alltypesagg", false);
432  client.release();
433  }
434  }
435 
436  private void assertStatsUnknown(Column column) {
437  assertEquals(-1, column.getStats().getNumDistinctValues());
438  assertEquals(-1, column.getStats().getNumNulls());
439  double expectedSize = column.getType().isFixedLengthType() ?
440  column.getType().getSlotSize() : -1;
441 
442  assertEquals(expectedSize, column.getStats().getAvgSerializedSize(), 0.0001);
443  assertEquals(expectedSize, column.getStats().getMaxSize(), 0.0001);
444  }
445 
446  @Test
448  // Cast will fail if table not an HBaseTable
449  HBaseTable table = (HBaseTable)
450  catalog_.getOrLoadTable("functional_hbase", "internal_hbase_table");
451  assertNotNull("functional_hbase.internal_hbase_table was not found", table);
452  }
453 
454  @Test
455  public void testDatabaseDoesNotExist() {
456  Db nonExistentDb = catalog_.getDb("doesnotexist");
457  assertNull(nonExistentDb);
458  }
459 
460  @Test
462  Table table = catalog_.getOrLoadTable("functional", "alltypes");
463  // Tables are created via Impala so the metadata should have been populated properly.
464  // alltypes is an external table.
465  assertEquals(System.getProperty("user.name"), table.getMetaStoreTable().getOwner());
466  assertEquals(TableType.EXTERNAL_TABLE.toString(),
467  table.getMetaStoreTable().getTableType());
468  // alltypesinsert is created using CREATE TABLE LIKE and is a MANAGED table
469  table = catalog_.getOrLoadTable("functional", "alltypesinsert");
470  assertEquals(System.getProperty("user.name"), table.getMetaStoreTable().getOwner());
471  assertEquals(TableType.MANAGED_TABLE.toString(),
472  table.getMetaStoreTable().getTableType());
473  }
474 
475  @Test
477  Table table = catalog_.getOrLoadTable("functional", "hive_index_tbl");
478  assertTrue(table instanceof IncompleteTable);
479  IncompleteTable incompleteTable = (IncompleteTable) table;
480  assertTrue(incompleteTable.getCause() instanceof TableLoadingException);
481  assertEquals("Unsupported table type 'INDEX_TABLE' for: functional.hive_index_tbl",
482  incompleteTable.getCause().getMessage());
483 
484  // Table with unsupported SerDe library.
485  table = catalog_.getOrLoadTable("functional", "bad_serde");
486  assertTrue(table instanceof IncompleteTable);
487  incompleteTable = (IncompleteTable) table;
488  assertTrue(incompleteTable.getCause() instanceof TableLoadingException);
489  assertEquals("Impala does not support tables of this type. REASON: SerDe" +
490  " library 'org.apache.hadoop.hive.serde2.binarysortable.BinarySortableSerDe' " +
491  "is not supported.", incompleteTable.getCause().getCause().getMessage());
492 
493  // Impala does not yet support Hive's LazyBinaryColumnarSerDe which can be
494  // used for RCFILE tables.
495  table = catalog_.getOrLoadTable("functional_rc", "rcfile_lazy_binary_serde");
496  assertTrue(table instanceof IncompleteTable);
497  incompleteTable = (IncompleteTable) table;
498  assertTrue(incompleteTable.getCause() instanceof TableLoadingException);
499  assertEquals("Impala does not support tables of this type. REASON: SerDe" +
500  " library 'org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe' " +
501  "is not supported.", incompleteTable.getCause().getCause().getMessage());
502  }
503 
504  // This table has metadata set so the escape is \n, which is also the tuple delim. This
505  // test validates that our representation of the catalog fixes this and removes the
506  // escape char.
508  HdfsTable table =
509  (HdfsTable) catalog_.getOrLoadTable("functional", "escapechartesttable");
510  List<HdfsPartition> partitions = table.getPartitions();
511  for (HdfsPartition p: partitions) {
512  HdfsStorageDescriptor desc = p.getInputFormatDescriptor();
514  }
515  }
516 
517  private List<String> getFunctionSignatures(String db) throws DatabaseNotFoundException {
518  List<Function> fns = catalog_.getFunctions(db);
519  List<String> names = Lists.newArrayList();
520  for (Function fn: fns) {
521  names.add(fn.signatureString());
522  }
523  return names;
524  }
525 
526  @Test
527  public void TestUdf() throws CatalogException {
528  List<String> fnNames = getFunctionSignatures("default");
529  assertEquals(fnNames.size(), 0);
530 
531  ArrayList<Type> args1 = Lists.newArrayList();
532  ArrayList<Type> args2 = Lists.<Type>newArrayList(Type.INT);
533  ArrayList<Type> args3 = Lists.<Type>newArrayList(Type.TINYINT);
534 
535  catalog_.removeFunction(
536  new Function(new FunctionName("default", "Foo"), args1,
537  Type.INVALID, false));
538  fnNames = getFunctionSignatures("default");
539  assertEquals(fnNames.size(), 0);
540 
541  ScalarFunction udf1 = new ScalarFunction(new FunctionName("default", "Foo"),
542  args1, Type.INVALID, new HdfsUri("/Foo"), "Foo.class", null, null);
543  catalog_.addFunction(udf1);
544  fnNames = getFunctionSignatures("default");
545  assertEquals(fnNames.size(), 1);
546  assertTrue(fnNames.contains("foo()"));
547 
548  // Same function name, overloaded arguments
549  ScalarFunction udf2 = new ScalarFunction(new FunctionName("default", "Foo"),
550  args2, Type.INVALID, new HdfsUri("/Foo"), "Foo.class", null, null);
551  catalog_.addFunction(udf2);
552  fnNames = getFunctionSignatures("default");
553  assertEquals(fnNames.size(), 2);
554  assertTrue(fnNames.contains("foo()"));
555  assertTrue(fnNames.contains("foo(INT)"));
556 
557  // Add a function with a new name
558  ScalarFunction udf3 = new ScalarFunction(new FunctionName("default", "Bar"),
559  args2, Type.INVALID, new HdfsUri("/Foo"), "Foo.class", null, null);
560  catalog_.addFunction(udf3);
561  fnNames = getFunctionSignatures("default");
562  assertEquals(fnNames.size(), 3);
563  assertTrue(fnNames.contains("foo()"));
564  assertTrue(fnNames.contains("foo(INT)"));
565  assertTrue(fnNames.contains("bar(INT)"));
566 
567  // Drop Foo()
568  catalog_.removeFunction(new Function(
569  new FunctionName("default", "Foo"), args1, Type.INVALID, false));
570  fnNames = getFunctionSignatures("default");
571  assertEquals(fnNames.size(), 2);
572  assertTrue(fnNames.contains("foo(INT)"));
573  assertTrue(fnNames.contains("bar(INT)"));
574 
575  // Drop it again, no-op
576  catalog_.removeFunction(new Function(
577  new FunctionName("default", "Foo"), args1, Type.INVALID, false));
578  fnNames = getFunctionSignatures("default");
579  assertEquals(fnNames.size(), 2);
580  assertTrue(fnNames.contains("foo(INT)"));
581  assertTrue(fnNames.contains("bar(INT)"));
582 
583  // Drop bar(), no-op
584  catalog_.removeFunction(new Function(
585  new FunctionName("default", "Bar"), args1, Type.INVALID, false));
586  fnNames = getFunctionSignatures("default");
587  assertEquals(fnNames.size(), 2);
588  assertTrue(fnNames.contains("foo(INT)"));
589  assertTrue(fnNames.contains("bar(INT)"));
590 
591  // Drop bar(tinyint), no-op
592  catalog_.removeFunction(new Function(
593  new FunctionName("default", "Bar"), args3, Type.INVALID, false));
594  fnNames = getFunctionSignatures("default");
595  assertEquals(fnNames.size(), 2);
596  assertTrue(fnNames.contains("foo(INT)"));
597  assertTrue(fnNames.contains("bar(INT)"));
598 
599  // Drop bar(int)
600  catalog_.removeFunction(new Function(
601  new FunctionName("default", "Bar"), args2, Type.INVALID, false));
602  fnNames = getFunctionSignatures("default");
603  assertEquals(fnNames.size(), 1);
604  assertTrue(fnNames.contains("foo(INT)"));
605 
606  // Drop foo(int)
607  catalog_.removeFunction(new Function(
608  new FunctionName("default", "foo"), args2, Type.INVALID, false));
609  fnNames = getFunctionSignatures("default");
610  assertEquals(fnNames.size(), 0);
611  }
612 }
void checkHBaseTableCols(Db db, String hiveTableName, String hbaseTableName, String[] hiveColNames, String[] colFamilies, String[] colQualifiers, Type[] colTypes)
static final ScalarType BIGINT
Definition: Type.java:50
static final ScalarType STRING
Definition: Type.java:53
void checkTableCols(Db db, String tblName, int numClusteringCols, String[] colNames, Type[] colTypes)
static final ScalarType BOOLEAN
Definition: Type.java:46
static final ScalarType SMALLINT
Definition: Type.java:48
static final ScalarType FLOAT
Definition: Type.java:51
String getHBaseTableName(org.apache.hadoop.hive.metastore.api.Table tbl)
static final ScalarType DOUBLE
Definition: Type.java:52
static final ScalarType TINYINT
Definition: Type.java:47
List< String > getFunctionSignatures(String db)
static final ScalarType INT
Definition: Type.java:49
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
static CatalogServiceCatalog catalog_
Column getColumn(String name)
Definition: Table.java:392
static final ScalarType INVALID
Definition: Type.java:44
Table getOrLoadTable(String dbName, String tblName)
static final ScalarType TIMESTAMP
Definition: Type.java:55