Skip to main content

try_subtract

Returns left-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_subtract function.

Syntax

Python
from pyspark.sql import functions as dbf

dbf.try_subtract(left=<left>, right=<right>)

Parameters

Parameter

Type

Description

left

pyspark.sql.Column or column name

right

pyspark.sql.Column or column name

Parameter

Type

Description

left

pyspark.sql.Column or column name

right

pyspark.sql.Column or column name

Examples

Python
from pyspark.sql import functions as dbf
spark.createDataFrame(
[(1982, 15), (1990, 2)], ["birth", "age"]
).select("*", dbf.try_subtract("birth", "age")).show()
Output
+-----+---+------------------------+
|birth|age|try_subtract(birth, age)|
+-----+---+------------------------+
| 1982| 15| 1967|
| 1990| 2| 1988|
+-----+---+------------------------+

Python
from pyspark.sql import functions as dbf
spark.sql(
"SELECT * FROM VALUES (DATE('2015-10-01')) AS TAB(date)"
).select("*", dbf.try_subtract("date", dbf.lit(1))).show()
Output
+----------+---------------------+
| date|try_subtract(date, 1)|
+----------+---------------------+
|2015-10-01| 2015-09-30|
+----------+---------------------+