Skip to content

Commit 37b52d2

Browse files
committed
Inherit row type of partial views from existing sink
1 parent f2e4444 commit 37b52d2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

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

+15-2
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.schema.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;

0 commit comments

Comments
 (0)