Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Make the max() and min() aggregate functions magnetic. |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
fe083ef8a6923041a161299181db7530 |
User & Date: | dan 2011-07-28 20:19:13 |
Context
2011-07-29
| ||
17:06 | Add missing file error01.test. check-in: e634279337 user: dan tags: trunk | |
2011-07-28
| ||
20:19 | Make the max() and min() aggregate functions magnetic. check-in: fe083ef8a6 user: dan tags: trunk | |
19:32 | Avoid concatenating multiple "syntax error" messages together. check-in: 2dd4975e9c user: dan tags: trunk | |
Changes
Changes to src/func.c.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 .. 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 .. 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 ... 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 ... 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 ... 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 ... 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
** 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() ................................................................................ ** 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 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; ................................................................................ } 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; ................................................................................ } 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; } ................................................................................ ** 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( !p->u.func.apArg ){ return XJD1_NOMEM; } return XJD1_OK; } static int aggExprStep(AggExpr *pAggExpr){ Expr *p = pAggExpr->pExpr; Function *pFunc = p->u.func.pFunction; int i; int nItem = p->u.func.args->nEItem; assert( p->eType==TK_FUNCTION && p->eClass==XJD1_EXPR_FUNC ); assert( pFunc->xStep && pFunc->xFinal && pFunc->xFunc==0 ); for(i=0; i<nItem; i++){ p->u.func.apArg[i] = xjd1ExprEval(p->u.func.args->apEItem[i].pExpr); } pFunc->xStep(nItem, p->u.func.apArg, &pAggExpr->pAggCtx); for(i=0; i<nItem; i++){ xjd1JsonFree(p->u.func.apArg[i]); } return XJD1_OK; } int xjd1AggregateStep(Aggregate *pAgg){ int i; /* Used to iterate through aggregate functions */ for(i=0; i<pAgg->nExpr; i++){ int rc = aggExprStep(&pAgg->aAggExpr[i]); if( rc!=XJD1_OK ) return rc; } return XJD1_OK;; } int xjd1AggregateFinalize(Aggregate *pAgg){ int i; |
| | | > | > | | | | | | > > > | |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 .. 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 .. 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 ... 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 ... 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 ... 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 ... 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
** or aggregate. */ struct Function { int nMinArg; int nMaxArg; const char *zName; JsonNode *(*xFunc)(int nArg, JsonNode **apArg); int (*xStep)(int nArg, JsonNode **apArg, void **, int *pbSave); JsonNode *(*xFinal)(void *); }; /************************************************************************* ** Start of implementation of aggregate functions: ** ** count() ................................................................................ ** 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 *pbSave){ int *pnCount = (int *)*pp; if( pnCount==0 ){ pnCount = xjd1MallocZero(sizeof(int)); *pp = (void *)pnCount; } if( nArg==0 || apArg[0]->eJType!=XJD1_NULL ){ (*pnCount)++; ................................................................................ } return pRet; } /* ** Aggregate functions min() and max(). */ static int xMinStep(int nArg, JsonNode **apArg, void **pp, int *pbSave){ JsonNode *pArg; JsonNode *pBest = *pp; assert( nArg==1 ); pArg = apArg[0]; if( !pBest || xjd1JsonCompare(pArg, pBest)<0 ){ xjd1JsonFree(pBest); *pp = (void *)xjd1JsonRef(pArg); *pbSave = 1; } return XJD1_OK; } static int xMaxStep(int nArg, JsonNode **apArg, void **pp, int *pbSave){ JsonNode *pArg; JsonNode *pBest = *pp; assert( nArg==1 ); pArg = apArg[0]; if( !pBest || xjd1JsonCompare(pArg, pBest)>0 ){ xjd1JsonFree(pBest); *pp = (void *)xjd1JsonRef(pArg); *pbSave = 1; } return XJD1_OK; } static JsonNode *xMinMaxFinal(void *p){ JsonNode *pRet; if( p ){ pRet = (JsonNode *)p; ................................................................................ } return pRet; } /* ** Aggregate function array(). */ static int xArrayStep(int nArg, JsonNode **apArg, void **pp, int *pbSave){ JsonNode *pArray = (JsonNode *)*pp; JsonNode *pArg = apArg[0]; int nNew; if( !pArray ){ pArray = xjd1JsonNew(0); pArray->eJType = XJD1_ARRAY; *pp = (void *)pArray; ................................................................................ } return pArray; } /* ** Aggregate function sum() */ static int xSumStep(int nArg, JsonNode **apArg, void **pp, int *pbSave){ JsonNode *pVal = (JsonNode *)*pp; double rVal = 0.0; if( !pVal ){ pVal = xjd1JsonNew(0); pVal->eJType = XJD1_REAL; *pp = (void *)pVal; } ................................................................................ ** Aggregate function avg() */ typedef struct AvgCtx AvgCtx; struct AvgCtx { int nRow; double rSum; }; static int xAvgStep(int nArg, JsonNode **apArg, void **pp, int *pbSave){ AvgCtx *pCtx = (AvgCtx *)*pp; double rVal = 0.0; if( !pCtx ){ pCtx = xjd1MallocZero(sizeof(AvgCtx)); *pp = (void *)pCtx; } pCtx->nRow++; ................................................................................ if( !p->u.func.apArg ){ return XJD1_NOMEM; } return XJD1_OK; } static int aggExprStep(AggExpr *pAggExpr, int *pbSave){ Expr *p = pAggExpr->pExpr; Function *pFunc = p->u.func.pFunction; int i; int nItem = p->u.func.args->nEItem; assert( p->eType==TK_FUNCTION && p->eClass==XJD1_EXPR_FUNC ); assert( pFunc->xStep && pFunc->xFinal && pFunc->xFunc==0 ); for(i=0; i<nItem; i++){ p->u.func.apArg[i] = xjd1ExprEval(p->u.func.args->apEItem[i].pExpr); } pFunc->xStep(nItem, p->u.func.apArg, &pAggExpr->pAggCtx, pbSave); for(i=0; i<nItem; i++){ xjd1JsonFree(p->u.func.apArg[i]); } return XJD1_OK; } int xjd1AggregateStep( Aggregate *pAgg, int *pbSave /* OUT: True if this row should be saved */ ){ int i; /* Used to iterate through aggregate functions */ for(i=0; i<pAgg->nExpr; i++){ int rc = aggExprStep(&pAgg->aAggExpr[i], pbSave); if( rc!=XJD1_OK ) return rc; } return XJD1_OK;; } int xjd1AggregateFinalize(Aggregate *pAgg){ int i; |
Changes to src/query.c.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 ... 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 ... 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
ResultItem *pNext = pHead->pNext; assert( pNext==0 || cmpResultItem(pHead, pNext, pEList)<=(uniq?-1:0) ); pHead = pNext; } #endif } static void popResultList(ResultList *pList){ ResultItem *pItem; int i; pItem = pList->pItem; pList->pItem = pItem->pNext; for(i=0; i<pList->nKey; i++){ xjd1JsonFree(pItem->apKey[i]); } } static void clearResultList(ResultList *pList){ while( pList->pItem ) popResultList(pList); xjd1PoolDelete(pList->pPool); memset(pList, 0, sizeof(ResultList)); } ................................................................................ ** GROUP BY, so no need to worry about that either. */ assert( p->u.simple.pHaving==0 ); if( p->u.simple.grouped.pPool==0 ){ JsonNode **apSrc; int nSrc; Pool *pPool; pPool = p->u.simple.grouped.pPool = xjd1PoolNew(); if( !pPool ) return XJD1_NOMEM; p->u.simple.grouped.nKey = nSrc = xjd1DataSrcCount(p->u.simple.pFrom); apSrc = (JsonNode **)xjd1PoolMallocZero(pPool, nSrc*sizeof(JsonNode *)); if( !apSrc ) return XJD1_NOMEM; /* Call the xStep() of each aggregate in the query for each row ** matched by the query WHERE clause. */ while( rc==XJD1_OK && XJD1_ROW==(rc = selectStepWhered(p) ) ){ rc = xjd1AggregateStep(pAgg); xjd1DataSrcCacheSave(p->u.simple.pFrom, apSrc); } assert( rc!=XJD1_OK ); if( rc==XJD1_DONE ){ rc = addToResultList(&p->u.simple.grouped, apSrc); } if( rc==XJD1_OK ){ rc = xjd1AggregateFinalize(pAgg); ................................................................................ p->eDocFrom = XJD1_FROM_GROUPED; pItem = p->u.simple.grouped.pItem; if( pItem==0 ){ rc = XJD1_DONE; }else{ while( 1 ){ ResultItem *pNext = pItem->pNext; rc = xjd1AggregateStep(pAgg); if( rc || !pNext || cmpResultItem(pItem, pNext, pGroupBy) ){ break; } popResultList(&p->u.simple.grouped); pItem = p->u.simple.grouped.pItem; } if( XJD1_OK==rc && XJD1_OK==(rc = xjd1AggregateFinalize(pAgg)) ){ rc = XJD1_ROW; } } }while( rc==XJD1_ROW && p->u.simple.pHaving && !xjd1ExprTrue(p->u.simple.pHaving) |
< | > > | > > > > > | < < > > > > > > > > > > > > > > > > > > > > > > > | > | > > > | > > | > |
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 ... 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 ... 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
ResultItem *pNext = pHead->pNext; assert( pNext==0 || cmpResultItem(pHead, pNext, pEList)<=(uniq?-1:0) ); pHead = pNext; } #endif } static void freeResultListItem(ResultList *pList, ResultItem *pItem){ int i; for(i=0; i<pList->nKey; i++){ xjd1JsonFree(pItem->apKey[i]); } } static void popResultList(ResultList *pList){ ResultItem *pItem; pItem = pList->pItem; if( pItem ){ pList->pItem = pItem->pNext; freeResultListItem(pList, pItem); } } /* ** These two are used GROUP BY processing. */ static void saveResultList(ResultList *pList){ if( pList->pSaved ){ freeResultListItem(pList, pList->pSaved); } pList->pSaved = pList->pItem; pList->pItem = pList->pItem->pNext; } static void restoreResultList(ResultList *pList){ if( pList->pSaved ){ popResultList(pList); pList->pSaved->pNext = pList->pItem; pList->pItem = pList->pSaved; pList->pSaved = 0; } } static void clearResultList(ResultList *pList){ while( pList->pItem ) popResultList(pList); xjd1PoolDelete(pList->pPool); memset(pList, 0, sizeof(ResultList)); } ................................................................................ ** GROUP BY, so no need to worry about that either. */ assert( p->u.simple.pHaving==0 ); if( p->u.simple.grouped.pPool==0 ){ JsonNode **apSrc; int nSrc; int saved = 0;; Pool *pPool; pPool = p->u.simple.grouped.pPool = xjd1PoolNew(); if( !pPool ) return XJD1_NOMEM; p->u.simple.grouped.nKey = nSrc = xjd1DataSrcCount(p->u.simple.pFrom); apSrc = (JsonNode **)xjd1PoolMallocZero(pPool, nSrc*sizeof(JsonNode *)); if( !apSrc ) return XJD1_NOMEM; /* Call the xStep() of each aggregate in the query for each row ** matched by the query WHERE clause. */ while( rc==XJD1_OK && XJD1_ROW==(rc = selectStepWhered(p) ) ){ int saveThisRow = 0; rc = xjd1AggregateStep(pAgg, &saveThisRow); if( saved==0 || saveThisRow ){ xjd1DataSrcCacheSave(p->u.simple.pFrom, apSrc); saved = 1; } } assert( rc!=XJD1_OK ); if( rc==XJD1_DONE ){ rc = addToResultList(&p->u.simple.grouped, apSrc); } if( rc==XJD1_OK ){ rc = xjd1AggregateFinalize(pAgg); ................................................................................ p->eDocFrom = XJD1_FROM_GROUPED; pItem = p->u.simple.grouped.pItem; if( pItem==0 ){ rc = XJD1_DONE; }else{ while( 1 ){ int saveThisRow = 0; ResultItem *pNext = pItem->pNext; rc = xjd1AggregateStep(pAgg, &saveThisRow); if( saveThisRow ) saveResultList(&p->u.simple.grouped); if( rc || !pNext || cmpResultItem(pItem, pNext, pGroupBy) ){ restoreResultList(&p->u.simple.grouped); break; } if( !saveThisRow ) popResultList(&p->u.simple.grouped); pItem = p->u.simple.grouped.pItem; } if( XJD1_OK==rc && XJD1_OK==(rc = xjd1AggregateFinalize(pAgg)) ){ rc = XJD1_ROW; } } }while( rc==XJD1_ROW && p->u.simple.pHaving && !xjd1ExprTrue(p->u.simple.pHaving) |
Changes to src/xjd1Int.h.
264
265
266
267
268
269
270
271
272
273
274
275
276
277
...
515
516
517
518
519
520
521
522
523
524
525
526
|
String errMsg; /* Error message string */
};
/* A list of sorted results. */
struct ResultList {
Pool *pPool;
int nKey;
ResultItem *pItem;
};
struct Aggregate {
int nExpr; /* Number of aggregate functions */
struct AggExpr {
Expr *pExpr; /* Array of aggregate functions */
................................................................................
/******************************** func.c *************************************/
int xjd1FunctionInit(Expr *p, xjd1_stmt *pStmt, Query *pQuery, int bAggOk);
JsonNode *xjd1FunctionEval(Expr *p);
void xjd1FunctionClose(Expr *p);
int xjd1AggregateInit(xjd1_stmt *, Query *, Expr *);
int xjd1AggregateStep(Aggregate *);
int xjd1AggregateFinalize(Aggregate *);
void xjd1AggregateClear(Query *);
#endif /* _XJD1INT_H */
|
>
|
|
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
516
517
518
519
520
521
522
523
524
525
526
527
|
String errMsg; /* Error message string */ }; /* A list of sorted results. */ struct ResultList { Pool *pPool; int nKey; ResultItem *pSaved; ResultItem *pItem; }; struct Aggregate { int nExpr; /* Number of aggregate functions */ struct AggExpr { Expr *pExpr; /* Array of aggregate functions */ ................................................................................ /******************************** func.c *************************************/ int xjd1FunctionInit(Expr *p, xjd1_stmt *pStmt, Query *pQuery, int bAggOk); JsonNode *xjd1FunctionEval(Expr *p); void xjd1FunctionClose(Expr *p); int xjd1AggregateInit(xjd1_stmt *, Query *, Expr *); int xjd1AggregateStep(Aggregate *, int *); int xjd1AggregateFinalize(Aggregate *); void xjd1AggregateClear(Query *); #endif /* _XJD1INT_H */ |
Changes to test/base05.test.
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
111
112
113
114
115
116
117
118
|
.testcase 12 SELECT count(c1) FROM c1; SELECT count(c1) FROM c1 WHERE c1.i < 10; .result 17 9 .testcase 13 SELECT {a: count(c1), b : c1} FROM c1; .result {"a":17,"b":{"i":11,"z":"Fig"}} .testcase 14 SELECT count(c1) FROM c1, c1 AS b; SELECT count(c1) FROM c1, c1 AS b WHERE c1.i<3 && b.i<3; SELECT count(c1) FROM c1, c1 AS b WHERE c1.i==1; .result 289 4 17 ................................................................................ .testcase 19 SELECT DISTINCT c2.a FROM c2; .result "a" "b" "c" .testcase 20 SELECT DISTINCT c2.a FROM c2 ORDER BY c2.a DESC; .result "c" "b" "a" |
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
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
|
.testcase 12 SELECT count(c1) FROM c1; SELECT count(c1) FROM c1 WHERE c1.i < 10; .result 17 9 .testcase 13 SELECT {a: count(c1), b : c1} FROM c1; .result {"a":17,"b":{"i":13,"z":"Grape"}} .testcase 14 SELECT count(c1) FROM c1, c1 AS b; SELECT count(c1) FROM c1, c1 AS b WHERE c1.i<3 && b.i<3; SELECT count(c1) FROM c1, c1 AS b WHERE c1.i==1; .result 289 4 17 ................................................................................ .testcase 19 SELECT DISTINCT c2.a FROM c2; .result "a" "b" "c" .testcase 20 SELECT DISTINCT c2.a FROM c2 ORDER BY c2.a DESC; .result "c" "b" "a" -- Test the magneticness of max() and min(). -- CREATE COLLECTION c3; INSERT INTO c3 VALUE { a:2, z:"two" }; INSERT INTO c3 VALUE { a:3, z:"three"}; INSERT INTO c3 VALUE { a:7, z:"seven"}; INSERT INTO c3 VALUE { a:5, z:"five"}; INSERT INTO c3 VALUE { a:1, z:"one"}; INSERT INTO c3 VALUE { a:4, z:"four"}; INSERT INTO c3 VALUE { a:6, z:"six"}; .testcase 21 SELECT { max: max(c3.a), z: c3.z } FROM c3; .json {max:7, z:"seven"} .testcase 22 SELECT { min: min(c3.a), z: c3.z } FROM c3; .json {min:1, z:"one"} .testcase 23 SELECT { min: min(c3.a), z: c3.z } FROM c3 GROUP BY c3.a%2; .json {min:2, z:"two"} {min:1, z:"one"} .testcase 24 SELECT { max: max(c3.a), z: c3.z } FROM c3 GROUP BY c3.a%2; .json {max:6, z:"six"} {max:7, z:"seven"} |