jtuglu1 commented on code in PR #18426:
URL: https://github.com/apache/druid/pull/18426#discussion_r2299812238
##########
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/sql/ThetaPostAggMacros.java:
##########
@@ -84,4 +103,51 @@ public ExpressionType getOutputType(InputBindingInspector
inspector)
return ExpressionType.DOUBLE;
}
}
+
+ public static class ThetaSketchEstimateWithErrorBoundsExpr extends
ExprMacroTable.BaseScalarMacroFunctionExpr
+ {
+ private Expr estimateExpr;
+ private Expr numStdDev;
+
+ public
ThetaSketchEstimateWithErrorBoundsExpr(ThetaSketchEstimateWithErrorBoundsExprMacro
macro, List<Expr> args)
+ {
+ super(macro, args);
+ this.estimateExpr = args.get(0);
+ this.numStdDev = args.get(1);
+ }
+
+ @Override
+ public ExprEval eval(ObjectBinding bindings)
+ {
+ int numStdDevs = numStdDev.eval(bindings).asInt();
+ ExprEval eval = estimateExpr.eval(bindings);
+
+ final Object valObj = eval.value();
+ if (valObj == null) {
+ return ExprEval.ofComplex(
+ THETA_SKETCH_ESTIMATE_WITH_ERROR_BOUNDS_TYPE,
+ new SketchEstimateWithErrorBounds(
+ 0.0D,
+ 0.0D,
+ 0.0D,
+ numStdDevs
+ )
+ );
+ }
+
+ if (valObj instanceof SketchHolder) {
+ SketchHolder thetaSketchHolder = (SketchHolder) valObj;
+ return
ExprEval.ofComplex(THETA_SKETCH_ESTIMATE_WITH_ERROR_BOUNDS_TYPE,
thetaSketchHolder.getEstimateWithErrorBounds(numStdDevs));
+ } else {
+ throw new IllegalArgumentException("requires a ThetaSketch as the
argument");
Review Comment:
nit: title-case exception message
##########
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/sql/HllPostAggExprMacros.java:
##########
@@ -88,5 +105,44 @@ public ExprEval eval(ObjectBinding bindings)
return round ? ExprEval.of(Math.round(estimate)) : ExprEval.of(estimate);
}
}
+
+ public static class HllSketchEstimateWithErrorBoundsExpr extends
ExprMacroTable.BaseScalarMacroFunctionExpr
+ {
+ private Expr estimateExpr;
+ private Expr numStdDev;
+
+ public
HllSketchEstimateWithErrorBoundsExpr(HllSketchEstimateWithErrorBoundsExprMacro
macro, List<Expr> args)
+ {
+ super(macro, args);
+ this.estimateExpr = args.get(0);
+ if (args.size() == 2) {
+ numStdDev = args.get(1);
+ }
+ }
+
+ @Nullable
+ @Override
+ public ExpressionType getOutputType(InputBindingInspector inspector)
+ {
+ return ExpressionType.DOUBLE_ARRAY;
+ }
+
+ @Override
+ public ExprEval eval(ObjectBinding bindings)
+ {
+ int numStdDevs =
HllSketchToEstimateWithBoundsPostAggregator.DEFAULT_NUM_STD_DEVS;
+ ExprEval eval = estimateExpr.eval(bindings);
+ if (numStdDev != null) {
+ numStdDevs = numStdDev.eval(bindings).asInt();
+ }
+
+ final Object valObj = eval.value();
+ if (valObj == null) {
+ return ExprEval.ofDoubleArray(new Double[]{0.0D, 0.0D, 0.0D});
+ }
+ HllSketchHolder sketch = HllSketchHolder.fromObj(valObj);
+ return ExprEval.ofDoubleArray(new Double[]{sketch.getEstimate(),
sketch.getLowerBound(numStdDevs), sketch.getUpperBound(numStdDevs)});
Review Comment:
I'm assuming it doesn't matter that `SketchEstimateWithErrorBounds` ctor
puts the parameter ordering like:
```
estimate
high
low
numstdev
```
##########
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/sql/HllPostAggExprMacros.java:
##########
@@ -24,17 +24,18 @@
import org.apache.druid.math.expr.ExprMacroTable;
import org.apache.druid.math.expr.ExpressionType;
import org.apache.druid.query.aggregation.datasketches.hll.HllSketchHolder;
+import
org.apache.druid.query.aggregation.datasketches.hll.HllSketchToEstimateWithBoundsPostAggregator;
import javax.annotation.Nullable;
import java.util.List;
public class HllPostAggExprMacros
{
public static final String HLL_SKETCH_ESTIMATE = "hll_sketch_estimate";
+ public static final String HLL_SKETCH_ESTIMATE_WITH_ERROR_BOUNDS =
"hll_sketch_estimate_with_error_bounds";
public static class HLLSketchEstimateExprMacro implements
ExprMacroTable.ExprMacro
{
-
Review Comment:
nit: unnecessary whitespace
##########
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/sql/HllPostAggExprMacros.java:
##########
@@ -88,5 +105,44 @@ public ExprEval eval(ObjectBinding bindings)
return round ? ExprEval.of(Math.round(estimate)) : ExprEval.of(estimate);
}
}
+
+ public static class HllSketchEstimateWithErrorBoundsExpr extends
ExprMacroTable.BaseScalarMacroFunctionExpr
+ {
+ private Expr estimateExpr;
Review Comment:
nit: might be worth marking these `estimateExpr` and `numStdDev` final in
both `HllSketchEstimateWithErrorBoundsExpr` and
`ThetaSketchEstimateWithErrorBoundsExpr`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]