Skip to content

Commit

Permalink
Add stub implementation of cidr:
Browse files Browse the repository at this point in the history
- Update Lexer and Parser.
- Add `DSL::cidr` and BuiltinFunctionName.CIDR`.
- Add `BinaryPredicateOperator::cidr`.
- Add `IPUtils` with stub implementation of `isAddressInRange`.

Signed-off-by: currantw <[email protected]>
  • Loading branch information
currantw committed Oct 22, 2024
1 parent e838e46 commit 6e63cc4
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
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 @@ -563,6 +563,10 @@ public static FunctionExpression regexp(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.REGEXP, expressions);
}

public static FunctionExpression cidr(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.CIDR, expressions);
}

public static FunctionExpression concat(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.CONCAT, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public enum BuiltinFunctionName {
/** Text Functions. */
TOSTRING(FunctionName.of("tostring")),

/** IP Functions. */
CIDR(FunctionName.of("cidr")),

/** Arithmetic Operators. */
ADD(FunctionName.of("+")),
ADDFUNCTION(FunctionName.of("add")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.opensearch.sql.expression.function.BuiltinFunctionName;
import org.opensearch.sql.expression.function.BuiltinFunctionRepository;
import org.opensearch.sql.expression.function.DefaultFunctionResolver;
import org.opensearch.sql.utils.IPUtils;
import org.opensearch.sql.utils.OperatorUtils;

/**
Expand Down Expand Up @@ -55,6 +56,7 @@ public static void register(BuiltinFunctionRepository repository) {
repository.register(like());
repository.register(notLike());
repository.register(regexp());
repository.register(cidr());
}

/**
Expand Down Expand Up @@ -118,7 +120,7 @@ public static void register(BuiltinFunctionRepository repository) {
* </tr>
* </table>
*/
private static Table<ExprValue, ExprValue, ExprValue> andTable =
private static final Table<ExprValue, ExprValue, ExprValue> andTable =
new ImmutableTable.Builder<ExprValue, ExprValue, ExprValue>()
.put(LITERAL_TRUE, LITERAL_TRUE, LITERAL_TRUE)
.put(LITERAL_TRUE, LITERAL_FALSE, LITERAL_FALSE)
Expand Down Expand Up @@ -193,7 +195,7 @@ public static void register(BuiltinFunctionRepository repository) {
* </tr>
* </table>
*/
private static Table<ExprValue, ExprValue, ExprValue> orTable =
private static final Table<ExprValue, ExprValue, ExprValue> orTable =
new ImmutableTable.Builder<ExprValue, ExprValue, ExprValue>()
.put(LITERAL_TRUE, LITERAL_TRUE, LITERAL_TRUE)
.put(LITERAL_TRUE, LITERAL_FALSE, LITERAL_TRUE)
Expand Down Expand Up @@ -268,7 +270,7 @@ public static void register(BuiltinFunctionRepository repository) {
* </tr>
* </table>
*/
private static Table<ExprValue, ExprValue, ExprValue> xorTable =
private static final Table<ExprValue, ExprValue, ExprValue> xorTable =
new ImmutableTable.Builder<ExprValue, ExprValue, ExprValue>()
.put(LITERAL_TRUE, LITERAL_TRUE, LITERAL_FALSE)
.put(LITERAL_TRUE, LITERAL_FALSE, LITERAL_TRUE)
Expand Down Expand Up @@ -396,6 +398,12 @@ private static DefaultFunctionResolver regexp() {
impl(nullMissingHandling(OperatorUtils::matchesRegexp), INTEGER, STRING, STRING));
}

private static DefaultFunctionResolver cidr() {
return define(
BuiltinFunctionName.CIDR.getName(),
impl(nullMissingHandling(IPUtils::isAddressInRange), BOOLEAN, STRING, STRING));
}

private static DefaultFunctionResolver notLike() {
return define(
BuiltinFunctionName.NOT_LIKE.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static ExprValue exprIfNull(ExprValue v1, ExprValue v2) {
*
* @param v1 varable 1
* @param v2 varable 2
* @return null if v1 equls to v2
* @return null if v1 equals to v2
*/
public static ExprValue exprNullIf(ExprValue v1, ExprValue v2) {
return v1.equals(v2) ? LITERAL_NULL : v1;
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/org/opensearch/sql/utils/IPUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.opensearch.sql.utils;

import org.opensearch.sql.data.model.ExprBooleanValue;
import org.opensearch.sql.data.model.ExprValue;

public class IPUtils {

/**
* Returns whether the given IP address is within the specified IP address range.
* Supports both IPv4 and IPv6 addresses.
*
* @param address IP address (e.g. "198.51.100.14" or "2001:0db8::ff00:42:8329").
* @param range IP address range in CIDR notation (e.g. "198.51.100.0/24" or "2001:0db8:/32")
* @return true value if IP address is in range; else false value.
*/
public static ExprBooleanValue isAddressInRange(ExprValue address, ExprValue range) {

// TODO - implementation
return ExprBooleanValue.of(true);
}
}
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ CAST: 'CAST';
LIKE: 'LIKE';
ISNULL: 'ISNULL';
ISNOTNULL: 'ISNOTNULL';
CIDR: 'CIDR';

// FLOWCONTROL FUNCTIONS
IFNULL: 'IFNULL';
Expand Down
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ conditionFunctionName
: LIKE
| ISNULL
| ISNOTNULL
| CIDR
;

// flow control function return non-boolean value
Expand Down

0 comments on commit 6e63cc4

Please sign in to comment.