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);
}
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)));
}
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));
}
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);
}
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;
}
Aggregations