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

Support BigQuery CASE expr flavor #382

Closed
shay-rf opened this issue Jan 28, 2022 · 3 comments
Closed

Support BigQuery CASE expr flavor #382

shay-rf opened this issue Jan 28, 2022 · 3 comments
Assignees

Comments

@shay-rf
Copy link

shay-rf commented Jan 28, 2022

BigQuery supports two flavors of CASE.
The regular is analogous to Clojure's 'cond' - where we provide pairs of condition and expression, with the optional default expression at the end.

The second flavor is similar to Clojure's 'case' - where we provide a single expression and then pairs of value and expression, with the optional default expression at the end.
https://cloud.google.com/bigquery/docs/reference/standard-sql/conditional_expressions#case_expr

So, one can provide something that computes a value once and then based on the result value provides the target value ( or default ).

I am wondering how could this be supported? For example something like:
[:case-val my-expression
1 "foo"
2 "bar"
"baz" ]

Is it something that better be added internally, or should a user registered op be constructed ?

@seancorfield
Copy link
Owner

I'm happy to add support for this. :case is currently "special syntax" so I'll make that into a reusable function and add :case-expr (which I think is more expressive and more accurate than :case-val since it accepts an expression).

@seancorfield
Copy link
Owner

seancorfield commented Jan 30, 2022

How does this look @shay-rf ?

(deftest test-case-expr
  (is (= ["SELECT CASE foo WHEN ? THEN ? WHEN ? THEN foo / ? ELSE ? END FROM bar"
          1 -1 2 2 0]
         (sut/format
          {:select [[[:case-expr :foo
                      1 -1
                      2 [:/ :foo 2]
                      :else 0]]]
           :from [:bar]}))))

@shay-rf
Copy link
Author

shay-rf commented Jan 30, 2022

Works perfectly :)
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants