Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CASE flattening optimizations #34175

Merged
merged 9 commits into from
Jul 12, 2024
Merged

Conversation

ranma42
Copy link
Contributor

@ranma42 ranma42 commented Jul 6, 2024

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
Copy link
Contributor Author

ranma42 commented Jul 6, 2024

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
@@ -714,13 +718,117 @@ public virtual SqlExpression Case(SqlExpression? operand, IReadOnlyList<CaseWhen
var typeMappedWhenClauses = new List<CaseWhenClause>();
foreach (var caseWhenClause in whenClauses)
{
var test = caseWhenClause.Test;

if (operand == null && test is CaseExpression { Operand: null, WhenClauses: [var clause] } testExpr)
Copy link
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)

src/Shared/EnumerableExtensions.cs Show resolved Hide resolved
=> expr is SqlConstantExpression { Value: false or null };

bool IsTrue(SqlExpression expr)
=> expr is SqlConstantExpression { Value: true };
Copy link
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
7 checks passed
@maumar
Copy link
Contributor

maumar commented Jul 12, 2024

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