Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plan, expression: add date function support for hash partition (#15068) #15618

Merged
merged 3 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions expression/partition_pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ func (p *hashPartitionPruner) tryEvalPartitionExpr(piExpr Expression) (val int64
case ast.Div:
return rightVal / leftVal, true, false
}
} else if pi.FuncName.L == ast.Year || pi.FuncName.L == ast.Month || pi.FuncName.L == ast.ToDays {
col := pi.GetArgs()[0].(*Column)
idx := p.getColID(col)
val := p.constantMap[idx]
if val != nil {
pi.GetArgs()[0] = val
ret, _, err := pi.EvalInt(p.ctx, chunk.Row{})
if err != nil {
return 0, false, false
}
return ret, true, false
}
return 0, false, false
}
case *Constant:
val, err := pi.Eval(chunk.Row{})
Expand Down
3 changes: 3 additions & 0 deletions expression/partition_pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (s *testSuite2) TestHashPartitionPruner(c *C) {
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t2(id int, a int, b int, primary key(id, a)) partition by hash(id + a) partitions 10;")
tk.MustExec("create table t1(id int primary key, a int, b int) partition by hash(id) partitions 10;")
tk.MustExec("create table t3(id int, a int, b int, primary key(id, a)) partition by hash(id) partitions 10;")
tk.MustExec("create table t4(d datetime, a int, b int, primary key(d, a)) partition by hash(year(d)) partitions 10;")
tk.MustExec("create table t5(d date, a int, b int, primary key(d, a)) partition by hash(month(d)) partitions 10;")

var input []string
var output []struct {
Expand Down
5 changes: 3 additions & 2 deletions expression/testdata/partition_pruner_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"explain select * from t1 left join t2 on true where t1.a = 1 and false",
"explain select * from t1 left join t2 on true where t1.a = 1 and null",
"explain select * from t1 left join t2 on true where t1.a = null",
"explain select * from t1 where t1.a > 7 and t1.a < 3",
"explain select * from t1 where t1.a between 7 and 3"
// Case with date.
Copy link
Member

Choose a reason for hiding this comment

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

json doesn't support comment?

"explain select * from t4 where d = '2019-10-07 10:40:00' and a = 1",
"explain select * from t5 where d = '2019-10-07'"
]
}
]
12 changes: 8 additions & 4 deletions expression/testdata/partition_pruner_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@
]
},
{
"SQL": "explain select * from t1 where t1.a > 7 and t1.a < 3",
"SQL": "explain select * from t4 where d = '2019-10-07 10:40:00' and a = 1",
"Result": [
"TableDual_6 0.00 root rows:0"
"IndexLookUp_8 1.00 root ",
"├─IndexScan_6 1.00 cop table:t4, partition:p9, index:d, a, range:[2019-10-07 10:40:00 1,2019-10-07 10:40:00 1], keep order:false, stats:pseudo",
"└─TableScan_7 1.00 cop table:t4, partition:p9, keep order:false, stats:pseudo"
]
},
{
"SQL": "explain select * from t1 where t1.a between 7 and 3",
"SQL": "explain select * from t5 where d = '2019-10-07'",
"Result": [
"TableDual_6 0.00 root rows:0"
"IndexLookUp_11 10.00 root ",
"├─IndexScan_9 10.00 cop table:t5, partition:p0, index:d, a, range:[2019-10-07,2019-10-07], keep order:false, stats:pseudo",
"└─TableScan_10 10.00 cop table:t5, partition:p0, keep order:false, stats:pseudo"
]
}
]
Expand Down