clintropolis commented on code in PR #15212: URL: https://github.com/apache/druid/pull/15212#discussion_r1412657738
########## docs/querying/sql-scalar.md: ########## @@ -200,11 +200,14 @@ For the IPv4 address functions, the `address` argument can either be an IPv4 dot (e.g., "192.168.0.1") or an IP address represented as an integer (e.g., 3232235521). The `subnet` argument should be a string formatted as an IPv4 address subnet in CIDR notation (e.g., "192.168.0.0/16"). +For the IPv6 address function, the `address` argument accepts a semicolon separated string (e.g. "75e9:efa4:29c6:85f6::232c"). The format of the `subnet` argument should be an IPv6 address subnet in CIDR notation (e.g. "75e9:efa4:29c6:85f6::/64"). + |Function|Notes| |---|---| |`IPV4_MATCH(address, subnet)`|Returns true if the `address` belongs to the `subnet` literal, else false. If `address` is not a valid IPv4 address, then false is returned. This function is more efficient if `address` is an integer instead of a string.| |`IPV4_PARSE(address)`|Parses `address` into an IPv4 address stored as an integer . If `address` is an integer that is a valid IPv4 address, then it is passed through. Returns null if `address` cannot be represented as an IPv4 address.| |`IPV4_STRINGIFY(address)`|Converts `address` into an IPv4 address dotted-decimal string. If `address` is a string that is a valid IPv4 address, then it is passed through. Returns null if `address` cannot be represented as an IPv4 address.| +| ipv6_match(address, subnet) | Returns 1 if the IPv6 `address` belongs to the `subnet` literal, else 0. If `address` is not a valid IPv6 address, then 0 is returned.| Review Comment: nit ```suggestion | IPV6_MATCH(address, subnet) | Returns 1 if the IPv6 `address` belongs to the `subnet` literal, else 0. If `address` is not a valid IPv6 address, then 0 is returned.| ``` ########## sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/IPv6AddressMatchOperatorConversion.java: ########## @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.expression.builtin; + +import org.apache.calcite.sql.SqlFunction; +import org.apache.calcite.sql.SqlFunctionCategory; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.type.OperandTypes; +import org.apache.calcite.sql.type.ReturnTypes; +import org.apache.calcite.sql.type.SqlSingleOperandTypeChecker; +import org.apache.calcite.sql.type.SqlTypeFamily; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.query.expression.IPv6AddressMatchExprMacro; +import org.apache.druid.sql.calcite.expression.DirectOperatorConversion; +import org.apache.druid.sql.calcite.expression.OperatorConversions; + +public class IPv6AddressMatchOperatorConversion extends DirectOperatorConversion +{ + private static final SqlSingleOperandTypeChecker ADDRESS_OPERAND = OperandTypes.or( + OperandTypes.family(SqlTypeFamily.STRING) + ); + + private static final SqlSingleOperandTypeChecker SUBNET_OPERAND = OperandTypes.family(SqlTypeFamily.STRING); + + private static final SqlFunction SQL_FUNCTION = OperatorConversions + .operatorBuilder(StringUtils.toUpperCase(IPv6AddressMatchExprMacro.FN_NAME)) + .operandTypeChecker( + OperandTypes.sequence( + "'" + StringUtils.toUpperCase(IPv6AddressMatchExprMacro.FN_NAME) + "(expr, string)'", + ADDRESS_OPERAND, + SUBNET_OPERAND + ) + ) Review Comment: nit: formatting seems off here ########## processing/src/main/java/org/apache/druid/query/expression/IPv6AddressMatchExprMacro.java: ########## @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.query.expression; + +import inet.ipaddr.IPAddressString; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.math.expr.Expr; +import org.apache.druid.math.expr.ExprEval; +import org.apache.druid.math.expr.ExprMacroTable; +import org.apache.druid.math.expr.ExpressionType; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +/** + * <pre> + * Implements an expression that checks if an IPv6 address belongs to a subnet. + * + * Expression signatures: + * - long ipv6_match(string address, string subnet) + * + * Valid "address" argument formats are: + * - IPv6 address string (e.g., "2001:4860:4860::8888") + * + * The argument format for the "subnet" argument should be a literal in CIDR notation + * (e.g., "2001:db8::/64 "). + * + * If the "address" argument does not represent an IPv6 address then false is returned. + * </pre> + * +*/ +public class IPv6AddressMatchExprMacro implements ExprMacroTable.ExprMacro +{ + public static final String FN_NAME = "IPV6_MATCH"; Review Comment: i suppose it doesn't really matter since native expression function names are forced to lowercase when registering, but just for consistency with other macro definitions this should probably be lowercase too -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
