diff --git a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/functions/tests/testSort.pure b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/functions/tests/testSort.pure index 831ba9c7707..fff8278206d 100644 --- a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/functions/tests/testSort.pure +++ b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/functions/tests/testSort.pure @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import meta::pure::graphFetch::execution::*; import meta::relational::mapping::*; import meta::relational::tests::model::simple::*; import meta::relational::tests::*; @@ -35,7 +36,7 @@ function <> meta::relational::tests::query::sort::testSortByLambdaSim assertEquals('select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "root".AGE as "age", "root".LASTNAME as "lastName", "root".LASTNAME as "order_key" from personTable as "root" order by "order_key"', $result->sqlRemoveFormatting()); } -function <> meta::relational::tests::query::sort::testSortByLambda__ColumnA_Plus_ColumnB():Boolean[1] +function <> meta::relational::tests::query::sort::testSortByLambdaColumnAddition():Boolean[1] { let result = execute(|Person.all()->sortBy(p | $p.lastName + '|' + $p.firstName), simpleRelationalMapping, testRuntime(), meta::relational::extension::relationalExtensions()); assertSize($result.values, 12); @@ -43,7 +44,7 @@ function <> meta::relational::tests::query::sort::testSortByLambda__C assertEquals('select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "root".AGE as "age", "root".LASTNAME as "lastName", concat("root".LASTNAME, \'|\', "root".FIRSTNAME) as "order_key" from personTable as "root" order by "order_key"', $result->sqlRemoveFormatting()); } -function <> meta::relational::tests::query::sort::testSortByLambda__If_ColumnAEqualsValue_Rank1_Else_Rank2():Boolean[1] +function <> meta::relational::tests::query::sort::testSortByLambdaWithIfElseValue():Boolean[1] { let result = execute(|Person.all()->filter(p|$p.lastName == 'Hill' || $p.firstName == 'John')->sortBy(p | if($p.lastName == 'Hill' && $p.firstName == 'John', | 0, |if ($p.lastName == 'Hill' || $p.firstName == 'John', | 10, | 20))), simpleRelationalMapping, testRuntime(), meta::relational::extension::relationalExtensions()); assertSize($result.values, 3); @@ -51,7 +52,7 @@ function <> meta::relational::tests::query::sort::testSortByLambda__I assertEquals('select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "root".AGE as "age", "root".LASTNAME as "lastName", case when ("root".LASTNAME = \'Hill\' and "root".FIRSTNAME = \'John\') then 0 else case when ("root".LASTNAME = \'Hill\' or "root".FIRSTNAME = \'John\') then 10 else 20 end end as "order_key" from personTable as "root" where ("root".LASTNAME = \'Hill\' or "root".FIRSTNAME = \'John\') order by "order_key"', $result->sqlRemoveFormatting()); } -function <> meta::relational::tests::query::sort::testSortByLambda__If_ColumnAEqualsValue_ColumnA_Else_ColumnB():Boolean[1] +function <> meta::relational::tests::query::sort::testSortByLambdaWIthIfElseColumn():Boolean[1] { let result = execute(|Person.all()->filter(p|$p.lastName == 'Hill' || $p.firstName == 'John')->sortBy(p | if($p.lastName == 'Hill', |$p.lastName, |$p.firstName)), simpleRelationalMapping, testRuntime(), meta::relational::extension::relationalExtensions()); assertSize($result.values, 3); @@ -59,7 +60,7 @@ function <> meta::relational::tests::query::sort::testSortByLambda__I assertEquals('select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "root".AGE as "age", "root".LASTNAME as "lastName", case when "root".LASTNAME = \'Hill\' then "root".LASTNAME else "root".FIRSTNAME end as "order_key" from personTable as "root" where ("root".LASTNAME = \'Hill\' or "root".FIRSTNAME = \'John\') order by "order_key"', $result->sqlRemoveFormatting()); } -function <> meta::relational::tests::query::sort::testSortByLambda_Deep():Boolean[1] +function <> meta::relational::tests::query::sort::testSortByLambdaDeepOptional():Boolean[1] { let result = execute(|Person.all()->sortBy(p | $p.address->toOne().name), simpleRelationalMapping, testRuntime(), meta::relational::extension::relationalExtensions()); assertSize($result.values, 12); @@ -68,6 +69,104 @@ function <> meta::relational::tests::query::sort::testSortByLambda_De assertEquals('select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "root".AGE as "age", "root".LASTNAME as "lastName", "addresstable_0".NAME as "order_key" from personTable as "root" left outer join addressTable as "addresstable_0" on ("addresstable_0".ID = "root".ADDRESSID) order by "order_key"', $result->sqlRemoveFormatting()); } +function <> meta::relational::tests::query::sort::testSortByLambdaAndGraphFetch():Boolean[1] +{ + let rawPlan = meta::pure::executionPlan::executionPlan(|Person.all()->sortBy(p | $p.address->toOne().name) + ->graphFetch( + #{ + Person{ + firstName, + addresses { + name + } + } + }# + )->serialize( + #{ + Person{ + firstName, + addresses { + name + } + } + }# + ), + simpleRelationalMapping, testRuntime(), meta::relational::extension::relationalExtensions()); + assertEquals( 'PureExp\n' + + '(\n' + + ' type = String\n' + + ' expression = -> serialize(#{meta::relational::tests::model::simple::Person {firstName, addresses() {name}}}#)\n' + + ' (\n' + + ' StoreMappingGlobalGraphFetch\n' + + ' (\n' + + ' type = PartialClass[impls=[(meta::relational::tests::model::simple::Person | simpleRelationalMappingInc.meta_relational_tests_model_simple_Person)], propertiesWithParameters = [addresses(), firstName]]\n' + + ' resultSizeRange = *\n' + + ' store = meta::relational::tests::db\n' + + ' localGraphFetchExecutionNode = \n' + + ' RelationalGraphFetch\n' + + ' (\n' + + ' type = PartialClass[impls=[(meta::relational::tests::model::simple::Person | simpleRelationalMappingInc.meta_relational_tests_model_simple_Person)], propertiesWithParameters = [addresses(), firstName]]\n' + + ' nodeIndex = 0\n' + + ' relationalNode = \n' + + ' SQL\n' + + ' (\n' + + ' type = meta::pure::metamodel::type::Any\n' + + ' resultColumns = [("pk_0", INT), ("firstName", VARCHAR(200)), ("order_key", VARCHAR(200))]\n' + + ' sql = select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "addresstable_0".NAME as "order_key" from personTable as "root" left outer join addressTable as "addresstable_0" on ("addresstable_0".ID = "root".ADDRESSID) order by "order_key"\n' + + ' connection = TestDatabaseConnection(type = "H2")\n' + + ' )\n' + + ' children = [\n' + + ' RelationalGraphFetch\n' + + ' (\n' + + ' type = PartialClass[impls=[(meta::relational::tests::model::simple::Address | simpleRelationalMappingInc.meta_relational_tests_model_simple_Address)], propertiesWithParameters = [name]]\n' + + ' nodeIndex = 2\n' + + ' relationalNode = \n' + + ' SQL\n' + + ' (\n' + + ' type = meta::pure::metamodel::type::Any\n' + + ' resultColumns = [("parent_key_gen_0", INT), ("pk_0", INT), ("name", VARCHAR(200))]\n' + + ' sql = select distinct "temp_table_node_0_0".pk_0 as "parent_key_gen_0", "unionalias_0".ID as "pk_0", "unionalias_0".NAME as "name" from (select * from (${temp_table_node_0}) as "root") as "temp_table_node_0_0" inner join personTable as "root" on ("temp_table_node_0_0".pk_0 = "root".ID) left outer join (select "addresstable_0".ID as ID, null as ADDRESSID from addressTable as "addresstable_0" UNION ALL select "firmtable_0".ID as ID, "firmtable_0".ADDRESSID as ADDRESSID from firmTable as "firmtable_0" left outer join addressTable as "addresstable_1" on ("addresstable_1".ID = "firmtable_0".ADDRESSID)) as "unionalias_0" on ("unionalias_0".ID = "root".FIRMID or "unionalias_0".ID = "root".ADDRESSID) where "unionalias_0".ID is not null\n' + + ' connection = TestDatabaseConnection(type = "H2")\n' + + ' )\n' + + ' children = [\n' + + ' \n' + + ' ]\n' + + ' )\n' + + '\n' + + ' ]\n' + + ' )\n' + + ' children = [\n' + + ' \n' + + ' ]\n' + + ' localTreeIndices = [0, 1, 2, 3]\n' + + ' dependencyIndices = []\n' + + ' )\n' + + ' )\n' + + ')\n',$rawPlan->planToString(meta::relational::extension::relationalExtensions())); +} + +function <> meta::relational::tests::query::sort::testSortByLambdaAndGraphFetchDeep():Boolean[1] +{ + let result = execute(|Person.all()->sortBy(p | $p.address->toOne().name) + ->graphFetch( + #{ + Person{ + firstName, + addresses + { + name + } + } + }# + ), + simpleRelationalMapping, testRuntime(), meta::relational::extension::relationalExtensions()); + assertSize($result.values, 12); + assertEquals(['Hoboken', 'Hong Kong', 'New York', 'New York', 'New York', 'New York', 'New York', 'New York', 'New York', 'New York', 'San Fransisco'], $result.values.address.name); + assertEquals(['No address', 'Peter', 'Oliver', 'John', 'John', 'Anthony', 'David', 'No firm', 'New', 'Elena', 'Don', 'Fabrice'], $result.values.firstName); + assertEquals('select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "root".AGE as "age", "root".LASTNAME as "lastName", "addresstable_0".NAME as "order_key" from personTable as "root" left outer join addressTable as "addresstable_0" on ("addresstable_0".ID = "root".ADDRESSID) order by "order_key"', $result->sqlRemoveFormatting()); +} + + function <> meta::relational::tests::query::sort::testSortByLambda_QueryWithParameters_Plan():Boolean[1] { let rawPlan = meta::pure::executionPlan::executionPlan({firstName: String[1], lastName: String[1]|Person.all()->filter(p|$p.lastName == $lastName || $p.firstName == $firstName)->sortBy(p | if($p.firstName == $firstName && $p.lastName == $lastName, | 0, |if ($p.lastName == $lastName || $p.firstName == $firstName, | 10, | 20)))}, simpleRelationalMapping, testRuntime(), meta::relational::extension::relationalExtensions());