Arvid Heise created FLINK-41827:
-----------------------------------
Summary: ClassCastException when a materialized table is scanned
twice with different projections
Key: FLINK-41827
URL: https://issues.apache.org/jira/browse/FLINK-41827
Project: Flink
Issue Type: Improvement
Components: Table SQL / Planner
Affects Versions: 2.4.0
Reporter: Arvid Heise
Assignee: Arvid Heise
Planning a query that reads the same materialized table twice with different
projections fails when source reuse is enabled (the default):
{code:sql}
SELECT T1.a, T1.b, T2.c FROM MyMaterializedTable T1, MyMaterializedTable T2
WHERE T1.a = T2.a
{code}
{noformat}
java.lang.ClassCastException: class
org.apache.flink.table.catalog.ResolvedCatalogMaterializedTable cannot be cast
to class org.apache.flink.table.catalog.ResolvedCatalogTable
at
org.apache.flink.table.planner.plan.nodes.exec.spec.DynamicTableSpecBase.loadOptionsFromCatalogTable(DynamicTableSpecBase.java:50)
at
org.apache.flink.table.planner.plan.nodes.exec.spec.DynamicTableSourceSpec.getTableSource(DynamicTableSourceSpec.java:96)
at
org.apache.flink.table.planner.plan.nodes.exec.spec.DynamicTableSourceSpec.getScanTableSource(DynamicTableSourceSpec.java:122)
at
org.apache.flink.table.planner.plan.reuse.ScanReuser.reuseDuplicatedScan(ScanReuser.java:213)
at
org.apache.flink.table.planner.plan.reuse.SubplanReuser$.reuseDuplicatedSubplan(SubplanReuser.scala:68)
at
org.apache.flink.table.planner.plan.optimize.CommonSubGraphBasedOptimizer.optimize(CommonSubGraphBasedOptimizer.scala:111)
{noformat}
It fails in both streaming and batch mode, for any parent operator (join, UNION
ALL).
*Cause*
* DatabaseCalciteSchema#getTable converts a materialized table to a plain table
via ContextResolvedTable#toCatalogTable, so the scan is planned against a
ResolvedCatalogTable.
* ScanReuser merges the differing projections into a new DynamicTableSourceSpec
without a cached source. DynamicTableSourceSpec#getTableSource then calls
DynamicTableSpecBase#loadOptionsFromCatalogTable, which fetches the table from
the CatalogManager again. This time the catalog returns the unconverted
ResolvedCatalogMaterializedTable.
* loadOptionsFromCatalogTable casts the result to ResolvedCatalogTable,
although it only needs getOptions().
*Behavior*
* Two scans with different projections: ClassCastException
* Two scans with the same projection, or a single scan: OK
* table.optimizer.reuse-source-enabled = false: OK
* Same query on a regular table: OK
*Proposed fix*
Read the options through ResolvedCatalogBaseTable in
DynamicTableSpecBase#loadOptionsFromCatalogTable. A materialized table's
options are the same map as those of its converted table. Add a ScanReuseTest
case with a catalog materialized table, for both streaming and batch.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)