Allow providers to customize aggregate lowering of grouped Any/All

Aperta
#39,363 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp

Direzione di ricerca

Start in src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs, especially TryTranslateAggregateMethodCall, TryTranslateQuantifierOverGrouping, and the grouping and predicate processing it uses. Read the grouped-quantifier implementation and the linked provider discussion to understand the proposed hook and null/empty-input semantics. Done means providers can customize grouped Any/All translation while the existing default lowering and unsupported-composition fallback remain available.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area-query

Scenario

The PostgreSQL provider would like to translate predicated Any/All over a grouping to PostgreSQL's native bool_or/bool_and aggregates, without introducing new EF.Functions APIs:

context.Items
    .GroupBy(i => i.CategoryId)
    .Select(g => new
    {
        g.Key,
        Any = g.Any(i => i.Flag),
        All = g.All(i => i.Flag)
    });

For this unfiltered grouping over a nonnullable boolean column, the desired SQL is:

SELECT i."CategoryId", bool_or(i."Flag") AS "Any", bool_and(i."Flag") AS "All"
FROM "Items" AS i
GROUP BY i."CategoryId"

The same customization is useful when the quantifier appears in ORDER BY or HAVING, including over joined grouping sources. Provider discussion: https://github.com/npgsql/efcore.pg/issues/2394.

This is an extensibility request, not a claim that native boolean aggregates necessarily outperform EF's count-based lowering.

Extensibility gap

The grouped-quantifier lowering added in https://github.com/dotnet/efcore/pull/38937 is implemented in RelationalSqlTranslatingExpressionVisitor:

The outer TryTranslateAggregateMethodCall is already protected virtual, but overriding it does not let the provider reuse the existing grouping processing: TryTranslateAsEnumerableExpression is private. Reimplementing that parsing would duplicate EF's handling of grouping-element Where, Select, Distinct, ordering, and unsupported compositions.

Predicate handling also matters: RemapLambda is private and includes shared-type entity expansion, so a provider should ideally be able to reuse that processing as well, rather than just copying the parameter replacement.

Current workaround

A provider-local implementation can intercept predicated Any/All and rewrite them to private, expression-only aggregate markers:

g.Any(p) -> PrivateBoolOr(g.Select(p))
g.All(p) -> PrivateBoolAnd(g.Select(p))

Passing these expressions through the base visitor's custom-aggregate path reuses EF's grouping/filter/selector handling and reaches the provider's aggregate translator. The original operator chain is preserved, and unsupported compositions can fall back to the original quantifier translation.

This avoids copying private EF machinery, but requires synthetic methods and an expression-tree detour solely to reach an otherwise inaccessible translation stage.

Requested extensibility

Provide a protected virtual hook for grouped quantifier translation after the grouping source and quantifier predicate have been processed, but before committing to the generic LongCount lowering. The hook should expose the original quantifier, the EnumerableExpression with its existing filter, and the translated predicate, so providers can return native aggregate SQL or delegate to the default implementation.

Making the existing TryTranslateQuantifierOverGrouping protected virtual would be a useful starting point; if it continues to accept an unprocessed LambdaExpression, the predicate-remapping/translation machinery would also need to be reusable. A hook after predicate translation could avoid exposing those additional helpers.

The provider would remain responsible for its native aggregate's null/empty-input semantics, while retaining EF's existing predicate-free Any() behavior and unsupported-composition fallback. For example, filtered Any must return false on empty input, filtered All must return true, and SQL NULL predicate results must not simply be ignored by bool_and.

Lingua principale
C#
Stelle
14.8k
Fork
3.4k
Merge medio
3g 18h
PR unite (30g)
159

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/efcore

Tutte le issue di dotnet/efcore

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.