Skip to content

Optimized processing of ORDER BY in window functions#34632

Merged
novikd merged 18 commits intoClickHouse:masterfrom
excitoon-favorites:optimizedprocessing
Jun 20, 2022
Merged

Optimized processing of ORDER BY in window functions#34632
novikd merged 18 commits intoClickHouse:masterfrom
excitoon-favorites:optimizedprocessing

Conversation

@excitoon
Copy link
Copy Markdown
Contributor

@excitoon excitoon commented Feb 16, 2022

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Optimized processing of ORDER BY in window functions.

Information about CI checks: https://clickhouse.tech/docs/en/development/continuous-integration/

Closes: #19289

@robot-clickhouse robot-clickhouse added the pr-improvement Pull request with some product improvements label Feb 16, 2022
@Enmk Enmk added the altinity label Feb 16, 2022
@novikd novikd self-assigned this Feb 21, 2022
@excitoon excitoon force-pushed the optimizedprocessing branch from 6013df4 to 154e61e Compare March 2, 2022 14:44
@excitoon excitoon marked this pull request as ready for review March 21, 2022 14:13
@alexey-milovidov
Copy link
Copy Markdown
Member

Please resolve conflicts.

@alexey-milovidov alexey-milovidov marked this pull request as draft April 4, 2022 22:11
@excitoon excitoon force-pushed the optimizedprocessing branch from 28be3af to 99fbfde Compare April 12, 2022 10:17
@excitoon excitoon force-pushed the optimizedprocessing branch 4 times, most recently from 0c1da3d to 9effb26 Compare April 25, 2022 07:18
@excitoon excitoon marked this pull request as ready for review April 27, 2022 09:50
@excitoon excitoon requested a review from novikd April 27, 2022 09:55
Copy link
Copy Markdown
Member

@novikd novikd left a comment

Choose a reason for hiding this comment

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

Looks good. Please, add a test with over (partition by ... order by ...).

}

void makeWindowDescriptionFromAST(const Context & context,
void ExpressionAnalyzer::makeWindowDescriptionFromAST(const Context & context_,
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.

You can remove the first parameter because it's a member function now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is still static member and we can't easily make that because basically lots of code for that class is made through static functions in corresponding cpp file. And same in two other cases in InterpreterSelectQuery.cpp.

}

static SortDescription getSortDescription(const ASTSelectQuery & query, ContextPtr context)
SortDescription InterpreterSelectQuery::getSortDescription(const ASTSelectQuery & query, ContextPtr context_)
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.

Here you can remove the second parameter.

@excitoon excitoon requested a review from novikd May 5, 2022 16:28
@filimonov
Copy link
Copy Markdown
Contributor

@filimonov
Copy link
Copy Markdown
Contributor

High memory usage for UNBOUNDED PRECEDING + UNBOUNDED FOLLOWING looks expected: we can not start steaming out any row before the last row in the window will be read.

So we need to store all the seen rows + all the whole window.

But in practice, we sometimes see almost 3.5x memory usage.

select number, lagInFrame(number) OVER (ORDER BY number ASC ROWS
                 BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS prev from test format Null;

-- Peak memory usage (for query): 11.81 GiB. -- while the data size is 3.2 Gb

That additional memory increase looks related to the block sizes (and their size prediction?)

set max_block_size = 256;
-- Peak memory usage (for query): 11.95 GiB.

SET max_block_size = 8192;
-- Peak memory usage (for query): 11.93 GiB.

set max_block_size = 65505; -- default
-- Peak memory usage (for query): 11.81 GiB.

SET max_block_size = 80000
-- Peak memory usage (for query): 9.58 GiB.

SET max_block_size = 90000
-- Peak memory usage (for query): 8.70 GiB.

SET max_block_size = 100000;
-- Peak memory usage (for query): 7.49 GiB.

SET max_block_size = 130000
-- Peak memory usage (for query): 6.50 GiB.

SET max_block_size = 524288
-- Peak memory usage (for query): 6.51 GiB.

Copy link
Copy Markdown
Member

@novikd novikd left a comment

Choose a reason for hiding this comment

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

LGTM, but needs to fix flaky check:

Stateless tests flaky check (address, actions) — Timeout, fail: 77, passed: 0

@excitoon excitoon force-pushed the optimizedprocessing branch from 5a16111 to e218a9b Compare May 23, 2022 08:18
@excitoon excitoon force-pushed the optimizedprocessing branch from e218a9b to 9f17929 Compare May 23, 2022 16:13
@excitoon excitoon requested a review from novikd May 24, 2022 07:34
@excitoon
Copy link
Copy Markdown
Contributor Author

+ for table in query_log zookeeper_log trace_log transactions_info_log
+ clickhouse-local --path /var/lib/clickhouse/ -q 'select * from system.query_log format TSVWithNamesAndTypes'
+ pigz
Code: 76. DB::Exception: Cannot lock file /var/lib/clickhouse/status. Another server instance in same directory is already running. (CANNOT_OPEN_FILE)

@novikd
Copy link
Copy Markdown
Member

novikd commented Jun 7, 2022

@Mergifyio update

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jun 7, 2022

update

❌ Pull request can't be updated with latest base branch changes

Details

Mergify needs the author permission to update the base branch of the pull request.
@excitoon-favorites needs to authorize modification on its head branch.
err-code: 9889F

@excitoon excitoon force-pushed the optimizedprocessing branch from 9f17929 to ca69c57 Compare June 7, 2022 17:23
@excitoon
Copy link
Copy Markdown
Contributor Author

excitoon commented Jun 7, 2022

Rebased.

@excitoon excitoon force-pushed the optimizedprocessing branch from ad5462b to ea156fe Compare June 20, 2022 02:15
@novikd
Copy link
Copy Markdown
Member

novikd commented Jun 20, 2022

Integration tests (asan, actions) [2/3] — fail: 21, passed: 619, flaky: 3
Integration tests (release, actions) [2/2] — fail: 1, passed: 993, flaky: 0
Stress test (debug, actions) — Backward compatibility check: OOM killer (or signal 9)

Unrelated

Stress test (undefined, actions) — Killed by signal (in clickhouse-server.log)

Created an issue #38247

@novikd novikd merged commit f6692c3 into ClickHouse:master Jun 20, 2022
excitoon pushed a commit to excitoon-favorites/ClickHouse that referenced this pull request Jun 24, 2022
excitoon pushed a commit to Altinity/ClickHouse that referenced this pull request Jun 24, 2022
excitoon pushed a commit to Altinity/ClickHouse that referenced this pull request Jun 27, 2022
excitoon pushed a commit to Altinity/ClickHouse that referenced this pull request Jul 14, 2022
arthurpassos added a commit to Altinity/ClickHouse that referenced this pull request Jul 15, 2022
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Jul 28, 2022
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Jul 28, 2022
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Aug 4, 2022
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Aug 4, 2022
@Enmk Enmk mentioned this pull request Sep 12, 2022
4 tasks
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Sep 13, 2022
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Sep 17, 2022
…4632, ClickHouse#37659 and ClickHouse#37882

Merge pull request ClickHouse#36944 from excitoon-favorites/better_exp_smooth
Merge pull request ClickHouse#34632 from excitoon-favorites/optimizedprocessing
Merge pull request ClickHouse#37659 from frew/master
Support `batch_delete` capability for GCS
Merge pull request ClickHouse#37882 from excitoon-favorites/nodeleteobjects
Fixes for objects removal in `S3ObjectStorage`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

window functions: use storage sorting key

6 participants