Commit 063df5ad authored by Geofrey Ernest's avatar Geofrey Ernest

add sleep built in function

This function can be used to delay execution.

This is similar  to pg_sleep for postgresql. It takes a single argument representing a number in secods.

```
select sleep(5)
```

Will sleep for 5 secods.
parent b1f03150
......@@ -3682,3 +3682,43 @@ left join condition on condition.key == product_condkey;
}
}
}
func TestSleep(t *testing.T) {
db, err := OpenMem()
if err != nil {
t.Fatal(err)
}
defer db.Close()
// sleep with duration
rst, _, err := db.run(nil, "select sleep($1);", time.Second)
if err != nil {
t.Fatal(err)
}
for _, rs := range rst {
_, err = rs.FirstRow()
if err != nil {
t.Fatal(err)
}
}
// sleep with an int
rst, _, err = db.run(nil, "select sleep(5);")
if err != nil {
t.Fatal(err)
}
for _, rs := range rst {
start := time.Now()
_, err := rs.FirstRow()
if err != nil {
t.Fatal(err)
}
end := time.Now().Sub(start)
// The duration should be 5 seconds
e := end.String()
if !strings.HasPrefix(e, "5.") {
t.Errorf("expected 5s got %s", e)
}
}
}
......@@ -53,6 +53,7 @@ var builtin = map[string]struct {
"second": {builtinSecond, 1, 1, true, false},
"seconds": {builtinSeconds, 1, 1, true, false},
"since": {builtinSince, 1, 1, false, false},
"sleep": {builtinSleep, 1, 1, false, false},
"sum": {builtinSum, 1, 1, false, true},
"timeIn": {builtinTimeIn, 2, 2, true, false},
"weekday": {builtinWeekday, 1, 1, true, false},
......@@ -873,6 +874,26 @@ func builtinSince(arg []interface{}, ctx map[interface{}]interface{}) (v interfa
}
}
func builtinSleep(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
switch x := arg[0].(type) {
case nil:
return nil, nil
case time.Duration:
time.Sleep(x)
return nil, nil
case idealInt:
v := time.Second * time.Duration(int64(x))
time.Sleep(v)
return nil, nil
case int64:
v := time.Second * time.Duration(x)
time.Sleep(v)
return nil, nil
default:
return nil, invArg(x, "sleep")
}
}
func builtinSum(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
if _, ok := ctx["$agg0"]; ok {
return
......
......@@ -8400,8 +8400,6 @@ SELECT * FROM t WHERE c1 == 1;
SELECT 42;
┌Selects values from dummy table
└Output field names [""]
┌Evaluate 42 as "",
└Output field names [""]
---- 1348
SELECT * FROM t WHERE EXISTS ( SELECT * FROM t WHERE i == 2 ) ORDER BY i;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment