IP addresses functions
Work with IPv4 and IPv6 addresses.
IP address functions in provide a set of tools for working with both IPv4 and IPv6 addresses.
Key capabilities include:
- Converting between string and numeric representations of IP addresses
- Extracting network information (e.g., subnets, CIDR ranges)
- Performing IP address arithmetic and comparisons
- Handling both IPv4 and IPv6 address formats
These functions are particularly useful for tasks such as:
- Network log analysis
- Geolocation services
- Access control and security applications
- Network configuration management
ClickHouse functions reference
IPv4NumToString
Converts an IPv4 address from its numeric representation to a string.
Syntax
Alias:
- INET_NTOA
Arguments
num
(UInt32
): IPv4 address as aUInt32
number in big endian format.
Returns:
- String representation of the IPv4 address in A.B.C.D format.
Example
Result:
This function is useful for converting numeric IP addresses stored in databases or logs into human-readable string format, often used in network analysis or when displaying IP addresses in user interfaces.
The input is interpreted as big endian, which is the standard network byte order for IP addresses.
IPv4StringToNum
Converts an IPv4 address from string format to its numeric representation.
Syntax:
Alias:
- INET_ATON
Arguments:
ip_address
(String
): IPv4 address in string format (A.B.C.D).
Returns:
- Numeric representation of the IPv4 address. (
UInt32
)
Example:
Result:
In this example, the IPv4 address ‘192.168.0.1’ is converted to its numeric representation.
If the IPv4 address has an invalid format, the function throws an exception.
See also:
IPv4NumToString
— Performs the reverse operation.IPv4StringToNumOrDefault
— Returns 0 for invalid IPv4 addresses instead of throwing an exception.IPv4StringToNumOrNull
— Returns NULL for invalid IPv4 addresses instead of throwing an exception.
This function is useful for storing IPv4 addresses efficiently in numeric format or for performing numeric operations on IP addresses.
IPv4StringToNumOrDefault
Converts an IPv4 address from a string to its numeric representation.
Syntax:
Arguments:
ip_address
(String
): The IPv4 address in string format (e.g., ‘192.168.0.1’).
Returns:
- A
UInt32
value representing the IPv4 address. - If the input string is not a valid IPv4 address, it returns 0.
Example:
Result:
In this example:
- ‘192.168.0.1’ is converted to its numeric representation.
- ‘256.0.0.1’ is invalid (256 is out of range for an octet), so it returns 0.
- ‘taco.special’ is not an IP address, so it returns 0.
This function is similar to IPv4StringToNum
, but instead of throwing an exception for invalid input, it returns 0. This can be useful in scenarios where you want to handle invalid IP addresses gracefully without causing query errors.
IPv4StringToNumOrNull
Converts an IPv4 address from a string format to its numeric representation.
Syntax:
Arguments:
ip_address
(String
): The IPv4 address in string format (e.g., ‘192.168.0.1’).
Returns:
- The numeric representation of the IPv4 address as a
UInt32
value. - Null if the input string is not a valid IPv4 address.
Example:
Result:
In this example:
- ‘192.168.0.1’ is converted to its numeric representation.
- ‘256.0.0.1’ is invalid (256 is out of range for an octet) and returns NULL.
- ‘taco.special.sauce’ is not an IP address and returns NULL.
This function is useful when you need to convert IP addresses to their numeric form for storage or comparison, while safely handling invalid inputs by returning NULL instead of throwing an exception.
IPv4NumToStringClassC
Converts an IPv4 address from a UInt32
number to a string, replacing the last octet with ‘xxx’.
Syntax:
Arguments:
num
(UInt32
): IPv4 address represented as a number in big endian format.
Returns:
A string containing the IPv4 address with the last octet replaced by ‘xxx’. [String
]
Example:
Result:
This function is useful for grouping IP addresses by their Class C network. For example:
Result:
This example shows the top 5 Class C networks by number of taco orders.
The use of ‘xxx’ for the last octet is unconventional. Future versions may change this format. It’s recommended not to rely on the exact format of this fragment in production code.
IPv6NumToString
Converts an IPv6 address from binary format to a string representation.
Syntax:
Alias:
- INET6_NTOA
Arguments:
x
(FixedString(16)
): The IPv6 address in binary format.
Returns:
A string containing the IPv6 address in text format. IPv6-mapped IPv4 addresses are output in the format ::ffff:111.222.33.44.
Examples:
Result:
Result:
In this example, we convert IPv6 addresses stored in the ClientIP6
column to their string representations and count their occurrences.
IPv6-mapped IPv4 addresses are displayed with the ::ffff:
prefix, as shown in the example results.
This function is particularly useful when you need to display IPv6 addresses in a human-readable format, such as in logs, user interfaces, or when preparing data for export.
IPv6StringToNum
Converts an IPv6 address from string format to its binary representation.
Syntax
Arguments
ip_address
(String
): IPv6 address as a string.
Returns
- The IPv6 address in binary format. [
FixedString(16)
]
Examples
Result:
This function can also handle IPv4-mapped IPv6 addresses:
Result:
If the input string is not a valid IPv6 address, the function will throw an exception.
- For IPv4 addresses, use
IPv4StringToNum
instead.
See Also
IPv6NumToString
: The reverse function that converts binary IPv6 to string format.toIPv6
: An alias for this function that returns an IPv6 type instead of FixedString(16).
IPv6StringToNumOrDefault
Converts an IPv6 address from string format to its binary representation. If the input string is not a valid IPv6 address, it returns a default value.
Syntax
Arguments
ip_address
(String
): IPv6 address as a string.
Returns
- Binary representation of the IPv6 address. (
FixedString(16)
) - If the input is not a valid IPv6 address, returns a string of 16 null bytes.
Example
Result:
Example with invalid input:
Result:
This function is useful when you need to convert IPv6 addresses to their binary form for storage or comparison, and you want to handle invalid inputs gracefully without throwing an exception.
If you need to distinguish between valid and invalid inputs, consider using IPv6StringToNumOrNull
instead, which returns NULL
for invalid IPv6 addresses.
IPv6StringToNumOrNull
Converts an IPv6 address from string format to its binary representation. If the input string is not a valid IPv6 address, the function returns NULL.
Syntax
Arguments
ip_address
(String
): IPv6 address as a string.
Returns
- The binary representation of the IPv6 address as a
FixedString(16)
. - NULL if the input is not a valid IPv6 address.
Example
Result:
Example with invalid input:
Result:
This function is useful when you need to convert IPv6 addresses to their binary form for storage or comparison, while handling potentially invalid input gracefully by returning NULL instead of throwing an exception.
IPv4ToIPv6
Converts an IPv4 address to its IPv6 representation.
Syntax:
Arguments:
ip
(UInt32
): The IPv4 address as a 32-bit unsigned integer.
Returns:
- The IPv6 representation of the input IPv4 address as
FixedString(16)
.
Example:
Result:
In this example, we convert the IPv4 address ‘192.168.0.1’ to its IPv6 equivalent. The result is the IPv6-mapped IPv4 address.
This function is useful when working with mixed IPv4 and IPv6 environments or when you need to represent IPv4 addresses in IPv6 format.
The resulting IPv6 address always starts with the prefix ::ffff:
, which is the standard prefix for IPv4-mapped IPv6 addresses.
cutIPv6
Cuts specified bytes from IPv6 and IPv4 addresses in binary format.
Syntax:
Arguments:
address
(FixedString(16)
): IPv6 address in binary format.bytesToCutForIPv6
(UInt8
): Number of bytes to cut for IPv6 addresses.bytesToCutForIPv4
(UInt8
): Number of bytes to cut for IPv4 addresses.
Returns:
A string containing the truncated IP address in text format.
Example:
Result:
In this example:
- For the IPv6 address, the last 2 bytes are cut off.
- For the IPv4 address (mapped to IPv6), the last 2 bytes of the IPv4 part are cut off.
This function is useful for anonymizing IP addresses or for grouping them by network prefix.
IPv4CIDRToRange
Converts an IPv4 address and CIDR prefix to a range of IPv4 addresses.
Syntax:
Arguments:
ipv4
(IPv4
orString
): The IPv4 address.cidr
(UInt8
): The CIDR prefix length.
Returns:
A tuple containing two IPv4 addresses representing the lower and upper bounds of the IP range.
Example:
Result:
This function is useful for determining the range of IP addresses within a given subnet. In the example, it shows that the IP address 192.168.5.2 with a CIDR prefix of 16 belongs to the range from 192.168.0.0 to 192.168.255.255.
This function works with IPv4 addresses only. For IPv6 addresses, use IPv6CIDRToRange.
See also:
IPv6CIDRToRange
Converts an IPv6 address and CIDR prefix to a range of IPv6 addresses.
Syntax:
Arguments:
ipv6
(IPv6
orString
): The IPv6 address.cidr
(UInt8
): The CIDR prefix length.
Returns:
A tuple containing two IPv6 addresses representing the lower and upper bounds of the IPv6 range.
Example:
Result:
In this example, we calculate the IPv6 range for the address ‘2001:db8::1’ with a CIDR prefix of 32. The result shows the lower bound (‘2001:db8::’) and upper bound (‘2001:db8:ffff:ffff:ffff:ffff:ffff:ffff’) of the range.
This function is useful for network calculations, IP address management, and firewall rule configurations in IPv6 networks.
toIPv4
Converts a string representation of an IPv4 address to an IPv4 type.
Syntax
Arguments
string
(String
) — A string containing an IPv4 address.
Returns
- The IPv4 address. [
IPv4
]
This function is an alias to IPv4StringToNum()
but returns a value of [IPv4
] type instead of UInt32
.
Example
Result:
See Also
IPv4StringToNum
toIPv4OrDefault
toIPv4OrNull
If you need to work with the binary representation of IPv4 addresses, consider using IPv4StringToNum
instead.
toIPv4OrDefault
Converts a string representation of an IPv4 address to its numeric form. If the input string is not a valid IPv4 address, it returns 0.0.0.0 (0 in IPv4 format).
Syntax
Arguments
string
(String
) — A string containing an IPv4 address.
Returns
- The IPv4 address in numeric form. (
IPv4
)
Example
Result:
In this example:
- ‘192.168.0.1’ is converted to its numeric IPv4 representation.
- ‘192.168.0.256’ is invalid (256 is out of range for an octet), so it returns 0.0.0.0.
- ‘taco.special’ is not an IP address at all, so it also returns 0.0.0.0.
This function is useful when you need to handle potentially invalid IP addresses in your data without causing errors, defaulting to 0.0.0.0 for any invalid input.
toIPv4OrNull
Converts a string representation of an IPv4 address to an IPv4 type. If the input string is not a valid IPv4 address, it returns NULL.
Syntax
Arguments
string
(String
) — A string containing an IPv4 address.
Returns
- The IPv4 address. [
IPv4
] - NULL if the input string is not a valid IPv4 address.
Example
Result:
In this example:
- ‘192.168.0.1’ is converted to a valid IPv4 address.
- ‘192.168.0.256’ is invalid (256 is out of range for an octet) and returns NULL.
- ‘taco.special’ is not an IP address at all and returns NULL.
This function is useful when you need to handle potentially invalid IP addresses in your data without causing errors, such as when processing user input or cleaning data from external sources.
toIPv6OrDefault
Converts a string representation of an IPv6 address to the IPv6 type. If the input string has an invalid IPv6 format, it returns the default IPv6 address (::).
Syntax
Arguments
string
(String
): A string containing an IPv6 address.
Returns
- The IPv6 address. [
IPv6
] - If the input is invalid, returns :: (the IPv6 equivalent of 0.0.0.0).
Example
Result:
In this example:
- The valid IPv6 address is converted correctly.
- The invalid input returns the default IPv6 address (::).
This function also accepts valid IPv4 addresses and returns their IPv6 equivalents:
Result:
This function is useful when you need to ensure all inputs are converted to valid IPv6 addresses, with invalid inputs defaulting to a known value instead of causing errors.
toIPv6OrNull
Converts a string representation of an IPv6 address to the IPv6
data type. If the input string is not a valid IPv6 address, it returns NULL
.
Syntax
Arguments
string
(String
): A string containing an IPv6 address.
Returns
- An IPv6 address of type
IPv6
. NULL
if the input string is not a valid IPv6 address.
Example
Result:
This example demonstrates:
- Converting a full IPv6 address
- Converting a compressed IPv6 address
- Converting an IPv4 address to its IPv6 representation
- Handling an invalid IP address (returns NULL)
When given a valid IPv4 address, the function returns its IPv6-mapped IPv4 address equivalent.
toIPv6
Converts a string representation of an IPv6 address to the IPv6
data type.
Syntax:
Arguments:
string
(String
): IP address.
Returns:
- The IP address as an
IPv6
type.
If the input string is an invalid IPv6 address, it returns an empty value.
If the input string contains a valid IPv4 address, it returns the IPv6 equivalent of that IPv4 address.
Examples:
Result:
Example with IPv4 address:
Result:
This function is similar to IPv6StringToNum
, but returns the IPv6
type instead of a binary format.
If you need to handle invalid input, consider using toIPv6OrDefault
or toIPv6OrNull
functions.
IPv6StringToNumOrDefault
Converts an IPv6 address from string format to its binary representation. If the input string is not a valid IPv6 address, it returns a default value.
Syntax
Arguments
ip_address
(String
): IPv6 address as a string.
Returns
- Binary representation of the IPv6 address. (
FixedString(16)
) - If the input is not a valid IPv6 address, returns a string of 16 null bytes (equivalent to the IPv6 address
::
)
Example
Result:
Example with invalid input:
Result:
This function is useful when you need to convert IPv6 addresses to their binary form for storage or comparison, and you want to handle invalid inputs gracefully without throwing an exception.
When working with IP addresses, consider using the IPv6
data type for more efficient storage and operations.
IPv6StringToNumOrNull
Converts an IPv6 address from string format to its binary representation. If the input string is not a valid IPv6 address, the function returns NULL.
Syntax
Arguments
ip_address
(String
): IPv6 address as a string.
Returns
- The binary representation of the IPv6 address as a
FixedString(16)
. - NULL if the input is not a valid IPv6 address.
Example
Result:
Result:
Result:
This function is useful when you need to convert IPv6 addresses to their binary form for storage or comparison, while safely handling invalid inputs by returning NULL instead of throwing an exception.
isIPv4String
Determines whether the input string is a valid IPv4 address.
Syntax:
Arguments:
address
(String
): The IP address to check.
Returns:
- 1 if the string is a valid IPv4 address, 0 otherwise. [
UInt8
]
Example:
Result:
In this example:
- Valid IPv4 addresses return 1.
- IPv6 addresses, invalid IP formats, and non-IP strings return 0.
- Note that ‘256.1.2.3’ is not a valid IPv4 address as octets cannot exceed 255.
This function returns 0 for IPv6 addresses, even if they are valid. Use isIPv6String
for checking IPv6 addresses.
isIPv6String
Determines whether the input string is an IPv6 address or not.
Syntax:
Arguments:
address
(String
): The IP address to check.
Returns:
- 1 if the string is a valid IPv6 address, 0 otherwise. [
UInt8
]
Example:
Result:
In this example:
::1
and2001:db8::1
are valid IPv6 addresses, soisIPv6String
returns 1.192.168.0.1
is a valid IPv4 address but not IPv6, so it returns 0.not_an_ip
is not a valid IP address at all, so it returns 0.::ffff:192.0.2.128
is a valid IPv6 address (IPv4-mapped IPv6 address), so it returns 1.
This function returns 0 for valid IPv4 addresses. Use isIPv4String
to check for IPv4 addresses specifically.
isIPAddressInRange
Determines if an IP address is contained within a network specified in CIDR notation.
Syntax:
Arguments:
address
(String
): An IPv4 or IPv6 address.prefix
(String
): An IPv4 or IPv6 network prefix in CIDR notation.
Returns:
- 1 if the IP address is in the specified range, 0 otherwise. (
UInt8
)
This function supports both IPv4 and IPv6 addresses and networks. It returns 0 if the IP version of the address and the CIDR don’t match.
Examples:
Result:
Example with IPv6 address:
Result:
Example with IP out of range:
Result:
Example with IPv6 address and IPv4 CIDR:
Result:
This function is useful for checking if an IP address belongs to a specific network range, which can be helpful in various networking and security applications.