Myracle commented on code in PR #27483: URL: https://github.com/apache/flink/pull/27483#discussion_r2957766105
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetAtonFunction.java: ########## @@ -0,0 +1,164 @@ +/* + * 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.data.binary.BinaryStringData; +import org.apache.flink.table.data.binary.BinaryStringDataUtil; +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 (following the C library {@code + * inet_aton} convention): + * + * <ul> + * <li>a — the value is stored directly as a 32-bit address + * <li>a.b — interpreted as a.0.0.b + * <li>a.b.c — interpreted as a.b.0.c + * <li>a.b.c.d — standard dotted-decimal format + * </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>Examples: + * + * <ul> + * <li>INET_ATON('1') returns 1 (single number: direct 32-bit value) + * <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) { + return null; + } + + BinaryStringData binaryStr = (BinaryStringData) ipAddress; + if (BinaryStringDataUtil.isEmpty(binaryStr)) { + return null; + } + + return ipToLong(binaryStr); + } + + /** + * Converts an IPv4 address string to a long value. + * + * <p>Operates directly on UTF-8 bytes from {@link BinaryStringData} to avoid unnecessary String + * object allocation. Since IPv4 addresses contain only ASCII characters ('0'-'9' and '.'), each + * character is exactly one byte in UTF-8 encoding. + * + * <p>Supports MySQL-compatible short-form addresses (following C library {@code inet_aton}): Review Comment: Makes sense — since our implementation only aims for MySQL compatibility and differs from the C library inet_aton behavior (e.g., the single-number range is [0, 255] in MySQL vs [0, 0xFFFFFFFF] in C), the C library reference is misleading. -- 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]
