UnQL

Check-in [dbbf1a25b5]
Login

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

Overview
Comment:Change the way OFFSET, LIMIT and ORDER BY are handled internally.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: dbbf1a25b5b95fe365ffcb63d98ff657068e243e
User & Date: dan 2011-07-23 11:52:47
Context
2011-07-23
14:58
Allow an "AS <id>" alias following the result expression of a SELECT. check-in: 0ae879da94 user: dan tags: trunk
11:52
Change the way OFFSET, LIMIT and ORDER BY are handled internally. check-in: dbbf1a25b5 user: dan tags: trunk
2011-07-22
19:12
Add support for UNION, EXCEPT, INTERSECT. Doesn't work properly with ORDER BY yet. check-in: 2d74ebd10f user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/func.c.

67
68
69
70
71
72
73
74
75


76
77
78
79
80
81
82
83
84
85
86
...
208
209
210
211
212
213
214
215
216

217
218
219
220
221
222
223
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  pRet->eJType = XJD1_REAL;
  pRet->u.r = (double)(int)p;
  return pRet;
}


int xjd1AggregateInit(xjd1_stmt *pStmt, Query *pQuery, Expr *p){
  Aggregate *pAgg = pQuery->pAgg;



  if( pAgg==0 ){
    pAgg = (Aggregate *)xjd1PoolMallocZero(&pStmt->sPool, sizeof(Aggregate));
    if( pAgg==0 ) return XJD1_NOMEM;
    pQuery->pAgg = pAgg;
  }

  if( p ){
    static const int ARRAY_ALLOC_INCR = 8;

    if( (pAgg->nExpr % ARRAY_ALLOC_INCR)==0 ){
      int nByte;                    /* Size of new allocation in bytes */
................................................................................

/*
** Call any outstanding xFinal() functions for aggregate functions in the
** query. This is required to reset the aggregate contexts when a query is 
** rewound following an error.
*/
void xjd1AggregateClear(Query *pQuery){
  Aggregate *pAgg = pQuery->pAgg;


  if( pAgg ){
    int i;
    for(i=0; i<pAgg->nExpr; i++){
      AggExpr *pAggExpr = &pAgg->aAggExpr[i];
      Expr *p = pAggExpr->pExpr;

      xjd1JsonFree( p->u.func.pFunction->xFinal(pAggExpr->pAggCtx) );
................................................................................
    }

    pRet = pFunc->xFunc(nItem, p->u.func.apArg);
    for(i=0; i<nItem; i++){
      xjd1JsonFree(p->u.func.apArg[i]);
    }
  }else{
    AggExpr *pAggExpr = &p->pQuery->pAgg->aAggExpr[p->u.func.iAgg];
    pRet = xjd1JsonRef(pAggExpr->pValue);
  }

  return pRet;
}








|
|
>
>



|







 







|

>







 







|






67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  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 ){
    pAgg = (Aggregate *)xjd1PoolMallocZero(&pStmt->sPool, sizeof(Aggregate));
    if( pAgg==0 ) return XJD1_NOMEM;
    pQuery->u.simple.pAgg = pAgg;
  }

  if( p ){
    static const int ARRAY_ALLOC_INCR = 8;

    if( (pAgg->nExpr % ARRAY_ALLOC_INCR)==0 ){
      int nByte;                    /* Size of new allocation in bytes */
................................................................................

/*
** Call any outstanding xFinal() functions for aggregate functions in the
** query. This is required to reset the aggregate contexts when a query is 
** rewound following an error.
*/
void xjd1AggregateClear(Query *pQuery){
  Aggregate *pAgg = pQuery->u.simple.pAgg;

  assert( pQuery->eQType==TK_SELECT );
  if( pAgg ){
    int i;
    for(i=0; i<pAgg->nExpr; i++){
      AggExpr *pAggExpr = &pAgg->aAggExpr[i];
      Expr *p = pAggExpr->pExpr;

      xjd1JsonFree( p->u.func.pFunction->xFinal(pAggExpr->pAggCtx) );
................................................................................
    }

    pRet = pFunc->xFunc(nItem, p->u.func.apArg);
    for(i=0; i<nItem; i++){
      xjd1JsonFree(p->u.func.apArg[i]);
    }
  }else{
    AggExpr *pAggExpr = &p->pQuery->u.simple.pAgg->aAggExpr[p->u.func.iAgg];
    pRet = xjd1JsonRef(pAggExpr->pValue);
  }

  return pRet;
}

Changes to src/parse.y.

334
335
336
337
338
339
340


341
342
343
344
345
346
347
348
349
...
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
386
...
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
  }
  A = pNew;
}

%left UNION EXCEPT.
%left INTERSECT.
%type select {Query*}


%type eselect {Query*}
%type oneselect {Query*}

%include {
  /* The value of a LIMIT ... OFFSET ... clause. */
  typedef struct LimitOffset {
    Expr *pLimit;
    Expr *pOffset;
  } LimitOffset;
................................................................................
  /* Construct a simple query object */
  static Query *simpleQuery(
    Parse *p,
    int isDistinct,
    Expr *pRes,
    DataSrc *pFrom,
    Expr *pWhere,
    GroupByHaving *pGroupBy,
    ExprList *pOrderBy,
    LimitOffset *pLimit
  ){
    Query *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));
    if( pNew ){
      pNew->eQType = TK_SELECT;
      pNew->u.simple.isDistinct = isDistinct;
      pNew->u.simple.pRes = pRes;
      pNew->u.simple.pFrom = pFrom;
      pNew->u.simple.pWhere = pWhere;
      pNew->u.simple.pGroupBy = pGroupBy ? pGroupBy->pGroupBy : 0;
      pNew->u.simple.pHaving = pGroupBy ? pGroupBy->pHaving : 0;
      pNew->u.simple.pOrderBy = pOrderBy;
      pNew->u.simple.pLimit = pLimit ? pLimit->pLimit : 0;
      pNew->u.simple.pOffset = pLimit ? pLimit->pOffset : 0;
    }
    return pNew;
  }

  /* Construct a compound query object */
  static Query *compoundQuery(
    Parse *p,
................................................................................
      pNew->u.compound.pRight = pRight;
    }
    return pNew;
  }
}


