Skip to content

Commit 6b241e2

Browse files
agonistasdine
authored andcommitted
add sqrt function (#515)
1 parent cdac7cf commit 6b241e2

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

cmd/chai/doc/functions.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ var builtinDocs = functionDocs{
1919
"typeof": "The typeof function returns the type of arg1.",
2020
"len": "The len function returns length of the arg1 expression if arg1 evals to string, array or object, either returns NULL.",
2121
"coalesce": "The coalesce function returns the first non-null argument. NULL is returned if all arguments are null.",
22-
"random": "The random function returns a random number between math.MinInt64 and math.MaxInt64.",
2322
}
2423

2524
var mathDocs = functionDocs{
26-
"abs": "Returns the absolute value of arg1.",
27-
"acos": "Returns the arcosine, in radiant, of arg1.",
28-
"acosh": "Returns the inverse hyperbolic cosine of arg1.",
29-
"asin": "Returns the arsine, in radiant, of arg1.",
30-
"asinh": "Returns the inverse hyperbolic sine of arg1.",
31-
"atan": "Returns the arctangent, in radians, of arg1.",
32-
"atan2": "Returns the arctangent of arg1/arg2, using the signs of the two to determine the quadrant of the return value.",
33-
"floor": "Returns the greatest integer value less than or equal to arg1.",
25+
"abs": "Returns the absolute value of arg1.",
26+
"acos": "Returns the arcosine, in radiant, of arg1.",
27+
"acosh": "Returns the inverse hyperbolic cosine of arg1.",
28+
"asin": "Returns the arsine, in radiant, of arg1.",
29+
"asinh": "Returns the inverse hyperbolic sine of arg1.",
30+
"atan": "Returns the arctangent, in radians, of arg1.",
31+
"atan2": "Returns the arctangent of arg1/arg2, using the signs of the two to determine the quadrant of the return value.",
32+
"floor": "Returns the greatest integer value less than or equal to arg1.",
33+
"random": "The random function returns a random number between math.MinInt64 and math.MaxInt64.",
34+
"sqrt": "The sqrt function returns the square root of arg1.",
3435
}
3536

3637
var stringsDocs = functionDocs{

internal/expr/functions/math.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var mathFunctions = Definitions{
2424
"atan": atan,
2525
"atan2": atan2,
2626
"random": random,
27+
"sqrt": sqrt,
2728
}
2829

2930
var floor = &ScalarDefinition{
@@ -175,3 +176,19 @@ var random = &ScalarDefinition{
175176
return types.NewIntegerValue(randomNum), nil
176177
},
177178
}
179+
180+
var sqrt = &ScalarDefinition{
181+
name: "sqrt",
182+
arity: 1,
183+
callFn: func(args ...types.Value) (types.Value, error) {
184+
if args[0].Type() != types.DoubleValue && args[0].Type() != types.IntegerValue {
185+
return types.NewNullValue(), nil
186+
}
187+
v, err := document.CastAs(args[0], types.DoubleValue)
188+
if err != nil {
189+
return nil, err
190+
}
191+
res := math.Sqrt(types.As[float64](v))
192+
return types.NewDoubleValue(res), nil
193+
},
194+
}

internal/expr/functions/testdata/math_functions.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,17 @@ NULL
121121
2.356194490192345
122122
! math.atan2('foo', 1)
123123
'cannot cast "foo" as double'
124+
125+
-- test: math.sqrt
126+
> math.sqrt(NULL)
127+
NULL
128+
> math.sqrt(4)
129+
2.0
130+
> math.sqrt(81)
131+
9.0
132+
> math.sqrt(15)
133+
3.872983346207417
134+
> math.sqrt(1.1)
135+
1.0488088481701516
136+
> math.sqrt('foo')
137+
NULL

0 commit comments

Comments
 (0)