Skip to content

Commit 49e72cb

Browse files
author
James Cor
committed
fix panic over setop with joins and subqueries
1 parent 7ee1006 commit 49e72cb

File tree

3 files changed

+90
-11
lines changed

3 files changed

+90
-11
lines changed

enginetest/queries/script_queries.go

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11206,8 +11206,8 @@ where
1120611206

1120711207
{
1120811208
// TODO: This test currently fails in Doltgres because Doltgres does not allow `create table...as select...`
11209-
// even though it's a valid Postgres query. Remove Dialect tag once fixed in Doltgres
11210-
// https://github.com/dolthub/doltgresql/issues/1669
11209+
// even though it's a valid Postgres query. Remove Dialect tag once fixed in Doltgres
11210+
// https://github.com/dolthub/doltgresql/issues/1669
1121111211
Dialect: "mysql",
1121211212
Name: "union field indexes",
1121311213
SetUpScript: []string{
@@ -11218,18 +11218,30 @@ where
1121811218
},
1121911219
Assertions: []ScriptTestAssertion{
1122011220
{
11221-
Query: "select * from " +
11222-
"(select id, words from t union " +
11223-
"select id,words from t2) as combined where combined.id=1",
11221+
Query: `
11222+
select * from (
11223+
select id, words from t
11224+
union
11225+
select id, words from t2
11226+
) as combined
11227+
where
11228+
combined.id = 1;
11229+
`,
1122411230
Expected: []sql.Row{
1122511231
{1, "foo"},
1122611232
{1, "boo"},
1122711233
},
1122811234
},
1122911235
{
11230-
Query: "select * from " +
11231-
"(select 'parent' as tbl, id, words from t union " +
11232-
"select 'child' as tbl, id,words from t2) as combined where combined.id=1",
11236+
Query: `
11237+
select * from (
11238+
select 'parent' as tbl, id, words from t
11239+
union
11240+
select 'child' as tbl, id, words from t2
11241+
) as combined
11242+
where
11243+
combined.id = 1;
11244+
`,
1123311245
Expected: []sql.Row{
1123411246
{"parent", 1, "foo"},
1123511247
{"child", 1, "boo"},
@@ -11270,6 +11282,71 @@ where
1127011282
},
1127111283
},
1127211284
},
11285+
{
11286+
// https://github.com/dolthub/dolt/issues/9631
11287+
Name: "test union/intersect/except over subqueries over joins",
11288+
SetUpScript: []string{
11289+
"create table t1 (i int primary key);",
11290+
"create table t2 (j int primary key);",
11291+
"insert into t1 values (0), (1);",
11292+
"insert into t2 values (0), (2);",
11293+
},
11294+
Assertions: []ScriptTestAssertion{
11295+
{
11296+
Query: `
11297+
select * from t1 union (
11298+
select j from t2
11299+
where (
11300+
j > 10
11301+
or
11302+
j in (
11303+
select j from t1 join t2
11304+
)
11305+
)
11306+
);
11307+
`,
11308+
Expected: []sql.Row{
11309+
{0},
11310+
{1},
11311+
{2},
11312+
},
11313+
},
11314+
{
11315+
Query: `
11316+
select * from t1 intersect (
11317+
select j from t2
11318+
where (
11319+
j = 1
11320+
or
11321+
j in (
11322+
select j from t1 join t2
11323+
)
11324+
)
11325+
);
11326+
`,
11327+
Expected: []sql.Row{
11328+
{0},
11329+
},
11330+
},
11331+
{
11332+
Query: `
11333+
select * from t1 except (
11334+
select j from t2
11335+
where (
11336+
j = 1
11337+
or
11338+
j in (
11339+
select j from t1 join t2
11340+
)
11341+
)
11342+
);
11343+
`,
11344+
Expected: []sql.Row{
11345+
{1},
11346+
},
11347+
},
11348+
},
11349+
},
1127311350
}
1127411351

1127511352
var SpatialScriptTests = []ScriptTest{

sql/analyzer/analyzer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ func NewFinalizeUnionSel(sel RuleSelector) RuleSelector {
450450
case
451451
// skip recursive resolve rules
452452
resolveSubqueriesId,
453-
resolveUnionsId:
453+
resolveUnionsId,
454+
// skip redundant finalize rules
455+
assignExecIndexesId:
454456
return false
455457
case finalizeSubqueriesId,
456458
hoistOutOfScopeFiltersId:

sql/analyzer/resolve_unions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ func finalizeUnions(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope
9595

9696
scope.SetJoin(false)
9797

98-
newn, err := n.WithChildren(left, right)
98+
newN, err := n.WithChildren(left, right)
9999
if err != nil {
100100
return nil, transform.SameTree, err
101101
}
102-
return newn, transform.NewTree, nil
102+
return newN, transform.NewTree, nil
103103
})
104104
}

0 commit comments

Comments
 (0)