Last active
August 17, 2022 03:37
-
-
Save kurochan/87ca09b21bbb7e2141d88416d7963834 to your computer and use it in GitHub Desktop.
Example of Generate IP Address list from IPv4 prefilx list on Snowflake
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
with parse_ip as ( | |
select | |
1 as k, | |
column1, | |
parse_ip(column1, 'INET'): ipv4 as ipv4, | |
power(2, 32 - parse_ip(column1, 'INET'): netmask_prefix_length) as subnet_size | |
from (values ('192.168.1.0/28'), ('172.16.1.0/29')) | |
), | |
ip_generated as ( | |
select | |
(ip.ipv4 + gen.idx) as ipv4 | |
from ( | |
select | |
1 as k, | |
row_number() over (order by seq4()) -1 as idx | |
from | |
table(generator(timelimit => 3, rowcount => 10000)) | |
) gen | |
left join parse_ip ip | |
where | |
gen.idx < ip.subnet_size | |
), | |
ip_human_readable as ( | |
select | |
bitshiftright(bitand(ip.ipv4, 4278190080), 24) oct_1, | |
bitshiftright(bitand(ip.ipv4, 16711680), 16) oct_2, | |
bitshiftright(bitand(ip.ipv4, 65280), 8) oct_3, | |
bitshiftright(bitand(ip.ipv4, 255), 0) oct_4 | |
from ip_generated ip | |
) | |
select | |
concat_ws('.', ip.oct_1, ip.oct_2, ip.oct_3, ip.oct_4) as ipv4 | |
from ip_human_readable ip | |
order by ip.oct_1, ip.oct_2, ip.oct_3, ip.oct_4 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment