Skip to content

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

@LantaoJin

Description

@LantaoJin

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?

Metadata

Metadata

Assignees

Labels

breakingbugSomething isn't workingcalcitecalcite migration releated

Type

No type

Projects

Status

Not Started

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions