Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-19.2: sql: bugfix to fractional year interval parsing #56250

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
27 changes: 20 additions & 7 deletions pkg/sql/sem/tree/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,11 @@ func parseDuration(s string) (duration.Duration, error) {
// A regular number followed by a unit, such as "9 day".
d = d.Add(unit.Mul(v))
if hasDecimal {
d = addFrac(d, unit, vp)
var err error
d, err = addFrac(d, unit, vp)
if err != nil {
return d, err
}
}
continue
}
Expand Down Expand Up @@ -537,14 +541,23 @@ func (l *intervalLexer) parseShortDuration(h int64, hasSign bool) (duration.Dura
// given as second argument multiplied by the factor in the third
// argument. For computing fractions there are 30 days to a month and
// 24 hours to a day.
func addFrac(d duration.Duration, unit duration.Duration, f float64) duration.Duration {
func addFrac(d duration.Duration, unit duration.Duration, f float64) (duration.Duration, error) {
if unit.Months > 0 {
f = f * float64(unit.Months)
d.Months += int64(f)
f = math.Mod(f, 1) * 30
d.Days += int64(f)
f = math.Mod(f, 1) * 24
d.SetNanos(d.Nanos() + int64(float64(time.Hour.Nanoseconds())*f))
switch unit.Months {
case 1:
f = math.Mod(f, 1) * 30
d.Days += int64(f)
f = math.Mod(f, 1) * 24
d.SetNanos(d.Nanos() + int64(float64(time.Hour.Nanoseconds())*f))
case 12:
// Nothing to do: Postgres limits the precision of fractional years to
// months. Do not continue to add precision to the interval.
// See issue #55226 for more details on this.
default:
return duration.Duration{}, errors.AssertionFailedf("unhandled unit type %v", unit)
}
} else if unit.Days > 0 {
f = f * float64(unit.Days)
d.Days += int64(f)
Expand All @@ -553,7 +566,7 @@ func addFrac(d duration.Duration, unit duration.Duration, f float64) duration.Du
} else {
d.SetNanos(d.Nanos() + int64(float64(unit.Nanos())*f))
}
return d
return d, nil
}

// floatToNanos converts a fractional number representing nanoseconds to the
Expand Down
9 changes: 7 additions & 2 deletions pkg/sql/sem/tree/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,13 @@ func TestPGIntervalSyntax(t *testing.T) {
{`1yr`, `1 year`, ``},
{`1yrs`, `1 year`, ``},
{`1.5y`, `1 year 6 mons`, ``},
{`1.1y`, `1 year 1 mon 6 days`, ``},
{`1.11y`, `1 year 1 mon 9 days 14:24:00`, ``},
{`1.1y`, `1 year 1 mon`, ``},
{`1.19y`, `1 year 2 mons`, ``},
{`1.11y`, `1 year 1 mon`, ``},
{`-1.5y`, `-1 years -6 mons`, ``},
{`-1.1y`, `-1 years -1 mons`, ``},
{`-1.19y`, `-1 years -2 mons`, ``},
{`-1.11y`, `-1 years -1 mons`, ``},

// Mixed unit/HH:MM:SS formats
{`1:2:3`, `01:02:03`, ``},
Expand Down