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

Merged
merged 2 commits into from
Mar 18, 2020
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
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
2 changes: 2 additions & 0 deletions expression/partition_pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (s *testSuite2) TestHashPartitionPruner(c *C) {
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: 4 additions & 1 deletion expression/testdata/partition_pruner_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
// Negtive cases.
"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 left join t2 on true where t1.a = null",
// Case with date.
"explain select * from t4 where d = '2019-10-07 10:40:00' and a = 1",
"explain select * from t5 where d = '2019-10-07'"
]
}
]
14 changes: 14 additions & 0 deletions expression/testdata/partition_pruner_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@
"Result": [
"TableDual_8 0.00 root rows:0"
]
},
{
"SQL": "explain select * from t4 where d = '2019-10-07 10:40:00' and a = 1",
"Result": [
"Point_Get_6 1.00 root table:t4, index:d a, partition:p9"
]
},
{
"SQL": "explain select * from t5 where d = '2019-10-07'",
"Result": [
"IndexLookUp_11 10.00 root ",
"├─IndexRangeScan_9(Build) 10.00 cop[tikv] table:t5, partition:p0, index:d, a, range:[2019-10-07,2019-10-07], keep order:false, stats:pseudo",
"└─TableRowIDScan_10(Probe) 10.00 cop[tikv] table:t5, partition:p0, keep order:false, stats:pseudo"
]
}
]
}
Expand Down