select(A) ::= oneselect(X).                        {A = X;}








select(A) ::= eselect(X) UNION(OP) eselect(Y).     {A=compoundQuery(p,X,@OP,Y);}
select(A) ::= eselect(X) UNION ALL(OP) eselect(Y). {A=compoundQuery(p,X,@OP,Y);}
select(A) ::= eselect(X) EXCEPT(OP) eselect(Y).    {A=compoundQuery(p,X,@OP,Y);}
select(A) ::= eselect(X) INTERSECT(OP) eselect(Y). {A=compoundQuery(p,X,@OP,Y);}
eselect(A) ::= select(X).                          {A = X;}
eselect(A) ::= LP select(X) RP.                    {A = X;}













oneselect(A) ::= SELECT distinct_opt(D) expr_opt(S) from(F) where_opt(W)
                    groupby_opt(G) orderby_opt(O) limit_opt(L).
  {A = simpleQuery(p,D,S,F,W,&G,O,&L);}


%type distinct_opt {int}
distinct_opt(A) ::= .                    {A = 0;}
distinct_opt(A) ::= DISTINCT.            {A = 1;}
distinct_opt(A) ::= ALL.                 {A = 0;}

// The result set of an expression can be either an JSON expression
// or nothing.
//
%type expr_opt {Expr*}
expr_opt(A) ::= .                       {A = 0;}
expr_opt(A) ::= expr(X).                {A = X;}







>
>
|
|







 







|
<
<










<
<
<







 







<
>
>
>
>
>
>
>
>
|
|
|
|
|
<
>
>
>
>
>
>
>
>
|
>
>
>
>
|
<
|


|
|
|
|







334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
...
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
...
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
428
429
430
431
432
433
434
435
436
437
438
439
  }
  A = pNew;
}

%left UNION EXCEPT.
%left INTERSECT.
%type select {Query*}
%type selectcore {Query*}
%type compound {Query*}
%type esel {Query*}
%type x {Query*}

%include {
  /* The value of a LIMIT ... OFFSET ... clause. */
  typedef struct LimitOffset {
    Expr *pLimit;
    Expr *pOffset;
  } LimitOffset;
................................................................................
  /* Construct a simple query object */
  static Query *simpleQuery(
    Parse *p,
    int isDistinct,
    Expr *pRes,
    DataSrc *pFrom,
    Expr *pWhere,
    GroupByHaving *pGroupBy


  ){
    Query *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));
    if( pNew ){
      pNew->eQType = TK_SELECT;
      pNew->u.simple.isDistinct = isDistinct;
      pNew->u.simple.pRes = pRes;
      pNew->u.simple.pFrom = pFrom;
      pNew->u.simple.pWhere = pWhere;
      pNew->u.simple.pGroupBy = pGroupBy ? pGroupBy->pGroupBy : 0;
      pNew->u.simple.pHaving = pGroupBy ? pGroupBy->pHaving : 0;



    }
    return pNew;
  }

  /* Construct a compound query object */
  static Query *compoundQuery(
    Parse *p,
................................................................................
      pNew->u.compound.pRight = pRight;
    }
    return pNew;
  }
}




select(A) ::= compound(Q) orderby_opt(O) limit_opt(L). {
  Q->pOrderBy = O;
  Q->pLimit = L.pLimit;
  Q->pOffset = L.pOffset;
  A = Q;
}
compound(A) ::= selectcore(X).                 {A = X;}
compound(A) ::= esel(X) UNION(OP) esel(Y).     {A=compoundQuery(p,X,@OP,Y);}
compound(A) ::= esel(X) UNION ALL(OP) esel(Y). {A=compoundQuery(p,X,@OP,Y);}
compound(A) ::= esel(X) EXCEPT(OP) esel(Y).    {A=compoundQuery(p,X,@OP,Y);}
compound(A) ::= esel(X) INTERSECT(OP) esel(Y). {A=compoundQuery(p,X,@OP,Y);}
esel(A) ::= compound(X).                       {A = X;}

esel(A) ::= expr(X). {
  /* TODO: Fix these error messages */
  if( X->eClass!=XJD1_EXPR_Q ){
    xjd1ParseError(p, XJD1_SYNTAX, "syntax error");
  }else{
    A = X->u.subq.p;
    if( A->pOrderBy || A->pLimit ){
      xjd1ParseError(p, XJD1_SYNTAX, "syntax error");
    }
  }
}

selectcore(A) ::= SELECT 
  dist_opt(D) expr_opt(S) from(F) where_opt(W) groupby_opt(G).

  {A = simpleQuery(p,D,S,F,W,&G);}


%type dist_opt {int}
dist_opt(A) ::= .                    {A = 0;}
dist_opt(A) ::= DISTINCT.            {A = 1;}
dist_opt(A) ::= ALL.                 {A = 0;}

// The result set of an expression can be either an JSON expression
// or nothing.
//
%type expr_opt {Expr*}
expr_opt(A) ::= .                       {A = 0;}
expr_opt(A) ::= expr(X).                {A = X;}

Changes to src/query.c.

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
...
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
...
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
...
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
...
433
434
435
436
437
438
439


440
441
442
443


444

445
446
447


448

449
450

451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514

515
516
517
518
519
520
521
...
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
...
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
...
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
...
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708

709
710
711
712
713
714
715
...
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747



748
749
750
  pQuery->pOuter = pOuter;
  if( pQuery->eQType==TK_SELECT ){
    rc = xjd1ExprInit(pQuery->u.simple.pRes, pStmt, pQuery, 1);
    if( !rc ) rc = xjd1DataSrcInit(pQuery->u.simple.pFrom, pQuery);
    if( !rc ) rc = xjd1ExprInit(pQuery->u.simple.pWhere, pStmt, pQuery, 0);
    if( !rc ) rc = xjd1ExprListInit(pQuery->u.simple.pGroupBy, pStmt, pQuery,0);
    if( !rc ) rc = xjd1ExprInit(pQuery->u.simple.pHaving, pStmt, pQuery, 1);
    if( !rc ) rc = xjd1ExprListInit(pQuery->u.simple.pOrderBy, pStmt, pQuery,1);
    if( !rc ) rc = xjd1ExprInit(pQuery->u.simple.pLimit, pStmt, pQuery, 0);
    if( !rc ) rc = xjd1ExprInit(pQuery->u.simple.pOffset, pStmt, pQuery, 0);
    if( !rc && pQuery->u.simple.pGroupBy ){ 
      rc = xjd1AggregateInit(pStmt, pQuery, 0);
    }
  }else{
    rc = xjd1QueryInit(pQuery->u.compound.pLeft, pStmt, pOuter);
    if( !rc ) rc = xjd1QueryInit(pQuery->u.compound.pRight, pStmt, pOuter);
  }




  return rc;
}

/*
** Rewind a query so that it is pointing at the first row.
*/
int xjd1QueryRewind(Query *p){
  if( p==0 ) return XJD1_OK;
  if( p->eQType==TK_SELECT ){
    xjd1DataSrcRewind(p->u.simple.pFrom);
    p->eDocFrom = XJD1_FROM_DATASRC;
    p->bLimitValid = 0;
    clearResultList(&p->ordered);
    clearResultList(&p->grouped);
    clearResultList(&p->distincted);
    xjd1AggregateClear(p);
  }else{
    xjd1QueryRewind(p->u.compound.pLeft);
    xjd1QueryRewind(p->u.compound.pRight);
    p->u.compound.doneLeft = 0;
    clearResultList(&p->u.compound.left);
    clearResultList(&p->u.compound.right);
    xjd1JsonFree(p->u.compound.pOut);
    p->u.compound.pOut = 0;
  }



  return XJD1_OK;
}

/*
** Advance to the next row of the TK_SELECT query passed as the first 
** argument, disregarding any ORDER BY, OFFSET or LIMIT clause.
**
................................................................................
    rc==XJD1_ROW
    && (p->u.simple.pWhere!=0 && !xjd1ExprTrue(p->u.simple.pWhere))
  );
  return rc;
}

static int selectStepGrouped(Query *p){

  int rc;

  if( p->pAgg ){
    Aggregate *pAgg = p->pAgg;
    ExprList *pGroupBy = p->u.simple.pGroupBy;

    rc = XJD1_OK;
    if( pGroupBy==0 ){
      /* An aggregate query with no GROUP BY clause. If there is no GROUP BY,
      ** then exactly one row is returned, which makes ORDER BY and DISTINCT 
      ** no-ops. And it is not possible to have a HAVING clause without a
      ** GROUP BY, so no need to worry about that either. 
      */
      assert( p->u.simple.pHaving==0 );

      if( p->grouped.pPool==0 ){
        JsonNode **apSrc;
        int nSrc;
        Pool *pPool;

        pPool = p->grouped.pPool = xjd1PoolNew();
        if( !pPool ) return XJD1_NOMEM;

        p->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->grouped, apSrc);
        }
        if( rc==XJD1_OK ){
          rc = xjd1AggregateFinalize(pAgg);
        }
        if( rc==XJD1_OK ){
          rc = XJD1_ROW;
          p->eDocFrom = XJD1_FROM_GROUPED;
................................................................................
      ** Use a ResultList to sort all the rows matched by the WHERE
      ** clause. The apKey[] array consists of each of the expressions
      ** in the GROUP BY clause, followed by each document in the FROM
      ** clause. */
      do {
        ResultItem *pItem;
  
        if( p->grouped.pPool==0 ){
          DataSrc *pFrom = p->u.simple.pFrom;
          JsonNode **apKey;
          int nByte;
          Pool *pPool;
  
          /* Allocate the memory pool for this ResultList. And apKey. */
          pPool = p->grouped.pPool = xjd1PoolNew();
          p->grouped.nKey = pGroupBy->nEItem + xjd1DataSrcCount(pFrom);
          if( !pPool ) return XJD1_NOMEM;
          nByte = p->grouped.nKey * sizeof(JsonNode *);
          apKey = (JsonNode **)xjd1PoolMallocZero(pPool, nByte);
          if( !apKey ) return XJD1_NOMEM;
  
          while( rc==XJD1_OK && XJD1_ROW==(rc = selectStepWhered(p) ) ){
            int i;
            for(i=0; i<pGroupBy->nEItem; i++){
              apKey[i] = xjd1ExprEval(pGroupBy->apEItem[i].pExpr);
            }
            xjd1DataSrcCacheSave(pFrom, &apKey[i]);
            rc = addToResultList(&p->grouped, apKey);
            memset(apKey, 0, nByte);
          }
          if( rc!=XJD1_DONE ) return rc;
          sortResultList(&p->grouped, pGroupBy, 0);
        }else{
          popResultList(&p->grouped);
        }
  
        p->eDocFrom = XJD1_FROM_GROUPED;
        pItem = p->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->grouped);
            pItem = p->grouped.pItem;
          }
          if( XJD1_OK==rc && XJD1_OK==(rc = xjd1AggregateFinalize(pAgg)) ){
            rc = XJD1_ROW;
          }
        }

      }while( rc==XJD1_ROW 
................................................................................

  return rc;
}

static int selectStepDistinct(Query *p){
  int rc;
  if( p->u.simple.isDistinct ){
    if( p->distincted.pPool==0 ){
      Pool *pPool;
      JsonNode **apKey;
      int nKey;

      nKey = 1 + xjd1DataSrcCount(p->u.simple.pFrom);
      pPool = p->distincted.pPool = xjd1PoolNew();
      if( !pPool ) return XJD1_NOMEM;
      p->distincted.nKey = nKey;
      apKey = xjd1PoolMallocZero(pPool, nKey * sizeof(JsonNode *));
      if( !apKey ) return XJD1_NOMEM;

      while( XJD1_ROW==(rc = selectStepGrouped(p) ) ){
        apKey[0] = xjd1QueryDoc(p, 0);
        xjd1DataSrcCacheSave(p->u.simple.pFrom, &apKey[1]);
        rc = addToResultList(&p->distincted, apKey);
        memset(apKey, 0, nKey * sizeof(JsonNode *));
        if( rc!=XJD1_OK ) break;
      }
      if( rc==XJD1_DONE ){
        sortResultList(&p->distincted, 0, 1);
        p->eDocFrom = XJD1_FROM_DISTINCTED;
        rc = XJD1_ROW;
      }
    }else{
      JsonNode *pPrev = xjd1JsonRef(p->distincted.pItem->apKey[0]);
      do{
        popResultList(&p->distincted);
      }while( p->distincted.pItem 
           && 0==xjd1JsonCompare(pPrev, p->distincted.pItem->apKey[0])
      );
      rc = p->distincted.pItem ? XJD1_ROW : XJD1_DONE;
    }
  }else{
    rc = selectStepGrouped(p);


  }
  return rc;
................................................................................
}


static int selectStepCompounded(Query *p){
  int rc;
  if( p->eQType==TK_SELECT ){
    rc = selectStepDistinct(p);


  }else if( p->eQType==TK_ALL ){
    rc = XJD1_DONE;
    if( p->u.compound.doneLeft==0 ){
      rc = selectStepCompounded(p->u.compound.pLeft);


    }

    if( rc==XJD1_DONE ){
      p->u.compound.doneLeft = 1;
      rc = selectStepCompounded(p->u.compound.pRight);


    }

  }else{
    JsonNode *pOut = 0;

    ResultList *p1;
    ResultList *p2;

    if( p->u.compound.left.pPool==0 ){
      rc = cacheQuery(&p->u.compound.left, p->u.compound.pLeft);
      if( rc!=XJD1_OK ) return rc;
      rc = cacheQuery(&p->u.compound.right, p->u.compound.pRight);
      if( rc!=XJD1_OK ) return rc;
    }

    p1 = &p->u.compound.left;
    p2 = &p->u.compound.right;
    rc = XJD1_DONE;

    switch( p->eQType ){
      case TK_UNION: {
        if( (p1->pItem || p2->pItem) ){
          int c;
          if( p1->pItem==0 ){
            c = 1;
          }else if( p2->pItem==0 ){
            c = -1;
          }else{
            c = cmpResultItem(p1->pItem, p2->pItem, 0);
          }
          
          if( c<0 ){
            pOut = xjd1JsonRef(p1->pItem->apKey[0]);
          }else{
            pOut = xjd1JsonRef(p2->pItem->apKey[0]);
          }
          if( c<=0 ) popResultList(p1);
          if( c>=0 ) popResultList(p2);
          rc = XJD1_ROW;
        }
        break;
      }

      case TK_INTERSECT:
        while( rc==XJD1_DONE && p1->pItem && p2->pItem ){
          int c = cmpResultItem(p1->pItem, p2->pItem, 0);
          if( c==0 ){
            pOut = xjd1JsonRef(p1->pItem->apKey[0]);
            rc = XJD1_ROW;
          }
          if( c<=0 ) popResultList(p1);
          if( c>=0 ) popResultList(p2);
        }
        break;

      case TK_EXCEPT:
        while( rc==XJD1_DONE && p1->pItem ){
          int c = p2->pItem ? cmpResultItem(p1->pItem, p2->pItem, 0) : -1;
          if( c<=0 ){
            if( c ){
              pOut = xjd1JsonRef(p1->pItem->apKey[0]);
              rc = XJD1_ROW;
            }
            popResultList(p1);
          }else{
            popResultList(p2);
          }
        }
        break;

    }

    xjd1JsonFree(p->u.compound.pOut);
    p->u.compound.pOut = pOut;
  }
  return rc;
}
................................................................................
** Advance to the next row of the TK_SELECT query passed as the first 
** argument, disregarding any OFFSET or LIMIT clause.
**
** Return XJD1_ROW if there is such a row, or XJD1_EOF if there is not. Or 
** return an error code if an error occurs.
*/
static int selectStepOrdered(Query *p){
  int rc = XJD1_OK;               /* Return Code */
  ExprList *pOrderBy;             /* ORDER BY clause (or NULL) */

  /* Find the ORDER BY clause, if any */
  Query *pRight;
  for(pRight=p; pRight->eQType!=TK_SELECT; pRight=pRight->u.compound.pRight);
  pOrderBy = pRight->u.simple.pOrderBy;

  if( pOrderBy ){

    /* TODO: Fix this hack. */
    extern void xjd1SetExprListQuery(ExprList *, Query *);
    xjd1SetExprListQuery(pOrderBy, p);

................................................................................

  if( p->bLimitValid==0 ){
    /* Calculate the values, if any, of the LIMIT and OFFSET clauses.
    **
    ** TBD: Should this throw an exception if the result of evaluating
    ** either of these clauses cannot be converted to a number?
    */
    Expr *pLimit;                 /* The LIMIT expression, or NULL */
    Expr *pOffset;                /* The OFFSET expression, or NULL */

    Query *pRight;
    for(pRight=p; pRight->eQType!=TK_SELECT; pRight=pRight->u.compound.pRight);
    pLimit = pRight->u.simple.pLimit;
    pOffset = pRight->u.simple.pOffset;

    if( pOffset ){
      JsonNode *pVal;             /* Result of evaluating expression pOffset */
      double rOffset;             /* pVal converted to a number */

      pVal = xjd1ExprEval(pOffset);
      if( 0==xjd1JsonToReal(pVal, &rOffset) ){
................................................................................
          if( rc!=XJD1_ROW ) break;
        }
      }
      xjd1JsonFree(pVal);
    }

    p->nLimit = -1;
    if( p->u.simple.pLimit ){
      double rLimit;
      JsonNode *pVal;

      pVal = xjd1ExprEval(pLimit);
      if( 0==xjd1JsonToReal(pVal, &rLimit) ){
        p->nLimit = (int)rLimit;
      }
................................................................................
        case XJD1_FROM_ORDERED:
          assert( zDocName==0 && p->ordered.pItem );
          pOut = xjd1JsonRef(p->ordered.pItem->apKey[p->ordered.nKey-1]);
          break;

        case XJD1_FROM_DISTINCTED:
          if( zDocName==0 ){
            pOut = xjd1JsonRef(p->distincted.pItem->apKey[0]);
          }else{
            JsonNode **apSrc = &p->distincted.pItem->apKey[1];
            pOut = xjd1DataSrcCacheRead(p->u.simple.pFrom, apSrc, zDocName);
          }
          break;

        case XJD1_FROM_GROUPED:
          if( zDocName==0 && p->u.simple.pRes ){
            pOut = xjd1ExprEval(p->u.simple.pRes);
          }else{
            JsonNode **apSrc = p->grouped.pItem->apKey;
            if( p->u.simple.pGroupBy ){
              apSrc = &apSrc[p->u.simple.pGroupBy->nEItem];
            }
            pOut = xjd1DataSrcCacheRead(p->u.simple.pFrom, apSrc, zDocName);
          }
          break;

................................................................................
          if( zDocName==0 && p->u.simple.pRes ){
            pOut = xjd1ExprEval(p->u.simple.pRes);
          }else{
            pOut = xjd1DataSrcDoc(p->u.simple.pFrom, zDocName);
          }
          break;
      }
    }else if( p->eQType==TK_ALL ){
      if( p->eDocFrom==XJD1_FROM_ORDERED ){
        assert( zDocName==0 && p->ordered.pItem );
        pOut = xjd1JsonRef(p->ordered.pItem->apKey[p->ordered.nKey-1]);
      }else if( 0==p->u.compound.doneLeft ){
        pOut = xjd1QueryDoc(p->u.compound.pLeft, zDocName);
      }else{
        pOut = xjd1QueryDoc(p->u.compound.pRight, zDocName);
      }
    }else{
      pOut = xjd1JsonRef(p->u.compound.pOut);

    }

    /* If no document has been found and this is a sub-query, search the 
    ** parent query for a document of the specified name. 
    */
    if( pOut==0 && zDocName ){
      pOut = xjd1QueryDoc(p->pOuter, zDocName);
................................................................................
** The destructor for a Query object.
*/
int xjd1QueryClose(Query *pQuery){
  int rc = XJD1_OK;
  if( pQuery==0 ) return rc;
  if( pQuery->eQType==TK_SELECT ){
    clearResultList(&pQuery->ordered);
    clearResultList(&pQuery->grouped);
    clearResultList(&pQuery->distincted);
    xjd1ExprClose(pQuery->u.simple.pRes);
    xjd1DataSrcClose(pQuery->u.simple.pFrom);
    xjd1ExprClose(pQuery->u.simple.pWhere);
    xjd1ExprListClose(pQuery->u.simple.pGroupBy);
    xjd1ExprClose(pQuery->u.simple.pHaving);
    xjd1ExprListClose(pQuery->u.simple.pOrderBy);
    xjd1ExprClose(pQuery->u.simple.pLimit);
    xjd1ExprClose(pQuery->u.simple.pOffset);
  }else{
    xjd1QueryClose(pQuery->u.compound.pLeft);
    xjd1QueryClose(pQuery->u.compound.pRight);
    clearResultList(&pQuery->u.compound.left);
    clearResultList(&pQuery->u.compound.right);
    xjd1JsonFree(pQuery->u.compound.pOut);
    pQuery->u.compound.pOut = 0;
  }



  return rc;
}








<
<
<







>
>
>
>










<
<
|
|
<










>
>
>







 







>


|
<











|




|


|











|







 







|






|
|

|









|



|

|



|









|
|







 







|





|

|






|




|




<
<
|
<
<
<
|







 







>
>
|
|
|
|
>
>
|
>
|
|
|
>
>
|
>
|
<
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>







 







|
|
<
<
<
<
<







 







|
|
<
<
<
<
<







 







|







 







|

|








|







 







|



<
<

<
<
<
|
>







 







|
|





<
<
<








>
>
>



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
...
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
281
...
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
...
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396


397



398
399
400
401
402
403
404
405
...
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
...
529
530
531
532
533
534
535
536
537





538
539
540
541
542
543
544
...
597
598
599
600
601
602
603
604
605





606
607
608
609
610
611
612
...
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
...
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
...
686
687
688
689
690
691
692
693
694
695
696


697



698
699
700
701
702
703
704
705
706
...
714
715
716
717
718
719
720
721
722
723
724
725
726
727



728
729
730
731
732
733
734
735
736
737
738
739
740
741
  pQuery->pOuter = pOuter;
  if( pQuery->eQType==TK_SELECT ){
    rc = xjd1ExprInit(pQuery->u.simple.pRes, pStmt, pQuery, 1);
    if( !rc ) rc = xjd1DataSrcInit(pQuery->u.simple.pFrom, pQuery);
    if( !rc ) rc = xjd1ExprInit(pQuery->u.simple.pWhere, pStmt, pQuery, 0);
    if( !rc ) rc = xjd1ExprListInit(pQuery->u.simple.pGroupBy, pStmt, pQuery,0);
    if( !rc ) rc = xjd1ExprInit(pQuery->u.simple.pHaving, pStmt, pQuery, 1);



    if( !rc && pQuery->u.simple.pGroupBy ){ 
      rc = xjd1AggregateInit(pStmt, pQuery, 0);
    }
  }else{
    rc = xjd1QueryInit(pQuery->u.compound.pLeft, pStmt, pOuter);
    if( !rc ) rc = xjd1QueryInit(pQuery->u.compound.pRight, pStmt, pOuter);
  }

  if( !rc ) rc = xjd1ExprListInit(pQuery->pOrderBy, pStmt, pQuery,1);
  if( !rc ) rc = xjd1ExprInit(pQuery->pLimit, pStmt, pQuery, 0);
  if( !rc ) rc = xjd1ExprInit(pQuery->pOffset, pStmt, pQuery, 0);
  return rc;
}

/*
** Rewind a query so that it is pointing at the first row.
*/
int xjd1QueryRewind(Query *p){
  if( p==0 ) return XJD1_OK;
  if( p->eQType==TK_SELECT ){
    xjd1DataSrcRewind(p->u.simple.pFrom);


    clearResultList(&p->u.simple.grouped);
    clearResultList(&p->u.simple.distincted);

    xjd1AggregateClear(p);
  }else{
    xjd1QueryRewind(p->u.compound.pLeft);
    xjd1QueryRewind(p->u.compound.pRight);
    p->u.compound.doneLeft = 0;
    clearResultList(&p->u.compound.left);
    clearResultList(&p->u.compound.right);
    xjd1JsonFree(p->u.compound.pOut);
    p->u.compound.pOut = 0;
  }
  clearResultList(&p->ordered);
  p->eDocFrom = XJD1_FROM_DATASRC;
  p->bLimitValid = 0;
  return XJD1_OK;
}

/*
** Advance to the next row of the TK_SELECT query passed as the first 
** argument, disregarding any ORDER BY, OFFSET or LIMIT clause.
**
................................................................................
    rc==XJD1_ROW
    && (p->u.simple.pWhere!=0 && !xjd1ExprTrue(p->u.simple.pWhere))
  );
  return rc;
}

static int selectStepGrouped(Query *p){
  Aggregate *pAgg = p->u.simple.pAgg;
  int rc;

  if( pAgg ){

    ExprList *pGroupBy = p->u.simple.pGroupBy;

    rc = XJD1_OK;
    if( pGroupBy==0 ){
      /* An aggregate query with no GROUP BY clause. If there is no GROUP BY,
      ** then exactly one row is returned, which makes ORDER BY and DISTINCT 
      ** no-ops. And it is not possible to have a HAVING clause without a
      ** 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);
        }
        if( rc==XJD1_OK ){
          rc = XJD1_ROW;
          p->eDocFrom = XJD1_FROM_GROUPED;
................................................................................
      ** Use a ResultList to sort all the rows matched by the WHERE
      ** clause. The apKey[] array consists of each of the expressions
      ** in the GROUP BY clause, followed by each document in the FROM
      ** clause. */
      do {
        ResultItem *pItem;
  
        if( p->u.simple.grouped.pPool==0 ){
          DataSrc *pFrom = p->u.simple.pFrom;
          JsonNode **apKey;
          int nByte;
          Pool *pPool;
  
          /* Allocate the memory pool for this ResultList. And apKey. */
          pPool = p->u.simple.grouped.pPool = xjd1PoolNew();
          p->u.simple.grouped.nKey = pGroupBy->nEItem + xjd1DataSrcCount(pFrom);
          if( !pPool ) return XJD1_NOMEM;
          nByte = p->u.simple.grouped.nKey * sizeof(JsonNode *);
          apKey = (JsonNode **)xjd1PoolMallocZero(pPool, nByte);
          if( !apKey ) return XJD1_NOMEM;
  
          while( rc==XJD1_OK && XJD1_ROW==(rc = selectStepWhered(p) ) ){
            int i;
            for(i=0; i<pGroupBy->nEItem; i++){
              apKey[i] = xjd1ExprEval(pGroupBy->apEItem[i].pExpr);
            }
            xjd1DataSrcCacheSave(pFrom, &apKey[i]);
            rc = addToResultList(&p->u.simple.grouped, apKey);
            memset(apKey, 0, nByte);
          }
          if( rc!=XJD1_DONE ) return rc;
          sortResultList(&p->u.simple.grouped, pGroupBy, 0);
        }else{
          popResultList(&p->u.simple.grouped);
        }
  
        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 
................................................................................

  return rc;
}

static int selectStepDistinct(Query *p){
  int rc;
  if( p->u.simple.isDistinct ){
    if( p->u.simple.distincted.pPool==0 ){
      Pool *pPool;
      JsonNode **apKey;
      int nKey;

      nKey = 1 + xjd1DataSrcCount(p->u.simple.pFrom);
      pPool = p->u.simple.distincted.pPool = xjd1PoolNew();
      if( !pPool ) return XJD1_NOMEM;
      p->u.simple.distincted.nKey = nKey;
      apKey = xjd1PoolMallocZero(pPool, nKey * sizeof(JsonNode *));
      if( !apKey ) return XJD1_NOMEM;

      while( XJD1_ROW==(rc = selectStepGrouped(p) ) ){
        apKey[0] = xjd1QueryDoc(p, 0);
        xjd1DataSrcCacheSave(p->u.simple.pFrom, &apKey[1]);
        rc = addToResultList(&p->u.simple.distincted, apKey);
        memset(apKey, 0, nKey * sizeof(JsonNode *));
        if( rc!=XJD1_OK ) break;
      }
      if( rc==XJD1_DONE ){
        sortResultList(&p->u.simple.distincted, 0, 1);
        p->eDocFrom = XJD1_FROM_DISTINCTED;
        rc = XJD1_ROW;
      }
    }else{


      popResultList(&p->u.simple.distincted);



      rc = p->u.simple.distincted.pItem ? XJD1_ROW : XJD1_DONE;
    }
  }else{
    rc = selectStepGrouped(p);


  }
  return rc;
................................................................................
}


static int selectStepCompounded(Query *p){
  int rc;
  if( p->eQType==TK_SELECT ){
    rc = selectStepDistinct(p);
  }else{
    JsonNode *pOut = 0;
    if( p->eQType==TK_ALL ){
      rc = XJD1_DONE;
      if( p->u.compound.doneLeft==0 ){
        rc = selectStepCompounded(p->u.compound.pLeft);
        if( rc==XJD1_ROW ){
          pOut = xjd1QueryDoc(p->u.compound.pLeft, 0);
        }
      }
      if( rc==XJD1_DONE ){
        p->u.compound.doneLeft = 1;
        rc = selectStepCompounded(p->u.compound.pRight);
        if( rc==XJD1_ROW ){
          pOut = xjd1QueryDoc(p->u.compound.pRight, 0);
        }
      }
    }else{


      ResultList *p1;
      ResultList *p2;
  
      if( p->u.compound.left.pPool==0 ){
        rc = cacheQuery(&p->u.compound.left, p->u.compound.pLeft);
        if( rc!=XJD1_OK ) return rc;
        rc = cacheQuery(&p->u.compound.right, p->u.compound.pRight);
        if( rc!=XJD1_OK ) return rc;
      }
  
      p1 = &p->u.compound.left;
      p2 = &p->u.compound.right;
      rc = XJD1_DONE;
  
      switch( p->eQType ){
        case TK_UNION: {
          if( (p1->pItem || p2->pItem) ){
            int c;
            if( p1->pItem==0 ){
              c = 1;
            }else if( p2->pItem==0 ){
              c = -1;
            }else{
              c = cmpResultItem(p1->pItem, p2->pItem, 0);
            }
            
            if( c<0 ){
              pOut = xjd1JsonRef(p1->pItem->apKey[0]);
            }else{
              pOut = xjd1JsonRef(p2->pItem->apKey[0]);
            }
            if( c<=0 ) popResultList(p1);
            if( c>=0 ) popResultList(p2);
            rc = XJD1_ROW;
          }
          break;
        }
  
        case TK_INTERSECT:
          while( rc==XJD1_DONE && p1->pItem && p2->pItem ){
            int c = cmpResultItem(p1->pItem, p2->pItem, 0);
            if( c==0 ){
              pOut = xjd1JsonRef(p1->pItem->apKey[0]);
              rc = XJD1_ROW;
            }
            if( c<=0 ) popResultList(p1);
            if( c>=0 ) popResultList(p2);
          }
          break;
  
        case TK_EXCEPT:
          while( rc==XJD1_DONE && p1->pItem ){
            int c = p2->pItem ? cmpResultItem(p1->pItem, p2->pItem, 0) : -1;
            if( c<=0 ){
              if( c ){
                pOut = xjd1JsonRef(p1->pItem->apKey[0]);
                rc = XJD1_ROW;
              }
              popResultList(p1);
            }else{
              popResultList(p2);
            }
          }
          break;
      }
    }

    xjd1JsonFree(p->u.compound.pOut);
    p->u.compound.pOut = pOut;
  }
  return rc;
}
................................................................................
** Advance to the next row of the TK_SELECT query passed as the first 
** argument, disregarding any OFFSET or LIMIT clause.
**
** Return XJD1_ROW if there is such a row, or XJD1_EOF if there is not. Or 
** return an error code if an error occurs.
*/
static int selectStepOrdered(Query *p){
  int rc = XJD1_OK;                    /* Return Code */
  ExprList *pOrderBy = p->pOrderBy;    /* ORDER BY clause (or NULL) */






  if( pOrderBy ){

    /* TODO: Fix this hack. */
    extern void xjd1SetExprListQuery(ExprList *, Query *);
    xjd1SetExprListQuery(pOrderBy, p);

................................................................................

  if( p->bLimitValid==0 ){
    /* Calculate the values, if any, of the LIMIT and OFFSET clauses.
    **
    ** TBD: Should this throw an exception if the result of evaluating
    ** either of these clauses cannot be converted to a number?
    */
    Expr *pLimit = p->pLimit;     /* The LIMIT expression, or NULL */
    Expr *pOffset = p->pOffset;   /* The OFFSET expression, or NULL */






    if( pOffset ){
      JsonNode *pVal;             /* Result of evaluating expression pOffset */
      double rOffset;             /* pVal converted to a number */

      pVal = xjd1ExprEval(pOffset);
      if( 0==xjd1JsonToReal(pVal, &rOffset) ){
................................................................................
          if( rc!=XJD1_ROW ) break;
        }
      }
      xjd1JsonFree(pVal);
    }

    p->nLimit = -1;
    if( p->pLimit ){
      double rLimit;
      JsonNode *pVal;

      pVal = xjd1ExprEval(pLimit);
      if( 0==xjd1JsonToReal(pVal, &rLimit) ){
        p->nLimit = (int)rLimit;
      }
................................................................................
        case XJD1_FROM_ORDERED:
          assert( zDocName==0 && p->ordered.pItem );
          pOut = xjd1JsonRef(p->ordered.pItem->apKey[p->ordered.nKey-1]);
          break;

        case XJD1_FROM_DISTINCTED:
          if( zDocName==0 ){
            pOut = xjd1JsonRef(p->u.simple.distincted.pItem->apKey[0]);
          }else{
            JsonNode **apSrc = &p->u.simple.distincted.pItem->apKey[1];
            pOut = xjd1DataSrcCacheRead(p->u.simple.pFrom, apSrc, zDocName);
          }
          break;

        case XJD1_FROM_GROUPED:
          if( zDocName==0 && p->u.simple.pRes ){
            pOut = xjd1ExprEval(p->u.simple.pRes);
          }else{
            JsonNode **apSrc = p->u.simple.grouped.pItem->apKey;
            if( p->u.simple.pGroupBy ){
              apSrc = &apSrc[p->u.simple.pGroupBy->nEItem];
            }
            pOut = xjd1DataSrcCacheRead(p->u.simple.pFrom, apSrc, zDocName);
          }
          break;

................................................................................
          if( zDocName==0 && p->u.simple.pRes ){
            pOut = xjd1ExprEval(p->u.simple.pRes);
          }else{
            pOut = xjd1DataSrcDoc(p->u.simple.pFrom, zDocName);
          }
          break;
      }
    }else{
      if( p->eDocFrom==XJD1_FROM_ORDERED ){
        assert( zDocName==0 && p->ordered.pItem );
        pOut = xjd1JsonRef(p->ordered.pItem->apKey[p->ordered.nKey-1]);


      }else{



        pOut = xjd1JsonRef(p->u.compound.pOut);
      }
    }

    /* If no document has been found and this is a sub-query, search the 
    ** parent query for a document of the specified name. 
    */
    if( pOut==0 && zDocName ){
      pOut = xjd1QueryDoc(p->pOuter, zDocName);
................................................................................
** The destructor for a Query object.
*/
int xjd1QueryClose(Query *pQuery){
  int rc = XJD1_OK;
  if( pQuery==0 ) return rc;
  if( pQuery->eQType==TK_SELECT ){
    clearResultList(&pQuery->ordered);
    clearResultList(&pQuery->u.simple.grouped);
    clearResultList(&pQuery->u.simple.distincted);
    xjd1ExprClose(pQuery->u.simple.pRes);
    xjd1DataSrcClose(pQuery->u.simple.pFrom);
    xjd1ExprClose(pQuery->u.simple.pWhere);
    xjd1ExprListClose(pQuery->u.simple.pGroupBy);
    xjd1ExprClose(pQuery->u.simple.pHaving);



  }else{
    xjd1QueryClose(pQuery->u.compound.pLeft);
    xjd1QueryClose(pQuery->u.compound.pRight);
    clearResultList(&pQuery->u.compound.left);
    clearResultList(&pQuery->u.compound.right);
    xjd1JsonFree(pQuery->u.compound.pOut);
    pQuery->u.compound.pOut = 0;
  }
  xjd1ExprListClose(pQuery->pOrderBy);
  xjd1ExprClose(pQuery->pLimit);
  xjd1ExprClose(pQuery->pOffset);
  return rc;
}

Changes to src/trace.c.

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
        xjd1TraceExprList(pOut, indent+3, p->u.simple.pGroupBy);
      }
      if( p->u.simple.pHaving ){
        xjd1StringAppendF(pOut, "%*sHAVING: ", indent, "");
        xjd1TraceExpr(pOut, p->u.simple.pWhere);
        xjd1StringAppendF(pOut, "\n");
      }
      if( p->u.simple.pOrderBy ){
        xjd1StringAppendF(pOut, "%*sORDER-BY:\n", indent, "");
        xjd1TraceExprList(pOut, indent+3, p->u.simple.pOrderBy);
      }
      if( p->u.simple.pLimit ){
        xjd1StringAppendF(pOut, "%*sLIMIT: ", indent, "");
        xjd1TraceExpr(pOut, p->u.simple.pLimit);
        xjd1StringAppendF(pOut, "\n");
      }
      if( p->u.simple.pOffset ){
        xjd1StringAppendF(pOut, "%*sOFFSET: ", indent, "");
        xjd1TraceExpr(pOut, p->u.simple.pOffset);
        xjd1StringAppendF(pOut, "\n");
      }
      break;
    }
    default: {
      if( p->u.compound.pLeft ){
        xjd1StringAppendF(pOut, "%*sLeft:\n", indent, "");
        xjd1TraceQuery(pOut, indent+3, p->u.compound.pLeft);
      }
      if( p->u.compound.pRight ){
        xjd1StringAppendF(pOut, "%*sLeft:\n", indent, "");
        xjd1TraceQuery(pOut, indent+3, p->u.compound.pRight);
      }
      break;
    }















  }
}

/*
** Output the content of a data source.
*/
void xjd1TraceDataSrc(String *pOut, int indent, const DataSrc *p){







<
<
<
<
<
<
<
<
<
<
<
<
<
<













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







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
        xjd1TraceExprList(pOut, indent+3, p->u.simple.pGroupBy);
      }
      if( p->u.simple.pHaving ){
        xjd1StringAppendF(pOut, "%*sHAVING: ", indent, "");
        xjd1TraceExpr(pOut, p->u.simple.pWhere);
        xjd1StringAppendF(pOut, "\n");
      }














      break;
    }
    default: {
      if( p->u.compound.pLeft ){
        xjd1StringAppendF(pOut, "%*sLeft:\n", indent, "");
        xjd1TraceQuery(pOut, indent+3, p->u.compound.pLeft);
      }
      if( p->u.compound.pRight ){
        xjd1StringAppendF(pOut, "%*sLeft:\n", indent, "");
        xjd1TraceQuery(pOut, indent+3, p->u.compound.pRight);
      }
      break;
    }

    if( p->pOrderBy ){
      xjd1StringAppendF(pOut, "%*sORDER-BY:\n", indent, "");
      xjd1TraceExprList(pOut, indent+3, p->pOrderBy);
    }
    if( p->pLimit ){
      xjd1StringAppendF(pOut, "%*sLIMIT: ", indent, "");
      xjd1TraceExpr(pOut, p->pLimit);
      xjd1StringAppendF(pOut, "\n");
    }
    if( p->pOffset ){
      xjd1StringAppendF(pOut, "%*sOFFSET: ", indent, "");
      xjd1TraceExpr(pOut, p->pOffset);
      xjd1StringAppendF(pOut, "\n");
    }
  }
}

/*
** Output the content of a data source.
*/
void xjd1TraceDataSrc(String *pOut, int indent, const DataSrc *p){

Changes to src/xjd1Int.h.

278
279
280
281
282
283
284
285
286
287
288
289
290

291
292
293
294
295
296
297
298
299
300
301
302
303
    struct {                    /* For simple queries */
      int isDistinct;             /* True if the DISTINCT keyword is present */
      Expr *pRes;                 /* Result JSON string */
      DataSrc *pFrom;             /* The FROM clause */
      Expr *pWhere;               /* The WHERE clause */
      ExprList *pGroupBy;         /* The GROUP BY clause */
      Expr *pHaving;              /* The HAVING clause */
      ExprList *pOrderBy;         /* The ORDER BY clause */
      Expr *pLimit;               /* The LIMIT clause */
      Expr *pOffset;              /* The OFFSET clause */
    } simple;
  } u;


  Aggregate *pAgg;                /* Aggregation info. 0 for non-aggregates */
  ResultList grouped;             /* Grouped results, for GROUP BY queries */
  ResultList distincted;          /* Distinct results */

  int eDocFrom;                   /* XJD1_FROM_* - configures xjd1QueryDoc() */

  ResultList ordered;             /* Query results in sorted order */
  int bLimitValid;                /* Set to true after nLimit is set */
  int nLimit;                     /* Stop after returning this many more rows */
};

