Repository navigation
Conversation
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.
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.
Contributor
Author
|
Reworked per @mccanne's suggestion: comparisons now handle options, and |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,==,!=andincompare an option with a value that isn't an option by its payload, sosome(1) == 1is true andwhere x == 1works on an optional fieldx. An option'snoneequals onlynone, sonone::int64 == 1is false andnone::int64 != 1is true. That's how a plainnonealready 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 becausenil < 1being true caused bugs. Hash join keys,switchcases, map indexing andnullif()treat options as==does, sosome(1)and1match in each of them.Since
parse_uri()returns optional fields (#7767), this also makeswhere parse_uri(url).fragment == "top"work. Onmain, that query returns nothing, with no error.Trying it out
On current tip of
main, commit c5adf05:On this branch, commit 766d7f7:
Decisions
noneequals a plainnone.none::int64 == noneis true, as it is with another option'snone. Sowhere x == nonefinds an optional field's nones, a hash join matches an optionnonekey with a plainnonekey (e.g., from?.on a missing field), andm[none]finds anone::int64key. Swift'sx == niland Kotlin'sx == nullwork the same way.{a:some(1)} == {a:1}and[some(1)] == [1]are still type mismatches, as onmain. Forin, 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 onmain), and an option right-hand side (1 in some([1])) is still an error.switch, map indexing andnullif()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 matches1,case 2matchessome(2),map{1:"a"}[some(1)]is"a", andnullif(x, 1)isnullfor{x?:1}. Beyond options, they match as onmain, e.g.,1::int32still doesn't join with1(Auto-casting of join keys #5071), though1::int32 == 1is true.switch, anonematches only anonecase. Anynone, an option's or a plain one (e.g., fromthis?.xon a missing field), goes tocase noneorcase none::int64, as==says they're equal, and to no other case. Onmain,switchcompares encoded bytes without types, so a plainnonewent tocase 0orcase ""(whose encodings are empty like anone's),case nonecaught a plain0, and an optional field'snonemissedcase none. Other type-blind matches onmain(e.g.,case nullcatching0and"") are unchanged.switchcases match the same values, the first one gets them.case 1followed bycase some(1)now match the same values, so1andsome(1)go only tocase 1. That's the rule the docs give forswitch's second form, and the docs say the first form's case values "must be distinct". It also changes a literal duplicate (case 1twice), which went to the last such case onmain.count() by xanddistinct xkeep{x?:1}and{x:1}apart, as they already keep1and1::int32apart although1 == 1::int32is true.?.is unchanged. It passes the field it reads through as is, so{y:this?.x}keepsyoptional.noneno longer skips BSUP frames. A frame's zone map leaves out nones, sowhere x == none, which this PR makes valid for an optional field, would skip frames that hold one. This also fixeswhere this?.a == noneonmainfor 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, aninlist that holds anything but literals, e.g.,where a in [1,b]on a file, no longer panics. That panic happens onmain, and this PR'swhere 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==ornot in.r.x=="a"and the named-nonestanza now expect results,some(a)=="x"now expectstype mismatch: option(int64) == string, and thenot instanza usessome(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)]andmap{none::int64:"bar"}[none]now find their entries, which adds two output lines.bsup/ztests/issue-7061.yaml:where b==1on an optionalbnow matches{a:1,b?:1}instead of failing.Testing
runtime/ztests/expr/compare-option.yamlcovers==,!=andinwith options, a plainnone, untyped input,nullif()and map indexing, and?.passing an option through. 9 of its 10 stanzas fail onmain. The other one checks that?.still passes the option through.runtime/ztests/op/join-option-key.yamlcovers 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 onmain. The other two check that option keys still match each other and that an option in a record key still isn't promoted.runtime/ztests/op/switch-expr-option.yamlcoversswitchon options, acase some(...), two cases that match the same values, andnonecases. All 5 stanzas fail onmain.bsup/ztests/zonemap-pruning.yamlcheck== noneandinlists with a non-literal or anoneon a file. Both fail onmain(the second with the panic). The existing stanzas inchecker-compare.yamlstill check that<needs the option handled.comparisons.md(with two new examples),containment.md,none.md(with a new example) andswitch.mddescribe the change.make test-unit,make test-system,make test-boomerangsandmake test-heavypass.