forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ESQL: ST_EXTENT_AGG optimize envelope extraction from doc-values for …
…Cartesian_shape (elastic#118802) When we cartesian shapes to Lucene, we encode additional information in the binary, including the extent, so we can read the extent directly from the binary encoding instead of (re-)computing it per shape, and replace the (possibly very complicated) shape with a rectangle. At the moment, this is only done for Cartesian shapes, since for Geo shapes, we need to take dateline wrapping into account, which means it can't be directly encoded as a rectangle. We will deal with geo shapes in a future PR.
- Loading branch information
1 parent
d4f5c1b
commit 8ca9989
Showing
28 changed files
with
872 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 118802 | ||
summary: ST_EXTENT_AGG optimize envelope extraction from doc-values for cartesian_shape | ||
area: "ES|QL" | ||
type: enhancement | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...r/src/test/java/org/elasticsearch/index/mapper/AbstractShapeGeometryFieldMapperTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.index.DirectoryReader; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.tests.index.RandomIndexWriter; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.geo.Orientation; | ||
import org.elasticsearch.geo.GeometryTestUtils; | ||
import org.elasticsearch.geo.ShapeTestUtils; | ||
import org.elasticsearch.geometry.Geometry; | ||
import org.elasticsearch.geometry.Rectangle; | ||
import org.elasticsearch.geometry.utils.SpatialEnvelopeVisitor; | ||
import org.elasticsearch.lucene.spatial.BinaryShapeDocValuesField; | ||
import org.elasticsearch.lucene.spatial.CartesianShapeIndexer; | ||
import org.elasticsearch.lucene.spatial.CoordinateEncoder; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.test.hamcrest.RectangleMatcher; | ||
import org.elasticsearch.test.hamcrest.WellKnownBinaryBytesRefMatcher; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.IntStream; | ||
|
||
public class AbstractShapeGeometryFieldMapperTests extends ESTestCase { | ||
public void testCartesianBoundsBlockLoader() throws IOException { | ||
testBoundsBlockLoaderAux( | ||
CoordinateEncoder.CARTESIAN, | ||
() -> ShapeTestUtils.randomGeometryWithoutCircle(0, false), | ||
CartesianShapeIndexer::new, | ||
SpatialEnvelopeVisitor::visitCartesian | ||
); | ||
} | ||
|
||
// TODO when we turn this optimization on for geo, this test should pass. | ||
public void ignoreTestGeoBoundsBlockLoader() throws IOException { | ||
testBoundsBlockLoaderAux( | ||
CoordinateEncoder.GEO, | ||
() -> GeometryTestUtils.randomGeometryWithoutCircle(0, false), | ||
field -> new GeoShapeIndexer(Orientation.RIGHT, field), | ||
g -> SpatialEnvelopeVisitor.visitGeo(g, SpatialEnvelopeVisitor.WrapLongitude.WRAP) | ||
); | ||
} | ||
|
||
private void testBoundsBlockLoaderAux( | ||
CoordinateEncoder encoder, | ||
Supplier<Geometry> generator, | ||
Function<String, ShapeIndexer> indexerFactory, | ||
Function<Geometry, Optional<Rectangle>> visitor | ||
) throws IOException { | ||
var geometries = IntStream.range(0, 20).mapToObj(i -> generator.get()).toList(); | ||
var loader = new AbstractShapeGeometryFieldMapper.AbstractShapeGeometryFieldType.BoundsBlockLoader("field", encoder); | ||
try (Directory directory = newDirectory()) { | ||
try (var iw = new RandomIndexWriter(random(), directory)) { | ||
for (Geometry geometry : geometries) { | ||
var shape = new BinaryShapeDocValuesField("field", encoder); | ||
shape.add(indexerFactory.apply("field").indexShape(geometry), geometry); | ||
var doc = new Document(); | ||
doc.add(shape); | ||
iw.addDocument(doc); | ||
} | ||
} | ||
var indices = IntStream.range(0, geometries.size() / 2).map(x -> x * 2).toArray(); | ||
try (DirectoryReader reader = DirectoryReader.open(directory)) { | ||
LeafReaderContext ctx = reader.leaves().get(0); | ||
TestBlock block = (TestBlock) loader.reader(ctx).read(TestBlock.factory(ctx.reader().numDocs()), TestBlock.docs(indices)); | ||
for (int i = 0; i < indices.length; i++) { | ||
var idx = indices[i]; | ||
var geometry = geometries.get(idx); | ||
var geoString = geometry.toString(); | ||
var geometryString = geoString.length() > 200 ? geoString.substring(0, 200) + "..." : geoString; | ||
Rectangle r = visitor.apply(geometry).get(); | ||
assertThat( | ||
Strings.format("geometries[%d] ('%s') wasn't extracted correctly", idx, geometryString), | ||
(BytesRef) block.get(i), | ||
WellKnownBinaryBytesRefMatcher.encodes(RectangleMatcher.closeToFloat(r, 1e-3, encoder)) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.