io.trino.spi.block.Int128ArrayBlockBuilder Maven / Gradle / Ivy
The newest version!
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.spi.block;
import jakarta.annotation.Nullable;
import java.util.Arrays;
import static io.airlift.slice.SizeOf.instanceSize;
import static io.airlift.slice.SizeOf.sizeOf;
import static io.trino.spi.block.BlockUtil.calculateNewArraySize;
import static java.lang.Math.max;
import static java.util.Objects.checkIndex;
public class Int128ArrayBlockBuilder
implements BlockBuilder
{
private static final int INSTANCE_SIZE = instanceSize(Int128ArrayBlockBuilder.class);
private static final Block NULL_VALUE_BLOCK = new Int128ArrayBlock(0, 1, new boolean[] {true}, new long[2]);
@Nullable
private final BlockBuilderStatus blockBuilderStatus;
private boolean initialized;
private final int initialEntryCount;
private int positionCount;
private boolean hasNullValue;
private boolean hasNonNullValue;
// it is assumed that these arrays are the same length
private boolean[] valueIsNull = new boolean[0];
private long[] values = new long[0];
private long retainedSizeInBytes;
public Int128ArrayBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this.blockBuilderStatus = blockBuilderStatus;
this.initialEntryCount = max(expectedEntries, 1);
updateRetainedSize();
}
public void writeInt128(long high, long low)
{
ensureCapacity(positionCount + 1);
int valueIndex = positionCount * 2;
values[valueIndex] = high;
values[valueIndex + 1] = low;
hasNonNullValue = true;
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION);
}
}
@Override
public void append(ValueBlock block, int position)
{
ensureCapacity(positionCount + 1);
Int128ArrayBlock int128ArrayBlock = (Int128ArrayBlock) block;
if (int128ArrayBlock.isNull(position)) {
valueIsNull[positionCount] = true;
hasNullValue = true;
}
else {
long[] rawValues = int128ArrayBlock.getRawValues();
int rawValuePosition = (int128ArrayBlock.getRawOffset() + position) * 2;
int positionIndex = positionCount * 2;
values[positionIndex] = rawValues[rawValuePosition];
values[positionIndex + 1] = rawValues[rawValuePosition + 1];
hasNonNullValue = true;
}
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION);
}
}
@Override
public void appendRepeated(ValueBlock block, int position, int count)
{
if (count == 0) {
return;
}
if (count == 1) {
append(block, position);
return;
}
ensureCapacity(positionCount + count);
Int128ArrayBlock int128ArrayBlock = (Int128ArrayBlock) block;
if (int128ArrayBlock.isNull(position)) {
Arrays.fill(valueIsNull, positionCount, positionCount + count, true);
hasNullValue = true;
}
else {
long[] rawValues = int128ArrayBlock.getRawValues();
int rawValuePosition = (int128ArrayBlock.getRawOffset() + position) * 2;
long valueHigh = rawValues[rawValuePosition];
long valueLow = rawValues[rawValuePosition + 1];
int positionIndex = positionCount * 2;
for (int i = 0; i < count; i++) {
values[positionIndex] = valueHigh;
values[positionIndex + 1] = valueLow;
positionIndex += 2;
}
hasNonNullValue = true;
}
positionCount += count;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(count * Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION);
}
}
@Override
public void appendRange(ValueBlock block, int offset, int length)
{
if (length == 0) {
return;
}
if (length == 1) {
append(block, offset);
return;
}
ensureCapacity(positionCount + length);
Int128ArrayBlock int128ArrayBlock = (Int128ArrayBlock) block;
int rawOffset = int128ArrayBlock.getRawOffset();
long[] rawValues = int128ArrayBlock.getRawValues();
System.arraycopy(rawValues, (rawOffset + offset) * 2, values, positionCount * 2, length * 2);
boolean[] rawValueIsNull = int128ArrayBlock.getRawValueIsNull();
if (rawValueIsNull != null) {
for (int i = 0; i < length; i++) {
if (rawValueIsNull[rawOffset + offset + i]) {
valueIsNull[positionCount + i] = true;
hasNullValue = true;
}
else {
hasNonNullValue = true;
}
}
}
else {
hasNonNullValue = true;
}
positionCount += length;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(length * LongArrayBlock.SIZE_IN_BYTES_PER_POSITION);
}
}
@Override
public void appendPositions(ValueBlock block, int[] positions, int offset, int length)
{
if (length == 0) {
return;
}
if (length == 1) {
append(block, positions[offset]);
return;
}
ensureCapacity(positionCount + length);
Int128ArrayBlock int128ArrayBlock = (Int128ArrayBlock) block;
int rawOffset = int128ArrayBlock.getRawOffset();
long[] rawValues = int128ArrayBlock.getRawValues();
boolean[] rawValueIsNull = int128ArrayBlock.getRawValueIsNull();
int positionIndex = positionCount * 2;
if (rawValueIsNull != null) {
for (int i = 0; i < length; i++) {
int rawPosition = positions[offset + i] + rawOffset;
if (rawValueIsNull[rawPosition]) {
valueIsNull[positionCount + i] = true;
hasNullValue = true;
}
else {
int rawValuePosition = rawPosition * 2;
values[positionIndex] = rawValues[rawValuePosition];
values[positionIndex + 1] = rawValues[rawValuePosition + 1];
hasNonNullValue = true;
}
positionIndex += 2;
}
}
else {
for (int i = 0; i < length; i++) {
int rawPosition = positions[offset + i] + rawOffset;
int rawValuePosition = rawPosition * 2;
values[positionIndex] = rawValues[rawValuePosition];
values[positionIndex + 1] = rawValues[rawValuePosition + 1];
positionIndex += 2;
}
hasNonNullValue = true;
}
positionCount += length;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(length * Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION);
}
}
@Override
public BlockBuilder appendNull()
{
ensureCapacity(positionCount + 1);
valueIsNull[positionCount] = true;
hasNullValue = true;
positionCount++;
if (blockBuilderStatus != null) {
blockBuilderStatus.addBytes(Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION);
}
return this;
}
@Override
public void resetTo(int position)
{
checkIndex(position, positionCount + 1);
positionCount = position;
}
@Override
public Block build()
{
if (!hasNonNullValue) {
return RunLengthEncodedBlock.create(NULL_VALUE_BLOCK, positionCount);
}
return buildValueBlock();
}
@Override
public Int128ArrayBlock buildValueBlock()
{
return new Int128ArrayBlock(0, positionCount, hasNullValue ? valueIsNull : null, values);
}
@Override
public BlockBuilder newBlockBuilderLike(int expectedEntries, BlockBuilderStatus blockBuilderStatus)
{
return new Int128ArrayBlockBuilder(blockBuilderStatus, expectedEntries);
}
private void ensureCapacity(int capacity)
{
if (valueIsNull.length >= capacity) {
return;
}
int newSize;
if (initialized) {
newSize = calculateNewArraySize(capacity);
}
else {
newSize = initialEntryCount;
initialized = true;
}
newSize = max(newSize, capacity);
valueIsNull = Arrays.copyOf(valueIsNull, newSize);
values = Arrays.copyOf(values, newSize * 2);
updateRetainedSize();
}
private void updateRetainedSize()
{
retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values);
if (blockBuilderStatus != null) {
retainedSizeInBytes += BlockBuilderStatus.INSTANCE_SIZE;
}
}
@Override
public long getSizeInBytes()
{
return Int128ArrayBlock.SIZE_IN_BYTES_PER_POSITION * (long) positionCount;
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes;
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder("Int128ArrayBlockBuilder{");
sb.append("positionCount=").append(getPositionCount());
sb.append('}');
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy