Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: add EliminateConstValueSubquery normalization rule #107133

Merged
merged 1 commit into from Jul 21, 2023

Conversation

mgartner
Copy link
Collaborator

The EliminateConstValueSubquery normalization rule replaces a subquery
with a constant value when the subquery's input is a single-rows,
single-column Values expression with a constant value. This enables
further optimization of the query.

Fixes #104218

Release note (performance improvement): The optimizer now produces more
efficient query plans in some cases for queries with subqueries and
user-defined functions.

@mgartner mgartner requested a review from a team as a code owner July 18, 2023 23:29
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm: Nice idea, I'm surprised we haven't done this before.

Reviewed 9 of 9 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner and @rharding6373)


pkg/sql/opt/norm/rules/subquery.opt line 10 at r1 (raw file):

[EliminateConstValueSubquery, Normalize]
(Subquery
    (Values $rows:*) &

I think you could move the logic into optgen like this if you want:

(Values [ (Tuple [ $constant(Const) ]) ])

@mgartner mgartner force-pushed the 104218-subquery-elimination branch 2 times, most recently from dea4c2a to 2f700fb Compare July 19, 2023 23:16
Copy link
Collaborator Author

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @DrewKimball and @rharding6373)


pkg/sql/opt/norm/rules/subquery.opt line 10 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

I think you could move the logic into optgen like this if you want:

(Values [ (Tuple [ $constant(Const) ]) ])

Brilliant. That's much better.


pkg/sql/logictest/testdata/logic_test/apply_join line 470 at r2 (raw file):

            (VALUES (tab_54747.col_95055)) AS tab_54752 (col_95061)
          WHERE
            (SELECT random()::INT) < tab_54752.col_95061

The new rule was eliminating a subquery, which broken this test. So I changed this to a non-constant so that the rule won't fire.

@mgartner mgartner force-pushed the 104218-subquery-elimination branch 3 times, most recently from 7b1ff6e to 0f90deb Compare July 19, 2023 23:24
Copy link
Collaborator

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

:lgtm: Very nice!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @DrewKimball)

@mgartner mgartner force-pushed the 104218-subquery-elimination branch from 0f90deb to 2170779 Compare July 20, 2023 15:20
@blathers-crl
Copy link

blathers-crl bot commented Jul 20, 2023

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl bot added the O-community Originated from the community label Jul 20, 2023
The EliminateConstValueSubquery normalization rule replaces a subquery
with a constant value when the subquery's input is a single-rows,
single-column Values expression with a constant value. This enables
further optimization of the query.

Fixes cockroachdb#104218

Release note (performance improvement): The optimizer now produces more
efficient query plans in some cases for queries with  subqueries and
user-defined functions.
@mgartner mgartner force-pushed the 104218-subquery-elimination branch from 2170779 to bbe36ee Compare July 20, 2023 20:30
@mgartner
Copy link
Collaborator Author

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 21, 2023

Build succeeded:

@craig craig bot merged commit e15b5bc into cockroachdb:master Jul 21, 2023
7 checks passed
@mgartner mgartner deleted the 104218-subquery-elimination branch July 21, 2023 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-community Originated from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: query using UDF does not use correct secondary index
4 participants