-
Notifications
You must be signed in to change notification settings - Fork 208
Add DISTINCT_COUNT_APPROX function #3654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
07a4200
08bbb45
f17dc66
7e211ab
4c41cca
07e29ed
5ebdb41
c8964f4
efe9c48
361d4d2
eb3e37d
56dcb3b
067b322
04d081a
a3e6a54
e9c0cff
9485929
30228c9
b90e7b8
4f1a0d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.udaf; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.tools.RelBuilder; | ||
| import org.opensearch.sql.calcite.CalcitePlanContext; | ||
|
|
||
| @FunctionalInterface | ||
| public interface AggHandler { | ||
| RelBuilder.AggCall apply( | ||
| boolean distinct, RexNode field, List<RexNode> argList, CalcitePlanContext context); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.udaf; | ||
|
|
||
| import static org.opensearch.sql.calcite.utils.CalciteToolsHelper.*; | ||
| import static org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils.TransferUserDefinedAggFunction; | ||
| import static org.opensearch.sql.expression.function.BuiltinFunctionName.*; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.sql.type.ReturnTypes; | ||
| import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils; | ||
| import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
|
|
||
| public class AggTransferFunctionMap { | ||
| public static Map<BuiltinFunctionName, AggHandler> AGG_FUNCTION_MAP; | ||
|
|
||
| static { | ||
| AGG_FUNCTION_MAP = new HashMap<>(); | ||
| AGG_FUNCTION_MAP.put(MAX, (distinct, field, argList, ctx) -> ctx.relBuilder.max(field)); | ||
|
|
||
| AGG_FUNCTION_MAP.put(MIN, (distinct, field, argList, ctx) -> ctx.relBuilder.min(field)); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| AVG, (distinct, field, argList, ctx) -> ctx.relBuilder.avg(distinct, null, field)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using map builder
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| COUNT, | ||
| (distinct, field, argList, ctx) -> | ||
| ctx.relBuilder.count( | ||
| distinct, null, field == null ? ImmutableList.of() : ImmutableList.of(field))); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| SUM, (distinct, field, argList, ctx) -> ctx.relBuilder.sum(distinct, null, field)); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| VARSAMP, | ||
| (distinct, field, argList, ctx) -> ctx.relBuilder.aggregateCall(VAR_SAMP_NULLABLE, field)); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| VARPOP, | ||
| (distinct, field, argList, ctx) -> ctx.relBuilder.aggregateCall(VAR_POP_NULLABLE, field)); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| STDDEV_SAMP, | ||
| (distinct, field, argList, ctx) -> | ||
| ctx.relBuilder.aggregateCall(STDDEV_SAMP_NULLABLE, field)); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| STDDEV_POP, | ||
| (distinct, field, argList, ctx) -> | ||
| ctx.relBuilder.aggregateCall(STDDEV_POP_NULLABLE, field)); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| TAKE, | ||
| (distinct, field, argList, ctx) -> | ||
| TransferUserDefinedAggFunction( | ||
| TakeAggFunction.class, | ||
| "TAKE", | ||
| UserDefinedFunctionUtils.getReturnTypeInferenceForArray(), | ||
| List.of(field), | ||
| argList, | ||
| ctx.relBuilder)); | ||
|
|
||
| AGG_FUNCTION_MAP.put( | ||
| PERCENTILE_APPROX, | ||
| (distinct, field, argList, ctx) -> { | ||
| List<RexNode> newArgList = new ArrayList<>(argList); | ||
| newArgList.add(ctx.rexBuilder.makeFlag(field.getType().getSqlTypeName())); | ||
| return TransferUserDefinedAggFunction( | ||
| PercentileApproxFunction.class, | ||
| "percentile_approx", | ||
| ReturnTypes.ARG0_FORCE_NULLABLE, | ||
| List.of(field), | ||
| newArgList, | ||
| ctx.relBuilder); | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| package org.opensearch.sql.opensearch.executor; | ||
|
|
||
| import static org.opensearch.sql.calcite.utils.OpenSearchTypeFactory.convertRelDataTypeToExprType; | ||
| import static org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils.TransferUserDefinedAggFunction; | ||
| import static org.opensearch.sql.expression.function.BuiltinFunctionName.DISTINCT_COUNT_APPROX; | ||
|
|
||
| import java.security.AccessController; | ||
| import java.security.PrivilegedAction; | ||
|
|
@@ -25,8 +27,11 @@ | |
| import org.apache.calcite.rel.type.RelDataTypeField; | ||
| import org.apache.calcite.runtime.Hook; | ||
| import org.apache.calcite.sql.SqlExplainLevel; | ||
| import org.apache.calcite.sql.type.ReturnTypes; | ||
| import org.opensearch.sql.ast.statement.Explain.ExplainFormat; | ||
| import org.opensearch.sql.calcite.CalcitePlanContext; | ||
| import org.opensearch.sql.calcite.udf.udaf.AggTransferFunctionMap; | ||
| import org.opensearch.sql.calcite.udf.udaf.AggTransferFunctionMap.*; | ||
| import org.opensearch.sql.calcite.utils.CalciteToolsHelper.OpenSearchRelRunners; | ||
| import org.opensearch.sql.common.response.ResponseListener; | ||
| import org.opensearch.sql.data.model.ExprTupleValue; | ||
|
|
@@ -41,6 +46,7 @@ | |
| import org.opensearch.sql.expression.function.PPLFuncImpTable; | ||
| import org.opensearch.sql.opensearch.client.OpenSearchClient; | ||
| import org.opensearch.sql.opensearch.executor.protector.ExecutionProtector; | ||
| import org.opensearch.sql.opensearch.functions.DistinctCountApproxAggFunction; | ||
| import org.opensearch.sql.opensearch.functions.GeoIpFunction; | ||
| import org.opensearch.sql.opensearch.util.JdbcOpenSearchDataTypeConvertor; | ||
| import org.opensearch.sql.planner.physical.PhysicalPlan; | ||
|
|
@@ -246,5 +252,16 @@ private void registerOpenSearchFunctions() { | |
| (builder, args) -> | ||
| builder.makeCall(new GeoIpFunction(client.getNodeClient()).toUDF("GEOIP"), args); | ||
| PPLFuncImpTable.INSTANCE.registerExternalFunction(BuiltinFunctionName.GEOIP, geoIpImpl); | ||
|
|
||
| AggTransferFunctionMap.AGG_FUNCTION_MAP.put( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think can move AGG_FUNCTION_MAP to internal of PPLFuncImpTable.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interface is different. The function's return is a RexNode while aggregation return an Relbuilder.AggCall. But I will put all content inside the PPLFuncImpTable. The code architecture could be better. |
||
| DISTINCT_COUNT_APPROX, | ||
| (distinct, field, argList, ctx) -> | ||
| TransferUserDefinedAggFunction( | ||
| DistinctCountApproxAggFunction.class, | ||
| "APPROX_DISTINCT_COUNT", | ||
| ReturnTypes.BIGINT_FORCE_NULLABLE, | ||
| List.of(field), | ||
| argList, | ||
| ctx.relBuilder)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a java doc here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the doc inside PPLFuncImpTable