Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FrontendTest.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.assertTrue;
7 import static org.junit.Assert.fail;
8 
9 import java.util.List;
10 import java.util.Set;
11 
12 import org.apache.hive.service.cli.thrift.TGetCatalogsReq;
13 import org.apache.hive.service.cli.thrift.TGetColumnsReq;
14 import org.apache.hive.service.cli.thrift.TGetFunctionsReq;
15 import org.apache.hive.service.cli.thrift.TGetInfoReq;
16 import org.apache.hive.service.cli.thrift.TGetSchemasReq;
17 import org.apache.hive.service.cli.thrift.TGetTablesReq;
18 import org.junit.Test;
19 
26 import com.cloudera.impala.thrift.TMetadataOpRequest;
27 import com.cloudera.impala.thrift.TMetadataOpcode;
28 import com.cloudera.impala.thrift.TQueryCtx;
29 import com.cloudera.impala.thrift.TResultRow;
30 import com.cloudera.impala.thrift.TResultSet;
31 import com.google.common.collect.Lists;
32 import com.google.common.collect.Sets;
33 
42 public class FrontendTest {
43  private static Frontend fe_ = new Frontend(
45 
46  @Test
47  public void TestCatalogNotReady() throws ImpalaException {
49  TQueryCtx queryCtx = TestUtils.createQueryContext("default", "fake_user");
50 
51  // Queries that do not touch catalog objects should succeed.
52  queryCtx.request.setStmt("select 1");
53  fe.createExecRequest(queryCtx, new StringBuilder());
54 
55  // A query that touches a catalog object should fail.
56  queryCtx.request.setStmt("show tables");
57  try {
58  fe.createExecRequest(queryCtx, new StringBuilder());
59  fail("Expected failure to due uninitialized catalog.");
60  } catch (AnalysisException e) {
61  assertEquals("This Impala daemon is not ready to accept user requests. " +
62  "Status: Waiting for catalog update from the StateStore.", e.getMessage());
63  }
64  }
65 
66  @Test
67  public void TestGetTypeInfo() throws ImpalaException {
68  // Verify that the correct number of types are returned.
69  TMetadataOpRequest getInfoReq = new TMetadataOpRequest();
70  getInfoReq.opcode = TMetadataOpcode.GET_TYPE_INFO;
71  getInfoReq.get_info_req = new TGetInfoReq();
72  TResultSet resp = execMetadataOp(getInfoReq);
73  // DatabaseMetaData.getTypeInfo has 18 columns.
74  assertEquals(18, resp.schema.columns.size());
75  assertEquals(18, resp.rows.get(0).colVals.size());
76  // All primitives types, except INVALID_TYPE, DATE, DATETIME, DECIMAL, CHAR,
77  // and VARCHAR should be returned.
78  // Therefore #supported types = PrimitiveType.values().length - 6.
79  assertEquals(PrimitiveType.values().length - 6, resp.rows.size());
80  }
81 
82  @Test
83  public void TestGetSchema() throws ImpalaException {
84  // "default%" should return schema "default"
85  TMetadataOpRequest req = new TMetadataOpRequest();
86  req.opcode = TMetadataOpcode.GET_SCHEMAS;
87  req.get_schemas_req = new TGetSchemasReq();
88  req.get_schemas_req.setSchemaName("default%");
89  TResultSet resp = execMetadataOp(req);
90  // HiveServer2 GetSchemas has 2 columns.
91  assertEquals(2, resp.schema.columns.size());
92  assertEquals(2, resp.rows.get(0).colVals.size());
93  assertEquals(1, resp.rows.size());
94  assertEquals("default", resp.rows.get(0).colVals.get(0).string_val.toLowerCase());
95  }
96 
97  @Test
98  public void TestGetTables() throws ImpalaException {
99  // It should return two tables: one in "default"db and one in "testdb1".
100  TMetadataOpRequest req = new TMetadataOpRequest();
101  req.opcode = TMetadataOpcode.GET_TABLES;
102  req.get_tables_req = new TGetTablesReq();
103  req.get_tables_req.setSchemaName("functional");
104  req.get_tables_req.setTableName("all_ypes");
105  TResultSet resp = execMetadataOp(req);
106  // HiveServer2 GetTables has 5 columns.
107  assertEquals(5, resp.schema.columns.size());
108  assertEquals(5, resp.rows.get(0).colVals.size());
109  assertEquals(1, resp.rows.size());
110  assertEquals("alltypes", resp.rows.get(0).colVals.get(2).string_val.toLowerCase());
111  }
112 
113  @Test
114  public void TestGetColumns() throws ImpalaException {
115  // It should return one column: default.alltypes.string_col
116  TMetadataOpRequest req = new TMetadataOpRequest();
117  req.opcode = TMetadataOpcode.GET_COLUMNS;
118  req.get_columns_req = new TGetColumnsReq();
119  req.get_columns_req.setSchemaName("functional");
120  req.get_columns_req.setTableName("alltypes");
121  req.get_columns_req.setColumnName("stri%");
122  TResultSet resp = execMetadataOp(req);
123  // TODO: HiveServer2 thrift says the result set columns should be the
124  // same as ODBC SQLColumns, which has 18 columns. But the HS2 implementation has
125  // 23 columns. Follow the HS2 implementation for now because the HS2 thrift comment
126  // could be outdated.
127  assertEquals(23, resp.schema.columns.size());
128  assertEquals(23, resp.rows.get(0).colVals.size());
129  assertEquals(1, resp.rows.size());
130  TResultRow row = resp.rows.get(0);
131  assertEquals("functional", row.colVals.get(1).string_val.toLowerCase());
132  assertEquals("alltypes", row.colVals.get(2).string_val.toLowerCase());
133  assertEquals("string_col", row.colVals.get(3).string_val.toLowerCase());
134  }
135 
136  @Test
137  public void TestGetCatalogs() throws ImpalaException {
138  // Hive/Impala does not have catalog concept. Should return zero rows.
139  TMetadataOpRequest req = new TMetadataOpRequest();
140  req.opcode = TMetadataOpcode.GET_CATALOGS;
141  req.get_catalogs_req = new TGetCatalogsReq();
142  TResultSet resp = execMetadataOp(req);
143 
144  // HiveServer2 GetCatalogs() has 1 column.
145  assertEquals(1, resp.schema.columns.size());
146  assertEquals(0, resp.rows.size());
147  }
148 
149  @Test
150  public void TestGetTableTypes() throws ImpalaException {
151  // Impala should only return TABLE as the only table type.
152  TMetadataOpRequest req = new TMetadataOpRequest();
153  req.opcode = TMetadataOpcode.GET_TABLE_TYPES;
154  TResultSet resp = execMetadataOp(req);
155  // HiveServer2 GetTableTypes() has 1 column.
156  assertEquals(1, resp.schema.columns.size());
157  assertEquals(1, resp.rows.get(0).colVals.size());
158  assertEquals(1, resp.rows.size());
159  assertEquals("TABLE", resp.rows.get(0).getColVals().get(0).string_val);
160  }
161 
162  @Test
163  public void TestGetFunctions() throws ImpalaException {
164  // "sub%" should return functions subdate, substr and substring.
165  TMetadataOpRequest req = new TMetadataOpRequest();
166  req.opcode = TMetadataOpcode.GET_FUNCTIONS;
167  req.get_functions_req = new TGetFunctionsReq();
168  req.get_functions_req.setFunctionName("sub%");
169  TResultSet resp = execMetadataOp(req);
170 
171  // HiveServer2 GetFunctions() has 6 columns.
172  assertEquals(6, resp.schema.columns.size());
173  assertEquals(6, resp.rows.get(0).colVals.size());
174 
175  Set<String> fns = Sets.newHashSet();
176  for (TResultRow row: resp.rows) {
177  String fn = row.colVals.get(2).string_val.toLowerCase();
178  fns.add(fn);
179  }
180  assertEquals(3, fns.size());
181 
182  List<String> expectedResult = Lists.newArrayList();
183  expectedResult.add("subdate");
184  expectedResult.add("substr");
185  expectedResult.add("substring");
186  for (String fn: fns) {
187  assertTrue(fn + " not found", expectedResult.remove(fn));
188  }
189  }
190 
191  private TResultSet execMetadataOp(TMetadataOpRequest req)
192  throws ImpalaException {
193  return fe_.execHiveServer2MetadataOp(req);
194  }
195 }
TResultSet execMetadataOp(TMetadataOpRequest req)
PrimitiveType
Definition: types.h:27
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)