Skip to content

Commit

Permalink
Expanding the way that context resolution works when functions are em…
Browse files Browse the repository at this point in the history
…bedded inside of others.
  • Loading branch information
cesarParra committed Jun 17, 2024
1 parent cdcb238 commit cbb57f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
12 changes: 1 addition & 11 deletions expression-src/main/src/interpreter/ContextResolver.cls
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,7 @@ public with sharing class ContextResolver implements Visitor {
this.queryContext = (Query) result;
resolve(function.arguments[1]);
this.queryContext = previousContext;

if (function.functionName == 'WHERE') {
// The WHERE function is a special case where we might want to keep building
// the query it started resolving, because where returns the List<SObject> back up, so
// the caller might want to keep operating on those records.
// See test: `combiningWhereAndMap`, and `combiningWhereAndMap_withRelationshipsBetweenDifferentSObjectTypes`
// for examples of why.
return result;
} else {
return null;
}
return result;
} else {
for (Expr argument : function.arguments) {
resolve(argument);
Expand Down
18 changes: 18 additions & 0 deletions expression-src/spec/language/functions/CollectionFunctionsTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,24 @@ private class CollectionFunctionsTest {
Assert.areEqual('Child2', result[0]);
}

@IsTest
private static void contextResolutionWhenDifferentFunctionCombinationsAreUsed() {
Account parentAccount = new Account(Name = 'Parent');
QDB.getInstance().doInsert(parentAccount);

Account childAccount1 = new Account(Name = 'Child1', ParentId = parentAccount.Id, NumberOfEmployees = 10, AnnualRevenue = 100);
Account childAccount2 = new Account(Name = 'Child2', ParentId = parentAccount.Id, NumberOfEmployees = 20, AnnualRevenue = 200);
QDB.getInstance().doInsert(new List<SObject>{
childAccount1, childAccount2
});

List<Object> result = (List<Object>) Evaluator.run('MAP(SORT(ChildAccounts, AnnualRevenue, "DESC"), Name)', parentAccount.Id);

Assert.areEqual(2, result.size());
Assert.areEqual('Child2', result[0]);
Assert.areEqual('Child1', result[1]);
}

@IsTest
private static void combiningWhereAndMap_withRelationshipsBetweenDifferentSObjectTypes() {
Account parentAccount = new Account(Name = 'Parent');
Expand Down

0 comments on commit cbb57f1

Please sign in to comment.