Skip to content

Commit

Permalink
Implement between
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>
  • Loading branch information
salyh committed Oct 16, 2024
1 parent 8461ff9 commit 04c30b7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ LIKE: 'LIKE';
ISNULL: 'ISNULL';
ISNOTNULL: 'ISNOTNULL';
ISPRESENT: 'ISPRESENT';
BETWEEN: 'BETWEEN';

// FLOWCONTROL FUNCTIONS
IFNULL: 'IFNULL';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ logicalExpression
| left = logicalExpression OR right = logicalExpression # logicalOr
| left = logicalExpression XOR right = logicalExpression # logicalXor
| booleanExpression # booleanExpr
| expr1 = functionArg NOT? BETWEEN expr2 = functionArg AND expr3 = functionArg # between
;

comparisonExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.apache.spark.sql.catalyst.expressions.Descending$;
import org.apache.spark.sql.catalyst.expressions.Exists$;
import org.apache.spark.sql.catalyst.expressions.Expression;
import org.apache.spark.sql.catalyst.expressions.GreaterThanOrEqual;
import org.apache.spark.sql.catalyst.expressions.InSubquery$;
import org.apache.spark.sql.catalyst.expressions.LessThanOrEqual;
import org.apache.spark.sql.catalyst.expressions.ListQuery$;
import org.apache.spark.sql.catalyst.expressions.NamedExpression;
import org.apache.spark.sql.catalyst.expressions.Predicate;
Expand All @@ -34,6 +36,7 @@
import org.opensearch.sql.ast.expression.AllFields;
import org.opensearch.sql.ast.expression.And;
import org.opensearch.sql.ast.expression.Argument;
import org.opensearch.sql.ast.expression.Between;
import org.opensearch.sql.ast.expression.BinaryExpression;
import org.opensearch.sql.ast.expression.Case;
import org.opensearch.sql.ast.expression.Compare;
Expand Down Expand Up @@ -829,5 +832,14 @@ public Expression visitExistsSubquery(ExistsSubquery node, CatalystPlanContext c
Option.empty());
return context.getNamedParseExpressions().push(existsSubQuery);
}

@Override
public Expression visitBetween(Between node, CatalystPlanContext context) {
Expression value = analyze(node.getValue(), context);
Expression lower = analyze(node.getLowerBound(), context);
Expression upper = analyze(node.getUpperBound(), context);
context.retainAllNamedParseExpressions(p -> p);
return context.getNamedParseExpressions().push(new org.apache.spark.sql.catalyst.expressions.And(new GreaterThanOrEqual(value, lower), new LessThanOrEqual(value, upper)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.opensearch.sql.ast.expression.AllFields;
import org.opensearch.sql.ast.expression.And;
import org.opensearch.sql.ast.expression.Argument;
import org.opensearch.sql.ast.expression.Between;
import org.opensearch.sql.ast.expression.Case;
import org.opensearch.sql.ast.expression.Compare;
import org.opensearch.sql.ast.expression.DataType;
Expand Down Expand Up @@ -269,6 +270,12 @@ public UnresolvedExpression visitConvertedDataType(OpenSearchPPLParser.Converted
return new Literal(ctx.getText(), DataType.STRING);
}

@Override
public UnresolvedExpression visitBetween(OpenSearchPPLParser.BetweenContext ctx) {
UnresolvedExpression betweenExpr = new Between(visit(ctx.expr1),visit(ctx.expr2),visit(ctx.expr3));
return ctx.NOT() != null ? new Not(betweenExpr) : betweenExpr;
}

private Function buildFunction(
String functionName, List<OpenSearchPPLParser.FunctionArgContext> args) {
return new Function(
Expand Down

0 comments on commit 04c30b7

Please sign in to comment.