Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ has the rules.

## [10.3.0]

Some calls answer differently: [Changed answers in 10.3.0](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1030).

### Changed answers

- EF Core: a value set `Union` with a NULL set, or `Remove` with a NULL set or element, is NULL in a query, as `Where(… == null)` already said. A `Select` loaded the other set instead, for a nullable column, a missing outer-join child or a `null` parameter ([#395](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/395), [Migration](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1030)).

### Added

- EF Core: `Length` on `Int32Range` and `Int64Range` runs as SQL, so a query can filter and sort by it. It counts the integers as memory does: 0 for the empty range, and `null` for an unbounded side or, on `Int64Range`, a count larger than `long.MaxValue` ([What runs where](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#what-runs-where)).
Expand Down
22 changes: 7 additions & 15 deletions docs/efcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ Good to know:
as `text`, and loading a row runs the element's validation again.
- **An empty set and `NULL` stay different.** They are `{}` and `NULL` in the column. Whether the
column allows `NULL` is up to the property.
- **A NULL set or element gives NULL.** In a query, `Union` gives NULL when either set is NULL, and
`Remove` gives NULL when the set or the element is. A `Select` loads `null` for those rows, and
`Where(… == null)` selects them. In memory, both throw for a `null`.
- **Plain arrays keep their own mapping.** `T[]` and `List<T>` properties keep their native Npgsql
mapping, and both can sit next to value sets in one model. Scaffolding a database gives you plain
arrays, because choosing a set type is a decision you make in your model.
Expand Down Expand Up @@ -870,22 +873,11 @@ Good to know:

### Known differences in 10.2.0

Two queries in 10.2.0 return something other than the same LINQ over the loaded rows, without an
error. Both are fixed in 11.0.0 first, and each fix comes to 10.x in a later minor.
One query in 10.2.0 returns something other than the same LINQ over the loaded rows, without an
error. Its fix lands in 11.0.0 first and comes to 10.x in a later minor. A value set `Union` or
`Remove` over NULL in a projection was the other one, and 10.3.0 fixes it
([Changed answers in 10.3.0](migration.md#changed-answers-in-1030)).

- **A value set `Union` or `Remove` over NULL, in a projection.** `array_cat` and `array_remove`
skip a NULL argument instead of returning NULL. So `b.MaybeTags.Union(more)` loads `more` for a
row whose `MaybeTags` is NULL, and `b.Tags.Remove(b.Label)` loads `Tags` unchanged when `Label`
is NULL. A filter such as `b.MaybeTags.Union(more) == null` treats both as NULL. The NULL can
come from a nullable column or from the missing side of an outer join. Test for null yourself,
and the projection loads `null` too:

```csharp
bookings.Select(b => b.MaybeTags == null ? null : b.MaybeTags.Union(more));
bookings.Select(b => b.Label == null ? null : b.Tags.Remove(b.Label));
```

The fix is [#395](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/395).
- **`RangeLowerBoundComparer` in a query.** `RangeLowerBoundComparer<TRange, T>.Instance.Compare(a, b)`,
and `RangeSet<TRange, T>.LowerBoundComparer`, compare in the [range order](ranges.md#sort-ranges)
when they run in the database, not by lower bound. EF Core rewrites every `Compare(a, b)` call
Expand Down
32 changes: 32 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,35 @@ range factory over a NULL element is NULL, where it built `empty` (`CreateFinite
selected before.
- **`CompareTo` and `==` keep their own rules for a NULL column** ([Sort ranges](ranges.md#sort-ranges)).
- **Value sets still throw in memory for a `null` set.** Only the query changes for them.

## Changed answers in 10.3.0

Some calls return a different value. Your code stays the same, so the compiler can't show you where.
Each section below says how to find the calls whose answer changed. 11.0.0 makes the same changes.

### Check value set `Union` and `Remove` over NULL in queries

Check EF Core queries that load a value set `Union` or `Remove` where a set or an element can be NULL:
a nullable column, a column of a missing child in an outer join, or a `null` parameter.

PostgreSQL's `array_cat` and `array_remove` skip a NULL: `array_cat(NULL, '{z}')` is `{z}`, and
`array_remove('{a}', NULL)` is `{a}`. Before 10.3.0, the EF Core package called them as they are,
while a filter treated the result as NULL. So the same expression was NULL in a `Where` and a set in a
`Select`. Now it is NULL in both. With `more` as `{z}`:

| Query | Before 10.3.0 | Now |
|---|---|---|
| `Select(b => b.MaybeTags!.Union(more))`, for a NULL `MaybeTags` | `{z}` | `null` |
| `Select(b => b.Tags.Remove(b.Label!))`, for a NULL `Label` | `Tags` as stored | `null` |
| `Select(b => b.Tags.Union(b.Visit!.Tags))`, for a booking without a visit | `Tags` as stored | `null` |
| `Where(b => b.Tags.Remove(b.Label!).IsEmpty)`, for a NULL `Label` | the rows whose `Tags` is empty | none of them |
| `Where(b => b.MaybeTags!.Union(more) == null)` | the NULL rows | the NULL rows |

The SQL of a required column, a non-null parameter and a constant doesn't change. A set or element
that can be NULL gets `CASE WHEN … IS NULL THEN NULL ELSE array_cat(…) END`.

- **Search for `Union` and `Remove` in a `Select`.** A `!` on a nullable set or element, as in
`b.MaybeTags!.Union(more)`, often marks the place.
- **Say which set you want for a NULL one.** `(b.MaybeTags ?? StringSet.Empty).Union(more)` loads
`more` for the NULL rows, as the query did before.
- **In memory, both still throw for a `null`.** Only the query changes.
6 changes: 6 additions & 0 deletions src/CodoMetis.ValueRanges.EFCore.PostgreSQL/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ covers all four packages, which share one version number and release together.

## [10.3.0]

Some calls answer differently: [Changed answers in 10.3.0](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1030).

### Changed answers

- EF Core: a value set `Union` with a NULL set, or `Remove` with a NULL set or element, is NULL in a query, as `Where(… == null)` already said. A `Select` loaded the other set instead, for a nullable column, a missing outer-join child or a `null` parameter ([#395](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/395), [Migration](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1030)).

### Added

- EF Core: `Length` on `Int32Range` and `Int64Range` runs as SQL, so a query can filter and sort by it. It counts the integers as memory does: 0 for the empty range, and `null` for an unbounded side or, on `Int64Range`, a count larger than `long.MaxValue` ([What runs where](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#what-runs-where)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,51 @@ SqlExpression Element(int index)
// and deduplicated once an element is dropped — so unlike Union this composes
// safely with Count and equality.
case nameof(ValueSetExtensions.Remove) when arguments.Count == 2:
return sqlExpressionFactory.Function(
"array_remove",
[Set(0), ApplyElementMapping(Element(1), definition)],
nullable: true,
argumentsPropagateNullability: [true, true],
definition.SetClrType,
definition.SetTypeMapping);
return NullOverNullOperand(definition, "array_remove", Set(0), ApplyElementMapping(Element(1), definition));

// The server-side result is unsorted and undeduplicated — fine inside predicates (the
// operators above ignore both), and a materialized result re-canonicalizes on read.
// Emitted under a placeholder name that only ValueSetSqlGuard renames to array_cat, so
// the guard cannot be skipped silently.
case nameof(ValueSetExtensions.Union) when arguments.Count == 2:
// The server-side result is unsorted and undeduplicated — fine inside
// predicates (the operators above ignore both), and a materialized result
// re-canonicalizes on read. Emitted under a placeholder name that only
// ValueSetSqlGuard renames to array_cat, so the guard cannot be skipped silently.
return sqlExpressionFactory.Function(
ValueSetSqlGuard.UnguardedUnion,
[Set(0), Set(1)],
nullable: true,
argumentsPropagateNullability: [true, true],
definition.SetClrType,
definition.SetTypeMapping);
return NullOverNullOperand(definition, ValueSetSqlGuard.UnguardedUnion, Set(0), Set(1));

default:
return null;
}
}

/// <summary>
/// <c>Union</c> and <c>Remove</c> over a NULL operand are NULL, as EF Core's own value-producing
/// members are over NULL (#360): <c>CASE WHEN a IS NULL OR b IS NULL THEN NULL ELSE f(a, b) END</c>.
/// Neither function is strict, so the bare call answered with the other operand —
/// <c>array_cat(NULL, '{a}')</c> and <c>array_remove('{a}', NULL)</c> are both <c>{a}</c> — in a
/// projection, while a filter's <c>== null</c>, following the declaration, selected the row. An
/// operand is NULL in a nullable column, on the optional side of an outer join, or as a
/// <see langword="null"/> parameter. EF Core's nullability processing folds the test away for a
/// required column, a non-null parameter and a constant, so their SQL is the bare call.
/// </summary>
/// <remarks>
/// The function is reached only with both operands non-NULL, so it is declared as never NULL
/// and as propagating nothing: a non-strict function declared as propagating would let EF Core
/// rewrite <c>f(a, b) IS NULL</c> to <c>a IS NULL OR b IS NULL</c>, which is the disagreement
/// this fixes. <see cref="ValueSetSqlGuard"/> follows a union through the <c>CASE</c>.
/// </remarks>
private SqlExpression NullOverNullOperand(ISetTypeDefinition definition, string function, SqlExpression left, SqlExpression right)
=> sqlExpressionFactory.Case(
[
new CaseWhenClause(
sqlExpressionFactory.OrElse(sqlExpressionFactory.IsNull(left), sqlExpressionFactory.IsNull(right)),
sqlExpressionFactory.Constant(null, definition.SetClrType, definition.SetTypeMapping))
],
sqlExpressionFactory.Function(
function,
[left, right],
nullable: false,
argumentsPropagateNullability: [false, false],
definition.SetClrType,
definition.SetTypeMapping));

/// <summary>
/// Resolves the set type definition from the operands, by concrete set CLR type only.
/// </summary>
Expand Down
Loading
Loading