Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: b0a1359b7ca9fc1a2b3b15213e6001ecbda5a230 [file] [log] [blame]
drhdc04c582002-02-24 01:55:151/*
2** 2002 February 23
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
peter.d.reid60ec9142014-09-06 16:39:4612** This file contains the C-language implementations for many of the SQL
drhede7ae32014-08-06 11:58:4013** functions of SQLite. (Some function, and in particular the date and
14** time functions, are implemented separately.)
drhdc04c582002-02-24 01:55:1515*/
drhb659e9b2005-01-28 01:29:0816#include "sqliteInt.h"
drhd3a149e2002-02-24 17:12:5317#include <stdlib.h>
drh0bce8352002-02-28 00:41:1018#include <assert.h>
drhef9f7192020-01-17 19:14:0819#ifndef SQLITE_OMIT_FLOATING_POINT
drh05d7bfd2019-05-10 12:06:4720#include <math.h>
drhef9f7192020-01-17 19:14:0821#endif
danielk197788208052004-05-25 01:13:2022#include "vdbeInt.h"
drh0bce8352002-02-28 00:41:1023
drh55ef4d92005-08-14 01:20:3724/*
25** Return the collating function associated with a function.
26*/
danielk1977dc1bdc42004-06-11 10:51:2727static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
drha9e03b12015-03-12 06:46:5228 VdbeOp *pOp;
29 assert( context->pVdbe!=0 );
30 pOp = &context->pVdbe->aOp[context->iOp-1];
drha15cc472014-09-25 13:17:3031 assert( pOp->opcode==OP_CollSeq );
32 assert( pOp->p4type==P4_COLLSEQ );
33 return pOp->p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:2734}
35
drh0bce8352002-02-28 00:41:1036/*
drh7a957892012-02-02 17:35:4337** Indicate that the accumulator load should be skipped on this
38** iteration of the aggregate loop.
39*/
40static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
drh21d59782018-01-23 19:24:5441 assert( context->isError<=0 );
42 context->isError = -1;
drh7a957892012-02-02 17:35:4343 context->skipFlag = 1;
44}
45
46/*
drh0bce8352002-02-28 00:41:1047** Implementation of the non-aggregate min() and max() functions
48*/
drhf9b596e2004-05-26 16:54:4249static void minmaxFunc(
50 sqlite3_context *context,
51 int argc,
52 sqlite3_value **argv
53){
drh0bce8352002-02-28 00:41:1054 int i;
drh268380c2004-02-25 13:47:3155 int mask; /* 0 for min() or 0xffffffff for max() */
drhf9b596e2004-05-26 16:54:4256 int iBest;
danielk1977dc1bdc42004-06-11 10:51:2757 CollSeq *pColl;
drh0bce8352002-02-28 00:41:1058
drh65595cd2009-02-02 16:32:5559 assert( argc>1 );
drhc44af712004-09-02 15:53:5660 mask = sqlite3_user_data(context)==0 ? 0 : -1;
danielk1977dc1bdc42004-06-11 10:51:2761 pColl = sqlite3GetFuncCollSeq(context);
62 assert( pColl );
danielk1977c572ef72004-05-27 09:28:4163 assert( mask==-1 || mask==0 );
drhf9b596e2004-05-26 16:54:4264 iBest = 0;
drh9c054832004-05-31 18:51:5765 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
drhf9b596e2004-05-26 16:54:4266 for(i=1; i<argc; i++){
drh9c054832004-05-31 18:51:5767 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
danielk1977dc1bdc42004-06-11 10:51:2768 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
drh65595cd2009-02-02 16:32:5569 testcase( mask==0 );
drhf9b596e2004-05-26 16:54:4270 iBest = i;
drh0bce8352002-02-28 00:41:1071 }
72 }
drhf4479502004-05-27 03:12:5373 sqlite3_result_value(context, argv[iBest]);
drh0bce8352002-02-28 00:41:1074}
drh0bce8352002-02-28 00:41:1075
drh268380c2004-02-25 13:47:3176/*
77** Return the type of the argument.
78*/
drhf9b596e2004-05-26 16:54:4279static void typeofFunc(
80 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:2681 int NotUsed,
drhf9b596e2004-05-26 16:54:4282 sqlite3_value **argv
83){
drh9d8e4012017-07-06 13:51:5084 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
85 int i = sqlite3_value_type(argv[0]) - 1;
danielk197762c14b32008-11-19 09:05:2686 UNUSED_PARAMETER(NotUsed);
drh9d8e4012017-07-06 13:51:5087 assert( i>=0 && i<ArraySize(azType) );
88 assert( SQLITE_INTEGER==1 );
89 assert( SQLITE_FLOAT==2 );
90 assert( SQLITE_TEXT==3 );
91 assert( SQLITE_BLOB==4 );
92 assert( SQLITE_NULL==5 );
drh3cef3642017-07-14 19:22:0893 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
94 ** the datatype code for the initial datatype of the sqlite3_value object
95 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
96 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
drh9d8e4012017-07-06 13:51:5097 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
drh0bce8352002-02-28 00:41:1098}
99
drh9d44f182022-01-09 16:54:02100/* subtype(X)
101**
102** Return the subtype of X
103*/
104static void subtypeFunc(
105 sqlite3_context *context,
106 int argc,
107 sqlite3_value **argv
108){
drh69b0ce32022-02-04 13:15:01109 UNUSED_PARAMETER(argc);
drh9d44f182022-01-09 16:54:02110 sqlite3_result_int(context, sqlite3_value_subtype(argv[0]));
111}
drh5708d2d2005-06-22 10:53:59112
113/*
drh0bce8352002-02-28 00:41:10114** Implementation of the length() function
115*/
drhf9b596e2004-05-26 16:54:42116static void lengthFunc(
117 sqlite3_context *context,
118 int argc,
119 sqlite3_value **argv
120){
drh0bce8352002-02-28 00:41:10121 assert( argc==1 );
danielk1977f3d3c272008-11-19 16:52:44122 UNUSED_PARAMETER(argc);
drhf9b596e2004-05-26 16:54:42123 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57124 case SQLITE_BLOB:
125 case SQLITE_INTEGER:
126 case SQLITE_FLOAT: {
drhf4479502004-05-27 03:12:53127 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
drhf9b596e2004-05-26 16:54:42128 break;
129 }
drh9c054832004-05-31 18:51:57130 case SQLITE_TEXT: {
drh2646da72005-12-09 20:02:05131 const unsigned char *z = sqlite3_value_text(argv[0]);
drh7ea34692018-01-23 04:22:33132 const unsigned char *z0;
133 unsigned char c;
drh7a521cf2007-04-25 18:23:52134 if( z==0 ) return;
drh7ea34692018-01-23 04:22:33135 z0 = z;
136 while( (c = *z)!=0 ){
137 z++;
138 if( c>=0xc0 ){
139 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
140 }
drh4a919112007-05-15 11:55:09141 }
drh7ea34692018-01-23 04:22:33142 sqlite3_result_int(context, (int)(z-z0));
drhf9b596e2004-05-26 16:54:42143 break;
144 }
145 default: {
146 sqlite3_result_null(context);
147 break;
148 }
149 }
drh0bce8352002-02-28 00:41:10150}
151
152/*
drhb3d7f1c2023-06-03 11:22:30153** Implementation of the octet_length() function
154*/
155static void bytelengthFunc(
156 sqlite3_context *context,
157 int argc,
158 sqlite3_value **argv
159){
160 assert( argc==1 );
161 UNUSED_PARAMETER(argc);
162 switch( sqlite3_value_type(argv[0]) ){
163 case SQLITE_BLOB: {
164 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
165 break;
166 }
167 case SQLITE_INTEGER:
168 case SQLITE_FLOAT: {
169 i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF8 ? 1 : 2;
170 sqlite3_result_int64(context, sqlite3_value_bytes(argv[0])*m);
171 break;
172 }
173 case SQLITE_TEXT: {
174 if( sqlite3_value_encoding(argv[0])<=SQLITE_UTF8 ){
175 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
176 }else{
177 sqlite3_result_int(context, sqlite3_value_bytes16(argv[0]));
178 }
179 break;
180 }
181 default: {
182 sqlite3_result_null(context);
183 break;
184 }
185 }
186}
187
188/*
drh2ba3ccc2009-12-08 02:06:08189** Implementation of the abs() function.
190**
191** IMP: R-23979-26855 The abs(X) function returns the absolute value of
larrybrbc917382023-06-07 08:40:31192** the numeric argument X.
drh0bce8352002-02-28 00:41:10193*/
danielk19770ae8b832004-05-25 12:05:56194static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:10195 assert( argc==1 );
danielk1977f3d3c272008-11-19 16:52:44196 UNUSED_PARAMETER(argc);
drhf9b596e2004-05-26 16:54:42197 switch( sqlite3_value_type(argv[0]) ){
drh9c054832004-05-31 18:51:57198 case SQLITE_INTEGER: {
danielk1977f93bbbe2004-05-27 10:30:52199 i64 iVal = sqlite3_value_int64(argv[0]);
drh52fc8492006-02-23 21:43:55200 if( iVal<0 ){
drh693e6712014-01-24 22:58:00201 if( iVal==SMALLEST_INT64 ){
drheb091cd2013-11-09 19:47:15202 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
203 ** then abs(X) throws an integer overflow error since there is no
drh2ba3ccc2009-12-08 02:06:08204 ** equivalent positive 64-bit two complement value. */
drh52fc8492006-02-23 21:43:55205 sqlite3_result_error(context, "integer overflow", -1);
206 return;
207 }
208 iVal = -iVal;
larrybrbc917382023-06-07 08:40:31209 }
danielk1977f93bbbe2004-05-27 10:30:52210 sqlite3_result_int64(context, iVal);
drhf9b596e2004-05-26 16:54:42211 break;
212 }
drh9c054832004-05-31 18:51:57213 case SQLITE_NULL: {
drh2ba3ccc2009-12-08 02:06:08214 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
drhf9b596e2004-05-26 16:54:42215 sqlite3_result_null(context);
216 break;
217 }
218 default: {
drh2ba3ccc2009-12-08 02:06:08219 /* Because sqlite3_value_double() returns 0.0 if the argument is not
220 ** something that can be converted into a number, we have:
drh643091f2014-11-20 23:21:23221 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
222 ** that cannot be converted to a numeric value.
drh2ba3ccc2009-12-08 02:06:08223 */
danielk1977f93bbbe2004-05-27 10:30:52224 double rVal = sqlite3_value_double(argv[0]);
drh52fc8492006-02-23 21:43:55225 if( rVal<0 ) rVal = -rVal;
danielk1977f93bbbe2004-05-27 10:30:52226 sqlite3_result_double(context, rVal);
drhf9b596e2004-05-26 16:54:42227 break;
228 }
229 }
drh0bce8352002-02-28 00:41:10230}
231
232/*
drhd55e0722012-10-25 03:07:29233** Implementation of the instr() function.
234**
235** instr(haystack,needle) finds the first occurrence of needle
236** in haystack and returns the number of previous characters plus 1,
237** or 0 if needle does not occur within haystack.
238**
239** If both haystack and needle are BLOBs, then the result is one more than
240** the number of bytes in haystack prior to the first occurrence of needle,
241** or 0 if needle never occurs in haystack.
242*/
243static void instrFunc(
244 sqlite3_context *context,
245 int argc,
246 sqlite3_value **argv
247){
248 const unsigned char *zHaystack;
249 const unsigned char *zNeedle;
250 int nHaystack;
251 int nNeedle;
252 int typeHaystack, typeNeedle;
253 int N = 1;
254 int isText;
drhc930b402019-01-08 15:18:24255 unsigned char firstChar;
drh97b02502019-09-17 03:16:29256 sqlite3_value *pC1 = 0;
257 sqlite3_value *pC2 = 0;
drhd55e0722012-10-25 03:07:29258
drh68c804b2012-12-04 11:03:11259 UNUSED_PARAMETER(argc);
drhd55e0722012-10-25 03:07:29260 typeHaystack = sqlite3_value_type(argv[0]);
261 typeNeedle = sqlite3_value_type(argv[1]);
262 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
263 nHaystack = sqlite3_value_bytes(argv[0]);
264 nNeedle = sqlite3_value_bytes(argv[1]);
dan895decf2016-12-30 14:15:56265 if( nNeedle>0 ){
266 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
267 zHaystack = sqlite3_value_blob(argv[0]);
268 zNeedle = sqlite3_value_blob(argv[1]);
269 isText = 0;
drh97b02502019-09-17 03:16:29270 }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){
dan895decf2016-12-30 14:15:56271 zHaystack = sqlite3_value_text(argv[0]);
272 zNeedle = sqlite3_value_text(argv[1]);
273 isText = 1;
drh97b02502019-09-17 03:16:29274 }else{
275 pC1 = sqlite3_value_dup(argv[0]);
276 zHaystack = sqlite3_value_text(pC1);
drh9d702842019-09-18 11:16:46277 if( zHaystack==0 ) goto endInstrOOM;
278 nHaystack = sqlite3_value_bytes(pC1);
drh97b02502019-09-17 03:16:29279 pC2 = sqlite3_value_dup(argv[1]);
280 zNeedle = sqlite3_value_text(pC2);
drh9d702842019-09-18 11:16:46281 if( zNeedle==0 ) goto endInstrOOM;
282 nNeedle = sqlite3_value_bytes(pC2);
drh97b02502019-09-17 03:16:29283 isText = 1;
dan895decf2016-12-30 14:15:56284 }
drh9d702842019-09-18 11:16:46285 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
drhc930b402019-01-08 15:18:24286 firstChar = zNeedle[0];
287 while( nNeedle<=nHaystack
288 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
289 ){
dan895decf2016-12-30 14:15:56290 N++;
291 do{
292 nHaystack--;
293 zHaystack++;
294 }while( isText && (zHaystack[0]&0xc0)==0x80 );
295 }
296 if( nNeedle>nHaystack ) N = 0;
drhd55e0722012-10-25 03:07:29297 }
drhd55e0722012-10-25 03:07:29298 sqlite3_result_int(context, N);
drh97b02502019-09-17 03:16:29299endInstr:
300 sqlite3_value_free(pC1);
301 sqlite3_value_free(pC2);
drh9d702842019-09-18 11:16:46302 return;
303endInstrOOM:
304 sqlite3_result_error_nomem(context);
305 goto endInstr;
drhd55e0722012-10-25 03:07:29306}
307
308/*
drh6bcd5852022-01-08 21:00:38309** Implementation of the printf() (a.k.a. format()) SQL function.
drha5c14162013-12-17 15:03:06310*/
311static void printfFunc(
312 sqlite3_context *context,
313 int argc,
314 sqlite3_value **argv
315){
316 PrintfArguments x;
317 StrAccum str;
318 const char *zFormat;
319 int n;
drhc0490572015-05-02 11:45:53320 sqlite3 *db = sqlite3_context_db_handle(context);
drha5c14162013-12-17 15:03:06321
322 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
323 x.nArg = argc-1;
324 x.nUsed = 0;
325 x.apArg = argv+1;
drhc0490572015-05-02 11:45:53326 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
drh5f4a6862016-01-30 12:50:25327 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
drh0cdbe1a2018-05-09 13:46:26328 sqlite3_str_appendf(&str, zFormat, &x);
drha5c14162013-12-17 15:03:06329 n = str.nChar;
330 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
331 SQLITE_DYNAMIC);
332 }
333}
334
335/*
drhf764e6f2007-05-15 01:13:47336** Implementation of the substr() function.
337**
338** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
339** p1 is 1-indexed. So substr(x,1,1) returns the first character
340** of x. If x is text, then we actually count UTF-8 characters.
341** If x is a blob, then we count bytes.
342**
343** If p1 is negative, then we begin abs(p1) from the end of x[].
shaneh779b8f12009-11-12 05:04:50344**
drhf7b54962013-05-28 12:11:54345** If p2 is negative, return the p2 characters preceding p1.
drh0bce8352002-02-28 00:41:10346*/
drhf9b596e2004-05-26 16:54:42347static void substrFunc(
348 sqlite3_context *context,
349 int argc,
350 sqlite3_value **argv
351){
drh2646da72005-12-09 20:02:05352 const unsigned char *z;
353 const unsigned char *z2;
drh023ae032007-05-08 12:12:16354 int len;
drhf764e6f2007-05-15 01:13:47355 int p0type;
drh023ae032007-05-08 12:12:16356 i64 p1, p2;
drhf9b596e2004-05-26 16:54:42357
drh64f31512007-10-12 19:11:55358 assert( argc==3 || argc==2 );
drhf764e6f2007-05-15 01:13:47359 p0type = sqlite3_value_type(argv[0]);
drhb097ef22024-12-18 20:29:29360 p1 = sqlite3_value_int64(argv[1]);
drhf764e6f2007-05-15 01:13:47361 if( p0type==SQLITE_BLOB ){
362 len = sqlite3_value_bytes(argv[0]);
363 z = sqlite3_value_blob(argv[0]);
364 if( z==0 ) return;
drh1f0feef2007-05-15 13:27:07365 assert( len==sqlite3_value_bytes(argv[0]) );
drhf764e6f2007-05-15 01:13:47366 }else{
367 z = sqlite3_value_text(argv[0]);
368 if( z==0 ) return;
drh4a919112007-05-15 11:55:09369 len = 0;
drh4adc4cb2009-11-11 20:53:31370 if( p1<0 ){
371 for(z2=z; *z2; len++){
372 SQLITE_SKIP_UTF8(z2);
373 }
drh4a919112007-05-15 11:55:09374 }
drhf764e6f2007-05-15 01:13:47375 }
drh64f31512007-10-12 19:11:55376 if( argc==3 ){
drhb097ef22024-12-18 20:29:29377 p2 = sqlite3_value_int64(argv[2]);
drh3efac4a2025-02-09 20:23:29378 if( p2==0 && sqlite3_value_type(argv[2])==SQLITE_NULL ) return;
drh64f31512007-10-12 19:11:55379 }else{
drhbb4957f2008-03-20 14:03:29380 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
drh64f31512007-10-12 19:11:55381 }
drh3efac4a2025-02-09 20:23:29382 if( p1==0 ){
383#ifdef SQLITE_SUBSTR_COMPATIBILITY
384 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
385 ** as substr(X,1,N) - it returns the first N characters of X. This
386 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
387 ** from 2009-02-02 for compatibility of applications that exploited the
388 ** old buggy behavior. */
389 p1 = 1; /* <rdar://problem/6778339> */
390#endif
391 if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return;
392 }
drh0bce8352002-02-28 00:41:10393 if( p1<0 ){
drh89425d52002-02-28 03:04:48394 p1 += len;
drh653bc752002-02-28 03:31:10395 if( p1<0 ){
drhe0190a62024-12-28 12:32:01396 if( p2<0 ){
397 p2 = 0;
398 }else{
399 p2 += p1;
400 }
drh653bc752002-02-28 03:31:10401 p1 = 0;
402 }
drh0bce8352002-02-28 00:41:10403 }else if( p1>0 ){
404 p1--;
drh65595cd2009-02-02 16:32:55405 }else if( p2>0 ){
406 p2--;
drh0bce8352002-02-28 00:41:10407 }
drhe0190a62024-12-28 12:32:01408 if( p2<0 ){
409 if( p2<-p1 ){
410 p2 = p1;
411 }else{
412 p2 = -p2;
drh4e79c592009-02-01 19:23:32413 }
drhe0190a62024-12-28 12:32:01414 p1 -= p2;
drh4e79c592009-02-01 19:23:32415 }
drh65595cd2009-02-02 16:32:55416 assert( p1>=0 && p2>=0 );
drhf764e6f2007-05-15 01:13:47417 if( p0type!=SQLITE_BLOB ){
drh4a919112007-05-15 11:55:09418 while( *z && p1 ){
419 SQLITE_SKIP_UTF8(z);
420 p1--;
drhf764e6f2007-05-15 01:13:47421 }
drh4a919112007-05-15 11:55:09422 for(z2=z; *z2 && p2; p2--){
423 SQLITE_SKIP_UTF8(z2);
drhf764e6f2007-05-15 01:13:47424 }
drhbbf483f2014-09-09 20:30:24425 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
426 SQLITE_UTF8);
drhf764e6f2007-05-15 01:13:47427 }else{
drh2dcd4fa2024-12-19 12:08:39428 if( p1>=len ){
429 p1 = p2 = 0;
430 }else if( p2>len-p1 ){
drh4adc4cb2009-11-11 20:53:31431 p2 = len-p1;
drh2dcd4fa2024-12-19 12:08:39432 assert( p2>0 );
drh4adc4cb2009-11-11 20:53:31433 }
drhbbf483f2014-09-09 20:30:24434 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
drh0bce8352002-02-28 00:41:10435 }
drh0bce8352002-02-28 00:41:10436}
437
438/*
439** Implementation of the round() function
440*/
shanefbd60f82009-02-04 03:59:25441#ifndef SQLITE_OMIT_FLOATING_POINT
danielk19770ae8b832004-05-25 12:05:56442static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh47bc07d2024-12-18 18:29:19443 i64 n = 0;
drh0bce8352002-02-28 00:41:10444 double r;
drh50d654d2009-06-03 01:24:54445 char *zBuf;
drh0bce8352002-02-28 00:41:10446 assert( argc==1 || argc==2 );
danielk197751ad0ec2004-05-24 12:39:02447 if( argc==2 ){
drh9c054832004-05-31 18:51:57448 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
drh47bc07d2024-12-18 18:29:19449 n = sqlite3_value_int64(argv[1]);
danielk197751ad0ec2004-05-24 12:39:02450 if( n>30 ) n = 30;
451 if( n<0 ) n = 0;
452 }
drhd589a922006-03-02 03:02:48453 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
drh4f26d6c2004-05-26 23:25:30454 r = sqlite3_value_double(argv[0]);
shaneh147e1762010-02-17 04:19:27455 /* If Y==0 and X will fit in a 64-bit int,
456 ** handle the rounding directly,
457 ** otherwise use printf.
458 */
drh84422db2019-05-30 13:47:10459 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
460 /* The value has no fractional part so there is nothing to round */
larrybrbc917382023-06-07 08:40:31461 }else if( n==0 ){
drh84422db2019-05-30 13:47:10462 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
drh50d654d2009-06-03 01:24:54463 }else{
drhccfb50d2024-12-19 19:52:13464 zBuf = sqlite3_mprintf("%!.*f",(int)n,r);
shaneh147e1762010-02-17 04:19:27465 if( zBuf==0 ){
466 sqlite3_result_error_nomem(context);
467 return;
468 }
drh55700bc2019-06-07 22:51:13469 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
drh50d654d2009-06-03 01:24:54470 sqlite3_free(zBuf);
drh50d654d2009-06-03 01:24:54471 }
shaneh147e1762010-02-17 04:19:27472 sqlite3_result_double(context, r);
drh0bce8352002-02-28 00:41:10473}
shanefbd60f82009-02-04 03:59:25474#endif
drhdc04c582002-02-24 01:55:15475
danielk197726783a52007-08-29 14:06:22476/*
drhf3cdcdc2015-04-29 16:50:28477** Allocate nByte bytes of space using sqlite3Malloc(). If the
danielk197726783a52007-08-29 14:06:22478** allocation fails, call sqlite3_result_error_nomem() to notify
drh27e62db2009-04-02 10:16:17479** the database handle that malloc() has failed and return NULL.
480** If nByte is larger than the maximum string or blob length, then
481** raise an SQLITE_TOOBIG exception and return NULL.
danielk197726783a52007-08-29 14:06:22482*/
drhb1a6c3c2008-03-20 16:30:17483static void *contextMalloc(sqlite3_context *context, i64 nByte){
drhbb4957f2008-03-20 14:03:29484 char *z;
drh27e62db2009-04-02 10:16:17485 sqlite3 *db = sqlite3_context_db_handle(context);
drhef31c6a2009-04-02 09:07:12486 assert( nByte>0 );
drh27e62db2009-04-02 10:16:17487 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
488 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
489 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhbb4957f2008-03-20 14:03:29490 sqlite3_result_error_toobig(context);
491 z = 0;
492 }else{
drhda4ca9d2014-09-09 17:27:35493 z = sqlite3Malloc(nByte);
drhef31c6a2009-04-02 09:07:12494 if( !z ){
drhbb4957f2008-03-20 14:03:29495 sqlite3_result_error_nomem(context);
496 }
danielk1977a1644fd2007-08-29 12:31:25497 }
498 return z;
499}
500
drhdc04c582002-02-24 01:55:15501/*
502** Implementation of the upper() and lower() SQL functions.
503*/
danielk19770ae8b832004-05-25 12:05:56504static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh7a521cf2007-04-25 18:23:52505 char *z1;
506 const char *z2;
drh9310ef22007-04-27 17:16:20507 int i, n;
drh1d34fde2009-02-03 15:50:33508 UNUSED_PARAMETER(argc);
drh7a521cf2007-04-25 18:23:52509 z2 = (char*)sqlite3_value_text(argv[0]);
drh1f0feef2007-05-15 13:27:07510 n = sqlite3_value_bytes(argv[0]);
511 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
512 assert( z2==(char*)sqlite3_value_text(argv[0]) );
drh7a521cf2007-04-25 18:23:52513 if( z2 ){
drhb1a6c3c2008-03-20 16:30:17514 z1 = contextMalloc(context, ((i64)n)+1);
drh7a521cf2007-04-25 18:23:52515 if( z1 ){
drhdf901d32011-10-13 18:00:11516 for(i=0; i<n; i++){
517 z1[i] = (char)sqlite3Toupper(z2[i]);
drh7a521cf2007-04-25 18:23:52518 }
drhdf901d32011-10-13 18:00:11519 sqlite3_result_text(context, z1, n, sqlite3_free);
drh7a521cf2007-04-25 18:23:52520 }
drhdc04c582002-02-24 01:55:15521 }
522}
danielk19770ae8b832004-05-25 12:05:56523static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdf901d32011-10-13 18:00:11524 char *z1;
drh7a521cf2007-04-25 18:23:52525 const char *z2;
drh9310ef22007-04-27 17:16:20526 int i, n;
drh1d34fde2009-02-03 15:50:33527 UNUSED_PARAMETER(argc);
drh7a521cf2007-04-25 18:23:52528 z2 = (char*)sqlite3_value_text(argv[0]);
drh1f0feef2007-05-15 13:27:07529 n = sqlite3_value_bytes(argv[0]);
530 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
531 assert( z2==(char*)sqlite3_value_text(argv[0]) );
drh7a521cf2007-04-25 18:23:52532 if( z2 ){
drhb1a6c3c2008-03-20 16:30:17533 z1 = contextMalloc(context, ((i64)n)+1);
drh7a521cf2007-04-25 18:23:52534 if( z1 ){
drhdf901d32011-10-13 18:00:11535 for(i=0; i<n; i++){
536 z1[i] = sqlite3Tolower(z2[i]);
drh7a521cf2007-04-25 18:23:52537 }
drhdf901d32011-10-13 18:00:11538 sqlite3_result_text(context, z1, n, sqlite3_free);
drh7a521cf2007-04-25 18:23:52539 }
drhdc04c582002-02-24 01:55:15540 }
541}
542
drhae6bb952009-11-11 00:24:31543/*
drhcca9f3d2013-09-06 15:23:29544** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
545** as VDBE code so that unused argument values do not have to be computed.
546** However, we still need some kind of function implementation for this
547** routines in the function table. The noopFunc macro provides this.
548** noopFunc will never be called so it doesn't matter what the implementation
549** is. We might as well use the "version()" function as a substitute.
drhae6bb952009-11-11 00:24:31550*/
drhcca9f3d2013-09-06 15:23:29551#define noopFunc versionFunc /* Substitute function - never called */
drh3212e182002-02-28 00:46:26552
553/*
larrybrbc917382023-06-07 08:40:31554** Implementation of random(). Return a random integer.
drhf9ffac92002-03-02 19:00:31555*/
drhf9b596e2004-05-26 16:54:42556static void randomFunc(
557 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26558 int NotUsed,
559 sqlite3_value **NotUsed2
drhf9b596e2004-05-26 16:54:42560){
drh52fc8492006-02-23 21:43:55561 sqlite_int64 r;
danielk197762c14b32008-11-19 09:05:26562 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh2fa18682008-03-19 14:15:34563 sqlite3_randomness(sizeof(r), &r);
drh3034e3d2009-04-02 14:05:21564 if( r<0 ){
larrybrbc917382023-06-07 08:40:31565 /* We need to prevent a random number of 0x8000000000000000
drh3034e3d2009-04-02 14:05:21566 ** (or -9223372036854775808) since when you do abs() of that
567 ** number of you get the same value back again. To do this
568 ** in a way that is testable, mask the sign bit off of negative
larrybrbc917382023-06-07 08:40:31569 ** values, resulting in a positive value. Then take the
drh3034e3d2009-04-02 14:05:21570 ** 2s complement of that positive value. The end result can
571 ** therefore be no less than -9223372036854775807.
572 */
drhaf8001b2012-02-11 19:53:24573 r = -(r & LARGEST_INT64);
drh3034e3d2009-04-02 14:05:21574 }
drh52fc8492006-02-23 21:43:55575 sqlite3_result_int64(context, r);
drhf9ffac92002-03-02 19:00:31576}
577
578/*
drh137c7282007-01-29 17:58:28579** Implementation of randomblob(N). Return a random blob
580** that is N bytes long.
drh63cf66f2007-01-29 15:50:05581*/
drh137c7282007-01-29 17:58:28582static void randomBlob(
drh63cf66f2007-01-29 15:50:05583 sqlite3_context *context,
584 int argc,
585 sqlite3_value **argv
586){
drh3cb79202019-01-18 14:53:15587 sqlite3_int64 n;
drh137c7282007-01-29 17:58:28588 unsigned char *p;
drh63cf66f2007-01-29 15:50:05589 assert( argc==1 );
danielk1977f3d3c272008-11-19 16:52:44590 UNUSED_PARAMETER(argc);
drh3cb79202019-01-18 14:53:15591 n = sqlite3_value_int64(argv[0]);
drh023ae032007-05-08 12:12:16592 if( n<1 ){
593 n = 1;
594 }
danielk1977a1644fd2007-08-29 12:31:25595 p = contextMalloc(context, n);
drh02d85832007-05-07 19:31:15596 if( p ){
drh2fa18682008-03-19 14:15:34597 sqlite3_randomness(n, p);
drh17435752007-08-16 04:30:38598 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
drh02d85832007-05-07 19:31:15599 }
drh63cf66f2007-01-29 15:50:05600}
601
602/*
drh6ed41ad2002-04-06 14:10:47603** Implementation of the last_insert_rowid() SQL function. The return
danielk197724b03fd2004-05-10 10:34:34604** value is the same as the sqlite3_last_insert_rowid() API function.
drh6ed41ad2002-04-06 14:10:47605*/
danielk197751ad0ec2004-05-24 12:39:02606static void last_insert_rowid(
larrybrbc917382023-06-07 08:40:31607 sqlite3_context *context,
608 int NotUsed,
danielk197762c14b32008-11-19 09:05:26609 sqlite3_value **NotUsed2
danielk197751ad0ec2004-05-24 12:39:02610){
drhfa4a4b92008-03-19 21:45:51611 sqlite3 *db = sqlite3_context_db_handle(context);
danielk197762c14b32008-11-19 09:05:26612 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhab2f1f92010-01-11 18:26:42613 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
614 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
615 ** function. */
drhf9b596e2004-05-26 16:54:42616 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
drh6ed41ad2002-04-06 14:10:47617}
618
rdcf146a772004-02-25 22:51:06619/*
drhab2f1f92010-01-11 18:26:42620** Implementation of the changes() SQL function.
621**
drhc0bd26a2021-09-14 18:57:30622** IMP: R-32760-32347 The changes() SQL function is a wrapper
623** around the sqlite3_changes64() C/C++ function and hence follows the
624** same rules for counting changes.
rdcf146a772004-02-25 22:51:06625*/
danielk1977b28af712004-06-21 06:50:26626static void changes(
drhf9b596e2004-05-26 16:54:42627 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26628 int NotUsed,
629 sqlite3_value **NotUsed2
drhf9b596e2004-05-26 16:54:42630){
drhfa4a4b92008-03-19 21:45:51631 sqlite3 *db = sqlite3_context_db_handle(context);
danielk197762c14b32008-11-19 09:05:26632 UNUSED_PARAMETER2(NotUsed, NotUsed2);
dan2c718872021-06-22 18:32:05633 sqlite3_result_int64(context, sqlite3_changes64(db));
rdcb0c374f2004-02-20 22:53:38634}
rdcf146a772004-02-25 22:51:06635
636/*
danielk1977b28af712004-06-21 06:50:26637** Implementation of the total_changes() SQL function. The return value is
larrybr10496f72021-06-23 16:07:20638** the same as the sqlite3_total_changes64() API function.
rdcf146a772004-02-25 22:51:06639*/
danielk1977b28af712004-06-21 06:50:26640static void total_changes(
641 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26642 int NotUsed,
643 sqlite3_value **NotUsed2
danielk197751ad0ec2004-05-24 12:39:02644){
drhfa4a4b92008-03-19 21:45:51645 sqlite3 *db = sqlite3_context_db_handle(context);
danielk197762c14b32008-11-19 09:05:26646 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhc0bd26a2021-09-14 18:57:30647 /* IMP: R-11217-42568 This function is a wrapper around the
648 ** sqlite3_total_changes64() C/C++ interface. */
larrybr10496f72021-06-23 16:07:20649 sqlite3_result_int64(context, sqlite3_total_changes64(db));
rdcb0c374f2004-02-20 22:53:38650}
651
drh6ed41ad2002-04-06 14:10:47652/*
drh4e5ffc52004-08-31 00:52:37653** A structure defining how to do GLOB-style comparisons.
danielk1977d02eb1f2004-06-06 09:44:03654*/
drh4e5ffc52004-08-31 00:52:37655struct compareInfo {
drh07e83472016-01-11 03:48:18656 u8 matchAll; /* "*" or "%" */
657 u8 matchOne; /* "?" or "_" */
658 u8 matchSet; /* "[" or 0 */
659 u8 noCase; /* true to ignore case differences */
danielk1977d02eb1f2004-06-06 09:44:03660};
drh55ef4d92005-08-14 01:20:37661
drhb9175ae2007-12-07 18:39:04662/*
663** For LIKE and GLOB matching on EBCDIC machines, assume that every
larrybrbc917382023-06-07 08:40:31664** character is exactly one byte in size. Also, provide the Utf8Read()
drhb0870482015-06-17 13:20:54665** macro for fast reading of the next character in the common case where
666** the next character is ASCII.
drhb9175ae2007-12-07 18:39:04667*/
668#if defined(SQLITE_EBCDIC)
drh88b33222014-09-25 03:51:37669# define sqlite3Utf8Read(A) (*((*A)++))
drhb0870482015-06-17 13:20:54670# define Utf8Read(A) (*(A++))
drhb9175ae2007-12-07 18:39:04671#else
drhb0870482015-06-17 13:20:54672# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
drhb9175ae2007-12-07 18:39:04673#endif
674
drh4e5ffc52004-08-31 00:52:37675static const struct compareInfo globInfo = { '*', '?', '[', 0 };
drh55ef4d92005-08-14 01:20:37676/* The correct SQL-92 behavior is for the LIKE operator to ignore
677** case. Thus 'a' LIKE 'A' would be true. */
678static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
679/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
680** is case sensitive causing 'a' LIKE 'A' to be false */
681static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
danielk1977d02eb1f2004-06-06 09:44:03682
683/*
drh698a01c2016-12-01 18:49:40684** Possible error returns from patternMatch()
685*/
686#define SQLITE_MATCH 0
687#define SQLITE_NOMATCH 1
688#define SQLITE_NOWILDCARDMATCH 2
689
690/*
691** Compare two UTF-8 strings for equality where the first string is
692** a GLOB or LIKE expression. Return values:
693**
694** SQLITE_MATCH: Match
695** SQLITE_NOMATCH: No match
696** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
drh0ac65892002-04-20 14:24:41697**
drh4e5ffc52004-08-31 00:52:37698** Globbing rules:
drh0ac65892002-04-20 14:24:41699**
drh4e5ffc52004-08-31 00:52:37700** '*' Matches any sequence of zero or more characters.
danielk1977d02eb1f2004-06-06 09:44:03701**
drh4e5ffc52004-08-31 00:52:37702** '?' Matches exactly one character.
703**
704** [...] Matches one character from the enclosed list of
705** characters.
706**
707** [^...] Matches one character not in the enclosed list.
708**
709** With the [...] and [^...] matching, a ']' character can be included
710** in the list by making it the first character after '[' or '^'. A
711** range of characters can be specified using '-'. Example:
712** "[a-z]" matches any single lower-case letter. To match a '-', make
713** it the last character in the list.
714**
drh9fdfdc82014-09-25 11:08:57715** Like matching rules:
larrybrbc917382023-06-07 08:40:31716**
drh9fdfdc82014-09-25 11:08:57717** '%' Matches any sequence of zero or more characters
718**
719*** '_' Matches any one character
720**
721** Ec Where E is the "esc" character and c is any other
722** character, including '%', '_', and esc, match exactly c.
723**
drhb0870482015-06-17 13:20:54724** The comments within this routine usually assume glob matching.
drh9fdfdc82014-09-25 11:08:57725**
drh4e5ffc52004-08-31 00:52:37726** This routine is usually quick, but can be N**2 in the worst case.
drh0ac65892002-04-20 14:24:41727*/
danielk19777c6303c2004-11-17 16:41:29728static int patternCompare(
drh4e5ffc52004-08-31 00:52:37729 const u8 *zPattern, /* The glob pattern */
730 const u8 *zString, /* The string to compare against the glob */
danielk19777c6303c2004-11-17 16:41:29731 const struct compareInfo *pInfo, /* Information about how to do the compare */
drh698a01c2016-12-01 18:49:40732 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
danielk197751ad0ec2004-05-24 12:39:02733){
drh9fdfdc82014-09-25 11:08:57734 u32 c, c2; /* Next pattern and input string chars */
735 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
736 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
drh9fdfdc82014-09-25 11:08:57737 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
738 const u8 *zEscaped = 0; /* One past the last escaped input char */
larrybrbc917382023-06-07 08:40:31739
drhb0870482015-06-17 13:20:54740 while( (c = Utf8Read(zPattern))!=0 ){
drh9fdfdc82014-09-25 11:08:57741 if( c==matchAll ){ /* Match "*" */
742 /* Skip over multiple "*" characters in the pattern. If there
743 ** are also "?" characters, skip those as well, but consume a
744 ** single character of the input string for each "?" skipped */
larrybrbc917382023-06-07 08:40:31745 while( (c=Utf8Read(zPattern)) == matchAll
drh33941692021-02-15 17:02:01746 || (c == matchOne && matchOne!=0) ){
drh42610962012-09-17 18:56:32747 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
drh698a01c2016-12-01 18:49:40748 return SQLITE_NOWILDCARDMATCH;
drh4e5ffc52004-08-31 00:52:37749 }
danielk1977ad7dd422004-06-06 12:41:49750 }
drh66150952007-07-23 19:12:41751 if( c==0 ){
drh698a01c2016-12-01 18:49:40752 return SQLITE_MATCH; /* "*" at the end of the pattern matches */
drh88b33222014-09-25 03:51:37753 }else if( c==matchOther ){
drh07e83472016-01-11 03:48:18754 if( pInfo->matchSet==0 ){
drh88b33222014-09-25 03:51:37755 c = sqlite3Utf8Read(&zPattern);
drh698a01c2016-12-01 18:49:40756 if( c==0 ) return SQLITE_NOWILDCARDMATCH;
drh88b33222014-09-25 03:51:37757 }else{
drh9fdfdc82014-09-25 11:08:57758 /* "[...]" immediately follows the "*". We have to do a slow
759 ** recursive search in this case, but it is an unusual case. */
drh88b33222014-09-25 03:51:37760 assert( matchOther<0x80 ); /* '[' is a single-byte character */
drh698a01c2016-12-01 18:49:40761 while( *zString ){
762 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
763 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
drh88b33222014-09-25 03:51:37764 SQLITE_SKIP_UTF8(zString);
765 }
drh698a01c2016-12-01 18:49:40766 return SQLITE_NOWILDCARDMATCH;
drh66150952007-07-23 19:12:41767 }
drh66150952007-07-23 19:12:41768 }
drh9fdfdc82014-09-25 11:08:57769
770 /* At this point variable c contains the first character of the
771 ** pattern string past the "*". Search in the input string for the
dan1a4a7372016-12-01 17:34:59772 ** first matching character and recursively continue the match from
drh9fdfdc82014-09-25 11:08:57773 ** that point.
774 **
775 ** For a case-insensitive search, set variable cx to be the same as
776 ** c but in the other case and search the input string for either
777 ** c or cx.
778 */
dan2bc4a6c2022-10-14 15:10:36779 if( c<0x80 ){
drheba21f92017-10-30 18:49:11780 char zStop[3];
drh698a01c2016-12-01 18:49:40781 int bMatch;
drh9fdfdc82014-09-25 11:08:57782 if( noCase ){
drheba21f92017-10-30 18:49:11783 zStop[0] = sqlite3Toupper(c);
784 zStop[1] = sqlite3Tolower(c);
785 zStop[2] = 0;
drh66150952007-07-23 19:12:41786 }else{
drheba21f92017-10-30 18:49:11787 zStop[0] = c;
788 zStop[1] = 0;
drh4e5ffc52004-08-31 00:52:37789 }
drheba21f92017-10-30 18:49:11790 while(1){
791 zString += strcspn((const char*)zString, zStop);
792 if( zString[0]==0 ) break;
793 zString++;
drh698a01c2016-12-01 18:49:40794 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
795 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
drh9fdfdc82014-09-25 11:08:57796 }
797 }else{
drh698a01c2016-12-01 18:49:40798 int bMatch;
drhb0870482015-06-17 13:20:54799 while( (c2 = Utf8Read(zString))!=0 ){
drh9fdfdc82014-09-25 11:08:57800 if( c2!=c ) continue;
drh698a01c2016-12-01 18:49:40801 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
802 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
drh9fdfdc82014-09-25 11:08:57803 }
drh66150952007-07-23 19:12:41804 }
drh698a01c2016-12-01 18:49:40805 return SQLITE_NOWILDCARDMATCH;
drh88b33222014-09-25 03:51:37806 }
drh88b33222014-09-25 03:51:37807 if( c==matchOther ){
drh07e83472016-01-11 03:48:18808 if( pInfo->matchSet==0 ){
drh88b33222014-09-25 03:51:37809 c = sqlite3Utf8Read(&zPattern);
drh698a01c2016-12-01 18:49:40810 if( c==0 ) return SQLITE_NOMATCH;
drh9fdfdc82014-09-25 11:08:57811 zEscaped = zPattern;
drh88b33222014-09-25 03:51:37812 }else{
813 u32 prior_c = 0;
drh9fdfdc82014-09-25 11:08:57814 int seen = 0;
815 int invert = 0;
drh88b33222014-09-25 03:51:37816 c = sqlite3Utf8Read(&zString);
drh698a01c2016-12-01 18:49:40817 if( c==0 ) return SQLITE_NOMATCH;
drh88b33222014-09-25 03:51:37818 c2 = sqlite3Utf8Read(&zPattern);
819 if( c2=='^' ){
820 invert = 1;
821 c2 = sqlite3Utf8Read(&zPattern);
822 }
823 if( c2==']' ){
824 if( c==']' ) seen = 1;
825 c2 = sqlite3Utf8Read(&zPattern);
826 }
827 while( c2 && c2!=']' ){
828 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
829 c2 = sqlite3Utf8Read(&zPattern);
830 if( c>=prior_c && c<=c2 ) seen = 1;
831 prior_c = 0;
832 }else{
833 if( c==c2 ){
834 seen = 1;
835 }
836 prior_c = c2;
837 }
838 c2 = sqlite3Utf8Read(&zPattern);
839 }
840 if( c2==0 || (seen ^ invert)==0 ){
drh698a01c2016-12-01 18:49:40841 return SQLITE_NOMATCH;
drh88b33222014-09-25 03:51:37842 }
843 continue;
844 }
845 }
drhb0870482015-06-17 13:20:54846 c2 = Utf8Read(zString);
drh88b33222014-09-25 03:51:37847 if( c==c2 ) continue;
drhc80937a2016-06-06 01:48:14848 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
drh9fdfdc82014-09-25 11:08:57849 continue;
850 }
851 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
drh698a01c2016-12-01 18:49:40852 return SQLITE_NOMATCH;
danielk197751ad0ec2004-05-24 12:39:02853 }
drh698a01c2016-12-01 18:49:40854 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
drh0ac65892002-04-20 14:24:41855}
drh4e5ffc52004-08-31 00:52:37856
drh55ef4d92005-08-14 01:20:37857/*
drh698a01c2016-12-01 18:49:40858** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
859** non-zero if there is no match.
drh56282a52013-04-10 16:13:38860*/
861int sqlite3_strglob(const char *zGlobPattern, const char *zString){
drh42dddb92022-10-25 13:44:18862 if( zString==0 ){
863 return zGlobPattern!=0;
864 }else if( zGlobPattern==0 ){
865 return 1;
866 }else {
867 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
868 }
drh56282a52013-04-10 16:13:38869}
870
871/*
drh698a01c2016-12-01 18:49:40872** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
873** a miss - like strcmp().
drh8b4a94a2015-11-24 21:23:59874*/
875int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
drh42dddb92022-10-25 13:44:18876 if( zStr==0 ){
877 return zPattern!=0;
878 }else if( zPattern==0 ){
879 return 1;
880 }else{
881 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
882 }
drh8b4a94a2015-11-24 21:23:59883}
884
885/*
drh55ef4d92005-08-14 01:20:37886** Count the number of times that the LIKE operator (or GLOB which is
887** just a variation of LIKE) gets called. This is used for testing
888** only.
889*/
890#ifdef SQLITE_TEST
891int sqlite3_like_count = 0;
892#endif
893
danielk19773f6b0872004-06-17 05:36:44894
895/*
896** Implementation of the like() SQL function. This function implements
larrybr55be2162023-06-07 17:03:22897** the built-in LIKE operator. The first argument to the function is the
danielk19773f6b0872004-06-17 05:36:44898** pattern and the second argument is the string. So, the SQL statements:
899**
900** A LIKE B
901**
902** is implemented as like(B,A).
903**
drh55ef4d92005-08-14 01:20:37904** This same function (with a different compareInfo structure) computes
905** the GLOB operator.
danielk19773f6b0872004-06-17 05:36:44906*/
907static void likeFunc(
larrybrbc917382023-06-07 08:40:31908 sqlite3_context *context,
909 int argc,
danielk19773f6b0872004-06-17 05:36:44910 sqlite3_value **argv
911){
drhbeb818d2007-05-08 15:34:47912 const unsigned char *zA, *zB;
drh07e83472016-01-11 03:48:18913 u32 escape;
drh27e62db2009-04-02 10:16:17914 int nPat;
drhbb4957f2008-03-20 14:03:29915 sqlite3 *db = sqlite3_context_db_handle(context);
drh07e83472016-01-11 03:48:18916 struct compareInfo *pInfo = sqlite3_user_data(context);
drh589c7872020-03-19 18:13:28917 struct compareInfo backupInfo;
drhbeb818d2007-05-08 15:34:47918
drh41d2e662015-12-01 21:23:07919#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
920 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
921 || sqlite3_value_type(argv[1])==SQLITE_BLOB
922 ){
923#ifdef SQLITE_TEST
924 sqlite3_like_count++;
925#endif
926 sqlite3_result_int(context, 0);
927 return;
928 }
929#endif
drh1f0feef2007-05-15 13:27:07930
drhbeb818d2007-05-08 15:34:47931 /* Limit the length of the LIKE or GLOB pattern to avoid problems
932 ** of deep recursion and N*N behavior in patternCompare().
933 */
drh27e62db2009-04-02 10:16:17934 nPat = sqlite3_value_bytes(argv[0]);
935 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
936 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
937 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
drhbeb818d2007-05-08 15:34:47938 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
939 return;
940 }
danielk19777c6303c2004-11-17 16:41:29941 if( argc==3 ){
942 /* The escape character string must consist of a single UTF-8 character.
943 ** Otherwise, return an error.
944 */
945 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
drh7a521cf2007-04-25 18:23:52946 if( zEsc==0 ) return;
drhee858132007-05-08 20:37:38947 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
larrybrbc917382023-06-07 08:40:31948 sqlite3_result_error(context,
danielk19777c6303c2004-11-17 16:41:29949 "ESCAPE expression must be a single character", -1);
950 return;
951 }
drh42610962012-09-17 18:56:32952 escape = sqlite3Utf8Read(&zEsc);
drh589c7872020-03-19 18:13:28953 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
954 memcpy(&backupInfo, pInfo, sizeof(backupInfo));
955 pInfo = &backupInfo;
956 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
957 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
958 }
drh07e83472016-01-11 03:48:18959 }else{
960 escape = pInfo->matchSet;
danielk19777c6303c2004-11-17 16:41:29961 }
drhcf833232019-04-30 11:54:36962 zB = sqlite3_value_text(argv[0]);
963 zA = sqlite3_value_text(argv[1]);
danielk19773f6b0872004-06-17 05:36:44964 if( zA && zB ){
drh55ef4d92005-08-14 01:20:37965#ifdef SQLITE_TEST
966 sqlite3_like_count++;
967#endif
drhf49759b2017-08-25 19:51:51968 sqlite3_result_int(context,
969 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
danielk197751ad0ec2004-05-24 12:39:02970 }
drh8912d102002-05-26 21:34:58971}
972
973/*
974** Implementation of the NULLIF(x,y) function. The result is the first
975** argument if the arguments are different. The result is NULL if the
976** arguments are equal to each other.
977*/
drhf9b596e2004-05-26 16:54:42978static void nullifFunc(
979 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26980 int NotUsed,
drhf9b596e2004-05-26 16:54:42981 sqlite3_value **argv
982){
danielk1977dc1bdc42004-06-11 10:51:27983 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk197762c14b32008-11-19 09:05:26984 UNUSED_PARAMETER(NotUsed);
danielk1977dc1bdc42004-06-11 10:51:27985 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
drhf4479502004-05-27 03:12:53986 sqlite3_result_value(context, argv[0]);
drh8912d102002-05-26 21:34:58987 }
drh0ac65892002-04-20 14:24:41988}
989
drh647cb0e2002-11-04 19:32:25990/*
drh47baebc2009-08-14 16:01:24991** Implementation of the sqlite_version() function. The result is the version
drh647cb0e2002-11-04 19:32:25992** of the SQLite library that is running.
993*/
drhf9b596e2004-05-26 16:54:42994static void versionFunc(
995 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26996 int NotUsed,
997 sqlite3_value **NotUsed2
drhf9b596e2004-05-26 16:54:42998){
danielk197762c14b32008-11-19 09:05:26999 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhab2f1f92010-01-11 18:26:421000 /* IMP: R-48699-48617 This function is an SQL wrapper around the
1001 ** sqlite3_libversion() C-interface. */
1002 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
drh647cb0e2002-11-04 19:32:251003}
1004
drh47baebc2009-08-14 16:01:241005/*
1006** Implementation of the sqlite_source_id() function. The result is a string
1007** that identifies the particular version of the source code used to build
1008** SQLite.
1009*/
1010static void sourceidFunc(
1011 sqlite3_context *context,
1012 int NotUsed,
1013 sqlite3_value **NotUsed2
1014){
1015 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhab2f1f92010-01-11 18:26:421016 /* IMP: R-24470-31136 This function is an SQL wrapper around the
1017 ** sqlite3_sourceid() C interface. */
1018 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
drh47baebc2009-08-14 16:01:241019}
1020
shanehbdea6d12010-02-23 04:19:541021/*
drh3ca84ef2011-04-25 18:03:101022** Implementation of the sqlite_log() function. This is a wrapper around
1023** sqlite3_log(). The return value is NULL. The function exists purely for
1024** its side-effects.
1025*/
drh840561f2011-04-27 18:08:421026static void errlogFunc(
drh3ca84ef2011-04-25 18:03:101027 sqlite3_context *context,
1028 int argc,
1029 sqlite3_value **argv
1030){
1031 UNUSED_PARAMETER(argc);
1032 UNUSED_PARAMETER(context);
1033 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
1034}
1035
1036/*
shanehdc97a8c2010-02-23 20:08:351037** Implementation of the sqlite_compileoption_used() function.
1038** The result is an integer that identifies if the compiler option
1039** was used to build SQLite.
shanehbdea6d12010-02-23 04:19:541040*/
shanehdc97a8c2010-02-23 20:08:351041#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1042static void compileoptionusedFunc(
shanehbdea6d12010-02-23 04:19:541043 sqlite3_context *context,
shanehdc97a8c2010-02-23 20:08:351044 int argc,
1045 sqlite3_value **argv
shanehbdea6d12010-02-23 04:19:541046){
shanehdc97a8c2010-02-23 20:08:351047 const char *zOptName;
1048 assert( argc==1 );
1049 UNUSED_PARAMETER(argc);
drha3e414c2010-08-03 18:06:251050 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
1051 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
1052 ** function.
1053 */
drh264a2d42010-02-25 15:28:411054 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
shanehdc97a8c2010-02-23 20:08:351055 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
shanehdc97a8c2010-02-23 20:08:351056 }
shanehbdea6d12010-02-23 04:19:541057}
shanehdc97a8c2010-02-23 20:08:351058#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1059
1060/*
larrybrbc917382023-06-07 08:40:311061** Implementation of the sqlite_compileoption_get() function.
1062** The result is a string that identifies the compiler options
shanehdc97a8c2010-02-23 20:08:351063** used to build SQLite.
1064*/
1065#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1066static void compileoptiongetFunc(
1067 sqlite3_context *context,
1068 int argc,
1069 sqlite3_value **argv
1070){
1071 int n;
1072 assert( argc==1 );
1073 UNUSED_PARAMETER(argc);
drha3e414c2010-08-03 18:06:251074 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
1075 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
1076 */
shanehdc97a8c2010-02-23 20:08:351077 n = sqlite3_value_int(argv[0]);
1078 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
1079}
1080#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
shanehbdea6d12010-02-23 04:19:541081
drh137c7282007-01-29 17:58:281082/* Array for converting from half-bytes (nybbles) into ASCII hex
1083** digits. */
1084static const char hexdigits[] = {
1085 '0', '1', '2', '3', '4', '5', '6', '7',
larrybrbc917382023-06-07 08:40:311086 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
drh137c7282007-01-29 17:58:281087};
danielk1977d641d642004-11-18 15:44:291088
drh47394702003-08-20 01:03:331089/*
drhef95d552021-12-10 21:01:241090** Append to pStr text that is the SQL literal representation of the
1091** value contained in pValue.
drh47394702003-08-20 01:03:331092*/
drhb6205d42025-02-24 13:51:241093void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue, int bEscape){
drhef95d552021-12-10 21:01:241094 /* As currently implemented, the string must be initially empty.
1095 ** we might relax this requirement in the future, but that will
1096 ** require enhancements to the implementation. */
1097 assert( pStr!=0 && pStr->nChar==0 );
1098
1099 switch( sqlite3_value_type(pValue) ){
drh9c054832004-05-31 18:51:571100 case SQLITE_FLOAT: {
drh72b3fbc2012-06-19 03:11:251101 double r1, r2;
drhef95d552021-12-10 21:01:241102 const char *zVal;
1103 r1 = sqlite3_value_double(pValue);
drhf79b0bd2024-02-26 22:28:211104 sqlite3_str_appendf(pStr, "%!0.15g", r1);
drhef95d552021-12-10 21:01:241105 zVal = sqlite3_str_value(pStr);
1106 if( zVal ){
1107 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
1108 if( r1!=r2 ){
1109 sqlite3_str_reset(pStr);
drhf79b0bd2024-02-26 22:28:211110 sqlite3_str_appendf(pStr, "%!0.20e", r1);
drhef95d552021-12-10 21:01:241111 }
drh72b3fbc2012-06-19 03:11:251112 }
drh72b3fbc2012-06-19 03:11:251113 break;
1114 }
1115 case SQLITE_INTEGER: {
drhef95d552021-12-10 21:01:241116 sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue));
drhf9b596e2004-05-26 16:54:421117 break;
1118 }
danielk19773f41e972004-06-08 00:39:011119 case SQLITE_BLOB: {
drhef95d552021-12-10 21:01:241120 char const *zBlob = sqlite3_value_blob(pValue);
drh79b9bc42022-12-21 19:11:561121 i64 nBlob = sqlite3_value_bytes(pValue);
drhef95d552021-12-10 21:01:241122 assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */
1123 sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4);
1124 if( pStr->accError==0 ){
1125 char *zText = pStr->zText;
danielk19773f41e972004-06-08 00:39:011126 int i;
1127 for(i=0; i<nBlob; i++){
1128 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
1129 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
1130 }
1131 zText[(nBlob*2)+2] = '\'';
1132 zText[(nBlob*2)+3] = '\0';
1133 zText[0] = 'X';
1134 zText[1] = '\'';
drhef95d552021-12-10 21:01:241135 pStr->nChar = nBlob*2 + 3;
danielk19773f41e972004-06-08 00:39:011136 }
1137 break;
1138 }
drh9c054832004-05-31 18:51:571139 case SQLITE_TEXT: {
drhef95d552021-12-10 21:01:241140 const unsigned char *zArg = sqlite3_value_text(pValue);
drhb6205d42025-02-24 13:51:241141 sqlite3_str_appendf(pStr, bEscape ? "%#Q" : "%Q", zArg);
drha0df4cc2009-02-02 17:29:591142 break;
1143 }
1144 default: {
drhef95d552021-12-10 21:01:241145 assert( sqlite3_value_type(pValue)==SQLITE_NULL );
1146 sqlite3_str_append(pStr, "NULL", 4);
drha0df4cc2009-02-02 17:29:591147 break;
drhf9b596e2004-05-26 16:54:421148 }
drh47394702003-08-20 01:03:331149 }
1150}
1151
drh137c7282007-01-29 17:58:281152/*
drh4d70dba2025-02-22 23:18:381153** Return true if z[] begins with N hexadecimal digits, and write
1154** a decoding of those digits into *pVal. Or return false if any
1155** one of the first N characters in z[] is not a hexadecimal digit.
1156*/
1157static int isNHex(const char *z, int N, u32 *pVal){
1158 int i;
1159 int v = 0;
1160 for(i=0; i<N; i++){
1161 if( !sqlite3Isxdigit(z[i]) ) return 0;
1162 v = (v<<4) + sqlite3HexToInt(z[i]);
1163 }
1164 *pVal = v;
1165 return 1;
1166}
1167
1168/*
1169** Implementation of the UNISTR() function.
1170**
1171** This is intended to be a work-alike of the UNISTR() function in
1172** PostgreSQL. Quoting from the PG documentation (PostgreSQL 17 -
1173** scraped on 2025-02-22):
1174**
1175** Evaluate escaped Unicode characters in the argument. Unicode
1176** characters can be specified as \XXXX (4 hexadecimal digits),
1177** \+XXXXXX (6 hexadecimal digits), \uXXXX (4 hexadecimal digits),
1178** or \UXXXXXXXX (8 hexadecimal digits). To specify a backslash,
1179** write two backslashes. All other characters are taken literally.
1180*/
1181static void unistrFunc(
1182 sqlite3_context *context,
1183 int argc,
1184 sqlite3_value **argv
1185){
1186 char *zOut;
1187 const char *zIn;
1188 int nIn;
1189 int i, j, n;
1190 u32 v;
1191
1192 assert( argc==1 );
drhb6205d42025-02-24 13:51:241193 UNUSED_PARAMETER( argc );
drh4d70dba2025-02-22 23:18:381194 zIn = (const char*)sqlite3_value_text(argv[0]);
1195 if( zIn==0 ) return;
1196 nIn = sqlite3_value_bytes(argv[0]);
1197 zOut = sqlite3_malloc64(nIn+1);
1198 if( zOut==0 ){
1199 sqlite3_result_error_nomem(context);
1200 return;
1201 }
1202 i = j = 0;
1203 while( i<nIn ){
1204 char *z = strchr(&zIn[i],'\\');
1205 if( z==0 ){
1206 n = nIn - i;
1207 memmove(&zOut[j], &zIn[i], n);
1208 j += n;
1209 break;
1210 }
1211 n = z - &zIn[i];
1212 if( n>0 ){
1213 memmove(&zOut[j], &zIn[i], n);
1214 j += n;
1215 i += n;
1216 }
1217 if( zIn[i+1]=='\\' ){
1218 i += 2;
1219 zOut[j++] = '\\';
1220 }else if( sqlite3Isxdigit(zIn[i+1]) ){
1221 if( !isNHex(&zIn[i+1], 4, &v) ) goto unistr_error;
1222 i += 5;
drha357a902025-02-25 11:47:341223 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
drh4d70dba2025-02-22 23:18:381224 }else if( zIn[i+1]=='+' ){
1225 if( !isNHex(&zIn[i+2], 6, &v) ) goto unistr_error;
1226 i += 8;
drha357a902025-02-25 11:47:341227 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
drh4d70dba2025-02-22 23:18:381228 }else if( zIn[i+1]=='u' ){
1229 if( !isNHex(&zIn[i+2], 4, &v) ) goto unistr_error;
1230 i += 6;
drha357a902025-02-25 11:47:341231 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
drh4d70dba2025-02-22 23:18:381232 }else if( zIn[i+1]=='U' ){
1233 if( !isNHex(&zIn[i+2], 8, &v) ) goto unistr_error;
1234 i += 10;
drha357a902025-02-25 11:47:341235 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
drh4d70dba2025-02-22 23:18:381236 }else{
1237 goto unistr_error;
1238 }
1239 }
1240 zOut[j] = 0;
1241 sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8);
1242 return;
1243
1244unistr_error:
1245 sqlite3_free(zOut);
1246 sqlite3_result_error(context, "invalid Unicode escape", -1);
1247 return;
1248}
1249
1250
1251/*
larrybrbc917382023-06-07 08:40:311252** Implementation of the QUOTE() function.
drhef95d552021-12-10 21:01:241253**
1254** The quote(X) function returns the text of an SQL literal which is the
1255** value of its argument suitable for inclusion into an SQL statement.
1256** Strings are surrounded by single-quotes with escapes on interior quotes
1257** as needed. BLOBs are encoded as hexadecimal literals. Strings with
1258** embedded NUL characters cannot be represented as string literals in SQL
1259** and hence the returned string literal is truncated prior to the first NUL.
drhb6205d42025-02-24 13:51:241260**
1261** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is
1262** implemented instead. The difference is that UNISTR_QUOTE() uses the
1263** UNISTR() function to escape control characters.
drhef95d552021-12-10 21:01:241264*/
1265static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1266 sqlite3_str str;
1267 sqlite3 *db = sqlite3_context_db_handle(context);
1268 assert( argc==1 );
1269 UNUSED_PARAMETER(argc);
1270 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
drhb6205d42025-02-24 13:51:241271 sqlite3QuoteValue(&str,argv[0],SQLITE_PTR_TO_INT(sqlite3_user_data(context)));
drhef95d552021-12-10 21:01:241272 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
1273 SQLITE_DYNAMIC);
danaf7b8dc2022-02-12 13:37:271274 if( str.accError!=SQLITE_OK ){
1275 sqlite3_result_null(context);
1276 sqlite3_result_error_code(context, str.accError);
drhef95d552021-12-10 21:01:241277 }
1278}
1279
1280/*
drhd495d8c2013-02-22 19:34:251281** The unicode() function. Return the integer unicode code-point value
larrybrbc917382023-06-07 08:40:311282** for the first character of the input string.
drhd495d8c2013-02-22 19:34:251283*/
1284static void unicodeFunc(
1285 sqlite3_context *context,
1286 int argc,
1287 sqlite3_value **argv
1288){
1289 const unsigned char *z = sqlite3_value_text(argv[0]);
drh1d59d032013-03-01 23:40:261290 (void)argc;
drhd495d8c2013-02-22 19:34:251291 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
1292}
1293
1294/*
1295** The char() function takes zero or more arguments, each of which is
1296** an integer. It constructs a string where each character of the string
1297** is the unicode character for the corresponding integer argument.
1298*/
1299static void charFunc(
1300 sqlite3_context *context,
1301 int argc,
1302 sqlite3_value **argv
1303){
1304 unsigned char *z, *zOut;
1305 int i;
drhf3cdcdc2015-04-29 16:50:281306 zOut = z = sqlite3_malloc64( argc*4+1 );
drhd495d8c2013-02-22 19:34:251307 if( z==0 ){
1308 sqlite3_result_error_nomem(context);
1309 return;
1310 }
1311 for(i=0; i<argc; i++){
mistachkinc9545442013-02-26 05:42:301312 sqlite3_int64 x;
drhd495d8c2013-02-22 19:34:251313 unsigned c;
1314 x = sqlite3_value_int64(argv[i]);
1315 if( x<0 || x>0x10ffff ) x = 0xfffd;
1316 c = (unsigned)(x & 0x1fffff);
drhfe7a5d12013-03-07 14:00:041317 if( c<0x00080 ){
1318 *zOut++ = (u8)(c&0xFF);
1319 }else if( c<0x00800 ){
1320 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
1321 *zOut++ = 0x80 + (u8)(c & 0x3F);
1322 }else if( c<0x10000 ){
1323 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
1324 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
1325 *zOut++ = 0x80 + (u8)(c & 0x3F);
drhd495d8c2013-02-22 19:34:251326 }else{
drhfe7a5d12013-03-07 14:00:041327 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
1328 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
1329 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
1330 *zOut++ = 0x80 + (u8)(c & 0x3F);
1331 } \
drhd495d8c2013-02-22 19:34:251332 }
drh61a5b6e2023-07-22 15:21:411333 *zOut = 0;
drhbbf483f2014-09-09 20:30:241334 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
drhd495d8c2013-02-22 19:34:251335}
1336
1337/*
drh137c7282007-01-29 17:58:281338** The hex() function. Interpret the argument as a blob. Return
1339** a hexadecimal rendering as text.
1340*/
1341static void hexFunc(
1342 sqlite3_context *context,
1343 int argc,
1344 sqlite3_value **argv
1345){
1346 int i, n;
1347 const unsigned char *pBlob;
1348 char *zHex, *z;
1349 assert( argc==1 );
danielk1977f3d3c272008-11-19 16:52:441350 UNUSED_PARAMETER(argc);
drh1f0feef2007-05-15 13:27:071351 pBlob = sqlite3_value_blob(argv[0]);
drh137c7282007-01-29 17:58:281352 n = sqlite3_value_bytes(argv[0]);
drh1f0feef2007-05-15 13:27:071353 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
drhb1a6c3c2008-03-20 16:30:171354 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
danielk1977a1644fd2007-08-29 12:31:251355 if( zHex ){
1356 for(i=0; i<n; i++, pBlob++){
1357 unsigned char c = *pBlob;
1358 *(z++) = hexdigits[(c>>4)&0xf];
1359 *(z++) = hexdigits[c&0xf];
1360 }
1361 *z = 0;
larrybr99d43972023-10-13 01:39:471362 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
1363 sqlite3_free, SQLITE_UTF8);
drh137c7282007-01-29 17:58:281364 }
drh137c7282007-01-29 17:58:281365}
1366
drh26b6d902007-03-17 13:27:541367/*
dana50db432023-01-24 17:19:471368** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
1369** contains character ch, or 0 if it does not.
1370*/
1371static int strContainsChar(const u8 *zStr, int nStr, u32 ch){
1372 const u8 *zEnd = &zStr[nStr];
1373 const u8 *z = zStr;
1374 while( z<zEnd ){
1375 u32 tst = Utf8Read(z);
1376 if( tst==ch ) return 1;
1377 }
1378 return 0;
1379}
1380
1381/*
1382** The unhex() function. This function may be invoked with either one or
1383** two arguments. In both cases the first argument is interpreted as text
1384** a text value containing a set of pairs of hexadecimal digits which are
1385** decoded and returned as a blob.
1386**
1387** If there is only a single argument, then it must consist only of an
larrybr55be2162023-06-07 17:03:221388** even number of hexadecimal digits. Otherwise, return NULL.
dana50db432023-01-24 17:19:471389**
1390** Or, if there is a second argument, then any character that appears in
1391** the second argument is also allowed to appear between pairs of hexadecimal
1392** digits in the first argument. If any other character appears in the
larrybrbc917382023-06-07 08:40:311393** first argument, or if one of the allowed characters appears between
dana50db432023-01-24 17:19:471394** two hexadecimal digits that make up a single byte, NULL is returned.
1395**
1396** The following expressions are all true:
1397**
1398** unhex('ABCD') IS x'ABCD'
1399** unhex('AB CD') IS NULL
1400** unhex('AB CD', ' ') IS x'ABCD'
1401** unhex('A BCD', ' ') IS NULL
dane3c11d52023-01-23 14:11:341402*/
1403static void unhexFunc(
1404 sqlite3_context *pCtx,
1405 int argc,
1406 sqlite3_value **argv
1407){
dana50db432023-01-24 17:19:471408 const u8 *zPass = (const u8*)"";
1409 int nPass = 0;
dane3c11d52023-01-23 14:11:341410 const u8 *zHex = sqlite3_value_text(argv[0]);
1411 int nHex = sqlite3_value_bytes(argv[0]);
dana50db432023-01-24 17:19:471412#ifdef SQLITE_DEBUG
drh3c7e90b2023-02-18 15:50:231413 const u8 *zEnd = zHex ? &zHex[nHex] : 0;
dana50db432023-01-24 17:19:471414#endif
1415 u8 *pBlob = 0;
1416 u8 *p = 0;
1417
1418 assert( argc==1 || argc==2 );
1419 if( argc==2 ){
1420 zPass = sqlite3_value_text(argv[1]);
1421 nPass = sqlite3_value_bytes(argv[1]);
1422 }
1423 if( !zHex || !zPass ) return;
1424
1425 p = pBlob = contextMalloc(pCtx, (nHex/2)+1);
1426 if( pBlob ){
1427 u8 c; /* Most significant digit of next byte */
1428 u8 d; /* Least significant digit of next byte */
1429
1430 while( (c = *zHex)!=0x00 ){
1431 while( !sqlite3Isxdigit(c) ){
1432 u32 ch = Utf8Read(zHex);
1433 assert( zHex<=zEnd );
1434 if( !strContainsChar(zPass, nPass, ch) ) goto unhex_null;
1435 c = *zHex;
1436 if( c==0x00 ) goto unhex_done;
dane3c11d52023-01-23 14:11:341437 }
dana50db432023-01-24 17:19:471438 zHex++;
1439 assert( *zEnd==0x00 );
1440 assert( zHex<=zEnd );
1441 d = *(zHex++);
1442 if( !sqlite3Isxdigit(d) ) goto unhex_null;
1443 *(p++) = (sqlite3HexToInt(c)<<4) | sqlite3HexToInt(d);
dane3c11d52023-01-23 14:11:341444 }
1445 }
dana50db432023-01-24 17:19:471446
1447 unhex_done:
1448 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
1449 return;
1450
1451 unhex_null:
1452 sqlite3_free(pBlob);
1453 return;
dane3c11d52023-01-23 14:11:341454}
1455
1456
1457/*
drh8cff3822007-05-02 02:08:281458** The zeroblob(N) function returns a zero-filled blob of size N bytes.
1459*/
1460static void zeroblobFunc(
1461 sqlite3_context *context,
1462 int argc,
1463 sqlite3_value **argv
1464){
drh98640a32007-06-07 19:08:321465 i64 n;
dana4d5ae82015-07-24 16:24:371466 int rc;
drh8cff3822007-05-02 02:08:281467 assert( argc==1 );
danielk1977f3d3c272008-11-19 16:52:441468 UNUSED_PARAMETER(argc);
drh98640a32007-06-07 19:08:321469 n = sqlite3_value_int64(argv[0]);
dana4d5ae82015-07-24 16:24:371470 if( n<0 ) n = 0;
1471 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
1472 if( rc ){
1473 sqlite3_result_error_code(context, rc);
drh98640a32007-06-07 19:08:321474 }
drh8cff3822007-05-02 02:08:281475}
1476
1477/*
drh26b6d902007-03-17 13:27:541478** The replace() function. Three arguments are all strings: call
1479** them A, B, and C. The result is also a string which is derived
drhf7b54962013-05-28 12:11:541480** from A by replacing every occurrence of B with C. The match
drh26b6d902007-03-17 13:27:541481** must be exact. Collating sequences are not used.
1482*/
1483static void replaceFunc(
1484 sqlite3_context *context,
1485 int argc,
1486 sqlite3_value **argv
1487){
1488 const unsigned char *zStr; /* The input string A */
1489 const unsigned char *zPattern; /* The pattern string B */
1490 const unsigned char *zRep; /* The replacement string C */
1491 unsigned char *zOut; /* The output */
1492 int nStr; /* Size of zStr */
1493 int nPattern; /* Size of zPattern */
1494 int nRep; /* Size of zRep */
drh2e6400b2007-05-08 15:46:181495 i64 nOut; /* Maximum size of zOut */
drh26b6d902007-03-17 13:27:541496 int loopLimit; /* Last zStr[] that might match zPattern[] */
1497 int i, j; /* Loop counters */
drhf3139522018-02-09 23:25:141498 unsigned cntExpand; /* Number zOut expansions */
1499 sqlite3 *db = sqlite3_context_db_handle(context);
drh26b6d902007-03-17 13:27:541500
1501 assert( argc==3 );
danielk1977f3d3c272008-11-19 16:52:441502 UNUSED_PARAMETER(argc);
drh26b6d902007-03-17 13:27:541503 zStr = sqlite3_value_text(argv[0]);
drh7a521cf2007-04-25 18:23:521504 if( zStr==0 ) return;
drh1f0feef2007-05-15 13:27:071505 nStr = sqlite3_value_bytes(argv[0]);
1506 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
drh26b6d902007-03-17 13:27:541507 zPattern = sqlite3_value_text(argv[1]);
drha605fe82009-02-01 18:08:401508 if( zPattern==0 ){
drh23336062009-02-03 13:19:121509 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
1510 || sqlite3_context_db_handle(context)->mallocFailed );
drha605fe82009-02-01 18:08:401511 return;
1512 }
1513 if( zPattern[0]==0 ){
1514 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
drh8dca1902024-01-20 13:18:221515 sqlite3_result_text(context, (const char*)zStr, nStr, SQLITE_TRANSIENT);
drha605fe82009-02-01 18:08:401516 return;
1517 }
drh1f0feef2007-05-15 13:27:071518 nPattern = sqlite3_value_bytes(argv[1]);
1519 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
drh26b6d902007-03-17 13:27:541520 zRep = sqlite3_value_text(argv[2]);
drh7a521cf2007-04-25 18:23:521521 if( zRep==0 ) return;
drh1f0feef2007-05-15 13:27:071522 nRep = sqlite3_value_bytes(argv[2]);
1523 assert( zRep==sqlite3_value_text(argv[2]) );
drh2e6400b2007-05-08 15:46:181524 nOut = nStr + 1;
1525 assert( nOut<SQLITE_MAX_LENGTH );
drhef86b942025-02-17 17:33:141526 zOut = contextMalloc(context, nOut);
drh2e6400b2007-05-08 15:46:181527 if( zOut==0 ){
1528 return;
drh26b6d902007-03-17 13:27:541529 }
larrybrbc917382023-06-07 08:40:311530 loopLimit = nStr - nPattern;
drhf3139522018-02-09 23:25:141531 cntExpand = 0;
drh26b6d902007-03-17 13:27:541532 for(i=j=0; i<=loopLimit; i++){
1533 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
1534 zOut[j++] = zStr[i];
1535 }else{
drhf3139522018-02-09 23:25:141536 if( nRep>nPattern ){
1537 nOut += nRep - nPattern;
drhc86d82f2018-02-10 02:31:301538 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
1539 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
drhf3139522018-02-09 23:25:141540 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1541 sqlite3_result_error_toobig(context);
1542 sqlite3_free(zOut);
1543 return;
1544 }
drhf3139522018-02-09 23:25:141545 cntExpand++;
1546 if( (cntExpand&(cntExpand-1))==0 ){
1547 /* Grow the size of the output buffer only on substitutions
1548 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
1549 u8 *zOld;
1550 zOld = zOut;
drhd924e7b2020-05-17 00:26:441551 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
drhf3139522018-02-09 23:25:141552 if( zOut==0 ){
1553 sqlite3_result_error_nomem(context);
1554 sqlite3_free(zOld);
1555 return;
1556 }
1557 }
drh2e6400b2007-05-08 15:46:181558 }
drh26b6d902007-03-17 13:27:541559 memcpy(&zOut[j], zRep, nRep);
1560 j += nRep;
1561 i += nPattern-1;
1562 }
1563 }
drhf3139522018-02-09 23:25:141564 assert( j+nStr-i+1<=nOut );
drh26b6d902007-03-17 13:27:541565 memcpy(&zOut[j], &zStr[i], nStr-i);
1566 j += nStr - i;
1567 assert( j<=nOut );
1568 zOut[j] = 0;
1569 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
1570}
1571
drh309b3382007-03-17 17:52:421572/*
1573** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
1574** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
1575*/
1576static void trimFunc(
1577 sqlite3_context *context,
1578 int argc,
1579 sqlite3_value **argv
1580){
1581 const unsigned char *zIn; /* Input string */
1582 const unsigned char *zCharSet; /* Set of characters to trim */
drh972da422021-06-15 14:34:211583 unsigned int nIn; /* Number of bytes in input */
drh7209c692008-04-27 18:40:111584 int flags; /* 1: trimleft 2: trimright 3: trim */
drhd1e3a612007-04-27 21:59:521585 int i; /* Loop counter */
drh972da422021-06-15 14:34:211586 unsigned int *aLen = 0; /* Length of each character in zCharSet */
drh1bd10f82008-12-10 21:19:561587 unsigned char **azChar = 0; /* Individual characters in zCharSet */
drhd1e3a612007-04-27 21:59:521588 int nChar; /* Number of characters in zCharSet */
1589
drh309b3382007-03-17 17:52:421590 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1591 return;
1592 }
1593 zIn = sqlite3_value_text(argv[0]);
drh7a521cf2007-04-25 18:23:521594 if( zIn==0 ) return;
drh972da422021-06-15 14:34:211595 nIn = (unsigned)sqlite3_value_bytes(argv[0]);
drh1f0feef2007-05-15 13:27:071596 assert( zIn==sqlite3_value_text(argv[0]) );
drh309b3382007-03-17 17:52:421597 if( argc==1 ){
drh972da422021-06-15 14:34:211598 static const unsigned lenOne[] = { 1 };
danielk1977a4de4532008-09-02 15:44:081599 static unsigned char * const azOne[] = { (u8*)" " };
drhd1e3a612007-04-27 21:59:521600 nChar = 1;
drh972da422021-06-15 14:34:211601 aLen = (unsigned*)lenOne;
danielk1977bc67da42007-12-11 04:23:191602 azChar = (unsigned char **)azOne;
drhd1e3a612007-04-27 21:59:521603 zCharSet = 0;
drh7a521cf2007-04-25 18:23:521604 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
drh309b3382007-03-17 17:52:421605 return;
drhd1e3a612007-04-27 21:59:521606 }else{
1607 const unsigned char *z;
1608 for(z=zCharSet, nChar=0; *z; nChar++){
drh4a919112007-05-15 11:55:091609 SQLITE_SKIP_UTF8(z);
drhd1e3a612007-04-27 21:59:521610 }
1611 if( nChar>0 ){
larrybrbc917382023-06-07 08:40:311612 azChar = contextMalloc(context,
drh972da422021-06-15 14:34:211613 ((i64)nChar)*(sizeof(char*)+sizeof(unsigned)));
drhd1e3a612007-04-27 21:59:521614 if( azChar==0 ){
1615 return;
1616 }
drh972da422021-06-15 14:34:211617 aLen = (unsigned*)&azChar[nChar];
drhd1e3a612007-04-27 21:59:521618 for(z=zCharSet, nChar=0; *z; nChar++){
danielk1977bc67da42007-12-11 04:23:191619 azChar[nChar] = (unsigned char *)z;
drh4a919112007-05-15 11:55:091620 SQLITE_SKIP_UTF8(z);
drh972da422021-06-15 14:34:211621 aLen[nChar] = (unsigned)(z - azChar[nChar]);
drhd1e3a612007-04-27 21:59:521622 }
1623 }
drh309b3382007-03-17 17:52:421624 }
drhd1e3a612007-04-27 21:59:521625 if( nChar>0 ){
shane1fc41292008-07-08 22:28:481626 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
drh309b3382007-03-17 17:52:421627 if( flags & 1 ){
drhd1e3a612007-04-27 21:59:521628 while( nIn>0 ){
drh972da422021-06-15 14:34:211629 unsigned int len = 0;
drhd1e3a612007-04-27 21:59:521630 for(i=0; i<nChar; i++){
1631 len = aLen[i];
drh27e62db2009-04-02 10:16:171632 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
drhd1e3a612007-04-27 21:59:521633 }
1634 if( i>=nChar ) break;
1635 zIn += len;
1636 nIn -= len;
drh309b3382007-03-17 17:52:421637 }
1638 }
1639 if( flags & 2 ){
drhd1e3a612007-04-27 21:59:521640 while( nIn>0 ){
drh972da422021-06-15 14:34:211641 unsigned int len = 0;
drhd1e3a612007-04-27 21:59:521642 for(i=0; i<nChar; i++){
1643 len = aLen[i];
1644 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
1645 }
1646 if( i>=nChar ) break;
1647 nIn -= len;
drh309b3382007-03-17 17:52:421648 }
1649 }
drhd1e3a612007-04-27 21:59:521650 if( zCharSet ){
1651 sqlite3_free(azChar);
1652 }
drh309b3382007-03-17 17:52:421653 }
1654 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
1655}
drh26b6d902007-03-17 13:27:541656
drhe1e67ab2023-08-29 15:24:411657/* The core implementation of the CONCAT(...) and CONCAT_WS(SEP,...)
1658** functions.
1659**
1660** Return a string value that is the concatenation of all non-null
1661** entries in argv[]. Use zSep as the separator.
1662*/
1663static void concatFuncCore(
1664 sqlite3_context *context,
1665 int argc,
1666 sqlite3_value **argv,
1667 int nSep,
1668 const char *zSep
1669){
drh23e59b32025-06-11 00:01:421670 i64 j, n = 0;
drhe1e67ab2023-08-29 15:24:411671 int i;
1672 char *z;
1673 for(i=0; i<argc; i++){
1674 n += sqlite3_value_bytes(argv[i]);
1675 }
drhf4fc2ee2025-02-16 10:57:251676 n += (argc-1)*(i64)nSep;
drhe1e67ab2023-08-29 15:24:411677 z = sqlite3_malloc64(n+1);
1678 if( z==0 ){
1679 sqlite3_result_error_nomem(context);
1680 return;
1681 }
1682 j = 0;
1683 for(i=0; i<argc; i++){
drh23e59b32025-06-11 00:01:421684 if( sqlite3_value_type(argv[i])!=SQLITE_NULL ){
1685 int k = sqlite3_value_bytes(argv[i]);
drhe1e67ab2023-08-29 15:24:411686 const char *v = (const char*)sqlite3_value_text(argv[i]);
drh43d71eb2023-10-02 15:56:371687 if( v!=0 ){
drhe1e67ab2023-08-29 15:24:411688 if( j>0 && nSep>0 ){
1689 memcpy(&z[j], zSep, nSep);
1690 j += nSep;
1691 }
1692 memcpy(&z[j], v, k);
1693 j += k;
1694 }
1695 }
1696 }
1697 z[j] = 0;
1698 assert( j<=n );
drh51e3f7a2023-10-02 17:06:281699 sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF8);
drhe1e67ab2023-08-29 15:24:411700}
1701
1702/*
1703** The CONCAT(...) function. Generate a string result that is the
1704** concatentation of all non-null arguments.
1705*/
1706static void concatFunc(
1707 sqlite3_context *context,
1708 int argc,
1709 sqlite3_value **argv
1710){
1711 concatFuncCore(context, argc, argv, 0, "");
1712}
1713
1714/*
1715** The CONCAT_WS(separator, ...) function.
1716**
1717** Generate a string that is the concatenation of 2nd through the Nth
1718** argument. Use the first argument (which must be non-NULL) as the
1719** separator.
1720*/
1721static void concatwsFunc(
1722 sqlite3_context *context,
1723 int argc,
1724 sqlite3_value **argv
1725){
1726 int nSep = sqlite3_value_bytes(argv[0]);
1727 const char *zSep = (const char*)sqlite3_value_text(argv[0]);
1728 if( zSep==0 ) return;
1729 concatFuncCore(context, argc-1, argv+1, nSep, zSep);
1730}
1731
danielk1977a4de4532008-09-02 15:44:081732
drhcc153132016-08-04 12:35:171733#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
1734/*
1735** The "unknown" function is automatically substituted in place of
1736** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
drhe0306192023-05-05 14:16:311737** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
drhcc153132016-08-04 12:35:171738** When the "sqlite3" command-line shell is built using this functionality,
1739** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
1740** involving application-defined functions to be examined in a generic
1741** sqlite3 shell.
1742*/
1743static void unknownFunc(
1744 sqlite3_context *context,
1745 int argc,
1746 sqlite3_value **argv
1747){
1748 /* no-op */
drh3547e492022-12-23 14:49:241749 (void)context;
1750 (void)argc;
1751 (void)argv;
drhcc153132016-08-04 12:35:171752}
1753#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
1754
1755
drh2ba3ccc2009-12-08 02:06:081756/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
1757** is only available if the SQLITE_SOUNDEX compile-time option is used
1758** when SQLite is built.
1759*/
drhd24cc422003-03-27 12:51:241760#ifdef SQLITE_SOUNDEX
1761/*
1762** Compute the soundex encoding of a word.
drh2ba3ccc2009-12-08 02:06:081763**
1764** IMP: R-59782-00072 The soundex(X) function returns a string that is the
larrybrbc917382023-06-07 08:40:311765** soundex encoding of the string X.
drhd24cc422003-03-27 12:51:241766*/
drh137c7282007-01-29 17:58:281767static void soundexFunc(
1768 sqlite3_context *context,
1769 int argc,
1770 sqlite3_value **argv
1771){
drhd24cc422003-03-27 12:51:241772 char zResult[8];
drh4c755c02004-08-08 20:22:171773 const u8 *zIn;
drhd24cc422003-03-27 12:51:241774 int i, j;
1775 static const unsigned char iCode[] = {
1776 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1777 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1778 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1779 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1780 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1781 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1782 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1783 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1784 };
1785 assert( argc==1 );
drh4c755c02004-08-08 20:22:171786 zIn = (u8*)sqlite3_value_text(argv[0]);
drhbdf67e02006-08-19 11:34:011787 if( zIn==0 ) zIn = (u8*)"";
drhdc86e2b2009-01-24 11:30:421788 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
drhd24cc422003-03-27 12:51:241789 if( zIn[i] ){
drhbdf67e02006-08-19 11:34:011790 u8 prevcode = iCode[zIn[i]&0x7f];
danielk197778ca0e72009-01-20 16:53:391791 zResult[0] = sqlite3Toupper(zIn[i]);
drhd24cc422003-03-27 12:51:241792 for(j=1; j<4 && zIn[i]; i++){
1793 int code = iCode[zIn[i]&0x7f];
1794 if( code>0 ){
drhbdf67e02006-08-19 11:34:011795 if( code!=prevcode ){
1796 prevcode = code;
1797 zResult[j++] = code + '0';
1798 }
1799 }else{
1800 prevcode = 0;
drhd24cc422003-03-27 12:51:241801 }
1802 }
1803 while( j<4 ){
1804 zResult[j++] = '0';
1805 }
1806 zResult[j] = 0;
danielk1977d8123362004-06-12 09:25:121807 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
drhd24cc422003-03-27 12:51:241808 }else{
drh2ba3ccc2009-12-08 02:06:081809 /* IMP: R-64894-50321 The string "?000" is returned if the argument
1810 ** is NULL or contains no ASCII alphabetic characters. */
danielk1977d8123362004-06-12 09:25:121811 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
drhd24cc422003-03-27 12:51:241812 }
1813}
drh2ba3ccc2009-12-08 02:06:081814#endif /* SQLITE_SOUNDEX */
drhd24cc422003-03-27 12:51:241815
drhfdb83b22006-06-17 14:12:471816#ifndef SQLITE_OMIT_LOAD_EXTENSION
1817/*
1818** A function that loads a shared-library extension then returns NULL.
1819*/
1820static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk197765fd59f2006-06-24 11:51:331821 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
drh7a521cf2007-04-25 18:23:521822 const char *zProc;
drhfa4a4b92008-03-19 21:45:511823 sqlite3 *db = sqlite3_context_db_handle(context);
drhfdb83b22006-06-17 14:12:471824 char *zErrMsg = 0;
1825
drh191dd062016-04-21 01:30:091826 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
drh1a55ded2016-04-20 00:30:051827 ** flag is set. See the sqlite3_enable_load_extension() API.
1828 */
drhf602a162016-04-21 01:58:211829 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
1830 sqlite3_result_error(context, "not authorized", -1);
1831 return;
1832 }
drh1a55ded2016-04-20 00:30:051833
drhfdb83b22006-06-17 14:12:471834 if( argc==2 ){
danielk197765fd59f2006-06-24 11:51:331835 zProc = (const char *)sqlite3_value_text(argv[1]);
drh7a521cf2007-04-25 18:23:521836 }else{
1837 zProc = 0;
drhfdb83b22006-06-17 14:12:471838 }
drh7a521cf2007-04-25 18:23:521839 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
drhfdb83b22006-06-17 14:12:471840 sqlite3_result_error(context, zErrMsg, -1);
1841 sqlite3_free(zErrMsg);
1842 }
1843}
1844#endif
1845
danielk197701427a62005-01-11 13:02:331846
drh0ac65892002-04-20 14:24:411847/*
drhd3a149e2002-02-24 17:12:531848** An instance of the following structure holds the context of a
drhdd5baa92002-02-27 19:50:591849** sum() or avg() aggregate computation.
1850*/
1851typedef struct SumCtx SumCtx;
1852struct SumCtx {
drh48114d02023-06-30 14:01:091853 double rSum; /* Running sum as as a double */
drh45d75622023-07-06 13:19:101854 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
drh48114d02023-06-30 14:01:091855 i64 iSum; /* Running sum as a signed integer */
drh8c08e862006-02-11 17:34:001856 i64 cnt; /* Number of elements summed */
drh12b198f2023-06-26 19:35:201857 u8 approx; /* True if any non-integer value was input to the sum */
drh48114d02023-06-30 14:01:091858 u8 ovrfl; /* Integer overflow seen */
drhdd5baa92002-02-27 19:50:591859};
1860
1861/*
drh45d75622023-07-06 13:19:101862** Do one step of the Kahan-Babushka-Neumaier summation.
1863**
1864** https://en.wikipedia.org/wiki/Kahan_summation_algorithm
1865**
1866** Variables are marked "volatile" to defeat c89 x86 floating point
1867** optimizations can mess up this algorithm.
1868*/
1869static void kahanBabuskaNeumaierStep(
1870 volatile SumCtx *pSum,
1871 volatile double r
1872){
drh26cd8bc2023-07-06 14:45:531873 volatile double s = pSum->rSum;
1874 volatile double t = s + r;
1875 if( fabs(s) > fabs(r) ){
1876 pSum->rErr += (s - t) + r;
drh45d75622023-07-06 13:19:101877 }else{
drh26cd8bc2023-07-06 14:45:531878 pSum->rErr += (r - t) + s;
drh45d75622023-07-06 13:19:101879 }
1880 pSum->rSum = t;
1881}
1882
1883/*
1884** Add a (possibly large) integer to the running sum.
1885*/
1886static void kahanBabuskaNeumaierStepInt64(volatile SumCtx *pSum, i64 iVal){
drh4c40b7b2023-07-08 17:42:241887 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
drh7d0103b2023-07-06 20:34:061888 i64 iBig, iSm;
1889 iSm = iVal % 16384;
1890 iBig = iVal - iSm;
1891 kahanBabuskaNeumaierStep(pSum, iBig);
1892 kahanBabuskaNeumaierStep(pSum, iSm);
1893 }else{
1894 kahanBabuskaNeumaierStep(pSum, (double)iVal);
drh45d75622023-07-06 13:19:101895 }
1896}
1897
1898/*
1899** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
1900*/
1901static void kahanBabuskaNeumaierInit(
drh7d0103b2023-07-06 20:34:061902 volatile SumCtx *p,
drh45d75622023-07-06 13:19:101903 i64 iVal
1904){
drh4c40b7b2023-07-08 17:42:241905 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
drh7d0103b2023-07-06 20:34:061906 i64 iSm = iVal % 16384;
1907 p->rSum = (double)(iVal - iSm);
1908 p->rErr = (double)iSm;
1909 }else{
1910 p->rSum = (double)iVal;
1911 p->rErr = 0.0;
1912 }
drh45d75622023-07-06 13:19:101913}
1914
1915/*
drha97fdd32006-01-12 22:17:501916** Routines used to compute the sum, average, and total.
1917**
1918** The SUM() function follows the (broken) SQL standard which means
1919** that it returns NULL if it sums over no inputs. TOTAL returns
1920** 0.0 in that case. In addition, TOTAL always returns a float where
1921** SUM might return an integer if it never encounters a floating point
stephan129203b2025-02-27 03:23:331922** value. TOTAL never fails, but SUM might throw an exception if
drhc806d852006-05-11 13:25:391923** it overflows an integer.
drhdd5baa92002-02-27 19:50:591924*/
danielk19770ae8b832004-05-25 12:05:561925static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhdd5baa92002-02-27 19:50:591926 SumCtx *p;
drh3d1d95e2005-09-08 10:37:011927 int type;
drh3f219f42005-09-08 19:45:571928 assert( argc==1 );
danielk1977f3d3c272008-11-19 16:52:441929 UNUSED_PARAMETER(argc);
drh4f26d6c2004-05-26 23:25:301930 p = sqlite3_aggregate_context(context, sizeof(*p));
drh29d72102006-02-09 22:13:411931 type = sqlite3_value_numeric_type(argv[0]);
drh3d1d95e2005-09-08 10:37:011932 if( p && type!=SQLITE_NULL ){
drh739105c2002-05-29 23:22:231933 p->cnt++;
drh48114d02023-06-30 14:01:091934 if( p->approx==0 ){
1935 if( type!=SQLITE_INTEGER ){
drh45d75622023-07-06 13:19:101936 kahanBabuskaNeumaierInit(p, p->iSum);
drh48114d02023-06-30 14:01:091937 p->approx = 1;
drh45d75622023-07-06 13:19:101938 kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
drh48114d02023-06-30 14:01:091939 }else{
1940 i64 x = p->iSum;
1941 if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){
1942 p->iSum = x;
1943 }else{
1944 p->ovrfl = 1;
drh45d75622023-07-06 13:19:101945 kahanBabuskaNeumaierInit(p, p->iSum);
drh48114d02023-06-30 14:01:091946 p->approx = 1;
drh37fd50d2023-07-19 09:52:101947 kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0]));
drh48114d02023-06-30 14:01:091948 }
1949 }
drh29d72102006-02-09 22:13:411950 }else{
drh45d75622023-07-06 13:19:101951 if( type==SQLITE_INTEGER ){
1952 kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0]));
1953 }else{
1954 p->ovrfl = 0;
1955 kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
1956 }
drh3f219f42005-09-08 19:45:571957 }
drh739105c2002-05-29 23:22:231958 }
drhdd5baa92002-02-27 19:50:591959}
dan67a9b8e2018-06-22 20:51:351960#ifndef SQLITE_OMIT_WINDOWFUNC
danc3a20c12018-05-23 20:55:371961static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
1962 SumCtx *p;
1963 int type;
1964 assert( argc==1 );
1965 UNUSED_PARAMETER(argc);
1966 p = sqlite3_aggregate_context(context, sizeof(*p));
1967 type = sqlite3_value_numeric_type(argv[0]);
drhfd4b7282018-07-07 19:47:211968 /* p is always non-NULL because sumStep() will have been called first
1969 ** to initialize it */
1970 if( ALWAYS(p) && type!=SQLITE_NULL ){
drha546ef22018-07-07 20:55:161971 assert( p->cnt>0 );
danc3a20c12018-05-23 20:55:371972 p->cnt--;
drh45d75622023-07-06 13:19:101973 if( !p->approx ){
drh802b0422025-02-04 02:38:231974 if( sqlite3SubInt64(&p->iSum, sqlite3_value_int64(argv[0])) ){
1975 p->ovrfl = 1;
1976 p->approx = 1;
1977 }
drh45d75622023-07-06 13:19:101978 }else if( type==SQLITE_INTEGER ){
1979 i64 iVal = sqlite3_value_int64(argv[0]);
1980 if( iVal!=SMALLEST_INT64 ){
1981 kahanBabuskaNeumaierStepInt64(p, -iVal);
1982 }else{
drh26cd8bc2023-07-06 14:45:531983 kahanBabuskaNeumaierStepInt64(p, LARGEST_INT64);
1984 kahanBabuskaNeumaierStepInt64(p, 1);
drh45d75622023-07-06 13:19:101985 }
1986 }else{
1987 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
danc3a20c12018-05-23 20:55:371988 }
1989 }
1990}
dan67a9b8e2018-06-22 20:51:351991#else
1992# define sumInverse 0
1993#endif /* SQLITE_OMIT_WINDOWFUNC */
danielk19770ae8b832004-05-25 12:05:561994static void sumFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:591995 SumCtx *p;
drhabfcea22005-09-06 20:36:481996 p = sqlite3_aggregate_context(context, 0);
drhc2bd9132005-09-08 20:37:431997 if( p && p->cnt>0 ){
drh12b198f2023-06-26 19:35:201998 if( p->approx ){
drh48114d02023-06-30 14:01:091999 if( p->ovrfl ){
drh12b198f2023-06-26 19:35:202000 sqlite3_result_error(context,"integer overflow",-1);
drh5ed044e2024-03-19 10:16:172001 }else if( !sqlite3IsOverflow(p->rErr) ){
drh45d75622023-07-06 13:19:102002 sqlite3_result_double(context, p->rSum+p->rErr);
drhbd953df2023-08-28 12:20:182003 }else{
2004 sqlite3_result_double(context, p->rSum);
drh12b198f2023-06-26 19:35:202005 }
drh48114d02023-06-30 14:01:092006 }else{
2007 sqlite3_result_int64(context, p->iSum);
drhc2bd9132005-09-08 20:37:432008 }
drh3d1d95e2005-09-08 10:37:012009 }
drhdd5baa92002-02-27 19:50:592010}
danielk19770ae8b832004-05-25 12:05:562011static void avgFinalize(sqlite3_context *context){
drhdd5baa92002-02-27 19:50:592012 SumCtx *p;
drhabfcea22005-09-06 20:36:482013 p = sqlite3_aggregate_context(context, 0);
drh739105c2002-05-29 23:22:232014 if( p && p->cnt>0 ){
drh48114d02023-06-30 14:01:092015 double r;
2016 if( p->approx ){
drh7bb5a6d2023-08-30 16:03:272017 r = p->rSum;
drh5ed044e2024-03-19 10:16:172018 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
drh48114d02023-06-30 14:01:092019 }else{
drh45d75622023-07-06 13:19:102020 r = (double)(p->iSum);
drh48114d02023-06-30 14:01:092021 }
2022 sqlite3_result_double(context, r/(double)p->cnt);
drhdd5baa92002-02-27 19:50:592023 }
2024}
drha97fdd32006-01-12 22:17:502025static void totalFinalize(sqlite3_context *context){
2026 SumCtx *p;
drh48114d02023-06-30 14:01:092027 double r = 0.0;
drha97fdd32006-01-12 22:17:502028 p = sqlite3_aggregate_context(context, 0);
drh48114d02023-06-30 14:01:092029 if( p ){
2030 if( p->approx ){
drh7bb5a6d2023-08-30 16:03:272031 r = p->rSum;
drh5ed044e2024-03-19 10:16:172032 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
drh48114d02023-06-30 14:01:092033 }else{
drh45d75622023-07-06 13:19:102034 r = (double)(p->iSum);
drh48114d02023-06-30 14:01:092035 }
2036 }
2037 sqlite3_result_double(context, r);
drha97fdd32006-01-12 22:17:502038}
drhdd5baa92002-02-27 19:50:592039
2040/*
drh0bce8352002-02-28 00:41:102041** The following structure keeps track of state information for the
2042** count() aggregate function.
2043*/
2044typedef struct CountCtx CountCtx;
2045struct CountCtx {
drhfc6ad392006-02-09 13:38:192046 i64 n;
dan7262ca92018-07-02 12:07:322047#ifdef SQLITE_DEBUG
2048 int bInverse; /* True if xInverse() ever called */
2049#endif
drh0bce8352002-02-28 00:41:102050};
drhdd5baa92002-02-27 19:50:592051
drh0bce8352002-02-28 00:41:102052/*
2053** Routines to implement the count() aggregate function.
2054*/
danielk19770ae8b832004-05-25 12:05:562055static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drh0bce8352002-02-28 00:41:102056 CountCtx *p;
drh4f26d6c2004-05-26 23:25:302057 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:572058 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
drh0bce8352002-02-28 00:41:102059 p->n++;
2060 }
drh2e79c3d2009-04-08 23:04:142061
drhd3264c72009-04-15 13:39:472062#ifndef SQLITE_OMIT_DEPRECATED
drh2e79c3d2009-04-08 23:04:142063 /* The sqlite3_aggregate_count() function is deprecated. But just to make
larrybrbc917382023-06-07 08:40:312064 ** sure it still operates correctly, verify that its count agrees with our
drh2e79c3d2009-04-08 23:04:142065 ** internal count when using count(*) and when the total count can be
2066 ** expressed as a 32-bit integer. */
dan7262ca92018-07-02 12:07:322067 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
drh2e79c3d2009-04-08 23:04:142068 || p->n==sqlite3_aggregate_count(context) );
drhd3264c72009-04-15 13:39:472069#endif
larrybrbc917382023-06-07 08:40:312070}
danielk19770ae8b832004-05-25 12:05:562071static void countFinalize(sqlite3_context *context){
drh0bce8352002-02-28 00:41:102072 CountCtx *p;
drhabfcea22005-09-06 20:36:482073 p = sqlite3_aggregate_context(context, 0);
drhfc6ad392006-02-09 13:38:192074 sqlite3_result_int64(context, p ? p->n : 0);
drh0bce8352002-02-28 00:41:102075}
dan7262ca92018-07-02 12:07:322076#ifndef SQLITE_OMIT_WINDOWFUNC
2077static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
2078 CountCtx *p;
2079 p = sqlite3_aggregate_context(ctx, sizeof(*p));
drhfd4b7282018-07-07 19:47:212080 /* p is always non-NULL since countStep() will have been called first */
2081 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){
dan7262ca92018-07-02 12:07:322082 p->n--;
2083#ifdef SQLITE_DEBUG
2084 p->bInverse = 1;
2085#endif
2086 }
larrybrbc917382023-06-07 08:40:312087}
dan6b4b8822018-07-02 15:03:502088#else
2089# define countInverse 0
2090#endif /* SQLITE_OMIT_WINDOWFUNC */
drh0bce8352002-02-28 00:41:102091
2092/*
drh0bce8352002-02-28 00:41:102093** Routines to implement min() and max() aggregate functions.
2094*/
danielk197762c14b32008-11-19 09:05:262095static void minmaxStep(
larrybrbc917382023-06-07 08:40:312096 sqlite3_context *context,
2097 int NotUsed,
danielk197762c14b32008-11-19 09:05:262098 sqlite3_value **argv
2099){
danielk197788208052004-05-25 01:13:202100 Mem *pArg = (Mem *)argv[0];
drh9eb516c2004-07-18 20:52:322101 Mem *pBest;
danielk197762c14b32008-11-19 09:05:262102 UNUSED_PARAMETER(NotUsed);
drh9eb516c2004-07-18 20:52:322103
drh9eb516c2004-07-18 20:52:322104 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
danielk19773aeab9e2004-06-24 00:20:042105 if( !pBest ) return;
drh268380c2004-02-25 13:47:312106
dan6fb2b542018-06-19 17:13:112107 if( sqlite3_value_type(pArg)==SQLITE_NULL ){
drh94a6d992012-02-02 18:42:092108 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
2109 }else if( pBest->flags ){
drh9eb516c2004-07-18 20:52:322110 int max;
2111 int cmp;
danielk1977dc1bdc42004-06-11 10:51:272112 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
danielk19777e18c252004-05-25 11:47:242113 /* This step function is used for both the min() and max() aggregates,
2114 ** the only difference between the two being that the sense of the
2115 ** comparison is inverted. For the max() aggregate, the
2116 ** sqlite3_user_data() function returns (void *)-1. For min() it
2117 ** returns (void *)db, where db is the sqlite3* database pointer.
2118 ** Therefore the next statement sets variable 'max' to 1 for the max()
2119 ** aggregate, or 0 for min().
2120 */
drh309b3382007-03-17 17:52:422121 max = sqlite3_user_data(context)!=0;
danielk1977dc1bdc42004-06-11 10:51:272122 cmp = sqlite3MemCompare(pBest, pArg, pColl);
danielk197788208052004-05-25 01:13:202123 if( (max && cmp<0) || (!max && cmp>0) ){
drhb21c8cd2007-08-21 19:33:562124 sqlite3VdbeMemCopy(pBest, pArg);
drh7a957892012-02-02 17:35:432125 }else{
2126 sqlite3SkipAccumulatorLoad(context);
danielk197788208052004-05-25 01:13:202127 }
drh268380c2004-02-25 13:47:312128 }else{
drh035e5632014-09-16 14:16:312129 pBest->db = sqlite3_context_db_handle(context);
drhb21c8cd2007-08-21 19:33:562130 sqlite3VdbeMemCopy(pBest, pArg);
drh0bce8352002-02-28 00:41:102131 }
2132}
dan6fb2b542018-06-19 17:13:112133static void minMaxValueFinalize(sqlite3_context *context, int bValue){
danielk197788208052004-05-25 01:13:202134 sqlite3_value *pRes;
drhabfcea22005-09-06 20:36:482135 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
2136 if( pRes ){
drh94a6d992012-02-02 18:42:092137 if( pRes->flags ){
drhabfcea22005-09-06 20:36:482138 sqlite3_result_value(context, pRes);
2139 }
dan6fb2b542018-06-19 17:13:112140 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
drh0bce8352002-02-28 00:41:102141 }
2142}
dan67a9b8e2018-06-22 20:51:352143#ifndef SQLITE_OMIT_WINDOWFUNC
dan6fb2b542018-06-19 17:13:112144static void minMaxValue(sqlite3_context *context){
drhc7bf5712018-07-09 22:49:012145 minMaxValueFinalize(context, 1);
dan6fb2b542018-06-19 17:13:112146}
dan67a9b8e2018-06-22 20:51:352147#else
2148# define minMaxValue 0
2149#endif /* SQLITE_OMIT_WINDOWFUNC */
dan6fb2b542018-06-19 17:13:112150static void minMaxFinalize(sqlite3_context *context){
drhc7bf5712018-07-09 22:49:012151 minMaxValueFinalize(context, 0);
dan6fb2b542018-06-19 17:13:112152}
drhdd5baa92002-02-27 19:50:592153
drhb0689692007-11-01 17:38:302154/*
2155** group_concat(EXPR, ?SEPARATOR?)
drhd5e040b2023-10-20 20:19:302156** string_agg(EXPR, SEPARATOR)
drhf06db3e2021-10-01 00:25:062157**
drh90fa4c72024-08-31 14:31:172158** Content is accumulated in GroupConcatCtx.str with the SEPARATOR
2159** coming before the EXPR value, except for the first entry which
2160** omits the SEPARATOR.
2161**
2162** It is tragic that the SEPARATOR goes before the EXPR string. The
drhf06db3e2021-10-01 00:25:062163** groupConcatInverse() implementation would have been easier if the
2164** SEPARATOR were appended after EXPR. And the order is undocumented,
2165** so we could change it, in theory. But the old behavior has been
2166** around for so long that we dare not, for fear of breaking something.
drhb0689692007-11-01 17:38:302167*/
larrybrdde13e62021-09-29 00:32:132168typedef struct {
2169 StrAccum str; /* The accumulated concatenation */
2170#ifndef SQLITE_OMIT_WINDOWFUNC
2171 int nAccum; /* Number of strings presently concatenated */
drhf06db3e2021-10-01 00:25:062172 int nFirstSepLength; /* Used to detect separator length change */
larrybrdde13e62021-09-29 00:32:132173 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
drhf06db3e2021-10-01 00:25:062174 ** stored as actually incorporated into presently accumulated result.
2175 ** (Hence, its slots in use number nAccum-1 between method calls.)
2176 ** If pnSepLengths==0, nFirstSepLength is the length used throughout.
2177 */
larrybrdde13e62021-09-29 00:32:132178 int *pnSepLengths;
2179#endif
2180} GroupConcatCtx;
2181
drhb0689692007-11-01 17:38:302182static void groupConcatStep(
2183 sqlite3_context *context,
2184 int argc,
2185 sqlite3_value **argv
2186){
2187 const char *zVal;
larrybrdde13e62021-09-29 00:32:132188 GroupConcatCtx *pGCC;
drhb0689692007-11-01 17:38:302189 const char *zSep;
drh07d31172009-02-02 21:57:052190 int nVal, nSep;
2191 assert( argc==1 || argc==2 );
2192 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
larrybrdde13e62021-09-29 00:32:132193 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
2194 if( pGCC ){
drhbb4957f2008-03-20 14:03:292195 sqlite3 *db = sqlite3_context_db_handle(context);
larrybrdde13e62021-09-29 00:32:132196 int firstTerm = pGCC->str.mxAlloc==0;
2197 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
drhf06db3e2021-10-01 00:25:062198 if( argc==1 ){
2199 if( !firstTerm ){
2200 sqlite3_str_appendchar(&pGCC->str, 1, ',');
drhb0689692007-11-01 17:38:302201 }
larrybrdde13e62021-09-29 00:32:132202#ifndef SQLITE_OMIT_WINDOWFUNC
drhf06db3e2021-10-01 00:25:062203 else{
2204 pGCC->nFirstSepLength = 1;
2205 }
2206#endif
2207 }else if( !firstTerm ){
2208 zSep = (char*)sqlite3_value_text(argv[1]);
2209 nSep = sqlite3_value_bytes(argv[1]);
2210 if( zSep ){
2211 sqlite3_str_append(&pGCC->str, zSep, nSep);
2212 }
2213#ifndef SQLITE_OMIT_WINDOWFUNC
2214 else{
2215 nSep = 0;
2216 }
larrybrdde13e62021-09-29 00:32:132217 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
drhf06db3e2021-10-01 00:25:062218 int *pnsl = pGCC->pnSepLengths;
2219 if( pnsl == 0 ){
2220 /* First separator length variation seen, start tracking them. */
2221 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
2222 if( pnsl!=0 ){
2223 int i = 0, nA = pGCC->nAccum-1;
2224 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
2225 }
2226 }else{
2227 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
2228 }
2229 if( pnsl!=0 ){
drh3dd01112021-10-01 02:45:482230 if( ALWAYS(pGCC->nAccum>0) ){
drhf06db3e2021-10-01 00:25:062231 pnsl[pGCC->nAccum-1] = nSep;
2232 }
2233 pGCC->pnSepLengths = pnsl;
2234 }else{
2235 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
2236 }
larrybrdde13e62021-09-29 00:32:132237 }
2238#endif
drhb0689692007-11-01 17:38:302239 }
larrybrdde13e62021-09-29 00:32:132240#ifndef SQLITE_OMIT_WINDOWFUNC
2241 else{
drh3dd01112021-10-01 02:45:482242 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
larrybrdde13e62021-09-29 00:32:132243 }
2244 pGCC->nAccum += 1;
2245#endif
drh07d31172009-02-02 21:57:052246 zVal = (char*)sqlite3_value_text(argv[0]);
2247 nVal = sqlite3_value_bytes(argv[0]);
larrybrdde13e62021-09-29 00:32:132248 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
drhb0689692007-11-01 17:38:302249 }
2250}
larrybrdde13e62021-09-29 00:32:132251
dan67a9b8e2018-06-22 20:51:352252#ifndef SQLITE_OMIT_WINDOWFUNC
dan03854d22018-06-08 11:45:282253static void groupConcatInverse(
2254 sqlite3_context *context,
2255 int argc,
2256 sqlite3_value **argv
2257){
larrybrdde13e62021-09-29 00:32:132258 GroupConcatCtx *pGCC;
drhc7bf5712018-07-09 22:49:012259 assert( argc==1 || argc==2 );
drh260ff082021-10-01 22:48:522260 (void)argc; /* Suppress unused parameter warning */
dan03854d22018-06-08 11:45:282261 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
larrybrdde13e62021-09-29 00:32:132262 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
2263 /* pGCC is always non-NULL since groupConcatStep() will have always
larrybrbc917382023-06-07 08:40:312264 ** run first to initialize it */
larrybrdde13e62021-09-29 00:32:132265 if( ALWAYS(pGCC) ){
drh90fa4c72024-08-31 14:31:172266 int nVS; /* Number of characters to remove */
drh4fc80672021-10-12 22:55:042267 /* Must call sqlite3_value_text() to convert the argument into text prior
2268 ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */
2269 (void)sqlite3_value_text(argv[0]);
2270 nVS = sqlite3_value_bytes(argv[0]);
larrybrdde13e62021-09-29 00:32:132271 pGCC->nAccum -= 1;
2272 if( pGCC->pnSepLengths!=0 ){
2273 assert(pGCC->nAccum >= 0);
2274 if( pGCC->nAccum>0 ){
drhf06db3e2021-10-01 00:25:062275 nVS += *pGCC->pnSepLengths;
2276 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
2277 (pGCC->nAccum-1)*sizeof(int));
larrybrdde13e62021-09-29 00:32:132278 }
dan683b0ff2018-07-05 18:19:292279 }else{
larrybrdde13e62021-09-29 00:32:132280 /* If removing single accumulated string, harmlessly over-do. */
2281 nVS += pGCC->nFirstSepLength;
dan03854d22018-06-08 11:45:282282 }
larrybrdde13e62021-09-29 00:32:132283 if( nVS>=(int)pGCC->str.nChar ){
2284 pGCC->str.nChar = 0;
dan03854d22018-06-08 11:45:282285 }else{
larrybrdde13e62021-09-29 00:32:132286 pGCC->str.nChar -= nVS;
2287 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
dan03854d22018-06-08 11:45:282288 }
larrybrdde13e62021-09-29 00:32:132289 if( pGCC->str.nChar==0 ){
2290 pGCC->str.mxAlloc = 0;
2291 sqlite3_free(pGCC->pnSepLengths);
2292 pGCC->pnSepLengths = 0;
2293 }
dan03854d22018-06-08 11:45:282294 }
2295}
dan67a9b8e2018-06-22 20:51:352296#else
2297# define groupConcatInverse 0
2298#endif /* SQLITE_OMIT_WINDOWFUNC */
drhb0689692007-11-01 17:38:302299static void groupConcatFinalize(sqlite3_context *context){
larrybrdde13e62021-09-29 00:32:132300 GroupConcatCtx *pGCC
2301 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
2302 if( pGCC ){
drh5bf47152021-10-03 00:12:432303 sqlite3ResultStrAccum(context, &pGCC->str);
larrybrdde13e62021-09-29 00:32:132304#ifndef SQLITE_OMIT_WINDOWFUNC
2305 sqlite3_free(pGCC->pnSepLengths);
2306#endif
drhb0689692007-11-01 17:38:302307 }
2308}
dan67a9b8e2018-06-22 20:51:352309#ifndef SQLITE_OMIT_WINDOWFUNC
dane2f781b2018-05-17 19:24:082310static void groupConcatValue(sqlite3_context *context){
larrybrdde13e62021-09-29 00:32:132311 GroupConcatCtx *pGCC
2312 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
2313 if( pGCC ){
2314 StrAccum *pAccum = &pGCC->str;
dane2f781b2018-05-17 19:24:082315 if( pAccum->accError==SQLITE_TOOBIG ){
2316 sqlite3_result_error_toobig(context);
2317 }else if( pAccum->accError==SQLITE_NOMEM ){
2318 sqlite3_result_error_nomem(context);
drhe6d3c572024-05-23 23:26:042319 }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){
2320 sqlite3_result_text(context, "", 1, SQLITE_STATIC);
larrybrbc917382023-06-07 08:40:312321 }else{
dane2f781b2018-05-17 19:24:082322 const char *zText = sqlite3_str_value(pAccum);
drhf06db3e2021-10-01 00:25:062323 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
dane2f781b2018-05-17 19:24:082324 }
2325 }
2326}
dan67a9b8e2018-06-22 20:51:352327#else
2328# define groupConcatValue 0
2329#endif /* SQLITE_OMIT_WINDOWFUNC */
drh4e5ffc52004-08-31 00:52:372330
drhd3a149e2002-02-24 17:12:532331/*
drha4741842010-04-25 20:58:372332** This routine does per-connection function registration. Most
2333** of the built-in functions above are part of the global function set.
2334** This routine only deals with those that are not global.
drhdc04c582002-02-24 01:55:152335*/
drh80738d92016-02-15 00:34:162336void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
drha4741842010-04-25 20:58:372337 int rc = sqlite3_overload_function(db, "MATCH", 2);
2338 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
2339 if( rc==SQLITE_NOMEM ){
drh4a642b62016-02-05 01:55:272340 sqlite3OomFault(db);
danielk1977832a58a2007-06-22 15:21:152341 }
drh55ef4d92005-08-14 01:20:372342}
2343
2344/*
drh08652b52019-05-08 17:27:182345** Re-register the built-in LIKE functions. The caseSensitive
drh55ef4d92005-08-14 01:20:372346** parameter determines whether or not the LIKE operator is case
drh08652b52019-05-08 17:27:182347** sensitive.
drh55ef4d92005-08-14 01:20:372348*/
2349void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
drh00eee072023-08-15 11:58:222350 FuncDef *pDef;
drh55ef4d92005-08-14 01:20:372351 struct compareInfo *pInfo;
drhea5c0402019-05-08 19:32:332352 int flags;
drh00eee072023-08-15 11:58:222353 int nArg;
drh55ef4d92005-08-14 01:20:372354 if( caseSensitive ){
2355 pInfo = (struct compareInfo*)&likeInfoAlt;
drhea5c0402019-05-08 19:32:332356 flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE;
drh55ef4d92005-08-14 01:20:372357 }else{
2358 pInfo = (struct compareInfo*)&likeInfoNorm;
drhea5c0402019-05-08 19:32:332359 flags = SQLITE_FUNC_LIKE;
drh55ef4d92005-08-14 01:20:372360 }
drh00eee072023-08-15 11:58:222361 for(nArg=2; nArg<=3; nArg++){
2362 sqlite3CreateFunc(db, "like", nArg, SQLITE_UTF8, pInfo, likeFunc,
2363 0, 0, 0, 0, 0);
2364 pDef = sqlite3FindFunction(db, "like", nArg, SQLITE_UTF8, 0);
2365 pDef->funcFlags |= flags;
2366 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE;
2367 }
drh55ef4d92005-08-14 01:20:372368}
2369
2370/*
2371** pExpr points to an expression which implements a function. If
2372** it is appropriate to apply the LIKE optimization to that function
drh1d42ea72017-07-27 20:24:292373** then set aWc[0] through aWc[2] to the wildcard characters and the
larrybrbc917382023-06-07 08:40:312374** escape character and then return TRUE. If the function is not a
drh1d42ea72017-07-27 20:24:292375** LIKE-style function then return FALSE.
2376**
2377** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
2378** operator if c is a string literal that is exactly one byte in length.
2379** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
2380** no ESCAPE clause.
drh16897072015-03-07 00:57:372381**
2382** *pIsNocase is set to true if uppercase and lowercase are equivalent for
2383** the function (default for LIKE). If the function makes the distinction
2384** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
2385** false.
drh55ef4d92005-08-14 01:20:372386*/
drhd64fe2f2005-08-28 17:00:232387int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
drh55ef4d92005-08-14 01:20:372388 FuncDef *pDef;
drh1d42ea72017-07-27 20:24:292389 int nExpr;
drh17988aa2021-01-22 20:28:302390 assert( pExpr!=0 );
2391 assert( pExpr->op==TK_FUNCTION );
drha4eeccd2021-10-07 17:43:302392 assert( ExprUseXList(pExpr) );
drh17988aa2021-01-22 20:28:302393 if( !pExpr->x.pList ){
drh55ef4d92005-08-14 01:20:372394 return 0;
2395 }
drh1d42ea72017-07-27 20:24:292396 nExpr = pExpr->x.pList->nExpr;
drhf9751072021-10-07 13:40:292397 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh1d42ea72017-07-27 20:24:292398 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
drh78b52202020-01-22 23:08:192399#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
2400 if( pDef==0 ) return 0;
2401#endif
drhd36e1042013-09-06 13:10:122402 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
drh55ef4d92005-08-14 01:20:372403 return 0;
2404 }
2405
2406 /* The memcpy() statement assumes that the wildcard characters are
2407 ** the first three statements in the compareInfo structure. The
2408 ** asserts() that follow verify that assumption
2409 */
2410 memcpy(aWc, pDef->pUserData, 3);
2411 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
2412 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
2413 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
drh589c7872020-03-19 18:13:282414
2415 if( nExpr<3 ){
2416 aWc[3] = 0;
2417 }else{
2418 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
2419 char *zEscape;
2420 if( pEscape->op!=TK_STRING ) return 0;
drhf9751072021-10-07 13:40:292421 assert( !ExprHasProperty(pEscape, EP_IntValue) );
drh589c7872020-03-19 18:13:282422 zEscape = pEscape->u.zToken;
2423 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
2424 if( zEscape[0]==aWc[0] ) return 0;
2425 if( zEscape[0]==aWc[1] ) return 0;
2426 aWc[3] = zEscape[0];
2427 }
2428
drhd36e1042013-09-06 13:10:122429 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
drh55ef4d92005-08-14 01:20:372430 return 1;
drhdc04c582002-02-24 01:55:152431}
danielk19778c0a7912008-08-20 14:49:232432
drh63f8f982020-12-07 21:13:062433/* Mathematical Constants */
2434#ifndef M_PI
2435# define M_PI 3.141592653589793238462643383279502884
2436#endif
2437#ifndef M_LN10
2438# define M_LN10 2.302585092994045684017991454684364208
2439#endif
2440#ifndef M_LN2
2441# define M_LN2 0.693147180559945309417232121458176568
2442#endif
2443
2444
drhf6e904b2020-12-07 17:15:322445/* Extra math functions that require linking with -lm
2446*/
2447#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
2448/*
2449** Implementation SQL functions:
2450**
2451** ceil(X)
2452** ceiling(X)
2453** floor(X)
2454**
2455** The sqlite3_user_data() pointer is a pointer to the libm implementation
2456** of the underlying C function.
2457*/
2458static void ceilingFunc(
2459 sqlite3_context *context,
2460 int argc,
2461 sqlite3_value **argv
2462){
2463 assert( argc==1 );
drh63f8f982020-12-07 21:13:062464 switch( sqlite3_value_numeric_type(argv[0]) ){
2465 case SQLITE_INTEGER: {
drhf6e904b2020-12-07 17:15:322466 sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
2467 break;
drh63f8f982020-12-07 21:13:062468 }
2469 case SQLITE_FLOAT: {
drhf6e904b2020-12-07 17:15:322470 double (*x)(double) = (double(*)(double))sqlite3_user_data(context);
2471 sqlite3_result_double(context, x(sqlite3_value_double(argv[0])));
2472 break;
2473 }
drh63f8f982020-12-07 21:13:062474 default: {
2475 break;
2476 }
drhf6e904b2020-12-07 17:15:322477 }
2478}
2479
2480/*
drh4fd4a7a2021-01-01 01:44:062481** On some systems, ceil() and floor() are intrinsic function. You are
drhc2dbf352021-01-07 16:10:142482** unable to take a pointer to these functions. Hence, we here wrap them
drh4fd4a7a2021-01-01 01:44:062483** in our own actual functions.
2484*/
2485static double xCeil(double x){ return ceil(x); }
2486static double xFloor(double x){ return floor(x); }
2487
2488/*
drh3c2688d2023-02-23 01:52:542489** Some systems do not have log2() and log10() in their standard math
2490** libraries.
2491*/
2492#if defined(HAVE_LOG10) && HAVE_LOG10==0
2493# define log10(X) (0.4342944819032517867*log(X))
2494#endif
2495#if defined(HAVE_LOG2) && HAVE_LOG2==0
2496# define log2(X) (1.442695040888963456*log(X))
2497#endif
2498
2499
2500/*
drhf6e904b2020-12-07 17:15:322501** Implementation of SQL functions:
2502**
2503** ln(X) - natural logarithm
drh63f8f982020-12-07 21:13:062504** log(X) - log X base 10
2505** log10(X) - log X base 10
2506** log(B,X) - log X base B
drhf6e904b2020-12-07 17:15:322507*/
2508static void logFunc(
2509 sqlite3_context *context,
2510 int argc,
2511 sqlite3_value **argv
2512){
drh63f8f982020-12-07 21:13:062513 double x, b, ans;
drhf6e904b2020-12-07 17:15:322514 assert( argc==1 || argc==2 );
drh63f8f982020-12-07 21:13:062515 switch( sqlite3_value_numeric_type(argv[0]) ){
2516 case SQLITE_INTEGER:
2517 case SQLITE_FLOAT:
2518 x = sqlite3_value_double(argv[0]);
drh02d6f9b2021-01-29 16:20:162519 if( x<=0.0 ) return;
drh63f8f982020-12-07 21:13:062520 break;
2521 default:
2522 return;
drhf6e904b2020-12-07 17:15:322523 }
drhf6e904b2020-12-07 17:15:322524 if( argc==2 ){
drh63f8f982020-12-07 21:13:062525 switch( sqlite3_value_numeric_type(argv[0]) ){
2526 case SQLITE_INTEGER:
2527 case SQLITE_FLOAT:
drh02d6f9b2021-01-29 16:20:162528 b = log(x);
2529 if( b<=0.0 ) return;
drh63f8f982020-12-07 21:13:062530 x = sqlite3_value_double(argv[1]);
drh02d6f9b2021-01-29 16:20:162531 if( x<=0.0 ) return;
drh63f8f982020-12-07 21:13:062532 break;
2533 default:
2534 return;
drhf6e904b2020-12-07 17:15:322535 }
drh02d6f9b2021-01-29 16:20:162536 ans = log(x)/b;
drh63f8f982020-12-07 21:13:062537 }else{
drh63f8f982020-12-07 21:13:062538 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
2539 case 1:
drh3c1572d2022-11-17 14:40:332540 ans = log10(x);
drh63f8f982020-12-07 21:13:062541 break;
2542 case 2:
drh3c1572d2022-11-17 14:40:332543 ans = log2(x);
drh63f8f982020-12-07 21:13:062544 break;
2545 default:
drh3c1572d2022-11-17 14:40:332546 ans = log(x);
drh63f8f982020-12-07 21:13:062547 break;
2548 }
drhf6e904b2020-12-07 17:15:322549 }
2550 sqlite3_result_double(context, ans);
2551}
drh63f8f982020-12-07 21:13:062552
2553/*
2554** Functions to converts degrees to radians and radians to degrees.
2555*/
2556static double degToRad(double x){ return x*(M_PI/180.0); }
2557static double radToDeg(double x){ return x*(180.0/M_PI); }
2558
2559/*
2560** Implementation of 1-argument SQL math functions:
2561**
2562** exp(X) - Compute e to the X-th power
2563*/
2564static void math1Func(
2565 sqlite3_context *context,
2566 int argc,
2567 sqlite3_value **argv
2568){
drh63f8f982020-12-07 21:13:062569 int type0;
2570 double v0, ans;
2571 double (*x)(double);
mistachkind97a4c02020-12-09 23:35:512572 assert( argc==1 );
drh63f8f982020-12-07 21:13:062573 type0 = sqlite3_value_numeric_type(argv[0]);
2574 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
2575 v0 = sqlite3_value_double(argv[0]);
2576 x = (double(*)(double))sqlite3_user_data(context);
2577 ans = x(v0);
2578 sqlite3_result_double(context, ans);
2579}
2580
2581/*
2582** Implementation of 2-argument SQL math functions:
2583**
2584** power(X,Y) - Compute X to the Y-th power
2585*/
2586static void math2Func(
2587 sqlite3_context *context,
2588 int argc,
2589 sqlite3_value **argv
2590){
drh63f8f982020-12-07 21:13:062591 int type0, type1;
2592 double v0, v1, ans;
2593 double (*x)(double,double);
mistachkind97a4c02020-12-09 23:35:512594 assert( argc==2 );
drh63f8f982020-12-07 21:13:062595 type0 = sqlite3_value_numeric_type(argv[0]);
2596 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
2597 type1 = sqlite3_value_numeric_type(argv[1]);
2598 if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return;
2599 v0 = sqlite3_value_double(argv[0]);
2600 v1 = sqlite3_value_double(argv[1]);
2601 x = (double(*)(double,double))sqlite3_user_data(context);
2602 ans = x(v0, v1);
2603 sqlite3_result_double(context, ans);
2604}
2605
2606/*
drh81e5a9a2021-04-16 11:05:192607** Implementation of 0-argument pi() function.
drh63f8f982020-12-07 21:13:062608*/
2609static void piFunc(
2610 sqlite3_context *context,
2611 int argc,
2612 sqlite3_value **argv
2613){
2614 assert( argc==0 );
drh3547e492022-12-23 14:49:242615 (void)argv;
drh63f8f982020-12-07 21:13:062616 sqlite3_result_double(context, M_PI);
2617}
2618
drhf6e904b2020-12-07 17:15:322619#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
2620
drh70a8ca32008-08-21 18:49:272621/*
drh63f8f982020-12-07 21:13:062622** Implementation of sign(X) function.
2623*/
2624static void signFunc(
2625 sqlite3_context *context,
2626 int argc,
2627 sqlite3_value **argv
2628){
drh63f8f982020-12-07 21:13:062629 int type0;
2630 double x;
drhe5baf5c2020-12-16 14:20:452631 UNUSED_PARAMETER(argc);
mistachkind97a4c02020-12-09 23:35:512632 assert( argc==1 );
drh63f8f982020-12-07 21:13:062633 type0 = sqlite3_value_numeric_type(argv[0]);
2634 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
2635 x = sqlite3_value_double(argv[0]);
2636 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
2637}
2638
drh453be552023-07-01 18:33:262639#ifdef SQLITE_DEBUG
2640/*
2641** Implementation of fpdecode(x,y,z) function.
2642**
2643** x is a real number that is to be decoded. y is the precision.
drh74672ac2024-10-06 15:01:312644** z is the maximum real precision. Return a string that shows the
2645** results of the sqlite3FpDecode() function.
2646**
2647** Used for testing and debugging only, specifically testing and debugging
2648** of the sqlite3FpDecode() function. This SQL function does not appear
2649** in production builds. This function is not an API and is subject to
2650** modification or removal in future versions of SQLite.
drh453be552023-07-01 18:33:262651*/
2652static void fpdecodeFunc(
2653 sqlite3_context *context,
2654 int argc,
2655 sqlite3_value **argv
2656){
2657 FpDecode s;
2658 double x;
2659 int y, z;
2660 char zBuf[100];
2661 UNUSED_PARAMETER(argc);
2662 assert( argc==3 );
2663 x = sqlite3_value_double(argv[0]);
2664 y = sqlite3_value_int(argv[1]);
2665 z = sqlite3_value_int(argv[2]);
drh34e4c6f2024-06-10 12:43:032666 if( z<=0 ) z = 1;
drh453be552023-07-01 18:33:262667 sqlite3FpDecode(&s, x, y, z);
2668 if( s.isSpecial==2 ){
2669 sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN");
2670 }else{
2671 sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP);
2672 }
2673 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
2674}
2675#endif /* SQLITE_DEBUG */
2676
drh74672ac2024-10-06 15:01:312677#ifdef SQLITE_DEBUG
2678/*
2679** Implementation of parseuri(uri,flags) function.
2680**
2681** Required Arguments:
2682** "uri" The URI to parse.
2683** "flags" Bitmask of flags, as if to sqlite3_open_v2().
2684**
2685** Additional arguments beyond the first two make calls to
2686** sqlite3_uri_key() for integers and sqlite3_uri_parameter for
2687** anything else.
2688**
2689** The result is a string showing the results of calling sqlite3ParseUri().
2690**
2691** Used for testing and debugging only, specifically testing and debugging
2692** of the sqlite3ParseUri() function. This SQL function does not appear
2693** in production builds. This function is not an API and is subject to
2694** modification or removal in future versions of SQLite.
2695*/
2696static void parseuriFunc(
2697 sqlite3_context *ctx,
2698 int argc,
2699 sqlite3_value **argv
2700){
2701 sqlite3_str *pResult;
2702 const char *zVfs;
2703 const char *zUri;
2704 unsigned int flgs;
2705 int rc;
2706 sqlite3_vfs *pVfs = 0;
2707 char *zFile = 0;
2708 char *zErr = 0;
2709
2710 if( argc<2 ) return;
2711 pVfs = sqlite3_vfs_find(0);
2712 assert( pVfs );
2713 zVfs = pVfs->zName;
2714 zUri = (const char*)sqlite3_value_text(argv[0]);
2715 if( zUri==0 ) return;
2716 flgs = (unsigned int)sqlite3_value_int(argv[1]);
2717 rc = sqlite3ParseUri(zVfs, zUri, &flgs, &pVfs, &zFile, &zErr);
2718 pResult = sqlite3_str_new(0);
2719 if( pResult ){
2720 int i;
2721 sqlite3_str_appendf(pResult, "rc=%d", rc);
2722 sqlite3_str_appendf(pResult, ", flags=0x%x", flgs);
2723 sqlite3_str_appendf(pResult, ", vfs=%Q", pVfs ? pVfs->zName: 0);
2724 sqlite3_str_appendf(pResult, ", err=%Q", zErr);
2725 sqlite3_str_appendf(pResult, ", file=%Q", zFile);
2726 if( zFile ){
2727 const char *z = zFile;
2728 z += sqlite3Strlen30(z)+1;
2729 while( z[0] ){
2730 sqlite3_str_appendf(pResult, ", %Q", z);
2731 z += sqlite3Strlen30(z)+1;
2732 }
2733 for(i=2; i<argc; i++){
2734 const char *zArg;
2735 if( sqlite3_value_type(argv[i])==SQLITE_INTEGER ){
2736 int k = sqlite3_value_int(argv[i]);
2737 sqlite3_str_appendf(pResult, ", '%d:%q'",k,sqlite3_uri_key(zFile, k));
2738 }else if( (zArg = (const char*)sqlite3_value_text(argv[i]))!=0 ){
2739 sqlite3_str_appendf(pResult, ", '%q:%q'",
2740 zArg, sqlite3_uri_parameter(zFile,zArg));
2741 }else{
2742 sqlite3_str_appendf(pResult, ", NULL");
2743 }
2744 }
2745 }
2746 sqlite3_result_text(ctx, sqlite3_str_finish(pResult), -1, sqlite3_free);
2747 }
2748 sqlite3_free_filename(zFile);
2749 sqlite3_free(zErr);
2750}
2751#endif /* SQLITE_DEBUG */
2752
drh63f8f982020-12-07 21:13:062753/*
peter.d.reid60ec9142014-09-06 16:39:462754** All of the FuncDef structures in the aBuiltinFunc[] array above
drh777c5382008-08-21 20:21:342755** to the global function hash table. This occurs at start-time (as
2756** a consequence of calling sqlite3_initialize()).
2757**
2758** After this routine runs
drh70a8ca32008-08-21 18:49:272759*/
drh80738d92016-02-15 00:34:162760void sqlite3RegisterBuiltinFunctions(void){
danielk197793ce7412008-09-01 19:14:022761 /*
2762 ** The following array holds FuncDef structures for all of the functions
2763 ** defined in this file.
2764 **
2765 ** The array cannot be constant since changes are made to the
2766 ** FuncDef.pHash elements at start-time. The elements of this array
2767 ** are read-only after initialization is complete.
drh80738d92016-02-15 00:34:162768 **
2769 ** For peak efficiency, put the most frequently used function last.
danielk197793ce7412008-09-01 19:14:022770 */
drh80738d92016-02-15 00:34:162771 static FuncDef aBuiltinFunc[] = {
drh171c50e2020-01-01 15:43:302772/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
drh3780f9a2021-09-17 13:07:152773#if !defined(SQLITE_UNTESTABLE)
drh171c50e2020-01-01 15:43:302774 TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0),
2775 TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0),
2776 TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0),
drh3780f9a2021-09-17 13:07:152777 TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0),
2778#endif /* !defined(SQLITE_UNTESTABLE) */
drh171c50e2020-01-01 15:43:302779/***** Regular functions *****/
drh80738d92016-02-15 00:34:162780#ifdef SQLITE_SOUNDEX
2781 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
2782#endif
2783#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh64de2a52019-12-31 18:39:232784 SFUNCTION(load_extension, 1, 0, 0, loadExt ),
2785 SFUNCTION(load_extension, 2, 0, 0, loadExt ),
drh80738d92016-02-15 00:34:162786#endif
drh80738d92016-02-15 00:34:162787#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2788 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
2789 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
2790#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
drh25c42962020-01-01 13:55:082791 INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
2792 INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
2793 INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
drh092457b2017-12-29 15:04:492794#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
drh645682a2022-06-01 11:05:592795 INLINE_FUNC(sqlite_offset, 1, INLINEFUNC_sqlite_offset, 0 ),
drh092457b2017-12-29 15:04:492796#endif
danielk197793ce7412008-09-01 19:14:022797 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
2798 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
2799 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
2800 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
2801 FUNCTION(trim, 1, 3, 0, trimFunc ),
2802 FUNCTION(trim, 2, 3, 0, trimFunc ),
drh2e899cc2025-01-21 15:12:002803 FUNCTION(min, -3, 0, 1, minmaxFunc ),
dan6fb2b542018-06-19 17:13:112804 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
drhbb301232021-07-15 19:29:432805 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ),
drh2e899cc2025-01-21 15:12:002806 FUNCTION(max, -3, 1, 1, minmaxFunc ),
dan6fb2b542018-06-19 17:13:112807 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
drhbb301232021-07-15 19:29:432808 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ),
drha748fdc2012-03-28 01:34:472809 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
drhf7f78a62024-10-07 18:06:172810 FUNCTION2(subtype, 1, 0, 0, subtypeFunc,
2811 SQLITE_FUNC_TYPEOF|SQLITE_SUBTYPE),
drha748fdc2012-03-28 01:34:472812 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
drh077efc22023-06-22 21:19:372813 FUNCTION2(octet_length, 1, 0, 0, bytelengthFunc,SQLITE_FUNC_BYTELEN),
drhd55e0722012-10-25 03:07:292814 FUNCTION(instr, 2, 0, 0, instrFunc ),
drha5c14162013-12-17 15:03:062815 FUNCTION(printf, -1, 0, 0, printfFunc ),
drh6bcd5852022-01-08 21:00:382816 FUNCTION(format, -1, 0, 0, printfFunc ),
drhd495d8c2013-02-22 19:34:252817 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
2818 FUNCTION(char, -1, 0, 0, charFunc ),
danielk197793ce7412008-09-01 19:14:022819 FUNCTION(abs, 1, 0, 0, absFunc ),
drh453be552023-07-01 18:33:262820#ifdef SQLITE_DEBUG
2821 FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ),
drh74672ac2024-10-06 15:01:312822 FUNCTION(parseuri, -1, 0, 0, parseuriFunc ),
drh453be552023-07-01 18:33:262823#endif
shanefbd60f82009-02-04 03:59:252824#ifndef SQLITE_OMIT_FLOATING_POINT
danielk197793ce7412008-09-01 19:14:022825 FUNCTION(round, 1, 0, 0, roundFunc ),
2826 FUNCTION(round, 2, 0, 0, roundFunc ),
shanefbd60f82009-02-04 03:59:252827#endif
danielk197793ce7412008-09-01 19:14:022828 FUNCTION(upper, 1, 0, 0, upperFunc ),
2829 FUNCTION(lower, 1, 0, 0, lowerFunc ),
danielk197793ce7412008-09-01 19:14:022830 FUNCTION(hex, 1, 0, 0, hexFunc ),
dane3c11d52023-01-23 14:11:342831 FUNCTION(unhex, 1, 0, 0, unhexFunc ),
dana50db432023-01-24 17:19:472832 FUNCTION(unhex, 2, 0, 0, unhexFunc ),
drh2e899cc2025-01-21 15:12:002833 FUNCTION(concat, -3, 0, 0, concatFunc ),
2834 FUNCTION(concat_ws, -4, 0, 0, concatwsFunc ),
drhffe421c2020-05-13 17:26:382835 INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ),
drhb1fba282013-11-21 14:33:482836 VFUNCTION(random, 0, 0, 0, randomFunc ),
2837 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
danielk197793ce7412008-09-01 19:14:022838 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
drh03bf26d2015-08-31 21:16:362839 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
2840 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
drh840561f2011-04-27 18:08:422841 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
drh4d70dba2025-02-22 23:18:382842 FUNCTION(unistr, 1, 0, 0, unistrFunc ),
danielk197793ce7412008-09-01 19:14:022843 FUNCTION(quote, 1, 0, 0, quoteFunc ),
drhb6205d42025-02-24 13:51:242844 FUNCTION(unistr_quote, 1, 1, 0, quoteFunc ),
drhb1fba282013-11-21 14:33:482845 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
2846 VFUNCTION(changes, 0, 0, 0, changes ),
2847 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
danielk197793ce7412008-09-01 19:14:022848 FUNCTION(replace, 3, 0, 0, replaceFunc ),
2849 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
drh80738d92016-02-15 00:34:162850 FUNCTION(substr, 2, 0, 0, substrFunc ),
2851 FUNCTION(substr, 3, 0, 0, substrFunc ),
drh1335ec72020-11-23 14:50:432852 FUNCTION(substring, 2, 0, 0, substrFunc ),
2853 FUNCTION(substring, 3, 0, 0, substrFunc ),
dan6fb2b542018-06-19 17:13:112854 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
2855 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
2856 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
larrybrbc917382023-06-07 08:40:312857 WAGGREGATE(count, 0,0,0, countStep,
drhbb301232021-07-15 19:29:432858 countFinalize, countFinalize, countInverse,
2859 SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ),
larrybrbc917382023-06-07 08:40:312860 WAGGREGATE(count, 1,0,0, countStep,
drhbb301232021-07-15 19:29:432861 countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ),
larrybrbc917382023-06-07 08:40:312862 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,
dan6fb2b542018-06-19 17:13:112863 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
larrybrbc917382023-06-07 08:40:312864 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,
dan6fb2b542018-06-19 17:13:112865 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
drhd5e040b2023-10-20 20:19:302866 WAGGREGATE(string_agg, 2, 0, 0, groupConcatStep,
2867 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
larrybrbc917382023-06-07 08:40:312868
danielk197793ce7412008-09-01 19:14:022869 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
drhcc153132016-08-04 12:35:172870#ifdef SQLITE_CASE_SENSITIVE_LIKE
danielk197793ce7412008-09-01 19:14:022871 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
2872 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
drhcc153132016-08-04 12:35:172873#else
danielk197793ce7412008-09-01 19:14:022874 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
2875 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
drhcc153132016-08-04 12:35:172876#endif
2877#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
2878 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
2879#endif
drhf6e904b2020-12-07 17:15:322880#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
drh4fd4a7a2021-01-01 01:44:062881 MFUNCTION(ceil, 1, xCeil, ceilingFunc ),
2882 MFUNCTION(ceiling, 1, xCeil, ceilingFunc ),
2883 MFUNCTION(floor, 1, xFloor, ceilingFunc ),
mistachkind97a4c02020-12-09 23:35:512884#if SQLITE_HAVE_C99_MATH_FUNCS
drh63f8f982020-12-07 21:13:062885 MFUNCTION(trunc, 1, trunc, ceilingFunc ),
mistachkind97a4c02020-12-09 23:35:512886#endif
drhf6e904b2020-12-07 17:15:322887 FUNCTION(ln, 1, 0, 0, logFunc ),
2888 FUNCTION(log, 1, 1, 0, logFunc ),
2889 FUNCTION(log10, 1, 1, 0, logFunc ),
drh63f8f982020-12-07 21:13:062890 FUNCTION(log2, 1, 2, 0, logFunc ),
drhf6e904b2020-12-07 17:15:322891 FUNCTION(log, 2, 0, 0, logFunc ),
drh63f8f982020-12-07 21:13:062892 MFUNCTION(exp, 1, exp, math1Func ),
2893 MFUNCTION(pow, 2, pow, math2Func ),
2894 MFUNCTION(power, 2, pow, math2Func ),
2895 MFUNCTION(mod, 2, fmod, math2Func ),
2896 MFUNCTION(acos, 1, acos, math1Func ),
2897 MFUNCTION(asin, 1, asin, math1Func ),
2898 MFUNCTION(atan, 1, atan, math1Func ),
2899 MFUNCTION(atan2, 2, atan2, math2Func ),
2900 MFUNCTION(cos, 1, cos, math1Func ),
2901 MFUNCTION(sin, 1, sin, math1Func ),
2902 MFUNCTION(tan, 1, tan, math1Func ),
2903 MFUNCTION(cosh, 1, cosh, math1Func ),
2904 MFUNCTION(sinh, 1, sinh, math1Func ),
2905 MFUNCTION(tanh, 1, tanh, math1Func ),
mistachkind97a4c02020-12-09 23:35:512906#if SQLITE_HAVE_C99_MATH_FUNCS
drh63f8f982020-12-07 21:13:062907 MFUNCTION(acosh, 1, acosh, math1Func ),
2908 MFUNCTION(asinh, 1, asinh, math1Func ),
2909 MFUNCTION(atanh, 1, atanh, math1Func ),
mistachkind97a4c02020-12-09 23:35:512910#endif
drh63f8f982020-12-07 21:13:062911 MFUNCTION(sqrt, 1, sqrt, math1Func ),
2912 MFUNCTION(radians, 1, degToRad, math1Func ),
2913 MFUNCTION(degrees, 1, radToDeg, math1Func ),
drhddc764b2024-10-07 21:04:572914 MFUNCTION(pi, 0, 0, piFunc ),
drhf6e904b2020-12-07 17:15:322915#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
drh63f8f982020-12-07 21:13:062916 FUNCTION(sign, 1, 0, 0, signFunc ),
drh2e899cc2025-01-21 15:12:002917 INLINE_FUNC(coalesce, -4, INLINEFUNC_coalesce, 0 ),
drh36279c22025-01-28 20:32:482918 INLINE_FUNC(iif, -4, INLINEFUNC_iif, 0 ),
2919 INLINE_FUNC(if, -4, INLINEFUNC_iif, 0 ),
danielk197793ce7412008-09-01 19:14:022920 };
drh545f5872010-04-24 14:02:592921#ifndef SQLITE_OMIT_ALTERTABLE
2922 sqlite3AlterFunctions();
2923#endif
dandfa552f2018-06-02 21:04:282924 sqlite3WindowFunctions();
drh80738d92016-02-15 00:34:162925 sqlite3RegisterDateTimeFunctions();
drh9dbf96b2022-01-06 01:40:092926 sqlite3RegisterJsonFunctions();
drh80738d92016-02-15 00:34:162927 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
2928
2929#if 0 /* Enable to print out how the built-in functions are hashed */
2930 {
2931 int i;
2932 FuncDef *p;
2933 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
2934 printf("FUNC-HASH %02d:", i);
2935 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
2936 int n = sqlite3Strlen30(p->zName);
2937 int h = p->zName[0] + n;
drhf9751072021-10-07 13:40:292938 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
drh80738d92016-02-15 00:34:162939 printf(" %s(%d)", p->zName, h);
2940 }
2941 printf("\n");
2942 }
2943 }
2944#endif
drh70a8ca32008-08-21 18:49:272945}