snowflakedb / spark-snowflake

Snowflake Data Source for Apache Spark.
http://www.snowflake.net
Apache License 2.0
211 stars 98 forks source link

Nulls order ignored when query is pushed down to Snowflake #534

Open Messlow opened 9 months ago

Messlow commented 9 months ago

The spark connector ignores the NULLS FIRST or NULLS LAST part of the order by clause when the query is pushed down to Snowflake. Also, for Snowflake NULL values are considered to be higher than any non-NULL values, which is the opposite as spark. This makes null ordering non-coherent between both. Here is a reproducible example, on Databricks with DBR 12,2:

spark.createDataFrame(
    [(None, None), (1.0, 2.0), (2.0, 1.0), (None, None)], schema=["col1", "col2"]
).write.format("snowflake").options(**snowflake_options).mode("overwrite").option(
    "dbtable", "TEST_NULLS"
).save()

data = (
    spark.read.format("snowflake")
    .options(**snowflake_options)
    .option("dbtable", "TEST_NULLS")
    .load()
)

data.sort(F.col("col1").desc_nulls_last()).show()

Expected output:

+----+----+
|COL1|COL2|
+----+----+
|   2|   1|
|   1|   2|
|null|null|
|null|null|
+----+----+

Observed output:

+----+----+
|COL1|COL2|
+----+----+
|null|null|
|null|null|
|   2|   1|
|   1|   2|
+----+----+