cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Row_Num function in spark-sql

Hemant
Valued Contributor II

I have a doubt row_num with order by in spark-sql gives different result(non-deterministic output) every time i execute it?

โ€‹

It's due to parallelism in spark ?โ€‹

โ€‹

Any approach how to takle it?

โ€‹

I order by with a date column and a integer column and take minimum row_num in the further down stream process.

Hemant Soni
2 REPLIES 2

shan_chandra
Databricks Employee
Databricks Employee

@Hemant Kumarโ€‹ - could you please try to coalesce to a single partition in order to generate continuous output?

Tharun-Kumar
Databricks Employee
Databricks Employee

@Hemant 

If the order by clause provided yields a unique result, then we would get deterministic output. 

For ex:

If we create a rowID for this dataset, with CustomerID used in OrderBy clause, then depending upon the runtime, we may get non-deterministic output for CustomerID 100, because any record may get picked up for Row Number 1.

CustomerIDSalesIDOrderDateTime
1009002023-07-08 10:00:00
1009012023-07-09 10:00:00
1019022023-07-09 10:00:00

In cases like this, it is recommended to use a column to define the ordering in a deterministic way. The usage of OrderDateTime column in the OrderBy clause will help us to achieve a deterministic output.

The same is also documented in the official documentation - https://docs.databricks.com/sql/language-manual/functions/row_number.html#:~:text=If%20the%20order%2....

 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group