Skip to content

feat: translate value set Union, Add, Intersect and Except canonically for every family but text (#478) - #480

Merged
CaffeinatedCoder merged 4 commits into
mainfrom
feat/478-canonical-set-operations
Oct 26, 2026
Merged

CaffeinatedCoder merged 4 commits into
mainfrom
feat/478-canonical-set-operations

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #478

Problem

Value set Intersect, Except and Add ran in memory only, and a server-side Union was array_cat, which the SQL guard allowed only under @>/<@/&&, a null test or the final projection. So equality, Count, GroupBy, Distinct, OrderBy, set operations, IN and ExecuteUpdate over any of them threw, for every family. The reason was text: ORDER BY sorts it by the database collation, not ordinally. For the other families PostgreSQL sorts as memory does, so canonical form can be built in plain SQL.

Change

  • Translator (ValueSetsMethodCallTranslator): for every family with the new ISetTypeDefinition.CanonicalizesInSql (all but StringSet and StringSet<T>), Union, Add, Intersect and Except emit a placeholder function inside the existing NULL-over-NULL CASE. Text keeps array_cat and the refusals.
  • Guard (ValueSetSqlGuard.CanonicalExpander): builds each placeholder after EF Core's alias pass, which a translator can't reach and which parses every alias as one letter and a number:
    ARRAY((SELECT DISTINCT ON (vrN.x) vrN.x FROM unnest(…) WITH ORDINALITY AS vrN(x) WHERE vrN.x IS NOT NULL [AND filter] ORDER BY vrN.x, vrN.ordinality)).
    ORDER BY x, ordinality keeps the first copy of each value, as ValueSetCore does (1.0/1.00, -0/0). NULL elements another tool stored are dropped. Intersect/Except filter by containment (b @> ARRAY[x]), because Npgsql's ANY node needs an enumerable CLR type. Where order and duplicates can't change the answer (@>, <@, &&, cardinality(…) = 0, a null test, an outermost projection that neither groups nor deduplicates, and a placeholder's own operands) it writes the multiset form instead: array_cat, or the filter without sorting. The walker treats a placeholder as canonical. Without the guard the placeholders reach PostgreSQL as unknown functions, so it still fails closed.
  • Same code on EF Core 10 and 11 (spike, Translate value set Intersect, Except, Add and a canonical Union for the non-text families with composed SQL #478): the internal nodes it uses (SelectExpression's immutable constructor, PgUnnestExpression, PgNewArrayExpression) have the same API on both.
  • Docs: efcore.md (value sets, Union, rows written by other tools, what runs where), value-sets.md (a wrapper's CompareTo must order as its underlying type does; Vogen and StronglyTypedId do), getting-started.md, the four package READMEs, architecture.md, testing.md, and AGENTS.md (the array_cat rule narrowed to text, a rule for canonical form added).
  • Oracles: claims E17–E20, E35 and E75 reworded or narrowed to text; E78–E82 added. translation-support.json reclassifies Intersect/Except/Add as server except on text. CanonicalSetOperationParityTests registered with the seed canonical-set-keeps-any-copy. The seed sql-processor-reuses-its-first-output regenerated against the changed line.
  • Changelogs: root, EF Core and EF Core NodaTime, under 11.0.0-preview.1. The 10.x port (cherry-pick into release/10.x, 10.4.0) follows this merge.

Tests

  • No database: CanonicalSetTranslationTests asserts the whole SQL for each form, every set column of the model (canonical for all but text, refused for text), aliases per subquery, and canonical form kept under GroupBy and Distinct. NodaCanonicalSetTranslationTests covers the five NodaTime sets and their arities. SqlGuardRegistrationTests asserts the four placeholders reach the SQL when the processor factory is replaced. The text refusals moved to StringSet (Intersect_OnText, Except_OnText, Add_OnText, TextWrapperSets_Union_Count_FailsTranslation).
  • PostgreSQL 16 and 18: CanonicalSetOperationParityTests runs every pair of subsets of three probes for every discovered family but text, stored canonical, out of order and twice, and with a NULL element, under ==, Count, ExecuteUpdate, the projection, containment and IsEmpty, against memory over the pairs as stored. Each question asserts its SQL reached the server and every row answered. It also checks the copy kept (thirty decimals a side by scale, and the sign of zero for floats) and that Add binds a Coptic date as the set stores it. CanonicalSetCompositionTests runs the compositions text still refuses over Int32Set. ValueSetNullOperandParityTests covers the four operations over a NULL operand.
  • Seeds: a plain DISTINCT (dropping ORDER BY x, ordinality) fails TheFirstCopyOfEachValue_IsKept_AsInMemory (DecimalSet and DecimalSet<Money> keep 1.00 where memory keeps 1.0). Binding Add's element through the provider's mapping instead of the set's fails Add_StoresTheElementAsTheSetWould_AsInMemory.
  • Runs: all no-database suites pass (core 1399, NodaTime 239, EF 380, EF NodaTime 105, conventions 417, ASP.NET Core 92). The integration suite passes 326/326 on PostgreSQL 16 and 18 with 4 test workers. With this Mac's default worker count the server runs out of its 100 connections; unchanged main does too there (2 failures), and the new tests run two families at a time and empty their pools.

🤖 Generated with Claude Code

CaffeinatedCoder and others added 4 commits October 26, 2026 10:48
…y for the non-text families (#478)

For every family PostgreSQL sorts as memory does, the translator emits a
placeholder per operation inside the NULL-over-NULL CASE, and the SQL guard
builds it after EF Core's alias pass: ARRAY((SELECT DISTINCT ON (x) x FROM
unnest(...) WITH ORDINALITY ... WHERE x IS NOT NULL ORDER BY x, ordinality)),
or the multiset form under @>, <@, &&, IsEmpty, a null test and the outermost
projection. Text keeps array_cat and the refusals (#479).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#478)

CanonicalSetOperationParityTests runs Union, Add, Intersect and Except over
every pair of subsets of three probes for every family but text, stored
canonical, out of order and twice, and with a NULL element, under ==, Count,
ExecuteUpdate, the projection, containment and IsEmpty; and the copy of a value
kept, for decimals by scale and floats by the sign of zero. The seed
canonical-set-keeps-any-copy (a plain DISTINCT) is caught by it.
CanonicalSetCompositionTests runs the compositions text still refuses, and the
NULL-operand parity covers the new operations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ut text (#478)

The EF guide, the value sets guide, getting started and the four package
READMEs say where the operations now run, that equal values keep the first
copy, what a NULL element another tool stored does, and that a wrapper's
CompareTo must order as its underlying type does. AGENTS.md narrows the
array_cat rule to text and adds the rule for canonical form; claims E17-E20,
E35 and E75 follow, E78-E82 are new; the translation inventory and three
changelogs record the change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.4.0 milestone Oct 26, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request area/value-sets Value set families and wrapper arities package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime labels Oct 26, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 46c31df into main Oct 26, 2026
14 of 16 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/478-canonical-set-operations branch October 26, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/value-sets Value set families and wrapper arities enhancement New feature or request package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translate value set Intersect, Except, Add and a canonical Union for the non-text families with composed SQL

1 participant