[CSHARP-3565] LINQ3: Add AstExpression UseVarIfNotSimple factory method Created: 12/Apr/21  Updated: 28/Oct/23  Resolved: 04/Oct/21

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: None
Fix Version/s: 2.14.0

Type: Task Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: CSHARP-2608

 Description   

Add the following helper method:

var (var, simpleAst) = AstExpression.UseVarIfNotSimple(name, expression); 

If the expression is "simple" return:

(null, expression) // null means no var is needed

if the expression is not "simple" return:

(var, simpleAst) 

where:

var = AstExpression.ComputedField(name, expression);
simpleAst = AstExpression.Field("$" + name); 

in other words, define a variable whose value is `expression` and create a reference to that variable.

This helper method will be used whenever there is a possibly complex expression that is used more than once in a translation. Rather than allowing the complex expression to appear more than once in the translation, the complex expression is assigned to a variable and then the variable is referenced as many times as necessary.

However, there is no need for the variable to be used when the expression is "simple". In that case the expression itself can be referenced multiple times with no performance or conceptual cost.

The current definition of "simple" is: null, a constant, or a variable reference. Everything else is "not simple".

Since `UseVarIfNotSimple` returns `null` if no variable is needed, change the behavior of `Let` to handle null vars differently and add a few new overloads:

AstExpression.Let(var, @in)
AstExpression.Let(var1, var2, @in)
AstExpression.Let(var1, var2, var3, @in) 

For each overload, if all vars are `null` no `$let` is needed and the `@in` expression is returned directly. If one or more vars are not `null` then a `$let` is created defining whichever vars are not null.

 



 Comments   
Comment by Robert Stam [ 12/Apr/21 ]

Done but not reviewed

Generated at Wed Feb 07 21:45:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.