xref: /sqlite-3.40.0/src/func.c (revision 47c8a679)
1dc04c583Sdrh /*
2dc04c583Sdrh ** 2002 February 23
3dc04c583Sdrh **
4dc04c583Sdrh ** The author disclaims copyright to this source code.  In place of
5dc04c583Sdrh ** a legal notice, here is a blessing:
6dc04c583Sdrh **
7dc04c583Sdrh **    May you do good and not evil.
8dc04c583Sdrh **    May you find forgiveness for yourself and forgive others.
9dc04c583Sdrh **    May you share freely, never taking more than you give.
10dc04c583Sdrh **
11dc04c583Sdrh *************************************************************************
12dc04c583Sdrh ** This file contains the C functions that implement various SQL
13dc04c583Sdrh ** functions of SQLite.
14dc04c583Sdrh **
15dc04c583Sdrh ** There is only one exported symbol in this file - the function
16dc04c583Sdrh ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
17dc04c583Sdrh ** All other code has file scope.
18dc04c583Sdrh **
19*47c8a679Sdrh ** $Id: func.c,v 1.12 2002/02/28 04:00:12 drh Exp $
20dc04c583Sdrh */
21dc04c583Sdrh #include <ctype.h>
22d3a149efSdrh #include <math.h>
23d3a149efSdrh #include <stdlib.h>
240bce8354Sdrh #include <assert.h>
250bce8354Sdrh #include "sqliteInt.h"
260bce8354Sdrh 
270bce8354Sdrh /*
280bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions
290bce8354Sdrh */
300bce8354Sdrh static void minFunc(sqlite_func *context, int argc, const char **argv){
310bce8354Sdrh   const char *zBest;
320bce8354Sdrh   int i;
330bce8354Sdrh 
3489425d5eSdrh   if( argc==0 ) return;
350bce8354Sdrh   zBest = argv[0];
360bce8354Sdrh   for(i=1; i<argc; i++){
370bce8354Sdrh     if( sqliteCompare(argv[i], zBest)<0 ){
380bce8354Sdrh       zBest = argv[i];
390bce8354Sdrh     }
400bce8354Sdrh   }
410bce8354Sdrh   sqlite_set_result_string(context, zBest, -1);
420bce8354Sdrh }
430bce8354Sdrh static void maxFunc(sqlite_func *context, int argc, const char **argv){
440bce8354Sdrh   const char *zBest;
450bce8354Sdrh   int i;
460bce8354Sdrh 
4789425d5eSdrh   if( argc==0 ) return;
480bce8354Sdrh   zBest = argv[0];
490bce8354Sdrh   for(i=1; i<argc; i++){
500bce8354Sdrh     if( sqliteCompare(argv[i], zBest)>0 ){
510bce8354Sdrh       zBest = argv[i];
520bce8354Sdrh     }
530bce8354Sdrh   }
540bce8354Sdrh   sqlite_set_result_string(context, zBest, -1);
550bce8354Sdrh }
560bce8354Sdrh 
570bce8354Sdrh /*
580bce8354Sdrh ** Implementation of the length() function
590bce8354Sdrh */
600bce8354Sdrh static void lengthFunc(sqlite_func *context, int argc, const char **argv){
610bce8354Sdrh   const char *z;
620bce8354Sdrh   int len;
630bce8354Sdrh 
640bce8354Sdrh   assert( argc==1 );
650bce8354Sdrh   z = argv[0];
660bce8354Sdrh   if( z==0 ){
670bce8354Sdrh     len = 0;
680bce8354Sdrh   }else{
690bce8354Sdrh #ifdef SQLITE_UTF8
700bce8354Sdrh     for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
710bce8354Sdrh #else
720bce8354Sdrh     len = strlen(z);
730bce8354Sdrh #endif
740bce8354Sdrh   }
750bce8354Sdrh   sqlite_set_result_int(context, len);
760bce8354Sdrh }
770bce8354Sdrh 
780bce8354Sdrh /*
790bce8354Sdrh ** Implementation of the abs() function
800bce8354Sdrh */
810bce8354Sdrh static void absFunc(sqlite_func *context, int argc, const char **argv){
820bce8354Sdrh   const char *z;
830bce8354Sdrh   assert( argc==1 );
840bce8354Sdrh   z = argv[0];
850bce8354Sdrh   if( z && z[0]=='-' && isdigit(z[1]) ) z++;
860bce8354Sdrh   sqlite_set_result_string(context, z, -1);
870bce8354Sdrh }
880bce8354Sdrh 
890bce8354Sdrh /*
900bce8354Sdrh ** Implementation of the substr() function
910bce8354Sdrh */
920bce8354Sdrh static void substrFunc(sqlite_func *context, int argc, const char **argv){
930bce8354Sdrh   const char *z;
940bce8354Sdrh #ifdef SQLITE_UTF8
950bce8354Sdrh   const char *z2;
960bce8354Sdrh   int i;
970bce8354Sdrh #endif
980bce8354Sdrh   int p1, p2, len;
990bce8354Sdrh   assert( argc==3 );
1000bce8354Sdrh   z = argv[0];
1010bce8354Sdrh   if( z==0 ) return;
1020bce8354Sdrh   p1 = atoi(argv[1]?argv[1]:0);
1030bce8354Sdrh   p2 = atoi(argv[2]?argv[2]:0);
1040bce8354Sdrh #ifdef SQLITE_UTF8
105*47c8a679Sdrh   for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
1060bce8354Sdrh #else
1070bce8354Sdrh   len = strlen(z);
1080bce8354Sdrh #endif
1090bce8354Sdrh   if( p1<0 ){
11089425d5eSdrh     p1 += len;
111653bc759Sdrh     if( p1<0 ){
112653bc759Sdrh       p2 += p1;
113653bc759Sdrh       p1 = 0;
114653bc759Sdrh     }
1150bce8354Sdrh   }else if( p1>0 ){
1160bce8354Sdrh     p1--;
1170bce8354Sdrh   }
1180bce8354Sdrh   if( p1+p2>len ){
1190bce8354Sdrh     p2 = len-p1;
1200bce8354Sdrh   }
1210bce8354Sdrh #ifdef SQLITE_UTF8
1220bce8354Sdrh   for(i=0; i<p1; i++){
1230bce8354Sdrh     assert( z[i] );
124*47c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p1++;
1250bce8354Sdrh   }
126*47c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
1270bce8354Sdrh   for(; i<p1+p2; i++){
1280bce8354Sdrh     assert( z[i] );
129*47c8a679Sdrh     if( (z[i]&0xc0)==0x80 ) p2++;
1300bce8354Sdrh   }
131*47c8a679Sdrh   while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
1320bce8354Sdrh #endif
133653bc759Sdrh   if( p2<0 ) p2 = 0;
1340bce8354Sdrh   sqlite_set_result_string(context, &z[p1], p2);
1350bce8354Sdrh }
1360bce8354Sdrh 
1370bce8354Sdrh /*
1380bce8354Sdrh ** Implementation of the round() function
1390bce8354Sdrh */
1400bce8354Sdrh static void roundFunc(sqlite_func *context, int argc, const char **argv){
1410bce8354Sdrh   int n;
1420bce8354Sdrh   double r;
1430bce8354Sdrh   char zBuf[100];
1440bce8354Sdrh   assert( argc==1 || argc==2 );
1450bce8354Sdrh   n = argc==2 && argv[1] ? atoi(argv[1]) : 0;
1460bce8354Sdrh   if( n>30 ) n = 30;
1470bce8354Sdrh   if( n<0 ) n = 0;
1480bce8354Sdrh   r = argv[0] ? atof(argv[0]) : 0.0;
1490bce8354Sdrh   sprintf(zBuf,"%.*f",n,r);
1500bce8354Sdrh   sqlite_set_result_string(context, zBuf, -1);
1510bce8354Sdrh }
152dc04c583Sdrh 
153dc04c583Sdrh /*
154dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions.
155dc04c583Sdrh */
1561350b030Sdrh static void upperFunc(sqlite_func *context, int argc, const char **argv){
157dc04c583Sdrh   char *z;
158dc04c583Sdrh   int i;
159dc04c583Sdrh   if( argc<1 || argv[0]==0 ) return;
160dc04c583Sdrh   z = sqlite_set_result_string(context, argv[0], -1);
161dc04c583Sdrh   if( z==0 ) return;
162dc04c583Sdrh   for(i=0; z[i]; i++){
163dc04c583Sdrh     if( islower(z[i]) ) z[i] = toupper(z[i]);
164dc04c583Sdrh   }
165dc04c583Sdrh }
1661350b030Sdrh static void lowerFunc(sqlite_func *context, int argc, const char **argv){
167dc04c583Sdrh   char *z;
168dc04c583Sdrh   int i;
169dc04c583Sdrh   if( argc<1 || argv[0]==0 ) return;
170dc04c583Sdrh   z = sqlite_set_result_string(context, argv[0], -1);
171dc04c583Sdrh   if( z==0 ) return;
172dc04c583Sdrh   for(i=0; z[i]; i++){
173dc04c583Sdrh     if( isupper(z[i]) ) z[i] = tolower(z[i]);
174dc04c583Sdrh   }
175dc04c583Sdrh }
176dc04c583Sdrh 
177dc04c583Sdrh /*
178fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
179fbc99082Sdrh ** All three do the same thing.  They return the first argument
180fbc99082Sdrh ** non-NULL argument.
1813212e182Sdrh */
1823212e182Sdrh static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
183fbc99082Sdrh   int i;
184fbc99082Sdrh   for(i=0; i<argc; i++){
185fbc99082Sdrh     if( argv[i] ){
186fbc99082Sdrh       sqlite_set_result_string(context, argv[i], -1);
187fbc99082Sdrh       break;
188fbc99082Sdrh     }
189fbc99082Sdrh   }
1903212e182Sdrh }
1913212e182Sdrh 
1923212e182Sdrh /*
193d3a149efSdrh ** An instance of the following structure holds the context of a
194dd5baa95Sdrh ** sum() or avg() aggregate computation.
195dd5baa95Sdrh */
196dd5baa95Sdrh typedef struct SumCtx SumCtx;
197dd5baa95Sdrh struct SumCtx {
198dd5baa95Sdrh   double sum;     /* Sum of terms */
199dd5baa95Sdrh };
200dd5baa95Sdrh 
201dd5baa95Sdrh /*
202dd5baa95Sdrh ** Routines used to compute the sum or average.
203dd5baa95Sdrh */
204dd5baa95Sdrh static void sumStep(sqlite_func *context, int argc, const char **argv){
205dd5baa95Sdrh   SumCtx *p;
206dd5baa95Sdrh   double x;
207dd5baa95Sdrh   if( argc<1 ) return;
208dd5baa95Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
209dd5baa95Sdrh   if( p==0 ) return;
210dd5baa95Sdrh   x = argv[0] ? atof(argv[0]) : 0.0;
211dd5baa95Sdrh   p->sum += x;
212dd5baa95Sdrh }
213dd5baa95Sdrh static void sumFinalize(sqlite_func *context){
214dd5baa95Sdrh   SumCtx *p;
215dd5baa95Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
21689425d5eSdrh   sqlite_set_result_double(context, p ? p->sum : 0.0);
217dd5baa95Sdrh }
218dd5baa95Sdrh static void avgFinalize(sqlite_func *context){
219dd5baa95Sdrh   SumCtx *p;
220dd5baa95Sdrh   double rN;
221dd5baa95Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
222dd5baa95Sdrh   rN = sqlite_aggregate_count(context);
223dd5baa95Sdrh   if( p && rN>0.0 ){
224dd5baa95Sdrh     sqlite_set_result_double(context, p->sum/rN);
225dd5baa95Sdrh   }
226dd5baa95Sdrh }
227dd5baa95Sdrh 
228dd5baa95Sdrh /*
229dd5baa95Sdrh ** An instance of the following structure holds the context of a
230a2ed5601Sdrh ** variance or standard deviation computation.
231d3a149efSdrh */
232d3a149efSdrh typedef struct StdDevCtx StdDevCtx;
233d3a149efSdrh struct StdDevCtx {
234d3a149efSdrh   double sum;     /* Sum of terms */
235d3a149efSdrh   double sum2;    /* Sum of the squares of terms */
236d3a149efSdrh };
237d3a149efSdrh 
238d3a149efSdrh /*
239d3a149efSdrh ** Routines used to compute the standard deviation as an aggregate.
240d3a149efSdrh */
2411350b030Sdrh static void stdDevStep(sqlite_func *context, int argc, const char **argv){
242d3a149efSdrh   StdDevCtx *p;
243d3a149efSdrh   double x;
2441350b030Sdrh   if( argc<1 ) return;
2451350b030Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
2461350b030Sdrh   if( p==0 ) return;
247dd5baa95Sdrh   x = argv[0] ? atof(argv[0]) : 0.0;
248d3a149efSdrh   p->sum += x;
249d3a149efSdrh   p->sum2 += x*x;
250d3a149efSdrh }
2511350b030Sdrh static void stdDevFinalize(sqlite_func *context){
252dd5baa95Sdrh   double rN = sqlite_aggregate_count(context);
2531350b030Sdrh   StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
254dd5baa95Sdrh   if( p && rN>1.0 ){
255d3a149efSdrh     sqlite_set_result_double(context,
256d3a149efSdrh        sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
257d3a149efSdrh   }
258d3a149efSdrh }
259d3a149efSdrh 
2600bce8354Sdrh /*
2610bce8354Sdrh ** The following structure keeps track of state information for the
2620bce8354Sdrh ** count() aggregate function.
2630bce8354Sdrh */
2640bce8354Sdrh typedef struct CountCtx CountCtx;
2650bce8354Sdrh struct CountCtx {
2660bce8354Sdrh   int n;
2670bce8354Sdrh };
268dd5baa95Sdrh 
2690bce8354Sdrh /*
2700bce8354Sdrh ** Routines to implement the count() aggregate function.
2710bce8354Sdrh */
2720bce8354Sdrh static void countStep(sqlite_func *context, int argc, const char **argv){
2730bce8354Sdrh   CountCtx *p;
2740bce8354Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
2750bce8354Sdrh   if( (argc==0 || argv[0]) && p ){
2760bce8354Sdrh     p->n++;
2770bce8354Sdrh   }
2780bce8354Sdrh }
2790bce8354Sdrh static void countFinalize(sqlite_func *context){
2800bce8354Sdrh   CountCtx *p;
2810bce8354Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
282f55f25f0Sdrh   sqlite_set_result_int(context, p ? p->n : 0);
2830bce8354Sdrh }
2840bce8354Sdrh 
2850bce8354Sdrh /*
2860bce8354Sdrh ** This function tracks state information for the min() and max()
2870bce8354Sdrh ** aggregate functions.
2880bce8354Sdrh */
2890bce8354Sdrh typedef struct MinMaxCtx MinMaxCtx;
2900bce8354Sdrh struct MinMaxCtx {
2910bce8354Sdrh   char *z;         /* The best so far */
2920bce8354Sdrh   char zBuf[28];   /* Space that can be used for storage */
2930bce8354Sdrh };
2940bce8354Sdrh 
2950bce8354Sdrh /*
2960bce8354Sdrh ** Routines to implement min() and max() aggregate functions.
2970bce8354Sdrh */
2980bce8354Sdrh static void minStep(sqlite_func *context, int argc, const char **argv){
2990bce8354Sdrh   MinMaxCtx *p;
3000bce8354Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
3010bce8354Sdrh   if( p==0 || argc<1 ) return;
3020bce8354Sdrh   if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){
3030bce8354Sdrh     if( p->z && p->z!=p->zBuf ){
3040bce8354Sdrh       sqliteFree(p->z);
3050bce8354Sdrh     }
3060bce8354Sdrh     if( argv[0] ){
3070bce8354Sdrh       int len = strlen(argv[0]);
3080bce8354Sdrh       if( len < sizeof(p->zBuf) ){
3090bce8354Sdrh         p->z = p->zBuf;
3100bce8354Sdrh       }else{
3110bce8354Sdrh         p->z = sqliteMalloc( len+1 );
3120bce8354Sdrh         if( p->z==0 ) return;
3130bce8354Sdrh       }
3140bce8354Sdrh       strcpy(p->z, argv[0]);
3150bce8354Sdrh     }else{
3160bce8354Sdrh       p->z = 0;
3170bce8354Sdrh     }
3180bce8354Sdrh   }
3190bce8354Sdrh }
3200bce8354Sdrh static void maxStep(sqlite_func *context, int argc, const char **argv){
3210bce8354Sdrh   MinMaxCtx *p;
3220bce8354Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
3230bce8354Sdrh   if( p==0 || argc<1 ) return;
3240bce8354Sdrh   if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){
3250bce8354Sdrh     if( p->z && p->z!=p->zBuf ){
3260bce8354Sdrh       sqliteFree(p->z);
3270bce8354Sdrh     }
3280bce8354Sdrh     if( argv[0] ){
3290bce8354Sdrh       int len = strlen(argv[0]);
3300bce8354Sdrh       if( len < sizeof(p->zBuf) ){
3310bce8354Sdrh         p->z = p->zBuf;
3320bce8354Sdrh       }else{
3330bce8354Sdrh         p->z = sqliteMalloc( len+1 );
3340bce8354Sdrh         if( p->z==0 ) return;
3350bce8354Sdrh       }
3360bce8354Sdrh       strcpy(p->z, argv[0]);
3370bce8354Sdrh     }else{
3380bce8354Sdrh       p->z = 0;
3390bce8354Sdrh     }
3400bce8354Sdrh   }
3410bce8354Sdrh }
3420bce8354Sdrh static void minMaxFinalize(sqlite_func *context){
3430bce8354Sdrh   MinMaxCtx *p;
3440bce8354Sdrh   p = sqlite_aggregate_context(context, sizeof(*p));
3450bce8354Sdrh   if( p && p->z ){
3460bce8354Sdrh     sqlite_set_result_string(context, p->z, strlen(p->z));
3470bce8354Sdrh   }
3480bce8354Sdrh   if( p && p->z && p->z!=p->zBuf ){
3490bce8354Sdrh     sqliteFree(p->z);
3500bce8354Sdrh   }
3510bce8354Sdrh }
352dd5baa95Sdrh 
353d3a149efSdrh /*
354a2ed5601Sdrh ** This function registered all of the above C functions as SQL
355a2ed5601Sdrh ** functions.  This should be the only routine in this file with
356a2ed5601Sdrh ** external linkage.
357dc04c583Sdrh */
358dc04c583Sdrh void sqliteRegisterBuildinFunctions(sqlite *db){
3590bce8354Sdrh   static struct {
3600bce8354Sdrh      char *zName;
3610bce8354Sdrh      int nArg;
3620bce8354Sdrh      void (*xFunc)(sqlite_func*,int,const char**);
3630bce8354Sdrh   } aFuncs[] = {
3640bce8354Sdrh     { "min",       -1, minFunc    },
365fbc99082Sdrh     { "min",        0, 0          },
3660bce8354Sdrh     { "max",       -1, maxFunc    },
367fbc99082Sdrh     { "max",        0, 0          },
3680bce8354Sdrh     { "length",     1, lengthFunc },
3690bce8354Sdrh     { "substr",     3, substrFunc },
3700bce8354Sdrh     { "abs",        1, absFunc    },
3710bce8354Sdrh     { "round",      1, roundFunc  },
3720bce8354Sdrh     { "round",      2, roundFunc  },
3730bce8354Sdrh     { "upper",      1, upperFunc  },
3740bce8354Sdrh     { "lower",      1, lowerFunc  },
375fbc99082Sdrh     { "coalesce",  -1, ifnullFunc },
376fbc99082Sdrh     { "coalesce",   0, 0          },
377fbc99082Sdrh     { "coalesce",   1, 0          },
378fbc99082Sdrh 
3790bce8354Sdrh   };
3800bce8354Sdrh   static struct {
3810bce8354Sdrh     char *zName;
3820bce8354Sdrh     int nArg;
3830bce8354Sdrh     void (*xStep)(sqlite_func*,int,const char**);
3840bce8354Sdrh     void (*xFinalize)(sqlite_func*);
3850bce8354Sdrh   } aAggs[] = {
3860bce8354Sdrh     { "min",    1, minStep,      minMaxFinalize },
3870bce8354Sdrh     { "max",    1, maxStep,      minMaxFinalize },
3880bce8354Sdrh     { "sum",    1, sumStep,      sumFinalize    },
3890bce8354Sdrh     { "avg",    1, sumStep,      avgFinalize    },
3900bce8354Sdrh     { "count",  0, countStep,    countFinalize  },
3910bce8354Sdrh     { "count",  1, countStep,    countFinalize  },
3920bce8354Sdrh     { "stddev", 1, stdDevStep,   stdDevFinalize },
3930bce8354Sdrh   };
3940bce8354Sdrh   int i;
3950bce8354Sdrh 
3960bce8354Sdrh   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
3970bce8354Sdrh     sqlite_create_function(db, aFuncs[i].zName,
3980bce8354Sdrh            aFuncs[i].nArg, aFuncs[i].xFunc, 0);
3990bce8354Sdrh   }
4000bce8354Sdrh   for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
4010bce8354Sdrh     sqlite_create_aggregate(db, aAggs[i].zName,
4020bce8354Sdrh            aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
4030bce8354Sdrh   }
404dc04c583Sdrh }
405