Search in sources :

Example 16 with AbstractSchema

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

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) {
    SqlDropTable dropTableNode = ((SqlDropTable) sqlNode);
    String originalTableName = DrillStringUtils.removeLeadingSlash(dropTableNode.getName());
    SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
    List<String> tableSchema = dropTableNode.getSchema();
    DrillConfig drillConfig = context.getConfig();
    UserSession session = context.getSession();
    AbstractSchema temporarySchema = SchemaUtilites.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);
        // TableType.OTHER started getting reported for H2 DB when it was upgraded to v2.
        if (tableToDrop == null || (tableToDrop.getJdbcTableType() != Schema.TableType.TABLE && tableToDrop.getJdbcTableType() != Schema.TableType.OTHER)) {
            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)

Example 17 with AbstractSchema

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

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 = DrillStringUtils.removeLeadingSlash(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 18 with AbstractSchema

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

the class DescribeSchemaHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException {
    SqlIdentifier schema = unwrap(sqlNode, SqlDescribeSchema.class).getSchema();
    SchemaPlus schemaPlus = SchemaUtilites.findSchema(config.getConverter().getDefaultSchema(), schema.names);
    if (schemaPlus == null) {
        throw UserException.validationError().message("Invalid schema name [%s]", Joiner.on(".").join(schema.names)).build(logger);
    }
    AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schemaPlus);
    StoragePlugin storagePlugin;
    try {
        storagePlugin = context.getStorage().getPlugin(drillSchema.getSchemaPath().get(0));
        if (storagePlugin == null) {
            throw new DrillRuntimeException(String.format("Unable to find storage plugin with the following name [%s].", drillSchema.getSchemaPath().get(0)));
        }
    } catch (PluginException e) {
        throw new DrillRuntimeException("Failure while retrieving storage plugin", e);
    }
    try {
        Map configMap = mapper.convertValue(storagePlugin.getConfig(), Map.class);
        if (storagePlugin instanceof FileSystemPlugin) {
            transformWorkspaces(drillSchema.getSchemaPath(), configMap);
        }
        String properties = mapper.writeValueAsString(configMap);
        return DirectPlan.createDirectPlan(context, new DescribeSchemaResult(drillSchema.getFullSchemaName(), properties));
    } catch (JsonProcessingException e) {
        throw new DrillRuntimeException("Error while trying to convert storage config to json string", e);
    }
}
Also used : FileSystemPlugin(org.apache.drill.exec.store.dfs.FileSystemPlugin) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SerializableString(com.fasterxml.jackson.core.SerializableString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SqlDescribeSchema(org.apache.calcite.sql.SqlDescribeSchema) StoragePlugin(org.apache.drill.exec.store.StoragePlugin)

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