Skip to content

Fix ST_Extent box2d cast being removed by EF simplifier#3828

Merged
roji merged 1 commit intomainfrom
fix-envelope-combine-box2d
May 4, 2026
Merged

Fix ST_Extent box2d cast being removed by EF simplifier#3828
roji merged 1 commit intomainfrom
fix-envelope-combine-box2d

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented May 4, 2026

ST_Extent returns box2d in PostgreSQL, but the aggregate expression was assigned the geometry type mapping. EF's SqlExpressionSimplifyingExpressionVisitor (added in the latest sync) strips Convert nodes when the operand and cast have the same store type. Since both had geometry, the ::geometry cast was removed, leaving the raw box2d result which has no binary output function in Npgsql.

Fix: give the ST_Extent aggregate a box2d store type (via WithStoreTypeAndSize), so the cast from box2d to geometry is recognized as a real type conversion and preserved.

Same root cause as the type mapping fixes in #3827.

ST_Extent returns box2d in PostgreSQL, but the aggregate expression was
assigned the geometry type mapping. EF's SqlExpressionSimplifyingExpressionVisitor
then stripped the ::geometry cast as a no-op (same store type), leaving the raw
box2d result which has no binary output function. Fix by giving the aggregate
a box2d store type so the cast is recognized as a real type conversion.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 4, 2026 14:38
@roji roji enabled auto-merge (squash) May 4, 2026 14:41
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts the NetTopologySuite aggregate translation for EnvelopeCombiner.CombineAsGeometry so that EF Core’s SQL simplifier doesn’t strip the required cast when translating to PostGIS ST_Extent. This ensures ST_Extent’s box2d result is properly cast to geometry, avoiding Npgsql binary read failures.

Changes:

  • Sets the ST_Extent aggregate’s result type mapping store type to box2d (via WithStoreTypeAndSize) so EF recognizes the subsequent cast to geometry as non-no-op.
  • Avoids repeated GetMapping() calls by caching the geometry mapping in a local.

@roji roji merged commit 35f5f07 into main May 4, 2026
19 checks passed
@roji roji deleted the fix-envelope-combine-box2d branch May 4, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants