age icon indicating copy to clipboard operation
age copied to clipboard

Preserve null-valued keys in map literals (#2391)

Open crprashant opened this issue 7 months ago • 4 comments

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 — the map: rule now sets n->keep_null = true with 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 the ORDER BY result.

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.

crprashant avatar Apr 23 '26 02:04 crprashant

Hi @jrgemignani , @MuhammadTahaNaveed could you pls review this..

crprashant avatar Apr 23 '26 23:04 crprashant

Hi @jrgemignani , @MuhammadTahaNaveed could you pls review this..

Sorry for the delay

jrgemignani avatar Jun 08 '26 18:06 jrgemignani

@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.

crprashant avatar Jun 26 '26 19:06 crprashant

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:

  1. 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 a MATCH verify, so the nested-preserve behaviour can't silently revert.
  2. Wording — tightened the PR description and the map: rule comment in cypher_gram.y from "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.

crprashant avatar Jun 26 '26 19:06 crprashant