pyspark.sql.functions.
assert_true
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.
Column
column name or column that represents the input column to test
A Python string literal or column containing the error message
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 ...