Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
98 changes: 97 additions & 1 deletion enginetest/queries/function_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ var FunctionQueryTests = []QueryTest{
Query: "SELECT TIMESTAMPDIFF(SECOND, null, '2007-12-31 00:00:00');",
Expected: []sql.Row{{nil}},
},
// https://github.com/dolthub/dolt/issues/10393
// TIMESTAMPDIFF YEAR tests https://github.com/dolthub/dolt/issues/10393
{
Query: "SELECT TIMESTAMPDIFF(YEAR, DATE '2011-07-05', DATE '2026-07-04')",
Expected: []sql.Row{{14}},
Expand All @@ -936,6 +936,102 @@ var FunctionQueryTests = []QueryTest{
Query: "SELECT TIMESTAMPDIFF(YEAR, DATE '2026-07-03', DATE '2025-07-04')",
Expected: []sql.Row{{0}},
},
{
Query: `select timestampdiff(year, "0050-01-01", "2020-01-01");`,
Expected: []sql.Row{{1970}},
},
{
Query: "select timestampdiff(year, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{9999}},
},
// TIMESTAMPDIFF MONTH tests https://github.com/dolthub/dolt/issues/10393
{
Query: `select timestampdiff(year, "2000-12-25", "2020-2-20");`,
Expected: []sql.Row{{19}},
},
{
Query: "SELECT TIMESTAMPDIFF(month, DATE '2011-07-05', DATE '2026-07-04')",
Expected: []sql.Row{{179}},
},
{
Query: "SELECT TIMESTAMPDIFF(month, DATE '2026-07-04', DATE '2011-07-05')",
Expected: []sql.Row{{-179}},
},
{
Query: `select timestampdiff(month, "2000-12-25", "2020-2-20");`,
Expected: []sql.Row{{229}},
},
{
Query: `select timestampdiff(month, "0050-01-01", "2020-01-01");`,
Expected: []sql.Row{{23640}},
},
{
Query: "select timestampdiff(month, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{119999}},
},
// TIMESTAMPDIFF QUARTER tests https://github.com/dolthub/dolt/issues/10393
{
Query: "SELECT TIMESTAMPDIFF(quarter, DATE '2011-07-05', DATE '2026-07-04')",
Expected: []sql.Row{{59}},
},
{
Query: "SELECT TIMESTAMPDIFF(quarter, DATE '2026-07-04', DATE '2011-07-05')",
Expected: []sql.Row{{-59}},
},
{
Query: `select timestampdiff(quarter, "0050-01-01", "2020-01-01");`,
Expected: []sql.Row{{7880}},
},
{
Query: `select timestampdiff(quarter, "2000-12-25", "2020-2-20");`,
Expected: []sql.Row{{76}},
},
{
Query: "select timestampdiff(quarter, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{39999}},
},
{
// https://github.com/dolthub/dolt/issues/10397
Skip: true,
// might need to change first date to 0001-01-01 since 0000 is a leap year in Go but not in MySQL
Query: "select timestampdiff(microsecond, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{315569433599999999}},
},
{
// https://github.com/dolthub/dolt/issues/10397
Skip: true,
// might need to change first date to 0001-01-01 since 0000 is a leap year in Go but not in MySQL
Query: "select timestampdiff(second, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{315569433599}},
},
{
// https://github.com/dolthub/dolt/issues/10397
Skip: true,
// might need to change first date to 0001-01-01 since 0000 is a leap year in Go but not in MySQL
Query: "select timestampdiff(minute, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{5259490559}},
},
{
// https://github.com/dolthub/dolt/issues/10397
Skip: true,
// might need to change first date to 0001-01-01 since 0000 is a leap year in Go but not in MySQL
Query: "select timestampdiff(hour, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{87658175}},
},
{
// https://github.com/dolthub/dolt/issues/10397
Skip: true,
// might need to change first date to 0001-01-01 since 0000 is a leap year in Go but not in MySQL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what a tragedy

Query: "select timestampdiff(day, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{3652423}},
},
{
// https://github.com/dolthub/dolt/issues/10397
Skip: true,
// might need to change first date to 0001-01-01 since 0000 is a leap year in Go but not in MySQL
Query: "select timestampdiff(week, '0000-01-01', '9999-12-31 23:59:59.999999');",
Expected: []sql.Row{{521774}},
},
// TRIM Function Tests
{
Query: `SELECT TRIM(mytable.s) AS s FROM mytable`,
Expand Down
116 changes: 50 additions & 66 deletions sql/expression/function/time_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,10 @@ func (t *TimestampDiff) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)

unit = strings.TrimPrefix(strings.ToLower(unit.(string)), "sql_tsi_")

date1 := expr1.(time.Time)
date2 := expr2.(time.Time)
time1 := expr1.(time.Time)
time2 := expr2.(time.Time)

diff := date2.Sub(date1)
diff := time2.Sub(time1)

var res int64
switch unit {
Expand All @@ -689,74 +689,58 @@ func (t *TimestampDiff) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
case "week":
res = int64(diff.Hours() / (24 * 7))
case "month":
// TODO: this calculation is not correct. Not every month is 30 days https://github.com/dolthub/dolt/issues/10393
res = int64(diff.Hours() / (24 * 30))
if res > 0 {
if date2.Day()-date1.Day() < 0 {
res -= 1
} else if date2.Hour()-date1.Hour() < 0 {
res -= 1
} else if date2.Minute()-date1.Minute() < 0 {
res -= 1
} else if date2.Second()-date1.Second() < 0 {
res -= 1
}
}
res = dateDiff(time1, time2, false)
case "quarter":
// TODO: this calculation is not correct. Not every month is 30 days https://github.com/dolthub/dolt/issues/10393
monthRes := int64(diff.Hours() / (24 * 30))
if monthRes > 0 {
if date2.Day()-date1.Day() < 0 {
monthRes -= 1
} else if date2.Hour()-date1.Hour() < 0 {
monthRes -= 1
} else if date2.Minute()-date1.Minute() < 0 {
monthRes -= 1
} else if date2.Second()-date1.Second() < 0 {
monthRes -= 1
}
}
res = monthRes / 3
res = dateDiff(time1, time2, false) / 3
case "year":
if diff == 0 {
return 0, nil
}
negate := false
before := date1
after := date2
if diff < 0 {
negate = true
before = date2
after = date1
}

beforeYear, beforeMonth, beforeDay := before.Date()
afterYear, afterMonth, afterDay := after.Date()
yearDiff := afterYear - beforeYear
if beforeMonth > afterMonth {
yearDiff -= 1
} else if beforeMonth == afterMonth {
if beforeDay > afterDay {
yearDiff -= 1
} else if beforeDay == afterDay {
beforeHour, beforeMin, beforeSec := before.Clock()
afterHour, afterMin, afterSec := after.Clock()
secondDiff := (afterHour-beforeHour)*3600 + (afterMin-beforeMin)*60 + (afterSec - beforeSec)
if secondDiff < 0 {
yearDiff -= 1
} else if secondDiff == 0 && before.Nanosecond() > after.Nanosecond() {
yearDiff -= 1
}
}
}

res = int64(yearDiff)
if negate {
res = -res
}
res = dateDiff(time1, time2, true)
default:
return nil, errors.NewKind("invalid interval unit: %s").New(unit)
}

return res, nil
}

// dateDiff calculates the difference between two time.Times based on their Date and Clock values. If yearDiff is set
// to true, dateDiff returns the difference in number of full years. Otherwise, dateDiff returns the difference in
// number of full months.
func dateDiff(date1, date2 time.Time, yearDiff bool) int64 {
Comment thread
angelamayxie marked this conversation as resolved.
Outdated
compare := date1.Compare(date2)
if compare == 0 {
return 0
}
sign := 1
before := date1
after := date2
if compare > 0 {
sign = -1
before = date2
after = date1
}

beforeYear, beforeMonth, beforeDay := before.Date()
afterYear, afterMonth, afterDay := after.Date()

checkDayClock := !yearDiff || afterMonth == beforeMonth
res := int64(afterYear-beforeYear)*12 + int64(afterMonth) - int64(beforeMonth)
if yearDiff {
res = res / 12
}

if checkDayClock {
if beforeDay > afterDay {
res -= 1
} else if beforeDay == afterDay {
beforeHour, beforeMin, beforeSec := before.Clock()
afterHour, afterMin, afterSec := after.Clock()
secondDiff := (afterHour-beforeHour)*3600 + (afterMin-beforeMin)*60 + (afterSec - beforeSec)
if secondDiff < 0 {
res -= 1
} else if secondDiff == 0 && before.Nanosecond() > after.Nanosecond() {
res -= 1
}
}
}

return int64(sign) * res
}
Loading