Skip to content

Add enum and comparison translators for Cosmos - #35405

Merged
AndriySvyryd merged 9 commits into
dotnet:mainfrom
ChrisJollyAU:enumtranslators
Sep 11, 2026
Merged

AndriySvyryd merged 9 commits into
dotnet:mainfrom
ChrisJollyAU:enumtranslators

Conversation

@ChrisJollyAU

Copy link
Copy Markdown
Contributor

Adds the EnumMethod and Comparison translators to Cosmos.

  • This supports the enum HasFlag and ToString
  • Also adds .CompareTo (in comparison)

The above translators are almost the same as the relational version.

With the expressions, I needed to add in the CaseExpression which is straight from the relational version (easier if it ends up both having a common base in the future).

Query sql generator: Ouputs an IIF statement when processing a CaseExpression

Moved the respective tests to the new test classes.

Making this a draft as some of the enum ToString tests don't work yet. The below line (71) currently only returns null whereas in relational it is a unary convert node. I suspect if combined with #35000 that should be fine after that

_sqlExpressionFactory.Convert(instance, typeof(string))

Fixes #35317
Also continues #17246

@AndriySvyryd

Copy link
Copy Markdown
Member

@ChrisJollyAU Do you still plan to finish this at some point?

@ChrisJollyAU

Copy link
Copy Markdown
Contributor Author

@AndriySvyryd Yes.

There is just one test I had still outstanding. a Comos NULL issue probably

 Microsoft.EntityFrameworkCore.Query.Translations.EnumTranslationsCosmosTest.ToString_nullable_enum_property_projection(async: True)
 Source: EnumTranslationsTestBase.cs line 182
 Duration: 55 ms

Message: 
Assert.Equal() Failure: Values differ
Expected:
Actual: null

@AndriySvyryd

Copy link
Copy Markdown
Member

@ChrisJollyAU There have been a lot of Cosmos changes in main, perhaps it works now

ChrisJollyAU and others added 3 commits August 6, 2026 17:36
…string

Nullable<TEnum>.ToString() returns an empty string when there is no value,
but the enum translator's else branch used CoalesceUndefined, which lowers to
Cosmos' ?? operator. That only substitutes for undefined, not for null, so a
null enum was projected as null.

The guard also has to sit on the instance rather than on the converted value:
unlike SQL's CAST(NULL AS nvarchar), Cosmos' ToString(null) yields the string
"null" rather than propagating the null, so there is nothing left to coalesce
afterwards.

Updates the four affected baselines.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@ChrisJollyAU
ChrisJollyAU marked this pull request as ready for review August 6, 2026 12:08
@ChrisJollyAU
ChrisJollyAU requested a review from a team as a code owner August 6, 2026 12:08
@ChrisJollyAU

Copy link
Copy Markdown
Contributor Author

@AndriySvyryd Found my problem. Now everything passes

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.

Pull request overview

Adds Cosmos query translation support for enum method patterns (HasFlag, ToString) and comparison methods (Compare, CompareTo) by introducing CASE/IIF SQL expression support in the Cosmos query pipeline, and reshuffling/expanding translation tests accordingly.

Changes:

  • Add Cosmos method-call translators for enum methods and comparison methods.
  • Introduce CaseExpression/CaseWhenClause and implement CASE construction/type-mapping + SQL generation via nested IIF(...).
  • Move/add enum ToString translation tests into EnumTranslationsTestBase and update provider-specific assertions; enable Cosmos comparison translation tests with new SQL baselines.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
test/EFCore.SqlServer.FunctionalTests/Query/Translations/EnumTranslationsSqlServerTest.cs Adds SQL Server assertions for enum ToString translations on basic-types model.
test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs Removes enum ToString overrides now moved out of GearsOfWar test base.
test/EFCore.SqlServer.FunctionalTests/Query/Inheritance/TPTGearsOfWarQuerySqlServerTest.cs Removes enum ToString overrides now moved out of GearsOfWar test base.
test/EFCore.SqlServer.FunctionalTests/Query/Inheritance/TPCGearsOfWarQuerySqlServerTest.cs Removes enum ToString overrides now moved out of GearsOfWar test base.
test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs Removes enum ToString overrides now moved out of GearsOfWar test base.
test/EFCore.Sqlite.FunctionalTests/Query/Translations/EnumTranslationsSqliteTest.cs Adds SQLite assertions for enum ToString translations on basic-types model.
test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs Removes enum ToString overrides now moved out of GearsOfWar test base.
test/EFCore.Specification.Tests/Query/Translations/EnumTranslationsTestBase.cs Adds new enum ToString test coverage to the shared spec base.
test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs Removes enum ToString tests from GearsOfWar spec base (moved to enum translations base).
test/EFCore.Cosmos.FunctionalTests/Query/Translations/StringTranslationsCosmosTest.cs Updates Cosmos string comparison translation tests to assert generated IIF(...) SQL.
test/EFCore.Cosmos.FunctionalTests/Query/Translations/MiscellaneousTranslationsCosmosTest.cs Updates Cosmos non-string comparison translation tests to assert generated IIF(...) SQL.
test/EFCore.Cosmos.FunctionalTests/Query/Translations/EnumTranslationsCosmosTest.cs Enables Cosmos enum HasFlag and adds Cosmos enum ToString SQL assertions.
src/EFCore.Cosmos/Query/Internal/Translators/CosmosEnumMethodTranslator.cs New Cosmos translator for enum HasFlag/ToString.
src/EFCore.Cosmos/Query/Internal/Translators/CosmosComparisonTranslator.cs New Cosmos translator for Compare/CompareTo patterns producing CASE expressions.
src/EFCore.Cosmos/Query/Internal/SqlExpressionFactory.cs Adds type-mapping support and factory methods for CASE expressions.
src/EFCore.Cosmos/Query/Internal/ISqlExpressionFactory.cs Adds CASE factory APIs to Cosmos SQL expression factory interface.
src/EFCore.Cosmos/Query/Internal/Expressions/StructuralTypeProjectionExpression.cs Adjusts scalar property access expression CLR type handling (nullable unwrap).
src/EFCore.Cosmos/Query/Internal/Expressions/CaseWhenClause.cs Introduces Cosmos CASE WHEN clause expression node.
src/EFCore.Cosmos/Query/Internal/Expressions/CaseExpression.cs Introduces Cosmos CASE expression node.
src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.cs Adjusts parameter expression creation (notably around type/nullability handling).
src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs Adds SQL generation for CASE by emitting nested IIF(...).
src/EFCore.Cosmos/Query/Internal/CosmosMethodCallTranslatorProvider.cs Wires new enum/comparison translators into Cosmos method-call translator provider.
src/EFCore.Cosmos/EFCore.Cosmos.csproj Suppresses EF9100 warning and annotates experimental precompiled-query warning.

Comment thread src/EFCore.Cosmos/EFCore.Cosmos.csproj Resolved
@ChrisJollyAU

Copy link
Copy Markdown
Contributor Author

@AndriySvyryd Fixed the minor issues (mostly formatting stuff) and all still pass

@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. I will merge this once main is open for v12 work

/// <param name="caseExpression">The <see cref="CaseExpression" /> for which to generate SQL.</param>
protected virtual Expression VisitCase(CaseExpression caseExpression)
{
//using (_sqlBuilder.Indent())

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.

Remove

@AndriySvyryd

Copy link
Copy Markdown
Member

@ChrisJollyAU Rebase on latest main please

@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 changed the title Add enum and comparison translators for cosmos Add enum and comparison translators for Cosmos Sep 11, 2026
@AndriySvyryd
AndriySvyryd merged commit fb8b604 into dotnet:main Sep 11, 2026
15 checks passed
AndriySvyryd pushed a commit that referenced this pull request Sep 25, 2026
@ChrisJollyAU
ChrisJollyAU deleted the enumtranslators branch October 24, 2026 06:42
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.

Cosmos: Translate Enum.HasFlag

4 participants