Source:
public RowExpression visitSpecialForm(SpecialFormExpression specialFormExpression, ConvertNormalFormVisitorContext context) { if (!isConjunctionOrDisjunction(specialFormExpression)) { return specialFormExpression; } RowExpression rewritten = combinePredicates( specialFormExpression.getForm(), extractPredicates(specialFormExpression.getForm(), specialFormExpression) .stream() .map(subPredicate -> subPredicate.accept(this, context.childContext())) .collect(toList())); if (!isConjunctionOrDisjunction(rewritten)) { return rewritten; } SpecialFormExpression rewrittenSpecialForm = (SpecialFormExpression) rewritten; Form currentForm = rewrittenSpecialForm.getForm(); List<List<RowExpression>> subPredicates = getSubPredicates(rewrittenSpecialForm); if (subPredicates.stream().mapToInt(List::size).sum() > ELIMINATE_COMMON_SIZE_LIMIT) { return combineSubPredicates(currentForm, subPredicates); } subPredicates = eliminateCommonPredicates(subPredicates); List<List<RowExpression>> flippedSubPredicates = extractCommonPredicates(currentForm, subPredicates); if (flippedSubPredicates != null) { subPredicates = flippedSubPredicates; currentForm = flip(currentForm); } int currentSize = subPredicates.stream() .mapToInt(List::size) .sum(); int expectedSize; try { expectedSize = multiplyExact( subPredicates.stream() .mapToInt(List::size) .reduce(1, Math::multiplyExact), subPredicates.size()); } catch (ArithmeticException e) { return combineSubPredicates(currentForm, subPredicates); } boolean deterministic = subPredicates.stream() .flatMap(List::stream) .allMatch(determinismEvaluator::isDeterministic); if (currentForm == context.clauseJoiner || expectedSize == currentSize || expectedSize > 2 * currentSize || !deterministic || context.levelFromRoot > 0) { return combineSubPredicates(currentForm, subPredicates); } subPredicates = crossProduct(subPredicates); return combineSubPredicates(context.clauseJoiner, subPredicates); }

Target:
public RowExpression visitSpecialForm(SpecialFormExpression specialFormExpression, ConvertNormalFormVisitorContext context) { if (!isConjunctionOrDisjunction(specialFormExpression)) { return specialFormExpression; } RowExpression rewritten = combinePredicates( specialFormExpression.getForm(), extractPredicates(specialFormExpression.getForm(), specialFormExpression).stream() .map(subPredicate -> subPredicate.accept(this, context.childContext())) .collect(toList())); if (!isConjunctionOrDisjunction(rewritten)) { return rewritten; } SpecialFormExpression rewrittenSpecialForm = (SpecialFormExpression) rewritten; Form expressionClauseJoiner = rewrittenSpecialForm.getForm(); List<List<RowExpression>> groupedClauses = getGroupedClauses(rewrittenSpecialForm); if (groupedClauses.stream().mapToInt(List::size).sum() > ELIMINATE_COMMON_SIZE_LIMIT) { return rewritten; } groupedClauses = eliminateCommonPredicates(groupedClauses); List<List<RowExpression>> groupedClausesWithFlippedJoiner = extractCommonPredicates(expressionClauseJoiner, groupedClauses); if (groupedClausesWithFlippedJoiner != null) { groupedClauses = groupedClausesWithFlippedJoiner; expressionClauseJoiner = flip(expressionClauseJoiner); } int numClauses = groupedClauses.stream().mapToInt(List::size).sum(); int numClausesProducedByDistributiveLaw = groupedClauses.size(); for (List<RowExpression> group : groupedClauses) { numClausesProducedByDistributiveLaw *= group.size(); if (context.depth > 0 || numClausesProducedByDistributiveLaw > numClauses * 2) { return combineGroupedClauses(expressionClauseJoiner, groupedClauses); } } if (numClausesProducedByDistributiveLaw == numClauses) { return combineGroupedClauses(expressionClauseJoiner, groupedClauses); } boolean deterministic = groupedClauses.stream() .flatMap(List::stream) .allMatch(determinismEvaluator::isDeterministic); if (expressionClauseJoiner == context.expectedClauseJoiner || !deterministic) { return combineGroupedClauses(expressionClauseJoiner, groupedClauses); } groupedClauses = crossProduct(groupedClauses); return combineGroupedClauses(context.expectedClauseJoiner, groupedClauses); }