Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2929801427


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetAtonFunction.java:
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+
+import javax.annotation.Nullable;
+
+/**
+ * Implementation of {@link BuiltInFunctionDefinitions#INET_ATON}.
+ *
+ * <p>This function converts an IPv4 address string to its numeric 
representation. It follows the
+ * MySQL INET_ATON function behavior, including support for short-form IPv4 
addresses.
+ *
+ * <p>The conversion formula for a standard IP address A.B.C.D is: A * 256^3 + 
B * 256^2 + C * 256 +
+ * D
+ *
+ * <p>MySQL-compatible short-form IPv4 addresses are supported:
+ *
+ * <ul>
+ *   <li>a.b is interpreted as a.0.0.b
+ *   <li>a.b.c is interpreted as a.b.0.c
+ * </ul>
+ *
+ * <p>Leading zeros in octets are parsed as decimal (consistent with MySQL), 
not octal.
+ *
+ * <p>Note: This function only supports IPv4 addresses. IPv6 addresses are not 
supported.
+ *
+ * <p><b>Implementation Note:</b> This implementation does not use utility 
classes such as {@code
+ * com.google.common.net.InetAddresses} or {@code sun.net.util.IPAddressUtil} 
because:
+ *
+ * <ul>
+ *   <li>Guava's {@code InetAddresses.forString()} does not support 
MySQL-compatible short-form IP
+ *       addresses (e.g., "127.1" interpreted as "127.0.0.1")
+ *   <li>Standard IP parsers may interpret leading zeros as octal (e.g., "010" 
as 8), while MySQL
+ *       treats them as decimal (e.g., "010" as 10)
+ *   <li>{@code sun.net.util.IPAddressUtil} is a JDK internal API requiring 
{@code --add-exports},
+ *       which introduces JDK version compatibility issues
+ * </ul>
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ *   <li>INET_ATON('127.0.0.1') returns 2130706433
+ *   <li>INET_ATON('127.1') returns 2130706433 (short-form: 127.0.0.1)
+ *   <li>INET_ATON('127.0.1') returns 2130706433 (short-form: 127.0.0.1)
+ *   <li>INET_ATON('10.0.0.1') returns 167772161
+ *   <li>INET_ATON('0.0.0.0') returns 0
+ * </ul>
+ */
+@Internal
+public class InetAtonFunction extends BuiltInScalarFunction {
+
+    public InetAtonFunction(SpecializedFunction.SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.INET_ATON, context);
+    }
+
+    /**
+     * Converts an IPv4 address string to its numeric representation.
+     *
+     * @param ipAddress the IPv4 address string in dotted-decimal notation 
(supports short-form)
+     * @return the numeric representation of the IP address, or null if input 
is null or invalid
+     */
+    public @Nullable Long eval(@Nullable StringData ipAddress) {
+        if (ipAddress == null) {

Review Comment:
   Thanks for the great suggestion!
   
   Agreed on both points. I've refactored InetAtonFunction to operate entirely 
on the BinaryStringData API without converting to Java String:
   
   Empty check: Replaced ipAddress.toString().isEmpty() with 
BinaryStringDataUtil.isEmpty((BinaryStringData) ipAddress) to avoid unnecessary 
materialization.
   
   Byte-level parsing: Rewrote ipToLong() to accept BinaryStringData directly 
and use byteAt() / getSizeInBytes() for parsing. Since IPv4 addresses only 
contain ASCII characters ('0'-'9' and '.'), each character is exactly one byte 
in UTF-8, making byte-level access a natural fit. This eliminates the String 
object allocation entirely.
   
   This follows the same pattern used by other scalar functions in the codebase 
(e.g., StartsWithFunction, BTrimFunction).



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

Reply via email to