Skip to content

Commit

Permalink
vam: Add ceil func
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Dec 23, 2024
1 parent 7b257de commit 0c69316
Show file tree
Hide file tree
Showing 3 changed files with 55 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 @@ -24,6 +24,8 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
argmin = 2
argmax = 2
f = &Bucket{zctx: zctx, name: name}
case "ceil":
f = &Ceil{zctx}
case "coalesce":
argmax = -1
f = &Coalesce{}
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 @@ -60,3 +60,39 @@ func (a *Abs) abs(vec vector.Any) vector.Any {
panic(vec)
}
}

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

func (c *Ceil) Call(args ...vector.Any) vector.Any {
vec := vector.Under(args[0])
switch id := vec.Type().ID(); {
case super.IsFloat(id):
return c.ceil(vec)
case super.IsUnsigned(id) || super.IsSigned(id):
return vec
}
return vector.NewWrappedError(c.zctx, "ceil: not a number", vec)
}

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

vector: true

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

0 comments on commit 0c69316

Please sign in to comment.