Skip to content

Bugfixes and performance improvements for parallel_hash - #37648

Merged
vdimir merged 22 commits into
ClickHouse:masterfrom
vdimir:enable_parallel_hash
Jul 13, 2022
Merged

vdimir merged 22 commits into
ClickHouse:masterfrom
vdimir:enable_parallel_hash

Conversation

@vdimir

@vdimir vdimir commented May 30, 2022 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

  • Bugfixes and performance improvements for parallel_hash

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

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-improvement Pull request with some product improvements label May 30, 2022
@amosbird

Copy link
Copy Markdown
Collaborator

I've observed significant slowdown when testing parallel_hash with ssb q1

-- ssb_q1 rewrite 
SELECT sum(`4` * `5`) AS `8`
FROM
(
    SELECT
        LO_ORDERDATE AS `3`,
        LO_EXTENDEDPRICE AS `4`,
        LO_DISCOUNT AS `5`,
        LO_QUANTITY AS `6`
    FROM lineorder
)
INNER JOIN
(
    SELECT
        D_DATEKEY AS `0`,
        D_YEAR AS `1`
    FROM date
    WHERE `1` = 1993
) ON `3` = `0`
SETTINGS max_threads = 16

ssb 100g. hash: 0.353 sec , parallel_hash: 1.167 sec.

@vdimir
vdimir marked this pull request as draft May 30, 2022 14:58
@lgbo-ustc

Copy link
Copy Markdown
Contributor

@amosbird

amosbird commented Jun 1, 2022

Copy link
Copy Markdown
Collaborator

@amosbird Do you have the sql to create table date ? I don't find it in https://clickhouse.com/docs/zh/getting-started/example-datasets/star-schema/

CREATE TABLE ssb.date
(
    `D_DATEKEY` Date,
    `D_DATE` String,
    `D_DAYOFWEEK` String,
    `D_MONTH` String,
    `D_YEAR` Int32,
    `D_YEARMONTHNUM` Int32,
    `D_YEARMONTH` String,
    `D_DAYNUMINWEEK` Int32,
    `D_DAYNUMINMONTH` Int32,
    `D_DAYNUMINYEAR` Int32,
    `D_MONTHNUMINYEAR` Int32,
    `D_WEEKNUMINYEAR` Int32,
    `D_SELLINGSEASON` String,
    `D_LASTDAYINWEEKFL` Int32,
    `D_LASTDAYINMONTHFL` Int32,
    `D_HOLIDAYFL` Int32,
    `D_WEEKDAYFL` Int32
)
ENGINE = MergeTree
ORDER BY D_DATEKEY

@lgbo-ustc

Copy link
Copy Markdown
Contributor

I observe that the right table is small. In this case, the overhead of dispatch blocks is high

@vdimir
vdimir force-pushed the enable_parallel_hash branch 4 times, most recently from 07425fe to 46d4378 Compare June 14, 2022 14:24
@vdimir

vdimir commented Jun 14, 2022

Copy link
Copy Markdown
Member Author

Slow down may happen if we select lots of columns from left table, because it'll dispatch (call scatter) all of them. Not solved yet.
So, not sure about enabling, but this PR fixes some bugs found by running parallel_hash on all tests set.

@vdimir
vdimir force-pushed the enable_parallel_hash branch from baca388 to 3e0fe1a Compare June 15, 2022 10:03
@nickitat nickitat self-assigned this Jun 15, 2022
BigRedEye added a commit to BigRedEye/ClickHouse that referenced this pull request Jun 19, 2022
@BigRedEye

Copy link
Copy Markdown
Contributor

I think I discovered a rather subtle bug while working on the related code at #38191. The bug was introduced in d712985 with transition from SipHash to WeakHash32 for sharding: the hash tables within buckets use the last bits of CRC32 as hash value as well. Thus, the distribution of hash values within each bucket is skewed, the number of collisions increases significantly, and the performance of hash tables degrades.
Maybe we should use an orthogonal hash function for sharding?

@vdimir
vdimir force-pushed the enable_parallel_hash branch from 79cef15 to 68b30fb Compare July 6, 2022 13:16
@vdimir vdimir changed the title Set default join_algorithm = parallel_hash Bugfixes and performance improvements for parallel_hash Jul 6, 2022
@vdimir
vdimir marked this pull request as ready for review July 6, 2022 13:19
@vdimir

vdimir commented Jul 6, 2022

Copy link
Copy Markdown
Member Author

I decided not to set it as a default for now, but running this algorithm on a complete test set helped to find some bugs and fix them.

@vdimir
vdimir requested a review from nickitat July 6, 2022 13:21
@vdimir
vdimir force-pushed the enable_parallel_hash branch from c8602a5 to 1bd334e Compare July 6, 2022 14:03

@nickitat nickitat left a comment

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.

lgtm, just minor remarks

