-
Notifications
You must be signed in to change notification settings - Fork 25.8k
ESQL: ST_EXTENT_AGG optimize envelope extraction from doc-values for cartesian_shape #118802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9ce03ad
a519779
d0c329f
7d75dd9
9055219
e61f7a8
753c8d7
f736839
b6ba18a
3f0b58f
47966cf
a8a2c95
4104176
05116c0
e89600b
e2743ae
72e3e34
d62e37d
f823bc5
71aadca
93e7129
48d8f94
58a491a
bc4a693
c6875bc
7aa6734
1d8ed8f
eff2f67
ebfd723
5553b68
a9d6027
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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: [] |
| 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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter not used
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. Still unused. |
||
| 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for this only looking at every second geometry?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verifying that the block loader extracts data from the correct docs. |
||
| 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)) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ | |
| import org.elasticsearch.index.fielddata.LeafFieldData; | ||
| import org.elasticsearch.index.fieldvisitor.LeafStoredFieldLoader; | ||
| import org.elasticsearch.index.fieldvisitor.StoredFieldLoader; | ||
| import org.elasticsearch.index.mapper.MappedFieldType.FieldExtractPreference; | ||
| import org.elasticsearch.index.query.SearchExecutionContext; | ||
| import org.elasticsearch.index.termvectors.TermVectorsService; | ||
| import org.elasticsearch.index.translog.Translog; | ||
|
|
@@ -87,8 +88,6 @@ | |
| import java.util.stream.IntStream; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
| import static org.elasticsearch.index.mapper.MappedFieldType.FieldExtractPreference.DOC_VALUES; | ||
| import static org.elasticsearch.index.mapper.MappedFieldType.FieldExtractPreference.NONE; | ||
| import static org.elasticsearch.test.MapMatcher.assertMap; | ||
| import static org.hamcrest.Matchers.anyOf; | ||
| import static org.hamcrest.Matchers.contains; | ||
|
|
@@ -1420,7 +1419,7 @@ public BlockReaderSupport(boolean columnAtATimeReader, MapperService mapper, Str | |
| this(columnAtATimeReader, true, mapper, loaderFieldName); | ||
| } | ||
|
|
||
| private BlockLoader getBlockLoader(boolean columnReader) { | ||
| private BlockLoader getBlockLoader(FieldExtractPreference fieldExtractPreference) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this move makes things less clear. In most types there is a clear binary choice between row-reading and column-reading. Only for spatial types is there an actual fieldExtractPreference.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep it this way and change it in the future, after we add more cases such as handling geo. |
||
| SearchLookup searchLookup = new SearchLookup(mapper.mappingLookup().fieldTypesLookup()::get, null, null); | ||
| return mapper.fieldType(loaderFieldName).blockLoader(new MappedFieldType.BlockLoaderContext() { | ||
| @Override | ||
|
|
@@ -1434,8 +1433,8 @@ public IndexSettings indexSettings() { | |
| } | ||
|
|
||
| @Override | ||
| public MappedFieldType.FieldExtractPreference fieldExtractPreference() { | ||
| return columnReader ? DOC_VALUES : NONE; | ||
| public FieldExtractPreference fieldExtractPreference() { | ||
| return fieldExtractPreference; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1493,7 +1492,9 @@ protected final void testBlockLoader( | |
| BlockReaderSupport blockReaderSupport, | ||
| SourceLoader sourceLoader | ||
| ) throws IOException { | ||
| BlockLoader loader = blockReaderSupport.getBlockLoader(columnReader); | ||
| // EXTRACT_SPATIAL_BOUNDS is not currently supported in this test path. | ||
| var fieldExtractPreference = columnReader ? FieldExtractPreference.DOC_VALUES : FieldExtractPreference.NONE; | ||
| BlockLoader loader = blockReaderSupport.getBlockLoader(fieldExtractPreference); | ||
| Function<Object, Object> valuesConvert = loadBlockExpected(blockReaderSupport, columnReader); | ||
| if (valuesConvert == null) { | ||
| assertNull(loader); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.