15 package com.cloudera.impala.service;
17 import java.sql.DatabaseMetaData;
18 import java.util.HashSet;
19 import java.util.List;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
37 import com.cloudera.impala.thrift.TColumn;
38 import com.cloudera.impala.thrift.TColumnValue;
39 import com.cloudera.impala.thrift.TResultRow;
40 import com.cloudera.impala.thrift.TResultSet;
41 import com.cloudera.impala.thrift.TResultSetMetadata;
43 import com.google.common.collect.Lists;
51 private static final Logger
LOG = LoggerFactory.getLogger(MetadataOp.class);
54 private static final TColumnValue
NULL_COL_VAL =
new TColumnValue();
59 private final static TResultSetMetadata
GET_CATALOGS_MD =
new TResultSetMetadata();
60 private final static TResultSetMetadata
GET_COLUMNS_MD =
new TResultSetMetadata();
61 private final static TResultSetMetadata
GET_SCHEMAS_MD =
new TResultSetMetadata();
62 private final static TResultSetMetadata
GET_TABLES_MD =
new TResultSetMetadata();
63 private static final TResultSetMetadata
GET_TYPEINFO_MD =
new TResultSetMetadata();
86 GET_COLUMNS_MD.addToColumns(
88 GET_COLUMNS_MD.addToColumns(
90 GET_COLUMNS_MD.addToColumns(
92 GET_COLUMNS_MD.addToColumns(
94 GET_COLUMNS_MD.addToColumns(
96 GET_COLUMNS_MD.addToColumns(
98 GET_COLUMNS_MD.addToColumns(
100 GET_COLUMNS_MD.addToColumns(
102 GET_COLUMNS_MD.addToColumns(
104 GET_COLUMNS_MD.addToColumns(
106 GET_COLUMNS_MD.addToColumns(
108 GET_COLUMNS_MD.addToColumns(
110 GET_COLUMNS_MD.addToColumns(
112 GET_COLUMNS_MD.addToColumns(
114 GET_COLUMNS_MD.addToColumns(
116 GET_COLUMNS_MD.addToColumns(
118 GET_COLUMNS_MD.addToColumns(
120 GET_COLUMNS_MD.addToColumns(
122 GET_COLUMNS_MD.addToColumns(
124 GET_COLUMNS_MD.addToColumns(
126 GET_COLUMNS_MD.addToColumns(
128 GET_COLUMNS_MD.addToColumns(
130 GET_COLUMNS_MD.addToColumns(
133 GET_SCHEMAS_MD.addToColumns(
135 GET_SCHEMAS_MD.addToColumns(
138 GET_TABLES_MD.addToColumns(
140 GET_TABLES_MD.addToColumns(
142 GET_TABLES_MD.addToColumns(
144 GET_TABLES_MD.addToColumns(
146 GET_TABLES_MD.addToColumns(
149 GET_TYPEINFO_MD.addToColumns(
151 GET_TYPEINFO_MD.addToColumns(
153 GET_TYPEINFO_MD.addToColumns(
155 GET_TYPEINFO_MD.addToColumns(
157 GET_TYPEINFO_MD.addToColumns(
159 GET_TYPEINFO_MD.addToColumns(
161 GET_TYPEINFO_MD.addToColumns(
163 GET_TYPEINFO_MD.addToColumns(
165 GET_TYPEINFO_MD.addToColumns(
167 GET_TYPEINFO_MD.addToColumns(
169 GET_TYPEINFO_MD.addToColumns(
171 GET_TYPEINFO_MD.addToColumns(
173 GET_TYPEINFO_MD.addToColumns(
175 GET_TYPEINFO_MD.addToColumns(
177 GET_TYPEINFO_MD.addToColumns(
179 GET_TYPEINFO_MD.addToColumns(
181 GET_TYPEINFO_MD.addToColumns(
183 GET_TYPEINFO_MD.addToColumns(
186 GET_TABLE_TYPES_MD.addToColumns(
189 GET_FUNCTIONS_MD.addToColumns(
191 GET_FUNCTIONS_MD.addToColumns(
193 GET_FUNCTIONS_MD.addToColumns(
195 GET_FUNCTIONS_MD.addToColumns(
197 GET_FUNCTIONS_MD.addToColumns(
199 GET_FUNCTIONS_MD.addToColumns(
209 public List<String>
dbs = Lists.newArrayList();
216 public List<List<List<Column>>>
columns = Lists.newArrayList();
219 public List<List<Function>>
functions = Lists.newArrayList();
245 String schemaName, String tableName, String columnName, String functionName,
256 PatternMatcher schemaPattern = PatternMatcher.createJdbcPatternMatcher(schemaName);
257 PatternMatcher tablePattern = PatternMatcher.createJdbcPatternMatcher(tableName);
258 PatternMatcher columnPattern = PatternMatcher.createJdbcPatternMatcher(columnName);
259 PatternMatcher fnPattern = PatternMatcher.createJdbcPatternMatcher(functionName);
262 for (String dbName: fe.getDbNames(null, user)) {
263 if (!schemaPattern.
matches(dbName))
continue;
265 Db db = catalog.getDb(dbName);
266 if (db == null)
continue;
268 if (functionName != null) {
270 List<Function> fns = db.getFunctions(null, fnPattern);
271 result.functions.add(fns);
274 List<String> tableList = Lists.newArrayList();
275 List<List<Column>> tablesColumnsList = Lists.newArrayList();
276 for (String tabName: fe.getTableNames(db.
getName(),
"*", user)) {
277 if (!tablePattern.matches(tabName))
continue;
278 tableList.add(tabName);
279 List<Column> columns = Lists.newArrayList();
283 table = catalog.getTable(dbName, tabName);
287 if (table == null)
continue;
291 if (!table.isLoaded()) {
292 result.missingTbls.add(
new TableName(dbName, tabName));
294 for (
Column column: table.getColumnsInHiveOrder()) {
295 String colName = column.getName();
296 if (!columnPattern.matches(colName))
continue;
300 tablesColumnsList.add(columns);
302 result.dbs.add(dbName);
303 result.tableNames.add(tableList);
304 result.columns.add(tablesColumnsList);
331 String catalogName, String schemaName, String tableName, String columnName,
338 while (dbsMetadata == null || !dbsMetadata.
missingTbls.isEmpty()) {
340 schemaName, tableName, columnName, null, user);
341 if (!fe.requestTblLoadAndWait(dbsMetadata.
missingTbls)) {
342 LOG.info(
"Timed out waiting for missing tables. Load request will be retried.");
346 for (
int i = 0; i < dbsMetadata.dbs.size(); ++i) {
347 String dbName = dbsMetadata.dbs.get(i);
348 for (
int j = 0; j < dbsMetadata.tableNames.get(i).size(); ++j) {
349 String tabName = dbsMetadata.tableNames.get(i).
get(j);
350 for (
int k = 0; k < dbsMetadata.columns.get(i).
get(j).size(); ++k) {
351 Column column = dbsMetadata.columns.get(i).
get(j).get(k);
352 Type colType = column.getType();
353 TResultRow row =
new TResultRow();
354 row.colVals = Lists.newArrayList();
383 result.rows.add(row);
387 LOG.debug(
"Returning " + result.rows.size() +
" table columns");
403 schemaName, null, null, null, user);
405 for (
int i = 0; i < dbsMetadata.dbs.size(); ++i) {
406 String dbName = dbsMetadata.dbs.get(i);
407 TResultRow row =
new TResultRow();
408 row.colVals = Lists.newArrayList();
411 result.rows.add(row);
414 LOG.debug(
"Returning " + result.rows.size() +
" schemas");
426 String schemaName, String tableName, List<String> tableTypes,
User user)
432 if (tableTypes != null && !tableTypes.isEmpty()) {
433 boolean hasTableType =
false;
434 for (String tableType: tableTypes) {
435 if (tableType.toLowerCase().equals(
"table")) {
447 schemaName, tableName, null, null, user);
449 for (
int i = 0; i < dbsMetadata.dbs.size(); ++i) {
450 String dbName = dbsMetadata.dbs.get(i);
451 for (
int j = 0; j < dbsMetadata.tableNames.get(i).size(); ++j) {
452 String tabName = dbsMetadata.tableNames.get(i).
get(j);
453 TResultRow row =
new TResultRow();
454 row.colVals = Lists.newArrayList();
461 result.rows.add(row);
464 LOG.debug(
"Returning " + result.rows.size() +
" tables");
490 TResultRow row =
new TResultRow();
491 row.colVals = Lists.newArrayList();
509 String catalogName, String schemaName, String functionName,
519 schemaName, null, null, functionName, user);
520 for (List<Function> fns: dbsMetadata.
functions) {
542 Type type = ScalarType.createType(ptype);
543 TResultRow row =
new TResultRow();
544 row.colVals = Lists.newArrayList();
563 GET_TYPEINFO_RESULTS.add(row);
571 TResultRow row =
new TResultRow();
572 row.colVals = Lists.newArrayList();
574 GET_TABLE_TYPES_RESULTS.add(row);
582 TResultSet result =
new TResultSet();
583 result.rows = Lists.newArrayList();
584 result.schema = metadata;
590 TColumnValue colVal =
new TColumnValue();
592 colVal.setString_val(val);
598 TColumnValue colVal =
new TColumnValue();
600 colVal.setInt_val(val.intValue());
606 TColumnValue colVal =
new TColumnValue();
608 colVal.setBool_val(val);
617 return (pattern == null) || pattern.isEmpty() ||
618 (pattern.length() == 1 && pattern.equals(
"%"));
Integer getNumPrecRadix()
static final ScalarType STRING
static final ScalarType BOOLEAN
static final ScalarType SMALLINT
boolean matches(String candidate)
static final ScalarType INT
void toThrift(TColumnType container)