From d46f1a0c5a2d4e9ee3ef0e6f6ac13d8a1658b4f1 Mon Sep 17 00:00:00 2001 From: ChrisHegarty Date: Mon, 2 Oct 2023 20:59:01 +0100 Subject: [PATCH] assert exact estimate in fixed vector builders --- .../compute/data/BooleanVectorFixedBuilder.java | 8 ++++++-- .../compute/data/DoubleVectorFixedBuilder.java | 8 ++++++-- .../elasticsearch/compute/data/IntVectorFixedBuilder.java | 8 ++++++-- .../compute/data/LongVectorFixedBuilder.java | 8 ++++++-- .../compute/aggregation/blockhash/BytesRefBlockHash.java | 4 ++-- .../compute/data/X-VectorFixedBuilder.java.st | 8 ++++++-- 6 files changed, 32 insertions(+), 12 deletions(-) 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 015a056f2fdd..93ff57f2336b 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 @@ -55,10 +55,14 @@ public BooleanVector build() { throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; + BooleanVector vector; if (values.length == 1) { - return blockFactory.newConstantBooleanBlockWith(values[0], 1, preAdjustedBytes).asVector(); + vector = blockFactory.newConstantBooleanBlockWith(values[0], 1, preAdjustedBytes).asVector(); + } else { + vector = blockFactory.newBooleanArrayVector(values, values.length, preAdjustedBytes); } - return blockFactory.newBooleanArrayVector(values, values.length, preAdjustedBytes); + assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; + return vector; } @Override 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 54d475bb1dfa..aa698a86b9c4 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 @@ -55,10 +55,14 @@ public DoubleVector build() { throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; + DoubleVector vector; if (values.length == 1) { - return blockFactory.newConstantDoubleBlockWith(values[0], 1, preAdjustedBytes).asVector(); + vector = blockFactory.newConstantDoubleBlockWith(values[0], 1, preAdjustedBytes).asVector(); + } else { + vector = blockFactory.newDoubleArrayVector(values, values.length, preAdjustedBytes); } - return blockFactory.newDoubleArrayVector(values, values.length, preAdjustedBytes); + assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; + return vector; } @Override 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 f0619f8cb222..3ee3bfb40d6f 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 @@ -55,10 +55,14 @@ public IntVector build() { throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; + IntVector vector; if (values.length == 1) { - return blockFactory.newConstantIntBlockWith(values[0], 1, preAdjustedBytes).asVector(); + vector = blockFactory.newConstantIntBlockWith(values[0], 1, preAdjustedBytes).asVector(); + } else { + vector = blockFactory.newIntArrayVector(values, values.length, preAdjustedBytes); } - return blockFactory.newIntArrayVector(values, values.length, preAdjustedBytes); + assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; + return vector; } @Override 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 1f631d604ddd..c95b0d07bec5 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 @@ -55,10 +55,14 @@ public LongVector build() { throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; + LongVector vector; if (values.length == 1) { - return blockFactory.newConstantLongBlockWith(values[0], 1, preAdjustedBytes).asVector(); + vector = blockFactory.newConstantLongBlockWith(values[0], 1, preAdjustedBytes).asVector(); + } else { + vector = blockFactory.newLongArrayVector(values, values.length, preAdjustedBytes); } - return blockFactory.newLongArrayVector(values, values.length, preAdjustedBytes); + assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; + return vector; } @Override diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash/BytesRefBlockHash.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash/BytesRefBlockHash.java index 56b907141ad7..da24baf984d1 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash/BytesRefBlockHash.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash/BytesRefBlockHash.java @@ -78,8 +78,8 @@ private IntVector add(BytesRefVector vector) { } private IntBlock add(BytesRefBlock block) { - MultivalueDedupe.HashResult result = new MultivalueDedupeBytesRef(Block.Ref.floating(block)).hash(bytesRefHash); // TODO: block - // factory + // TODO: use block factory + MultivalueDedupe.HashResult result = new MultivalueDedupeBytesRef(Block.Ref.floating(block)).hash(bytesRefHash); seenNull |= result.sawNull(); return result.ords(); } 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 3149202bcad0..69993d0945fe 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 @@ -55,10 +55,14 @@ final class $Type$VectorFixedBuilder implements $Type$Vector.FixedBuilder { throw new IllegalStateException("expected to write [" + values.length + "] entries but wrote [" + nextIndex + "]"); } nextIndex = -1; + $Type$Vector vector; if (values.length == 1) { - return blockFactory.newConstant$Type$BlockWith(values[0], 1, preAdjustedBytes).asVector(); + vector = blockFactory.newConstant$Type$BlockWith(values[0], 1, preAdjustedBytes).asVector(); + } else { + vector = blockFactory.new$Type$ArrayVector(values, values.length, preAdjustedBytes); } - return blockFactory.new$Type$ArrayVector(values, values.length, preAdjustedBytes); + assert vector.ramBytesUsed() == preAdjustedBytes : "fixed Builders should estimate the exact ram bytes used"; + return vector; } @Override