Search in sources :

Example 11 with SqlCharStringLiteral

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral 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 ForemanSetupException {
    SqlShowTables node = unwrap(sqlNode, SqlShowTables.class);
    List<SqlNode> selectList = Arrays.asList(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO));
    SqlNode fromClause = new SqlIdentifier(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.TABLES.name()), SqlParserPos.ZERO);
    SchemaPlus schemaPlus;
    if (node.getDb() != null) {
        List<String> schemaNames = node.getDb().names;
        schemaPlus = SchemaUtilites.findSchema(config.getConverter().getDefaultSchema(), schemaNames);
        if (schemaPlus == null) {
            throw UserException.validationError().message("Invalid schema name [%s]", SchemaUtilites.getSchemaPath(schemaNames)).build(logger);
        }
    } else {
        // If no schema is given in SHOW TABLES command, list tables from current schema
        schemaPlus = config.getConverter().getDefaultSchema();
    }
    if (SchemaUtilites.isRootSchema(schemaPlus)) {
        // 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);
    }
    AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schemaPlus);
    SqlNode where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(drillSchema.getFullSchemaName(), Util.getDefaultCharset().name(), SqlParserPos.ZERO));
    SqlNode filter = null;
    if (node.getLikePattern() != null) {
        SqlNode likePattern = node.getLikePattern();
        SqlNode column = new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO);
        // wrap columns name values and condition in lower function if case insensitive
        if (!drillSchema.areTableNamesCaseSensitive() && likePattern instanceof SqlCharStringLiteral) {
            NlsString conditionString = ((SqlCharStringLiteral) likePattern).getNlsString();
            likePattern = SqlCharStringLiteral.createCharString(conditionString.getValue().toLowerCase(), conditionString.getCharsetName(), likePattern.getParserPosition());
            column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
        }
        filter = DrillParserUtil.createCondition(column, 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);
}
Also used : SqlShowTables(org.apache.drill.exec.planner.sql.parser.SqlShowTables) SqlSelect(org.apache.calcite.sql.SqlSelect) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) NlsString(org.apache.calcite.util.NlsString) SqlNodeList(org.apache.calcite.sql.SqlNodeList) NlsString(org.apache.calcite.util.NlsString) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)10 SqlNode (org.apache.calcite.sql.SqlNode)5 SqlNodeList (org.apache.calcite.sql.SqlNodeList)5 NlsString (org.apache.calcite.util.NlsString)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3 SqlSelect (org.apache.calcite.sql.SqlSelect)3 UserException (org.apache.drill.common.exceptions.UserException)3 ForemanSetupException (org.apache.drill.exec.work.foreman.ForemanSetupException)3 IOException (java.io.IOException)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2 VersionMismatchException (org.apache.drill.exec.exception.VersionMismatchException)2 RemoteFunctionRegistry (org.apache.drill.exec.expr.fn.registry.RemoteFunctionRegistry)2 SqlDropFunction (org.apache.drill.exec.planner.sql.parser.SqlDropFunction)2 Jar (org.apache.drill.exec.proto.UserBitShared.Jar)2 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)2 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)1 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)1 AsExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression)1 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)1