Skip to content

Commit

Permalink
* no changes needed for relationalGraphFetch
Browse files Browse the repository at this point in the history
* always route to otherwiseMapping for graphFetchFlow

* fix otherwise embedded mapping for graphfetch

* behave as if embedded mapping were empty
  • Loading branch information
tanujgirdhar committed Nov 24, 2023
1 parent 85fef69 commit 5bc1ee3
Show file tree
Hide file tree
Showing 10 changed files with 551 additions and 19 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::relational::mapping::*;
import meta::pure::router::store::routing::*;
import meta::pure::lineage::scanProperties::*;
import meta::pure::milestoning::*;
Expand Down Expand Up @@ -64,11 +65,14 @@ function meta::pure::graphFetch::routing::propertyMappingInfo(prop: PropertyGrap
let fe = $routedProperty->byPassRouterInfo()->cast(@FunctionExpression);
let sourceSets = if($prop.subType->isEmpty(), | $fe, | $fe.parametersValues->evaluateAndDeactivate()->at(0)->byPassRouterInfo()->cast(@FunctionExpression)).parametersValues->evaluateAndDeactivate()->at(0)->cast(@StoreMappingRoutedValueSpecification).sets->resolveOperation($mapping)->cast(@InstanceSetImplementation);
$sourceSets->map({ss |
let propMaps = $ss->_propertyMappingsByPropertyName($property.name->toOne())->filter(x | $x.targetSetImplementationId->in($targetSets.id));
let propMaps = $ss->propertyMappingsByPropertyName($property.name->toOne())->filter(x | $x.targetSetImplementationId->in($targetSets.id));
$propMaps->map(pm | $mapping->classMappingById($pm.targetSetImplementationId)->resolveOperation($mapping)->map(ts | ^$pm(targetSetImplementationId = $ts.id)));
});,
| $propertyMappings
);



^PropertyMappingInfo(property=$property, propertyMappings=$resolvedPropertyMappings, sets=$sets););
}

Expand Down Expand Up @@ -218,7 +222,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 +465,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(), // 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 @@ -93,14 +93,14 @@ 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
}
}
}#;
Expand All @@ -111,9 +111,33 @@ 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}}]',
'[{"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
}
}
}#;
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 5bc1ee3

Please sign in to comment.