opensearch-project / opensearch-project/sql

[BUG] Sum multiple `null` values should return `null` instead of `0`

Open
#3,408 4 comments 0 reactions 1 assignee View on GitHub

@LantaoJin is already working on this.

Since Jun 27, 2025.

breaking bug calcite dependencies
Dominant language
Java
Stars
176
Forks
229
Avg merge
2d 21h
Merged PRs (30d)
43

Description

What is the bug?
In most databases. Below query will return null

CREATE TABLE employees (
    emp_id INT PRIMARY KEY,
    name VARCHAR(50),
    department VARCHAR(50),
    salary INT
);

INSERT INTO employees VALUES
(1, 'John', 'IT', 60000),
(2, 'Alice', 'IT', 55000),
(3, 'Bob', 'IT', null),
(4, 'Sarah', 'HR', 52000),
(5, 'Mike', 'HR', 48000),
(6, 'Lisa', 'Sales', null),
(7, 'Tom', 'Sales', null);

select sum(salary) from employees where department = 'Sales';

Return

+-------------+
| sum(salary) |
+-------------+
|        NULL |
+-------------+

But in PPL v2, it return 0. For example

  @Test
  public void testSumNull() {
    String response =
        execute(
            String.format(
                "source=%s | where department = 'Sales' | stats sum(salary)",
                TEST_INDEX_BANK_WITH_NULL_VALUES));
    assertEquals(
        ""
            + "{\n"
            + "  \"schema\": [\n"
            + "    {\n"
            + "      \"name\": \"sum(salary)\",\n"
            + "      \"type\": \"long\"\n"
            + "    }\n"
            + "  ],\n"
            + "  \"datarows\": [\n"
            + "    [\n"
            + "      0\n"
            + "    ]\n"
            + "  ],\n"
            + "  \"total\": 1,\n"
            + "  \"size\": 1\n"
            + "}",
        response);
  }

Current the v3 (Calcite) will return null as well. Should we change this behaviour align with other databases?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.