AarushShetty346 opened a new issue, #890:
URL: https://github.com/apache/wayang/issues/890

   **`ORDER BY` in the SQL API fails with a `NullPointerException` as soon as 
the sort column has a NULL value.**
   
   ### The bug
   
   `SortKeyExtractor` compares sort keys directly at 
[`SortKeyExtractor.java:50`](https://github.com/apache/wayang/blob/713faa7d8bf4726536f6b0920ebb73053160274c/wayang-api/wayang-api-sql/src/main/java/org/apache/wayang/api/sql/calcite/converter/functions/SortKeyExtractor.java#L50):
   
   ```java
   final int comparison = direction.isDescending() ? 
-thisField.compareTo(thatField) : thisField.compareTo(thatField);
   ```
   
   An empty cell in a numeric CSV column is read as `null`, so `thisField` (or 
`thatField`) can be null here. The null direction of the collation (`NULLS 
FIRST` / `NULLS LAST`) is also never passed on: 
[`WayangSortVisitor.java:60-61`](https://github.com/apache/wayang/blob/713faa7d8bf4726536f6b0920ebb73053160274c/wayang-api/wayang-api-sql/src/main/java/org/apache/wayang/api/sql/calcite/converter/WayangSortVisitor.java#L60-L61)
 only maps `getDirection()`.
   
   ### To reproduce
   
   A table with one empty value in an int column:
   
   ```
   col1:int,col2:string
   1;a
   ;b
   3;c
   ```
   
   ```sql
   SELECT col1, col2 FROM fs.exampleNull ORDER BY col1
   ```
   
   on the Java platform gives:
   
   ```
   Caused by: java.lang.NullPointerException: Cannot invoke 
"java.lang.Comparable.compareTo(Object)" because "thisField" is null
       at 
org.apache.wayang.api.sql.calcite.converter.functions.SortKeyExtractor$1.compareTo(SortKeyExtractor.java:50)
       at 
org.apache.wayang.api.sql.calcite.converter.functions.SortKeyExtractor$1.compareTo(SortKeyExtractor.java:39)
       at 
org.apache.wayang.java.operators.JavaSortOperator.lambda$evaluate$0(JavaSortOperator.java:79)
   ```
   
   ### Expected
   
   The NULL row is sorted instead of crashing, using Calcite's default when no 
null direction is given (NULLS LAST for ASC, NULLS FIRST for DESC):
   
   - `ORDER BY col1` → `(1, a), (3, c), (null, b)`
   - `ORDER BY col1 DESC` → `(null, b), (3, c), (1, a)`
   - `ORDER BY col1 NULLS FIRST` → `(null, b), (1, a), (3, c)`
   
   Tested on main at 713faa7. I have a fix with tests and will open a PR for it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to