Skip to content

Commit bccff37

Browse files
authored
fix panic over SetOps with joins and subqueries (#3145)
1 parent 7ee1006 commit bccff37

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

enginetest/queries/script_queries.go

Lines changed: 87 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,73 @@ where
1127011282
},
1127111283
},
1127211284
},
11285+
{
11286+
// TODO: Doltgres does not support this query
11287+
// https://github.com/dolthub/dolt/issues/9631
11288+
Name: "test union/intersect/except over subqueries over joins",
11289+
Dialect: "mysql",
11290+
SetUpScript: []string{
11291+
"create table t1 (i int primary key);",
11292+
"create table t2 (j int primary key);",
11293+
"insert into t1 values (0), (1);",
11294+
"insert into t2 values (0), (2);",
11295+
},
11296+
Assertions: []ScriptTestAssertion{
11297+
{
11298+
Query: `
11299+
select * from t1 union (
11300+
select j from t2
11301+
where (
11302+
j > 10
11303+
or
11304+
j in (
11305+
select j from t1 join t2
11306+
)
11307+
)
11308+
);
11309+
`,
11310+
Expected: []sql.Row{
11311+
{0},
11312+
{1},
11313+
{2},
11314+
},
11315+
},
11316+
{
11317+
Query: `
11318+
select * from t1 intersect (
11319+
select j from t2
11320+
where (
11321+
j > 10
11322+
or
11323+
j in (
11324+
select j from t1 join t2
11325+
)
11326+
)
11327+
);
11328+
`,
11329+
Expected: []sql.Row{
11330+
{0},
11331+
},
11332+
},
11333+
{
11334+
Query: `
11335+
select * from t1 except (
11336+
select j from t2
11337+
where (
11338+
j > 10
11339+
or
11340+
j in (
11341+
select j from t1 join t2
11342+
)
11343+
)
11344+
);
11345+
`,
11346+
Expected: []sql.Row{
11347+
{1},
11348+
},
11349+
},
11350+
},
11351+
},
1127311352
}
1127411353

1127511354
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)