From 0d0d20db086c65cd676ab3b0a9188a80a27d10de Mon Sep 17 00:00:00 2001 From: Craig Taverner Date: Mon, 18 Dec 2023 11:22:26 +0100 Subject: [PATCH] Remove SpatialPoint from block implementation Using instead double x and y values (or double[] xValues, yValues). This might improve memory usage and performance, but we still need to verify that. --- .../operator/MultivalueDedupeBenchmark.java | 2 +- .../index/mapper/BlockLoader.java | 4 +- .../index/mapper/BlockSourceReader.java | 5 +- .../elasticsearch/index/mapper/TestBlock.java | 7 +- x-pack/plugin/esql/compute/build.gradle | 30 ++--- .../gen/ConvertEvaluatorImplementer.java | 42 +++++-- .../compute/data/BooleanArrayVector.java | 2 +- .../compute/data/BooleanBlock.java | 6 +- .../compute/data/BooleanBlockBuilder.java | 12 +- .../compute/data/BooleanVector.java | 2 +- .../compute/data/BooleanVectorBuilder.java | 4 +- .../data/BooleanVectorFixedBuilder.java | 12 +- .../compute/data/BytesRefBlock.java | 4 +- .../compute/data/BytesRefBlockBuilder.java | 8 +- .../compute/data/ConstantPointVector.java | 28 +++-- .../compute/data/DoubleArrayVector.java | 2 +- .../compute/data/DoubleBlock.java | 6 +- .../compute/data/DoubleBlockBuilder.java | 12 +- .../compute/data/DoubleVector.java | 2 +- .../compute/data/DoubleVectorBuilder.java | 4 +- .../data/DoubleVectorFixedBuilder.java | 12 +- .../compute/data/IntArrayVector.java | 2 +- .../elasticsearch/compute/data/IntBlock.java | 6 +- .../compute/data/IntBlockBuilder.java | 12 +- .../elasticsearch/compute/data/IntVector.java | 2 +- .../compute/data/IntVectorBuilder.java | 4 +- .../compute/data/IntVectorFixedBuilder.java | 12 +- .../compute/data/LongArrayVector.java | 2 +- .../elasticsearch/compute/data/LongBlock.java | 6 +- .../compute/data/LongBlockBuilder.java | 12 +- .../compute/data/LongVector.java | 2 +- .../compute/data/LongVectorBuilder.java | 4 +- .../compute/data/LongVectorFixedBuilder.java | 12 +- .../compute/data/PointArrayBlock.java | 54 +++++---- .../compute/data/PointArrayVector.java | 45 ++++---- .../compute/data/PointBigArrayVector.java | 38 ++++--- .../compute/data/PointBlock.java | 45 ++++---- .../compute/data/PointBlockBuilder.java | 39 ++++--- .../compute/data/PointVector.java | 41 ++++--- .../compute/data/PointVectorBlock.java | 10 +- .../compute/data/PointVectorBuilder.java | 25 +++-- .../compute/data/PointVectorFixedBuilder.java | 27 +++-- .../operator/MultivalueDedupeBytesRef.java | 16 +-- .../operator/MultivalueDedupeDouble.java | 14 +-- .../compute/operator/MultivalueDedupeInt.java | 14 +-- .../operator/MultivalueDedupeLong.java | 14 +-- .../operator/MultivalueDedupePoint.java | 52 +++++---- .../operator/topn/KeyExtractorForPoint.java | 43 ++++--- .../operator/topn/ResultBuilderForPoint.java | 9 +- .../operator/topn/ValueExtractorForPoint.java | 9 +- .../compute/aggregation/X-ArrayState.java.st | 2 +- .../compute/aggregation/X-State.java.st | 4 +- .../compute/data/BlockFactory.java | 31 +++--- .../compute/data/BlockUtils.java | 6 +- .../compute/data/ConstantNullBlock.java | 9 +- .../compute/data/ConstantNullVector.java | 9 +- .../compute/data/X-ArrayBlock.java.st | 71 ++++++++---- .../compute/data/X-ArrayVector.java.st | 55 ++++++--- .../compute/data/X-BigArrayVector.java.st | 60 ++++++++-- .../compute/data/X-Block.java.st | 42 ++++--- .../compute/data/X-BlockBuilder.java.st | 65 +++++++---- .../compute/data/X-ConstantVector.java.st | 43 ++++--- .../compute/data/X-Vector.java.st | 38 ++++--- .../compute/data/X-VectorBlock.java.st | 11 +- .../compute/data/X-VectorBuilder.java.st | 43 +++++-- .../compute/data/X-VectorFixedBuilder.java.st | 44 ++++---- .../compute/operator/BatchEncoder.java | 14 +-- .../operator/X-MultivalueDedupe.java.st | 105 +++++++++++++++--- .../topn/DefaultUnsortableTopNEncoder.java | 6 +- .../operator/topn/SortableTopNEncoder.java | 6 +- .../compute/operator/topn/TopNEncoder.java | 2 +- .../operator/topn/X-KeyExtractor.java.st | 48 +++++--- .../operator/topn/X-ResultBuilder.java.st | 24 ++++ .../operator/topn/X-ValueExtractor.java.st | 10 +- .../compute/data/BasicBlockTests.java | 2 +- .../compute/data/BlockTestUtils.java | 2 +- .../compute/data/VectorBuilderTests.java | 2 +- .../compute/data/VectorFixedBuilderTests.java | 2 +- .../operator/MultivalueDedupeTests.java | 14 ++- .../ToCartesianPointFromLongEvaluator.java | 8 +- .../ToCartesianPointFromStringEvaluator.java | 8 +- .../convert/ToGeoPointFromLongEvaluator.java | 8 +- .../ToGeoPointFromStringEvaluator.java | 8 +- .../ToLongFromCartesianPointEvaluator.java | 8 +- .../convert/ToLongFromGeoPointEvaluator.java | 8 +- .../ToStringFromCartesianPointEvaluator.java | 8 +- .../ToStringFromGeoPointEvaluator.java | 8 +- .../xpack/esql/action/EsqlQueryResponse.java | 7 +- .../esql/action/EsqlQueryResponseTests.java | 9 +- .../xpack/esql/formatter/TextFormatTests.java | 12 +- .../esql/formatter/TextFormatterTests.java | 6 +- .../xpack/ql/util/SpatialCoordinateTypes.java | 7 ++ 92 files changed, 1066 insertions(+), 586 deletions(-) diff --git a/benchmarks/src/main/java/org/elasticsearch/benchmark/compute/operator/MultivalueDedupeBenchmark.java b/benchmarks/src/main/java/org/elasticsearch/benchmark/compute/operator/MultivalueDedupeBenchmark.java index c82b5f29af61c..6e310dc4a9f4e 100644 --- a/benchmarks/src/main/java/org/elasticsearch/benchmark/compute/operator/MultivalueDedupeBenchmark.java +++ b/benchmarks/src/main/java/org/elasticsearch/benchmark/compute/operator/MultivalueDedupeBenchmark.java @@ -167,7 +167,7 @@ public void setup() { Randomness.shuffle(values); builder.beginPositionEntry(); for (SpatialPoint v : values) { - builder.appendPoint(v); + builder.appendPoint(v.getX(), v.getY()); } builder.endPositionEntry(); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/BlockLoader.java b/server/src/main/java/org/elasticsearch/index/mapper/BlockLoader.java index 6a8ada395f9c9..7803791fe7a1e 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/BlockLoader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/BlockLoader.java @@ -461,9 +461,9 @@ interface BytesRefBuilder extends Builder { interface PointBuilder extends Builder { /** - * Appends a SpatialPoint to the current entry. + * Appends a spatial point to the current entry. */ - PointBuilder appendPoint(SpatialPoint value); + PointBuilder appendPoint(double x, double y); } interface DoubleBuilder extends Builder { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/BlockSourceReader.java b/server/src/main/java/org/elasticsearch/index/mapper/BlockSourceReader.java index 39287ce4a8401..5e13c12d1ee88 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/BlockSourceReader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/BlockSourceReader.java @@ -181,14 +181,13 @@ private static class Points extends BlockSourceReader { @Override protected void append(BlockLoader.Builder builder, Object v) { if (v instanceof SpatialPoint point) { - ((BlockLoader.PointBuilder) builder).appendPoint(point); + ((BlockLoader.PointBuilder) builder).appendPoint(point.getX(), point.getY()); } else if (v instanceof String wkt) { try { // TODO: figure out why this is not already happening in the GeoPointFieldMapper Geometry geometry = WellKnownText.fromWKT(GeometryValidator.NOOP, false, wkt); if (geometry instanceof Point point) { - // TODO: perhaps we should not create points for later GC here, and pass in primitives only? - ((BlockLoader.PointBuilder) builder).appendPoint(new SpatialPoint(point.getX(), point.getY())); + ((BlockLoader.PointBuilder) builder).appendPoint(point.getX(), point.getY()); } else { throw new IllegalArgumentException("Cannot convert geometry into point:: " + geometry.type()); } diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/TestBlock.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/TestBlock.java index f33d75fbbd2b0..ef489dbe45569 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/TestBlock.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/TestBlock.java @@ -11,7 +11,6 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.geo.SpatialPoint; import java.io.IOException; import java.io.UncheckedIOException; @@ -62,9 +61,9 @@ public BytesRefsBuilder appendBytesRef(BytesRef value) { public BlockLoader.PointBuilder points(int expectedCount) { class PointsBuilder extends TestBlock.Builder implements BlockLoader.PointBuilder { @Override - public PointsBuilder appendPoint(SpatialPoint value) { - add(value.getX()); - add(value.getY()); + public PointsBuilder appendPoint(double x, double y) { + add(x); + add(y); return this; } } diff --git a/x-pack/plugin/esql/compute/build.gradle b/x-pack/plugin/esql/compute/build.gradle index 7807f3b5ebed4..aef8e8c365ffd 100644 --- a/x-pack/plugin/esql/compute/build.gradle +++ b/x-pack/plugin/esql/compute/build.gradle @@ -30,30 +30,34 @@ spotless { } } -def prop(Type, type, TYPE, BYTES, Array) { +def prop(Type, type, TYPE, BYTES, Array, typeValue, value, typeArrayValues, arrayValues) { return [ "Type" : Type, "type" : type, "TYPE" : TYPE, "BYTES" : BYTES, "Array" : Array, + "typeValue" : typeValue, + "value": value, + "typeArrayValues" : typeArrayValues, + "arrayValues" : arrayValues, - "int" : type == "int" ? "true" : "", - "long" : type == "long" ? "true" : "", - "double" : type == "double" ? "true" : "", - "BytesRef" : type == "BytesRef" ? "true" : "", - "Point" : type == "SpatialPoint" ? "true" : "", - "boolean" : type == "boolean" ? "true" : "", + "int" : Type == "Int" ? "true" : "", + "long" : Type == "Long" ? "true" : "", + "double" : Type == "Double" ? "true" : "", + "BytesRef" : Type == "BytesRef" ? "true" : "", + "Point" : Type == "Point" ? "true" : "", + "boolean" : Type == "Boolean" ? "true" : "", ] } tasks.named('stringTemplates').configure { - var intProperties = prop("Int", "int", "INT", "Integer.BYTES", "IntArray") - var longProperties = prop("Long", "long", "LONG", "Long.BYTES", "LongArray") - var doubleProperties = prop("Double", "double", "DOUBLE", "Double.BYTES", "DoubleArray") - var bytesRefProperties = prop("BytesRef", "BytesRef", "BYTES_REF", "org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF", "") - var pointProperties = prop("Point", "SpatialPoint", "POINT", "16", "ObjectArray") - var booleanProperties = prop("Boolean", "boolean", "BOOLEAN", "Byte.BYTES", "BitArray") + var intProperties = prop("Int", "int", "INT", "Integer.BYTES", "IntArray", "int value", "value", "int[] values", "values") + var longProperties = prop("Long", "long", "LONG", "Long.BYTES", "LongArray", "long value", "value", "long[] values", "values") + var doubleProperties = prop("Double", "double", "DOUBLE", "Double.BYTES", "DoubleArray", "double value", "value", "double[] values", "values") + var bytesRefProperties = prop("BytesRef", "BytesRef", "BYTES_REF", "org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF", "", "BytesRef value", "value", "BytesRefArray values", "values") + var pointProperties = prop("Point", "double", "POINT", "16", "DoubleArray", "double x, double y", "x, y", "double[] xValues, double[] yValues", "xValues, yValues") + var booleanProperties = prop("Boolean", "boolean", "BOOLEAN", "Byte.BYTES", "BitArray", "boolean value", "value", "boolean[] values", "values") // primitive vectors File vectorInputFile = new File("${projectDir}/src/main/java/org/elasticsearch/compute/data/X-Vector.java.st") template { diff --git a/x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/ConvertEvaluatorImplementer.java b/x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/ConvertEvaluatorImplementer.java index f875cd7e6480e..13f58428c08cd 100644 --- a/x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/ConvertEvaluatorImplementer.java +++ b/x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/ConvertEvaluatorImplementer.java @@ -32,6 +32,7 @@ import static org.elasticsearch.compute.gen.Types.DRIVER_CONTEXT; import static org.elasticsearch.compute.gen.Types.EXPRESSION_EVALUATOR; import static org.elasticsearch.compute.gen.Types.EXPRESSION_EVALUATOR_FACTORY; +import static org.elasticsearch.compute.gen.Types.POINT; import static org.elasticsearch.compute.gen.Types.SOURCE; import static org.elasticsearch.compute.gen.Types.VECTOR; import static org.elasticsearch.compute.gen.Types.blockType; @@ -130,11 +131,19 @@ private MethodSpec evalVector() { { catchingWarnExceptions(builder, () -> { var constVectType = blockType(resultType); - builder.addStatement( - "return driverContext.blockFactory().newConstant$TWith($N, positionCount)", - constVectType, - evalValueCall("vector", "0", scratchPadName) - ); + if (resultType.equals(POINT)) { + builder.addStatement("$T point = $N", resultType, evalValueCall("vector", "0", scratchPadName)); + builder.addStatement( + "return driverContext.blockFactory().newConstant$TWith(point.getX(), point.getY(), positionCount)", + constVectType + ); + } else { + builder.addStatement( + "return driverContext.blockFactory().newConstant$TWith($N, positionCount)", + constVectType, + evalValueCall("vector", "0", scratchPadName) + ); + } }, () -> builder.addStatement("return driverContext.blockFactory().newConstantNullBlock(positionCount)")); } builder.endControlFlow(); @@ -148,11 +157,14 @@ private MethodSpec evalVector() { { builder.beginControlFlow("for (int p = 0; p < positionCount; p++)"); { - catchingWarnExceptions( - builder, - () -> builder.addStatement("builder.$L($N)", appendMethod(resultType), evalValueCall("vector", "p", scratchPadName)), - () -> builder.addStatement("builder.appendNull()") - ); + catchingWarnExceptions(builder, () -> { + if (resultType.equals(POINT)) { + builder.addStatement("$T point = $N", resultType, evalValueCall("vector", "p", scratchPadName)); + builder.addStatement("builder.$L(point.getX(), point.getY())", appendMethod(resultType)); + } else { + builder.addStatement("builder.$L($N)", appendMethod(resultType), evalValueCall("vector", "p", scratchPadName)); + } + }, () -> builder.addStatement("builder.appendNull()")); } builder.endControlFlow(); builder.addStatement("return builder.build()"); @@ -215,7 +227,11 @@ private MethodSpec evalBlock() { builder.addStatement("positionOpened = true"); } builder.endControlFlow(); - builder.addStatement("builder.$N(value)", appendMethod); + if (resultType.equals(POINT)) { + builder.addStatement("builder.$N(value.getX(), value.getY())", appendMethod); + } else { + builder.addStatement("builder.$N(value)", appendMethod); + } builder.addStatement("valuesAppended = true"); }, () -> {}); } @@ -265,6 +281,10 @@ private MethodSpec evalValue(boolean forVector) { if (argumentType.equals(BYTES_REF)) { builder.addParameter(BYTES_REF, "scratchPad"); builder.addStatement("$T value = container.$N(index, scratchPad)", argumentType, getMethod(argumentType)); + } else if (argumentType.equals(POINT)) { + builder.addStatement("double x = container.getX(index)"); + builder.addStatement("double y = container.getY(index)"); + builder.addStatement("$T value = new $T(x, y)", argumentType, argumentType); } else { builder.addStatement("$T value = container.$N(index)", argumentType, getMethod(argumentType)); } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanArrayVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanArrayVector.java index 5aa8724eb0ca2..d5a20e57ac4f8 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanArrayVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanArrayVector.java @@ -12,7 +12,7 @@ import java.util.Arrays; /** - * Vector implementation that stores an array of boolean values. + * Vector implementation that stores an array of Boolean values. * This class is generated. Do not edit it. */ public final class BooleanArrayVector extends AbstractVector implements BooleanVector { diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlock.java index 352ee783d8614..b31dc54890979 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlock.java @@ -15,7 +15,7 @@ import java.io.IOException; /** - * Block that stores boolean values. + * Block that stores Boolean values. * This class is generated. Do not edit it. */ public sealed interface BooleanBlock extends Block permits BooleanArrayBlock, BooleanVectorBlock, ConstantNullBlock { @@ -236,14 +236,14 @@ sealed interface Builder extends Block.Builder, BlockLoader.BooleanBuilder permi Builder mvOrdering(Block.MvOrdering mvOrdering); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ @Override Builder appendAllValuesToCurrentPosition(Block block); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ Builder appendAllValuesToCurrentPosition(BooleanBlock block); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlockBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlockBuilder.java index 1c3549c06ca87..8114f71745ed5 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlockBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlockBuilder.java @@ -108,8 +108,8 @@ public BooleanBlockBuilder appendAllValuesToCurrentPosition(BooleanBlock block) for (int p = 0; p < positionCount; p++) { int count = block.getValueCount(p); int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendBoolean(block.getBoolean(i++)); + for (int v = 0; v < count; v++, i++) { + appendBoolean(block.getBoolean(i)); } } } @@ -158,8 +158,8 @@ private void copyFromBlock(BooleanBlock block, int beginInclusive, int endExclus beginPositionEntry(); } int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendBoolean(block.getBoolean(i++)); + for (int v = 0; v < count; v++, i++) { + appendBoolean(block.getBoolean(i)); } if (count > 1) { endPositionEntry(); @@ -187,8 +187,8 @@ public BooleanBlock build() { if (hasNonNullValue && positionCount == 1 && valueCount == 1) { theBlock = blockFactory.newConstantBooleanBlockWith(values[0], 1, estimatedBytes); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } if (isDense() && singleValued()) { theBlock = blockFactory.newBooleanArrayVector(values, positionCount, estimatedBytes).asBlock(); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVector.java index ec4ab8f7def1c..76b954b9332ff 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVector.java @@ -13,7 +13,7 @@ import java.io.IOException; /** - * Vector that stores boolean values. + * Vector that stores Boolean values. * This class is generated. Do not edit it. */ public sealed interface BooleanVector extends Vector permits ConstantBooleanVector, BooleanArrayVector, BooleanBigArrayVector, diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorBuilder.java index effb90267702f..180f673d86a68 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorBuilder.java @@ -54,8 +54,8 @@ public BooleanVector build() { if (valueCount == 1) { vector = blockFactory.newConstantBooleanBlockWith(values[0], 1, estimatedBytes).asVector(); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } vector = blockFactory.newBooleanArrayVector(values, valueCount, estimatedBytes); } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorFixedBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorFixedBuilder.java index 33daff853eecb..0bbc0835fc55d 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorFixedBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanVectorFixedBuilder.java @@ -46,20 +46,24 @@ private static long ramBytesUsed(int size) { ); } + private int valuesLength() { + return values.length; + } + @Override public BooleanVector build() { if (nextIndex < 0) { throw new IllegalStateException("already closed"); } - if (nextIndex != values.length) { - throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); + if (nextIndex != valuesLength()) { + throw new IllegalStateException("expected to write [" + valuesLength() + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; BooleanVector vector; - if (values.length == 1) { + if (valuesLength() == 1) { vector = blockFactory.newConstantBooleanBlockWith(values[0], 1, preAdjustedBytes).asVector(); } else { - vector = blockFactory.newBooleanArrayVector(values, values.length, preAdjustedBytes); + vector = blockFactory.newBooleanArrayVector(values, valuesLength(), preAdjustedBytes); } assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; return vector; diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlock.java index 50611f3e15130..99126a3d0b48b 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlock.java @@ -241,14 +241,14 @@ sealed interface Builder extends Block.Builder, BlockLoader.BytesRefBuilder perm Builder mvOrdering(Block.MvOrdering mvOrdering); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ @Override Builder appendAllValuesToCurrentPosition(Block block); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ Builder appendAllValuesToCurrentPosition(BytesRefBlock block); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlockBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlockBuilder.java index 70e20ac9f1d00..6d8261a701dc8 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlockBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlockBuilder.java @@ -117,8 +117,8 @@ public BytesRefBlockBuilder appendAllValuesToCurrentPosition(BytesRefBlock block for (int p = 0; p < positionCount; p++) { int count = block.getValueCount(p); int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendBytesRef(block.getBytesRef(i++, scratch)); + for (int v = 0; v < count; v++, i++) { + appendBytesRef(block.getBytesRef(i, scratch)); } } } @@ -168,8 +168,8 @@ private void copyFromBlock(BytesRefBlock block, int beginInclusive, int endExclu beginPositionEntry(); } int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendBytesRef(block.getBytesRef(i++, scratch)); + for (int v = 0; v < count; v++, i++) { + appendBytesRef(block.getBytesRef(i, scratch)); } if (count > 1) { endPositionEntry(); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/ConstantPointVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/ConstantPointVector.java index 8c842f8d59056..1a4aac9e7a3c8 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/ConstantPointVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/ConstantPointVector.java @@ -8,33 +8,37 @@ package org.elasticsearch.compute.data; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; /** - * Vector implementation that stores a constant SpatialPoint value. + * Vector implementation that stores a constant double value. * This class is generated. Do not edit it. */ public final class ConstantPointVector extends AbstractVector implements PointVector { static final long RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(ConstantPointVector.class); - private final SpatialPoint value; - + private final double x, y; private final PointBlock block; - public ConstantPointVector(SpatialPoint value, int positionCount) { - this(value, positionCount, BlockFactory.getNonBreakingInstance()); + public ConstantPointVector(double x, double y, int positionCount) { + this(x, y, positionCount, BlockFactory.getNonBreakingInstance()); } - public ConstantPointVector(SpatialPoint value, int positionCount, BlockFactory blockFactory) { + public ConstantPointVector(double x, double y, int positionCount, BlockFactory blockFactory) { super(positionCount, blockFactory); - this.value = value; + this.x = x; + this.y = y; this.block = new PointVectorBlock(this); } @Override - public SpatialPoint getPoint(int position) { - return value; + public double getX(int position) { + return x; + } + + @Override + public double getY(int position) { + return y; } @Override @@ -44,7 +48,7 @@ public PointBlock asBlock() { @Override public PointVector filter(int... positions) { - return new ConstantPointVector(value, positions.length); + return new ConstantPointVector(x, y, positions.length); } @Override @@ -76,7 +80,7 @@ public int hashCode() { } public String toString() { - return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", value=" + value + ']'; + return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", x=" + x + ", y=" + y + ']'; } @Override diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleArrayVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleArrayVector.java index 08e51b0e313d8..97f0bebd44743 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleArrayVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleArrayVector.java @@ -12,7 +12,7 @@ import java.util.Arrays; /** - * Vector implementation that stores an array of double values. + * Vector implementation that stores an array of Double values. * This class is generated. Do not edit it. */ public final class DoubleArrayVector extends AbstractVector implements DoubleVector { diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlock.java index 31d0000d28515..03d1a55863984 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlock.java @@ -15,7 +15,7 @@ import java.io.IOException; /** - * Block that stores double values. + * Block that stores Double values. * This class is generated. Do not edit it. */ public sealed interface DoubleBlock extends Block permits DoubleArrayBlock, DoubleVectorBlock, ConstantNullBlock { @@ -237,14 +237,14 @@ sealed interface Builder extends Block.Builder, BlockLoader.DoubleBuilder permit Builder mvOrdering(Block.MvOrdering mvOrdering); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ @Override Builder appendAllValuesToCurrentPosition(Block block); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ Builder appendAllValuesToCurrentPosition(DoubleBlock block); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlockBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlockBuilder.java index 7781e4c353e8e..8e16637f11583 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlockBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlockBuilder.java @@ -108,8 +108,8 @@ public DoubleBlockBuilder appendAllValuesToCurrentPosition(DoubleBlock block) { for (int p = 0; p < positionCount; p++) { int count = block.getValueCount(p); int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendDouble(block.getDouble(i++)); + for (int v = 0; v < count; v++, i++) { + appendDouble(block.getDouble(i)); } } } @@ -158,8 +158,8 @@ private void copyFromBlock(DoubleBlock block, int beginInclusive, int endExclusi beginPositionEntry(); } int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendDouble(block.getDouble(i++)); + for (int v = 0; v < count; v++, i++) { + appendDouble(block.getDouble(i)); } if (count > 1) { endPositionEntry(); @@ -187,8 +187,8 @@ public DoubleBlock build() { if (hasNonNullValue && positionCount == 1 && valueCount == 1) { theBlock = blockFactory.newConstantDoubleBlockWith(values[0], 1, estimatedBytes); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } if (isDense() && singleValued()) { theBlock = blockFactory.newDoubleArrayVector(values, positionCount, estimatedBytes).asBlock(); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVector.java index acabd0deb17f6..2b65a8026ab19 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVector.java @@ -13,7 +13,7 @@ import java.io.IOException; /** - * Vector that stores double values. + * Vector that stores Double values. * This class is generated. Do not edit it. */ public sealed interface DoubleVector extends Vector permits ConstantDoubleVector, DoubleArrayVector, DoubleBigArrayVector, diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorBuilder.java index f4e7be406e1ca..5f0645196af25 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorBuilder.java @@ -54,8 +54,8 @@ public DoubleVector build() { if (valueCount == 1) { vector = blockFactory.newConstantDoubleBlockWith(values[0], 1, estimatedBytes).asVector(); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } vector = blockFactory.newDoubleArrayVector(values, valueCount, estimatedBytes); } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorFixedBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorFixedBuilder.java index 7353515e8ffd8..c8e891d0dcceb 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorFixedBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleVectorFixedBuilder.java @@ -46,20 +46,24 @@ private static long ramBytesUsed(int size) { ); } + private int valuesLength() { + return values.length; + } + @Override public DoubleVector build() { if (nextIndex < 0) { throw new IllegalStateException("already closed"); } - if (nextIndex != values.length) { - throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); + if (nextIndex != valuesLength()) { + throw new IllegalStateException("expected to write [" + valuesLength() + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; DoubleVector vector; - if (values.length == 1) { + if (valuesLength() == 1) { vector = blockFactory.newConstantDoubleBlockWith(values[0], 1, preAdjustedBytes).asVector(); } else { - vector = blockFactory.newDoubleArrayVector(values, values.length, preAdjustedBytes); + vector = blockFactory.newDoubleArrayVector(values, valuesLength(), preAdjustedBytes); } assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; return vector; diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntArrayVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntArrayVector.java index 9c8c27efa0806..94abbfdbfdcca 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntArrayVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntArrayVector.java @@ -12,7 +12,7 @@ import java.util.Arrays; /** - * Vector implementation that stores an array of int values. + * Vector implementation that stores an array of Int values. * This class is generated. Do not edit it. */ public final class IntArrayVector extends AbstractVector implements IntVector { diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlock.java index 3909d2b6761be..7433ac3197e1e 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlock.java @@ -15,7 +15,7 @@ import java.io.IOException; /** - * Block that stores int values. + * Block that stores Int values. * This class is generated. Do not edit it. */ public sealed interface IntBlock extends Block permits IntArrayBlock, IntVectorBlock, ConstantNullBlock { @@ -236,14 +236,14 @@ sealed interface Builder extends Block.Builder, BlockLoader.IntBuilder permits I Builder mvOrdering(Block.MvOrdering mvOrdering); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ @Override Builder appendAllValuesToCurrentPosition(Block block); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ Builder appendAllValuesToCurrentPosition(IntBlock block); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlockBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlockBuilder.java index 49c3b156ce44b..f2019fbd60b5f 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlockBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlockBuilder.java @@ -108,8 +108,8 @@ public IntBlockBuilder appendAllValuesToCurrentPosition(IntBlock block) { for (int p = 0; p < positionCount; p++) { int count = block.getValueCount(p); int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendInt(block.getInt(i++)); + for (int v = 0; v < count; v++, i++) { + appendInt(block.getInt(i)); } } } @@ -158,8 +158,8 @@ private void copyFromBlock(IntBlock block, int beginInclusive, int endExclusive) beginPositionEntry(); } int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendInt(block.getInt(i++)); + for (int v = 0; v < count; v++, i++) { + appendInt(block.getInt(i)); } if (count > 1) { endPositionEntry(); @@ -187,8 +187,8 @@ public IntBlock build() { if (hasNonNullValue && positionCount == 1 && valueCount == 1) { theBlock = blockFactory.newConstantIntBlockWith(values[0], 1, estimatedBytes); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } if (isDense() && singleValued()) { theBlock = blockFactory.newIntArrayVector(values, positionCount, estimatedBytes).asBlock(); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVector.java index 645288565c431..a6ccba3a1223b 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVector.java @@ -13,7 +13,7 @@ import java.io.IOException; /** - * Vector that stores int values. + * Vector that stores Int values. * This class is generated. Do not edit it. */ public sealed interface IntVector extends Vector permits ConstantIntVector, IntArrayVector, IntBigArrayVector, ConstantNullVector { diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorBuilder.java index 09bbb32cefe79..d8f9940294e5d 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorBuilder.java @@ -54,8 +54,8 @@ public IntVector build() { if (valueCount == 1) { vector = blockFactory.newConstantIntBlockWith(values[0], 1, estimatedBytes).asVector(); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } vector = blockFactory.newIntArrayVector(values, valueCount, estimatedBytes); } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorFixedBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorFixedBuilder.java index a4755addf0b16..721166f86e9d4 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorFixedBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntVectorFixedBuilder.java @@ -46,20 +46,24 @@ private static long ramBytesUsed(int size) { ); } + private int valuesLength() { + return values.length; + } + @Override public IntVector build() { if (nextIndex < 0) { throw new IllegalStateException("already closed"); } - if (nextIndex != values.length) { - throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); + if (nextIndex != valuesLength()) { + throw new IllegalStateException("expected to write [" + valuesLength() + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; IntVector vector; - if (values.length == 1) { + if (valuesLength() == 1) { vector = blockFactory.newConstantIntBlockWith(values[0], 1, preAdjustedBytes).asVector(); } else { - vector = blockFactory.newIntArrayVector(values, values.length, preAdjustedBytes); + vector = blockFactory.newIntArrayVector(values, valuesLength(), preAdjustedBytes); } assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; return vector; diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongArrayVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongArrayVector.java index 0a3ada321d94c..3925b3d132a61 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongArrayVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongArrayVector.java @@ -12,7 +12,7 @@ import java.util.Arrays; /** - * Vector implementation that stores an array of long values. + * Vector implementation that stores an array of Long values. * This class is generated. Do not edit it. */ public final class LongArrayVector extends AbstractVector implements LongVector { diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlock.java index 41ac8f7237f64..7d484d84a5278 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlock.java @@ -15,7 +15,7 @@ import java.io.IOException; /** - * Block that stores long values. + * Block that stores Long values. * This class is generated. Do not edit it. */ public sealed interface LongBlock extends Block permits LongArrayBlock, LongVectorBlock, ConstantNullBlock { @@ -237,14 +237,14 @@ sealed interface Builder extends Block.Builder, BlockLoader.LongBuilder permits Builder mvOrdering(Block.MvOrdering mvOrdering); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ @Override Builder appendAllValuesToCurrentPosition(Block block); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ Builder appendAllValuesToCurrentPosition(LongBlock block); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlockBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlockBuilder.java index 1692c4cff6a57..eb1a859132b21 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlockBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlockBuilder.java @@ -108,8 +108,8 @@ public LongBlockBuilder appendAllValuesToCurrentPosition(LongBlock block) { for (int p = 0; p < positionCount; p++) { int count = block.getValueCount(p); int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendLong(block.getLong(i++)); + for (int v = 0; v < count; v++, i++) { + appendLong(block.getLong(i)); } } } @@ -158,8 +158,8 @@ private void copyFromBlock(LongBlock block, int beginInclusive, int endExclusive beginPositionEntry(); } int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendLong(block.getLong(i++)); + for (int v = 0; v < count; v++, i++) { + appendLong(block.getLong(i)); } if (count > 1) { endPositionEntry(); @@ -187,8 +187,8 @@ public LongBlock build() { if (hasNonNullValue && positionCount == 1 && valueCount == 1) { theBlock = blockFactory.newConstantLongBlockWith(values[0], 1, estimatedBytes); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } if (isDense() && singleValued()) { theBlock = blockFactory.newLongArrayVector(values, positionCount, estimatedBytes).asBlock(); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVector.java index a312d7aeab0cc..735e0be1b7da9 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVector.java @@ -13,7 +13,7 @@ import java.io.IOException; /** - * Vector that stores long values. + * Vector that stores Long values. * This class is generated. Do not edit it. */ public sealed interface LongVector extends Vector permits ConstantLongVector, LongArrayVector, LongBigArrayVector, ConstantNullVector { diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorBuilder.java index eb4e54781a020..3e389525821ca 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorBuilder.java @@ -54,8 +54,8 @@ public LongVector build() { if (valueCount == 1) { vector = blockFactory.newConstantLongBlockWith(values[0], 1, estimatedBytes).asVector(); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } vector = blockFactory.newLongArrayVector(values, valueCount, estimatedBytes); } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorFixedBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorFixedBuilder.java index 4a11012e769d8..f8a129e80ac82 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorFixedBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongVectorFixedBuilder.java @@ -46,20 +46,24 @@ private static long ramBytesUsed(int size) { ); } + private int valuesLength() { + return values.length; + } + @Override public LongVector build() { if (nextIndex < 0) { throw new IllegalStateException("already closed"); } - if (nextIndex != values.length) { - throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); + if (nextIndex != valuesLength()) { + throw new IllegalStateException("expected to write [" + valuesLength() + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; LongVector vector; - if (values.length == 1) { + if (valuesLength() == 1) { vector = blockFactory.newConstantLongBlockWith(values[0], 1, preAdjustedBytes).asVector(); } else { - vector = blockFactory.newLongArrayVector(values, values.length, preAdjustedBytes); + vector = blockFactory.newLongArrayVector(values, valuesLength(), preAdjustedBytes); } assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; return vector; diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayBlock.java index df9d76018b7ba..56148ab48a9ed 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayBlock.java @@ -8,31 +8,34 @@ package org.elasticsearch.compute.data; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; import java.util.Arrays; import java.util.BitSet; -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_ARRAY_HEADER; -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF; -import static org.apache.lucene.util.RamUsageEstimator.alignObjectSize; - /** - * Block implementation that stores an array of SpatialPoint. + * Block implementation that stores an array of double. * This class is generated. Do not edit it. */ public final class PointArrayBlock extends AbstractArrayBlock implements PointBlock { private static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(PointArrayBlock.class); - private final SpatialPoint[] values; + private final double[] xValues, yValues; - public PointArrayBlock(SpatialPoint[] values, int positionCount, int[] firstValueIndexes, BitSet nulls, MvOrdering mvOrdering) { - this(values, positionCount, firstValueIndexes, nulls, mvOrdering, BlockFactory.getNonBreakingInstance()); + public PointArrayBlock( + double[] xValues, + double[] yValues, + int positionCount, + int[] firstValueIndexes, + BitSet nulls, + MvOrdering mvOrdering + ) { + this(xValues, yValues, positionCount, firstValueIndexes, nulls, mvOrdering, BlockFactory.getNonBreakingInstance()); } public PointArrayBlock( - SpatialPoint[] values, + double[] xValues, + double[] yValues, int positionCount, int[] firstValueIndexes, BitSet nulls, @@ -40,7 +43,8 @@ public PointArrayBlock( BlockFactory blockFactory ) { super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory); - this.values = values; + this.xValues = xValues; + this.yValues = yValues; } @Override @@ -49,8 +53,13 @@ public PointVector asVector() { } @Override - public SpatialPoint getPoint(int valueIndex) { - return values[valueIndex]; + public double getX(int valueIndex) { + return xValues[valueIndex]; + } + + @Override + public double getY(int valueIndex) { + return yValues[valueIndex]; } @Override @@ -64,11 +73,11 @@ public PointBlock filter(int... positions) { int valueCount = getValueCount(pos); int first = getFirstValueIndex(pos); if (valueCount == 1) { - builder.appendPoint(getPoint(getFirstValueIndex(pos))); + builder.appendPoint(getX(getFirstValueIndex(pos)), getY(getFirstValueIndex(pos))); } else { builder.beginPositionEntry(); for (int c = 0; c < valueCount; c++) { - builder.appendPoint(getPoint(first + c)); + builder.appendPoint(getX(first + c), getY(first + c)); } builder.endPositionEntry(); } @@ -98,22 +107,21 @@ public PointBlock expand() { int first = getFirstValueIndex(pos); int end = first + getValueCount(pos); for (int i = first; i < end; i++) { - builder.appendPoint(getPoint(i)); + builder.appendPoint(getX(i), getY(i)); } } return builder.mvOrdering(MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING).build(); } } - public static long ramBytesEstimated(SpatialPoint[] values, int[] firstValueIndexes, BitSet nullsMask) { - long valuesEstimate = (long) NUM_BYTES_ARRAY_HEADER + ((long) Long.BYTES * 2 + (long) NUM_BYTES_OBJECT_REF) * values.length; - return BASE_RAM_BYTES_USED + alignObjectSize(valuesEstimate) + BlockRamUsageEstimator.sizeOf(firstValueIndexes) + public static long ramBytesEstimated(double[] values, int[] firstValueIndexes, BitSet nullsMask) { + return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values) * 2 + BlockRamUsageEstimator.sizeOf(firstValueIndexes) + BlockRamUsageEstimator.sizeOfBitSet(nullsMask); } @Override public long ramBytesUsed() { - return ramBytesEstimated(values, firstValueIndexes, nullsMask); + return ramBytesEstimated(xValues, firstValueIndexes, nullsMask); } @Override @@ -136,8 +144,10 @@ public String toString() { + getPositionCount() + ", mvOrdering=" + mvOrdering() - + ", values=" - + Arrays.toString(values) + + ", x=" + + Arrays.toString(xValues) + + ", y=" + + Arrays.toString(yValues) + ']'; } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayVector.java index adfa118a63b6d..c385ca1ab8107 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointArrayVector.java @@ -8,33 +8,29 @@ package org.elasticsearch.compute.data; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; import java.util.Arrays; -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_ARRAY_HEADER; -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF; -import static org.apache.lucene.util.RamUsageEstimator.alignObjectSize; - /** - * Vector implementation that stores an array of SpatialPoint values. + * Vector implementation that stores an array of Point values. * This class is generated. Do not edit it. */ public final class PointArrayVector extends AbstractVector implements PointVector { static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(PointArrayVector.class); - private final SpatialPoint[] values; + private final double[] xValues, yValues; private final PointBlock block; - public PointArrayVector(SpatialPoint[] values, int positionCount) { - this(values, positionCount, BlockFactory.getNonBreakingInstance()); + public PointArrayVector(double[] xValues, double[] yValues, int positionCount) { + this(xValues, yValues, positionCount, BlockFactory.getNonBreakingInstance()); } - public PointArrayVector(SpatialPoint[] values, int positionCount, BlockFactory blockFactory) { + public PointArrayVector(double[] xValues, double[] yValues, int positionCount, BlockFactory blockFactory) { super(positionCount, blockFactory); - this.values = values; + this.xValues = xValues; + this.yValues = yValues; this.block = new PointVectorBlock(this); } @@ -44,8 +40,13 @@ public PointBlock asBlock() { } @Override - public SpatialPoint getPoint(int position) { - return values[position]; + public double getX(int position) { + return xValues[position]; + } + + @Override + public double getY(int position) { + return yValues[position]; } @Override @@ -62,20 +63,19 @@ public boolean isConstant() { public PointVector filter(int... positions) { try (PointVector.Builder builder = blockFactory().newPointVectorBuilder(positions.length)) { for (int pos : positions) { - builder.appendPoint(values[pos]); + builder.appendPoint(xValues[pos], yValues[pos]); } return builder.build(); } } - public static long ramBytesEstimated(SpatialPoint[] values) { - long valuesEstimate = (long) NUM_BYTES_ARRAY_HEADER + ((long) Long.BYTES * 2 + (long) NUM_BYTES_OBJECT_REF) * values.length; - return BASE_RAM_BYTES_USED + alignObjectSize(valuesEstimate); + public static long ramBytesEstimated(double[] xValues, double[] yValues) { + return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(xValues) * 2; } @Override public long ramBytesUsed() { - return ramBytesEstimated(values); + return ramBytesEstimated(xValues, yValues); } @Override @@ -93,7 +93,14 @@ public int hashCode() { @Override public String toString() { - return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", values=" + Arrays.toString(values) + ']'; + return getClass().getSimpleName() + + "[positions=" + + getPositionCount() + + ", x=" + + Arrays.toString(xValues) + + ", y=" + + Arrays.toString(yValues) + + ']'; } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBigArrayVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBigArrayVector.java index 760652e94814f..269777668ebbf 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBigArrayVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBigArrayVector.java @@ -8,8 +8,7 @@ package org.elasticsearch.compute.data; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; -import org.elasticsearch.common.util.ObjectArray; +import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.core.Releasable; /** @@ -20,17 +19,18 @@ public final class PointBigArrayVector extends AbstractVector implements PointVe private static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(PointBigArrayVector.class); - private final ObjectArray values; + private final DoubleArray xValues, yValues; private final PointBlock block; - public PointBigArrayVector(ObjectArray values, int positionCount) { - this(values, positionCount, BlockFactory.getNonBreakingInstance()); + public PointBigArrayVector(DoubleArray xValues, DoubleArray yValues, int positionCount) { + this(xValues, yValues, positionCount, BlockFactory.getNonBreakingInstance()); } - public PointBigArrayVector(ObjectArray values, int positionCount, BlockFactory blockFactory) { + public PointBigArrayVector(DoubleArray xValues, DoubleArray yValues, int positionCount, BlockFactory blockFactory) { super(positionCount, blockFactory); - this.values = values; + this.xValues = xValues; + this.yValues = yValues; this.block = new PointVectorBlock(this); } @@ -40,8 +40,13 @@ public PointBlock asBlock() { } @Override - public SpatialPoint getPoint(int position) { - return values.get(position); + public double getX(int position) { + return xValues.get(position); + } + + @Override + public double getY(int position) { + return yValues.get(position); } @Override @@ -56,17 +61,19 @@ public boolean isConstant() { @Override public long ramBytesUsed() { - return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values); + return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(xValues) * 2; } @Override public PointVector filter(int... positions) { var blockFactory = blockFactory(); - final ObjectArray filtered = blockFactory.bigArrays().newObjectArray(positions.length); + final DoubleArray xFiltered = blockFactory.bigArrays().newDoubleArray(positions.length); + final DoubleArray yFiltered = blockFactory.bigArrays().newDoubleArray(positions.length); for (int i = 0; i < positions.length; i++) { - filtered.set(i, values.get(positions[i])); + xFiltered.set(i, xValues.get(positions[i])); + yFiltered.set(i, yValues.get(positions[i])); } - return new PointBigArrayVector(filtered, positions.length, blockFactory); + return new PointBigArrayVector(xFiltered, yFiltered, positions.length, blockFactory); } @Override @@ -75,7 +82,8 @@ public void close() { throw new IllegalStateException("can't release already released vector [" + this + "]"); } released = true; - values.close(); + xValues.close(); + yValues.close(); } @Override @@ -93,6 +101,6 @@ public int hashCode() { @Override public String toString() { - return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", values=" + values + ']'; + return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", x=" + xValues + ", y=" + yValues + ']'; } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlock.java index 81c22b6325b8e..554f4ed19db7d 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlock.java @@ -16,21 +16,27 @@ import java.io.IOException; /** - * Block that stores SpatialPoint values. + * Block that stores Point values. * This class is generated. Do not edit it. */ public sealed interface PointBlock extends Block permits PointArrayBlock, PointVectorBlock, ConstantNullBlock { /** - * Retrieves the SpatialPoint value stored at the given value index. + * Retrieves the double x, double y stored at the given value index. * *

Values for a given position are between getFirstValueIndex(position) (inclusive) and * getFirstValueIndex(position) + getValueCount(position) (exclusive). * * @param valueIndex the value index - * @return the data value (as a SpatialPoint) + * @return the data value (as a double) */ - SpatialPoint getPoint(int valueIndex); + double getX(int valueIndex); + + double getY(int valueIndex); + + default SpatialPoint getPoint(int valueIndex) { + return new SpatialPoint(getX(valueIndex), getY(valueIndex)); + } @Override PointVector asVector(); @@ -63,7 +69,7 @@ private static PointBlock readFrom(BlockStreamInput in) throws IOException { final int valueCount = in.readVInt(); builder.beginPositionEntry(); for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) { - builder.appendPoint(new SpatialPoint(in.readDouble(), in.readDouble())); + builder.appendPoint(in.readDouble(), in.readDouble()); } builder.endPositionEntry(); } @@ -89,9 +95,8 @@ default void writeTo(StreamOutput out) throws IOException { final int valueCount = getValueCount(pos); out.writeVInt(valueCount); for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) { - SpatialPoint point = getPoint(getFirstValueIndex(pos) + valueIndex); - out.writeDouble(point.getX()); - out.writeDouble(point.getY()); + out.writeDouble(getX(getFirstValueIndex(pos) + valueIndex)); + out.writeDouble(getY(getFirstValueIndex(pos) + valueIndex)); } } } @@ -136,7 +141,8 @@ static boolean equals(PointBlock block1, PointBlock block2) { final int b1ValueIdx = block1.getFirstValueIndex(pos); final int b2ValueIdx = block2.getFirstValueIndex(pos); for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) { - if (block1.getPoint(b1ValueIdx + valueIndex).equals(block2.getPoint(b2ValueIdx + valueIndex)) == false) { + if (block1.getX(b1ValueIdx + valueIndex) != block2.getX(b2ValueIdx + valueIndex) + || block1.getY(b1ValueIdx + valueIndex) != block2.getY(b2ValueIdx + valueIndex)) { return false; } } @@ -162,10 +168,9 @@ static int hash(PointBlock block) { result = 31 * result + valueCount; final int firstValueIdx = block.getFirstValueIndex(pos); for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) { - SpatialPoint value = block.getPoint(firstValueIdx + valueIndex); - long element = Double.doubleToLongBits(value.getX()); + long element = Double.doubleToLongBits(block.getX(firstValueIdx + valueIndex)); result = 31 * result + (int) (element ^ (element >>> 32)); - element = Double.doubleToLongBits(value.getY()); + element = Double.doubleToLongBits(block.getY(firstValueIdx + valueIndex)); result = 31 * result + (int) (element ^ (element >>> 32)); } } @@ -198,8 +203,8 @@ static Builder newBlockBuilder(int estimatedSize, BlockFactory blockFactory) { */ // Eventually, we want to remove this entirely, always passing an explicit BlockFactory @Deprecated - static PointBlock newConstantBlockWith(SpatialPoint value, int positions) { - return newConstantBlockWith(value, positions, BlockFactory.getNonBreakingInstance()); + static PointBlock newConstantBlockWith(double x, double y, int positions) { + return newConstantBlockWith(x, y, positions, BlockFactory.getNonBreakingInstance()); } /** @@ -207,8 +212,8 @@ static PointBlock newConstantBlockWith(SpatialPoint value, int positions) { * @deprecated use {@link BlockFactory#newConstantPointBlockWith} */ @Deprecated - static PointBlock newConstantBlockWith(SpatialPoint value, int positions, BlockFactory blockFactory) { - return blockFactory.newConstantPointBlockWith(value, positions); + static PointBlock newConstantBlockWith(double x, double y, int positions, BlockFactory blockFactory) { + return blockFactory.newConstantPointBlockWith(x, y, positions); } /** @@ -216,10 +221,10 @@ static PointBlock newConstantBlockWith(SpatialPoint value, int positions, BlockF */ sealed interface Builder extends Block.Builder, BlockLoader.PointBuilder permits PointBlockBuilder { /** - * Appends a SpatialPoint to the current entry. + * Appends a double to the current entry. */ @Override - Builder appendPoint(SpatialPoint value); + Builder appendPoint(double x, double y); /** * Copy the values in {@code block} from {@code beginInclusive} to @@ -243,14 +248,14 @@ sealed interface Builder extends Block.Builder, BlockLoader.PointBuilder permits Builder mvOrdering(Block.MvOrdering mvOrdering); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ @Override Builder appendAllValuesToCurrentPosition(Block block); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ Builder appendAllValuesToCurrentPosition(PointBlock block); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlockBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlockBuilder.java index 443c0699fa225..6f516d322cd24 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlockBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointBlockBuilder.java @@ -9,7 +9,6 @@ import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.common.breaker.CircuitBreakingException; -import org.elasticsearch.common.geo.SpatialPoint; import java.util.Arrays; @@ -19,19 +18,21 @@ */ final class PointBlockBuilder extends AbstractBlockBuilder implements PointBlock.Builder { - private SpatialPoint[] values; + private double[] xValues, yValues; PointBlockBuilder(int estimatedSize, BlockFactory blockFactory) { super(blockFactory); int initialSize = Math.max(estimatedSize, 2); adjustBreaker(RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + initialSize * elementSize()); - values = new SpatialPoint[initialSize]; + xValues = new double[initialSize]; + yValues = new double[initialSize]; } @Override - public PointBlockBuilder appendPoint(SpatialPoint value) { + public PointBlockBuilder appendPoint(double x, double y) { ensureCapacity(); - values[valueCount] = value; + xValues[valueCount] = x; + yValues[valueCount] = y; hasNonNullValue = true; valueCount++; updatePosition(); @@ -45,12 +46,13 @@ protected int elementSize() { @Override protected int valuesLength() { - return values.length; + return xValues.length; } @Override protected void growValuesArray(int newSize) { - values = Arrays.copyOf(values, newSize); + xValues = Arrays.copyOf(xValues, newSize); + yValues = Arrays.copyOf(yValues, newSize); } @Override @@ -103,14 +105,14 @@ public PointBlockBuilder appendAllValuesToCurrentPosition(PointBlock block) { final PointVector vector = block.asVector(); if (vector != null) { for (int p = 0; p < positionCount; p++) { - appendPoint(vector.getPoint(p)); + appendPoint(vector.getX(p), vector.getY(p)); } } else { for (int p = 0; p < positionCount; p++) { int count = block.getValueCount(p); int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendPoint(block.getPoint(i++)); + for (int v = 0; v < count; v++, i++) { + appendPoint(block.getX(i), block.getY(i)); } } } @@ -159,8 +161,8 @@ private void copyFromBlock(PointBlock block, int beginInclusive, int endExclusiv beginPositionEntry(); } int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { - appendPoint(block.getPoint(i++)); + for (int v = 0; v < count; v++, i++) { + appendPoint(block.getX(i), block.getY(i)); } if (count > 1) { endPositionEntry(); @@ -170,7 +172,7 @@ private void copyFromBlock(PointBlock block, int beginInclusive, int endExclusiv private void copyFromVector(PointVector vector, int beginInclusive, int endExclusive) { for (int p = beginInclusive; p < endExclusive; p++) { - appendPoint(vector.getPoint(p)); + appendPoint(vector.getX(p), vector.getY(p)); } } @@ -186,16 +188,17 @@ public PointBlock build() { finish(); PointBlock theBlock; if (hasNonNullValue && positionCount == 1 && valueCount == 1) { - theBlock = blockFactory.newConstantPointBlockWith(values[0], 1, estimatedBytes); + theBlock = blockFactory.newConstantPointBlockWith(xValues[0], yValues[0], 1, estimatedBytes); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } if (isDense() && singleValued()) { - theBlock = blockFactory.newPointArrayVector(values, positionCount, estimatedBytes).asBlock(); + theBlock = blockFactory.newPointArrayVector(xValues, yValues, positionCount, estimatedBytes).asBlock(); } else { theBlock = blockFactory.newPointArrayBlock( - values, + xValues, + yValues, positionCount, firstValueIndexes, nullsMask, diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVector.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVector.java index 5537b1c4c4cfc..fcef786cafef3 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVector.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVector.java @@ -14,12 +14,18 @@ import java.io.IOException; /** - * Vector that stores SpatialPoint values. + * Vector that stores Point values. * This class is generated. Do not edit it. */ public sealed interface PointVector extends Vector permits ConstantPointVector, PointArrayVector, PointBigArrayVector, ConstantNullVector { - SpatialPoint getPoint(int position); + double getX(int position); + + double getY(int position); + + default SpatialPoint getPoint(int valueIndex) { + return new SpatialPoint(getX(valueIndex), getY(valueIndex)); + } @Override PointBlock asBlock(); @@ -50,7 +56,7 @@ static boolean equals(PointVector vector1, PointVector vector2) { return false; } for (int pos = 0; pos < positions; pos++) { - if (vector1.getPoint(pos).equals(vector2.getPoint(pos)) == false) { + if (vector1.getX(pos) != vector2.getY(pos) || vector1.getY(pos) != vector2.getY(pos)) { return false; } } @@ -67,10 +73,11 @@ static int hash(PointVector vector) { final int len = vector.getPositionCount(); int result = 1; for (int pos = 0; pos < len; pos++) { - SpatialPoint point = vector.getPoint(pos); - long element = Double.doubleToLongBits(point.getX()); + double x = vector.getX(pos); + double y = vector.getY(pos); + long element = Double.doubleToLongBits(x); result = 31 * result + (int) (element ^ (element >>> 32)); - element = Double.doubleToLongBits(point.getY()); + element = Double.doubleToLongBits(y); result = 31 * result + (int) (element ^ (element >>> 32)); } return result; @@ -81,11 +88,11 @@ static PointVector readFrom(BlockFactory blockFactory, StreamInput in) throws IO final int positions = in.readVInt(); final boolean constant = in.readBoolean(); if (constant && positions > 0) { - return blockFactory.newConstantPointVector(new SpatialPoint(in.readDouble(), in.readDouble()), positions); + return blockFactory.newConstantPointVector(in.readDouble(), in.readDouble(), positions); } else { try (var builder = blockFactory.newPointVectorFixedBuilder(positions)) { for (int i = 0; i < positions; i++) { - builder.appendPoint(new SpatialPoint(in.readDouble(), in.readDouble())); + builder.appendPoint(in.readDouble(), in.readDouble()); } return builder.build(); } @@ -98,14 +105,12 @@ default void writeTo(StreamOutput out) throws IOException { out.writeVInt(positions); out.writeBoolean(isConstant()); if (isConstant() && positions > 0) { - SpatialPoint point = getPoint(0); - out.writeDouble(point.getX()); - out.writeDouble(point.getY()); + out.writeDouble(getX(0)); + out.writeDouble(getY(0)); } else { for (int i = 0; i < positions; i++) { - SpatialPoint point = getPoint(i); - out.writeDouble(point.getX()); - out.writeDouble(point.getY()); + out.writeDouble(getX(i)); + out.writeDouble(getY(i)); } } } @@ -145,9 +150,9 @@ static FixedBuilder newVectorFixedBuilder(int size, BlockFactory blockFactory) { */ sealed interface Builder extends Vector.Builder permits PointVectorBuilder { /** - * Appends a SpatialPoint to the current entry. + * Appends a double to the current entry. */ - Builder appendPoint(SpatialPoint value); + Builder appendPoint(double x, double y); @Override PointVector build(); @@ -158,9 +163,9 @@ sealed interface Builder extends Vector.Builder permits PointVectorBuilder { */ sealed interface FixedBuilder extends Vector.Builder permits PointVectorFixedBuilder { /** - * Appends a SpatialPoint to the current entry. + * Appends a double to the current entry. */ - FixedBuilder appendPoint(SpatialPoint value); + FixedBuilder appendPoint(double x, double y); @Override PointVector build(); diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBlock.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBlock.java index 281a68ed5d0f8..016e2e14c94c2 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBlock.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBlock.java @@ -7,7 +7,6 @@ package org.elasticsearch.compute.data; -import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.core.Releasables; /** @@ -32,8 +31,13 @@ public PointVector asVector() { } @Override - public SpatialPoint getPoint(int valueIndex) { - return vector.getPoint(valueIndex); + public double getX(int valueIndex) { + return vector.getX(valueIndex); + } + + @Override + public double getY(int valueIndex) { + return vector.getY(valueIndex); } @Override diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBuilder.java index 33ed33549fd41..016f5de0568dd 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorBuilder.java @@ -7,8 +7,6 @@ package org.elasticsearch.compute.data; -import org.elasticsearch.common.geo.SpatialPoint; - import java.util.Arrays; /** @@ -17,19 +15,21 @@ */ final class PointVectorBuilder extends AbstractVectorBuilder implements PointVector.Builder { - private SpatialPoint[] values; + private double[] xValues, yValues; PointVectorBuilder(int estimatedSize, BlockFactory blockFactory) { super(blockFactory); int initialSize = Math.max(estimatedSize, 2); adjustBreaker(initialSize); - values = new SpatialPoint[Math.max(estimatedSize, 2)]; + xValues = new double[Math.max(estimatedSize, 2)]; + yValues = new double[Math.max(estimatedSize, 2)]; } @Override - public PointVectorBuilder appendPoint(SpatialPoint value) { + public PointVectorBuilder appendPoint(double x, double y) { ensureCapacity(); - values[valueCount] = value; + xValues[valueCount] = x; + yValues[valueCount] = y; valueCount++; return this; } @@ -41,12 +41,13 @@ protected int elementSize() { @Override protected int valuesLength() { - return values.length; + return xValues.length; } @Override protected void growValuesArray(int newSize) { - values = Arrays.copyOf(values, newSize); + xValues = Arrays.copyOf(xValues, newSize); + yValues = Arrays.copyOf(yValues, newSize); } @Override @@ -54,12 +55,12 @@ public PointVector build() { finish(); PointVector vector; if (valueCount == 1) { - vector = blockFactory.newConstantPointBlockWith(values[0], 1, estimatedBytes).asVector(); + vector = blockFactory.newConstantPointBlockWith(xValues[0], yValues[0], 1, estimatedBytes).asVector(); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } - vector = blockFactory.newPointArrayVector(values, valueCount, estimatedBytes); + vector = blockFactory.newPointArrayVector(xValues, yValues, valueCount, estimatedBytes); } built(); return vector; diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorFixedBuilder.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorFixedBuilder.java index aa26f5cd60afc..957b44c30c5cb 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorFixedBuilder.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/PointVectorFixedBuilder.java @@ -8,7 +8,6 @@ package org.elasticsearch.compute.data; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; /** * Builder for {@link PointVector}s that never grows. Prefer this to @@ -18,7 +17,7 @@ */ final class PointVectorFixedBuilder implements PointVector.FixedBuilder { private final BlockFactory blockFactory; - private final SpatialPoint[] values; + private final double[] xValues, yValues; private final long preAdjustedBytes; /** * The next value to write into. {@code -1} means the vector has already @@ -30,12 +29,14 @@ final class PointVectorFixedBuilder implements PointVector.FixedBuilder { preAdjustedBytes = ramBytesUsed(size); blockFactory.adjustBreaker(preAdjustedBytes, false); this.blockFactory = blockFactory; - this.values = new SpatialPoint[size]; + this.xValues = new double[size]; + this.yValues = new double[size]; } @Override - public PointVectorFixedBuilder appendPoint(SpatialPoint value) { - values[nextIndex++] = value; + public PointVectorFixedBuilder appendPoint(double x, double y) { + xValues[nextIndex++] = x; + yValues[nextIndex++] = y; return this; } @@ -43,24 +44,28 @@ private static long ramBytesUsed(int size) { return size == 1 ? ConstantPointVector.RAM_BYTES_USED : PointArrayVector.BASE_RAM_BYTES_USED + RamUsageEstimator.alignObjectSize( - (long) RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + (long) size * (16 + RamUsageEstimator.NUM_BYTES_OBJECT_REF) + (long) RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + size * 16 ); } + private int valuesLength() { + return xValues.length; + } + @Override public PointVector build() { if (nextIndex < 0) { throw new IllegalStateException("already closed"); } - if (nextIndex != values.length) { - throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); + if (nextIndex != valuesLength()) { + throw new IllegalStateException("expected to write [" + valuesLength() + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; PointVector vector; - if (values.length == 1) { - vector = blockFactory.newConstantPointBlockWith(values[0], 1, preAdjustedBytes).asVector(); + if (valuesLength() == 1) { + vector = blockFactory.newConstantPointBlockWith(xValues[0], yValues[0], 1, preAdjustedBytes).asVector(); } else { - vector = blockFactory.newPointArrayVector(values, values.length, preAdjustedBytes); + vector = blockFactory.newPointArrayVector(xValues, yValues, valuesLength(), preAdjustedBytes); } assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; return vector; diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeBytesRef.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeBytesRef.java index 1fd4c1ea3562d..ab105361f673d 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeBytesRef.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeBytesRef.java @@ -162,8 +162,8 @@ public MultivalueDedupe.HashResult hash(BlockFactory blockFactory, BytesRefHash builder.appendInt(0); } case 1 -> { - BytesRef v = block.getBytesRef(first, work[0]); - hash(builder, hash, v); + BytesRef value = block.getBytesRef(first, work[0]); + hash(builder, hash, value); } default -> { if (count < ALWAYS_COPY_MISSING) { @@ -204,13 +204,13 @@ protected void readNextBatch() { switch (count) { case 0 -> encodeNull(); case 1 -> { - BytesRef v = block.getBytesRef(first, work[0]); - if (hasCapacity(v.length, 1)) { + BytesRef value = block.getBytesRef(first, work[0]); + if (hasCapacity(value.length, 1)) { startPosition(); - encode(v); + encode(value); endPosition(); } else { - work[0] = v; + work[0] = value; w = 1; return; } @@ -391,7 +391,7 @@ private void fillWork(int from, int to) { } } - private void hash(IntBlock.Builder builder, BytesRefHash hash, BytesRef v) { - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(v)))); + private void hash(IntBlock.Builder builder, BytesRefHash hash, BytesRef value) { + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(value)))); } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeDouble.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeDouble.java index 157b6670e95af..80270ab89d320 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeDouble.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeDouble.java @@ -159,8 +159,8 @@ public MultivalueDedupe.HashResult hash(BlockFactory blockFactory, LongHash hash builder.appendInt(0); } case 1 -> { - double v = block.getDouble(first); - hash(builder, hash, v); + double value = block.getDouble(first); + hash(builder, hash, value); } default -> { if (count < ALWAYS_COPY_MISSING) { @@ -201,13 +201,13 @@ protected void readNextBatch() { switch (count) { case 0 -> encodeNull(); case 1 -> { - double v = block.getDouble(first); + double value = block.getDouble(first); if (hasCapacity(1)) { startPosition(); - encode(v); + encode(value); endPosition(); } else { - work[0] = v; + work[0] = value; w = 1; return; } @@ -369,7 +369,7 @@ private void grow(int size) { work = ArrayUtil.grow(work, size); } - private void hash(IntBlock.Builder builder, LongHash hash, double v) { - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(Double.doubleToLongBits(v))))); + private void hash(IntBlock.Builder builder, LongHash hash, double value) { + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(Double.doubleToLongBits(value))))); } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeInt.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeInt.java index eade5e1fdc347..cad9713468278 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeInt.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeInt.java @@ -159,8 +159,8 @@ public MultivalueDedupe.HashResult hash(BlockFactory blockFactory, LongHash hash builder.appendInt(0); } case 1 -> { - int v = block.getInt(first); - hash(builder, hash, v); + int value = block.getInt(first); + hash(builder, hash, value); } default -> { if (count < ALWAYS_COPY_MISSING) { @@ -201,13 +201,13 @@ protected void readNextBatch() { switch (count) { case 0 -> encodeNull(); case 1 -> { - int v = block.getInt(first); + int value = block.getInt(first); if (hasCapacity(1)) { startPosition(); - encode(v); + encode(value); endPosition(); } else { - work[0] = v; + work[0] = value; w = 1; return; } @@ -369,7 +369,7 @@ private void grow(int size) { work = ArrayUtil.grow(work, size); } - private void hash(IntBlock.Builder builder, LongHash hash, int v) { - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(v)))); + private void hash(IntBlock.Builder builder, LongHash hash, int value) { + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(value)))); } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeLong.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeLong.java index acbc9139a75c5..78b6fc4fb8890 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeLong.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupeLong.java @@ -160,8 +160,8 @@ public MultivalueDedupe.HashResult hash(BlockFactory blockFactory, LongHash hash builder.appendInt(0); } case 1 -> { - long v = block.getLong(first); - hash(builder, hash, v); + long value = block.getLong(first); + hash(builder, hash, value); } default -> { if (count < ALWAYS_COPY_MISSING) { @@ -202,13 +202,13 @@ protected void readNextBatch() { switch (count) { case 0 -> encodeNull(); case 1 -> { - long v = block.getLong(first); + long value = block.getLong(first); if (hasCapacity(1)) { startPosition(); - encode(v); + encode(value); endPosition(); } else { - work[0] = v; + work[0] = value; w = 1; return; } @@ -370,7 +370,7 @@ private void grow(int size) { work = ArrayUtil.grow(work, size); } - private void hash(IntBlock.Builder builder, LongHash hash, long v) { - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(v)))); + private void hash(IntBlock.Builder builder, LongHash hash, long value) { + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(value)))); } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupePoint.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupePoint.java index 67749a7744ad8..8b6d4d36ccfdb 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupePoint.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/MultivalueDedupePoint.java @@ -31,6 +31,7 @@ public class MultivalueDedupePoint { */ private static final int ALWAYS_COPY_MISSING = 110; private final PointBlock block; + // TODO: figure out a way to remove this object array, currently needed for Arrays.sort() below private SpatialPoint[] work = new SpatialPoint[ArrayUtil.oversize(2, 16)]; private int w; @@ -53,7 +54,7 @@ public PointBlock dedupeToBlockAdaptive(BlockFactory blockFactory) { int first = block.getFirstValueIndex(p); switch (count) { case 0 -> builder.appendNull(); - case 1 -> builder.appendPoint(block.getPoint(first)); + case 1 -> builder.appendPoint(block.getX(first), block.getY(first)); default -> { /* * It's better to copyMissing when there are few unique values @@ -103,7 +104,7 @@ public PointBlock dedupeToBlockUsingCopyAndSort(BlockFactory blockFactory) { int first = block.getFirstValueIndex(p); switch (count) { case 0 -> builder.appendNull(); - case 1 -> builder.appendPoint(block.getPoint(first)); + case 1 -> builder.appendPoint(block.getX(first), block.getY(first)); default -> { copyAndSort(first, count); writeSortedWork(builder); @@ -133,7 +134,7 @@ public PointBlock dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) { int first = block.getFirstValueIndex(p); switch (count) { case 0 -> builder.appendNull(); - case 1 -> builder.appendPoint(block.getPoint(first)); + case 1 -> builder.appendPoint(block.getX(first), block.getY(first)); default -> { copyMissing(first, count); writeUniquedWork(builder); @@ -160,8 +161,9 @@ public MultivalueDedupe.HashResult hash(BlockFactory blockFactory, LongHash hash builder.appendInt(0); } case 1 -> { - SpatialPoint v = block.getPoint(first); - hash(builder, hash, v); + double x = block.getX(first); + double y = block.getY(first); + hash(builder, hash, x, y); } default -> { if (count < ALWAYS_COPY_MISSING) { @@ -202,13 +204,15 @@ protected void readNextBatch() { switch (count) { case 0 -> encodeNull(); case 1 -> { - SpatialPoint v = block.getPoint(first); + double x = block.getX(first); + double y = block.getY(first); if (hasCapacity(1)) { startPosition(); - encode(v); + encode(x, y); endPosition(); } else { - work[0] = v; + // We should be able to replace this with two double[] if we can figure out the sorting below + work[0] = new SpatialPoint(x, y); w = 1; return; } @@ -245,7 +249,7 @@ private void copyAndSort(int first, int count) { w = 0; for (int i = first; i < end; i++) { - work[w++] = block.getPoint(i); + work[w++] = new SpatialPoint(block.getX(i), block.getY(i)); } Arrays.sort(work, 0, w); @@ -259,10 +263,10 @@ private void copyMissing(int first, int count) { grow(count); int end = first + count; - work[0] = block.getPoint(first); + work[0] = new SpatialPoint(block.getX(first), block.getY(first)); w = 1; i: for (int i = first + 1; i < end; i++) { - SpatialPoint v = block.getPoint(i); + SpatialPoint v = new SpatialPoint(block.getX(i), block.getY(i)); for (int j = 0; j < w; j++) { if (v.equals(work[j])) { continue i; @@ -277,12 +281,12 @@ private void copyMissing(int first, int count) { */ private void writeUniquedWork(PointBlock.Builder builder) { if (w == 1) { - builder.appendPoint(work[0]); + builder.appendPoint(work[0].getX(), work[0].getY()); return; } builder.beginPositionEntry(); for (int i = 0; i < w; i++) { - builder.appendPoint(work[i]); + builder.appendPoint(work[i].getX(), work[i].getY()); } builder.endPositionEntry(); } @@ -292,16 +296,16 @@ private void writeUniquedWork(PointBlock.Builder builder) { */ private void writeSortedWork(PointBlock.Builder builder) { if (w == 1) { - builder.appendPoint(work[0]); + builder.appendPoint(work[0].getX(), work[0].getY()); return; } builder.beginPositionEntry(); SpatialPoint prev = work[0]; - builder.appendPoint(prev); + builder.appendPoint(prev.getX(), prev.getY()); for (int i = 1; i < w; i++) { if (false == prev.equals(work[i])) { prev = work[i]; - builder.appendPoint(prev); + builder.appendPoint(prev.getX(), prev.getY()); } } builder.endPositionEntry(); @@ -312,12 +316,12 @@ private void writeSortedWork(PointBlock.Builder builder) { */ private void hashUniquedWork(LongHash hash, IntBlock.Builder builder) { if (w == 1) { - hash(builder, hash, work[0]); + hash(builder, hash, work[0].getX(), work[0].getY()); return; } builder.beginPositionEntry(); for (int i = 0; i < w; i++) { - hash(builder, hash, work[i]); + hash(builder, hash, work[i].getX(), work[i].getY()); } builder.endPositionEntry(); } @@ -327,16 +331,16 @@ private void hashUniquedWork(LongHash hash, IntBlock.Builder builder) { */ private void hashSortedWork(LongHash hash, IntBlock.Builder builder) { if (w == 1) { - hash(builder, hash, work[0]); + hash(builder, hash, work[0].getX(), work[0].getY()); return; } builder.beginPositionEntry(); SpatialPoint prev = work[0]; - hash(builder, hash, prev); + hash(builder, hash, prev.getX(), prev.getY()); for (int i = 1; i < w; i++) { if (false == prev.equals(work[i])) { prev = work[i]; - hash(builder, hash, prev); + hash(builder, hash, prev.getX(), prev.getY()); } } builder.endPositionEntry(); @@ -347,7 +351,7 @@ private void hashSortedWork(LongHash hash, IntBlock.Builder builder) { */ private void encodeUniquedWork(BatchEncoder.Points encoder) { for (int i = 0; i < w; i++) { - encoder.encode(work[i]); + encoder.encode(work[i].getX(), work[i].getY()); } } @@ -370,7 +374,7 @@ private void grow(int size) { work = ArrayUtil.grow(work, size); } - private void hash(IntBlock.Builder builder, LongHash hash, SpatialPoint v) { - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(v.hashCode())))); + private void hash(IntBlock.Builder builder, LongHash hash, double x, double y) { + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(31L * Double.hashCode(x) + Double.hashCode(y))))); } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/KeyExtractorForPoint.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/KeyExtractorForPoint.java index f1750a1681d0e..8dc694cd17128 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/KeyExtractorForPoint.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/KeyExtractorForPoint.java @@ -7,7 +7,6 @@ package org.elasticsearch.compute.operator.topn; -import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.compute.data.PointBlock; import org.elasticsearch.compute.data.PointVector; import org.elasticsearch.compute.operator.BreakingBytesRefBuilder; @@ -37,9 +36,9 @@ static KeyExtractorForPoint extractorFor(TopNEncoder encoder, boolean ascending, this.nonNul = nonNul; } - protected final int nonNul(BreakingBytesRefBuilder key, SpatialPoint value) { + protected final int nonNul(BreakingBytesRefBuilder key, double x, double y) { key.append(nonNul); - TopNEncoder.DEFAULT_SORTABLE.encodePoint(value, key); + TopNEncoder.DEFAULT_SORTABLE.encodePoint(x, y, key); return 16 + 1; } @@ -58,7 +57,7 @@ static class ForVector extends KeyExtractorForPoint { @Override public int writeKey(BreakingBytesRefBuilder key, int position) { - return nonNul(key, vector.getPoint(position)); + return nonNul(key, vector.getX(position), vector.getY(position)); } } @@ -75,7 +74,8 @@ public int writeKey(BreakingBytesRefBuilder key, int position) { if (block.isNull(position)) { return nul(key); } - return nonNul(key, block.getPoint(block.getFirstValueIndex(position))); + int index = block.getFirstValueIndex(position); + return nonNul(key, block.getX(index), block.getY(index)); } } @@ -92,7 +92,8 @@ public int writeKey(BreakingBytesRefBuilder key, int position) { if (block.isNull(position)) { return nul(key); } - return nonNul(key, block.getPoint(block.getFirstValueIndex(position) + block.getValueCount(position) - 1)); + int index = block.getFirstValueIndex(position) + block.getValueCount(position) - 1; + return nonNul(key, block.getX(index), block.getY(index)); } } @@ -112,14 +113,19 @@ public int writeKey(BreakingBytesRefBuilder key, int position) { } int start = block.getFirstValueIndex(position); int end = start + size; - SpatialPoint min = block.getPoint(start); + double minX = block.getX(start); + double minY = block.getY(start); for (int i = start + 1; i < end; i++) { - SpatialPoint other = block.getPoint(i); - if (other.compareTo(min) < 0) { - min = other; + double x = block.getX(i); + double y = block.getY(i); + if (x < minX) { + minX = x; + } + if (y < minY) { + minY = y; } } - return nonNul(key, min); + return nonNul(key, minX, minY); } } @@ -139,14 +145,19 @@ public int writeKey(BreakingBytesRefBuilder key, int position) { } int start = block.getFirstValueIndex(position); int end = start + size; - SpatialPoint max = block.getPoint(start); + double maxX = block.getX(start); + double maxY = block.getY(start); for (int i = start + 1; i < end; i++) { - SpatialPoint other = block.getPoint(i); - if (other.compareTo(max) > 0) { - max = other; + double x = block.getX(i); + double y = block.getY(i); + if (x > maxX) { + maxX = x; + } + if (y > maxY) { + maxY = y; } } - return nonNul(key, max); + return nonNul(key, maxX, maxY); } } } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ResultBuilderForPoint.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ResultBuilderForPoint.java index 525b0799e9dab..c5536963e7cf6 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ResultBuilderForPoint.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ResultBuilderForPoint.java @@ -20,6 +20,7 @@ class ResultBuilderForPoint implements ResultBuilder { /** * The value previously set by {@link #decodeKey}. */ + // TODO: we should be able to get rid of this object private SpatialPoint key; ResultBuilderForPoint(BlockFactory blockFactory, TopNEncoder encoder, boolean inKey, int initialSize) { @@ -41,11 +42,15 @@ public void decodeValue(BytesRef values) { case 0 -> { builder.appendNull(); } - case 1 -> builder.appendPoint(inKey ? key : readValueFromValues(values)); + case 1 -> { + SpatialPoint value = inKey ? key : readValueFromValues(values); + builder.appendPoint(value.getX(), value.getY()); + } default -> { builder.beginPositionEntry(); for (int i = 0; i < count; i++) { - builder.appendPoint(readValueFromValues(values)); + SpatialPoint point = readValueFromValues(values); + builder.appendPoint(point.getX(), point.getY()); } builder.endPositionEntry(); } diff --git a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ValueExtractorForPoint.java b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ValueExtractorForPoint.java index a459434238e35..0943fd717bb27 100644 --- a/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ValueExtractorForPoint.java +++ b/x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/operator/topn/ValueExtractorForPoint.java @@ -7,7 +7,6 @@ package org.elasticsearch.compute.operator.topn; -import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.compute.data.PointBlock; import org.elasticsearch.compute.data.PointVector; import org.elasticsearch.compute.operator.BreakingBytesRefBuilder; @@ -32,8 +31,8 @@ protected final void writeCount(BreakingBytesRefBuilder values, int count) { TopNEncoder.DEFAULT_UNSORTABLE.encodeVInt(count, values); } - protected final void actualWriteValue(BreakingBytesRefBuilder values, SpatialPoint value) { - TopNEncoder.DEFAULT_UNSORTABLE.encodePoint(value, values); + protected final void actualWriteValue(BreakingBytesRefBuilder values, double x, double y) { + TopNEncoder.DEFAULT_UNSORTABLE.encodePoint(x, y, values); } static class ForVector extends ValueExtractorForPoint { @@ -51,7 +50,7 @@ public void writeValue(BreakingBytesRefBuilder values, int position) { // will read results from the key return; } - actualWriteValue(values, vector.getPoint(position)); + actualWriteValue(values, vector.getX(position), vector.getY(position)); } } @@ -74,7 +73,7 @@ public void writeValue(BreakingBytesRefBuilder values, int position) { int start = block.getFirstValueIndex(position); int end = start + size; for (int i = start; i < end; i++) { - actualWriteValue(values, block.getPoint(i)); + actualWriteValue(values, block.getX(i), block.getY(i)); } } } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-ArrayState.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-ArrayState.java.st index 42f86580a228d..87828f74d30ef 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-ArrayState.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-ArrayState.java.st @@ -57,7 +57,7 @@ final class $Type$ArrayState extends AbstractArrayState implements GroupingAggre return groupId < values.size() ? values.get(groupId) : init; } - void set(int groupId, $type$ value) { + void set(int groupId, $typeValue$) { ensureCapacity(groupId); values.set(groupId, value); trackGroupId(groupId); diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-State.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-State.java.st index 427d1a0c312cc..4070eb3d34e3a 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-State.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-State.java.st @@ -15,7 +15,7 @@ import org.elasticsearch.compute.operator.DriverContext; * This class is generated. Do not edit it. */ final class $Type$State implements AggregatorState { - private $type$ value; + private $typeValue$; private boolean seen; $Type$State() { @@ -30,7 +30,7 @@ final class $Type$State implements AggregatorState { return value; } - void $type$Value($type$ value) { + void $type$Value($typeValue$) { this.value = value; } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockFactory.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockFactory.java index 13dfa90b8e206..479a85edfa394 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockFactory.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockFactory.java @@ -11,7 +11,6 @@ import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.breaker.CircuitBreakingException; import org.elasticsearch.common.breaker.NoopCircuitBreaker; -import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.BytesRefArray; @@ -392,24 +391,26 @@ public PointBlock.Builder newPointBlockBuilder(int estimatedSize) { } public final PointBlock newPointArrayBlock( - SpatialPoint[] values, + double[] xValues, + double[] yValues, int pc, int[] firstValueIndexes, BitSet nulls, MvOrdering mvOrdering ) { - return newPointArrayBlock(values, pc, firstValueIndexes, nulls, mvOrdering, 0L); + return newPointArrayBlock(xValues, yValues, pc, firstValueIndexes, nulls, mvOrdering, 0L); } public PointBlock newPointArrayBlock( - SpatialPoint[] values, + double[] xValues, + double[] yValues, int pc, int[] fvi, BitSet nulls, MvOrdering mvOrdering, long preAdjustedBytes ) { - var b = new PointArrayBlock(values, pc, fvi, nulls, mvOrdering, this); + var b = new PointArrayBlock(xValues, yValues, pc, fvi, nulls, mvOrdering, this); adjustBreaker(b.ramBytesUsed() - preAdjustedBytes, true); return b; } @@ -425,29 +426,29 @@ public PointVector.FixedBuilder newPointVectorFixedBuilder(int size) { return new PointVectorFixedBuilder(size, this); } - public final PointVector newPointArrayVector(SpatialPoint[] values, int positionCount) { - return newPointArrayVector(values, positionCount, 0L); + public final PointVector newPointArrayVector(double[] xValues, double[] yValues, int positionCount) { + return newPointArrayVector(xValues, yValues, positionCount, 0L); } - public PointVector newPointArrayVector(SpatialPoint[] values, int positionCount, long preAdjustedBytes) { - var b = new PointArrayVector(values, positionCount, this); + public PointVector newPointArrayVector(double[] xValues, double[] yValues, int positionCount, long preAdjustedBytes) { + var b = new PointArrayVector(xValues, yValues, positionCount, this); adjustBreaker(b.ramBytesUsed() - preAdjustedBytes, true); return b; } - public final PointBlock newConstantPointBlockWith(SpatialPoint value, int positions) { - return newConstantPointBlockWith(value, positions, 0L); + public final PointBlock newConstantPointBlockWith(double x, double y, int positions) { + return newConstantPointBlockWith(x, y, positions, 0L); } - public PointBlock newConstantPointBlockWith(SpatialPoint value, int positions, long preAdjustedBytes) { - var b = new ConstantPointVector(value, positions, this).asBlock(); + public PointBlock newConstantPointBlockWith(double x, double y, int positions, long preAdjustedBytes) { + var b = new ConstantPointVector(x, y, positions, this).asBlock(); adjustBreaker(b.ramBytesUsed() - preAdjustedBytes, true); return b; } - public PointVector newConstantPointVector(SpatialPoint value, int positions) { + public PointVector newConstantPointVector(double x, double y, int positions) { adjustBreaker(ConstantPointVector.RAM_BYTES_USED, false); - var v = new ConstantPointVector(value, positions, this); + var v = new ConstantPointVector(x, y, positions, this); assert v.ramBytesUsed() == ConstantPointVector.RAM_BYTES_USED; return v; } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockUtils.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockUtils.java index eb5a59b18d340..103a614526ff8 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockUtils.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockUtils.java @@ -211,7 +211,7 @@ public static void appendValue(Block.Builder builder, Object val, ElementType ty case BYTES_REF -> ((BytesRefBlock.Builder) builder).appendBytesRef(toBytesRef(val)); case DOUBLE -> ((DoubleBlock.Builder) builder).appendDouble((Double) val); case BOOLEAN -> ((BooleanBlock.Builder) builder).appendBoolean((Boolean) val); - case POINT -> ((PointBlock.Builder) builder).appendPoint((SpatialPoint) val); + case POINT -> ((PointBlock.Builder) builder).appendPoint(((SpatialPoint) val).getX(), ((SpatialPoint) val).getY()); default -> throw new UnsupportedOperationException("unsupported element type [" + type + "]"); } } @@ -232,7 +232,7 @@ private static Block constantBlock(BlockFactory blockFactory, ElementType type, case BYTES_REF -> BytesRefBlock.newConstantBlockWith(toBytesRef(val), size, blockFactory); case DOUBLE -> DoubleBlock.newConstantBlockWith((double) val, size, blockFactory); case BOOLEAN -> BooleanBlock.newConstantBlockWith((boolean) val, size, blockFactory); - case POINT -> PointBlock.newConstantBlockWith((SpatialPoint) val, size, blockFactory); + case POINT -> PointBlock.newConstantBlockWith(((SpatialPoint) val).getX(), ((SpatialPoint) val).getY(), size, blockFactory); default -> throw new UnsupportedOperationException("unsupported element type [" + type + "]"); }; } @@ -275,7 +275,7 @@ private static Object valueAtOffset(Block block, int offset) { DocVector v = ((DocBlock) block).asVector(); yield new Doc(v.shards().getInt(offset), v.segments().getInt(offset), v.docs().getInt(offset)); } - case POINT -> ((PointBlock) block).getPoint(offset); + case POINT -> new SpatialPoint(((PointBlock) block).getX(offset), ((PointBlock) block).getY(offset)); case UNKNOWN -> throw new IllegalArgumentException("can't read values from [" + block + "]"); }; } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullBlock.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullBlock.java index 6c9d9bf2a08f5..97f6efcdc85ca 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullBlock.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullBlock.java @@ -9,7 +9,6 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -230,7 +229,13 @@ public double getDouble(int valueIndex) { } @Override - public SpatialPoint getPoint(int valueIndex) { + public double getX(int valueIndex) { + assert false : "null block"; + throw new UnsupportedOperationException("null block"); + } + + @Override + public double getY(int valueIndex) { assert false : "null block"; throw new UnsupportedOperationException("null block"); } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullVector.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullVector.java index be54dc8105567..48fadd9c9943e 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullVector.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullVector.java @@ -8,7 +8,6 @@ package org.elasticsearch.compute.data; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.common.io.stream.StreamOutput; import java.io.IOException; @@ -66,7 +65,13 @@ public double getDouble(int position) { } @Override - public SpatialPoint getPoint(int position) { + public double getX(int position) { + assert false : "null vector"; + throw new UnsupportedOperationException("null vector"); + } + + @Override + public double getY(int position) { assert false : "null vector"; throw new UnsupportedOperationException("null vector"); } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayBlock.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayBlock.java.st index ae9dd690b78a9..2a5d0a9a7f6c3 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayBlock.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayBlock.java.st @@ -13,11 +13,6 @@ import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.common.util.BytesRefArray; import org.elasticsearch.core.Releasables; -$elseif(Point)$ -import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; - -import java.util.Arrays; $else$ import org.apache.lucene.util.RamUsageEstimator; @@ -25,12 +20,6 @@ import java.util.Arrays; $endif$ import java.util.BitSet; -$if(Point)$ -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_ARRAY_HEADER; -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF; -import static org.apache.lucene.util.RamUsageEstimator.alignObjectSize; -$endif$ - /** * Block implementation that stores an array of $type$. * This class is generated. Do not edit it. @@ -42,16 +31,38 @@ public final class $Type$ArrayBlock extends AbstractArrayBlock implements $Type$ $if(BytesRef)$ private final BytesRefArray values; +$elseif(Point)$ + private final double[] xValues, yValues; + $else$ private final $type$[] values; $endif$ +$if(Point)$ + public $Type$ArrayBlock( + double[] xValues, + double[] yValues, + int positionCount, + int[] firstValueIndexes, + BitSet nulls, + MvOrdering mvOrdering + ) { + this(xValues, yValues, positionCount, firstValueIndexes, nulls, mvOrdering, BlockFactory.getNonBreakingInstance()); + } + +$else$ public $Type$ArrayBlock($if(BytesRef)$BytesRefArray$else$$type$[]$endif$ values, int positionCount, int[] firstValueIndexes, BitSet nulls, MvOrdering mvOrdering) { this(values, positionCount, firstValueIndexes, nulls, mvOrdering, BlockFactory.getNonBreakingInstance()); } +$endif$ public $Type$ArrayBlock( +$if(Point)$ + double[] xValues, + double[] yValues, +$else$ $if(BytesRef)$BytesRefArray$else$$type$[]$endif$ values, +$endif$ int positionCount, int[] firstValueIndexes, BitSet nulls, @@ -59,7 +70,12 @@ $endif$ BlockFactory blockFactory ) { super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory); +$if(Point)$ + this.xValues = xValues; + this.yValues = yValues; +$else$ this.values = values; +$endif$ } @Override @@ -71,6 +87,14 @@ $endif$ $if(BytesRef)$ public BytesRef getBytesRef(int valueIndex, BytesRef dest) { return values.get(valueIndex, dest); +$elseif(Point)$ + public double getX(int valueIndex) { + return xValues[valueIndex]; + } + + @Override + public double getY(int valueIndex) { + return yValues[valueIndex]; $else$ public $type$ get$Type$(int valueIndex) { return values[valueIndex]; @@ -91,11 +115,19 @@ $endif$ int valueCount = getValueCount(pos); int first = getFirstValueIndex(pos); if (valueCount == 1) { +$if(Point)$ + builder.append$Type$(getX(getFirstValueIndex(pos)), getY(getFirstValueIndex(pos))); +$else$ builder.append$Type$(get$Type$(getFirstValueIndex(pos)$if(BytesRef)$, scratch$endif$)); +$endif$ } else { builder.beginPositionEntry(); for (int c = 0; c < valueCount; c++) { +$if(Point)$ + builder.appendPoint(getX(first + c), getY(first + c)); +$else$ builder.append$Type$(get$Type$(first + c$if(BytesRef)$, scratch$endif$)); +$endif$ } builder.endPositionEntry(); } @@ -130,6 +162,8 @@ $endif$ for (int i = first; i < end; i++) { $if(BytesRef)$ builder.append$Type$(get$Type$(i, scratch)); +$elseif(Point)$ + builder.append$Type$(getX(i), getY(i)); $else$ builder.append$Type$(get$Type$(i)); $endif$ @@ -140,19 +174,13 @@ $endif$ } public static long ramBytesEstimated($if(BytesRef)$BytesRefArray$else$$type$[]$endif$ values, int[] firstValueIndexes, BitSet nullsMask) { - $if(Point)$ - long valuesEstimate = (long) NUM_BYTES_ARRAY_HEADER + ((long) Long.BYTES * 2 + (long) NUM_BYTES_OBJECT_REF) * values.length; - return BASE_RAM_BYTES_USED + alignObjectSize(valuesEstimate) + BlockRamUsageEstimator.sizeOf(firstValueIndexes) - + BlockRamUsageEstimator.sizeOfBitSet(nullsMask); - $else$ - return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values) + BlockRamUsageEstimator.sizeOf(firstValueIndexes) + return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values)$if(Point)$ * 2$endif$ + BlockRamUsageEstimator.sizeOf(firstValueIndexes) + BlockRamUsageEstimator.sizeOfBitSet(nullsMask); - $endif$ } @Override public long ramBytesUsed() { - return ramBytesEstimated(values, firstValueIndexes, nullsMask); + return ramBytesEstimated($if(Point)$xValues$else$values$endif$, firstValueIndexes, nullsMask); } @Override @@ -178,6 +206,11 @@ $endif$ $if(BytesRef)$ + ", values=" + values.size() +$elseif(Point)$ + + ", x=" + + Arrays.toString(xValues) + + ", y=" + + Arrays.toString(yValues) $else$ + ", values=" + Arrays.toString(values) diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayVector.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayVector.java.st index b460a6d7dc8e2..025dd403059f5 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayVector.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ArrayVector.java.st @@ -13,16 +13,6 @@ import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.common.util.BytesRefArray; import org.elasticsearch.core.Releasables; -$elseif(Point)$ -import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; - -import java.util.Arrays; - -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_ARRAY_HEADER; -import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF; -import static org.apache.lucene.util.RamUsageEstimator.alignObjectSize; - $else$ import org.apache.lucene.util.RamUsageEstimator; @@ -30,7 +20,7 @@ import java.util.Arrays; $endif$ /** - * Vector implementation that stores an array of $type$ values. + * Vector implementation that stores an array of $Type$ values. * This class is generated. Do not edit it. */ public final class $Type$ArrayVector extends AbstractVector implements $Type$Vector { @@ -40,19 +30,27 @@ public final class $Type$ArrayVector extends AbstractVector implements $Type$Vec $if(BytesRef)$ private final BytesRefArray values; +$elseif(Point)$ + private final double[] xValues, yValues; + $else$ private final $type$[] values; $endif$ private final $Type$Block block; - public $Type$ArrayVector($if(BytesRef)$BytesRefArray$else$$type$[]$endif$ values, int positionCount) { - this(values, positionCount, BlockFactory.getNonBreakingInstance()); + public $Type$ArrayVector($typeArrayValues$, int positionCount) { + this($arrayValues$, positionCount, BlockFactory.getNonBreakingInstance()); } - public $Type$ArrayVector($if(BytesRef)$BytesRefArray$else$$type$[]$endif$ values, int positionCount, BlockFactory blockFactory) { + public $Type$ArrayVector($typeArrayValues$, int positionCount, BlockFactory blockFactory) { super(positionCount, blockFactory); +$if(Point)$ + this.xValues = xValues; + this.yValues = yValues; +$else$ this.values = values; +$endif$ this.block = new $Type$VectorBlock(this); } @@ -67,6 +65,17 @@ $if(BytesRef)$ return values.get(position, dest); } +$elseif(Point)$ + @Override + public double getX(int position) { + return xValues[position]; + } + + @Override + public double getY(int position) { + return yValues[position]; + } + $else$ @Override public $type$ get$Type$(int position) { @@ -93,6 +102,8 @@ $endif$ for (int pos : positions) { $if(BytesRef)$ builder.append$Type$(values.get(pos, scratch)); + $elseif(Point)$ + builder.append$Type$(xValues[pos], yValues[pos]); $else$ builder.append$Type$(values[pos]); $endif$ @@ -101,10 +112,9 @@ $endif$ } } - public static long ramBytesEstimated($if(BytesRef)$BytesRefArray$else$$type$[]$endif$ values) { + public static long ramBytesEstimated($typeArrayValues$) { $if(Point)$ - long valuesEstimate = (long) NUM_BYTES_ARRAY_HEADER + ((long) Long.BYTES * 2 + (long) NUM_BYTES_OBJECT_REF) * values.length; - return BASE_RAM_BYTES_USED + alignObjectSize(valuesEstimate); + return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(xValues) * 2; $else$ return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values); $endif$ @@ -112,7 +122,7 @@ $endif$ @Override public long ramBytesUsed() { - return ramBytesEstimated(values); + return ramBytesEstimated($arrayValues$); } @Override @@ -132,6 +142,15 @@ $endif$ public String toString() { $if(BytesRef)$ return getClass().getSimpleName() + "[positions=" + getPositionCount() + ']'; +$elseif(Point)$ + return getClass().getSimpleName() + + "[positions=" + + getPositionCount() + + ", x=" + + Arrays.toString(xValues) + + ", y=" + + Arrays.toString(yValues) + + ']'; $else$ return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", values=" + Arrays.toString(values) + ']'; $endif$ diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BigArrayVector.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BigArrayVector.java.st index 12fcb322d6f86..959f5f91c1c53 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BigArrayVector.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BigArrayVector.java.st @@ -8,12 +8,7 @@ package org.elasticsearch.compute.data; import org.apache.lucene.util.RamUsageEstimator; -$if(Point)$ -import org.elasticsearch.common.geo.SpatialPoint; -import org.elasticsearch.common.util.ObjectArray; -$else$ import org.elasticsearch.common.util.$Array$; -$endif$ import org.elasticsearch.core.Releasable; /** @@ -24,17 +19,30 @@ public final class $Type$BigArrayVector extends AbstractVector implements $Type$ private static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance($Type$BigArrayVector.class); - private final $Array$ values; + private final $Array$ $arrayValues$; private final $Type$Block block; +$if(Point)$ + public $Type$BigArrayVector($Array$ xValues, $Array$ yValues, int positionCount) { +$else$ public $Type$BigArrayVector($Array$ values, int positionCount) { - this(values, positionCount, BlockFactory.getNonBreakingInstance()); +$endif$ + this($arrayValues$, positionCount, BlockFactory.getNonBreakingInstance()); } +$if(Point)$ + public $Type$BigArrayVector($Array$ xValues, $Array$ yValues, int positionCount, BlockFactory blockFactory) { +$else$ public $Type$BigArrayVector($Array$ values, int positionCount, BlockFactory blockFactory) { +$endif$ super(positionCount, blockFactory); +$if(Point)$ + this.xValues = xValues; + this.yValues = yValues; +$else$ this.values = values; +$endif$ this.block = new $Type$VectorBlock(this); } @@ -43,11 +51,24 @@ public final class $Type$BigArrayVector extends AbstractVector implements $Type$ return block; } +$if(Point)$ + @Override + public double getX(int position) { + return xValues.get(position); + } + + @Override + public double getY(int position) { + return yValues.get(position); + } + +$else$ @Override public $type$ get$Type$(int position) { return values.get(position); } +$endif$ @Override public ElementType elementType() { return ElementType.$TYPE$; @@ -60,7 +81,11 @@ public final class $Type$BigArrayVector extends AbstractVector implements $Type$ @Override public long ramBytesUsed() { +$if(Point)$ + return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(xValues) * 2; +$else$ return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values); +$endif$ } @Override @@ -69,7 +94,8 @@ public final class $Type$BigArrayVector extends AbstractVector implements $Type$ $if(boolean)$ final BitArray filtered = new BitArray(positions.length, blockFactory.bigArrays()); $elseif(Point)$ - final $Array$ filtered = blockFactory.bigArrays().newObjectArray(positions.length); + final $Array$ xFiltered = blockFactory.bigArrays().new$Array$(positions.length); + final $Array$ yFiltered = blockFactory.bigArrays().new$Array$(positions.length); $else$ final $Array$ filtered = blockFactory.bigArrays().new$Array$(positions.length); $endif$ @@ -79,12 +105,21 @@ public final class $Type$BigArrayVector extends AbstractVector implements $Type$ filtered.set(i); } } + $elseif(Point)$ + for (int i = 0; i < positions.length; i++) { + xFiltered.set(i, xValues.get(positions[i])); + yFiltered.set(i, yValues.get(positions[i])); + } $else$ for (int i = 0; i < positions.length; i++) { filtered.set(i, values.get(positions[i])); } $endif$ + $if(Point)$ + return new $Type$BigArrayVector(xFiltered, yFiltered, positions.length, blockFactory); + $else$ return new $Type$BigArrayVector(filtered, positions.length, blockFactory); + $endif$ } @Override @@ -93,7 +128,12 @@ public final class $Type$BigArrayVector extends AbstractVector implements $Type$ throw new IllegalStateException("can't release already released vector [" + this + "]"); } released = true; +$if(Point)$ + xValues.close(); + yValues.close(); +$else$ values.close(); +$endif$ } @Override @@ -111,6 +151,10 @@ public final class $Type$BigArrayVector extends AbstractVector implements $Type$ @Override public String toString() { +$if(Point)$ + return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", x=" + xValues + ", y=" + yValues + ']'; +$else$ return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", values=" + values + ']'; +$endif$ } } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Block.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Block.java.st index d8b57ae65137b..e185bb7b44e73 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Block.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Block.java.st @@ -21,7 +21,7 @@ import org.elasticsearch.index.mapper.BlockLoader; import java.io.IOException; /** - * Block that stores $type$ values. + * Block that stores $Type$ values. * This class is generated. Do not edit it. */ public sealed interface $Type$Block extends Block permits $Type$ArrayBlock, $Type$VectorBlock, ConstantNullBlock { @@ -31,7 +31,7 @@ $if(BytesRef)$ $endif$ /** - * Retrieves the $type$ value stored at the given value index. + * Retrieves the $typeValue$ stored at the given value index. * *

Values for a given position are between getFirstValueIndex(position) (inclusive) and * getFirstValueIndex(position) + getValueCount(position) (exclusive). @@ -45,6 +45,15 @@ $endif$ $if(BytesRef)$ BytesRef getBytesRef(int valueIndex, BytesRef dest); +$elseif(Point)$ + $type$ getX(int valueIndex); + + $type$ getY(int valueIndex); + + default SpatialPoint get$Type$(int valueIndex) { + return new SpatialPoint(getX(valueIndex), getY(valueIndex)); + } + $else$ $type$ get$Type$(int valueIndex); $endif$ @@ -81,7 +90,7 @@ $endif$ builder.beginPositionEntry(); for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) { $if(Point)$ - builder.appendPoint(new SpatialPoint(in.readDouble(), in.readDouble())); + builder.appendPoint(in.readDouble(), in.readDouble()); $else$ builder.append$Type$(in.read$Type$()); $endif$ @@ -113,9 +122,8 @@ $endif$ $if(BytesRef)$ out.write$Type$(get$Type$(getFirstValueIndex(pos) + valueIndex, new BytesRef())); $elseif(Point)$ - SpatialPoint point = getPoint(getFirstValueIndex(pos) + valueIndex); - out.writeDouble(point.getX()); - out.writeDouble(point.getY()); + out.writeDouble(getX(getFirstValueIndex(pos) + valueIndex)); + out.writeDouble(getY(getFirstValueIndex(pos) + valueIndex)); $else$ out.write$Type$(get$Type$(getFirstValueIndex(pos) + valueIndex)); $endif$ @@ -167,7 +175,8 @@ $if(BytesRef)$ if (block1.getBytesRef(b1ValueIdx + valueIndex, new BytesRef()) .equals(block2.getBytesRef(b2ValueIdx + valueIndex, new BytesRef())) == false) { $elseif(Point)$ - if (block1.get$Type$(b1ValueIdx + valueIndex).equals(block2.get$Type$(b2ValueIdx + valueIndex)) == false) { + if (block1.getX(b1ValueIdx + valueIndex) != block2.getX(b2ValueIdx + valueIndex) + || block1.getY(b1ValueIdx + valueIndex) != block2.getY(b2ValueIdx + valueIndex)) { $else$ if (block1.get$Type$(b1ValueIdx + valueIndex) != block2.get$Type$(b2ValueIdx + valueIndex)) { $endif$ @@ -209,10 +218,9 @@ $elseif(double)$ long element = Double.doubleToLongBits(block.getDouble(firstValueIdx + valueIndex)); result = 31 * result + (int) (element ^ (element >>> 32)); $elseif(Point)$ - $type$ value = block.get$Type$(firstValueIdx + valueIndex); - long element = Double.doubleToLongBits(value.getX()); + long element = Double.doubleToLongBits(block.getX(firstValueIdx + valueIndex)); result = 31 * result + (int) (element ^ (element >>> 32)); - element = Double.doubleToLongBits(value.getY()); + element = Double.doubleToLongBits(block.getY(firstValueIdx + valueIndex)); result = 31 * result + (int) (element ^ (element >>> 32)); $endif$ } @@ -246,8 +254,8 @@ $endif$ */ // Eventually, we want to remove this entirely, always passing an explicit BlockFactory @Deprecated - static $Type$Block newConstantBlockWith($type$ value, int positions) { - return newConstantBlockWith(value, positions, BlockFactory.getNonBreakingInstance()); + static $Type$Block newConstantBlockWith($typeValue$, int positions) { + return newConstantBlockWith($value$, positions, BlockFactory.getNonBreakingInstance()); } /** @@ -255,8 +263,8 @@ $endif$ * @deprecated use {@link BlockFactory#newConstant$Type$BlockWith} */ @Deprecated - static $Type$Block newConstantBlockWith($type$ value, int positions, BlockFactory blockFactory) { - return blockFactory.newConstant$Type$BlockWith(value, positions); + static $Type$Block newConstantBlockWith($typeValue$, int positions, BlockFactory blockFactory) { + return blockFactory.newConstant$Type$BlockWith($value$, positions); } /** @@ -267,7 +275,7 @@ $endif$ * Appends a $type$ to the current entry. */ @Override - Builder append$Type$($type$ value); + Builder append$Type$($typeValue$); /** * Copy the values in {@code block} from {@code beginInclusive} to @@ -291,14 +299,14 @@ $endif$ Builder mvOrdering(Block.MvOrdering mvOrdering); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ @Override Builder appendAllValuesToCurrentPosition(Block block); /** - * Appends the all values of the given block into a the current position + * Appends the all values of the given block into the current position * in this builder. */ Builder appendAllValuesToCurrentPosition($Type$Block block); diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BlockBuilder.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BlockBuilder.java.st index d6f1e07754003..3473399061dea 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BlockBuilder.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-BlockBuilder.java.st @@ -14,13 +14,6 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.BytesRefArray; import org.elasticsearch.core.Releasables; -$elseif(Point)$ -import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.breaker.CircuitBreakingException; -import org.elasticsearch.common.geo.SpatialPoint; - -import java.util.Arrays; - $else$ import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.common.breaker.CircuitBreakingException; @@ -46,8 +39,19 @@ $if(BytesRef)$ values = new BytesRefArray(Math.max(estimatedSize, 2), bigArrays); } +$elseif(Point)$ + private $type$[] $arrayValues$; + + $Type$BlockBuilder(int estimatedSize, BlockFactory blockFactory) { + super(blockFactory); + int initialSize = Math.max(estimatedSize, 2); + adjustBreaker(RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + initialSize * elementSize()); + xValues = new $type$[initialSize]; + yValues = new $type$[initialSize]; + } + $else$ - private $type$[] values; + private $type$[] $arrayValues$; $Type$BlockBuilder(int estimatedSize, BlockFactory blockFactory) { super(blockFactory); @@ -58,10 +62,13 @@ $else$ $endif$ @Override - public $Type$BlockBuilder append$Type$($type$ value) { + public $Type$BlockBuilder append$Type$($typeValue$) { ensureCapacity(); $if(BytesRef)$ values.append(value); +$elseif(Point)$ + xValues[valueCount] = x; + yValues[valueCount] = y; $else$ values[valueCount] = value; $endif$ @@ -80,6 +87,8 @@ $endif$ protected int valuesLength() { $if(BytesRef)$ return Integer.MAX_VALUE; // allow the BytesRefArray through its own append +$elseif(Point)$ + return xValues.length; $else$ return values.length; $endif$ @@ -89,6 +98,9 @@ $endif$ protected void growValuesArray(int newSize) { $if(BytesRef)$ throw new AssertionError("should not reach here"); +$elseif(Point)$ + xValues = Arrays.copyOf(xValues, newSize); + yValues = Arrays.copyOf(yValues, newSize); $else$ values = Arrays.copyOf(values, newSize); $endif$ @@ -156,6 +168,8 @@ $endif$ for (int p = 0; p < positionCount; p++) { $if(BytesRef)$ appendBytesRef(vector.getBytesRef(p, scratch)); +$elseif(Point)$ + append$Type$(vector.getX(p), vector.getY(p)); $else$ append$Type$(vector.get$Type$(p)); $endif$ @@ -164,11 +178,13 @@ $endif$ for (int p = 0; p < positionCount; p++) { int count = block.getValueCount(p); int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { + for (int v = 0; v < count; v++, i++) { $if(BytesRef)$ - appendBytesRef(block.getBytesRef(i++, scratch)); + appendBytesRef(block.getBytesRef(i, scratch)); +$elseif(Point)$ + append$Type$(block.getX(i), block.getY(i)); $else$ - append$Type$(block.get$Type$(i++)); + append$Type$(block.get$Type$(i)); $endif$ } } @@ -221,11 +237,13 @@ $endif$ beginPositionEntry(); } int i = block.getFirstValueIndex(p); - for (int v = 0; v < count; v++) { + for (int v = 0; v < count; v++, i++) { $if(BytesRef)$ - appendBytesRef(block.getBytesRef(i++, scratch)); + appendBytesRef(block.getBytesRef(i, scratch)); +$elseif(Point)$ + append$Type$(block.getX(i), block.getY(i)); $else$ - append$Type$(block.get$Type$(i++)); + append$Type$(block.get$Type$(i)); $endif$ } if (count > 1) { @@ -241,6 +259,8 @@ $endif$ for (int p = beginInclusive; p < endExclusive; p++) { $if(BytesRef)$ appendBytesRef(vector.getBytesRef(p, scratch)); +$elseif(Point)$ + append$Type$(vector.getX(p), vector.getY(p)); $else$ append$Type$(vector.get$Type$(p)); $endif$ @@ -291,16 +311,21 @@ $endif$ values = null; $else$ if (hasNonNullValue && positionCount == 1 && valueCount == 1) { - theBlock = blockFactory.newConstant$Type$BlockWith(values[0], 1, estimatedBytes); + theBlock = blockFactory.newConstant$Type$BlockWith($if(Point)$xValues[0], yValues[0]$else$values[0]$endif$, 1, estimatedBytes); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } if (isDense() && singleValued()) { - theBlock = blockFactory.new$Type$ArrayVector(values, positionCount, estimatedBytes).asBlock(); + theBlock = blockFactory.new$Type$ArrayVector($arrayValues$, positionCount, estimatedBytes).asBlock(); } else { theBlock = blockFactory.new$Type$ArrayBlock( - values, +$if(Point)$ + xValues, + yValues, +$else$ + $arrayValues$, +$endif$ positionCount, firstValueIndexes, nullsMask, diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ConstantVector.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ConstantVector.java.st index 670cac921e046..767866020f43e 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ConstantVector.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-ConstantVector.java.st @@ -9,15 +9,8 @@ package org.elasticsearch.compute.data; $if(BytesRef)$ import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.RamUsageEstimator; - -$elseif(Point)$ -import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; - -$else$ -import org.apache.lucene.util.RamUsageEstimator; $endif$ +import org.apache.lucene.util.RamUsageEstimator; /** * Vector implementation that stores a constant $type$ value. @@ -32,27 +25,45 @@ $else$ static final long RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(Constant$Type$Vector.class); $endif$ - private final $type$ value; +$if(Point)$ + private final double x, y; +$else$ + private final $typeValue$; +$endif$ private final $Type$Block block; - public Constant$Type$Vector($type$ value, int positionCount) { - this(value, positionCount, BlockFactory.getNonBreakingInstance()); + public Constant$Type$Vector($typeValue$, int positionCount) { + this($value$, positionCount, BlockFactory.getNonBreakingInstance()); } - public Constant$Type$Vector($type$ value, int positionCount, BlockFactory blockFactory) { + public Constant$Type$Vector($typeValue$, int positionCount, BlockFactory blockFactory) { super(positionCount, blockFactory); +$if(Point)$ + this.x = x; + this.y = y; +$else$ this.value = value; +$endif$ this.block = new $Type$VectorBlock(this); } @Override $if(BytesRef)$ public BytesRef getBytesRef(int position, BytesRef ignore) { + return value; +$elseif(Point)$ + public double getX(int position) { + return x; + } + + @Override + public double getY(int position) { + return y; $else$ public $type$ get$Type$(int position) { -$endif$ return value; +$endif$ } @Override @@ -62,7 +73,7 @@ $endif$ @Override public $Type$Vector filter(int... positions) { - return new Constant$Type$Vector(value, positions.length); + return new Constant$Type$Vector($value$, positions.length); } @Override @@ -106,7 +117,11 @@ $endif$ } public String toString() { +$if(Point)$ + return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", x=" + x + ", y=" + y + ']'; +$else$ return getClass().getSimpleName() + "[positions=" + getPositionCount() + ", value=" + value + ']'; +$endif$ } @Override diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Vector.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Vector.java.st index 30a04d2137aa8..fc418c8b27e2c 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Vector.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-Vector.java.st @@ -19,7 +19,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import java.io.IOException; /** - * Vector that stores $type$ values. + * Vector that stores $Type$ values. * This class is generated. Do not edit it. */ $if(BytesRef)$ @@ -37,6 +37,15 @@ $endif$ $if(BytesRef)$ BytesRef getBytesRef(int position, BytesRef dest); +$elseif(Point)$ + double getX(int position); + + double getY(int position); + + default SpatialPoint get$Type$(int valueIndex) { + return new SpatialPoint(getX(valueIndex), getY(valueIndex)); + } + $else$ $type$ get$Type$(int position); $endif$ @@ -73,7 +82,7 @@ $endif$ $if(BytesRef)$ if (vector1.getBytesRef(pos, new BytesRef()).equals(vector2.getBytesRef(pos, new BytesRef())) == false) { $elseif(Point)$ - if (vector1.getPoint(pos).equals(vector2.getPoint(pos)) == false) { + if (vector1.getX(pos) != vector2.getY(pos) || vector1.getY(pos) != vector2.getY(pos)) { $else$ if (vector1.get$Type$(pos) != vector2.get$Type$(pos)) { $endif$ @@ -106,10 +115,11 @@ $elseif(double)$ long element = Double.doubleToLongBits(vector.getDouble(pos)); result = 31 * result + (int) (element ^ (element >>> 32)); $elseif(Point)$ - SpatialPoint point = vector.getPoint(pos); - long element = Double.doubleToLongBits(point.getX()); + double x = vector.getX(pos); + double y = vector.getY(pos); + long element = Double.doubleToLongBits(x); result = 31 * result + (int) (element ^ (element >>> 32)); - element = Double.doubleToLongBits(point.getY()); + element = Double.doubleToLongBits(y); result = 31 * result + (int) (element ^ (element >>> 32)); $endif$ } @@ -122,7 +132,7 @@ $endif$ final boolean constant = in.readBoolean(); if (constant && positions > 0) { $if(Point)$ - return blockFactory.newConstantPointVector(new SpatialPoint(in.readDouble(), in.readDouble()), positions); + return blockFactory.newConstant$Type$Vector(in.readDouble(), in.readDouble(), positions); $else$ return blockFactory.newConstant$Type$Vector(in.read$Type$(), positions); $endif$ @@ -130,7 +140,7 @@ $endif$ try (var builder = blockFactory.new$Type$Vector$if(BytesRef)$$else$Fixed$endif$Builder(positions)) { for (int i = 0; i < positions; i++) { $if(Point)$ - builder.appendPoint(new SpatialPoint(in.readDouble(), in.readDouble())); + builder.append$Type$(in.readDouble(), in.readDouble()); $else$ builder.append$Type$(in.read$Type$()); $endif$ @@ -149,9 +159,8 @@ $endif$ $if(BytesRef)$ out.write$Type$(get$Type$(0, new BytesRef())); $elseif(Point)$ - SpatialPoint point = getPoint(0); - out.writeDouble(point.getX()); - out.writeDouble(point.getY()); + out.writeDouble(getX(0)); + out.writeDouble(getY(0)); $else$ out.write$Type$(get$Type$(0)); $endif$ @@ -160,9 +169,8 @@ $endif$ $if(BytesRef)$ out.write$Type$(get$Type$(i, new BytesRef())); $elseif(Point)$ - SpatialPoint point = getPoint(i); - out.writeDouble(point.getX()); - out.writeDouble(point.getY()); + out.writeDouble(getX(i)); + out.writeDouble(getY(i)); $else$ out.write$Type$(get$Type$(i)); $endif$ @@ -228,7 +236,7 @@ $endif$ /** * Appends a $type$ to the current entry. */ - Builder append$Type$($type$ value); + Builder append$Type$($typeValue$); @Override $Type$Vector build(); @@ -243,7 +251,7 @@ $else$ /** * Appends a $type$ to the current entry. */ - FixedBuilder append$Type$($type$ value); + FixedBuilder append$Type$($typeValue$); @Override $Type$Vector build(); diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBlock.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBlock.java.st index 665f270445748..57ee32a50573d 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBlock.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBlock.java.st @@ -10,9 +10,6 @@ package org.elasticsearch.compute.data; $if(BytesRef)$ import org.apache.lucene.util.BytesRef; $endif$ -$if(Point)$ -import org.elasticsearch.common.geo.SpatialPoint; -$endif$ import org.elasticsearch.core.Releasables; /** @@ -40,6 +37,14 @@ public final class $Type$VectorBlock extends AbstractVectorBlock implements $Typ $if(BytesRef)$ public BytesRef getBytesRef(int valueIndex, BytesRef dest) { return vector.getBytesRef(valueIndex, dest); +$elseif(Point)$ + public double getX(int valueIndex) { + return vector.getX(valueIndex); + } + + @Override + public double getY(int valueIndex) { + return vector.getY(valueIndex); $else$ public $type$ get$Type$(int valueIndex) { return vector.get$Type$(valueIndex); diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBuilder.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBuilder.java.st index a052bd3f3c8bc..c06ed5b3b9996 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBuilder.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorBuilder.java.st @@ -13,11 +13,6 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.BytesRefArray; import org.elasticsearch.core.Releasables; -$elseif(Point)$ -import org.elasticsearch.common.geo.SpatialPoint; - -import java.util.Arrays; - $else$ import java.util.Arrays; $endif$ @@ -40,8 +35,19 @@ $if(BytesRef)$ values = new BytesRefArray(Math.max(estimatedSize, 2), bigArrays); } +$elseif(Point)$ + private $type$[] $arrayValues$; + + $Type$VectorBuilder(int estimatedSize, BlockFactory blockFactory) { + super(blockFactory); + int initialSize = Math.max(estimatedSize, 2); + adjustBreaker(initialSize); + xValues = new $type$[Math.max(estimatedSize, 2)]; + yValues = new $type$[Math.max(estimatedSize, 2)]; + } + $else$ - private $type$[] values; + private $type$[] $arrayValues$; $Type$VectorBuilder(int estimatedSize, BlockFactory blockFactory) { super(blockFactory); @@ -52,10 +58,13 @@ $else$ $endif$ @Override - public $Type$VectorBuilder append$Type$($type$ value) { + public $Type$VectorBuilder append$Type$($typeValue$) { ensureCapacity(); $if(BytesRef)$ values.append(value); +$elseif(Point)$ + xValues[valueCount] = x; + yValues[valueCount] = y; $else$ values[valueCount] = value; $endif$ @@ -72,6 +81,8 @@ $endif$ protected int valuesLength() { $if(BytesRef)$ return Integer.MAX_VALUE; // allow the BytesRefArray through its own append +$elseif(Point)$ + return xValues.length; $else$ return values.length; $endif$ @@ -81,6 +92,9 @@ $endif$ protected void growValuesArray(int newSize) { $if(BytesRef)$ throw new AssertionError("should not reach here"); +$elseif(Point)$ + xValues = Arrays.copyOf(xValues, newSize); + yValues = Arrays.copyOf(yValues, newSize); $else$ values = Arrays.copyOf(values, newSize); $endif$ @@ -117,14 +131,23 @@ $if(BytesRef)$ blockFactory.adjustBreaker(vector.ramBytesUsed() - values.bigArraysRamBytesUsed(), false); } values = null; +$elseif(Point)$ + if (valueCount == 1) { + vector = blockFactory.newConstant$Type$BlockWith(xValues[0], yValues[0], 1, estimatedBytes).asVector(); + } else { + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); + } + vector = blockFactory.new$Type$ArrayVector($arrayValues$, valueCount, estimatedBytes); + } $else$ if (valueCount == 1) { vector = blockFactory.newConstant$Type$BlockWith(values[0], 1, estimatedBytes).asVector(); } else { - if (values.length - valueCount > 1024 || valueCount < (values.length / 2)) { - values = Arrays.copyOf(values, valueCount); + if (valuesLength() - valueCount > 1024 || valueCount < (valuesLength() / 2)) { + growValuesArray(valueCount); } - vector = blockFactory.new$Type$ArrayVector(values, valueCount, estimatedBytes); + vector = blockFactory.new$Type$ArrayVector($arrayValues$, valueCount, estimatedBytes); } $endif$ built(); diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorFixedBuilder.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorFixedBuilder.java.st index 04932de506786..60858c9cb7341 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorFixedBuilder.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/X-VectorFixedBuilder.java.st @@ -7,13 +7,7 @@ package org.elasticsearch.compute.data; -$if(Point)$ import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; - -$else$ -import org.apache.lucene.util.RamUsageEstimator; -$endif$ /** * Builder for {@link $Type$Vector}s that never grows. Prefer this to @@ -23,7 +17,7 @@ $endif$ */ final class $Type$VectorFixedBuilder implements $Type$Vector.FixedBuilder { private final BlockFactory blockFactory; - private final $type$[] values; + private final $type$[] $arrayValues$; private final long preAdjustedBytes; /** * The next value to write into. {@code -1} means the vector has already @@ -35,28 +29,38 @@ final class $Type$VectorFixedBuilder implements $Type$Vector.FixedBuilder { preAdjustedBytes = ramBytesUsed(size); blockFactory.adjustBreaker(preAdjustedBytes, false); this.blockFactory = blockFactory; +$if(Point)$ + this.xValues = new $type$[size]; + this.yValues = new $type$[size]; +$else$ this.values = new $type$[size]; +$endif$ } @Override - public $Type$VectorFixedBuilder append$Type$($type$ value) { + public $Type$VectorFixedBuilder append$Type$($typeValue$) { +$if(Point)$ + xValues[nextIndex++] = x; + yValues[nextIndex++] = y; +$else$ values[nextIndex++] = value; +$endif$ return this; } private static long ramBytesUsed(int size) { -$if(Point)$ - return size == 1 - ? ConstantPointVector.RAM_BYTES_USED - : PointArrayVector.BASE_RAM_BYTES_USED + RamUsageEstimator.alignObjectSize( - (long) RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + (long) size * ($BYTES$ + RamUsageEstimator.NUM_BYTES_OBJECT_REF) - ); -$else$ return size == 1 ? Constant$Type$Vector.RAM_BYTES_USED : $Type$ArrayVector.BASE_RAM_BYTES_USED + RamUsageEstimator.alignObjectSize( (long) RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + size * $BYTES$ ); + } + + private int valuesLength() { +$if(Point)$ + return xValues.length; +$else$ + return values.length; $endif$ } @@ -65,15 +69,15 @@ $endif$ if (nextIndex < 0) { throw new IllegalStateException("already closed"); } - if (nextIndex != values.length) { - throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); + if (nextIndex != valuesLength()) { + throw new IllegalStateException("expected to write [" + valuesLength() + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; $Type$Vector vector; - if (values.length == 1) { - vector = blockFactory.newConstant$Type$BlockWith(values[0], 1, preAdjustedBytes).asVector(); + if (valuesLength() == 1) { + vector = blockFactory.newConstant$Type$BlockWith($if(Point)$xValues[0], yValues[0]$else$values[0]$endif$, 1, preAdjustedBytes).asVector(); } else { - vector = blockFactory.new$Type$ArrayVector(values, values.length, preAdjustedBytes); + vector = blockFactory.new$Type$ArrayVector($arrayValues$, valuesLength(), preAdjustedBytes); } assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; return vector; diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/BatchEncoder.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/BatchEncoder.java index 3a48078cf0b2d..33db80d06e5db 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/BatchEncoder.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/BatchEncoder.java @@ -12,7 +12,6 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefBuilder; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.compute.data.Block; import org.elasticsearch.compute.data.BooleanBlock; import org.elasticsearch.compute.data.BytesRefBlock; @@ -531,11 +530,11 @@ protected final void ensureCapacity(int count) { /** * Encode a {@code SpatialPoint} and advance to the next position. */ - protected final void encode(SpatialPoint v) { + protected final void encode(double x, double y) { addingValue(); - doubleHandle.set(bytes.bytes(), bytes.length(), v.getX()); + doubleHandle.set(bytes.bytes(), bytes.length(), x); bytes.setLength(bytes.length() + Double.BYTES); - doubleHandle.set(bytes.bytes(), bytes.length(), v.getY()); + doubleHandle.set(bytes.bytes(), bytes.length(), y); bytes.setLength(bytes.length() + Double.BYTES); } } @@ -550,9 +549,8 @@ protected int readValueAtBlockIndex(int valueIndex, BytesRefBuilder dst) { int before = dst.length(); int after = before + Double.BYTES * 2; dst.grow(after); - SpatialPoint v = ((PointBlock) block).getPoint(valueIndex); - doubleHandle.set(dst.bytes(), before, v.getX()); - doubleHandle.set(dst.bytes(), before + Double.BYTES, v.getY()); + doubleHandle.set(dst.bytes(), before, ((PointBlock) block).getX(valueIndex)); + doubleHandle.set(dst.bytes(), before + Double.BYTES, ((PointBlock) block).getY(valueIndex)); dst.setLength(after); return Double.BYTES * 2; } @@ -573,7 +571,7 @@ public void decode(Block.Builder builder, IsNull isNull, BytesRef[] encoded, int double y = (double) doubleHandle.get(e.bytes, e.offset); e.offset += Double.BYTES; e.length -= Double.BYTES; - b.appendPoint(new SpatialPoint(x, y)); + b.appendPoint(x, y); } } } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/X-MultivalueDedupe.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/X-MultivalueDedupe.java.st index fa86355949d98..a45dd0e68eb45 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/X-MultivalueDedupe.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/X-MultivalueDedupe.java.st @@ -60,7 +60,12 @@ $else$ $endif$ private final $Type$Block block; +$if(Point)$ + // TODO: figure out a way to remove this object array, currently needed for Arrays.sort() below + private SpatialPoint[] work = new SpatialPoint[ArrayUtil.oversize(2, $BYTES$)]; +$else$ private $type$[] work = new $type$[ArrayUtil.oversize(2, $BYTES$)]; +$endif$ private int w; public MultivalueDedupe$Type$($Type$Block block) { @@ -88,6 +93,8 @@ $endif$ case 0 -> builder.appendNull(); $if(BytesRef)$ case 1 -> builder.appendBytesRef(block.getBytesRef(first, work[0])); + $elseif(Point)$ + case 1 -> builder.append$Type$(block.getX(first), block.getY(first)); $else$ case 1 -> builder.append$Type$(block.get$Type$(first)); $endif$ @@ -142,6 +149,8 @@ $endif$ case 0 -> builder.appendNull(); $if(BytesRef)$ case 1 -> builder.appendBytesRef(block.getBytesRef(first, work[0])); + $elseif(Point)$ + case 1 -> builder.append$Type$(block.getX(first), block.getY(first)); $else$ case 1 -> builder.append$Type$(block.get$Type$(first)); $endif$ @@ -176,6 +185,8 @@ $endif$ case 0 -> builder.appendNull(); $if(BytesRef)$ case 1 -> builder.appendBytesRef(block.getBytesRef(first, work[0])); + $elseif(Point)$ + case 1 -> builder.appendPoint(block.getX(first), block.getY(first)); $else$ case 1 -> builder.append$Type$(block.get$Type$(first)); $endif$ @@ -210,11 +221,14 @@ $endif$ } case 1 -> { $if(BytesRef)$ - BytesRef v = block.getBytesRef(first, work[0]); + BytesRef value = block.getBytesRef(first, work[0]); +$elseif(Point)$ + double x = block.getX(first); + double y = block.getY(first); $else$ - $type$ v = block.get$Type$(first); + $typeValue$ = block.get$Type$(first); $endif$ - hash(builder, hash, v); + hash(builder, hash, $value$); } default -> { if (count < ALWAYS_COPY_MISSING) { @@ -260,17 +274,26 @@ $endif$ case 0 -> encodeNull(); case 1 -> { $if(BytesRef)$ - BytesRef v = block.getBytesRef(first, work[0]); - if (hasCapacity(v.length, 1)) { + BytesRef value = block.getBytesRef(first, work[0]); + if (hasCapacity(value.length, 1)) { +$elseif(Point)$ + double x = block.getX(first); + double y = block.getY(first); + if (hasCapacity(1)) { $else$ - $type$ v = block.get$Type$(first); + $typeValue$ = block.get$Type$(first); if (hasCapacity(1)) { $endif$ startPosition(); - encode(v); + encode($value$); endPosition(); } else { - work[0] = v; +$if(Point)$ + // We should be able to replace this with two double[] if we can figure out the sorting below + work[0] = new SpatialPoint(x, y); +$else$ + work[0] = value; +$endif$ w = 1; return; } @@ -323,6 +346,8 @@ $endif$ $if(BytesRef)$ work[w] = block.getBytesRef(i, work[w]); w++; +$elseif(Point)$ + work[w++] = new SpatialPoint(block.getX(i), block.getY(i)); $else$ work[w++] = block.get$Type$(i); $endif$ @@ -341,6 +366,8 @@ $endif$ $if(BytesRef)$ work[0] = block.getBytesRef(first, work[0]); +$elseif(Point)$ + work[0] = new SpatialPoint(block.getX(first), block.getY(first)); $else$ work[0] = block.get$Type$(first); $endif$ @@ -348,6 +375,8 @@ $endif$ i: for (int i = first + 1; i < end; i++) { $if(BytesRef)$ $type$ v = block.getBytesRef(i, work[w]); +$elseif(Point)$ + SpatialPoint v = new SpatialPoint(block.getX(i), block.getY(i)); $else$ $type$ v = block.get$Type$(i); $endif$ @@ -371,12 +400,20 @@ $endif$ */ private void writeUniquedWork($Type$Block.Builder builder) { if (w == 1) { +$if(Point)$ + builder.appendPoint(work[0].getX(), work[0].getY()); +$else$ builder.append$Type$(work[0]); +$endif$ return; } builder.beginPositionEntry(); for (int i = 0; i < w; i++) { +$if(Point)$ + builder.appendPoint(work[i].getX(), work[i].getY()); +$else$ builder.append$Type$(work[i]); +$endif$ } builder.endPositionEntry(); } @@ -386,12 +423,21 @@ $endif$ */ private void writeSortedWork($Type$Block.Builder builder) { if (w == 1) { +$if(Point)$ + builder.appendPoint(work[0].getX(), work[0].getY()); +$else$ builder.append$Type$(work[0]); +$endif$ return; } builder.beginPositionEntry(); +$if(Point)$ + SpatialPoint prev = work[0]; + builder.appendPoint(prev.getX(), prev.getY()); +$else$ $type$ prev = work[0]; builder.append$Type$(prev); +$endif$ for (int i = 1; i < w; i++) { $if(BytesRef)$ if (false == prev.equals(work[i])) { @@ -401,7 +447,11 @@ $else$ if (prev != work[i]) { $endif$ prev = work[i]; +$if(Point)$ + builder.appendPoint(prev.getX(), prev.getY()); +$else$ builder.append$Type$(prev); +$endif$ } } builder.endPositionEntry(); @@ -416,12 +466,20 @@ $else$ private void hashUniquedWork(LongHash hash, IntBlock.Builder builder) { $endif$ if (w == 1) { +$if(Point)$ + hash(builder, hash, work[0].getX(), work[0].getY()); +$else$ hash(builder, hash, work[0]); +$endif$ return; } builder.beginPositionEntry(); for (int i = 0; i < w; i++) { +$if(Point)$ + hash(builder, hash, work[i].getX(), work[i].getY()); +$else$ hash(builder, hash, work[i]); +$endif$ } builder.endPositionEntry(); } @@ -435,12 +493,21 @@ $else$ private void hashSortedWork(LongHash hash, IntBlock.Builder builder) { $endif$ if (w == 1) { +$if(Point)$ + hash(builder, hash, work[0].getX(), work[0].getY()); +$else$ hash(builder, hash, work[0]); +$endif$ return; } builder.beginPositionEntry(); +$if(Point)$ + SpatialPoint prev = work[0]; + hash(builder, hash, prev.getX(), prev.getY()); +$else$ $type$ prev = work[0]; hash(builder, hash, prev); +$endif$ for (int i = 1; i < w; i++) { $if(BytesRef)$ if (false == prev.equals(work[i])) { @@ -450,7 +517,11 @@ $else$ if (prev != work[i]) { $endif$ prev = work[i]; +$if(Point)$ + hash(builder, hash, prev.getX(), prev.getY()); +$else$ hash(builder, hash, prev); +$endif$ } } builder.endPositionEntry(); @@ -461,7 +532,11 @@ $endif$ */ private void encodeUniquedWork(BatchEncoder.$Type$s encoder) { for (int i = 0; i < w; i++) { +$if(Point)$ + encoder.encode(work[i].getX(), work[i].getY()); +$else$ encoder.encode(work[i]); +$endif$ } } @@ -469,7 +544,11 @@ $endif$ * Converts {@link #work} from sorted array to a deduplicated array. */ private void convertSortedWorkToUnique() { +$if(Point)$ + SpatialPoint prev = work[0]; +$else$ $type$ prev = work[0]; +$endif$ int end = w; w = 1; for (int i = 1; i < end; i++) { @@ -514,16 +593,16 @@ $if(BytesRef)$ $endif$ $if(BytesRef)$ - private void hash(IntBlock.Builder builder, BytesRefHash hash, BytesRef v) { + private void hash(IntBlock.Builder builder, BytesRefHash hash, BytesRef value) { $else$ - private void hash(IntBlock.Builder builder, LongHash hash, $type$ v) { + private void hash(IntBlock.Builder builder, LongHash hash, $typeValue$) { $endif$ $if(double)$ - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(Double.doubleToLongBits(v))))); + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(Double.doubleToLongBits(value))))); $elseif(Point)$ - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(v.hashCode())))); + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(31L * Double.hashCode(x) + Double.hashCode(y))))); $else$ - builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(v)))); + builder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroupNullReserved(hash.add(value)))); $endif$ } } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/DefaultUnsortableTopNEncoder.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/DefaultUnsortableTopNEncoder.java index 96b00f81e0826..023d52dc7bfba 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/DefaultUnsortableTopNEncoder.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/DefaultUnsortableTopNEncoder.java @@ -140,10 +140,10 @@ public double decodeDouble(BytesRef bytes) { } @Override - public void encodePoint(SpatialPoint value, BreakingBytesRefBuilder bytesRefBuilder) { + public void encodePoint(double x, double y, BreakingBytesRefBuilder bytesRefBuilder) { bytesRefBuilder.grow(bytesRefBuilder.length() + Double.BYTES * 2); - DOUBLE.set(bytesRefBuilder.bytes(), bytesRefBuilder.length(), value.getX()); - DOUBLE.set(bytesRefBuilder.bytes(), bytesRefBuilder.length() + Double.BYTES, value.getY()); + DOUBLE.set(bytesRefBuilder.bytes(), bytesRefBuilder.length(), x); + DOUBLE.set(bytesRefBuilder.bytes(), bytesRefBuilder.length() + Double.BYTES, y); bytesRefBuilder.setLength(bytesRefBuilder.length() + Long.BYTES * 2); } diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/SortableTopNEncoder.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/SortableTopNEncoder.java index 3848442a41766..0636b5605174d 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/SortableTopNEncoder.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/SortableTopNEncoder.java @@ -71,10 +71,10 @@ public final double decodeDouble(BytesRef bytes) { } @Override - public final void encodePoint(SpatialPoint value, BreakingBytesRefBuilder bytesRefBuilder) { + public final void encodePoint(double x, double y, BreakingBytesRefBuilder bytesRefBuilder) { bytesRefBuilder.grow(bytesRefBuilder.length() + Long.BYTES * 2); - long xi = NumericUtils.doubleToSortableLong(value.getX()); - long yi = NumericUtils.doubleToSortableLong(value.getY()); + long xi = NumericUtils.doubleToSortableLong(x); + long yi = NumericUtils.doubleToSortableLong(y); NumericUtils.longToSortableBytes(xi, bytesRefBuilder.bytes(), bytesRefBuilder.length()); NumericUtils.longToSortableBytes(yi, bytesRefBuilder.bytes(), bytesRefBuilder.length() + Long.BYTES); bytesRefBuilder.setLength(bytesRefBuilder.length() + Long.BYTES * 2); diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/TopNEncoder.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/TopNEncoder.java index e80d2620998bb..1b11fbbcc8ba5 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/TopNEncoder.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/TopNEncoder.java @@ -59,7 +59,7 @@ public interface TopNEncoder { double decodeDouble(BytesRef bytes); - void encodePoint(SpatialPoint value, BreakingBytesRefBuilder bytesRefBuilder); + void encodePoint(double x, double y, BreakingBytesRefBuilder bytesRefBuilder); SpatialPoint decodePoint(BytesRef bytes); diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-KeyExtractor.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-KeyExtractor.java.st index a085e67e66e2a..6ec654864561d 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-KeyExtractor.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-KeyExtractor.java.st @@ -9,8 +9,6 @@ package org.elasticsearch.compute.operator.topn; $if(BytesRef)$ import org.apache.lucene.util.BytesRef; -$elseif(Point)$ -import org.elasticsearch.common.geo.SpatialPoint; $endif$ import org.elasticsearch.compute.data.$Type$Block; import org.elasticsearch.compute.data.$Type$Vector; @@ -49,15 +47,15 @@ $endif$ this.nonNul = nonNul; } - protected final int nonNul(BreakingBytesRefBuilder key, $type$ value) { + protected final int nonNul(BreakingBytesRefBuilder key, $typeValue$) { key.append(nonNul); $if(BytesRef)$ - return encoder.encodeBytesRef(value, key) + 1; + return encoder.encodeBytesRef($value$, key) + 1; $elseif(boolean)$ - TopNEncoder.DEFAULT_SORTABLE.encodeBoolean(value, key); + TopNEncoder.DEFAULT_SORTABLE.encodeBoolean($value$, key); return Byte.BYTES + 1; $else$ - TopNEncoder.DEFAULT_SORTABLE.encode$Type$(value, key); + TopNEncoder.DEFAULT_SORTABLE.encode$Type$($value$, key); return $BYTES$ + 1; $endif$ } @@ -79,6 +77,8 @@ $endif$ public int writeKey(BreakingBytesRefBuilder key, int position) { $if(BytesRef)$ return nonNul(key, vector.get$Type$(position, scratch)); +$elseif(Point)$ + return nonNul(key, vector.getX(position), vector.getY(position)); $else$ return nonNul(key, vector.get$Type$(position)); $endif$ @@ -100,6 +100,9 @@ $endif$ } $if(BytesRef)$ return nonNul(key, block.get$Type$(block.getFirstValueIndex(position), scratch)); +$elseif(Point)$ + int index = block.getFirstValueIndex(position); + return nonNul(key, block.getX(index), block.getY(index)); $else$ return nonNul(key, block.get$Type$(block.getFirstValueIndex(position))); $endif$ @@ -121,6 +124,9 @@ $endif$ } $if(BytesRef)$ return nonNul(key, block.get$Type$(block.getFirstValueIndex(position) + block.getValueCount(position) - 1, scratch)); +$elseif(Point)$ + int index = block.getFirstValueIndex(position) + block.getValueCount(position) - 1; + return nonNul(key, block.getX(index), block.getY(index)); $else$ return nonNul(key, block.get$Type$(block.getFirstValueIndex(position) + block.getValueCount(position) - 1)); $endif$ @@ -166,14 +172,19 @@ $elseif(boolean)$ } return nonNul(key, true); $elseif(Point)$ - $type$ min = block.get$Type$(start); + double minX = block.getX(start); + double minY = block.getY(start); for (int i = start + 1; i < end; i++) { - SpatialPoint other = block.getPoint(i); - if (other.compareTo(min) < 0) { - min = other; + double x = block.getX(i); + double y = block.getY(i); + if (x < minX) { + minX = x; + } + if (y < minY) { + minY = y; } } - return nonNul(key, min); + return nonNul(key, minX, minY); $else$ $type$ min = block.get$Type$(start); for (int i = start + 1; i < end; i++) { @@ -223,14 +234,19 @@ $elseif(boolean)$ } return nonNul(key, false); $elseif(Point)$ - $type$ max = block.get$Type$(start); + double maxX = block.getX(start); + double maxY = block.getY(start); for (int i = start + 1; i < end; i++) { - SpatialPoint other = block.getPoint(i); - if (other.compareTo(max) > 0) { - max = other; + double x = block.getX(i); + double y = block.getY(i); + if (x > maxX) { + maxX = x; + } + if (y > maxY) { + maxY = y; } } - return nonNul(key, max); + return nonNul(key, maxX, maxY); $else$ $type$ max = block.get$Type$(start); for (int i = start + 1; i < end; i++) { diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ResultBuilder.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ResultBuilder.java.st index a8f7e3ca6da79..82b74b4e7c4b8 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ResultBuilder.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ResultBuilder.java.st @@ -28,7 +28,13 @@ $endif$ /** * The value previously set by {@link #decodeKey}. */ +$if(Point)$ + // TODO: we should be able to get rid of this object + private SpatialPoint key; + +$else$ private $type$ key; +$endif$ ResultBuilderFor$Type$(BlockFactory blockFactory, TopNEncoder encoder, boolean inKey, int initialSize) { $if(BytesRef)$ @@ -45,6 +51,8 @@ $endif$ assert inKey; $if(BytesRef)$ key = encoder.toSortable().decodeBytesRef(keys, scratch); +$elseif(Point)$ + key = TopNEncoder.DEFAULT_SORTABLE.decode$Type$(keys); $else$ key = TopNEncoder.DEFAULT_SORTABLE.decode$Type$(keys); $endif$ @@ -57,18 +65,34 @@ $endif$ case 0 -> { builder.appendNull(); } +$if(Point)$ + case 1 -> { + SpatialPoint value = inKey ? key : readValueFromValues(values); + builder.appendPoint(value.getX(), value.getY()); + } +$else$ case 1 -> builder.append$Type$(inKey ? key : readValueFromValues(values)); +$endif$ default -> { builder.beginPositionEntry(); for (int i = 0; i < count; i++) { +$if(Point)$ + SpatialPoint point = readValueFromValues(values); + builder.appendPoint(point.getX(), point.getY()); +$else$ builder.append$Type$(readValueFromValues(values)); +$endif$ } builder.endPositionEntry(); } } } +$if(Point)$ + private SpatialPoint readValueFromValues(BytesRef values) { +$else$ private $type$ readValueFromValues(BytesRef values) { +$endif$ $if(BytesRef)$ return encoder.toUnsortable().decodeBytesRef(values, scratch); $else$ diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ValueExtractor.java.st b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ValueExtractor.java.st index b5bd66b558112..062bc3d71ba98 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ValueExtractor.java.st +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/X-ValueExtractor.java.st @@ -9,8 +9,6 @@ package org.elasticsearch.compute.operator.topn; $if(BytesRef)$ import org.apache.lucene.util.BytesRef; -$elseif(Point)$ -import org.elasticsearch.common.geo.SpatialPoint; $endif$ import org.elasticsearch.compute.data.$Type$Block; import org.elasticsearch.compute.data.$Type$Vector; @@ -46,11 +44,11 @@ $endif$ TopNEncoder.DEFAULT_UNSORTABLE.encodeVInt(count, values); } - protected final void actualWriteValue(BreakingBytesRefBuilder values, $type$ value) { + protected final void actualWriteValue(BreakingBytesRefBuilder values, $typeValue$) { $if(BytesRef)$ encoder.encodeBytesRef(value, values); $else$ - TopNEncoder.DEFAULT_UNSORTABLE.encode$Type$(value, values); + TopNEncoder.DEFAULT_UNSORTABLE.encode$Type$($value$, values); $endif$ } @@ -71,6 +69,8 @@ $endif$ } $if(BytesRef)$ actualWriteValue(values, vector.get$Type$(position, scratch)); +$elseif(Point)$ + actualWriteValue(values, vector.getX(position), vector.getY(position)); $else$ actualWriteValue(values, vector.get$Type$(position)); $endif$ @@ -98,6 +98,8 @@ $endif$ for (int i = start; i < end; i++) { $if(BytesRef)$ actualWriteValue(values, block.getBytesRef(i, scratch)); +$elseif(Point)$ + actualWriteValue(values, block.getX(i), block.getY(i)); $else$ actualWriteValue(values, block.get$Type$(i)); $endif$ diff --git a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BasicBlockTests.java b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BasicBlockTests.java index e84c553e45356..31831eb4ba302 100644 --- a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BasicBlockTests.java +++ b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BasicBlockTests.java @@ -916,7 +916,7 @@ public static RandomBlock randomBlock( case POINT -> { SpatialPoint pt = new SpatialPoint(pointSupplier.get()); valuesAtPosition.add(pt); - ((PointBlock.Builder) builder).appendPoint(pt); + ((PointBlock.Builder) builder).appendPoint(pt.getX(), pt.getY()); } default -> throw new IllegalArgumentException("unsupported element type [" + elementType + "]"); } diff --git a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BlockTestUtils.java b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BlockTestUtils.java index dbbef39255895..6aa0e83b6de8c 100644 --- a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BlockTestUtils.java +++ b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BlockTestUtils.java @@ -63,7 +63,7 @@ public static void append(Block.Builder builder, Object value) { } else if (builder instanceof DocBlock.Builder b && value instanceof BlockUtils.Doc v) { b.appendShard(v.shard()).appendSegment(v.segment()).appendDoc(v.doc()); } else if (builder instanceof PointBlock.Builder b && value instanceof SpatialPoint v) { - b.appendPoint(v); + b.appendPoint(v.getX(), v.getY()); } else { throw new IllegalArgumentException("Can't append [" + value + "/" + value.getClass() + "] to [" + builder + "]"); } diff --git a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorBuilderTests.java b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorBuilderTests.java index 4151c70dd738e..fa4f709c71ee3 100644 --- a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorBuilderTests.java +++ b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorBuilderTests.java @@ -153,7 +153,7 @@ private void fill(Vector.Builder builder, Vector from) { } case POINT -> { for (int p = 0; p < from.getPositionCount(); p++) { - ((PointVector.Builder) builder).appendPoint(((PointVector) from).getPoint(p)); + ((PointVector.Builder) builder).appendPoint(((PointVector) from).getX(p), ((PointVector) from).getY(p)); } } } diff --git a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorFixedBuilderTests.java b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorFixedBuilderTests.java index 754026474a4b7..6a949b5d63d3b 100644 --- a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorFixedBuilderTests.java +++ b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/VectorFixedBuilderTests.java @@ -149,7 +149,7 @@ private void fill(Vector.Builder builder, Vector from) { } case POINT -> { for (int p = 0; p < from.getPositionCount(); p++) { - ((PointVector.FixedBuilder) builder).appendPoint(((PointVector) from).getPoint(p)); + ((PointVector.FixedBuilder) builder).appendPoint(((PointVector) from).getX(p), ((PointVector) from).getY(p)); } } } diff --git a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/MultivalueDedupeTests.java b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/MultivalueDedupeTests.java index 8db9a53dc9e12..666b8368626f8 100644 --- a/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/MultivalueDedupeTests.java +++ b/x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/MultivalueDedupeTests.java @@ -349,11 +349,21 @@ private void assertDoubleHash(Set previousValues, BasicBlockTests.Random private void assertPointHash(Set previousValues, BasicBlockTests.RandomBlock b) { LongHash hash = new LongHash(1, BigArrays.NON_RECYCLING_INSTANCE); - previousValues.stream().forEach(p -> hash.add(p.hashCode())); + previousValues.stream().forEach(p -> hash.add(pointHashCode(p))); MultivalueDedupe.HashResult hashes = new MultivalueDedupePoint((PointBlock) b.block()).hash(blockFactory(), hash); try (IntBlock ords = hashes.ords()) { assertThat(hashes.sawNull(), equalTo(b.values().stream().anyMatch(Objects::isNull))); - assertHash(b, ords, hash.size(), previousValues, hash::get, p -> (long) p.hashCode()); + assertHash(b, ords, hash.size(), previousValues, hash::get, MultivalueDedupeTests::pointHashCode); + } + } + + private static long pointHashCode(Object obj) { + // TODO: remove if we can get SpatialPoint.hashCode to use same implementation as MultivalueDedupePoint.hash() + if (obj instanceof SpatialPoint point) { + // Must match MultivalueDedupePoint.hash() implementation + return 31L * Double.hashCode(point.getX()) + Double.hashCode(point.getY()); + } else { + return obj.hashCode(); } } diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromLongEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromLongEvaluator.java index f95c2503363a2..8b21e0bcaca64 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromLongEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromLongEvaluator.java @@ -38,7 +38,8 @@ public Block evalVector(Vector v) { int positionCount = v.getPositionCount(); if (vector.isConstant()) { try { - return driverContext.blockFactory().newConstantPointBlockWith(evalValue(vector, 0), positionCount); + SpatialPoint point = evalValue(vector, 0); + return driverContext.blockFactory().newConstantPointBlockWith(point.getX(), point.getY(), positionCount); } catch (IllegalArgumentException e) { registerException(e); return driverContext.blockFactory().newConstantNullBlock(positionCount); @@ -47,7 +48,8 @@ public Block evalVector(Vector v) { try (PointBlock.Builder builder = driverContext.blockFactory().newPointBlockBuilder(positionCount)) { for (int p = 0; p < positionCount; p++) { try { - builder.appendPoint(evalValue(vector, p)); + SpatialPoint point = evalValue(vector, p); + builder.appendPoint(point.getX(), point.getY()); } catch (IllegalArgumentException e) { registerException(e); builder.appendNull(); @@ -80,7 +82,7 @@ public Block evalBlock(Block b) { builder.beginPositionEntry(); positionOpened = true; } - builder.appendPoint(value); + builder.appendPoint(value.getX(), value.getY()); valuesAppended = true; } catch (IllegalArgumentException e) { registerException(e); diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromStringEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromStringEvaluator.java index aaaada02aa5a5..04ebd72f0d4bc 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromStringEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointFromStringEvaluator.java @@ -40,7 +40,8 @@ public Block evalVector(Vector v) { BytesRef scratchPad = new BytesRef(); if (vector.isConstant()) { try { - return driverContext.blockFactory().newConstantPointBlockWith(evalValue(vector, 0, scratchPad), positionCount); + SpatialPoint point = evalValue(vector, 0, scratchPad); + return driverContext.blockFactory().newConstantPointBlockWith(point.getX(), point.getY(), positionCount); } catch (IllegalArgumentException e) { registerException(e); return driverContext.blockFactory().newConstantNullBlock(positionCount); @@ -49,7 +50,8 @@ public Block evalVector(Vector v) { try (PointBlock.Builder builder = driverContext.blockFactory().newPointBlockBuilder(positionCount)) { for (int p = 0; p < positionCount; p++) { try { - builder.appendPoint(evalValue(vector, p, scratchPad)); + SpatialPoint point = evalValue(vector, p, scratchPad); + builder.appendPoint(point.getX(), point.getY()); } catch (IllegalArgumentException e) { registerException(e); builder.appendNull(); @@ -83,7 +85,7 @@ public Block evalBlock(Block b) { builder.beginPositionEntry(); positionOpened = true; } - builder.appendPoint(value); + builder.appendPoint(value.getX(), value.getY()); valuesAppended = true; } catch (IllegalArgumentException e) { registerException(e); diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromLongEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromLongEvaluator.java index dfc3350235001..ef524cffcd509 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromLongEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromLongEvaluator.java @@ -38,7 +38,8 @@ public Block evalVector(Vector v) { int positionCount = v.getPositionCount(); if (vector.isConstant()) { try { - return driverContext.blockFactory().newConstantPointBlockWith(evalValue(vector, 0), positionCount); + SpatialPoint point = evalValue(vector, 0); + return driverContext.blockFactory().newConstantPointBlockWith(point.getX(), point.getY(), positionCount); } catch (IllegalArgumentException e) { registerException(e); return driverContext.blockFactory().newConstantNullBlock(positionCount); @@ -47,7 +48,8 @@ public Block evalVector(Vector v) { try (PointBlock.Builder builder = driverContext.blockFactory().newPointBlockBuilder(positionCount)) { for (int p = 0; p < positionCount; p++) { try { - builder.appendPoint(evalValue(vector, p)); + SpatialPoint point = evalValue(vector, p); + builder.appendPoint(point.getX(), point.getY()); } catch (IllegalArgumentException e) { registerException(e); builder.appendNull(); @@ -80,7 +82,7 @@ public Block evalBlock(Block b) { builder.beginPositionEntry(); positionOpened = true; } - builder.appendPoint(value); + builder.appendPoint(value.getX(), value.getY()); valuesAppended = true; } catch (IllegalArgumentException e) { registerException(e); diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromStringEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromStringEvaluator.java index 73bcfab3713fd..2706b58cfd918 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromStringEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointFromStringEvaluator.java @@ -40,7 +40,8 @@ public Block evalVector(Vector v) { BytesRef scratchPad = new BytesRef(); if (vector.isConstant()) { try { - return driverContext.blockFactory().newConstantPointBlockWith(evalValue(vector, 0, scratchPad), positionCount); + SpatialPoint point = evalValue(vector, 0, scratchPad); + return driverContext.blockFactory().newConstantPointBlockWith(point.getX(), point.getY(), positionCount); } catch (IllegalArgumentException e) { registerException(e); return driverContext.blockFactory().newConstantNullBlock(positionCount); @@ -49,7 +50,8 @@ public Block evalVector(Vector v) { try (PointBlock.Builder builder = driverContext.blockFactory().newPointBlockBuilder(positionCount)) { for (int p = 0; p < positionCount; p++) { try { - builder.appendPoint(evalValue(vector, p, scratchPad)); + SpatialPoint point = evalValue(vector, p, scratchPad); + builder.appendPoint(point.getX(), point.getY()); } catch (IllegalArgumentException e) { registerException(e); builder.appendNull(); @@ -83,7 +85,7 @@ public Block evalBlock(Block b) { builder.beginPositionEntry(); positionOpened = true; } - builder.appendPoint(value); + builder.appendPoint(value.getX(), value.getY()); valuesAppended = true; } catch (IllegalArgumentException e) { registerException(e); diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromCartesianPointEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromCartesianPointEvaluator.java index a51fe752ace45..357c55bea43b7 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromCartesianPointEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromCartesianPointEvaluator.java @@ -47,7 +47,9 @@ public Block evalVector(Vector v) { } private static long evalValue(PointVector container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToLong.fromCartesianPoint(value); } @@ -82,7 +84,9 @@ public Block evalBlock(Block b) { } private static long evalValue(PointBlock container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToLong.fromCartesianPoint(value); } diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromGeoPointEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromGeoPointEvaluator.java index 714f25842c355..7f7c641e56de7 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromGeoPointEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToLongFromGeoPointEvaluator.java @@ -47,7 +47,9 @@ public Block evalVector(Vector v) { } private static long evalValue(PointVector container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToLong.fromGeoPoint(value); } @@ -82,7 +84,9 @@ public Block evalBlock(Block b) { } private static long evalValue(PointBlock container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToLong.fromGeoPoint(value); } diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromCartesianPointEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromCartesianPointEvaluator.java index e79ab047781d6..0e67cd6a8ea9f 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromCartesianPointEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromCartesianPointEvaluator.java @@ -48,7 +48,9 @@ public Block evalVector(Vector v) { } private static BytesRef evalValue(PointVector container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToString.fromCartesianPoint(value); } @@ -83,7 +85,9 @@ public Block evalBlock(Block b) { } private static BytesRef evalValue(PointBlock container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToString.fromCartesianPoint(value); } diff --git a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromGeoPointEvaluator.java b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromGeoPointEvaluator.java index 7d5fc08ac9dab..8eb017122b07d 100644 --- a/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromGeoPointEvaluator.java +++ b/x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromGeoPointEvaluator.java @@ -48,7 +48,9 @@ public Block evalVector(Vector v) { } private static BytesRef evalValue(PointVector container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToString.fromGeoPoint(value); } @@ -83,7 +85,9 @@ public Block evalBlock(Block b) { } private static BytesRef evalValue(PointBlock container, int index) { - SpatialPoint value = container.getPoint(index); + double x = container.getX(index); + double y = container.getY(index); + SpatialPoint value = new SpatialPoint(x, y); return ToString.fromGeoPoint(value); } diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponse.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponse.java index 9b2bdfd3c7ab4..1135334e31123 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponse.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponse.java @@ -318,7 +318,7 @@ private static SpatialPoint pointValueAt(SpatialCoordinateTypes spatial, String if (block instanceof LongBlock longBlock) { return spatial.longAsPoint(longBlock.getLong(offset)); } else if (block instanceof PointBlock pointBlock) { - return spatial.pointAsPoint(pointBlock.getPoint(offset)); + return spatial.pointAsPoint(pointBlock.getX(offset), pointBlock.getY(offset)); } else { throw new IllegalArgumentException("Unsupported block type for " + dataType + ": " + block.getWriteableName()); } @@ -366,12 +366,13 @@ private static Page valuesToPage(List dataTypes, List> valu } } case "geo_point" -> { + // TODO: check that we are not serializing/deserializing unnecessarily here SpatialPoint pointVal = GEO.stringAsPoint(value.toString()); - ((PointBlock.Builder) builder).appendPoint(pointVal); + ((PointBlock.Builder) builder).appendPoint(pointVal.getX(), pointVal.getY()); } case "cartesian_point" -> { SpatialPoint pointVal = CARTESIAN.stringAsPoint(value.toString()); - ((PointBlock.Builder) builder).appendPoint(pointVal); + ((PointBlock.Builder) builder).appendPoint(pointVal.getX(), pointVal.getY()); } default -> throw EsqlIllegalArgumentException.illegalDataType(dataTypes.get(c)); } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java index a0bb12b29dafd..3130e06b98ecf 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.geo.SpatialPoint; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; @@ -129,8 +130,8 @@ private Page randomPage(List columns) { new BytesRef(UnsupportedValueSource.UNSUPPORTED_OUTPUT) ); case "version" -> ((BytesRefBlock.Builder) builder).appendBytesRef(new Version(randomIdentifier()).toBytesRef()); - case "geo_point" -> ((PointBlock.Builder) builder).appendPoint(randomGeoPoint()); - case "cartesian_point" -> ((PointBlock.Builder) builder).appendPoint(randomCartesianPoint()); + case "geo_point" -> appendPoint((PointBlock.Builder) builder, randomGeoPoint()); + case "cartesian_point" -> appendPoint((PointBlock.Builder) builder, randomCartesianPoint()); case "null" -> builder.appendNull(); case "_source" -> { try { @@ -152,6 +153,10 @@ private Page randomPage(List columns) { }).toArray(Block[]::new)); } + private void appendPoint(PointBlock.Builder builder, SpatialPoint point) { + builder.appendPoint(point.getX(), point.getY()); + } + @Override protected EsqlQueryResponse mutateInstance(EsqlQueryResponse instance) { boolean allNull = true; diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatTests.java index 9f114feb9c40b..10d61027d4da1 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatTests.java @@ -264,13 +264,23 @@ private static EsqlQueryResponse regularData() { .build(), new IntArrayVector(new int[] { 11 * 60 + 48, 4 * 60 + 40 }, 2).asBlock(), new LongArrayVector(new long[] { GEO.pointAsLong(12, 56), GEO.pointAsLong(-97, 26) }, 2).asBlock(), - new PointArrayVector(new SpatialPoint[] { new SpatialPoint(1234, 5678), new SpatialPoint(-9753, 2611) }, 2).asBlock() + makePointArrayVector(new SpatialPoint(1234, 5678), new SpatialPoint(-9753, 2611)).asBlock() ) ); return new EsqlQueryResponse(headers, values, null, false); } + static PointArrayVector makePointArrayVector(SpatialPoint... points) { + double[] x = new double[points.length]; + double[] y = new double[points.length]; + for (int i = 0; i < points.length; i++) { + x[i] = points[i].getX(); + y[i] = points[i].getY(); + } + return new PointArrayVector(x, y, points.length); + } + private static EsqlQueryResponse escapedData() { // headers List headers = asList(new ColumnInfo("first", "keyword"), new ColumnInfo("\"special\"", "keyword")); diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatterTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatterTests.java index 41a89a61e4db2..1fcdab8ac762f 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatterTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/formatter/TextFormatterTests.java @@ -15,7 +15,6 @@ import org.elasticsearch.compute.data.DoubleArrayVector; import org.elasticsearch.compute.data.LongArrayVector; import org.elasticsearch.compute.data.Page; -import org.elasticsearch.compute.data.PointArrayVector; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.esql.action.ColumnInfo; import org.elasticsearch.xpack.esql.action.EsqlQueryResponse; @@ -24,6 +23,7 @@ import java.util.List; import static org.elasticsearch.rest.RestResponseUtils.getTextBodyContent; +import static org.elasticsearch.xpack.esql.formatter.TextFormatTests.makePointArrayVector; import static org.elasticsearch.xpack.ql.util.DateUtils.UTC_DATE_TIME_FORMATTER; import static org.elasticsearch.xpack.ql.util.SpatialCoordinateTypes.GEO; import static org.hamcrest.Matchers.arrayWithSize; @@ -61,7 +61,7 @@ public class TextFormatterTests extends ESTestCase { 2 ).asBlock(), new LongArrayVector(new long[] { GEO.pointAsLong(12, 56), GEO.pointAsLong(-97, 26) }, 2).asBlock(), - new PointArrayVector(new SpatialPoint[] { new SpatialPoint(1234, 5678), new SpatialPoint(-9753, 2611) }, 2).asBlock(), + makePointArrayVector(new SpatialPoint(1234, 5678), new SpatialPoint(-9753, 2611)).asBlock(), Block.constantNullBlock(2) ) ), @@ -125,7 +125,7 @@ public void testFormatWithoutHeader() { 2 ).asBlock(), new LongArrayVector(new long[] { GEO.pointAsLong(12, 56), GEO.pointAsLong(-97, 26) }, 2).asBlock(), - new PointArrayVector(new SpatialPoint[] { new SpatialPoint(1234, 5678), new SpatialPoint(-9753, 2611) }, 2).asBlock(), + makePointArrayVector(new SpatialPoint(1234, 5678), new SpatialPoint(-9753, 2611)).asBlock(), Block.constantNullBlock(2) ) ), diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/SpatialCoordinateTypes.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/SpatialCoordinateTypes.java index 59ad9315b42ef..8db6247a519c5 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/SpatialCoordinateTypes.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/SpatialCoordinateTypes.java @@ -90,4 +90,11 @@ public SpatialPoint stringAsPoint(String string) { public SpatialPoint pointAsPoint(SpatialPoint point) { return point; } + + /** + * Convert point to the correct class for the upper column type. For example, create a GeoPoint from a cartesian point. + */ + public SpatialPoint pointAsPoint(double x, double y) { + return pointAsPoint(new SpatialPoint(x, y)); + } }