-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) |