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

Đang mở
#891 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@AarushShetty346 đang làm issue này rồi.

Từ ngày 18/11/2026.

  • #893 của @AarushShetty346 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
20/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
java, spark, sql
Lĩnh vực
backend, databases

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Java
Star
284
Fork
147
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
13

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/wayang

Tất cả issue của apache/wayang

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.