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
Update Pipeline: Use C# null semantics for WHERE clauses (concurrency tokens) #3023
This may work in other databases, but PostgreSQL always returns false for = NULL even when using a parameter. Now I can see that in a select query EF generates different SQL (IS NULL vs. equality operator) based on whether the parameter is null or not (I wonder how this interacts with caching, do you key on the nullness of parameters?). However, this logic seems to be totally absent from UpdateSqlGenerator, where the equality operator is always used.
Any tips on how to proceed? I could override AppendWhereCondition and use PostgreSQL's IS NOT DISTINCT FROM as a workaround (it's like the equality operator except it returns true for nulls). I could also test the ColumnModification's Value and generate IS NULL or equality based on that (although is the update SQL cached somewhere?).
However, this seems like something that needs to be handled outside the provider in Relational... Ideally, shouldn't the code generating where clauses (and perhaps other SQL components) be shared between query generation and modification generation?
Agree, ideally update would just gen a SQL predicate expression that could then be rendered by the provider in the normal way.
WRT caching, we perform SQL gen for every query execution, and use the parameter values to inform the generation process. In this way we can gen quite nice SQL. At some point we may have a second level of caching that caches the SQL based on specific sets of parameter values, but we will only likely do that if we have to.
Oh, I thought you guys were caching actual generated SQL (that would be obviously problematic with respect to parameters value having an effect on the SQL like this case). Never mind.
I'll work around this for now with IS NOT DISTINCT FROM until you fix it in a better way.
@roji It seems that the PostgreSQL provider (and any relational databases that support the standard operator) could short-circuit the three-valued logic compensations we are doing in our query pipeline and generate IS [NOT] DISTINCT FROM instead of regular equal/not equal comparisons when relational nulls are disabled on the context.
Not sure how complex the short-circuiting would be (@maumar may have an idea) but just in case, do you see any downsides on doing that?
@divega, it's an interesting idea although I've seen rumors that using IS [NOT] DISTINCT FROM may have a detrimental impact on the use of indices (nothing verified yet). Is there an advantage to be gained by short-circuiting vs. just leaving things as they are?
Interesting... Let me start by saying that if SQL Server supported IS [NOT] DISTINCT FROM we probably wouldn't have thought about generating three-valued logic compensation in the first place 😄
I don't think this is high priority. It just made me wonder how it would look like.
We do compensate by default because many customers gave us strong feedback that when they write equality comparisons in their LINQ queries, they expect the behavior on the database server to match the behavior in memory.
As Andrew mentioned we do the trick of sniffing into parameter values to generate the simplest queries we can, but it is still possible that in some cases the compensations are still detrimental to the performance. And I imagine for a database server that has built-in support for IS [NOT] DISTINCT FROM it could be easier to optimize that than to optimize the compensations.
Here are some examples of what we do:
Assuming C is a nullable column and P is a nullable parameter:
For _C == P_ in C#
If P !=null the SQL is C = P
If P == null the SQL is C IS NULL
For _C != P_ in C#
If P != null the SQL is C <> P OR C IS NULL
If P == null the SQL is C IS NOT NULL
For C a nullable column and D another nullable column we cannot do any sniffing, so we need to generate a more complete expansion:
For _C == D_ in C# the SQL is C = D OR (C IS NULL AND D IS NULL)
For _C != D_ in C# the SQL is (C <> D OR (C IS NULL OR D IS NULL)) AND (C IS NOT NULL OR D IS NOT NULL)
As you can see, the first four cases are pretty good. By comparison IS [NOT] DISTINCT FROM would probably look clumsier. But for the last two cases it could be an improvement.
@divega I see, that makes sense, and IS [NOT] DISTINCT FROM definitely looks relevant for those last 2 cases (I had only the first 4 in mind). It does seems that IS [NOT] DISTINCT FROM is in the SQL:1999 standard but not very supported across providers.
I'll try to figure out the performance impact of using IS [NOT] DISTINCT FROM in PostgreSQL, but I'm pretty sure that it's not going to be worse than your last two cases. So it would be nice to be able to override them for PostgreSQL - although obviously nothing critical, I could open a backlog issue for you guys if you want.
Of course this discussion is unrelated to generating where clauses in a single place rather than one for queries and another for updates. Should this issue continue tracking that?
Support for IS DISTINCT FROM for query should be relatively easy to implement in providers using newly added expression fragment translators. We would just need to add new Expression(s) to represent the concept, and providers can then write a translator that would convert regular equality node into that new IsDistinctFrom node, as well as translate it to correct SQL in provider specific SQL generator. You wouldn't have the ability to peek into the parameter values to make the optimizations that we make in null semantics at the moment, but its a good start
Activity
anpete commentedon Sep 4, 2015
Agree, ideally update would just gen a SQL predicate expression that could then be rendered by the provider in the normal way.
WRT caching, we perform SQL gen for every query execution, and use the parameter values to inform the generation process. In this way we can gen quite nice SQL. At some point we may have a second level of caching that caches the SQL based on specific sets of parameter values, but we will only likely do that if we have to.
cc @AndriySvyryd for update.
roji commentedon Sep 4, 2015
Oh, I thought you guys were caching actual generated SQL (that would be obviously problematic with respect to parameters value having an effect on the SQL like this case). Never mind.
I'll work around this for now with
IS NOT DISTINCT FROMuntil you fix it in a better way.divega commentedon Sep 4, 2015
@roji It seems that the PostgreSQL provider (and any relational databases that support the standard operator) could short-circuit the three-valued logic compensations we are doing in our query pipeline and generate
IS [NOT] DISTINCT FROMinstead of regular equal/not equal comparisons when relational nulls are disabled on the context.Not sure how complex the short-circuiting would be (@maumar may have an idea) but just in case, do you see any downsides on doing that?
roji commentedon Sep 4, 2015
@divega, it's an interesting idea although I've seen rumors that using
IS [NOT] DISTINCT FROMmay have a detrimental impact on the use of indices (nothing verified yet). Is there an advantage to be gained by short-circuiting vs. just leaving things as they are?divega commentedon Sep 4, 2015
Interesting... Let me start by saying that if SQL Server supported
IS [NOT] DISTINCT FROMwe probably wouldn't have thought about generating three-valued logic compensation in the first place 😄I don't think this is high priority. It just made me wonder how it would look like.
We do compensate by default because many customers gave us strong feedback that when they write equality comparisons in their LINQ queries, they expect the behavior on the database server to match the behavior in memory.
As Andrew mentioned we do the trick of sniffing into parameter values to generate the simplest queries we can, but it is still possible that in some cases the compensations are still detrimental to the performance. And I imagine for a database server that has built-in support for
IS [NOT] DISTINCT FROMit could be easier to optimize that than to optimize the compensations.Here are some examples of what we do:
C == P_ in C#P !=nullthe SQL isC = PP == nullthe SQL isC IS NULLC != P_ in C#P != nullthe SQL isC <> P OR C IS NULLP == nullthe SQL isC IS NOT NULLC == D_ in C# the SQL isC = D OR (C IS NULL AND D IS NULL)C != D_ in C# the SQL is(C <> D OR (C IS NULL OR D IS NULL)) AND (C IS NOT NULL OR D IS NOT NULL)As you can see, the first four cases are pretty good. By comparison
IS [NOT] DISTINCT FROMwould probably look clumsier. But for the last two cases it could be an improvement.roji commentedon Sep 5, 2015
@divega I see, that makes sense, and
IS [NOT] DISTINCT FROMdefinitely looks relevant for those last 2 cases (I had only the first 4 in mind). It does seems thatIS [NOT] DISTINCT FROMis in the SQL:1999 standard but not very supported across providers.I'll try to figure out the performance impact of using
IS [NOT] DISTINCT FROMin PostgreSQL, but I'm pretty sure that it's not going to be worse than your last two cases. So it would be nice to be able to override them for PostgreSQL - although obviously nothing critical, I could open a backlog issue for you guys if you want.Of course this discussion is unrelated to generating where clauses in a single place rather than one for queries and another for updates. Should this issue continue tracking that?
divega commentedon Sep 5, 2015
Yes for the backlog item and yes for tracking the ability to override the where clauses here.
[-]Equality operator used in modification null comparison[/-][+]Use query generator to generate SQL for updates[/+][EF7] Workaround for null comparison in update
maumar commentedon Sep 5, 2015
Support for IS DISTINCT FROM for query should be relatively easy to implement in providers using newly added expression fragment translators. We would just need to add new Expression(s) to represent the concept, and providers can then write a translator that would convert regular equality node into that new IsDistinctFrom node, as well as translate it to correct SQL in provider specific SQL generator. You wouldn't have the ability to peek into the parameter values to make the optimizations that we make in null semantics at the moment, but its a good start
17 remaining items
Issue 'IS NULL' instead of '= null' in condition expressions during u…
roji commentedon Jun 16, 2016
Thanks for doing this for the RTM guys!
divega commentedon Jun 16, 2016
Thanks for letting us know about your concerns @roji!
Remove IS NOT DISTINCT FROM