cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

BLOCK_OFFSET_INSIDE_BLOCK ROW_OFFSET_INSIDE_BLOCK is not working

databicky
Contributor II

 

BLOCK_OFFSET_INSIDE_BLOCK ROW_OFFSET_INSIDE_BLOCK command is not working in spark, but these command is running in hive , when running in spark it get failed with invalid column like that

4 REPLIES 4

Kaniz
Community Manager
Community Manager

Hi @databicky, The BLOCK_OFFSET_INSIDE_BLOCK and ROW_OFFSET_INSIDE_BLOCK functions, specific to Hive, are not supported in Spark SQL due to differences in syntax and query execution. To achieve similar functionality in Spark SQL, you can use the row_number() window function.

Here's an example of how to do that:

 

SELECT *,
       row_number() OVER (
         PARTITION BY col1, col2
         ORDER BY col3 ASC
       ) - 1 AS BLOCK_OFFSET_INSIDE_BLOCK,
       
       row_number() OVER (
         PARTITION BY col1, col2, col3
         ORDER BY col4 ASC
       ) - 1 AS ROW_OFFSET_INSIDE_BLOCK
FROM your_table

In this example, we use the row_number() function with the OVER clause to partition the data by specified columns and order it as needed. Subtracting 1 from each result aligns it with the zero-based values returned by the BLOCK_OFFSET_INSIDE_BLOCK and ROW_OFFSET_INSIDE_BLOCK functions. Keep in mind that the specific syntax may vary depending on your table and use case, but this approach can help you achieve similar results in Spark SQL.

Screenshot_2023-11-03-14-46-12-48_40deb401b9ffe8e1df2f1cc5ba480b12.jpg

โ€ƒi want to convert the attached screenshot code into spark one.

Kaniz
Community Manager
Community Manager

Hi @databicky, Could you paste your code stack here instead of the screenshot?

not able to upload as a text format it is showing error as unable to complete the action you requested

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.