devozerov commented on a change in pull request #2371:
URL: https://github.com/apache/calcite/pull/2371#discussion_r593860128



##########
File path: server/src/main/java/org/apache/calcite/server/ServerDdlExecutor.java
##########
@@ -281,41 +281,54 @@ public void execute(SqlCreateFunction create,
    * {@code DROP VIEW} commands. */
   public void execute(SqlDropObject drop,
       CalcitePrepare.Context context) {
-    final List<String> path = context.getDefaultSchemaPath();
-    CalciteSchema schema = context.getRootSchema();
-    for (String p : path) {
-      schema = schema.getSubSchema(p, true);
-    }
-    final boolean existed;
+    final Pair<CalciteSchema, String> pair = schema(context, false, drop.name);
+    CalciteSchema schema = pair.left;
+    String objectName = pair.right;
+    assert objectName != null;
+
+    boolean existed;
     switch (drop.getKind()) {
     case DROP_TABLE:
     case DROP_MATERIALIZED_VIEW:
-      existed = schema.removeTable(drop.name.getSimple());
-      if (!existed && !drop.ifExists) {
+      Table materializedView = schema != null && drop.getKind() == 
SqlKind.DROP_MATERIALIZED_VIEW
+          ? schema.plus().getTable(objectName) : null;
+
+      existed = schema != null && schema.removeTable(objectName);
+      if (existed) {
+        if (materializedView != null) {
+          if (materializedView instanceof Wrapper) {
+            ((Wrapper) materializedView).maybeUnwrap(MaterializationKey.class)
+                .ifPresent(materializationKey -> {
+                  MaterializationService.instance()
+                      .removeMaterialization(materializationKey);
+                });
+          }
+        }
+      } else if (!drop.ifExists) {
         throw SqlUtil.newContextException(drop.name.getParserPosition(),
-            RESOURCE.tableNotFound(drop.name.getSimple()));
+            RESOURCE.tableNotFound(objectName));
       }
       break;
     case DROP_VIEW:
       // Not quite right: removes any other functions with the same name
-      existed = schema.removeFunction(drop.name.getSimple());
+      existed = schema != null && schema.removeFunction(objectName);

Review comment:
       Refactored. Note that for every object type we throw an exception with a 
different error message.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to