Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add aggregate functions min(), max(), avg(), sum(), and array(). Enhance count() to accept either one or zero arguments. |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
0ef5e83925be9dcecca0c823a101f934 |
User & Date: | dan 2011-07-28 16:00:11 |
Context
2011-07-28
| ||
16:43 | Add optional ASYNCHRONOUS and SYNCHRONOUS keywords in front of INSERT. check-in: 0030b61e78 user: drh tags: trunk | |
16:00 | Add aggregate functions min(), max(), avg(), sum(), and array(). Enhance count() to accept either one or zero arguments. check-in: 0ef5e83925 user: dan tags: trunk | |
15:29 | Fix the UPDATE command so that each changes is independent. check-in: 7b79de2564 user: drh tags: trunk | |
Changes
Changes to src/func.c.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 ... 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 ... 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ************************************************************************* */ #include "xjd1Int.h" struct Function { int nArg; const char *zName; JsonNode *(*xFunc)(int nArg, JsonNode **apArg); int (*xStep)(int nArg, JsonNode **apArg, void **); JsonNode *(*xFinal)(void *); }; /* ** Implementation of scalar function "length(x)". */ static JsonNode *xLength(int nArg, JsonNode **apArg){ JsonNode *pRet; JsonNode *pStr; int nRet; assert( nArg==1 ); pStr = apArg[0]; if( pStr->eJType==XJD1_STRING ){ nRet = xjd1Strlen30(pStr->u.z); }else{ String str; xjd1StringInit(&str, 0, 0); xjd1JsonToString(pStr, &str); nRet = str.nUsed; xjd1StringClear(&str); } pRet = xjd1JsonNew(0); pRet->eJType = XJD1_REAL; pRet->u.r = (double)nRet; return pRet; } static int xCountStep(int nArg, JsonNode **apArg, void **pp){ int nCount = (int)*pp; nCount++; *pp = (void *)nCount; return XJD1_OK; } static JsonNode *xCountFinal(void *p){ JsonNode *pRet; pRet = xjd1JsonNew(0); pRet->eJType = XJD1_REAL; pRet->u.r = (double)(int)p; return pRet; } int xjd1AggregateInit(xjd1_stmt *pStmt, Query *pQuery, Expr *p){ Aggregate *pAgg; assert( pQuery->eQType==TK_SELECT ); pAgg = pQuery->u.simple.pAgg; if( pAgg==0 ){ ................................................................................ */ int xjd1FunctionInit(Expr *p, xjd1_stmt *pStmt, Query *pQuery, int eExpr){ char *zName; int nArg; int nByte; int i; int bAggOk; static Function aFunc[] = { { 1, "length", xLength, 0, 0 }, { -1, "count", 0, xCountStep, xCountFinal }, }; assert( p->eType==TK_FUNCTION && p->eClass==XJD1_EXPR_FUNC ); assert( pQuery || eExpr==0 ); /* Set bAggOk to true if aggregate functions may be used in this context. */ bAggOk = (pQuery && pQuery->eQType==TK_SELECT ................................................................................ zName = p->u.func.zFName; nArg = p->u.func.args->nEItem; for(i=0; i<ArraySize(aFunc); i++){ Function *pFunc = &aFunc[i]; assert( (pFunc->xFunc==0)==(pFunc->xStep && pFunc->xFinal) ); if( !strcmp(pFunc->zName, zName) && (nArg==pFunc->nArg || 0>pFunc->nArg) ){ if( pFunc->xStep ){ int rc; if( bAggOk==0 ){ const char *zErrMsg = "illegal use of aggregate function: %s"; xjd1StmtError(pStmt, XJD1_ERROR, zErrMsg, zName); return XJD1_ERROR; } /* Add this aggregate function to the Query.pAgg->apExpr[] array. */ rc = xjd1AggregateInit(pStmt, pQuery, p); if( rc!=XJD1_OK ) return rc; } p->u.func.pFunction = pFunc; break; } } if( !p->u.func.pFunction ){ xjd1StmtError(pStmt, XJD1_ERROR, "no such function: %s", zName); return XJD1_ERROR; } nByte = sizeof(JsonNode *) * nArg; p->u.func.apArg = (JsonNode **)xjd1PoolMallocZero(&pStmt->sPool, nByte); if( !p->u.func.apArg ){ return XJD1_NOMEM; |
> > > > | > < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | < < > | < < < < < < < < < < < < < > > | > | > > > > > | > | | | | | | | | | | | | > > > > > > > | > |
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 ... 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 ... 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ************************************************************************* */ #include "xjd1Int.h" /* ** A structure used to store the definition of a query language function ** or aggregate. */ struct Function { int nMinArg; int nMaxArg; const char *zName; JsonNode *(*xFunc)(int nArg, JsonNode **apArg); int (*xStep)(int nArg, JsonNode **apArg, void **); JsonNode *(*xFinal)(void *); }; /************************************************************************* ** Start of implementation of aggregate functions: ** ** count() ** This aggregate may be invoked with zero or one argument. If no ** arguments are specified, it returns the number of rows visited. If ** one argument is supplied, it returns the number of rows visited ** for which the argument was anything other than a NULL. In all cases, ** this function returns a non-negative integer value. ** ** min() ** This aggregate is invoked with exactly one argument. If no rows are ** visited, NULL is returned. Otherwise, it returns a copy of the ** smallest argument it is passed. The comparisons used to determine ** which argument is the smallest are performed using the same rules ** as for the < and > operators. ** ** max() ** This aggregate is invoked with exactly one argument. If no rows are ** visited, NULL is returned. Otherwise, it returns a copy of the ** largest argument it is passed. The comparisons used to determine ** which argument is the largest are performed using the same rules ** as for the < and > operators. ** ** array() ** This aggregate is invoked with exactly one argument. If no rows are ** visited, an empty array is returned. Otherwise, an array containing ** the argument passed to this aggregate for each row visited is returned. ** The order in which the values appear in the returned array is not ** defined. ** ** sum() ** This aggregate is invoked with exactly one argument. If no rows are ** visited, the value 0.0 (a number) is returned. Otherwise, each argument ** is converted to a number and the sum of all converted arguments ** returned. Any value that cannot be converted to a number (i.e. a struct, ** an array or any string value that does not look like a number) is ** treated as 0.0. ** ** avg() ** This aggregate is invoked with exactly one argument. If no rows are ** visited, NULL is returned. Otherwise, the expression avg(X) is ** equivalent to (sum(X)/count()). */ /* ** Aggregate function count(). */ static int xCountStep(int nArg, JsonNode **apArg, void **pp){ int *pnCount = (int *)*pp; if( pnCount==0 ){ pnCount = xjd1MallocZero(sizeof(int)); *pp = (void *)pnCount; } if( nArg==0 || apArg[0]->eJType!=XJD1_NULL ){ (*pnCount)++; } return XJD1_OK; } static JsonNode *xCountFinal(void *p){ JsonNode *pRet; pRet = xjd1JsonNew(0); pRet->eJType = XJD1_REAL; if( p ){ pRet->u.r = (double)*(int *)p; xjd1_free(p); } return pRet; } /* ** Aggregate functions min() and max(). */ static int xMinStep(int nArg, JsonNode **apArg, void **pp){ JsonNode *pArg; JsonNode *pBest = *pp; assert( nArg==1 ); pArg = apArg[0]; if( !pBest || xjd1JsonCompare(pArg, pBest)<0 ){ xjd1JsonFree(pBest); *pp = (void *)xjd1JsonRef(pArg); } return XJD1_OK; } static int xMaxStep(int nArg, JsonNode **apArg, void **pp){ JsonNode *pArg; JsonNode *pBest = *pp; assert( nArg==1 ); pArg = apArg[0]; if( !pBest || xjd1JsonCompare(pArg, pBest)>0 ){ xjd1JsonFree(pBest); *pp = (void *)xjd1JsonRef(pArg); } return XJD1_OK; } static JsonNode *xMinMaxFinal(void *p){ JsonNode *pRet; if( p ){ pRet = (JsonNode *)p; }else{ pRet = xjd1JsonNew(0); pRet->eJType = XJD1_NULL; } return pRet; } /* ** Aggregate function array(). */ static int xArrayStep(int nArg, JsonNode **apArg, void **pp){ JsonNode *pArray = (JsonNode *)*pp; JsonNode *pArg = apArg[0]; int nNew; if( !pArray ){ pArray = xjd1JsonNew(0); pArray->eJType = XJD1_ARRAY; *pp = (void *)pArray; } nNew = pArray->u.ar.nElem+1; pArray->u.ar.apElem = (JsonNode **)xjd1_realloc( pArray->u.ar.apElem, nNew*sizeof(JsonNode *) ); pArray->u.ar.nElem = nNew; pArray->u.ar.apElem[nNew-1] = xjd1JsonRef(pArg); return XJD1_OK; } static JsonNode *xArrayFinal(void *p){ JsonNode *pArray = (JsonNode *)p; if( !pArray ){ pArray = xjd1JsonNew(0); pArray->eJType = XJD1_ARRAY; } return pArray; } /* ** Aggregate function sum() */ static int xSumStep(int nArg, JsonNode **apArg, void **pp){ JsonNode *pVal = (JsonNode *)*pp; double rVal = 0.0; if( !pVal ){ pVal = xjd1JsonNew(0); pVal->eJType = XJD1_REAL; *pp = (void *)pVal; } if( XJD1_OK==xjd1JsonToReal(apArg[0], &rVal) ){ pVal->u.r += rVal; } return XJD1_OK; } static JsonNode *xSumFinal(void *p){ JsonNode *pVal = (JsonNode *)p; if( !pVal ){ pVal = xjd1JsonNew(0); pVal->eJType = XJD1_REAL; } return pVal; } /* ** Aggregate function avg() */ typedef struct AvgCtx AvgCtx; struct AvgCtx { int nRow; double rSum; }; static int xAvgStep(int nArg, JsonNode **apArg, void **pp){ AvgCtx *pCtx = (AvgCtx *)*pp; double rVal = 0.0; if( !pCtx ){ pCtx = xjd1MallocZero(sizeof(AvgCtx)); *pp = (void *)pCtx; } pCtx->nRow++; if( XJD1_OK==xjd1JsonToReal(apArg[0], &rVal) ){ pCtx->rSum += rVal; } return XJD1_OK; } static JsonNode *xAvgFinal(void *p){ AvgCtx *pCtx = (AvgCtx *)p; JsonNode *pRet; pRet = xjd1JsonNew(0); if( pCtx ){ pRet->eJType = XJD1_REAL; pRet->u.r = pCtx->rSum/(double)pCtx->nRow; xjd1_free(pCtx); }else{ pRet->eJType = XJD1_NULL; } return pRet; } /* ** End of implementation of aggregates. *************************************************************************/ /************************************************************************* ** Start of implementation of scalar functions: ** ** length() ** This function is called with exactly one argument. It converts ** that argument to a string, and returns the number of characters ** in that string. ** */ /* ** Scalar function "length(x)". */ static JsonNode *xLength(int nArg, JsonNode **apArg){ JsonNode *pRet; JsonNode *pStr; int nRet; assert( nArg==1 ); pStr = apArg[0]; if( pStr->eJType==XJD1_STRING ){ nRet = xjd1Strlen30(pStr->u.z); }else{ String str; xjd1StringInit(&str, 0, 0); xjd1JsonToString(pStr, &str); nRet = str.nUsed; xjd1StringClear(&str); } pRet = xjd1JsonNew(0); pRet->eJType = XJD1_REAL; pRet->u.r = (double)nRet; return pRet; } /* ** End of implementation of scalar functions. *************************************************************************/ int xjd1AggregateInit(xjd1_stmt *pStmt, Query *pQuery, Expr *p){ Aggregate *pAgg; assert( pQuery->eQType==TK_SELECT ); pAgg = pQuery->u.simple.pAgg; if( pAgg==0 ){ ................................................................................ */ int xjd1FunctionInit(Expr *p, xjd1_stmt *pStmt, Query *pQuery, int eExpr){ char *zName; int nArg; int nByte; int i; int bAggOk; int bWrongNumArgs = 0; static Function aFunc[] = { { 1,1, "length", xLength, 0, 0 }, { 0,1, "count", 0, xCountStep, xCountFinal }, { 1,1, "min", 0, xMinStep, xMinMaxFinal }, { 1,1, "max", 0, xMaxStep, xMinMaxFinal }, { 1,1, "array", 0, xArrayStep, xArrayFinal }, { 1,1, "sum", 0, xSumStep, xSumFinal }, { 1,1, "avg", 0, xAvgStep, xAvgFinal }, }; assert( p->eType==TK_FUNCTION && p->eClass==XJD1_EXPR_FUNC ); assert( pQuery || eExpr==0 ); /* Set bAggOk to true if aggregate functions may be used in this context. */ bAggOk = (pQuery && pQuery->eQType==TK_SELECT ................................................................................ zName = p->u.func.zFName; nArg = p->u.func.args->nEItem; for(i=0; i<ArraySize(aFunc); i++){ Function *pFunc = &aFunc[i]; assert( (pFunc->xFunc==0)==(pFunc->xStep && pFunc->xFinal) ); if( !strcmp(pFunc->zName, zName) ){ if( nArg<=pFunc->nMaxArg && nArg>=pFunc->nMinArg ){ if( pFunc->xStep ){ int rc; if( bAggOk==0 ){ const char *zErrMsg = "illegal use of aggregate function: %s"; xjd1StmtError(pStmt, XJD1_ERROR, zErrMsg, zName); return XJD1_ERROR; } /* Add this aggregate function to the Query.pAgg->apExpr[] array. */ rc = xjd1AggregateInit(pStmt, pQuery, p); if( rc!=XJD1_OK ) return rc; } p->u.func.pFunction = pFunc; }else{ bWrongNumArgs = 1; } break; } } if( !p->u.func.pFunction ){ if( bWrongNumArgs ){ xjd1StmtError(pStmt, XJD1_ERROR, "wrong number of arguments to function %s()", zName); }else{ xjd1StmtError(pStmt, XJD1_ERROR, "no such function: %s", zName); } return XJD1_ERROR; } nByte = sizeof(JsonNode *) * nArg; p->u.func.apArg = (JsonNode **)xjd1PoolMallocZero(&pStmt->sPool, nByte); if( !p->u.func.apArg ){ return XJD1_NOMEM; |
Changes to src/parse.y.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
/* Generate an Expr object that is a function call. */ static Expr *funcExpr(Parse *p, Token *pFName, ExprList *pArgs){ Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew)); if( pNew ){ pNew->eType = TK_FUNCTION; pNew->eClass = XJD1_EXPR_FUNC; pNew->u.func.zFName = tokenStr(p, pFName); pNew->u.func.args = pArgs; } return pNew; } /* Generate an Expr object that is a subquery. */ static Expr *subqExpr(Parse *p, Query *pQuery){ |
> > > |
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
/* Generate an Expr object that is a function call. */ static Expr *funcExpr(Parse *p, Token *pFName, ExprList *pArgs){ Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew)); if( pNew ){ pNew->eType = TK_FUNCTION; pNew->eClass = XJD1_EXPR_FUNC; pNew->u.func.zFName = tokenStr(p, pFName); if( pArgs==0 ){ pArgs = xjd1PoolMallocZero(p->pPool, sizeof(ExprList)); } pNew->u.func.args = pArgs; } return pNew; } /* Generate an Expr object that is a subquery. */ static Expr *subqExpr(Parse *p, Query *pQuery){ |
Changes to test/all.test.
4 5 6 7 8 9 10 |
.read base02.test .read base03.test .read base04.test .read base05.test .read base06.test .read base07.test .read base08.test |
> |
4 5 6 7 8 9 10 11 |
.read base02.test
.read base03.test
.read base04.test
.read base05.test
.read base06.test
.read base07.test
.read base08.test
.read base09.test
|
Added test/base09.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 |
-- Test the aggregate functions - avg() -- .new t1.db CREATE COLLECTION c1; INSERT INTO c1 VALUE {a:1, b:6, c:4, d:"abc"}; INSERT INTO c1 VALUE {a:2, b:4, c:3, d:null }; INSERT INTO c1 VALUE {a:3, b:2, c:2, d:0.0 }; INSERT INTO c1 VALUE {a:4, b:8, c:1 }; INSERT INTO c1 VALUE {a:5, b:10, c:5, d:-1.0 }; INSERT INTO c1 VALUE {a:6, b:10, c:5, d:null }; .testcase 1 SELECT count() FROM c1 WHERE c1.a == -1; SELECT count() FROM c1 WHERE c1.a == 1; SELECT count(c1.d) FROM c1 WHERE c1.a<5; .json 0 1 2 .testcase 2 SELECT min(c1.a) FROM c1 WHERE c1.a==-1; SELECT min(c1.a) FROM c1 WHERE c1.a<5; SELECT min(c1.c) FROM c1 WHERE c1.a<5; SELECT min(c1.b) FROM c1 WHERE c1.a<5; SELECT min(c1.d) FROM c1 WHERE c1.a<5; SELECT min(c1.d) FROM c1 WHERE c1.a<6; SELECT min(c1.d) FROM c1 WHERE c1.a<3; .result null 1 1 2 0 -1 null .testcase 3 SELECT max(c1.a) FROM c1 WHERE c1.a==-1; SELECT max(c1.a) FROM c1 WHERE c1.a<5; SELECT max(c1.c) FROM c1 WHERE c1.a<5; SELECT max(c1.b) FROM c1 WHERE c1.a<5; SELECT max(c1.d) FROM c1 WHERE c1.a<5; SELECT max(c1.d) FROM c1 WHERE c1.a<6; SELECT max(c1.d) FROM c1 WHERE c1.a<3; SELECT max(c1.d) FROM c1 WHERE c1.a>4; .result null 4 4 8 "abc" "abc" "abc" null .testcase 4 SELECT array(c1.a) FROM c1 WHERE c1.a==-1; SELECT array(c1.a) FROM c1 WHERE c1.a<7; .json [] [1,2,3,4,5,6] .testcase 5 SELECT sum(c1.a) FROM c1 WHERE c1.a==-1; SELECT sum(c1.a) FROM c1 WHERE c1.a<7; SELECT sum(c1.d) FROM c1 WHERE c1.a<7 && c1.a>4; .json 0 21 -1 .testcase 6 SELECT avg(c1.a) FROM c1 WHERE c1.a==-1; SELECT avg(c1.a) FROM c1 WHERE c1.a<7; SELECT avg(c1.d) FROM c1 WHERE c1.a<7 && c1.a>1; .json null 3.5 -0.2 .testcase 7 SELECT count(c1.a,c1.b) FROM c1; .error ERROR wrong number of arguments to function count() .testcase 8 SELECT array() FROM c1; .error ERROR wrong number of arguments to function array() .testcase 9 SELECT array(c1.a,c1.b) FROM c1; .error ERROR wrong number of arguments to function array() .testcase 10 SELECT max() FROM c1; .error ERROR wrong number of arguments to function max() .testcase 11 SELECT max(c1.a,c1.b) FROM c1; .error ERROR wrong number of arguments to function max() |