Search in sources :

Example 1 with AbstractSchema

use of org.apache.drill.exec.store.AbstractSchema in project drill by apache.

the class InfoSchemaRecordGenerator method visitTables.

/**
   * Visit the tables in the given schema. The
   * @param  schemaPath  the path to the given schema
   * @param  schema  the given schema
   */
public void visitTables(String schemaPath, SchemaPlus schema) {
    final AbstractSchema drillSchema = schema.unwrap(AbstractSchema.class);
    final List<String> tableNames = Lists.newArrayList(schema.getTableNames());
    for (Pair<String, ? extends Table> tableNameToTable : drillSchema.getTablesByNames(tableNames)) {
        final String tableName = tableNameToTable.getKey();
        final Table table = tableNameToTable.getValue();
        final TableType tableType = table.getJdbcTableType();
        // Visit the table, and if requested ...
        if (shouldVisitTable(schemaPath, tableName, tableType) && visitTable(schemaPath, tableName, table)) {
            // ... do for each of the table's fields.
            final RelDataType tableRow = table.getRowType(new JavaTypeFactoryImpl());
            for (RelDataTypeField field : tableRow.getFieldList()) {
                if (shouldVisitColumn(schemaPath, tableName, field.getName())) {
                    visitField(schemaPath, tableName, field);
                }
            }
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Table(org.apache.calcite.schema.Table) TableType(org.apache.calcite.schema.Schema.TableType) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 2 with AbstractSchema

use of org.apache.drill.exec.store.AbstractSchema in project drill by apache.

the class SchemaUtilites method resolveToMutableDrillSchema.

/**
   * Given reference to default schema in schema tree, search for schema with given <i>schemaPath</i>. Once a schema is
   * found resolve it into a mutable <i>AbstractDrillSchema</i> instance. A {@link UserException} is throws when:
   *   <li>No schema for given <i>schemaPath</i> is found.</li>
   *   <li>Schema found for given <i>schemaPath</i> is a root schema.</li>
   *   <li>Resolved schema is not a mutable schema.</li>
   *
   * @param defaultSchema default schema
   * @param schemaPath current schema path
   * @return mutable schema, exception otherwise
   */
public static AbstractSchema resolveToMutableDrillSchema(final SchemaPlus defaultSchema, List<String> schemaPath) {
    final SchemaPlus schema = findSchema(defaultSchema, schemaPath);
    if (schema == null) {
        throwSchemaNotFoundException(defaultSchema, SCHEMA_PATH_JOINER.join(schemaPath));
    }
    if (isRootSchema(schema)) {
        throw UserException.validationError().message("Root schema is immutable. Creating or dropping tables/views is not allowed in root schema." + "Select a schema using 'USE schema' command.").build(logger);
    }
    final AbstractSchema drillSchema = unwrapAsDrillSchemaInstance(schema);
    if (!drillSchema.isMutable()) {
        throw UserException.validationError().message("Unable to create or drop tables/views. Schema [%s] is immutable.", getSchemaPath(schema)).build(logger);
    }
    return drillSchema;
}
Also used : AbstractSchema(org.apache.drill.exec.store.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 3 with AbstractSchema

use of org.apache.drill.exec.store.AbstractSchema in project drill by axbaretto.

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);
}
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) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with AbstractSchema

use of org.apache.drill.exec.store.AbstractSchema in project drill by axbaretto.

the class InfoSchemaRecordGenerator method shouldVisitSchema.

protected boolean shouldVisitSchema(String schemaName, SchemaPlus schema) {
    try {
        // if the schema path is null or empty (try for root schema)
        if (schemaName == null || schemaName.isEmpty()) {
            return false;
        }
        AbstractSchema drillSchema = schema.unwrap(AbstractSchema.class);
        if (!drillSchema.showInInformationSchema()) {
            return false;
        }
        if (filter == null) {
            return true;
        }
        final Map<String, String> recordValues = ImmutableMap.of(CATS_COL_CATALOG_NAME, IS_CATALOG_NAME, SHRD_COL_TABLE_SCHEMA, schemaName, SCHS_COL_SCHEMA_NAME, schemaName);
        // For other two results (TRUE, INCONCLUSIVE) continue to visit the schema.
        return filter.evaluate(recordValues) != Result.FALSE;
    } catch (ClassCastException e) {
    // ignore and return true as this is not a Drill schema
    }
    return true;
}
Also used : AbstractSchema(org.apache.drill.exec.store.AbstractSchema)

Example 5 with AbstractSchema

use of org.apache.drill.exec.store.AbstractSchema in project drill by apache.

the class DescribeTableHandler method rewrite.

/**
 * Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.COLUMNS ...
 */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws ForemanSetupException {
    DrillSqlDescribeTable node = unwrap(sqlNode, DrillSqlDescribeTable.class);
    try {
        List<SqlNode> selectList = Arrays.asList(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_DATA_TYPE, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_IS_NULLABLE, SqlParserPos.ZERO));
        SqlNode fromClause = new SqlIdentifier(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.COLUMNS.name()), SqlParserPos.ZERO);
        SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
        List<String> schemaPathGivenInCmd = Util.skipLast(node.getTable().names);
        SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);
        if (schema == null) {
            SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.getSchemaPath(schemaPathGivenInCmd));
        }
        if (SchemaUtilites.isRootSchema(schema)) {
            throw UserException.validationError().message("No schema selected.").build(logger);
        }
        // find resolved schema path
        AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schema);
        String schemaPath = drillSchema.getFullSchemaName();
        String tableName = Util.last(node.getTable().names);
        if (schema.getTable(tableName) == null) {
            throw UserException.validationError().message("Unknown table [%s] in schema [%s]", tableName, schemaPath).build(logger);
        }
        SqlNode schemaCondition = null;
        if (!SchemaUtilites.isRootSchema(schema)) {
            schemaCondition = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(schemaPath, Util.getDefaultCharset().name(), SqlParserPos.ZERO));
        }
        SqlNode tableNameColumn = new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO);
        // if table names are case insensitive, wrap column values and condition in lower function
        if (!drillSchema.areTableNamesCaseSensitive()) {
            tableNameColumn = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, tableNameColumn);
            tableName = tableName.toLowerCase();
        }
        SqlNode where = DrillParserUtil.createCondition(tableNameColumn, SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableName, Util.getDefaultCharset().name(), SqlParserPos.ZERO));
        where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
        SqlNode columnFilter = null;
        if (node.getColumn() != null) {
            columnFilter = DrillParserUtil.createCondition(SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO)), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(node.getColumn().toString().toLowerCase(), Util.getDefaultCharset().name(), SqlParserPos.ZERO));
        } else if (node.getColumnQualifier() != null) {
            SqlNode columnQualifier = node.getColumnQualifier();
            SqlNode column = new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO);
            if (columnQualifier instanceof SqlCharStringLiteral) {
                NlsString conditionString = ((SqlCharStringLiteral) columnQualifier).getNlsString();
                columnQualifier = SqlCharStringLiteral.createCharString(conditionString.getValue().toLowerCase(), conditionString.getCharsetName(), columnQualifier.getParserPosition());
                column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
            }
            columnFilter = DrillParserUtil.createCondition(column, SqlStdOperatorTable.LIKE, columnQualifier);
        }
        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 UserException.planError(ex).message("Error while rewriting DESCRIBE query: %d", ex.getMessage()).build(logger);
    }
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) UserException(org.apache.drill.common.exceptions.UserException) ValidationException(org.apache.calcite.tools.ValidationException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlSelect(org.apache.calcite.sql.SqlSelect) DrillSqlDescribeTable(org.apache.drill.exec.planner.sql.parser.DrillSqlDescribeTable) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) NlsString(org.apache.calcite.util.NlsString) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

AbstractSchema (org.apache.drill.exec.store.AbstractSchema)18 SchemaPlus (org.apache.calcite.schema.SchemaPlus)10 Table (org.apache.calcite.schema.Table)6 RelDataType (org.apache.calcite.rel.type.RelDataType)5 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)4 SqlNode (org.apache.calcite.sql.SqlNode)4 SqlSelect (org.apache.calcite.sql.SqlSelect)4 DrillConfig (org.apache.drill.common.config.DrillConfig)4 RelNode (org.apache.calcite.rel.RelNode)3 SqlNodeList (org.apache.calcite.sql.SqlNodeList)3 PhysicalPlan (org.apache.drill.exec.physical.PhysicalPlan)3 PhysicalOperator (org.apache.drill.exec.physical.base.PhysicalOperator)3 DrillRel (org.apache.drill.exec.planner.logical.DrillRel)3 Prel (org.apache.drill.exec.planner.physical.Prel)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 TableType (org.apache.calcite.schema.Schema.TableType)2 SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)2 NlsString (org.apache.calcite.util.NlsString)2 ProjectAllowDupPrel (org.apache.drill.exec.planner.physical.ProjectAllowDupPrel)2