Skip to content

Commit 63ab3be

Browse files
committed
ESQL: Docs: add example of date bucketing with offset (#116680)
Add an example of how to create date histograms with an offset. Fixes #114167
1 parent a355f19 commit 63ab3be

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
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/java/org/elasticsearch/xpack/esql/CsvTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.GEO;
6464

6565
public final class CsvTestUtils {
66-
private static final int MAX_WIDTH = 20;
66+
private static final int MAX_WIDTH = 80;
6767
private static final CsvPreference CSV_SPEC_PREFERENCES = new CsvPreference.Builder('"', '|', "\r\n").build();
6868
private static final String NULL_VALUE = "null";
6969
private static final char ESCAPE_CHAR = '\\';

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 = MV_SORT(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, b
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+
[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
162+
[1957-04-04T00:00:00.000Z, 1957-05-23T00:00:00.000Z, 1957-05-25T00:00:00.000Z, 1957-12-03T00:00:00.000Z]|1956-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 hour` to buckets of `1 year` looks like this:""",
175+
file = "bucket",
176+
tag = "bucketWithOffset"
166177
) }
167178
)
168179
public Bucket(

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,14 @@ private void doTest() throws Exception {
319319
}
320320

321321
protected void assertResults(ExpectedResults expected, ActualResults actual, boolean ignoreOrder, Logger logger) {
322-
CsvAssert.assertResults(expected, actual, ignoreOrder, logger);
323322
/*
324-
* Comment the assertion above and enable the next two lines to see the results returned by ES without any assertions being done.
323+
* Enable the next two lines to see the results returned by ES.
325324
* This is useful when creating a new test or trying to figure out what are the actual results.
326325
*/
327326
// CsvTestUtils.logMetaData(actual.columnNames(), actual.columnTypes(), LOGGER);
328327
// CsvTestUtils.logData(actual.values(), LOGGER);
328+
329+
CsvAssert.assertResults(expected, actual, ignoreOrder, logger);
329330
}
330331

331332
private static IndexResolution loadIndexResolution(String mappingName, String indexName, Map<String, String> typeMapping) {

0 commit comments

Comments
 (0)