Skip to content

Commit e248c01

Browse files
committed
Removed constants inside enums, will be replaced by more cleaner way in subsequent PRs.
1 parent c03517e commit e248c01

File tree

4 files changed

+19
-51
lines changed

4 files changed

+19
-51
lines changed

presto-main/src/main/java/com/facebook/presto/cost/ScalarStatsAnnotationProcessor.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
import java.util.List;
2727
import java.util.Map;
2828

29-
import static com.facebook.presto.spi.function.StatsPropagationBehavior.NON_NULL_ROW_COUNT;
30-
import static com.facebook.presto.spi.function.StatsPropagationBehavior.ROW_COUNT;
3129
import static com.facebook.presto.spi.function.StatsPropagationBehavior.SUM_ARGUMENTS;
32-
import static com.facebook.presto.spi.function.StatsPropagationBehavior.SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT;
3330
import static com.facebook.presto.spi.function.StatsPropagationBehavior.UNKNOWN;
3431
import static com.facebook.presto.spi.function.StatsPropagationBehavior.USE_SOURCE_STATS;
3532
import static com.facebook.presto.util.MoreMath.max;
3633
import static com.facebook.presto.util.MoreMath.min;
3734
import static com.facebook.presto.util.MoreMath.minExcludingNaNs;
38-
import static com.facebook.presto.util.MoreMath.nearlyEqual;
3935
import static com.google.common.base.Preconditions.checkArgument;
4036
import static com.google.common.collect.ImmutableList.toImmutableList;
4137
import static java.lang.Double.NaN;
@@ -93,19 +89,11 @@ public static VariableStatsEstimate process(
9389
.setHighValue(maxValue)
9490
.setNullsFraction(nullFraction)
9591
.setAverageRowSize(firstFiniteValue(scalarStatsHeader.getAvgRowSize(), averageRowSize, returnNaNIfTypeWidthUnknown(getReturnTypeWidth(callExpression, UNKNOWN))))
96-
.setDistinctValuesCount(processDistinctValuesCount(outputRowCount, nullFraction, scalarStatsHeader.getDistinctValuesCount(), distinctValuesCount)).build();
92+
.setDistinctValuesCount(processDistinctValuesCount(outputRowCount, scalarStatsHeader.getDistinctValuesCount(), distinctValuesCount)).build();
9793
}
9894

