Skip to content

Commit

Permalink
Update at-modifier to apply after truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Jan 20, 2025
1 parent f480b54 commit 503fb1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 10 additions & 8 deletions runtime/pkg/rilltime/rilltime.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ type Grain struct {
}

type AtModifiers struct {
Offset *TimeAnchor `parser:"@@?"`
TimeZone *string `parser:"@TimeZone?"`
Offset *Grain `parser:"@@?"`
TimeZone *string `parser:"@TimeZone?"`
}

// ParseOptions allows for additional options that could probably not be added to the time range itself
Expand Down Expand Up @@ -208,18 +208,18 @@ func (e *Expression) Eval(evalOpts EvalOptions) (time.Time, time.Time, error) {
}

if e.Start != nil {
start = e.Modify(evalOpts, e.Start, start, true)
start = e.modify(evalOpts, e.Start, start)
}

end := evalOpts.Watermark
if e.End != nil {
end = e.Modify(evalOpts, e.End, end, true)
end = e.modify(evalOpts, e.End, end)
}

return start, end, nil
}

func (e *Expression) Modify(evalOpts EvalOptions, ta *TimeAnchor, tm time.Time, addOffset bool) time.Time {
func (e *Expression) modify(evalOpts EvalOptions, ta *TimeAnchor, tm time.Time) time.Time {
isTruncate := true
truncateGrain := e.truncateGrain
isBoundary := false
Expand Down Expand Up @@ -265,9 +265,6 @@ func (e *Expression) Modify(evalOpts EvalOptions, ta *TimeAnchor, tm time.Time,
if ta.Offset != nil {
tm = ta.Offset.offset(tm)
}
if addOffset && e.AtModifiers != nil && e.AtModifiers.Offset != nil {
tm = e.Modify(evalOpts, e.AtModifiers.Offset, tm, false)
}

if ta.Trunc != nil {
truncateGrain = grainMap[*ta.Trunc]
Expand All @@ -281,6 +278,11 @@ func (e *Expression) Modify(evalOpts EvalOptions, ta *TimeAnchor, tm time.Time,
modifiedTime = timeutil.CeilTime(tm, truncateGrain, e.timeZone, evalOpts.FirstDay, evalOpts.FirstMonth)
}

// Add offset after truncate
if e.AtModifiers != nil && e.AtModifiers.Offset != nil {
modifiedTime = e.AtModifiers.Offset.offset(modifiedTime)
}

if isBoundary && modifiedTime.Equal(timeBeforeOffset) && (e.Modifiers == nil || e.Modifiers.CompleteGrain == nil) {
// edge case where the end time falls on a boundary. add +1grain to make sure the last data point is included
n := 1
Expand Down
3 changes: 3 additions & 0 deletions runtime/pkg/rilltime/rilltime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func Test_Resolve(t *testing.T) {
{`-6d, now : |h|`, "2024-08-03T00:00:00Z", "2024-08-09T10:00:00Z"},
{`-6d, now : h`, "2024-08-03T00:00:00Z", "2024-08-09T11:00:00Z"},

{`0d : h`, "2024-08-05T00:00:00Z", "2024-08-05T07:00:00Z"},
{`0d : h @ -1d`, "2024-08-04T00:00:00Z", "2024-08-04T07:00:00Z"},

{`-7d, -5d : h`, "2024-07-29T00:00:00Z", "2024-07-31T00:00:00Z"},
{`-2d, now/d : h @ -5d`, "2024-08-02T00:00:00Z", "2024-08-04T00:00:00Z"},
{`-2d, now/d @ -5d`, "2024-08-02T00:00:00Z", "2024-08-04T00:00:00Z"},
Expand Down

0 comments on commit 503fb1b

Please sign in to comment.