Skip to content

Remove useless scalar subqueries evaluation - #21414

Closed
amosbird wants to merge 3 commits into
ClickHouse:masterfrom
amosbird:withscalar2
Closed

amosbird wants to merge 3 commits into
ClickHouse:masterfrom
amosbird:withscalar2

Conversation

@amosbird

@amosbird amosbird commented Mar 3, 2021

Copy link
Copy Markdown
Collaborator

I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Cache scalar subqueries during query analysis. This should fix #21067

Detailed description / Documentation draft:

.

@robot-clickhouse robot-clickhouse added the pr-performance Pull request with some performance improvements label Mar 3, 2021
@amosbird amosbird changed the title Also cache scalar subqueries for analysis Remove useless scalar subqueries evaluation Mar 3, 2021
@amosbird
amosbird force-pushed the withscalar2 branch 2 times, most recently from 9775384 to e0b3f64 Compare March 8, 2021 16:21
@amosbird

amosbird commented Mar 9, 2021

Copy link
Copy Markdown
Collaborator Author

Integration test errors look irrelevant.

Comment thread src/Interpreters/TreeRewriter.cpp Resolved
Comment thread src/Parsers/ASTWithAlias.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This flag looks a bit foreign for ASTWithAlias member. Maybe we can store flags separately? For example build mapping from ast pointer to bool in QueryNormalizer, save it in visitor's data, and perform lookup in removeUnusedAliasesFromWithClause.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good idea.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, we have to record this used flag inside ASTWithAlias. Because there are many places that analysis/normalize the query ast multiple times. Further analysis will remove used with elements because they are already expanded and are not considered as used anymore.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, we will delete more than actually need or do not delete unused?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

we will delete more than actually need because we cannot tell whether it's expanded or really unused.

Comment thread src/Interpreters/TreeRewriter.cpp Outdated

@vdimir vdimir Mar 26, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need also call removeUnusedAliasesFromWithClause in TreeRewriter::analyze after normalize ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Nope, it's for ASTSelect WITH clause exclusively.

@vdimir
vdimir marked this pull request as draft April 29, 2021 10:21
@vdimir

vdimir commented Apr 29, 2021

Copy link
Copy Markdown
Member

Desided to postpone due to finding cleaner solution

@amosbird
amosbird marked this pull request as ready for review April 29, 2021 13:17
Comment thread src/Interpreters/TreeRewriter.cpp Resolved
Comment thread src/Interpreters/QueryNormalizer.h Resolved
Comment thread src/Parsers/ASTWithAlias.h Resolved
@vdimir

vdimir commented Apr 30, 2021

Copy link
Copy Markdown
Member

@amosbird 01414_push_predicate_when_contains_with_clause failed :(
https://clickhouse-test-reports.s3.yandex.net/21414/51444be49f194ecd3d115d5527069862a8445d3f/fast_test/test_log.txt

Error
2021-04-29 17:23:54 01523_interval_operator_support_string_literal:                         [ OK ]
2021-04-29 17:23:55 01414_push_predicate_when_contains_with_clause:                         [ FAIL ] - result differs with reference:
2021-04-29 17:23:55 --- /ClickHouse/tests/queries/0_stateless/01414_push_predicate_when_contains_with_clause.reference	2021-04-29 16:54:02.829357008 +0300
2021-04-29 17:23:55 +++ /ClickHouse/tests/queries/0_stateless/01414_push_predicate_when_contains_with_clause.stdout	2021-04-29 17:23:55.172527509 +0300
2021-04-29 17:23:55 @@ -5,10 +5,9 @@
2021-04-29 17:23:55      square_number
2021-04-29 17:23:55  FROM 
2021-04-29 17:23:55  (
2021-04-29 17:23:55 -    WITH number * 2 AS square_number
2021-04-29 17:23:55      SELECT
2021-04-29 17:23:55          number,
2021-04-29 17:23:55 -        square_number
2021-04-29 17:23:55 +        number * 2 AS square_number
2021-04-29 17:23:55      FROM numbers_indexed
2021-04-29 17:23:55      WHERE number = 999
2021-04-29 17:23:55  ) AS squares
2021-04-29 17:23:55 

@amosbird

Copy link
Copy Markdown
Collaborator Author

@amosbird 01414_push_predicate_when_contains_with_clause failed :(
https://clickhouse-test-reports.s3.yandex.net/21414/51444be49f194ecd3d115d5527069862a8445d3f/fast_test/test_log.txt

Error

2021-04-29 17:23:54 01523_interval_operator_support_string_literal:                         [ OK ]
2021-04-29 17:23:55 01414_push_predicate_when_contains_with_clause:                         [ FAIL ] - result differs with reference:
2021-04-29 17:23:55 --- /ClickHouse/tests/queries/0_stateless/01414_push_predicate_when_contains_with_clause.reference	2021-04-29 16:54:02.829357008 +0300
2021-04-29 17:23:55 +++ /ClickHouse/tests/queries/0_stateless/01414_push_predicate_when_contains_with_clause.stdout	2021-04-29 17:23:55.172527509 +0300
2021-04-29 17:23:55 @@ -5,10 +5,9 @@
2021-04-29 17:23:55      square_number
2021-04-29 17:23:55  FROM 
2021-04-29 17:23:55  (
2021-04-29 17:23:55 -    WITH number * 2 AS square_number
2021-04-29 17:23:55      SELECT
2021-04-29 17:23:55          number,
2021-04-29 17:23:55 -        square_number
2021-04-29 17:23:55 +        number * 2 AS square_number
2021-04-29 17:23:55      FROM numbers_indexed
2021-04-29 17:23:55      WHERE number = 999
2021-04-29 17:23:55  ) AS squares
2021-04-29 17:23:55 

Interesting. Let's find out why it removes the alias.

@amosbird

amosbird commented May 3, 2021

Copy link
Copy Markdown
Collaborator Author

ya.make failure is not related to this PR

@amosbird

amosbird commented May 3, 2021

Copy link
Copy Markdown
Collaborator Author

Intergration tests look weird.

@amosbird
amosbird force-pushed the withscalar2 branch 2 times, most recently from 50063fa to bc96c5f Compare June 6, 2021 14:15
@alexey-milovidov

Copy link
Copy Markdown
Member

Need to merge with master.

@vdimir

vdimir commented Jul 13, 2021

Copy link
Copy Markdown
Member

Sorry for delay, lost last commit with real fix.

I've resolved conflicts, rebased on master. Let's look at tests.

@amosbird

Copy link
Copy Markdown
Collaborator Author

Let's close it in favor of #23194.

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

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance regression in 21.2 - query analysis time explosion with several WITH and JOINs

5 participants