public ShowTablesHandler(Planner planner, QueryContext context) { super(planner, context); }
/** Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.`TABLES` ... */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException{
SqlShowTables node = unwrap(sqlNode, SqlShowTables.class);
List<SqlNode> selectList = Lists.newArrayList();
SqlNode fromClause;
SqlNode where;
// create select columns
selectList.add(new SqlIdentifier("TABLE_SCHEMA", SqlParserPos.ZERO));
selectList.add(new SqlIdentifier("TABLE_NAME", SqlParserPos.ZERO));
fromClause = new SqlIdentifier(ImmutableList.of("INFORMATION_SCHEMA", "TABLES"), SqlParserPos.ZERO);
final SqlIdentifier db = node.getDb();
String tableSchema;
if (db != null) {
tableSchema = db.toString();
} else {
// If no schema is given in SHOW TABLES command, list tables from current schema
SchemaPlus schema = context.getNewDefaultSchema();
if (isRootSchema(schema)) {
// If the default schema is a root schema, throw an error to select a default schema
throw new RelConversionException("No schema selected. Select a schema using 'USE schema' command");
}
AbstractSchema drillSchema;
try {
drillSchema = getDrillSchema(schema);
} catch(Exception ex) {
throw new RelConversionException("Error while rewriting SHOW TABLES query: " + ex.getMessage(), ex);
}
tableSchema = drillSchema.getFullSchemaName();
}
where = DrillParserUtil.createCondition(
new SqlIdentifier("TABLE_SCHEMA", SqlParserPos.ZERO),
SqlStdOperatorTable.EQUALS,
SqlLiteral.createCharString(tableSchema, CHARSET, SqlParserPos.ZERO));
SqlNode filter = null;
final SqlNode likePattern = node.getLikePattern();
if (likePattern != null) {
filter = DrillParserUtil.createCondition(
new SqlIdentifier("TABLE_NAME", SqlParserPos.ZERO),
SqlStdOperatorTable.LIKE,
likePattern);
} else if (node.getWhereClause() != null) {
filter = node.getWhereClause();
}
where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, filter);
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO),