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

Fix control flow execution of conditional functions IF(), IFNULL() #216

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -479,6 +479,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
Loading