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 str

column name or column that represents the input column to test

errMsgColumn or str, 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

>>> df = spark.createDataFrame([(0,1)], ['a', 'b'])
>>> df.select(assert_true(df.a < df.b).alias('r')).collect()
[Row(r=None)]
>>> df.select(assert_true(df.a < df.b, df.a).alias('r')).collect()
[Row(r=None)]
>>> df.select(assert_true(df.a < df.b, 'error').alias('r')).collect()
[Row(r=None)]
>>> df.select(assert_true(df.a > df.b, 'My error msg').alias('r')).collect() 
...
java.lang.RuntimeException: My error msg
...