Skip to content

main: the empty-root rewrite casts EF Core's _ord column to the range type (NULL::int4range AS _ord) #319

Closed

Description

@CaffeinatedCoder

Problem

EmptyCollectionRewriter casts every NULL constant of EF Core's empty query root whose CLR type is a registered range or value set type. EF Core gives the constant of its _ord ordering column that type too, so _ord is cast to the range type. Measured on main at 5f08625 with a translation probe:

var empty = new List<Int32Range>();
db.Bookings.Select(b => empty.Where(w => w.Overlaps(b.Seats)).FirstOrDefault())
SELECT (
    SELECT e."Value"
    FROM (
        SELECT NULL::int4range AS _ord, NULL::int4range AS "Value"
        WHERE FALSE
    ) AS e
    WHERE e."Value" && b."Seats"
    ORDER BY e._ord
    LIMIT 1)
FROM "Bookings" AS b

The same query over a non-empty list writes FROM (VALUES (0, @full1), (1, @full2)) AS f(_ord, "Value"), an integer _ord. On release/10.x the EF Core 10.0.0/10.0.1 path writes 0 for it (OneRowOfNulls).

This is latent. Under WHERE FALSE no row exists, and ORDER BY over a range-typed NULL is valid SQL, so nothing fails today. A shape in which EF Core compares _ord with an integer would fail on the server, for the empty list only, with operator does not exist: int4range = integer.

Found by the review of PR #292 on 2026-10-19, and reproduced since.

Change

Leave _ord out of the cast in TypeTheNulls (match the projection alias, as OneRowOfNulls does on 10.x) and write it as an integer, the type it has when the list has elements. Assert the whole subquery of the FirstOrDefault shape in RangeCollectionTranslationTests, and prove the test by reverting the change.

Release

Not silent: no answer changes. #327 asks whether the cast belongs in the type mappings instead. The port to release/10.x is #325.

Activity

  1. added this to the 11.0.0 milestone on Oct 19, 2026
  2. added
    bug
    area/range-set
    area/value-sets
    package/efcore
    package/efcore-nodatime
    on Oct 19, 2026
  3. added
    severity/low
    on Oct 19, 2026
  4. CaffeinatedCoder commented on Oct 24, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Re-evaluated 2026-10-24: the port, #325, is now in 10.2.0 (epic #358), so this main fix comes before the 10.2.0 tag.

  5. CaffeinatedCoder commented on Oct 24, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed in PR #389. _ord is now 0, and the whole FirstOrDefault statement is pinned; the test was proven by reverting the fix. The 10.x port is PR #390 (#325).

  6. added a commit that references this issue on Oct 24, 2026
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

    area/range-setarea/value-setsbugpackage/efcorepackage/efcore-nodatimeseverity/low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions