Skip to content

Commit 0c69316

Browse files
committed
vam: Add ceil func
1 parent 7b257de commit 0c69316

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

runtime/vam/expr/function/function.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
2424
argmin = 2
2525
argmax = 2
2626
f = &Bucket{zctx: zctx, name: name}
27+
case "ceil":
28+
f = &Ceil{zctx}
2729
case "coalesce":
2830
argmax = -1
2931
f = &Coalesce{}

runtime/vam/expr/function/math.go

+36
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,39 @@ func (a *Abs) abs(vec vector.Any) vector.Any {
6060
panic(vec)
6161
}
6262
}
63+
64+
// https://github.com/brimdata/super/blob/main/docs/language/functions.md#ceil
65+
type Ceil struct {
66+
zctx *super.Context
67+
}
68+
69+
func (c *Ceil) Call(args ...vector.Any) vector.Any {
70+
vec := vector.Under(args[0])
71+
switch id := vec.Type().ID(); {
72+
case super.IsFloat(id):
73+
return c.ceil(vec)
74+
case super.IsUnsigned(id) || super.IsSigned(id):
75+
return vec
76+
}
77+
return vector.NewWrappedError(c.zctx, "ceil: not a number", vec)
78+
}
79+
80+
func (a *Ceil) ceil(vec vector.Any) vector.Any {
81+
switch vec := vec.(type) {
82+
case *vector.Const:
83+
val := super.NewFloat(vec.Type(), math.Ceil(vec.Value().Float()))
84+
return vector.NewConst(val, vec.Len(), vec.Nulls)
85+
case *vector.View:
86+
return vector.NewView(a.ceil(vec.Any), vec.Index)
87+
case *vector.Dict:
88+
return vector.NewDict(a.ceil(vec.Any), vec.Index, vec.Counts, vec.Nulls)
89+
case *vector.Float:
90+
var floats []float64
91+
for _, v := range vec.Values {
92+
floats = append(floats, math.Ceil(v))
93+
}
94+
return vector.NewFloat(vec.Type(), floats, vec.Nulls)
95+
default:
96+
panic(vec)
97+
}
98+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
zed: ceil(this)
2+
3+
vector: true
4+
5+
input: |
6+
1.5
7+
-1.5
8+
1(uint8)
9+
1.5(float32)
10+
"foo"
11+
12+
output: |
13+
2.
14+
-1.
15+
1(uint8)
16+
2.(float32)
17+
error({message:"ceil: not a number",on:"foo"})

0 commit comments

Comments
 (0)