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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations