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

[String] Cast to INT64 should use base-10 parsing #159

Merged
merged 3 commits into from
Mar 9, 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
7 changes: 6 additions & 1 deletion internal/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,12 @@ func (sv StringValue) ToInt64() (int64, error) {
if sv == "" {
return 0, nil
}
return strconv.ParseInt(string(sv), 0, 64)
toParse := string(sv)
base := 10
if strings.Contains(strings.ToLower(toParse), "0x") {
base = 0
}
return strconv.ParseInt(toParse, base, 64)
}

func (sv StringValue) ToString() (string, error) {
Expand Down
12 changes: 12 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,18 @@ SELECT item FROM Produce WHERE Produce.category = 'vegetable' QUALIFY RANK() OVE
query: `SELECT CAST('0x87a' as INT64), CAST(CONCAT('0x', '87a') as INT64), CAST(SUBSTR('q0x87a', 2) as INT64), CAST(s AS INT64) FROM (SELECT CONCAT('0x', '87a') AS s)`,
expectedRows: [][]interface{}{{int64(2170), int64(2170), int64(2170), int64(2170)}},
},
{
name: "cast string to int64 - leading zeros",
query: `WITH toks AS (
SELECT "000800" AS x
UNION ALL SELECT "-0900"
UNION ALL SELECT "+000100"
UNION ALL SELECT "0"
UNION ALL SELECT "0000"
)
SELECT ARRAY_AGG(CAST(x AS INT64)) FROM toks`,
expectedRows: [][]interface{}{{[]any{int64(800), int64(-900), int64(100), int64(0), int64(0)}}},
},

// hash functions
{
Expand Down
Loading