Skip to content

opt: eliminate nested subquery expression#161891

Open
DrewKimball wants to merge 1 commit intocockroachdb:masterfrom
DrewKimball:eliminate-subquery
Open

opt: eliminate nested subquery expression#161891
DrewKimball wants to merge 1 commit intocockroachdb:masterfrom
DrewKimball:eliminate-subquery

Conversation

@DrewKimball
Copy link
Copy Markdown
Collaborator

When one subquery is nested inside of another with no intervening expressions (other than the Values the nested subquery is embedded in), it is possible to replace the parent with the child. This may improve performance and make other optimizations possible for complex PL/pgSQL routines.

Informs #160836

Release note: None

When one subquery is nested inside of another with no intervening
expressions (other than the Values the nested subquery is embedded in),
it is possible to replace the parent with the child. This may improve
performance and make other optimizations possible for complex PL/pgSQL
routines.

Informs cockroachdb#160836

Release note: None
@DrewKimball DrewKimball requested a review from a team as a code owner January 27, 2026 21:28
@DrewKimball DrewKimball requested review from yuzefovich and removed request for a team January 27, 2026 21:28
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Nice, sounds reasonable to me. :lgtm:

@yuzefovich reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball).

Copy link
Copy Markdown
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Nice idea! :lgtm:

Might be good to add a couple more cases where the cardinality of the subquery is unknown and the subquery produces more than one row or more than one column.

Might also be good to add a case or two with VALUES () or (SELECT) where the rule shouldn't apply.

@michae2 reviewed 2 files and all commit messages, and made 3 comments.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @DrewKimball).


pkg/sql/opt/norm/rules/scalar.opt line 299 at r1 (raw file):

# single-row, single-column Values expression in the parent subquery.
[EliminateNestedSubquery, Normalize]
(Subquery (Values [ (Tuple [ $subquery:(Subquery) ]) ]))

Is the inner subquery ever allowed to produce more than one row or column?


pkg/sql/opt/norm/testdata/rules/scalar line 1285 at r1 (raw file):

 ├── fd: ()-->(3)
 └── tuple
      └── subquery

Should there be a max1-row operator somewhere in this plan?

(Oh, maybe we only add that if cardinality is > 1?)

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.

4 participants