-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support match function as filter in SQL and PPL (#204)
* supported match in sql and ppl where Signed-off-by: chloe-zh <chloezh1102@gmail.com> * added legacy syntax in new parser Signed-off-by: chloe-zh <chloezh1102@gmail.com> * added integration test Signed-off-by: chloe-zh <chloezh1102@gmail.com> * updated user manual Signed-off-by: chloe-zh <chloezh1102@gmail.com> * updated user manual Signed-off-by: chloe-zh <chloezh1102@gmail.com> * update Signed-off-by: chloe-zh <chloezh1102@gmail.com> * added ppl integ test, updated ppl manual Signed-off-by: chloe-zh <chloezh1102@gmail.com> * update Signed-off-by: chloe-zh <chloezh1102@gmail.com> * update Signed-off-by: chloe-zh <chloezh1102@gmail.com> * update Signed-off-by: chloe-zh <chloezh1102@gmail.com>
- Loading branch information
Showing
29 changed files
with
916 additions
and
3 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
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
48 changes: 48 additions & 0 deletions
48
core/src/main/java/org/opensearch/sql/expression/NamedArgumentExpression.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,48 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.expression; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.env.Environment; | ||
|
||
/** | ||
* Named argument expression that represents function argument with name. | ||
*/ | ||
@RequiredArgsConstructor | ||
@Getter | ||
@EqualsAndHashCode | ||
@ToString | ||
public class NamedArgumentExpression implements Expression { | ||
private final String argName; | ||
private final Expression value; | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
return value.valueOf(valueEnv); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return value.type(); | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(ExpressionNodeVisitor<T, C> visitor, C context) { | ||
return visitor.visitNamedArgument(this, context); | ||
} | ||
} |
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
116 changes: 116 additions & 0 deletions
116
core/src/main/java/org/opensearch/sql/expression/function/OpenSearchFunctions.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,116 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.expression.function; | ||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.ToString; | ||
import lombok.experimental.UtilityClass; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
import org.opensearch.sql.expression.NamedArgumentExpression; | ||
import org.opensearch.sql.expression.env.Environment; | ||
|
||
@UtilityClass | ||
public class OpenSearchFunctions { | ||
public void register(BuiltinFunctionRepository repository) { | ||
repository.register(match()); | ||
} | ||
|
||
private static FunctionResolver match() { | ||
FunctionName funcName = BuiltinFunctionName.MATCH.getName(); | ||
return new FunctionResolver(funcName, | ||
ImmutableMap.<FunctionSignature, FunctionBuilder>builder() | ||
.put(new FunctionSignature(funcName, ImmutableList.of(STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList.of(STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList.of(STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, | ||
STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, | ||
STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, | ||
STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, | ||
STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.put(new FunctionSignature(funcName, ImmutableList | ||
.of(STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, | ||
STRING, STRING, STRING, STRING, STRING)), | ||
args -> new OpenSearchFunction(funcName, args)) | ||
.build()); | ||
} | ||
|
||
private static class OpenSearchFunction extends FunctionExpression { | ||
private final FunctionName functionName; | ||
private final List<Expression> arguments; | ||
|
||
public OpenSearchFunction(FunctionName functionName, List<Expression> arguments) { | ||
super(functionName, arguments); | ||
this.functionName = functionName; | ||
this.arguments = arguments; | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
throw new UnsupportedOperationException(String.format( | ||
"OpenSearch defined function [%s] is only supported in WHERE and HAVING clause.", | ||
functionName)); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return ExprCoreType.BOOLEAN; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
List<String> args = arguments.stream() | ||
.map(arg -> String.format("%s=%s", ((NamedArgumentExpression) arg) | ||
.getArgName(), ((NamedArgumentExpression) arg).getValue().toString())) | ||
.collect(Collectors.toList()); | ||
return String.format("%s(%s)", functionName, String.join(", ", args)); | ||
} | ||
} | ||
} |
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/NamedArgumentExpressionTest.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 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.expression; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
public class NamedArgumentExpressionTest extends ExpressionTestBase { | ||
@Test | ||
void name_an_argument() { | ||
LiteralExpression value = DSL.literal("search"); | ||
NamedArgumentExpression namedArgument = dsl.namedArgument("query", value); | ||
|
||
assertEquals("query", namedArgument.getArgName()); | ||
assertEquals(value.type(), namedArgument.type()); | ||
assertEquals(value.valueOf(valueEnv()), namedArgument.valueOf(valueEnv())); | ||
} | ||
} |
Oops, something went wrong.