Skip to content

Commit 9b89fbb

Browse files
authored
Add ability to drop views (#111)
* Add ability to drop views * Add comments
1 parent 5288aad commit 9b89fbb

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

hoptimator-jdbc/src/main/java/com/linkedin/hoptimator/jdbc/HoptimatorDdlExecutor.java

+55
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.linkedin.hoptimator.jdbc;
2121

2222
import java.io.Reader;
23+
import java.sql.SQLException;
2324
import java.util.ArrayList;
2425
import java.util.Arrays;
2526
import java.util.Collections;
@@ -42,12 +43,14 @@
4243
import org.apache.calcite.server.ServerDdlExecutor;
4344
import org.apache.calcite.sql.SqlCall;
4445
import org.apache.calcite.sql.SqlIdentifier;
46+
import org.apache.calcite.sql.SqlKind;
4547
import org.apache.calcite.sql.SqlNode;
4648
import org.apache.calcite.sql.SqlNodeList;
4749
import org.apache.calcite.sql.SqlSelect;
4850
import org.apache.calcite.sql.SqlUtil;
4951
import org.apache.calcite.sql.ddl.SqlCreateMaterializedView;
5052
import org.apache.calcite.sql.ddl.SqlCreateView;
53+
import org.apache.calcite.sql.ddl.SqlDropObject;
5154
import org.apache.calcite.sql.dialect.CalciteSqlDialect;
5255
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
5356
import org.apache.calcite.sql.parser.SqlAbstractParserImpl;
@@ -94,6 +97,7 @@ public DdlExecutor getDdlExecutor() {
9497
// N.B. copy-pasted from Apache Calcite
9598

9699
/** Executes a {@code CREATE VIEW} command. */
100+
@Override
97101
public void execute(SqlCreateView create, CalcitePrepare.Context context) {
98102
final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
99103
final SchemaPlus schemaPlus = pair.left.plus();
@@ -131,6 +135,7 @@ public void execute(SqlCreateView create, CalcitePrepare.Context context) {
131135
// N.B. copy-pasted from Apache Calcite
132136

133137
/** Executes a {@code CREATE MATERIALIZED VIEW} command. */
138+
@Override
134139
public void execute(SqlCreateMaterializedView create, CalcitePrepare.Context context) {
135140
final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
136141
if (pair.left == null) {
@@ -212,6 +217,56 @@ public void execute(SqlCreateMaterializedView create, CalcitePrepare.Context con
212217
}
213218
}
214219

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+
215270
// N.B. copy-pasted from Apache Calcite
216271

217272
/** Returns the schema in which to create an object. */

hoptimator-k8s/src/main/java/com/linkedin/hoptimator/k8s/K8sPipelineTable.java

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import com.linkedin.hoptimator.k8s.models.V1alpha1Pipeline;
66
import com.linkedin.hoptimator.k8s.models.V1alpha1PipelineList;
7-
import com.linkedin.hoptimator.k8s.models.V1alpha1PipelineSpec;
87

98

109
public class K8sPipelineTable extends K8sTable<V1alpha1Pipeline, V1alpha1PipelineList, K8sPipelineTable.Row> {

hoptimator-k8s/src/test/resources/k8s-ddl.id

+19
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,22 @@ select * from ads.target2;
8888

8989
!ok
9090

91+
drop materialized view ads.target2;
92+
(0 rows modified)
93+
94+
!update
95+
96+
drop view ads.target;
97+
(0 rows modified)
98+
99+
!update
100+
101+
drop materialized view ads.audience2;
102+
(0 rows modified)
103+
104+
!update
105+
106+
drop view ads.audience;
107+
(0 rows modified)
108+
109+
!update

hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/pipeline/PipelineReconciler.java

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.time.Duration;
44
import java.util.Arrays;
5-
import java.util.Properties;
65

76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;

0 commit comments

Comments
 (0)