Skip to content

Commit d492b69

Browse files
winorosti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#56752
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 963344f commit d492b69

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

planner/core/rule_join_reorder_greedy.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ import (
1818
"math"
1919
"sort"
2020

21+
<<<<<<< HEAD:planner/core/rule_join_reorder_greedy.go
2122
"github.com/pingcap/tidb/expression"
23+
=======
24+
"github.com/pingcap/tidb/pkg/expression"
25+
"github.com/pingcap/tidb/pkg/planner/core/base"
26+
"github.com/pingcap/tidb/pkg/util/intest"
27+
>>>>>>> eebdcfebd5b (planner, stats: overflow estimation may lead to wrong join reorder (#56752)):pkg/planner/core/rule_join_reorder_greedy.go
2228
)
2329

2430
type joinReorderGreedySolver struct {
@@ -95,9 +101,15 @@ func (s *joinReorderGreedySolver) constructConnectedJoinTree(tracer *joinReorder
95101
s.curJoinGroup = s.curJoinGroup[1:]
96102
for {
97103
bestCost := math.MaxFloat64
104+
<<<<<<< HEAD:planner/core/rule_join_reorder_greedy.go
98105
bestIdx := -1
99106
var finalRemainOthers []expression.Expression
100107
var bestJoin LogicalPlan
108+
=======
109+
bestIdx, whateverValidOneIdx := -1, -1
110+
var finalRemainOthers, remainOthersOfWhateverValidOne []expression.Expression
111+
var bestJoin, whateverValidOne base.LogicalPlan
112+
>>>>>>> eebdcfebd5b (planner, stats: overflow estimation may lead to wrong join reorder (#56752)):pkg/planner/core/rule_join_reorder_greedy.go
101113
for i, node := range s.curJoinGroup {
102114
newJoin, remainOthers := s.checkConnectionAndMakeJoin(curJoinTree.p, node.p)
103115
if newJoin == nil {
@@ -107,6 +119,9 @@ func (s *joinReorderGreedySolver) constructConnectedJoinTree(tracer *joinReorder
107119
if err != nil {
108120
return nil, err
109121
}
122+
whateverValidOne = newJoin
123+
whateverValidOneIdx = i
124+
remainOthersOfWhateverValidOne = remainOthers
110125
curCost := s.calcJoinCumCost(newJoin, curJoinTree, node)
111126
tracer.appendLogicalJoinCost(newJoin, curCost)
112127
if bestCost > curCost {
@@ -118,7 +133,16 @@ func (s *joinReorderGreedySolver) constructConnectedJoinTree(tracer *joinReorder
118133
}
119134
// If we could find more join node, meaning that the sub connected graph have been totally explored.
120135
if bestJoin == nil {
121-
break
136+
if whateverValidOne == nil {
137+
break
138+
}
139+
// This branch is for the unexpected case.
140+
// We throw assertion in test env. And create a valid join to avoid wrong result in the production env.
141+
intest.Assert(false, "Join reorder should find one valid join but failed.")
142+
bestJoin = whateverValidOne
143+
bestCost = math.MaxFloat64
144+
bestIdx = whateverValidOneIdx
145+
finalRemainOthers = remainOthersOfWhateverValidOne
122146
}
123147
curJoinTree = &jrNode{
124148
p: bestJoin,

statistics/histogram.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ type Histogram struct {
6969

7070
// Used for estimating fraction of the interval [lower, upper] that lies within the [lower, value].
7171
// For some types like `Int`, we do not build it because we can get them directly from `Bounds`.
72+
<<<<<<< HEAD:statistics/histogram.go
7273
scalars []scalar
74+
=======
75+
Scalars []scalar
76+
ID int64 // Column ID.
77+
NDV int64 // Number of distinct values. Note that It contains the NDV of the TopN which is excluded from histogram.
78+
NullCount int64 // Number of null values.
79+
// LastUpdateVersion is the version that this histogram updated last time.
80+
LastUpdateVersion uint64
81+
82+
>>>>>>> eebdcfebd5b (planner, stats: overflow estimation may lead to wrong join reorder (#56752)):pkg/statistics/histogram.go
7383
// TotColSize is the total column size for the histogram.
7484
// For unfixed-len types, it includes LEN and BYTE.
7585
TotColSize int64
@@ -919,6 +929,10 @@ func (hg *Histogram) outOfRangeRowCount(sctx sessionctx.Context, lDatum, rDatum
919929
if histWidth <= 0 {
920930
return 0
921931
}
932+
if math.IsInf(histWidth, 1) {
933+
// The histogram is too wide. As a quick fix, we return 0 to indicate that the overlap percentage is near 0.
934+
return 0
935+
}
922936
boundL := histL - histWidth
923937
boundR := histR + histWidth
924938

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
CREATE TABLE `lrr_test` ( `COL102` double DEFAULT NULL, `COL1` double GENERATED ALWAYS AS (`COL102` + 10) STORED NOT NULL, PRIMARY KEY (`COL1`) /*T![clustered_index] CLUSTERED */ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
2+
insert into lrr_test (col102) values(-1.704648925036604e308), (-1.6888619680353582e308), (-1.6685908644498436e308), (-1.6311134967437805e308), (-1.6128280680807152e308), (-1.5899713947158026e308), (-1.5709457594070477e308), (-1.4925714566991343e308), (-1.4705985087370154e308), (-1.4451316666300040e308), (-1.3946576985986583e308), (-1.3695679630646804e308), (-1.3208992137984086e308), (-1.2887981369134862e308), (-1.2119996449796167e308), (-1.195172956104992e308), (-1.1929781068369925e308), (-1.1746351299417647e308), (-1.1237012620945195e308), (-1.1223448185004882e308), (-1.0974439629672084e308), (-1.0657654808610821e308), (-1.0582598945271716e308), (-1.0565276887850733e308), (-1.0416104832981696e308), (-1.0368741532690337e308), (-1.033521479407133e308), (-1.0232269544119505e308), (-9.31943312515408e307), (-9.05107332838438e307), (-8.276443475796885e307), (-7.845086666145396e307), (-7.664543340054255e307), (-7.235369799352141e307), (-7.047280050755922e307), (-6.62205033356235e307), (-6.35964999739255e307), (-5.989391229038818e307), (-5.974526205854541e307), (-5.798684586589338e307), (-4.98047732376121e307), (-4.4623979626128605e307), (-4.3248436443381234e307), (-3.3391152928792773e307), (-3.2694282487729395e307), (-3.2461091065368577e307), (-2.8613054009714654e307), (-2.7176814604572905e307), (-2.1301127705458223e307), (-1.7280065154718344e307), (-1.6743061442642827e307), (-4.862812928655648e306), (-3.3262533560429795e305), (4.124952267435051e305), (5.4576487694211726e306), (1.1237742400537221e307), (1.569984332645614e307), (1.7966188405412235e307), (1.8619233341238355e307), (2.1152066540419881e307), (2.1764927570795164e307), (2.99416682762135e307), (3.0545414962788647e307), (3.262967770716021e307), (3.288944887183685e307), (4.9025219351381e307), (5.250864486081297e307), (5.52054372134351e307), (6.311436996747818e307), (6.870852232080436e307), (7.501871137935436e307), (7.925709054822421e307), (8.438195254661318e307), (8.446731596918706e307), (9.43580947190119e307), (9.66735866233596e307), (1.0022043827847664e308), (1.020869767928594e308), (1.0327408606815872e308), (1.0402383684235906e308), (1.0690255622829305e308), (1.1623306052784659e308), (1.1906116361044565e308), (1.2221839628780758e308), (1.3112927565356536e308), (1.3307364382402157e308), (1.3646958839720612e308), (1.425066345632827e308), (1.4433864261103511e308), (1.5038532858735658e308), (1.5079450808097928e308), (1.553628680980576e308), (1.6241456663280369e308), (1.6295729949930798e308), (1.6328703529666413e308), (1.6832354056195887e308), (1.7017315016390902e308), (1.7134206410400048e308), (1.7240829054261275e308), (1.7257738639648862e308), (1.7262297095455299e308), (1.7905151735809062e308);
3+
analyze table lrr_test;
4+
select t1.col1, t2.col1 from lrr_test as t1 right join lrr_test as t2 on t1.col1 = t2.col1 where t1.col1 >=0 order by t1.col1;
5+
col1 col1
6+
4.124952267435051e305 4.124952267435051e305
7+
5.4576487694211726e306 5.4576487694211726e306
8+
1.1237742400537221e307 1.1237742400537221e307
9+
1.569984332645614e307 1.569984332645614e307
10+
1.7966188405412235e307 1.7966188405412235e307
11+
1.8619233341238355e307 1.8619233341238355e307
12+
2.1152066540419881e307 2.1152066540419881e307
13+
2.1764927570795164e307 2.1764927570795164e307
14+
2.99416682762135e307 2.99416682762135e307
15+
3.0545414962788647e307 3.0545414962788647e307
16+
3.262967770716021e307 3.262967770716021e307
17+
3.288944887183685e307 3.288944887183685e307
18+
4.9025219351381e307 4.9025219351381e307
19+
5.250864486081297e307 5.250864486081297e307
20+
5.52054372134351e307 5.52054372134351e307
21+
6.311436996747818e307 6.311436996747818e307
22+
6.870852232080436e307 6.870852232080436e307
23+
7.501871137935436e307 7.501871137935436e307
24+
7.925709054822421e307 7.925709054822421e307
25+
8.438195254661318e307 8.438195254661318e307
26+
8.446731596918706e307 8.446731596918706e307
27+
9.43580947190119e307 9.43580947190119e307
28+
9.66735866233596e307 9.66735866233596e307
29+
1.0022043827847664e308 1.0022043827847664e308
30+
1.020869767928594e308 1.020869767928594e308
31+
1.0327408606815872e308 1.0327408606815872e308
32+
1.0402383684235906e308 1.0402383684235906e308
33+
1.0690255622829305e308 1.0690255622829305e308
34+
1.1623306052784659e308 1.1623306052784659e308
35+
1.1906116361044565e308 1.1906116361044565e308
36+
1.2221839628780758e308 1.2221839628780758e308
37+
1.3112927565356536e308 1.3112927565356536e308
38+
1.3307364382402157e308 1.3307364382402157e308
39+
1.3646958839720612e308 1.3646958839720612e308
40+
1.425066345632827e308 1.425066345632827e308
41+
1.4433864261103511e308 1.4433864261103511e308
42+
1.5038532858735658e308 1.5038532858735658e308
43+
1.5079450808097928e308 1.5079450808097928e308
44+
1.553628680980576e308 1.553628680980576e308
45+
1.6241456663280369e308 1.6241456663280369e308
46+
1.6295729949930798e308 1.6295729949930798e308
47+
1.6328703529666413e308 1.6328703529666413e308
48+
1.6832354056195887e308 1.6832354056195887e308
49+
1.7017315016390902e308 1.7017315016390902e308
50+
1.7134206410400048e308 1.7134206410400048e308
51+
1.7240829054261275e308 1.7240829054261275e308
52+
1.7257738639648862e308 1.7257738639648862e308
53+
1.7262297095455299e308 1.7262297095455299e308
54+
1.7905151735809062e308 1.7905151735809062e308
55+
explain format='brief' select t1. col1, t2.col1 from lrr_test as t1 right join lrr_test as t2 on t1.col1 = t2.col1 where t1.col1 >=0;
56+
id estRows task access object operator info
57+
MergeJoin 49.00 root inner join, left key:statistics__overflow_calc.lrr_test.col1, right key:statistics__overflow_calc.lrr_test.col1
58+
├─TableReader(Build) 49.00 root data:TableRangeScan
59+
│ └─TableRangeScan 49.00 cop[tikv] table:t2 range:[0,+inf], keep order:true
60+
└─TableReader(Probe) 49.00 root data:TableRangeScan
61+
└─TableRangeScan 49.00 cop[tikv] table:t1 range:[0,+inf], keep order:true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://github.com/pingcap/tidb/issues/56704
2+
CREATE TABLE `lrr_test` ( `COL102` double DEFAULT NULL, `COL1` double GENERATED ALWAYS AS (`COL102` + 10) STORED NOT NULL, PRIMARY KEY (`COL1`) /*T![clustered_index] CLUSTERED */ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
3+
insert into lrr_test (col102) values(-1.704648925036604e308), (-1.6888619680353582e308), (-1.6685908644498436e308), (-1.6311134967437805e308), (-1.6128280680807152e308), (-1.5899713947158026e308), (-1.5709457594070477e308), (-1.4925714566991343e308), (-1.4705985087370154e308), (-1.4451316666300040e308), (-1.3946576985986583e308), (-1.3695679630646804e308), (-1.3208992137984086e308), (-1.2887981369134862e308), (-1.2119996449796167e308), (-1.195172956104992e308), (-1.1929781068369925e308), (-1.1746351299417647e308), (-1.1237012620945195e308), (-1.1223448185004882e308), (-1.0974439629672084e308), (-1.0657654808610821e308), (-1.0582598945271716e308), (-1.0565276887850733e308), (-1.0416104832981696e308), (-1.0368741532690337e308), (-1.033521479407133e308), (-1.0232269544119505e308), (-9.31943312515408e307), (-9.05107332838438e307), (-8.276443475796885e307), (-7.845086666145396e307), (-7.664543340054255e307), (-7.235369799352141e307), (-7.047280050755922e307), (-6.62205033356235e307), (-6.35964999739255e307), (-5.989391229038818e307), (-5.974526205854541e307), (-5.798684586589338e307), (-4.98047732376121e307), (-4.4623979626128605e307), (-4.3248436443381234e307), (-3.3391152928792773e307), (-3.2694282487729395e307), (-3.2461091065368577e307), (-2.8613054009714654e307), (-2.7176814604572905e307), (-2.1301127705458223e307), (-1.7280065154718344e307), (-1.6743061442642827e307), (-4.862812928655648e306), (-3.3262533560429795e305), (4.124952267435051e305), (5.4576487694211726e306), (1.1237742400537221e307), (1.569984332645614e307), (1.7966188405412235e307), (1.8619233341238355e307), (2.1152066540419881e307), (2.1764927570795164e307), (2.99416682762135e307), (3.0545414962788647e307), (3.262967770716021e307), (3.288944887183685e307), (4.9025219351381e307), (5.250864486081297e307), (5.52054372134351e307), (6.311436996747818e307), (6.870852232080436e307), (7.501871137935436e307), (7.925709054822421e307), (8.438195254661318e307), (8.446731596918706e307), (9.43580947190119e307), (9.66735866233596e307), (1.0022043827847664e308), (1.020869767928594e308), (1.0327408606815872e308), (1.0402383684235906e308), (1.0690255622829305e308), (1.1623306052784659e308), (1.1906116361044565e308), (1.2221839628780758e308), (1.3112927565356536e308), (1.3307364382402157e308), (1.3646958839720612e308), (1.425066345632827e308), (1.4433864261103511e308), (1.5038532858735658e308), (1.5079450808097928e308), (1.553628680980576e308), (1.6241456663280369e308), (1.6295729949930798e308), (1.6328703529666413e308), (1.6832354056195887e308), (1.7017315016390902e308), (1.7134206410400048e308), (1.7240829054261275e308), (1.7257738639648862e308), (1.7262297095455299e308), (1.7905151735809062e308);
4+
5+
analyze table lrr_test;
6+
7+
select t1.col1, t2.col1 from lrr_test as t1 right join lrr_test as t2 on t1.col1 = t2.col1 where t1.col1 >=0 order by t1.col1;
8+
explain format='brief' select t1. col1, t2.col1 from lrr_test as t1 right join lrr_test as t2 on t1.col1 = t2.col1 where t1.col1 >=0;

0 commit comments

Comments
 (0)