use of io.trino.spi.function.TypeParameter in project trino by trinodb.
the class ApproximateCountDistinctAggregation method input.
@InputFunction
@TypeParameter("T")
public static void input(@OperatorDependency(operator = XX_HASH_64, argumentTypes = "T", convention = @Convention(arguments = NEVER_NULL, result = FAIL_ON_NULL)) MethodHandle methodHandle, @AggregationState HyperLogLogState state, @SqlType("T") double value, @SqlType(StandardTypes.DOUBLE) double maxStandardError) {
HyperLogLog hll = getOrCreateHyperLogLog(state, maxStandardError);
state.addMemoryUsage(-hll.estimatedInMemorySize());
long hash;
try {
hash = (long) methodHandle.invokeExact(value);
} catch (Throwable t) {
throw internalError(t);
}
hll.addHash(hash);
state.addMemoryUsage(hll.estimatedInMemorySize());
}
use of io.trino.spi.function.TypeParameter in project trino by trinodb.
the class MapEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("array(row(K,V))")
public Block mapFromEntries(@TypeParameter("row(K,V)") RowType rowType, @SqlType("map(K,V)") Block block) {
verify(rowType.getTypeParameters().size() == 2);
verify(block.getPositionCount() % 2 == 0);
Type keyType = rowType.getTypeParameters().get(0);
Type valueType = rowType.getTypeParameters().get(1);
ArrayType arrayType = new ArrayType(rowType);
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = block.getPositionCount() / 2;
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
for (int i = 0; i < entryCount; i++) {
BlockBuilder rowBuilder = entryBuilder.beginBlockEntry();
keyType.appendTo(block, 2 * i, rowBuilder);
valueType.appendTo(block, 2 * i + 1, rowBuilder);
entryBuilder.closeEntry();
}
blockBuilder.closeEntry();
pageBuilder.declarePosition();
return arrayType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.function.TypeParameter in project trino by trinodb.
the class MapFromEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual keyEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "K", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode keyHashCode, @TypeParameter("map(K,V)") MapType mapType, ConnectorSession session, @SqlType("array(row(K,V))") Block mapEntries) {
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
RowType mapEntryType = RowType.anonymous(ImmutableList.of(keyType, valueType));
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = mapEntries.getPositionCount();
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
TypedSet uniqueKeys = createEqualityTypedSet(keyType, keyEqual, keyHashCode, entryCount, "map_from_entries");
for (int i = 0; i < entryCount; i++) {
if (mapEntries.isNull(i)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block mapEntryBlock = mapEntryType.getObject(mapEntries, i);
if (mapEntryBlock.isNull(0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (!uniqueKeys.add(mapEntryBlock, 0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(session, mapEntryBlock, 0)));
}
keyType.appendTo(mapEntryBlock, 0, resultBuilder);
valueType.appendTo(mapEntryBlock, 1, resultBuilder);
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.function.TypeParameter in project trino by trinodb.
the class MultimapFromEntriesFunction method multimapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,array(V))")
@SqlNullable
public Block multimapFromEntries(@TypeParameter("map(K,array(V))") MapType mapType, @OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual keyEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "K", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode keyHashCode, @SqlType("array(row(K,V))") Block mapEntries) {
Type keyType = mapType.getKeyType();
Type valueType = ((ArrayType) mapType.getValueType()).getElementType();
RowType mapEntryType = RowType.anonymous(ImmutableList.of(keyType, valueType));
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = mapEntries.getPositionCount();
if (entryCount > entryIndicesList.length) {
initializeEntryIndicesList(entryCount);
}
TypedSet keySet = createEqualityTypedSet(keyType, keyEqual, keyHashCode, entryCount, NAME);
for (int i = 0; i < entryCount; i++) {
if (mapEntries.isNull(i)) {
clearEntryIndices(keySet.size());
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block mapEntryBlock = mapEntryType.getObject(mapEntries, i);
if (mapEntryBlock.isNull(0)) {
clearEntryIndices(keySet.size());
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (keySet.add(mapEntryBlock, 0)) {
entryIndicesList[keySet.size() - 1].add(i);
} else {
entryIndicesList[keySet.positionOf(mapEntryBlock, 0)].add(i);
}
}
BlockBuilder multimapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder mapWriter = multimapBlockBuilder.beginBlockEntry();
for (int i = 0; i < keySet.size(); i++) {
keyType.appendTo(mapEntryType.getObject(mapEntries, entryIndicesList[i].getInt(0)), 0, mapWriter);
BlockBuilder valuesArray = mapWriter.beginBlockEntry();
for (int entryIndex : entryIndicesList[i]) {
valueType.appendTo(mapEntryType.getObject(mapEntries, entryIndex), 1, valuesArray);
}
mapWriter.closeEntry();
}
multimapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
clearEntryIndices(keySet.size());
return mapType.getObject(multimapBlockBuilder, multimapBlockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.function.TypeParameter in project trino by trinodb.
the class ArrayToArrayCast method filterLong.
@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType("array(T)")
public static Block filterLong(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
int positionCount = array.getPositionCount();
BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
for (int position = 0; position < positionCount; position++) {
if (!array.isNull(position)) {
Long value = (Long) cast.invokeExact(session, array, position);
if (value != null) {
resultType.writeLong(resultBuilder, value);
continue;
}
}
resultBuilder.appendNull();
}
return resultBuilder.build();
}
Aggregations