Skip to content

Consider removing string concatenation null protection (COALESCE) #36210

Closed as not planned

Description

@roji

Our nullability processor has logic that identifies string concatenation, and if one side is nullable, adds a COALESCE to an empty string. IIUC this is inconsistent with the rest of EF's behavior: we do not add coalesce-to-zero for nullable arithmetic columns, and we don't add COALESCE nodes to function arguments when a null would make that function return null.

This is also the source of npgsql/efcore.pg#2889 on the PG side: we use string concatenation to generate an interval expression: CAST(t."Diff"::text || ' days' AS interval. But because the Diff column is nullable, SqlNullabilityProcessor adds the coalesce it to CAST(COALESCE(t."Diff"::text, '') || ' days' AS interval, causing mangled SQL (an empty string is bad here, just having the null would work as expected).

I'm not sure there's a specific reason we need to have special null protection for string concatenation and for nothing else..

Activity

  1. self-assigned this
    on Jun 8, 2025
  2. maumar commented on Jun 9, 2025

    @maumar
    Contributor

    introduced as fix to #3836

  3. roji commented on Jun 9, 2025

    @roji
    MemberAuthor

    I looked a little bit into this, an in C# itself the concatenation operator provides coalesce-to-empty-string behavior:

    string? s = null;
    Console.WriteLine(s + "foo"); // foo
    Console.WriteLine((s + "foo") is null); // False

    The same doesn't apply e.g. for integer addition:

    int? i = null;
    Console.WriteLine(i + 8); // empty string
    Console.WriteLine((i + 8) is null); // true

    So EF seems to be doing the right thing here, i.e. replicating C# behavior, even if that C# behavior could be considered somewhat inconsistent:

    • String concatenation operator: coalesces null to empty string
    • Integer addition operator: propagates null (return null if one of the operands is null)
    • Various other context: throw NullReferenceException

    I'll go ahead and close this as the expected behavior and try to think of a creative way to work around npgsql/efcore.pg#2889.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions