Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 jakarta.annotation.Nullable;
import java.util.Arrays;
import java.util.Optional;
import java.util.OptionalInt;
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.checkArrayRange;
import static io.trino.spi.block.BlockUtil.checkReadablePosition;
import static io.trino.spi.block.BlockUtil.checkValidPositions;
import static io.trino.spi.block.BlockUtil.checkValidRegion;
import static io.trino.spi.block.BlockUtil.compactArray;
import static io.trino.spi.block.BlockUtil.compactOffsets;
import static io.trino.spi.block.BlockUtil.copyIsNullAndAppendNull;
import static io.trino.spi.block.BlockUtil.copyOffsetsAndAppendNull;
import static io.trino.spi.block.BlockUtil.countAndMarkSelectedPositionsFromOffsets;
import static io.trino.spi.block.BlockUtil.countSelectedPositionsFromOffsets;
import static io.trino.spi.block.MapHashTables.HASH_MULTIPLIER;
import static io.trino.spi.block.MapHashTables.HashBuildMode.DUPLICATE_NOT_CHECKED;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public final class MapBlock
implements ValueBlock
{
private static final int INSTANCE_SIZE = instanceSize(MapBlock.class);
private final MapType mapType;
private final int startOffset;
private final int positionCount;
@Nullable
private final boolean[] mapIsNull;
private final int[] offsets;
private final Block keyBlock;
private final Block valueBlock;
private final MapHashTables hashTables;
private final long baseSizeInBytes;
private volatile long valueSizeInBytes = -1;
private final long retainedSizeInBytes;
/**
* Create a map block directly from columnar nulls, keys, values, and offsets into the keys and values.
* A null map must have no entries.
*/
public static MapBlock fromKeyValueBlock(
Optional mapIsNull,
int[] offsets,
Block keyBlock,
Block valueBlock,
MapType mapType)
{
return fromKeyValueBlock(mapIsNull, offsets, offsets.length - 1, keyBlock, valueBlock, mapType);
}
public static MapBlock fromKeyValueBlock(
Optional mapIsNull,
int[] offsets,
int mapCount,
Block keyBlock,
Block valueBlock,
MapType mapType)
{
validateConstructorArguments(mapType, 0, mapCount, mapIsNull.orElse(null), offsets, keyBlock, valueBlock);
return createMapBlockInternal(
mapType,
0,
mapCount,
mapIsNull,
offsets,
keyBlock,
valueBlock,
new MapHashTables(mapType, DUPLICATE_NOT_CHECKED, mapCount, Optional.empty()));
}
/**
* Create a map block directly without per element validations.
*
* Internal use by this package and io.trino.spi.Type only.
*/
public static MapBlock createMapBlockInternal(
MapType mapType,
int startOffset,
int positionCount,
Optional mapIsNull,
int[] offsets,
Block keyBlock,
Block valueBlock,
MapHashTables hashTables)
{
validateConstructorArguments(mapType, startOffset, positionCount, mapIsNull.orElse(null), offsets, keyBlock, valueBlock);
requireNonNull(hashTables, "hashTables is null");
return new MapBlock(mapType, startOffset, positionCount, mapIsNull.orElse(null), offsets, keyBlock, valueBlock, hashTables);
}
private static void validateConstructorArguments(
MapType mapType, int startOffset,
int positionCount,
@Nullable boolean[] mapIsNull,
int[] offsets,
Block keyBlock,
Block valueBlock)
{
if (startOffset < 0) {
throw new IllegalArgumentException("startOffset is negative");
}
if (positionCount < 0) {
throw new IllegalArgumentException("positionCount is negative");
}
if (mapIsNull != null && mapIsNull.length - startOffset < positionCount) {
throw new IllegalArgumentException("isNull length is less than positionCount");
}
requireNonNull(offsets, "offsets is null");
if (offsets.length - startOffset < positionCount + 1) {
throw new IllegalArgumentException("offsets length is less than positionCount");
}
requireNonNull(keyBlock, "keyBlock is null");
requireNonNull(valueBlock, "valueBlock is null");
if (keyBlock.getPositionCount() != valueBlock.getPositionCount()) {
throw new IllegalArgumentException(format("keyBlock and valueBlock has different size: %s %s", keyBlock.getPositionCount(), valueBlock.getPositionCount()));
}
requireNonNull(mapType, "mapType is null");
}
/**
* Use createRowBlockInternal or fromKeyValueBlock instead of this method. The caller of this method is assumed to have
* validated the arguments with validateConstructorArguments.
*/
private MapBlock(
MapType mapType,
int startOffset,
int positionCount,
@Nullable boolean[] mapIsNull,
int[] offsets,
Block keyBlock,
Block valueBlock,
MapHashTables hashTables)
{
this.mapType = requireNonNull(mapType, "mapType is null");
int[] rawHashTables = hashTables.tryGet().orElse(null);
if (rawHashTables != null && rawHashTables.length < keyBlock.getPositionCount() * HASH_MULTIPLIER) {
throw new IllegalArgumentException(format("keyBlock/valueBlock size does not match hash table size: %s %s", keyBlock.getPositionCount(), rawHashTables.length));
}
this.startOffset = startOffset;
this.positionCount = positionCount;
this.mapIsNull = mapIsNull;
this.offsets = offsets;
this.keyBlock = keyBlock;
this.valueBlock = valueBlock;
this.hashTables = hashTables;
int entryCount = offsets[startOffset + positionCount] - offsets[startOffset];
this.baseSizeInBytes = Integer.BYTES * HASH_MULTIPLIER * (long) entryCount +
(Integer.BYTES + Byte.BYTES) * (long) this.positionCount +
calculateSize(keyBlock);
this.retainedSizeInBytes = INSTANCE_SIZE + sizeOf(offsets) + sizeOf(mapIsNull);
}
Block getRawKeyBlock()
{
return keyBlock;
}
Block getRawValueBlock()
{
return valueBlock;
}
MapHashTables getHashTables()
{
return hashTables;
}
int[] getOffsets()
{
return offsets;
}
int getOffsetBase()
{
return startOffset;
}
@Override
public boolean mayHaveNull()
{
return mapIsNull != null;
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public long getSizeInBytes()
{
if (valueSizeInBytes < 0) {
if (!valueBlock.isLoaded()) {
return baseSizeInBytes + valueBlock.getSizeInBytes();
}
valueSizeInBytes = calculateSize(valueBlock);
}
return baseSizeInBytes + valueSizeInBytes;
}
private long calculateSize(Block block)
{
int entriesStart = offsets[startOffset];
int entriesEnd = offsets[startOffset + positionCount];
int entryCount = entriesEnd - entriesStart;
return block.getRegionSizeInBytes(entriesStart, entryCount);
}
@Override
public long getRetainedSizeInBytes()
{
return retainedSizeInBytes + keyBlock.getRetainedSizeInBytes() + valueBlock.getRetainedSizeInBytes() + hashTables.getRetainedSizeInBytes();
}
@Override
public void retainedBytesForEachPart(ObjLongConsumer