AHeise opened a new pull request, #30268:
URL: https://github.com/apache/flink/pull/30268

   ## What is the purpose of the change
   
   Two places narrow a table fetched from the catalog to one specific kind 
before checking what it is. Both fail with a `ClassCastException` for the 
"wrong" kind, although the code only needs the common 
`ResolvedCatalogBaseTable` API up to that point.
   
   **1. Scan reuse on a materialized table.** Planning a query that reads one 
materialized table twice with different projections fails when source reuse is 
enabled (the default), in both streaming and batch mode:
   
   ```sql
   SELECT T1.a, T1.b, T2.c FROM MyMaterializedTable T1, MyMaterializedTable T2 
WHERE T1.a = T2.a
   ```
   
   ```
   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)
   ```
   
   `DatabaseCalciteSchema` plans the scan against the table converted by 
`ContextResolvedTable#toCatalogTable`. `ScanReuser` merges the projections into 
a new `DynamicTableSourceSpec`, whose `loadOptionsFromCatalogTable` fetches the 
table from the `CatalogManager` again and gets the unconverted 
`ResolvedCatalogMaterializedTable`. The method only needs `getOptions()`, which 
is the same map for a materialized table and its converted table. The same 
reload also runs when a compiled plan with a materialized table source or sink 
is restored with the default `table.plan.restore.catalog-objects = ALL`.
   
   **2. `SHOW CREATE MATERIALIZED TABLE` on a table or view.**
   
   ```sql
   SHOW CREATE MATERIALIZED TABLE `source`  -- `source` is a regular table
   ```
   
   | | Result |
   |---|---|
   | Before | `ClassCastException: class 
org.apache.flink.table.catalog.ResolvedCatalogTable cannot be cast to class 
org.apache.flink.table.catalog.ResolvedCatalogMaterializedTable` |
   | After | `TableException: SHOW CREATE MATERIALIZED TABLE is only supported 
for materialized tables, but `default_catalog`.`default_database`.`source` is a 
table. Please use SHOW CREATE TABLE instead.` |
   
   `ShowCreateUtil#buildShowCreateMaterializedTableRow` already calls 
`validateTableKind`, but its parameter was typed 
`ResolvedCatalogMaterializedTable`, so the caller's implicit cast threw first. 
The parameter now takes `ResolvedCatalogBaseTable<?>` like 
`buildShowCreateTableRow`, and the cast happens after the check. `SHOW CREATE 
TABLE` and `SHOW CREATE VIEW` already behave this way.
   
   ## Brief change log
   
     - `DynamicTableSpecBase#loadOptionsFromCatalogTable` reads catalog options 
through `ResolvedCatalogBaseTable`.
     - `ShowCreateUtil#buildShowCreateMaterializedTableRow` takes 
`ResolvedCatalogBaseTable<?>` and casts after validating the table kind.
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - `ScanReuseTest#testProjectOnMaterializedTable` (streaming and batch): a 
self-join of a catalog materialized table with differing projections. It fails 
with the first `ClassCastException` without the fix; with the fix, both plans 
show a single reused scan with the merged projection.
     - `CatalogTableITCase#testCreateViewAndShowCreateTable`: `SHOW CREATE 
MATERIALIZED TABLE` on a table and on a view now report the kind error, 
matching the existing `SHOW CREATE TABLE`/`VIEW` assertions. Fails with the 
second `ClassCastException` without the fix.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with 
`@Public(Evolving)`: no (`ShowCreateUtil` is `@Internal`; the parameter is 
widened, so existing callers compile unchanged)
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): no
     - Anything that affects deployment or recovery: JobManager (and its 
components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? not applicable
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to