Skip to content

Query: optimize CASE statement - remove conditions that are never hit #18935

Description

@maumar
CASE
        WHEN CAST(0 AS bit) THEN x
        ELSE y
    END

we can simply remove the WHEN block since it's condition is never met so the result would be just y

If all the WHEN blocks have been removed and there is no ELSE condition we can return NULL

Activity

  1. added this to the Backlog milestone on Nov 18, 2019
  2. smitpatel commented on Mar 16, 2020

    @smitpatel
    Contributor

    @maumar - Is this already done?

  3. ranma42 commented on Jun 23, 2024

    @ranma42
    Contributor

    Yes, this should already be optimized by

    else if (IsFalse(testCondition))
    {
    // if test evaluates to 'false' we can remove the WhenClause
    RestoreNonNullableColumnsList(currentNonNullableColumnsCount);
    RestoreNullValueColumnsList(currentNullValueColumnsCount);
    continue;
    }

    EDIT: clauses whose test is NULL instead of FALSE are currently left in; I'm working on it.

  4. modified the milestones: Backlog, on Jul 12, 2024
  5. modified the milestones: , 9.0.0 on Oct 12, 2024
  6. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions