Source:
private JoinComplementStats calculateJoinComplementStats( PlanNodeStatsEstimate leftStats, PlanNodeStatsEstimate rightStats, EquiJoinClause drivingClause, List<EquiJoinClause> remainingClauses) { PlanNodeStatsEstimate result = leftStats; SymbolStatsEstimate leftColumnStats = leftStats.getSymbolStatistics(drivingClause.getLeft()); SymbolStatsEstimate rightColumnStats = rightStats.getSymbolStatistics(drivingClause.getRight()); double leftNDV = leftColumnStats.getDistinctValuesCount(); double matchingRightNDV = rightColumnStats.getDistinctValuesCount() * unmatchedJoinComplementNdvsCoefficient; if (leftNDV > matchingRightNDV) { double drivingClauseRangeFraction = leftColumnStats.getValuesFraction() * (leftNDV - matchingRightNDV) / leftNDV; double scaleFactor = drivingClauseRangeFraction + leftColumnStats.getNullsFraction(); double newLeftNullsFraction = leftColumnStats.getNullsFraction() / scaleFactor; result = result.mapSymbolColumnStatistics(drivingClause.getLeft(), columnStats -> SymbolStatsEstimate.buildFrom(columnStats) .setLowValue(leftColumnStats.getLowValue()) .setHighValue(leftColumnStats.getHighValue()) .setNullsFraction(newLeftNullsFraction) .setDistinctValuesCount(leftNDV - matchingRightNDV) .build()); result = result.mapOutputRowCount(rowCount -> rowCount * scaleFactor); } else if (leftNDV <= matchingRightNDV) { result = result.mapSymbolColumnStatistics(drivingClause.getLeft(), columnStats -> SymbolStatsEstimate.buildFrom(columnStats) .setLowValue(NaN) .setHighValue(NaN) .setNullsFraction(1.0) .setDistinctValuesCount(0.0) .build()); result = result.mapOutputRowCount(rowCount -> rowCount * leftColumnStats.getNullsFraction()); } else { return new JoinComplementStats(UNKNOWN_STATS, Optional.empty()); } for (int i = 0; i < remainingClauses.size(); ++i) { result = result.mapOutputRowCount(rowCount -> min(leftStats.getOutputRowCount(), rowCount / UNKNOWN_FILTER_COEFFICIENT)); } return new JoinComplementStats(result, Optional.of(drivingClause.getLeft())); }

Target:
private PlanNodeStatsEstimate calculateJoinComplementStats( PlanNodeStatsEstimate leftStats, PlanNodeStatsEstimate rightStats, EquiJoinClause drivingClause, List<EquiJoinClause> remainingClauses) { PlanNodeStatsEstimate result = leftStats; SymbolStatsEstimate leftColumnStats = leftStats.getSymbolStatistics(drivingClause.getLeft()); SymbolStatsEstimate rightColumnStats = rightStats.getSymbolStatistics(drivingClause.getRight()); double leftNDV = leftColumnStats.getDistinctValuesCount(); double matchingRightNDV = rightColumnStats.getDistinctValuesCount() * unmatchedJoinComplementNdvsCoefficient; if (leftNDV > matchingRightNDV) { double nonMatchingLeftValuesFraction = leftColumnStats.getValuesFraction() * (leftNDV - matchingRightNDV) / leftNDV; double scaleFactor = nonMatchingLeftValuesFraction + leftColumnStats.getNullsFraction(); double newLeftNullsFraction = leftColumnStats.getNullsFraction() / scaleFactor; result = result.mapSymbolColumnStatistics(drivingClause.getLeft(), columnStats -> SymbolStatsEstimate.buildFrom(columnStats) .setLowValue(leftColumnStats.getLowValue()) .setHighValue(leftColumnStats.getHighValue()) .setNullsFraction(newLeftNullsFraction) .setDistinctValuesCount(leftNDV - matchingRightNDV) .build()); result = result.mapOutputRowCount(rowCount -> rowCount * scaleFactor); } else if (leftNDV <= matchingRightNDV) { result = result.mapSymbolColumnStatistics(drivingClause.getLeft(), columnStats -> SymbolStatsEstimate.buildFrom(columnStats) .setLowValue(NaN) .setHighValue(NaN) .setNullsFraction(1.0) .setDistinctValuesCount(0.0) .build()); result = result.mapOutputRowCount(rowCount -> rowCount * leftColumnStats.getNullsFraction()); } else { return UNKNOWN_STATS; } for (int i = 0; i < remainingClauses.size(); ++i) { result = result.mapOutputRowCount(rowCount -> min(leftStats.getOutputRowCount(), rowCount / UNKNOWN_FILTER_COEFFICIENT)); } return result; }