Add the percentile_cont(Y,P) variant of percentile() to the percentile

extension.

FossilOrigin-Name: 095c22e62248f8ef50cd8531171827f50a7bdd4fc1128bf0e616a3eb2dce980e
This commit is contained in:
drh 2024-07-24 13:41:09 +00:00
parent 4eabec5b18
commit b18d851119
4 changed files with 49 additions and 10 deletions

View File

@ -59,6 +59,9 @@
** into SQLite using the sqlite3_load_extension() interface.
**
** (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).
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
@ -110,7 +113,7 @@ static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){
if( argc==1 ){
/* Requirement 13: median(Y) is the same as percentile(Y,50). */
rPct = 50.0;
}else{
}else if( sqlite3_user_data(pCtx)==0 ){
/* Requirement 3: P must be a number between 0 and 100 */
eType = sqlite3_value_numeric_type(argv[1]);
rPct = sqlite3_value_double(argv[1]);
@ -120,6 +123,17 @@ static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){
"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. */
@ -266,5 +280,10 @@ int sqlite3_percentile_init(
SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
0, percentStep, percentFinal);
}
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "percentile_cont", 2,
SQLITE_UTF8|SQLITE_INNOCUOUS, &percentStep,
0, percentStep, percentFinal);
}
return rc;
}

View File

@ -1,5 +1,5 @@
C JNI\sSQLTester:\sfix\s--new\scommand\sto\sforce-replace\sdb\sif\sit\salready\sexists.\sAdd\sno-op\simpls\sfor\s--stmt-cache\sand\s--jsonglob\scommands.
D 2024-07-24T12:12:11.773
C Add\sthe\spercentile_cont(Y,P)\svariant\sof\spercentile()\sto\sthe\spercentile\nextension.
D 2024-07-24T13:41:09.099
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -404,7 +404,7 @@ F ext/misc/nextchar.c 7877914c2a80c2f181dd04c3dbef550dfb54c93495dc03da2403b5dd58
F ext/misc/noop.c f1a21cc9b7a4e667e5c8458d80ba680b8bd4315a003f256006046879f679c5a0
F ext/misc/normalize.c bd84355c118e297522aba74de34a4fd286fc775524e0499b14473918d09ea61f
F ext/misc/pcachetrace.c f4227ce03fb16aa8d6f321b72dd051097419d7a028a9853af048bee7645cb405
F ext/misc/percentile.c f15eb0fb0c2a95a482382b41415b9d1ae66eabf857ecac8e1e0eb3aa6b44e4d8
F ext/misc/percentile.c af1941dc87d45dd0c2698a3087fbfe9ee0d157e5e72da521430c4b784abcbe81
F ext/misc/prefixes.c 82645f79229877afab08c8b08ca1e7fa31921280906b90a61c294e4f540cd2a6
F ext/misc/qpvtab.c fc189e127f68f791af90a487f4460ec91539a716daf45a0c357e963fd47cc06c
F ext/misc/randomjson.c ef835fc64289e76ac4873b85fe12f9463a036168d7683cf2b773e36e6262c4ed
@ -1503,7 +1503,7 @@ F test/parser1.test 6ccdf5e459a5dc4673d3273dc311a7e9742ca952dd0551a6a6320d27035c
F test/pcache.test c8acbedd3b6fd0f9a7ca887a83b11d24a007972b
F test/pcache2.test af7f3deb1a819f77a6d0d81534e97d1cf62cd442
F test/pendingrace.test 6aa33756b950c4529f79c4f3817a9a1e4025bd0d9961571a05c0279bd183d9c6
F test/percentile.test 16fa70efabac459223d4886604abc629cacfb5f00ca7c3160da47fbeac870674
F test/percentile.test 74e383216a075251512d6ba98beb9dccd6da465e3f73817fc438379e3a628de7
F test/permutations.test 405542f1d659942994a6b38a9e024cf5cfd23eaa68c806aeb24a72d7c9186e80
F test/pg_common.tcl 3b27542224db1e713ae387459b5d117c836a5f6e328846922993b6d2b7640d9f
F test/pragma.test 11cb9310c42f921918f7f563e3c0b6e70f9f9c3a6a1cf12af8fccb6c574f3882
@ -2195,8 +2195,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 6e31b1bab1f014933c671a12a5930c1e88d611cfe68d389e9ce888bd8842addd
R 9fff1942fcabca82c9428b463cd9f75c
U stephan
Z 85b41f77db581220e66c2d5ad0a7d9f1
P f1185dc349d076a6b8ea22f9b5cd527bfd60e9afd55eaec754c0e2b4d7edd5d4
R 3c3a23f8bfc329ad379bd8e32b882636
U drh
Z 74cc206d7bf9e16b26dee128329dd3d6
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
f1185dc349d076a6b8ea22f9b5cd527bfd60e9afd55eaec754c0e2b4d7edd5d4
095c22e62248f8ef50cd8531171827f50a7bdd4fc1128bf0e616a3eb2dce980e

View File

@ -42,6 +42,20 @@ do_execsql_test percentile-1.1.median {
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 {
@ -112,6 +126,9 @@ do_test percentile-1.11 {
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
#
@ -121,6 +138,9 @@ do_test percentile-1.13 {
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
#