Skip to content

Commit 66a9025

Browse files
authored
planner, stats: overflow estimation may lead to wrong join reorder (#56752) (#56763)
close #56704
1 parent d1e7e0a commit 66a9025

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

planner/core/rule_join_reorder_greedy.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ func (s *joinReorderGreedySolver) constructConnectedJoinTree(tracer *joinReorder
9595
s.curJoinGroup = s.curJoinGroup[1:]
9696
for {
9797
bestCost := math.MaxFloat64
98-
bestIdx := -1
99-
var finalRemainOthers []expression.Expression
100-
var bestJoin LogicalPlan
98+
bestIdx, whateverValidOneIdx := -1, -1
99+
var finalRemainOthers, remainOthersOfWhateverValidOne []expression.Expression
100+
var bestJoin, whateverValidOne LogicalPlan
101101
for i, node := range s.curJoinGroup {
102102
newJoin, remainOthers := s.checkConnectionAndMakeJoin(curJoinTree.p, node.p)
103103
if newJoin == nil {
@@ -107,6 +107,9 @@ func (s *joinReorderGreedySolver) constructConnectedJoinTree(tracer *joinReorder
107107
if err != nil {
108108
return nil, err
109109
}
110+
whateverValidOne = newJoin
111+
whateverValidOneIdx = i
112+
remainOthersOfWhateverValidOne = remainOthers
110113
curCost := s.calcJoinCumCost(newJoin, curJoinTree, node)
111114
tracer.appendLogicalJoinCost(newJoin, curCost)
112115
if bestCost > curCost {
@@ -118,7 +121,14 @@ func (s *joinReorderGreedySolver) constructConnectedJoinTree(tracer *joinReorder
118121
}
119122
// If we could find more join node, meaning that the sub connected graph have been totally explored.
120123
if bestJoin == nil {
121-
break
124+
if whateverValidOne == nil {
125+
break
126+
}
127+
// This branch is for the unexpected case.
128+
bestJoin = whateverValidOne
129+
bestCost = math.MaxFloat64
130+
bestIdx = whateverValidOneIdx
131+
finalRemainOthers = remainOthersOfWhateverValidOne
122132
}
123133
curJoinTree = &jrNode{
124134
p: bestJoin,

planner/core/rule_join_reorder_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,22 @@ FROM t3 tt
158158
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
159159
))
160160
}
161+
162+
// https://github.com/pingcap/tidb/issues/56704
163+
func TestJoinReorderWithPossibleCostCalcOverflow(t *testing.T) {
164+
store := testkit.CreateMockStore(t)
165+
166+
tk := testkit.NewTestKit(t, store)
167+
168+
tk.MustExec("use test;")
169+
tk.MustExec("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;")
170+
tk.MustExec("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);")
171+
tk.MustExec("analyze table lrr_test;")
172+
173+
result := tk.MustQuery("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;")
174+
require.Equal(t, 49, len(result.Rows()))
175+
result = tk.MustQuery("explain 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;")
176+
// Layout of explain: operator, estimated rows, task type, access object, operator info.
177+
require.NotContains(t, "NaN", result.Rows()[0][1])
178+
require.NotContains(t, "CARTEISAN", result.Rows()[0][4])
179+
}

statistics/histogram.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,10 @@ func (hg *Histogram) outOfRangeRowCount(sctx sessionctx.Context, lDatum, rDatum
919919
if histWidth <= 0 {
920920
return 0
921921
}
922+
if math.IsInf(histWidth, 1) {
923+
// The histogram is too wide. As a quick fix, we return 0 to indicate that the overlap percentage is near 0.
924+
return 0
925+
}
922926
boundL := histL - histWidth
923927
boundR := histR + histWidth
924928

0 commit comments

Comments
 (0)