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

Conversation

totem3
Copy link
Sponsor Collaborator

@totem3 totem3 commented Apr 10, 2024

This Pull Request closes goccy/bigquery-emulator#217

I added a unary minus operator because it was not implemented before.
As tested here, It seems to work fine with number literals, but using - on columns causes an error as described in the linked issue.

In this PR, I implemented the unary minus operator. It is basically the same as the SAFE_NEGATE function, as shown in the link below. What different is, an exception occurs when there's an overflow.
https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#safe_negate

out of scope

This unary minus operator implementation uses SAFE_NEGATE inside, but do not change SAFE_NEGATE behaviour. Currently, SAFE_NEGATE has some incompatibilities. For example,

  • Given math.MinInt64, it returns an unexpected value on ubuntu. (expected 9223372036854775807 but returns -9223372036854775808)
  • Given NaN, NaN is expected but it returns nil
  • Given a large BigNumeric, it returns an unexpected value.

return nil, err
}
if i <= math.MinInt64 {
return nil, fmt.Errorf("int64 overflow: -%d", i)
Copy link
Sponsor 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

@totem3 totem3 marked this pull request as draft April 10, 2024 01:52
@totem3 totem3 marked this pull request as ready for review April 10, 2024 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BigQueryException: no such function: zetasqlite_unary_minus"
1 participant