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

fix NOT expression in conjunction with WHERE EXISTS(<subquery>) #2331

Merged
merged 1 commit into from
Feb 18, 2024
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 enginetest/join_planning_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,16 @@ where u in (select * from rec);`,
{3, 3},
},
},
{
q: "select * from xy where not exists (select * from empty_tbl) and x is not null order by x",
types: []plan.JoinType{plan.JoinTypeLeftOuter},
exp: []sql.Row{
{0, 2},
{1, 0},
{2, 1},
{3, 3},
},
},
{
q: "select /*+ MERGE_JOIN(xy,uv) */ * from xy where x not in (select u from uv WHERE u = 2) order by x",
types: []plan.JoinType{plan.JoinTypeLeftOuterMerge},
Expand Down
8 changes: 8 additions & 0 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7667,6 +7667,14 @@ Select * from (
{1},
},
},
{
Query: `SELECT i FROM mytable WHERE EXISTS (SELECT 1 from mytable) AND i IS NOT NULL;`,
Expected: []sql.Row{
{1},
{2},
{3},
},
},
{
Query: `SELECT * FROM two_pk WHERE EXISTS (SELECT pk FROM one_pk WHERE pk > 4)`,
Expected: []sql.Row{},
Expand Down
8 changes: 2 additions & 6 deletions sql/analyzer/unnest_exists_subqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,16 @@ func unnestExistSubqueries(ctx *sql.Context, scope *plan.Scope, a *Analyzer, fil
var s *hoistSubquery
var err error

joinType := plan.JoinTypeSemi
if not, ok := f.(*expression.Not); ok {
f = not.Child
joinType = plan.JoinTypeAnti
}

// match subquery expression
joinType := plan.JoinTypeSemi
var sq *plan.Subquery
switch e := f.(type) {
case *plan.ExistsSubquery:
sq = e.Query
case *expression.Not:
if esq, ok := e.Child.(*plan.ExistsSubquery); ok {
sq = esq.Query
joinType = plan.JoinTypeAnti
}
default:
}
Expand Down
Loading