Skip to content

Make ? skip the rest of the path and give no output on a type mismatch - #82

Closed
davesnx wants to merge 3 commits into
mainfrom
feat/optional-chaining
Closed

davesnx wants to merge 3 commits into
mainfrom
feat/optional-chaining

Conversation

@davesnx

@davesnx davesnx commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Closes #62.

A path through irregular data needed a ? on every step, and the result had null values that could not be traced to a step. Now one ? on the step that can be absent is enough.

# users: alice has no metadata; bob's preferences are ["dark_mode", {"notifications": {"email": ...}}]
query-json '.users[].metadata?.preferences[].notifications?.email'
# null                 <- alice: no metadata
# "bob@example.com"
  • ? skips the rest of the path when its step gives null, like ?. in JavaScript. {} | .a?.b.c gives null. The path ends at |, ,, an operator, ?? or a closing bracket, so (.a?).b and .a? | .b still fail.
  • Steps without ? stay strict: {"a": {}} | .a?.b is still key_not_found.
  • ? and try without catch give no output on a type mismatch, as jq does: "x" | .a?, 1 | .[]?, 123 | keys?. An absent value still gives null: a missing key, a key or an index on null, an index out of bounds, first? on [].
  • ? and try without catch no longer hide an undefined function: foo? reports the error.
  • .[1:2]? parses without parentheses. A slice on null or a number is an error; it used to stop with an OCaml Assertion failed.
  • path(.a?.b.c) and .a?.b.c |= 9 now work for paths of three or more steps, as the two-step forms already did.

Optional in the AST now holds the step and the rest of its path; a plain ? has Identity as the rest. The interpreter runs the rest outside the error handler, so only the marked step is forgiven. Runtime_error.when_caught is the one place that maps an error kind to null, no output, or propagation.

Differences from jq, on the issue's data:

Query main (2c08a49) This branch (fbe6d6f) jq 1.7
.users[].metadata?.preferences?[]?.notifications?.email? null, null, "bob@example.com" null, "bob@example.com" "bob@example.com"
.users[].metadata?.preferences[].notifications?.email error null_access null, "bob@example.com" error: cannot iterate over null
... | values after either query "bob@example.com"

The proposal in #62 to skip missing values by default is not taken: plain access stays strict, which also covers the proposed !.

Checked at fbe6d6f with dune test --force --root .: 867 runtime and parser tests and 103 reader tests pass. Each commit's new tests were run against its parent first and failed there.

Risk: two-way door for the code; a revert restores the old ?. It is a breaking language change for users: queries that relied on ? giving null for a type mismatch, or on a null flowing into the next step after ?, give different output. CHANGES.md and docs/JQ_COMPATIBILITY.md describe it.

@coderabbitai

coderabbitai Bot commented Nov 9, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@davesnx davesnx closed this Nov 9, 2026
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.

Open discussion on query-json language design decisions

1 participant