From e6d4eaf57ac7e15f5eababf7b2be081bed132375 Mon Sep 17 00:00:00 2001 From: James Cor Date: Fri, 16 Feb 2024 14:25:00 -0800 Subject: [PATCH] fix and test --- enginetest/join_planning_tests.go | 10 ++++++++++ enginetest/queries/queries.go | 8 ++++++++ sql/analyzer/unnest_exists_subqueries.go | 8 ++------ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/enginetest/join_planning_tests.go b/enginetest/join_planning_tests.go index 915dd6cd7c..b505207a03 100644 --- a/enginetest/join_planning_tests.go +++ b/enginetest/join_planning_tests.go @@ -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}, diff --git a/enginetest/queries/queries.go b/enginetest/queries/queries.go index 2733d2fc0a..29a9be997a 100644 --- a/enginetest/queries/queries.go +++ b/enginetest/queries/queries.go @@ -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{}, diff --git a/sql/analyzer/unnest_exists_subqueries.go b/sql/analyzer/unnest_exists_subqueries.go index 8b896e8f18..880647e617 100644 --- a/sql/analyzer/unnest_exists_subqueries.go +++ b/sql/analyzer/unnest_exists_subqueries.go @@ -119,13 +119,8 @@ 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: @@ -133,6 +128,7 @@ func unnestExistSubqueries(ctx *sql.Context, scope *plan.Scope, a *Analyzer, fil case *expression.Not: if esq, ok := e.Child.(*plan.ExistsSubquery); ok { sq = esq.Query + joinType = plan.JoinTypeAnti } default: }