Skip to content

Commit 6d8e6ad

Browse files
authored
[ESQL] date nanos binary comparisons (elastic#111908)
resolves elastic#109992 Nothing fancy here. Nanosecond dates are still longs, and we can just compare them as longs. Please note that, as mentioned in the linked issue, this only supports comparing date nanos to other date nanos, and not comparing to millisecond dates. With the cast functions added in elastic#111850, users can explicitly cast to millisecond dates (or longs) to compare nanos to other things.
1 parent 1ba72e4 commit 6d8e6ad

File tree

12 files changed

+96
-12
lines changed

12 files changed

+96
-12
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/Equals.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Equals extends EsqlBinaryComparison implements Negatable<EsqlBinary
3535
Map.entry(DataType.LONG, EqualsLongsEvaluator.Factory::new),
3636
Map.entry(DataType.UNSIGNED_LONG, EqualsLongsEvaluator.Factory::new),
3737
Map.entry(DataType.DATETIME, EqualsLongsEvaluator.Factory::new),
38+
Map.entry(DataType.DATE_NANOS, EqualsLongsEvaluator.Factory::new),
3839
Map.entry(DataType.GEO_POINT, EqualsGeometriesEvaluator.Factory::new),
3940
Map.entry(DataType.CARTESIAN_POINT, EqualsGeometriesEvaluator.Factory::new),
4041
Map.entry(DataType.GEO_SHAPE, EqualsGeometriesEvaluator.Factory::new),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/GreaterThan.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class GreaterThan extends EsqlBinaryComparison implements Negatable<EsqlB
3434
Map.entry(DataType.LONG, GreaterThanLongsEvaluator.Factory::new),
3535
Map.entry(DataType.UNSIGNED_LONG, GreaterThanLongsEvaluator.Factory::new),
3636
Map.entry(DataType.DATETIME, GreaterThanLongsEvaluator.Factory::new),
37+
Map.entry(DataType.DATE_NANOS, GreaterThanLongsEvaluator.Factory::new),
3738
Map.entry(DataType.KEYWORD, GreaterThanKeywordsEvaluator.Factory::new),
3839
Map.entry(DataType.TEXT, GreaterThanKeywordsEvaluator.Factory::new),
3940
Map.entry(DataType.VERSION, GreaterThanKeywordsEvaluator.Factory::new),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/GreaterThanOrEqual.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class GreaterThanOrEqual extends EsqlBinaryComparison implements Negatabl
3434
Map.entry(DataType.LONG, GreaterThanOrEqualLongsEvaluator.Factory::new),
3535
Map.entry(DataType.UNSIGNED_LONG, GreaterThanOrEqualLongsEvaluator.Factory::new),
3636
Map.entry(DataType.DATETIME, GreaterThanOrEqualLongsEvaluator.Factory::new),
37+
Map.entry(DataType.DATE_NANOS, GreaterThanOrEqualLongsEvaluator.Factory::new),
3738
Map.entry(DataType.KEYWORD, GreaterThanOrEqualKeywordsEvaluator.Factory::new),
3839
Map.entry(DataType.TEXT, GreaterThanOrEqualKeywordsEvaluator.Factory::new),
3940
Map.entry(DataType.VERSION, GreaterThanOrEqualKeywordsEvaluator.Factory::new),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/LessThan.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class LessThan extends EsqlBinaryComparison implements Negatable<EsqlBina
3434
Map.entry(DataType.LONG, LessThanLongsEvaluator.Factory::new),
3535
Map.entry(DataType.UNSIGNED_LONG, LessThanLongsEvaluator.Factory::new),
3636
Map.entry(DataType.DATETIME, LessThanLongsEvaluator.Factory::new),
37+
Map.entry(DataType.DATE_NANOS, LessThanLongsEvaluator.Factory::new),
3738
Map.entry(DataType.KEYWORD, LessThanKeywordsEvaluator.Factory::new),
3839
Map.entry(DataType.TEXT, LessThanKeywordsEvaluator.Factory::new),
3940
Map.entry(DataType.VERSION, LessThanKeywordsEvaluator.Factory::new),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/LessThanOrEqual.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class LessThanOrEqual extends EsqlBinaryComparison implements Negatable<E
3434
Map.entry(DataType.LONG, LessThanOrEqualLongsEvaluator.Factory::new),
3535
Map.entry(DataType.UNSIGNED_LONG, LessThanOrEqualLongsEvaluator.Factory::new),
3636
Map.entry(DataType.DATETIME, LessThanOrEqualLongsEvaluator.Factory::new),
37+
Map.entry(DataType.DATE_NANOS, LessThanOrEqualLongsEvaluator.Factory::new),
3738
Map.entry(DataType.KEYWORD, LessThanOrEqualKeywordsEvaluator.Factory::new),
3839
Map.entry(DataType.TEXT, LessThanOrEqualKeywordsEvaluator.Factory::new),
3940
Map.entry(DataType.VERSION, LessThanOrEqualKeywordsEvaluator.Factory::new),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/NotEquals.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class NotEquals extends EsqlBinaryComparison implements Negatable<EsqlBin
3535
Map.entry(DataType.LONG, NotEqualsLongsEvaluator.Factory::new),
3636
Map.entry(DataType.UNSIGNED_LONG, NotEqualsLongsEvaluator.Factory::new),
3737
Map.entry(DataType.DATETIME, NotEqualsLongsEvaluator.Factory::new),
38+
Map.entry(DataType.DATE_NANOS, NotEqualsLongsEvaluator.Factory::new),
3839
Map.entry(DataType.GEO_POINT, NotEqualsGeometriesEvaluator.Factory::new),
3940
Map.entry(DataType.CARTESIAN_POINT, NotEqualsGeometriesEvaluator.Factory::new),
4041
Map.entry(DataType.GEO_SHAPE, NotEqualsGeometriesEvaluator.Factory::new),

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/EqualsTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public static Iterable<Object[]> parameters() {
116116
)
117117
);
118118
// Datetime
119-
// TODO: I'm surprised this passes. Shouldn't there be a cast from DateTime to Long?
120119
suppliers.addAll(
121120
TestCaseSupplier.forBinaryNotCasting(
122121
"EqualsLongsEvaluator",
@@ -131,6 +130,20 @@ public static Iterable<Object[]> parameters() {
131130
)
132131
);
133132

133+
suppliers.addAll(
134+
TestCaseSupplier.forBinaryNotCasting(
135+
"EqualsLongsEvaluator",
136+
"lhs",
137+
"rhs",
138+
Object::equals,
139+
DataType.BOOLEAN,
140+
TestCaseSupplier.dateNanosCases(),
141+
TestCaseSupplier.dateNanosCases(),
142+
List.of(),
143+
false
144+
)
145+
);
146+
134147
suppliers.addAll(
135148
TestCaseSupplier.stringCases(
136149
Object::equals,
@@ -204,7 +217,7 @@ public static Iterable<Object[]> parameters() {
204217
}
205218

206219
private static String typeErrorString =
207-
"boolean, cartesian_point, cartesian_shape, datetime, double, geo_point, geo_shape, integer, ip, keyword, long, text, "
220+
"boolean, cartesian_point, cartesian_shape, datetime, date_nanos, double, geo_point, geo_shape, integer, ip, keyword, long, text, "
208221
+ "unsigned_long or version";
209222

210223
@Override

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/GreaterThanOrEqualTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public static Iterable<Object[]> parameters() {
106106
)
107107
);
108108
// Datetime
109-
// TODO: I'm surprised this passes. Shouldn't there be a cast from DateTime to Long?
110109
suppliers.addAll(
111110
TestCaseSupplier.forBinaryNotCasting(
112111
"GreaterThanOrEqualLongsEvaluator",
@@ -121,6 +120,20 @@ public static Iterable<Object[]> parameters() {
121120
)
122121
);
123122

123+
suppliers.addAll(
124+
TestCaseSupplier.forBinaryNotCasting(
125+
"GreaterThanOrEqualLongsEvaluator",
126+
"lhs",
127+
"rhs",
128+
(l, r) -> ((Number) l).longValue() >= ((Number) r).longValue(),
129+
DataType.BOOLEAN,
130+
TestCaseSupplier.dateNanosCases(),
131+
TestCaseSupplier.dateNanosCases(),
132+
List.of(),
133+
false
134+
)
135+
);
136+
124137
suppliers.addAll(
125138
TestCaseSupplier.stringCases(
126139
(l, r) -> ((BytesRef) l).compareTo((BytesRef) r) >= 0,
@@ -137,7 +150,7 @@ public static Iterable<Object[]> parameters() {
137150
o,
138151
v,
139152
t,
140-
(l, p) -> "datetime, double, integer, ip, keyword, long, text, unsigned_long or version"
153+
(l, p) -> "date_nanos, datetime, double, integer, ip, keyword, long, text, unsigned_long or version"
141154
)
142155
)
143156
);

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/GreaterThanTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public static Iterable<Object[]> parameters() {
106106
)
107107
);
108108
// Datetime
109-
// TODO: I'm surprised this passes. Shouldn't there be a cast from DateTime to Long?
110109
suppliers.addAll(
111110
TestCaseSupplier.forBinaryNotCasting(
112111
"GreaterThanLongsEvaluator",
@@ -121,6 +120,20 @@ public static Iterable<Object[]> parameters() {
121120
)
122121
);
123122

123+
suppliers.addAll(
124+
TestCaseSupplier.forBinaryNotCasting(
125+
"GreaterThanLongsEvaluator",
126+
"lhs",
127+
"rhs",
128+
(l, r) -> ((Number) l).longValue() > ((Number) r).longValue(),
129+
DataType.BOOLEAN,
130+
TestCaseSupplier.dateNanosCases(),
131+
TestCaseSupplier.dateNanosCases(),
132+
List.of(),
133+
false
134+
)
135+
);
136+
124137
suppliers.addAll(
125138
TestCaseSupplier.stringCases(
126139
(l, r) -> ((BytesRef) l).compareTo((BytesRef) r) > 0,
@@ -137,7 +150,7 @@ public static Iterable<Object[]> parameters() {
137150
o,
138151
v,
139152
t,
140-
(l, p) -> "datetime, double, integer, ip, keyword, long, text, unsigned_long or version"
153+
(l, p) -> "date_nanos, datetime, double, integer, ip, keyword, long, text, unsigned_long or version"
141154
)
142155
)
143156
);

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/LessThanOrEqualTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public static Iterable<Object[]> parameters() {
106106
)
107107
);
108108
// Datetime
109-
// TODO: I'm surprised this passes. Shouldn't there be a cast from DateTime to Long?
110109
suppliers.addAll(
111110
TestCaseSupplier.forBinaryNotCasting(
112111
"LessThanOrEqualLongsEvaluator",
@@ -121,6 +120,20 @@ public static Iterable<Object[]> parameters() {
121120
)
122121
);
123122

123+
suppliers.addAll(
124+
TestCaseSupplier.forBinaryNotCasting(
125+
"LessThanOrEqualLongsEvaluator",
126+
"lhs",
127+
"rhs",
128+
(l, r) -> ((Number) l).longValue() <= ((Number) r).longValue(),
129+
DataType.BOOLEAN,
130+
TestCaseSupplier.dateNanosCases(),
131+
TestCaseSupplier.dateNanosCases(),
132+
List.of(),
133+
false
134+
)
135+
);
136+
124137
suppliers.addAll(
125138
TestCaseSupplier.stringCases(
126139
(l, r) -> ((BytesRef) l).compareTo((BytesRef) r) <= 0,
@@ -137,7 +150,7 @@ public static Iterable<Object[]> parameters() {
137150
o,
138151
v,
139152
t,
140-
(l, p) -> "datetime, double, integer, ip, keyword, long, text, unsigned_long or version"
153+
(l, p) -> "date_nanos, datetime, double, integer, ip, keyword, long, text, unsigned_long or version"
141154
)
142155
)
143156
);

0 commit comments

Comments
 (0)