Skip to content

Commit

Permalink
Estimate row count when BETWEEN value is an expression
Browse files Browse the repository at this point in the history
  • Loading branch information
raunaqmorarka committed Aug 2, 2023
1 parent 0aa3da0 commit 37a3820
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ protected PlanNodeStatsEstimate visitIsNullPredicate(IsNullPredicate node, Void
@Override
protected PlanNodeStatsEstimate visitBetweenPredicate(BetweenPredicate node, Void context)
{
if (!(node.getValue() instanceof SymbolReference)) {
SymbolStatsEstimate valueStats = getExpressionStats(node.getValue());
if (valueStats.isUnknown()) {
return PlanNodeStatsEstimate.unknown();
}
if (!getExpressionStats(node.getMin()).isSingleValue()) {
Expand All @@ -324,7 +325,6 @@ protected PlanNodeStatsEstimate visitBetweenPredicate(BetweenPredicate node, Voi
return PlanNodeStatsEstimate.unknown();
}

SymbolStatsEstimate valueStats = input.getSymbolStatistics(Symbol.from(node.getValue()));
Expression lowerBound = new ComparisonExpression(GREATER_THAN_OR_EQUAL, node.getValue(), node.getMin());
Expression upperBound = new ComparisonExpression(LESS_THAN_OR_EQUAL, node.getValue(), node.getMax());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,16 @@ public void testBetweenOperatorFilter()
.highValue(100.0)
.nullsFraction(0.0));

// Expression as value. CAST from DOUBLE to DECIMAL(7,2)
// Produces row count estimate without updating symbol stats
assertExpression("CAST(x AS DECIMAL(7,2)) BETWEEN CAST(DECIMAL '-2.50' AS DECIMAL(7, 2)) AND CAST(DECIMAL '2.50' AS DECIMAL(7, 2))")
.outputRowsCount(219.726563)
.symbolStats("x", symbolStats ->
symbolStats.distinctValuesCount(xStats.getDistinctValuesCount())
.lowValue(xStats.getLowValue())
.highValue(xStats.getHighValue())
.nullsFraction(xStats.getNullsFraction()));

assertExpression("'a' IN ('a', 'b')").equalTo(standardInputStatistics);
assertExpression("'a' IN ('a', 'b', NULL)").equalTo(standardInputStatistics);
assertExpression("'a' IN ('b', 'c')").outputRowsCount(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local exchange (GATHER, SINGLE, [])
scan customer_demographics
local exchange (GATHER, SINGLE, [])
remote exchange (REPLICATE, BROADCAST, [])
scan web_page
scan reason
local exchange (GATHER, SINGLE, [])
remote exchange (REPLICATE, BROADCAST, [])
scan reason
scan web_page

0 comments on commit 37a3820

Please sign in to comment.