Skip to content

Commit 3b2021f

Browse files
authored
[ESQL] Fix Binary Comparisons on Date Nanos (#116346)
Relates to #109992 When I implemented binary comparisons for date nanos in #111908 I failed to update the verifyBinaryComparisons rule, which implements another layer of type checking for binary comparisons, external to the type logic in the Expression classes. Since the unit tests for the expressions don't run the full query processing stack, this bug was invisible to them. Only full integration tests, such as the CSV tests I've added here, can catch this kind of error. The initial PR did not include these because we didn't have enough supporting functions (notably TO_DATE_NANOS) to write the tests. This PR fixes the issue by adding DATE_NANOS to the list of allowed types for binary comparisons in the verifier.
1 parent b1f2087 commit 3b2021f

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

docs/changelog/116346.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 116346
2+
summary: "[ESQL] Fix Binary Comparisons on Date Nanos"
3+
area: ES|QL
4+
type: bug
5+
issues: []

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,83 @@ d:date_nanos
202202
null
203203
;
204204

205+
date nanos greater than
206+
required_capability: to_date_nanos
207+
required_capability: date_nanos_binary_comparison
208+
209+
FROM date_nanos | WHERE MV_MIN(nanos) > TO_DATE_NANOS("2023-10-23T12:27:28.948000000Z") | SORT nanos DESC;
210+
211+
millis:date | nanos:date_nanos | num:long
212+
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543123456Z | 1698069301543123456
213+
2023-10-23T13:53:55.832Z | 2023-10-23T13:53:55.832987654Z | 1698069235832987654
214+
2023-10-23T13:52:55.015Z | 2023-10-23T13:52:55.015787878Z | 1698069175015787878
215+
2023-10-23T13:51:54.732Z | 2023-10-23T13:51:54.732102837Z | 1698069114732102837
216+
2023-10-23T13:33:34.937Z | 2023-10-23T13:33:34.937193000Z | 1698068014937193000
217+
;
218+
219+
date nanos greater than or equal
220+
required_capability: to_date_nanos
221+
required_capability: date_nanos_binary_comparison
222+
223+
FROM date_nanos | WHERE MV_MIN(nanos) >= TO_DATE_NANOS("2023-10-23T12:27:28.948000000Z") | SORT nanos DESC;
224+
225+
millis:date | nanos:date_nanos | num:long
226+
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543123456Z | 1698069301543123456
227+
2023-10-23T13:53:55.832Z | 2023-10-23T13:53:55.832987654Z | 1698069235832987654
228+
2023-10-23T13:52:55.015Z | 2023-10-23T13:52:55.015787878Z | 1698069175015787878
229+
2023-10-23T13:51:54.732Z | 2023-10-23T13:51:54.732102837Z | 1698069114732102837
230+
2023-10-23T13:33:34.937Z | 2023-10-23T13:33:34.937193000Z | 1698068014937193000
231+
2023-10-23T12:27:28.948Z | 2023-10-23T12:27:28.948000000Z | 1698064048948000000
232+
;
233+
234+
date nanos less than
235+
required_capability: to_date_nanos
236+
required_capability: date_nanos_binary_comparison
237+
238+
FROM date_nanos | WHERE MV_MIN(nanos) < TO_DATE_NANOS("2023-10-23T12:27:28.948000000Z") AND millis > "2000-01-01" | SORT nanos DESC;
239+
240+
millis:date | nanos:date_nanos | num:long
241+
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360103847Z | 1698063303360103847
242+
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360103847Z | 1698063303360103847
243+
;
244+
245+
date nanos less than equal
246+
required_capability: to_date_nanos
247+
required_capability: date_nanos_binary_comparison
248+
249+
FROM date_nanos | WHERE MV_MIN(nanos) <= TO_DATE_NANOS("2023-10-23T12:27:28.948000000Z") AND millis > "2000-01-01" | SORT nanos DESC;
250+
251+
millis:date | nanos:date_nanos | num:long
252+
2023-10-23T12:27:28.948Z | 2023-10-23T12:27:28.948000000Z | 1698064048948000000
253+
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360103847Z | 1698063303360103847
254+
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360103847Z | 1698063303360103847
255+
;
256+
257+
date nanos equals
258+
required_capability: to_date_nanos
259+
required_capability: date_nanos_binary_comparison
260+
261+
FROM date_nanos | WHERE MV_MIN(nanos) == TO_DATE_NANOS("2023-10-23T12:27:28.948000000Z");
262+
263+
millis:date | nanos:date_nanos | num:long
264+
2023-10-23T12:27:28.948Z | 2023-10-23T12:27:28.948000000Z | 1698064048948000000
265+
;
266+
267+
date nanos not equals
268+
required_capability: to_date_nanos
269+
required_capability: date_nanos_binary_comparison
270+
271+
FROM date_nanos | WHERE MV_MIN(nanos) != TO_DATE_NANOS("2023-10-23T12:27:28.948000000Z") AND millis > "2000-01-01" | SORT nanos DESC;
272+
273+
millis:date | nanos:date_nanos | num:long
274+
2023-10-23T13:55:01.543Z | 2023-10-23T13:55:01.543123456Z | 1698069301543123456
275+
2023-10-23T13:53:55.832Z | 2023-10-23T13:53:55.832987654Z | 1698069235832987654
276+
2023-10-23T13:52:55.015Z | 2023-10-23T13:52:55.015787878Z | 1698069175015787878
277+
2023-10-23T13:51:54.732Z | 2023-10-23T13:51:54.732102837Z | 1698069114732102837
278+
2023-10-23T13:33:34.937Z | 2023-10-23T13:33:34.937193000Z | 1698068014937193000
279+
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360103847Z | 1698063303360103847
280+
2023-10-23T12:15:03.360Z | 2023-10-23T12:15:03.360103847Z | 1698063303360103847
281+
;
205282

206283
date nanos to long, index version
207284
required_capability: to_date_nanos

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ public enum Cap {
319319
*/
320320
TO_DATE_NANOS(EsqlCorePlugin.DATE_NANOS_FEATURE_FLAG),
321321

322+
/**
323+
* Support for date nanos type in binary comparisons
324+
*/
325+
DATE_NANOS_BINARY_COMPARISON(EsqlCorePlugin.DATE_NANOS_FEATURE_FLAG),
326+
322327
/**
323328
* Support Least and Greatest functions on Date Nanos type
324329
*/

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/Verifier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ public static Failure validateBinaryComparison(BinaryComparison bc) {
507507
}
508508
allowed.add(DataType.IP);
509509
allowed.add(DataType.DATETIME);
510+
allowed.add(DataType.DATE_NANOS);
510511
allowed.add(DataType.VERSION);
511512
allowed.add(DataType.GEO_POINT);
512513
allowed.add(DataType.GEO_SHAPE);

0 commit comments

Comments
 (0)