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

feat: new inet functions #12575

Merged
merged 3 commits into from
Jul 2, 2024
Merged

feat: new inet functions #12575

merged 3 commits into from
Jul 2, 2024

Conversation

panga
Copy link
Contributor

@panga panga commented Jun 17, 2024

Currently, INET extension is missing functions for INET range operations, related issue #10947.
However, this PR aims to add the subnet contains operations and other helper functions.

netmask Computes the network mask for the address's network.

netmask(inet '192.168.1.5/24') → 255.255.255.0

network Returns the network part of the address, zeroing out whatever is to the right of the netmask.

network(inet '192.168.1.5/24') → 192.168.1.0/24

broadcast Computes the broadcast address for the address's network.

broadcast(inet '192.168.1.5/24') → 192.168.1.255/24

<<= Is subnet contained by or equal to subnet?

inet '192.168.1.5/32' <<= inet '192.168.1.0/24' → true

>>= Does subnet contain or equal subnet?

inet '192.168.1.5/32' >>= inet '192.168.1.0/24' → false

Note: I'm not familiar with duckdb code and it is my first contribution.

@duckdb-draftbot duckdb-draftbot marked this pull request as draft June 18, 2024 11:40
@panga panga marked this pull request as ready for review June 18, 2024 11:40
@duckdb-draftbot duckdb-draftbot marked this pull request as draft June 18, 2024 15:11
@panga panga marked this pull request as ready for review June 18, 2024 15:11
@Mytherin Mytherin requested a review from chrisiou June 19, 2024 12:51
Copy link
Contributor

@chrisiou chrisiou left a comment

Choose a reason for hiding this comment

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

hey @panga, thanks a lot for opening this PR! Everything looks good.
Could you please include some non-happy path test cases like more complex scenarios or failure cases?
To ensure comprehensive testing coverage and prevent issues from future changes, it'd be nice to cover the following scenarios across all the new functions such as:

  • subnet mask scenarios:
    • a single host mask (/32 for IPv4, /128 for IPv6)
    • a subnet mask covering all addresses (/0 for both IPv4 and IPv6)
    • invalid mask values (e.g. NULL input, or bigger number than 32)
  • some edge cases:
    • 192.168.1.255/24 or 2001:0db8::/96
    • 0.0.0.0/19 or ::42
  • invalid address input values or invalid mask lengths
    • 999.999.999.999/24, the existence of characters or NULL values

To test unexpected/incorrect usage, you can use:

statement error
<statement>
----
<error_msg>

SELECT netmask(INET '192.168.1.5/16')
----
255.255.0.0/16

Copy link
Contributor

Choose a reason for hiding this comment

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

GenericExecutor::ExecuteUnary applies a unary operation to each element in args.data[0]. Please also include a statement to test this but for many inputs.
Something like:

SELECT * FROM ( VALUES (netmask('10.10.10.1/12')), (broadcast('10.10.10.2/8')), (network('10.10.10.3')));

Copy link
Contributor

@chrisiou chrisiou Jun 21, 2024

Choose a reason for hiding this comment

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

You can also use the above if you want to group some of the test scenarios I mentioned before, or you can create a table with different inet values and then test the functionality on them (it'd also be nice to have a test on tables).
e.g.

CREATE TABLE ips(ip INET);
INSERT INTO ips VALUES('0.255.0.1/24'::INET), ('192.168.32.2/12'::INET), ('10.10.10.3/4'::INET);
SELECT netmask(ip) FROM ips;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The requested new test cases were added.

@@ -30,6 +30,39 @@ SELECT family('::ffff:127.0.0.1/17')
----
6

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please also include the above scenarios for the IPv6?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

query IIII
SELECT tbl.id, tbl2.id, i, j FROM tbl JOIN tbl2 ON (i=j) ORDER BY tbl.id
----
1 3 127.0.0.1 127.0.0.1
5 3 127.0.0.1 127.0.0.1

# subset
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please include some test cases that you check (for both operators):

  • equal subnets
  • one subnet which fully contains another
  • partial overlap
  • different prefix lengths, but the same network address
  • edge case: 0.0.0.0/0 >>= 192.168.1.0/0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

5 3 ::1 ::1

# subset
Copy link
Contributor

Choose a reason for hiding this comment

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

To ensure thorough testing, please also include here similar testing as in the ipv4 test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@chrisiou chrisiou added the Needs Documentation Use for issues or PRs that require changes in the documentation label Jun 21, 2024
@panga panga requested a review from chrisiou June 24, 2024 13:20
@duckdb-draftbot duckdb-draftbot marked this pull request as draft June 24, 2024 13:20
@panga panga marked this pull request as ready for review June 24, 2024 13:21
@panga
Copy link
Contributor Author

panga commented Jun 24, 2024

@chrisiou thank you so much for the review. I've added the requested test cases.
Please note the new functions rely on INET datatype and thus only operates on valid IP v4/v6.
You can find the type conversion tests with invalid ip/mask in the test_inet_type.test files.

Copy link
Contributor

@chrisiou chrisiou left a comment

Choose a reason for hiding this comment

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

Looks good to me, thank you @panga!

@Mytherin Mytherin merged commit 0be3e7b into duckdb:main Jul 2, 2024
42 checks passed
@Mytherin
Copy link
Collaborator

Mytherin commented Jul 2, 2024

Thanks!

github-actions bot pushed a commit to duckdb/duckdb-r that referenced this pull request Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Documentation Use for issues or PRs that require changes in the documentation Ready To Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants