forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable
concat()
string function to support multiple string arguments (
#200) * Refactor concat() to support multiple string arguments Signed-off-by: Margarit Hakobyan <[email protected]>
- Loading branch information
1 parent
c6a59f7
commit 1f924f5
Showing
15 changed files
with
399 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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.