Search in sources :

Example 1 with BuiltInScalarFunctionImplementation

use of io.prestosql.spi.function.BuiltInScalarFunctionImplementation in project hetu-core by openlookeng.

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(io.prestosql.spi.type.ArrayType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with BuiltInScalarFunctionImplementation

use of io.prestosql.spi.function.BuiltInScalarFunctionImplementation in project hetu-core by openlookeng.

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(io.prestosql.spi.type.MapType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BlockBuilderAppender.createBlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) BlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender) MapType(io.prestosql.spi.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with BuiltInScalarFunctionImplementation

use of io.prestosql.spi.function.BuiltInScalarFunctionImplementation 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 4 with BuiltInScalarFunctionImplementation

use of io.prestosql.spi.function.BuiltInScalarFunctionImplementation in project hetu-core by openlookeng.

the class ElementToArrayConcatFunction method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    Type type = boundVariables.getTypeVariable("E");
    MethodHandle methodHandle;
    if (type.getJavaType() == boolean.class) {
        methodHandle = METHOD_HANDLE_BOOLEAN;
    } else if (type.getJavaType() == long.class) {
        methodHandle = METHOD_HANDLE_LONG;
    } else if (type.getJavaType() == double.class) {
        methodHandle = METHOD_HANDLE_DOUBLE;
    } else if (type.getJavaType() == Slice.class) {
        methodHandle = METHOD_HANDLE_SLICE;
    } else {
        methodHandle = METHOD_HANDLE_OBJECT;
    }
    methodHandle = methodHandle.bindTo(type);
    return new BuiltInScalarFunctionImplementation(false, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL), valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
Also used : Type(io.prestosql.spi.type.Type) Slice(io.airlift.slice.Slice) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle)

Example 5 with BuiltInScalarFunctionImplementation

use of io.prestosql.spi.function.BuiltInScalarFunctionImplementation in project hetu-core by openlookeng.

the class ArrayToElementConcatFunction method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    Type type = boundVariables.getTypeVariable("E");
    MethodHandle methodHandle;
    if (type.getJavaType() == boolean.class) {
        methodHandle = METHOD_HANDLE_BOOLEAN;
    } else if (type.getJavaType() == long.class) {
        methodHandle = METHOD_HANDLE_LONG;
    } else if (type.getJavaType() == double.class) {
        methodHandle = METHOD_HANDLE_DOUBLE;
    } else if (type.getJavaType() == Slice.class) {
        methodHandle = METHOD_HANDLE_SLICE;
    } else {
        methodHandle = METHOD_HANDLE_OBJECT;
    }
    methodHandle = methodHandle.bindTo(type);
    return new BuiltInScalarFunctionImplementation(false, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL), valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
Also used : Type(io.prestosql.spi.type.Type) Slice(io.airlift.slice.Slice) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

BuiltInScalarFunctionImplementation (io.prestosql.spi.function.BuiltInScalarFunctionImplementation)61 Type (io.prestosql.spi.type.Type)42 MethodHandle (java.lang.invoke.MethodHandle)34 Slice (io.airlift.slice.Slice)14 ImmutableList (com.google.common.collect.ImmutableList)13 Signature (io.prestosql.spi.function.Signature)13 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)13 Block (io.prestosql.spi.block.Block)12 OperatorType (io.prestosql.spi.function.OperatorType)12 ArgumentProperty.valueTypeArgumentProperty (io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty)11 Test (org.testng.annotations.Test)10 Optional (java.util.Optional)9 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)8 MapType (io.prestosql.spi.type.MapType)8 StandardTypes (io.prestosql.spi.type.StandardTypes)8 FunctionHandle (io.prestosql.spi.function.FunctionHandle)7 SCALAR (io.prestosql.spi.function.FunctionKind.SCALAR)7 SqlTypeBytecodeExpression.constantType (io.prestosql.sql.gen.SqlTypeBytecodeExpression.constantType)7 Math.toIntExact (java.lang.Math.toIntExact)7 ImmutableMap (com.google.common.collect.ImmutableMap)6