age icon indicating copy to clipboard operation
age copied to clipboard

Propagate null through unnest and single() three-valued logic

Open MuhammadTahaNaveed opened this issue 7 months ago • 2 comments

Two related defects in how AGE handled cypher null inside list-iterating constructs.

age_unnest packaged every iterated element as a non-SQL-NULL agtype datum, even AGTV_NULL scalars. SQL IS NULL / IS NOT NULL then couldn't see those nulls, so [x IN [null, 1] WHERE x IS NULL] dropped the null it was meant to keep, and WHERE x IS NOT NULL kept the null it was meant to drop. The same mismatch surfaced in UNWIND. AGE already treats SQL NULL as the row-level representation of cypher null elsewhere (RETURN null AS v yields SQL NULL, strict operators short-circuit on it); age_unnest now does the same by emitting the row with nulls[0] = true when the element is AGTV_NULL.

single() previously transformed to SELECT count(*) FROM unnest(list) AS x WHERE pred IS TRUE, with the grammar wrapping the result as (subquery) = 1. With the unnest fix, [null, 5] WHERE x > 0 left one definite true after the WHERE filter -> count = 1 -> true. Neo4j returns null because the unknown predicate could itself be a second match. Rewritten to a CASE built on count(*) FILTER (WHERE pred IS TRUE) and bool_or(pred IS NULL):

CASE WHEN count() FILTER (WHERE pred IS TRUE) >= 2 THEN false WHEN bool_or(pred IS NULL) THEN NULL WHEN count() FILTER (WHERE pred IS TRUE) = 1 THEN true ELSE false END

The >=2 arm runs first so two definite trues dominate any unknowns. Fits inside the existing make_predicate_case_expr helper alongside all/any/none, removes the special-case transform branch and the grammar = 1 wrap. A small make_count_star_filter_agg helper mirrors the existing make_bool_or_agg. Verified against Neo4j for the new edge cases (one-true-plus-null, two-trues-plus-null, all-nulls, mixed-true-false-null).

The predicate_functions regression also picks up the corrected behavior of any/all/none over null elements: null > 0 now yields SQL NULL instead of being silently treated as true, so the three-valued combinators in those functions produce the openCypher results the comments previously documented as buggy.

MuhammadTahaNaveed avatar Apr 22 '26 14:04 MuhammadTahaNaveed

@MuhammadTahaNaveed It looks like you need to rebase this PR

jrgemignani avatar Apr 30 '26 00:04 jrgemignani

@jrgemignani — a couple of cross-cutting notes from going through the open parser/agtype PRs, in case they help with sequencing:

1. #2406 and #2411 are competing fixes for the same UNWIND [null] bug (#2383). They take opposite approaches and will conflict — whichever lands first forces a rework of the other:

  • #2411 is surgical: a separate age_unwind SRF, with only transform_cypher_unwind repointed to it. age_unnest is untouched, so list comprehensions and predicate functions can't regress. The trade-off is it intentionally leaves the related list-comprehension null-filter bug (#2393) and the predicate-function three-valued logic unfixed, since those callers currently rely on agtype_null > agtype_integer being truthy.
  • #2406 (this PR) is root-cause: it fixes age_unnest itself and rewrites the predicate-function 3VL so that truthy-null quirk becomes proper three-valued logic — fixing UNWIND, list comprehensions (#2393), and single() together.

Per openCypher, null > 0 should be null rather than truthy, so this PR is the more correct end state; #2411 is the smaller, lower-risk step. Both are sound for their stated scope — it's really a scoping call on whether to fix the whole null-handling family at once or just the UNWIND symptom. Flagging so they don't both get merged independently.

2. A few of these are blocked more on process than on code.

  • No CI run: #2411 and #2412 don't show any check runs — the contributor branches likely need a maintainer to kick off the workflow (or a push) before they can be evaluated for merge.
  • Need a rebase: #2295 and #2411 are currently CONFLICTING with master. #2295 in particular dates to Dec 2025 and touches parser/agtype areas that have moved a lot since, so it'll want a rebase + fresh green CI before a real merge pass.

Happy to take another look at any of these once they're rebased / CI is green.

gregfelice avatar Jun 26 '26 19:06 gregfelice

@MuhammadTahaNaveed The approach here is solid and I'd like to get it in — it's currently blocked only on a rebase plus two small tidy-ups. If you can:

  1. Rebase onto latest master (it's conflicting now, and @jrgemignani asked back on Apr 30) so CI runs green again.
  2. INT8PASSBYVAL fix — the two single() makeConst(INT8OID, …) calls use FLOAT8PASSBYVAL for the byval flag; they should use INT8PASSBYVAL. Functionally identical on all supported platforms, but it's the correct macro for an int8 const.
  3. Empty-list comment — the revised comment still says "all aggregates return NULL on zero rows"; count(*) (incl. count(*) FILTER (…)) returns 0, not NULL. The logic is right, just the comment — worth a one-line correction.

(The make_count_star_filter_agg hardcoded INT8OID return is fine to leave as-is.)

Once that's done and CI's green, I'll approve. Thanks for working the full three-valued-logic fix rather than just the UNWIND symptom.

gregfelice avatar Jul 03 '26 10:07 gregfelice

@gregfelice fixed.

MuhammadTahaNaveed avatar Jul 03 '26 10:07 MuhammadTahaNaveed