Skip to main content

ip_host

Applies to: check marked yes Databricks Runtime 18.2 and above

Beta

This feature is in Beta. Workspace admins can control access to this feature from the Previews page. See Manage Databricks previews.

Returns the canonical representation of an IPv4 or IPv6 address.

For the corresponding SQL function, see ip_host function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.ip_host(col=<col>)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or str

A STRING or BINARY value representing a valid IPv4 or IPv6 address.

Examples

Example 1: Validate an IPv4 address.

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('192.168.1.5',)], ['ipv4'])
df.select(dbf.ip_host('ipv4').alias('result')).collect()
Output
[Row(result='192.168.1.5')]

Example 2: Canonicalize an IPv6 address.

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('2001:0DB8:0000:0000:0000:0000:0000:0001',)], ['ipv6'])
df.select(dbf.ip_host('ipv6').alias('result')).collect()
Output
[Row(result='2001:db8::1')]

Example 3: Validate an IPv4-mapped IPv6 address.

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('::ffff:192.0.2.128',)], ['ip'])
df.select(dbf.ip_host('ip').alias('result')).collect()
Output
[Row(result='::ffff:192.0.2.128')]

Example 4: Validate an IPv4 address in binary format. The input is the binary representation of the IPv4 address 192.168.1.5.

Python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql.functions import hex
df = spark.createDataFrame([(bytearray([0xC0, 0xA8, 0x01, 0x05]),)], ['ip'])
df.select(hex(dbf.ip_host('ip')).alias('result')).collect()
Output
[Row(result='C0A80105')]

Example 5: None input returns None.

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(None,)], 'ip: string')
df.select(dbf.ip_host('ip').alias('result')).collect()
Output
[Row(result=None)]