Skip to content

Commit d8fe1db

Browse files
committed
Merge branch 'main' of https://github.com/elastic/elasticsearch into esql-reranker-boostrap
2 parents 39fe811 + 1c7867d commit d8fe1db

File tree

45 files changed

+704
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+704
-348
lines changed

docs/changelog/120302.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 120302
2+
summary: "ESQL: Enhanced `DATE_TRUNC` with arbitrary intervals"
3+
area: ES|QL
4+
type: enhancement
5+
issues:
6+
- 120094

docs/changelog/125896.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 125896
2+
summary: Support explicit Z/M attributes using WKT geometry
3+
area: Geo
4+
type: enhancement
5+
issues: [123111]

docs/reference/query-languages/esql/_snippets/functions/description/date_trunc.md

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

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

Lines changed: 2 additions & 1 deletion
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/date_trunc.json

Lines changed: 1 addition & 1 deletion
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/date_trunc.md

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

libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class WellKnownText {
4444
public static final String RPAREN = ")";
4545
public static final String COMMA = ",";
4646
public static final String NAN = "NaN";
47+
public static final String Z = "Z";
48+
public static final String M = "M";
4749
public static final int MAX_NESTED_DEPTH = 1000;
4850

4951
private static final String NUMBER = "<NUMBER>";
@@ -440,7 +442,8 @@ public static Geometry fromWKT(GeometryValidator validator, boolean coerce, Stri
440442
*/
441443
private static Geometry parseGeometry(StreamTokenizer stream, boolean coerce, int depth) throws IOException, ParseException {
442444
final String type = nextWord(stream).toLowerCase(Locale.ROOT);
443-
return switch (type) {
445+
final boolean isExplicitlySpecifiesZorM = isZOrMNext(stream);
446+
Geometry geometry = switch (type) {
444447
case "point" -> parsePoint(stream);
445448
case "multipoint" -> parseMultiPoint(stream);
446449
case "linestring" -> parseLine(stream);
@@ -453,6 +456,16 @@ private static Geometry parseGeometry(StreamTokenizer stream, boolean coerce, in
453456
parseCircle(stream);
454457
default -> throw new IllegalArgumentException("Unknown geometry type: " + type);
455458
};
459+
checkZorMAttribute(isExplicitlySpecifiesZorM, geometry.hasZ());
460+
return geometry;
461+
}
462+
463+
private static void checkZorMAttribute(boolean isExplicitlySpecifiesZorM, boolean hasZ) {
464+
if (isExplicitlySpecifiesZorM && hasZ == false) {
465+
throw new IllegalArgumentException(
466+
"When specifying 'Z' or 'M', coordinates must include three values. Only two coordinates were provided"
467+
);
468+
}
456469
}
457470

458471
private static GeometryCollection<Geometry> parseGeometryCollection(StreamTokenizer stream, boolean coerce, int depth)
@@ -710,6 +723,21 @@ private static boolean isNumberNext(StreamTokenizer stream) throws IOException {
710723
return type == StreamTokenizer.TT_WORD;
711724
}
712725

726+
private static boolean isZOrMNext(StreamTokenizer stream) {
727+
String token;
728+
try {
729+
token = nextWord(stream);
730+
if (token.equals(Z) || token.equals(M)) {
731+
return true;
732+
}
733+
stream.pushBack();
734+
return false;
735+
} catch (ParseException | IOException e) {
736+
return false;
737+
}
738+
739+
}
740+
713741
private static String nextEmptyOrOpen(StreamTokenizer stream) throws IOException, ParseException {
714742
final String next = nextWord(stream);
715743
if (next.equals(EMPTY) || next.equals(LPAREN)) {

libs/geo/src/test/java/org/elasticsearch/geometry/BaseGeometryTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
abstract class BaseGeometryTestCase<T extends Geometry> extends AbstractWireTestCase<T> {
2323

24+
public static final String ZorMMustIncludeThreeValuesMsg =
25+
"When specifying 'Z' or 'M', coordinates must include three values. Only two coordinates were provided";
26+
2427
@Override
2528
protected final T createTestInstance() {
2629
boolean hasAlt = randomBoolean();

libs/geo/src/test/java/org/elasticsearch/geometry/GeometryCollectionTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.text.ParseException;
2020
import java.util.Arrays;
2121
import java.util.Collections;
22+
import java.util.List;
2223

2324
import static org.hamcrest.Matchers.containsString;
2425

@@ -93,6 +94,35 @@ private int countNestedGeometryCollections(GeometryCollection<?> geometry) {
9394
return count;
9495
}
9596

97+
public void testParseGeometryCollectionZorMWithThreeCoordinates() throws IOException, ParseException {
98+
GeometryValidator validator = GeographyValidator.instance(true);
99+
100+
GeometryCollection<Geometry> expected = new GeometryCollection<>(
101+
Arrays.asList(
102+
new Point(20.0, 10.0, 100.0),
103+
new Line(new double[] { 10.0, 20.0 }, new double[] { 5.0, 15.0 }, new double[] { 50.0, 150.0 })
104+
)
105+
);
106+
107+
String point = "(POINT Z (20.0 10.0 100.0)";
108+
String lineString = "LINESTRING M (10.0 5.0 50.0, 20.0 15.0 150.0)";
109+
assertEquals(expected, WellKnownText.fromWKT(validator, true, "GEOMETRYCOLLECTION Z " + point + ", " + lineString + ")"));
110+
111+
assertEquals(expected, WellKnownText.fromWKT(validator, true, "GEOMETRYCOLLECTION M " + point + ", " + lineString + ")"));
112+
}
113+
114+
public void testParseGeometryCollectionZorMWithTwoCoordinatesThrowsException() {
115+
GeometryValidator validator = GeographyValidator.instance(true);
116+
List<String> gcWkt = List.of(
117+
"GEOMETRYCOLLECTION Z (POINT (20.0 10.0), LINESTRING (10.0 5.0, 20.0 15.0))",
118+
"GEOMETRYCOLLECTION M (POINT (20.0 10.0), LINESTRING (10.0 5.0, 20.0 15.0))"
119+
);
120+
for (String gc : gcWkt) {
121+
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> WellKnownText.fromWKT(validator, true, gc));
122+
assertEquals(ZorMMustIncludeThreeValuesMsg, ex.getMessage());
123+
}
124+
}
125+
96126
@Override
97127
protected GeometryCollection<Geometry> mutateInstance(GeometryCollection<Geometry> instance) {
98128
return null;// TODO implement https://github.com/elastic/elasticsearch/issues/25929

libs/geo/src/test/java/org/elasticsearch/geometry/LineTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.IOException;
1919
import java.text.ParseException;
20+
import java.util.List;
2021

2122
public class LineTests extends BaseGeometryTestCase<Line> {
2223
@Override
@@ -82,6 +83,25 @@ public void testWKTValidation() {
8283
assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage());
8384
}
8485

86+
public void testParseLineZorMWithThreeCoordinates() throws IOException, ParseException {
87+
GeometryValidator validator = GeographyValidator.instance(true);
88+
89+
Line expectedZ = new Line(new double[] { 20.0, 30.0 }, new double[] { 10.0, 15.0 }, new double[] { 100.0, 200.0 });
90+
assertEquals(expectedZ, WellKnownText.fromWKT(validator, true, "LINESTRING Z (20.0 10.0 100.0, 30.0 15.0 200.0)"));
91+
92+
Line expectedM = new Line(new double[] { 20.0, 30.0 }, new double[] { 10.0, 15.0 }, new double[] { 100.0, 200.0 });
93+
assertEquals(expectedM, WellKnownText.fromWKT(validator, true, "LINESTRING M (20.0 10.0 100.0, 30.0 15.0 200.0)"));
94+
}
95+
96+
public void testParseLineZorMWithTwoCoordinatesThrowsException() {
97+
GeometryValidator validator = GeographyValidator.instance(true);
98+
List<String> linesWkt = List.of("LINESTRING Z (20.0 10.0, 30.0 15.0)", "LINESTRING M (20.0 10.0, 30.0 15.0)");
99+
for (String line : linesWkt) {
100+
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> WellKnownText.fromWKT(validator, true, line));
101+
assertEquals(ZorMMustIncludeThreeValuesMsg, ex.getMessage());
102+
}
103+
}
104+
85105
@Override
86106
protected Line mutateInstance(Line instance) {
87107
return null;// TODO implement https://github.com/elastic/elasticsearch/issues/25929

0 commit comments

Comments
 (0)