pyspark.sql.functions.try_add#

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

Returns the sum of left`and `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 plus Integer.

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

Example 2: Date plus Integer.

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

Example 3: Date plus 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_add("date", "i")).show()
+----------------+
|try_add(date, i)|
+----------------+
|      2016-09-30|
+----------------+

Example 4: Interval plus 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_add("i", "j")).show()
+-----------------+
|    try_add(i, j)|
+-----------------+
|INTERVAL '3' 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_add(sf.lit(sys.maxsize), sf.lit(sys.maxsize))).show()
... finally:
...     spark.conf.set("spark.sql.ansi.enabled", origin)
+-------------------------------------------------+
|try_add(9223372036854775807, 9223372036854775807)|
+-------------------------------------------------+
|                                             NULL|
+-------------------------------------------------+