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

Query: optimize nested CASE blocks #18774

Closed
maumar opened this issue Nov 6, 2019 · 3 comments · Fixed by #34175
Closed

Query: optimize nested CASE blocks #18774

maumar opened this issue Nov 6, 2019 · 3 comments · Fixed by #34175
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Nov 6, 2019

Some nested CASE blocks, e.g.:

CASE 
    WHEN (condition1)
    THEN 
    CASE
        WHEN (condition2)
        THEN result1
        ELSE result2 
    END
    ELSE result2
END

can be optimized to:

CASE
    WHEN condition1 AND condition2
    THEN result1
    ELSE result2
END

In practice we get those kinds of chained CASE blocks in the following scenarios:

context.Set<Product>().OrderBy(p => p.UnitsInStock > 10 ? p.ProductID > 40 : p.ProductID <= 40)
@ajcvickers ajcvickers added this to the Backlog milestone Nov 8, 2019
@maumar maumar self-assigned this Nov 8, 2019
@smitpatel
Copy link
Member

Duplicate of #2881

@smitpatel smitpatel marked this as a duplicate of #2881 Nov 12, 2019
@maumar
Copy link
Contributor Author

maumar commented Nov 13, 2019

this is somewhat different scenario, where nested block happens in the THEN part, not the ELSE part

@ranma42
Copy link
Contributor

ranma42 commented Jul 6, 2024

There are 3 ways in which CASE statements could be nested, each with a slightly different optimization:

ELSE

CASE 
    WHEN t1 THEN r1
    ELSE CASE
        WHEN t2 THEN r2
        ELSE r3
    END
END

is already being optimized into

CASE 
    WHEN t1 THEN r1
    WHEN t2 THEN r2
    ELSE r3
END

The optimization could be extended to also allow flattening of ELSE CASEs with the same operand as the outer CASE (as long as side-effects are not possible/considered).

THEN

CASE 
    WHEN t1 THEN CASE
        WHEN t2 THEN r1
        ELSE r2
    END
    ELSE r2
END

can be simplified to

CASE 
    WHEN t1 AND t2 THEN r1
    ELSE r2
END

(the main target of this issue). This requires matching r2 results and can only be applied (in a trivial way) to the last clause.

CASE

CASE 
    WHEN CASE
        WHEN t1 THEN TRUE -- b1
        ELSE FALSE -- b2
    END THEN r1
    ELSE r2
END

can be simplified to

CASE 
    WHEN t1 THEN r1
    ELSE r2
END

This also works when b2 is NULL; it can easily be extended to also work when b1 is NULL or FALSE and b2 is TRUE by negating t1 in the result.

@maumar maumar modified the milestones: Backlog, 9.0.0-preview7 Jul 12, 2024
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 12, 2024
@roji roji modified the milestones: 9.0.0-preview7, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants