Skip to content

Implement null semantics properly for PG-specific operations #1142

Closed

Description

@roji

We have all kinds of PostgreSQL-specific operations, but haven't really attempted to implement correct null semantics for them.

  • In some cases we simply ignore null semantics and translate as-is (e.g. array-related methods in NpgsqlSqlTranslatingExpressionVisitor.VisitArrayMethodCall)
  • In other cases we implement null semantics but don't allow opt-out with the relational nulls context option (e.g. the Contains to ANY translation in NpgsqlArrayMethodTranslator.Translator).

Null semantics is currently being refactored on the EF Core side, with provider extensibility in mind. When that's done we should do a pass on our side and see what to do.

Specific cases:

  • GREATEST/LEAST: need special handling since they don't propagate nulls, but return null if only nulls are present. WIP in https://github.com/roji/efcore.pg/tree/GreatestLeast, but blocking on lack of way to set _nullable in VisitSqlFunction. This is necessary even thought Math.{Min,Max} accept only non-nullable int, because we propagate nulls through optional navigations (C.NullNavigation.IntProperty).
  • ComplexNavigationsQueryTestBase.Accessing_optional_property_inside_result_operator_subquery. Our null semantics is probably wrong there.

Activity

  1. added this to the 5.0.0 milestone on Nov 26, 2019
  2. self-assigned this
    on Nov 26, 2019
  3. KinoshitaShimizu commented on Feb 3, 2020

    @KinoshitaShimizu

    I'd like to comment, I am having a terrible issue related to this.

    I have the following snippet of C# code:

    int[] myIdsToSelect = new int[] { 10, 22, 33, 44, 55 };
    var results = await DB.Items.Where(x => myIdsToSelect.Contains(x.Id)).ToListAsync();

    Which generates the following WHERE SQL:

    WHERE COALESCE(i.id = ANY(ARRAY[10, 22, 33, 44, 55]), FALSE)

    Now, this is terrible. This is beyond terrible. My queries which regularly take 0.010 seconds to execute, are taking 15 seconds. The reason is that PostgreSQL is not going to be able to optimize a COALESCE(i.id = ANY($1)).

    As solution I have had to rollback.

    If this is the wrong issue to notify this, please let me know. I just tracked down the issue locally and found a reference to this issue on it. Thank you.

  4. roji commented on Feb 3, 2020

    @roji
    MemberAuthor

    @KinoshitaShimizu have you tried 3.1.1 which was released a couple days ago? Because that should be gone there (see #1152).

  5. KinoshitaShimizu commented on Feb 3, 2020

    @KinoshitaShimizu

    @roji Thank you very much for your reply.

    I have tried with 3.1.1 and 3.1.1.1, the issue seems to persist (ensured by checking the Npgsql.EntityFrameworkCore.PostgreSQL.dll version on /Debug folder).

  6. roji commented on Feb 3, 2020

    @roji
    MemberAuthor

    Can you please open a new issue for this, with the coffee sample? I'll look into it.

  7. KinoshitaShimizu commented on Feb 3, 2020

    @KinoshitaShimizu

    @roji Created the issue #1254.

    I do not know what the "coffee sample" is, although I looked it up. Made a simple project to reproduce, logger set to output generated query.

    If I should correct, please let me know.

    Thank you very much for your hard work.

  8. added 6 commits that reference this issue on May 16, 2020
    bedc9f6
    b4f28aa
    c41d875
    5d96ede
    c741e2d
    7385c75
  9. added a commit that references this issue on May 25, 2020
    ed428fd
  10. roji commented on Jun 7, 2020

    @roji
    MemberAuthor

    Reopening to take another look for when relational semantics is activated. As @maumar pointed out, the current code returns nullable=false in some incorrect cases where it should be true.

  11. reopened this on Jun 7, 2020
  12. roji commented on Aug 29, 2020

    @roji
    MemberAuthor

    Closing for now, will investigate based on user reports.

  13. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions