Skip to content

Commit fa62e94

Browse files
[8.18] backport LOOKUP JOIN type tests and DATE_NANOS (elastic#127962) (elastic#129138)
* Integration tests for LOOKUP JOIN over wider range of data types (elastic#126150) This test suite tests the lookup join functionality in ESQL with various data types. For each pair of types being tested, it builds a main index called "index" containing a single document with as many fields as types being tested on the left of the pair, and then creates that many other lookup indexes, each with a single document containing exactly two fields: the field to join on, and a field to return. The assertion is that for valid combinations, the return result should exist, and for invalid combinations an exception should be thrown. If no exception is thrown, and no result is returned, our validation rules are not aligned with the internal behaviour (ie. a bug). Since the `LOOKUP JOIN` command requires the match field name to be the same between the main index and the lookup index, we will have field names that correctly represent the type of the field in the main index, but not the type of the field in the lookup index. This can be confusing, but it is important to remember that the field names are not the same as the types. * Just use one lookup-settings file This change simplifies backports from 9.x branches where these changes were done as part of other work. * Support DATE_NANOS in LOOKUP JOIN (elastic#127962) We reported in elastic#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 compile error after backport * Update docs/changelog/129138.yaml * SEMANTIC_TEXT was removed in later PRs, so not really testable in 8.18. * Delete docs/changelog/129138.yaml * Removed incorrectly added changelog This is a backport
1 parent 5425db6 commit fa62e94

File tree

15 files changed

+1038
-85
lines changed

15 files changed

+1038
-85
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: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,12 @@ public DateFieldType(String name) {
473473
this(name, true, true, false, true, DEFAULT_DATE_TIME_FORMATTER, Resolution.MILLISECONDS, null, null, Collections.emptyMap());
474474
}
475475

476+
public DateFieldType(String name, boolean isIndexed, Resolution resolution) {
477+
this(name, isIndexed, isIndexed, false, true, DEFAULT_DATE_TIME_FORMATTER, resolution, null, null, Collections.emptyMap());
478+
}
479+
476480
public DateFieldType(String name, boolean isIndexed) {
477-
this(
478-
name,
479-
isIndexed,
480-
isIndexed,
481-
false,
482-
true,
483-
DEFAULT_DATE_TIME_FORMATTER,
484-
Resolution.MILLISECONDS,
485-
null,
486-
null,
487-
Collections.emptyMap()
488-
);
481+
this(name, isIndexed, Resolution.MILLISECONDS);
489482
}
490483

491484
public DateFieldType(String name, DateFormatter dateFormatter) {
@@ -699,6 +692,54 @@ public static long parseToLong(
699692
return resolution.convert(dateParser.parse(BytesRefs.toString(value), now, roundUp, zone));
700693
}
701694

695+
/**
696+
* Similar to the {@link DateFieldType#termQuery} method, but works on dates that are already parsed to a long
697+
* in the same precision as the field mapper.
698+
*/
699+
public Query equalityQuery(Long value, @Nullable SearchExecutionContext context) {
700+
return rangeQuery(value, value, true, true, context);
701+
}
702+
703+
/**
704+
* Similar to the existing
705+
* {@link DateFieldType#rangeQuery(Object, Object, boolean, boolean, ShapeRelation, ZoneId, DateMathParser, SearchExecutionContext)}
706+
* method, but works on dates that are already parsed to a long in the same precision as the field mapper.
707+
*/
708+
public Query rangeQuery(
709+
Long lowerTerm,
710+
Long upperTerm,
711+
boolean includeLower,
712+
boolean includeUpper,
713+
SearchExecutionContext context
714+
) {
715+
failIfNotIndexedNorDocValuesFallback(context);
716+
long l, u;
717+
if (lowerTerm == null) {
718+
l = Long.MIN_VALUE;
719+
} else {
720+
l = (includeLower == false) ? lowerTerm + 1 : lowerTerm;
721+
}
722+
if (upperTerm == null) {
723+
u = Long.MAX_VALUE;
724+
} else {
725+
u = (includeUpper == false) ? upperTerm - 1 : upperTerm;
726+
}
727+
Query query;
728+
if (isIndexed()) {
729+
query = LongPoint.newRangeQuery(name(), l, u);
730+
if (hasDocValues()) {
731+
Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
732+
query = new IndexOrDocValuesQuery(query, dvQuery);
733+
}
734+
} else {
735+
query = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
736+
}
737+
if (hasDocValues() && context.indexSortedOnField(name())) {
738+
query = new IndexSortSortedNumericDocValuesRangeQuery(name(), l, u, query);
739+
}
740+
return query;
741+
}
742+
702743
@Override
703744
public Query distanceFeatureQuery(Object origin, String pivot, SearchExecutionContext context) {
704745
failIfNotIndexedNorDocValuesFallback(context);

0 commit comments

Comments
 (0)