#
#***********************************************************************
# This file implements regression tests for SQLite library. The
-# focus of this file is percentile.c extension
+# focus of this file is percentile.c extension. This also tests
+# the SQLITE_ENABLE_ORDERED_SET_AGGREGATES compile-time option.
#
set testdir [file dirname $argv0]
}
execsql {SELECT percentile(x,0) FROM t1}
} {1.0}
-foreach {in out} {
- 100 11.0
- 50 8.0
- 12.5 4.0
- 15 4.4
- 20 5.2
- 80 11.0
- 89 11.0
+foreach {in out disc} {
+ 100 11.0 11.0
+ 50 8.0 8.0
+ 12.5 4.0 4.0
+ 15 4.4 4.0
+ 20 5.2 4.0
+ 80 11.0 11.0
+ 89 11.0 11.0
} {
- do_test percentile-1.1.$in {
+ do_test percentile-1.1.$in.1 {
execsql {SELECT percentile(x,$in) FROM t1}
} $out
+ do_test percentile-1.1.$in.2 {
+ execsql {SELECT percentile_cont(x,$in*0.01) FROM t1}
+ } $out
+ do_test percentile-1.1.$in.3 {
+ execsql {SELECT percentile_disc(x,$in*0.01) FROM t1}
+ } $disc
+ if {$in==50} {
+ do_test percentile-1.1.$in.4 {
+ execsql {SELECT median(x) FROM t1}
+ } $out
+ }
+ ifcapable ordered_set_aggregates {
+ do_test percentile-1.1.$in.5 {
+ execsql {SELECT percentile($in)WITHIN GROUP(ORDER BY x) FROM t1}
+ } $out
+ do_test percentile-1.1.$in.6 {
+ execsql {SELECT percentile_cont($in*0.01) WITHIN GROUP(ORDER BY x)
+ FROM t1}
+ } $out
+ do_test percentile-1.1.$in.7 {
+ execsql {SELECT percentile_disc($in*0.01) WITHIN GROUP(ORDER BY x)
+ FROM t1}
+ } $disc
+ if {$in==50} {
+ do_test percentile-1.1.$in.8 {
+ execsql {SELECT median() WITHIN GROUP (ORDER BY x) FROM t1}
+ } $out
+ }
+ }
}
do_execsql_test percentile-1.1.median {
SELECT median(x) FROM t1;
} 8.0
+ifcapable ordered_set_aggregates {
+ do_execsql_test percentile-1.1.median {
+ SELECT median() WITHIN GROUP (ORDER BY x) FROM t1;
+ } 8.0
+ do_execsql_test percentile-1.1.distinct.1 {
+ SELECT median(DISTINCT x) FROM t1;
+ } 7.0
+ do_catchsql_test percentile-1.1.distinct.2 {
+ SELECT percentile(DISTINCT 50) WITHIN GROUP (ORDER BY x) FROM t1;
+ } {1 {DISTINCT not allowed on ordered-set aggregate percentile()}}
+} else {
+ do_catchsql_test percentile-1.1.median {
+ SELECT median() WITHIN GROUP (ORDER BY x) FROM t1;
+ } {1 {near "(": syntax error}}
+}
# Add some NULL values.
#
do_test percentile-1.2 {
execsql {INSERT INTO t1 VALUES(NULL),(NULL);}
} {}
-foreach {in out} {
- 100 11.0
- 50 8.0
- 12.5 4.0
- 15 4.4
- 20 5.2
- 80 11.0
- 89 11.0
+foreach {in out disc} {
+ 100 11.0 11.0
+ 50 8.0 8.0
+ 12.5 4.0 4.0
+ 15 4.4 4.0
+ 20 5.2 4.0
+ 80 11.0 11.0
+ 89 11.0 11.0
} {
- do_test percentile-1.3.$in {
+ do_test percentile-1.3.$in.1 {
execsql {SELECT percentile(x,$in) FROM t1}
} $out
+ do_test percentile-1.3.$in.2 {
+ execsql {SELECT percentile_cont(x,$in*0.01) FROM t1}
+ } $out
+ do_test percentile-1.3.$in.3 {
+ execsql {SELECT percentile_disc(x,$in*0.01) FROM t1}
+ } $disc
+ if {$in==50} {
+ do_test percentile-1.3.$in.4 {
+ execsql {SELECT median(x) FROM t1}
+ } $out
+ }
+ ifcapable ordered_set_aggregates {
+ do_test percentile-1.3.$in.5 {
+ execsql {SELECT percentile($in)WITHIN GROUP(ORDER BY x) FROM t1}
+ } $out
+ do_test percentile-1.3.$in.6 {
+ execsql {SELECT percentile_cont($in*0.01) WITHIN GROUP(ORDER BY x)
+ FROM t1}
+ } $out
+ do_test percentile-1.3.$in.7 {
+ execsql {SELECT percentile_disc($in*0.01) WITHIN GROUP(ORDER BY x)
+ FROM t1}
+ } $disc
+ if {$in==50} {
+ do_test percentile-1.3.$in.8 {
+ execsql {SELECT median() WITHIN GROUP (ORDER BY x) FROM t1}
+ } $out
+ }
+ }
}
# The second argument to percentile can change some, but not much.
#
-do_test percentile-1.4 {
+do_test percentile-1.4.1 {
catchsql {SELECT round(percentile(x, 15+0.000001*rowid),1) FROM t1}
} {0 4.4}
-do_test percentile-1.5 {
- catchsql {SELECT round(percentile(x, 15+0.1*rowid),1) FROM t1}
+do_test percentile-1.4.2 {
+ catchsql {SELECT round(percentile_cont(x,(15+0.000001*rowid)*0.01),1) FROM t1}
+} {0 4.4}
+do_test percentile-1.4.3 {
+ catchsql {SELECT percentile_disc(x, (15+0.000001*rowid)*0.01) FROM t1}
+} {0 4.0}
+do_test percentile-1.5.1 {
+ catchsql {SELECT percentile(x, 15+0.1*rowid) FROM t1}
} {1 {the fraction argument to percentile() is not the same for all input rows}}
+do_test percentile-1.5.2 {
+ catchsql {SELECT percentile_cont(x, (15+0.1*rowid)*0.01) FROM t1}
+} {1 {the fraction argument to percentile_cont() is not the same for all input rows}}
+do_test percentile-1.5.3 {
+ catchsql {SELECT percentile_disc(x, (15+0.1*rowid)*0.01) FROM t1}
+} {1 {the fraction argument to percentile_disc() is not the same for all input rows}}
# Input values in a random order
#
INSERT INTO t2 SELECT x+0.0 FROM t1 ORDER BY random();
}
} {}
-foreach {in out} {
- 100 11.0
- 50 8.0
- 12.5 4.0
- 15 4.4
- 20 5.2
- 80 11.0
- 89 11.0
+foreach {in out disc} {
+ 100 11.0 11.0
+ 50 8.0 8.0
+ 12.5 4.0 4.0
+ 15 4.4 4.0
+ 20 5.2 4.0
+ 80 11.0 11.0
+ 89 11.0 11.0
} {
- do_test percentile-1.7.$in {
+ do_test percentile-1.7.$in.1 {
execsql {SELECT percentile(x,$in) FROM t2}
} $out
+ do_test percentile-1.7.$in.2 {
+ execsql {SELECT percentile_cont(x,$in*0.01) FROM t2}
+ } $out
+ do_test percentile-1.7.$in.3 {
+ execsql {SELECT percentile_disc(x,$in*0.01) FROM t2}
+ } $disc
+ if {$in==50} {
+ do_test percentile-1.7.$in.4 {
+ execsql {SELECT median(x) FROM t2}
+ } $out
+ }
+ ifcapable ordered_set_aggregates {
+ do_test percentile-1.7.$in.5 {
+ execsql {SELECT percentile($in)WITHIN GROUP(ORDER BY x) FROM t2}
+ } $out
+ do_test percentile-1.7.$in.6 {
+ execsql {SELECT percentile_cont($in*0.01) WITHIN GROUP(ORDER BY x)
+ FROM t2}
+ } $out
+ do_test percentile-1.7.$in.7 {
+ execsql {SELECT percentile_disc($in*0.01) WITHIN GROUP(ORDER BY x)
+ FROM t2}
+ } $disc
+ if {$in==50} {
+ do_test percentile-1.7.$in.8 {
+ execsql {SELECT median() WITHIN GROUP (ORDER BY x) FROM t2}
+ } $out
+ }
+ }
}
# Wrong number of arguments
#
-do_test percentile-1.8 {
+do_test percentile-1.8.1 {
catchsql {SELECT percentile(x,0,1) FROM t1}
} {1 {wrong number of arguments to function percentile()}}
-do_test percentile-1.9 {
+do_test percentile-1.8.2 {
+ catchsql {SELECT percentile_cont(x,0,1) FROM t1}
+} {1 {wrong number of arguments to function percentile_cont()}}
+do_test percentile-1.8.3 {
+ catchsql {SELECT percentile_disc(x,0,1) FROM t1}
+} {1 {wrong number of arguments to function percentile_disc()}}
+do_test percentile-1.8.4 {
+ catchsql {SELECT median(x,0) FROM t1}
+} {1 {wrong number of arguments to function median()}}
+ifcapable ordered_set_aggregates {
+ do_test percentile-1.8.5 {
+ catchsql {SELECT percentile(0,1) WITHIN GROUP(ORDER BY x) FROM t1}
+ } {1 {wrong number of arguments to function percentile()}}
+ do_test percentile-1.8.2 {
+ catchsql {SELECT percentile_cont(0,1)WITHIN GROUP (ORDER BY x) FROM t1}
+ } {1 {wrong number of arguments to function percentile_cont()}}
+ do_test percentile-1.8.3 {
+ catchsql {SELECT percentile_disc(0,1)WITHIN GROUP (ORDER BY x) FROM t1}
+ } {1 {wrong number of arguments to function percentile_disc()}}
+ do_test percentile-1.8.4 {
+ catchsql {SELECT median(x) WITHIN GROUP (ORDER BY x) FROM t1}
+ } {1 {wrong number of arguments to function median()}}
+}
+do_test percentile-1.9.1 {
catchsql {SELECT percentile(x) FROM t1}
} {1 {wrong number of arguments to function percentile()}}
+do_test percentile-1.9.2 {
+ catchsql {SELECT percentile_cont(x) FROM t1}
+} {1 {wrong number of arguments to function percentile_cont()}}
+do_test percentile-1.9.3 {
+ catchsql {SELECT percentile_disc(x) FROM t1}
+} {1 {wrong number of arguments to function percentile_disc()}}
+do_test percentile-1.9.4 {
+ catchsql {SELECT median() FROM t1}
+} {1 {wrong number of arguments to function median()}}
+ifcapable ordered_set_aggregates {
+ do_test percentile-1.9.5 {
+ catchsql {SELECT percentile() WITHIN GROUP(ORDER BY x) FROM t1}
+ } {1 {wrong number of arguments to function percentile()}}
+ do_test percentile-1.9.6 {
+ catchsql {SELECT percentile_cont()WITHIN GROUP (ORDER BY x) FROM t1}
+ } {1 {wrong number of arguments to function percentile_cont()}}
+ do_test percentile-1.9.7 {
+ catchsql {SELECT percentile_disc()WITHIN GROUP (ORDER BY x) FROM t1}
+ } {1 {wrong number of arguments to function percentile_disc()}}
+}
# Second argument must be numeric
#
# First argument is not NULL and is not NUMERIC
#
-do_test percentile-1.15 {
+do_test percentile-1.15.1 {
catchsql {
BEGIN;
UPDATE t1 SET x='50' WHERE x IS NULL;
SELECT percentile(x, 50) FROM t1;
}
-} {1 {1st argument to percentile() is not numeric}}
+} {1 {input to percentile() is not numeric}}
+do_test percentile-1.15.2 {
+ catchsql {
+ SELECT percentile_cont(x, 0.50) FROM t1;
+ }
+} {1 {input to percentile_cont() is not numeric}}
+do_test percentile-1.15.3 {
+ catchsql {
+ SELECT percentile_disc(x, 0.50) FROM t1;
+ }
+} {1 {input to percentile_disc() is not numeric}}
+do_test percentile-1.15.4 {
+ catchsql {
+ SELECT median(x) FROM t1;
+ }
+} {1 {input to median() is not numeric}}
do_test percentile-1.16 {
catchsql {
ROLLBACK;
UPDATE t1 SET x=x'3530' WHERE x IS NULL;
SELECT percentile(x, 50) FROM t1;
}
-} {1 {1st argument to percentile() is not numeric}}
+} {1 {input to percentile() is not numeric}}
do_test percentile-1.17 {
catchsql {
ROLLBACK;
# Infinity as an input
#
-do_test percentile-1.20 {
+do_test percentile-1.20.1 {
catchsql {
DELETE FROM t1;
INSERT INTO t1 SELECT x+0.0 FROM t2;
SELECT percentile(x,50) from t1;
}
} {1 {Inf input to percentile()}}
+do_test percentile-1.20.2 {
+ catchsql {
+ SELECT percentile_cont(x,0.50) from t1;
+ }
+} {1 {Inf input to percentile_cont()}}
+do_test percentile-1.20.3 {
+ catchsql {
+ SELECT percentile_disc(x,0.50) from t1;
+ }
+} {1 {Inf input to percentile_disc()}}
+do_test percentile-1.20.4 {
+ catchsql {
+ SELECT median(x) from t1;
+ }
+} {1 {Inf input to median()}}
+ifcapable ordered_set_aggregates {
+ do_test percentile-1.20.5 {
+ catchsql {
+ SELECT percentile(50) WITHIN GROUP (ORDER BY x) from t1;
+ }
+ } {1 {Inf input to percentile()}}
+ do_test percentile-1.20.6 {
+ catchsql {
+ SELECT percentile_cont(0.50) WITHIN GROUP (ORDER BY x) from t1;
+ }
+ } {1 {Inf input to percentile_cont()}}
+ do_test percentile-1.20.7 {
+ catchsql {
+ SELECT percentile_disc(0.50) WITHIN GROUP(ORDER BY X) from t1;
+ }
+ } {1 {Inf input to percentile_disc()}}
+ do_test percentile-1.20.8 {
+ catchsql {
+ SELECT median() WITHIN GROUP (ORDER BY x) from t1;
+ }
+ } {1 {Inf input to median()}}
+}
do_test percentile-1.21 {
catchsql {
UPDATE t1 SET x=-1.0e300*1.0e300 WHERE rowid=5;
(6, 'F', 'three', 0.0),
(7, 'G', 'one', 2.7);
}
-do_execsql_test percentile-3.1 {
-SELECT a, b, c, d,
- group_concat(b,'.') OVER W1 AS 'elements',
- median(d) OVER W1 AS 'median'
- FROM t1
- WINDOW W1 AS (ORDER BY c, a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
-} {
- 1 A one 8.4 A.D 9.7
- 4 D one 11.0 A.D.G 8.4
- 7 G one 2.7 D.G.C 5.9
- 3 C three 5.9 G.C.F 2.7
- 6 F three 0.0 C.F.B 5.9
- 2 B two 7.1 F.B.E 7.1
- 5 E two 12.5 B.E 9.8
-}
-do_execsql_test percentile-3.2 {
-SELECT a, b, c, d,
- group_concat(b,'.') OVER W1 AS 'elements',
- median(d) OVER W1 AS 'median'
- FROM t1
- WINDOW W1 AS (ORDER BY c, a ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
-} {
- 1 A one 8.4 A.D 9.7
- 4 D one 11.0 A.D.G 8.4
- 7 G one 2.7 A.D.G.C 7.15
- 3 C three 5.9 A.D.G.C.F 5.9
- 6 F three 0.0 A.D.G.C.F.B 6.5
- 2 B two 7.1 A.D.G.C.F.B.E 7.1
- 5 E two 12.5 A.D.G.C.F.B.E 7.1
-}
-do_execsql_test percentile-3.3 {
-SELECT a, b, c, d,
- group_concat(b,'.') OVER W1 AS 'elements',
- median(d) OVER W1 AS 'median'
- FROM t1
- WINDOW W1 AS (ORDER BY c, a ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
+foreach {id oba expr} {
+ 1 0 "median(d)"
+ 2 0 "percentile(d,50)"
+ 3 0 "percentile_cont(d,0.5)"
+ 4 1 "median() WITHIN GROUP (ORDER BY d)"
+ 5 1 "percentile(50) WITHIN GROUP (ORDER BY d)"
+ 6 1 "percentile_cont(0.5) WITHIN GROUP (ORDER BY d)"
} {
- 1 A one 8.4 A.D.G.C.F.B.E 7.1
- 4 D one 11.0 A.D.G.C.F.B.E 7.1
- 7 G one 2.7 D.G.C.F.B.E 6.5
- 3 C three 5.9 G.C.F.B.E 5.9
- 6 F three 0.0 C.F.B.E 6.5
- 2 B two 7.1 F.B.E 7.1
- 5 E two 12.5 B.E 9.8
+ if {$oba} {
+ ifcapable !ordered_set_aggregates break
+ }
+ set sql "SELECT a, b, c, d, \
+ group_concat(b,'.') OVER w1 AS 'elements', \
+ $expr OVER w1 AS 'median' \
+ FROM t1 \
+ WINDOW w1 AS (ORDER BY c, a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)"
+ do_execsql_test percentile-3.$id.1 $sql {
+ 1 A one 8.4 A.D 9.7
+ 4 D one 11.0 A.D.G 8.4
+ 7 G one 2.7 D.G.C 5.9
+ 3 C three 5.9 G.C.F 2.7
+ 6 F three 0.0 C.F.B 5.9
+ 2 B two 7.1 F.B.E 7.1
+ 5 E two 12.5 B.E 9.8
+ }
+
+ set sql "SELECT a, b, c, d, \
+ group_concat(b,'.') OVER w1 AS 'elements', \
+ $expr OVER w1 AS 'median' \
+ FROM t1 \
+ WINDOW w1 AS (ORDER BY c, a \
+ ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)"
+ do_execsql_test percentile-3.$id.2 $sql {
+ 1 A one 8.4 A.D 9.7
+ 4 D one 11.0 A.D.G 8.4
+ 7 G one 2.7 A.D.G.C 7.15
+ 3 C three 5.9 A.D.G.C.F 5.9
+ 6 F three 0.0 A.D.G.C.F.B 6.5
+ 2 B two 7.1 A.D.G.C.F.B.E 7.1
+ 5 E two 12.5 A.D.G.C.F.B.E 7.1
+ }
+
+ set sql "SELECT a, b, c, d, \
+ group_concat(b,'.') OVER w1 AS 'elements', \
+ $expr OVER w1 AS 'median' \
+ FROM t1 \
+ WINDOW w1 AS (ORDER BY c, a \
+ ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)"
+ do_execsql_test percentile-3.$id.3 $sql {
+ 1 A one 8.4 A.D.G.C.F.B.E 7.1
+ 4 D one 11.0 A.D.G.C.F.B.E 7.1
+ 7 G one 2.7 D.G.C.F.B.E 6.5
+ 3 C three 5.9 G.C.F.B.E 5.9
+ 6 F three 0.0 C.F.B.E 6.5
+ 2 B two 7.1 F.B.E 7.1
+ 5 E two 12.5 B.E 9.8
+ }
}
# Test case adapted from examples shown at