This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Feature/pow sqrt #534
Merged
Merged
Feature/pow sqrt #534
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,135 @@ | ||
package function | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
) | ||
|
||
// Sqrt is a function that returns the square value of the number provided. | ||
type Sqrt struct { | ||
expression.UnaryExpression | ||
} | ||
|
||
// NewSqrt creates a new Sqrt expression. | ||
func NewSqrt(e sql.Expression) sql.Expression { | ||
return &Sqrt{expression.UnaryExpression{Child: e}} | ||
} | ||
|
||
func (s *Sqrt) String() string { | ||
return fmt.Sprintf("sqrt(%s)", s.Child.String()) | ||
} | ||
|
||
// Type implements the Expression interface. | ||
func (s *Sqrt) Type() sql.Type { | ||
return sql.Float64 | ||
} | ||
|
||
// IsNullable implements the Expression interface. | ||
func (s *Sqrt) IsNullable() bool { | ||
return s.Child.IsNullable() | ||
} | ||
|
||
// TransformUp implements the Expression interface. | ||
func (s *Sqrt) TransformUp(fn sql.TransformExprFunc) (sql.Expression, error) { | ||
child, err := s.Child.TransformUp(fn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return fn(NewSqrt(child)) | ||
} | ||
|
||
// Eval implements the Expression interface. | ||
func (s *Sqrt) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
child, err := s.Child.Eval(ctx, row) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if child == nil { | ||
return nil, nil | ||
} | ||
|
||
child, err = sql.Float64.Convert(child) | ||
if err != nil { | ||
return nil, nil | ||
} | ||
|
||
return math.Sqrt(child.(float64)), nil | ||
} | ||
|
||
// Power is a function that returns value of X raised to the power of Y. | ||
type Power struct { | ||
expression.BinaryExpression | ||
} | ||
|
||
// NewPad creates a new Power expression. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment doesn't match the function name. |
||
func NewPower(e1, e2 sql.Expression) sql.Expression { | ||
return &Power{ | ||
expression.BinaryExpression{ | ||
Left: e1, | ||
Right: e2, | ||
}, | ||
} | ||
} | ||
|
||
// Type implements the Expression interface. | ||
func (p *Power) Type() sql.Type { return sql.Float64 } | ||
|
||
// IsNullable implements the Expression interface. | ||
func (p *Power) IsNullable() bool { return p.Left.IsNullable() || p.Right.IsNullable() } | ||
|
||
func (p *Power) String() string { | ||
return fmt.Sprintf("power(%s, %s)", p.Left, p.Right) | ||
} | ||
|
||
// TransformUp implements the Expression interface. | ||
func (p *Power) TransformUp(fn sql.TransformExprFunc) (sql.Expression, error) { | ||
left, err := p.Left.TransformUp(fn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
right, err := p.Right.TransformUp(fn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return fn(NewPower(left, right)) | ||
} | ||
|
||
// Eval implements the Expression interface. | ||
func (p *Power) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
left, err := p.Left.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if left == nil { | ||
return nil, nil | ||
} | ||
|
||
left, err = sql.Float64.Convert(left) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
right, err := p.Right.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if right == nil { | ||
return nil, nil | ||
} | ||
|
||
right, err = sql.Float64.Convert(right) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return math.Pow(left.(float64), right.(float64)), nil | ||
} |
This file contains hidden or 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,110 @@ | ||
package function | ||
|
||
import ( | ||
"testing" | ||
"math" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
) | ||
|
||
func TestSqrt(t *testing.T) { | ||
f := NewSqrt( | ||
expression.NewGetField(0, sql.Float64, "n", false), | ||
) | ||
testCases := []struct { | ||
name string | ||
row sql.Row | ||
expected interface{} | ||
err bool | ||
}{ | ||
{"null input", sql.NewRow(nil), nil, false}, | ||
{"invalid string", sql.NewRow("foo"), nil, false}, | ||
{"valid string", sql.NewRow("9"), float64(3), false}, | ||
{"number is zero", sql.NewRow(0), float64(0), false}, | ||
{"positive number", sql.NewRow(8), float64(2.8284271247461903), false}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Helper() | ||
require := require.New(t) | ||
ctx := sql.NewEmptyContext() | ||
|
||
v, err := f.Eval(ctx, tt.row) | ||
if tt.err { | ||
require.Error(err) | ||
} else { | ||
require.NoError(err) | ||
require.Equal(tt.expected, v) | ||
} | ||
}) | ||
} | ||
|
||
// Test negative number | ||
f = NewSqrt( | ||
expression.NewGetField(0, sql.Float64, "n", false), | ||
) | ||
require := require.New(t) | ||
v, err := f.Eval(sql.NewEmptyContext(), []interface{}{float64(-4)}) | ||
require.NoError(err) | ||
require.IsType(float64(0), v) | ||
require.True(math.IsNaN(v.(float64))) | ||
} | ||
|
||
func TestPower(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
rowType sql.Type | ||
row sql.Row | ||
expected interface{} | ||
err bool | ||
}{ | ||
{"Base and exp are nil", sql.Float64, sql.NewRow(nil, nil), nil, false}, | ||
{"Base is nil", sql.Float64, sql.NewRow(2, nil), nil, false}, | ||
{"Exp is nil", sql.Float64, sql.NewRow(nil, 2), nil, false}, | ||
|
||
{"Base is 0", sql.Float64, sql.NewRow(0, 2), float64(0), false}, | ||
{"Base and exp is 0", sql.Float64, sql.NewRow(0, 0), float64(1), false}, | ||
{"Exp is 0", sql.Float64, sql.NewRow(2, 0), float64(1), false}, | ||
{"Base is negative", sql.Float64, sql.NewRow(-2, 2), float64(4), false}, | ||
{"Exp is negative", sql.Float64, sql.NewRow(2, -2), float64(0.25), false}, | ||
{"Base and exp are invalid strings", sql.Float64, sql.NewRow("a", "b"), nil, true}, | ||
{"Base and exp are valid strings", sql.Float64, sql.NewRow("2", "2"), float64(4), false}, | ||
} | ||
for _, tt := range testCases { | ||
f := NewPower( | ||
expression.NewGetField(0, tt.rowType, "", false), | ||
expression.NewGetField(1, tt.rowType, "", false), | ||
) | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Helper() | ||
require := require.New(t) | ||
ctx := sql.NewEmptyContext() | ||
|
||
v, err := f.Eval(ctx, tt.row) | ||
if tt.err { | ||
require.Error(err) | ||
} else { | ||
require.NoError(err) | ||
require.Equal(tt.expected, v) | ||
} | ||
}) | ||
} | ||
|
||
// Test inf numbers | ||
f := NewPower( | ||
expression.NewGetField(0, sql.Float64, "", false), | ||
expression.NewGetField(1, sql.Float64, "", false), | ||
) | ||
require := require.New(t) | ||
v, err := f.Eval(sql.NewEmptyContext(), sql.NewRow(2, math.Inf(1))) | ||
require.NoError(err) | ||
require.IsType(float64(0), v) | ||
require.True(math.IsInf(v.(float64), 1)) | ||
|
||
v, err = f.Eval(sql.NewEmptyContext(), sql.NewRow(math.Inf(1), 2)) | ||
require.NoError(err) | ||
require.IsType(float64(0), v) | ||
require.True(math.IsInf(v.(float64), 1)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.