Skip to content

Instantly share code, notes, and snippets.

@rg443a
Created June 7, 2024 20:30
Show Gist options
  • Save rg443a/6ae8b251b16c0d1c1f17339ad2bb20b2 to your computer and use it in GitHub Desktop.
Save rg443a/6ae8b251b16c0d1c1f17339ad2bb20b2 to your computer and use it in GitHub Desktop.
duckdb inet_aton function
CREATE OR REPLACE FUNCTION inet_aton(ip) AS (
cast(
split_part(ip, '.', 1)::UINTEGER * 16777216 +
split_part(ip, '.', 2)::UTINYINT * 65536 +
split_part(ip, '.', 3)::UTINYINT * 256 +
split_part(ip, '.', 4)::UTINYINT as UINTEGER)
);
-- select inet_aton('255.255.255.252');
@rg443a
Copy link
Author

rg443a commented Oct 26, 2024

CREATE OR REPLACE macro inet_ntoa (ip) as concat_ws('.', (ip >> 24) & 255 , (ip >> 16) & 255 , (ip >> 8) & 255 , ip & 255);

@rg443a
Copy link
Author

rg443a commented Oct 26, 2024

select #5,(#5>>24 & 255) || '.' || (#5>>16 & 255) || '.' || (#5>>8 & 255) || '.' || (#5 & 255) ip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment