Search in sources :

Example 6 with AbstractSchema

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

the class SchemaUtilites method resolveToDrillSchemaInternal.

private static AbstractSchema resolveToDrillSchemaInternal(SchemaPlus defaultSchema, List<String> schemaPath, boolean checkMutable) {
    final SchemaPlus schema = findSchema(defaultSchema, schemaPath);
    if (schema == null) {
        throwSchemaNotFoundException(defaultSchema, SCHEMA_PATH_JOINER.join(schemaPath));
    }
    if (checkMutable && isRootSchema(schema)) {
        throw UserException.validationError().message("Root schema is immutable. Drill does not allow creating or deleting tables or views in the root schema. " + "Select a schema using 'USE schema' command.").build(logger);
    }
    final AbstractSchema drillSchema = unwrapAsDrillSchemaInstance(schema);
    if (checkMutable && !drillSchema.isMutable()) {
        throw UserException.validationError().message("Unable to create or drop objects. 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 7 with AbstractSchema

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

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 8 with AbstractSchema

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

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 9 with AbstractSchema

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

the class CreateTableHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
    final SqlCreateTable sqlCreateTable = unwrap(sqlNode, SqlCreateTable.class);
    final String originalTableName = sqlCreateTable.getName();
    final ConvertedRelNode convertedRelNode = validateAndConvert(sqlCreateTable.getQuery());
    final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
    final RelNode queryRelNode = convertedRelNode.getConvertedNode();
    final RelNode newTblRelNode = SqlHandlerUtil.resolveNewTableRel(false, sqlCreateTable.getFieldNames(), validatedRowType, queryRelNode);
    final DrillConfig drillConfig = context.getConfig();
    final AbstractSchema drillSchema = resolveSchema(sqlCreateTable, config.getConverter().getDefaultSchema(), drillConfig);
    final boolean checkTableNonExistence = sqlCreateTable.checkTableNonExistence();
    final String schemaPath = drillSchema.getFullSchemaName();
    // Check table creation possibility
    if (!checkTableCreationPossibility(drillSchema, originalTableName, drillConfig, context.getSession(), schemaPath, checkTableNonExistence)) {
        return DirectPlan.createDirectPlan(context, false, String.format("A table or view with given name [%s] already exists in schema [%s]", originalTableName, schemaPath));
    }
    final RelNode newTblRelNodeWithPCol = SqlHandlerUtil.qualifyPartitionCol(newTblRelNode, sqlCreateTable.getPartitionColumns());
    log("Calcite", newTblRelNodeWithPCol, logger, null);
    // Convert the query to Drill Logical plan and insert a writer operator on top.
    StorageStrategy storageStrategy = sqlCreateTable.isTemporary() ? StorageStrategy.TEMPORARY : new StorageStrategy(context.getOption(ExecConstants.PERSISTENT_TABLE_UMASK).string_val, false);
    // If we are creating temporary table, initial table name will be replaced with generated table name.
    // Generated table name is unique, UUID.randomUUID() is used for its generation.
    // Original table name is stored in temporary tables cache, so it can be substituted to generated one during querying.
    String newTableName = sqlCreateTable.isTemporary() ? context.getSession().registerTemporaryTable(drillSchema, originalTableName, drillConfig) : originalTableName;
    DrillRel drel = convertToDrel(newTblRelNodeWithPCol, drillSchema, newTableName, sqlCreateTable.getPartitionColumns(), newTblRelNode.getRowType(), storageStrategy);
    Prel prel = convertToPrel(drel, newTblRelNode.getRowType(), sqlCreateTable.getPartitionColumns());
    logAndSetTextPlan("Drill Physical", prel, logger);
    PhysicalOperator pop = convertToPop(prel);
    PhysicalPlan plan = convertToPlan(pop);
    log("Drill Plan", plan, logger);
    String message = String.format("Creating %s table [%s].", sqlCreateTable.isTemporary() ? "temporary" : "persistent", originalTableName);
    logger.info(message);
    return plan;
}
Also used : StorageStrategy(org.apache.drill.exec.store.StorageStrategy) PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlCreateTable(org.apache.drill.exec.planner.sql.parser.SqlCreateTable) WriterPrel(org.apache.drill.exec.planner.physical.WriterPrel) Prel(org.apache.drill.exec.planner.physical.Prel) ProjectAllowDupPrel(org.apache.drill.exec.planner.physical.ProjectAllowDupPrel) ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) RelNode(org.apache.calcite.rel.RelNode) DrillConfig(org.apache.drill.common.config.DrillConfig) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) DrillRel(org.apache.drill.exec.planner.logical.DrillRel)

Example 10 with AbstractSchema

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

the class DropTableHandler method getPlan.

/**
 * Function resolves the schema and invokes the drop method
 * (while IF EXISTS statement is used function invokes the drop method only if table exists).
 * Raises an exception if the schema is immutable.
 *
 * @param sqlNode - SqlDropTable (SQL parse tree of drop table [if exists] query)
 * @return - Single row indicating drop succeeded or table is not found while IF EXISTS statement is used,
 * raise exception otherwise
 */
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException {
    SqlDropTable dropTableNode = ((SqlDropTable) sqlNode);
    String originalTableName = dropTableNode.getName();
    SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
    List<String> tableSchema = dropTableNode.getSchema();
    DrillConfig drillConfig = context.getConfig();
    UserSession session = context.getSession();
    AbstractSchema temporarySchema = resolveToTemporarySchema(tableSchema, defaultSchema, drillConfig);
    boolean isTemporaryTable = session.isTemporaryTable(temporarySchema, drillConfig, originalTableName);
    if (isTemporaryTable) {
        session.removeTemporaryTable(temporarySchema, originalTableName, drillConfig);
    } else {
        AbstractSchema drillSchema = SchemaUtilites.resolveToMutableDrillSchema(defaultSchema, tableSchema);
        Table tableToDrop = SqlHandlerUtil.getTableFromSchema(drillSchema, originalTableName);
        if (tableToDrop == null || tableToDrop.getJdbcTableType() != Schema.TableType.TABLE) {
            if (dropTableNode.checkTableExistence()) {
                return DirectPlan.createDirectPlan(context, false, String.format("Table [%s] not found", originalTableName));
            } else {
                throw UserException.validationError().message("Table [%s] not found", originalTableName).build(logger);
            }
        }
        SqlHandlerUtil.dropTableFromSchema(drillSchema, originalTableName);
    }
    String message = String.format("%s [%s] dropped", isTemporaryTable ? "Temporary table" : "Table", originalTableName);
    logger.info(message);
    return DirectPlan.createDirectPlan(context, true, message);
}
Also used : Table(org.apache.calcite.schema.Table) SqlDropTable(org.apache.drill.exec.planner.sql.parser.SqlDropTable) DrillConfig(org.apache.drill.common.config.DrillConfig) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) UserSession(org.apache.drill.exec.rpc.user.UserSession) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlDropTable(org.apache.drill.exec.planner.sql.parser.SqlDropTable)

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