**
** (13) A separate median(Y) function is the equivalent percentile(Y,50).
**
-** (14) A separate percentile_cond(Y,X) function is the equivalent of
-** percentile(Y,X*100.0).
+** (14) Both median() and percentile(Y,P) can be used as window functions.
**
-** (15) All three SQL functions implemented by this module can also be
-** used as window-functions.
+** Differences from standard SQL:
+**
+** * The percentile(X,P) function is equivalent to the following in
+** standard SQL:
+**
+** (percentile(P/100.0) WITHIN GROUP (ORDER BY X))
+**
+** The SQLite syntax is much more compact. Note also that the
+** range of the P argument is 0..100 in SQLite, but 0..1 in the
+** standard.
+**
+** * No merge(X) function exists in the standard. Application developers
+** are expected to write "percentile_cont(0.5)WITHIN GROUP(ORDER BY X)".
**
** Implementation notes as of 2024-08-31:
**
-** * The regular aggregate-function versions of the merge(), percentile(),
-** and percentile_cond() routines work by accumulating all values in
-** an array of doubles, then sorting that array using a quicksort
-** before computing the answer. Thus the runtime is O(NlogN) where
-** N is the number of rows of input.
+** * The regular aggregate-function versions of the merge() and percentile(),
+** routines work by accumulating all values in an array of doubles, then
+** sorting that array using a quicksort before computing the answer. Thus
+** the runtime is O(NlogN) where N is the number of rows of input.
**
** * For the window-function versions of these routines, the array of
** inputs is sorted as soon as the first value is computed. Thereafter,
** the array is kept in sorted order using an insert-sort. This
** results in O(N*K) performance where K is the size of the window.
-** One can devise alternative implementations that give O(N*logN*logK)
+** One can imagine alternative implementations that give O(N*logN*logK)
** performance, but they require more complex logic and data structures.
** The developers have elected to keep the asymptotically slower
** algorithm for now, for simplicity, under the theory that window
if( argc==1 ){
/* Requirement 13: median(Y) is the same as percentile(Y,50). */
rPct = 50.0;
- }else if( sqlite3_user_data(pCtx)==0 ){
+ }else{
/* Requirement 3: P must be a number between 0 and 100 */
eType = sqlite3_value_numeric_type(argv[1]);
rPct = sqlite3_value_double(argv[1]);
"a number between 0.0 and 100.0", -1);
return;
}
- }else{
- /* Requirement 3: P must be a number between 0 and 1 */
- eType = sqlite3_value_numeric_type(argv[1]);
- rPct = sqlite3_value_double(argv[1]);
- if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
- || rPct<0.0 || rPct>1.0 ){
- sqlite3_result_error(pCtx, "2nd argument to percentile_cont() is not "
- "a number between 0.0 and 1.0", -1);
- return;
- }
- rPct *= 100.0;
}
/* Allocate the session context. */
percentStep, percentFinal,
percentValue, percentInverse, 0);
}
- if( rc==SQLITE_OK ){
- rc = sqlite3_create_window_function(db, "percentile_cont", 2,
- SQLITE_UTF8|SQLITE_INNOCUOUS, &percentStep,
- percentStep, percentFinal,
- percentValue, percentInverse, 0);
- }
return rc;
}
-C Allow\spercentile()\sand\smedian()\sto\sact\sas\swindow\sfunctions.
-D 2024-08-31T18:08:31.388
+C Omit\sthe\spercentile_cont()\sfunction\sadded\sby\s[095c22e62248f8ef]\s(and\snot\syet\nreleased)\ssince\sits\susage\sconflicts\swith\sthe\sPG\spercentile_cont()\sfunction.
+D 2024-08-31T18:35:10.951
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F ext/misc/noop.c f1a21cc9b7a4e667e5c8458d80ba680b8bd4315a003f256006046879f679c5a0
F ext/misc/normalize.c bd84355c118e297522aba74de34a4fd286fc775524e0499b14473918d09ea61f
F ext/misc/pcachetrace.c f4227ce03fb16aa8d6f321b72dd051097419d7a028a9853af048bee7645cb405
-F ext/misc/percentile.c 46627b7495c69344d384f667bb6c80ba2c4aeb779997a4e22fea1a39cd20beb9
+F ext/misc/percentile.c c2f03cfc67a64508817fb7daf1c978ca328b0cde9105a9d7369d89061b13b3ba
F ext/misc/prefixes.c 82645f79229877afab08c8b08ca1e7fa31921280906b90a61c294e4f540cd2a6
F ext/misc/qpvtab.c fc189e127f68f791af90a487f4460ec91539a716daf45a0c357e963fd47cc06c
F ext/misc/randomjson.c ef835fc64289e76ac4873b85fe12f9463a036168d7683cf2b773e36e6262c4ed
F test/pcache.test c8acbedd3b6fd0f9a7ca887a83b11d24a007972b
F test/pcache2.test af7f3deb1a819f77a6d0d81534e97d1cf62cd442
F test/pendingrace.test e99efc5ab3584da3dfc8cd6a0ec4e5a42214820574f5ea24ee93f1d84655f463
-F test/percentile.test 9ae96346c6f5f000eeeca023cabf85efefd744f7b66031147354a4da6dcb50d6
+F test/percentile.test 96b941965a5e5dff7c7d0d31c099385324897a692a300a4c4370b2fb760e7dd0
F test/permutations.test 405542f1d659942994a6b38a9e024cf5cfd23eaa68c806aeb24a72d7c9186e80
F test/pg_common.tcl 3b27542224db1e713ae387459b5d117c836a5f6e328846922993b6d2b7640d9f
F test/pragma.test 11cb9310c42f921918f7f563e3c0b6e70f9f9c3a6a1cf12af8fccb6c574f3882
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P ddc55efd2d59df3f20743b0533550436da945453c069025a3f871d28d40e13d4 f09904608195dac38172b0dd4dcab3190f33c116d468beff27f913a7433b400e
-R 55d55ff8a79d8904a0d256c79a96924e
-T +closed f09904608195dac38172b0dd4dcab3190f33c116d468beff27f913a7433b400e
+P 94cf96af8fee55449080655bddf81cbf5c078a02d7bb5dd7e4903b36f83a8c07
+R 13ad1198c1167f377793e03ac3caed1a
U drh
-Z debd110319d009b555173f7d44f9f322
+Z bc23090135f77ee58ac855dbcaf87c06
# Remove this line to create a well-formed Fossil manifest.
-94cf96af8fee55449080655bddf81cbf5c078a02d7bb5dd7e4903b36f83a8c07
+3fe0a852978f3f1218e37a58f0d3b54016d4116a3301aa32efa7c4c12c767755
SELECT median(x) FROM t1;
} 8.0
-foreach {in out} {
- 1.0 11.0
- 0.5 8.0
- 0.125 4.0
- 0.15 4.4
- 0.2 5.2
- 0.8 11.0
- 0.89 11.0
-} {
- do_test percentile-1.1b-$in {
- execsql {SELECT percentile_cont(x,$in) FROM t1}
- } $out
-}
-
# Add some NULL values.
#
do_test percentile-1.2 {
do_test percentile-1.12 {
catchsql {SELECT percentile(x,x'3530') FROM t1}
} {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}}
-do_test percentile-1.12b {
- catchsql {SELECT percentile_cont(x,x'3530') FROM t1}
-} {1 {2nd argument to percentile_cont() is not a number between 0.0 and 1.0}}
# Second argument is out of range
#
do_test percentile-1.14 {
catchsql {SELECT percentile(x,100.0000001) FROM t1}
} {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}}
-do_test percentile-1.14b {
- catchsql {SELECT percentile_cont(x,1.0000001) FROM t1}
-} {1 {2nd argument to percentile_cont() is not a number between 0.0 and 1.0}}
# First argument is not NULL and is not NUMERIC
#
}
do_execsql_test percentile-5.1 {
SELECT name, class, cost,
- percentile_cont(cost, 0.00) OVER w1 AS 'P0',
- percentile_cont(cost, 0.25) OVER w1 AS 'P1',
- percentile_cont(cost, 0.50) OVER w1 AS 'P2',
- percentile_cont(cost, 0.75) OVER w1 AS 'P3',
- percentile_cont(cost, 1.00) OVER w1 AS 'P4'
+ percentile(cost, 0) OVER w1 AS 'P0',
+ percentile(cost, 25) OVER w1 AS 'P1',
+ percentile(cost, 50) OVER w1 AS 'P2',
+ percentile(cost, 75) OVER w1 AS 'P3',
+ percentile(cost, 100) OVER w1 AS 'P4'
FROM user
WINDOW w1 AS (PARTITION BY class)
ORDER BY class, cost;