Skip to content

Commit

Permalink
[String] Do not cast integer/float-like strings to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaibbq committed Feb 27, 2024
1 parent 961ce06 commit 635786c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
9 changes: 0 additions & 9 deletions internal/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,6 @@ func (sv StringValue) ToTime() (time.Time, error) {
case isTimestamp(raw):
return parseTimestamp(raw, time.UTC)
}
if f, err := strconv.ParseFloat(raw, 64); err == nil {
return TimestampFromFloatValue(f)
}
if i64, err := strconv.ParseInt(raw, 10, 64); err == nil {
if i64 > time.Unix(0, 0).Unix()*int64(time.Millisecond) {
return TimestampFromInt64Value(i64)
}
return DateFromInt64Value(i64)
}
return time.Time{}, fmt.Errorf("failed to convert %s to time.Time type", sv)
}

Expand Down
6 changes: 6 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2803,6 +2803,12 @@ SELECT item FROM Produce WHERE Produce.category = 'vegetable' QUALIFY RANK() OVE
query: `SELECT CAST("apple" AS INT64) AS not_a_number`,
expectedErr: `failed to analyze: INVALID_ARGUMENT: Could not cast literal "apple" to type INT64 [at 1:13]`,
},
// Regression test for goccy/go-zetasqlite#175
{
name: "cast integer to datetime",
query: `WITH toks AS (SELECT "20100317" AS dt) SELECT CAST(dt AS DATETIME) FROM toks;`,
expectedErr: "failed to convert 20100317 to time.Time type",
},
{
name: "safe cast",
query: `SELECT SAFE_CAST(x AS STRING) FROM UNNEST([1, 2, 3]) AS x`,
Expand Down

0 comments on commit 635786c

Please sign in to comment.