final SchemaPlus schema = findSchema(context.getRootSchema(), context.getNewDefaultSchema(),
Util.skipLast(table.names));
final String tableName = Util.last(table.names);
if (schema.getTable(tableName) == null) {
throw new RelConversionException(String.format("Table %s is not valid", Util.sepList(table.names, ".")));
}
SqlNode schemaCondition = null;
if (!isRootSchema(schema)) {
AbstractSchema drillSchema = getDrillSchema(schema);
schemaCondition = DrillParserUtil.createCondition(
new SqlIdentifier("TABLE_SCHEMA", SqlParserPos.ZERO),
SqlStdOperatorTable.EQUALS,
SqlLiteral.createCharString(drillSchema.getFullSchemaName(), CHARSET, SqlParserPos.ZERO)
);
}
SqlNode where = DrillParserUtil.createCondition(
new SqlIdentifier("TABLE_NAME", SqlParserPos.ZERO),
SqlStdOperatorTable.EQUALS,
SqlLiteral.createCharString(tableName, CHARSET, SqlParserPos.ZERO));
where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
SqlNode columnFilter = null;
if (node.getColumn() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier("COLUMN_NAME", SqlParserPos.ZERO),
SqlStdOperatorTable.EQUALS,
SqlLiteral.createCharString(node.getColumn().toString(), CHARSET, SqlParserPos.ZERO));
} else if (node.getColumnQualifier() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier("COLUMN_NAME", SqlParserPos.ZERO),
SqlStdOperatorTable.LIKE, node.getColumnQualifier());
}
where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, columnFilter);
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO),
fromClause, where, null, null, null, null, null, null);
} catch (Exception ex) {
throw new RelConversionException("Error while rewriting DESCRIBE query: " + ex.getMessage(), ex);
}
}