Skip to content

[CALCITE-7825] RelMdTableReferences does not account for tables in RexSubQuery expressions or Correlate - #5298

Open
ehds wants to merge 1 commit into
apache:mainfrom
ehds:hds/fix-table-references-missing-sub-query
Open

ehds wants to merge 1 commit into
apache:mainfrom
ehds:hds/fix-table-references-missing-sub-query

Conversation

@ehds

@ehds ehds commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7825
This is the follow-up to the #5288. @zzwqqq @vlsi Could you please review these changes?

RelMdTableReferences misses tables in RexSubQuery expressions in Project, Filter, Join, and Calc. It also has no Correlate handler, so rewriting a sub-query can change the result to null.
Other nodes also return null despite having determinable table references: Values should return an empty set, unary nodes should pass through their input references, and RepeatUnion and Combine should merge theirs.

Changes Proposed

Extend RelMdTableReferences to cover sub-queries, Correlate, and other nodes that previously returned null despite having determinable table references. Refactor Join to use the shared references extractor.

@ehds ehds changed the title fix [CALCITE-7825] RelMdTableReferences does not account for tables in RexSubQuery expressions or Correlate Sep 28, 2026
@sonarqubecloud

Copy link
Copy Markdown

assertNull(tableReferences);
}

@Test void testTableReferencesValues() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests are not easy for a human to review, I hope they are right

@mihaibudiu
mihaibudiu self-requested a review September 28, 2026 18:20
* Table references from TableFunctionScan.
*
* <p>Returns an empty set if there are no inputs, and {@code null} if the table
* references of any input cannot be determined. Tables accessed internally

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete this comment?


/** Table references from Snapshot. */
public @Nullable Set<RelTableRef> getTableReferences(Snapshot rel, RelMetadataQuery mq) {
return mq.getTableReferences(rel.getInput());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to analyze the period operand to?


/** Table references from Match. */
public @Nullable Set<RelTableRef> getTableReferences(Match rel, RelMetadataQuery mq) {
return mq.getTableReferences(rel.getInput());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to analyze the pattern and measures operands?

}

/** Table references from Spool. */
public @Nullable Set<RelTableRef> getTableReferences(Spool rel, RelMetadataQuery mq) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently there are still some classes not handled, like Window, Sort, TableModify

List<RelNode> inputs, RelMetadataQuery mq) {
List<RelNode> inputs, List<? extends RexNode> expressions, RelMetadataQuery mq) {
final List<RelNode> rels = new ArrayList<>(inputs);
final RexVisitorImpl<Void> visitor = new RexVisitorImpl<Void>(true) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can reuse the SubQueryCollector visitor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants