Github user paul-rogers commented on a diff in the pull request: https://github.com/apache/drill/pull/666#discussion_r94689164 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/CreateTableHandler.java --- @@ -67,43 +71,64 @@ public CreateTableHandler(SqlHandlerConfig config, Pointer<String> textPlan) { @Override public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException { SqlCreateTable sqlCreateTable = unwrap(sqlNode, SqlCreateTable.class); - final String newTblName = sqlCreateTable.getName(); + 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 String temporaryWorkspace = context.getConfig().getString(ExecConstants.DEFAULT_TEMPORARY_WORKSPACE); + final AbstractSchema drillSchema = SchemaUtilites.resolveToMutableDrillSchema(config.getConverter().getDefaultSchema(), - sqlCreateTable.getSchemaPath()); - final String schemaPath = drillSchema.getFullSchemaName(); + getSchemaPath(sqlCreateTable, temporaryWorkspace)); + + boolean isTemporaryWorkspace = drillSchema.getFullSchemaName().equals(temporaryWorkspace); - if (SqlHandlerUtil.getTableFromSchema(drillSchema, newTblName) != null) { - throw UserException.validationError() - .message("A table or view with given name [%s] already exists in schema [%s]", newTblName, schemaPath) + if (sqlCreateTable.isTemporary() && !isTemporaryWorkspace) { + throw UserException + .validationError() + .message(String.format("Temporary tables are not allowed to be created " + + "outside of default temporary workspace [%s] defined by [%s] " + + "configuration parameter", temporaryWorkspace, ExecConstants.DEFAULT_TEMPORARY_WORKSPACE)) .build(logger); } - final RelNode newTblRelNodeWithPCol = SqlHandlerUtil.qualifyPartitionCol(newTblRelNode, sqlCreateTable.getPartitionColumns()); + checkDuplicatedObjectExistence(drillSchema, originalTableName, isTemporaryWorkspace, context.getSession()); - log("Calcite", newTblRelNodeWithPCol, logger, null); + 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. - DrillRel drel = convertToDrel(newTblRelNodeWithPCol, drillSchema, newTblName, sqlCreateTable.getPartitionColumns(), newTblRelNode.getRowType()); + StorageStrategy storageStrategy = sqlCreateTable.isTemporary() ? + StorageStrategy.TEMPORARY : StorageStrategy.PERSISTENT; + String newTableName = sqlCreateTable.isTemporary() ? --- End diff -- Perhaps a comment to explain how the new table name differs from the original table name. Reason: can the new name conflict with other table names in the query? If I had A, B and A is a temp table, can the new name turn out to be B which collides with the existing B?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---