Skip to content

Commit

Permalink
Merge branch 'master' into issue-28577
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun authored Mar 25, 2022
2 parents 2ac1b36 + 59fac2a commit 457a081
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 229 deletions.
8 changes: 4 additions & 4 deletions cmd/explaintest/r/explain_generate_column_substitute.result
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,14 @@ StreamAgg 4.80 root group by:upper(test.t.b), funcs:max(upper(test.t.b))->Colum
└─IndexFullScan 6.00 cop[tikv] table:t, index:expression_index_2(upper(`b`)) keep order:true
desc format = 'brief' select count(upper(b)) from t use index() group by upper(b);
id estRows task access object operator info
HashAgg 6.00 root group by:Column#11, funcs:count(Column#10)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#10, upper(test.t.b)->Column#11
HashAgg 6.00 root group by:Column#9, funcs:count(Column#8)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#8, upper(test.t.b)->Column#9
└─TableReader 6.00 root data:TableFullScan
└─TableFullScan 6.00 cop[tikv] table:t keep order:false
desc format = 'brief' select max(upper(b)) from t use index() group by upper(b);
id estRows task access object operator info
HashAgg 6.00 root group by:Column#11, funcs:max(Column#10)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#10, upper(test.t.b)->Column#11
HashAgg 6.00 root group by:Column#9, funcs:max(Column#8)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#8, upper(test.t.b)->Column#9
└─TableReader 6.00 root data:TableFullScan
└─TableFullScan 6.00 cop[tikv] table:t keep order:false
drop table if exists t;
Expand Down
10 changes: 5 additions & 5 deletions cmd/explaintest/r/index_merge.result
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,11 @@ c1 c2 c3 c4 c5
explain select /*+ use_index_merge(t1) */ * from t1 where (c1 < 10 or c2 < 10) and greatest(c1, c2, c4) = 1 order by 1;
id estRows task access object operator info
Sort_5 4433.77 root test.t1.c1
└─IndexMerge_12 4433.77 root
├─IndexRangeScan_8(Build) 3323.33 cop[tikv] table:t1, index:c1(c1) range:[-inf,10), keep order:false, stats:pseudo
├─IndexRangeScan_9(Build) 3323.33 cop[tikv] table:t1, index:c2(c2) range:[-inf,10), keep order:false, stats:pseudo
└─Selection_11(Probe) 4433.77 cop[tikv] eq(greatest(test.t1.c1, test.t1.c2, test.t1.c4), 1)
└─TableRowIDScan_10 5542.21 cop[tikv] table:t1 keep order:false, stats:pseudo
└─Selection_12 4433.77 root eq(greatest(test.t1.c1, test.t1.c2, test.t1.c4), 1)
└─IndexMerge_11 5542.21 root
├─IndexRangeScan_8(Build) 3323.33 cop[tikv] table:t1, index:c1(c1) range:[-inf,10), keep order:false, stats:pseudo
├─IndexRangeScan_9(Build) 3323.33 cop[tikv] table:t1, index:c2(c2) range:[-inf,10), keep order:false, stats:pseudo
└─TableRowIDScan_10(Probe) 5542.21 cop[tikv] table:t1 keep order:false, stats:pseudo
select /*+ use_index_merge(t1) */ * from t1 where (c1 < 10 or c2 < 10) and greatest(c1, c2, c4) = 1 order by 1;
c1 c2 c3 c4 c5
1 1 1 1 1
Expand Down
100 changes: 0 additions & 100 deletions expression/expr_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1331,81 +1331,6 @@ func TestExprPushDownToTiKV(t *testing.T) {
// retType: types.NewFieldType(mysql.TypeDouble),
// args: []Expression{intColumn, intColumn},
//},
{
functionName: ast.Bin,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{intColumn},
},
{
functionName: ast.Unhex,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.Locate,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{stringColumn, stringColumn},
},
{
functionName: ast.Ord,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{stringColumn},
},
{
functionName: ast.Lpad,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn, intColumn, stringColumn},
},
{
functionName: ast.Rpad,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn, intColumn, stringColumn},
},
{
functionName: ast.Trim,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.FromBase64,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.ToBase64,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.MakeSet,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{intColumn, stringColumn},
},
{
functionName: ast.SubstringIndex,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn, stringColumn, intColumn},
},
{
functionName: ast.Quote,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.Oct,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{intColumn},
},
{
functionName: ast.FindInSet,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{stringColumn, stringColumn},
},
{
functionName: ast.Repeat,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn, intColumn},
},
{
functionName: ast.Date,
retType: types.NewFieldType(mysql.TypeDate),
Expand All @@ -1431,31 +1356,6 @@ func TestExprPushDownToTiKV(t *testing.T) {
retType: types.NewFieldType(mysql.TypeDate),
args: []Expression{dateColumn, dateColumn},
},
{
functionName: ast.Lower,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.InsertFunc,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn, intColumn, intColumn, stringColumn},
},
{
functionName: ast.Greatest,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{intColumn, intColumn},
},
{
functionName: ast.Least,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{intColumn, intColumn},
},
{
functionName: ast.Upper,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.Mod,
retType: types.NewFieldType(mysql.TypeInt24),
Expand Down
10 changes: 5 additions & 5 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ func scalarExprSupportedByTiKV(sf *ScalarFunction) bool {

// compare functions.
ast.LT, ast.LE, ast.EQ, ast.NE, ast.GE, ast.GT, ast.NullEQ, ast.In, ast.IsNull, ast.Like, ast.IsTruthWithoutNull, ast.IsTruthWithNull, ast.IsFalsity,
ast.Greatest, ast.Least, /* ast.Interval */
// ast.Greatest, ast.Least, ast.Interval

// arithmetical functions.
ast.PI, /* ast.Truncate */
Expand All @@ -968,10 +968,10 @@ func scalarExprSupportedByTiKV(sf *ScalarFunction) bool {
ast.Case, ast.If, ast.Ifnull, ast.Coalesce,

// string functions.
ast.Bin, ast.Unhex, ast.Locate, ast.Ord, ast.Lpad, ast.Rpad,
ast.Trim, ast.FromBase64, ast.ToBase64, ast.Upper, ast.Lower, ast.InsertFunc,
ast.MakeSet, ast.SubstringIndex /* ast.Instr */, ast.Quote, ast.Oct,
ast.FindInSet, ast.Repeat,
// ast.Bin, ast.Unhex, ast.Locate, ast.Ord, ast.Lpad, ast.Rpad,
// ast.Trim, ast.FromBase64, ast.ToBase64, ast.Upper, ast.Lower, ast.InsertFunc,
// ast.MakeSet, ast.SubstringIndex, ast.Instr, ast.Quote, ast.Oct,
// ast.FindInSet, ast.Repeat,
ast.Length, ast.BitLength, ast.Concat, ast.ConcatWS, ast.Replace, ast.ASCII, ast.Hex,
ast.Reverse, ast.LTrim, ast.RTrim, ast.Strcmp, ast.Space, ast.Elt, ast.Field,
InternalFuncFromBinary, InternalFuncToBinary, ast.Mid, ast.Substring, ast.Substr, ast.CharLength,
Expand Down
20 changes: 12 additions & 8 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3498,29 +3498,33 @@ func TestExprPushdown(t *testing.T) {

// case 1, index scan without double read, some filters can not be pushed to cop task
rows := tk.MustQuery("explain format = 'brief' select col2, col1 from t use index(key1) where col2 like '5%' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Rows()
require.Equal(t, "cop[tikv]", fmt.Sprintf("%v", rows[2][2]))
require.Equal(t, "eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), \"4\"), like(test.t.col2, \"5%\", 92)", fmt.Sprintf("%v", rows[2][4]))
require.Equal(t, "root", fmt.Sprintf("%v", rows[1][2]))
require.Equal(t, "eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), \"4\")", fmt.Sprintf("%v", rows[1][4]))
require.Equal(t, "cop[tikv]", fmt.Sprintf("%v", rows[3][2]))
require.Equal(t, "like(test.t.col2, \"5%\", 92)", fmt.Sprintf("%v", rows[3][4]))
tk.MustQuery("select col2, col1 from t use index(key1) where col2 like '5%' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("511 411111"))
tk.MustQuery("select count(col2) from t use index(key1) where col2 like '5%' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("1"))

// case 2, index scan without double read, none of the filters can be pushed to cop task
rows = tk.MustQuery("explain format = 'brief' select col1, col2 from t use index(key2) where from_base64(to_base64(substr(col2, 1, 1))) = '5' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Rows()
require.Equal(t, "cop[tikv]", fmt.Sprintf("%v", rows[1][2]))
require.Equal(t, `eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), "4"), eq(from_base64(to_base64(substr(test.t.col2, 1, 1))), "5")`, fmt.Sprintf("%v", rows[1][4]))
require.Equal(t, "root", fmt.Sprintf("%v", rows[0][2]))
require.Equal(t, "eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), \"4\"), eq(from_base64(to_base64(substr(test.t.col2, 1, 1))), \"5\")", fmt.Sprintf("%v", rows[0][4]))
tk.MustQuery("select col1, col2 from t use index(key2) where from_base64(to_base64(substr(col2, 1, 1))) = '5' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("411111 511"))
tk.MustQuery("select count(col1) from t use index(key2) where from_base64(to_base64(substr(col2, 1, 1))) = '5' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("1"))

// case 3, index scan with double read, some filters can not be pushed to cop task
rows = tk.MustQuery("explain format = 'brief' select id from t use index(key1) where col2 like '5%' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Rows()
require.Equal(t, "cop[tikv]", fmt.Sprintf("%v", rows[2][2]))
require.Equal(t, `eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), "4"), like(test.t.col2, "5%", 92)`, fmt.Sprintf("%v", rows[2][4]))
require.Equal(t, "root", fmt.Sprintf("%v", rows[1][2]))
require.Equal(t, "eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), \"4\")", fmt.Sprintf("%v", rows[1][4]))
require.Equal(t, "cop[tikv]", fmt.Sprintf("%v", rows[3][2]))
require.Equal(t, "like(test.t.col2, \"5%\", 92)", fmt.Sprintf("%v", rows[3][4]))
tk.MustQuery("select id from t use index(key1) where col2 like '5%' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("3"))
tk.MustQuery("select count(id) from t use index(key1) where col2 like '5%' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("1"))

// case 4, index scan with double read, none of the filters can be pushed to cop task
rows = tk.MustQuery("explain format = 'brief' select id from t use index(key2) where from_base64(to_base64(substr(col2, 1, 1))) = '5' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Rows()
require.Equal(t, "cop[tikv]", fmt.Sprintf("%v", rows[2][2]))
require.Equal(t, `eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), "4"), eq(from_base64(to_base64(substr(test.t.col2, 1, 1))), "5")`, fmt.Sprintf("%v", rows[2][4]))
require.Equal(t, "root", fmt.Sprintf("%v", rows[1][2]))
require.Equal(t, "eq(from_base64(to_base64(substr(test.t.col1, 1, 1))), \"4\"), eq(from_base64(to_base64(substr(test.t.col2, 1, 1))), \"5\")", fmt.Sprintf("%v", rows[1][4]))
tk.MustQuery("select id from t use index(key2) where from_base64(to_base64(substr(col2, 1, 1))) = '5' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("3"))
tk.MustQuery("select count(id) from t use index(key2) where from_base64(to_base64(substr(col2, 1, 1))) = '5' and from_base64(to_base64(substr(col1, 1, 1))) = '4'").Check(testkit.Rows("1"))
}
Expand Down
4 changes: 3 additions & 1 deletion parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ func (*Join) resultSet() {}
// We get (t1 join t3) left join t2, the semantics is correct.
func NewCrossJoin(left, right ResultSetNode) (n *Join) {
rj, ok := right.(*Join)
if !ok || rj.Right == nil {
// don't break the explicit parents name scope constraints.
// this kind of join re-order can be done in logical-phase after the name resolution.
if !ok || rj.Right == nil || rj.ExplicitParens {
return &Join{Left: left, Right: right, Tp: CrossJoin}
}

Expand Down
6 changes: 3 additions & 3 deletions parser/ast/dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ func TestJoinRestore(t *testing.T) {
//{"(select a from t) t1 join t t2, t3;", "((SELECT `a` FROM `t`) AS `t1` JOIN `t` AS `t2`) JOIN `t3`"},
}
testChangedCases := []NodeRestoreTestCase{
{"(a al left join b bl on al.a1 > bl.b1) join (a ar right join b br on ar.a1 > br.b1)", "((`a` AS `al` LEFT JOIN `b` AS `bl` ON `al`.`a1`>`bl`.`b1`) JOIN `b` AS `br`) LEFT JOIN `a` AS `ar` ON `ar`.`a1`>`br`.`b1`"},
{"(a al left join b bl on al.a1 > bl.b1) join (a ar right join b br on ar.a1 > br.b1)", "(`a` AS `al` LEFT JOIN `b` AS `bl` ON `al`.`a1`>`bl`.`b1`) JOIN (`a` AS `ar` RIGHT JOIN `b` AS `br` ON `ar`.`a1`>`br`.`b1`)"},
{"a al left join b bl on al.a1 > bl.b1, a ar right join b br on ar.a1 > br.b1", "(`a` AS `al` LEFT JOIN `b` AS `bl` ON `al`.`a1`>`bl`.`b1`) JOIN (`a` AS `ar` RIGHT JOIN `b` AS `br` ON `ar`.`a1`>`br`.`b1`)"},
{"t1 join (t2 right join t3 on t2.a > t3.a join (t4 right join t5 on t4.a > t5.a))", "(((`t1` JOIN `t2`) RIGHT JOIN `t3` ON `t2`.`a`>`t3`.`a`) JOIN `t5`) LEFT JOIN `t4` ON `t4`.`a`>`t5`.`a`"},
{"t1 join (t2 right join t3 on t2.a > t3.a join (t4 right join t5 on t4.a > t5.a))", "`t1` JOIN ((`t2` RIGHT JOIN `t3` ON `t2`.`a`>`t3`.`a`) JOIN (`t4` RIGHT JOIN `t5` ON `t4`.`a`>`t5`.`a`))"},
{"t1 join t2 right join t3 on t2.a=t3.a", "(`t1` JOIN `t2`) RIGHT JOIN `t3` ON `t2`.`a`=`t3`.`a`"},
{"t1 join (t2 right join t3 on t2.a=t3.a)", "(`t1` JOIN `t3`) LEFT JOIN `t2` ON `t2`.`a`=`t3`.`a`"},
{"t1 join (t2 right join t3 on t2.a=t3.a)", "`t1` JOIN (`t2` RIGHT JOIN `t3` ON `t2`.`a`=`t3`.`a`)"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).From.TableRefs
Expand Down
Loading

0 comments on commit 457a081

Please sign in to comment.