Skip to content

Handle NULL values in SQL ORDER BY - #892

Open
AarushShetty346 wants to merge 1 commit into
apache:mainfrom
AarushShetty346:fix-sql-sort-nulls
Open

AarushShetty346 wants to merge 1 commit into
apache:mainfrom
AarushShetty346:fix-sql-sort-nulls

Conversation

@AarushShetty346

Copy link
Copy Markdown

Fixes #890

What changes were proposed in this pull request?

ORDER BY on a column with NULL values threw a NullPointerException in SortKeyExtractor, because it called compareTo on the null value.

  • WayangSortVisitor now passes the null direction of each field collation to SortKeyExtractor. If it is UNSPECIFIED, it uses Direction.defaultNullDirection() (LAST for ASC, FIRST for DESC), which is Calcite's default.
  • SortKeyExtractor.compareTo places nulls by that direction before comparing the values. NULLS FIRST / NULLS LAST aren't flipped for DESC, so they stay absolute like in standard SQL.
  • The existing two-argument constructor is kept and uses the default null directions.

How was this patch tested?

I added javaSortWithNullsAscending, javaSortWithNullsDescending and javaSortWithNullsFirst to SqlToWayangRelTest. They use a new three-row exampleNull.csv with one empty col1. All three fail with the NPE on main and pass with this change. The other wayang-api-sql tests still pass (34 in total).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL API: ORDER BY throws NullPointerException when the sort column contains NULL

1 participant