forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
concat() support multiple string arguments
#200
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
margarit-h
merged 12 commits into
integ-concat-support-many-args
from
dev-concat-support-many-args
Jan 13, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8a9db78
Refactor concat() to support multiple string arguments
margarit-h 042ef75
Allow not less than 2 args
margarit-h b9ca7d3
Address a PR review comment
margarit-h 96f583d
Address a PR review comment
margarit-h a93640c
Refactoring
margarit-h 8e3cd32
Added unit and integration tests
margarit-h 3d4e690
Add doctests
margarit-h 1702ae7
Add correctness tests, fix checkstyle
margarit-h 95167cc
Edit docs
margarit-h c67b9f2
Add another unit test
margarit-h 40ba368
Edited a javadoc
margarit-h 82acb60
Address PR review feedback
margarit-h 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
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
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
22 changes: 22 additions & 0 deletions
22
core/src/main/java/org/opensearch/sql/expression/function/SerializableVarargsFunction.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,22 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
|
|
||
| package org.opensearch.sql.expression.function; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * Serializable Varargs Function. | ||
| */ | ||
| public interface SerializableVarargsFunction<T, R> extends Serializable { | ||
| /** | ||
| * Applies this function to the given arguments. | ||
| * | ||
| * @param t the function argument | ||
| * @return the function result | ||
| */ | ||
| R apply(T... t); | ||
| } |
69 changes: 69 additions & 0 deletions
69
core/src/main/java/org/opensearch/sql/expression/function/VarargsFunctionResolver.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,69 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function; | ||
|
|
||
| import java.util.AbstractMap; | ||
| import java.util.Map; | ||
| import java.util.PriorityQueue; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.Singular; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.opensearch.sql.exception.ExpressionEvaluationException; | ||
|
|
||
| /** | ||
| * The Function Resolver hold the overload {@link FunctionBuilder} implementation. | ||
| * is composed by {@link FunctionName} which identified the function name | ||
| * and a map of {@link FunctionSignature} and {@link FunctionBuilder} | ||
| * to represent the overloaded implementation | ||
| */ | ||
| @Builder | ||
| @RequiredArgsConstructor | ||
| public class VarargsFunctionResolver implements FunctionResolver { | ||
| @Getter | ||
| private final FunctionName functionName; | ||
| @Singular("functionBundle") | ||
| private final Map<FunctionSignature, FunctionBuilder> functionBundle; | ||
|
|
||
| /** | ||
| * Resolve the {@link FunctionBuilder} by using input {@link FunctionSignature}. | ||
| * If the {@link FunctionBuilder} exactly match the input {@link FunctionSignature}, return it. | ||
| * If applying the widening rule, found the most match one, return it. | ||
| * If nothing found, throw {@link ExpressionEvaluationException} | ||
| * | ||
| * @return function signature and its builder | ||
| */ | ||
| @Override | ||
| public Pair<FunctionSignature, FunctionBuilder> resolve(FunctionSignature unresolvedSignature) { | ||
| PriorityQueue<Map.Entry<Integer, FunctionSignature>> functionMatchQueue = new PriorityQueue<>( | ||
| Map.Entry.comparingByKey()); | ||
|
|
||
| for (FunctionSignature functionSignature : functionBundle.keySet()) { | ||
| functionMatchQueue.add( | ||
| new AbstractMap.SimpleEntry<>(unresolvedSignature.match(functionSignature), | ||
| functionSignature)); | ||
| } | ||
| Map.Entry<Integer, FunctionSignature> bestMatchEntry = functionMatchQueue.peek(); | ||
| if (unresolvedSignature.getParamTypeList().isEmpty()) { | ||
| throw new ExpressionEvaluationException( | ||
| String.format("%s function expected %s, but get %s", functionName, | ||
| formatFunctions(functionBundle.keySet()), | ||
| unresolvedSignature.formatTypes() | ||
| )); | ||
| } else { | ||
| FunctionSignature resolvedSignature = bestMatchEntry.getValue(); | ||
| return Pair.of(resolvedSignature, functionBundle.get(resolvedSignature)); | ||
| } | ||
| } | ||
|
|
||
| private String formatFunctions(Set<FunctionSignature> functionSignatures) { | ||
| return functionSignatures.stream().map(FunctionSignature::formatTypes) | ||
| .collect(Collectors.joining(",", "{", "}")); | ||
| } | ||
| } |
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
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
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
32 changes: 32 additions & 0 deletions
32
core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplVarargsTest.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,32 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function; | ||
|
|
||
| import static org.opensearch.sql.expression.function.FunctionDSL.impl; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.opensearch.sql.expression.DSL; | ||
| import org.opensearch.sql.expression.Expression; | ||
|
|
||
| class FunctionDSLimplVarargsTest extends FunctionDSLimplTestBase { | ||
|
|
||
| @Override | ||
| SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> | ||
| getImplementationGenerator() { | ||
| return impl(varrgs, ANY_TYPE, ANY_TYPE, true); | ||
| } | ||
|
|
||
| @Override | ||
| List<Expression> getSampleArguments() { | ||
| return List.of(DSL.literal(ANY)); | ||
| } | ||
|
|
||
| @Override | ||
| String getExpected_toString() { | ||
| return "sample(ANY)"; | ||
| } | ||
| } |
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.
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.
sourceTypes twice?
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.
As
concat()has variable number of args and targetTypes will always be 0 (as the function signature is not fixed), failure will occur. So, in this case sourceTypes are passed instead of targetTypes to address that.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.
@acarbonetto,
see #200 (comment)
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.
I think a code comment is required to make things clear