ip_cidr

Applies to: check marked yes Databricks Runtime 18.2 and above

Important

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

Returns the canonical representation of an IPv4 or IPv6 CIDR block.

For the corresponding SQL function, see ip_cidr function.

Syntax

from pyspark.databricks.sql import functions as dbf

dbf.ip_cidr(col=<col>)

Parameters

Parameter Type Description
col pyspark.sql.Column or str A STRING or BINARY value representing a valid IPv4 or IPv6 CIDR block.

Examples

Example 1: Canonicalize an IPv4 CIDR block.

from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('192.168.1.5/24',)], ['cidr'])
df.select(dbf.ip_cidr('cidr').alias('result')).collect()
[Row(result='192.168.1.0/24')]

Example 2: Canonicalize an IPv6 CIDR block.

from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('2001:db8::1/64',)], ['cidr'])
df.select(dbf.ip_cidr('cidr').alias('result')).collect()
[Row(result='2001:db8::/64')]

Example 3: Canonicalize a CIDR block in binary format. The input is the binary representation of 192.168.1.5/24.

from pyspark.databricks.sql import functions as dbf
from pyspark.sql.functions import hex
df = spark.createDataFrame([(bytearray([0xC0, 0xA8, 0x01, 0x05, 0x18]),)], ['cidr'])
df.select(hex(dbf.ip_cidr('cidr')).alias('result')).collect()
[Row(result='C0A8010018')]

Example 4: None input returns None.

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