@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException {
SqlDescribeTable node = unwrap(sqlNode, SqlDescribeTable.class);
try {
List<SqlNode> selectList = ImmutableList.of((SqlNode) new SqlIdentifier("COLUMN_NAME", SqlParserPos.ZERO),
new SqlIdentifier("DATA_TYPE", SqlParserPos.ZERO),
new SqlIdentifier("IS_NULLABLE", SqlParserPos.ZERO));
SqlNode fromClause = new SqlIdentifier(
ImmutableList.of("INFORMATION_SCHEMA", "COLUMNS"), null, SqlParserPos.ZERO, null);
final SqlIdentifier table = node.getTable();
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);