Skip to content

Commit

Permalink
Add ability to drop views (#111)
Browse files Browse the repository at this point in the history
* Add ability to drop views

* Add comments
  • Loading branch information
jogrogan authored Feb 21, 2025
1 parent 5288aad commit 9b89fbb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.linkedin.hoptimator.jdbc;

import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -42,12 +43,14 @@
import org.apache.calcite.server.ServerDdlExecutor;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.ddl.SqlCreateMaterializedView;
import org.apache.calcite.sql.ddl.SqlCreateView;
import org.apache.calcite.sql.ddl.SqlDropObject;
import org.apache.calcite.sql.dialect.CalciteSqlDialect;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlAbstractParserImpl;
Expand Down Expand Up @@ -94,6 +97,7 @@ public DdlExecutor getDdlExecutor() {
// N.B. copy-pasted from Apache Calcite

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

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

// N.B. largely copy-pasted from Apache Calcite

/** Executes {@code DROP FUNCTION}, {@code DROP TABLE}, {@code DROP MATERIALIZED VIEW}, {@code DROP TYPE},
* {@code DROP VIEW} commands. */
@Override
public void execute(SqlDropObject drop, CalcitePrepare.Context context) {
// The logic below is only applicable for DROP VIEW and DROP MATERIALIZED VIEW.
if (!drop.getKind().equals(SqlKind.DROP_MATERIALIZED_VIEW) && !drop.getKind().equals(SqlKind.DROP_VIEW)) {
super.execute(drop, context);
return;
}

final Pair<CalciteSchema, String> pair = schema(context, false, drop.name);
String viewName = pair.right;

SchemaPlus schemaPlus = pair.left.plus();
String schemaName = schemaPlus.getName();
Table table = schemaPlus.getTable(viewName);
if (table == null) {
if (drop.ifExists) {
return;
}
throw SqlUtil.newContextException(drop.name.getParserPosition(), RESOURCE.tableNotFound(viewName));
}

final List<String> schemaPath = pair.left.path(null);
List<String> viewPath = new ArrayList<>();
viewPath.addAll(schemaPath);
viewPath.add(viewName);

if (table instanceof MaterializedViewTable) {
MaterializedViewTable materializedViewTable = (MaterializedViewTable) table;
try {
DeploymentService.delete(materializedViewTable.viewTable(), ViewTable.class, connectionProperties);
} catch (SQLException e) {
throw new RuntimeException("Cannot DROP MATERIALIZED VIEW in " + schemaName + ": " + e.getMessage(), e);
}
} else if (table instanceof ViewTable) {
ViewTable viewTable = (ViewTable) table;
try {
DeploymentService.delete(viewTable, ViewTable.class, connectionProperties);
} catch (SQLException e) {
throw new RuntimeException("Cannot DROP VIEW in " + schemaName + ": " + e.getMessage(), e);
}
} else {
throw new RuntimeException("Cannot DROP in " + schemaName + ": " + viewName + " is not a view.");
}
schemaPlus.removeTable(viewName);
}

// N.B. copy-pasted from Apache Calcite

/** Returns the schema in which to create an object. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.linkedin.hoptimator.k8s.models.V1alpha1Pipeline;
import com.linkedin.hoptimator.k8s.models.V1alpha1PipelineList;
import com.linkedin.hoptimator.k8s.models.V1alpha1PipelineSpec;


public class K8sPipelineTable extends K8sTable<V1alpha1Pipeline, V1alpha1PipelineList, K8sPipelineTable.Row> {
Expand Down
19 changes: 19 additions & 0 deletions hoptimator-k8s/src/test/resources/k8s-ddl.id
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,22 @@ select * from ads.target2;

!ok

drop materialized view ads.target2;
(0 rows modified)

!update

drop view ads.target;
(0 rows modified)

!update

drop materialized view ads.audience2;
(0 rows modified)

!update

drop view ads.audience;
(0 rows modified)

!update
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down

0 comments on commit 9b89fbb

Please sign in to comment.