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