apache / pinot

Apache Pinot - A realtime distributed OLAP datastore
https://pinot.apache.org/
Apache License 2.0
5.27k stars 1.23k forks source link

Case statement doesn't result in short circuiting #8747

Closed cliandy closed 2 years ago

cliandy commented 2 years ago

Currently CASE statements don't follow SQL conventions to short circuit at the first match. In the documentation, we have a given example:

SELECT
    CASE
      WHEN price > 30 THEN 3
      WHEN price > 20 THEN 2
      WHEN price > 10 THEN 1
      ELSE 0
    END AS price_category
FROM myTable

This results in the last met WHEN condition taking precedence instead of the first. In the example above, all values with price > 10 will evaluate to 1.

Curious if this is intentional for any reason as the code below explicitly takes the max condition index. Otherwise, happy to make this change.

https://github.com/apache/pinot/blob/5cd6a7dc05135a82f8dc292efcce682c8d377338/pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/CaseTransformFunction.java#L226-L232

richardstartin commented 2 years ago

Hi @cliandy thanks for reporting this bug, this is unintentional. I have added a fix and regression test in #8748 8748

cliandy commented 2 years ago

Thanks for the prompt fix @richardstartin.