age icon indicating copy to clipboard operation
age copied to clipboard

Fix UNWIND [null] causing count() to miscount null rows (#2383)

Open crprashant opened this issue 7 months ago • 1 comments

Fixes #2383.

Problem

Per Neo4j / openCypher semantics, count() must ignore nulls. On Apache AGE:

UNWIND [null] AS x RETURN count(x)   -- returns 1 (buggy), should be 0

The control cases already returned the correct 0:

RETURN count(null)
WITH null AS x RETURN count(x)

Root cause

age_unnest() in src/backend/utils/adt/agtype.c materialized every top-level array element into a heap tuple with nulls[0] = false, even when the element's agtype_value.type == AGTV_NULL. The row that reached count() therefore carried a non-NULL datum wrapping an agtype JSON null, so the aggregate's strict null check never skipped it.

Fix

A naïve fix inside age_unnest() breaks other callers. construct_age_function_name() in src/backend/parser/cypher_expr.c rewrites unnest → age_unnest, so list comprehensions and the predicate functions (all / any / none / single) also route through age_unnest. Those callers deliberately rely on the quirk that AGE's agtype_null > agtype_integer evaluates to agtype_null (truthy), not SQL NULL. Changing the SRF broke five predicate_functions expected results.

Introduce a dedicated SRF age_unwind() with UNWIND-specific null semantics:

  • Top-level AGTV_NULL elements → SQL NULL rows so null-strict operators (count, IS NULL, WHERE x IS NOT NULL, …) match WITH null AS x.
  • Non-null elements are returned unchanged.
  • Nested nulls inside arrays/objects are preserved as agtype-null.

transform_cypher_unwind() is the only call site changed from age_unnest to age_unwind. List comprehensions and predicate functions continue to use age_unnest and their behavior is untouched.

Before / after

-- before
UNWIND [null] AS x RETURN count(x)                  -- 1
UNWIND [1, null, 2, null, 3] AS x RETURN count(x)   -- 5
UNWIND [null] AS x RETURN x IS NULL                 -- false

-- after
UNWIND [null] AS x RETURN count(x)                  -- 0 ✓
UNWIND [1, null, 2, null, 3] AS x RETURN count(x)   -- 3 ✓
UNWIND [null] AS x RETURN x IS NULL                 -- true ✓

Regression tests (regress/sql/cypher_unwind.sql)

  • UNWIND [null] RETURN count(x) = 0
  • UNWIND [1, null, 2, null, 3] RETURN count(x) = 3
  • UNWIND [null] RETURN x IS NULL
  • UNWIND [1, null, 2] WITH x WHERE x IS NOT NULL RETURN count(x) = 2
  • UNWIND [null] RETURN count(*) = 1 (row still produced)
  • UNWIND [[null], {k: null}] RETURN count(x) = 2 (nested nulls preserved)
  • UNWIND [null] RETURN x — asserts the row value itself is SQL NULL

Files changed

  • src/backend/utils/adt/agtype.c — new age_unwind SRF
  • src/backend/parser/cypher_clause.c — UNWIND calls age_unwind
  • sql/agtype_typecast.sql — declare age_unwind
  • age--1.7.0--y.y.y.sql — add age_unwind for in-place upgrades
  • regress/sql/cypher_unwind.sql + regress/expected/cypher_unwind.out

Test results

make installcheck with PostgreSQL 18 on Ubuntu 24.04:

  • 32/33 tests pass.
  • age_upgrade fails identically on apache/age master HEAD (pre-existing, unrelated; same age_prepare_pg_upgrade already exists error).
  • cypher_unwind, predicate_functions, list_comprehension all pass.

crprashant avatar Apr 23 '26 01:04 crprashant

Heads-up for reviewers — just noticed @MuhammadTahaNaveed's #2406 fixes the same root cause (agtype AGTV_NULL not being emitted as SQL NULL from age_unnest) with a broader scope that also covers single() / all() / any() / none() three-valued semantics. That direction was my first instinct too, but I backed off after it altered the predicate_functions regression; #2406 goes further and reworks those predicates to match Neo4j three-valued logic, which is the right call. #2406 subsumes this PR.

Happy to close #2411 in favor of #2406 once it lands. The 7 regression cases in regress/sql/cypher_unwind.sql here specifically cover UNWIND [null] AS x RETURN count(x) = 0 (issue #2383) and a few UNWIND-only boundaries (nested lists, mixed nulls, DISTINCT over UNWIND) that #2406's tests don't exercise — glad to open a small follow-up PR porting just those tests on top of #2406 if that would be useful.

Thanks for the fast work on the null-propagation family of bugs.

crprashant avatar Apr 23 '26 02:04 crprashant

Thanks for this, @crprashant. We ended up landing #2406 for #2383, which fixes the root cause in age_unnest itself and so covers UNWIND [null] plus the related list-comprehension null-filter (#2393) and single() three-valued logic in one change. Since that supersedes the UNWIND-specific fix here, I'm closing this PR — but the analysis in it was genuinely helpful in scoping the fix, so thank you.

If you'd like to keep contributing in this area, #2416 (keyword-named aliases) is still open and yours — happy to review once it's ready.

gregfelice avatar Jul 03 '26 11:07 gregfelice