Skip to content

Commit

Permalink
Merge branch 'agg_bit_type' of https://github.com/XuHuaiyu/tidb into …
Browse files Browse the repository at this point in the history
…agg_bit_type
  • Loading branch information
XuHuaiyu committed Jul 31, 2018
2 parents 92d2401 + fe07ec0 commit b582aa0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
5 changes: 5 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,11 @@ func (s *testSuite) TestUpdate(c *C) {
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1265 Data Truncated"))
r = tk.MustQuery("select * from decimals")
r.Check(testkit.Rows("202"))

tk.MustExec("drop table t")
tk.MustExec("CREATE TABLE `t` ( `c1` year DEFAULT NULL, `c2` year DEFAULT NULL, `c3` date DEFAULT NULL, `c4` datetime DEFAULT NULL, KEY `idx` (`c1`,`c2`))")
_, err = tk.Exec("UPDATE t SET c2=16777215 WHERE c1>= -8388608 AND c1 < -9 ORDER BY c1 LIMIT 2")
c.Assert(err.Error(), Equals, "cannot convert datum from bigint to type year.")
}

// TestUpdateCastOnlyModifiedValues for issue #4514.
Expand Down
35 changes: 21 additions & 14 deletions plan/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,22 +601,29 @@ func (p *LogicalJoin) tryToGetIndexJoin(prop *requiredProp) ([]PhysicalPlan, boo
plans = append(plans, join...)
}
case InnerJoin:
join := p.getIndexJoinByOuterIdx(prop, 0)
if join != nil {
// If the plan is not nil and matches the hint, return it directly.
if leftOuter {
return join, true
}
plans = append(plans, join...)
lhsCardinality := p.Children()[0].statsInfo().Count()
rhsCardinality := p.Children()[1].statsInfo().Count()

leftJoins := p.getIndexJoinByOuterIdx(prop, 0)
if leftOuter && leftJoins != nil {
return leftJoins, true
}
join = p.getIndexJoinByOuterIdx(prop, 1)
if join != nil {
// If the plan is not nil and matches the hint, return it directly.
if rightOuter {
return join, true
}
plans = append(plans, join...)

rightJoins := p.getIndexJoinByOuterIdx(prop, 1)
if rightOuter && rightJoins != nil {
return rightJoins, true
}

if leftJoins != nil && lhsCardinality < rhsCardinality {
return leftJoins, leftOuter
}

if rightJoins != nil && rhsCardinality < lhsCardinality {
return rightJoins, rightOuter
}

plans = append(plans, leftJoins...)
plans = append(plans, rightJoins...)
}
return plans, false
}
Expand Down
28 changes: 28 additions & 0 deletions plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (b *planBuilder) buildResultSetNode(node ast.ResultSetNode) LogicalPlan {
b.err = ErrUnsupportedType.GenByArgs(v)
}
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
if v, ok := p.(*DataSource); ok {
Expand Down Expand Up @@ -266,11 +267,13 @@ func (b *planBuilder) buildJoin(joinNode *ast.Join) LogicalPlan {

leftPlan := b.buildResultSetNode(joinNode.Left)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}

rightPlan := b.buildResultSetNode(joinNode.Right)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
joinPlan := LogicalJoin{StraightJoin: joinNode.StraightJoin || b.inStraightJoin}.init(b.ctx)
Expand Down Expand Up @@ -1622,16 +1625,19 @@ func (b *planBuilder) buildSelect(sel *ast.SelectStmt) LogicalPlan {
p = b.buildTableDual()
}
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
originalFields := sel.Fields.Fields
sel.Fields.Fields = b.unfoldWildStar(p, sel.Fields.Fields)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
if sel.GroupBy != nil {
p, gbyCols = b.resolveGbyExprs(p, sel.GroupBy, sel.Fields.Fields)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
Expand All @@ -1646,6 +1652,7 @@ func (b *planBuilder) buildSelect(sel *ast.SelectStmt) LogicalPlan {
if sel.Where != nil {
p = b.buildSelection(p, sel.Where, nil)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
Expand All @@ -1656,6 +1663,7 @@ func (b *planBuilder) buildSelect(sel *ast.SelectStmt) LogicalPlan {
if hasAgg {
aggFuncs, totalMap = b.extractAggFuncs(sel.Fields.Fields)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
var aggIndexMap map[int]int
Expand All @@ -1664,36 +1672,42 @@ func (b *planBuilder) buildSelect(sel *ast.SelectStmt) LogicalPlan {
totalMap[k] = aggIndexMap[v]
}
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
var oldLen int
p, oldLen = b.buildProjection(p, sel.Fields.Fields, totalMap)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
if sel.Having != nil {
b.curClause = havingClause
p = b.buildSelection(p, sel.Having.Expr, havingMap)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
if sel.Distinct {
p = b.buildDistinct(p, oldLen)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
if sel.OrderBy != nil {
p = b.buildSort(p, sel.OrderBy.Items, orderMap)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
if sel.Limit != nil {
p = b.buildLimit(p, sel.Limit)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
Expand Down Expand Up @@ -2024,6 +2038,7 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) Plan {
sel := &ast.SelectStmt{Fields: &ast.FieldList{}, From: update.TableRefs, Where: update.Where, OrderBy: update.Order, Limit: update.Limit}
p := b.buildResultSetNode(sel.From.TableRefs)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}

Expand All @@ -2040,23 +2055,27 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) Plan {
if sel.Where != nil {
p = b.buildSelection(p, sel.Where, nil)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
if sel.OrderBy != nil {
p = b.buildSort(p, sel.OrderBy.Items, nil)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
if sel.Limit != nil {
p = b.buildLimit(p, sel.Limit)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
orderedList, np := b.buildUpdateLists(tableList, update.List, p)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
p = np
Expand All @@ -2066,6 +2085,10 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) Plan {
}.init(b.ctx)
updt.SetSchema(p.Schema())
updt.SelectPlan, b.err = doOptimize(b.optFlag, p)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
updt.ResolveIndices()
return updt
}
Expand Down Expand Up @@ -2197,24 +2220,28 @@ func (b *planBuilder) buildDelete(delete *ast.DeleteStmt) Plan {
}
p := b.buildResultSetNode(sel.From.TableRefs)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}

if sel.Where != nil {
p = b.buildSelection(p, sel.Where, nil)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
if sel.OrderBy != nil {
p = b.buildSort(p, sel.OrderBy.Items, nil)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
if sel.Limit != nil {
p = b.buildLimit(p, sel.Limit)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
}
Expand All @@ -2230,6 +2257,7 @@ func (b *planBuilder) buildDelete(delete *ast.DeleteStmt) Plan {
}.init(b.ctx)
del.SelectPlan, b.err = doOptimize(b.optFlag, p)
if b.err != nil {
b.err = errors.Trace(b.err)
return nil
}
del.SetSchema(expression.NewSchema())
Expand Down

0 comments on commit b582aa0

Please sign in to comment.