/* Candidate values for Query.eDocFrom */
#define XJD1_FROM_DATASRC    0







|
|
|


<
>
|
|
<
<

<







278
279
280
281
282
283
284
285
286
287
288
289

290
291
292


293

294
295
296
297
298
299
300
    struct {                    /* For simple queries */
      int isDistinct;             /* True if the DISTINCT keyword is present */
      Expr *pRes;                 /* Result JSON string */
      DataSrc *pFrom;             /* The FROM clause */
      Expr *pWhere;               /* The WHERE clause */
      ExprList *pGroupBy;         /* The GROUP BY clause */
      Expr *pHaving;              /* The HAVING clause */
      Aggregate *pAgg;            /* Aggregation info. 0 for non-aggregates */
      ResultList grouped;         /* Grouped results, for GROUP BY queries */
      ResultList distincted;      /* Distinct results */
    } simple;
  } u;

  ExprList *pOrderBy;             /* The ORDER BY clause */
  Expr *pLimit;                   /* The LIMIT clause */
  Expr *pOffset;                  /* The OFFSET clause */


  int eDocFrom;                   /* XJD1_FROM_* - configures xjd1QueryDoc() */

  ResultList ordered;             /* Query results in sorted order */
  int bLimitValid;                /* Set to true after nLimit is set */
  int nLimit;                     /* Stop after returning this many more rows */
};

/* Candidate values for Query.eDocFrom */
#define XJD1_FROM_DATASRC    0