Skip to content

Implement CASE flattening optimizations#34175

Merged
maumar merged 9 commits into
dotnet:mainfrom
ranma42:factory-case
Jul 12, 2024
Merged

Implement CASE flattening optimizations#34175
maumar merged 9 commits into
dotnet:mainfrom
ranma42:factory-case

Conversation

@ranma42

@ranma42 ranma42 commented Jul 6, 2024

Copy link
Copy Markdown
Contributor

It is possible to perform most of the CASE optimizations at construction time.

This improves the SQL emitted by SqlServer and makes it easy to implement the optimizations proposed in #18774.

Fixes #18774.
Fixes #18935.

@ranma42

ranma42 commented Jul 6, 2024

Copy link
Copy Markdown
Contributor Author

I'll mark it as ready after adding some tests for the new transformations (they already affect some existing tests, but having some dedicated ones is probably better).

@ranma42
ranma42 marked this pull request as ready for review July 6, 2024 16:36
@ranma42
ranma42 marked this pull request as draft July 6, 2024 17:13
@ranma42
ranma42 marked this pull request as ready for review July 6, 2024 19:10
@ranma42
ranma42 force-pushed the factory-case branch 2 times, most recently from 62b6472 to d838062 Compare July 7, 2024 11:32
Comment thread src/Shared/EnumerableExtensions.cs
{
var test = caseWhenClause.Test;

if (operand == null && test is CaseExpression { Operand: null, WhenClauses: [var clause] } testExpr)

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.

Suggested change
if (operand == null && test is CaseExpression { Operand: null, WhenClauses: [var clause] } testExpr)
if (operand == null && test is CaseExpression { Operand: null, WhenClauses: [var nestedSingleClause] } testExpr)

Comment thread src/Shared/EnumerableExtensions.cs
=> expr is SqlConstantExpression { Value: false or null };

bool IsTrue(SqlExpression expr)
=> expr is SqlConstantExpression { Value: true };

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.

Honestly, with pattern matching, the syntax is simple enough that maybe we should just inline, rather than have a helper method like this…

@maumar
maumar merged commit bc170c7 into dotnet:main Jul 12, 2024
@maumar

maumar commented Jul 12, 2024

Copy link
Copy Markdown
Contributor

nice improvement, thanks!

@ranma42
ranma42 deleted the factory-case branch July 13, 2024 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query: optimize CASE statement - remove conditions that are never hit Query: optimize nested CASE blocks

3 participants