Skip to content

promote options in equality comparisons - #7771

Open
philrz wants to merge 2 commits into
mainfrom
qdot-optional-fields
Open

philrz wants to merge 2 commits into
mainfrom
qdot-optional-fields

Conversation

@philrz

@philrz philrz commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Comparing an optional field with a plain value is an error on main (option value not handled (consider ??)), and the workaround, (f.ok() ?? null) == rhs, is long and easy to get wrong. With this PR, ==, != and in compare an option with a value that isn't an option by its payload, so some(1) == 1 is true and where x == 1 works on an optional field x. An option's none equals only none, so none::int64 == 1 is false and none::int64 != 1 is true. That's how a plain none already compares, and how Swift and Kotlin compare optionals. Ordering comparisons (<, <=, >, >=) are unchanged: the option must still be handled first, e.g., with ??, as in Swift, which dropped < on optionals because nil < 1 being true caused bugs. Hash join keys, switch cases, map indexing and nullif() treat options as == does, so some(1) and 1 match in each of them.

Since parse_uri() returns optional fields (#7767), this also makes where parse_uri(url).fragment == "top" work. On main, that query returns nothing, with no error.

Trying it out

On current tip of main, commit c5adf05:

$ super -version && super -c 'values {x?:1},{x?:none::int64},{x?:3} | where x == 1'
Version: v0.5.0-30-gc5adf0508
option(int64): option value not handled (consider ??) at line 1, column 47:
values {x?:1},{x?:none::int64},{x?:3} | where x == 1
                                              ~~~~~~

On this branch, commit 766d7f7:

$ super -version && super -c 'values {x?:1},{x?:none::int64},{x?:3} | where x == 1'
Version: v0.5.0-32-g766d7f748
{x?:1}

$ super -c 'values {x?:1},{x?:none::int64},{x?:3} | values {x, eq:x==1, ne:x!=1, in:x in [1,3]}'
{x?:1,eq:true,ne:false,in:true}
{x?:none::int64,eq:false,ne:true,in:false}
{x?:3,eq:false,ne:true,in:true}

Decisions

  • An option's none equals a plain none. none::int64 == none is true, as it is with another option's none. So where x == none finds an optional field's nones, a hash join matches an option none key with a plain none key (e.g., from ?. on a missing field), and m[none] finds a none::int64 key. Swift's x == nil and Kotlin's x == null work the same way.
  • Only an operand itself is promoted. An option nested in a record or array isn't: {a:some(1)} == {a:1} and [some(1)] == [1] are still type mismatches, as on main. For in, only the left-hand side is promoted. Options nested in the right-hand side were already searched by their payloads (1 in [some(1)] is true on main), and an option right-hand side (1 in some([1])) is still an error.
  • Hash joins, switch, map indexing and nullif() treat options as == does. In a join on several conditions, each key is promoted on its own, while a record written as a key (on {a:left.x} == {a:right.y}) is compared as a whole, so an option inside it isn't promoted. case some(1) now also matches 1, case 2 matches some(2), map{1:"a"}[some(1)] is "a", and nullif(x, 1) is null for {x?:1}. Beyond options, they match as on main, e.g., 1::int32 still doesn't join with 1 (Auto-casting of join keys #5071), though 1::int32 == 1 is true.
  • In switch, a none matches only a none case. Any none, an option's or a plain one (e.g., from this?.x on a missing field), goes to case none or case none::int64, as == says they're equal, and to no other case. On main, switch compares encoded bytes without types, so a plain none went to case 0 or case "" (whose encodings are empty like a none's), case none caught a plain 0, and an optional field's none missed case none. Other type-blind matches on main (e.g., case null catching 0 and "") are unchanged.
  • When two switch cases match the same values, the first one gets them. case 1 followed by case some(1) now match the same values, so 1 and some(1) go only to case 1. That's the rule the docs give for switch's second form, and the docs say the first form's case values "must be distinct". It also changes a literal duplicate (case 1 twice), which went to the last such case on main.
  • Grouping is unchanged. count() by x and distinct x keep {x?:1} and {x:1} apart, as they already keep 1 and 1::int32 apart although 1 == 1::int32 is true.
  • ?. is unchanged. It passes the field it reads through as is, so {y:this?.x} keeps y optional.
  • A comparison with none no longer skips BSUP frames. A frame's zone map leaves out nones, so where x == none, which this PR makes valid for an optional field, would skip frames that hold one. This also fixes where this?.a == none on main for a field that's missing from some records: it returns nothing from a file, though the same data on stdin gives the right answer. In the same code, an in list that holds anything but literals, e.g., where a in [1,b] on a file, no longer panics. That panic happens on main, and this PR's where x in [1,none::int64] hit it too.

Changes to existing tests

  • compiler/semantic/ztests/checker-compare.yaml: four stanzas expected an error for an option compared with a plain value by == or not in. r.x=="a" and the named-none stanza now expect results, some(a)=="x" now expects type mismatch: option(int64) == string, and the not in stanza uses some(1) not in ["a"] so it still checks an error.
  • compiler/semantic/ztests/cast-type.yaml: a stanza that used == to show an option type in an error message now uses <, which still reports it.
  • runtime/ztests/expr/index.yaml: map{1:"foo"}[some(1)] and map{none::int64:"bar"}[none] now find their entries, which adds two output lines.
  • bsup/ztests/issue-7061.yaml: where b==1 on an optional b now matches {a:1,b?:1} instead of failing.

Testing

  • New runtime/ztests/expr/compare-option.yaml covers ==, != and in with options, a plain none, untyped input, nullif() and map indexing, and ?. passing an option through. 9 of its 10 stanzas fail on main. The other one checks that ?. still passes the option through.
  • New runtime/ztests/op/join-option-key.yaml covers left, right and inner hash joins on an optional field, a join on two conditions, and a record written as a key. 3 of its 5 stanzas fail on main. The other two check that option keys still match each other and that an option in a record key still isn't promoted.
  • New runtime/ztests/op/switch-expr-option.yaml covers switch on options, a case some(...), two cases that match the same values, and none cases. All 5 stanzas fail on main.
  • Two new stanzas in bsup/ztests/zonemap-pruning.yaml check == none and in lists with a non-literal or a none on a file. Both fail on main (the second with the panic). The existing stanzas in checker-compare.yaml still check that < needs the option handled.
  • comparisons.md (with two new examples), containment.md, none.md (with a new example) and switch.md describe the change.
  • make test-unit, make test-system, make test-boomerangs and make test-heavy pass.

e?.f treated an option none to the left of the dot as a plain none but
gave an optional field f's value as the option, so comparing it with a
plain value was an error.  Now e?.f is f's value or, where f has none, a
plain none, as for a field that may be missing, so e?.f == "a" is true
or false rather than an error.  A plain . still gives the option.

The optimizer no longer replaces a ?. reference to an explicit field of
a record expression with the field's expression, which would keep the
option.  It also no longer uses a comparison with none to skip frames,
since zone maps omit nones.  Before, where this?.a == none on a file
could skip frames holding a none, including ones where a is missing.
@philrz philrz self-assigned this Dec 4, 2026
@philrz
philrz requested a review from mccanne December 4, 2026 21:00
Per review, move the option handling out of ?. and into comparisons,
and drop the ?. changes from the previous commit along with the
optimizer changes that followed from them.  ?. again passes the field
it reads through as is.

==, != and in now compare an option with a value that isn't an option
by its payload, so some(1) == 1 is true, while an option's none equals
only a none, so none::int64 == 1 is false and none::int64 != 1 is true.
Ordering comparisons still require the option to be handled.  Hash join
keys (including each key of a join on several conditions), switch
cases, map indexing and nullif() treat options as == does.  A record
written as a join key is still compared as a whole, as == compares it.
In switch, a none (an option's or not) matches only a case of a none,
so it no longer matches a case of 0 or "", whose encodings are empty
like its own.  When two switch cases match the same values, as case 1
and case some(1) now do, the first one receives them.

A comparison with none no longer skips BSUP frames, since zone maps
omit nones, so where x == none on an optional field x finds them.  The
pruner also no longer panics on an in list holding a non-literal, as
in where a in [1,b] on a file.
@philrz philrz changed the title make ?. unwrap optional fields promote options in equality comparisons Dec 5, 2026
@philrz

philrz commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Reworked per @mccanne's suggestion: comparisons now handle options, and ?. is back to how it works on main. I've updated the title and description to match. One call his outline left open: an option's none equals a plain none (none::int64 == none is true), as in Swift and Kotlin. The details are under Decisions.

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.

1 participant