generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
Add unified function interface with function discovery API #5039
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
Merged
dai-chen
merged 13 commits into
opensearch-project:main
from
dai-chen:add-unified-function-api
Jan 14, 2026
+467
−1
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d47fdcd
Add initial impl for unified function and repository
dai-chen bf995d5
Simplify unified function impl by built-in rex executor
dai-chen de2d5c0
Extract unified function builder abstraction
dai-chen bdd3eb0
Refactor unified function repository internal and javadoc
dai-chen 1e9bb55
Remove type converter and use Calcite's sql type name as unified type…
dai-chen 9d7030b
Update readme
dai-chen 12b6c8b
Refactor unified function repository API and javadoc
dai-chen 9d0aa0c
Refactor unified function calcite adapter to remove rex executable field
dai-chen 88cdbf8
Update readme
dai-chen 4c15604
Polish all javadoc comments
dai-chen de35dbb
Refactor calcite adapter create with utility methods
dai-chen 3488e4d
Address comments from CodeRabbit
dai-chen 82d9e4d
Merge branch 'main' into add-unified-function-api
dai-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
api/src/main/java/org/opensearch/sql/api/function/UnifiedFunction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.api.function; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A unified function abstraction that provides an engine-agnostic way to represent and evaluate | ||
| * functions, enabling functions to be implemented once and used across multiple execution engines | ||
| * without engine-specific code duplication. | ||
| * | ||
| * <p>Note: types are represented as engine-agnostic SQL type name strings (e.g., {@code "VARCHAR"}, | ||
| * {@code "INTEGER"}, {@code "ARRAY<T>"}, {@code "STRUCT<...>"}) to avoid introducing a dedicated | ||
| * {@code UnifiedType} abstraction until it’s needed. | ||
| * | ||
| * @see java.io.Serializable | ||
| */ | ||
| public interface UnifiedFunction extends Serializable { | ||
|
|
||
| /** | ||
| * Returns the name of the function. | ||
| * | ||
| * @return the function name | ||
| */ | ||
| String getFunctionName(); | ||
|
|
||
| /** | ||
| * Returns the unified type names expected for the input arguments. | ||
| * | ||
| * @return list of unified type names for input arguments | ||
| */ | ||
| List<String> getInputTypes(); | ||
|
|
||
| /** | ||
| * Returns the unified type name of the function result. | ||
| * | ||
| * @return unified type name of the function result | ||
| */ | ||
| String getReturnType(); | ||
|
|
||
| /** | ||
| * Evaluates the function with the provided input values. | ||
| * | ||
| * @param inputs argument values evaluated by the caller | ||
| * @return the evaluated result, may be null depending on the function implementation | ||
| */ | ||
| Object eval(List<Object> inputs); | ||
| } |
118 changes: 118 additions & 0 deletions
118
api/src/main/java/org/opensearch/sql/api/function/UnifiedFunctionCalciteAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.api.function; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.ToString; | ||
| import org.apache.calcite.DataContext; | ||
| import org.apache.calcite.DataContexts; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.rex.RexExecutable; | ||
| import org.apache.calcite.rex.RexExecutorImpl; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
| import org.opensearch.sql.expression.function.PPLFuncImpTable; | ||
|
|
||
| /** Adapter that implements {@link UnifiedFunction} using Calcite's {@link RexExecutorImpl}. */ | ||
| @ToString | ||
| @EqualsAndHashCode(exclude = "compiledCode") | ||
| @RequiredArgsConstructor | ||
| public class UnifiedFunctionCalciteAdapter implements UnifiedFunction { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| /** | ||
| * Key used by RexExecutorImpl's InputGetter to retrieve input values from DataContext. This is a | ||
| * Calcite internal convention. | ||
| */ | ||
| private static final String INPUT_RECORD_KEY = "inputRecord"; | ||
|
|
||
| /** Unified function name. */ | ||
| @Getter private final String functionName; | ||
|
|
||
| /** Unified type name of the return value. */ | ||
| @Getter private final String returnType; | ||
|
|
||
| /** Unified type names of the input arguments. */ | ||
| @Getter private final List<String> inputTypes; | ||
|
|
||
| /** | ||
| * Compiled Java source for evaluating the function. | ||
| * | ||
| * <p>The generated code reads inputs from the {@code "inputRecord"} entry in {@link DataContext}. | ||
| * Arguments are mapped to field variables named {@code "_0"}, {@code "_1"}, etc. | ||
| * | ||
| * <pre>{@code | ||
| * // For UPPER(input) function: | ||
| * Object[] inputRecord = (Object[]) dataContext.get("inputRecord"); | ||
| * String _0 = (String) inputRecord[0]; | ||
| * return _0 == null ? null : _0.toUpperCase(); | ||
| * }</pre> | ||
| */ | ||
| private final String compiledCode; | ||
|
|
||
| @Override | ||
| public Object eval(List<Object> inputs) { | ||
| RexExecutable rexExecutor = new RexExecutable(compiledCode, functionName); | ||
| DataContext dataContext = DataContexts.of(Map.of(INPUT_RECORD_KEY, inputs.toArray())); | ||
| rexExecutor.setDataContext(dataContext); | ||
|
|
||
| Object[] results = rexExecutor.execute(); | ||
| return (results == null || results.length == 0) ? null : results[0]; | ||
| } | ||
|
|
||
| /** | ||
| * Creates Calcite RexNode adapter for a unified function. | ||
| * | ||
| * <p>Note: this method pre-compiles the resolved function expression and stores the generated | ||
| * source code as a string. This avoids serializing {@link RexNode} instances and simplifies | ||
| * distribution across execution engines. If performance or security concerns arise, we can change | ||
| * this internal implementation. | ||
| * | ||
| * @param rexBuilder RexBuilder for creating expressions | ||
| * @param functionName function name | ||
| * @param inputTypes function argument types | ||
| * @return configured adapter instance | ||
| */ | ||
| public static UnifiedFunctionCalciteAdapter create( | ||
| RexBuilder rexBuilder, String functionName, List<String> inputTypes) { | ||
| RexNode[] inputRefs = makeInputRefs(rexBuilder, inputTypes); | ||
| RexNode resolved = PPLFuncImpTable.INSTANCE.resolve(rexBuilder, functionName, inputRefs); | ||
| RelDataType inputRowType = buildInputRowType(rexBuilder, inputTypes); | ||
| RexExecutable executable = | ||
| RexExecutorImpl.getExecutable(rexBuilder, List.of(resolved), inputRowType); | ||
| String returnType = resolved.getType().getSqlTypeName().getName(); | ||
|
|
||
| return new UnifiedFunctionCalciteAdapter( | ||
| functionName, returnType, List.copyOf(inputTypes), executable.getSource()); | ||
| } | ||
|
|
||
| private static RelDataType buildInputRowType(RexBuilder rexBuilder, List<String> inputTypes) { | ||
| RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory(); | ||
| RelDataTypeFactory.Builder builder = typeFactory.builder(); | ||
| for (int i = 0; i < inputTypes.size(); i++) { | ||
| RelDataType relType = typeFactory.createSqlType(SqlTypeName.valueOf(inputTypes.get(i))); | ||
| builder.add("_" + i, relType); | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| private static RexNode[] makeInputRefs(RexBuilder rexBuilder, List<String> inputTypes) { | ||
| RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory(); | ||
| RexNode[] inputRefs = new RexNode[inputTypes.size()]; | ||
| for (int i = 0; i < inputTypes.size(); i++) { | ||
| RelDataType relType = typeFactory.createSqlType(SqlTypeName.valueOf(inputTypes.get(i))); | ||
| inputRefs[i] = rexBuilder.makeInputRef(relType, i); | ||
| } | ||
| return inputRefs; | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
api/src/main/java/org/opensearch/sql/api/function/UnifiedFunctionRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.api.function; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.Value; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.sql.validate.SqlUserDefinedFunction; | ||
| import org.opensearch.sql.api.UnifiedQueryContext; | ||
| import org.opensearch.sql.expression.function.PPLBuiltinOperators; | ||
|
|
||
| /** Repository for discovering and loading PPL functions as {@link UnifiedFunction} instances. */ | ||
| @RequiredArgsConstructor | ||
| public class UnifiedFunctionRepository { | ||
|
|
||
| /** Unified query context containing CalcitePlanContext for creating Rex expressions. */ | ||
| private final UnifiedQueryContext context; | ||
|
|
||
| /** | ||
| * Loads all PPL functions from {@link PPLBuiltinOperators} as descriptors. | ||
| * | ||
| * @return list of function descriptors | ||
| */ | ||
| public List<UnifiedFunctionDescriptor> loadFunctions() { | ||
| RexBuilder rexBuilder = context.getPlanContext().rexBuilder; | ||
| return PPLBuiltinOperators.instance().getOperatorList().stream() | ||
| .filter(SqlUserDefinedFunction.class::isInstance) | ||
| .map( | ||
| operator -> { | ||
| String functionName = operator.getName(); | ||
| UnifiedFunctionBuilder builder = | ||
| inputTypes -> | ||
| UnifiedFunctionCalciteAdapter.create(rexBuilder, functionName, inputTypes); | ||
| return new UnifiedFunctionDescriptor(functionName, builder); | ||
| }) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
| * Loads a specific PPL function by name. | ||
| * | ||
| * @param functionName the name of the function to load (case-insensitive) | ||
| * @return optional function descriptor, empty if not found | ||
| */ | ||
| public Optional<UnifiedFunctionDescriptor> loadFunction(String functionName) { | ||
penghuo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return loadFunctions().stream() | ||
| .filter(desc -> desc.getFunctionName().equalsIgnoreCase(functionName)) | ||
| .findFirst(); | ||
| } | ||
|
|
||
| /** Function descriptor with name and builder for creating {@link UnifiedFunction} instances. */ | ||
| @Value | ||
| public static class UnifiedFunctionDescriptor { | ||
| /** The name of the function in upper case. */ | ||
| String functionName; | ||
|
|
||
| /** Builder for creating {@link UnifiedFunction} instances with specific input types. */ | ||
| UnifiedFunctionBuilder builder; | ||
| } | ||
|
|
||
| /** Builder for creating {@link UnifiedFunction} instances with specific input types. */ | ||
| @FunctionalInterface | ||
| public interface UnifiedFunctionBuilder { | ||
|
|
||
| /** | ||
| * Builds a {@link UnifiedFunction} instance for the specified input types. | ||
| * | ||
| * @param inputTypes Unified type names for function arguments | ||
| * @return a UnifiedFunction instance configured for the specified input types | ||
| */ | ||
| UnifiedFunction build(List<String> inputTypes); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.