-
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.
This is mostly the framework for calling functions. It includes the implementation of one function: Math.sqrt(). Additional functions to be added in subsequent PRs.
- Loading branch information
aswan
authored
Mar 18, 2020
1 parent
2d8a04c
commit b53f254
Showing
10 changed files
with
1,427 additions
and
852 deletions.
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
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,46 @@ | ||
package expr | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math" | ||
|
||
"github.com/brimsec/zq/zng" | ||
) | ||
|
||
type Function func([]NativeValue) (NativeValue, error) | ||
|
||
var ErrWrongArgc = errors.New("wrong number of arguments") | ||
var ErrBadArgument = errors.New("bad argument") | ||
|
||
var allFns = map[string]Function{ | ||
"Math.sqrt": mathSqrt, | ||
} | ||
|
||
func mathSqrt(args []NativeValue) (NativeValue, error) { | ||
if len(args) < 1 || len(args) > 1 { | ||
return NativeValue{}, fmt.Errorf("Math.sqrt: %w", ErrWrongArgc) | ||
} | ||
|
||
var x float64 | ||
switch args[0].typ.ID() { | ||
case zng.IdFloat64: | ||
x = args[0].value.(float64) | ||
case zng.IdInt16, zng.IdInt32, zng.IdInt64: | ||
x = float64(args[0].value.(int64)) | ||
case zng.IdByte, zng.IdUint16, zng.IdUint32, zng.IdUint64: | ||
x = float64(args[0].value.(uint64)) | ||
default: | ||
return NativeValue{}, fmt.Errorf("Math.sqrt: %w", ErrBadArgument) | ||
} | ||
|
||
r := math.Sqrt(x) | ||
if math.IsNaN(r) { | ||
// For now we can't represent non-numeric values in a float64, | ||
// we will revisit this but it has implications for file | ||
// formats, zql, etc. | ||
return NativeValue{}, fmt.Errorf("Math.sqrt: %w", ErrBadArgument) | ||
} | ||
|
||
return NativeValue{zng.TypeFloat64, r}, nil | ||
} |
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,27 @@ | ||
package expr_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/brimsec/zq/expr" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBadFunction(t *testing.T) { | ||
testError(t, "notafunction()", nil, expr.ErrNoSuchFunction, "calling nonexistent function") | ||
} | ||
|
||
func TestSqrt(t *testing.T) { | ||
record, err := parseOneRecord(` | ||
#0:record[f:float64,i:int32] | ||
0:[6.25;9;]`) | ||
require.NoError(t, err) | ||
|
||
testSuccessful(t, "Math.sqrt(4.0)", record, zfloat64(2.0)) | ||
testSuccessful(t, "Math.sqrt(f)", record, zfloat64(2.5)) | ||
testSuccessful(t, "Math.sqrt(i)", record, zfloat64(3.0)) | ||
|
||
testError(t, "Math.sqrt()", record, expr.ErrWrongArgc, "sqrt with no args") | ||
testError(t, "Math.sqrt(1, 2)", record, expr.ErrWrongArgc, "sqrt with too many args") | ||
testError(t, "Math.sqrt(-1)", record, expr.ErrBadArgument, "sqrt of negative") | ||
} |
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
Oops, something went wrong.