Skip to content

Add AggregateFunctionExpression as a relational base for provider aggregate functions - #39234

Merged
AndriySvyryd merged 4 commits into
dotnet:mainfrom
Abde1rahman1:feature/aggregate-function-expression
Oct 15, 2026
Merged

AndriySvyryd merged 4 commits into
dotnet:mainfrom
Abde1rahman1:feature/aggregate-function-expression

Conversation

@Abde1rahman1

@Abde1rahman1 Abde1rahman1 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #38357

Adds a public AggregateFunctionExpression to Relational. It is the base type for provider aggregate function expressions: SqlServerAggregateFunctionExpression now, and later
SqliteAggregateFunctionExpression and Npgsql's PgFunctionExpression.

Changes

Relational

  • New AggregateFunctionExpression (Query/SqlExpressions) with Name, Arguments, Orderings, IsNullable and ArgumentsPropagateNullability.
    • Subclasses override one hook, protected virtual Create(...), so Update, ApplyTypeMapping and VisitChildren keep returning the derived type.
    • protected virtual QuoteConstructorArguments() lets subclasses with the same constructor implement Quote() without duplicating code.
    • Equals also compares the runtime type.
  • QuerySqlGenerator: generates NAME(args ORDER BY ...), the SQL-standard form, by default. Providers can change where the orderings go by overriding the new
    GenerateAggregateFunctionOrderings(expression, insideArgumentList) hook.
  • SqlNullabilityProcessor: the nullability logic for aggregate functions moved here from the SQL Server processor.
  • SqlExpressionFactory.ApplyTypeMapping: handles AggregateFunctionExpression.

SQL Server

  • SqlServerAggregateFunctionExpression is now a thin subclass with the constructor, Create, Quote and its WITHIN GROUP printing.
  • SqlServerQuerySqlGenerator overrides only the orderings hook, to emit WITHIN GROUP (ORDER BY ...).
  • Removed the duplicated code from SqlServerSqlNullabilityProcessor, SqlServerQuerySqlGenerator and SqlServerSqlExpressionFactory.

Design notes

  • IsDistinct and FILTER (WHERE ...) are deliberately left out of the base. Relational already represents them with DistinctExpression and CASE WHEN, and no in-box provider emits FILTER.
    They can be added later without a breaking change, and Npgsql can carry them in its subclass.
  • This PR is a refactor only. The generated SQL does not change. Translating ordered group_concat on SQLite (which needs SQLite 3.44 or later) will come in a follow-up PR built on this base.

Copilot AI balanced review requested due to automatic review settings October 12, 2026 20:52
@Abde1rahman1
Abde1rahman1 requested a review from a team as a code owner October 12, 2026 20:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 12, 2026 21:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 12, 2026 21:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The public API lacks validation and focused coverage, and the advertised visitor extension point is absent.

2 open findings

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 14, 2026 23:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Abde1rahman1

Copy link
Copy Markdown
Contributor Author

@AndriySvyryd I resolved all Copilot issues

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The refactor preserves existing behavior; the remaining focused regression-test suggestion is non-blocking.

0 open findings

Previously missed (1)

In code that hasn't changed since last review

Low severity Add regression tests for preserving subclass state during expression recreation

src/​EFCore.Relational/​Query/​SqlExpressions/​AggregateFunctionExpression.cs:84

The new Create dispatch lacks focused regression coverage: the added generator tests render an unchanged base instance, and the existing SQL Server precompiled-query test covers quoting rather than preservation of extra subclass state. Add relational expression tests with a subclass carrying extra state. Force recreation through Update, ApplyTypeMapping, and visitor rewrites of arguments and orderings, then assert that the runtime type and state survive. Also verify that an unchanged Update returns the same instance.

🧠 Review effort: Balanced

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 137806d into dotnet:main Oct 15, 2026
31 of 33 checks passed
@github-actions github-actions Bot added the api-review This PR or issue is introducing public API changes that need to be reviewed label Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

API review baseline changes for src/EFCore.Relational/EFCore.Relational.baseline.json

Show diff

The diff below was generated by ApiChief between the base and the PR.

  class Microsoft.EntityFrameworkCore.Query.QuerySqlGenerator : System.Linq.Expressions.ExpressionVisitor
+ virtual void GenerateAggregateFunctionOrderings(AggregateFunctionExpression aggregateFunctionExpression, bool insideArgumentList);
+ virtual Expression VisitAggregateFunction(AggregateFunctionExpression aggregateFunctionExpression);
+ class Microsoft.EntityFrameworkCore.Query.SqlExpressions.AggregateFunctionExpression : Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlExpression
+ virtual IReadOnlyList<SqlExpression> Arguments { get; }
+ virtual IReadOnlyList<bool> ArgumentsPropagateNullability { get; }
+ virtual bool IsNullable { get; }
+ virtual string Name { get; }
+ virtual IReadOnlyList<OrderingExpression> Orderings { get; }
+ AggregateFunctionExpression(string name, IReadOnlyList<SqlExpression> arguments, IReadOnlyList<OrderingExpression> orderings, bool nullable,
+     IEnumerable<bool> argumentsPropagateNullability, Type type, RelationalTypeMapping? typeMapping);
+ virtual AggregateFunctionExpression ApplyTypeMapping(RelationalTypeMapping? typeMapping);
+ virtual AggregateFunctionExpression Create(string name, IReadOnlyList<SqlExpression> arguments, IReadOnlyList<OrderingExpression> orderings, bool nullable,
+     IEnumerable<bool> argumentsPropagateNullability, Type type, RelationalTypeMapping? typeMapping);
+ virtual IEnumerable<Expression> QuoteConstructorArguments();
+ virtual AggregateFunctionExpression Update(IReadOnlyList<SqlExpression> arguments, IReadOnlyList<OrderingExpression> orderings);
  class Microsoft.EntityFrameworkCore.Query.SqlNullabilityProcessor : System.Linq.Expressions.ExpressionVisitor
+ virtual SqlExpression VisitAggregateFunction(AggregateFunctionExpression aggregateFunctionExpression, bool allowOptimizedExpansion, out bool nullable);

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 20, 2026
@AndriySvyryd AndriySvyryd removed the api-review This PR or issue is introducing public API changes that need to be reviewed label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create AggregateFunctionExpression

3 participants