Skip to content

[FEATURE] Add support for CASE statement in OpenSearch PPL #3523

@RyanL1997

Description

@RyanL1997

Description

Currently, there is no native support for SQL-style CASE expressions in eval. This limitation makes it difficult to implement multi-branch conditional logic in a clear and maintainable way. Users are required to nest multiple IF() functions, which reduces readability and increases the complexity of query construction and maintenance.

Goal

Add support for standard CASE WHEN ... THEN ... ELSE ... END syntax in PPL, particularly within eval, to allow users to define conditional logic more intuitively and clearly. This functionality would align PPL with widely adopted SQL syntax and improve query clarity in scenarios requiring multi-condition branching.

Workarounds and alternatives

Nested IF() expressions have been used to replicate CASE logic. However, this approach becomes cumbersome and error-prone as the number of conditions increases, and lacks the structural clarity provided by CASE statements.

Reference

  • Example by using IF (as the work around for now):
| eval grade = if(score >= 95, "A+",
             if(score >= 90, "A",
               if(score >= 85, "B+",
                 if(score >= 80, "B",
                   if(score >= 75, "C+",
                     if(score >= 70, "C",
                       if(score >= 65, "D+",
                         if(score >= 60, "D", "F"))))))))
# As more conditions are added, readability degrades significantly
  • Example by using CASE:
| eval grade = case(
    when score >= 95 then "A+",
    when score >= 90 then "A",
    when score >= 85 then "B+",
    when score >= 80 then "B",
    when score >= 75 then "C+",
    when score >= 70 then "C",
    when score >= 65 then "D+",
    when score >= 60 then "D",
    else "F"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    PPLPiped processing languageenhancementNew feature or request

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions