Preserve null-valued keys in map literals (#2391)
Summary
Fixes #2391.
Map literals such as RETURN {a: null} previously dropped keys whose values were null, producing {} instead of {"a": null}. This diverged from the openCypher / Neo4j semantics where map literals preserve every key the user wrote, including those bound to null.
Root cause
cypher_map.keep_null defaulted to false (zero-initialised), so the grammar-produced node fed agtype_build_map_nonull, which strips null entries. Call sites that legitimately need strip-null semantics (CREATE node/edge property maps and SET = assignments) already set keep_null = false explicitly on the top-level property map, and the MATCH pattern path sets it to true explicitly. Flipping the grammar default to true therefore only affects the cases that were buggy (bare map expressions and nested map values).
Top-level CREATE / SET property null-stripping is unchanged — those call sites override keep_null = false on the top-level property map node. Note the override is not recursive: a nested map value inside a CREATE / SET property is its own cypher_map node, so it now inherits the new true default and preserves its null-valued keys (e.g. CREATE (n {a: {b: null}}) stores a -> {"b": null} rather than a -> {}). This is the intended, more consistent behaviour — a nested map literal should preserve its keys regardless of context — and is pinned by new regression tests.
Changes
src/backend/parser/cypher_gram.y— themap:rule now setsn->keep_null = truewith an explanatory comment noting the top-level-only nature of the CREATE / SET override.regress/sql/expr.sql— dedicated regression coverage for #2391 (single-key null, multiple nulls,keys(),coalesce, nested map values, mixed null / non-null, empty map, top-level CREATE strip control, MATCH verify, and nested map values under CREATE / SET=).regress/expected/expr.out— two preexisting tests that encoded the old buggy output now show the preserved"z": null; plus expected output for the new #2391 block.regress/expected/agtype.out— nested{bool: true, i: null}inside an orderability test no longer drops its null entry, shifting one row in theORDER BYresult.
Behaviour before / after
RETURN {a: null} AS m
-- before: {}
-- after: {"a": null}
RETURN keys({a: null}) AS ks
-- before: []
-- after: ["a"]
CREATE and SET = still strip top-level null-valued keys (unchanged), matching openCypher semantics for property writes. Nested map literals inside a write now preserve their null keys, e.g. CREATE (n {a: {b: null}}) stores a -> {"b": null}.
Test results
make installcheck against PostgreSQL 18: 32 / 33 pass. The only failure is age_upgrade, which is a preexisting failure unrelated to this change.
Hi @jrgemignani , @MuhammadTahaNaveed could you pls review this..
Hi @jrgemignani , @MuhammadTahaNaveed could you pls review this..
Sorry for the delay
@gregfelice Thanks for the review. To enable CI running in PR flow, someone from maintainers group who has permission to do that, can trigger the CI, henc it did not start the CI yet. @jrgemignani can do that i believe.
Thanks @gregfelice — good catch, and you're right on both counts. I traced it the same way: the CREATE (cypher_clause.c:7049) and SET = (cypher_clause.c:2378) overrides only set keep_null = false on the top-level property map node and don't recurse, so a nested map value is its own cypher_map node that now inherits the true default and preserves its null keys. Confirmed empirically:
CREATE (n {a: {b: null}})
-- before: a -> {}
-- after: a -> {"b": null}
Agreed this is the more consistent behaviour, so I've pinned it rather than suppressed it. Addressed in 71f8d44:
- Nested write coverage — added to the Issue 2391 block in
expr.sql/expr.out:CREATE (n:Nested {a: {b: null}}) RETURN n,MATCH (n:Nested) SET n = {a: {b: null}} RETURN n, and aMATCHverify, so the nested-preserve behaviour can't silently revert. - Wording — tightened the PR description and the
map:rule comment incypher_gram.yfrom "leaves CREATE / SET behaviour unchanged" to "top-level CREATE / SET property null-stripping is unchanged", and the comment now notes nested map values keep the null-preserving default.
On CI: full make installcheck against PostgreSQL 18 passes locally (all tests green, including age_upgrade) on the rebased branch. The branch is now rebased onto current master.