Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestPredicateSimplification(tt *testing.T) {
c2 VARCHAR(64) NOT NULL,
PRIMARY KEY (c1, c2)
);`)
tk.MustExec(`create table t6(a int, b int, c int, d int, index(a,b));`)
tk.MustExec(`CREATE TABLE t7c899916 (
col_37 text COLLATE gbk_bin DEFAULT NULL,
col_38 datetime DEFAULT CURRENT_TIMESTAMP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"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;",
"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' ) )",
"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');",
"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 );"
"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 );",
"select * from t6 where (a, b, c) in ((1, 1, 1), (2, 2, 2))"
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
"Projection 0.00 root 1->Column#48, Column#46, Column#47, test.tad03b424.col_41",
"└─TableDual 0.00 root rows:0"
]
},
{
"SQL": "select * from t6 where (a, b, c) in ((1, 1, 1), (2, 2, 2))",
"Plan": [
"IndexLookUp 1.00 root ",
"├─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",
"└─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))))",
" └─TableRowIDScan 1.25 cop[tikv] table:t6 keep order:false, stats:pseudo"
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
"Projection 0.00 root 1->Column#48, Column#46, Column#47, test.tad03b424.col_41",
"└─TableDual 0.00 root rows:0"
]
},
{
"SQL": "select * from t6 where (a, b, c) in ((1, 1, 1), (2, 2, 2))",
"Plan": [
"IndexLookUp 1.00 root ",
"├─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",
"└─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))))",
" └─TableRowIDScan 1.25 cop[tikv] table:t6 keep order:false, stats:pseudo"
]
}
]
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/ranger/detacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,12 @@ func extractBestCNFItemRanges(sctx *rangerctx.RangerContext, conds []expression.
}
// take the union of the two columnValues
columnValues = unionColumnValues(columnValues, res.ColumnValues)
if len(res.AccessConds) == 0 || len(res.RemainedConds) > 0 {
if len(res.AccessConds) == 0 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice fix
how about add the comment here
// for a=1 or b =1 and c =1 of index(a,b), although we can only use first two of them as access conds.
// we can also keep the range from (a=1 or b =1) into consecutive merge consideration.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

add the comment here.

continue
}
curRes := getCNFItemRangeResult(sctx, res, i)
bestRes = mergeTwoCNFRanges(sctx, cond, bestRes, curRes)
}

if bestRes != nil && bestRes.rangeResult != nil {
bestRes.rangeResult.IsDNFCond = false
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/util/ranger/ranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2126,17 +2126,17 @@ func TestRangeFallbackForDetachCondAndBuildRangeForIndex(t *testing.T) {
res, err = ranger.DetachCondAndBuildRangeForIndex(rctx, conds, cols, lengths, 0)
require.NoError(t, err)
checkDetachRangeResult(t, res,
"[or(eq(test.t2.a, aaa), eq(test.t2.a, ccc))]",
"[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)))]",
"[[\"aa\",\"aa\"] [\"cc\",\"cc\"]]")
"[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)]",
"[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)]",
`[["aa" "bb" "ee","aa" "bb" "ee"] ["cc" "dd" "ee","cc" "dd" "ee"]]`)
checkRangeFallbackAndReset(t, sctx, false)
quota = res.Ranges.MemUsage() - 1
res, err = ranger.DetachCondAndBuildRangeForIndex(rctx, conds, cols, lengths, quota)
require.NoError(t, err)
checkDetachRangeResult(t, res,
"[]",
"[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))]",
"[[NULL,+inf]]")
"[or(and(eq(test.t2.a, aaa), eq(test.t2.b, bbb)), and(eq(test.t2.a, ccc), eq(test.t2.b, ddd)))]",
"[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)]",
`[["aa" "bb","aa" "bb"] ["cc" "dd","cc" "dd"]]`)
checkRangeFallbackAndReset(t, sctx, true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ IndexReader root index:IndexRangeScan
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;
id task access object operator info
IndexReader root index:Selection
└─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))))
└─IndexRangeScan cop[tikv] table:t2, index:idx_a_b_c_d(a, b, c, d) range:[1,1], keep order:true, stats:pseudo
└─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))))
└─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
drop table if exists t;
create table t(a int not null, b int not null);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ id task access object operator info
Sort root planner__core__range_scan_for_like.t.a, planner__core__range_scan_for_like.t.b
└─TableReader root data:Selection
└─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)
└─TableRangeScan cop[tikv] table:t range:["\xfb@\xedK","\xfb@\xedK"], keep order:false, stats:pseudo
└─TableRangeScan cop[tikv] table:t range:["\xfb@\xedK" "asdfgh","\xfb@\xedK" "asdfgh!"), keep order:false, stats:pseudo
select * from t use index (primary) where a like '测试1' and b like 'asdfgh %' order by a,b;
a b c
explain format='plan_tree' select * from t use index (primary) where a like 'こんにち_' and b like 'zxc%' order by a,b;
Expand Down Expand Up @@ -548,7 +548,7 @@ id task access object operator info
Sort root planner__core__range_scan_for_like.t.a, planner__core__range_scan_for_like.t.b
└─IndexLookUp root
├─Selection(Build) cop[tikv] like(planner__core__range_scan_for_like.t.a, "测试1", 92)
│ └─IndexRangeScan cop[tikv] table:t, index:PRIMARY(a, b) range:["\x89\a\xba%1","\x89\a\xba%1"], keep order:false, stats:pseudo
│ └─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
└─Selection(Probe) cop[tikv] like(planner__core__range_scan_for_like.t.b, "asdfgh %", 92)
└─TableRowIDScan cop[tikv] table:t keep order:false, stats:pseudo
select * from t use index (primary) where a like '测试1' and b like 'asdfgh %' order by a,b;
Expand Down Expand Up @@ -592,7 +592,7 @@ id task access object operator info
Sort root planner__core__range_scan_for_like.t.a, planner__core__range_scan_for_like.t.b
└─IndexLookUp root
├─Selection(Build) cop[tikv] like(planner__core__range_scan_for_like.t.a, " asdfgh", 92)
│ └─IndexRangeScan cop[tikv] table:t, index:PRIMARY(a, b) range:[" ASDFGH"," ASDFGH"], keep order:false, stats:pseudo
│ └─IndexRangeScan cop[tikv] table:t, index:PRIMARY(a, b) range:[" ASDFGH" "HoLa"," ASDFGH" "HoLb"), keep order:false, stats:pseudo
└─Selection(Probe) cop[tikv] like(planner__core__range_scan_for_like.t.b, "HoLa%", 92)
└─TableRowIDScan cop[tikv] table:t keep order:false, stats:pseudo
select * from t use index (primary) where b like 'HoLa%' and a like ' asdfgh' order by a,b;
Expand Down