Skip to content

Commit 6ed28da

Browse files
authored
Inherit row type of partial views from existing sink (#94)
1 parent fd156b0 commit 6ed28da

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import org.apache.calcite.jdbc.CalciteSchema;
3030
import org.apache.calcite.rel.RelRoot;
3131
import org.apache.calcite.rel.type.RelDataType;
32+
import org.apache.calcite.rel.type.RelDataTypeFactory;
3233
import org.apache.calcite.rel.type.RelDataTypeSystem;
3334
import org.apache.calcite.schema.Function;
3435
import org.apache.calcite.schema.SchemaPlus;
36+
import org.apache.calcite.schema.Table;
3537
import org.apache.calcite.schema.TranslatableTable;
3638
import org.apache.calcite.schema.impl.ViewTable;
3739
import org.apache.calcite.schema.impl.ViewTableMacro;
@@ -160,16 +162,27 @@ public void execute(SqlCreateMaterializedView create, CalcitePrepare.Context con
160162
String database = ((Database) pair.left.schema).databaseName();
161163

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

167170
// Suport "partial views", i.e. CREATE VIEW FOO$BAR, where the view name
168171
// is "foo-bar" and the sink is just FOO.
169172
String sinkName = viewName.split("\\$", 2)[0];
170173
List<String> sinkPath = new ArrayList<>();
171174
sinkPath.addAll(schemaPath);
172-
sinkPath.add(sinkName);
175+
sinkPath.add(sinkName);
176+
Table sink = pair.left.plus().getTable(sinkName);
177+
178+
final RelDataType rowType;
179+
if (sink != null) {
180+
// For "partial views", the sink may already exist. Use the existing row type.
181+
rowType = sink.getRowType(typeFactory);
182+
} else {
183+
// For normal views, we create the sink based on the view row type.
184+
rowType = viewRowType;
185+
}
173186

174187
// Plan a pipeline to materialize the view.
175188
RelRoot root = HoptimatorDriver.convert(context, sql).root;

hoptimator-util/src/main/java/com/linkedin/hoptimator/util/planner/HoptimatorJdbcSchema.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public List<Engine> engines() {
5252

5353
@Override
5454
public Table getTable(String name) {
55-
return new HoptimatorJdbcTable((JdbcTable) super.getTable(name), convention);
55+
JdbcTable table = (JdbcTable) super.getTable(name);
56+
if (table == null) {
57+
throw new RuntimeException("Could not find table " + name + " in database " + database);
58+
}
59+
return new HoptimatorJdbcTable(table, convention);
5660
}
5761

5862
@Override

0 commit comments

Comments
 (0)