Skip to content

Commit

Permalink
vam: Add floor func
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Dec 23, 2024
1 parent eea12eb commit 9d54cff
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
f = &Error{zctx}
case "fields":
f = NewFields(zctx)
case "floor":
f = &Floor{zctx}
case "grep":
argmax = 2
f = &Grep{zctx: zctx}
Expand Down
36 changes: 36 additions & 0 deletions runtime/vam/expr/function/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,39 @@ func (c *Ceil) ceil(vec vector.Any) vector.Any {
panic(vec)
}
}

// https://github.com/brimdata/super/blob/main/docs/language/functions.md#floor
type Floor struct {
zctx *super.Context
}

func (f *Floor) Call(args ...vector.Any) vector.Any {
vec := vector.Under(args[0])
switch id := vec.Type().ID(); {
case super.IsFloat(id):
return f.floor(vec)
case super.IsNumber(id):
return vec
}
return vector.NewWrappedError(f.zctx, "floor: not a number", vec)
}

func (f *Floor) floor(vec vector.Any) vector.Any {
switch vec := vec.(type) {
case *vector.Const:
val := super.NewFloat(vec.Type(), math.Floor(vec.Value().Float()))
return vector.NewConst(val, vec.Len(), vec.Nulls)
case *vector.View:
return vector.NewView(f.floor(vec.Any), vec.Index)
case *vector.Dict:
return vector.NewDict(f.floor(vec.Any), vec.Index, vec.Counts, vec.Nulls)
case *vector.Float:
var floats []float64
for _, v := range vec.Values {
floats = append(floats, math.Floor(v))
}
return vector.NewFloat(vec.Type(), floats, vec.Nulls)
default:
panic(vec)
}
}
19 changes: 19 additions & 0 deletions runtime/ztests/expr/function/floor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
zed: floor(this)

vector: true

input: |
1.5
1.7
-1.5
1(uint8)
1.5(float32)
"foo"
output: |
1.
1.
-2.
1(uint8)
1.(float32)
error({message:"floor: not a number",on:"foo"})

0 comments on commit 9d54cff

Please sign in to comment.