Repository navigation
Convert date_histogram bounds, time_zone and stats - #159
serhiy-bzhezytskyy wants to merge 5 commits into
Conversation
A converted date_histogram began at NOW/YEAR-10YEAR, so data older than ten years fell outside every bucket. Take start and end from the query's range on the same field, align a fixed_interval to the epoch as the engine does, and leave a loud placeholder when the query has no such range.
Dropping time_zone moves every bucket edge without failing, and stats was skipped, so the metrics of a bucket were missing. Pass the zone as TZ, enumerate day-based fixed intervals across the offset change, and turn stats into min, max, sum and avg.
The converter page said only that date_histogram becomes a range facet, which hid that its bounds come from the query and that a missing range leaves placeholders.
There was a problem hiding this comment.
🟡 Changes recommended
Critical issues remain in calendar and millisecond bucket alignment, inclusive upper bounds, per-aggregation time zones, fractional-second parsing, and metric-name collisions, causing incorrect results or runtime failures.
6 open issues
Preserve per-aggregation time zones · New Include the bucket at an inclusive upper boundary · New Align millisecond intervals with integer precision · New Detect collisions between generated and sibling metric names · New Align all calendar intervals to calendar boundaries · New Accept fractional-second ISO instants · New
What changed in this PR
This PR addresses #158 by improving how Solr Orbit converts OpenSearch date histograms and statistics, replacing clock-relative bounds that excluded historical datasets such as nyc_taxis.
Changes:
- Derives histogram bounds from query ranges, aligns fixed intervals, and adds time-zone handling.
- Expands
statsaggregations into min, max, sum, and average facets. - Adds regression tests and documents placeholders requiring manual bounds.
| File | Description |
|---|---|
tests/unit/solr/test_workload_converter.py |
Tests bounds, interval alignment, time zones, and stats conversion. |
solrorbit/conversion/query.py |
Implements query-derived bounds, zoned buckets, and stats facets. |
docs/converter/what-converts.md |
Documents conversion support and manual-bound placeholders. |
Review effort: Balanced
| zone = _time_zone_of(aggs) | ||
| if zone and facets: | ||
| result["params"] = {"TZ": zone} |
There was a problem hiding this comment.
Fixed in 4db7c4b: differing zones in one request, including a zoned and an unzoned histogram, now raise an error that names them.
| lo, _, hi, _ = _range_bounds(bounds) | ||
| if _SOLR_INSTANT.fullmatch(str(lo)) and _SOLR_INSTANT.fullmatch(str(hi)): | ||
| return lo, hi |
There was a problem hiding this comment.
Fixed in 4db7c4b: an inclusive end on a bucket edge adds one gap, so those documents are counted (4 of 4 on a sample index, was 2 of 4).
| seconds = {"ms": 0.001, "s": 1, "m": 60, "h": 3600, "d": 86400}[match.group(2)] | ||
| step = int(match.group(1) or 1) * seconds | ||
| epoch = datetime(1970, 1, 1) | ||
| elapsed = (datetime.strptime(start, "%Y-%m-%dT%H:%M:%SZ") - epoch).total_seconds() | ||
| return (epoch + timedelta(seconds=(elapsed // step) * step)).strftime("%Y-%m-%dT%H:%M:%SZ") |
There was a problem hiding this comment.
Fixed in 4db7c4b: the alignment uses integer milliseconds and keeps the fraction (2699999ms gives 23:51:14.048Z).
| for stat in ("min", "max", "sum", "avg"): | ||
| result[f"{agg_name}_{stat}"] = f"{stat}({field})" |
There was a problem hiding this comment.
Fixed in 4db7c4b: a stats name that collides with a sibling aggregation raises an error, in either order.
| start, end = "REPLACE_WITH_START", "REPLACE_WITH_END" | ||
| zone = dh_conf.get("time_zone") | ||
| ranges, in_zone = None, False | ||
| if zone and bounds: |
There was a problem hiding this comment.
Fixed in 4db7c4b: without a zone the start is rounded down to the unit in UTC; weeks start on Monday, quarters on the quarter. Zoned weeks and quarters still warn.
| return date_str, False | ||
|
|
||
|
|
||
| _SOLR_INSTANT = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z") |
There was a problem hiding this comment.
Fixed in 4db7c4b: instants with fractional seconds are accepted in the bounds and in the alignment and zoned-range code.
A review of the bounds, time_zone and stats conversion found inputs that gave wrong buckets without an error: one TZ taken for several differently zoned histograms, an inclusive upper bound on a bucket edge dropping the documents on it, float arithmetic in the epoch alignment, a stats name overwriting a sibling, calendar intervals starting in the middle of their unit, and bounds with fractional seconds read as missing. Refuse what one Solr request cannot express and fix the rest.
Keep both sides of the tests file: the date_histogram test classes of this branch and the test class for search bodies without a query from apache#161.

Fixes #158.
The converter put every
date_histogramonNOW/YEAR-10YEARtoNOW/YEAR+1YEAR, so for nyc_taxis (2015) its facets came back empty, and it droppedtime_zoneandstats. Three commits: bounds from the query's range (with a placeholder and a warning when there is none, so such an operation now fails when run instead of returning nothing);time_zoneandstats; the converter docs.Converting nyc_taxis now gives the same bucket keys and counts as OpenSearch on a 300,640-document sample for the three affected operations. The new tests fail without the change.
The three operations this produces: apache/solr-orbit-workloads#27.