-
Notifications
You must be signed in to change notification settings - Fork 6.2k
statistics: add global singleton estimation for sample-based NDV #67593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ti-chi-bot
merged 21 commits into
pingcap:master
from
0xPoe:poe-patch-sample-based-ndv-singleton
Apr 10, 2026
+308
−1
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
96482e5
stats: add global singleton estimation
0xPoe 183bd2a
test: add cases for global signleton estimation
0xPoe 488ea14
docs: add more comments
0xPoe d3076a0
fix: check panics
0xPoe 012a6f5
docs: rename region to node in global singleton estimation
0xPoe 7b1b938
fix: do not allow nil
0xPoe e2654f6
fix: move checks under comments
0xPoe 9f1c109
fix: handle the negative delta
0xPoe ec78c2f
docs: more commnets
0xPoe 352d92b
feat: impove the perf
0xPoe 03d8d0c
fix: correct the size
0xPoe e11f2a3
fix: better code
0xPoe beb4fc8
feat: improve perf futher
0xPoe 8e0c541
fix: avoid overflow
0xPoe 0ffa0d1
docs: add notes
0xPoe 918e33d
Update pkg/statistics/estimate.go
0xPoe 2a37e83
refactor: reduce noisy
0xPoe 479f7e7
fix: remove useless nil checks
0xPoe 7b1de2a
docs: add more comments
0xPoe cedb658
refactor: all use slice access
0xPoe 35b6a87
docs: add more comments
0xPoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package statistics | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/pingcap/tidb/pkg/types" | ||
| "github.com/pingcap/tidb/pkg/util/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // newFMSketchFromHashValues builds an FM sketch by directly inserting hash values. | ||
| // With a large maxSize and few values, the sketch gives exact NDV (mask stays 0). | ||
| func newFMSketchFromHashValues(vals ...uint64) *FMSketch { | ||
| s := NewFMSketch(1000) | ||
| for _, v := range vals { | ||
| s.insertHashValue(v) | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // newFMSketchesFromSamples builds a pair of sketches from the same sample rows: | ||
| // the NDV sketch sees every sample, while the singleton sketch keeps only | ||
| // values that occur exactly once in that sample set. | ||
| func newFMSketchesFromSamples(t *testing.T, maxSize int, samples ...int64) (*FMSketch, *FMSketch) { | ||
| t.Helper() | ||
| ctx := mock.NewContext() | ||
| ndvSketch := NewFMSketch(maxSize) | ||
| singletonSketch := NewFMSketch(maxSize) | ||
| counts := make(map[int64]int, len(samples)) | ||
| for _, v := range samples { | ||
| counts[v]++ | ||
| err := ndvSketch.InsertValue(ctx.GetSessionVars().StmtCtx, types.NewIntDatum(v)) | ||
| require.NoError(t, err) | ||
| } | ||
| for _, v := range samples { | ||
| if counts[v] != 1 { | ||
| continue | ||
| } | ||
| err := singletonSketch.InsertValue(ctx.GetSessionVars().StmtCtx, types.NewIntDatum(v)) | ||
| require.NoError(t, err) | ||
| delete(counts, v) | ||
| } | ||
| return ndvSketch, singletonSketch | ||
| } | ||
|
|
||
| func TestEstimateGlobalSingletonBySketches(t *testing.T) { | ||
| // Use distinct hash values to represent distinct data values. | ||
| // With maxSize=1000 and few insertions, mask stays 0 so NDV = len(hashset) (exact). | ||
| const ( | ||
| a = uint64(100) | ||
| b = uint64(200) | ||
| c = uint64(300) | ||
| d = uint64(400) | ||
| e = uint64(500) | ||
| f = uint64(600) | ||
| ) | ||
|
|
||
| t.Run("DocCommentExample", func(t *testing.T) { | ||
| // Node 0: all distinct = {a, b, c}, local singletons = {a, b, c} | ||
| // Node 1: all distinct = {b, c, d}, local singletons = {b, d} | ||
| // Node 2: all distinct = {c, e, f}, local singletons = {e, f} | ||
| // Global singletons = {a, d, e, f} = 4 | ||
| ndvSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b, c), | ||
| newFMSketchFromHashValues(b, c, d), | ||
| newFMSketchFromHashValues(c, e, f), | ||
| } | ||
| singletonSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b, c), | ||
| newFMSketchFromHashValues(b, d), | ||
| newFMSketchFromHashValues(e, f), | ||
| } | ||
| got := EstimateGlobalSingletonBySketches(ndvSketches, singletonSketches) | ||
| require.Equal(t, uint64(4), got) | ||
| }) | ||
|
|
||
| t.Run("SingleNode", func(t *testing.T) { | ||
| // With one node, all local singletons are global singletons. | ||
| ndvSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b, c), | ||
| } | ||
| singletonSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b, c), | ||
| } | ||
| got := EstimateGlobalSingletonBySketches(ndvSketches, singletonSketches) | ||
| require.Equal(t, uint64(3), got) | ||
| }) | ||
|
|
||
| t.Run("NoOverlap", func(t *testing.T) { | ||
| // Nodes have disjoint values. All local singletons are global singletons. | ||
| ndvSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b), | ||
| newFMSketchFromHashValues(c, d), | ||
| newFMSketchFromHashValues(e, f), | ||
| } | ||
| singletonSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b), | ||
| newFMSketchFromHashValues(c, d), | ||
| newFMSketchFromHashValues(e, f), | ||
| } | ||
| got := EstimateGlobalSingletonBySketches(ndvSketches, singletonSketches) | ||
| require.Equal(t, uint64(6), got) | ||
| }) | ||
|
|
||
| t.Run("FullOverlap", func(t *testing.T) { | ||
| // Every local singleton also appears in another node's NDV. | ||
| // Node 0: all = {a, b}, singletons = {a, b} | ||
| // Node 1: all = {a, b}, singletons = {a, b} | ||
| // No value is unique to a single node → 0 global singletons. | ||
| ndvSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b), | ||
| newFMSketchFromHashValues(a, b), | ||
| } | ||
| singletonSketches := []*FMSketch{ | ||
| newFMSketchFromHashValues(a, b), | ||
| newFMSketchFromHashValues(a, b), | ||
| } | ||
| got := EstimateGlobalSingletonBySketches(ndvSketches, singletonSketches) | ||
| require.Equal(t, uint64(0), got) | ||
| }) | ||
|
|
||
| t.Run("NegativeContributionIsClamped", func(t *testing.T) { | ||
| // Both sketches are built from the same local samples. | ||
| // | ||
| // Node 0 samples: [0] | ||
| // Node 1 samples: [0, 0, 0, 1, 1, 4, 7] | ||
| // | ||
| // The true global singleton set is {4, 7}, so the result should be 2. | ||
| // Before the fix, node 0's contribution could become negative due to FM | ||
| // sketch merge behavior, making the final estimate incorrect. | ||
| ndv0, singleton0 := newFMSketchesFromSamples(t, 3, 0) | ||
| ndv1, singleton1 := newFMSketchesFromSamples(t, 3, 0, 0, 0, 1, 1, 4, 7) | ||
| ndvSketches := []*FMSketch{ndv0, ndv1} | ||
| singletonSketches := []*FMSketch{singleton0, singleton1} | ||
|
|
||
| got := EstimateGlobalSingletonBySketches(ndvSketches, singletonSketches) | ||
| require.Equal(t, uint64(2), got) | ||
| }) | ||
|
|
||
| t.Run("NilEntry", func(t *testing.T) { | ||
| require.PanicsWithValue(t, "assert failed, ndvSketches must not contain nil entries", func() { | ||
| EstimateGlobalSingletonBySketches( | ||
| []*FMSketch{nil, newFMSketchFromHashValues(c, d)}, | ||
| []*FMSketch{newFMSketchFromHashValues(a, b), newFMSketchFromHashValues(c, d)}, | ||
| ) | ||
| }) | ||
| require.PanicsWithValue(t, "assert failed, singletonSketches must not contain nil entries", func() { | ||
| EstimateGlobalSingletonBySketches( | ||
| []*FMSketch{newFMSketchFromHashValues(a, b), newFMSketchFromHashValues(c, d)}, | ||
| []*FMSketch{nil, newFMSketchFromHashValues(c, d)}, | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("EmptyInput", func(t *testing.T) { | ||
| require.PanicsWithValue(t, "assert failed, ndvSketches shouldn't be empty", func() { | ||
| EstimateGlobalSingletonBySketches(nil, nil) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("MismatchedLengths", func(t *testing.T) { | ||
| ndvSketches := []*FMSketch{newFMSketchFromHashValues(a)} | ||
| singletonSketches := []*FMSketch{newFMSketchFromHashValues(a), newFMSketchFromHashValues(b)} | ||
| require.PanicsWithValue(t, "assert failed, ndvSketches and singletonSketches should have the same length", func() { | ||
| EstimateGlobalSingletonBySketches(ndvSketches, singletonSketches) | ||
| }) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.