Skip to content

Commit

Permalink
vam: nest_dotted
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Jan 24, 2025
1 parent c388844 commit de40859
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
8 changes: 6 additions & 2 deletions runtime/sam/expr/function/nestdotted.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ func (n *NestDotted) lookupBuilderAndType(in *super.TypeRecord) (*super.RecordBu

func (n *NestDotted) Call(_ super.Allocator, args []super.Value) super.Value {
val := args[len(args)-1]
b, typ, err := n.lookupBuilderAndType(super.TypeRecordOf(val.Type()))
rtyp := super.TypeRecordOf(val.Type())
if rtyp == nil {
return n.zctx.WrapError("nest_dotted: non-record value", val)
}
b, typ, err := n.lookupBuilderAndType(rtyp)
if err != nil {
return n.zctx.WrapError("nest_dotted(): "+err.Error(), val)
return n.zctx.WrapError("nest_dotted: "+err.Error(), val)
}
if b == nil {
return val
Expand Down
11 changes: 0 additions & 11 deletions runtime/sam/expr/ztests/nest_dotted.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
f = &Log{zctx}
case "lower":
f = &ToLower{zctx}
case "nest_dotted":
path = field.Path{}
argmin = 0
f = &NestDotted{zctx}
case "network_of":
argmax = 2
f = &NetworkOf{zctx}
Expand Down
51 changes: 51 additions & 0 deletions runtime/vam/expr/function/nestdotted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package function

import (
"github.com/brimdata/super"
"github.com/brimdata/super/pkg/field"
"github.com/brimdata/super/vector"
)

type NestDotted struct {
zctx *super.Context
}

func (n *NestDotted) Call(args ...vector.Any) vector.Any {
vec := args[len(args)-1]
view, ok := vec.(*vector.View)
if ok {
vec = view.Any
}
record, ok := vec.(*vector.Record)
if !ok {
return vector.NewWrappedError(n.zctx, "nest_dotted: non-record value", args[len(args)-1])
}
b, err := n.getBuilder(record.Typ)
if err != nil {
return vector.NewWrappedError(n.zctx, "nest_dotted: "+err.Error(), args[len(args)-1])
}
if b == nil {
return args[len(args)-1]
}
out := vector.Any(b.New(record.Fields))
if view != nil {
out = vector.NewView(out, view.Index)
}
return out
}

func (n *NestDotted) getBuilder(in *super.TypeRecord) (*vector.RecordBuilder, error) {
var foundDotted bool
var fields field.List
for _, f := range in.Fields {
dotted := field.Dotted(f.Name)
if len(dotted) > 1 {
foundDotted = true
}
fields = append(fields, dotted)
}
if !foundDotted {
return nil, nil
}
return vector.NewRecordBuilder(n.zctx, fields)
}
15 changes: 15 additions & 0 deletions runtime/ztests/expr/function/nest_dotted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
zed: nest_dotted(this)

vector: true

input: |
{a:1,"b.a":2,"b.b":3,"b.c.a":4,c:5}
{a:1,b:{a:2,b:3,c:{a:4}},c:5}
{a:1,"b.a":2}
"foo"
output: |
{a:1,b:{a:2,b:3,c:{a:4}},c:5}
{a:1,b:{a:2,b:3,c:{a:4}},c:5}
{a:1,b:{a:2}}
error({message:"nest_dotted: non-record value",on:"foo"})

0 comments on commit de40859

Please sign in to comment.