Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions book/src/super-sql/types/none.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?

Expand All @@ -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
Expand Down
86 changes: 81 additions & 5 deletions compiler/semantic/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ??)")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions compiler/semantic/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -1361,16 +1361,17 @@ 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})
}
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 {
Expand Down
10 changes: 5 additions & 5 deletions compiler/semantic/ztests/agg-missing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
~

Expand Down Expand Up @@ -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)
~

Expand All @@ -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)
~

Expand All @@ -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
~

Expand All @@ -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
~

Expand Down
2 changes: 1 addition & 1 deletion compiler/semantic/ztests/cast-type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
~~~~~~~~~~~~~~~~~~

Expand Down
18 changes: 9 additions & 9 deletions compiler/semantic/ztests/missing-result.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
~

Expand All @@ -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]
~

Expand All @@ -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}
~
---
Expand All @@ -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
~~~

Expand Down Expand Up @@ -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
~

Expand Down
6 changes: 3 additions & 3 deletions compiler/semantic/ztests/none.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
~

Expand Down Expand Up @@ -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
~

Expand Down Expand Up @@ -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
~~~
2 changes: 1 addition & 1 deletion db/ztests/table-type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading