Skip to content

fix: EmptyCollectionRewriter visits each SQL node once - #263

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/empty-collection-single-visit
Oct 19, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/empty-collection-single-visit

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #249

Problem

EmptyCollectionRewriter (#137) visited the children of every non-SELECT node twice: if (base.VisitExtension(node) is not SelectExpression select) return base.VisitExtension(node);. So its pass over the final SQL tree took time exponential in the tree's depth. It runs on every query, ExecuteUpdate and ExecuteDelete, through ValueRangesParameterBasedSqlProcessorFactory, whether or not the query touches a range or set. A filter of 22 OR'd terms took 1.4 s against 1 ms without the plugin, and 30 or more terms effectively never compiled.

Change

Tests

SqlProcessingCostTests (EF Core translation tests, no database):

  • EmptyCollectionRewriter_VisitsEachNodeOnce feeds the rewriter a chain of 12 counting extension nodes and expects 12 visits. This count doesn't depend on timing.
  • AFilterOfFortyOredTerms_Translates_InAboutAMillisecondPerTerm translates 40 OR'd non-foldable predicates through ToQueryString within a 20 s bound. The wait is bounded, so with the bug the test fails instead of hanging.
  • Proved by reverting. With the double visit restored, the count is 8190 and the 40-term translation had not finished after 20 s.

The EF Core translation suite (287) passes.

🤖 Generated with Claude Code

The rewriter visited the children of every non-SELECT node twice, once to test the result and
once to return it, so its pass over the final SQL tree took 2^depth visits. It runs on every
query, ExecuteUpdate and ExecuteDelete through ValueRangesParameterBasedSqlProcessorFactory,
whether or not a range or set is involved: 22 OR'd terms took 1.4 s to translate against 1 ms
without the plugin. The other visitors under Query/ visit once.

SqlProcessingCostTests counts visits through a counting extension node (12 levels: 12 visits,
8190 with the double visit) and translates a 40-term OR filter within a 20 s bound, which it
does not finish with the double visit restored.

Closes #249

Found-By: CA
Defect-Class: SQL
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/high Wrong results, crash or failed writes in a realistic scenario labels Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/high Wrong results, crash or failed writes in a realistic scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EmptyCollectionRewriter visits every node twice: query compilation is exponential in SQL tree depth

1 participant