Skip to content

Commit

Permalink
Reuse type analysis within DomainTranslator.visitComparisonExpression
Browse files Browse the repository at this point in the history
The single invocation of `DomainTranslator.visitComparisonExpression`
used to call type analysis 3 times: in `toNormalizedSimpleComparison`,
in `isImplicitCoercion` and once directly in
`visitComparisonExpression`.
  • Loading branch information
findepi committed Aug 11, 2022
1 parent bfb1c63 commit 7a8cf4b
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ protected ExtractionResult visitSymbolReference(SymbolReference node, Boolean co
@Override
protected ExtractionResult visitComparisonExpression(ComparisonExpression node, Boolean complement)
{
Optional<NormalizedSimpleComparison> optionalNormalized = toNormalizedSimpleComparison(node);
Map<NodeRef<Expression>, Type> expressionTypes = analyzeExpression(node);
Optional<NormalizedSimpleComparison> optionalNormalized = toNormalizedSimpleComparison(expressionTypes, node);
if (optionalNormalized.isEmpty()) {
return super.visitComparisonExpression(node, complement);
}
Expand All @@ -501,7 +502,7 @@ protected ExtractionResult visitComparisonExpression(ComparisonExpression node,
}
if (symbolExpression instanceof Cast) {
Cast castExpression = (Cast) symbolExpression;
if (!isImplicitCoercion(castExpression)) {
if (!isImplicitCoercion(expressionTypes, castExpression)) {
//
// we cannot use non-coercion cast to literal_type on symbol side to build tuple domain
//
Expand All @@ -523,7 +524,8 @@ protected ExtractionResult visitComparisonExpression(ComparisonExpression node,
return super.visitComparisonExpression(node, complement);
}

Type castSourceType = typeAnalyzer.getType(session, types, castExpression.getExpression()); // type of expression which is then cast to type of value
// type of expression which is then cast to type of value
Type castSourceType = requireNonNull(expressionTypes.get(NodeRef.of(castExpression.getExpression())), "No type for Cast source expression");

// we use saturated floor cast value -> castSourceType to rewrite original expression to new one with one cast peeled off the symbol side
Optional<Expression> coercedExpression = coerceComparisonWithRounding(
Expand All @@ -541,9 +543,8 @@ protected ExtractionResult visitComparisonExpression(ComparisonExpression node,
/**
* Extract a normalized simple comparison between a QualifiedNameReference and a native value if possible.
*/
private Optional<NormalizedSimpleComparison> toNormalizedSimpleComparison(ComparisonExpression comparison)
private Optional<NormalizedSimpleComparison> toNormalizedSimpleComparison(Map<NodeRef<Expression>, Type> expressionTypes, ComparisonExpression comparison)
{
Map<NodeRef<Expression>, Type> expressionTypes = analyzeExpression(comparison);
Object left = new ExpressionInterpreter(comparison.getLeft(), plannerContext, session, expressionTypes).optimize(NoOpSymbolResolver.INSTANCE);
Object right = new ExpressionInterpreter(comparison.getRight(), plannerContext, session, expressionTypes).optimize(NoOpSymbolResolver.INSTANCE);

Expand Down Expand Up @@ -576,11 +577,10 @@ private Optional<NormalizedSimpleComparison> toNormalizedSimpleComparison(Compar
return Optional.of(new NormalizedSimpleComparison(symbolExpression, comparisonOperator, value));
}

private boolean isImplicitCoercion(Cast cast)
private boolean isImplicitCoercion(Map<NodeRef<Expression>, Type> expressionTypes, Cast cast)
{
Map<NodeRef<Expression>, Type> expressionTypes = analyzeExpression(cast);
Type actualType = expressionTypes.get(NodeRef.of(cast.getExpression()));
Type expectedType = expressionTypes.get(NodeRef.<Expression>of(cast));
Type actualType = requireNonNull(expressionTypes.get(NodeRef.of(cast.getExpression())), "No type for Cast source expression");
Type expectedType = requireNonNull(expressionTypes.get(NodeRef.of(cast)), "No type for Cast expression");
return typeCoercion.canCoerce(actualType, expectedType);
}

Expand Down

0 comments on commit 7a8cf4b

Please sign in to comment.