Skip to content

Commit

Permalink
Add random function (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
agonist authored Nov 24, 2023
1 parent 79af2d0 commit 71477da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/genji/doc/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var builtinDocs = functionDocs{
"typeof": "The typeof function returns the type of arg1.",
"len": "The len function returns length of the arg1 expression if arg1 evals to string, array or document, either returns NULL.",
"coalesce": "The coalesce function returns the first non-null argument. NULL is returned if all arguments are null.",
"random": "The random function returns a random number between math.MinInt64 and math.MaxInt64.",
}

var mathDocs = functionDocs{
Expand Down
27 changes: 19 additions & 8 deletions internal/expr/functions/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package functions
import (
"fmt"
"math"
"math/rand"

"github.com/genjidb/genji/document"
"github.com/genjidb/genji/types"
Expand All @@ -14,14 +15,15 @@ func MathFunctions() Definitions {
}

var mathFunctions = Definitions{
"floor": floor,
"abs": abs,
"acos": acos,
"acosh": acosh,
"asin": asin,
"asinh": asinh,
"atan": atan,
"atan2": atan2,
"floor": floor,
"abs": abs,
"acos": acos,
"acosh": acosh,
"asin": asin,
"asinh": asinh,
"atan": atan,
"atan2": atan2,
"random": random,
}

var floor = &ScalarDefinition{
Expand Down Expand Up @@ -164,3 +166,12 @@ var atan2 = &ScalarDefinition{
return types.NewDoubleValue(res), nil
},
}

var random = &ScalarDefinition{
name: "random",
arity: 0,
callFn: func(args ...types.Value) (types.Value, error) {
randomNum := rand.Int63()
return types.NewIntegerValue(randomNum), nil
},
}

0 comments on commit 71477da

Please sign in to comment.