Search in sources :

Example 71 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestRowExpressionSerde method testHllLiteral.

@Test
public void testHllLiteral() {
    RowExpression rowExpression = getRoundTrip("empty_approx_set()", true);
    assertTrue(rowExpression instanceof ConstantExpression);
    Object value = ((ConstantExpression) rowExpression).getValue();
    assertEquals(HyperLogLog.newInstance((Slice) value).cardinality(), 0);
}
Also used : ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 72 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class SpatialJoinUtils method getFlippedFunctionHandle.

public static FunctionHandle getFlippedFunctionHandle(CallExpression callExpression, FunctionAndTypeManager functionAndTypeManager) {
    FunctionMetadata callExpressionMetadata = functionAndTypeManager.getFunctionMetadata(callExpression.getFunctionHandle());
    checkArgument(callExpressionMetadata.getOperatorType().isPresent());
    OperatorType operatorType = flip(callExpressionMetadata.getOperatorType().get());
    List<TypeSignatureProvider> typeProviderList = fromTypes(callExpression.getArguments().stream().map(RowExpression::getType).collect(toImmutableList()));
    checkArgument(typeProviderList.size() == 2, "Expected there to be only two arguments in type provider");
    return functionAndTypeManager.resolveOperator(operatorType, ImmutableList.of(typeProviderList.get(1), typeProviderList.get(0)));
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) RowExpression(com.facebook.presto.spi.relation.RowExpression) OperatorType(com.facebook.presto.common.function.OperatorType)

Example 73 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class WindowFunctionMatcher method getAssignedVariable.

@Override
public Optional<VariableReferenceExpression> getAssignedVariable(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    Optional<VariableReferenceExpression> result = Optional.empty();
    if (!(node instanceof WindowNode)) {
        return result;
    }
    WindowNode windowNode = (WindowNode) node;
    FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases);
    Optional<WindowNode.Frame> expectedFrame = frameMaker.map(maker -> maker.getExpectedValue(symbolAliases));
    List<VariableReferenceExpression> matchedOutputs = windowNode.getWindowFunctions().entrySet().stream().filter(assignment -> {
        if (!expectedCall.getName().equals(QualifiedName.of(metadata.getFunctionAndTypeManager().getFunctionMetadata(assignment.getValue().getFunctionCall().getFunctionHandle()).getName().getObjectName()))) {
            return false;
        }
        if (!functionHandle.map(assignment.getValue().getFunctionHandle()::equals).orElse(true)) {
            return false;
        }
        if (!expectedFrame.map(assignment.getValue().getFrame()::equals).orElse(true)) {
            return false;
        }
        List<Expression> expectedExpressions = expectedCall.getArguments();
        List<RowExpression> actualExpressions = assignment.getValue().getFunctionCall().getArguments();
        if (expectedExpressions.size() != actualExpressions.size()) {
            return false;
        }
        for (int i = 0; i < expectedExpressions.size(); i++) {
            Expression expectedExpression = expectedExpressions.get(i);
            RowExpression actualExpression = actualExpressions.get(i);
            if (!isExpression(actualExpression)) {
                SymbolAliases.Builder builder = SymbolAliases.builder();
                ImmutableSet.copyOf(VariablesExtractor.extractAllSymbols(expectedExpression)).forEach(symbol -> builder.put(symbol.getName(), symbol.toSymbolReference()));
                if (!new RowExpressionVerifier(builder.build(), metadata, session).process(expectedExpression, actualExpression)) {
                    return false;
                }
            } else {
                if (!expectedExpression.equals(castToExpression(actualExpression))) {
                    return false;
                }
            }
        }
        return true;
    }).map(Map.Entry::getKey).collect(toImmutableList());
    checkState(matchedOutputs.size() <= 1, "Ambiguous function calls in %s", windowNode);
    if (matchedOutputs.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(matchedOutputs.get(0));
}
Also used : WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) Expression(com.facebook.presto.sql.tree.Expression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Map(java.util.Map)

Example 74 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class RowExpressionVerifier method visitSimpleCaseExpression.

@Override
protected Boolean visitSimpleCaseExpression(SimpleCaseExpression expected, RowExpression actual) {
    if (!(actual instanceof SpecialFormExpression && ((SpecialFormExpression) actual).getForm().equals(SWITCH))) {
        return false;
    }
    SpecialFormExpression actualCase = (SpecialFormExpression) actual;
    if (!process(expected.getOperand(), actualCase.getArguments().get(0))) {
        return false;
    }
    List<RowExpression> whenClauses;
    Optional<RowExpression> elseValue;
    RowExpression last = actualCase.getArguments().get(actualCase.getArguments().size() - 1);
    if (last instanceof SpecialFormExpression && ((SpecialFormExpression) last).getForm().equals(WHEN)) {
        whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size());
        elseValue = Optional.empty();
    } else {
        whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size() - 1);
        elseValue = Optional.of(last);
    }
    if (!process(expected.getWhenClauses(), whenClauses)) {
        return false;
    }
    return process(expected.getDefaultValue(), elseValue);
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression)

