Skip to content

Commit 8a1d37e

Browse files
committed
explicitly cast to max precision for comparisons
1 parent c5d4cc2 commit 8a1d37e

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

sql/expression/comparison.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,7 @@ func (c *comparison) castLeftAndRight(ctx *sql.Context, left, right interface{})
222222
}
223223

224224
if types.IsTime(leftType) || types.IsTime(rightType) {
225-
l, _, err := types.DatetimeMaxPrecision.Convert(ctx, left)
226-
if err != nil {
227-
return nil, nil, nil, err
228-
}
229-
r, _, err := types.DatetimeMaxPrecision.Convert(ctx, right)
225+
l, r, err := convertLeftAndRight(ctx, left, right, ConvertToDatetime)
230226
if err != nil {
231227
return nil, nil, nil, err
232228
}
@@ -305,13 +301,16 @@ func (c *comparison) castLeftAndRight(ctx *sql.Context, left, right interface{})
305301
}
306302

307303
func convertLeftAndRight(ctx *sql.Context, left, right interface{}, convertTo string) (interface{}, interface{}, error) {
308-
// TODO: typeLength and typeScale should be the actual length and scale of the type to be converted
309-
l, err := convertValue(ctx, left, convertTo, nil, 0, 0)
304+
typeLength := 0
305+
if convertTo == ConvertToDatetime {
306+
typeLength = types.MaxDatetimePrecision
307+
}
308+
l, err := convertValue(ctx, left, convertTo, nil, typeLength, 0)
310309
if err != nil {
311310
return nil, nil, err
312311
}
313312

314-
r, err := convertValue(ctx, right, convertTo, nil, 0, 0)
313+
r, err := convertValue(ctx, right, convertTo, nil, typeLength, 0)
315314
if err != nil {
316315
return nil, nil, err
317316
}

sql/types/datetime.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const ZeroTimestampDatetimeStr = "0000-00-00 00:00:00"
3636

3737
const MinDatetimeStringLength = 8 // length of "2000-1-1"
3838

39+
const MaxDatetimePrecision = 6
40+
3941
var (
4042
// ErrConvertingToTime is thrown when a value cannot be converted to a Time
4143
ErrConvertingToTime = errors.NewKind("Incorrect datetime value: '%v'")
@@ -105,13 +107,13 @@ var (
105107
// DatetimeDefaultPrecision is a date and a time without a specified precision
106108
DatetimeDefaultPrecision = MustCreateDatetimeType(sqltypes.Datetime, 0)
107109
// DatetimeMaxPrecision is a date and a time with maximum precision
108-
DatetimeMaxPrecision = MustCreateDatetimeType(sqltypes.Datetime, 6)
110+
DatetimeMaxPrecision = MustCreateDatetimeType(sqltypes.Datetime, MaxDatetimePrecision)
109111
// Timestamp is a UNIX timestamp with default precision (no fractional seconds).
110112
Timestamp = MustCreateDatetimeType(sqltypes.Timestamp, 0)
111113
// TimestampMaxPrecision is a UNIX timestamp with maximum precision
112-
TimestampMaxPrecision = MustCreateDatetimeType(sqltypes.Timestamp, 6)
114+
TimestampMaxPrecision = MustCreateDatetimeType(sqltypes.Timestamp, MaxDatetimePrecision)
113115
// DatetimeMaxRange is a date and a time with maximum precision and maximum range.
114-
DatetimeMaxRange = MustCreateDatetimeType(sqltypes.Datetime, 6)
116+
DatetimeMaxRange = MustCreateDatetimeType(sqltypes.Datetime, MaxDatetimePrecision)
115117

116118
datetimeValueType = reflect.TypeOf(time.Time{})
117119
)
@@ -128,7 +130,7 @@ var _ sql.CollationCoercible = datetimeType{}
128130
func CreateDatetimeType(baseType query.Type, precision int) (sql.DatetimeType, error) {
129131
switch baseType {
130132
case sqltypes.Date, sqltypes.Datetime, sqltypes.Timestamp:
131-
if precision < 0 || precision > 6 {
133+
if precision < 0 || precision > MaxDatetimePrecision {
132134
return nil, fmt.Errorf("precision must be between 0 and 6, got %d", precision)
133135
}
134136
return datetimeType{
@@ -221,7 +223,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
221223
}
222224

223225
// Round the date to the precision of this type
224-
if t.precision < 6 {
226+
if t.precision < MaxDatetimePrecision {
225227
truncationDuration := time.Second / time.Duration(precisionConversion[t.precision])
226228
res = res.Round(truncationDuration)
227229
} else {

0 commit comments

Comments
 (0)