Search in sources :

Example 41 with MapType

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++;
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) SqlTimestamp(io.trino.spi.type.SqlTimestamp) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) VarbinaryType(io.trino.spi.type.VarbinaryType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) VarbinaryType(io.trino.spi.type.VarbinaryType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 42 with MapType

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);
        }
    }
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MapType(io.trino.spi.type.MapType) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 43 with MapType

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);
}
Also used : MapType(io.trino.spi.type.MapType) StructuralTestUtil.appendToBlockBuilder(io.trino.util.StructuralTestUtil.appendToBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 44 with MapType

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));
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Expression(org.apache.iceberg.expressions.Expression) VerifyException(com.google.common.base.VerifyException) ArrayList(java.util.ArrayList) RowType(io.trino.spi.type.RowType) Range(io.trino.spi.predicate.Range) MapType(io.trino.spi.type.MapType)

Example 45 with MapType

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);
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Map(java.util.Map) MapType(io.trino.spi.type.MapType) TypeOperators(io.trino.spi.type.TypeOperators) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Aggregations

MapType (io.trino.spi.type.MapType)85 Type (io.trino.spi.type.Type)45 ArrayType (io.trino.spi.type.ArrayType)42 RowType (io.trino.spi.type.RowType)38 BlockBuilder (io.trino.spi.block.BlockBuilder)28 VarcharType (io.trino.spi.type.VarcharType)26 Block (io.trino.spi.block.Block)17 DecimalType (io.trino.spi.type.DecimalType)17 Map (java.util.Map)17 Test (org.testng.annotations.Test)17 ImmutableList (com.google.common.collect.ImmutableList)16 List (java.util.List)14 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)13 CharType (io.trino.spi.type.CharType)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 TypeOperators (io.trino.spi.type.TypeOperators)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 VarbinaryType (io.trino.spi.type.VarbinaryType)8 Collectors.toList (java.util.stream.Collectors.toList)8