try_add
Returns the sum of leftand right and the result is null on overflow. The acceptable input types are the same with the + operator. Supports Spark Connect.
For the corresponding Databricks SQL function, see try_add function.
Syntax
Python
from pyspark.databricks.sql import functions as dbf
dbf.try_add(left=<left>, right=<right>)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The left side value |
|
| The right side value |
Examples
Python
from pyspark.databricks.sql import functions as dbf
spark.createDataFrame(
[(1982, 15), (1990, 2)], ["birth", "age"]
).select("*", dbf.try_add("birth", "age")).show()
Output
+-----+---+-------------------+
|birth|age|try_add(birth, age)|
+-----+---+-------------------+
| 1982| 15| 1997|
| 1990| 2| 1992|
+-----+---+-------------------+
Python
from pyspark.databricks.sql import functions as dbf
spark.sql("SELECT * FROM VALUES (DATE('2015-09-30')) AS TAB(date)").select("*", dbf.try_add("date", dbf.lit(1))).show()
Output
+----------+----------------+
| date|try_add(date, 1)|
+----------+----------------+
|2015-09-30| 2015-10-01|
+----------+----------------+