use of io.trino.spi.block.BlockBuilder 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.block.BlockBuilder in project trino by trinodb.
the class QuantileDigestFunctions method valuesAtQuantilesBigint.
@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(bigint)")
public static Block valuesAtQuantilesBigint(@SqlType("qdigest(bigint)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
QuantileDigest digest = new QuantileDigest(input);
BlockBuilder output = BIGINT.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
BIGINT.writeLong(output, digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)));
}
return output.build();
}
use of io.trino.spi.block.BlockBuilder in project trino by trinodb.
the class QuantileDigestFunctions method valuesAtQuantilesDouble.
@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(double)")
public static Block valuesAtQuantilesDouble(@SqlType("qdigest(double)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
QuantileDigest digest = new QuantileDigest(input);
BlockBuilder output = DOUBLE.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
DOUBLE.writeDouble(output, sortableLongToDouble(digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i))));
}
return output.build();
}
use of io.trino.spi.block.BlockBuilder in project trino by trinodb.
the class QuantileDigestFunctions method valuesAtQuantilesReal.
@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(real)")
public static Block valuesAtQuantilesReal(@SqlType("qdigest(real)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
QuantileDigest digest = new QuantileDigest(input);
BlockBuilder output = REAL.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
REAL.writeLong(output, floatToRawIntBits(sortableIntToFloat((int) digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)))));
}
return output.build();
}
use of io.trino.spi.block.BlockBuilder in project trino by trinodb.
the class JsonToRowCast method toRow.
@UsedByGeneratedCode
public static Block toRow(RowType rowType, BlockBuilderAppender rowAppender, ConnectorSession connectorSession, Slice json) {
try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
jsonParser.nextToken();
if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
return null;
}
BlockBuilder rowBlockBuilder = rowType.createBlockBuilder(null, 1);
rowAppender.append(jsonParser, rowBlockBuilder);
if (jsonParser.nextToken() != null) {
throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
}
return rowType.getObject(rowBlockBuilder, 0);
} catch (TrinoException | JsonCastException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", rowType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
} catch (Exception e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", rowType, truncateIfNecessaryForErrorMessage(json)), e);
}
}
Aggregations