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.
Add CIDR function to PPL (opensearch-project#3036) (opensearch-projec…
…t#3110) Signed-off-by: currantw <[email protected]>
- Loading branch information
Showing
42 changed files
with
623 additions
and
254 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
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
299 changes: 151 additions & 148 deletions
299
...expression/datetime/DateTimeFunction.java → ...xpression/datetime/DateTimeFunctions.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
105 changes: 105 additions & 0 deletions
105
core/src/main/java/org/opensearch/sql/expression/ip/IPFunctions.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,105 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.ip; | ||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.BOOLEAN; | ||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.define; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.impl; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling; | ||
|
||
import inet.ipaddr.AddressStringException; | ||
import inet.ipaddr.IPAddressString; | ||
import inet.ipaddr.IPAddressStringParameters; | ||
import lombok.experimental.UtilityClass; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.model.ExprValueUtils; | ||
import org.opensearch.sql.exception.SemanticCheckException; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionRepository; | ||
import org.opensearch.sql.expression.function.DefaultFunctionResolver; | ||
|
||
/** Utility class that defines and registers IP functions. */ | ||
@UtilityClass | ||
public class IPFunctions { | ||
|
||
public void register(BuiltinFunctionRepository repository) { | ||
repository.register(cidrmatch()); | ||
} | ||
|
||
private DefaultFunctionResolver cidrmatch() { | ||
|
||
// TODO #3145: Add support for IP address data type. | ||
return define( | ||
BuiltinFunctionName.CIDRMATCH.getName(), | ||
impl(nullMissingHandling(IPFunctions::exprCidrMatch), BOOLEAN, STRING, STRING)); | ||
} | ||
|
||
/** | ||
* Returns whether the given IP address is within the specified inclusive CIDR IP address range. | ||
* Supports both IPv4 and IPv6 addresses. | ||
* | ||
* @param addressExprValue IP address as a string (e.g. "198.51.100.14" or | ||
* "2001:0db8::ff00:42:8329"). | ||
* @param rangeExprValue IP address range in CIDR notation as a string (e.g. "198.51.100.0/24" or | ||
* "2001:0db8::/32") | ||
* @return true if the address is in the range; otherwise false. | ||
* @throws SemanticCheckException if the address or range is not valid, or if they do not use the | ||
* same version (IPv4 or IPv6). | ||
*/ | ||
private ExprValue exprCidrMatch(ExprValue addressExprValue, ExprValue rangeExprValue) { | ||
|
||
// TODO #3145: Update to support IP address data type. | ||
String addressString = addressExprValue.stringValue(); | ||
String rangeString = rangeExprValue.stringValue(); | ||
|
||
final IPAddressStringParameters validationOptions = | ||
new IPAddressStringParameters.Builder() | ||
.allowEmpty(false) | ||
.setEmptyAsLoopback(false) | ||
.allow_inet_aton(false) | ||
.allowSingleSegment(false) | ||
.toParams(); | ||
|
||
// Get and validate IP address. | ||
IPAddressString address = | ||
new IPAddressString(addressExprValue.stringValue(), validationOptions); | ||
|
||
try { | ||
address.validate(); | ||
} catch (AddressStringException e) { | ||
String msg = | ||
String.format( | ||
"IP address '%s' is not valid. Error details: %s", addressString, e.getMessage()); | ||
throw new SemanticCheckException(msg, e); | ||
} | ||
|
||
// Get and validate CIDR IP address range. | ||
IPAddressString range = new IPAddressString(rangeExprValue.stringValue(), validationOptions); | ||
|
||
try { | ||
range.validate(); | ||
} catch (AddressStringException e) { | ||
String msg = | ||
String.format( | ||
"CIDR IP address range '%s' is not valid. Error details: %s", | ||
rangeString, e.getMessage()); | ||
throw new SemanticCheckException(msg, e); | ||
} | ||
|
||
// Address and range must use the same IP version (IPv4 or IPv6). | ||
if (address.isIPv4() ^ range.isIPv4()) { | ||
String msg = | ||
String.format( | ||
"IP address '%s' and CIDR IP address range '%s' are not compatible. Both must be" | ||
+ " either IPv4 or IPv6.", | ||
addressString, rangeString); | ||
throw new SemanticCheckException(msg); | ||
} | ||
|
||
return ExprValueUtils.booleanValue(range.contains(address)); | ||
} | ||
} |
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
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
Oops, something went wrong.