@Override void readFirstField(Block block, int position, BlockAndBlockPositionValueState state) { state.setFirst((Block) firstType.getObject(block, position)); }
@Override public void deserialize(Block block, int index, BlockState state) { state.setBlock((Block) type.getObject(block, index)); } }
@Override public Object getObject(int field) { return types.get(field).getObject(page.getBlock(field), position); }
@Override public void deserialize(Block block, int index, HistogramState state) { state.deserialize((Block) serializedType.getObject(block, index), type, EXPECTED_SIZE_FOR_HASHING); } }
@Override public Object getObject(int field) { checkState(position >= 0, "Not yet advanced"); checkState(position < page.getPositionCount(), "Already finished"); Type type = types.get(field); return type.getObject(page.getBlock(field), position); }
public static boolean isDistinctFrom(Type rowType, List<MethodHandle> argumentMethods, Block leftRow, int leftPosition, Block rightRow, int rightPosition) { return isDistinctFrom( rowType, argumentMethods, (Block) rowType.getObject(leftRow, leftPosition), leftRow.isNull(leftPosition), (Block) rowType.getObject(rightRow, rightPosition), rightRow.isNull(rightPosition)); } }
@Override public void deserialize(Block block, int index, MinMaxByNState state) { Block currentBlock = (Block) serializedType.getObject(block, index); state.setTypedKeyValueHeap(TypedKeyValueHeap.deserialize(currentBlock, keyType, valueType, blockComparator)); } }
private void fillUnnesters() { for (int i = 0; i < unnestTypes.size(); i++) { Type type = unnestTypes.get(i); int channel = unnestChannels.get(i); Block block = null; if (!currentPage.getBlock(channel).isNull(currentPosition)) { block = (Block) type.getObject(currentPage.getBlock(channel), currentPosition); } unnesters.get(i).setBlock(block); } ordinalityCount = 0; }
@UsedByGeneratedCode public static Object objectSubscript(Type elementType, Block array, long index) { checkIndex(array, index); int position = toIntExact(index - 1); if (array.isNull(position)) { return null; } return elementType.getObject(array, position); }
public Object getObject(int channel, int position) { long pageAddress = valueAddresses.getLong(position); Block block = channels[channel].get(decodeSliceIndex(pageAddress)); int blockPosition = decodePosition(pageAddress); return types.get(channel).getObject(block, blockPosition); }
@Override public void deserialize(Block block, int index, ArrayAggregationState state) { state.reset(); Block stateBlock = (Block) arrayType.getObject(block, index); for (int i = 0; i < stateBlock.getPositionCount(); i++) { state.add(stateBlock, i); } } }
@Override public void deserialize(Block block, int index, MinMaxNState state) { Block currentBlock = (Block) serializedType.getObject(block, index); int capacity = toIntExact(BIGINT.getLong(currentBlock, 0)); Block heapBlock = arrayType.getObject(currentBlock, 1); TypedHeap heap = new TypedHeap(blockComparator, elementType, capacity); heap.addAll(heapBlock); state.setTypedHeap(heap); } }
/** * Encodes the given map into a Block. * * @param mapType Presto type of the map * @param map Map of key/value pairs to encode * @return Presto Block */ static Block getBlockFromMap(Type mapType, Map<?, ?> map) { Type keyType = mapType.getTypeParameters().get(0); Type valueType = mapType.getTypeParameters().get(1); BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1); BlockBuilder builder = mapBlockBuilder.beginBlockEntry(); for (Entry<?, ?> entry : map.entrySet()) { writeObject(builder, keyType, entry.getKey()); writeObject(builder, valueType, entry.getValue()); } mapBlockBuilder.closeEntry(); return (Block) mapType.getObject(mapBlockBuilder, 0); }
@ScalarFunction @SqlType("map(bigint,smallint)") public static Block hashCounts(@TypeParameter("map<bigint,smallint>") Type mapType, @SqlType(SetDigestType.NAME) Slice slice) { SetDigest digest = SetDigest.newInstance(slice); // Maybe use static BlockBuilderStatus in order avoid `new`? BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 1); BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry(); for (Map.Entry<Long, Short> entry : digest.getHashCounts().entrySet()) { BIGINT.writeLong(singleMapBlockBuilder, entry.getKey()); SMALLINT.writeLong(singleMapBlockBuilder, entry.getValue()); } blockBuilder.closeEntry(); return (Block) mapType.getObject(blockBuilder, 0); }
@TypeParameter("T") @TypeParameterSpecialization(name = "T", nativeContainerType = Block.class) @SqlType("array(T)") public Block sortObject( @TypeParameter("T") Type type, @SqlType("array(T)") Block block, @SqlType("function(T, T, int)") ComparatorBlockLambda function) { int arrayLength = block.getPositionCount(); initPositionsList(arrayLength); Comparator<Integer> comparator = (x, y) -> comparatorResult(function.apply( block.isNull(x) ? null : (Block) type.getObject(block, x), block.isNull(y) ? null : (Block) type.getObject(block, y))); sortPositions(arrayLength, comparator); return computeResultBlock(type, block, arrayLength); }
/** * Get the native value as an object in the value at {@code position} of {@code block}. */ public static Object readNativeValue(Type type, Block block, int position) { Class<?> javaType = type.getJavaType(); if (block.isNull(position)) { return null; } if (javaType == long.class) { return type.getLong(block, position); } if (javaType == double.class) { return type.getDouble(block, position); } if (javaType == boolean.class) { return type.getBoolean(block, position); } if (javaType == Slice.class) { return type.getSlice(block, position); } return type.getObject(block, position); }
@TypeParameter("E") @SqlNullable @SqlType("E") public static Block blockElementAt(@TypeParameter("E") Type elementType, @SqlType("array(E)") Block array, @SqlType("bigint") long index) { int position = checkedIndexToBlockPosition(array, index); if (position == -1) { return null; } if (array.isNull(position)) { return null; } return (Block) elementType.getObject(array, position); }
public static Block flatten(Type type, Type arrayType, Block array) { if (array.getPositionCount() == 0) { return type.createBlockBuilder(null, 0).build(); } BlockBuilder builder = type.createBlockBuilder(null, array.getPositionCount(), toIntExact(array.getSizeInBytes() / array.getPositionCount())); for (int i = 0; i < array.getPositionCount(); i++) { if (!array.isNull(i)) { Block subArray = (Block) arrayType.getObject(array, i); for (int j = 0; j < subArray.getPositionCount(); j++) { type.appendTo(subArray, j, builder); } } } return builder.build(); } }
@TypeParameter("T") @TypeParameterSpecialization(name = "T", nativeContainerType = Block.class) @SqlType("array(T)") public static Block filterBlock( @TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") FilterBlockLambda function) { int positionCount = arrayBlock.getPositionCount(); BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount); for (int position = 0; position < positionCount; position++) { Block input = null; if (!arrayBlock.isNull(position)) { input = (Block) elementType.getObject(arrayBlock, position); } Boolean keep = function.apply(input); if (TRUE.equals(keep)) { elementType.appendTo(arrayBlock, position, resultBuilder); } } return resultBuilder.build(); }
@Override protected Object getGreaterValue(Object value) { RowBlockBuilder blockBuilder = (RowBlockBuilder) TYPE.createBlockBuilder(null, 1); SingleRowBlockWriter singleRowBlockWriter; Block block = (Block) value; singleRowBlockWriter = blockBuilder.beginBlockEntry(); BIGINT.writeLong(singleRowBlockWriter, block.getSingleValueBlock(0).getLong(0, 0) + 1); VARCHAR.writeSlice(singleRowBlockWriter, block.getSingleValueBlock(1).getSlice(0, 0, 1)); blockBuilder.closeEntry(); return TYPE.getObject(blockBuilder.build(), 0); } }