use of org.apache.drill.exec.planner.sql.parser.SqlShowTables in project drill by apache.
the class ShowTablesHandler method rewrite.
/** Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.`TABLES` ... */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
SqlShowTables node = unwrap(sqlNode, SqlShowTables.class);
List<SqlNode> selectList = Lists.newArrayList();
SqlNode fromClause;
SqlNode where;
// create select columns
selectList.add(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO));
selectList.add(new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO));
fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_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 = config.getConverter().getDefaultSchema();
if (SchemaUtilites.isRootSchema(schema)) {
// If the default schema is a root schema, throw an error to select a default schema
throw UserException.validationError().message("No default schema selected. Select a schema using 'USE schema' command").build(logger);
}
final AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schema);
tableSchema = drillSchema.getFullSchemaName();
}
final String charset = Util.getDefaultCharset().name();
where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_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(SHRD_COL_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), fromClause, where, null, null, null, null, null, null);
}
Aggregations