goccy / go-zetasqlite

A database driver library that interprets ZetaSQL queries and runs them using SQLite3
MIT License
57 stars 30 forks source link

`IF()` statements are not rewritten to use proper control flow #215

Closed ohaibbq closed 5 months ago

ohaibbq commented 5 months ago

There's currently special handling for case statements such that expressions for a case are executed only when their condition is matched.

In contrast to this behavior, the IF() function will execute its case expressions prior to evaluating the input condition.

This test case shows the failing behavior:

{
    name:         "if with case that causes errors",
    query:        `SELECT IF(FALSE, SPLIT(CAST(NULL AS STRING), " ")[OFFSET(0)], "false")`,
    expectedRows: [][]interface{}{{"false"}},
},

returns

=== RUN   TestQuery/if_with_case_that_causes_errors
    query_test.go:5948: OFFSET(0) is out of range

Passing CASE test:

{
    name:         "case with case that causes errors",
    query:        `SELECT CASE WHEN FALSE THEN SPLIT(CAST(NULL AS STRING), " ")[OFFSET(0)] ELSE "false" END`,
    expectedRows: [][]interface{}{{"false"}},
},