Search in sources :

Example 1 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class TestRowOperators method assertRowHashOperator.

private void assertRowHashOperator(String inputString, List<Type> types, List<Object> elements) {
    checkArgument(types.size() == elements.size(), "types and elements must have the same size");
    RowType rowType = RowType.anonymous(types);
    BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1);
    BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < types.size(); i++) {
        appendToBlockBuilder(types.get(i), elements.get(i), singleRowBlockWriter);
    }
    blockBuilder.closeEntry();
    assertOperator(HASH_CODE, inputString, BIGINT, rowType.hash(blockBuilder.build(), 0));
}
Also used : RowType(io.prestosql.spi.type.RowType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(io.prestosql.util.StructuralTestUtil.appendToBlockBuilder)

Example 2 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class RowExpressionVerifier method visitDereferenceExpression.

@Override
protected Boolean visitDereferenceExpression(DereferenceExpression expected, RowExpression actual) {
    if (!(actual instanceof SpecialForm) || !(((SpecialForm) actual).getForm().equals(DEREFERENCE))) {
        return false;
    }
    SpecialForm actualDereference = (SpecialForm) actual;
    if (actualDereference.getArguments().size() == 2 && actualDereference.getArguments().get(0).getType() instanceof RowType && actualDereference.getArguments().get(1) instanceof ConstantExpression) {
        RowType rowType = (RowType) actualDereference.getArguments().get(0).getType();
        Object value = LiteralInterpreter.evaluate((ConstantExpression) actualDereference.getArguments().get(1));
        checkState(value instanceof Long);
        long index = (Long) value;
        checkState(index >= 0 && index < rowType.getFields().size());
        RowType.Field field = rowType.getFields().get(toIntExact(index));
        checkState(field.getName().isPresent());
        return expected.getField().getValue().equals(field.getName().get()) && process(expected.getBase(), actualDereference.getArguments().get(0));
    }
    return false;
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowType(io.prestosql.spi.type.RowType) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 3 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class AbstractTestType method getNonNullValueForType.

/**
 * @return a non-null value, represented in native container type
 */
private static Object getNonNullValueForType(Type type) {
    if (type.getJavaType() == boolean.class) {
        return true;
    }
    if (type.getJavaType() == long.class) {
        return 1L;
    }
    if (type.getJavaType() == double.class) {
        return 1.0;
    }
    if (type.getJavaType() == Slice.class) {
        return Slices.utf8Slice("_");
    }
    if (type instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) type;
        Type elementType = arrayType.getElementType();
        Object elementNonNullValue = getNonNullValueForType(elementType);
        return arrayBlockOf(elementType, elementNonNullValue);
    }
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        Type keyType = mapType.getKeyType();
        Type valueType = mapType.getValueType();
        Object keyNonNullValue = getNonNullValueForType(keyType);
        Object valueNonNullValue = getNonNullValueForType(valueType);
        Map<?, ?> map = ImmutableMap.of(keyNonNullValue, valueNonNullValue);
        return mapBlockOf(keyType, valueType, map);
    }
    if (type instanceof RowType) {
        RowType rowType = (RowType) type;
        List<Type> elementTypes = rowType.getTypeParameters();
        Object[] elementNonNullValues = elementTypes.stream().map(AbstractTestType::getNonNullValueForType).toArray(Object[]::new);
        return toRow(elementTypes, elementNonNullValues);
    }
    throw new IllegalStateException("Unsupported Java type " + type.getJavaType() + " (for type " + type + ")");
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) UnknownType(io.prestosql.spi.type.UnknownType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType)

Example 4 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class JsonToRowCast method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    checkArgument(arity == 1, "Expected arity to be 1");
    RowType rowType = (RowType) boundVariables.getTypeVariable("T");
    checkCondition(canCastFromJson(rowType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", rowType);
    List<Field> rowFields = rowType.getFields();
    BlockBuilderAppender[] fieldAppenders = rowFields.stream().map(rowField -> createBlockBuilderAppender(rowField.getType())).toArray(BlockBuilderAppender[]::new);
    MethodHandle methodHandle = METHOD_HANDLE.bindTo(rowType).bindTo(fieldAppenders).bindTo(getFieldNameToIndex(rowFields));
    return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
Also used : BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) StandardTypes(io.prestosql.spi.type.StandardTypes) BoundVariables(io.prestosql.metadata.BoundVariables) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) BlockBuilderAppender.createBlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) BlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) Failures.checkCondition(io.prestosql.util.Failures.checkCondition) START_OBJECT(com.fasterxml.jackson.core.JsonToken.START_OBJECT) OperatorType(io.prestosql.spi.function.OperatorType) Map(java.util.Map) RowType(io.prestosql.spi.type.RowType) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) Block(io.prestosql.spi.block.Block) PrestoException(io.prestosql.spi.PrestoException) JsonParser(com.fasterxml.jackson.core.JsonParser) BlockBuilder(io.prestosql.spi.block.BlockBuilder) SqlOperator(io.prestosql.metadata.SqlOperator) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) INVALID_CAST_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_CAST_ARGUMENT) JsonUtil.getFieldNameToIndex(io.prestosql.util.JsonUtil.getFieldNameToIndex) String.format(java.lang.String.format) List(java.util.List) JSON_FACTORY(io.prestosql.util.JsonUtil.JSON_FACTORY) START_ARRAY(com.fasterxml.jackson.core.JsonToken.START_ARRAY) JsonUtil.canCastFromJson(io.prestosql.util.JsonUtil.canCastFromJson) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) Optional(java.util.Optional) Field(io.prestosql.spi.type.RowType.Field) Signature.withVariadicBound(io.prestosql.spi.function.Signature.withVariadicBound) JsonUtil.truncateIfNecessaryForErrorMessage(io.prestosql.util.JsonUtil.truncateIfNecessaryForErrorMessage) SingleRowBlockWriter(io.prestosql.spi.block.SingleRowBlockWriter) JsonUtil.parseJsonToSingleRowBlock(io.prestosql.util.JsonUtil.parseJsonToSingleRowBlock) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) JsonCastException(io.prestosql.util.JsonCastException) Field(io.prestosql.spi.type.RowType.Field) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BlockBuilderAppender.createBlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) BlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender) RowType(io.prestosql.spi.type.RowType) MethodHandle(java.lang.invoke.MethodHandle)

Example 5 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).intValue());
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).shortValue());
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).byteValue());
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, ((Number) value).doubleValue());
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (TIME.equals(type)) {
        SqlTime time = (SqlTime) value;
        if (time.isLegacyTimestamp()) {
            type.writeLong(blockBuilder, time.getMillisUtc());
        } else {
            type.writeLong(blockBuilder, time.getMillis());
        }
    } else if (TIME_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (TIMESTAMP.equals(type)) {
        long millisUtc = ((SqlTimestamp) value).getMillis();
        type.writeLong(blockBuilder, millisUtc);
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        List<Object> list = (List<Object>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<Object, Object> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<Object> row = (List<Object>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlTime(io.prestosql.spi.type.SqlTime) SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) MapType(io.prestosql.spi.type.MapType) VarcharType(io.prestosql.spi.type.VarcharType) SqlTimestampWithTimeZone(io.prestosql.spi.type.SqlTimestampWithTimeZone) SqlDate(io.prestosql.spi.type.SqlDate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CharType(io.prestosql.spi.type.CharType) TimeZoneKey(io.prestosql.spi.type.TimeZoneKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Aggregations

RowType (io.prestosql.spi.type.RowType)44 ArrayType (io.prestosql.spi.type.ArrayType)21 Type (io.prestosql.spi.type.Type)21 MapType (io.prestosql.spi.type.MapType)18 BlockBuilder (io.prestosql.spi.block.BlockBuilder)13 Test (org.testng.annotations.Test)13 ImmutableList (com.google.common.collect.ImmutableList)12 Block (io.prestosql.spi.block.Block)10 List (java.util.List)10 VarcharType (io.prestosql.spi.type.VarcharType)8 ImmutableMap (com.google.common.collect.ImmutableMap)5 CharType (io.prestosql.spi.type.CharType)5 Field (io.prestosql.spi.type.RowType.Field)5 UnknownType (io.prestosql.spi.type.UnknownType)5 BigintType (io.prestosql.spi.type.BigintType)4 BooleanType (io.prestosql.spi.type.BooleanType)4 DecimalType (io.prestosql.spi.type.DecimalType)4 DoubleType (io.prestosql.spi.type.DoubleType)4 IntegerType (io.prestosql.spi.type.IntegerType)4 RealType (io.prestosql.spi.type.RealType)4