feat: derive a distinct count from primary key and unique constraints - #24520
feat: derive a distinct count from primary key and unique constraints#24520Dandandan wants to merge 5 commits into
Conversation
A join has to estimate how many rows it produces, which needs the number of distinct values in each key. No file format stores one, so a scan reports it as unknown even for a column the table declares unique, and the estimate is a guess. A single-column primary key or unique constraint gives that number directly: one distinct value per row, less the nulls a unique column may repeat. A composite key says nothing about its columns on their own, so it is left alone, and the count is inexact because constraints are not verified. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012YiAABcW4WSqij31zz2P6c
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #24520 +/- ##
==========================================
+ Coverage 81.30% 81.32% +0.01%
==========================================
Files 1117 1117
Lines 395881 396383 +502
Branches 395881 396383 +502
==========================================
+ Hits 321886 322343 +457
- Misses 55178 55197 +19
- Partials 18817 18843 +26 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
run benchmarks |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing feat/pk-cardinality-estimation (32d17cb) to 858d3bf (merge-base) diff Run configurationrun benchmark tpchResults will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing feat/pk-cardinality-estimation (32d17cb) to 858d3bf (merge-base) diff Run configurationrun benchmark clickbench_partitionedResults will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing feat/pk-cardinality-estimation (32d17cb) to 858d3bf (merge-base) diff Run configurationrun benchmark tpcdsResults will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing feat/pk-cardinality-estimation (32d17cb) to 858d3bf (merge-base) diff Run configurationrun benchmark tpchCPU Details (lscpu)Details
Resource Usagetpch — base (merge-base)
tpch — branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing feat/pk-cardinality-estimation (32d17cb) to 858d3bf (merge-base) diff Run configurationrun benchmark tpcdsCPU Details (lscpu)Details
Resource Usagetpcds — base (merge-base)
tpcds — branch
File an issue against this benchmark runner |
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012YiAABcW4WSqij31zz2P6c
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing feat/pk-cardinality-estimation (32d17cb) to 858d3bf (merge-base) diff Run configurationrun benchmark clickbench_partitionedCPU Details (lscpu)Details
Resource Usageclickbench_partitioned — base (merge-base)
clickbench_partitioned — branch
File an issue against this benchmark runner |
…row count A declared key is treated as fact elsewhere -- an ordering requirement is considered satisfied because of one -- so the count it implies is not a guess. Only an unknown null count on a unique column, which may repeat NULL, leaves it inexact. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012YiAABcW4WSqij31zz2P6c
A filter estimated an equality from the column's value range, or from the default selectivity when the type has no range to read. Neither can say what a unique constraint does: the column holds each value once, so an equality matches a single row. The distinct count a key constraint now supplies is what identifies such a column -- one distinct value per non-null row -- so the estimate only tightens where that count is known. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012YiAABcW4WSqij31zz2P6c
|
@Rich-T-kid are you perhaps interested in reviewing this one? |
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012YiAABcW4WSqij31zz2P6c
eb6cfa5 to
d16d257
Compare
|
Yes I'd be happy too. I may need a bit of time to catch up to speed so hopefully tonight |
Which issue does this PR close?
Rationale for this change
From constraints on columns such as primary key / uniqueness we can derive distinct counts (number of rows = distinct counts).
This helps making it more accurate in cases when filtering / joining on those columns.
What changes are included in this PR?
Are there any user-facing changes?