use of io.trino.sql.tree.InPredicate in project trino by trinodb.
the class UnwrapSingleColumnRowInApply method apply.
@Override
public Result apply(ApplyNode node, Captures captures, Context context) {
Assignments.Builder inputAssignments = Assignments.builder().putIdentities(node.getInput().getOutputSymbols());
Assignments.Builder nestedPlanAssignments = Assignments.builder().putIdentities(node.getSubquery().getOutputSymbols());
boolean applied = false;
Assignments.Builder applyAssignments = Assignments.builder();
for (Map.Entry<Symbol, Expression> assignment : node.getSubqueryAssignments().entrySet()) {
Symbol output = assignment.getKey();
Expression expression = assignment.getValue();
Optional<Unwrapping> unwrapped = Optional.empty();
if (expression instanceof InPredicate) {
InPredicate predicate = (InPredicate) expression;
unwrapped = unwrapSingleColumnRow(context, predicate.getValue(), predicate.getValueList(), (value, list) -> new InPredicate(value.toSymbolReference(), list.toSymbolReference()));
} else if (expression instanceof QuantifiedComparisonExpression) {
QuantifiedComparisonExpression comparison = (QuantifiedComparisonExpression) expression;
unwrapped = unwrapSingleColumnRow(context, comparison.getValue(), comparison.getSubquery(), (value, list) -> new QuantifiedComparisonExpression(comparison.getOperator(), comparison.getQuantifier(), value.toSymbolReference(), list.toSymbolReference()));
}
if (unwrapped.isPresent()) {
applied = true;
Unwrapping unwrapping = unwrapped.get();
inputAssignments.add(unwrapping.getInputAssignment());
nestedPlanAssignments.add(unwrapping.getNestedPlanAssignment());
applyAssignments.put(output, unwrapping.getExpression());
} else {
applyAssignments.put(assignment);
}
}
if (!applied) {
return Result.empty();
}
return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), new ApplyNode(node.getId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getInput(), inputAssignments.build()), new ProjectNode(context.getIdAllocator().getNextId(), node.getSubquery(), nestedPlanAssignments.build()), applyAssignments.build(), node.getCorrelation(), node.getOriginSubquery()), Assignments.identity(node.getOutputSymbols())));
}
use of io.trino.sql.tree.InPredicate in project trino by trinodb.
the class SubqueryPlanner method planInPredicate.
private PlanBuilder planInPredicate(PlanBuilder subPlan, Cluster<InPredicate> cluster, Analysis.SubqueryAnalysis subqueries) {
// Plan one of the predicates from the cluster
InPredicate predicate = cluster.getRepresentative();
Expression value = predicate.getValue();
SubqueryExpression subquery = (SubqueryExpression) predicate.getValueList();
Symbol output = symbolAllocator.newSymbol(predicate, BOOLEAN);
subPlan = handleSubqueries(subPlan, value, subqueries);
subPlan = planInPredicate(subPlan, value, subquery, output, predicate, analysis.getPredicateCoercions(predicate));
return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(mapAll(cluster, subPlan.getScope(), output)), subPlan.getRoot());
}
use of io.trino.sql.tree.InPredicate in project trino by trinodb.
the class ExpressionVerifier method visitInPredicate.
@Override
protected Boolean visitInPredicate(InPredicate actual, Node expectedExpression) {
if (!(expectedExpression instanceof InPredicate)) {
return false;
}
InPredicate expected = (InPredicate) expectedExpression;
if (actual.getValueList() instanceof InListExpression || !(expected.getValueList() instanceof InListExpression)) {
return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), expected.getValueList());
}
/*
* In some cases, actual.getValueList() and expected.getValueList() might be of different types,
* although they originated from identical single-element InListExpression.
*
* This happens because actual passes through the analyzer, planner, and possibly optimizers,
* one of which sometimes takes the liberty of unpacking the InListExpression.
*
* Since the expected value doesn't go through all of that, we have to deal with the case
* of the actual value being unpacked, but the expected value being an InListExpression.
*
* If the expected value is a value list, but the actual is e.g. a SymbolReference,
* we need to unpack the value from the list to enable comparison: so that when we hit
* visitSymbolReference, the expected.toString() call returns something that the symbolAliases
* actually contains.
* For example, InListExpression.toString returns "(onlyitem)" rather than "onlyitem".
*/
List<Expression> values = ((InListExpression) expected.getValueList()).getValues();
checkState(values.size() == 1, "Multiple expressions in expected value list %s, but actual value is not a list", values, actual.getValue());
Expression onlyExpectedExpression = values.get(0);
return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), onlyExpectedExpression);
}
use of io.trino.sql.tree.InPredicate in project trino by trinodb.
the class TestDomainTranslator method testFromUnprocessableInPredicate.
@Test
public void testFromUnprocessableInPredicate() {
assertUnsupportedPredicate(new InPredicate(unprocessableExpression1(C_BIGINT), new InListExpression(ImmutableList.of(TRUE_LITERAL))));
assertUnsupportedPredicate(new InPredicate(C_BOOLEAN.toSymbolReference(), new InListExpression(ImmutableList.of(unprocessableExpression1(C_BOOLEAN)))));
assertUnsupportedPredicate(new InPredicate(C_BOOLEAN.toSymbolReference(), new InListExpression(ImmutableList.of(TRUE_LITERAL, unprocessableExpression1(C_BOOLEAN)))));
assertPredicateTranslates(not(new InPredicate(C_BOOLEAN.toSymbolReference(), new InListExpression(ImmutableList.of(unprocessableExpression1(C_BOOLEAN))))), tupleDomain(C_BOOLEAN, Domain.notNull(BOOLEAN)), not(equal(C_BOOLEAN, unprocessableExpression1(C_BOOLEAN))));
}
use of io.trino.sql.tree.InPredicate in project trino by trinodb.
the class TestDomainTranslator method testFromInPredicateWithCastsAndNulls.
@Test
public void testFromInPredicateWithCastsAndNulls() {
assertPredicateIsAlwaysFalse(new InPredicate(C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT)))));
assertUnsupportedPredicate(not(new InPredicate(cast(C_SMALLINT, BIGINT), new InListExpression(ImmutableList.of(toExpression(null, BIGINT))))));
assertPredicateTranslates(new InPredicate(C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, BIGINT)))), tupleDomain(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L)), false)));
assertPredicateIsAlwaysFalse(not(new InPredicate(C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, SMALLINT))))));
}
Aggregations