99-
private static double processDistinctValuesCount(double outputRowCount, double nullFraction, double distinctValuesCountFromConstant, double distinctValuesCount)
95+
private static double processDistinctValuesCount(double outputRowCount, double distinctValuesCountFromConstant, double distinctValuesCount)
10096
{
101-
if (isFinite(distinctValuesCountFromConstant)) {
102-
if (nearlyEqual(distinctValuesCountFromConstant, NON_NULL_ROW_COUNT.getValue(), 0.1)) {
103-
distinctValuesCountFromConstant = outputRowCount * (1 - firstFiniteValue(nullFraction, 0.0));
104-
}
105-
else if (nearlyEqual(distinctValuesCount, ROW_COUNT.getValue(), 0.1)) {
106-
distinctValuesCountFromConstant = outputRowCount;
107-
}
108-
}
10997
double distinctValuesCountFinal = firstFiniteValue(distinctValuesCountFromConstant, distinctValuesCount);
11098
if (distinctValuesCountFinal > outputRowCount) {
11199
distinctValuesCountFinal = NaN;
@@ -143,9 +131,6 @@ private static double processSingleArgumentStatistic(
143131
case SUM_ARGUMENTS:
144132
statValue = statValue + sourceStats.get(i);
145133
break;
146-
case SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT:
147-
statValue = min(statValue + sourceStats.get(i), outputRowCount);
148-
break;
149134
}
150135
}
151136
}
@@ -197,7 +182,7 @@ private static int getReturnTypeWidth(CallExpression callExpression, StatsPropag
197182
if (!returnType.isUnbounded()) {
198183
return returnType.getLengthSafe();
199184
}
200-
if (operation == SUM_ARGUMENTS || operation == SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT) {
185+
if (operation == SUM_ARGUMENTS) {
201186
// since return type is an unbounded varchar and operation is SUM_ARGUMENTS,
202187
// calculating the type width by doing a SUM of each argument's varchar type bounds - if available.
203188
int sum = 0;

presto-main/src/test/java/com/facebook/presto/cost/TestScalarStatsAnnotationProcessor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import static com.facebook.presto.spi.function.StatsPropagationBehavior.NON_NULL_ROW_COUNT;
4444
import static com.facebook.presto.spi.function.StatsPropagationBehavior.ROW_COUNT;
4545
import static com.facebook.presto.spi.function.StatsPropagationBehavior.SUM_ARGUMENTS;
46-
import static com.facebook.presto.spi.function.StatsPropagationBehavior.SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT;
4746
import static com.facebook.presto.spi.function.StatsPropagationBehavior.UNKNOWN;
4847
import static com.facebook.presto.spi.function.StatsPropagationBehavior.USE_MAX_ARGUMENT;
4948
import static com.facebook.presto.spi.function.StatsPropagationBehavior.USE_MIN_ARGUMENT;
@@ -159,7 +158,7 @@ public void testProcessTypeWidthBoundaryConditions2()
159158
CONSTANT_STATS_UNKNOWN,
160159
ImmutableMap.of(1, createScalarPropagateSourceStatsInstance(false,
161160
USE_MIN_ARGUMENT, SUM_ARGUMENTS, SUM_ARGUMENTS, SUM_ARGUMENTS,
162-
SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT)));
161+
USE_MAX_ARGUMENT)));
163162
VariableStatsEstimate actualStats =
164163
ScalarStatsAnnotationProcessor.process(Double.MAX_VALUE - 1, callExpression, ImmutableList.of(statsEstimateLarge, statsEstimateLarge), scalarStatsHeader);
165164
VariableStatsEstimate expectedStats = VariableStatsEstimate
@@ -179,7 +178,7 @@ public void testProcessConstantStats()
179178
CallExpression callExpression =
180179
new CallExpression("test", new BuiltInFunctionHandle(signature), createVarcharType(10), ImmutableList.of());
181180
ScalarStatsHeader scalarStatsHeader = new ScalarStatsHeader(
182-
createScalarFunctionConstantStatsInstance(0, 1, 0.1, 8, NON_NULL_ROW_COUNT.getValue()),
181+
createScalarFunctionConstantStatsInstance(0, 1, 0.1, 8, 900),
183182
ImmutableMap.of());
184183
VariableStatsEstimate actualStats = ScalarStatsAnnotationProcessor.process(1000, callExpression, STATS_ESTIMATE_LIST, scalarStatsHeader);
185184
VariableStatsEstimate expectedStats = VariableStatsEstimate.builder()
@@ -198,7 +197,7 @@ public void testProcessConstantNDVWithNullFractionFromArgumentStats()
198197
Signature signature = new Signature(QualifiedObjectName.valueOf("presto.default.test"), SCALAR, VARCHAR_TYPE_10, VARCHAR_TYPE_10, VARCHAR_TYPE_10);
199198
CallExpression callExpression = new CallExpression("test", new BuiltInFunctionHandle(signature), createVarcharType(10), TWO_ARGUMENTS);
200199
ScalarStatsHeader scalarStatsHeader = new ScalarStatsHeader(
201-
createScalarFunctionConstantStatsInstance(0, 1, NaN, NaN, NON_NULL_ROW_COUNT.getValue()),
200+
createScalarFunctionConstantStatsInstance(0, 1, NaN, NaN, 900),
202201
ImmutableMap.of(0, createScalarPropagateSourceStatsInstance(false, UNKNOWN, UNKNOWN, SUM_ARGUMENTS, USE_SOURCE_STATS, UNKNOWN)));
203202
VariableStatsEstimate actualStats = ScalarStatsAnnotationProcessor.process(1000, callExpression, STATS_ESTIMATE_LIST, scalarStatsHeader);
204203
VariableStatsEstimate expectedStats = VariableStatsEstimate.builder()

