use of io.trino.spi.type.MapType in project trino by trinodb.
the class AbstractRowEncoder method appendColumnValue.
@Override
public void appendColumnValue(Block block, int position) {
checkArgument(currentColumnIndex < columnHandles.size(), format("currentColumnIndex '%d' is greater than number of columns '%d'", currentColumnIndex, columnHandles.size()));
Type type = columnHandles.get(currentColumnIndex).getType();
if (block.isNull(position)) {
appendNullValue();
} else if (type == BOOLEAN) {
appendBoolean(type.getBoolean(block, position));
} else if (type == BIGINT) {
appendLong(type.getLong(block, position));
} else if (type == INTEGER) {
appendInt(toIntExact(type.getLong(block, position)));
} else if (type == SMALLINT) {
appendShort(Shorts.checkedCast(type.getLong(block, position)));
} else if (type == TINYINT) {
appendByte(SignedBytes.checkedCast(type.getLong(block, position)));
} else if (type == DOUBLE) {
appendDouble(type.getDouble(block, position));
} else if (type == REAL) {
appendFloat(intBitsToFloat(toIntExact(type.getLong(block, position))));
} else if (type instanceof VarcharType) {
appendString(type.getSlice(block, position).toStringUtf8());
} else if (type instanceof VarbinaryType) {
appendByteBuffer(type.getSlice(block, position).toByteBuffer());
} else if (type == DATE) {
appendSqlDate((SqlDate) type.getObjectValue(session, block, position));
} else if (type instanceof TimeType) {
appendSqlTime((SqlTime) type.getObjectValue(session, block, position));
} else if (type instanceof TimeWithTimeZoneType) {
appendSqlTimeWithTimeZone((SqlTimeWithTimeZone) type.getObjectValue(session, block, position));
} else if (type instanceof TimestampType) {
appendSqlTimestamp((SqlTimestamp) type.getObjectValue(session, block, position));
} else if (type instanceof TimestampWithTimeZoneType) {
appendSqlTimestampWithTimeZone((SqlTimestampWithTimeZone) type.getObjectValue(session, block, position));
} else if (type instanceof ArrayType) {
appendArray((List<Object>) type.getObjectValue(session, block, position));
} else if (type instanceof MapType) {
appendMap((Map<Object, Object>) type.getObjectValue(session, block, position));
} else if (type instanceof RowType) {
appendRow((List<Object>) type.getObjectValue(session, block, position));
} else {
throw new UnsupportedOperationException(format("Unsupported type '%s' for column '%s'", type, columnHandles.get(currentColumnIndex).getName()));
}
currentColumnIndex++;
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class PrometheusRecordCursor method writeObject.
private static void writeObject(BlockBuilder builder, Type type, Object obj) {
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
BlockBuilder arrayBuilder = builder.beginBlockEntry();
for (Object item : (List<?>) obj) {
writeObject(arrayBuilder, elementType, item);
}
builder.closeEntry();
} else if (type instanceof MapType) {
MapType mapType = (MapType) type;
BlockBuilder mapBlockBuilder = builder.beginBlockEntry();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
writeObject(mapBlockBuilder, mapType.getKeyType(), entry.getKey());
writeObject(mapBlockBuilder, mapType.getValueType(), entry.getValue());
}
builder.closeEntry();
} else {
if (BOOLEAN.equals(type) || TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type) || type instanceof VarcharType) {
TypeUtils.writeNativeValue(type, builder, obj);
}
}
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class StructuralTestUtil method mapBlockOf.
public static Block mapBlockOf(Type keyType, Type valueType, Object[] keys, Object[] values) {
checkArgument(keys.length == values.length, "keys/values must have the same length");
MapType mapType = mapType(keyType, valueType);
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 10);
BlockBuilder singleMapBlockWriter = blockBuilder.beginBlockEntry();
for (int i = 0; i < keys.length; i++) {
Object key = keys[i];
Object value = values[i];
appendToBlockBuilder(keyType, key, singleMapBlockWriter);
appendToBlockBuilder(valueType, value, singleMapBlockWriter);
}
blockBuilder.closeEntry();
return mapType.getObject(blockBuilder, 0);
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class ExpressionConverter method toIcebergExpression.
private static Expression toIcebergExpression(String columnName, Type type, Domain domain) {
if (domain.isAll()) {
return alwaysTrue();
}
if (domain.getValues().isNone()) {
return domain.isNullAllowed() ? isNull(columnName) : alwaysFalse();
}
if (domain.getValues().isAll()) {
return domain.isNullAllowed() ? alwaysTrue() : not(isNull(columnName));
}
// Skip structural types. TODO (https://github.com/trinodb/trino/issues/8759) Evaluate Apache Iceberg's support for predicate on structural types
if (type instanceof ArrayType || type instanceof MapType || type instanceof RowType) {
// Fail fast. Ignoring expression could lead to data loss in case of deletions.
throw new UnsupportedOperationException("Unsupported type for expression: " + type);
}
if (type.isOrderable()) {
List<Range> orderedRanges = domain.getValues().getRanges().getOrderedRanges();
List<Object> icebergValues = new ArrayList<>();
List<Expression> rangeExpressions = new ArrayList<>();
for (Range range : orderedRanges) {
if (range.isSingleValue()) {
icebergValues.add(getIcebergLiteralValue(type, range.getLowBoundedValue()));
} else {
rangeExpressions.add(toIcebergExpression(columnName, range));
}
}
Expression ranges = or(rangeExpressions);
Expression values = icebergValues.isEmpty() ? alwaysFalse() : in(columnName, icebergValues);
Expression nullExpression = domain.isNullAllowed() ? isNull(columnName) : alwaysFalse();
return or(nullExpression, or(values, ranges));
}
throw new VerifyException(format("Unsupported type %s with domain values %s", type, domain));
}
use of io.trino.spi.type.MapType in project yauaa by nielsbasjes.
the class ParseUserAgentFunction method parseUserAgent.
@ScalarFunction("parse_user_agent")
@Description("Tries to parse and analyze the provided useragent string and extract as many attributes " + "as possible. Uses Yauaa (Yet Another UserAgent Analyzer) version " + Version.PROJECT_VERSION + ". " + "See https://yauaa.basjes.nl/udf/trino/ for documentation.")
@SqlType("map(varchar, varchar)")
public static Block parseUserAgent(@SqlType(StandardTypes.VARCHAR) Slice userAgentSlice) throws IllegalArgumentException {
String userAgentStringToParse = null;
if (userAgentSlice != null) {
userAgentStringToParse = userAgentSlice.toStringUtf8();
}
UserAgentAnalyzer userAgentAnalyzer = getInstance();
UserAgent userAgent = userAgentAnalyzer.parse(userAgentStringToParse);
Map<String, String> resultMap = userAgent.toMap(userAgent.getAvailableFieldNamesSorted());
MapType mapType = new MapType(VARCHAR, VARCHAR, new TypeOperators());
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, resultMap.size());
BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<String, String> entry : resultMap.entrySet()) {
VARCHAR.writeString(singleMapBlockBuilder, entry.getKey());
VARCHAR.writeString(singleMapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Aggregations