Skip to content

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

Open

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
    bugSomething isn't working
    area/range-setRangeSet and multirange algebra
    area/value-setsValue set families and wrapper arities
    package/efcoreAffects CodoMetis.ValueRanges.EFCore.PostgreSQL
    package/efcore-nodatimeAffects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime
    on Oct 19, 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-setRangeSet and multirange algebraarea/value-setsValue set families and wrapper aritiesbugSomething isn't workingpackage/efcoreAffects CodoMetis.ValueRanges.EFCore.PostgreSQLpackage/efcore-nodatimeAffects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTimeseverity/lowCosmetic, message or documentation-level difference

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions