This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #649 from juanjux/sql-sleep
Added MySQL SLEEP function
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,7 @@ | |
- LN | ||
- LOG2 | ||
- LOG10 | ||
- SLEEP | ||
|
||
## Time functions | ||
- DAY | ||
|
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,66 @@ | ||
package function | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
) | ||
|
||
// Sleep is a function that just waits for the specified number of seconds | ||
// and returns 0. | ||
// It can be useful to test timeouts or long queries. | ||
type Sleep struct { | ||
expression.UnaryExpression | ||
} | ||
|
||
// NewSleep creates a new Sleep expression. | ||
func NewSleep(e sql.Expression) sql.Expression { | ||
return &Sleep{expression.UnaryExpression{Child: e}} | ||
} | ||
|
||
// Eval implements the Expression interface. | ||
func (s *Sleep) 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, err | ||
} | ||
|
||
time.Sleep(time.Duration(child.(float64) * 1000) * time.Millisecond) | ||
return 0, nil | ||
} | ||
|
||
// String implements the Stringer interface. | ||
func (s *Sleep) String() string { | ||
return fmt.Sprintf("SLEEP(%s)", s.Child) | ||
} | ||
|
||
// IsNullable implements the Expression interface. | ||
func (s *Sleep) IsNullable() bool { | ||
return false | ||
} | ||
|
||
// TransformUp implements the Expression interface. | ||
func (s *Sleep) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { | ||
child, err := s.Child.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return f(NewSleep(child)) | ||
} | ||
|
||
// Type implements the Expression interface. | ||
func (s *Sleep) Type() sql.Type { | ||
return sql.Int32 | ||
} |
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,50 @@ | ||
package function | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"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 TestSleep(t *testing.T) { | ||
f := NewSleep( | ||
expression.NewGetField(0, sql.Text, "n", false), | ||
) | ||
testCases := []struct { | ||
name string | ||
row sql.Row | ||
expected interface{} | ||
waitTime float64 | ||
err bool | ||
}{ | ||
{"null input", sql.NewRow(nil), nil, 0, false}, | ||
{"string input", sql.NewRow("foo"), nil, 0, true}, | ||
{"int input", sql.NewRow(3), int(0), 3.0, false}, | ||
{"number is zero", sql.NewRow(0), int(0), 0, false}, | ||
{"negative number", sql.NewRow(-4), int(0), 0, false}, | ||
{"positive number", sql.NewRow(4.48), int(0), 4.48, false}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Helper() | ||
require := require.New(t) | ||
ctx := sql.NewEmptyContext() | ||
|
||
t1 := time.Now() | ||
v, err := f.Eval(ctx, tt.row) | ||
t2 := time.Now() | ||
if tt.err { | ||
require.Error(err) | ||
} else { | ||
require.NoError(err) | ||
require.Equal(tt.expected, v) | ||
|
||
waited := t2.Sub(t1).Seconds() | ||
require.InDelta(waited, tt.waitTime, 0.1) | ||
} | ||
}) | ||
} | ||
} |