Skip to content

Commit

Permalink
Support assigning relation to variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Feb 3, 2025
1 parent 3d83c22 commit d54e74d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ Class meta::pure::executionPlan::ExecutionOptionContext extends MultiExecutionCo

Class meta::pure::executionPlan::PlanVarPlaceHolder extends meta::pure::executionPlan::FunctionParameter
{
routingStrategy: meta::pure::router::metamodel::RoutingStrategy[0..1];
}

Class meta::pure::executionPlan::PlanSetPlaceHolder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,6 @@ function <<access.private>> meta::pure::router::preeval::stopPreeval(value : Any
p : meta::external::format::shared::ExternalFormatExternalizeConfig[1] | true,
p : meta::external::format::shared::ExternalFormatInternalizeConfig[1] | true,
f : SimpleFunctionExpression[1]| $f.func->in([letFunction_String_1__T_m__T_m_]),
//p: meta::pure::executionPlan::PlanVarPlaceHolder[*] | true, // todo move to a better place?
a : Any[*]| $a->type()
->match([
c:Class<Any>[1]|$c->isSimpleType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import meta::pure::router::store::metamodel::clustering::*;
import meta::pure::extension::*;
import meta::pure::router::clustering::*;
import meta::pure::router::externalFormat::metamodel::*;
Expand Down Expand Up @@ -116,26 +115,11 @@ function <<access.private>> meta::pure::router::createPlanVarPlaceHolderInScopeV
name = $varName,
genericType = $vs.genericType,
multiplicity = $vs.multiplicity,
supportsStream = $vs.multiplicity->isToMany(),
routingStrategy = $vs->meta::pure::router::getVariableRoutingStrategy()
supportsStream = $vs.multiplicity->isToMany()
)
)
}

function <<access.private>> meta::pure::router::getVariableRoutingStrategy(vs: ValueSpecification[0..1]): RoutingStrategy[0..1]
{
$vs->match([
smc: StoreMappingClusteredValueSpecification[1] | $smc.runtime->map(r | getRoutingStrategyFromMappingAndRuntime($smc.mapping, $r)),
sc: StoreClusteredValueSpecification[1] | $sc.runtime->map(r | getRoutingStrategyFromRuntime($r)),
c: ClusteredValueSpecification[1] | $c.val->byPassRouterInfo()
->cast(@SimpleFunctionExpression)
->filter(f | $f->isLetFunction()).parametersValues
->last()
->meta::pure::router::getVariableRoutingStrategy(),
a: Any[0..1] | []
]);
}

function meta::pure::router::routeFunction(f:FunctionDefinition<Any>[1], routingStrategy:RoutingStrategy[1], exeCtx: meta::pure::runtime::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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,13 @@ function meta::pure::router::routing::routeValueSpecification(state:RoutingState
r:NoSetRoutedValueSpecification[1] | ^$state(value=$r.value)->routeValueSpecification($executionContext, $vars, $inScopeVars, $extensions, $debug),
fe:FunctionExpression[1] | $fe->routeFunctionExpression($state, $executionContext, $vars, $inScopeVars, $extensions, $debug),
i:InstanceValue[1] | if(
[ pair(
| $i.values->size() == 1 && $i.values->at(0)->instanceOf(meta::pure::executionPlan::PlanVarPlaceHolder)
&& $i.values->at(0)->cast(@meta::pure::executionPlan::PlanVarPlaceHolder).genericType.rawType->toOne()->subTypeOf(Relation)
&& $i.values->at(0)->cast(@meta::pure::executionPlan::PlanVarPlaceHolder).routingStrategy->isNotEmpty(),
|
^$state(routingStrategy = $i.values->at(0)->cast(@meta::pure::executionPlan::PlanVarPlaceHolder).routingStrategy->toOne());
),
[
pair(
| $i.values->size() == 1 && $i.values->at(0)->instanceOf(RelationStoreAccessor) || ($i.values->isEmpty() && $i.multiplicity == PureOne && $i.genericType.rawType->isNotEmpty() && $i.genericType.rawType->toOne()->subTypeOf(Relation)),
| // We match cast to Relation & RelationStoreAccessor.. However we're only processing the Accessor
if($i.values->size() == 1 && $i.values->at(0)->instanceOf(RelationStoreAccessor),
| if($state.routingStrategy.processRelationStoreAccessor->isEmpty(),
| fail('RelationStoreAccessor should be used with a from(Rumtime) expression.'); $state;,
| fail('RelationStoreAccessor should be used with a from(Runtime) expression.'); $state;,
| $state.routingStrategy.processRelationStoreAccessor($i.values->at(0)->cast(@RelationStoreAccessor<Any>), $i, $state, $executionContext, $debug)
),
| $state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function meta::pure::router::store::routing::specializedFunctionExpressionRoute

let routedFunc = routeFunction($preEvalFuncToRoute, $routingStrategy, $exCtx, [], $extensions, $debug->indent());

// todo beyraf | validate routed func is one cluster...
// todo rafael bey | validate routed func is one cluster...
let clusterValues = $routedFunc.expressionSequence->evaluateAndDeactivate()->cast(@ClusteredValueSpecification).val;
let unclusteredRoutedFunc = ^$routedFunc(expressionSequence = $clusterValues);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,18 @@ public void run(ServerConfiguration configuration, Environment environment) thro
postInit();

long ct = System.currentTimeMillis();
LOGGER.info("Initializing Pure runtime...");
LOGGER.info("**************************************\n" +
"**** Initializing Pure runtime... ****\n" +
"**************************************"
);
try
{
this.pureSession.getPureRuntime().initialize();
LOGGER.info("Initialization of Pure runtime completed in {}s.", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - ct));
}
catch (Exception e)
{
LOGGER.info("Initialization of Pure runtime failed in {}s.", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - ct), e);
LOGGER.warn("Initialization of Pure runtime failed in {}s.", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - ct), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,40 +346,78 @@ function meta::relational::tests::pct::process::addWith(func: FunctionDefinition

assert($mapping->size() <= 1, 'Only one mapping supported');

let withWith = ^SimpleFunctionExpression
(
importGroup=system::imports::coreImport,
func = if($mapping->isEmpty(), |with_FunctionDefinition_1__Runtime_1__T_m_, |with_FunctionDefinition_1__Mapping_1__Runtime_1__T_m_),
functionName = 'with',
genericType = $returnGenericType,
multiplicity = $returnMultiplicity,
parametersValues = ^InstanceValue(
genericType = $func->genericType()->toOne(),
multiplicity = PureOne,
values = $func
)
->concatenate(if ($mapping->isEmpty(),
|[],
|^InstanceValue
(
genericType = ^GenericType(rawType=Mapping),
multiplicity = PureOne,
values = $mapping->toOne()
)
))
->concatenate(^InstanceValue
(
genericType = ^GenericType(rawType=Runtime),
multiplicity = PureOne,
values = $runtime
)
)
,
resolvedMultiplicityParameters = $returnMultiplicity,
resolvedTypeParameters = $returnGenericType
);
// for single expression keep in-line behavior of with
// for multi, use the with that takes the whole function definition
// the reason for this is that the latter preeval, invalidating many PCT test as preeval will evaluate constant values that PCT aims to push to DB
$func.expressionSequence->evaluateAndDeactivate()->match([
vs: ValueSpecification[1] |
let withWith = ^SimpleFunctionExpression
(
importGroup=system::imports::coreImport,
func = if($mapping->isEmpty(), |with_T_m__Runtime_1__T_m_, |with_T_m__Mapping_1__Runtime_1__T_m_),
functionName = 'with',
genericType = $returnGenericType,
multiplicity = $returnMultiplicity,
parametersValues = $vs
->concatenate(if ($mapping->isEmpty(),
|[],
|^InstanceValue
(
genericType = ^GenericType(rawType=Mapping),
multiplicity = PureOne,
values = $mapping->toOne()
)
))
->concatenate(^InstanceValue
(
genericType = ^GenericType(rawType=Runtime),
multiplicity = PureOne,
values = $runtime
)
)
,
resolvedMultiplicityParameters = $returnMultiplicity,
resolvedTypeParameters = $returnGenericType
);
^$func(expressionSequence = $withWith);
,
multi: ValueSpecification[1..*] |
let withWith = ^SimpleFunctionExpression
(
importGroup=system::imports::coreImport,
func = if($mapping->isEmpty(), |with_FunctionDefinition_1__Runtime_1__T_m_, |with_FunctionDefinition_1__Mapping_1__Runtime_1__T_m_),
functionName = 'with',
genericType = $returnGenericType,
multiplicity = $returnMultiplicity,
parametersValues = ^InstanceValue(
genericType = $func->genericType()->toOne(),
multiplicity = PureOne,
values = $func
)
->concatenate(if ($mapping->isEmpty(),
|[],
|^InstanceValue
(
genericType = ^GenericType(rawType=Mapping),
multiplicity = PureOne,
values = $mapping->toOne()
)
))
->concatenate(^InstanceValue
(
genericType = ^GenericType(rawType=Runtime),
multiplicity = PureOne,
values = $runtime
)
)
,
resolvedMultiplicityParameters = $returnMultiplicity,
resolvedTypeParameters = $returnGenericType
);

^$func(expressionSequence = $withWith);
^$func(expressionSequence = $withWith);,
a: Any[*] | fail('not supported: ' + $a->type()->elementToPath() + ': ' + $a->size()->toString()); $func;
]);
}

function meta::relational::tests::pct::process::setupDatabase(csvs: String[*], database: Database[1], runtime: Runtime[1], debug: DebugContext[1]): String[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,10 @@ function meta::relational::functions::pureToSqlQuery::processValue(vals:Any[*],
paths = $v.tdsColumns->map(c| pair($c.name,^PathInformation(type = $c.type ->toOne(), relationalType = $c.sourceDataType->cast(@meta::relational::metamodel::datatype::DataType)->toOne()))));
^$operation(select = ^$select(data=^RootJoinTreeNode(alias = ^TableAlias(name = 'root', relationalElement = $newTable))));,
v:PlanVarPlaceHolder[1] |
let resolvedPlanVar = $state.functionReferenceScope->map(x | $x.resolveFunctionReferenceByName($v.name));
let ifs = [
// do we have a reference already?
pair(|$state.functionReferenceScope->map(x | $x.resolveFunctionReferenceByName($v.name))->isNotEmpty(),
|
$state.functionReferenceScope->map(x | $x.resolveFunctionReferenceByName($v.name));
),
pair(|$resolvedPlanVar->isNotEmpty(), |$resolvedPlanVar->toOne()),
// if is relation, translate to VarSetPlaceHolder
pair(|$v.genericType.rawType->toOne()->subTypeOf(meta::pure::metamodel::relation::Relation),
|
Expand Down

0 comments on commit d54e74d

Please sign in to comment.