presto-main/src/test/java/com/facebook/presto/cost/TestScalarStatsCalculator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void testStatsPropagationForCustomAdd()
133133
expression("custom_add(x, y)"),
134134
TWO_ARGUMENTS_BIGINT_SOURCE_STATS,
135135
TypeProvider.viewOf(TWO_ARGUMENTS_BIGINT_NAME_TO_TYPE_MAP))
136-
.distinctValuesCount(7)
136+
.distinctValuesCount(4)
137137
.lowValue(-3)
138138
.highValue(15)
139139
.nullsFraction(0.3)
@@ -845,7 +845,7 @@ public static long customAdd(
845845
@ScalarPropagateSourceStats(
846846
propagateAllStats = false,
847847
nullFraction = StatsPropagationBehavior.SUM_ARGUMENTS,
848-
distinctValuesCount = StatsPropagationBehavior.SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT,
848+
distinctValuesCount = StatsPropagationBehavior.USE_MAX_ARGUMENT,
849849
minValue = StatsPropagationBehavior.SUM_ARGUMENTS,
850850
maxValue = StatsPropagationBehavior.SUM_ARGUMENTS) @SqlType(StandardTypes.BIGINT) long x,
851851
@SqlType(StandardTypes.BIGINT) long y)

presto-spi/src/main/java/com/facebook/presto/spi/function/StatsPropagationBehavior.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,31 @@
2222
public enum StatsPropagationBehavior
2323
{
2424
/** Use the max value across all arguments to derive the new stats value */
25-
USE_MAX_ARGUMENT(0),
25+
USE_MAX_ARGUMENT,
2626
/** Use the min value across all arguments to derive the new stats value */
27-
USE_MIN_ARGUMENT(0),
27+
USE_MIN_ARGUMENT,
2828
/** Sum the stats value of all arguments to derive the new stats value */
29-
SUM_ARGUMENTS(0),
30-
/** Sum the stats value of all arguments to derive the new stats value, but upper bounded to row count. */
31-
SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT(0),
29+
SUM_ARGUMENTS,
3230
/** Propagate the source stats as-is */
33-
USE_SOURCE_STATS(0),
31+
USE_SOURCE_STATS,
3432
// Following stats are independent of source stats.
3533
/** Use the value of output row count. */
36-
ROW_COUNT(-1),
34+
ROW_COUNT,
3735
/** Use the value of row_count * (1 - null_fraction). */
38-
NON_NULL_ROW_COUNT(-10),
36+
NON_NULL_ROW_COUNT,
3937
/** use the value of TYPE_WIDTH in varchar(TYPE_WIDTH) */
40-
USE_TYPE_WIDTH_VARCHAR(0),
38+
USE_TYPE_WIDTH_VARCHAR,
4139
/** Take max of type width of arguments with varchar type. */
42-
MAX_TYPE_WIDTH_VARCHAR(0),
40+
MAX_TYPE_WIDTH_VARCHAR,
4341
/** Stats are unknown and thus no action is performed. */
44-
UNKNOWN(0);
42+
UNKNOWN;
4543
/*
4644
* Stats are multi argument when their value is calculated by operating on stats from source stats or other properties of the all the arguments.
4745
*/
4846
private static final Set<StatsPropagationBehavior> MULTI_ARGUMENT_STATS =
49-
unmodifiableSet(
50-
new HashSet<>(Arrays.asList(MAX_TYPE_WIDTH_VARCHAR, USE_MAX_ARGUMENT, USE_MIN_ARGUMENT, SUM_ARGUMENTS, SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT)));
47+
unmodifiableSet(new HashSet<>(Arrays.asList(MAX_TYPE_WIDTH_VARCHAR, USE_MAX_ARGUMENT, USE_MIN_ARGUMENT, SUM_ARGUMENTS)));
5148
private static final Set<StatsPropagationBehavior> SOURCE_STATS_DEPENDENT_STATS =
52-
unmodifiableSet(
53-
new HashSet<>(Arrays.asList(USE_MAX_ARGUMENT, USE_MIN_ARGUMENT, SUM_ARGUMENTS, SUM_ARGUMENTS_UPPER_BOUNDED_TO_ROW_COUNT, USE_SOURCE_STATS)));
54-
55-
private final int value;
56-
57-
StatsPropagationBehavior(int value)
58-
{
59-
this.value = value;
60-
}
61-
62-
public int getValue()
63-
{
64-
return this.value;
65-
}
49+
unmodifiableSet(new HashSet<>(Arrays.asList(USE_MAX_ARGUMENT, USE_MIN_ARGUMENT, SUM_ARGUMENTS, USE_SOURCE_STATS)));
6650

6751
public boolean isMultiArgumentStat()
6852
{

0 commit comments

Comments
 (0)