Skip to content

Commit 466e563

Browse files
committed
ES|QL: Add test for TBucket into the AnalyzerTests
Closes #131068
1 parent b378d10 commit 466e563

File tree

5 files changed

+73
-37
lines changed

5 files changed

+73
-37
lines changed

docs/reference/query-languages/esql/_snippets/functions/examples/tbucket.md

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

docs/reference/query-languages/esql/kibana/definition/functions/tbucket.json

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

docs/reference/query-languages/esql/kibana/docs/functions/tbucket.md

Lines changed: 1 addition & 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/tbucket.csv-spec

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tbucketByTenSecondsDuration#[skip:-9.1.99,reason:new grouping function added in
44
required_capability: tbucket
55

66
FROM sample_data
7-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(10 seconds)
7+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(10 seconds)
88
| SORT min
99
;
1010

@@ -23,7 +23,7 @@ required_capability: implicit_casting_string_literal_to_temporal_amount
2323
required_capability: tbucket
2424

2525
FROM sample_data
26-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("10 seconds")
26+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("10 seconds")
2727
| SORT min
2828
;
2929

@@ -41,47 +41,47 @@ tbucketByTenMinutesDuration#[skip:-9.1.99,reason:new grouping function added in
4141
required_capability: tbucket
4242

4343
FROM sample_data
44-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(10 minutes)
44+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(10 minutes)
4545
| SORT min
4646
;
4747

4848
min:datetime | max:datetime | bucket:datetime
4949
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360Z | 2023-10-23T12:10:00.000Z
5050
2023-10-23T12:27:28.948Z | 2023-10-23T12:27:28.948Z | 2023-10-23T12:20:00.000Z
5151
2023-10-23T13:33:34.937Z | 2023-10-23T13:33:34.937Z | 2023-10-23T13:30:00.000Z
52-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:50:00.000Z
52+
2023-10-23T13:51:54.732Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:50:00.000Z
5353
;
5454

5555
tbucketByTenMinutesDurationAsString#[skip:-9.1.99,reason:new grouping function added in 9.2]
5656
required_capability: implicit_casting_string_literal_to_temporal_amount
5757
required_capability: tbucket
5858

5959
FROM sample_data
60-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("10 minutes")
60+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("10 minutes")
6161
| SORT min
6262
;
6363

6464
min:datetime | max:datetime | bucket:datetime
6565
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360Z | 2023-10-23T12:10:00.000Z
6666
2023-10-23T12:27:28.948Z | 2023-10-23T12:27:28.948Z | 2023-10-23T12:20:00.000Z
6767
2023-10-23T13:33:34.937Z | 2023-10-23T13:33:34.937Z | 2023-10-23T13:30:00.000Z
68-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:50:00.000Z
68+
2023-10-23T13:51:54.732Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:50:00.000Z
6969
;
7070

7171
docsTBucketByOneHourDuration#[skip:-9.1.99,reason:new grouping function added in 9.2]
7272
required_capability: tbucket
7373

7474
// tag::docsTBucketByOneHourDuration[]
7575
FROM sample_data
76-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 hour)
76+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 hour)
7777
| SORT min
7878
// end::docsTBucketByOneHourDuration[]
7979
;
8080

8181
// tag::docsTBucketByOneHourDuration-result[]
8282
min:datetime | max:datetime | bucket:datetime
83-
2023-10-23T12:27:28.948Z | 2023-10-23T12:27:28.948Z | 2023-10-23T12:00:00.000Z
84-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:00:00.000Z
83+
2023-10-23T12:15:03.360Z | 2023-10-23T12:27:28.948Z | 2023-10-23T12:00:00.000Z
84+
2023-10-23T13:33:34.937Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:00:00.000Z
8585
// end::docsTBucketByOneHourDuration-result[]
8686
;
8787

@@ -91,114 +91,114 @@ required_capability: tbucket
9191

9292
// tag::docsTBucketByOneHourDurationAsString[]
9393
FROM sample_data
94-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 hour")
94+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 hour")
9595
| SORT min
9696
// end::docsTBucketByOneHourDurationAsString[]
9797
;
9898

9999
// tag::docsTBucketByOneHourDurationAsString-result[]
100100
min:datetime | max:datetime | bucket:datetime
101-
2023-10-23T12:27:28.948Z | 2023-10-23T12:27:28.948Z | 2023-10-23T12:00:00.000Z
102-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:00:00.000Z
101+
2023-10-23T12:15:03.360Z | 2023-10-23T12:27:28.948Z | 2023-10-23T12:00:00.000Z
102+
2023-10-23T13:33:34.937Z | 2023-10-23T13:55:01.543Z | 2023-10-23T13:00:00.000Z
103103
// end::docsTBucketByOneHourDurationAsString-result[]
104104
;
105105

106106
tbucketByOneDayDuration#[skip:-9.1.99,reason:new grouping function added in 9.2]
107107
required_capability: tbucket
108108

109109
FROM sample_data
110-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 day)
110+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 day)
111111
| SORT min
112112
;
113113

114114
min:datetime | max:datetime | bucket:datetime
115-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
115+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
116116
;
117117

118118
tbucketByOneDayDurationAsString#[skip:-9.1.99,reason:new grouping function added in 9.2]
119119
required_capability: implicit_casting_string_literal_to_temporal_amount
120120
required_capability: tbucket
121121

122122
FROM sample_data
123-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 day")
123+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 day")
124124
| SORT min
125125
;
126126

127127
min:datetime | max:datetime | bucket:datetime
128-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
128+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
129129
;
130130

131131
tbucketByOneWeekDuration#[skip:-9.1.99,reason:new grouping function added in 9.2]
132132
required_capability: tbucket
133133

134134
FROM sample_data
135-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 week)
135+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 week)
136136
| SORT min
137137
;
138138

139139
min:datetime | max:datetime | bucket:datetime
140-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
140+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
141141
;
142142

143143
tbucketByOneWeekDurationAsString#[skip:-9.1.99,reason:new grouping function added in 9.2]
144144
required_capability: implicit_casting_string_literal_to_temporal_amount
145145
required_capability: tbucket
146146

147147
FROM sample_data
148-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 week")
148+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 week")
149149
| SORT min
150150
;
151151

152152
min:datetime | max:datetime | bucket:datetime
153-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
153+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-10-23T00:00:00.000Z
154154
;
155155

156156
tbucketByOneMonthDuration#[skip:-9.1.99,reason:new grouping function added in 9.2]
157157
required_capability: tbucket
158158

159159
FROM sample_data
160-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 month)
160+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 month)
161161
| SORT min
162162
;
163163

164164
min:datetime | max:datetime | bucket:datetime
165-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-01T00:00:00.000Z
165+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-10-01T00:00:00.000Z
166166
;
167167

168168
tbucketByOneMonthDurationAsString#[skip:-9.1.99,reason:new grouping function added in 9.2]
169169
required_capability: implicit_casting_string_literal_to_temporal_amount
170170
required_capability: tbucket
171171

172172
FROM sample_data
173-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 month")
173+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 month")
174174
| SORT min
175175
;
176176

177177
min:datetime | max:datetime | bucket:datetime
178-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-10-01T00:00:00.000Z
178+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-10-01T00:00:00.000Z
179179
;
180180

181181
tbucketByOneYearDuration#[skip:-9.1.99,reason:new grouping function added in 9.2]
182182
required_capability: tbucket
183183

184184
FROM sample_data
185-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 year)
185+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET(1 year)
186186
| SORT min
187187
;
188188

189189
min:datetime | max:datetime | bucket:datetime
190-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-01-01T00:00:00.000Z
190+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-01-01T00:00:00.000Z
191191
;
192192

193193
tbucketByOneYearDurationAsString#[skip:-9.1.99,reason:new grouping function added in 9.2]
194194
required_capability: implicit_casting_string_literal_to_temporal_amount
195195
required_capability: tbucket
196196

197197
FROM sample_data
198-
| STATS min = MAX(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 year")
198+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 year")
199199
| SORT min
200200
;
201201

202202
min:datetime | max:datetime | bucket:datetime
203-
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543Z | 2023-01-01T00:00:00.000Z
203+
2023-10-23T12:15:03.360Z | 2023-10-23T13:55:01.543Z | 2023-01-01T00:00:00.000Z
204204
;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.elasticsearch.xpack.esql.expression.function.fulltext.MultiMatch;
5151
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryString;
5252
import org.elasticsearch.xpack.esql.expression.function.grouping.Bucket;
53+
import org.elasticsearch.xpack.esql.expression.function.grouping.TBucket;
5354
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToDateNanos;
5455
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToDatetime;
5556
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToInteger;
@@ -4132,6 +4133,41 @@ public void testGroupingOverridesInInlinestats() {
41324133
""", "Found 1 problem\n" + "line 2:49: Unknown column [x]", "mapping-default.json");
41334134
}
41344135

4136+
public void testTBucketWithIntervalInStringInBothAggregationAndGrouping() {
4137+
var plan = analyze("""
4138+
FROM sample_data
4139+
| STATS min = MIN(@timestamp), max = MAX(@timestamp) BY bucket = TBUCKET("1 week")
4140+
| SORT min
4141+
""", "mapping-sample_data.json");
4142+
4143+
var limit = as(plan, Limit.class);
4144+
var orderBy = as(limit.child(), OrderBy.class);
4145+
var agg = as(orderBy.child(), Aggregate.class);
4146+
var aggregates = agg.aggregates();
4147+
assertThat(aggregates, hasSize(3));
4148+
Alias a = as(aggregates.get(0), Alias.class);
4149+
assertEquals("min", a.name());
4150+
Min min = as(a.child(), Min.class);
4151+
FieldAttribute fa = as(min.field(), FieldAttribute.class);
4152+
assertEquals("@timestamp", fa.name());
4153+
a = as(aggregates.get(1), Alias.class);
4154+
assertEquals("max", a.name());
4155+
Max max = as(a.child(), Max.class);
4156+
fa = as(max.field(), FieldAttribute.class);
4157+
assertEquals("@timestamp", fa.name());
4158+
ReferenceAttribute ra = as(aggregates.get(2), ReferenceAttribute.class);
4159+
assertEquals("bucket", ra.name());
4160+
List<Expression> groupings = agg.groupings();
4161+
assertEquals(1, groupings.size());
4162+
a = as(groupings.get(0), Alias.class); // reference in groupings is resolved
4163+
TBucket tbucket = as(a.child(), TBucket.class);
4164+
fa = as(tbucket.field(), FieldAttribute.class);
4165+
assertEquals("@timestamp", fa.name());
4166+
Literal literal = as(tbucket.buckets(), Literal.class);
4167+
Literal oneWeek = new Literal(EMPTY, Period.ofWeeks(1), DATE_PERIOD);
4168+
assertEquals(oneWeek, literal);
4169+
}
4170+
41354171
private void verifyNameAndType(String actualName, DataType actualType, String expectedName, DataType expectedType) {
41364172
assertEquals(expectedName, actualName);
41374173
assertEquals(expectedType, actualType);

0 commit comments

Comments
 (0)