SQL API: aggregates don't skip NULL values (COUNT(col) counts them, SUM and AVG throw NPE)

Aperta
#891 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@AarushShetty346 ci sta già lavorando.

Dal 18/11/2026.

  • #893 di @AarushShetty346 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
20/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
java, spark, sql
Ambito
backend, databases

Direzione di ricerca

Start with AggregateAddCols.java, AggregateFunction.java, and AggregateGetResult.java at the linked lines; trace how NULL values are handled through aggregation and how AVG gets its count. Reproduce the listed queries on the Java platform, including a GROUP BY case, and check the behavior on Spark. Done means COUNT(col), SUM, and AVG match the expected results while COUNT(*) still counts every row.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Aggregates in the SQL API treat NULL as a value. COUNT(col) counts NULL rows, and SUM / AVG throw a NullPointerException when a column has a NULL.

In SQL, every aggregate except COUNT(*) ignores NULL values. There are three spots where this goes wrong:

  1. AggregateAddCols.java:53 sets every COUNT column to 1, so COUNT(col) is always the same as COUNT(*).
  2. AggregateFunction.java:118-124 picks the type from whichever side isn't null, then still passes the null to the function. For example, Integer::sum is applied to (1, null) and fails on unboxing.
  3. AggregateGetResult.java:55-56 divides the AVG sum by the row count, not by the number of non-null values. AVG would therefore be wrong even without the NPE.
To reproduce
col1:int,col2:string
1;a
;b
3;c

On the Java platform:

query result expected
SELECT COUNT(*) FROM fs.exampleNull 3 3
SELECT COUNT(col1) FROM fs.exampleNull 3 2
SELECT SUM(col1) FROM fs.exampleNull NPE 4
SELECT AVG(col1) FROM fs.exampleNull NPE 2.0
java.lang.NullPointerException
    at org.apache.wayang.api.sql.calcite.converter.functions.AggregateFunction.castAndMap(AggregateFunction.java:127)
    at org.apache.wayang.api.sql.calcite.converter.functions.AggregateFunction.apply(AggregateFunction.java:58)

The same thing happens with GROUP BY when a group has both NULL and non-NULL values. It fails in JavaReduceBy, and in Spark too.

Tested on main at 713faa7. I have a fix with tests and will open a PR for it.

Lingua principale
Java
Stelle
284
Fork
147
Merge medio
2g 13h
PR unite (30g)
13

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/wayang

Tutte le issue di apache/wayang

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.