You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "Myracle (via GitHub)" <gi...@apache.org> on 2026/01/28 08:25:12 UTC
[PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Myracle opened a new pull request, #27483:
URL: https://github.com/apache/flink/pull/27483
## What is the purpose of the change
This pull request adds two new MySQL-compatible built-in SQL functions INET_ATON and INET_NTOA for IP address conversion. INET_ATON converts an IPv4 address string to its numeric representation, while INET_NTOA performs the reverse conversion. These functions are commonly used in network data analysis and are compatible with MySQL's INET_ATON/INET_NTOA functions, including support for short-form IPv4 addresses (e.g., a.b → a.0.0.b).
## Brief change log
- *Added InetAtonFunction to convert IPv4 address strings to their numeric (BIGINT) representation*
- *Added InetNtoaFunction to convert numeric values back to IPv4 address strings*
- *Added function definitions in BuiltInFunctionDefinitions for both functions*
- *Registered the functions in BuiltInFunctions for Table API and SQL access*
- *Added inetAton() and inetNtoa() methods to ApiExpressionUtils for Table API support*
- *Supports MySQL-compatible short-form addresses (a.b, a.b.c) and treats leading zeros as decimal*
## Verifying this change
This change added tests and can be verified as follows:
- *Added InetFunctionsITCase integration test class covering:*
- *Standard IPv4 address conversion (e.g., 127.0.0.1 ↔ 2130706433)*
- *Short-form IPv4 addresses (e.g., 10.1, 192.168.1)*
- *Leading zeros handling as decimal values*
- *Boundary cases including 0.0.0.0, 255.255.255.255, and max BIGINT values*
- *NULL input handling*
- *Invalid IP address format handling*
- *INT and BIGINT input types for INET_NTOA*
- *Negative integer boundary test (e.g., -1 as unsigned int)*
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): (no)
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
- The serializers: (no)
- The runtime per-record code paths (performance sensitive): (no)
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
- The S3 file system connector: (no)
## Documentation
- Does this pull request introduce a new feature? (yes)
- If yes, how is the feature documented? (JavaDocs)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2958899905
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java:
##########
@@ -1419,6 +1421,61 @@ public OutType urlEncode() {
return toApiSpecificExpression(unresolvedCall(URL_ENCODE, toExpr()));
}
+ /**
+ * Converts an IPv4 address string to its numeric representation. This function follows MySQL
+ * INET_ATON behavior.
+ *
+ * <p>The conversion formula is: A * 256^3 + B * 256^2 + C * 256 + D for an IP address A.B.C.D
+ *
+ * <p>MySQL-compatible short-form IPv4 addresses are supported:
+ *
+ * <ul>
+ * <li>a — the value is stored directly as an address (value must be in [0, 255])
+ * <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 (not octal), consistent with MySQL.
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ * <li>INET_ATON('1') returns 1 (single number)
+ * <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('0.0.0.0') returns 0
+ * </ul>
+ *
+ * @return the numeric representation of the IP address, or null if the input is null or invalid
+ */
+ public OutType inetAton() {
+ return toApiSpecificExpression(unresolvedCall(INET_ATON, toExpr()));
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation back to its string format. This function
+ * follows MySQL INET_NTOA behavior.
Review Comment:
nit: The Python API doc for `inet_ntoa` has already removed the MySQL reference per our earlier discussion, but this line still says "follows MySQL INET_NTOA behavior". Let's keep them consistent — either remove it here as well, or be more specific (e.g., mention the `-1 → NULL` behavior).
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetAtonFunction.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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:
+ *
+ * <ul>
+ * <li>a — the value is stored directly as an address (value must be in [0, 255])
+ * <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)
+ * <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:
+ *
+ * <ul>
+ * <li>a — direct value (value must be in [0, 255])
+ * <li>a.b — a.0.0.b (each part must be in [0, 255])
+ * <li>a.b.c — a.b.0.c (each part must be in [0, 255])
+ * <li>a.b.c.d — standard format (each part must be in [0, 255])
+ * </ul>
+ *
+ * <p>Leading zeros are treated as decimal (not octal), consistent with MySQL behavior.
+ *
+ * @param ip the IPv4 address as BinaryStringData
+ * @return the long value, or null if the IP address is invalid
+ */
+ private static @Nullable Long ipToLong(BinaryStringData ip) {
+ int len = ip.getSizeInBytes();
+ int partCount = 0;
+ int partStart = 0;
+ long[] parts = new long[4];
+
+ for (int i = 0; i <= len; i++) {
+ if (i == len || ip.byteAt(i) == '.') {
+ if (partCount >= 4) {
+ return null;
+ }
+ if (partStart == i) {
+ return null;
+ }
+
+ // Parse number manually from bytes
+ long value = 0;
+ for (int j = partStart; j < i; j++) {
+ byte b = ip.byteAt(j);
+ if (b < '0' || b > '9') {
+ return null;
+ }
+ value = value * 10 + (b - '0');
+ if (value > 0xFFFFFFFFL) {
Review Comment:
Since every part must be in `[0, 255]` regardless of the short-form variant, we can check `value > 255` here in the loop directly and simplify the switch below — no need for repeated boundary checks in each case branch.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #27483:
URL: https://github.com/apache/flink/pull/27483#issuecomment-3809817880
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "293dc710f328693998f4a46f0c9f9404ea3a5d50",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "293dc710f328693998f4a46f0c9f9404ea3a5d50",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 293dc710f328693998f4a46f0c9f9404ea3a5d50 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on PR #27483:
URL: https://github.com/apache/flink/pull/27483#issuecomment-3889151917
@lincoln-lil Thanks for the suggestion! I evaluated using com.google.common.net.InetAddresses and sun.net.util.IPAddressUtil, but decided to keep the custom implementation for the following reasons:
1. MySQL short-form IP compatibility: Guava's InetAddresses.forString() does not support MySQL-compatible short-form IP addresses (e.g., "127.1" interpreted as "127.0.0.1").
2. Leading zeros handling: Standard IP parsers may interpret leading zeros as octal (e.g., "010" → 8), while MySQL treats them as decimal ("010" → 10). Our implementation follows MySQL behavior.
3. JDK internal API concerns: As you mentioned, sun.net.util.IPAddressUtil requires --add-exports, which introduces JDK version compatibility issues.
I've added detailed Implementation Note in the Javadoc for both InetAtonFunction and InetNtoaFunction to document these design decisions for future maintainers.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2929811561
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetNtoaFunction.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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_NTOA}.
+ *
+ * <p>This function converts a numeric IPv4 address representation back to its string format. It
+ * follows the MySQL INET_NTOA function behavior.
+ *
+ * <p>The conversion extracts each octet from the numeric value using bit shifting and masking.
+ *
+ * <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>The conversion from number to IP string is straightforward and does not benefit from
+ * external libraries
+ * <li>Using a custom implementation avoids unnecessary dependencies and object allocations (e.g.,
+ * creating {@code InetAddress} objects)
+ * <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_NTOA(2130706433) returns '127.0.0.1'
+ * <li>INET_NTOA(167772161) returns '10.0.0.1'
+ * <li>INET_NTOA(0) returns '0.0.0.0'
+ * </ul>
+ */
+@Internal
+public class InetNtoaFunction extends BuiltInScalarFunction {
+
+ private static final long MAX_IPV4_VALUE = 0xFFFFFFFFL;
+ private static final int OCTET_MASK = 0xFF;
+ private static final int BITS_PER_OCTET = 8;
+
+ public InetNtoaFunction(SpecializedFunction.SpecializedContext context) {
+ super(BuiltInFunctionDefinitions.INET_NTOA, context);
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation (Long) to its string format.
+ *
+ * @param ipNumber the numeric representation of the IPv4 address
+ * @return the IPv4 address string in dotted-decimal notation, or null if input is null or out
+ * of valid range
+ */
+ public @Nullable StringData eval(@Nullable Long ipNumber) {
+ if (ipNumber == null) {
+ return null;
+ }
+ return convertToIp(ipNumber);
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation (Integer) to its string format.
+ *
+ * <p>This overload handles INT type inputs which Flink may pass directly without implicit
+ * conversion to Long.
+ *
+ * @param ipNumber the numeric representation of the IPv4 address
+ * @return the IPv4 address string in dotted-decimal notation, or null if input is null or out
+ * of valid range
+ */
+ public @Nullable StringData eval(@Nullable Integer ipNumber) {
+ if (ipNumber == null) {
+ return null;
+ }
+ // Convert to long, treating negative integers as unsigned
+ // For example, -1 (0xFFFFFFFF as signed int) should be treated as 4294967295
+ return convertToIp(Integer.toUnsignedLong(ipNumber));
+ }
+
+ /**
+ * Internal conversion method.
+ *
+ * @param ipNumber the numeric representation
+ * @return the IPv4 address string, or null if out of valid range
+ */
+ private @Nullable StringData convertToIp(long ipNumber) {
+ // Check if the number is within valid IPv4 range [0, 4294967295]
+ if (ipNumber < 0 || ipNumber > MAX_IPV4_VALUE) {
+ return null;
+ }
+
+ return StringData.fromString(longToIp(ipNumber));
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation to its string format.
+ *
+ * @param ipNumber the numeric representation
+ * @return the IPv4 address string
+ */
+ private static String longToIp(long ipNumber) {
Review Comment:
greed on both points. I've simplified the code:
Merged convertToIp and longToIp into a single longToIp method that handles both the range validation and the conversion. The intermediate convertToIp method was just a thin wrapper, so merging them removes unnecessary indirection.
Inlined the static constants — MAX_IPV4_VALUE (0xFFFFFFFFL), OCTET_MASK (0xFF), and BITS_PER_OCTET (8) are indeed self-explanatory in this context (standard IPv4 bit manipulation), so they don't need named constants.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "lincoln-lil (via GitHub)" <gi...@apache.org>.
lincoln-lil merged PR #27483:
URL: https://github.com/apache/flink/pull/27483
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on PR #27483:
URL: https://github.com/apache/flink/pull/27483#issuecomment-3976508182
@flinkbot run azure
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2958899911
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetAtonFunction.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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:
+ *
+ * <ul>
+ * <li>a — the value is stored directly as an address (value must be in [0, 255])
+ * <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)
+ * <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:
+ *
+ * <ul>
+ * <li>a — direct value (value must be in [0, 255])
+ * <li>a.b — a.0.0.b (each part must be in [0, 255])
+ * <li>a.b.c — a.b.0.c (each part must be in [0, 255])
+ * <li>a.b.c.d — standard format (each part must be in [0, 255])
+ * </ul>
+ *
+ * <p>Leading zeros are treated as decimal (not octal), consistent with MySQL behavior.
+ *
+ * @param ip the IPv4 address as BinaryStringData
+ * @return the long value, or null if the IP address is invalid
+ */
+ private static @Nullable Long ipToLong(BinaryStringData ip) {
+ int len = ip.getSizeInBytes();
+ int partCount = 0;
+ int partStart = 0;
+ long[] parts = new long[4];
+
+ for (int i = 0; i <= len; i++) {
+ if (i == len || ip.byteAt(i) == '.') {
+ if (partCount >= 4) {
+ return null;
+ }
+ if (partStart == i) {
+ return null;
+ }
+
+ // Parse number manually from bytes
+ long value = 0;
+ for (int j = partStart; j < i; j++) {
+ byte b = ip.byteAt(j);
+ if (b < '0' || b > '9') {
+ return null;
+ }
+ value = value * 10 + (b - '0');
+ if (value > 0xFFFFFFFFL) {
Review Comment:
Since every part must be in `[0, 255]` regardless of the short-form variant, we can check `value > 255` here in the loop directly and simplify the switch below — no need for repeated boundary checks in each case branch.
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java:
##########
@@ -1419,6 +1421,61 @@ public OutType urlEncode() {
return toApiSpecificExpression(unresolvedCall(URL_ENCODE, toExpr()));
}
+ /**
+ * Converts an IPv4 address string to its numeric representation. This function follows MySQL
+ * INET_ATON behavior.
+ *
+ * <p>The conversion formula is: A * 256^3 + B * 256^2 + C * 256 + D for an IP address A.B.C.D
+ *
+ * <p>MySQL-compatible short-form IPv4 addresses are supported:
+ *
+ * <ul>
+ * <li>a — the value is stored directly as an address (value must be in [0, 255])
+ * <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 (not octal), consistent with MySQL.
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ * <li>INET_ATON('1') returns 1 (single number)
+ * <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('0.0.0.0') returns 0
+ * </ul>
+ *
+ * @return the numeric representation of the IP address, or null if the input is null or invalid
+ */
+ public OutType inetAton() {
+ return toApiSpecificExpression(unresolvedCall(INET_ATON, toExpr()));
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation back to its string format. This function
+ * follows MySQL INET_NTOA behavior.
Review Comment:
nit: The Python API doc for `inet_ntoa` has already removed the MySQL reference per our earlier discussion, but this line still says "follows MySQL INET_NTOA behavior". Let's keep them consistent — either remove it here as well, or be more specific (e.g., mention the `-1 → NULL` behavior).
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "lincoln-lil (via GitHub)" <gi...@apache.org>.
lincoln-lil commented on PR #27483:
URL: https://github.com/apache/flink/pull/27483#issuecomment-3888530821
From an implementation perspective, can we directly leverage existing utility classes for the conversion, such as `com.google.common.net.InetAddresses` or `sun.net.util.IPAddressUtil`? (From a JDK version standpoint we need `add-exports` for this, so it might not be the preferred option.)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2958926319
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetAtonFunction.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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:
+ *
+ * <ul>
+ * <li>a — the value is stored directly as an address (value must be in [0, 255])
+ * <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)
+ * <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:
+ *
+ * <ul>
+ * <li>a — direct value (value must be in [0, 255])
+ * <li>a.b — a.0.0.b (each part must be in [0, 255])
+ * <li>a.b.c — a.b.0.c (each part must be in [0, 255])
+ * <li>a.b.c.d — standard format (each part must be in [0, 255])
+ * </ul>
+ *
+ * <p>Leading zeros are treated as decimal (not octal), consistent with MySQL behavior.
+ *
+ * @param ip the IPv4 address as BinaryStringData
+ * @return the long value, or null if the IP address is invalid
+ */
+ private static @Nullable Long ipToLong(BinaryStringData ip) {
+ int len = ip.getSizeInBytes();
+ int partCount = 0;
+ int partStart = 0;
+ long[] parts = new long[4];
+
+ for (int i = 0; i <= len; i++) {
+ if (i == len || ip.byteAt(i) == '.') {
+ if (partCount >= 4) {
+ return null;
+ }
+ if (partStart == i) {
+ return null;
+ }
+
+ // Parse number manually from bytes
+ long value = 0;
+ for (int j = partStart; j < i; j++) {
+ byte b = ip.byteAt(j);
+ if (b < '0' || b > '9') {
+ return null;
+ }
+ value = value * 10 + (b - '0');
+ if (value > 0xFFFFFFFFL) {
+ return null;
+ }
+ }
+ parts[partCount++] = value;
+ partStart = i + 1;
+ }
+ }
+
+ switch (partCount) {
+ case 1:
+ // Single number: direct value (must be in [0, 255])
+ return parts[0] <= 255 ? parts[0] : null;
Review Comment:
nit: We can move all the checking to line 133.
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java:
##########
@@ -1419,6 +1421,55 @@ public OutType urlEncode() {
return toApiSpecificExpression(unresolvedCall(URL_ENCODE, toExpr()));
}
+ /**
+ * Converts an IPv4 address string to its numeric representation. This function follows MySQL
+ * INET_ATON behavior.
+ *
+ * <p>The conversion formula is: A * 256^3 + B * 256^2 + C * 256 + D for an IP address A.B.C.D
+ *
+ * <p>Supports MySQL-compatible short-form addresses:
+ *
+ * <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 (not octal), consistent with MySQL.
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ * <li>INET_ATON('127.0.0.1') returns 2130706433
+ * <li>INET_ATON('127.1') returns 2130706433 (short-form)
+ * <li>INET_ATON('0.0.0.0') returns 0
+ * </ul>
+ *
+ * @return the numeric representation of the IP address, or null if the input is null or invalid
+ */
+ public OutType inetAton() {
+ return toApiSpecificExpression(unresolvedCall(INET_ATON, toExpr()));
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation back to its string format. This function
+ * follows MySQL INET_NTOA behavior.
Review Comment:
nit: You forget to remove the phrase `This function follows MySQL INET_NTOA behavior.`.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2929947577
##########
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) {
+ return null;
+ }
+
+ final String ip = ipAddress.toString();
+ if (ip.isEmpty()) {
+ return null;
+ }
+
+ return ipToLong(ip);
+ }
+
+ /**
+ * Converts an IPv4 address string to a long value.
+ *
+ * <p>Supports MySQL-compatible short-form addresses:
+ *
+ * <ul>
+ * <li>a.b -> a.0.0.b
+ * <li>a.b.c -> a.b.0.c
+ * <li>a.b.c.d -> standard format
+ * </ul>
+ *
+ * <p>Leading zeros are treated as decimal (not octal), consistent with MySQL behavior.
+ *
+ * @param ip the IPv4 address string
+ * @return the long value, or null if the IP address is invalid
+ */
+ private static @Nullable Long ipToLong(String ip) {
+ int len = ip.length();
+ int octetCount = 0;
+ int octetStart = 0;
+ long[] octets = new long[4];
+
+ for (int i = 0; i <= len; i++) {
+ if (i == len || ip.charAt(i) == '.') {
+ if (octetCount >= 4) {
+ return null;
+ }
+ if (octetStart == i) {
+ return null;
+ }
+
+ // Parse number manually
+ long octet = 0;
+ for (int j = octetStart; j < i; j++) {
+ char c = ip.charAt(j);
+ if (c < '0' || c > '9') {
+ return null;
+ }
+ octet = octet * 10 + (c - '0');
+ if (octet > 255) {
+ return null;
+ }
+ }
+ octets[octetCount++] = octet;
+ octetStart = i + 1;
+ }
+ }
+
+ if (octetCount < 2) {
+ return null;
+ }
Review Comment:
> Thanks for pointing this out!
>
> You're right — I double-checked and MySQL does support single-number addresses. Under the hood, MySQL's `INET_ATON` relies on the C library `inet_aton(3)`, which follows these rules for short-form addresses:
>
> | Parts | Format | Interpretation |
> |-------|--------|---------------|
> | 1 | `a` | Direct 32-bit value |
> | 2 | `a.b` | `a.0.0.b` |
> | 3 | `a.b.c` | `a.b.0.c` |
> | 4 | `a.b.c.d` | Standard dotted-decimal |
>
> So `INET_ATON('1')` returns `1` in MySQL (treating the input as a direct 32-bit address value). My previous implementation rejected single-number inputs with the `if (octetCount < 2)` guard.
>
> I've made the following changes:
>
> 1. **Added `case 1` to the switch in `ipToLong()`** — single-number inputs are now accepted as direct 32-bit values (must be in `[0, 0xFFFFFFFF]`). The per-part overflow check in the parsing loop was also raised from `255` to `0xFFFFFFFFL` to accommodate this.
>
> 2. **Added `inetAtonSingleNumberTestCases()`** — tests `'1'` → `1`, `'0'` → `0`, `'255'` → `255`, `'167772161'` → `167772161` (equivalent to `10.0.0.1`), `'4294967295'` → `4294967295` (max), and `'4294967296'` → `null` (overflow).
>
> 3. **Moved `'1'` out of invalid input test cases** — it was previously listed as an invalid single-octet input; now it's a valid single-number address.
>
> Note: MySQL's official docs state that short-form behavior "*may or may not*" return non-NULL (and recommends against relying on it), but in practice MySQL consistently supports it via the C library `inet_aton`. Since our goal is MySQL compatibility, I've implemented the full `inet_aton(3)` behavior.
>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2929862940
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -463,6 +463,43 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
"org.apache.flink.table.runtime.functions.scalar.UrlEncodeFunction")
.build();
+ /**
+ * Converts an IPv4 address string to its numeric representation. Follows MySQL INET_ATON
+ * behavior.
+ *
+ * <p>Supports MySQL-compatible short-form addresses (a.b, a.b.c) and treats leading zeros as
+ * decimal.
+ *
+ * <p>Example: INET_ATON('127.0.0.1') returns 2130706433; INET_ATON('127.1') returns 2130706433
+ */
+ public static final BuiltInFunctionDefinition INET_ATON =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("INET_ATON")
+ .kind(SCALAR)
+ .inputTypeStrategy(sequence(logical(LogicalTypeFamily.CHARACTER_STRING)))
+ .outputTypeStrategy(explicit(BIGINT().nullable()))
+ .runtimeClass(
+ "org.apache.flink.table.runtime.functions.scalar.InetAtonFunction")
+ .build();
+
+ /**
+ * Converts a numeric IPv4 address representation back to its string format. Follows MySQL
+ * INET_NTOA behavior.
+ *
+ * <p>Accepts BIGINT or INT input. Input must be in valid range [0, 4294967295].
+ *
+ * <p>Example: INET_NTOA(2130706433) returns '127.0.0.1'
+ */
+ public static final BuiltInFunctionDefinition INET_NTOA =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("INET_NTOA")
+ .kind(SCALAR)
+ .inputTypeStrategy(sequence(logical(LogicalTypeFamily.INTEGER_NUMERIC)))
Review Comment:
Thanks for pointing this out!
Great catch — since INET_NTOA accepts logical(LogicalTypeFamily.INTEGER_NUMERIC), it should handle all integer types in that family, including SMALLINT and TINYINT. However, Flink's FamilyArgumentTypeStrategy does not implicitly cast types within the same family, so SMALLINT (→ Short) and TINYINT (→ Byte) inputs would actually fail at runtime without explicit eval overloads.
I've made the following changes:
Added eval(Short) and eval(Byte) overloads to InetNtoaFunction.java — these treat the values as unsigned (via Short.toUnsignedLong() / Byte.toUnsignedLong()) before delegating to longToIp().
Added inetNtoaSmallintInputTestCases() — tests SMALLINT inputs including (short) 256 → "0.0.1.0", (short) 0 → "0.0.0.0", (short) -1 → "0.0.255.255" (unsigned), and null handling.
Added inetNtoaTinyintInputTestCases() — tests TINYINT inputs including (byte) 127 → "0.0.0.127", (byte) -1 → "0.0.0.255" (unsigned), and null handling.
Please take another look when you get a chance.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2929778829
##########
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>
Review Comment:
Thanks for the suggestion!
Agreed — the Implementation Note explaining why we don't use InetAddresses, IPAddressUtil, etc. is more of a design decision rationale than something future readers need in the Javadoc. I've moved it out of the codebase.
I've removed the Implementation Note section from both InetAtonFunction.java and InetNtoaFunction.java (which had a similar note). The Javadoc now focuses purely on functional documentation — what the function does, the supported formats, and usage examples.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2929765409
##########
flink-python/pyflink/table/expression.py:
##########
@@ -1447,6 +1447,43 @@ def url_encode(self) -> 'Expression[str]':
"""
return _unary_op("urlEncode")(self)
+ def inet_aton(self) -> 'Expression':
+ """
+ Converts an IPv4 address string to its numeric representation (BIGINT).
+ This function follows MySQL INET_ATON behavior.
+
+ The conversion formula is: A * 256^3 + B * 256^2 + C * 256 + D for an IP address A.B.C.D
+
+ Supports MySQL-compatible short-form addresses:
+ - a.b is interpreted as a.0.0.b
+ - a.b.c is interpreted as a.b.0.c
+
+ Examples:
+ - lit('127.0.0.1').inet_aton() returns 2130706433
+ - lit('127.1').inet_aton() returns 2130706433 (short-form)
+ - lit('0.0.0.0').inet_aton() returns 0
+
+ :return: the numeric representation of the IP address, or null if the input is
+ null or invalid
+ """
+ return _unary_op("inetAton")(self)
+
+ def inet_ntoa(self) -> 'Expression[str]':
+ """
+ Converts a numeric IPv4 address representation (BIGINT or INT) back to its string format.
+ This function follows MySQL INET_NTOA behavior.
Review Comment:
Thanks for the feedback!
Good point. The INET_NTOA function performs a straightforward numeric-to-IP-string conversion with no MySQL-specific compatibility behavior involved, so there's no need to reference MySQL here.
I've removed the "follows MySQL INET_NTOA behavior" line from:
flink-python/pyflink/table/expression.py (Python docstring)
docs/data/sql_functions.yml (English SQL function docs)
docs/data/sql_functions_zh.yml (Chinese SQL function docs)
InetNtoaFunction.java (Java implementation Javadoc)
Note: I kept the MySQL reference in INET_ATON since it does have MySQL-compatible short-form address parsing behavior (e.g., a.b → a.0.0.b), which is worth documenting.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2937800621
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java:
##########
@@ -1419,6 +1421,55 @@ public OutType urlEncode() {
return toApiSpecificExpression(unresolvedCall(URL_ENCODE, toExpr()));
}
+ /**
+ * Converts an IPv4 address string to its numeric representation. This function follows MySQL
+ * INET_ATON behavior.
+ *
+ * <p>The conversion formula is: A * 256^3 + B * 256^2 + C * 256 + D for an IP address A.B.C.D
+ *
+ * <p>Supports MySQL-compatible short-form addresses:
+ *
+ * <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 (not octal), consistent with MySQL.
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ * <li>INET_ATON('127.0.0.1') returns 2130706433
+ * <li>INET_ATON('127.1') returns 2130706433 (short-form)
+ * <li>INET_ATON('0.0.0.0') returns 0
+ * </ul>
+ *
+ * @return the numeric representation of the IP address, or null if the input is null or invalid
+ */
+ public OutType inetAton() {
+ return toApiSpecificExpression(unresolvedCall(INET_ATON, toExpr()));
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation back to its string format. This function
+ * follows MySQL INET_NTOA behavior.
Review Comment:
Update here as well.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetNtoaFunction.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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_NTOA}.
+ *
+ * <p>This function converts a numeric IPv4 address representation back to its string format.
+ *
+ * <p>The conversion extracts each octet from the numeric value using bit shifting and masking.
+ *
+ * <p>Note: This function only supports IPv4 addresses. IPv6 addresses are not supported.
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ * <li>INET_NTOA(2130706433) returns '127.0.0.1'
+ * <li>INET_NTOA(167772161) returns '10.0.0.1'
+ * <li>INET_NTOA(0) returns '0.0.0.0'
+ * </ul>
+ */
+@Internal
+public class InetNtoaFunction extends BuiltInScalarFunction {
+
+ public InetNtoaFunction(SpecializedFunction.SpecializedContext context) {
+ super(BuiltInFunctionDefinitions.INET_NTOA, context);
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation (Long) to its string format.
+ *
+ * @param ipNumber the numeric representation of the IPv4 address
+ * @return the IPv4 address string in dotted-decimal notation, or null if input is null or out
+ * of valid range
+ */
+ public @Nullable StringData eval(@Nullable Long ipNumber) {
Review Comment:
Consider replacing the four `eval` overloads with a single `eval(Number)` using `Number.longValue()`. This would also unify the behavior for negative inputs — currently `-1` as `INT` returns `"255.255.255.255"` but as `BIGINT` returns `null`, which is inconsistent. With `longValue()`, all negative inputs would uniformly return `null`, consistent with MySQL's `INET_NTOA(-1) = NULL`.
Also, I'm not sure we need to support negative inputs at all — in MySQL, `INET_NTOA(-1)` returns `NULL`. The only scenario where negative values arise is migrating from MySQL UNSIGNED INT columns, but since `INET_ATON` already returns `BIGINT`, the recommended approach is to store IP numbers in `BIGINT` columns from the start. What do you think?
##########
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:
Our implementation is only MySQL-compatible, which is different from C library, it would be better to remove the latter part.
##########
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}):
+ *
+ * <ul>
+ * <li>a -> direct 32-bit value (value must be in [0, 0xFFFFFFFF])
Review Comment:
value must be in [0, 255] in MySQL.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on PR #27483:
URL: https://github.com/apache/flink/pull/27483#issuecomment-3823124964
Thanks for the contribution! To pass the completeness check, could you also add the Python API for these functions?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "lincoln-lil (via GitHub)" <gi...@apache.org>.
lincoln-lil commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2965221344
##########
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.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
Review Comment:
nit: "partially compatible with MySQL INET_ATON" maybe more accurately.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on PR #27483:
URL: https://github.com/apache/flink/pull/27483#issuecomment-3976517703
@lincoln-lil PTAL when you're free. Thanks!
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2957751467
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java:
##########
@@ -1419,6 +1421,55 @@ public OutType urlEncode() {
return toApiSpecificExpression(unresolvedCall(URL_ENCODE, toExpr()));
}
+ /**
+ * Converts an IPv4 address string to its numeric representation. This function follows MySQL
+ * INET_ATON behavior.
+ *
+ * <p>The conversion formula is: A * 256^3 + B * 256^2 + C * 256 + D for an IP address A.B.C.D
+ *
+ * <p>Supports MySQL-compatible short-form addresses:
+ *
+ * <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 (not octal), consistent with MySQL.
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ * <li>INET_ATON('127.0.0.1') returns 2130706433
+ * <li>INET_ATON('127.1') returns 2130706433 (short-form)
+ * <li>INET_ATON('0.0.0.0') returns 0
+ * </ul>
+ *
+ * @return the numeric representation of the IP address, or null if the input is null or invalid
+ */
+ public OutType inetAton() {
+ return toApiSpecificExpression(unresolvedCall(INET_ATON, toExpr()));
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation back to its string format. This function
+ * follows MySQL INET_NTOA behavior.
Review Comment:
Good catch — the inetAton() Javadoc in BaseExpressions.java was still using the old short-form description. Updated it to match the canonical format in InetAtonFunction.java: all four short-form variants (a, a.b, a.b.c, a.b.c.d) with em dashes, plus the INET_ATON('1') example.
Also applied the same update to the Python docstring in expression.py.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2957782217
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetNtoaFunction.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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_NTOA}.
+ *
+ * <p>This function converts a numeric IPv4 address representation back to its string format.
+ *
+ * <p>The conversion extracts each octet from the numeric value using bit shifting and masking.
+ *
+ * <p>Note: This function only supports IPv4 addresses. IPv6 addresses are not supported.
+ *
+ * <p>Examples:
+ *
+ * <ul>
+ * <li>INET_NTOA(2130706433) returns '127.0.0.1'
+ * <li>INET_NTOA(167772161) returns '10.0.0.1'
+ * <li>INET_NTOA(0) returns '0.0.0.0'
+ * </ul>
+ */
+@Internal
+public class InetNtoaFunction extends BuiltInScalarFunction {
+
+ public InetNtoaFunction(SpecializedFunction.SpecializedContext context) {
+ super(BuiltInFunctionDefinitions.INET_NTOA, context);
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation (Long) to its string format.
+ *
+ * @param ipNumber the numeric representation of the IPv4 address
+ * @return the IPv4 address string in dotted-decimal notation, or null if input is null or out
+ * of valid range
+ */
+ public @Nullable StringData eval(@Nullable Long ipNumber) {
Review Comment:
Fully agree — there's no real need to support negative inputs as valid values. In our current implementation, all negative inputs uniformly return NULL, which is consistent with MySQL's INET_NTOA(-1) = NULL behavior.
As you pointed out, the only scenario where negative values could arise is migrating from MySQL UNSIGNED INT columns, but since INET_ATON already returns BIGINT, users should store IP numbers in BIGINT columns from the start, avoiding the negative value issue entirely.
So the current behavior is: we accept any integer type (TINYINT/SMALLINT/INT/BIGINT) as input, but negative values are treated as invalid and return NULL — no special unsigned conversion is performed. This keeps the semantics clean and MySQL-compatible.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #27483:
URL: https://github.com/apache/flink/pull/27483#discussion_r2922004494
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -463,6 +463,43 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
"org.apache.flink.table.runtime.functions.scalar.UrlEncodeFunction")
.build();
+ /**
+ * Converts an IPv4 address string to its numeric representation. Follows MySQL INET_ATON
+ * behavior.
+ *
+ * <p>Supports MySQL-compatible short-form addresses (a.b, a.b.c) and treats leading zeros as
+ * decimal.
+ *
+ * <p>Example: INET_ATON('127.0.0.1') returns 2130706433; INET_ATON('127.1') returns 2130706433
+ */
Review Comment:
nit: We don't add comments here usually.
##########
flink-python/pyflink/table/expression.py:
##########
@@ -1447,6 +1447,43 @@ def url_encode(self) -> 'Expression[str]':
"""
return _unary_op("urlEncode")(self)
+ def inet_aton(self) -> 'Expression':
+ """
+ Converts an IPv4 address string to its numeric representation (BIGINT).
+ This function follows MySQL INET_ATON behavior.
+
+ The conversion formula is: A * 256^3 + B * 256^2 + C * 256 + D for an IP address A.B.C.D
+
+ Supports MySQL-compatible short-form addresses:
+ - a.b is interpreted as a.0.0.b
+ - a.b.c is interpreted as a.b.0.c
+
+ Examples:
+ - lit('127.0.0.1').inet_aton() returns 2130706433
+ - lit('127.1').inet_aton() returns 2130706433 (short-form)
+ - lit('0.0.0.0').inet_aton() returns 0
+
+ :return: the numeric representation of the IP address, or null if the input is
+ null or invalid
+ """
+ return _unary_op("inetAton")(self)
+
+ def inet_ntoa(self) -> 'Expression[str]':
+ """
+ Converts a numeric IPv4 address representation (BIGINT or INT) back to its string format.
+ This function follows MySQL INET_NTOA behavior.
Review Comment:
Is there any compatibility issue for this func? If not, we can remove this line and update the doc.
##########
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:
Use `BinaryStringDataUtil.isEmpty()` to avoid unnecessary conversion.
BTW, it looks like we can implement this function using just `BinaryStringData` API without `String`.
##########
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>
Review Comment:
nit: Would it be better to leave this note in pr comments, not in the code base?
##########
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) {
+ return null;
+ }
+
+ final String ip = ipAddress.toString();
+ if (ip.isEmpty()) {
+ return null;
+ }
+
+ return ipToLong(ip);
+ }
+
+ /**
+ * Converts an IPv4 address string to a long value.
+ *
+ * <p>Supports MySQL-compatible short-form addresses:
+ *
+ * <ul>
+ * <li>a.b -> a.0.0.b
+ * <li>a.b.c -> a.b.0.c
+ * <li>a.b.c.d -> standard format
+ * </ul>
+ *
+ * <p>Leading zeros are treated as decimal (not octal), consistent with MySQL behavior.
+ *
+ * @param ip the IPv4 address string
+ * @return the long value, or null if the IP address is invalid
+ */
+ private static @Nullable Long ipToLong(String ip) {
+ int len = ip.length();
+ int octetCount = 0;
+ int octetStart = 0;
+ long[] octets = new long[4];
+
+ for (int i = 0; i <= len; i++) {
+ if (i == len || ip.charAt(i) == '.') {
+ if (octetCount >= 4) {
+ return null;
+ }
+ if (octetStart == i) {
+ return null;
+ }
+
+ // Parse number manually
+ long octet = 0;
+ for (int j = octetStart; j < i; j++) {
+ char c = ip.charAt(j);
+ if (c < '0' || c > '9') {
+ return null;
+ }
+ octet = octet * 10 + (c - '0');
+ if (octet > 255) {
+ return null;
+ }
+ }
+ octets[octetCount++] = octet;
+ octetStart = i + 1;
+ }
+ }
+
+ if (octetCount < 2) {
+ return null;
+ }
Review Comment:
It seems that MySQL supports single num address, for example, `'1' -> 1`. Could you double check this behaviour?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/InetNtoaFunction.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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_NTOA}.
+ *
+ * <p>This function converts a numeric IPv4 address representation back to its string format. It
+ * follows the MySQL INET_NTOA function behavior.
+ *
+ * <p>The conversion extracts each octet from the numeric value using bit shifting and masking.
+ *
+ * <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>The conversion from number to IP string is straightforward and does not benefit from
+ * external libraries
+ * <li>Using a custom implementation avoids unnecessary dependencies and object allocations (e.g.,
+ * creating {@code InetAddress} objects)
+ * <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_NTOA(2130706433) returns '127.0.0.1'
+ * <li>INET_NTOA(167772161) returns '10.0.0.1'
+ * <li>INET_NTOA(0) returns '0.0.0.0'
+ * </ul>
+ */
+@Internal
+public class InetNtoaFunction extends BuiltInScalarFunction {
+
+ private static final long MAX_IPV4_VALUE = 0xFFFFFFFFL;
+ private static final int OCTET_MASK = 0xFF;
+ private static final int BITS_PER_OCTET = 8;
+
+ public InetNtoaFunction(SpecializedFunction.SpecializedContext context) {
+ super(BuiltInFunctionDefinitions.INET_NTOA, context);
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation (Long) to its string format.
+ *
+ * @param ipNumber the numeric representation of the IPv4 address
+ * @return the IPv4 address string in dotted-decimal notation, or null if input is null or out
+ * of valid range
+ */
+ public @Nullable StringData eval(@Nullable Long ipNumber) {
+ if (ipNumber == null) {
+ return null;
+ }
+ return convertToIp(ipNumber);
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation (Integer) to its string format.
+ *
+ * <p>This overload handles INT type inputs which Flink may pass directly without implicit
+ * conversion to Long.
+ *
+ * @param ipNumber the numeric representation of the IPv4 address
+ * @return the IPv4 address string in dotted-decimal notation, or null if input is null or out
+ * of valid range
+ */
+ public @Nullable StringData eval(@Nullable Integer ipNumber) {
+ if (ipNumber == null) {
+ return null;
+ }
+ // Convert to long, treating negative integers as unsigned
+ // For example, -1 (0xFFFFFFFF as signed int) should be treated as 4294967295
+ return convertToIp(Integer.toUnsignedLong(ipNumber));
+ }
+
+ /**
+ * Internal conversion method.
+ *
+ * @param ipNumber the numeric representation
+ * @return the IPv4 address string, or null if out of valid range
+ */
+ private @Nullable StringData convertToIp(long ipNumber) {
+ // Check if the number is within valid IPv4 range [0, 4294967295]
+ if (ipNumber < 0 || ipNumber > MAX_IPV4_VALUE) {
+ return null;
+ }
+
+ return StringData.fromString(longToIp(ipNumber));
+ }
+
+ /**
+ * Converts a numeric IPv4 address representation to its string format.
+ *
+ * @param ipNumber the numeric representation
+ * @return the IPv4 address string
+ */
+ private static String longToIp(long ipNumber) {
Review Comment:
nit: `convertToIp` and `longToIp` can be merged into one, and the static constants can also be inlined, as the values are self-explanatory in this context.
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -463,6 +463,43 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
"org.apache.flink.table.runtime.functions.scalar.UrlEncodeFunction")
.build();
+ /**
+ * Converts an IPv4 address string to its numeric representation. Follows MySQL INET_ATON
+ * behavior.
+ *
+ * <p>Supports MySQL-compatible short-form addresses (a.b, a.b.c) and treats leading zeros as
+ * decimal.
+ *
+ * <p>Example: INET_ATON('127.0.0.1') returns 2130706433; INET_ATON('127.1') returns 2130706433
+ */
+ public static final BuiltInFunctionDefinition INET_ATON =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("INET_ATON")
+ .kind(SCALAR)
+ .inputTypeStrategy(sequence(logical(LogicalTypeFamily.CHARACTER_STRING)))
+ .outputTypeStrategy(explicit(BIGINT().nullable()))
+ .runtimeClass(
+ "org.apache.flink.table.runtime.functions.scalar.InetAtonFunction")
+ .build();
+
+ /**
+ * Converts a numeric IPv4 address representation back to its string format. Follows MySQL
+ * INET_NTOA behavior.
+ *
+ * <p>Accepts BIGINT or INT input. Input must be in valid range [0, 4294967295].
+ *
+ * <p>Example: INET_NTOA(2130706433) returns '127.0.0.1'
+ */
+ public static final BuiltInFunctionDefinition INET_NTOA =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("INET_NTOA")
+ .kind(SCALAR)
+ .inputTypeStrategy(sequence(logical(LogicalTypeFamily.INTEGER_NUMERIC)))
Review Comment:
Could you also test `SMALLINT` and `TINYINT` input?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-38988][Table SQL/API] Support INET_ATON and INET_NTOA functions for IP address conversion [flink]
Posted by "Myracle (via GitHub)" <gi...@apache.org>.
Myracle commented on PR #27483:
URL: https://github.com/apache/flink/pull/27483#issuecomment-3889153670
@dylanhz Good catch! I have done it.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org