From b50bc49c52f06fc076b87e02c4dff17307330c47 Mon Sep 17 00:00:00 2001 From: Noah Treuhaft Date: Mon, 26 Jan 2026 16:56:01 -0500 Subject: [PATCH] vector: replace KindOf function with Any.Kind method --- runtime/vam/expr/arith.go | 8 +-- runtime/vam/expr/compare.go | 4 +- runtime/vam/expr/function/math.go | 2 +- runtime/vam/expr/function/string.go | 2 +- runtime/vam/expr/function/types.go | 2 +- runtime/vam/expr/index.go | 2 +- runtime/vam/expr/logic.go | 3 +- runtime/vam/expr/search.go | 2 +- runtime/vam/expr/slice.go | 4 +- vector/any.go | 1 + vector/array.go | 4 ++ vector/bool.go | 4 ++ vector/bytes.go | 4 ++ vector/const.go | 29 +++++++++++ vector/dynamic.go | 4 ++ vector/enum.go | 2 + vector/error.go | 4 ++ vector/float.go | 4 ++ vector/int.go | 4 ++ vector/ip.go | 4 ++ vector/kind.go | 78 ++--------------------------- vector/map.go | 4 ++ vector/net.go | 4 ++ vector/record.go | 4 ++ vector/set.go | 4 ++ vector/string.go | 4 ++ vector/type.go | 4 ++ vector/uint.go | 4 ++ vector/union.go | 4 ++ 29 files changed, 114 insertions(+), 89 deletions(-) diff --git a/runtime/vam/expr/arith.go b/runtime/vam/expr/arith.go index f1563bcfe3..842e665457 100644 --- a/runtime/vam/expr/arith.go +++ b/runtime/vam/expr/arith.go @@ -31,18 +31,18 @@ func (a *Arith) Eval(val vector.Any) vector.Any { func (a *Arith) eval(vecs ...vector.Any) (out vector.Any) { lhs := enumToIndex(vector.Under(vecs[0])) rhs := enumToIndex(vector.Under(vecs[1])) - if k := vector.KindOf(lhs); k == vector.KindNull || k == vector.KindError { + if k := lhs.Kind(); k == vector.KindNull || k == vector.KindError { return lhs } - if k := vector.KindOf(rhs); k == vector.KindNull || k == vector.KindError { + if k := rhs.Kind(); k == vector.KindNull || k == vector.KindError { return rhs } lhs, rhs, errVal := coerceVals(a.sctx, lhs, rhs) if errVal != nil { return errVal } - kind := vector.KindOf(lhs) - if kind != vector.KindOf(rhs) { + kind := lhs.Kind() + if kind != rhs.Kind() { panic(fmt.Sprintf("vector kind mismatch after coerce (%#v and %#v)", lhs, rhs)) } if kind == vector.KindFloat && a.opCode == vector.ArithMod { diff --git a/runtime/vam/expr/compare.go b/runtime/vam/expr/compare.go index 433d4d65d0..7fdffb02e6 100644 --- a/runtime/vam/expr/compare.go +++ b/runtime/vam/expr/compare.go @@ -47,8 +47,8 @@ func (c *Compare) eval(vecs ...vector.Any) vector.Any { return vector.NewConst(super.False, vecs[0].Len(), nulls) } //XXX need to handle overflow (see sam) - kind := vector.KindOf(lhs) - if kind != vector.KindOf(rhs) { + kind := lhs.Kind() + if kind != rhs.Kind() { panic("vector kind mismatch after coerce") } switch kind { diff --git a/runtime/vam/expr/function/math.go b/runtime/vam/expr/function/math.go index 788bc44a9a..7c6d974ebb 100644 --- a/runtime/vam/expr/function/math.go +++ b/runtime/vam/expr/function/math.go @@ -141,7 +141,7 @@ type Log struct { func (l *Log) Call(args ...vector.Any) vector.Any { arg := vector.Under(args[0]) if !super.IsNumber(arg.Type().ID()) { - if vector.KindOf(arg) == vector.KindError { + if arg.Kind() == vector.KindError { return arg } return vector.NewWrappedError(l.sctx, "log: not a number", arg) diff --git a/runtime/vam/expr/function/string.go b/runtime/vam/expr/function/string.go index 0c40842af4..c496bd2052 100644 --- a/runtime/vam/expr/function/string.go +++ b/runtime/vam/expr/function/string.go @@ -17,7 +17,7 @@ type Concat struct { func (c *Concat) Call(args ...vector.Any) vector.Any { args = underAll(args) for _, arg := range args { - switch vector.KindOf(arg) { + switch arg.Kind() { case vector.KindError: return arg case vector.KindString: diff --git a/runtime/vam/expr/function/types.go b/runtime/vam/expr/function/types.go index bdf0bbbfd8..1a5bc9f0cd 100644 --- a/runtime/vam/expr/function/types.go +++ b/runtime/vam/expr/function/types.go @@ -115,7 +115,7 @@ type IsErr struct{} func (IsErr) Call(args ...vector.Any) vector.Any { vec := vector.Under(args[0]) - if vector.KindOf(vec) != vector.KindError { + if vec.Kind() != vector.KindError { return vector.NewConst(super.False, vec.Len(), bitvec.Zero) } nulls := vector.NullsOf(vec) diff --git a/runtime/vam/expr/index.go b/runtime/vam/expr/index.go index a1b07b8794..c26de5688b 100644 --- a/runtime/vam/expr/index.go +++ b/runtime/vam/expr/index.go @@ -28,7 +28,7 @@ func (i *Index) eval(args ...vector.Any) vector.Any { this := args[0] container := vector.Under(i.container.Eval(this)) index := vector.Under(i.index.Eval(this)) - switch vector.KindOf(container) { + switch container.Kind() { case vector.KindArray, vector.KindSet: return indexArrayOrSet(i.sctx, container, index, i.base1) case vector.KindRecord: diff --git a/runtime/vam/expr/logic.go b/runtime/vam/expr/logic.go index 429d68fa85..6011f79111 100644 --- a/runtime/vam/expr/logic.go +++ b/runtime/vam/expr/logic.go @@ -154,7 +154,8 @@ func orError(err, vec vector.Any) vector.Any { func evalBool(sctx *super.Context, fn func(...vector.Any) vector.Any, vecs ...vector.Any) vector.Any { return vector.Apply(false, func(vecs ...vector.Any) vector.Any { for i, vec := range vecs { - if vec := vector.Under(vec); vec.Type() == super.TypeBool || vector.KindOf(vec) == vector.KindError { + vec := vector.Under(vec) + if k := vec.Kind(); k == vector.KindBool || k == vector.KindError { vecs[i] = vec } else { vecs[i] = vector.NewWrappedError(sctx, "not type bool", vec) diff --git a/runtime/vam/expr/search.go b/runtime/vam/expr/search.go index df01c067bf..0b97a88a48 100644 --- a/runtime/vam/expr/search.go +++ b/runtime/vam/expr/search.go @@ -29,7 +29,7 @@ func NewSearch(s string, val super.Value, e Evaluator) Evaluator { } eq := NewCompare(super.NewContext() /* XXX */, "==", nil, nil) vectorPred := func(vec vector.Any) vector.Any { - if net.IsValid() && vector.KindOf(vec) == vector.KindIP { + if net.IsValid() && vec.Kind() == vector.KindIP { out := vector.NewFalse(vec.Len()) for i := range vec.Len() { if ip, null := vector.IPValue(vec, i); !null && net.Contains(ip) { diff --git a/runtime/vam/expr/slice.go b/runtime/vam/expr/slice.go index 25d4d8b089..6ebe424ebc 100644 --- a/runtime/vam/expr/slice.go +++ b/runtime/vam/expr/slice.go @@ -53,7 +53,7 @@ func (s *sliceExpr) eval(vecs ...vector.Any) vector.Any { return vector.NewStringError(s.sctx, "slice index is not a number", to.Len()) } } - switch vector.KindOf(container) { + switch container.Kind() { case vector.KindArray, vector.KindSet: return s.evalArrayOrSlice(container, from, to, s.base1) case vector.KindBytes, vector.KindString: @@ -116,7 +116,7 @@ func (s *sliceExpr) evalArrayOrSlice(vec, fromVec, toVec vector.Any, base1 bool) } var out vector.Any inner = vector.Pick(inner, innerIndex) - if vector.KindOf(vec) == vector.KindArray { + if vec.Kind() == vector.KindArray { out = vector.NewArray(vec.Type().(*super.TypeArray), newOffsets, inner, nullsOut) } else { out = vector.NewSet(vec.Type().(*super.TypeSet), newOffsets, inner, nullsOut) diff --git a/vector/any.go b/vector/any.go index bad2e4aaf7..f39aadab1c 100644 --- a/vector/any.go +++ b/vector/any.go @@ -7,6 +7,7 @@ import ( type Any interface { Type() super.Type + Kind() Kind Len() uint32 Serialize(*scode.Builder, uint32) } diff --git a/vector/array.go b/vector/array.go index 596fa9b457..74e1b81b47 100644 --- a/vector/array.go +++ b/vector/array.go @@ -19,6 +19,10 @@ func NewArray(typ *super.TypeArray, offsets []uint32, values Any, nulls bitvec.B return &Array{Typ: typ, Offsets: offsets, Values: values, Nulls: nulls} } +func (*Array) Kind() Kind { + return KindArray +} + func (a *Array) Type() super.Type { return a.Typ } diff --git a/vector/bool.go b/vector/bool.go index a4680e1c5f..91d2b848fd 100644 --- a/vector/bool.go +++ b/vector/bool.go @@ -35,6 +35,10 @@ func NewTrue(length uint32) *Bool { return NewBool(bitvec.NewTrue(length), bitvec.Zero) } +func (*Bool) Kind() Kind { + return KindBool +} + func (b *Bool) Type() super.Type { return super.TypeBool } diff --git a/vector/bytes.go b/vector/bytes.go index 84851eaa1d..1f2050f1da 100644 --- a/vector/bytes.go +++ b/vector/bytes.go @@ -26,6 +26,10 @@ func (b *Bytes) Append(v []byte) { b.table.Append(v) } +func (*Bytes) Kind() Kind { + return KindBytes +} + func (b *Bytes) Type() super.Type { return super.TypeBytes } diff --git a/vector/const.go b/vector/const.go index 53bfb028b3..01208a4f18 100644 --- a/vector/const.go +++ b/vector/const.go @@ -1,6 +1,8 @@ package vector import ( + "fmt" + "github.com/brimdata/super" "github.com/brimdata/super/runtime/sam/expr/coerce" "github.com/brimdata/super/scode" @@ -22,6 +24,33 @@ func NewConst(val super.Value, len uint32, nulls bitvec.Bits) *Const { return &Const{val: val, len: len, Nulls: nulls} } +func (c *Const) Kind() Kind { + // c.val must be a primitive. + switch id := c.val.Type().ID(); { + case super.IsUnsigned(id): + return KindUint + case super.IsSigned(id): + return KindInt + case super.IsFloat(id): + return KindFloat + case id == super.IDBool: + return KindBool + case id == super.IDBytes: + return KindBytes + case id == super.IDString: + return KindString + case id == super.IDIP: + return KindIP + case id == super.IDNet: + return KindNet + case id == super.IDType: + return KindType + case id == super.IDNull: + return KindNull + } + panic(fmt.Sprintf("%#v\n", super.TypeUnder(c.val.Type()))) +} + func (c *Const) Type() super.Type { return c.val.Type() } diff --git a/vector/dynamic.go b/vector/dynamic.go index 2ddd9301a0..f20486dfff 100644 --- a/vector/dynamic.go +++ b/vector/dynamic.go @@ -29,6 +29,10 @@ func NewDynamic(tags []uint32, values []Any) *Dynamic { return &Dynamic{Tags: tags, Values: values} } +func (*Dynamic) Kind() Kind { + return KindInvalid +} + func (*Dynamic) Type() super.Type { panic("can't call Type() on a vector.Dynamic") } diff --git a/vector/enum.go b/vector/enum.go index 9254e9e135..df6271b0c9 100644 --- a/vector/enum.go +++ b/vector/enum.go @@ -17,4 +17,6 @@ func NewEnum(typ *super.TypeEnum, vals []uint64, nulls bitvec.Bits) *Enum { } } +func (*Enum) Kind() Kind { return KindEnum } + func (e *Enum) Type() super.Type { return e.Typ } diff --git a/vector/error.go b/vector/error.go index 0348c60599..c07b0b61c9 100644 --- a/vector/error.go +++ b/vector/error.go @@ -20,6 +20,10 @@ func NewError(typ *super.TypeError, vals Any, nulls bitvec.Bits) *Error { return &Error{Typ: typ, Vals: vals, Nulls: nulls} } +func (*Error) Kind() Kind { + return KindError +} + func (e *Error) Type() super.Type { return e.Typ } diff --git a/vector/float.go b/vector/float.go index aec22dd00e..69d47fe6fd 100644 --- a/vector/float.go +++ b/vector/float.go @@ -26,6 +26,10 @@ func (f *Float) Append(v float64) { f.Values = append(f.Values, v) } +func (*Float) Kind() Kind { + return KindFloat +} + func (f *Float) Type() super.Type { return f.Typ } diff --git a/vector/int.go b/vector/int.go index 4a99254ccc..ffe6ead236 100644 --- a/vector/int.go +++ b/vector/int.go @@ -27,6 +27,10 @@ func (i *Int) Append(v int64) { i.Values = append(i.Values, v) } +func (*Int) Kind() Kind { + return KindInt +} + func (i *Int) Type() super.Type { return i.Typ } diff --git a/vector/ip.go b/vector/ip.go index 8050b27483..a4c4668ddb 100644 --- a/vector/ip.go +++ b/vector/ip.go @@ -19,6 +19,10 @@ func NewIP(values []netip.Addr, nulls bitvec.Bits) *IP { return &IP{Values: values, Nulls: nulls} } +func (*IP) Kind() Kind { + return KindIP +} + func (i *IP) Type() super.Type { return super.TypeIP } diff --git a/vector/kind.go b/vector/kind.go index b4646fc2ec..c466ea2dea 100644 --- a/vector/kind.go +++ b/vector/kind.go @@ -2,8 +2,6 @@ package vector import ( "fmt" - - "github.com/brimdata/super" ) type Kind int @@ -25,6 +23,9 @@ const ( KindSet = 12 KindMap = 13 KindRecord = 14 + KindBool = 15 + KindUnion = 16 + KindEnum = 17 ) const ( @@ -34,47 +35,6 @@ const ( FormConst = 3 ) -//XXX might not need Kind... - -func KindOf(v Any) Kind { - switch v := v.(type) { - case *Array: - return KindArray - case *Int: - return KindInt - case *Uint: - return KindUint - case *Float: - return KindFloat - case *Bytes: - return KindBytes - case *String: - return KindString - case *Error: - return KindError - case *IP: - return KindIP - case *Net: - return KindNet - case *TypeValue: - return KindType - case *Map: - return KindMap - case *Record: - return KindRecord - case *Set: - return KindSet - case *Dict: - return KindOf(v.Any) - case *View: - return KindOf(v.Any) - case *Const: - return KindOfType(v.Value().Type()) - default: - return KindInvalid - } -} - func KindFromString(v string) Kind { switch v { case "Int": @@ -102,38 +62,6 @@ func KindFromString(v string) Kind { } } -func KindOfType(typ super.Type) Kind { - switch super.TypeUnder(typ).(type) { - case *super.TypeOfInt16, *super.TypeOfInt32, *super.TypeOfInt64, *super.TypeOfDuration, *super.TypeOfTime: - return KindInt - case *super.TypeOfUint16, *super.TypeOfUint32, *super.TypeOfUint64: - return KindUint - case *super.TypeOfFloat16, *super.TypeOfFloat32, *super.TypeOfFloat64: - return KindFloat - case *super.TypeOfString: - return KindString - case *super.TypeOfBytes: - return KindBytes - case *super.TypeOfIP: - return KindIP - case *super.TypeOfNet: - return KindNet - case *super.TypeOfType: - return KindType - case *super.TypeOfNull: - return KindNull - case *super.TypeArray: - return KindArray - case *super.TypeSet: - return KindSet - case *super.TypeMap: - return KindMap - case *super.TypeRecord: - return KindRecord - } - return KindInvalid -} - func FormOf(v Any) (Form, bool) { switch v.(type) { case *Int, *Uint, *Float, *Bytes, *String, *TypeValue: //XXX IP, Net diff --git a/vector/map.go b/vector/map.go index 0af57f3400..655427ecd3 100644 --- a/vector/map.go +++ b/vector/map.go @@ -20,6 +20,10 @@ func NewMap(typ *super.TypeMap, offsets []uint32, keys Any, values Any, nulls bi return &Map{Typ: typ, Offsets: offsets, Keys: keys, Values: values, Nulls: nulls} } +func (*Map) Kind() Kind { + return KindMap +} + func (m *Map) Type() super.Type { return m.Typ } diff --git a/vector/net.go b/vector/net.go index 1c07a898d1..0d4a17d832 100644 --- a/vector/net.go +++ b/vector/net.go @@ -19,6 +19,10 @@ func NewNet(values []netip.Prefix, nulls bitvec.Bits) *Net { return &Net{Values: values, Nulls: nulls} } +func (*Net) Kind() Kind { + return KindNet +} + func (n *Net) Type() super.Type { return super.TypeNet } diff --git a/vector/record.go b/vector/record.go index ee2d90da50..f77a3ca805 100644 --- a/vector/record.go +++ b/vector/record.go @@ -19,6 +19,10 @@ func NewRecord(typ *super.TypeRecord, fields []Any, length uint32, nulls bitvec. return &Record{Typ: typ, Fields: fields, len: length, Nulls: nulls} } +func (*Record) Kind() Kind { + return KindRecord +} + func (r *Record) Type() super.Type { return r.Typ } diff --git a/vector/set.go b/vector/set.go index ae19291ba6..2e4d158707 100644 --- a/vector/set.go +++ b/vector/set.go @@ -19,6 +19,10 @@ func NewSet(typ *super.TypeSet, offsets []uint32, values Any, nulls bitvec.Bits) return &Set{Typ: typ, Offsets: offsets, Values: values, Nulls: nulls} } +func (*Set) Kind() Kind { + return KindSet +} + func (s *Set) Type() super.Type { return s.Typ } diff --git a/vector/string.go b/vector/string.go index d2f2924e0e..b9ab938e1a 100644 --- a/vector/string.go +++ b/vector/string.go @@ -25,6 +25,10 @@ func (s *String) Append(v string) { s.table.Append([]byte(v)) } +func (*String) Kind() Kind { + return KindString +} + func (s *String) Type() super.Type { return super.TypeString } diff --git a/vector/type.go b/vector/type.go index 52b508ef77..10960d1cbf 100644 --- a/vector/type.go +++ b/vector/type.go @@ -25,6 +25,10 @@ func (t *TypeValue) Append(v []byte) { t.table.Append(v) } +func (*TypeValue) Kind() Kind { + return KindType +} + func (t *TypeValue) Type() super.Type { return super.TypeType } diff --git a/vector/uint.go b/vector/uint.go index 9570fc58e2..177a4a0aa0 100644 --- a/vector/uint.go +++ b/vector/uint.go @@ -27,6 +27,10 @@ func (u *Uint) Append(v uint64) { u.Values = append(u.Values, v) } +func (*Uint) Kind() Kind { + return KindUint +} + func (u *Uint) Type() super.Type { return u.Typ } diff --git a/vector/union.go b/vector/union.go index c1f5cca1cb..78bf51a7ac 100644 --- a/vector/union.go +++ b/vector/union.go @@ -20,6 +20,10 @@ func NewUnion(typ *super.TypeUnion, tags []uint32, vals []Any, nulls bitvec.Bits return &Union{NewDynamic(tags, vals), typ, nulls} } +func (*Union) Kind() Kind { + return KindUnion +} + func (u *Union) Type() super.Type { return u.Typ }