pyspark.sql.functions.assert_true#

pyspark.sql.functions.assert_true(col, errMsg=None)[source]#

Returns null if the input column is true; throws an exception with the provided error message otherwise.

New in version 3.1.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or column name

column name or column that represents the input column to test

errMsgColumn or literal string, optional

A Python string literal or column containing the error message

Returns
Column

null if the input column is true otherwise throws an error with specified message.

Examples

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([(0, 1)], ['a', 'b'])
>>> df.select('*', sf.assert_true(df.a < df.b)).show() 
+------------------------------------------------------+
|assert_true((a < b), '(a#788L < b#789L)' is not true!)|
+------------------------------------------------------+
|                                                  NULL|
+------------------------------------------------------+
>>> df.select('*', sf.assert_true(df.a < df.b, df.a)).show()
+---+---+-----------------------+
|  a|  b|assert_true((a < b), a)|
+---+---+-----------------------+
|  0|  1|                   NULL|
+---+---+-----------------------+
>>> df.select('*', sf.assert_true(df.a < df.b, 'error')).show()
+---+---+---------------------------+
|  a|  b|assert_true((a < b), error)|
+---+---+---------------------------+
|  0|  1|                       NULL|
+---+---+---------------------------+
>>> df.select('*', sf.assert_true(df.a > df.b, 'My error msg')).show() 
...
java.lang.RuntimeException: My error msg
...