Skip to content

Commit 21c48ca

Browse files
committed
Add vector.FlattenUnions
Add FlattenUnions to vector package that recursive flattens unions into then dynamic if necessary.
1 parent 6cb5b3d commit 21c48ca

5 files changed

Lines changed: 44 additions & 24 deletions

File tree

runtime/vam/expr/mapcall.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,7 @@ func (m *mapCall) eval(vecs ...vector.Any) vector.Any {
3535
}
3636
inner = m.lambda.Eval(inner)
3737
if d, ok := inner.(*vector.Dynamic); ok {
38-
var typs []super.Type
39-
for _, vec := range d.Values {
40-
typs = append(typs, vec.Type())
41-
}
42-
utyp, ok := m.sctx.LookupTypeUnion(super.UniqueTypes(typs))
43-
if !ok {
44-
//XXX need to vector deunion
45-
panic(typs)
46-
}
47-
inner = vector.NewUnion(utyp, d.Tags, d.Values)
38+
inner = vector.NewUnionFromDynamic(m.sctx, vector.FlattenUnions(d))
4839
}
4940
var out vector.Any
5041
switch vec := vec.(type) {

runtime/vam/expr/mapexpr.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ func (m *MapExpr) build(vecs []vector.Any) vector.Any {
6969
}
7070
out := vb.Build(m.sctx)
7171
if d, ok := out.(*vector.Dynamic); ok {
72-
utyp, ok := m.sctx.LookupTypeUnion(super.UniqueTypes(typs))
73-
if !ok {
74-
//XXX need vector deunion
75-
panic(typs)
76-
}
77-
out = &vector.Union{Typ: utyp, Dynamic: d}
72+
out = vector.NewUnionFromDynamic(m.sctx, vector.FlattenUnions(d))
7873
}
7974
return out
8075
}

runtime/vam/expr/quiet.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ func (d *Dequiet) dequiet(vec vector.Any) vector.Any {
9393
index := b.ToArray()
9494
vec = vector.ReversePick(vec, index)
9595
out := vector.Combine(vec, index, quiet).(*vector.Dynamic)
96-
utyp, ok := d.sctx.LookupTypeUnion([]super.Type{vec.Type(), quiet.Type()})
97-
if !ok {
98-
// XXX need to deunion vec when it's a union
99-
panic(vec.Type())
100-
}
101-
return vector.NewUnion(utyp, out.Tags, out.Values)
96+
return vector.NewUnionFromDynamic(d.sctx, vector.FlattenUnions(out))
10297
}
10398

10499
func (d *Dequiet) quietTmp(n uint32) vector.Any {

runtime/ztests/expr/map-func.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ output: |
3434
spq: |
3535
values map([1,2,3], lambda x: x=1?1:'foo'::(bool|string))
3636
37-
# XXX this will be re-enabled before VENTURA
38-
vector: false
37+
vector: true
3938

4039
output: |
4140
[1,"foo","foo"]::[int64|bool|string]

vector/union.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package vector
22

33
import (
4+
"slices"
5+
46
"github.com/brimdata/super"
57
"github.com/brimdata/super/scode"
68
)
@@ -49,3 +51,41 @@ func Deunion(vec Any) Any {
4951
}
5052
return vec
5153
}
54+
55+
// FlattenUnions takes a Dynamic and recursively replaces any Union values
56+
// with their inner values, rewriting tags so that each slot points directly
57+
// to the leaf value vector.
58+
func FlattenUnions(d *Dynamic) *Dynamic {
59+
hasUnion := slices.ContainsFunc(d.Values, func(vec Any) bool {
60+
_, ok := vec.(*Union)
61+
return ok
62+
})
63+
if !hasUnion {
64+
return d
65+
}
66+
bases := make([]uint32, len(d.Values))
67+
unions := make([]*Dynamic, len(d.Values))
68+
var newValues []Any
69+
for i, val := range d.Values {
70+
bases[i] = uint32(len(newValues))
71+
if u, ok := val.(*Union); ok {
72+
flat := FlattenUnions(u.Dynamic)
73+
unions[i] = flat
74+
newValues = append(newValues, flat.Values...)
75+
} else {
76+
newValues = append(newValues, val)
77+
}
78+
}
79+
forward := d.ForwardTagMap()
80+
newTags := make([]uint32, len(d.Tags))
81+
for slot, oldTag := range d.Tags {
82+
base := bases[oldTag]
83+
if d := unions[oldTag]; d != nil {
84+
innerSlot := forward[slot]
85+
newTags[slot] = base + d.Tags[innerSlot]
86+
} else {
87+
newTags[slot] = base
88+
}
89+
}
90+
return NewDynamic(newTags, newValues)
91+
}

0 commit comments

Comments
 (0)