|
29 | 29 | import org.apache.calcite.jdbc.CalciteSchema;
|
30 | 30 | import org.apache.calcite.rel.RelRoot;
|
31 | 31 | import org.apache.calcite.rel.type.RelDataType;
|
| 32 | +import org.apache.calcite.rel.type.RelDataTypeFactory; |
32 | 33 | import org.apache.calcite.rel.type.RelDataTypeSystem;
|
33 | 34 | import org.apache.calcite.schema.Function;
|
34 | 35 | import org.apache.calcite.schema.SchemaPlus;
|
| 36 | +import org.apache.calcite.schema.Table; |
35 | 37 | import org.apache.calcite.schema.TranslatableTable;
|
36 | 38 | import org.apache.calcite.schema.impl.ViewTable;
|
37 | 39 | import org.apache.calcite.schema.impl.ViewTableMacro;
|
@@ -160,16 +162,27 @@ public void execute(SqlCreateMaterializedView create, CalcitePrepare.Context con
|
160 | 162 | String database = ((Database) pair.left.schema).databaseName();
|
161 | 163 |
|
162 | 164 | // Table does not exist. Create it.
|
| 165 | + RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); |
163 | 166 | ViewTableMacro viewTableMacro = ViewTable.viewMacro(schemaPlus, sql, schemaPath, viewPath, false);
|
164 | 167 | MaterializedViewTable materializedViewTable = new MaterializedViewTable(viewTableMacro);
|
165 |
| - RelDataType rowType = materializedViewTable.getRowType(new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT)); |
| 168 | + RelDataType viewRowType = materializedViewTable.getRowType(typeFactory); |
166 | 169 |
|
167 | 170 | // Suport "partial views", i.e. CREATE VIEW FOO$BAR, where the view name
|
168 | 171 | // is "foo-bar" and the sink is just FOO.
|
169 | 172 | String sinkName = viewName.split("\\$", 2)[0];
|
170 | 173 | List<String> sinkPath = new ArrayList<>();
|
171 | 174 | 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 | + } |
173 | 186 |
|
174 | 187 | // Plan a pipeline to materialize the view.
|
175 | 188 | RelRoot root = HoptimatorDriver.convert(context, sql).root;
|
|
0 commit comments