Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Respect precision when casting to datetime
  • Loading branch information
angelamayxie committed Oct 22, 2025
commit 4244a2a881f0f1e1a3f7178efe37393e4541788f
12 changes: 0 additions & 12 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -4246,48 +4246,36 @@ SELECT * FROM cte WHERE d = 2;`,
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 0, time.UTC)}},
},
{
// TODO: implement precision for datetime casting
Skip: true,
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(1));",
ExpectedWarning: 1292,
ExpectedWarningsCount: 1,
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 100000000, time.UTC)}},
},
{
// TODO: implement precision for datetime casting
Skip: true,
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(2));",
ExpectedWarning: 1292,
ExpectedWarningsCount: 1,
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 120000000, time.UTC)}},
},
{
// TODO: implement precision for datetime casting
Skip: true,
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(3));",
ExpectedWarning: 1292,
ExpectedWarningsCount: 1,
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 123000000, time.UTC)}},
},
{
// TODO: implement precision for datetime casting
Skip: true,
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(4));",
ExpectedWarning: 1292,
ExpectedWarningsCount: 1,
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 123500000, time.UTC)}},
},
{
// TODO: implement precision for datetime casting
Skip: true,
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(5));",
ExpectedWarning: 1292,
ExpectedWarningsCount: 1,
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 123460000, time.UTC)}},
},
{
// TODO: implement precision for datetime casting
Skip: true,
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(6));",
ExpectedWarning: 1292,
ExpectedWarningsCount: 1,
Expand Down
4 changes: 3 additions & 1 deletion sql/expression/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,13 @@ func (c *comparison) castLeftAndRight(ctx *sql.Context, left, right interface{})
}

if types.IsTime(leftType) || types.IsTime(rightType) {
// TODO: We need to set to actual Datetime type
l, r, err := convertLeftAndRight(ctx, left, right, ConvertToDatetime)
if err != nil {
return nil, nil, nil, err
}

return l, r, types.DatetimeMaxPrecision, nil
return l, r, types.DatetimeDefaultPrecision, nil
}

// Rely on types.JSON.Compare to handle JSON comparisons
Expand Down Expand Up @@ -302,6 +303,7 @@ func (c *comparison) castLeftAndRight(ctx *sql.Context, left, right interface{})
}

func convertLeftAndRight(ctx *sql.Context, left, right interface{}, convertTo string) (interface{}, interface{}, error) {
// TODO: typeLength and typeScale should be the actual length and scale of the type to be converted
l, err := convertValue(ctx, left, convertTo, nil, 0, 0)
if err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion sql/expression/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
if !(isTime || isString || isBinary) {
return nil, nil
}
d, _, err := types.DatetimeDefaultPrecision.Convert(ctx, val)
d, _, err := types.MustCreateDatetimeType(sqltypes.Datetime, typeLength).Convert(ctx, val)
if err != nil {
if !sql.ErrTruncatedIncorrect.Is(err) {
return nil, err
Expand Down