-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,7 @@ | |
import java.lang.invoke.MethodHandle; | ||
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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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 -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about multiple levels of nesting? E.g., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ll add that as a new commit.
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); | ||
} | ||
|
||
|
@@ -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)); | ||
} | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.