You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add EF.Parameter to force parameterization in query #28151
In F#, closure parameters are integrated into the query expression tree as constant nodes - see dotnet/fsharp#12841 for the full details. This means that by default, all EF queries generate SQL with constants rather than parameters.
A workaround is to use a F# ref, which gets properly parameterized by EF Core:
leti= ref 8
db.Events.Where(fun x -> x.Data = i.Value).ToList();
However, while this works well with local variables, it's problematic for use with function parameters, since the parameter needs to be typed as Ref<T> (it isn't great to require people to change method signatures just so that an EF query inside get properly parameterized). Note that the variable name is also lost, so the SQL contains @__Value_0.
We could introduced an EF.Param() mechanism, which is identified by EF Core and forces parameterization. The same mechanism could be used in C# to force parameterization for better perf when two queries use different constants, without needing to extract a variable:
Reacted by Arthur Vickers, Felix, Gaston Cababie, Friedrich von Never, Enderlook, pinkfloydx33, Don Syme, Jack Kerr, Mariusz Stępień, Daniel Turan and 2 more
Additional advantage of this: for users building an expression tree dynamically, it would make it considerably easier to integrate a value as a parameter into the expression tree.
Restriction on the API would be that the argument to the method must be a constant value. (this also works in dynamic tree building scenario where user generally creates ConstantExpression).
We would also evaluate anything which composes over as long as it is evaluate-able using normal rules of maximum possible evaluation.
e.g. EF.Param("foo").Length would get fully evaluated and generated parameter of value 3 in expression tree.
This would be really useful for us. We have a bunch of query building methods that dynamically craft an expression tree. We were ending up with constant values plugged directly into the query because we were using Expression.Constant. As a workaround I use the following helper method to force a closure over the value(s) passed into our builders:
Which we then use in lieu of Expression.Constant when dealing with various input values. So far this seems to work w/r/t parametrization, however we end up with parameters all named constant_{n} (the name of the parameter/display class field) which is a bit unfortunate. I'm also not sure if it's guaranteed to work or not and it feels a tad bit hacky.
It'd be great if we had some built-in method we could Expression.Call instead that would properly recognize this and turn the inputs into parameters named accordingly.
@pinkfloydx33 your workaround above is fine and guaranteed to work - it just reproduces the normal non-dynamic thing that happens when a closure variable is captured. But it's true that the parameter name isn't pretty.
We would also evaluate anything which composes over as long as it is evaluate-able using normal rules of maximum possible evaluation.
e.g. EF.Param("foo").Length would get fully evaluated and generated parameter of value 3 in expression tree
Evaluating operators composed over EF.Param seems unnecessary, since they'd get evaluated anyway without it; in other words, p.Length already gets fully parameterized, so there's no reason to use EF.Param() here in the first place.
There might conceivably also be cases where a user may want to block evaluation at a higher level, i.e. ensuring that some part of the tree is sent as a parameter, and not subsumed into a larger parameter (as contrived as it sounds, this may be helpful to optimize certain SQL queries).
(plus it's a bit more difficult implementation-wise to allow EF.Param() to be evaluated within a larger subtree)
changed the title [-]Consider adding EF.Param to force parameterization in query[/-][+]Add EF.Parameter to force parameterization in query[/+]on Nov 27, 2023
Activity
roji commentedon Jun 3, 2022
Additional advantage of this: for users building an expression tree dynamically, it would make it considerably easier to integrate a value as a parameter into the expression tree.
smitpatel commentedon Jun 3, 2022
Restriction on the API would be that the argument to the method must be a constant value. (this also works in dynamic tree building scenario where user generally creates ConstantExpression).
We would also evaluate anything which composes over as long as it is evaluate-able using normal rules of maximum possible evaluation.
e.g.
EF.Param("foo").Lengthwould get fully evaluated and generated parameter of value3in expression tree.pinkfloydx33 commentedon Jan 7, 2023
This would be really useful for us. We have a bunch of query building methods that dynamically craft an expression tree. We were ending up with constant values plugged directly into the query because we were using
Expression.Constant. As a workaround I use the following helper method to force a closure over the value(s) passed into our builders:Which we then use in lieu of
Expression.Constantwhen dealing with various input values. So far this seems to work w/r/t parametrization, however we end up with parameters all namedconstant_{n}(the name of the parameter/display class field) which is a bit unfortunate. I'm also not sure if it's guaranteed to work or not and it feels a tad bit hacky.It'd be great if we had some built-in method we could
Expression.Callinstead that would properly recognize this and turn the inputs into parameters named accordingly.roji commentedon Jan 7, 2023
@pinkfloydx33 your workaround above is fine and guaranteed to work - it just reproduces the normal non-dynamic thing that happens when a closure variable is captured. But it's true that the parameter name isn't pretty.
roji commentedon Aug 31, 2023
Do this together with #31552, which does the opposite, adding EF.Constant to force constantization.
roji commentedon Nov 27, 2023
Thinink about #28151 (comment):
Evaluating operators composed over EF.Param seems unnecessary, since they'd get evaluated anyway without it; in other words,
p.Lengthalready gets fully parameterized, so there's no reason to use EF.Param() here in the first place.There might conceivably also be cases where a user may want to block evaluation at a higher level, i.e. ensuring that some part of the tree is sent as a parameter, and not subsumed into a larger parameter (as contrived as it sounds, this may be helpful to optimize certain SQL queries).
(plus it's a bit more difficult implementation-wise to allow EF.Param() to be evaluated within a larger subtree)
Implement EF.Parameter
[-]Consider adding EF.Param to force parameterization in query[/-][+]Add EF.Parameter to force parameterization in query[/+]Implement EF.Parameter
Implement EF.Parameter (#32427)
kjeske commentedon Jan 4, 2024
I really like it and thank you for it. But do we need to wait till November 2024 to have it in non-prerelease version?
roji commentedon Jan 4, 2024
@kjeske yes.