Github user paul-rogers commented on a diff in the pull request:
https://github.com/apache/drill/pull/666#discussion_r94705117
--- Diff:
exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
---
@@ -74,36 +93,49 @@ public PhysicalPlan getPlan(SqlNode sqlNode) throws
ValidationException, RelConv
final SchemaPlus defaultSchema = context.getNewDefaultSchema();
final AbstractSchema drillSchema =
SchemaUtilites.resolveToMutableDrillSchema(defaultSchema,
createView.getSchemaPath());
- final String schemaPath = drillSchema.getFullSchemaName();
final View view = new View(newViewName, viewSql,
newViewRelNode.getRowType(),
SchemaUtilites.getSchemaPathAsList(defaultSchema));
- final Table existingTable =
SqlHandlerUtil.getTableFromSchema(drillSchema, newViewName);
-
- if (existingTable != null) {
- if (existingTable.getJdbcTableType() != Schema.TableType.VIEW) {
- // existing table is not a view
- throw UserException.validationError()
- .message("A non-view table with given name [%s] already
exists in schema [%s]",
- newViewName, schemaPath)
- .build(logger);
- }
-
- if (existingTable.getJdbcTableType() == Schema.TableType.VIEW &&
!createView.getReplace()) {
- // existing table is a view and create view has no "REPLACE"
clause
- throw UserException.validationError()
- .message("A view with given name [%s] already exists in
schema [%s]",
- newViewName, schemaPath)
- .build(logger);
- }
- }
+ validateViewCreationPossibility(drillSchema, newViewName,
createView.getReplace());
final boolean replaced = drillSchema.createView(view);
final String summary = String.format("View '%s' %s successfully in
'%s' schema",
- createView.getName(), replaced ? "replaced" : "created",
schemaPath);
+ createView.getName(), replaced ? "replaced" : "created",
drillSchema.getFullSchemaName());
return DirectPlan.createDirectPlan(context, true, summary);
}
+
+ /**
+ * Validates if view can be created in indicated schema:
+ * checks if object (persistent / temporary table) with the same exists
+ * in indicated schema, or if view exists but replace flag is not set.
+ *
+ * @param drillSchema schema where views will be created
+ * @param viewName view name
+ * @param replaceView replace view if exists
+ * @throws UserException if views can be created in indicated schema
+ */
+ private void validateViewCreationPossibility(AbstractSchema
drillSchema, String viewName, boolean replaceView) {
+ final String schemaPath = drillSchema.getFullSchemaName();
+ final Table existingTable =
SqlHandlerUtil.getTableFromSchema(drillSchema, viewName);
+
+ if ((existingTable != null && existingTable.getJdbcTableType() !=
Schema.TableType.VIEW) ||
--- End diff --
Yet another appearance of the temp table check...
Given how often we do these checks, should we factor them out into
functions?
```
void assertTable(List<String> name, Boolean exists, Boolean isTemp)
```
Where this one function checks if the thing is or is not a table, does or
does not exist (or doesn't care if exists is null), is or is not a temp table
(again with a doesn't care option).
Having simpler functions makes code review easier and will certainly make
changing the code easier as the same logic does not have to be modified in many
places.
The form of the function is not important; feel free to improve it. It is
the repetition that we want to factor out.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---