io.trino.spi.block.MapBlockBuilder Maven / Gradle / Ivy
/*
* 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 io.trino.spi.type.MapType;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.ObjLongConsumer;
import static io.airlift.slice.SizeOf.instanceSize;
import static io.airlift.slice.SizeOf.sizeOf;
import static io.trino.spi.block.BlockUtil.calculateNewArraySize;
import static io.trino.spi.block.MapBlock.createMapBlockInternal;
import static io.trino.spi.block.MapHashTables.HASH_MULTIPLIER;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class MapBlockBuilder
extends AbstractMapBlock
implements BlockBuilder
{
private static final int INSTANCE_SIZE = instanceSize(MapBlockBuilder.class);
@Nullable
private final BlockBuilderStatus blockBuilderStatus;
private int positionCount;
private int[] offsets;
private boolean[] mapIsNull;
private boolean hasNullValue;
private final BlockBuilder keyBlockBuilder;
private final BlockBuilder valueBlockBuilder;
private final MapHashTables hashTables;
private boolean currentEntryOpened;
private boolean strict;
public MapBlockBuilder(MapType mapType, BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
this(
mapType,
blockBuilderStatus,
mapType.getKeyType().createBlockBuilder(blockBuilderStatus, expectedEntries),
mapType.getValueType().createBlockBuilder(blockBuilderStatus, expectedEntries),
new int[expectedEntries + 1],
new boolean[expectedEntries]);
}
private MapBlockBuilder(
MapType mapType,
@Nullable BlockBuilderStatus blockBuilderStatus,
BlockBuilder keyBlockBuilder,
BlockBuilder valueBlockBuilder,
int[] offsets,
boolean[] mapIsNull)
{
super(mapType);
this.blockBuilderStatus = blockBuilderStatus;
this.positionCount = 0;
this.offsets = requireNonNull(offsets, "offsets is null");
this.mapIsNull = requireNonNull(mapIsNull, "mapIsNull is null");
this.keyBlockBuilder = requireNonNull(keyBlockBuilder, "keyBlockBuilder is null");
this.valueBlockBuilder = requireNonNull(valueBlockBuilder, "valueBlockBuilder is null");
int[] hashTable = new int[mapIsNull.length * HASH_MULTIPLIER];
Arrays.fill(hashTable, -1);
this.hashTables = new MapHashTables(mapType, Optional.of(hashTable));
}
public MapBlockBuilder strict()
{
this.strict = true;
return this;
}
@Override
protected Block getRawKeyBlock()
{
return keyBlockBuilder;
}
@Override
protected Block getRawValueBlock()
{
return valueBlockBuilder;
}
@Override
protected MapHashTables getHashTables()
{
return hashTables;
}
@Override
protected int[] getOffsets()
{
return offsets;
}
@Override
protected int getOffsetBase()
{
return 0;
}
@Nullable
@Override
protected boolean[] getMapIsNull()
{
return hasNullValue ? mapIsNull : null;
}
@Override
public boolean mayHaveNull()
{
return hasNullValue;
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
return keyBlockBuilder.getSizeInBytes() + valueBlockBuilder.getSizeInBytes() +
(Integer.BYTES + Byte.BYTES) * (long) positionCount +
Integer.BYTES * HASH_MULTIPLIER * (long) keyBlockBuilder.getPositionCount();
}
@Override
public long getRetainedSizeInBytes()
{
long size = INSTANCE_SIZE
+ keyBlockBuilder.getRetainedSizeInBytes()
+ valueBlockBuilder.getRetainedSizeInBytes()
+ sizeOf(offsets)
+ sizeOf(mapIsNull)
+ hashTables.getRetainedSizeInBytes();
if (blockBuilderStatus != null) {
size += BlockBuilderStatus.INSTANCE_SIZE;
}
return size;
}
@Override
public void retainedBytesForEachPart(ObjLongConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy