Search in sources :

Example 1 with Field

use of io.prestosql.spi.type.RowType.Field 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 2 with Field

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

the class TypeCoercion method typeCompatibilityForRow.

private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) {
    List<Field> firstFields = firstType.getFields();
    List<Field> secondFields = secondType.getFields();
    if (firstFields.size() != secondFields.size()) {
        return TypeCompatibility.incompatible();
    }
    ImmutableList.Builder<Field> fields = ImmutableList.builder();
    boolean coercible = true;
    for (int i = 0; i < firstFields.size(); i++) {
        Type firstFieldType = firstFields.get(i).getType();
        Type secondFieldType = secondFields.get(i).getType();
        TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType);
        if (!typeCompatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        Type commonParameterType = typeCompatibility.getCommonSuperType();
        Optional<String> firstParameterName = firstFields.get(i).getName();
        Optional<String> secondParameterName = secondFields.get(i).getName();
        Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty();
        // ignore parameter name for coercible
        coercible &= typeCompatibility.isCoercible();
        fields.add(new Field(commonName, commonParameterType));
    }
    return TypeCompatibility.compatible(RowType.from(fields.build()), coercible);
}
Also used : Field(io.prestosql.spi.type.RowType.Field) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) LikePatternType(io.prestosql.spi.type.LikePatternType) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) SetDigestType(io.prestosql.type.setdigest.SetDigestType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.prestosql.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with Field

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

the class BuiltInTypeRegistry method typeCompatibilityForRow.

private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) {
    List<Field> firstFields = firstType.getFields();
    List<Field> secondFields = secondType.getFields();
    if (firstFields.size() != secondFields.size()) {
        return TypeCompatibility.incompatible();
    }
    ImmutableList.Builder<Field> fields = ImmutableList.builder();
    boolean coercible = true;
    for (int i = 0; i < firstFields.size(); i++) {
        Type firstFieldType = firstFields.get(i).getType();
        Type secondFieldType = secondFields.get(i).getType();
        TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType);
        if (!typeCompatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        Type commonParameterType = typeCompatibility.getCommonSuperType();
        Optional<String> firstParameterName = firstFields.get(i).getName();
        Optional<String> secondParameterName = secondFields.get(i).getName();
        Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty();
        // ignore parameter name for coercible
        coercible &= typeCompatibility.isCoercible();
        fields.add(new Field(commonName, commonParameterType));
    }
    return TypeCompatibility.compatible(RowType.from(fields.build()), coercible);
}
Also used : Field(io.prestosql.spi.type.RowType.Field) Re2JRegexpType(io.prestosql.type.Re2JRegexpType) DecimalType(io.prestosql.spi.type.DecimalType) ColorType(io.prestosql.type.ColorType) ParametricType(io.prestosql.spi.type.ParametricType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) LikePatternType(io.prestosql.spi.type.LikePatternType) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) CharParametricType(io.prestosql.spi.type.CharParametricType) VarcharParametricType(io.prestosql.spi.type.VarcharParametricType) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) JoniRegexpType(io.prestosql.type.JoniRegexpType) CodePointsType(io.prestosql.type.CodePointsType) MapType(io.prestosql.spi.type.MapType) JsonPathType(io.prestosql.type.JsonPathType) SetDigestType(io.prestosql.type.setdigest.SetDigestType) DecimalParametricType(io.prestosql.spi.type.DecimalParametricType) VarcharType(io.prestosql.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList)

Example 4 with Field

use of io.prestosql.spi.type.RowType.Field in project pulsar by apache.

the class PulsarProtobufNativeColumnDecoder method serializeRow.

private static Block serializeRow(BlockBuilder parentBlockBuilder, Object value, Type type, String columnName) {
    if (value == null) {
        checkState(parentBlockBuilder != null, "parent block builder is null");
        parentBlockBuilder.appendNull();
        return null;
    }
    BlockBuilder blockBuilder;
    if (parentBlockBuilder != null) {
        blockBuilder = parentBlockBuilder;
    } else {
        blockBuilder = type.createBlockBuilder(null, 1);
    }
    BlockBuilder singleRowBuilder = blockBuilder.beginBlockEntry();
    checkState(value instanceof DynamicMessage, "Row Field value should be DynamicMessage type.");
    DynamicMessage record = (DynamicMessage) value;
    List<Field> fields = ((RowType) type).getFields();
    for (Field field : fields) {
        checkState(field.getName().isPresent(), "field name not found");
        serializeObject(singleRowBuilder, record.getField(((DynamicMessage) value).getDescriptorForType().findFieldByName(field.getName().get())), field.getType(), columnName);
    }
    blockBuilder.closeEntry();
    if (parentBlockBuilder == null) {
        return blockBuilder.getObject(0, Block.class);
    }
    return null;
}
Also used : Field(io.prestosql.spi.type.RowType.Field) RowType(io.prestosql.spi.type.RowType) DynamicMessage(com.google.protobuf.DynamicMessage) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 5 with Field

use of io.prestosql.spi.type.RowType.Field in project pulsar by apache.

the class PulsarAvroColumnDecoder method serializeRow.

private static Block serializeRow(BlockBuilder parentBlockBuilder, Object value, Type type, String columnName) {
    if (value == null) {
        checkState(parentBlockBuilder != null, "parent block builder is null");
        parentBlockBuilder.appendNull();
        return null;
    }
    BlockBuilder blockBuilder;
    if (parentBlockBuilder != null) {
        blockBuilder = parentBlockBuilder;
    } else {
        blockBuilder = type.createBlockBuilder(null, 1);
    }
    BlockBuilder singleRowBuilder = blockBuilder.beginBlockEntry();
    GenericRecord record = (GenericRecord) value;
    List<Field> fields = ((RowType) type).getFields();
    for (Field field : fields) {
        checkState(field.getName().isPresent(), "field name not found");
        serializeObject(singleRowBuilder, record.get(field.getName().get()), field.getType(), columnName);
    }
    blockBuilder.closeEntry();
    if (parentBlockBuilder == null) {
        return blockBuilder.getObject(0, Block.class);
    }
    return null;
}
Also used : Field(io.prestosql.spi.type.RowType.Field) RowType(io.prestosql.spi.type.RowType) GenericRecord(org.apache.avro.generic.GenericRecord) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Aggregations

RowType (io.prestosql.spi.type.RowType)5 Field (io.prestosql.spi.type.RowType.Field)5 ImmutableList (com.google.common.collect.ImmutableList)3 BlockBuilder (io.prestosql.spi.block.BlockBuilder)3 ArrayType (io.prestosql.spi.type.ArrayType)2 CharType (io.prestosql.spi.type.CharType)2 CharType.createCharType (io.prestosql.spi.type.CharType.createCharType)2 DecimalType (io.prestosql.spi.type.DecimalType)2 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)2 LikePatternType (io.prestosql.spi.type.LikePatternType)2 MapType (io.prestosql.spi.type.MapType)2 Type (io.prestosql.spi.type.Type)2 UnknownType (io.prestosql.spi.type.UnknownType)2 VarcharType (io.prestosql.spi.type.VarcharType)2 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)2 VarcharType.createVarcharType (io.prestosql.spi.type.VarcharType.createVarcharType)2 SetDigestType (io.prestosql.type.setdigest.SetDigestType)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 START_ARRAY (com.fasterxml.jackson.core.JsonToken.START_ARRAY)1