Example 75 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class RowExpressionDomainTranslator method extractDisjuncts.

private List<RowExpression> extractDisjuncts(Type type, Ranges ranges, RowExpression reference) {
    List<RowExpression> disjuncts = new ArrayList<>();
    List<RowExpression> singleValues = new ArrayList<>();
    List<Range> orderedRanges = ranges.getOrderedRanges();
    SortedRangeSet sortedRangeSet = SortedRangeSet.copyOf(type, orderedRanges);
    SortedRangeSet complement = sortedRangeSet.complement();
    List<Range> singleValueExclusionsList = complement.getOrderedRanges().stream().filter(Range::isSingleValue).collect(toList());
    List<Range> originalUnionSingleValues = SortedRangeSet.copyOf(type, singleValueExclusionsList).union(sortedRangeSet).getOrderedRanges();
    PeekingIterator<Range> singleValueExclusions = peekingIterator(singleValueExclusionsList.iterator());
    for (Range range : originalUnionSingleValues) {
        if (range.isSingleValue()) {
            singleValues.add(toRowExpression(reference.getSourceLocation(), range.getSingleValue(), type));
            continue;
        }
        // attempt to optimize ranges that can be coalesced as long as single value points are excluded
        List<RowExpression> singleValuesInRange = new ArrayList<>();
        while (singleValueExclusions.hasNext() && range.contains(singleValueExclusions.peek())) {
            singleValuesInRange.add(toRowExpression(reference.getSourceLocation(), singleValueExclusions.next().getSingleValue(), type));
        }
        if (!singleValuesInRange.isEmpty()) {
            disjuncts.add(combineRangeWithExcludedPoints(type, reference, range, singleValuesInRange));
            continue;
        }
        disjuncts.add(processRange(type, range, reference));
    }
    // Add back all of the possible single values either as an equality or an IN predicate
    if (singleValues.size() == 1) {
        disjuncts.add(equal(reference, getOnlyElement(singleValues)));
    } else if (singleValues.size() > 1) {
        disjuncts.add(in(reference, singleValues));
    }
    return disjuncts;
}
Also used : SortedRangeSet(com.facebook.presto.common.predicate.SortedRangeSet) ArrayList(java.util.ArrayList) LiteralEncoder.toRowExpression(com.facebook.presto.sql.planner.LiteralEncoder.toRowExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Range(com.facebook.presto.common.predicate.Range)

Aggregations

RowExpression (com.facebook.presto.spi.relation.RowExpression)237 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)97 Test (org.testng.annotations.Test)87 ImmutableList (com.google.common.collect.ImmutableList)58 CallExpression (com.facebook.presto.spi.relation.CallExpression)52 Map (java.util.Map)49 List (java.util.List)42 Type (com.facebook.presto.common.type.Type)41 PlanNode (com.facebook.presto.spi.plan.PlanNode)41 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)40 ImmutableMap (com.google.common.collect.ImmutableMap)38 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)37 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)35 Optional (java.util.Optional)35 Expression (com.facebook.presto.sql.tree.Expression)31 ColumnHandle (com.facebook.presto.spi.ColumnHandle)27 Objects.requireNonNull (java.util.Objects.requireNonNull)27 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)24 Set (java.util.Set)24 ArrayList (java.util.ArrayList)23