SQL API: ORDER BY throws NullPointerException when the sort column contains NULL
Maintainers usually reply within 3 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- backend-api-design, databases
Research direction
Start with SortKeyExtractor.java at the comparison on line 50 and WayangSortVisitor.java at lines 60–61, which maps the sort direction. Trace how Calcite’s null direction reaches the comparator, then run the SQL API sorting tests with the provided CSV and queries. Done when ascending, descending, and explicit NULLS FIRST produce the expected row order without an exception.
Written by the indexing model from the issue text.
Description
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:
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 only maps getDirection().
To reproduce
A table with one empty value in an int column:
col1:int,col2:string
1;a
;b
3;c
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.
- Dominant language
- Java
- Stars
- 284
- Forks
- 147
- Avg merge
- 3d 17h
- Merged PRs (30d)
- 15
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/wayang
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 3 days
-
JavaReservoirSampleOperator returns the whole input when the dataset size is unknownPossibly taken @sasmith-14 claimed this 11 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/wayang#884 · 3 comments ·
Maintainers usually reply within 3 days
-
Record.compareTo() throws ClassCastException when comparing recordsPossibly taken @harshitsingh070 claimed this 73 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 3 days
-
Unused parameter in function ProjectionDescriptor.createPojoJavaImplementation()Possibly taken @AliRana30 claimed this 81 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
apache/wayang#732 · 3 comments ·
Maintainers usually reply within 3 days
-
SQL API: aggregates don't skip NULL values (COUNT(col) counts them, SUM and AVG throw NPE)Possibly taken @AarushShetty346 claimed this 8 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 20/100
Maintainers usually reply within 3 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
micronaut-projects/micronaut-core#14585 ·
Maintainers usually reply within 1 day
-
Mend: dependency security vulnerability untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
opensearch-project/cross-cluster-replication#1805 ·
Maintainers usually reply within 1 day
-
feature request needs triage
Difficulty 1/5 Under an hour Newbie friendliness 66/100
TeamNewPipe/NewPipe#14083 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
PCL-Community/PCL-CE#3907 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 2 days