Skip to content

Commit

Permalink
Add position() to V2 engine (#177)
Browse files Browse the repository at this point in the history
Signed-off-by: Margarit Hakobyan <[email protected]>

Rbased
  • Loading branch information
margarit-h committed Dec 5, 2022
1 parent 3786f04 commit fc1bccf
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.opensearch.sql.ast.expression.Literal;
import org.opensearch.sql.ast.expression.Not;
import org.opensearch.sql.ast.expression.Or;
import org.opensearch.sql.ast.expression.PositionFunction;
import org.opensearch.sql.ast.expression.QualifiedName;
import org.opensearch.sql.ast.expression.RelevanceFieldList;
import org.opensearch.sql.ast.expression.Span;
Expand Down Expand Up @@ -201,6 +202,13 @@ public Expression visitHighlightFunction(HighlightFunction node, AnalysisContext
return new HighlightExpression(expr);
}

@Override
public Expression visitPositionFunction(PositionFunction node, AnalysisContext context) {
Expression stringPatternExpr = node.getStringPatternExpr().accept(this, context);
Expression searchStringExpr = node.getSearchStringExpr().accept(this, context);
return DSL.position(stringPatternExpr, searchStringExpr);
}

@Override
public Expression visitIn(In node, AnalysisContext context) {
return visitIn(node.getField(), node.getValueList(), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.opensearch.sql.ast.expression.Map;
import org.opensearch.sql.ast.expression.Not;
import org.opensearch.sql.ast.expression.Or;
import org.opensearch.sql.ast.expression.PositionFunction;
import org.opensearch.sql.ast.expression.QualifiedName;
import org.opensearch.sql.ast.expression.RelevanceFieldList;
import org.opensearch.sql.ast.expression.Span;
Expand Down Expand Up @@ -273,6 +274,10 @@ public T visitHighlightFunction(HighlightFunction node, C context) {
return visitChildren(node, context);
}

public T visitPositionFunction(PositionFunction node, C context) {
return visitChildren(node, context);
}

public T visitStatement(Statement node, C context) {
return visit(node, context);
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.opensearch.sql.ast.expression.Not;
import org.opensearch.sql.ast.expression.Or;
import org.opensearch.sql.ast.expression.ParseMethod;
import org.opensearch.sql.ast.expression.PositionFunction;
import org.opensearch.sql.ast.expression.QualifiedName;
import org.opensearch.sql.ast.expression.Span;
import org.opensearch.sql.ast.expression.SpanUnit;
Expand Down Expand Up @@ -283,6 +284,11 @@ public UnresolvedExpression highlight(UnresolvedExpression fieldName,
return new HighlightFunction(fieldName, arguments);
}

public UnresolvedExpression position(UnresolvedExpression stringPatternExpr,
UnresolvedExpression searchStringExpr) {
return new PositionFunction(stringPatternExpr, searchStringExpr);
}

public UnresolvedExpression window(UnresolvedExpression function,
List<UnresolvedExpression> partitionByList,
List<Pair<SortOption, UnresolvedExpression>> sortList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.ast.expression;

import java.util.Arrays;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.opensearch.sql.ast.AbstractNodeVisitor;


/**
* Expression node of Position function.
*/
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Getter
@ToString
public class PositionFunction extends UnresolvedExpression {
@Getter
private UnresolvedExpression stringPatternExpr;
@Getter
private UnresolvedExpression searchStringExpr;

@Override
public List<UnresolvedExpression> getChild() {
return Arrays.asList(stringPatternExpr, searchStringExpr);
}

@Override
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) {
return nodeVisitor.visitPositionFunction(this, context);
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public static FunctionExpression position(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.POSITION, expressions);
}

public static FunctionExpression position(Expression... expressions) {
return compile(BuiltinFunctionName.POSITION, expressions);
}

public static FunctionExpression truncate(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.TRUNCATE, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.opensearch.sql.ast.expression.Alias;
import org.opensearch.sql.ast.expression.HighlightFunction;
import org.opensearch.sql.ast.expression.Literal;
import org.opensearch.sql.ast.expression.PositionFunction;
import org.opensearch.sql.expression.NamedExpression;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -48,4 +49,14 @@ void visit_highlight() {
NamedExpression analyze = analyzer.analyze(alias, analysisContext);
assertEquals("highlight(fieldA)", analyze.getNameOrAlias());
}

@Test
void visit_position() {
Alias alias = AstDSL.alias("position(fieldA IN fieldB)",
new PositionFunction(AstDSL.stringLiteral("fieldA"), AstDSL.stringLiteral("fieldB")));
NamedExpressionAnalyzer analyzer = new NamedExpressionAnalyzer(expressionAnalyzer);

NamedExpression analyze = analyzer.analyze(alias, analysisContext);
assertEquals("position(fieldA IN fieldB)", analyze.getNameOrAlias());
}
}
6 changes: 3 additions & 3 deletions sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ highlightFunction
: HIGHLIGHT LR_BRACKET relevanceField (COMMA highlightArg)* RR_BRACKET
;

positionFunction
: POSITION LR_BRACKET functionArg IN functionArg RR_BRACKET
;
positionFunction
: POSITION LR_BRACKET functionArg IN functionArg RR_BRACKET
;

scalarFunctionName
: mathematicalFunctionName
Expand Down

0 comments on commit fc1bccf

Please sign in to comment.