Search in sources :

Example 1 with FunctionHandle

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

the class MySqlRowExpressionConverter method visitCall.

@Override
public String visitCall(CallExpression call, JdbcConverterContext context) {
    // remote udf verify
    FunctionHandle functionHandle = call.getFunctionHandle();
    if (!isDefaultFunction(call)) {
        Optional<String> result = mySqlApplyRemoteFunctionPushDown.rewriteRemoteFunction(call, this, context);
        if (result.isPresent()) {
            return result.get();
        }
        throw new PrestoException(NOT_SUPPORTED, String.format("MySql connector does not support remote function: %s.%s", call.getDisplayName(), call.getFunctionHandle().getFunctionNamespace()));
    }
    if (standardFunctionResolution.isArrayConstructor(functionHandle)) {
        throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support array constructor");
    }
    if (standardFunctionResolution.isSubscriptFunction(functionHandle)) {
        throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support subscript expression");
    }
    if (standardFunctionResolution.isCastFunction(functionHandle)) {
        // deal with literal, when generic literal expression translate to rowExpression, it will be
        // translated to a 'CAST' rowExpression with a varchar type 'CONSTANT' rowExpression, in some
        // case, 'CAST' is superfluous
        RowExpression argument = call.getArguments().get(0);
        Type type = call.getType();
        if (argument instanceof ConstantExpression && argument.getType().equals(VARCHAR)) {
            String value = argument.accept(this, context);
            if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType || type instanceof DecimalType || type instanceof RealType || type instanceof DoubleType) {
                return value;
            }
        }
        if (call.getType().getDisplayName().equals(LIKE_PATTERN_NAME)) {
            return call.getArguments().get(0).accept(this, context);
        }
        return getCastExpression(call.getArguments().get(0).accept(this, context), call.getType());
    }
    return super.visitCall(call, context);
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) DoubleType(io.prestosql.spi.type.DoubleType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) VarcharType(io.prestosql.spi.type.VarcharType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) DoubleType(io.prestosql.spi.type.DoubleType) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Example 2 with FunctionHandle

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

the class TestTypeValidator method testValidWindow.

