Skip to content

Commit db47a7c

Browse files
authored
Fix TDS union (finos#4037)
1 parent 5555fee commit db47a7c

File tree

3 files changed

+170
-8
lines changed
  • legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/pure/router/routing
  • legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/legend-engine-xt-relationalStore-core-pure/src/main/resources/core_relational/relational/tds/tests

3 files changed

+170
-8
lines changed

legend-engine-core/legend-engine-core-pure/legend-engine-pure-code-compiled-core/src/main/resources/core/pure/router/routing/router_routing.pure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ function meta::pure::router::routing::shouldStopFunctions(extensions:meta::pure:
864864
groupByWithWindowSubset_K_MANY__Function_MANY__AggregateValue_MANY__String_MANY__String_MANY__String_MANY__TabularDataSet_1_,
865865
concatenate_TabularDataSet_1__TabularDataSet_1__TabularDataSet_1_,
866866
concatenate_TabularDataSet_1__TabularDataSet_MANY__TabularDataSet_1_,
867+
union_T_MANY__T_MANY__T_MANY_,
867868
renameColumns_TabularDataSet_1__Pair_MANY__TabularDataSet_1_,
868869
olapGroupBy_TabularDataSet_1__String_MANY__SortInformation_$0_1$__OlapOperation_1__String_1__TabularDataSet_1_,
869870
olapGroupBy_TabularDataSet_1__String_MANY__OlapOperation_1__String_1__TabularDataSet_1_,

legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/legend-engine-xt-relationalStore-core-pure/src/main/resources/core_relational/relational/tds/tests/testTDSConcatenate.pure

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,27 +199,26 @@ function <<test.Test>> meta::relational::tests::tds::tdsConcatenate::testConcate
199199
}
200200

201201

202-
function <<test.Test, test.ToFix>> meta::relational::tests::tds::tdsConcatenate::testConcatenateWithJoin():Boolean[1]
202+
function <<test.Test>> meta::relational::tests::tds::tdsConcatenate::testConcatenateWithJoin():Boolean[1]
203203
{
204204
let result = execute(
205205
|Person.all()
206206
->project([col(p|$p.lastName, 'lhs_lastName')])
207207
->concatenate(Person.all()->project([col(p|$p.lastName, 'lhs_lastName')]))
208-
->join(Person.all()->project([col(p|$p.lastName, 'rhs_lastName'),col(p|$p.lastName, 'rhs_firstName')]), JoinType.INNER, 'lhs_lastName', 'rhs_lastName')
208+
->join(Person.all()->project([col(p|$p.lastName, 'rhs_lastName'),col(p|$p.firstName, 'rhs_firstName')]), JoinType.INNER, 'lhs_lastName', 'rhs_lastName')
209209
->restrict(['lhs_lastName', 'rhs_firstName'])
210210
,
211211
simpleRelationalMapping,
212212
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
213213

214-
let tds = $result.values->toOne()->sort([asc('lastName')]);
214+
let tds = $result.values->toOne()->sort([asc('lhs_lastName')]);
215215

216216
assertSize($tds.columns, 2);
217217

218-
//assertEquals('',
219-
// $tds.rows->map(r|$r.values->makeString('|'))->makeString(','));
218+
assertEquals('Allen|Anthony,Allen|Anthony,Harris|David,Harris|David,Hill|John,Hill|John,Hill|John,Hill|John,Hill|Oliver,Hill|Oliver,Hill|Oliver,Hill|Oliver,Johnson|John,Johnson|John,Roberts|Fabrice,Roberts|Fabrice,Smith|Peter,Smith|Peter',
219+
$tds.rows->map(r|$r.values->makeString('|'))->makeString(','));
220220

221-
//assertEquals('select "union"."lastName" as "lastName" from (select "root".LASTNAME as "lastName" from personTable as "root" union all select "root".LASTNAME as "lastName" from personTable as "root") as "union" where "union"."lastName" = \'hello\'',
222-
// $result->sqlRemoveFormatting());
221+
assertEquals('select "unionalias_0"."lhs_lastName" as "lhs_lastName", "unionalias_0"."rhs_firstName" as "rhs_firstName" from (select "unionalias_1"."lhs_lastName" as "lhs_lastName", "persontable_1"."rhs_lastName" as "rhs_lastName", "persontable_1"."rhs_firstName" as "rhs_firstName" from (select "unionalias_2"."lhs_lastName" as "lhs_lastName" from (select "root".LASTNAME as "lhs_lastName" from personTable as "root" union all select "root".LASTNAME as "lhs_lastName" from personTable as "root") as "unionalias_2") as "unionalias_1" inner join (select "root".LASTNAME as "rhs_lastName", "root".FIRSTNAME as "rhs_firstName" from personTable as "root") as "persontable_1" on ("unionalias_1"."lhs_lastName" = "persontable_1"."rhs_lastName")) as "unionalias_0"',
222+
$result->sqlRemoveFormatting());
223223

224-
fail('Expectations need updating once it works');
225224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Copyright 2025 Goldman Sachs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import meta::relational::metamodel::*;
16+
import meta::pure::executionPlan::*;
17+
import meta::relational::metamodel::join::*;
18+
import meta::relational::functions::sqlstring::*;
19+
import meta::relational::tests::csv::*;
20+
import meta::relational::tests::model::simple::*;
21+
import meta::relational::mapping::*;
22+
import meta::external::store::relational::runtime::*;
23+
import meta::relational::runtime::*;
24+
import meta::relational::tests::*;
25+
import meta::external::store::relational::tests::*;
26+
27+
28+
function <<test.Test>> meta::relational::tests::projection::union::testSimpleRelationalUnion():Boolean[1]
29+
{
30+
let result = executionPlan(
31+
|Person.all()
32+
->project(~[lastName:p|$p.lastName])
33+
->union(Person.all()->project(~[lastName:p|$p.lastName]))
34+
,
35+
simpleRelationalMapping,
36+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
37+
38+
assertEquals('select "unionalias_0"."lastName" as "lastName" from (select "root".LASTNAME as "lastName" from personTable as "root" union select "root".LASTNAME as "lastName" from personTable as "root") as "unionalias_0"',
39+
$result.rootExecutionNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
40+
}
41+
42+
function <<test.Test>> meta::relational::tests::tds::tdsUnion::testSimpleUnion():Boolean[1]
43+
{
44+
let result = execute(
45+
|Person.all()
46+
->project(p|$p.lastName,'lastName')
47+
->union(Person.all()->project(p|$p.lastName,'lastName'))
48+
,
49+
simpleRelationalMapping,
50+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
51+
52+
let tds = $result.values->toOne()->sort(asc('lastName'));
53+
54+
assertSize($tds.columns, 1);
55+
56+
assertEquals('Allen,Harris,Hill,Johnson,Roberts,Smith',
57+
$tds.rows->map(r|$r.values->makeString('|'))->makeString(','));
58+
59+
assertEquals('select "unionalias_0"."lastName" as "lastName" from (select "root".LASTNAME as "lastName" from personTable as "root" union select "root".LASTNAME as "lastName" from personTable as "root") as "unionalias_0"',
60+
$result->sqlRemoveFormatting());
61+
}
62+
63+
function <<test.Test>> meta::relational::tests::tds::tdsUnion::testUnionWithGroupBy():Boolean[1]
64+
{
65+
let result = executionPlan(
66+
|Person.all()
67+
->project(~[lastName:p|$p.lastName,firstName:p|$p.firstName,age:p|$p.age])
68+
->union(Person.all()->project(~[lastName:p|$p.lastName,firstName:p|$p.firstName,age:p|$p.age]))
69+
->toOne()
70+
->groupBy(~[lastName],~[age:x|$x.age: y|$y->sum()])
71+
,
72+
simpleRelationalMapping,
73+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
74+
75+
76+
assertEquals('select "unionalias_0"."lastName" as "lastName", sum("unionalias_0"."age") as "age" from (select "root".LASTNAME as "lastName", "root".FIRSTNAME as "firstName", "root".AGE as "age" from personTable as "root" union select "root".LASTNAME as "lastName", "root".FIRSTNAME as "firstName", "root".AGE as "age" from personTable as "root") as "unionalias_0" group by "lastName"',
77+
$result.rootExecutionNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
78+
}
79+
80+
function <<test.Test>> meta::relational::tests::tds::tdsUnion::testMultiUnion():Boolean[1]
81+
{
82+
let result = executionPlan(
83+
|Person.all()
84+
->project(~[lastName:p|$p.lastName + '_0'])
85+
->union([
86+
Person.all()->project(~[lastName:p|$p.lastName + '_1']),
87+
Person.all()->project(~[lastName:p|$p.lastName + '_2']),
88+
Person.all()->project(~[lastName:p|$p.lastName + '_3'])
89+
])
90+
,
91+
simpleRelationalMapping,
92+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
93+
94+
assertEquals('select "unionalias_0"."lastName" as "lastName" from (select concat("root".LASTNAME, \'_0\') as "lastName" from personTable as "root" union select concat("root".LASTNAME, \'_1\') as "lastName" from personTable as "root" union select concat("root".LASTNAME, \'_2\') as "lastName" from personTable as "root" union select concat("root".LASTNAME, \'_3\') as "lastName" from personTable as "root") as "unionalias_0"',
95+
$result.rootExecutionNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
96+
}
97+
98+
function <<test.Test>> meta::relational::tests::tds::tdsUnion::testUnionWithPreOperation():Boolean[1]
99+
{
100+
let result = executionPlan(
101+
|Person.all()
102+
->project(~[lastName:p|$p.lastName])
103+
->filter(p|$p.lastName != 'hello')
104+
->union(Person.all()->project(~[lastName:p|$p.lastName]))
105+
,
106+
simpleRelationalMapping,
107+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
108+
109+
assertEquals('select "unionalias_0"."lastName" as "lastName" from (select "root".LASTNAME as "lastName" from personTable as "root" where "root".LASTNAME <> \'hello\' or "root".LASTNAME is null union select "root".LASTNAME as "lastName" from personTable as "root") as "unionalias_0"',
110+
$result.rootExecutionNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
111+
}
112+
113+
114+
function <<test.Test>> meta::relational::tests::tds::tdsUnion::testUnionWithPostOperation():Boolean[1]
115+
{
116+
let result = executionPlan(
117+
|Person.all()
118+
->project(~[lastName:p|$p.lastName])
119+
->union(Person.all()->project(~[lastName:p|$p.lastName])->filter(p|$p.lastName != 'hello'))
120+
,
121+
simpleRelationalMapping,
122+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
123+
124+
125+
assertEquals('select "unionalias_0"."lastName" as "lastName" from (select "root".LASTNAME as "lastName" from personTable as "root" union select "root".LASTNAME as "lastName" from personTable as "root" where "root".LASTNAME <> \'hello\' or "root".LASTNAME is null) as "unionalias_0"',
126+
$result.rootExecutionNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
127+
}
128+
129+
function <<test.Test>> meta::relational::tests::tds::tdsUnion::testUnionWithPrePostOperation():Boolean[1]
130+
{
131+
let result = executionPlan(
132+
|Person.all()
133+
->project(~[lastName:p|$p.lastName])
134+
->filter(p|$p.lastName->in(['hello']))
135+
->union(Person.all()->project(~[lastName:p|$p.lastName])->filter(p|$p.lastName->in(['hello'])))
136+
,
137+
simpleRelationalMapping,
138+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
139+
140+
assertEquals('select "unionalias_0"."lastName" as "lastName" from (select "root".LASTNAME as "lastName" from personTable as "root" where "root".LASTNAME = \'hello\' union select "root".LASTNAME as "lastName" from personTable as "root" where "root".LASTNAME = \'hello\') as "unionalias_0"',
141+
$result.rootExecutionNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
142+
}
143+
144+
145+
function <<test.Test>> meta::relational::tests::tds::tdsUnion::testUnionWithJoin():Boolean[1]
146+
{
147+
let result = executionPlan(
148+
|Person.all()
149+
->project(~[lhs_lastName:p|$p.lastName])
150+
->union(Person.all()->project(~[lhs_lastName:p|$p.lastName]))
151+
->toOne()
152+
->join(Person.all()->project(~[rhs_lastName:p|$p.lastName,rhs_firstName:p|$p.firstName]), JoinKind.INNER, {x,y| $x.lhs_lastName == $y.rhs_lastName})
153+
->select(~[lhs_lastName,rhs_firstName])
154+
,
155+
simpleRelationalMapping,
156+
meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
157+
158+
assertEquals('select "unionalias_0"."lhs_lastName" as "lhs_lastName", "unionalias_0"."rhs_firstName" as "rhs_firstName" from (select "unionalias_1"."lhs_lastName" as "lhs_lastName", "persontable_1"."rhs_lastName" as "rhs_lastName", "persontable_1"."rhs_firstName" as "rhs_firstName" from (select "unionalias_2"."lhs_lastName" as "lhs_lastName" from (select "root".LASTNAME as "lhs_lastName" from personTable as "root" union select "root".LASTNAME as "lhs_lastName" from personTable as "root") as "unionalias_2") as "unionalias_1" inner join (select "root".LASTNAME as "rhs_lastName", "root".FIRSTNAME as "rhs_firstName" from personTable as "root") as "persontable_1" on ("unionalias_1"."lhs_lastName" = "persontable_1"."rhs_lastName")) as "unionalias_0"',
159+
$result.rootExecutionNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
160+
161+
}
162+

0 commit comments

Comments
 (0)