Skip to content

Commit fe5700c

Browse files
[8.19] Support DATE_NANOS in LOOKUP JOIN (#127962) (#128957)
* Support DATE_NANOS in LOOKUP JOIN (#127962) We reported in #127249, there is no support for DATE_NANOS in LOOKUP JOIN, even though DATETIME is supported. This PR attempts to fix that. The way that date-time was supported in LOOKUP JOIN (and ENRICH) was by using the `DateFieldMapper.DateFieldType.rangeQuery` (hidden behind the `termQuery` function) which internally takes our long values, casts them to Object, renders them to a string, parses that string back into an Instant (with a bunch of fancy and unnecessary checks for date-math, etc.), and then converts that instant back into a long for the actual query. Parts of this complex process are precision aware (ie. differentiate between ms and ns dates), but not the whole process. Simply dividing the original longs by 1_000_000 before passing them in actually works, but obviously looses precision. And the only reason it works anyway is that the date parsing code will accept a string containing a simple number and interpret it as either ms since the epoch, or years if the number is short enough. This does not work for nano-second dates, and in fact is far from ideal for LOOKUP JOIN on dates which does not need to re-parse the values at all. This complex loop only makes sense in the Query DSL, where we can get all kinds of interesting sources of range values, but seems quite crazy for LOOKUP JOIN where we will always provide the join key from a LongBlock (the backing store of the DATE_TIME DataType, and the DATE_NANOS too). So what we do here for DateNanos is provide two new methods to `DateFieldType`: * `equalityQuery(Long, ...)` to replace `termQuery(Object, ...)` * `rangeQuery(Long, Long, ...)` to replace `rangeQuery(Object, Object, ...)` This allows us to pass in already parsed `long` values, and entirely skip the conversion to strings and re-parsing logic. The new methods are based on the original methods, but considerably simplified due to the removal of the complex parsing logic. The reason for both `equalityQuery` and `rangeQuery` is that it mimics the pattern used by the old `termQuery` with delegated directly down to `rangeQuery`. In addition to this, we hope to support range matching in `LOOKUP JOIN` in the near future. * Fix compile error after backport * Fix failing tests after backport
1 parent b88839c commit fe5700c

File tree

14 files changed

+456
-77
lines changed

14 files changed

+456
-77
lines changed

docs/changelog/127962.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 127962
2+
summary: Support DATE_NANOS in LOOKUP JOIN
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 127249

server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public DateFieldType(String name) {
518518
);
519519
}
520520

521-
public DateFieldType(String name, boolean isIndexed) {
521+
public DateFieldType(String name, boolean isIndexed, Resolution resolution) {
522522
this(
523523
name,
524524
isIndexed,
@@ -527,13 +527,17 @@ public DateFieldType(String name, boolean isIndexed) {
527527
true,
528528
false,
529529
DEFAULT_DATE_TIME_FORMATTER,
530-
Resolution.MILLISECONDS,
530+
resolution,
531531
null,
532532
null,
533533
Collections.emptyMap()
534534
);
535535
}
536536

537+
public DateFieldType(String name, boolean isIndexed) {
538+
this(name, isIndexed, Resolution.MILLISECONDS);
539+
}
540+
537541
public DateFieldType(String name, DateFormatter dateFormatter) {
538542
this(name, true, true, false, true, false, dateFormatter, Resolution.MILLISECONDS, null, null, Collections.emptyMap());
539543
}
@@ -745,6 +749,54 @@ public static long parseToLong(
745749
return resolution.convert(dateParser.parse(BytesRefs.toString(value), now, roundUp, zone));
746750
}
747751

752+
/**
753+
* Similar to the {@link DateFieldType#termQuery} method, but works on dates that are already parsed to a long
754+
* in the same precision as the field mapper.
755+
*/
756+
public Query equalityQuery(Long value, @Nullable SearchExecutionContext context) {
757+
return rangeQuery(value, value, true, true, context);
758+
}
759+
760+
/**
761+
* Similar to the existing
762+
* {@link DateFieldType#rangeQuery(Object, Object, boolean, boolean, ShapeRelation, ZoneId, DateMathParser, SearchExecutionContext)}
763+
* method, but works on dates that are already parsed to a long in the same precision as the field mapper.
764+
*/
765+
public Query rangeQuery(
766+
Long lowerTerm,
767+
Long upperTerm,
768+
boolean includeLower,
769+
boolean includeUpper,
770+
SearchExecutionContext context
771+
) {
772+
failIfNotIndexedNorDocValuesFallback(context);
773+
long l, u;
774+
if (lowerTerm == null) {
775+
l = Long.MIN_VALUE;
776+
} else {
777+
l = (includeLower == false) ? lowerTerm + 1 : lowerTerm;
778+
}
779+
if (upperTerm == null) {
780+
u = Long.MAX_VALUE;
781+
} else {
782+
u = (includeUpper == false) ? upperTerm - 1 : upperTerm;
783+
}
784+
Query query;
785+
if (isIndexed()) {
786+
query = LongPoint.newRangeQuery(name(), l, u);
787+
if (hasDocValues()) {
788+
Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
789+
query = new IndexOrDocValuesQuery(query, dvQuery);
790+
}
791+
} else {
792+
query = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
793+
}
794+
if (hasDocValues() && context.indexSortedOnField(name())) {
795+
query = new XIndexSortSortedNumericDocValuesRangeQuery(name(), l, u, query);
796+
}
797+
return query;
798+
}
799+
748800
@Override
749801
public Query distanceFeatureQuery(Object origin, String pivot, SearchExecutionContext context) {
750802
failIfNotIndexedNorDocValuesFallback(context);

0 commit comments

Comments
 (0)