Search in sources :

Example 1 with INVALID_CAST_ARGUMENT

use of com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT 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

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 OperatorType (com.facebook.presto.common.function.OperatorType)1 SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)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 RETURN_NULL_ON_NULL (com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.RETURN_NULL_ON_NULL)1 PrestoException (com.facebook.presto.spi.PrestoException)1 INVALID_CAST_ARGUMENT (com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT)1 Signature.withVariadicBound (com.facebook.presto.spi.function.Signature.withVariadicBound)1 Failures.checkCondition (com.facebook.presto.util.Failures.checkCondition)1 JsonCastException (com.facebook.presto.util.JsonCastException)1