Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vam: Add abs function #5546

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/sam/expr/function/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Abs struct {
}

func (a *Abs) Call(_ super.Allocator, args []super.Value) super.Value {
val := args[0]
val := args[0].Under()
switch id := val.Type().ID(); {
case super.IsUnsigned(id):
return val
Expand Down
2 changes: 2 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
var path field.Path
var f expr.Function
switch name {
case "abs":
f = &Abs{zctx}
case "base64":
f = &Base64{zctx}
case "bucket":
Expand Down
62 changes: 62 additions & 0 deletions runtime/vam/expr/function/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package function

import (
"math"

"github.com/brimdata/super"
"github.com/brimdata/super/vector"
)

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

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

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

vector: true

input: |
1
-1
0
1.0
-1.0
-1(int8)
1(uint8)
"foo"

output: |
1
1
0
1.
1.
1(int8)
1(uint8)
error({message:"abs: not a number",on:"foo"})