Skip to content
Open
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
56 changes: 56 additions & 0 deletions enginetest/queries/join_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,62 @@ on w = 0;`,
}

var JoinScriptTests = []ScriptTest{
{
// dolthub/dolt#11421: EXISTS with select-list window (ROW_NUMBER)
Name: "EXISTS with select-list window function",
SetUpScript: []string{
"CREATE TABLE t(id INT PRIMARY KEY, a INT);",
"CREATE TABLE u(x INT);",
"CREATE TABLE u2(x INT, y INT);",
"INSERT INTO t VALUES (1,1),(2,2),(3,3);",
"INSERT INTO u VALUES (1),(1),(2);",
"INSERT INTO u2 VALUES (1,10),(1,20),(2,30);",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT id FROM t WHERE EXISTS (SELECT 1 FROM u WHERE u.x = t.a) ORDER BY id",
Expected: []sql.Row{
{1},
{2},
},
},
{
Query: "SELECT id FROM t WHERE EXISTS (SELECT ROW_NUMBER() OVER () FROM u WHERE u.x = t.a) ORDER BY id",
Expected: []sql.Row{
{1},
{2},
},
},
{
Query: "SELECT id FROM t WHERE NOT EXISTS (SELECT ROW_NUMBER() OVER () FROM u WHERE u.x = t.a) ORDER BY id",
Expected: []sql.Row{
{3},
},
},
// Multi-window EXISTS bodies (>=2 distinct partition schemes) silently returned wrong
// rows on main (1,2,3 / empty) instead of crashing; assert correct existence.
{
Query: "SELECT id FROM t WHERE EXISTS (SELECT ROW_NUMBER() OVER (PARTITION BY u2.y), RANK() OVER (ORDER BY u2.y) FROM u2 WHERE u2.x = t.a) ORDER BY id",
Expected: []sql.Row{
{1},
{2},
},
},
{
Query: "SELECT id FROM t WHERE NOT EXISTS (SELECT ROW_NUMBER() OVER (PARTITION BY u2.y), RANK() OVER (ORDER BY u2.y) FROM u2 WHERE u2.x = t.a) ORDER BY id",
Expected: []sql.Row{
{3},
},
},
// Uncorrelated NOT EXISTS + window: on main this panics (nil pointer in
// sql.EvaluateCondition via existsIter) via the len(joinFilters)==0 path;
// patched build correctly returns empty.
{
Query: "SELECT id FROM t WHERE NOT EXISTS (SELECT ROW_NUMBER() OVER () FROM u) ORDER BY id",
Expected: []sql.Row{},
},
},
},
{
Name: "Simple join query",
SetUpScript: []string{},
Expand Down
4 changes: 4 additions & 0 deletions sql/analyzer/unnest_exists_subqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func simplifyPartialJoinParents(n sql.Node) (sql.Node, bool) {
switch n := ret.(type) {
case *plan.Having:
return nil, false
case *plan.Window:
// Window functions are 1:1 row-preserving (empty child -> empty output; see
// sql/rowexec/rel_iters.go windowToIter), so stripping cannot change row existence.
ret = n.Children()[0]
case *plan.Project, *plan.GroupBy, *plan.Sort, *plan.Distinct, *plan.TopN, *plan.Limit:
// TODO: In most cases, it's necessary to remove *plan.Limit because child Filter nodes will have been
// hoisted out. But what if Limit.Limit evals to 0? https://github.com/dolthub/dolt/issues/10493
Expand Down
Loading