Search in sources :

Example 26 with FunctionHandle

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

the class TestDynamicFilters method testExtractDynamicFiltersAsUnion.

@Test
public void testExtractDynamicFiltersAsUnion() {
    LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), new FunctionResolution(metadata.getFunctionAndTypeManager()), metadata.getFunctionAndTypeManager());
    ConstantExpression staticExpression1 = new ConstantExpression(utf8Slice("age"), VarcharType.VARCHAR);
    VariableReferenceExpression variableExpression1 = new VariableReferenceExpression("col1", VarcharType.VARCHAR);
    ConstantExpression staticExpression2 = new ConstantExpression(utf8Slice("id"), VarcharType.VARCHAR);
    VariableReferenceExpression variableExpression2 = new VariableReferenceExpression("col2", VarcharType.VARCHAR);
    FunctionHandle functionHandle = metadata.getFunctionAndTypeManager().lookupFunction("rank", ImmutableList.of());
    RowExpression dynamicFilterExpression1 = call(DynamicFilters.Function.NAME, functionHandle, staticExpression1.getType(), asList(staticExpression1, variableExpression1), Optional.empty());
    RowExpression dynamicFilterExpression2 = call(DynamicFilters.Function.NAME, functionHandle, staticExpression2.getType(), asList(staticExpression2, variableExpression2), Optional.empty());
    List<DynamicFilters.Descriptor> dynamicFilter1 = new ArrayList<>();
    dynamicFilter1.add(new DynamicFilters.Descriptor("age", variableExpression1, Optional.empty()));
    List<DynamicFilters.Descriptor> dynamicFilter2 = new ArrayList<>();
    dynamicFilter2.add(new DynamicFilters.Descriptor("id", variableExpression2, Optional.empty()));
    List<List<DynamicFilters.Descriptor>> dynamicFilterUnion = new ArrayList<>();
    dynamicFilterUnion.add(dynamicFilter1);
    dynamicFilterUnion.add(dynamicFilter2);
    RowExpression combineDynamicFilterExpression = logicalRowExpressions.combineDisjuncts(asList(dynamicFilterExpression1, dynamicFilterExpression2));
    RowExpression combinedExpression = logicalRowExpressions.combineConjuncts(staticExpression1, combineDynamicFilterExpression);
    List<List<DynamicFilters.Descriptor>> extractedExpression = DynamicFilters.extractDynamicFiltersAsUnion(Optional.of(combinedExpression), metadata);
    assertEquals(extractedExpression, dynamicFilterUnion);
}
Also used : RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) LogicalRowExpressions(io.prestosql.expressions.LogicalRowExpressions) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Arrays.asList(java.util.Arrays.asList) FunctionHandle(io.prestosql.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Example 27 with FunctionHandle

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

the class BaseJdbcRowExpressionConverter method visitCall.

@Override
public String visitCall(CallExpression call, JdbcConverterContext context) {
    // remote udf verify
    FunctionHandle functionHandle = call.getFunctionHandle();
    if (!isDefaultFunction(call)) {
        throw new PrestoException(NOT_SUPPORTED, String.format("This connector do not support push down %s.%s", functionHandle.getFunctionNamespace(), call.getDisplayName()));
    }
    context.setDefaultFunctionVisited(true);
    if (!isDeterministic(rowExpressionService.getDeterminismEvaluator(), call)) {
        throw new PrestoException(NOT_SUPPORTED, "Unsupported not deterministic function push down.");
    }
    if (standardFunctionResolution.isNotFunction(functionHandle)) {
        return format("(NOT %s)", call.getArguments().get(0).accept(this, context));
    }
    if (standardFunctionResolution.isTryFunction(functionHandle)) {
        return format("TRY(%s)", call.getArguments().get(0).accept(this, context));
    }
    if (standardFunctionResolution.isLikeFunction(functionHandle)) {
        return format("(%s LIKE %s)", call.getArguments().get(0).accept(this, context), call.getArguments().get(1).accept(this, context));
    }
    if (standardFunctionResolution.isArrayConstructor(functionHandle)) {
        String arguments = Joiner.on(",").join(call.getArguments().stream().map(expression -> expression.accept(this, context)).collect(toList()));
        return format("ARRAY[%s]", arguments);
    }
    FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(functionHandle);
    if (standardFunctionResolution.isOperator(functionHandle)) {
        return handleOperator(call, functionMetadata, context);
    }
    if (functionMetadata.getName().getObjectName().equals(TIMESTAMP_LITERAL)) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        long time = (long) ((ConstantExpression) call.getArguments().get(0)).getValue();
        return format("TIMESTAMP '%s'", formatter.format(Instant.ofEpochMilli(time).atZone(UTC).toLocalDateTime()));
    }
    return handleFunction(call, functionMetadata, context);
}
Also used : FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) PrestoException(io.prestosql.spi.PrestoException) FunctionHandle(io.prestosql.spi.function.FunctionHandle) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 28 with FunctionHandle

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

the class BaseJdbcRowExpressionConverter method handleOperator.

