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] Do not cast integer/float-like strings to datetime #30

Merged
merged 1 commit into from
Feb 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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