Skip to content

Commit

Permalink
Fix control flow execution of conditional functions IF(), IFNULL() (
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaibbq authored Jun 24, 2024
1 parent a81591f commit 76b0d28
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ func (n *FunctionCallNode) FormatSQL(ctx context.Context) (string, error) {
return "", err
}
switch funcName {
case "zetasqlite_ifnull":
return fmt.Sprintf(
"CASE WHEN %s IS NULL THEN %s ELSE %s END",
args[0],
args[1],
args[0],
), nil
case "zetasqlite_if":
return fmt.Sprintf(
"CASE WHEN %s THEN %s ELSE %s END",
args[0],
args[1],
args[2],
), nil
case "zetasqlite_case_no_value":
var whenStmts []string
for i := 0; i < len(args)-1; i += 2 {
Expand Down
15 changes: 15 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,21 @@ FROM UNNEST([1, 2, 3, 4]) AS val`,
query: `SELECT IF("a" = "a", "true", "false")`,
expectedRows: [][]interface{}{{"true"}},
},
{
name: "if with case that causes errors",
query: `SELECT IF(FALSE, ERROR("error case!"), "false")`,
expectedRows: [][]interface{}{{"false"}},
},
{
name: "ifnull with case that causes errors",
query: `SELECT IFNULL("STRING", ERROR("error case!"))`,
expectedRows: [][]interface{}{{"STRING"}},
},
{
name: "case with case that causes errors",
query: `SELECT CASE WHEN FALSE THEN ERROR("error case!") ELSE "false" END`,
expectedRows: [][]interface{}{{"false"}},
},
{
name: "ifnull",
query: `SELECT IFNULL(10, 0)`,
Expand Down

0 comments on commit 76b0d28

Please sign in to comment.