Skip to content

Commit 91f83ef

Browse files
committed
propogate error/warning
1 parent c7325c1 commit 91f83ef

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

enginetest/queries/queries.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4188,7 +4188,7 @@ SELECT * FROM cte WHERE d = 2;`,
41884188
Query: "select cast('2020-01-01 a' as datetime)",
41894189
ExpectedWarning: 1292,
41904190
ExpectedWarningsCount: 1,
4191-
Expected: []sql.Row{{"2020-01-01 00:00:00"}},
4191+
Expected: []sql.Row{{time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)}},
41924192
},
41934193
{},
41944194
{

sql/expression/convert.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
343343
}
344344
d, _, err := types.DatetimeMaxPrecision.Convert(ctx, val)
345345
if err != nil {
346-
return nil, err
346+
if !sql.ErrTruncatedIncorrect.Is(err) {
347+
return nil, err
348+
}
349+
ctx.Warn(mysql.ERTruncatedWrongValue, "%s", err.Error())
347350
}
348351
return d, nil
349352
case ConvertToDecimal:

sql/types/datetime.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ func (t datetimeType) Convert(ctx context.Context, v interface{}) (interface{},
188188
return nil, sql.InRange, nil
189189
}
190190
res, err := ConvertToTime(ctx, v, t)
191-
if err != nil {
191+
if err != nil && !sql.ErrTruncatedIncorrect.Is(err) {
192192
return nil, sql.OutOfRange, err
193193
}
194-
return res, sql.InRange, nil
194+
return res, sql.InRange, err
195195
}
196196

197197
// precisionConversion is a conversion ratio to divide time.Second by to truncate the appropriate amount for the
@@ -206,7 +206,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
206206
}
207207

208208
res, err := t.ConvertWithoutRangeCheck(ctx, v)
209-
if err != nil {
209+
if err != nil && !sql.ErrTruncatedIncorrect.Is(err) {
210210
return time.Time{}, err
211211
}
212212

@@ -227,7 +227,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
227227
if validated == nil {
228228
return time.Time{}, ErrConvertingToTimeOutOfRange.New(v, t)
229229
}
230-
return validated.(time.Time), nil
230+
return validated.(time.Time), err
231231
}
232232

233233
switch t.baseType {
@@ -245,7 +245,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
245245
}
246246
}
247247

248-
return res, nil
248+
return res, err
249249
}
250250

251251
// ConvertWithoutRangeCheck converts the parameter to time.Time without checking the range.
@@ -267,7 +267,7 @@ func (t datetimeType) ConvertWithoutRangeCheck(ctx context.Context, v interface{
267267
}
268268
// TODO: consider not using time.Parse if we want to match MySQL exactly ('2010-06-03 11:22.:.:.:.:' is a valid timestamp)
269269
var parsed bool
270-
res, parsed = parseDatetime(value)
270+
res, parsed, err = t.parseDatetime(value)
271271
if !parsed {
272272
return zeroTime, ErrConvertingToTime.New(v)
273273
}
@@ -361,20 +361,24 @@ func (t datetimeType) ConvertWithoutRangeCheck(ctx context.Context, v interface{
361361
res = res.Truncate(24 * time.Hour)
362362
}
363363

364-
return res, nil
364+
return res, err
365365
}
366366

367-
func parseDatetime(value string) (time.Time, bool) {
368-
end := len(value)
367+
func (t datetimeType) parseDatetime(value string) (time.Time, bool, error) {
368+
valueLen := len(value)
369+
end := valueLen
369370
for end > 0 {
370371
for _, layout := range TimestampDatetimeLayouts {
371372
if t, err := time.Parse(layout, value[0:end]); err == nil {
372-
return t.UTC(), true
373+
if end != valueLen {
374+
err = sql.ErrTruncatedIncorrect.New(t, value)
375+
}
376+
return t.UTC(), true, err
373377
}
374378
}
375379
end--
376380
}
377-
return time.Time{}, false
381+
return time.Time{}, false, nil
378382
}
379383

380384
// Equals implements the Type interface.

0 commit comments

Comments
 (0)