Skip to content

Commit

Permalink
planner: fix bug caused by wrong column indice in NominalSort's sort …
Browse files Browse the repository at this point in the history
…item (#31324)

close #31035
  • Loading branch information
guo-shaoge authored Jan 10, 2022
1 parent 0c19f92 commit 4baab3c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
9 changes: 9 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5196,6 +5196,15 @@ func (s *testIntegrationSuite) TestIssue20510(c *C) {
))
}

func (s *testIntegrationSuite) TestIssue31035(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1(c1 longtext, c2 decimal(37, 4), unique key(c1(10)), unique key(c2));")
tk.MustExec("insert into t1 values('眐', -962541614831459.7458);")
tk.MustQuery("select * from t1 order by c2 + 10;").Check(testkit.Rows("眐 -962541614831459.7458"))
}

// TestDNFCondSelectivityWithConst test selectivity calculation with DNF conditions with one is const.
// Close https://github.com/pingcap/tidb/issues/31096
func (s *testIntegrationSuite) TestDNFCondSelectivityWithConst(c *C) {
Expand Down
31 changes: 26 additions & 5 deletions planner/core/resolve_indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package core

import (
"github.com/pingcap/errors"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/planner/util"
"github.com/pingcap/tidb/util/disjointset"
)

Expand Down Expand Up @@ -410,21 +412,40 @@ func (p *basePhysicalAgg) ResolveIndices() (err error) {
return
}

// ResolveIndices implements Plan interface.
func (p *PhysicalSort) ResolveIndices() (err error) {
err = p.basePhysicalPlan.ResolveIndices()
func resolveIndicesForSort(p basePhysicalPlan) (err error) {
err = p.ResolveIndices()
if err != nil {
return err
}
for _, item := range p.ByItems {
item.Expr, err = item.Expr.ResolveIndices(p.children[0].Schema())

var byItems []*util.ByItems
switch x := p.self.(type) {
case *PhysicalSort:
byItems = x.ByItems
case *NominalSort:
byItems = x.ByItems
default:
return errors.Errorf("expect PhysicalSort or NominalSort, but got %s", p.TP())
}
for _, item := range byItems {
item.Expr, err = item.Expr.ResolveIndices(p.Children()[0].Schema())
if err != nil {
return err
}
}
return err
}

// ResolveIndices implements Plan interface.
func (p *PhysicalSort) ResolveIndices() (err error) {
return resolveIndicesForSort(p.basePhysicalPlan)
}

// ResolveIndices implements Plan interface.
func (p *NominalSort) ResolveIndices() (err error) {
return resolveIndicesForSort(p.basePhysicalPlan)
}

// ResolveIndices implements Plan interface.
func (p *PhysicalWindow) ResolveIndices() (err error) {
err = p.physicalSchemaProducer.ResolveIndices()
Expand Down

0 comments on commit 4baab3c

Please sign in to comment.