-
Notifications
You must be signed in to change notification settings - Fork 190
Add udf interface #3374
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
Add udf interface #3374
Changes from 1 commit
4cbf2ca
89207a1
28869cf
d1f8f11
2464c85
df6728a
958cac9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,34 @@ | |
|
|
||
| package org.opensearch.sql.calcite.udf; | ||
|
|
||
| public interface UserDefinedAggFunction<S extends Accumulator> { | ||
| public interface UserDefinedAggFunction<S extends UserDefinedAggFunction.Accumulator> { | ||
| /** | ||
| * @return {@link Accumulator} | ||
| */ | ||
| S init(); | ||
|
|
||
| /** | ||
| * | ||
| * @param {@link Accumulator} | ||
| * @return final result | ||
| */ | ||
| Object result(S accumulator); | ||
|
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. in UDF, the method name is
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. For the function name of agg function, it's defined in calcite here https://github.com/apache/calcite/blob/1793ba79a328c61fb42842f443334cc1353c985f/core/src/main/java/org/apache/calcite/schema/impl/AggregateFunctionImpl.java#L91. We cannot modify them. I will left comment. |
||
|
|
||
| // Add values to the accumulator | ||
| /** | ||
| * Add values to the accumulator. Notice some init argument will also be here like the 50 in Percentile(field, 50). | ||
| * @param acc {@link Accumulator} | ||
| * @param values the value to add to accumulator | ||
| * @return {@link Accumulator} | ||
| */ | ||
| S add(S acc, Object... values); | ||
|
|
||
| /** | ||
| * Maintain the state when {@link UserDefinedAggFunction} add values | ||
| */ | ||
| interface Accumulator { | ||
| /** | ||
| * @return the final aggregation value | ||
|
||
| */ | ||
| Object value(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.udaf; | ||
|
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. copyright header missing
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. |
||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.opensearch.sql.calcite.udf.Accumulator; | ||
|
|
||
| import org.opensearch.sql.calcite.udf.UserDefinedAggFunction; | ||
|
|
||
| public class TakeAggFunction implements UserDefinedAggFunction<TakeAggFunction.TakeAccumulator> { | ||
|
|
@@ -14,7 +19,7 @@ public TakeAccumulator init() { | |
|
|
||
| @Override | ||
| public Object result(TakeAccumulator accumulator) { | ||
| return accumulator.result(); | ||
| return accumulator.value(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -40,7 +45,7 @@ public TakeAccumulator() { | |
| } | ||
|
|
||
| @Override | ||
| public Object result() { | ||
| public Object value() { | ||
| return hits; | ||
| } | ||
|
Comment on lines
47
to
49
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. Code indentation problem, please run
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. |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,16 @@ static SqlOperator translate(String op) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Translates function arguments to align with Calcite's expectations, ensuring compatibility | ||
| * with PPL (Piped Processing Language). This is necessary because Calcite's input argument | ||
| * order or default values may differ from PPL's function definitions. | ||
| * | ||
| * @param op The function name as a string. | ||
| * @param argList A list of {@link RexNode} representing the parsed arguments from the PPL statement. | ||
| * @param context The {@link CalcitePlanContext} providing necessary utilities such as {@code rexBuilder}. | ||
| * @return A modified list of {@link RexNode} that correctly maps to Calcite’s function expectations. | ||
| */ | ||
| static List<RexNode> translateArgument( | ||
|
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. Since this is a framework work, we will change this method frequently, could you add some comments to explain this method for developers
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. Sure, already added. |
||
| String op, List<RexNode> argList, CalcitePlanContext context) { | ||
| switch (op.toUpperCase(Locale.ROOT)) { | ||
|
|
||
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.
format issue
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.
Done.