walterddr commented on code in PR #9501:
URL: https://github.com/apache/pinot/pull/9501#discussion_r1016983469


##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/IpAddressFunctions.java:
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.pinot.common.function.scalar;
+
+import inet.ipaddr.AddressStringException;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import inet.ipaddr.PrefixLenException;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+/**
+ * Inbuilt IP related transform functions
+ *
+ * Functions added:
+ * isSubnetOf(String ipPrefix, String ipAddress) --> boolean
+ *
+ * Functions to add:
+ * ipPrefix(String ipAddress, int prefixBits) -> String ipPrefix
+ * ipSubnetMin(String ipPrefix) -> String ipMin
+ * ipSubnetMax(String ipPrefix) -> String ipMax
+ */
+public class IpAddressFunctions {
+
+  private IpAddressFunctions() {
+  }
+
+  /**
+   * Validates IP prefix prefixStr and returns IPAddress if validated
+   */
+  private static IPAddress getPrefix(String prefixStr) {
+    IPAddress prefixAddr = getAddress(prefixStr);
+    if (!prefixAddr.isPrefixed()) {
+      throw new IllegalArgumentException("IP Address " + prefixStr + " should 
be prefixed.");
+    }
+    try {
+      return prefixAddr.toPrefixBlock();
+    } catch (PrefixLenException e) {
+      throw e;
+    }
+  }
+
+  /**
+   * Validates IP address ipString and returns IPAddress if validated
+   */
+  private static IPAddress getAddress(String ipString) {
+    try {
+      return new IPAddressString(ipString).toAddress();
+    } catch (AddressStringException e) {
+      throw new IllegalArgumentException("Invalid IP Address format for " + 
ipString);
+    }
+  }
+
+  @ScalarFunction
+  public static boolean isSubnetOf(String ipPrefix, String ipAddress) {
+    IPAddress prefix = getPrefix(ipPrefix);
+    IPAddress ip = getAddress(ipAddress);
+    if (ip.isPrefixed()) {
+      throw new IllegalArgumentException("IP Address " + ipAddress + " should 
not be prefixed.");
+    }
+    return prefix.contains(ip);

Review Comment:
   actually no worries. we can follow up. 
   
   - upon checking some other systems, very little has IPADDRESS built in
   - PostgreSQL throws when IP varchar cast is a malform string 
   - MySQL has a is IP address function to check if a varchar is a IP address 
format. 
   
   so IMO, 
   - for now this impl is good.  
   - we can provide a safety net like MySQL later: `CASE WHEN 
is_ipaddress(col1) AND is_ipaddress(col2) THEN isSubnetOf(col1, col2) ELSE 
false END`



-- 
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]

Reply via email to