Skip to content
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

statistics: fix get wrong topn with data -0 and 0 #57464

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

hawkingrei
Copy link
Member

@hawkingrei hawkingrei commented Nov 18, 2024

What problem does this PR solve?

Issue Number: close #57448

Problem Summary:

What changed and how does it work?

first, you can add the following code to debug this code, then create the table and insert the data as describe by the issue.

Subject: [PATCH] statistics: fix get wrong topn with data -0 and 0

Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
---
 pkg/statistics/builder.go | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/pkg/statistics/builder.go b/pkg/statistics/builder.go
index f93afe6c73..761ca90402 100644
--- a/pkg/statistics/builder.go
+++ b/pkg/statistics/builder.go
@@ -16,6 +16,7 @@ package statistics
 
 import (
        "bytes"
+       "fmt"
        "math"
 
        "github.com/pingcap/errors"
@@ -336,7 +337,20 @@ func BuildHistAndTopN(
        }
        curCnt := float64(0)
        var corrXYSum float64
-
+       fmt.Println("check data")
+       for i := int64(0); i < sampleNum; i++ {
+               strstr, err := samples[i].Value.ToString()
+               if err != nil {
+                       continue
+               }
+               fmt.Printf("str: %s\n", strstr)
+               sampleBytes, err := getComparedBytes(samples[i].Value)
+               if err != nil {
+                       continue
+               }
+               fmt.Printf("% X\n", sampleBytes)
+       }
+       fmt.Println("start debug")
        // Iterate through the samples
        for i := int64(0); i < sampleNum; i++ {
                if isColumn {
@@ -362,6 +376,9 @@ func BuildHistAndTopN(
                }
                // case 2-2, now topn is empty: append the "current" count directly
                if len(topNList) == 0 {
+                       fmt.Println("===== =======")
+                       fmt.Printf("first: % X %d\n", cur)
+                       fmt.Println("===== =======")
                        topNList = append(topNList, TopNMeta{Encoded: cur, Count: uint64(curCnt)})
                        cur, curCnt = sampleBytes, 1
                        continue
@@ -380,13 +397,28 @@ func BuildHistAndTopN(
                }
                topNList = append(topNList, TopNMeta{})
                copy(topNList[j+1:], topNList[j:])
+               fmt.Println("===== =======")
+               fmt.Printf("insert: % X %d %d\n", cur, curCnt, j)
                topNList[j] = TopNMeta{Encoded: cur, Count: uint64(curCnt)}
                if len(topNList) > numTopN {
                        topNList = topNList[:numTopN]
                }
+
+               for _, item := range topNList {
+                       fmt.Printf("% X %d %d\n", item.Encoded, item.Count, j)
+               }
+               fmt.Println("===== =======")
                cur, curCnt = sampleBytes, 1
        }
-
+       checkMap := make(map[string]struct{}, len(samples))
+       for _, sample := range topNList {
+               _, ok := checkMap[string(sample.Encoded)]
+               if !ok {
+                       checkMap[string(sample.Encoded)] = struct{}{}
+               } else {
+                       fmt.Println("WTF")
+               }
+       }
        // Calc the correlation of the column between the handle column.
        if isColumn {
                hg.Correlation = calcCorrelation(sampleNum, corrXYSum)
-- 
2.47.0

so you will get the print log as following.

check data
str: -8
05 3F DF FF FF FF FF FF FF
str: -8
05 3F DF FF FF FF FF FF FF
str: -8
05 3F DF FF FF FF FF FF FF
str: -8
05 3F DF FF FF FF FF FF FF
str: -8
05 3F DF FF FF FF FF FF FF
str: -8
05 3F DF FF FF FF FF FF FF
str: -8
05 3F DF FF FF FF FF FF FF
str: -8
05 3F DF FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -6
05 3F E7 FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -4
05 3F EF FF FF FF FF FF FF
str: -2
05 3F FF FF FF FF FF FF FF
str: -2
05 3F FF FF FF FF FF FF FF
str: -2
05 3F FF FF FF FF FF FF FF
str: -2
05 3F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: 0
05 80 00 00 00 00 00 00 00
str: -0
05 7F FF FF FF FF FF FF FF
str: 0
05 80 00 00 00 00 00 00 00
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: 0
05 80 00 00 00 00 00 00 00
str: 0
05 80 00 00 00 00 00 00 00
str: -0
05 7F FF FF FF FF FF FF FF
str: -0
05 7F FF FF FF FF FF FF FF
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 2
05 C0 00 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 4
05 C0 10 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 6
05 C0 18 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
str: 8
05 C0 20 00 00 00 00 00 00
start debug
===== =======
first: 05 3F DF FF FF FF FF FF FF %!d(MISSING)
===== =======
===== =======
insert: 05 3F E7 FF FF FF FF FF FF %!d(float64=15) 0
05 3F E7 FF FF FF FF FF FF 15 0
05 3F DF FF FF FF FF FF FF 8 0
===== =======
===== =======
insert: 05 3F EF FF FF FF FF FF FF %!d(float64=11) 1
05 3F E7 FF FF FF FF FF FF 15 1
05 3F EF FF FF FF FF FF FF 11 1
05 3F DF FF FF FF FF FF FF 8 1
===== =======
===== =======
insert: 05 3F FF FF FF FF FF FF FF %!d(float64=4) 3
05 3F E7 FF FF FF FF FF FF 15 3
05 3F EF FF FF FF FF FF FF 11 3
05 3F DF FF FF FF FF FF FF 8 3
05 3F FF FF FF FF FF FF FF 4 3
===== =======
===== =======
insert: 05 7F FF FF FF FF FF FF FF %!d(float64=7) 3
05 3F E7 FF FF FF FF FF FF 15 3
05 3F EF FF FF FF FF FF FF 11 3
05 3F DF FF FF FF FF FF FF 8 3
05 7F FF FF FF FF FF FF FF 7 3
05 3F FF FF FF FF FF FF FF 4 3
===== =======
===== =======
insert: 05 80 00 00 00 00 00 00 00 %!d(float64=1) 5
05 3F E7 FF FF FF FF FF FF 15 5
05 3F EF FF FF FF FF FF FF 11 5
05 3F DF FF FF FF FF FF FF 8 5
05 7F FF FF FF FF FF FF FF 7 5
05 3F FF FF FF FF FF FF FF 4 5
05 80 00 00 00 00 00 00 00 1 5
===== =======
===== =======
insert: 05 7F FF FF FF FF FF FF FF %!d(float64=1) 5
05 3F E7 FF FF FF FF FF FF 15 5
05 3F EF FF FF FF FF FF FF 11 5
05 3F DF FF FF FF FF FF FF 8 5
05 7F FF FF FF FF FF FF FF 7 5
05 3F FF FF FF FF FF FF FF 4 5
05 7F FF FF FF FF FF FF FF 1 5
05 80 00 00 00 00 00 00 00 1 5
===== =======
===== =======
insert: 05 80 00 00 00 00 00 00 00 %!d(float64=1) 5
05 3F E7 FF FF FF FF FF FF 15 5
05 3F EF FF FF FF FF FF FF 11 5
05 3F DF FF FF FF FF FF FF 8 5
05 7F FF FF FF FF FF FF FF 7 5
05 3F FF FF FF FF FF FF FF 4 5
05 80 00 00 00 00 00 00 00 1 5
05 7F FF FF FF FF FF FF FF 1 5
05 80 00 00 00 00 00 00 00 1 5
===== =======
===== =======
insert: 05 7F FF FF FF FF FF FF FF %!d(float64=3) 5
05 3F E7 FF FF FF FF FF FF 15 5
05 3F EF FF FF FF FF FF FF 11 5
05 3F DF FF FF FF FF FF FF 8 5
05 7F FF FF FF FF FF FF FF 7 5
05 3F FF FF FF FF FF FF FF 4 5
05 7F FF FF FF FF FF FF FF 3 5
05 80 00 00 00 00 00 00 00 1 5
05 7F FF FF FF FF FF FF FF 1 5
05 80 00 00 00 00 00 00 00 1 5
===== =======
===== =======
insert: 05 80 00 00 00 00 00 00 00 %!d(float64=2) 6
05 3F E7 FF FF FF FF FF FF 15 6
05 3F EF FF FF FF FF FF FF 11 6
05 3F DF FF FF FF FF FF FF 8 6
05 7F FF FF FF FF FF FF FF 7 6
05 3F FF FF FF FF FF FF FF 4 6
05 7F FF FF FF FF FF FF FF 3 6
05 80 00 00 00 00 00 00 00 2 6
05 80 00 00 00 00 00 00 00 1 6
05 7F FF FF FF FF FF FF FF 1 6
05 80 00 00 00 00 00 00 00 1 6
===== =======
===== =======
insert: 05 7F FF FF FF FF FF FF FF %!d(float64=2) 6
05 3F E7 FF FF FF FF FF FF 15 6
05 3F EF FF FF FF FF FF FF 11 6
05 3F DF FF FF FF FF FF FF 8 6
05 7F FF FF FF FF FF FF FF 7 6
05 3F FF FF FF FF FF FF FF 4 6
05 7F FF FF FF FF FF FF FF 3 6
05 7F FF FF FF FF FF FF FF 2 6
05 80 00 00 00 00 00 00 00 2 6
05 80 00 00 00 00 00 00 00 1 6
05 7F FF FF FF FF FF FF FF 1 6
05 80 00 00 00 00 00 00 00 1 6
===== =======
===== =======
insert: 05 C0 00 00 00 00 00 00 00 %!d(float64=10) 2
05 3F E7 FF FF FF FF FF FF 15 2
05 3F EF FF FF FF FF FF FF 11 2
05 C0 00 00 00 00 00 00 00 10 2
05 3F DF FF FF FF FF FF FF 8 2
05 7F FF FF FF FF FF FF FF 7 2
05 3F FF FF FF FF FF FF FF 4 2
05 7F FF FF FF FF FF FF FF 3 2
05 7F FF FF FF FF FF FF FF 2 2
05 80 00 00 00 00 00 00 00 2 2
05 80 00 00 00 00 00 00 00 1 2
05 7F FF FF FF FF FF FF FF 1 2
05 80 00 00 00 00 00 00 00 1 2
===== =======
===== =======
insert: 05 C0 10 00 00 00 00 00 00 %!d(float64=13) 1
05 3F E7 FF FF FF FF FF FF 15 1
05 C0 10 00 00 00 00 00 00 13 1
05 3F EF FF FF FF FF FF FF 11 1
05 C0 00 00 00 00 00 00 00 10 1
05 3F DF FF FF FF FF FF FF 8 1
05 7F FF FF FF FF FF FF FF 7 1
05 3F FF FF FF FF FF FF FF 4 1
05 7F FF FF FF FF FF FF FF 3 1
05 7F FF FF FF FF FF FF FF 2 1
05 80 00 00 00 00 00 00 00 2 1
05 80 00 00 00 00 00 00 00 1 1
05 7F FF FF FF FF FF FF FF 1 1
05 80 00 00 00 00 00 00 00 1 1
===== =======
===== =======
insert: 05 C0 18 00 00 00 00 00 00 %!d(float64=8) 4
05 3F E7 FF FF FF FF FF FF 15 4
05 C0 10 00 00 00 00 00 00 13 4
05 3F EF FF FF FF FF FF FF 11 4
05 C0 00 00 00 00 00 00 00 10 4
05 C0 18 00 00 00 00 00 00 8 4
05 3F DF FF FF FF FF FF FF 8 4
05 7F FF FF FF FF FF FF FF 7 4
05 3F FF FF FF FF FF FF FF 4 4
05 7F FF FF FF FF FF FF FF 3 4
05 7F FF FF FF FF FF FF FF 2 4
05 80 00 00 00 00 00 00 00 2 4
05 80 00 00 00 00 00 00 00 1 4
05 7F FF FF FF FF FF FF FF 1 4
05 80 00 00 00 00 00 00 00 1 4
===== =======

From the logs below, it can be observed that the final topn contains multiple identical values. Additionally, the inserted data also has duplicates. Finally, in the sorted results, we found that the order of 0 and -0 is incorrect.

the root cause is that sortSampleItems is sorted by the semantics, but getComparedBytes is compared by the binary order.

The two semantics are different, which led to the final issue.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

fix get wrong topn with data -0 and 0

修复因为 0 和 -0 ,导致构建统计信息 topn 构建错误

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 77.50000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 73.6385%. Comparing base (1b6d01b) to head (85f2113).
Report is 8 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #57464        +/-   ##
================================================
+ Coverage   72.8394%   73.6385%   +0.7991%     
================================================
  Files          1672       1702        +30     
  Lines        462873     470888      +8015     
================================================
+ Hits         337154     346755      +9601     
+ Misses       104938     102721      -2217     
- Partials      20781      21412       +631     
Flag Coverage Δ
integration 46.1452% <57.5000%> (?)
unit 72.2282% <77.5000%> (+0.0095%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.7673% <ø> (ø)
parser ∅ <ø> (∅)
br 45.0999% <ø> (-0.0092%) ⬇️
---- 🚨 Try these New Features:

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Nov 18, 2024
@hawkingrei hawkingrei marked this pull request as draft November 19, 2024 03:10
@ti-chi-bot ti-chi-bot bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 19, 2024
@hawkingrei hawkingrei marked this pull request as ready for review November 19, 2024 07:55
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 19, 2024
@hawkingrei
Copy link
Member Author

/retest

Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
@hawkingrei
Copy link
Member Author

/retest

1 similar comment
@hawkingrei
Copy link
Member Author

/retest

Copy link

tiprow bot commented Nov 19, 2024

@hawkingrei: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
fast_test_tiprow 85f2113 link true /test fast_test_tiprow

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

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

rest lgtm

@@ -61,15 +62,36 @@ func CopySampleItems(items []*SampleItem) []*SampleItem {
return n
}

func sortSampleItems(sc *stmtctx.StatementContext, items []*SampleItem) error {
func sortDatumByBinary(items []types.Datum, getComparedBytes func(datum types.Datum) ([]byte, error)) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

could you comment on why binary format matters rather than simple item value comparison? for later reviewer's quick grabbing

Copy link

ti-chi-bot bot commented Nov 19, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: winoros

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Nov 19, 2024
Copy link

ti-chi-bot bot commented Nov 19, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-11-19 14:02:24.014586044 +0000 UTC m=+969706.205455041: ☑️ agreed by winoros.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Runtime error occurs when analyzing table
3 participants