You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "MartijnVisser (via GitHub)" <gi...@apache.org> on 2026/07/14 12:38:16 UTC

[PR] [FLINK-36298][table-planner] Re-establish Calcite metadata provider on foreign threads [flink]

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

   ## What is the purpose of the change
   
   Fixes the intermittent `NullPointerException: metadataHandlerProvider` (Calcite `RelMetadataQueryBase`) that has recurred for years, only in flink-python (e.g. `StreamPandasConversionTests`).
   
   Root cause: Calcite keeps the metadata handler provider in a thread-local (`RelMetadataQueryBase.THREAD_PROVIDERS`), seeded only on the thread that builds the cached, shared `RelOptCluster` (`FlinkRelOptClusterFactory`). PyFlink runs optimization (`to_pandas()` -> `ArrowUtils.collectAsPandasDataFrame` -> `table.execute().collect()`) on a py4j gateway thread that can differ from the cluster-building thread, where the thread-local is unset, so the first metadata-handler lookup NPEs. The nondeterministic py4j thread assignment is why it is intermittent and PyFlink-only.
   
   The fix re-seeds the thread-local in `FlinkRelMetadataQuery.instance()`, the single funnel through which all metadata queries are obtained (directly, via `reuseOrCreate`, and via the cluster's metadata-query supplier), using the same provider `FlinkRelOptClusterFactory` installs.
   
   Notes for reviewers:
   - Scope: this addresses the sequential cross-thread case (the observed failures). Concurrent optimization on multiple threads against the same shared, non-thread-safe `RelMetadataQuery` is a separate, pre-existing concern that this change neither introduces nor fixes.
   - The thread-local is set and not cleared on foreign (pooled py4j) threads, by parity with Calcite's own `RelOptCluster.setMetadataProvider` seeding; the value is the stateless `FlinkDefaultRelMetadataProvider` singleton chain.
   - Design choice: healing the `instance()` funnel is provider-agnostic and robust to any foreign-thread caller, versus seeding at the PyFlink entry point, which is more invasive and PyFlink-specific.
   
   Likely duplicates of the same NPE: FLINK-33531, FLINK-38392.
   
   ## Brief change log
   
     - Guard `FlinkRelMetadataQuery.instance()` to re-seed `RelMetadataQueryBase.THREAD_PROVIDERS` when unset on the current thread
     - Cross-reference comment in `FlinkRelOptClusterFactory` documenting the single provider both sites rely on
     - Add `FlinkRelMetadataQueryThreadLocalTest`, a deterministic red/green reproduction of the cross-thread NPE
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - `FlinkRelMetadataQueryThreadLocalTest` builds the cluster on the main thread and issues a metadata query on a fresh worker thread; it fails with `NullPointerException: metadataHandlerProvider` without the fix and passes with it
     - `mvn test -Dtest=FlinkRelMd*` in `flink-table-planner` passes (315 tests)
   
   ## 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
     - 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 (Claude Code with Claude Opus 4.8)
   
   Generated-by: Claude Opus 4.8 (1M context)
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36298][table-planner] Re-establish Calcite metadata provider on foreign threads [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin merged PR #28738:
URL: https://github.com/apache/flink/pull/28738


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36298][table-planner] Re-establish Calcite metadata provider on foreign threads [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #28738:
URL: https://github.com/apache/flink/pull/28738#issuecomment-4969312816

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a6bdff882adb05ee5ad7239e2dcdc5921f02c6df",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "a6bdff882adb05ee5ad7239e2dcdc5921f02c6df",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a6bdff882adb05ee5ad7239e2dcdc5921f02c6df UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36298][table-planner] Re-establish Calcite metadata provider on foreign threads [flink]

Posted by "MartijnVisser (via GitHub)" <gi...@apache.org>.
MartijnVisser commented on PR #28738:
URL: https://github.com/apache/flink/pull/28738#issuecomment-4970896245

   @flinkbot run azure


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org