@Test
public void testValidWindow() {
    Symbol windowSymbol = planSymbolAllocator.newSymbol("sum", DOUBLE);
    FunctionHandle functionHandle = FUNCTION_MANAGER.lookupFunction("sum", fromTypes(DOUBLE));
    WindowNode.Frame frame = new WindowNode.Frame(WindowFrameType.RANGE, FrameBoundType.UNBOUNDED_PRECEDING, Optional.empty(), FrameBoundType.UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty());
    WindowNode.Function function = new WindowNode.Function(call("sum", functionHandle, DOUBLE, ImmutableList.of(VariableReferenceSymbolConverter.toVariableReference(columnC, DOUBLE))), ImmutableList.of(VariableReferenceSymbolConverter.toVariableReference(columnC, DOUBLE)), frame);
    WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(), Optional.empty());
    PlanNode node = new WindowNode(newId(), baseTableScan, specification, ImmutableMap.of(windowSymbol, function), Optional.empty(), ImmutableSet.of(), 0);
    assertTypesValid(node);
}
Also used : WindowNode(io.prestosql.spi.plan.WindowNode) PlanNode(io.prestosql.spi.plan.PlanNode) Symbol(io.prestosql.spi.plan.Symbol) FunctionHandle(io.prestosql.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Example 3 with FunctionHandle

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

the class TestExpressionOptimizer method testIfConstantOptimization.

@Test
public void testIfConstantOptimization() {
    assertEquals(optimizer.optimize(ifExpression(constant(true, BOOLEAN), 1L, 2L)), constant(1L, BIGINT));
    assertEquals(optimizer.optimize(ifExpression(constant(false, BOOLEAN), 1L, 2L)), constant(2L, BIGINT));
    assertEquals(optimizer.optimize(ifExpression(constant(null, BOOLEAN), 1L, 2L)), constant(2L, BIGINT));
    FunctionHandle bigintEquals = functionAndTypeManager.resolveOperatorFunctionHandle(EQUAL, fromTypes(BIGINT, BIGINT));
    RowExpression condition = new CallExpression(EQUAL.name(), bigintEquals, BOOLEAN, ImmutableList.of(constant(3L, BIGINT), constant(3L, BIGINT)), Optional.empty());
    assertEquals(optimizer.optimize(ifExpression(condition, 1L, 2L)), constant(1L, BIGINT));
}
Also used : RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 4 with FunctionHandle

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

the class TestExpressionOptimizer method testCastWithJsonParseOptimization.

@Test
public void testCastWithJsonParseOptimization() {
    FunctionHandle jsonParseFunctionHandle = functionAndTypeManager.lookupFunction("json_parse", fromTypes(VARCHAR));
    // constant
    FunctionHandle jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(integer)"));
    RowExpression jsonCastExpression = new CallExpression(CAST.name(), jsonCastFunctionHandle, new ArrayType(INTEGER), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, constant(utf8Slice("[1, 2]"), VARCHAR))), Optional.empty());
    RowExpression resultExpression = optimizer.optimize(jsonCastExpression);
    assertInstanceOf(resultExpression, ConstantExpression.class);
    Object resultValue = ((ConstantExpression) resultExpression).getValue();
    assertInstanceOf(resultValue, IntArrayBlock.class);
    assertEquals(toValues(INTEGER, (IntArrayBlock) resultValue), ImmutableList.of(1, 2));
    // varchar to array
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(varchar)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, new ArrayType(VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimizer.optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_ARRAY_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ARRAY_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("array(varchar)")), new ArrayType(VARCHAR), field(1, VARCHAR)));
    // varchar to row
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("row(varchar,bigint)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimizer.optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_ROW_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ROW_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("row(varchar,bigint)")), RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), field(1, VARCHAR)));
    // varchar to map
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("map(integer,varchar)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, mapType(INTEGER, VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimizer.optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_MAP_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_MAP_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("map(integer, varchar)")), mapType(INTEGER, VARCHAR), field(1, VARCHAR)));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) IntArrayBlock(io.prestosql.spi.block.IntArrayBlock) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 5 with FunctionHandle

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

the class FormatFunction method valueConverter.

private static BiFunction<ConnectorSession, Block, Object> valueConverter(FunctionAndTypeManager functionAndTypeManager, Type type, int position) {
    if (type.equals(UNKNOWN)) {
        return (session, block) -> null;
    }
    if (type.equals(BOOLEAN)) {
        return (session, block) -> type.getBoolean(block, position);
    }
    if (type.equals(TINYINT) || type.equals(SMALLINT) || type.equals(INTEGER) || type.equals(BIGINT)) {
        return (session, block) -> type.getLong(block, position);
    }
    if (type.equals(REAL)) {
        return (session, block) -> intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type.equals(DOUBLE)) {
        return (session, block) -> type.getDouble(block, position);
    }
    if (type.equals(DATE)) {
        return (session, block) -> LocalDate.ofEpochDay(type.getLong(block, position));
    }
    if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
        return (session, block) -> toZonedDateTime(type.getLong(block, position));
    }
    if (type.equals(TIMESTAMP)) {
        return (session, block) -> toLocalDateTime(type.getLong(block, position));
    }
    if (type.equals(TIME)) {
        return (session, block) -> toLocalTime(session, type.getLong(block, position));
    }
    // TODO: support TIME WITH TIME ZONE by making SqlTimeWithTimeZone implement TemporalAccessor
    if (type.equals(JSON)) {
        FunctionHandle functionHandle = functionAndTypeManager.resolveFunction(Optional.empty(), QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "json_format"), fromTypes(JSON));
        MethodHandle handle = functionAndTypeManager.getBuiltInScalarFunctionImplementation(functionHandle).getMethodHandle();
        return (session, block) -> convertToString(handle, type.getSlice(block, position));
    }
    if (isShortDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale);
    }
    if (isLongDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> new BigDecimal(decodeUnscaledValue(type.getSlice(block, position)), scale);
    }
    if (isVarcharType(type) || isCharType(type)) {
        return (session, block) -> type.getSlice(block, position).toStringUtf8();
    }
    BiFunction<ConnectorSession, Block, Object> function;
    if (type.getJavaType() == long.class) {
        function = (session, block) -> type.getLong(block, position);
    } else if (type.getJavaType() == double.class) {
        function = (session, block) -> type.getDouble(block, position);
    } else if (type.getJavaType() == boolean.class) {
        function = (session, block) -> type.getBoolean(block, position);
    } else if (type.getJavaType() == Slice.class) {
        function = (session, block) -> type.getSlice(block, position);
    } else {
        function = (session, block) -> type.getObject(block, position);
    }
    MethodHandle handle = castToVarchar(functionAndTypeManager, type);
    if ((handle == null) || (handle.type().parameterCount() != 1)) {
        throw new PrestoException(NOT_SUPPORTED, "Type not supported for formatting: " + type.getDisplayName());
    }
    return (session, block) -> convertToString(handle, function.apply(session, block));
}
Also used : DateTimeEncoding.unpackZoneKey(io.prestosql.spi.type.DateTimeEncoding.unpackZoneKey) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) OperatorNotFoundException(io.prestosql.metadata.OperatorNotFoundException) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) DecimalType(io.prestosql.spi.type.DecimalType) BoundVariables(io.prestosql.metadata.BoundVariables) DEFAULT_NAMESPACE(io.prestosql.spi.connector.CatalogSchemaName.DEFAULT_NAMESPACE) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) BigDecimal(java.math.BigDecimal) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) LocalTime(java.time.LocalTime) IllegalFormatException(java.util.IllegalFormatException) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) JSON(io.prestosql.type.JsonType.JSON) Type(io.prestosql.spi.type.Type) ZoneOffset(java.time.ZoneOffset) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) PrestoException(io.prestosql.spi.PrestoException) DateTimeEncoding.unpackMillisUtc(io.prestosql.spi.type.DateTimeEncoding.unpackMillisUtc) CastType(io.prestosql.metadata.CastType) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TIME(io.prestosql.spi.type.TimeType.TIME) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) Decimals.isLongDecimal(io.prestosql.spi.type.Decimals.isLongDecimal) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) TINYINT(io.prestosql.spi.type.TinyintType.TINYINT) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) FunctionHandle(io.prestosql.spi.function.FunctionHandle) Decimals.isShortDecimal(io.prestosql.spi.type.Decimals.isShortDecimal) List(java.util.List) LocalDate(java.time.LocalDate) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) Optional(java.util.Optional) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) TIMESTAMP_WITH_TIME_ZONE(io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) Signature.withVariadicBound(io.prestosql.spi.function.Signature.withVariadicBound) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) UNKNOWN(io.prestosql.spi.type.UnknownType.UNKNOWN) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) LocalDateTime(java.time.LocalDateTime) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) Failures.internalError(io.prestosql.util.Failures.internalError) ImmutableList(com.google.common.collect.ImmutableList) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) DATE(io.prestosql.spi.type.DateType.DATE) REAL(io.prestosql.spi.type.RealType.REAL) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.prestosql.spi.function.Signature) Block(io.prestosql.spi.block.Block) Streams.mapWithIndex(com.google.common.collect.Streams.mapWithIndex) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) Decimals.decodeUnscaledValue(io.prestosql.spi.type.Decimals.decodeUnscaledValue) SMALLINT(io.prestosql.spi.type.SmallintType.SMALLINT) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) Block(io.prestosql.spi.block.Block) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) PrestoException(io.prestosql.spi.PrestoException) FunctionHandle(io.prestosql.spi.function.FunctionHandle) BigDecimal(java.math.BigDecimal) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

FunctionHandle (io.prestosql.spi.function.FunctionHandle)44 RowExpression (io.prestosql.spi.relation.RowExpression)19 Type (io.prestosql.spi.type.Type)17 CallExpression (io.prestosql.spi.relation.CallExpression)16 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)13 Symbol (io.prestosql.spi.plan.Symbol)12 Test (org.testng.annotations.Test)11 ImmutableList (com.google.common.collect.ImmutableList)10 PrestoException (io.prestosql.spi.PrestoException)10 BuiltInScalarFunctionImplementation (io.prestosql.spi.function.BuiltInScalarFunctionImplementation)10 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)10 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)9 PlanNode (io.prestosql.spi.plan.PlanNode)8 Map (java.util.Map)8 List (java.util.List)7 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)6 Variable (io.airlift.bytecode.Variable)6 IfStatement (io.airlift.bytecode.control.IfStatement)6 OperatorType (io.prestosql.spi.function.OperatorType)6 Optional (java.util.Optional)6