From 2c2416b15d9b3cf65bad07e6fc37e2d267e61587 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Sat, 9 Mar 2024 07:03:01 -0800 Subject: [PATCH] [String] Cast to INT64 should use base-10 parsing (#159) * [String] Cast to INT64 should use base-10 parsing * refix 0x * invert --- internal/value.go | 7 ++++++- query_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/value.go b/internal/value.go index fd9d288..69fb72f 100644 --- a/internal/value.go +++ b/internal/value.go @@ -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) { diff --git a/query_test.go b/query_test.go index d0bdf18..cd92aaf 100644 --- a/query_test.go +++ b/query_test.go @@ -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 {