Skip to content

Fix chained MATCH rebinding a zero-row variable into a path - #2627

Open
leo7martins wants to merge 1 commit into
apache:masterfrom
leo7martins:fix/2616-rebound-empty-match
Open

leo7martins wants to merge 1 commit into
apache:masterfrom
leo7martins:fix/2616-rebound-empty-match

Conversation

@leo7martins

Copy link
Copy Markdown

Fixes #2616.

On an empty graph, these queries abort with ERROR: argument 1 must not be null instead of returning count = 0:

MATCH (m1)-[:rt3]->(m2) MATCH q = (m2) RETURN count(q)
MATCH (m1)-[:rt3]->(m2) WITH m2 MATCH q = (m2) RETURN count(q)

A fresh variable (MATCH q = (zz)) returns 0. After one :rt3 edge exists, the rebound query returns 1. This is the same error text as #882, reached through a chained MATCH that rebinds a variable.

A missing label is compiled as a NULL agtype constant plus a false WHERE, so the first MATCH yields no rows. The following MATCH q = (var) only sees a Var pointing at that constant and builds _agtype_build_path(var). That function is IMMUTABLE and not STRICT, and it raises on a NULL argument. Subquery pull-up substitutes the NULL constant into the call, and the planner evaluates it while folding constants, before the empty match can produce zero rows. A path in the same clause already turned a NULL entity expression into a NULL path; a rebound variable skipped that check because the expression is a Var. An existing label with no edges does not produce a NULL constant, and that query already returned 0.

expr_is_null_constant() follows the Var through subquery target lists. When it reaches a NULL constant, the path variable is a NULL constant and _agtype_build_path is not called. Matches that bind a real vertex still build the path from that vertex. SELECT _agtype_build_path(NULL) still errors.

Regression cases in regress/sql/cypher_match.sql cover the empty chained MATCH, the empty WITH form, a fresh variable on the empty graph, and both rebound shapes after one edge.

Tested on PostgreSQL 18.7:

  • Before the change, the two rebound queries on a fresh graph raised argument 1 must not be null; MATCH q = (zz) returned 0; after one :rt3 edge, the rebound query returned 1.
  • After the change, those queries returned 0, 0, 0, and 1.
  • make PG_CONFIG=/usr/bin/pg_config installcheck: 44 of 44 tests passed (# All 44 tests passed.), including cypher_match.
  • make PG_CONFIG=/usr/bin/pg_config COPT=-Werror src/backend/parser/cypher_clause.o compiled with no warnings.

This was written with help from an AI coding assistant.

A MATCH on a missing label projects NULL constants and a false WHERE,
so it yields no rows. A following MATCH q = (var), including one after
WITH, builds _agtype_build_path() around a Var that points at that
constant. The function is IMMUTABLE and rejects NULL, and subquery
pull-up substitutes the constant into the call. The planner evaluates
it while folding constants and raises "argument 1 must not be null"
instead of returning count 0.

Same-clause paths already turned a NULL entity into a NULL path.
Follow a rebound Var through subquery target lists and do the same
when it refers to a NULL constant, so the function is not called.
Non-empty matches still build the path from the real vertex.

Fixes 2616
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.

Chained MATCH rebinding a variable bound to zero rows errors with "argument 1 must not be null"

1 participant