use of io.trino.sql.tree.IfExpression in project trino by trinodb.
the class SimplifyFilterPredicate method simplifyFilterExpression.
private Optional<Expression> simplifyFilterExpression(Expression expression) {
if (expression instanceof IfExpression) {
IfExpression ifExpression = (IfExpression) expression;
Expression condition = ifExpression.getCondition();
Expression trueValue = ifExpression.getTrueValue();
Optional<Expression> falseValue = ifExpression.getFalseValue();
if (trueValue.equals(TRUE_LITERAL) && (falseValue.isEmpty() || isNotTrue(falseValue.get()))) {
return Optional.of(condition);
}
if (isNotTrue(trueValue) && falseValue.isPresent() && falseValue.get().equals(TRUE_LITERAL)) {
return Optional.of(isFalseOrNullPredicate(condition));
}
if (falseValue.isPresent() && falseValue.get().equals(trueValue) && isDeterministic(trueValue, metadata)) {
return Optional.of(trueValue);
}
if (isNotTrue(trueValue) && (falseValue.isEmpty() || isNotTrue(falseValue.get()))) {
return Optional.of(FALSE_LITERAL);
}
if (condition.equals(TRUE_LITERAL)) {
return Optional.of(trueValue);
}
if (isNotTrue(condition)) {
return Optional.of(falseValue.orElse(FALSE_LITERAL));
}
return Optional.empty();
}
if (expression instanceof NullIfExpression) {
NullIfExpression nullIfExpression = (NullIfExpression) expression;
return Optional.of(LogicalExpression.and(nullIfExpression.getFirst(), isFalseOrNullPredicate(nullIfExpression.getSecond())));
}
if (expression instanceof SearchedCaseExpression) {
SearchedCaseExpression caseExpression = (SearchedCaseExpression) expression;
Optional<Expression> defaultValue = caseExpression.getDefaultValue();
List<Expression> operands = caseExpression.getWhenClauses().stream().map(WhenClause::getOperand).collect(toImmutableList());
List<Expression> results = caseExpression.getWhenClauses().stream().map(WhenClause::getResult).collect(toImmutableList());
long trueResultsCount = results.stream().filter(result -> result.equals(TRUE_LITERAL)).count();
long notTrueResultsCount = results.stream().filter(SimplifyFilterPredicate::isNotTrue).count();
// all results true
if (trueResultsCount == results.size() && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
return Optional.of(TRUE_LITERAL);
}
// all results not true
if (notTrueResultsCount == results.size() && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
return Optional.of(FALSE_LITERAL);
}
// one result true, and remaining results not true
if (trueResultsCount == 1 && notTrueResultsCount == results.size() - 1 && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
ImmutableList.Builder<Expression> builder = ImmutableList.builder();
for (WhenClause whenClause : caseExpression.getWhenClauses()) {
Expression operand = whenClause.getOperand();
Expression result = whenClause.getResult();
if (isNotTrue(result)) {
builder.add(isFalseOrNullPredicate(operand));
} else {
builder.add(operand);
return Optional.of(combineConjuncts(metadata, builder.build()));
}
}
}
// all results not true, and default true
if (notTrueResultsCount == results.size() && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
ImmutableList.Builder<Expression> builder = ImmutableList.builder();
operands.stream().forEach(operand -> builder.add(isFalseOrNullPredicate(operand)));
return Optional.of(combineConjuncts(metadata, builder.build()));
}
// skip clauses with not true conditions
List<WhenClause> whenClauses = new ArrayList<>();
for (WhenClause whenClause : caseExpression.getWhenClauses()) {
Expression operand = whenClause.getOperand();
if (operand.equals(TRUE_LITERAL)) {
if (whenClauses.isEmpty()) {
return Optional.of(whenClause.getResult());
}
return Optional.of(new SearchedCaseExpression(whenClauses, Optional.of(whenClause.getResult())));
}
if (!isNotTrue(operand)) {
whenClauses.add(whenClause);
}
}
if (whenClauses.isEmpty()) {
return Optional.of(defaultValue.orElse(FALSE_LITERAL));
}
if (whenClauses.size() < caseExpression.getWhenClauses().size()) {
return Optional.of(new SearchedCaseExpression(whenClauses, defaultValue));
}
return Optional.empty();
}
if (expression instanceof SimpleCaseExpression) {
SimpleCaseExpression caseExpression = (SimpleCaseExpression) expression;
Optional<Expression> defaultValue = caseExpression.getDefaultValue();
if (caseExpression.getOperand() instanceof NullLiteral) {
return Optional.of(defaultValue.orElse(FALSE_LITERAL));
}
List<Expression> results = caseExpression.getWhenClauses().stream().map(WhenClause::getResult).collect(toImmutableList());
if (results.stream().allMatch(result -> result.equals(TRUE_LITERAL)) && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
return Optional.of(TRUE_LITERAL);
}
if (results.stream().allMatch(SimplifyFilterPredicate::isNotTrue) && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
return Optional.of(FALSE_LITERAL);
}
return Optional.empty();
}
return Optional.empty();
}
use of io.trino.sql.tree.IfExpression in project trino by trinodb.
the class TransformUncorrelatedSubqueryToJoin method apply.
@Override
public Result apply(CorrelatedJoinNode correlatedJoinNode, Captures captures, Context context) {
// handle INNER and LEFT correlated join
if (correlatedJoinNode.getType() == INNER || correlatedJoinNode.getType() == LEFT) {
return Result.ofPlanNode(rewriteToJoin(correlatedJoinNode, correlatedJoinNode.getType().toJoinNodeType(), correlatedJoinNode.getFilter()));
}
checkState(correlatedJoinNode.getType() == RIGHT || correlatedJoinNode.getType() == FULL, "unexpected CorrelatedJoin type: " + correlatedJoinNode.getType());
// handle RIGHT and FULL correlated join ON TRUE
JoinNode.Type type;
if (correlatedJoinNode.getType() == RIGHT) {
type = JoinNode.Type.INNER;
} else {
type = JoinNode.Type.LEFT;
}
JoinNode joinNode = rewriteToJoin(correlatedJoinNode, type, TRUE_LITERAL);
if (correlatedJoinNode.getFilter().equals(TRUE_LITERAL)) {
return Result.ofPlanNode(joinNode);
}
// handle RIGHT correlated join on condition other than TRUE
if (correlatedJoinNode.getType() == RIGHT) {
Assignments.Builder assignments = Assignments.builder();
assignments.putIdentities(Sets.intersection(ImmutableSet.copyOf(correlatedJoinNode.getSubquery().getOutputSymbols()), ImmutableSet.copyOf(correlatedJoinNode.getOutputSymbols())));
for (Symbol inputSymbol : Sets.intersection(ImmutableSet.copyOf(correlatedJoinNode.getInput().getOutputSymbols()), ImmutableSet.copyOf(correlatedJoinNode.getOutputSymbols()))) {
assignments.put(inputSymbol, new IfExpression(correlatedJoinNode.getFilter(), inputSymbol.toSymbolReference(), new NullLiteral()));
}
ProjectNode projectNode = new ProjectNode(context.getIdAllocator().getNextId(), joinNode, assignments.build());
return Result.ofPlanNode(projectNode);
}
// no support for FULL correlated join on condition other than TRUE
return Result.empty();
}
use of io.trino.sql.tree.IfExpression in project trino by trinodb.
the class TestSimplifyFilterPredicate method testSimplifyIfExpression.
@Test
public void testSimplifyIfExpression() {
// true result iff the condition is true
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, true, false)"), p.values(p.symbol("a")))).matches(filter("a", values("a")));
// true result iff the condition is true
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, true)"), p.values(p.symbol("a")))).matches(filter("a", values("a")));
// true result iff the condition is null or false
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, false, true)"), p.values(p.symbol("a")))).matches(filter("a IS NULL OR NOT a", values("a")));
// true result iff the condition is null or false
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, null, true)"), p.values(p.symbol("a")))).matches(filter("a IS NULL OR NOT a", values("a")));
// always true
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, true, true)"), p.values(p.symbol("a")))).matches(filter("true", values("a")));
// always false
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, false, false)"), p.values(p.symbol("a")))).matches(filter("false", values("a")));
// both results equal
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, b > 0, b > 0)"), p.values(p.symbol("a"), p.symbol("b")))).matches(filter("b > 0", values("a", "b")));
// both results are equal non-deterministic expressions
FunctionCall randomFunction = new FunctionCall(tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("random"), ImmutableList.of()).toQualifiedName(), ImmutableList.of());
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(new IfExpression(expression("a"), new ComparisonExpression(EQUAL, randomFunction, new LongLiteral("0")), new ComparisonExpression(EQUAL, randomFunction, new LongLiteral("0"))), p.values(p.symbol("a")))).doesNotFire();
// always null (including the default) -> simplified to FALSE
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, null)"), p.values(p.symbol("a")))).matches(filter("false", values("a")));
// condition is true -> first branch
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(true, a, NOT a)"), p.values(p.symbol("a")))).matches(filter("a", values("a")));
// condition is true -> second branch
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(false, a, NOT a)"), p.values(p.symbol("a")))).matches(filter("NOT a", values("a")));
// condition is true, no second branch -> the result is null, simplified to FALSE
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(false, a)"), p.values(p.symbol("a")))).matches(filter("false", values("a")));
// not known result (`b`) - cannot optimize
tester().assertThat(new SimplifyFilterPredicate(tester().getMetadata())).on(p -> p.filter(expression("IF(a, true, b)"), p.values(p.symbol("a"), p.symbol("b")))).doesNotFire();
}
use of io.trino.sql.tree.IfExpression in project trino by trinodb.
the class LogicalPlanner method noTruncationCast.
/*
According to the standard, for the purpose of store assignment (INSERT),
no non-space characters of a character string, and no non-zero octets
of a binary string must be lost when the inserted value is truncated to
fit in the target column type.
The following method returns a cast from source type to target type
with a guarantee of no illegal truncation.
TODO Once BINARY and parametric VARBINARY types are supported, they should be handled here.
TODO This workaround is insufficient to handle structural types
*/
private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
if (fromType instanceof UnknownType || (!(toType instanceof VarcharType) && !(toType instanceof CharType))) {
return new Cast(expression, toSqlType(toType));
}
int targetLength;
if (toType instanceof VarcharType) {
if (((VarcharType) toType).isUnbounded()) {
return new Cast(expression, toSqlType(toType));
}
targetLength = ((VarcharType) toType).getBoundedLength();
} else {
targetLength = ((CharType) toType).getLength();
}
checkState(fromType instanceof VarcharType || fromType instanceof CharType, "inserting non-character value to column of character type");
ResolvedFunction spaceTrimmedLength = metadata.resolveFunction(session, QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
ResolvedFunction fail = metadata.resolveFunction(session, QualifiedName.of("fail"), fromTypes(VARCHAR));
return new IfExpression(// check if the trimmed value fits in the target type
new ComparisonExpression(GREATER_THAN_OR_EQUAL, new GenericLiteral("BIGINT", Integer.toString(targetLength)), new CoalesceExpression(new FunctionCall(spaceTrimmedLength.toQualifiedName(), ImmutableList.of(new Cast(expression, toSqlType(VARCHAR)))), new GenericLiteral("BIGINT", "0"))), new Cast(expression, toSqlType(toType)), new Cast(new FunctionCall(fail.toQualifiedName(), ImmutableList.of(new Cast(new StringLiteral(format("Cannot truncate non-space characters when casting from %s to %s on INSERT", fromType.getDisplayName(), toType.getDisplayName())), toSqlType(VARCHAR)))), toSqlType(toType)));
}
use of io.trino.sql.tree.IfExpression in project trino by trinodb.
the class TestPatternRecognitionNodeSerialization method testExpressionAndValuePointersRoundtrip.
@Test
public void testExpressionAndValuePointersRoundtrip() {
ObjectMapperProvider provider = new ObjectMapperProvider();
provider.setJsonSerializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionSerializer()));
provider.setJsonDeserializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionDeserializer(new SqlParser())));
JsonCodec<ExpressionAndValuePointers> codec = new JsonCodecFactory(provider).jsonCodec(ExpressionAndValuePointers.class);
assertJsonRoundTrip(codec, new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()));
assertJsonRoundTrip(codec, new ExpressionAndValuePointers(new IfExpression(new ComparisonExpression(GREATER_THAN, new SymbolReference("classifier"), new SymbolReference("x")), new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()), new ArithmeticUnaryExpression(MINUS, new SymbolReference("match_number"))), ImmutableList.of(new Symbol("classifier"), new Symbol("x"), new Symbol("match_number")), ImmutableList.of(new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), false, true, 1, -1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("B")), true, false, 2, 1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), true, true, 0, 0), new Symbol("input_symbol_a"))), ImmutableSet.of(new Symbol("classifier")), ImmutableSet.of(new Symbol("match_number"))));
}
Aggregations