private String handleOperator(CallExpression call, FunctionMetadata functionMetadata, JdbcConverterContext context) {
    FunctionHandle functionHandle = call.getFunctionHandle();
    List<RowExpression> arguments = call.getArguments();
    if (standardFunctionResolution.isCastFunction(functionHandle)) {
        if (call.getType().getDisplayName().equals(LIKE_PATTERN_NAME)) {
            return arguments.get(0).accept(this, context);
        }
        return format("CAST(%s AS %s)", arguments.get(0).accept(this, context), call.getType().getDisplayName());
    }
    if (call.getArguments().size() == 1 && standardFunctionResolution.isNegateFunction(functionHandle)) {
        String value = call.getArguments().get(0).accept(this, context);
        String separator = value.startsWith("-") ? " " : "";
        return format("-%s%s", separator, value);
    }
    Optional<OperatorType> operatorTypeOptional = functionMetadata.getOperatorType();
    if (operatorTypeOptional.isPresent() && arguments.size() == 2 && (standardFunctionResolution.isComparisonFunction(functionHandle) || standardFunctionResolution.isArithmeticFunction(functionHandle))) {
        return format("(%s %s %s)", arguments.get(0).accept(this, context), operatorTypeOptional.get().getOperator(), arguments.get(1).accept(this, context));
    }
    if (standardFunctionResolution.isSubscriptFunction(functionHandle)) {
        String base = call.getArguments().get(0).accept(this, context);
        String index = call.getArguments().get(1).accept(this, context);
        return format("%s[%s]", base, index);
    }
    throw new PrestoException(NOT_SUPPORTED, String.format("Unknown operator %s in push down", operatorTypeOptional));
}
Also used : RowExpression(io.prestosql.spi.relation.RowExpression) PrestoException(io.prestosql.spi.PrestoException) FunctionHandle(io.prestosql.spi.function.FunctionHandle) OperatorType(io.prestosql.spi.function.OperatorType)

Example 29 with FunctionHandle

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

the class SplitFiltering method isSupportedExpression.

private static boolean isSupportedExpression(RowExpression predicate) {
    if (predicate instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) predicate;
        switch(specialForm.getForm()) {
            case BETWEEN:
            case IN:
                return true;
            case AND:
            case OR:
                return isSupportedExpression(specialForm.getArguments().get(0)) && isSupportedExpression(specialForm.getArguments().get(1));
            default:
                return false;
        }
    }
    if (predicate instanceof CallExpression) {
        CallExpression call = (CallExpression) predicate;
        FunctionHandle builtInFunctionHandle = call.getFunctionHandle();
        if (builtInFunctionHandle instanceof BuiltInFunctionHandle) {
            Signature signature = ((BuiltInFunctionHandle) builtInFunctionHandle).getSignature();
            if (signature.getName().getObjectName().equals("not")) {
                return true;
            }
            try {
                OperatorType operatorType = Signature.unmangleOperator(signature.getName().getObjectName());
                if (operatorType.isComparisonOperator() && operatorType != IS_DISTINCT_FROM) {
                    return true;
                }
                return false;
            } catch (IllegalArgumentException e) {
                return false;
            }
        }
    }
    return false;
}
Also used : Signature(io.prestosql.spi.function.Signature) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) SpecialForm(io.prestosql.spi.relation.SpecialForm) OperatorType(io.prestosql.spi.function.OperatorType)

Example 30 with FunctionHandle

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

the class SplitFiltering method extractExpression.

private static RowExpression extractExpression(RowExpression expression) {
    if (expression instanceof CallExpression) {
        FunctionHandle builtInFunctionHandle = ((CallExpression) expression).getFunctionHandle();
        Signature signature;
        if (builtInFunctionHandle instanceof BuiltInFunctionHandle) {
            signature = ((BuiltInFunctionHandle) builtInFunctionHandle).getSignature();
            if (signature.getName().getObjectName().contains("CAST")) {
                // extract the inner expression for CAST expressions
                return extractExpression(((CallExpression) expression).getArguments().get(0));
            }
        }
    }
    return expression;
}
Also used : Signature(io.prestosql.spi.function.Signature) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Aggregations

FunctionHandle (io.prestosql.spi.function.FunctionHandle)49 RowExpression (io.prestosql.spi.relation.RowExpression)24 CallExpression (io.prestosql.spi.relation.CallExpression)21 Type (io.prestosql.spi.type.Type)21 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)13 Symbol (io.prestosql.spi.plan.Symbol)13 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)13 ImmutableList (com.google.common.collect.ImmutableList)11 Test (org.testng.annotations.Test)11 PrestoException (io.prestosql.spi.PrestoException)10 BuiltInScalarFunctionImplementation (io.prestosql.spi.function.BuiltInScalarFunctionImplementation)10 Signature (io.prestosql.spi.function.Signature)10 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)10 PlanNode (io.prestosql.spi.plan.PlanNode)9 Map (java.util.Map)9 List (java.util.List)8 ArrayList (java.util.ArrayList)7 Optional (java.util.Optional)7 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)6 Variable (io.airlift.bytecode.Variable)6