pyspark.sql.functions.try_subtract#

pyspark.sql.functions.try_subtract(left, right)[source]#

Returns left-right and the result is null on overflow. The acceptable input types are the same with the - operator.

New in version 3.5.0.

Parameters
leftColumn or str
rightColumn or str

Examples

Example 1: Integer minus Integer.

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

Example 2: Date minus Integer.

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

Example 3: Date minus Interval.

>>> import pyspark.sql.functions as sf
>>> spark.sql(
...     "SELECT * FROM VALUES (DATE('2015-09-30'), INTERVAL 1 YEAR) AS TAB(date, i)"
... ).select(sf.try_subtract("date", "i")).show()
+---------------------+
|try_subtract(date, i)|
+---------------------+
|           2014-09-30|
+---------------------+

Example 4: Interval minus Interval.

>>> import pyspark.sql.functions as sf
>>> spark.sql(
...     "SELECT * FROM VALUES (INTERVAL 1 YEAR, INTERVAL 2 YEAR) AS TAB(i, j)"
... ).select(sf.try_subtract("i", "j")).show()
+------------------+
|try_subtract(i, j)|
+------------------+
|INTERVAL '-1' YEAR|
+------------------+

Example 5: Overflow results in NULL when ANSI mode is on

>>> import pyspark.sql.functions as sf
>>> origin = spark.conf.get("spark.sql.ansi.enabled")
>>> spark.conf.set("spark.sql.ansi.enabled", "true")
>>> try:
...     df = spark.range(1)
...     df.select(sf.try_subtract(sf.lit(-sys.maxsize), sf.lit(sys.maxsize))).show()
... finally:
...     spark.conf.set("spark.sql.ansi.enabled", origin)
+-------------------------------------------------------+
|try_subtract(-9223372036854775807, 9223372036854775807)|
+-------------------------------------------------------+
|                                                   NULL|
+-------------------------------------------------------+