Skip to content

Instantly share code, notes, and snippets.

@coderholic
Created March 18, 2021 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderholic/426ac304b4639e0520afc6432d5cebcd to your computer and use it in GitHub Desktop.
Save coderholic/426ac304b4639e0520afc6432d5cebcd to your computer and use it in GitHub Desktop.
IPinfo.io Snowflake UDFs
create or replace secure function IPINFO.PUBLIC.to_join_key(ip text) returns integer as
\$\$
AS_INTEGER(PARSE_IP(CONCAT(ip, '/16'), 'inet'):ipv4_range_start)
\$\$
;
create or replace secure function IPINFO.PUBLIC.to_int(ip text) returns integer as
\$\$
AS_INTEGER(PARSE_IP(ip, 'inet'):ipv4)
\$\$
;
CREATE OR REPLACE SECURE FUNCTION IPINFO.PUBLIC.to_ip(ip integer) RETURNS text AS
\$\$
(BITAND(BITSHIFTRIGHT(ip,24),255)::text || '.'
|| BITAND(BITSHIFTRIGHT(ip,16),255)::text || '.'
|| BITAND(BITSHIFTRIGHT(ip,8),255)::text || '.'
|| BITAND(ip,255)::text)
\$\$
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment