Skip to content

Commit 26a8753

Browse files
committed
Add Vector and Blocks for Dense Vector to factories and interfaces
1 parent 4704ca2 commit 26a8753

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockFactory.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,78 @@ public final AggregateMetricDoubleBlock newAggregateMetricDoubleBlock(
487487
public long maxPrimitiveArrayBytes() {
488488
return maxPrimitiveArrayBytes;
489489
}
490+
491+
public DenseVectorBlock.Builder newDenseVectorBlockBuilder(int estimatedSize) {
492+
return new DenseVectorBlockBuilder(estimatedSize, this);
493+
}
494+
495+
public final DenseVectorBlock newDenseVectorArrayBlock(
496+
float[][] values,
497+
int dimensions,
498+
int positionCount,
499+
int[] firstValueIndexes,
500+
BitSet nulls,
501+
MvOrdering mvOrdering
502+
) {
503+
return newDenseVectorArrayBlock(values, dimensions, positionCount, firstValueIndexes, nulls, mvOrdering, 0L);
504+
}
505+
506+
public DenseVectorBlock newDenseVectorArrayBlock(
507+
float[][] values,
508+
int dimensions,
509+
int pc,
510+
int[] fvi,
511+
BitSet nulls,
512+
MvOrdering mvOrdering,
513+
long preAdjustedBytes
514+
) {
515+
var b = new DenseVectorArrayBlock(values, pc, fvi, nulls, mvOrdering, this);
516+
adjustBreaker(b.ramBytesUsed() - preAdjustedBytes);
517+
return b;
518+
}
519+
520+
public DenseVectorVector.Builder newDenseVectorVectorBuilder(int estimatedSize, int dimensions) {
521+
return new DenseVectorVectorBuilder(dimensions, estimatedSize, this);
522+
}
523+
524+
/**
525+
* Build a {@link DenseVectorVector.FixedBuilder} that never grows.
526+
*/
527+
public DenseVectorVector.FixedBuilder newDenseVectorVectorFixedBuilder(int dimensions, int size) {
528+
return new DenseVectorVectorFixedBuilder(dimensions, size, this);
529+
}
530+
531+
/**
532+
* Creates a new Vector with the given values and positionCount, where the caller has already
533+
* pre-adjusted a number of bytes with the factory's breaker.
534+
*
535+
* long preAdjustedBytes = blockFactory.preAdjustBreakerForDenseVector(positionCount);
536+
* int[] values = new int[positionCount];
537+
* for (int i = 0; i < positionCount; i++) {
538+
* values[i] = doWhateverStuff
539+
* }
540+
* var vector = blockFactory.newDenseVectorArrayVector(values, positionCount, preAdjustedBytes);
541+
*/
542+
public DenseVectorVector newDenseVectorArrayVector(float[][] values, int positionCount, long preAdjustedBytes) {
543+
var b = new DenseVectorArrayVector(values, positionCount, this);
544+
adjustBreaker(b.ramBytesUsed() - preAdjustedBytes);
545+
return b;
546+
}
547+
548+
public final DenseVectorBlock newConstantDenseVectorBlockWith(float[] value, int positions) {
549+
return newConstantDenseVectorBlockWith(value, positions, 0L);
550+
}
551+
552+
public DenseVectorBlock newConstantDenseVectorBlockWith(float[] value, int positions, long preAdjustedBytes) {
553+
var b = new ConstantDenseVectorVector(value, positions, this).asBlock();
554+
adjustBreaker(b.ramBytesUsed() - preAdjustedBytes);
555+
return b;
556+
}
557+
558+
public DenseVectorVector newConstantDenseVectorVector(float[] value, int positions) {
559+
adjustBreaker(ConstantDenseVectorVector.RAM_BYTES_USED);
560+
var v = new ConstantDenseVectorVector(value, positions, this);
561+
assert v.ramBytesUsed() == ConstantDenseVectorVector.RAM_BYTES_USED;
562+
return v;
563+
}
490564
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ yield new AggregateMetricDoubleLiteral(
299299
aggBlock.countBlock().getInt(offset)
300300
);
301301
}
302+
case DENSE_VECTOR -> ((DenseVectorBlock) block).getDenseVector(offset);
302303
case UNKNOWN -> throw new IllegalArgumentException("can't read values from [" + block + "]");
303304
};
304305
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullBlock.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public final class ConstantNullBlock extends AbstractNonThreadSafeRefCounted
2626
FloatBlock,
2727
DoubleBlock,
2828
BytesRefBlock,
29-
AggregateMetricDoubleBlock {
29+
AggregateMetricDoubleBlock,
30+
DenseVectorBlock {
3031

3132
private static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(ConstantNullBlock.class);
3233
private final int positionCount;
@@ -292,6 +293,12 @@ public long getLong(int valueIndex) {
292293
throw new UnsupportedOperationException("null block");
293294
}
294295

296+
@Override
297+
public float[] getDenseVector(int valueIndex) {
298+
assert false : "null block";
299+
throw new UnsupportedOperationException("null block");
300+
}
301+
295302
@Override
296303
public int getTotalValueCount() {
297304
return 0;

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullVector.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public final class ConstantNullVector extends AbstractVector
2424
DoubleVector,
2525
FloatVector,
2626
IntVector,
27-
LongVector {
27+
LongVector,
28+
DenseVectorVector {
2829

2930
private ConstantNullVector(int positionCount, BlockFactory blockFactory) {
3031
super(positionCount, blockFactory);
@@ -66,6 +67,11 @@ public ConstantNullBlock keepMask(BooleanVector mask) {
6667
throw new UnsupportedOperationException("null vector");
6768
}
6869

70+
@Override
71+
public int dimensions() {
72+
return 0;
73+
}
74+
6975
@Override
7076
public ReleasableIterator<ConstantNullBlock> lookup(IntBlock positions, ByteSizeValue targetBlockSize) {
7177
assert false : "null vector";
@@ -108,6 +114,12 @@ public long getLong(int position) {
108114
throw new UnsupportedOperationException("null vector");
109115
}
110116

117+
@Override
118+
public float[] getDenseVector(int position) {
119+
assert false : "null vector";
120+
throw new UnsupportedOperationException("null vector");
121+
}
122+
111123
@Override
112124
public int min() {
113125
assert false : "null vector";

0 commit comments

Comments
 (0)