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 fail when we compare multi fields in the subquery #21699

Merged
merged 5 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ func (er *expressionRewriter) handleCompareSubquery(ctx context.Context, v *ast.
er.handleEQAll(lexpr, rexpr, np)
} else {
// `a = any(subq)` will be rewriten as `a in (subq)`.
er.asScalar = true
er.buildSemiApplyFromEqualSubq(np, lexpr, rexpr, false)
if er.err != nil {
return v, true
Expand All @@ -538,6 +539,7 @@ func (er *expressionRewriter) handleCompareSubquery(ctx context.Context, v *ast.
} else if v.Op == opcode.NE {
if v.All {
// `a != all(subq)` will be rewriten as `a not in (subq)`.
er.asScalar = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case does not cover this?

er.buildSemiApplyFromEqualSubq(np, lexpr, rexpr, true)
if er.err != nil {
return v, true
Expand Down
21 changes: 21 additions & 0 deletions planner/core/expression_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,24 @@ func (s *testExpressionRewriterSuite) TestIssue17652(c *C) {
tk.MustQuery("select ifnull(max(x), 0) from t").Check(
testkit.Rows("9999999703771440633"))
}

func (s *testExpressionRewriterSuite) TestCompareMultiFieldsInSubquery(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()
tk.MustExec("use test;")
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("CREATE TABLE t1(c1 int, c2 int);")
tk.MustExec("CREATE TABLE t2(c1 int, c2 int);")
tk.MustExec("INSERT INTO t1 VALUES (0, 0), (NULL, NULL);")
tk.MustExec("INSERT INTO t2 VALUES (0, 0), (NULL, NULL);")
// issue #13551 and #21674
tk.MustQuery("SELECT * FROM t2 WHERE (SELECT c1, c2 FROM t2 LIMIT 1) = ANY (SELECT c1, c2 FROM t1);").Check(testkit.Rows("0 0", "<nil> <nil>"))
tk.MustQuery("SELECT * FROM t2 WHERE (SELECT c1 FROM t2 LIMIT 1) = ANY (SELECT c1 FROM t1);").Check(testkit.Rows("0 0", "<nil> <nil>"))
tk.MustQuery("SELECT * FROM t2 WHERE (SELECT c1, c2 FROM t2 order by c1 LIMIT 1) = ANY (SELECT c1, c2 FROM t1);").Check(testkit.Rows())
}