Skip to content

Update Pipeline: Use C# null semantics for WHERE clauses (concurrency tokens) #3023

Closed

Description

@roji

I'm having some OptimisticConcurrency tests fail because the following SQL gets sent to the database:

UPDATE "Drivers" SET "Podiums" = $1 WHERE "Id" = $2 AND "Version" = $3 RETURNING "Version"

with $3 being equal to null.

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?

Activity

  1. anpete commented on Sep 4, 2015

    @anpete
    Contributor

    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.

  2. roji commented on Sep 4, 2015

    @roji
    MemberAuthor

    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.

  3. divega commented on Sep 4, 2015

    @divega
    Contributor

    @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?

  4. roji commented on Sep 4, 2015

    @roji
    MemberAuthor

    @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?

  5. divega commented on Sep 4, 2015

    @divega
    Contributor

    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.

  6. roji commented on Sep 5, 2015

    @roji
    MemberAuthor

    @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?

  7. divega commented on Sep 5, 2015

    @divega
    Contributor

    Yes for the backlog item and yes for tracking the ability to override the where clauses here.

  8. changed the title [-]Equality operator used in modification null comparison[/-] [+]Use query generator to generate SQL for updates[/+] on Sep 5, 2015
  9. maumar commented on Sep 5, 2015

    @maumar
    Contributor

    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

  10. added this to the 7.0.0 milestone on Sep 8, 2015
  11. added this to the milestone on Sep 17, 2015
  12. 17 remaining items

  13. added a commit that references this issue on Jun 3, 2016
    f41d2fc
  14. modified the milestones: 1.0.0, 1.0.1 on Jun 3, 2016
  15. removed their assignment
    on Jun 3, 2016
  16. roji commented on Jun 16, 2016

    @roji
    MemberAuthor

    Thanks for doing this for the RTM guys!

  17. divega commented on Jun 16, 2016

    @divega
    Contributor

    Thanks for letting us know about your concerns @roji!

  18. added a commit that references this issue on Jun 16, 2016
  19. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions