Search in sources :

Example 1 with BlockBuilderAppender

use of com.facebook.presto.util.JsonUtil.BlockBuilderAppender in project presto by prestodb.

the class JsonToMapCast method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    checkArgument(arity == 1, "Expected arity to be 1");
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MapType mapType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    checkCondition(canCastFromJson(mapType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", mapType);
    BlockBuilderAppender keyAppender = createBlockBuilderAppender(mapType.getKeyType());
    BlockBuilderAppender valueAppender = createBlockBuilderAppender(mapType.getValueType());
    MethodHandle methodHandle = METHOD_HANDLE.bindTo(mapType).bindTo(keyAppender).bindTo(valueAppender);
    return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) BlockBuilderAppender.createBlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) BlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender) MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with BlockBuilderAppender

use of com.facebook.presto.util.JsonUtil.BlockBuilderAppender in project presto by prestodb.

the class JsonToArrayCast method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    checkArgument(arity == 1, "Expected arity to be 1");
    Type type = boundVariables.getTypeVariable("T");
    ArrayType arrayType = (ArrayType) functionAndTypeManager.getParameterizedType(StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.of(type.getTypeSignature())));
    checkCondition(canCastFromJson(arrayType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", arrayType);
    BlockBuilderAppender elementAppender = BlockBuilderAppender.createBlockBuilderAppender(arrayType.getElementType());
    MethodHandle methodHandle = METHOD_HANDLE.bindTo(arrayType).bindTo(elementAppender);
    return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) BlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with BlockBuilderAppender

use of com.facebook.presto.util.JsonUtil.BlockBuilderAppender in project presto by prestodb.

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 : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) MethodHandle(java.lang.invoke.MethodHandle) StandardTypes(com.facebook.presto.common.type.StandardTypes) Slice(io.airlift.slice.Slice) Field(com.facebook.presto.common.type.RowType.Field) BlockBuilderAppender.createBlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) SqlOperator(com.facebook.presto.metadata.SqlOperator) SingleRowBlockWriter(com.facebook.presto.common.block.SingleRowBlockWriter) PrestoException(com.facebook.presto.spi.PrestoException) JsonUtil.canCastFromJson(com.facebook.presto.util.JsonUtil.canCastFromJson) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Reflection.methodHandle(com.facebook.presto.util.Reflection.methodHandle) START_OBJECT(com.fasterxml.jackson.core.JsonToken.START_OBJECT) Map(java.util.Map) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonToken(com.fasterxml.jackson.core.JsonToken) BlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender) RETURN_NULL_ON_NULL(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.RETURN_NULL_ON_NULL) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) JsonUtil.parseJsonToSingleRowBlock(com.facebook.presto.util.JsonUtil.parseJsonToSingleRowBlock) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) BoundVariables(com.facebook.presto.metadata.BoundVariables) JsonParser(com.fasterxml.jackson.core.JsonParser) JsonCastException(com.facebook.presto.util.JsonCastException) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) INVALID_CAST_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT) JsonUtil.truncateIfNecessaryForErrorMessage(com.facebook.presto.util.JsonUtil.truncateIfNecessaryForErrorMessage) OperatorType(com.facebook.presto.common.function.OperatorType) JsonUtil.getFieldNameToIndex(com.facebook.presto.util.JsonUtil.getFieldNameToIndex) String.format(java.lang.String.format) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) List(java.util.List) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) START_ARRAY(com.fasterxml.jackson.core.JsonToken.START_ARRAY) Optional(java.util.Optional) Signature.withVariadicBound(com.facebook.presto.spi.function.Signature.withVariadicBound) JSON_FACTORY(com.facebook.presto.util.JsonUtil.JSON_FACTORY) Block(com.facebook.presto.common.block.Block) RowType(com.facebook.presto.common.type.RowType) Field(com.facebook.presto.common.type.RowType.Field) BlockBuilderAppender.createBlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) BlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender) RowType(com.facebook.presto.common.type.RowType) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

OperatorType (com.facebook.presto.common.function.OperatorType)3 BlockBuilderAppender (com.facebook.presto.util.JsonUtil.BlockBuilderAppender)3 MethodHandle (java.lang.invoke.MethodHandle)3 Type (com.facebook.presto.common.type.Type)2 BlockBuilderAppender.createBlockBuilderAppender (com.facebook.presto.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 Block (com.facebook.presto.common.block.Block)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 SingleRowBlockWriter (com.facebook.presto.common.block.SingleRowBlockWriter)1 SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 MapType (com.facebook.presto.common.type.MapType)1 RowType (com.facebook.presto.common.type.RowType)1 Field (com.facebook.presto.common.type.RowType.Field)1 StandardTypes (com.facebook.presto.common.type.StandardTypes)1 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)1 BoundVariables (com.facebook.presto.metadata.BoundVariables)1 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)1 SqlOperator (com.facebook.presto.metadata.SqlOperator)1 ArgumentProperty.valueTypeArgumentProperty (com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty)1