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

Implement unary minus operator #206

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions internal/function_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"errors"
"fmt"
"math"
"sync"

"github.com/goccy/go-json"
Expand Down Expand Up @@ -184,6 +185,23 @@ func bindOpDiv(args ...Value) (Value, error) {
return OP_DIV(args[0], args[1])
}

func bindUnaryMinus(args ...Value) (Value, error) {
if existsNull(args) {
return nil, nil
}
v, ok := args[0].(IntValue)
if ok {
i, err := v.ToInt64()
if err != nil {
return nil, err
}
if i == math.MinInt64 {
return nil, fmt.Errorf("int64 overflow: -%d", i)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message follows the error message format used by BigQuery:

 cat <<QUERY | bq query --nouse_legacy_sql --project_id myProject
with x as (select -9223372036854775808 as v union all select null) select -v from x
QUERY
Error in query string: Error processing job 'myProject:bqjob_r79f8300c97a6ae52_0000018ec5362324_1': int64 overflow: --9223372036854775808

}
}
return SAFE_NEGATE(args[0])
}

func bindEqual(args ...Value) (Value, error) {
if existsNull(args) {
return nil, nil
Expand Down
1 change: 1 addition & 0 deletions internal/function_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var normalFuncs = []*FuncInfo{
{Name: "subtract", BindFunc: bindSub},
{Name: "multiply", BindFunc: bindMul},
{Name: "divide", BindFunc: bindOpDiv},
{Name: "unary_minus", BindFunc: bindUnaryMinus},
{Name: "equal", BindFunc: bindEqual},
{Name: "not_equal", BindFunc: bindNotEqual},
{Name: "greater", BindFunc: bindGreater},
Expand Down
18 changes: 17 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,26 @@ UNION ALL
expectedRows: [][]interface{}{{int64(1)}},
},
{
name: "unary minus operator",
name: "unary minus operator with literal",
query: "SELECT -2",
expectedRows: [][]interface{}{{int64(-2)}},
},
{
name: "unary minus operator with column",
query: `WITH x AS (
SELECT 2 AS i, 2.0 AS f
UNION ALL
SELECT NULL, -9223372036854775808
) SELECT -i, -f FROM x`,
expectedRows: [][]interface{}{
{int64(-2), float64(-2.0)},
{nil, float64(9223372036854775808)}},
},
{
name: "unary minus operator overflow",
query: "WITH x AS (SELECT -9223372036854775807-1 as v) SELECT -v FROM x",
expectedErr: "int64 overflow: --9223372036854775808",
},
{
name: "bit not operator",
query: "SELECT ~1",
Expand Down
Loading