1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
open Sqlite3
let%test "test_window" =
Printf.printf "Using version %s\n" (sqlite_version_info ());
if sqlite_version () >= 3025000 then (
let db = db_open "t_fun" in
let getval p = Data.FLOAT p in
Aggregate.create_fun1 db "product" ~init:1.0
~step:(fun p v -> p *. Data.to_float_exn v)
~inverse:(fun p v -> p /. Data.to_float_exn v)
~value:getval ~final:getval;
let s =
prepare db
"WITH cte(id, num) AS (VALUES (0,2.0),(1,3.0),(2,4.0),(3,5.0),(4,6.0)) \
SELECT id, product(num) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING \
AND CURRENT ROW) FROM cte ORDER BY id"
in
let expected = [| 2.0; 6.0; 24.0; 60.0; 120.0 |] in
print_endline "Testing window functions.";
while step s = Rc.ROW do
Printf.printf "got %f expected %f\n" (column_double s 1)
expected.(column_int s 0)
done)
else prerr_endline "Skipping window function test.";
true
|