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*fbc99082Sdrh ** $Id: func.c,v 1.10 2002/02/28 03:14:18 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 1050bce8354Sdrh for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z)!=0x80 ) len++; } 1060bce8354Sdrh #else 1070bce8354Sdrh len = strlen(z); 1080bce8354Sdrh #endif 1090bce8354Sdrh if( p1<0 ){ 11089425d5eSdrh p1 += len; 1110bce8354Sdrh }else if( p1>0 ){ 1120bce8354Sdrh p1--; 1130bce8354Sdrh } 1140bce8354Sdrh if( p1+p2>len ){ 1150bce8354Sdrh p2 = len-p1; 1160bce8354Sdrh } 1170bce8354Sdrh #ifdef SQLITE_UTF8 1180bce8354Sdrh for(i=0; i<p1; i++){ 1190bce8354Sdrh assert( z[i] ); 1200bce8354Sdrh if( (z[i]&0xc0)!=0x80 ) p1++; 1210bce8354Sdrh } 1220bce8354Sdrh for(; i<p1+p2; i++){ 1230bce8354Sdrh assert( z[i] ); 1240bce8354Sdrh if( (z[i]&0xc0)!=0x80 ) p2++; 1250bce8354Sdrh } 1260bce8354Sdrh #endif 1270bce8354Sdrh sqlite_set_result_string(context, &z[p1], p2); 1280bce8354Sdrh } 1290bce8354Sdrh 1300bce8354Sdrh /* 1310bce8354Sdrh ** Implementation of the round() function 1320bce8354Sdrh */ 1330bce8354Sdrh static void roundFunc(sqlite_func *context, int argc, const char **argv){ 1340bce8354Sdrh int n; 1350bce8354Sdrh double r; 1360bce8354Sdrh char zBuf[100]; 1370bce8354Sdrh assert( argc==1 || argc==2 ); 1380bce8354Sdrh n = argc==2 && argv[1] ? atoi(argv[1]) : 0; 1390bce8354Sdrh if( n>30 ) n = 30; 1400bce8354Sdrh if( n<0 ) n = 0; 1410bce8354Sdrh r = argv[0] ? atof(argv[0]) : 0.0; 1420bce8354Sdrh sprintf(zBuf,"%.*f",n,r); 1430bce8354Sdrh sqlite_set_result_string(context, zBuf, -1); 1440bce8354Sdrh } 145dc04c583Sdrh 146dc04c583Sdrh /* 147dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 148dc04c583Sdrh */ 1491350b030Sdrh static void upperFunc(sqlite_func *context, int argc, const char **argv){ 150dc04c583Sdrh char *z; 151dc04c583Sdrh int i; 152dc04c583Sdrh if( argc<1 || argv[0]==0 ) return; 153dc04c583Sdrh z = sqlite_set_result_string(context, argv[0], -1); 154dc04c583Sdrh if( z==0 ) return; 155dc04c583Sdrh for(i=0; z[i]; i++){ 156dc04c583Sdrh if( islower(z[i]) ) z[i] = toupper(z[i]); 157dc04c583Sdrh } 158dc04c583Sdrh } 1591350b030Sdrh static void lowerFunc(sqlite_func *context, int argc, const char **argv){ 160dc04c583Sdrh char *z; 161dc04c583Sdrh int i; 162dc04c583Sdrh if( argc<1 || argv[0]==0 ) return; 163dc04c583Sdrh z = sqlite_set_result_string(context, argv[0], -1); 164dc04c583Sdrh if( z==0 ) return; 165dc04c583Sdrh for(i=0; z[i]; i++){ 166dc04c583Sdrh if( isupper(z[i]) ) z[i] = tolower(z[i]); 167dc04c583Sdrh } 168dc04c583Sdrh } 169dc04c583Sdrh 170dc04c583Sdrh /* 171*fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. 172*fbc99082Sdrh ** All three do the same thing. They return the first argument 173*fbc99082Sdrh ** non-NULL argument. 1743212e182Sdrh */ 1753212e182Sdrh static void ifnullFunc(sqlite_func *context, int argc, const char **argv){ 176*fbc99082Sdrh int i; 177*fbc99082Sdrh for(i=0; i<argc; i++){ 178*fbc99082Sdrh if( argv[i] ){ 179*fbc99082Sdrh sqlite_set_result_string(context, argv[i], -1); 180*fbc99082Sdrh break; 181*fbc99082Sdrh } 182*fbc99082Sdrh } 1833212e182Sdrh } 1843212e182Sdrh 1853212e182Sdrh /* 186d3a149efSdrh ** An instance of the following structure holds the context of a 187dd5baa95Sdrh ** sum() or avg() aggregate computation. 188dd5baa95Sdrh */ 189dd5baa95Sdrh typedef struct SumCtx SumCtx; 190dd5baa95Sdrh struct SumCtx { 191dd5baa95Sdrh double sum; /* Sum of terms */ 192dd5baa95Sdrh }; 193dd5baa95Sdrh 194dd5baa95Sdrh /* 195dd5baa95Sdrh ** Routines used to compute the sum or average. 196dd5baa95Sdrh */ 197dd5baa95Sdrh static void sumStep(sqlite_func *context, int argc, const char **argv){ 198dd5baa95Sdrh SumCtx *p; 199dd5baa95Sdrh double x; 200dd5baa95Sdrh if( argc<1 ) return; 201dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 202dd5baa95Sdrh if( p==0 ) return; 203dd5baa95Sdrh x = argv[0] ? atof(argv[0]) : 0.0; 204dd5baa95Sdrh p->sum += x; 205dd5baa95Sdrh } 206dd5baa95Sdrh static void sumFinalize(sqlite_func *context){ 207dd5baa95Sdrh SumCtx *p; 208dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 20989425d5eSdrh sqlite_set_result_double(context, p ? p->sum : 0.0); 210dd5baa95Sdrh } 211dd5baa95Sdrh static void avgFinalize(sqlite_func *context){ 212dd5baa95Sdrh SumCtx *p; 213dd5baa95Sdrh double rN; 214dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 215dd5baa95Sdrh rN = sqlite_aggregate_count(context); 216dd5baa95Sdrh if( p && rN>0.0 ){ 217dd5baa95Sdrh sqlite_set_result_double(context, p->sum/rN); 218dd5baa95Sdrh } 219dd5baa95Sdrh } 220dd5baa95Sdrh 221dd5baa95Sdrh /* 222dd5baa95Sdrh ** An instance of the following structure holds the context of a 223a2ed5601Sdrh ** variance or standard deviation computation. 224d3a149efSdrh */ 225d3a149efSdrh typedef struct StdDevCtx StdDevCtx; 226d3a149efSdrh struct StdDevCtx { 227d3a149efSdrh double sum; /* Sum of terms */ 228d3a149efSdrh double sum2; /* Sum of the squares of terms */ 229d3a149efSdrh }; 230d3a149efSdrh 231d3a149efSdrh /* 232d3a149efSdrh ** Routines used to compute the standard deviation as an aggregate. 233d3a149efSdrh */ 2341350b030Sdrh static void stdDevStep(sqlite_func *context, int argc, const char **argv){ 235d3a149efSdrh StdDevCtx *p; 236d3a149efSdrh double x; 2371350b030Sdrh if( argc<1 ) return; 2381350b030Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 2391350b030Sdrh if( p==0 ) return; 240dd5baa95Sdrh x = argv[0] ? atof(argv[0]) : 0.0; 241d3a149efSdrh p->sum += x; 242d3a149efSdrh p->sum2 += x*x; 243d3a149efSdrh } 2441350b030Sdrh static void stdDevFinalize(sqlite_func *context){ 245dd5baa95Sdrh double rN = sqlite_aggregate_count(context); 2461350b030Sdrh StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p)); 247dd5baa95Sdrh if( p && rN>1.0 ){ 248d3a149efSdrh sqlite_set_result_double(context, 249d3a149efSdrh sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0))); 250d3a149efSdrh } 251d3a149efSdrh } 252d3a149efSdrh 2530bce8354Sdrh /* 2540bce8354Sdrh ** The following structure keeps track of state information for the 2550bce8354Sdrh ** count() aggregate function. 2560bce8354Sdrh */ 2570bce8354Sdrh typedef struct CountCtx CountCtx; 2580bce8354Sdrh struct CountCtx { 2590bce8354Sdrh int n; 2600bce8354Sdrh }; 261dd5baa95Sdrh 2620bce8354Sdrh /* 2630bce8354Sdrh ** Routines to implement the count() aggregate function. 2640bce8354Sdrh */ 2650bce8354Sdrh static void countStep(sqlite_func *context, int argc, const char **argv){ 2660bce8354Sdrh CountCtx *p; 2670bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 2680bce8354Sdrh if( (argc==0 || argv[0]) && p ){ 2690bce8354Sdrh p->n++; 2700bce8354Sdrh } 2710bce8354Sdrh } 2720bce8354Sdrh static void countFinalize(sqlite_func *context){ 2730bce8354Sdrh CountCtx *p; 2740bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 275f55f25f0Sdrh sqlite_set_result_int(context, p ? p->n : 0); 2760bce8354Sdrh } 2770bce8354Sdrh 2780bce8354Sdrh /* 2790bce8354Sdrh ** This function tracks state information for the min() and max() 2800bce8354Sdrh ** aggregate functions. 2810bce8354Sdrh */ 2820bce8354Sdrh typedef struct MinMaxCtx MinMaxCtx; 2830bce8354Sdrh struct MinMaxCtx { 2840bce8354Sdrh char *z; /* The best so far */ 2850bce8354Sdrh char zBuf[28]; /* Space that can be used for storage */ 2860bce8354Sdrh }; 2870bce8354Sdrh 2880bce8354Sdrh /* 2890bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 2900bce8354Sdrh */ 2910bce8354Sdrh static void minStep(sqlite_func *context, int argc, const char **argv){ 2920bce8354Sdrh MinMaxCtx *p; 2930bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 2940bce8354Sdrh if( p==0 || argc<1 ) return; 2950bce8354Sdrh if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){ 2960bce8354Sdrh if( p->z && p->z!=p->zBuf ){ 2970bce8354Sdrh sqliteFree(p->z); 2980bce8354Sdrh } 2990bce8354Sdrh if( argv[0] ){ 3000bce8354Sdrh int len = strlen(argv[0]); 3010bce8354Sdrh if( len < sizeof(p->zBuf) ){ 3020bce8354Sdrh p->z = p->zBuf; 3030bce8354Sdrh }else{ 3040bce8354Sdrh p->z = sqliteMalloc( len+1 ); 3050bce8354Sdrh if( p->z==0 ) return; 3060bce8354Sdrh } 3070bce8354Sdrh strcpy(p->z, argv[0]); 3080bce8354Sdrh }else{ 3090bce8354Sdrh p->z = 0; 3100bce8354Sdrh } 3110bce8354Sdrh } 3120bce8354Sdrh } 3130bce8354Sdrh static void maxStep(sqlite_func *context, int argc, const char **argv){ 3140bce8354Sdrh MinMaxCtx *p; 3150bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 3160bce8354Sdrh if( p==0 || argc<1 ) return; 3170bce8354Sdrh if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){ 3180bce8354Sdrh if( p->z && p->z!=p->zBuf ){ 3190bce8354Sdrh sqliteFree(p->z); 3200bce8354Sdrh } 3210bce8354Sdrh if( argv[0] ){ 3220bce8354Sdrh int len = strlen(argv[0]); 3230bce8354Sdrh if( len < sizeof(p->zBuf) ){ 3240bce8354Sdrh p->z = p->zBuf; 3250bce8354Sdrh }else{ 3260bce8354Sdrh p->z = sqliteMalloc( len+1 ); 3270bce8354Sdrh if( p->z==0 ) return; 3280bce8354Sdrh } 3290bce8354Sdrh strcpy(p->z, argv[0]); 3300bce8354Sdrh }else{ 3310bce8354Sdrh p->z = 0; 3320bce8354Sdrh } 3330bce8354Sdrh } 3340bce8354Sdrh } 3350bce8354Sdrh static void minMaxFinalize(sqlite_func *context){ 3360bce8354Sdrh MinMaxCtx *p; 3370bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 3380bce8354Sdrh if( p && p->z ){ 3390bce8354Sdrh sqlite_set_result_string(context, p->z, strlen(p->z)); 3400bce8354Sdrh } 3410bce8354Sdrh if( p && p->z && p->z!=p->zBuf ){ 3420bce8354Sdrh sqliteFree(p->z); 3430bce8354Sdrh } 3440bce8354Sdrh } 345dd5baa95Sdrh 346d3a149efSdrh /* 347a2ed5601Sdrh ** This function registered all of the above C functions as SQL 348a2ed5601Sdrh ** functions. This should be the only routine in this file with 349a2ed5601Sdrh ** external linkage. 350dc04c583Sdrh */ 351dc04c583Sdrh void sqliteRegisterBuildinFunctions(sqlite *db){ 3520bce8354Sdrh static struct { 3530bce8354Sdrh char *zName; 3540bce8354Sdrh int nArg; 3550bce8354Sdrh void (*xFunc)(sqlite_func*,int,const char**); 3560bce8354Sdrh } aFuncs[] = { 3570bce8354Sdrh { "min", -1, minFunc }, 358*fbc99082Sdrh { "min", 0, 0 }, 3590bce8354Sdrh { "max", -1, maxFunc }, 360*fbc99082Sdrh { "max", 0, 0 }, 3610bce8354Sdrh { "length", 1, lengthFunc }, 3620bce8354Sdrh { "substr", 3, substrFunc }, 3630bce8354Sdrh { "abs", 1, absFunc }, 3640bce8354Sdrh { "round", 1, roundFunc }, 3650bce8354Sdrh { "round", 2, roundFunc }, 3660bce8354Sdrh { "upper", 1, upperFunc }, 3670bce8354Sdrh { "lower", 1, lowerFunc }, 368*fbc99082Sdrh { "coalesce", -1, ifnullFunc }, 369*fbc99082Sdrh { "coalesce", 0, 0 }, 370*fbc99082Sdrh { "coalesce", 1, 0 }, 371*fbc99082Sdrh 3720bce8354Sdrh }; 3730bce8354Sdrh static struct { 3740bce8354Sdrh char *zName; 3750bce8354Sdrh int nArg; 3760bce8354Sdrh void (*xStep)(sqlite_func*,int,const char**); 3770bce8354Sdrh void (*xFinalize)(sqlite_func*); 3780bce8354Sdrh } aAggs[] = { 3790bce8354Sdrh { "min", 1, minStep, minMaxFinalize }, 3800bce8354Sdrh { "max", 1, maxStep, minMaxFinalize }, 3810bce8354Sdrh { "sum", 1, sumStep, sumFinalize }, 3820bce8354Sdrh { "avg", 1, sumStep, avgFinalize }, 3830bce8354Sdrh { "count", 0, countStep, countFinalize }, 3840bce8354Sdrh { "count", 1, countStep, countFinalize }, 3850bce8354Sdrh { "stddev", 1, stdDevStep, stdDevFinalize }, 3860bce8354Sdrh }; 3870bce8354Sdrh int i; 3880bce8354Sdrh 3890bce8354Sdrh for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 3900bce8354Sdrh sqlite_create_function(db, aFuncs[i].zName, 3910bce8354Sdrh aFuncs[i].nArg, aFuncs[i].xFunc, 0); 3920bce8354Sdrh } 3930bce8354Sdrh for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ 3940bce8354Sdrh sqlite_create_aggregate(db, aAggs[i].zName, 3950bce8354Sdrh aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0); 3960bce8354Sdrh } 397dc04c583Sdrh } 398