Skip to content

Commit

Permalink
Inherit row type of partial views from existing sink (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryannedolan authored Jan 24, 2025
1 parent fd156b0 commit 6ed28da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.rel.RelRoot;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.schema.Function;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.schema.Table;
import org.apache.calcite.schema.TranslatableTable;
import org.apache.calcite.schema.impl.ViewTable;
import org.apache.calcite.schema.impl.ViewTableMacro;
Expand Down Expand Up @@ -160,16 +162,27 @@ public void execute(SqlCreateMaterializedView create, CalcitePrepare.Context con
String database = ((Database) pair.left.schema).databaseName();

// Table does not exist. Create it.
RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
ViewTableMacro viewTableMacro = ViewTable.viewMacro(schemaPlus, sql, schemaPath, viewPath, false);
MaterializedViewTable materializedViewTable = new MaterializedViewTable(viewTableMacro);
RelDataType rowType = materializedViewTable.getRowType(new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT));
RelDataType viewRowType = materializedViewTable.getRowType(typeFactory);

// Suport "partial views", i.e. CREATE VIEW FOO$BAR, where the view name
// is "foo-bar" and the sink is just FOO.
String sinkName = viewName.split("\\$", 2)[0];
List<String> sinkPath = new ArrayList<>();
sinkPath.addAll(schemaPath);
sinkPath.add(sinkName);
sinkPath.add(sinkName);
Table sink = pair.left.plus().getTable(sinkName);

final RelDataType rowType;
if (sink != null) {
// For "partial views", the sink may already exist. Use the existing row type.
rowType = sink.getRowType(typeFactory);
} else {
// For normal views, we create the sink based on the view row type.
rowType = viewRowType;
}

// Plan a pipeline to materialize the view.
RelRoot root = HoptimatorDriver.convert(context, sql).root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public List<Engine> engines() {

@Override
public Table getTable(String name) {
return new HoptimatorJdbcTable((JdbcTable) super.getTable(name), convention);
JdbcTable table = (JdbcTable) super.getTable(name);
if (table == null) {
throw new RuntimeException("Could not find table " + name + " in database " + database);
}
return new HoptimatorJdbcTable(table, convention);
}

@Override
Expand Down

0 comments on commit 6ed28da

Please sign in to comment.