Skip to content

NVL and COALESCE does not work on RIGHT JOIN #18559

@JRobTS

Description

@JRobTS

When using NVL or COALESCE in a SQL query involving a NULL value from the right-side table, the function returns null instead of the expected value.

Affected Version

Druid 33.0.0

Description

Using this sample query:

SELECT ds1.Key, COALESCE(ds1.LastUsed, 'N/A'), COALESCE(ds2.LastUsed, 'N/A')
FROM (
  SELECT Key, MAX(__time) AS LastUsed
  FROM datasource1
  WHERE __time >= CURRENT_TIMESTAMP - INTERVAL 90 DAY
  GROUP BY 1
) ds1
FULL OUTER JOIN (
  SELECT Key, MAX(__time) AS LastUsed
  FROM datasource2
  WHERE __time >= CURRENT_TIMESTAMP - INTERVAL 90 DAY
  GROUP BY 1
) ds2 ON ds1.Key = ds2.Key
ORDER BY 1, COALESCE(ds2.LastUsed, ds1.LastUsed) DESC

Given data like:

datasource1

__time              | Key
--------------------+---- 
2025-09-22T01:00:00 | 1 
2025-09-22T02:00:00 | 2


datasource2

__time              | Key
--------------------+---- 
2025-09-22T11:00:00 | 1 
2025-09-22T13:00:00 | 3

Expected Result would be:

Key | EXPR$1              | EXPR$2
----+---------------------+--------------------
  1 | 2025-09-22T01:00:00 | 2025-09-22T11:00:00
  2 | 2025-09-22T02:00:00 | N/A
  3 | N/A                 | 2025-09-22T13:00:00

Actual Result is:

Key | EXPR$1              | EXPR$2
----+---------------------+--------------------
  1 | 2025-09-22T01:00:00 | 2025-09-22T11:00:00
  2 | 2025-09-22T02:00:00 | null
  3 | N/A                 | 2025-09-22T13:00:00

The same holds true for NVL. For example: SELECT ds1.Key, NVL(ds1.LastUsed, CURRENT_TIMESTAMP), NVL(ds2.LastUsed, CURRENT_TIMESTAMP) returns a null entry in EXPR$2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions