Skip to content

Commit

Permalink
update pureToSQLQuery.pure
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishoya-gs committed Nov 20, 2023
1 parent acc7b11 commit 039ad1f
Showing 1 changed file with 103 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -35,31 +36,31 @@ function <<test.Test>> 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 <<test.Test>> meta::relational::tests::query::sort::testSortByLambda__ColumnA_Plus_ColumnB():Boolean[1]
function <<test.Test>> 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);
assertEquals(['Allen|Anthony', 'Firm B|Elena', 'Harris|David', 'Hill|John', 'Hill|Oliver', 'Johnson|John', 'New York|Don', 'Roberts|Fabrice', 'Smith|No address', 'Smith|Peter', 'York|New', 'no Firm|No firm'], zip($result.values.lastName, $result.values.firstName)->map(pair | $pair.first + '|' + $pair.second));
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 <<test.Test>> meta::relational::tests::query::sort::testSortByLambda__If_ColumnAEqualsValue_Rank1_Else_Rank2():Boolean[1]
function <<test.Test>> 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);
assertEquals(['John Hill', 'John Johnson', 'Oliver Hill'], zip($result.values.firstName, $result.values.lastName)->map(pair | $pair.first + ' ' + $pair.second));
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 <<test.Test>> meta::relational::tests::query::sort::testSortByLambda__If_ColumnAEqualsValue_ColumnA_Else_ColumnB():Boolean[1]
function <<test.Test>> 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);
assertEquals(['John Hill', 'Oliver Hill', 'John Johnson'], zip($result.values.firstName, $result.values.lastName)->map(pair | $pair.first + ' ' + $pair.second));
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 <<test.Test>> meta::relational::tests::query::sort::testSortByLambda_Deep():Boolean[1]
function <<test.Test>> 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);
Expand All @@ -68,6 +69,104 @@ function <<test.Test>> 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 <<test.Test>> 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 <<test.Test>> 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 <<test.Test>> 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());
Expand Down

0 comments on commit 039ad1f

Please sign in to comment.