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*ef2daf54Sdrh ** $Id: func.c,v 1.14 2002/03/04 02:26:16 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 10547c8a679Sdrh 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] ); 12447c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p1++; 1250bce8354Sdrh } 12647c8a679Sdrh while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } 1270bce8354Sdrh for(; i<p1+p2; i++){ 1280bce8354Sdrh assert( z[i] ); 12947c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p2++; 1300bce8354Sdrh } 13147c8a679Sdrh 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 /* 193f9ffac96Sdrh ** Implementation of random(). Return a random integer. 194f9ffac96Sdrh */ 195f9ffac96Sdrh static void randomFunc(sqlite_func *context, int argc, const char **argv){ 196f9ffac96Sdrh sqlite_set_result_int(context, sqliteRandomInteger()); 197f9ffac96Sdrh } 198f9ffac96Sdrh 199f9ffac96Sdrh /* 200d3a149efSdrh ** An instance of the following structure holds the context of a 201dd5baa95Sdrh ** sum() or avg() aggregate computation. 202dd5baa95Sdrh */ 203dd5baa95Sdrh typedef struct SumCtx SumCtx; 204dd5baa95Sdrh struct SumCtx { 205dd5baa95Sdrh double sum; /* Sum of terms */ 206dd5baa95Sdrh }; 207dd5baa95Sdrh 208dd5baa95Sdrh /* 209dd5baa95Sdrh ** Routines used to compute the sum or average. 210dd5baa95Sdrh */ 211dd5baa95Sdrh static void sumStep(sqlite_func *context, int argc, const char **argv){ 212dd5baa95Sdrh SumCtx *p; 213dd5baa95Sdrh double x; 214dd5baa95Sdrh if( argc<1 ) return; 215dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 216dd5baa95Sdrh if( p==0 ) return; 217dd5baa95Sdrh x = argv[0] ? atof(argv[0]) : 0.0; 218dd5baa95Sdrh p->sum += x; 219dd5baa95Sdrh } 220dd5baa95Sdrh static void sumFinalize(sqlite_func *context){ 221dd5baa95Sdrh SumCtx *p; 222dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 22389425d5eSdrh sqlite_set_result_double(context, p ? p->sum : 0.0); 224dd5baa95Sdrh } 225dd5baa95Sdrh static void avgFinalize(sqlite_func *context){ 226dd5baa95Sdrh SumCtx *p; 227dd5baa95Sdrh double rN; 228dd5baa95Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 229dd5baa95Sdrh rN = sqlite_aggregate_count(context); 230dd5baa95Sdrh if( p && rN>0.0 ){ 231dd5baa95Sdrh sqlite_set_result_double(context, p->sum/rN); 232dd5baa95Sdrh } 233dd5baa95Sdrh } 234dd5baa95Sdrh 235dd5baa95Sdrh /* 236dd5baa95Sdrh ** An instance of the following structure holds the context of a 237a2ed5601Sdrh ** variance or standard deviation computation. 238d3a149efSdrh */ 239d3a149efSdrh typedef struct StdDevCtx StdDevCtx; 240d3a149efSdrh struct StdDevCtx { 241d3a149efSdrh double sum; /* Sum of terms */ 242d3a149efSdrh double sum2; /* Sum of the squares of terms */ 243d3a149efSdrh }; 244d3a149efSdrh 245*ef2daf54Sdrh #if 0 /* Omit because math library is required */ 246d3a149efSdrh /* 247d3a149efSdrh ** Routines used to compute the standard deviation as an aggregate. 248d3a149efSdrh */ 2491350b030Sdrh static void stdDevStep(sqlite_func *context, int argc, const char **argv){ 250d3a149efSdrh StdDevCtx *p; 251d3a149efSdrh double x; 2521350b030Sdrh if( argc<1 ) return; 2531350b030Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 2541350b030Sdrh if( p==0 ) return; 255dd5baa95Sdrh x = argv[0] ? atof(argv[0]) : 0.0; 256d3a149efSdrh p->sum += x; 257d3a149efSdrh p->sum2 += x*x; 258d3a149efSdrh } 2591350b030Sdrh static void stdDevFinalize(sqlite_func *context){ 260dd5baa95Sdrh double rN = sqlite_aggregate_count(context); 2611350b030Sdrh StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p)); 262dd5baa95Sdrh if( p && rN>1.0 ){ 263d3a149efSdrh sqlite_set_result_double(context, 264d3a149efSdrh sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0))); 265d3a149efSdrh } 266d3a149efSdrh } 267*ef2daf54Sdrh #endif 268d3a149efSdrh 2690bce8354Sdrh /* 2700bce8354Sdrh ** The following structure keeps track of state information for the 2710bce8354Sdrh ** count() aggregate function. 2720bce8354Sdrh */ 2730bce8354Sdrh typedef struct CountCtx CountCtx; 2740bce8354Sdrh struct CountCtx { 2750bce8354Sdrh int n; 2760bce8354Sdrh }; 277dd5baa95Sdrh 2780bce8354Sdrh /* 2790bce8354Sdrh ** Routines to implement the count() aggregate function. 2800bce8354Sdrh */ 2810bce8354Sdrh static void countStep(sqlite_func *context, int argc, const char **argv){ 2820bce8354Sdrh CountCtx *p; 2830bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 2840bce8354Sdrh if( (argc==0 || argv[0]) && p ){ 2850bce8354Sdrh p->n++; 2860bce8354Sdrh } 2870bce8354Sdrh } 2880bce8354Sdrh static void countFinalize(sqlite_func *context){ 2890bce8354Sdrh CountCtx *p; 2900bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 291f55f25f0Sdrh sqlite_set_result_int(context, p ? p->n : 0); 2920bce8354Sdrh } 2930bce8354Sdrh 2940bce8354Sdrh /* 2950bce8354Sdrh ** This function tracks state information for the min() and max() 2960bce8354Sdrh ** aggregate functions. 2970bce8354Sdrh */ 2980bce8354Sdrh typedef struct MinMaxCtx MinMaxCtx; 2990bce8354Sdrh struct MinMaxCtx { 3000bce8354Sdrh char *z; /* The best so far */ 3010bce8354Sdrh char zBuf[28]; /* Space that can be used for storage */ 3020bce8354Sdrh }; 3030bce8354Sdrh 3040bce8354Sdrh /* 3050bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 3060bce8354Sdrh */ 3070bce8354Sdrh static void minStep(sqlite_func *context, int argc, const char **argv){ 3080bce8354Sdrh MinMaxCtx *p; 3090bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 3100bce8354Sdrh if( p==0 || argc<1 ) return; 3110bce8354Sdrh if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)<0 ){ 3120bce8354Sdrh if( p->z && p->z!=p->zBuf ){ 3130bce8354Sdrh sqliteFree(p->z); 3140bce8354Sdrh } 3150bce8354Sdrh if( argv[0] ){ 3160bce8354Sdrh int len = strlen(argv[0]); 3170bce8354Sdrh if( len < sizeof(p->zBuf) ){ 3180bce8354Sdrh p->z = p->zBuf; 3190bce8354Sdrh }else{ 3200bce8354Sdrh p->z = sqliteMalloc( len+1 ); 3210bce8354Sdrh if( p->z==0 ) return; 3220bce8354Sdrh } 3230bce8354Sdrh strcpy(p->z, argv[0]); 3240bce8354Sdrh }else{ 3250bce8354Sdrh p->z = 0; 3260bce8354Sdrh } 3270bce8354Sdrh } 3280bce8354Sdrh } 3290bce8354Sdrh static void maxStep(sqlite_func *context, int argc, const char **argv){ 3300bce8354Sdrh MinMaxCtx *p; 3310bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 3320bce8354Sdrh if( p==0 || argc<1 ) return; 3330bce8354Sdrh if( sqlite_aggregate_count(context)==1 || sqliteCompare(argv[0],p->z)>0 ){ 3340bce8354Sdrh if( p->z && p->z!=p->zBuf ){ 3350bce8354Sdrh sqliteFree(p->z); 3360bce8354Sdrh } 3370bce8354Sdrh if( argv[0] ){ 3380bce8354Sdrh int len = strlen(argv[0]); 3390bce8354Sdrh if( len < sizeof(p->zBuf) ){ 3400bce8354Sdrh p->z = p->zBuf; 3410bce8354Sdrh }else{ 3420bce8354Sdrh p->z = sqliteMalloc( len+1 ); 3430bce8354Sdrh if( p->z==0 ) return; 3440bce8354Sdrh } 3450bce8354Sdrh strcpy(p->z, argv[0]); 3460bce8354Sdrh }else{ 3470bce8354Sdrh p->z = 0; 3480bce8354Sdrh } 3490bce8354Sdrh } 3500bce8354Sdrh } 3510bce8354Sdrh static void minMaxFinalize(sqlite_func *context){ 3520bce8354Sdrh MinMaxCtx *p; 3530bce8354Sdrh p = sqlite_aggregate_context(context, sizeof(*p)); 3540bce8354Sdrh if( p && p->z ){ 3550bce8354Sdrh sqlite_set_result_string(context, p->z, strlen(p->z)); 3560bce8354Sdrh } 3570bce8354Sdrh if( p && p->z && p->z!=p->zBuf ){ 3580bce8354Sdrh sqliteFree(p->z); 3590bce8354Sdrh } 3600bce8354Sdrh } 361dd5baa95Sdrh 362d3a149efSdrh /* 363a2ed5601Sdrh ** This function registered all of the above C functions as SQL 364a2ed5601Sdrh ** functions. This should be the only routine in this file with 365a2ed5601Sdrh ** external linkage. 366dc04c583Sdrh */ 367dc04c583Sdrh void sqliteRegisterBuildinFunctions(sqlite *db){ 3680bce8354Sdrh static struct { 3690bce8354Sdrh char *zName; 3700bce8354Sdrh int nArg; 3710bce8354Sdrh void (*xFunc)(sqlite_func*,int,const char**); 3720bce8354Sdrh } aFuncs[] = { 3730bce8354Sdrh { "min", -1, minFunc }, 374fbc99082Sdrh { "min", 0, 0 }, 3750bce8354Sdrh { "max", -1, maxFunc }, 376fbc99082Sdrh { "max", 0, 0 }, 3770bce8354Sdrh { "length", 1, lengthFunc }, 3780bce8354Sdrh { "substr", 3, substrFunc }, 3790bce8354Sdrh { "abs", 1, absFunc }, 3800bce8354Sdrh { "round", 1, roundFunc }, 3810bce8354Sdrh { "round", 2, roundFunc }, 3820bce8354Sdrh { "upper", 1, upperFunc }, 3830bce8354Sdrh { "lower", 1, lowerFunc }, 384fbc99082Sdrh { "coalesce", -1, ifnullFunc }, 385fbc99082Sdrh { "coalesce", 0, 0 }, 386fbc99082Sdrh { "coalesce", 1, 0 }, 387f9ffac96Sdrh { "random", -1, randomFunc }, 3880bce8354Sdrh }; 3890bce8354Sdrh static struct { 3900bce8354Sdrh char *zName; 3910bce8354Sdrh int nArg; 3920bce8354Sdrh void (*xStep)(sqlite_func*,int,const char**); 3930bce8354Sdrh void (*xFinalize)(sqlite_func*); 3940bce8354Sdrh } aAggs[] = { 3950bce8354Sdrh { "min", 1, minStep, minMaxFinalize }, 3960bce8354Sdrh { "max", 1, maxStep, minMaxFinalize }, 3970bce8354Sdrh { "sum", 1, sumStep, sumFinalize }, 3980bce8354Sdrh { "avg", 1, sumStep, avgFinalize }, 3990bce8354Sdrh { "count", 0, countStep, countFinalize }, 4000bce8354Sdrh { "count", 1, countStep, countFinalize }, 401*ef2daf54Sdrh #if 0 4020bce8354Sdrh { "stddev", 1, stdDevStep, stdDevFinalize }, 403*ef2daf54Sdrh #endif 4040bce8354Sdrh }; 4050bce8354Sdrh int i; 4060bce8354Sdrh 4070bce8354Sdrh for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 4080bce8354Sdrh sqlite_create_function(db, aFuncs[i].zName, 4090bce8354Sdrh aFuncs[i].nArg, aFuncs[i].xFunc, 0); 4100bce8354Sdrh } 4110bce8354Sdrh for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ 4120bce8354Sdrh sqlite_create_aggregate(db, aAggs[i].zName, 4130bce8354Sdrh aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0); 4140bce8354Sdrh } 415dc04c583Sdrh } 416