Skip to content

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

Closed

Description

@CaffeinatedCoder

Problem

EmptyCollectionRewriter (#137) visits every non-SELECT node twice, so the plugin's SQL processing takes time exponential in the SQL tree's depth, on every query, ExecuteUpdate and ExecuteDelete (it runs through ValueRangesParameterBasedSqlProcessorFactory). The query does not need to touch a range or set column. Found by the final pre-release analysis on add665c.

src/CodoMetis.ValueRanges.EFCore.PostgreSQL/Query/EmptyCollectionRewriter.cs:53:

if (base.VisitExtension(node) is not SelectExpression select) return base.VisitExtension(node);

Where(r => r.N + 0 > r.Id || r.N + 1 > r.Id || …), timed with ToQueryString:

OR terms 16 18 20 22
with the plugin 36 ms 107 ms 353 ms 1,446 ms
without the plugin 1 ms 1 ms 1 ms 1 ms

It doubles with each term, so a dynamic filter of 30 or more terms effectively never compiles. #137 is new in 10.0.0, so nothing that shipped is affected.

Change

var visited = base.VisitExtension(node); if (visited is not SelectExpression select) return visited;. With that change, all four sizes take 1–2 ms, and the EF unit (285) and integration (265) suites stay green. Check the plugin's other ExpressionVisitors in Query/ for the same double visit.

Done when

A test translates a query with 40 OR'd non-foldable predicates within a generous bound (or counts the rewriter's visits so the result doesn't depend on timing), and fails with the double visit restored.

Activity

  1. added this to the 10.0.0 milestone on Oct 19, 2026
  2. added
    bug
    package/efcore
    severity/high
    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

    bugpackage/efcoreseverity/high

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions