Skip to content

[CALCITE-5261] UNION(ALL) inside of the CURSOR throws an exception while validating the query#5056

Merged
xuzifu666 merged 1 commit into
apache:mainfrom
xuzifu666:calcite-7229
Jul 2, 2026
Merged

[CALCITE-5261] UNION(ALL) inside of the CURSOR throws an exception while validating the query#5056
xuzifu666 merged 1 commit into
apache:mainfrom
xuzifu666:calcite-7229

Conversation

@xuzifu666

Copy link
Copy Markdown
Member

}
};
ns = createSetopNamespace(call, call);
} else {

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.

This part is not obvious to me, and it does not have coverage in the tests. Can it be reached? Could you also reduce the set case in the same way?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Here was reachable and in fact contained a bug: for a query like cursor(values ('a'), ('b')), it would hit the else branch and throw ClassCastException because of the cast (SqlSelect) query.

I refactored the code to handle the non-SELECT cases explicitly:

  1. SELECT keeps the original SelectScope + SelectNamespace behavior.
  2. UNION/INTERSECT/EXCEPT now use a ListScope + SetopNamespace.
  3. VALUES uses TableConstructorNamespace.
  4. WITH uses WithNamespace.
  5. Anything else throws Util.unexpected(...) (because can not use real sql to cover this condition).

Regarding reducing the set case the same way: I tried using parentScope directly for SET_QUERY, but it fails because parentScope is often a DelegatingScope, whose addChild throws UnsupportedOperationException. So the ListScope maybe still needed.

I also added test coverage for all branches in SqlValidatorTest.testCollectionTableWithCursorParam and a new testDeclareCursorUnexpectedKind.

@xuzifu666 xuzifu666 changed the title [CALCITE-5261] UNION(ALL) inside of the CURSUR throws an exception while validating the query [CALCITE-5261] UNION(ALL) inside of the CURSOR throws an exception while validating the query Jun 30, 2026

@mihaibudiu mihaibudiu left a comment

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.

Are there any quidem tests with cursor?
(If so, it would be nice to have one, but maybe it's not supported)

@xuzifu666

Copy link
Copy Markdown
Member Author

Are there any quidem tests with cursor? (If so, it would be nice to have one, but maybe it's not supported)

Yes, That was my plan at first, too. But Maybe there is no way to add a quidem test for CURSOR alone either.

The reason is that CURSOR itself is not executable SQL. It is just a constructor that only makes sense as an argument to a table function. In Calcite, the only built-in function that accepts a CURSOR argument is DEDUP, which is a mock function without an actual execution implementor.

Since quidem is an end-to-end execution test framework:

  • Without DEDUP, there is no function to wrap CURSOR(...), so the SQL is not syntactically valid.
  • With DEDUP, the execution phase fails because DEDUP has no Enumerable implementor.

I verified earlier that select * from table(dedup(cursor(select ...), 'ENAME')) passes validation and SqlToRel conversion, but fails at execution.

So CURSOR can only be covered in validator / SqlToRel converter unit tests for now, not in quidem. @mihaibudiu

@sonarqubecloud

sonarqubecloud Bot commented Jul 1, 2026

Copy link
Copy Markdown

@xuzifu666

Copy link
Copy Markdown
Member Author

Since this pr has been approved, I had squashed the commits. If there are no other comments, I will merge it.

@xuzifu666 xuzifu666 added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jul 1, 2026
@xuzifu666 xuzifu666 merged commit f229a2a into apache:main Jul 2, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants