Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CIDR function to PPL (#3036) #3110

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

currantw
Copy link

Description

Adds the cidr function to PPL, which returns whether an IP address is within an IP address range specified in CIDR notation.

Related Issues

Resolves #3036.

Check List

  • [Y] New functionality includes testing.
  • [Y] New functionality has been documented.
  • [Y ] New functionality has javadoc added.
  • [Y] New functionality has a user manual doc added.
  • [N/A] API changes companion pull request created.
  • [Y] Commits are signed per the DCO using --signoff.
  • [?] Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

- 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]>
…nge` implementation to return `false` rather than raising an exception.

Signed-off-by: currantw <[email protected]>
Copy link

@jduo jduo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be an integration test as well.
Please check if there is an explain test needed and others such as AstBuiderTest, AstExpressionBuliderTest

@@ -0,0 +1,33 @@
package org.opensearch.sql.utils;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the license header.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Done. ✅


return ExprBooleanValue.of(Arrays.equals(addressBytes, 0, prefixLengthBytes, rangeBytes, 0, prefixLengthBytes));
} catch (Exception e) {
return ExprBooleanValue.of(false);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this return ExprNullValue instead?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually these parameters are inputs from the user PPL (and cannot be field references), is that correct? Would it make sense for the parser to resolve this instead? And throw a syntax exception if it is a bad format?

byte[] rangeBytes = Arrays.copyOfRange(InetAddresses.forString(rangeFields[0]).getAddress(), 0, prefixLengthBytes);

return ExprBooleanValue.of(Arrays.equals(addressBytes, 0, prefixLengthBytes, rangeBytes, 0, prefixLengthBytes));
} catch (Exception e) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should explicitly check that the string is one of the permitted formats rather than throw/catch potential exceptions like index out of bounds exceptions.

@ParameterizedTest(name = "and({0}, {1})")
@MethodSource("binaryPredicateArguments")
public void test_and(Boolean v1, Boolean v2) {
FunctionExpression and = DSL.and(DSL.literal(booleanValue(v1)), DSL.literal(booleanValue(v2)));
assertEquals(BOOLEAN, and.type());
assertEquals(v1 && v2, ExprValueUtils.getBooleanValue(and.valueOf(valueEnv())));
assertEquals(String.format("and(%s, %s)", v1.toString(), v2.toString()), and.toString());
assertEquals(String.format("and(%s, %s)", v1, v2.toString()), and.toString());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the adding of final are good changes, but we should keep changes in this PR specific to the issue you're working on. Let's have the clean-up improvements in a separate Issue and PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I've reverted the cleanup changes to final and toString. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE]PPL Add CIDR IP range command support
2 participants