From c2f5288a8c474cf29e4b16f9dad342ebfaf86365 Mon Sep 17 00:00:00 2001 From: Steven McCanne Date: Sun, 22 Nov 2026 15:03:32 -0800 Subject: [PATCH] another refinement of ?. semantics Make ?. turn a missing field into none, so this?.right?.color ?? "unpainted" works after a left join. Errors still pass through, and a field no type can have is still a compile error. The checker now types outer join output with the unmatched side as possibly missing, and anti join output as {left} only. So a bare right.color after a left join is flagged. The "field may be missing" error now suggests ok() or ?.. Updates the none/missing docs, adds runtime/ztests/expr/noneish-dot-missing.yaml, and updates join-expr.yaml for the new check. --- book/src/super-sql/types/none.md | 21 +++-- compiler/semantic/checker.go | 86 ++++++++++++++++++-- compiler/semantic/op.go | 5 +- compiler/semantic/ztests/agg-missing.yaml | 10 +-- compiler/semantic/ztests/cast-type.yaml | 2 +- compiler/semantic/ztests/missing-result.yaml | 18 ++-- compiler/semantic/ztests/none.yaml | 6 +- db/ztests/table-type.yaml | 2 +- runtime/expr/dot.go | 31 +++---- runtime/ztests/expr/arith-errors.yaml | 2 +- runtime/ztests/expr/complex-record-math.yaml | 8 +- runtime/ztests/expr/noneish-dot-missing.yaml | 58 +++++++++++++ runtime/ztests/op/join-expr.yaml | 6 +- 13 files changed, 199 insertions(+), 56 deletions(-) create mode 100644 runtime/ztests/expr/noneish-dot-missing.yaml diff --git a/book/src/super-sql/types/none.md b/book/src/super-sql/types/none.md index 06f46f502..6b9b9828a 100644 --- a/book/src/super-sql/types/none.md +++ b/book/src/super-sql/types/none.md @@ -15,7 +15,8 @@ what you want, usually by converting it to `none` with A plain `none` is the result of an expression that produces nothing, e.g., * [ok](../functions/generics/ok.md) of an error, -* `?.` applied to `none`, +* `?.` applied to `none` or to a field that's absent, e.g., `this?.a` where + `a` is missing, * a [type assertion](../expressions/type-assertions.md) that fails, * a [match](../expressions/match.md) with no matching case and no `else`, or @@ -87,23 +88,27 @@ values {a:1},{b:2} | values a ?? 0 # input # expected output -field may be missing (consider ok()) at line 1, column 29: +field may be missing (consider ok() or ?.) at line 1, column 29: values {a:1},{b:2} | values a ?? 0 ~ ``` -Note that `??` and `?.` handle `none`, not `missing`. To handle a field -that may be absent, convert it to `none` with +Note that `??` handles `none`, not `missing`. To handle a field +that may be absent, read it with `?.`, which produces `none` where the field +is absent, or convert it to `none` with [ok](../functions/generics/ok.md), then drop the `none` or replace it with `??`: ```mdtest-spq # spq -values {a:1},{b:2} | values {input:this, ok:a.ok(), with_default:a.ok() ?? 0} +values {a:1},{b:2} | values {input:this, ok:a.ok(), dot:this?.a, with_default:this?.a ?? 0} # input # expected output -{input:{a:1},ok:1,with_default:1} +{input:{a:1},ok:1,dot:1,with_default:1} {input:{b:2},with_default:0} ``` +`?.` handles only an absent field. A field that none of the values can have +is still reported, e.g., `this?.c` above is an error since neither record has +a field `c`, and an error value read with `?.` is passed through as is. ## Why Two Types? @@ -116,8 +121,8 @@ some of the input has the field. If an absent field quietly became `none`, such mistakes would silently drop values from the output. Keeping the two apart lets SuperSQL drop `none` quietly while still catching -every read of a field that may not be there. Writing `ok()` makes the choice -explicit: where the field is absent, treat it as nothing. +every read of a field that may not be there. Writing `?.` or `ok()` makes the +choice explicit: where the field is absent, treat it as nothing. This resembles the distinction between `null` and `missing` in [SQL++](https://asterixdb.apache.org/docs/0.9.7.1/sqlpp/manual.html), but in SuperSQL a field that isn't diff --git a/compiler/semantic/checker.go b/compiler/semantic/checker.go index f20f99104..237097df7 100644 --- a/compiler/semantic/checker.go +++ b/compiler/semantic/checker.go @@ -194,7 +194,41 @@ func (c *checker) join(types []super.Type, op *sem.JoinOp) super.Type { super.NewField(op.RightAlias, types[1]), }) c.expr(typ, op.Cond) - return typ + return c.joinType(op.Style, op.LeftAlias, op.RightAlias, types[0], types[1]) +} + +// joinType returns the type of the values produced by a join of the given +// style. An unmatched value of an outer join has no field for the other +// side, so that side may be missing, and an anti join produces only the +// left values. +func (c *checker) joinType(style, leftAlias, rightAlias string, left, right super.Type) super.Type { + switch style { + case "left": + right = c.orMissing(right) + case "right": + left = c.orMissing(left) + case "anti": + return c.t.sctx.MustLookupTypeRecord([]super.Field{ + super.NewField(leftAlias, left), + }) + } + return c.t.sctx.MustLookupTypeRecord([]super.Field{ + super.NewField(leftAlias, left), + super.NewField(rightAlias, right), + }) +} + +// orMissing returns the type of a field of type typ that may be missing. +func (c *checker) orMissing(typ super.Type) super.Type { + if hasUnknown(typ) { + return typ + } + typ = defuse(typ) + if hasMissing(typ) { + return typ + } + types := super.Flatten([]super.Type{typ, super.TypeMissing}) + return c.t.sctx.MustLookupTypeFusion(c.t.sctx.MustLookupTypeUnion(types)) } // unnest returns the type of the values produced by unnesting a value of type @@ -657,7 +691,7 @@ func defuse(typ super.Type) super.Type { return typ } -var errMissing = errors.New("field may be missing (consider ok())") +var errMissing = errors.New("field may be missing (consider ok() or ?.)") var errNone = errors.New("none not handled (consider ?? or ?.)") var errOption = errors.New("option value not handled (consider ??)") @@ -762,6 +796,31 @@ func (c *checker) derefNullish(loc ast.Node, typ super.Type, field string, nonei return out } +// missingAsNone returns typ with any missing type replaced by a plain none. +func (c *checker) missingAsNone(typ super.Type) super.Type { + under := defuse(typ) + if hasUnknown(under) || !hasMissing(under) { + return typ + } + rest, ok := c.withoutTypes(under, isMissingType) + if !ok { + return super.TypeNone + } + return c.orNone(rest) +} + +// hasField reports whether typ is a record type with a field named field or +// is a type other than a record type, i.e., whether a field access on a value +// of type typ is something other than a record without the field. +func hasField(typ super.Type, field string) bool { + rec, ok := coerce.Under(typ).(*super.TypeRecord) + if !ok || hasUnknown(rec) { + return true + } + _, ok = rec.IndexOfField(field) + return ok +} + // missingAsNull returns typ with any missing type replaced by null. func (c *checker) missingAsNull(typ super.Type) super.Type { under := defuse(typ) @@ -1381,7 +1440,12 @@ func (c *checker) deref(loc ast.Node, inType super.Type, field string, noneish b } return c.unknown } - return typ.Fields[which].Type + fieldType := typ.Fields[which].Type + if noneish { + // e?.f is a plain none when field f may be missing from e. + return c.missingAsNone(fieldType) + } + return fieldType case *super.TypeUnion: if hasNone(typ) { // The value may be a bare none (e.g., it's from ok()), which @@ -1408,11 +1472,18 @@ func (c *checker) deref(loc ast.Node, inType super.Type, field string, noneish b // found is true if a type has the field, i.e., other than null and // missing, whose fields are null and missing. var found bool + // none is true if e?.f is a plain none for a record type in the + // union without field f. + var none bool for _, t := range typ.Types { if t == super.TypeNull { types = append(types, t) continue } + if noneish && !hasField(t, field) { + none = true + continue + } c.pushErrs() fieldType := c.deref(loc, t, field, noneish) if len(c.popErrs()) == 0 { @@ -1424,7 +1495,8 @@ func (c *checker) deref(loc ast.Node, inType super.Type, field string, noneish b failed = append(failed, t) } } - if len(types) == 0 { + if len(types) == 0 || none && !found { + // No type has the field, so ?. doesn't apply. c.error(loc, fmt.Errorf("%w %q: . applied to %s", errNoSuchField, field, c.formatType(inType))) return c.unknown } @@ -1436,7 +1508,11 @@ func (c *checker) deref(loc ast.Node, inType super.Type, field string, noneish b } c.error(loc, err) } - return c.fuse(types) + out := c.fuse(types) + if none { + out = c.orNone(out) + } + return out case *super.TypeOption: // e?.f is a plain none when e is none and otherwise e.f, so its // type is typeof(e.f)|none, whereas e.f on an option doesn't handle diff --git a/compiler/semantic/op.go b/compiler/semantic/op.go index 7e4a748c1..3c2d90ad0 100644 --- a/compiler/semantic/op.go +++ b/compiler/semantic/op.go @@ -1361,8 +1361,9 @@ func (t *translator) joinOp(op *ast.JoinOp, parent sem.Seq, inTypes []super.Type RightAlias: rightAlias, Cond: cond, } + outType := t.checker.joinType(style, leftAlias, rightAlias, leftType, rightType) if subquery == nil { - return append(parent, join), typ + return append(parent, join), outType } if len(parent) == 0 { parent = append(parent, &sem.PassOp{Node: join}) @@ -1370,7 +1371,7 @@ func (t *translator) joinOp(op *ast.JoinOp, parent sem.Seq, inTypes []super.Type fork := &sem.ForkOp{ Paths: []sem.Seq{parent, subquery}, } - return sem.Seq{fork, join}, typ + return sem.Seq{fork, join}, outType } func (t *translator) pipeJoinCond(cond ast.JoinCond, leftAlias, rightAlias string, inType super.Type) sem.Expr { diff --git a/compiler/semantic/ztests/agg-missing.yaml b/compiler/semantic/ztests/agg-missing.yaml index 9dae20952..9edc5a934 100644 --- a/compiler/semantic/ztests/agg-missing.yaml +++ b/compiler/semantic/ztests/agg-missing.yaml @@ -7,7 +7,7 @@ input: | {x:3,y:"a"} error: | - field may be missing (consider ok()) at line 1, column 17: + field may be missing (consider ok() or ?.) at line 1, column 17: aggregate count(y) ~ @@ -35,7 +35,7 @@ input: | {x:3,y:"a"} error: | - field may be missing (consider ok()) at line 1, column 7: + field may be missing (consider ok() or ?.) at line 1, column 7: count(y) ~ @@ -50,7 +50,7 @@ input: | {x:3,b:false} error: | - field may be missing (consider ok()) at line 1, column 27: + field may be missing (consider ok() or ?.) at line 1, column 27: aggregate count() filter (b) ~ @@ -65,7 +65,7 @@ input: | {x:3,y:"a"} error: | - field may be missing (consider ok()) at line 1, column 21: + field may be missing (consider ok() or ?.) at line 1, column 21: aggregate sum(x) by y ~ @@ -80,7 +80,7 @@ input: | {x:3,y:"a"} error: | - field may be missing (consider ok()) at line 1, column 14: + field may be missing (consider ok() or ?.) at line 1, column 14: aggregate by y ~ diff --git a/compiler/semantic/ztests/cast-type.yaml b/compiler/semantic/ztests/cast-type.yaml index c1e71ac23..68d840e17 100644 --- a/compiler/semantic/ztests/cast-type.yaml +++ b/compiler/semantic/ztests/cast-type.yaml @@ -79,7 +79,7 @@ input: | {x:2,y:3} error: | - field may be missing (consider ok()) at line 1, column 8: + field may be missing (consider ok() or ?.) at line 1, column 8: values y::(int64|missing) ~~~~~~~~~~~~~~~~~~ diff --git a/compiler/semantic/ztests/missing-result.yaml b/compiler/semantic/ztests/missing-result.yaml index 66601f08a..2aba350d4 100644 --- a/compiler/semantic/ztests/missing-result.yaml +++ b/compiler/semantic/ztests/missing-result.yaml @@ -8,7 +8,7 @@ input: | {x:2,y:3} error: | - field may be missing (consider ok()) at line 1, column 8: + field may be missing (consider ok() or ?.) at line 1, column 8: values y ~ @@ -21,16 +21,16 @@ input: | {x:2,y:3} error: | - field may be missing (consider ok()) at line 1, column 11: + field may be missing (consider ok() or ?.) at line 1, column 11: values {x,y}, [y], map{'foo':y}, set[y] ~ - field may be missing (consider ok()) at line 1, column 16: + field may be missing (consider ok() or ?.) at line 1, column 16: values {x,y}, [y], map{'foo':y}, set[y] ~ - field may be missing (consider ok()) at line 1, column 30: + field may be missing (consider ok() or ?.) at line 1, column 30: values {x,y}, [y], map{'foo':y}, set[y] ~ - field may be missing (consider ok()) at line 1, column 38: + field may be missing (consider ok() or ?.) at line 1, column 38: values {x,y}, [y], map{'foo':y}, set[y] ~ @@ -44,7 +44,7 @@ input: | {x:2,y:3} error: | - field may be missing (consider ok()) at line 1, column 12: + field may be missing (consider ok() or ?.) at line 1, column 12: values map{y:1} ~ --- @@ -58,7 +58,7 @@ input: | {r:{x:1}} error: | - field may be missing (consider ok()) at line 1, column 1: + field may be missing (consider ok() or ?.) at line 1, column 1: r.y ~~~ @@ -94,10 +94,10 @@ input: | {x:2,y:3} error: | - field may be missing (consider ok()) at line 1, column 8: + field may be missing (consider ok() or ?.) at line 1, column 8: put z:=y | cut y ~ - field may be missing (consider ok()) at line 1, column 16: + field may be missing (consider ok() or ?.) at line 1, column 16: put z:=y | cut y ~ diff --git a/compiler/semantic/ztests/none.yaml b/compiler/semantic/ztests/none.yaml index bf693ff29..aec500415 100644 --- a/compiler/semantic/ztests/none.yaml +++ b/compiler/semantic/ztests/none.yaml @@ -19,7 +19,7 @@ input: | {y:1} error: | - field may be missing (consider ok()) at line 1, column 8: + field may be missing (consider ok() or ?.) at line 1, column 8: values x+1 ~ @@ -95,7 +95,7 @@ input: | {a:3} error: | - field may be missing (consider ok()) at line 1, column 8: + field may be missing (consider ok() or ?.) at line 1, column 8: values b ?? 1000 ~ @@ -165,6 +165,6 @@ input: | {s:1} error: | - field may be missing (consider ok()) at line 1, column 8: + field may be missing (consider ok() or ?.) at line 1, column 8: values r.f + 1 ~~~ diff --git a/db/ztests/table-type.yaml b/db/ztests/table-type.yaml index c187f5648..4293e594d 100644 --- a/db/ztests/table-type.yaml +++ b/db/ztests/table-type.yaml @@ -50,7 +50,7 @@ outputs: select z from t ~ === field may be missing - field may be missing (consider ok()) at line 1, column 20: + field may be missing (consider ok() or ?.) at line 1, column 20: from t | values {x,y} ~ === glob diff --git a/runtime/expr/dot.go b/runtime/expr/dot.go index 767699caa..4bbc2c090 100644 --- a/runtime/expr/dot.go +++ b/runtime/expr/dot.go @@ -87,20 +87,13 @@ func (d *DotExpr) eval(vecs ...vector.Any) vector.Any { case *vector.Record: i, ok := vec.Typ.IndexOfField(d.key) if !ok { - if d.nullish { - return vector.NewNull(vec.Len()) - } - return d.noFieldErr(vec) + return d.missing(vec) } out := vec.Fields[i] if _, ok := out.(*vector.Missing); ok { // A missing field (e.g., from a projection of a record type - // without the field) is the same as no field. For nullish - // expressions, turn missing into null. - if d.nullish { - return vector.NewNull(vec.Len()) - } - return d.noFieldErr(vec) + // without the field) is the same as no field. + return d.missing(vec) } if !hasUnionMissing(out) { return out @@ -109,10 +102,7 @@ func (d *DotExpr) eval(vecs ...vector.Any) vector.Any { unripped := vecs[0] ripped := vecs[1] if _, ok := ripped.(*vector.Missing); ok { - if d.nullish { - return vector.NewNull(ripped.Len()) - } - return d.noFieldErr(ripped) + return d.missing(ripped) } return unripped }, vector.AddNoRip(out), out) @@ -129,6 +119,19 @@ func (d *DotExpr) eval(vecs ...vector.Any) vector.Any { return vector.NewStringError(d.sctx, fmt.Sprintf("cannot access field '%s' of %s", d.key, kindName(vec.Type())), vec.Len()) } +// missing returns the result of accessing field d.key where it is missing +// from the values of vec: null for nullish expressions, a plain none for +// e?.f, which ?. has handled, and otherwise an error. +func (d *DotExpr) missing(vec vector.Any) vector.Any { + if d.nullish { + return vector.NewNull(vec.Len()) + } + if d.noneish { + return vector.NewNone(vec.Len()) + } + return d.noFieldErr(vec) +} + // noneOfField returns a plain none vector for e?.f where vec is an option // none. ?. handles only the none, so it's an error, as it would be for a // some, if the option's type has no field d.key. diff --git a/runtime/ztests/expr/arith-errors.yaml b/runtime/ztests/expr/arith-errors.yaml index 325577a11..b88f76de3 100644 --- a/runtime/ztests/expr/arith-errors.yaml +++ b/runtime/ztests/expr/arith-errors.yaml @@ -22,7 +22,7 @@ input: | {s:1} error: | - field may be missing (consider ok()) at line 1, column 1: + field may be missing (consider ok() or ?.) at line 1, column 1: s/0 ~ diff --git a/runtime/ztests/expr/complex-record-math.yaml b/runtime/ztests/expr/complex-record-math.yaml index 236cdfbb3..bf2a4f739 100644 --- a/runtime/ztests/expr/complex-record-math.yaml +++ b/runtime/ztests/expr/complex-record-math.yaml @@ -28,16 +28,16 @@ input: | {a:2::(int64|null),b:1::(int64|null)} error: | - field may be missing (consider ok()) at line 1, column 13: + field may be missing (consider ok() or ?.) at line 1, column 13: cut r:={x:a+b,y:a-b},a1:=[a],a2:=[a+b,a-b] ~ - field may be missing (consider ok()) at line 1, column 19: + field may be missing (consider ok() or ?.) at line 1, column 19: cut r:={x:a+b,y:a-b},a1:=[a],a2:=[a+b,a-b] ~ - field may be missing (consider ok()) at line 1, column 37: + field may be missing (consider ok() or ?.) at line 1, column 37: cut r:={x:a+b,y:a-b},a1:=[a],a2:=[a+b,a-b] ~ - field may be missing (consider ok()) at line 1, column 41: + field may be missing (consider ok() or ?.) at line 1, column 41: cut r:={x:a+b,y:a-b},a1:=[a],a2:=[a+b,a-b] ~ diff --git a/runtime/ztests/expr/noneish-dot-missing.yaml b/runtime/ztests/expr/noneish-dot-missing.yaml new file mode 100644 index 000000000..31eecc5f5 --- /dev/null +++ b/runtime/ztests/expr/noneish-dot-missing.yaml @@ -0,0 +1,58 @@ +# e?.f is a plain none when field f is missing from e, so ?? can supply +# a default, but a field no type of e has is still an error and other +# errors pass through. +script: | + super -s -c 'values this?.b ?? 0' r.sup + ! super -s -c 'values b' r.sup + ! super -s -c 'values this?.c' r.sup + echo '{a:1} {b:2} {a:error("boom")}' | super -s -c 'values this?.a ?? 0' - + echo === left join + super -s -c 'left join (from p.sup) on left.id=right.id | values this?.right?.color ?? "unpainted"' s.sup + ! super -s -c 'left join (from p.sup) on left.id=right.id | values right.color' s.sup + echo === right join + super -s -c 'right join (from s.sup) on left.id=right.id | values this?.left?.color ?? "unpainted"' p.sup + echo === anti join + ! super -s -c 'anti join (from p.sup) on left.id=right.id | values right' s.sup + +inputs: + - name: r.sup + data: | + {a:1,b:2} + {a:3} + - name: p.sup + data: | + {id:1,color:"red"} + - name: s.sup + data: | + {id:1} + {id:2} + +outputs: + - name: stdout + data: | + 2 + 0 + 1 + 0 + error("boom") + === left join + "red" + "unpainted" + === right join + "red" + "unpainted" + === anti join + - name: stderr + data: | + field may be missing (consider ok() or ?.) at line 1, column 8: + values b + ~ + no such field "c" at line 1, column 14: + values this?.c + ~ + field may be missing (consider ok() or ?.) at line 1, column 53: + left join (from p.sup) on left.id=right.id | values right.color + ~~~~~~~~~~~ + no such field "right" at line 1, column 53: + anti join (from p.sup) on left.id=right.id | values right + ~~~~~ diff --git a/runtime/ztests/op/join-expr.yaml b/runtime/ztests/op/join-expr.yaml index d8c9f8087..18bdb0820 100644 --- a/runtime/ztests/op/join-expr.yaml +++ b/runtime/ztests/op/join-expr.yaml @@ -3,9 +3,9 @@ script: | echo === super -s -c 'left join (from B.sup) as {l,r} on lower(r.s)=l.s | sort' A.sup echo === - super -s -c 'left join (from B.sup) on (lower(right.s))=(lower(left.s)) | values {...left,b:right.b } | sort' A.sup + super -s -c 'left join (from B.sup) on (lower(right.s))=(lower(left.s)) | values {...left,b:this?.right?.b} | sort' A.sup echo === nested loop join - super -s -c 'left join (from B.sup) on lower(left.s)