Skip to content

Conversation

@ggevay
Copy link
Contributor

@ggevay ggevay commented Feb 3, 2026

Fixes https://github.com/MaterializeInc/database-issues/issues/10067. (And unblocks #34881)

The problem was that we were simply always statically allowing subqueries in the ExprContext in sql_impl. This meant that we allowed subqueries there even if an outer context (such as the SET part of an UPDATE) didn't allow it. This PR fixes this by passing in the outer context, and inheriting whether that allows subqueries.

(Unfortunately, the error msg didn't come out nice, see the issue that is linked from the slt.)

Edit: The second commit tweaks how we handle errors in generate_column_casts. The issue was that that function relied on the buggy behavior of sql_impl that allowed subqueries without regard to the outer context, so that it can catch subqueries specifically by lower_uncorrelated. So, I've now had to explicitly make the outer context allow subqueries, so that lower_uncorrelated can keep catching it.

@ggevay ggevay added the A-ADAPTER Topics related to the ADAPTER layer label Feb 3, 2026
@def- def- mentioned this pull request Feb 3, 2026
5 tasks
@ggevay
Copy link
Contributor Author

ggevay commented Feb 3, 2026

The CI fail is caused by the PR; I'm investigating. Edit: Fixed in the second commit.

@ggevay ggevay force-pushed the sql_impl-ExprContext branch from e9c7987 to 6a790c3 Compare February 3, 2026 15:30
@ggevay ggevay marked this pull request as ready for review February 3, 2026 15:43
@ggevay ggevay requested a review from a team as a code owner February 3, 2026 15:43
@ggevay ggevay requested a review from aljoscha February 3, 2026 15:43
Copy link
Contributor

@def- def- left a comment

Choose a reason for hiding this comment

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

Definitely fixes the bug, thanks for adding a test

@def-
Copy link
Contributor

def- commented Feb 9, 2026

Any chance to merge this? Currently blocking introducing SQLancer++ testing

@ggevay ggevay requested a review from mtabebe February 10, 2026 18:02
# TODO: Bad error msg, because we handle ExprContext names badly when inner contexts are created by `with_name`. See
# https://github.com/MaterializeInc/database-issues/issues/10074
statement error db error: ERROR: static function definition \(or its outer context 'mz_internal\.mz_role_oid'\) does not allow subqueries
UPDATE t0 SET c1=HAS_SECRET_PRIVILEGE(t0.c1, '30 minutes') WHERE (t0.c1 BETWEEN t0.c1 AND t0.c1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be good to have a basic test that shows that the subquery is still allowed (e.g., outside the context of a set).

SELECT HAS_SECRET_PRIVILEGE(t0.c1, '30 minutes') FROM t0 LIMIT 0;

Additionally, maybe it would be good to have some examples of deeper nesting?

Copy link
Contributor

@mtabebe mtabebe left a comment

Choose a reason for hiding this comment

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

My reading of this makes sense, just some small test suggestions

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

Labels

A-ADAPTER Topics related to the ADAPTER layer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants