Skip to content

Commit 1d746d8

Browse files
authored
range: wrongly skip the candidate in the extractBestCNFItemRanges (#62585)
close #62547
1 parent a759ee7 commit 1d746d8

8 files changed

Lines changed: 33 additions & 14 deletions

File tree

pkg/planner/core/casetest/rule/rule_predicate_simplification_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestPredicateSimplification(tt *testing.T) {
5454
c2 VARCHAR(64) NOT NULL,
5555
PRIMARY KEY (c1, c2)
5656
);`)
57+
tk.MustExec(`create table t6(a int, b int, c int, d int, index(a,b));`)
5758
tk.MustExec(`CREATE TABLE t7c899916 (
5859
col_37 text COLLATE gbk_bin DEFAULT NULL,
5960
col_38 datetime DEFAULT CURRENT_TIMESTAMP,

pkg/planner/core/casetest/rule/testdata/predicate_simplification_in.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"SELECT i.id, ip_products.products FROM t1 AS i LEFT JOIN t4 ON i.id = t4.c3 LEFT JOIN (SELECT t4.c3, GROUP_CONCAT(DISTINCT t2.c3 ORDER BY t2.c3 ASC) AS products FROM t4 JOIN t3 ON t4.c1 = t3.c1 AND t4.c2 = t3.c2 LEFT JOIN t2 ON t4.c1 = t2.c1 AND t4.c2 = t2.c2 WHERE t3.c3 = 'production' AND t4.state = 'ACTIVE' GROUP BY t4.c3, t4.c1, t4.c2) AS ip_products ON t4.c3 = ip_products.c3 LEFT JOIN t5 ON i.id = t5.c1 AND t5.c2 = 'production' WHERE t4.state = 'ACTIVE' AND t5.c1 IS NULL GROUP BY i.id, ip_products.products HAVING FIND_IN_SET('info', products) ORDER BY i.id ASC LIMIT 500 OFFSET 5500;",
66
"with cte_81 ( col_108 ) AS ( select space( 2 ) as r0 from t7c899916 where t7c899916.col_44 = '[17764220206423580415]' and t7c899916.col_44 != '[5725396597060626308,5860284933591136807,11766074999769332834,11879855656810383243]' order by r0 limit 385374223 ) ( select 1,col_108 from cte_81 where cte_81.col_108 = null or cte_81.col_108 in ( '^w#txObGH*=J*Jn5kq' ,'+PzzjiWQ7y&PQV' ,'=%4C7Oain' ) )",
77
"select * from (select col_47 from t7c899916 limit 1) t where col_47 in ('2034-05-27', '2001-06-27') and col_47 in (null, '1996-06-27');",
8-
"with cte_263 ( col_1350,col_1351,col_1352 ) AS ( select /*+ read_from_storage(tiflash[ tad03b424,tlfdfece63 ]) */ /*+ use_index_merge( tlfdfece63,tad03b424 ) */ /*+ merge_join( tlfdfece63 , tad03b424 */ rpad( tad03b424.col_44 , 6 , tad03b424.col_48 ) as r0 , insert( tlfdfece63.col_44 , 0 , 10 , tlfdfece63.col_43 ) as r1 , tad03b424.col_41 as r2 from tlfdfece63 , tad03b424 where not( tlfdfece63.col_42 = '[\"Sl9DRlDnSdIOxbfequ02VeikDWiphuDO6suBf0F7esJeCWrRJWQbd3BK3vT58Coz\",\"MmC5saHdTUqosY50IrxprAR52oD08XgGhqJCcYeoaDJKrYxBdbi0QuVDDArCghyL\"]' ) order by r0,r1,r2 limit 348170821 ) ( select 1,col_1350,col_1351,col_1352 from cte_263 where not( cte_263.col_1352 != '2020-06-30' ) and cte_263.col_1352 in ( null ,'1983-08-09' ) order by 1,2,3,4 );"
8+
"with cte_263 ( col_1350,col_1351,col_1352 ) AS ( select /*+ read_from_storage(tiflash[ tad03b424,tlfdfece63 ]) */ /*+ use_index_merge( tlfdfece63,tad03b424 ) */ /*+ merge_join( tlfdfece63 , tad03b424 */ rpad( tad03b424.col_44 , 6 , tad03b424.col_48 ) as r0 , insert( tlfdfece63.col_44 , 0 , 10 , tlfdfece63.col_43 ) as r1 , tad03b424.col_41 as r2 from tlfdfece63 , tad03b424 where not( tlfdfece63.col_42 = '[\"Sl9DRlDnSdIOxbfequ02VeikDWiphuDO6suBf0F7esJeCWrRJWQbd3BK3vT58Coz\",\"MmC5saHdTUqosY50IrxprAR52oD08XgGhqJCcYeoaDJKrYxBdbi0QuVDDArCghyL\"]' ) order by r0,r1,r2 limit 348170821 ) ( select 1,col_1350,col_1351,col_1352 from cte_263 where not( cte_263.col_1352 != '2020-06-30' ) and cte_263.col_1352 in ( null ,'1983-08-09' ) order by 1,2,3,4 );",
9+
"select * from t6 where (a, b, c) in ((1, 1, 1), (2, 2, 2))"
910
]
1011
}
1112
]

pkg/planner/core/casetest/rule/testdata/predicate_simplification_out.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
"Projection 0.00 root 1->Column#48, Column#46, Column#47, test.tad03b424.col_41",
5959
"└─TableDual 0.00 root rows:0"
6060
]
61+
},
62+
{
63+
"SQL": "select * from t6 where (a, b, c) in ((1, 1, 1), (2, 2, 2))",
64+
"Plan": [
65+
"IndexLookUp 1.00 root ",
66+
"├─IndexRangeScan(Build) 1.25 cop[tikv] table:t6, index:a(a, b) range:[1 1,1 1], [2 2,2 2], keep order:false, stats:pseudo",
67+
"└─Selection(Probe) 1.00 cop[tikv] or(and(eq(test.t6.a, 1), and(eq(test.t6.b, 1), eq(test.t6.c, 1))), and(eq(test.t6.a, 2), and(eq(test.t6.b, 2), eq(test.t6.c, 2))))",
68+
" └─TableRowIDScan 1.25 cop[tikv] table:t6 keep order:false, stats:pseudo"
69+
]
6170
}
6271
]
6372
}

pkg/planner/core/casetest/rule/testdata/predicate_simplification_xut.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
"Projection 0.00 root 1->Column#48, Column#46, Column#47, test.tad03b424.col_41",
5959
"└─TableDual 0.00 root rows:0"
6060
]
61+
},
62+
{
63+
"SQL": "select * from t6 where (a, b, c) in ((1, 1, 1), (2, 2, 2))",
64+
"Plan": [
65+
"IndexLookUp 1.00 root ",
66+
"├─IndexRangeScan(Build) 1.25 cop[tikv] table:t6, index:a(a, b) range:[1 1,1 1], [2 2,2 2], keep order:false, stats:pseudo",
67+
"└─Selection(Probe) 1.00 cop[tikv] or(and(eq(test.t6.a, 1), and(eq(test.t6.b, 1), eq(test.t6.c, 1))), and(eq(test.t6.a, 2), and(eq(test.t6.b, 2), eq(test.t6.c, 2))))",
68+
" └─TableRowIDScan 1.25 cop[tikv] table:t6 keep order:false, stats:pseudo"
69+
]
6170
}
6271
]
6372
}

pkg/util/ranger/detacher.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,12 @@ func extractBestCNFItemRanges(sctx *rangerctx.RangerContext, conds []expression.
338338
}
339339
// take the union of the two columnValues
340340
columnValues = unionColumnValues(columnValues, res.ColumnValues)
341-
if len(res.AccessConds) == 0 || len(res.RemainedConds) > 0 {
341+
if len(res.AccessConds) == 0 {
342342
continue
343343
}
344344
curRes := getCNFItemRangeResult(sctx, res, i)
345345
bestRes = mergeTwoCNFRanges(sctx, cond, bestRes, curRes)
346346
}
347-
348347
if bestRes != nil && bestRes.rangeResult != nil {
349348
bestRes.rangeResult.IsDNFCond = false
350349
}

pkg/util/ranger/ranger_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,17 +2126,17 @@ func TestRangeFallbackForDetachCondAndBuildRangeForIndex(t *testing.T) {
21262126
res, err = ranger.DetachCondAndBuildRangeForIndex(rctx, conds, cols, lengths, 0)
21272127
require.NoError(t, err)
21282128
checkDetachRangeResult(t, res,
2129-
"[or(eq(test.t2.a, aaa), eq(test.t2.a, ccc))]",
2130-
"[eq(test.t2.c, eee) or(and(eq(test.t2.a, aaa), eq(test.t2.b, bbb)), and(eq(test.t2.a, ccc), eq(test.t2.b, ddd)))]",
2131-
"[[\"aa\",\"aa\"] [\"cc\",\"cc\"]]")
2129+
"[or(and(eq(test.t2.a, aaa), eq(test.t2.b, bbb)), and(eq(test.t2.a, ccc), eq(test.t2.b, ddd))) eq(test.t2.c, eee)]",
2130+
"[or(and(eq(test.t2.a, aaa), eq(test.t2.b, bbb)), and(eq(test.t2.a, ccc), eq(test.t2.b, ddd))) eq(test.t2.c, eee)]",
2131+
`[["aa" "bb" "ee","aa" "bb" "ee"] ["cc" "dd" "ee","cc" "dd" "ee"]]`)
21322132
checkRangeFallbackAndReset(t, sctx, false)
21332133
quota = res.Ranges.MemUsage() - 1
21342134
res, err = ranger.DetachCondAndBuildRangeForIndex(rctx, conds, cols, lengths, quota)
21352135
require.NoError(t, err)
21362136
checkDetachRangeResult(t, res,
2137-
"[]",
2138-
"[eq(test.t2.c, eee) or(and(eq(test.t2.a, aaa), eq(test.t2.b, bbb)), and(eq(test.t2.a, ccc), eq(test.t2.b, ddd))) or(eq(test.t2.a, aaa), eq(test.t2.a, ccc))]",
2139-
"[[NULL,+inf]]")
2137+
"[or(and(eq(test.t2.a, aaa), eq(test.t2.b, bbb)), and(eq(test.t2.a, ccc), eq(test.t2.b, ddd)))]",
2138+
"[or(and(eq(test.t2.a, aaa), eq(test.t2.b, bbb)), and(eq(test.t2.a, ccc), eq(test.t2.b, ddd))) eq(test.t2.c, eee)]",
2139+
`[["aa" "bb","aa" "bb"] ["cc" "dd","cc" "dd"]]`)
21402140
checkRangeFallbackAndReset(t, sctx, true)
21412141
}
21422142

tests/integrationtest/r/planner/core/casetest/integration.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ IndexReader root index:IndexRangeScan
447447
explain format='plan_tree' select * from t2 where ((a = 1 and b = 1 and d < 3) or (a = 1 and b = 1 and d > 6)) and c = 3 order by d;
448448
id task access object operator info
449449
IndexReader root index:Selection
450-
└─Selection cop[tikv] eq(planner__core__casetest__integration.t2.c, 3), or(and(eq(planner__core__casetest__integration.t2.a, 1), and(eq(planner__core__casetest__integration.t2.b, 1), lt(planner__core__casetest__integration.t2.d, 3))), and(eq(planner__core__casetest__integration.t2.a, 1), and(eq(planner__core__casetest__integration.t2.b, 1), gt(planner__core__casetest__integration.t2.d, 6))))
451-
└─IndexRangeScan cop[tikv] table:t2, index:idx_a_b_c_d(a, b, c, d) range:[1,1], keep order:true, stats:pseudo
450+
└─Selection cop[tikv] or(and(eq(planner__core__casetest__integration.t2.a, 1), and(eq(planner__core__casetest__integration.t2.b, 1), lt(planner__core__casetest__integration.t2.d, 3))), and(eq(planner__core__casetest__integration.t2.a, 1), and(eq(planner__core__casetest__integration.t2.b, 1), gt(planner__core__casetest__integration.t2.d, 6))))
451+
└─IndexRangeScan cop[tikv] table:t2, index:idx_a_b_c_d(a, b, c, d) range:[1 1 3,1 1 3], keep order:true, stats:pseudo
452452
drop table if exists t;
453453
create table t(a int not null, b int not null);
454454
explain format='plan_tree' select * from t where exists (select 1 from t t1 join t t2 where t1.a = t2.a and t1.a = t.a);

tests/integrationtest/r/planner/core/range_scan_for_like.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ id task access object operator info
445445
Sort root planner__core__range_scan_for_like.t.a, planner__core__range_scan_for_like.t.b
446446
└─TableReader root data:Selection
447447
└─Selection cop[tikv] like(planner__core__range_scan_for_like.t.a, "测试1", 92), like(planner__core__range_scan_for_like.t.b, "asdfgh %", 92)
448-
└─TableRangeScan cop[tikv] table:t range:["\xfb@\xedK","\xfb@\xedK"], keep order:false, stats:pseudo
448+
└─TableRangeScan cop[tikv] table:t range:["\xfb@\xedK" "asdfgh","\xfb@\xedK" "asdfgh!"), keep order:false, stats:pseudo
449449
select * from t use index (primary) where a like '测试1' and b like 'asdfgh %' order by a,b;
450450
a b c
451451
explain format='plan_tree' select * from t use index (primary) where a like 'こんにち_' and b like 'zxc%' order by a,b;
@@ -548,7 +548,7 @@ id task access object operator info
548548
Sort root planner__core__range_scan_for_like.t.a, planner__core__range_scan_for_like.t.b
549549
└─IndexLookUp root
550550
├─Selection(Build) cop[tikv] like(planner__core__range_scan_for_like.t.a, "测试1", 92)
551-
│ └─IndexRangeScan cop[tikv] table:t, index:PRIMARY(a, b) range:["\x89\a\xba%1","\x89\a\xba%1"], keep order:false, stats:pseudo
551+
│ └─IndexRangeScan cop[tikv] table:t, index:PRIMARY(a, b) range:["\x89\a\xba%1" "asdfg","\x89\a\xba%1" "asdfh"), keep order:false, stats:pseudo
552552
└─Selection(Probe) cop[tikv] like(planner__core__range_scan_for_like.t.b, "asdfgh %", 92)
553553
└─TableRowIDScan cop[tikv] table:t keep order:false, stats:pseudo
554554
select * from t use index (primary) where a like '测试1' and b like 'asdfgh %' order by a,b;
@@ -592,7 +592,7 @@ id task access object operator info
592592
Sort root planner__core__range_scan_for_like.t.a, planner__core__range_scan_for_like.t.b
593593
└─IndexLookUp root
594594
├─Selection(Build) cop[tikv] like(planner__core__range_scan_for_like.t.a, " asdfgh", 92)
595-
│ └─IndexRangeScan cop[tikv] table:t, index:PRIMARY(a, b) range:[" ASDFGH"," ASDFGH"], keep order:false, stats:pseudo
595+
│ └─IndexRangeScan cop[tikv] table:t, index:PRIMARY(a, b) range:[" ASDFGH" "HoLa"," ASDFGH" "HoLb"), keep order:false, stats:pseudo
596596
└─Selection(Probe) cop[tikv] like(planner__core__range_scan_for_like.t.b, "HoLa%", 92)
597597
└─TableRowIDScan cop[tikv] table:t keep order:false, stats:pseudo
598598
select * from t use index (primary) where b like 'HoLa%' and a like ' asdfgh' order by a,b;

0 commit comments

Comments
 (0)