feat(analyzer): resolve a relative origin against the compile clock

What does this MR do and why?

Before: origin on a fixed-day date bucket (!529 (merged)) took an absolute instant only. A dashboard that filters on a rolling window (created >= -60d) could not keep its buckets aligned with it - an absolute origin drifts by a day every day (raised in !529 (comment 3876801369)).

After: origin also takes the relative dates filters take: -60d, -2w, -1m, -1y, sign optional. It resolves at compile time against the same clock filters use, to midnight UTC of that day, and is canonicalised like an absolute value - so bare, quoted and absolute spellings give the same GraphQL argument and the same slug. Sorts pick it up through the existing parameter snapshot.

Changed surface:

  • Parser: a duration token is N + one of dwmy with an optional sign, so origin=-2w parses bare like -60d did. granularity=30w now fails in the analyzer with the granularity error instead of a parse error.

  • Date arithmetic, shared with filters: start_of_day, calculate_days and add_months are checked. A count the clock cannot hold is Invalid date calculation instead of a chrono panic - or, for a huge month count, a silently wrong year from the as i32 cast.

  • Canonical timestamp: years outside 0000-9999 are refused, for absolute values too, since %Y would emit a signed five-digit year that a GraphQL Time rejects at run time.

  • Errors: a relative count that overflows, or a date the clock cannot hold, reports that reason; anything else gets the accepted forms, which now quote the date-time example as the schema vocabulary already said:

    `yesterday` is not a valid value for `started(origin=...)`. Expected an ISO 8601 date or date-time, for example 2026-07-16 or "2026-07-16T00:00:00Z", or a relative date such as -60d
  • src/schema/vocabulary.rs describes the relative form; schema.json regenerated.

Unchanged / out of scope: origin=today() and origin=startOfDay("-60") still don't parse as field-function arguments (the issue's option 1). Gem and WASM consumers resolve against the real clock; there is no clock injection through CompileContext, same as filters. Switching the DAP Impact YAML to origin=-60d is a follow-up in gitlab-org/gitlab.

Tradeoffs: a relative origin changes the emitted argument and response key every day - by design, like relative filters. Alignment with a rolling filter needs >=: created > -60d emits 23:59 of day -60 and excludes it, while origin=-60d anchors at its midnight.

Closes #230 (closed)

Example Usage

Last 30 days vs. the 30 before, anchored on the same rolling window as the filter:

mode: analytics
query: type = pipeline and started >= -60d
dimensions: started(granularity=30d, origin=-60d)
metrics: totalCount
sort: started asc
Test via ruby extension
cd glql_rb
bundle install
bundle exec rake compile

bundle exec ruby -Ilib - <<'RUBY'
require "gitlab_query_language"
require "json"
require "net/http"

query   = "type = pipeline and started >= -60d"
context = { mode: "analytics", dimensions: "started(granularity=30d, origin=-60d)", metrics: "totalCount", sort: "started asc", project: "gitlab-org/gitlab" }

compiled = Glql.compile(query, context)
puts "== Generated GraphQL =="
puts compiled["output"]

uri  = URI("https://gitlab.com/api/graphql")
http = Net::HTTP.new(uri.host, uri.port); http.use_ssl = true
req  = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
req["PRIVATE-TOKEN"] = ENV.fetch("GITLAB_TOKEN")
req.body = { query: compiled["output"], variables: { limit: 5 } }.to_json
response = JSON.parse(http.request(req).body)

transformed = Glql.transform(response["data"], { mode: context[:mode], fields: compiled["fields"] })
puts "\n== Transformed rows =="
puts JSON.pretty_generate(transformed["data"])
RUBY

Output from a run on 2026-09-22 - startedAtFrom and origin name the same day, and both move with the clock:

pipelines(startedAtFrom: "2026-07-24 00:00") {
  aggregated(orderBy: [{direction: ASC, identifier: "startedAt", parameters: {granularity: "30d", origin: "2026-07-24T00:00:00Z"}}]) {
    nodes {
      dimensions { startedAt(granularity: "30d", origin: "2026-07-24T00:00:00Z") }
      totalCount
    }
  }
}
[
  { "totalCount": 165292, "started": "2026-07-24" },
  { "totalCount": 180754, "started": "2026-08-23" },
  { "totalCount": 6447,   "started": "2026-09-22" }
]

The third row is the partial bucket that started that day at midnight UTC.

How to set up and validate locally

Run the snippet above with GITLAB_TOKEN set. Then try origin="yesterday" for the accepted-forms error, origin=-999999999999d for Invalid date calculation, and origin=-2w bare to see it resolve.

Edited by Daniele Rossetti

Merge request reports

Loading
Loading