Skip to content

Commit

Permalink
vector: Support Enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Jan 7, 2025
1 parent dd555cf commit 20f335b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
16 changes: 14 additions & 2 deletions runtime/vam/expr/arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (a *Arith) Eval(val vector.Any) vector.Any {
}

func (a *Arith) eval(vecs ...vector.Any) (out vector.Any) {
lhs := vector.Under(vecs[0])
rhs := vector.Under(vecs[1])
lhs := enumToIndex(vector.Under(vecs[0]))
rhs := enumToIndex(vector.Under(vecs[1]))
lhs, rhs, errVal := coerceVals(a.zctx, lhs, rhs)
if errVal != nil {
return errVal
Expand Down Expand Up @@ -67,6 +67,18 @@ func (a *Arith) eval(vecs ...vector.Any) (out vector.Any) {
return vector.CopyAndSetNulls(out, vector.Or(vector.NullsOf(lhs), vector.NullsOf(rhs)))
}

func enumToIndex(vec vector.Any) vector.Any {
switch vec := vec.(type) {
case *vector.View:
if enum, ok := vec.Any.(*vector.Enum); ok {
return vector.NewView(enum.Uint, vec.Index)
}
case *vector.Enum:
return vec.Uint
}
return vec
}

func (a *Arith) evalDivideByZero(kind vector.Kind, lhs, rhs vector.Any) vector.Any {
var errs []uint32
var out vector.Any
Expand Down
12 changes: 12 additions & 0 deletions runtime/vam/expr/cast/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ func castToString(vec vector.Any, index []uint32) (vector.Any, []uint32, bool) {
bytes = append(bytes, vec.Values[idx].String()...)
offs = append(offs, uint32(len(bytes)))
}
case *vector.Enum:
for i := range n {
idx := i
if index != nil {
idx = index[i]
}
if !nulls.Value(i) {
val := vec.Uint.Values[idx]
bytes = append(bytes, vec.Typ.Symbols[val]...)
}
offs = append(offs, uint32(len(bytes)))
}
default:
var b zcode.Builder
for i := range n {
Expand Down
8 changes: 8 additions & 0 deletions runtime/vcache/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ func (l *loader) loadVals(typ super.Type, s *primitive, nulls *vector.Bool) (vec
}
offs[length] = off
return vector.NewTypeValue(offs, bytes, nulls), nil
case *super.TypeEnum:
values := make([]uint64, length)
for slot := range length {
if !nulls.Value(slot) {
values[slot] = super.DecodeUint(it.Next())
}
}
return vector.NewEnum(typ, values, nulls), nil
case *super.TypeOfNull:
return vector.NewConst(super.Null, s.length(), nil), nil
}
Expand Down
2 changes: 2 additions & 0 deletions ztests/enum.yaml → runtime/ztests/expr/enum.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
zed: "put s:=string(e), v:=e+1"

vector: true

input: |
{e:%foo(enum(foo,bar,baz))}
{e:%bar(enum(foo,bar,baz))}
Expand Down
7 changes: 7 additions & 0 deletions vector/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ func NullsOf(v Any) *Bool {
return v.Nulls
case *Dict:
return v.Nulls
case *Enum:
return v.Nulls
case *Error:
return Or(v.Nulls, NullsOf(v.Vals))
case *Float:
Expand Down Expand Up @@ -254,6 +256,11 @@ func CopyAndSetNulls(v Any, nulls *Bool) Any {
Counts: v.Counts,
Nulls: nulls,
}
case *Enum:
return &Enum{
Typ: v.Typ,
Uint: CopyAndSetNulls(v.Uint, nulls).(*Uint),
}
case *Error:
return &Error{
Typ: v.Typ,
Expand Down
19 changes: 19 additions & 0 deletions vector/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package vector

import (
"github.com/brimdata/super"
)

type Enum struct {
*Uint
Typ *super.TypeEnum
}

func NewEnum(typ *super.TypeEnum, vals []uint64, nulls *Bool) *Enum {
return &Enum{
Typ: typ,
Uint: NewUint(super.TypeUint64, vals, nulls),
}
}

func (e *Enum) Type() super.Type { return e.Typ }
2 changes: 2 additions & 0 deletions vng/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func NewEncoder(typ super.Type) Encoder {
return NewNullsEncoder(NewMapEncoder(typ))
case *super.TypeUnion:
return NewNullsEncoder(NewUnionEncoder(typ))
case *super.TypeEnum:
return NewNullsEncoder(NewPrimitiveEncoder(typ, false))
default:
if !super.IsPrimitiveType(typ) {
panic(fmt.Sprintf("unsupported type in VNG file: %T", typ))
Expand Down

0 comments on commit 20f335b

Please sign in to comment.