Skip to content

Commit 17b12d9

Browse files
author
James Cor
committed
fix panic over setop with joins and subqueries
1 parent 466cdf1 commit 17b12d9

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,25 +11218,102 @@ 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"},
1123611248
},
1123711249
},
1123811250
},
1123911251
},
11252+
{
11253+
// https://github.com/dolthub/dolt/issues/9631
11254+
Name: "test union/intersect/except over subqueries over joins",
11255+
SetUpScript: []string{
11256+
"create table t1 (i int primary key);",
11257+
"create table t2 (j int primary key);",
11258+
"insert into t1 values (0), (1);",
11259+
"insert into t2 values (0), (2);",
11260+
},
11261+
Assertions: []ScriptTestAssertion{
11262+
{
11263+
Query: `
11264+
select * from t1 union (
11265+
select j from t2
11266+
where (
11267+
j > 10
11268+
or
11269+
j in (
11270+
select j from t1 join t2
11271+
)
11272+
)
11273+
);
11274+
`,
11275+
Expected: []sql.Row{
11276+
{0},
11277+
{1},
11278+
{2},
11279+
},
11280+
},
11281+
{
11282+
Query: `
11283+
select * from t1 intersect (
11284+
select j from t2
11285+
where (
11286+
j = 1
11287+
or
11288+
j in (
11289+
select j from t1 join t2
11290+
)
11291+
)
11292+
);
11293+
`,
11294+
Expected: []sql.Row{
11295+
{0},
11296+
},
11297+
},
11298+
{
11299+
Query: `
11300+
select * from t1 except (
11301+
select j from t2
11302+
where (
11303+
j = 1
11304+
or
11305+
j in (
11306+
select j from t1 join t2
11307+
)
11308+
)
11309+
);
11310+
`,
11311+
Expected: []sql.Row{
11312+
{1},
11313+
},
11314+
},
11315+
},
11316+
},
1124011317
}
1124111318

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