pyspark.sql.functions.try_multiply#

pyspark.sql.functions.try_multiply(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

multiplicand

rightColumn or str

multiplier

Examples

Example 1: Integer multiplied by Integer.

>>> import pyspark.sql.functions as sf
>>> spark.createDataFrame(
...     [(6000, 15), (1990, 2)], ["a", "b"]
... ).select(sf.try_multiply("a", "b")).show()
+------------------+
|try_multiply(a, b)|
+------------------+
|             90000|
|              3980|
+------------------+

Example 2: Interval multiplied by Integer.

>>> import pyspark.sql.functions as sf
>>> spark.range(6).select(
...     sf.try_multiply(sf.make_interval(sf.lit(0), sf.lit(3)), "id")
... ).show()
+----------------------------------------------------+
|try_multiply(make_interval(0, 3, 0, 0, 0, 0, 0), id)|
+----------------------------------------------------+
|                                           0 seconds|
|                                            3 months|
|                                            6 months|
|                                            9 months|
|                                             1 years|
|                                    1 years 3 months|
+----------------------------------------------------+

Example 3: 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_multiply(sf.lit(sys.maxsize), sf.lit(sys.maxsize))).show()
... finally:
...     spark.conf.set("spark.sql.ansi.enabled", origin)
+------------------------------------------------------+
|try_multiply(9223372036854775807, 9223372036854775807)|
+------------------------------------------------------+
|                                                  NULL|
+------------------------------------------------------+