Can anyone explain to me the behavior of a comparison between a string and a null value.
DataFrame:
import pyspark.sql.functions as F
df = spark.createDataFrame([
["text_1,","text_1"],
["text_1,","text_2"],
["text_1,",None]
]).toDF("col1", "col2")
df1 = df.withColumn("equal", F.when(F.col("col1") == F.col("col2"), "equal").otherwise("not equal")
+-------+-------+----------+
|col1 |col2 |equal |
+-------+-------+----------+
|text_1 |text_1 |equal |
|text_1 |text_2 |not equal |
|text_1 |null |not equal | <*
+-------+-------+----------+
df2 = df.withColumn("equal", F.when(F.col("col1") != F.col("col2"), "equal").otherwise("not equal")
+-------+-------+----------+
|col1 |col2 |equal |
+-------+-------+----------+
|text_1 |text_1 |equal |
|text_1 |text_2 |not equal |
|text_1 |null |equal | <*
+-------+-------+----------+
The is equal comparison seems to go well but the is not equal goes wrong.
Can anyone explain this to me and how can I solve this without checking on .isNotNull or fill the null values with an empty string (if possible).