use of org.apache.drill.exec.planner.sql.parser.SqlDropTable 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) 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