public Expression rewriteUsingBounds(QuantifiedComparisonExpression quantifiedComparison, Symbol minValue, Symbol maxValue, Symbol countAllValue, Symbol countNonNullValue)
{
BooleanLiteral emptySetResult = quantifiedComparison.getQuantifier().equals(ALL) ? TRUE_LITERAL : FALSE_LITERAL;
Function<List<Expression>, Expression> quantifier = quantifiedComparison.getQuantifier().equals(ALL) ?
ExpressionUtils::combineConjuncts : ExpressionUtils::combineDisjuncts;
Expression comparisonWithExtremeValue = getBoundComparisons(quantifiedComparison, minValue, maxValue);
return new SimpleCaseExpression(
countAllValue.toSymbolReference(),
ImmutableList.of(new WhenClause(
new GenericLiteral("bigint", "0"),
emptySetResult)),
Optional.of(quantifier.apply(ImmutableList.of(
comparisonWithExtremeValue,
new SearchedCaseExpression(
ImmutableList.of(
new WhenClause(
new ComparisonExpression(NOT_EQUAL, countAllValue.toSymbolReference(), countNonNullValue.toSymbolReference()),
new Cast(new NullLiteral(), BooleanType.BOOLEAN.toString()))),
Optional.of(emptySetResult))))));
}