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
15 changes: 15 additions & 0 deletions book/src/super-sql/types/record.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ The spread form inserts all of the fields from the resulting record.
If a spread expression results in a non-record type (e.g., errors), then that
part of the record is simply elided. Note that the field names for
the spread come from the constituent record values.
When the spread expression results in a [union](union.md) value, the value
under the union is spread, so a record member of the union contributes its
fields and a member that isn't a record contributes none, e.g.,
```mdtest-spq {data-layout="stacked"}
# spq
values {...this, x:1}
# input
{a:1}::({a:int64}|{b:string})
{b:"foo"}::({a:int64}|{b:string})
1::(int64|{a:int64})
# expected output
{a:1,x:1}
{b:"foo",x:1}
{x:1}
```

The fields of a record expression are evaluated left to right and when
field names collide the rightmost instance of the name determines that
Expand Down
54 changes: 51 additions & 3 deletions compiler/semantic/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,56 @@ func (c *checker) recordElems(typ super.Type, elems []sem.RecordElem) super.Type
return c.formRecordElems(elems, types)
}

// maxSpreadAlternatives limits the number of combinations of union members
// that formRecordElems considers for spreads of unions.
const maxSpreadAlternatives = 64

func (c *checker) formRecordElems(elems []sem.RecordElem, types []super.Type) super.Type {
// The value spread from a union is one of its members, so the record
// is one of the records formed from each combination of the members
// of the spread unions.
alts := [][]super.Type{nil}
for k, elem := range elems {
if _, ok := elem.(*sem.SpreadElem); !ok {
continue
}
union, ok := coerce.Under(types[k]).(*super.TypeUnion)
if !ok {
continue
}
if len(alts)*len(union.Types) > maxSpreadAlternatives {
return c.unknown
}
var next [][]super.Type
for _, alt := range alts {
for _, t := range union.Types {
next = append(next, append(slices.Clone(alt), t))
}
}
alts = next
}
if len(alts[0]) == 0 {
return c.formRecord(elems, types)
}
var out []super.Type
for _, alt := range alts {
altTypes := slices.Clone(types)
var i int
for k, elem := range elems {
if _, ok := elem.(*sem.SpreadElem); !ok {
continue
}
if _, ok := coerce.Under(types[k]).(*super.TypeUnion); ok {
altTypes[k] = alt[i]
i++
}
}
out = append(out, c.formRecord(elems, altTypes))
}
return c.fuse(out)
}

func (c *checker) formRecord(elems []sem.RecordElem, types []super.Type) super.Type {
var fields []super.Field
order := make(map[string]int)
for k, elem := range elems {
Expand All @@ -907,11 +956,10 @@ func (c *checker) formRecordElems(elems []sem.RecordElem, types []super.Type) su
// know the result at all. Return unknown for the whole thing.
return c.unknown
}
// If it's not unknown, it should have passed type checking as a record.
// A value that isn't a record contributes no fields.
recType, ok := coerce.Under(typ).(*super.TypeRecord)
if !ok {
//XXX
return c.unknown
continue
}
for _, f := range recType.Fields {
if k, ok := order[f.Name]; ok {
Expand Down
14 changes: 13 additions & 1 deletion runtime/expr/recordexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ func (r *recordExpr) Eval(this vector.Any) vector.Any {
}

func (r *recordExpr) eval(vecs ...vector.Any) vector.Any {
for k, vec := range vecs {
if _, ok := r.elems[k].(*SpreadElem); !ok {
continue
}
if union, ok := vector.Under(vec).(*vector.Union); ok {
// As with field access, spread the value under a union so
// that each record in the union contributes its fields.
vecs = slices.Clone(vecs)
vecs[k] = union.Dynamic()
return vector.Apply(vector.ApplyNone, r.eval, vecs...)
}
}
r.fields = r.fields[:0]
clear(r.fieldIndexes)
r.fieldVecs = make([]vector.Any, 0, len(r.elems))
Expand Down Expand Up @@ -121,7 +133,7 @@ func (r *recordExpr) deleteField(name string) {
}

func (r *recordExpr) spread(vec vector.Any) {
// Ignore non-record values.
// A value that isn't a record contributes no fields.
switch vec := vector.Under(vec).(type) {
case *vector.Record:
for k, f := range super.TypeRecordOf(vec.Type()).Fields {
Expand Down
64 changes: 64 additions & 0 deletions runtime/ztests/expr/spread-union.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Spreading a union value spreads the record under the union, as field access
# does, so each record member contributes its fields. A member that isn't a
# record contributes no fields, just as spreading any non-record value does.
# The type checker types the result as the records formed from each member.
script: |
super -s -c 'values {...this, big:true}' shapes.sup
echo ===
super -s -c 'values {...this, big:true}' - < shapes.sup
echo ===
super -s -c 'values {...this, x:1}' unnamed.sup
echo ===
super -s -c 'values {...this, x:1}' nonrecord.sup
echo ===
super -s -c 'values {...u, ...w, x:1}' two.sup
echo ===
super -s -c 'values {...this, big:true} | values {id, r:this?.dimensions?.radius ?? 0}' shapes.sup
! super -s -c 'values {...this, big:true} | values nope' shapes.sup

inputs:
- name: shapes.sup
data: |
type Circle={id:int64,type:string,dimensions:{radius:int64}}
type Rectangle={id:int64,type:string,dimensions:{width:int64,height:int64}}
type Shape=Circle|Rectangle
{id:1,type:"Rectangle",dimensions:{width:2,height:1}}::Rectangle::Shape
{id:2,type:"Circle",dimensions:{radius:3}}::Circle::Shape
- name: unnamed.sup
data: |
{a:1}::({a:int64}|{b:string})
{b:"x"}::({a:int64}|{b:string})
- name: nonrecord.sup
data: |
1::(int64|{a:int64})
{a:2}::(int64|{a:int64})
- name: two.sup
data: |
{u:{a:1}::({a:int64}|{b:string}),w:{c:3}::({c:int64}|int64)}
{u:{b:"y"}::({a:int64}|{b:string}),w:5::({c:int64}|int64)}

outputs:
- name: stdout
data: |
{id:1,type:"Rectangle",dimensions:{width:2,height:1},big:true}
{id:2,type:"Circle",dimensions:{radius:3},big:true}
===
{id:1,type:"Rectangle",dimensions:{width:2,height:1},big:true}
{id:2,type:"Circle",dimensions:{radius:3},big:true}
===
{a:1,x:1}
{b:"x",x:1}
===
{x:1}
{a:2,x:1}
===
{a:1,c:3,x:1}
{b:"y",x:1}
===
{id:1,r:0}
{id:2,r:3}
- name: stderr
data: |
no such field "nope" at line 1, column 37:
values {...this, big:true} | values nope
~~~~
Loading