Search in sources :

Example 1 with SqlDropTableMetadata

use of org.apache.drill.exec.planner.sql.parser.SqlDropTableMetadata in project drill by apache.

the class MetastoreDropTableMetadataHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException {
    if (!context.getOptions().getOption(ExecConstants.METASTORE_ENABLED_VALIDATOR)) {
        throw UserException.validationError().message("Running ANALYZE TABLE DROP command when Metastore is disabled (`metastore.enabled` is set to false)").build(logger);
    }
    SqlDropTableMetadata dropTableMetadata = unwrap(sqlNode, SqlDropTableMetadata.class);
    AbstractSchema drillSchema = SchemaUtilites.resolveToDrillSchema(config.getConverter().getDefaultSchema(), dropTableMetadata.getSchemaPath());
    List<String> schemaPath = drillSchema.getSchemaPath();
    String pluginName = schemaPath.get(0);
    String workspaceName = Strings.join(schemaPath.subList(1, schemaPath.size()), AbstractSchema.SCHEMA_SEPARATOR);
    TableInfo tableInfo = TableInfo.builder().name(dropTableMetadata.getName()).storagePlugin(pluginName).workspace(workspaceName).build();
    try {
        Tables tables = context.getMetastoreRegistry().get().tables();
        MetastoreTableInfo metastoreTableInfo = tables.basicRequests().metastoreTableInfo(tableInfo);
        if (!metastoreTableInfo.isExists()) {
            if (dropTableMetadata.checkMetadataExistence()) {
                throw UserException.validationError().message("Metadata for table [%s] not found.", dropTableMetadata.getName()).build(logger);
            }
            return DirectPlan.createDirectPlan(context, false, String.format("Metadata for table [%s] does not exist.", dropTableMetadata.getName()));
        }
        tables.modify().delete(Delete.builder().metadataType(MetadataType.ALL).filter(tableInfo.toFilter()).build()).execute();
    } catch (MetastoreException e) {
        logger.error("Error when dropping metadata for table {}", dropTableMetadata.getName(), e);
        return DirectPlan.createDirectPlan(context, false, e.getMessage());
    }
    return DirectPlan.createDirectPlan(context, true, String.format("Metadata for table [%s] dropped.", dropTableMetadata.getName()));
}
Also used : MetastoreTableInfo(org.apache.drill.metastore.components.tables.MetastoreTableInfo) SqlDropTableMetadata(org.apache.drill.exec.planner.sql.parser.SqlDropTableMetadata) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) MetastoreException(org.apache.drill.metastore.exceptions.MetastoreException) Tables(org.apache.drill.metastore.components.tables.Tables) TableInfo(org.apache.drill.metastore.metadata.TableInfo) MetastoreTableInfo(org.apache.drill.metastore.components.tables.MetastoreTableInfo)

Aggregations

SqlDropTableMetadata (org.apache.drill.exec.planner.sql.parser.SqlDropTableMetadata)1 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)1 MetastoreTableInfo (org.apache.drill.metastore.components.tables.MetastoreTableInfo)1 Tables (org.apache.drill.metastore.components.tables.Tables)1 MetastoreException (org.apache.drill.metastore.exceptions.MetastoreException)1 TableInfo (org.apache.drill.metastore.metadata.TableInfo)1