Skip to content

Commit

Permalink
Shallow copy when expanding with nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-spies committed Dec 22, 2023
1 parent 4c78428 commit c3cf919
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,27 @@ final class BooleanArrayBlock extends AbstractArrayBlock implements BooleanBlock
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
this(
new BooleanArrayVector(values, firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount], blockFactory),
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
);
}

private BooleanArrayBlock(
BooleanArrayVector vector,
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
int vectorLength = firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount];
this.vector = new BooleanArrayVector(values, vectorLength, blockFactory);
this.vector = vector;
}

@Override
Expand All @@ -47,6 +64,7 @@ public boolean getBoolean(int valueIndex) {

@Override
public BooleanBlock filter(int... positions) {
// TODO use reference counting to share the vector
try (var builder = blockFactory().newBooleanBlockBuilder(positions.length)) {
for (int pos : positions) {
if (isNull(pos)) {
Expand Down Expand Up @@ -80,25 +98,20 @@ public BooleanBlock expand() {
incRef();
return this;
}
vector.incRef();
if (nullsMask == null) {
vector.incRef();
return vector.asBlock();
}
// TODO use reference counting to share the vector
try (var builder = blockFactory().newBooleanBlockBuilder(firstValueIndexes[getPositionCount()])) {
for (int pos = 0; pos < getPositionCount(); pos++) {
if (isNull(pos)) {
builder.appendNull();
continue;
}
int first = getFirstValueIndex(pos);
int end = first + getValueCount(pos);
for (int i = first; i < end; i++) {
builder.appendBoolean(getBoolean(i));
}
}
return builder.mvOrdering(MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING).build();
}
BooleanArrayBlock expanded = new BooleanArrayBlock(
vector,
vector.getPositionCount(),
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock(), true);
return expanded;
}

private long ramBytesUsedOnlyBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,27 @@ public BooleanBigArrayBlock(
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
this(
new BooleanBigArrayVector(values, firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount], blockFactory),
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
);
}

private BooleanBigArrayBlock(
BooleanBigArrayVector vector,
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
int vectorLength = firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount];
this.vector = new BooleanBigArrayVector(values, vectorLength, blockFactory);
this.vector = vector;
}

@Override
Expand All @@ -48,6 +65,7 @@ public boolean getBoolean(int valueIndex) {

@Override
public BooleanBlock filter(int... positions) {
// TODO use reference counting to share the vector
try (var builder = blockFactory().newBooleanBlockBuilder(positions.length)) {
for (int pos : positions) {
if (isNull(pos)) {
Expand Down Expand Up @@ -81,25 +99,21 @@ public BooleanBlock expand() {
incRef();
return this;
}
vector.incRef();
if (nullsMask == null) {
vector.incRef();
return vector.asBlock();
}
// TODO use reference counting to share the vector
try (var builder = blockFactory().newBooleanBlockBuilder(firstValueIndexes[getPositionCount()])) {
for (int pos = 0; pos < getPositionCount(); pos++) {
if (isNull(pos)) {
builder.appendNull();
continue;
}
int first = getFirstValueIndex(pos);
int end = first + getValueCount(pos);
for (int i = first; i < end; i++) {
builder.appendBoolean(getBoolean(i));
}
}
return builder.mvOrdering(MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING).build();
}
BooleanBigArrayBlock expanded = new BooleanBigArrayBlock(
vector,
vector.getPositionCount(),
null,
// TODO: we probably need to adjust the breaker before computing the shifted null mask
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock(), true);
return expanded;
}

private long ramBytesUsedOnlyBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,27 @@ final class BytesRefArrayBlock extends AbstractArrayBlock implements BytesRefBlo
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
this(
new BytesRefArrayVector(values, firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount], blockFactory),
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
);
}

private BytesRefArrayBlock(
BytesRefArrayVector vector,
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
int vectorLength = firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount];
this.vector = new BytesRefArrayVector(values, vectorLength, blockFactory);
this.vector = vector;
}

@Override
Expand All @@ -50,6 +67,7 @@ public BytesRef getBytesRef(int valueIndex, BytesRef dest) {

@Override
public BytesRefBlock filter(int... positions) {
// TODO use reference counting to share the vector
final BytesRef scratch = new BytesRef();
try (var builder = blockFactory().newBytesRefBlockBuilder(positions.length)) {
for (int pos : positions) {
Expand Down Expand Up @@ -84,26 +102,20 @@ public BytesRefBlock expand() {
incRef();
return this;
}
vector.incRef();
if (nullsMask == null) {
vector.incRef();
return vector.asBlock();
}
// TODO use reference counting to share the vector
final BytesRef scratch = new BytesRef();
try (var builder = blockFactory().newBytesRefBlockBuilder(firstValueIndexes[getPositionCount()])) {
for (int pos = 0; pos < getPositionCount(); pos++) {
if (isNull(pos)) {
builder.appendNull();
continue;
}
int first = getFirstValueIndex(pos);
int end = first + getValueCount(pos);
for (int i = first; i < end; i++) {
builder.appendBytesRef(getBytesRef(i, scratch));
}
}
return builder.mvOrdering(MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING).build();
}
BytesRefArrayBlock expanded = new BytesRefArrayBlock(
vector,
vector.getPositionCount(),
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock(), true);
return expanded;
}

private long ramBytesUsedOnlyBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,27 @@ final class DoubleArrayBlock extends AbstractArrayBlock implements DoubleBlock {
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
this(
new DoubleArrayVector(values, firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount], blockFactory),
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
);
}

private DoubleArrayBlock(
DoubleArrayVector vector,
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
int vectorLength = firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount];
this.vector = new DoubleArrayVector(values, vectorLength, blockFactory);
this.vector = vector;
}

@Override
Expand All @@ -47,6 +64,7 @@ public double getDouble(int valueIndex) {

@Override
public DoubleBlock filter(int... positions) {
// TODO use reference counting to share the vector
try (var builder = blockFactory().newDoubleBlockBuilder(positions.length)) {
for (int pos : positions) {
if (isNull(pos)) {
Expand Down Expand Up @@ -80,25 +98,20 @@ public DoubleBlock expand() {
incRef();
return this;
}
vector.incRef();
if (nullsMask == null) {
vector.incRef();
return vector.asBlock();
}
// TODO use reference counting to share the vector
try (var builder = blockFactory().newDoubleBlockBuilder(firstValueIndexes[getPositionCount()])) {
for (int pos = 0; pos < getPositionCount(); pos++) {
if (isNull(pos)) {
builder.appendNull();
continue;
}
int first = getFirstValueIndex(pos);
int end = first + getValueCount(pos);
for (int i = first; i < end; i++) {
builder.appendDouble(getDouble(i));
}
}
return builder.mvOrdering(MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING).build();
}
DoubleArrayBlock expanded = new DoubleArrayBlock(
vector,
vector.getPositionCount(),
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock(), true);
return expanded;
}

private long ramBytesUsedOnlyBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,27 @@ public DoubleBigArrayBlock(
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
this(
new DoubleBigArrayVector(values, firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount], blockFactory),
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
);
}

private DoubleBigArrayBlock(
DoubleBigArrayVector vector,
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
int vectorLength = firstValueIndexes == null ? positionCount : firstValueIndexes[positionCount];
this.vector = new DoubleBigArrayVector(values, vectorLength, blockFactory);
this.vector = vector;
}

@Override
Expand All @@ -48,6 +65,7 @@ public double getDouble(int valueIndex) {

@Override
public DoubleBlock filter(int... positions) {
// TODO use reference counting to share the vector
try (var builder = blockFactory().newDoubleBlockBuilder(positions.length)) {
for (int pos : positions) {
if (isNull(pos)) {
Expand Down Expand Up @@ -81,25 +99,21 @@ public DoubleBlock expand() {
incRef();
return this;
}
vector.incRef();
if (nullsMask == null) {
vector.incRef();
return vector.asBlock();
}
// TODO use reference counting to share the vector
try (var builder = blockFactory().newDoubleBlockBuilder(firstValueIndexes[getPositionCount()])) {
for (int pos = 0; pos < getPositionCount(); pos++) {
if (isNull(pos)) {
builder.appendNull();
continue;
}
int first = getFirstValueIndex(pos);
int end = first + getValueCount(pos);
for (int i = first; i < end; i++) {
builder.appendDouble(getDouble(i));
}
}
return builder.mvOrdering(MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING).build();
}
DoubleBigArrayBlock expanded = new DoubleBigArrayBlock(
vector,
vector.getPositionCount(),
null,
// TODO: we probably need to adjust the breaker before computing the shifted null mask
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock(), true);
return expanded;
}

private long ramBytesUsedOnlyBlock() {
Expand Down
Loading

0 comments on commit c3cf919

Please sign in to comment.