Comment thread src/Interpreters/ConcurrentHashJoin.cpp Resolved
Comment thread src/Interpreters/ConcurrentHashJoin.cpp Resolved
Comment thread src/Interpreters/ConcurrentHashJoin.cpp 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.

do we have trace like in aggregator to see how data were distributed in actuality?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't know about such thing, but it would be good to see this information.
I've made this change after @BigRedEye comment. I've checked on simple synthetic case and it was actually ~20% faster.

@vdimir

vdimir commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

Stateless tests flaky check (address, actions) — Tests are not finished, fail: 0, passed: 1536

Just because lots of tests were changed and timeout doesn't adjust to the time required to run all of them

Stress test (address, actions) — Download script failed
Stress test (debug, actions) — Download script failed
Stress test (memory, actions) — Download script failed
Stress test (thread, actions) — Download script failed
Stress test (undefined, actions) — Download script failed

Looks suspicious

@vdimir
vdimir force-pushed the enable_parallel_hash branch from a54254f to d636674 Compare July 12, 2022 18:22
@vdimir
vdimir merged commit 27ec36c into ClickHouse:master Jul 13, 2022
@vdimir
vdimir deleted the enable_parallel_hash branch July 13, 2022 12:53
gregakinman added a commit to gregakinman/ClickHouse that referenced this pull request May 19, 2026
ConcurrentHashJoin scatters right-side blocks by hashing the equality key,
so rows with the same equality key always land in the same internal HashJoin
partition. The per-key SortedLookupVector that backs ASOF lives entirely
within that partition, so the parallel build behaves the same way as for
non-ASOF strictness.

