jtuglu1 commented on code in PR #18885: URL: https://github.com/apache/druid/pull/18885#discussion_r2666541579
########## extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/sql/SpectatorHistogramPercentileSqlAggregator.java: ########## @@ -0,0 +1,276 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.spectator.histogram.sql; + +import com.google.common.collect.ImmutableList; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlAggFunction; +import org.apache.calcite.sql.SqlFunctionCategory; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlOperatorBinding; +import org.apache.calcite.sql.type.OperandTypes; +import org.apache.calcite.sql.type.SqlReturnTypeInference; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.Optionality; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.query.aggregation.post.FieldAccessPostAggregator; +import org.apache.druid.spectator.histogram.SpectatorHistogramAggregatorFactory; +import org.apache.druid.spectator.histogram.SpectatorHistogramPercentilePostAggregator; +import org.apache.druid.spectator.histogram.SpectatorHistogramPercentilesPostAggregator; +import org.apache.druid.sql.calcite.aggregation.Aggregation; +import org.apache.druid.sql.calcite.aggregation.Aggregations; +import org.apache.druid.sql.calcite.aggregation.SqlAggregator; +import org.apache.druid.sql.calcite.expression.DruidExpression; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.rel.InputAccessor; +import org.apache.druid.sql.calcite.rel.VirtualColumnRegistry; + +import javax.annotation.Nullable; +import java.util.List; + +/** + * SQL aggregator for computing percentiles from SpectatorHistograms. + * <p> + * Supports two forms: + * - SPECTATOR_PERCENTILE(column, percentile) -> DOUBLE (single percentile) + * - SPECTATOR_PERCENTILE(column, ARRAY[p1, p2, ...]) -> DOUBLE ARRAY (multiple percentiles) + * <p> + * Percentile values should be in the range [0, 100] (e.g., 95 for p95). + */ +public class SpectatorHistogramPercentileSqlAggregator implements SqlAggregator +{ + private static final SqlAggFunction FUNCTION_INSTANCE = new SpectatorHistogramPercentileSqlAggFunction(); + private static final String NAME = "SPECTATOR_PERCENTILE"; + + @Override + public SqlAggFunction calciteFunction() + { + return FUNCTION_INSTANCE; + } + + @Nullable + @Override + public Aggregation toDruidAggregation( + final PlannerContext plannerContext, + final VirtualColumnRegistry virtualColumnRegistry, + final String name, + final AggregateCall aggregateCall, + final InputAccessor inputAccessor, + final List<Aggregation> existingAggregations, + final boolean finalizeAggregations + ) + { + final DruidExpression input = Aggregations.toDruidExpressionForNumericAggregator( + plannerContext, + inputAccessor.getInputRowSignature(), + inputAccessor.getField(aggregateCall.getArgList().get(0)) + ); + if (input == null) { + return null; + } + + final RexNode percentileArg = inputAccessor.getField(aggregateCall.getArgList().get(1)); + + // Check if percentile argument is an array or a single value + if (percentileArg.isA(SqlKind.ARRAY_VALUE_CONSTRUCTOR)) { + return handleArrayPercentiles( + virtualColumnRegistry, + name, + input, + percentileArg, + existingAggregations + ); + } else if (percentileArg.isA(SqlKind.LITERAL)) { + return handleSinglePercentile( + virtualColumnRegistry, + name, + input, + percentileArg, + existingAggregations + ); + } + + // Cannot handle non-literal percentile arguments + return null; + } + + private Aggregation handleSinglePercentile( + final VirtualColumnRegistry virtualColumnRegistry, + final String name, + final DruidExpression input, + final RexNode percentileArg, + final List<Aggregation> existingAggregations + ) + { + final double percentile = ((Number) RexLiteral.value(percentileArg)).doubleValue(); Review Comment: It will throw an (admittedly ugly) exception: ``` Error: UNCATEGORIZED (ADMIN) class org.apache.calcite.util.NlsString cannot be cast to class java.lang.Number (org.apache.calcite.util.NlsString is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap') ``` I followed what similar custom functions were doing (there are no checks). -- 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]
