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: fix the col resolution priority between outer scope and nature join folded col #43247

Merged
merged 1 commit into from
Apr 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,18 @@ func (er *expressionRewriter) toColumn(v *ast.ColumnName) {
er.ctxStackAppend(column, er.names[idx])
return
}
if _, ok := er.p.(*LogicalUnionAll); ok && v.Table.O != "" {
er.err = ErrTablenameNotAllowedHere.GenWithStackByArgs(v.Table.O, "SELECT", clauseMsg[er.b.curClause])
return
}
col, name, err := findFieldNameFromNaturalUsingJoin(er.p, v)
if err != nil {
er.err = err
return
} else if col != nil {
er.ctxStackAppend(col, name)
return
}
for i := len(er.b.outerSchemas) - 1; i >= 0; i-- {
outerSchema, outerName := er.b.outerSchemas[i], er.b.outerNames[i]
idx, err = expression.FindFieldName(outerName, v)
Expand All @@ -2037,18 +2049,6 @@ func (er *expressionRewriter) toColumn(v *ast.ColumnName) {
return
}
}
if _, ok := er.p.(*LogicalUnionAll); ok && v.Table.O != "" {
er.err = ErrTablenameNotAllowedHere.GenWithStackByArgs(v.Table.O, "SELECT", clauseMsg[er.b.curClause])
return
}
col, name, err := findFieldNameFromNaturalUsingJoin(er.p, v)
if err != nil {
er.err = err
return
} else if col != nil {
er.ctxStackAppend(col, name)
return
}
if er.b.curClause == globalOrderByClause {
er.b.curClause = orderByClause
}
Expand Down
13 changes: 13 additions & 0 deletions planner/core/expression_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,16 @@ func TestConvertIfNullToCast(t *testing.T) {
"[ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
))
}

func TestColResolutionPriBetweenOuterAndNatureJoin(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("DROP TABLE if exists t0;")
tk.MustExec("DROP VIEW if exists t0;")
tk.MustExec("CREATE TABLE t0(c0 TEXT(328) );")
tk.MustExec("CREATE definer='root'@'localhost' VIEW v0(c0) AS SELECT 'c' FROM t0;")
tk.MustExec("INSERT INTO t0 VALUES (-12);")
tk.MustQuery("SELECT v0.c0 AS c0 FROM v0 NATURAL RIGHT JOIN t0 WHERE (1 !=((v0.c0)REGEXP(-7)));").Check(testkit.Rows())
tk.MustQuery("SELECT COUNT(v0.c0) AS c0 FROM v0 WHERE EXISTS(SELECT v0.c0 AS c0 FROM v0 NATURAL RIGHT JOIN t0 WHERE (1 !=((v0.c0)REGEXP(-7))));").Check(testkit.Rows("0"))
}