Skip to content

Commit

Permalink
Relational Graph Fetch - Otherwise embedded mapping support (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanujgirdhar authored Nov 30, 2023
1 parent 8e935d4 commit 7fa7306
Show file tree
Hide file tree
Showing 11 changed files with 668 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function meta::pure::graphFetch::routing::routeRootGraphFetchTree(root: RootGrap
let rootGetAllExpression = createGetAllApplicationForRootGraphFetchTree($root, $set);

let dummyLambda = {|'ok'};
let routedFunction = ^$dummyLambda(expressionSequence = $rootGetAllExpression)->routeFunction($mapping, ^Runtime(), ^ExecutionContext(), $extensions, noDebug())->evaluateAndDeactivate()->toOne();
let routedFunction = ^$dummyLambda(expressionSequence = $rootGetAllExpression)->routeFunction(getRoutingStrategyFromMappingAndRuntime($mapping, ^Runtime()), ^ExecutionContext(), [], true, $extensions, noDebug())->evaluateAndDeactivate()->toOne();
let ext = $routedFunction.expressionSequence->evaluateAndDeactivate()->toOne()->cast(@StoreMappingClusteredValueSpecification).val->cast(@StoreMappingRoutedValueSpecification);

let extended = if($exeCtx.enableConstraints->isEmpty() || $exeCtx.enableConstraints->toOne(),
Expand Down Expand Up @@ -461,7 +461,8 @@ function meta::pure::graphFetch::routing::routePropertyGraphFetchTree(prop: Prop
let dummyLambda = {|'ok'};
let cls = $functionExpression.genericType.rawType->toOne()->cast(@Class<Any>);
// TODO Only allow no mapping routing for expanded (sub)trees
let routedFunctions = ^$dummyLambda(expressionSequence = $propertyApplicationExporession)->routeFunction($mapping, ^Runtime(), $extensions, noDebug())->evaluateAndDeactivate();

let routedFunctions = ^$dummyLambda(expressionSequence = $propertyApplicationExporession)->routeFunction(getRoutingStrategyFromMappingAndRuntime($mapping, ^Runtime()), ^ExecutionContext(), [], true, $extensions, noDebug())->evaluateAndDeactivate();

if($routedFunctions->isNotEmpty(),
| let routedFunction = $routedFunctions->toOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,13 @@ function meta::pure::router::routing::findMappingsFromProperty(p:AbstractPropert
),
| if($embeddedMappings->at(0)->instanceOf(InlineEmbeddedSetImplementation),
|$embeddedMappings->at(0)->cast(@InlineEmbeddedSetImplementation)->inlineEmbeddedMapping($mapping),
|$embeddedMappings
)

|
if($state.graphFetchFlow == true && $embeddedMappings->at(0)->instanceOf(OtherwiseEmbeddedSetImplementation),
| let otherwiseTargetIds = $embeddedMappings->at(0)->cast(@OtherwiseEmbeddedSetImplementation).otherwisePropertyMapping.targetSetImplementationId;
let classMappingsById = if ($otherwiseTargetIds->isEmpty(),|[],|findMappingsFromProperty($p, $mapping, $mapping->_classMappingByIdRecursive($otherwiseTargetIds), $state, $extensions));
$classMappingsById;,
| $embeddedMappings
))
);

// We don't find anything in the old flow (if the source is an operation and the target type is not directly mapped...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function meta::pure::router::routeFunction(f:FunctionDefinition<Any>[1], exeCtx:
}

function meta::pure::router::routeFunction(f:FunctionDefinition<Any>[1], routingStrategy:RoutingStrategy[1], exeCtx: ExecutionContext[1], inScopeVars:Map<String, List<Any>>[0..1], extensions:meta::pure::extension::Extension[*], debug:DebugContext[1]):FunctionDefinition<Any>[1]
{
routeFunction($f, $routingStrategy, $exeCtx, $inScopeVars, false, $extensions, $debug);
}

function meta::pure::router::routeFunction(f:FunctionDefinition<Any>[1], routingStrategy:RoutingStrategy[1], exeCtx: ExecutionContext[1], inScopeVars:Map<String, List<Any>>[0..1], graphFetchFlow:Boolean[0..1], extensions:meta::pure::extension::Extension[*], debug:DebugContext[1]):FunctionDefinition<Any>[1]
{
let fOpenVariables = $f->openVariableValues();
let openVariables = if($inScopeVars->isNotEmpty(),
Expand All @@ -123,7 +128,7 @@ function meta::pure::router::routeFunction(f:FunctionDefinition<Any>[1], routing

// Enriching Function Expressions with relevant info (mapping / binding / platform)
print(if($debug.debug,|'\n'+$debug.space+'Enriching Function Expressions with relevant info (mapping / binding / platform) and assigning routing strategy:\n',|''));
let enrichedExpressions = enrichFunctionExpressions($functionExpressions, $routingStrategy, $exeCtx, $openVariables, $extensions, $debug);
let enrichedExpressions = enrichFunctionExpressions($functionExpressions, $routingStrategy, $exeCtx, $openVariables, $graphFetchFlow, $extensions, $debug);

// Enriching Function Expressions with more information based on type of expression (subTypes of ExtendedRoutedValueSpecification)
print(if($debug.debug,|'\n'+$debug.space+'Enriching Function Expressions with strategy based info (mapping / binding / platform):\n',|''));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ Class meta::pure::router::routing::RoutingState
routingStrategy : RoutingStrategy[1];
shouldBeRouted : Boolean[1];
value : Any[0..1];
graphFetchFlow : Boolean[0..1];
}

Class meta::pure::router::routing::PropertyMap
{
v : Pair<String,Any>[*];
}

function meta::pure::router::routing::enrichFunctionExpressions(expressions:FunctionExpression[*], routingStrategy:RoutingStrategy[1], executionContext:ExecutionContext[1], inScopeVars:Map<String, List<Any>>[1], extensions:meta::pure::extension::Extension[*], debug:DebugContext[1]):ExtendedRoutedValueSpecification[*]
function meta::pure::router::routing::enrichFunctionExpressions(expressions:FunctionExpression[*], routingStrategy:RoutingStrategy[1], executionContext:ExecutionContext[1], inScopeVars:Map<String, List<Any>>[1], graphFetchFlow: Boolean[0..1],extensions:meta::pure::extension::Extension[*], debug:DebugContext[1]):ExtendedRoutedValueSpecification[*]
{
let processedFunctions = processCollection(^RoutingState(shouldBeRouted=false, lambdaContext=[], counter=0, depth='', propertyMap = ^PropertyMap(), routingStrategy = $routingStrategy),
let processedFunctions = processCollection(^RoutingState(shouldBeRouted=false, lambdaContext=[], counter=0, depth='', propertyMap = ^PropertyMap(), routingStrategy = $routingStrategy, graphFetchFlow = $graphFetchFlow),
$expressions,
$executionContext,
newMap([]->cast(@Pair<VariableExpression, ValueSpecification>), VariableExpression->classPropertyByName('name')->cast(@Property<VariableExpression,String|1>)),
Expand All @@ -67,6 +68,11 @@ function meta::pure::router::routing::enrichFunctionExpressions(expressions:Func
]));
}

function meta::pure::router::routing::enrichFunctionExpressions(expressions:FunctionExpression[*], routingStrategy:RoutingStrategy[1], executionContext:ExecutionContext[1], inScopeVars:Map<String, List<Any>>[1], extensions:meta::pure::extension::Extension[*], debug:DebugContext[1]):ExtendedRoutedValueSpecification[*]
{
enrichFunctionExpressions($expressions, $routingStrategy, $executionContext, $inScopeVars, false, $extensions, $debug);
}

function meta::pure::router::routing::processCollection(state:RoutingState[1], col:Any[*], executionContext:ExecutionContext[1], vars:Map<VariableExpression, ValueSpecification>[1], inScopeVars:Map<String, List<Any>>[1], shouldProcess:Function<{Any[1]->Boolean[1]}>[1], extensions:meta::pure::extension::Extension[*], debug:DebugContext[1]):RoutingState[*]
{
$col->fold({c,a| let last = $a->last()->toOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ function meta::pure::router::store::routing::build(v:ValueSpecification[1], l:Li
let res = if($resWithAssoc->isEmpty() && $f.func->instanceOf(Property) && $s->isPropertyAutoMapped($f.func->cast(@Property<Nil, Any|*>), $extensions),
|$s->cast(@InstanceSetImplementation)->meta::pure::mapping::propertyMappingsByPropertyName($propertyName),
|$resWithAssoc);
print(if($debug.debug,|$debug.space+' -> Found: '+$res->size()->toString(),|''));
$res;

let withOtherwise = if($res->isEmpty() && $f.func->instanceOf(Property), // for graphFetch flow, the parent set gets resolved to the otherwiseProperty mapping always.
| let byName = $s->cast(@InstanceSetImplementation)->meta::pure::mapping::_propertyMappingsByPropertyName($propertyName)->filter(pm|$pm->instanceOf(OtherwiseEmbeddedSetImplementation))->map(pm|$pm->cast(@OtherwiseEmbeddedSetImplementation));
let pms = $byName.otherwisePropertyMapping->filter(pm|$pm.targetSetImplementationId->in($targetIds));
$pms;,
| $res );

print(if($debug.debug,|$debug.space+' -> Found: '+$withOtherwise->size()->toString(),|''));
$withOtherwise;
);
);
let fWithBuiltExprSeq = if ($f.func->instanceOf(QualifiedProperty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ function <<access.private>> meta::relational::graphFetch::executionPlan::generat
let res = if ($isQualified,
{|
let newVars = newVarsForMilestoningContext($propTree, $inScopeVars);
let updatedState = ^$pureToSqlState(inGetterFlow = true, qualifierBase=^OperationWithParentPropertyMapping(element = $base), inScopeVars = $pureToSqlState.inScopeVars->putAll($newVars));
let updatedState = ^$pureToSqlState(inGetterFlow = true, milestoningUseOtherwise = true, qualifierBase=^OperationWithParentPropertyMapping(element = $base), inScopeVars = $pureToSqlState.inScopeVars->putAll($newVars));
processQualifiedProperty($propTree.property->cast(@QualifiedProperty<Any>), [], $base, ^Map<VariableExpression, ValueSpecification>(), $updatedState, JoinType.LEFT_OUTER, '_gftq', ^List<ColumnGroup>(), $debug, $extensions).element->cast(@SelectWithCursor);
},
{|
Expand Down Expand Up @@ -796,7 +796,7 @@ function meta::relational::graphFetch::executionPlan::generateNonPrimitiveProper
let newVars = newVarsForMilestoningContext($propTree, $inScopeVars);
let mcForProperty = milestoningContextFromPropertyTree($propTree, $parentTree, $mc, ^$pureToSqlState(inScopeVars = $pureToSqlState.inScopeVars->putAll($newVars)), $debug->indent(), $extensions);
let baseWithMc = ^$base(milestoningContext = $mcForProperty);
let updatedState = ^$pureToSqlState(inGetterFlow = true, qualifierBase = ^OperationWithParentPropertyMapping(element = $baseWithMc), inScopeVars = $pureToSqlState.inScopeVars->putAll($newVars));
let updatedState = ^$pureToSqlState(inGetterFlow = true, milestoningUseOtherwise = true, qualifierBase = ^OperationWithParentPropertyMapping(element = $baseWithMc), inScopeVars = $pureToSqlState.inScopeVars->putAll($newVars));
let withQualifier = processQualifiedProperty($propTree.property->cast(@QualifiedProperty<Any>), [], $baseWithMc, ^Map<VariableExpression, ValueSpecification>(), $updatedState, JoinType.LEFT_OUTER, '_gftq', ^List<ColumnGroup>(), $debug, $extensions).element->cast(@SelectWithCursor);
let newSelect = $withQualifier.select->pushFilters($extensions);
let innerRes = ^$withQualifier(select = ^$newSelect(leftSideOfFilter = []));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,40 @@ function <<test.Test, test.AlloyOnly>> {serverVersion.start='v1_19_0'} meta::rel
);
}

function <<test.Test, test.ToFix, test.AlloyOnly>> {serverVersion.start='v1_19_0'} meta::relational::graphFetch::tests::embedded::testEmbeddedMappingQualifiedPropertyAccess(): Boolean[1]
function <<test.Test, test.AlloyOnly>> {serverVersion.start='v1_19_0'} meta::relational::graphFetch::tests::embedded::testEmbeddedMappingQualifiedPropertyAccess(): Boolean[1]
{
let tree = #{
Person {
firstName,
firm {
legalName,
'employeeByLastNameFirstName': employeeByLastNameFirstName('Smith')
nameAndAddress
}
}
}#;
let query = {|Person.all()->graphFetch($tree)->serialize($tree)};
let mapping = meta::relational::tests::mapping::embedded::model::mapping::testMappingEmbedded;
let runtime = meta::external::store::relational::tests::testRuntime();

let result = execute($query, $mapping, $runtime, meta::relational::extension::relationalExtensions()).values;

assertJsonStringsEqual(
'[{"firstName":"Peter","firm":{"legalName":"Firm X","nameAndAddress()":"Firm X,200 west"}},' +
'{"firstName":"John","firm":{"legalName":"Firm X","nameAndAddress()":"Firm X,200 west"}},' +
'{"firstName":"Fabrice","firm":{"legalName":"Firm A","nameAndAddress()":"Firm A,3 somewhere"}}]',
$result
);
}


function <<test.Test, test.AlloyOnly, test.ToFix>> {serverVersion.start='v1_19_0'} meta::relational::graphFetch::tests::embedded::testEmbeddedMappingQualifiedPropertyAccess2(): Boolean[1]
{
let tree = #{
Person {
firstName,
firm {
legalName,
maxEmployeesAge
}
}
}#;
Expand All @@ -111,9 +137,30 @@ function <<test.Test, test.ToFix, test.AlloyOnly>> {serverVersion.start='v1_19_0
let result = execute($query, $mapping, $runtime, meta::relational::extension::relationalExtensions()).values;

assertJsonStringsEqual(
'[{"firstName":"Peter","firm":{"legalName":"Firm X","employeeByLastNameFirstName":"Peter"}},' +
'{"firstName":"John","firm":{"legalName":"Firm X","employeeByLastNameFirstName":"Peter"}},' +
'{"firstName":"Fabrice","firm":{"legalName":"Firm A","employeeByLastNameFirstName":null}}]',
'{}',
$result
);
}

function <<test.Test, test.AlloyOnly, test.ToFix>> {serverVersion.start='v1_19_0'} meta::relational::graphFetch::tests::embedded::testEmbeddedMappingQualifiedPropertyAccessWithArgs(): Boolean[1]
{
let tree = #{
Person {
firstName,
firm {
legalName,
'employeeByLastNameFirstName': employeeByLastNameFirstName('Smith')
}
}
}#;
let query = {|Person.all()->graphFetch($tree)->serialize($tree)};
let mapping = meta::relational::tests::mapping::embedded::model::mapping::testMappingEmbedded;
let runtime = meta::external::store::relational::tests::testRuntime();

let result = execute($query, $mapping, $runtime, meta::relational::extension::relationalExtensions()).values;

assertJsonStringsEqual(
'{}',
$result
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2023 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import meta::pure::executionPlan::profiles::*;
import meta::pure::graphFetch::execution::*;
import meta::relational::tests::mapping::embedded::advanced::model::*;


function <<test.BeforePackage>> meta::relational::graphFetch::tests::embedded::inline::setup(): Boolean[1]
{
meta::relational::tests::mapping::embedded::advanced::setUp();
}


function <<test.Test, test.AlloyOnly>> {serverVersion.start='v1_19_0'} meta::relational::graphFetch::tests::embedded::inline::testInlineEmbeddedMapping(): Boolean[1]
{
let tree = #{
Product {
name,
bondDetails {
description
}
}
}#;
let query = {|Product.all()->graphFetch($tree)->serialize($tree)};
let mapping = meta::relational::tests::mapping::embedded::advanced::mapping::testMappingEmbedded;
let runtime = meta::external::store::relational::tests::testRuntime();

let result = execute($query, $mapping, $runtime, meta::relational::extension::relationalExtensions()).values;

assertJsonStringsEqual(
'[{"name":"Product 1","bondDetails":{"description":"Bond 1"}},' +
'{"name":"Product 2","bondDetails":{"description":"Bond 2"}},' +
'{"name":"Product 3","bondDetails":{"description":"SuperBond 3 super"}}]',
$result
);
}

function <<test.Test, test.AlloyOnly>> {serverVersion.start='v1_19_0'} meta::relational::graphFetch::tests::embedded::inline::testQualifierWithArgs(): Boolean[1]
{
let tree = #{
Product {
name,
bondDetails {
'duration': durationStartsWith('5')
}
}
}#;
let query = {|Product.all()->graphFetch($tree)->serialize($tree)};
let mapping = meta::relational::tests::mapping::embedded::advanced::mapping::testMappingEmbedded;
let runtime = meta::external::store::relational::tests::testRuntime();

let result = execute($query, $mapping, $runtime, meta::relational::extension::relationalExtensions()).values;

assertJsonStringsEqual(
'[{"name":"Product 1","bondDetails":{"duration":true}},' +
'{"name":"Product 2","bondDetails":{"duration":true}},' +
'{"name":"Product 3","bondDetails":{"duration":false}}]',
$result
);
}

function <<test.Test, test.AlloyOnly>> {serverVersion.start='v1_19_0'} meta::relational::graphFetch::tests::embedded::inline::testWithAssociationFromRootMappingWithFilter(): Boolean[1]
{
let tree = #{
Product {
name,
bondDetails {
bondClassification {
type
}
}
}
}#;
let query = {|Product.all()->filter(p|$p.name == 'Product 1')->graphFetch($tree)->serialize($tree)};
let mapping = meta::relational::tests::mapping::embedded::advanced::mapping::testMappingEmbeddedParent;
let runtime = meta::external::store::relational::tests::testRuntime();

let result = execute($query, $mapping, $runtime, meta::relational::extension::relationalExtensions()).values;

assertJsonStringsEqual(
'{"name":"Product 1","bondDetails":{"bondClassification":[{"type":"Corporate"}]}}',
$result
);
}
Loading

0 comments on commit 7fa7306

Please sign in to comment.