-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query: Support for BETWEEN ... AND ... #12634
Comments
Also in PostgreSQL: https://www.postgresql.org/docs/current/static/functions-comparison.html. |
@smitpatel you can see this, maybe it is the simplest solution to this problem. If you like PR, tests will be implemented in SimpleQueryTestBase, and then provider writers will be able to rewrite it if there are any quirks. |
I would love to see this, what needs to be done to revive this PR @ralmsdeveloper ? |
@tomasbruckner @ralmsdeveloper what do you think this would provide beyond a simple |
@roji I had started a draft but I ran out of time! Answering your question: The only thing that can change is just the order in which the query can be made. DECLARE @id int = 3;
select 'OK' where @id between 2 and 4;
select 'OK' where @id between 4 and 2;
select 'OK' where @id>=2 and @id<=4;
select 'OK' where @id<=4 and @id>=2; |
I also investigated if there's any potential perf benefit and didn't find any... As such I'd really deprioritize this, compared to other issues we have... |
@roji If there are a lot of people wanting the feature, maybe it would be a good idea to take it to EF.Functions, but it's just thoughts! |
Coming back to this after a while... We've recently been focusing a bit on reducing duplication of expressions in our generated SQL, especially around null semantics. For example, translating to "x IS NOT DISTINCT FROM y" instead of This is also an area where BETWEEN is valuable: instead of evaluating x twice ( Database support:
Notes:
/cc @ranma42 |
Note that in most cases the original expression will already contain the duplication. AFAICT currently to make EFCore introduce duplication without having it in the original query you should combine queriable
.Select(e => new { X = e.A + e.B * EF.Functions.Random(), Entity = e})
.Where(e => e.X >= y && e.X <= z)
.Select(e => e.Entity) (because of #33791)
Doing this in the post-processing is certainly possible and I believe it should be reasonably straightforward. OTOH if we plan on improving the behavior around impure functions (rand, etc) it might not be sufficient to cover all of the cases. Nonetheless, it could definitely start in the postprocessing as a first step and then, if/when needed, we could evaluate whether an additional syntax to guarantee it makes sense (maybe we could avoid it completely if in the meantime we get another form of value binding... yes, CTE, I am looking at you).
👍
They are already performed quite early: since #34142 they are part of the efcore/src/EFCore.Relational/Query/SqlExpressionFactory.cs Lines 646 to 652 in ca891c2
Overall, I think that doing this as an optimization makes sense and should be straightforward; it could also help with some cases in which side-effects are not respected, but that is a bigger issue, that will also require bigger changes. |
Oh right 🤦 I forgot you did this change... Even better. There's the question of whether we should be doing this even when the source LINQ query contains duplication, e.g. So overall this does seem fine to me, at least until we have a better story for impurity (which we currently don't handle properly in other scenarios as well). Once we know about impure expressions, we can avoid the transformation to BETWEEN the moment there's any impurity detected. |
Available in T-SQL/SQLite/CosmosSql
The text was updated successfully, but these errors were encountered: