Skip to content

Commit

Permalink
sql: age returns normalized intervals
Browse files Browse the repository at this point in the history
Release note (bug fix): The age function previous did not normalize the
duration for large day or H:M:S values in the same way PostgreSQL does.
This is now fixed.
  • Loading branch information
ekalinin committed Jul 7, 2020
1 parent 9304ecd commit 88a5d94
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
7 changes: 6 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/datetime
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,18 @@ SELECT '5874897-12-31'::date - '4714-11-24 BC'::date
query T
SELECT age('2001-04-10 22:06:45', '1957-06-13')
----
384190:06:45
44 years 5 mons 17 days 22:06:45

query B
SELECT age('1957-06-13') - age(now(), '1957-06-13') = interval '0s'
----
true

query T
select age('2017-12-10'::timestamptz, '2017-12-01'::timestamptz)
----
9 days

query B
SELECT now() - timestamp '2015-06-13' > interval '100h'
----
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/timestamp
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ SELECT
FROM example
ORDER BY a
----
2010-11-07 22:59:00 -0600 CST 2010-11-07 23:59:00 -0600 CST 2010-12-06 23:59:00 -0600 CST 2010-11-05 23:59:00 -0500 CDT 2010-11-05 23:59:00 -0500 CDT 2010-10-06 23:59:00 -0500 CDT 00:00:00 -25:00:00 2010-11-06 23:59:00-05:00
2010-11-08 23:59:00 -0600 CST 2010-11-08 23:59:00 -0600 CST 2010-12-07 23:59:00 -0600 CST 2010-11-07 00:59:00 -0500 CDT 2010-11-06 23:59:00 -0500 CDT 2010-10-07 23:59:00 -0500 CDT 25:00:00 00:00:00 2010-11-07 23:59:00-06:00
2010-11-07 22:59:00 -0600 CST 2010-11-07 23:59:00 -0600 CST 2010-12-06 23:59:00 -0600 CST 2010-11-05 23:59:00 -0500 CDT 2010-11-05 23:59:00 -0500 CDT 2010-10-06 23:59:00 -0500 CDT 00:00:00 -1 days -01:00:00 2010-11-06 23:59:00-05:00
2010-11-08 23:59:00 -0600 CST 2010-11-08 23:59:00 -0600 CST 2010-12-07 23:59:00 -0600 CST 2010-11-07 00:59:00 -0500 CDT 2010-11-06 23:59:00 -0500 CDT 2010-10-07 23:59:00 -0500 CDT 1 day 01:00:00 00:00:00 2010-11-07 23:59:00-06:00

statement ok
DROP TABLE example
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/norm/testdata/rules/fold_constants
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ values
├── cardinality: [1 - 1]
├── key: ()
├── fd: ()-->(1)
└── ('-24:00:00',)
└── ('-1 days',)

# Fold stable operation.
norm expect=FoldBinary
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
ReturnType: types.Interval,
Fn: func(_ *EvalContext, left Datum, right Datum) (Datum, error) {
nanos := left.(*DTimestamp).Sub(right.(*DTimestamp).Time).Nanoseconds()
return &DInterval{Duration: duration.MakeDuration(nanos, 0, 0)}, nil
return &DInterval{Duration: duration.MakeNormalizedDuration(nanos, 0, 0)}, nil
},
Volatility: VolatilityImmutable,
},
Expand All @@ -982,7 +982,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
ReturnType: types.Interval,
Fn: func(_ *EvalContext, left Datum, right Datum) (Datum, error) {
nanos := left.(*DTimestampTZ).Sub(right.(*DTimestampTZ).Time).Nanoseconds()
return &DInterval{Duration: duration.MakeDuration(nanos, 0, 0)}, nil
return &DInterval{Duration: duration.MakeNormalizedDuration(nanos, 0, 0)}, nil
},
Volatility: VolatilityImmutable,
},
Expand All @@ -998,7 +998,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
return nil, err
}
nanos := left.(*DTimestamp).Sub(stripped.Time).Nanoseconds()
return &DInterval{Duration: duration.MakeDuration(nanos, 0, 0)}, nil
return &DInterval{Duration: duration.MakeNormalizedDuration(nanos, 0, 0)}, nil
},
Volatility: VolatilityStable,
},
Expand All @@ -1014,7 +1014,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
return nil, err
}
nanos := stripped.Sub(right.(*DTimestamp).Time).Nanoseconds()
return &DInterval{Duration: duration.MakeDuration(nanos, 0, 0)}, nil
return &DInterval{Duration: duration.MakeNormalizedDuration(nanos, 0, 0)}, nil
},
Volatility: VolatilityStable,
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/duration/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func MakeDuration(nanos, days, months int64) Duration {
}
}

// MakeNormalizedDuration returns a normalized Duration.
func MakeNormalizedDuration(nanos, days, months int64) Duration {
return Duration{
Months: months,
Days: days,
nanos: rounded(nanos),
}.normalize()
}

// DecodeDuration returns a Duration without rounding nanos.
func DecodeDuration(months, days, nanos int64) Duration {
return Duration{
Expand Down

0 comments on commit 88a5d94

Please sign in to comment.