Skip to content

feat(cross-shard): support count(DISTINCT) on the sharding key - #2084

Open
murex971 wants to merge 1 commit into
murex-agg-sharding-key-lookupfrom
murex-count-distinct-sharding-key
Open

murex971 wants to merge 1 commit into
murex-agg-sharding-key-lookupfrom
murex-count-distinct-sharding-key

Conversation

@murex971

Copy link
Copy Markdown
Contributor

Summary

count(DISTINCT x) across shards is refused today. When x is the
sharding key, no two shards hold the same value, so each shard's count
covers keys no other shard has and the counts add up.

SELECT count(DISTINCT user_id) FROM orders;
SELECT status, count(DISTINCT user_id) FROM orders GROUP BY status;

The projection rewrite records the SELECT list positions of
count(DISTINCT key) on its plan, using the ShardingKey lookup.
The merge treats those columns as a plain count and sums them with
the existing accumulator.

It applies when the query reads one plain table and the argument is the
bare key column. Joins, subqueries in FROM, CTEs and expressions over
the key are still refused, as is DISTINCT on any other column.

@murex971
murex971 added this pull request to stack #2073 November 21, 2026 08:21
@murex971
murex971 force-pushed the murex-count-distinct-sharding-key branch from 02fdf79 to 10a2521 Compare November 21, 2026 08:22
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.63768% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...parser/rewrite/statement/aggregate/sharding_key.rs 98.41% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

1 participant