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

planner: add missing column for Apply convert to Join #27246

Merged
merged 2 commits into from
Aug 17, 2021
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
10 changes: 10 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10208,3 +10208,13 @@ func (s *testIntegrationSuite) TestConstPropNullFunctions(c *C) {
tk.MustExec("insert into t2 values (0, 'c', null), (1, null, 0.1), (3, 'b', 0.01), (2, 'q', 0.12), (null, 'a', -0.1), (null, null, null)")
tk.MustQuery("select * from t2 where t2.i2=((select count(1) from t1 where t1.i1=t2.i2))").Check(testkit.Rows("1 <nil> 0.1"))
}

func (s *testIntegrationSuite) TestIssue27233(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t;")
tk.MustExec("CREATE TABLE `t` (\n `COL1` tinyint(45) NOT NULL,\n `COL2` tinyint(45) NOT NULL,\n PRIMARY KEY (`COL1`,`COL2`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
tk.MustExec("insert into t values(122,100),(124,-22),(124,34),(127,103);")
tk.MustQuery("SELECT col2 FROM t AS T1 WHERE ( SELECT count(DISTINCT COL1, COL2) FROM t AS T2 WHERE T2.COL1 > T1.COL1 ) > 2 ;").
Check(testkit.Rows("100"))
}
33 changes: 33 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,39 @@ func (s *testPlanSuite) TestEnumIndex(c *C) {
}
}

func (s *testPlanSuite) TestIssue27233(c *C) {
var (
input []string
output []struct {
SQL string
Plan []string
Result []string
}
)
s.testData.GetTestCases(c, &input, &output)
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer func() {
dom.Close()
store.Close()
}()
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `PK_S_MULTI_31` (\n `COL1` tinyint(45) NOT NULL,\n `COL2` tinyint(45) NOT NULL,\n PRIMARY KEY (`COL1`,`COL2`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
tk.MustExec("insert into PK_S_MULTI_31 values(122,100),(124,-22),(124,34),(127,103);")

for i, ts := range input {
s.testData.OnRecord(func() {
output[i].SQL = ts
output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain format='brief'" + ts).Rows())
output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(ts).Sort().Rows())
})
tk.MustQuery("explain format='brief' " + ts).Check(testkit.Rows(output[i].Plan...))
tk.MustQuery(ts).Sort().Check(testkit.Rows(output[i].Result...))
}
}

func (s *testPlanSuite) TestPossibleProperties(c *C) {
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
Expand Down
32 changes: 19 additions & 13 deletions planner/core/rule_decorrelate.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,28 @@ func (s *decorrelateSolver) optimize(ctx context.Context, p LogicalPlan) (Logica
resetNotNullFlag(apply.schema, outerPlan.Schema().Len(), apply.schema.Len())

for i, aggFunc := range agg.AggFuncs {
switch expr := aggFunc.Args[0].(type) {
case *expression.Column:
if idx := apply.schema.ColumnIndex(expr); idx != -1 {
desc, err := aggregation.NewAggFuncDesc(agg.ctx, agg.AggFuncs[i].Name, []expression.Expression{apply.schema.Columns[idx]}, agg.AggFuncs[i].HasDistinct)
if err != nil {
return nil, err
aggArgs := make([]expression.Expression, 0, len(aggFunc.Args))
for _, arg := range aggFunc.Args {
switch expr := arg.(type) {
case *expression.Column:
if idx := apply.schema.ColumnIndex(expr); idx != -1 {
aggArgs = append(aggArgs, apply.schema.Columns[idx])
} else {
aggArgs = append(aggArgs, expr)
}
newAggFuncs = append(newAggFuncs, desc)
case *expression.ScalarFunction:
expr.RetType = expr.RetType.Clone()
expr.RetType.Flag &= ^mysql.NotNullFlag
aggArgs = append(aggArgs, expr)
default:
aggArgs = append(aggArgs, expr)
}
case *expression.ScalarFunction:
expr.RetType = expr.RetType.Clone()
expr.RetType.Flag &= ^mysql.NotNullFlag
newAggFuncs = append(newAggFuncs, aggFunc)
default:
newAggFuncs = append(newAggFuncs, aggFunc)
}
desc, err := aggregation.NewAggFuncDesc(agg.ctx, agg.AggFuncs[i].Name, aggArgs, agg.AggFuncs[i].HasDistinct)
if err != nil {
return nil, err
}
newAggFuncs = append(newAggFuncs, desc)
}
agg.AggFuncs = newAggFuncs
np, err := s.optimize(ctx, p)
Expand Down
6 changes: 6 additions & 0 deletions planner/core/testdata/plan_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -716,5 +716,11 @@
"select e from t where e = ''",
"select e from t where e != ''"
]
},
{
"name": "TestIssue27233",
"cases": [
"SELECT col2 FROM PK_S_MULTI_31 AS T1 WHERE (SELECT count(DISTINCT COL1, COL2) FROM PK_S_MULTI_31 AS T2 WHERE T2.COL1>T1.COL1)>2 order by col2;"
]
}
]
22 changes: 22 additions & 0 deletions planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -2559,5 +2559,27 @@
]
}
]
},
{
"Name": "TestIssue27233",
"Cases": [
{
"SQL": "SELECT col2 FROM PK_S_MULTI_31 AS T1 WHERE (SELECT count(DISTINCT COL1, COL2) FROM PK_S_MULTI_31 AS T2 WHERE T2.COL1>T1.COL1)>2 order by col2;",
"Plan": [
"Sort 0.80 root test.pk_s_multi_31.col2",
"└─Projection 0.80 root test.pk_s_multi_31.col2",
" └─Selection 0.80 root gt(Column#7, 2)",
" └─HashAgg 1.00 root group by:test.pk_s_multi_31.col1, test.pk_s_multi_31.col2, funcs:firstrow(test.pk_s_multi_31.col2)->test.pk_s_multi_31.col2, funcs:count(distinct test.pk_s_multi_31.col1, test.pk_s_multi_31.col2)->Column#7",
" └─HashJoin 100000000.00 root CARTESIAN left outer join, other cond:gt(test.pk_s_multi_31.col1, test.pk_s_multi_31.col1)",
" ├─IndexReader(Build) 10000.00 root index:IndexFullScan",
" │ └─IndexFullScan 10000.00 cop[tikv] table:T2, index:PRIMARY(COL1, COL2) keep order:false, stats:pseudo",
" └─IndexReader(Probe) 10000.00 root index:IndexFullScan",
" └─IndexFullScan 10000.00 cop[tikv] table:T1, index:PRIMARY(COL1, COL2) keep order:false, stats:pseudo"
],
"Result": [
"100"
]
}
]
}
]