Skip to content

Commit

Permalink
[String] Cast to INT64 should use base-10 parsing (#159)
Browse files Browse the repository at this point in the history
* [String] Cast to INT64 should use base-10 parsing

* refix 0x

* invert
  • Loading branch information
ohaibbq authored Mar 9, 2024
1 parent 5416a73 commit 2c2416b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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 @@ -2894,6 +2894,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

0 comments on commit 2c2416b

Please sign in to comment.