|
20 | 20 | package com.linkedin.hoptimator.jdbc;
|
21 | 21 |
|
22 | 22 | import java.io.Reader;
|
| 23 | +import java.sql.SQLException; |
23 | 24 | import java.util.ArrayList;
|
24 | 25 | import java.util.Arrays;
|
25 | 26 | import java.util.Collections;
|
|
42 | 43 | import org.apache.calcite.server.ServerDdlExecutor;
|
43 | 44 | import org.apache.calcite.sql.SqlCall;
|
44 | 45 | import org.apache.calcite.sql.SqlIdentifier;
|
| 46 | +import org.apache.calcite.sql.SqlKind; |
45 | 47 | import org.apache.calcite.sql.SqlNode;
|
46 | 48 | import org.apache.calcite.sql.SqlNodeList;
|
47 | 49 | import org.apache.calcite.sql.SqlSelect;
|
48 | 50 | import org.apache.calcite.sql.SqlUtil;
|
49 | 51 | import org.apache.calcite.sql.ddl.SqlCreateMaterializedView;
|
50 | 52 | import org.apache.calcite.sql.ddl.SqlCreateView;
|
| 53 | +import org.apache.calcite.sql.ddl.SqlDropObject; |
51 | 54 | import org.apache.calcite.sql.dialect.CalciteSqlDialect;
|
52 | 55 | import org.apache.calcite.sql.fun.SqlStdOperatorTable;
|
53 | 56 | import org.apache.calcite.sql.parser.SqlAbstractParserImpl;
|
@@ -94,6 +97,7 @@ public DdlExecutor getDdlExecutor() {
|
94 | 97 | // N.B. copy-pasted from Apache Calcite
|
95 | 98 |
|
96 | 99 | /** Executes a {@code CREATE VIEW} command. */
|
| 100 | + @Override |
97 | 101 | public void execute(SqlCreateView create, CalcitePrepare.Context context) {
|
98 | 102 | final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
|
99 | 103 | final SchemaPlus schemaPlus = pair.left.plus();
|
@@ -131,6 +135,7 @@ public void execute(SqlCreateView create, CalcitePrepare.Context context) {
|
131 | 135 | // N.B. copy-pasted from Apache Calcite
|
132 | 136 |
|
133 | 137 | /** Executes a {@code CREATE MATERIALIZED VIEW} command. */
|
| 138 | + @Override |
134 | 139 | public void execute(SqlCreateMaterializedView create, CalcitePrepare.Context context) {
|
135 | 140 | final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
|
136 | 141 | if (pair.left == null) {
|
@@ -212,6 +217,56 @@ public void execute(SqlCreateMaterializedView create, CalcitePrepare.Context con
|
212 | 217 | }
|
213 | 218 | }
|
214 | 219 |
|
| 220 | + // N.B. largely copy-pasted from Apache Calcite |
| 221 | + |
| 222 | + /** Executes {@code DROP FUNCTION}, {@code DROP TABLE}, {@code DROP MATERIALIZED VIEW}, {@code DROP TYPE}, |
| 223 | + * {@code DROP VIEW} commands. */ |
| 224 | + @Override |
| 225 | + public void execute(SqlDropObject drop, CalcitePrepare.Context context) { |
| 226 | + // The logic below is only applicable for DROP VIEW and DROP MATERIALIZED VIEW. |
| 227 | + if (!drop.getKind().equals(SqlKind.DROP_MATERIALIZED_VIEW) && !drop.getKind().equals(SqlKind.DROP_VIEW)) { |
| 228 | + super.execute(drop, context); |
| 229 | + return; |
| 230 | + } |
| 231 | + |
| 232 | + final Pair<CalciteSchema, String> pair = schema(context, false, drop.name); |
| 233 | + String viewName = pair.right; |
| 234 | + |
| 235 | + SchemaPlus schemaPlus = pair.left.plus(); |
| 236 | + String schemaName = schemaPlus.getName(); |
| 237 | + Table table = schemaPlus.getTable(viewName); |
| 238 | + if (table == null) { |
| 239 | + if (drop.ifExists) { |
| 240 | + return; |
| 241 | + } |
| 242 | + throw SqlUtil.newContextException(drop.name.getParserPosition(), RESOURCE.tableNotFound(viewName)); |
| 243 | + } |
| 244 | + |
| 245 | + final List<String> schemaPath = pair.left.path(null); |
| 246 | + List<String> viewPath = new ArrayList<>(); |
| 247 | + viewPath.addAll(schemaPath); |
| 248 | + viewPath.add(viewName); |
| 249 | + |
| 250 | + if (table instanceof MaterializedViewTable) { |
| 251 | + MaterializedViewTable materializedViewTable = (MaterializedViewTable) table; |
| 252 | + try { |
| 253 | + DeploymentService.delete(materializedViewTable.viewTable(), ViewTable.class, connectionProperties); |
| 254 | + } catch (SQLException e) { |
| 255 | + throw new RuntimeException("Cannot DROP MATERIALIZED VIEW in " + schemaName + ": " + e.getMessage(), e); |
| 256 | + } |
| 257 | + } else if (table instanceof ViewTable) { |
| 258 | + ViewTable viewTable = (ViewTable) table; |
| 259 | + try { |
| 260 | + DeploymentService.delete(viewTable, ViewTable.class, connectionProperties); |
| 261 | + } catch (SQLException e) { |
| 262 | + throw new RuntimeException("Cannot DROP VIEW in " + schemaName + ": " + e.getMessage(), e); |
| 263 | + } |
| 264 | + } else { |
| 265 | + throw new RuntimeException("Cannot DROP in " + schemaName + ": " + viewName + " is not a view."); |
| 266 | + } |
| 267 | + schemaPlus.removeTable(viewName); |
| 268 | + } |
| 269 | + |
215 | 270 | // N.B. copy-pasted from Apache Calcite
|
216 | 271 |
|
217 | 272 | /** Returns the schema in which to create an object. */
|
|
0 commit comments