The exclusion in allowParallelHashJoin appears to be defensive carryover from
the original ConcurrentHashJoin PR (ClickHouse#37648, April 2022) — it shipped with the
gate in place from day one with no rationale in the source or PR text, and
ConcurrentHashJoin itself contains zero ASOF-specific code paths or
assertions.

For workloads where the ASOF equality key has low cardinality (e.g. SQL
generated by visualization tools that compute multi-channel derived
metrics like vector magnitude sqrt(x^2 + y^2 + z^2) and use a constant
equality predicate to satisfy ASOF's syntactic requirement), this
unblocks parallelism on the build phase: e.g. one of our workloads goes
from ~93s wall (single-core build of a 250M-row hash table x 2 stacked
ASOF joins) toward what the existing manual UNION-ALL time-shard
workaround achieves (~25s with 4x CPU utilisation), per
ClickHouse#73086.

Adds a regression test that runs the same ASOF INNER and ASOF LEFT
queries under both join_algorithm = 'hash' and 'parallel_hash' and
checks per-row identity via mutual EXCEPT.
gregakinman added a commit to gregakinman/ClickHouse that referenced this pull request May 19, 2026
ConcurrentHashJoin scatters right-side blocks by hashing the equality key,
so rows with the same equality key always land in the same internal HashJoin
partition. The per-key SortedLookupVector that backs ASOF lives entirely
within that partition, so the parallel build behaves the same way as for
non-ASOF strictness.

The exclusion in allowParallelHashJoin appears to be defensive carryover from
the original ConcurrentHashJoin PR (ClickHouse#37648, April 2022) — it shipped with the
gate in place from day one with no rationale in the source or PR text, and
ConcurrentHashJoin itself contains zero ASOF-specific code paths or
assertions.

For workloads where the ASOF equality key has low cardinality (e.g. SQL
generated by visualization tools that compute multi-channel derived
metrics like vector magnitude sqrt(x^2 + y^2 + z^2) and use a constant
equality predicate to satisfy ASOF's syntactic requirement), this
unblocks parallelism on the build phase: e.g. one of our workloads goes
from ~93s wall (single-core build of a 250M-row hash table x 2 stacked
ASOF joins) toward what the existing manual UNION-ALL time-shard
workaround achieves (~25s with 4x CPU utilisation), per
ClickHouse#73086.

Adds a regression test that runs the same ASOF INNER and ASOF LEFT
queries under both join_algorithm = 'hash' and 'parallel_hash' and
checks per-row identity via mutual EXCEPT.
gregakinman added a commit to gregakinman/ClickHouse that referenced this pull request May 19, 2026
ConcurrentHashJoin scatters right-side blocks by hashing the equality key,
so rows with the same equality key always land in the same internal HashJoin
partition. The per-key SortedLookupVector that backs ASOF lives entirely
within that partition, so the parallel build behaves the same way as for
non-ASOF strictness.

The exclusion in allowParallelHashJoin appears to be defensive carryover from
the original ConcurrentHashJoin PR (ClickHouse#37648, April 2022) — it shipped with the
gate in place from day one with no rationale in the source or PR text, and
ConcurrentHashJoin itself contains zero ASOF-specific code paths or
assertions.

For workloads where the ASOF equality key has low cardinality (e.g. SQL
generated by visualization tools that compute multi-channel derived
metrics like vector magnitude sqrt(x^2 + y^2 + z^2) and use a constant
equality predicate to satisfy ASOF's syntactic requirement), this
unblocks parallelism on the build phase: e.g. one of our workloads goes
from ~93s wall (single-core build of a 250M-row hash table x 2 stacked
ASOF joins) toward what the existing manual UNION-ALL time-shard
workaround achieves (~25s with 4x CPU utilisation), per
ClickHouse#73086.

Adds a regression test that runs the same ASOF INNER and ASOF LEFT
queries under both join_algorithm = 'hash' and 'parallel_hash' and
checks per-row identity via mutual EXCEPT.
gregakinman added a commit to gregakinman/ClickHouse that referenced this pull request May 20, 2026
ConcurrentHashJoin scatters right-side blocks by hashing the equality key,
so rows with the same equality key always land in the same internal HashJoin
partition. The per-key SortedLookupVector that backs ASOF lives entirely
within that partition, so the parallel build behaves the same way as for
non-ASOF strictness.

The exclusion in allowParallelHashJoin appears to be defensive carryover from
the original ConcurrentHashJoin PR (ClickHouse#37648, April 2022) — it shipped with the
gate in place from day one with no rationale in the source or PR text, and
ConcurrentHashJoin itself contains zero ASOF-specific code paths or
assertions.

For workloads where the ASOF equality key has low cardinality (e.g. SQL
generated by visualization tools that compute multi-channel derived
metrics like vector magnitude sqrt(x^2 + y^2 + z^2) and use a constant
equality predicate to satisfy ASOF's syntactic requirement), this
unblocks parallelism on the build phase: e.g. one of our workloads goes
from ~93s wall (single-core build of a 250M-row hash table x 2 stacked
ASOF joins) toward what the existing manual UNION-ALL time-shard
workaround achieves (~25s with 4x CPU utilisation), per
ClickHouse#73086.

Adds a regression test that runs the same ASOF INNER and ASOF LEFT
queries under both join_algorithm = 'hash' and 'parallel_hash' and
checks per-row identity via mutual EXCEPT.
gregakinman added a commit to gregakinman/ClickHouse that referenced this pull request May 20, 2026
ConcurrentHashJoin scatters right-side blocks by hashing the equality key,
so rows with the same equality key always land in the same internal HashJoin
partition. The per-key SortedLookupVector that backs ASOF lives entirely
within that partition, so the parallel build behaves the same way as for
non-ASOF strictness.

The exclusion in allowParallelHashJoin appears to be defensive carryover from
the original ConcurrentHashJoin PR (ClickHouse#37648, April 2022) — it shipped with the
gate in place from day one with no rationale in the source or PR text, and
ConcurrentHashJoin itself contains zero ASOF-specific code paths or
assertions.

For workloads where the ASOF equality key has low cardinality (e.g. SQL
generated by visualization tools that compute multi-channel derived
metrics like vector magnitude sqrt(x^2 + y^2 + z^2) and use a constant
equality predicate to satisfy ASOF's syntactic requirement), this
unblocks parallelism on the build phase: e.g. one of our workloads goes
from ~93s wall (single-core build of a 250M-row hash table x 2 stacked
ASOF joins) toward what the existing manual UNION-ALL time-shard
workaround achieves (~25s with 4x CPU utilisation), per
ClickHouse#73086.

Adds a regression test that runs the same ASOF INNER and ASOF LEFT
queries under both join_algorithm = 'hash' and 'parallel_hash' and
checks per-row identity via mutual EXCEPT.
gregakinman added a commit to gregakinman/ClickHouse that referenced this pull request May 20, 2026
ConcurrentHashJoin scatters right-side blocks by hashing the equality key,
so rows with the same equality key always land in the same internal HashJoin
partition. The per-key SortedLookupVector that backs ASOF lives entirely
within that partition, so the parallel build behaves the same way as for
non-ASOF strictness.

The exclusion in allowParallelHashJoin appears to be defensive carryover from
the original ConcurrentHashJoin PR (ClickHouse#37648, April 2022) — it shipped with the
gate in place from day one with no rationale in the source or PR text, and
ConcurrentHashJoin itself contains zero ASOF-specific code paths or
assertions.

For workloads where the ASOF equality key has low cardinality (e.g. SQL
generated by visualization tools that compute multi-channel derived
metrics like vector magnitude sqrt(x^2 + y^2 + z^2) and use a constant
equality predicate to satisfy ASOF's syntactic requirement), this
unblocks parallelism on the build phase: e.g. one of our workloads goes
from ~93s wall (single-core build of a 250M-row hash table x 2 stacked
ASOF joins) toward what the existing manual UNION-ALL time-shard
workaround achieves (~25s with 4x CPU utilisation), per
ClickHouse#73086.

Adds a regression test that runs the same ASOF INNER and ASOF LEFT
queries under both join_algorithm = 'hash' and 'parallel_hash' and
checks per-row identity via mutual EXCEPT.
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.

6 participants