ip_cidr
Applies to: 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 CIDR block.
For the corresponding SQL function, see ip_cidr function.
Syntax
Python
from pyspark.databricks.sql import functions as dbf
dbf.ip_cidr(col=<col>)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| A STRING or BINARY value representing a valid IPv4 or IPv6 CIDR block. |
Examples
Example 1: Canonicalize an IPv4 CIDR block.
Python
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()
Output
[Row(result='192.168.1.0/24')]
Example 2: Canonicalize an IPv6 CIDR block.
Python
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()
Output
[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.
Python
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()
Output
[Row(result='C0A8010018')]
Example 4: None input returns None.
Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(None,)], 'cidr: string')
df.select(dbf.ip_cidr('cidr').alias('result')).collect()
Output
[Row(result=None)]