/ Check-in [21981097]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added a "stddev()" aggregate function for testing the new user aggregate function interface. (CVS 393)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:2198109712ccf988f93db5740a4f248e80fb9f5d
User & Date: drh 2002-02-24 17:12:54
Context
2002-02-26
23:24
Fix a bug in the quoting of .dump output. By Rajit Singh. (CVS 394) check-in: f2310f36 user: drh tags: trunk
2002-02-24
17:12
Added a "stddev()" aggregate function for testing the new user aggregate function interface. (CVS 393) check-in: 21981097 user: drh tags: trunk
03:25
Code for user-defined aggregates added. Legacy tests all pass but there has been no testing of the new user-defined aggregate code. (CVS 392) check-in: 1e037eb3 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/func.c.

12
13
14
15
16
17
18
19
20
21


22
23
24
25
26
27
28
..
40
41
42
43
44
45
46










































47
48
49
50
51
52
53
54

55
** This file contains the C functions that implement various SQL
** functions of SQLite.  
**
** There is only one exported symbol in this file - the function
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.1 2002/02/24 01:55:17 drh Exp $
*/
#include <ctype.h>


#include "sqlite.h"

/*
** Implementation of the upper() and lower() SQL functions.
*/
static void upperFunc(void *context, int argc, const char **argv){
  char *z;
................................................................................
  if( argc<1 || argv[0]==0 ) return;
  z = sqlite_set_result_string(context, argv[0], -1);
  if( z==0 ) return;
  for(i=0; z[i]; i++){
    if( isupper(z[i]) ) z[i] = tolower(z[i]);
  }
}











































/*
** This file registered all of the above C functions as SQL
** functions.
*/
void sqliteRegisterBuildinFunctions(sqlite *db){
  sqlite_create_function(db, "upper", 1, upperFunc);
  sqlite_create_function(db, "lower", 1, lowerFunc);

}







|


>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








>

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
** This file contains the C functions that implement various SQL
** functions of SQLite.  
**
** There is only one exported symbol in this file - the function
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.2 2002/02/24 17:12:54 drh Exp $
*/
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include "sqlite.h"

/*
** Implementation of the upper() and lower() SQL functions.
*/
static void upperFunc(void *context, int argc, const char **argv){
  char *z;
................................................................................
  if( argc<1 || argv[0]==0 ) return;
  z = sqlite_set_result_string(context, argv[0], -1);
  if( z==0 ) return;
  for(i=0; z[i]; i++){
    if( isupper(z[i]) ) z[i] = tolower(z[i]);
  }
}

/*
** An instance of the following structure holds the context of a
** standard deviation computation.
*/
typedef struct StdDevCtx StdDevCtx;
struct StdDevCtx {
  double sum;     /* Sum of terms */
  double sum2;    /* Sum of the squares of terms */
  int n;          /* Number of terms seen so far */
};

/*
** Routines used to compute the standard deviation as an aggregate.
*/
static void *stdDevStep(void *stddev, int argc, char **argv){
  StdDevCtx *p;
  double x;
  if( argc<1 ) return 0;
  if( stddev==0 ){
    p = malloc( sizeof(*p) );
    p->n = 0;
    p->sum = 0.0;
    p->sum2 = 0.0;
  }else{
    p = (StdDevCtx*)stddev;
  }
  x = atof(argv[0]);
  p->sum += x;
  p->sum2 += x*x;
  p->n++;
  return p;
}
static void stdDevFinalize(void *stddev, void *context){
  StdDevCtx *p = (StdDevCtx*)stddev;
  if( context && p && p->n>1 ){
    double rN = p->n;
    sqlite_set_result_double(context, 
       sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
  }
  if( stddev ) free(stddev);
}

/*
** This file registered all of the above C functions as SQL
** functions.
*/
void sqliteRegisterBuildinFunctions(sqlite *db){
  sqlite_create_function(db, "upper", 1, upperFunc);
  sqlite_create_function(db, "lower", 1, lowerFunc);
  sqlite_create_aggregate(db, "stddev", 1, stdDevStep, stdDevFinalize);
}