Skip to content

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

Open
crprashant wants to merge 1 commit intoapache:masterfrom
crprashant:fix/2391-map-literal-null-keys
Open

Preserve null-valued keys in map literals (#2391)#2412
crprashant wants to merge 1 commit intoapache:masterfrom
crprashant:fix/2391-map-literal-null-keys

Conversation

@crprashant
Copy link
Copy Markdown

@crprashant crprashant commented Apr 23, 2026

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, 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), and leaves CREATE / SET behaviour unchanged.

Changes

  • src/backend/parser/cypher_gram.y — the map: rule now sets n->keep_null = true with an explanatory comment.
  • regress/sql/expr.sql — dedicated regression coverage for Map literals may drop keys whose values are null. #2391 (single-key null, multiple nulls, keys(), coalesce, nested map values, mixed null / non-null, empty map, CREATE control, MATCH verify).
  • 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 Map literals may drop keys whose values are null. #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 null-valued keys (unchanged), matching openCypher semantics for property writes.

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.

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, 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),
and leaves CREATE/SET behaviour unchanged.

Two preexisting tests encoded the old buggy output and are updated:
expr.out (bare RETURN maps now keep the null value) and agtype.out
(a nested map inside an orderability test no longer drops its null
entry, shifting one row in the ORDER BY result). Dedicated regression
coverage for apache#2391 is added to regress/sql/expr.sql.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@crprashant
Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Map literals may drop keys whose values are null.

1 participant