Skip to content

Commit 4244a2a

Browse files
committed
Respect precision when casting to datetime
1 parent b9d804f commit 4244a2a

File tree

3 files changed

+4
-14
lines changed

3 files changed

+4
-14
lines changed

enginetest/queries/queries.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,48 +4246,36 @@ SELECT * FROM cte WHERE d = 2;`,
42464246
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 0, time.UTC)}},
42474247
},
42484248
{
4249-
// TODO: implement precision for datetime casting
4250-
Skip: true,
42514249
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(1));",
42524250
ExpectedWarning: 1292,
42534251
ExpectedWarningsCount: 1,
42544252
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 100000000, time.UTC)}},
42554253
},
42564254
{
4257-
// TODO: implement precision for datetime casting
4258-
Skip: true,
42594255
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(2));",
42604256
ExpectedWarning: 1292,
42614257
ExpectedWarningsCount: 1,
42624258
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 120000000, time.UTC)}},
42634259
},
42644260
{
4265-
// TODO: implement precision for datetime casting
4266-
Skip: true,
42674261
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(3));",
42684262
ExpectedWarning: 1292,
42694263
ExpectedWarningsCount: 1,
42704264
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 123000000, time.UTC)}},
42714265
},
42724266
{
4273-
// TODO: implement precision for datetime casting
4274-
Skip: true,
42754267
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(4));",
42764268
ExpectedWarning: 1292,
42774269
ExpectedWarningsCount: 1,
42784270
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 123500000, time.UTC)}},
42794271
},
42804272
{
4281-
// TODO: implement precision for datetime casting
4282-
Skip: true,
42834273
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(5));",
42844274
ExpectedWarning: 1292,
42854275
ExpectedWarningsCount: 1,
42864276
Expected: []sql.Row{{time.Date(2020, time.January, 1, 12, 34, 56, 123460000, time.UTC)}},
42874277
},
42884278
{
4289-
// TODO: implement precision for datetime casting
4290-
Skip: true,
42914279
Query: "select cast('2020-01-01 12:34:56.123456abc' as datetime(6));",
42924280
ExpectedWarning: 1292,
42934281
ExpectedWarningsCount: 1,

sql/expression/comparison.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,13 @@ func (c *comparison) castLeftAndRight(ctx *sql.Context, left, right interface{})
222222
}
223223

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

230-
return l, r, types.DatetimeMaxPrecision, nil
231+
return l, r, types.DatetimeDefaultPrecision, nil
231232
}
232233

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

304305
func convertLeftAndRight(ctx *sql.Context, left, right interface{}, convertTo string) (interface{}, interface{}, error) {
306+
// TODO: typeLength and typeScale should be the actual length and scale of the type to be converted
305307
l, err := convertValue(ctx, left, convertTo, nil, 0, 0)
306308
if err != nil {
307309
return nil, nil, err

sql/expression/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
341341
if !(isTime || isString || isBinary) {
342342
return nil, nil
343343
}
344-
d, _, err := types.DatetimeDefaultPrecision.Convert(ctx, val)
344+
d, _, err := types.MustCreateDatetimeType(sqltypes.Datetime, typeLength).Convert(ctx, val)
345345
if err != nil {
346346
if !sql.ErrTruncatedIncorrect.Is(err) {
347347
return nil, err

0 commit comments

Comments
 (0)