是否有任何功能可以在雪花中不使用 Parse_IP 将 CIDR 块拆分为 IP 开始和 IP 结束 [英] Is there any function to split CIDR block into IP start and IP end without using Parse_IP in snowflake
问题描述
例如对于 cidr 2c0f:eb00:400::/40 (jw.org) ip_start 应该是 - 2c0f:eb00:400:: ip_end 将是 - 2c0f:eb00:4ff:ffff:ffff:ffff:ffff:噗
我提交了一个内部错误来修复此边缘情况的 PARSE_IP()
内部函数 (SNOW-374145).
同时,您可以使用 Java UDF,例如:
select ipv6range('FF02:0:0:0:0:1:FF00::/104');
返回:
<预><代码>[/ff02:0:0:0:0:1:ff00:0",/ff02:0:0:0:0:1:ffff:ffff"]代码取自 https://github.com/edazdarevic/CIDRUtils:
创建或替换函数ipv6range(s string)返回数组语言 java处理程序='MyClass.x'作为 $$/** 麻省理工学院许可证** 版权所有 (c) 2013 Edin Dazdarevic (edin.dazdarevic@gmail.com)* 特此向任何获得副本的人免费授予许可* 本软件和相关文档文件(软件"),以处理* 在软件中不受限制,包括但不限于权利* 使用、复制、修改、合并、发布、分发、再许可和/或出售* 软件的副本,并允许软件的使用人* 提供这样做,受以下条件的约束:* 以上版权声明和本许可声明应包含在* 本软件的所有副本或重要部分.* 本软件按原样"提供,不提供任何形式的明示或保证* 暗示,包括但不限于适销性保证,* 适合特定用途且不侵权.在任何情况下都不得* 作者或版权持有人应对任何索赔、损害或其他责任* 责任,无论是由于合同、侵权行为还是其他原因引起的,* 与软件或使用或其他交易无关或与之相关* 软件.** */导入 java.math.BigInteger;导入 java.net.InetAddress;导入 java.net.UnknownHostException;导入 java.nio.ByteBuffer;导入 java.util.ArrayList;导入 java.util.List;公共类 MyClass{public static String[] x(String x) 抛出异常{CIDRUtils cidr = new CIDRUtils(x);返回新字符串[]{cidr.startAddress.toString(), cidr.endAddress.toString()};}}/*** 一个能够从 CIDR 规范中获取 IP 范围的类.它支持* IPv4 和 IPv6.*/公共类 CIDRUtils {私人最终字符串cidr;私有 InetAddress inetAddress;InetAddress startAddress;InetAddress endAddress;private final int prefixLength;公共 CIDRUtils(String cidr) 抛出 UnknownHostException {this.cidr = cidr;/* 将 CIDR 拆分为地址和前缀部分 */如果(this.cidr.contains(/")){int index = this.cidr.indexOf("/");String addressPart = this.cidr.substring(0, index);String networkPart = this.cidr.substring(index + 1);inetAddress = InetAddress.getByName(addressPart);prefixLength = Integer.parseInt(networkPart);计算();} 别的 {throw new IllegalArgumentException(不是有效的 CIDR 格式!");}}私有无效计算()抛出 UnknownHostException {ByteBuffer 掩码缓冲区;整数目标大小;如果 (inetAddress.getAddress().length == 4) {掩码缓冲区 =字节缓冲区.分配(4).putInt(-1);目标大小 = 4;} 别的 {maskBuffer = ByteBuffer.allocate(16).putLong(-1L).putLong(-1L);目标大小 = 16;}BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength);ByteBuffer 缓冲区 = ByteBuffer.wrap(inetAddress.getAddress());BigInteger ipVal = new BigInteger(1, buffer.array());BigInteger startIp = ipVal.and(mask);BigInteger endIp = startIp.add(mask.not());byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize);byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize);this.startAddress = InetAddress.getByAddress(startIpArr);this.endAddress = InetAddress.getByAddress(endIpArr);}private byte[] toBytes(byte[] array, int targetSize) {整数计数器 = 0;列表<字节>newArr = new ArrayList();while (counter < targetSize && (array.length - 1 - counter >= 0)) {newArr.add(0, array[array.length - 1 - counter]);计数器++;}int size = newArr.size();for (int i = 0; i < (targetSize - size); i++) {newArr.add(0, (byte) 0);}字节[] ret = 新字节[newArr.size()];for (int i = 0; i < newArr.size(); i++) {ret[i] = newArr.get(i);}返回 ret;}公共字符串 getNetworkAddress() {返回 this.startAddress.getHostAddress();}公共字符串 getBroadcastAddress() {返回 this.endAddress.getHostAddress();}public boolean isInRange(String ipAddress) 抛出 UnknownHostException {InetAddress 地址 = InetAddress.getByName(ipAddress);BigInteger start = new BigInteger(1, this.startAddress.getAddress());BigInteger end = new BigInteger(1, this.endAddress.getAddress());BigInteger target = new BigInteger(1, address.getAddress());int st = start.compareTo(target);int te = target.compareTo(end);返回 (st == -1 || st == 0) &&(te == -1 || te == 0);}}$$;
顺便说一句,PARSE_IP() 显示的错误(直到修复)是解析 IP 时出错:IPv6 地址无效.IP 地址包含太多字段."
for example For cidr 2c0f:eb00:400::/40 (jw.org) ip_start should be - 2c0f:eb00:400:: ip_end will be - 2c0f:eb00:4ff:ffff:ffff:ffff:ffff:ffff
I filed an internal bug to fix the PARSE_IP()
internal function for this edge case (SNOW-374145).
In the meantime, you can use a Java UDF, like:
select ipv6range('FF02:0:0:0:0:1:FF00::/104');
Returns:
[
"/ff02:0:0:0:0:1:ff00:0",
"/ff02:0:0:0:0:1:ffff:ffff"
]
With code taken from https://github.com/edazdarevic/CIDRUtils:
create or replace function ipv6range(s string)
returns array
language java
handler='MyClass.x'
as $$
/*
* The MIT License
*
* Copyright (c) 2013 Edin Dazdarevic (edin.dazdarevic@gmail.com)
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* */
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
public class MyClass{
public static String[] x(String x) throws Exception{
CIDRUtils cidr = new CIDRUtils(x);
return new String[]{cidr.startAddress.toString(), cidr.endAddress.toString()};
}
}
/**
* A class that enables to get an IP range from CIDR specification. It supports
* both IPv4 and IPv6.
*/
public class CIDRUtils {
private final String cidr;
private InetAddress inetAddress;
InetAddress startAddress;
InetAddress endAddress;
private final int prefixLength;
public CIDRUtils(String cidr) throws UnknownHostException {
this.cidr = cidr;
/* split CIDR to address and prefix part */
if (this.cidr.contains("/")) {
int index = this.cidr.indexOf("/");
String addressPart = this.cidr.substring(0, index);
String networkPart = this.cidr.substring(index + 1);
inetAddress = InetAddress.getByName(addressPart);
prefixLength = Integer.parseInt(networkPart);
calculate();
} else {
throw new IllegalArgumentException("not an valid CIDR format!");
}
}
private void calculate() throws UnknownHostException {
ByteBuffer maskBuffer;
int targetSize;
if (inetAddress.getAddress().length == 4) {
maskBuffer =
ByteBuffer
.allocate(4)
.putInt(-1);
targetSize = 4;
} else {
maskBuffer = ByteBuffer.allocate(16)
.putLong(-1L)
.putLong(-1L);
targetSize = 16;
}
BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength);
ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress());
BigInteger ipVal = new BigInteger(1, buffer.array());
BigInteger startIp = ipVal.and(mask);
BigInteger endIp = startIp.add(mask.not());
byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize);
byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize);
this.startAddress = InetAddress.getByAddress(startIpArr);
this.endAddress = InetAddress.getByAddress(endIpArr);
}
private byte[] toBytes(byte[] array, int targetSize) {
int counter = 0;
List<Byte> newArr = new ArrayList<Byte>();
while (counter < targetSize && (array.length - 1 - counter >= 0)) {
newArr.add(0, array[array.length - 1 - counter]);
counter++;
}
int size = newArr.size();
for (int i = 0; i < (targetSize - size); i++) {
newArr.add(0, (byte) 0);
}
byte[] ret = new byte[newArr.size()];
for (int i = 0; i < newArr.size(); i++) {
ret[i] = newArr.get(i);
}
return ret;
}
public String getNetworkAddress() {
return this.startAddress.getHostAddress();
}
public String getBroadcastAddress() {
return this.endAddress.getHostAddress();
}
public boolean isInRange(String ipAddress) throws UnknownHostException {
InetAddress address = InetAddress.getByName(ipAddress);
BigInteger start = new BigInteger(1, this.startAddress.getAddress());
BigInteger end = new BigInteger(1, this.endAddress.getAddress());
BigInteger target = new BigInteger(1, address.getAddress());
int st = start.compareTo(target);
int te = target.compareTo(end);
return (st == -1 || st == 0) && (te == -1 || te == 0);
}
}
$$
;
Btw, the error shown by PARSE_IP() (until fixed) is "Error parsing IP: Invalid IPv6 address. IP address includes too many fields."
这篇关于是否有任何功能可以在雪花中不使用 Parse_IP 将 CIDR 块拆分为 IP 开始和 IP 结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!