Skip to content

Commit 4172bdf

Browse files
committed
Add one example with offset
1 parent ade29fb commit 4172bdf

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

docs/reference/esql/functions/examples/bucket.asciidoc

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/kibana/definition/bucket.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/qa/testFixtures/src/main/resources/bucket.csv-spec

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ AVG(salary):double | bucket:date
145145
// end::bucket_in_agg-result[]
146146
;
147147

148+
bucketWithOffset#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
149+
// tag::bucketWithOffset[]
150+
FROM employees
151+
| STATS dates = values(birth_date) BY b = BUCKET(birth_date + 1 hour, 1 year) - 1 hour
152+
| EVAL d_count = MV_COUNT(dates)
153+
| SORT d_count
154+
| LIMIT 3
155+
// end::bucketWithOffset[]
156+
;
157+
158+
// tag::bucketWithOffset-result[]
159+
dates:date |b:date |d_count:integer
160+
1965-01-03T00:00:00.000Z |1964-12-31T23:00:00.000Z|1
161+
[1964-06-02T00:00:00.000Z, 1964-04-18T00:00:00.000Z, 1964-10-18T00:00:00.000Z, 1964-06-11T00:00:00.000Z]|1963-12-31T23:00:00.000Z|4
162+
[1955-01-21T00:00:00.000Z, 1955-08-20T00:00:00.000Z, 1955-08-28T00:00:00.000Z, 1955-10-04T00:00:00.000Z]|1954-12-31T23:00:00.000Z|4
163+
// end::bucketWithOffset-result[]
164+
;
165+
148166
docsBucketMonth#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
149167
//tag::docsBucketMonth[]
150168
FROM employees

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/grouping/Bucket.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ another in which the bucket size is provided directly (two parameters).
163163
grouping part, or that it is invoked with the exact same expression:""",
164164
file = "bucket",
165165
tag = "reuseGroupingFunctionWithExpression"
166+
),
167+
@Example(
168+
description = """
169+
Sometimes you need to change the start value of each bucket by a given duration (similar to date histogram
170+
aggregation's <<search-aggregations-bucket-histogram-aggregation,`offset`>> parameter). To do so, you will need to
171+
take into account how the language handles expressions within the `STATS` command: if these contain functions or
172+
arithmetic operators, a virtual `EVAL` is inserted before and/or after the `STATS` command. Consequently, a double
173+
compensation is needed to adjust the bucketed date value before the aggregation and then again after. For instance,
174+
inserting a negative offset of `1 day` you would like this:""",
175+
file = "bucket",
176+
tag = "bucketWithOffset"
166177
) }
167178
)
168179
public Bucket(

0 commit comments

Comments
 (0)