Search in sources :

Example 6 with BoundVariables

use of com.facebook.presto.metadata.BoundVariables 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

BoundVariables (com.facebook.presto.metadata.BoundVariables)6 MethodHandle (java.lang.invoke.MethodHandle)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)4 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)4 PrestoException (com.facebook.presto.spi.PrestoException)4 Reflection.methodHandle (com.facebook.presto.util.Reflection.methodHandle)4 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)3 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)3 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)3 FunctionRegistry (com.facebook.presto.metadata.FunctionRegistry)3 Signature (com.facebook.presto.metadata.Signature)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 FINAL (com.facebook.presto.bytecode.Access.FINAL)2 PRIVATE (com.facebook.presto.bytecode.Access.PRIVATE)2 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)2 STATIC (com.facebook.presto.bytecode.Access.STATIC)2 Access.a (com.facebook.presto.bytecode.Access.a)2 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)2