Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify coalesce expressions #35

Merged
merged 2 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import java.lang.invoke.MethodHandle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a description in this commit message explaining what kind of simplification is being done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have updated the commit message.

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -118,6 +119,7 @@
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.getOnlyElement;
import static io.prestosql.SystemSessionProperties.isLegacyRowFieldOrdinalAccessEnabled;
import static io.prestosql.operator.scalar.ScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL;
import static io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED;
Expand All @@ -128,6 +130,7 @@
import static io.prestosql.sql.analyzer.ExpressionAnalyzer.createConstantAnalyzer;
import static io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes;
import static io.prestosql.sql.gen.VarArgsToMapAdapterGenerator.generateVarArgsToMapAdapter;
import static io.prestosql.sql.planner.DeterminismEvaluator.isDeterministic;
import static io.prestosql.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression;
import static io.prestosql.type.LikeFunctions.isLikePattern;
import static io.prestosql.type.LikeFunctions.unescapeLiteralLikePattern;
Expand Down Expand Up @@ -546,19 +549,38 @@ protected Object visitCoalesceExpression(CoalesceExpression node, Object context
List<Object> values = node.getOperands().stream()
.map(value -> processWithExceptionHandling(value, context))
.filter(Objects::nonNull)
.flatMap(expression -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is one kind of simplification handled by this commit, namely, flattening nested calls to coalesce: coalesce(coalesce(a, b), c) => coalesce(a, b, c). I would pull it out into a separate commit as well as any associated tests, since it stands logically by itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about multiple levels of nesting? E.g., coalesce(coalesce(coalesce(a,b),c),d)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ll add that as a new commit.

What about multiple levels of nesting?

ExpressionInterpreter#visitCoalesceExpression is called in a recursive way so
coalesce(coalesce(coalesce(a,b),c),d) => coalesce(coalesce(a,b,c),d)
coalesce(coalesce(a,b,c),d) => coalesce(a,b,c,d)

Have added a test to evaluate the same

if (expression instanceof CoalesceExpression) {
return ((CoalesceExpression) expression).getOperands().stream();
}
return Stream.of(expression);
})
.collect(Collectors.toList());

if ((!values.isEmpty() && !(values.get(0) instanceof Expression)) || values.size() == 1) {
return values.get(0);
}

List<Expression> expressions = values.stream()
.map(value -> toExpression(value, type))
.collect(Collectors.toList());
ImmutableList.Builder<Expression> operandsBuilder = ImmutableList.builder();
Set<Expression> visitedExpression = new HashSet<>();
for (Object value : values) {
Expression expression = toExpression(value, type);
if (!isDeterministic(expression) || visitedExpression.add(expression)) {
operandsBuilder.add(expression);
}
// TODO: Replace this logic with an anlyzer which specifies whether it evaluates to null
if (expression instanceof Literal && !(expression instanceof NullLiteral)) {
break;
}
}
List<Expression> expressions = operandsBuilder.build();

if (expressions.isEmpty()) {
return null;
}

if (expressions.size() == 1) {
return getOnlyElement(expressions);
}
return new CoalesceExpression(expressions);
}

Expand Down Expand Up @@ -642,7 +664,7 @@ else if (!found && result) {
.filter(DeterminismEvaluator::isDeterministic)
.distinct(),
expressionValues.stream()
.filter((expression -> !DeterminismEvaluator.isDeterministic(expression))))
.filter((expression -> !isDeterministic(expression))))
.collect(toImmutableList());
return new InPredicate(toExpression(value, type), new InListExpression(simplifiedExpressionValues));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public class TestExpressionInterpreter
.put(new Symbol("unbound_integer"), INTEGER)
.put(new Symbol("unbound_long"), BIGINT)
.put(new Symbol("unbound_long2"), BIGINT)
.put(new Symbol("unbound_long3"), BIGINT)
.put(new Symbol("unbound_string"), VARCHAR)
.put(new Symbol("unbound_double"), DOUBLE)
.put(new Symbol("unbound_boolean"), BOOLEAN)
Expand Down Expand Up @@ -1136,7 +1137,19 @@ public void testCoalesce()
assertOptimizedEquals("coalesce(2 * 3 * unbound_integer, 1.0E0/2.0E0, null)", "coalesce(6 * unbound_integer, 0.5E0)");
assertOptimizedEquals("coalesce(unbound_integer, 2, 1.0E0/2.0E0, 12.34E0, null)", "coalesce(unbound_integer, 2.0E0, 0.5E0, 12.34E0)");
assertOptimizedMatches("coalesce(0 / 0 > 1, unbound_boolean, 0 / 0 = 0)",
"coalesce(cast(fail() as boolean), unbound_boolean, cast(fail() as boolean))");
"coalesce(cast(fail() as boolean), unbound_boolean)");
assertOptimizedMatches("coalesce(unbound_long, unbound_long)", "unbound_long");
assertOptimizedMatches("coalesce(2 * unbound_long, 2 * unbound_long)", "BIGINT '2' * unbound_long");
assertOptimizedMatches("coalesce(unbound_long, unbound_long2, unbound_long)", "coalesce(unbound_long, unbound_long2)");
assertOptimizedMatches("coalesce(unbound_long, unbound_long2, unbound_long, unbound_long3)", "coalesce(unbound_long, unbound_long2, unbound_long3)");
assertOptimizedEquals("coalesce(6, unbound_long2, unbound_long, unbound_long3)", "6");
assertOptimizedEquals("coalesce(2 * 3, unbound_long2, unbound_long, unbound_long3)", "6");
assertOptimizedMatches("coalesce(random(), random(), 5)", "coalesce(random(), random(), 5E0)");
assertOptimizedMatches("coalesce(unbound_long, coalesce(unbound_long, 1))", "coalesce(unbound_long, BIGINT '1')");
assertOptimizedMatches("coalesce(coalesce(unbound_long, coalesce(unbound_long, 1)), unbound_long2)", "coalesce(unbound_long, BIGINT '1')");
assertOptimizedMatches("coalesce(unbound_long, 2, coalesce(unbound_long, 1))", "coalesce(unbound_long, BIGINT '2')");
assertOptimizedMatches("coalesce(coalesce(unbound_long, coalesce(unbound_long2, unbound_long3)), 1)", "coalesce(unbound_long, unbound_long2, unbound_long3, BIGINT '1')");
assertOptimizedMatches("coalesce(unbound_double, coalesce(random(), unbound_double))", "coalesce(unbound_double, random())");
}

@Test
Expand Down