Skip to content

Commit 1f43ee8

Browse files
committed
cleanup extra funcs
1 parent a0b7544 commit 1f43ee8

File tree

5 files changed

+16
-45
lines changed

5 files changed

+16
-45
lines changed

sql/expression/function/time_math.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (d *DateAdd) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
232232
}
233233

234234
var dateVal interface{}
235-
dateVal, _, err = types.DatetimeMaxLimit.Convert(ctx, date)
235+
dateVal, _, err = types.DatetimeMaxRange.Convert(ctx, date)
236236
if err != nil {
237237
ctx.Warn(1292, err.Error())
238238
return nil, nil
@@ -380,7 +380,7 @@ func (d *DateSub) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
380380
}
381381

382382
var dateVal interface{}
383-
dateVal, _, err = types.DatetimeMaxPrecision.Convert(ctx, date)
383+
dateVal, _, err = types.DatetimeMaxRange.Convert(ctx, date)
384384
if err != nil {
385385
ctx.Warn(1292, err.Error())
386386
return nil, nil

sql/expression/interval.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,24 +238,23 @@ const (
238238
)
239239

240240
// isLeapYear determines if a given year is a leap year
241-
// Uses Go's built-in date handling for accuracy
242241
func isLeapYear(year int) bool {
243242
return daysInMonth(year, time.February) == 29
244243
}
245244

246245
// daysInMonth returns the number of days in a given month/year combination
247-
// Uses Go's built-in date handling: day 0 of next month = last day of current month
248246
func daysInMonth(year int, month time.Month) int {
249247
return time.Date(year, month+1, 0, 0, 0, 0, 0, time.UTC).Day()
250248
}
251249

250+
// apply applies the time delta to the given time, using the specified sign
252251
func (td TimeDelta) apply(t time.Time, sign int64) time.Time {
253252
if td.Years != 0 {
254253
targetYear := t.Year() + int(td.Years*sign)
255254

256-
// Special handling for Feb 29 on leap years
255+
// special handling for Feb 29 on leap years
257256
if t.Month() == time.February && t.Day() == 29 && !isLeapYear(targetYear) {
258-
// If we're on Feb 29 and target year is not a leap year,
257+
// if we're on Feb 29 and target year is not a leap year,
259258
// move to Feb 28
260259
t = time.Date(targetYear, time.February, 28,
261260
t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
@@ -266,17 +265,17 @@ func (td TimeDelta) apply(t time.Time, sign int64) time.Time {
266265
}
267266

268267
if td.Months != 0 {
269-
totalMonths := int(t.Month()) - 1 + int(td.Months*sign) // Convert to 0-based
268+
totalMonths := int(t.Month()) - 1 + int(td.Months*sign) // convert to 0-based
270269

271-
// Calculate target year and month
270+
// calculate target year and month
272271
yearOffset := totalMonths / 12
273272
if totalMonths < 0 {
274-
yearOffset = (totalMonths - 11) / 12 // Handle negative division correctly
273+
yearOffset = (totalMonths - 11) / 12 // handle negative division correctly
275274
}
276275
targetYear := t.Year() + yearOffset
277-
targetMonth := time.Month((totalMonths%12+12)%12 + 1) // Ensure positive month
276+
targetMonth := time.Month((totalMonths%12+12)%12 + 1) // ensure positive month
278277

279-
// Handle end-of-month edge cases
278+
// handle end-of-month edge cases
280279
originalDay := t.Day()
281280
maxDaysInTargetMonth := daysInMonth(targetYear, targetMonth)
282281

sql/rowexec/insert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ func getFieldIndexFromUpdateExpr(updateExpr sql.Expression) (int, bool) {
261261

262262
// resolveValues resolves all VALUES functions.
263263
func (i *insertIter) resolveValues(ctx *sql.Context, insertRow sql.Row) error {
264+
// if vals empty then no need to resolve
264265
for _, updateExpr := range i.updateExprs {
265266
var err error
266267
sql.Inspect(updateExpr, func(expr sql.Expression) bool {

sql/types/datetime.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package types
1717
import (
1818
"context"
1919
"fmt"
20-
"math"
2120
"reflect"
2221
"time"
2322

@@ -104,8 +103,8 @@ var (
104103
Timestamp = MustCreateDatetimeType(sqltypes.Timestamp, 0)
105104
// TimestampMaxPrecision is a UNIX timestamp with maximum precision
106105
TimestampMaxPrecision = MustCreateDatetimeType(sqltypes.Timestamp, 6)
107-
// DatetimeMaxLimit is a date and a time with maximum precision and maximum range.
108-
DatetimeMaxLimit = MustCreateDatetimeType(sqltypes.Datetime, 6)
106+
// DatetimeMaxRange is a date and a time with maximum precision and maximum range.
107+
DatetimeMaxRange = MustCreateDatetimeType(sqltypes.Datetime, 6)
109108

110109
datetimeValueType = reflect.TypeOf(time.Time{})
111110
)
@@ -222,7 +221,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
222221
res = res.Round(time.Microsecond)
223222
}
224223

225-
if t == DatetimeMaxLimit {
224+
if t == DatetimeMaxRange {
226225
validated := ValidateTime(res)
227226
if validated == nil {
228227
return time.Time{}, ErrConvertingToTimeOutOfRange.New(v, t)
@@ -237,10 +236,10 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
237236
}
238237
case sqltypes.Datetime:
239238
if res.Year() < 0 || res.Year() > 9999 {
240-
return time.Time{}, ErrConvertingToTimeOutOfRange.New(res.Format(sql.DatetimeLayoutNoTrim), t.String())
239+
return time.Time{}, ErrConvertingToTimeOutOfRange.New(res.Format(sql.TimestampDatetimeLayout), t.String())
241240
}
242241
case sqltypes.Timestamp:
243-
if res.Before(time.Unix(1, 0)) || res.After(time.Unix(math.MaxInt32, 999999000)) {
242+
if ValidateTimestamp(res) == nil {
244243
return time.Time{}, ErrConvertingToTimeOutOfRange.New(res.Format(sql.TimestampDatetimeLayout), t.String())
245244
}
246245
}
@@ -506,15 +505,6 @@ func ValidateTime(t time.Time) interface{} {
506505
return t
507506
}
508507

509-
// ValidateDatetime receives a time and returns either that time or nil if it's
510-
// not a valid datetime.
511-
func ValidateDatetime(t time.Time) interface{} {
512-
if t.Before(datetimeTypeMinDatetime) || t.After(datetimeTypeMaxDatetime) {
513-
return nil
514-
}
515-
return t
516-
}
517-
518508
// ValidateTimestamp receives a time and returns either that time or nil if it's
519509
// not a valid timestamp.
520510
func ValidateTimestamp(t time.Time) interface{} {
@@ -523,12 +513,3 @@ func ValidateTimestamp(t time.Time) interface{} {
523513
}
524514
return t
525515
}
526-
527-
// validateDate receives a time and returns either that time or nil if it's
528-
// not a valid date.
529-
func ValidateDate(t time.Time) interface{} {
530-
if t.Before(datetimeTypeMinDate) || t.After(datetimeTypeMaxDate) {
531-
return nil
532-
}
533-
return t
534-
}

sql/types/datetime_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,7 @@ func TestDatetimeOverflowUnderflow(t *testing.T) {
413413
val interface{}
414414
expectError bool
415415
}{
416-
//// Date underflow
417-
//{Date, "0999-12-31", true},
418-
//// Date overflow
419-
//{Date, "10000-01-01", true},
420-
//// Datetime underflow
421-
//{Datetime, "0999-12-31 23:59:59", true},
422-
//// Datetime overflow
423-
//{Datetime, "10000-01-01 00:00:00", true},
424-
// Timestamp underflow
425416
{Timestamp, "1969-12-31 23:59:59", true},
426-
// Timestamp overflow
427417
{Timestamp, "2038-01-19 03:14:08", true},
428418
{Date, Date.MinimumTime().Format("2006-01-02"), false},
429419
{Date, Date.MaximumTime().Format("2006-01-02"), false},

0 commit comments

Comments
 (0)