UnQL

Check-in [2d74ebd10f]
Login

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

Overview
Comment:Add support for UNION, EXCEPT, INTERSECT. Doesn't work properly with ORDER BY yet.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2d74ebd10f05358045d4e1775d67740906266833
User & Date: dan 2011-07-22 19:12:29
Context
2011-07-23
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
2011-07-21
19:56
Add an optional AS clause on the result set of a SELECT in the bubble diagrams. check-in: 66e597b565 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/expr.c.

740
741
742
743
744
745
746



















  if( pValue ){
    rc = isTrue(pValue);
    assert( rc==1 || rc==0 );
    xjd1JsonFree(pValue);
  }
  return rc;
}


























>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
  if( pValue ){
    rc = isTrue(pValue);
    assert( rc==1 || rc==0 );
    xjd1JsonFree(pValue);
  }
  return rc;
}

static int setExprQuery(Expr *p, WalkAction *pAction){
  p->pQuery = pAction->pQuery;
  return XJD1_OK;
}

void xjd1SetExprListQuery(ExprList *pList, Query *p){
  int i;
  WalkAction action;

  action.xQueryAction = 0;
  action.pQuery = p;
  action.pStmt = 0;
  action.pArg = 0;
  action.xNodeAction= setExprQuery;
  for(i=0; i<pList->nEItem; i++){
    walkExpr(pList->apEItem[i].pExpr, &action);
  }
}

Changes to src/parse.y.

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

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) ::= expr(X).       // must be (SELECT...)
  {A = X->u.subq.p;}

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}







|
<







401
402
403
404
405
406
407
408

409
410
411
412
413
414
415

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}

Changes to src/query.c.

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
118
119
120
121
122
123
124
125
126




















127
128
129
130
131
132
133
...
175
176
177
178
179
180
181
182
183
184
185
186
187
188

189



190
191
192
193
194
195
196
197
...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
...
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
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
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
...
544
545
546
547
548
549
550
551




552
553
554
555
556







557
558
559
560
561
562
563
...
579
580
581
582
583
584
585




586
587
588
589
      }
    }
  }

  return pRet;
}

static void sortResultList(ResultList *pList, ExprList *pEList){
  int i;                          /* Used to iterate through aList[] */
  ResultItem *aList[40];          /* Array of slots for merge sort */
  ResultItem *pHead;

  memset(aList, 0, sizeof(aList));
  pHead = pList->pItem;

................................................................................
    pHead = pNext;
  }

  pHead = aList[0];
  for(i=1; i<ArraySize(aList); i++){
    pHead = mergeResultItems(pEList, pHead, aList[i]);
  }

  pList->pItem = pHead;




















}

static void popResultList(ResultList *pList){
  ResultItem *pItem;
  int i;

  pItem = pList->pItem;
................................................................................
** 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->bStarted = 0;
    clearResultList(&p->ordered);
    clearResultList(&p->grouped);
    clearResultList(&p->distincted);
    xjd1AggregateClear(p);
  }else{
    xjd1QueryRewind(p->u.compound.pLeft);

    p->u.compound.doneLeft = 0;



    xjd1QueryRewind(p->u.compound.pRight);
  }
  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.
................................................................................
              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);
        }else{
          popResultList(&p->grouped);
        }
  
        p->eDocFrom = XJD1_FROM_GROUPED;
        pItem = p->grouped.pItem;
        if( pItem==0 ){
................................................................................
        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);
        p->eDocFrom = XJD1_FROM_DISTINCTED;
        rc = XJD1_ROW;
      }
    }else{
      JsonNode *pPrev = xjd1JsonRef(p->distincted.pItem->apKey[0]);
      do{
        popResultList(&p->distincted);
................................................................................
    rc = selectStepGrouped(p);


  }
  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 */





  if( p->u.simple.pOrderBy ){







    /* A non-aggregate with an ORDER BY clause. */
    if( p->ordered.pPool==0 ){
      int nKey = p->u.simple.pOrderBy->nEItem + 1;
      ExprList *pOrderBy = p->u.simple.pOrderBy;
      Pool *pPool;
      JsonNode **apKey;

      p->ordered.nKey = nKey;
      pPool = p->ordered.pPool = xjd1PoolNew();
      if( !pPool ) return XJD1_NOMEM;
      apKey = xjd1PoolMallocZero(pPool, nKey * sizeof(JsonNode *));
      if( !apKey ) return XJD1_NOMEM;

      while( XJD1_ROW==(rc = selectStepDistinct(p) ) ){
        int i;
        for(i=0; i<pOrderBy->nEItem; i++){
          apKey[i] = xjd1ExprEval(pOrderBy->apEItem[i].pExpr);
        }
        apKey[i] = xjd1QueryDoc(p, 0);

        rc = addToResultList(&p->ordered, apKey);
        if( rc!=XJD1_OK ) break;
      }
      if( rc!=XJD1_DONE ) return rc;

      sortResultList(&p->ordered, p->u.simple.pOrderBy);
      p->eDocFrom = XJD1_FROM_ORDERED;
    }else{
      assert( p->eDocFrom==XJD1_FROM_ORDERED );
      popResultList(&p->ordered);
    }

    rc = p->ordered.pItem ? XJD1_ROW : XJD1_DONE;
  }else{
    /* No ORDER BY clause. */
    rc = selectStepDistinct(p);
  }

  return rc;
}

/*
** Advance a query to the next row.  Return XDJ1_DONE if there is no
** next row, or XJD1_ROW if the step was successful.
*/
int xjd1QueryStep(Query *p){
  int rc = XJD1_ROW;
  if( p==0 ) return XJD1_DONE;


  if( p->eQType==TK_SELECT ){




    /* 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?
    */
    if( p->bStarted==0 ){






      if( p->u.simple.pOffset ){
        double rOffset;

        JsonNode *pOffset;



        pOffset = xjd1ExprEval(p->u.simple.pOffset);
        if( 0==xjd1JsonToReal(pOffset, &rOffset) ){
          int nOffset;
          for(nOffset=(int)rOffset; nOffset>0; nOffset--){
            rc = selectStepOrdered(p);
            if( rc!=XJD1_ROW ) break;
          }
        }
        xjd1JsonFree(pOffset);
      }

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

        pLimit = xjd1ExprEval(p->u.simple.pLimit);
        if( 0==xjd1JsonToReal(pLimit, &rLimit) ){
          p->nLimit = (int)rLimit;
        }
        xjd1JsonFree(pLimit);
      }

    }

    if( rc==XJD1_ROW ){
      if( p->nLimit==0 ){
        rc = XJD1_DONE;
      }else{
        rc = selectStepOrdered(p);
        if( p->nLimit>0 ) p->nLimit--;
      }
    }
    p->bStarted = 1;
  }else{
    if( !p->u.compound.doneLeft ){
      rc = xjd1QueryStep(p->u.compound.pLeft);
      if( rc==XJD1_DONE ) p->u.compound.doneLeft = 1;
    }
    if( p->u.compound.doneLeft ){
      rc = xjd1QueryStep(p->u.compound.pRight);
    }
  }
  return rc;
}

/*
** Return a document currently referenced by a query.  If zDocName==0 then
** return the constructed result set of the query.
**
................................................................................
          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->u.compound.doneLeft ){
      pOut = xjd1QueryDoc(p->u.compound.pLeft, zDocName);
    }else{
      pOut = xjd1QueryDoc(p->u.compound.pRight, zDocName);
    }







    if( pOut==0 && zDocName ){
      pOut = xjd1QueryDoc(p->pOuter, zDocName);
    }
  }
  return pOut;
}

................................................................................
    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);




  }
  return rc;
}








|







 







<

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







 







|






>

>
>
>
|







 







|







 







|







 







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









>

>
>
>
|
>
>
>
>
>
>



|
<









|











|









|












>
>
|
>
>

>





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

|
|
|
|
|
|
|
|
|
|

|
|
|
|

|
|
|
|
|
|
>
|

|
|
|
|
|
|
|
|
<
<
<
<
<
|
<
<
<
<







 







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







 







>
>
>
>




94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
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
...
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
...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
...
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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

604
605
606
607
608
609
610

611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648





649




650
651
652
653
654
655
656
...
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
716
717
718
719
720
...
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
      }
    }
  }

  return pRet;
}

static void sortResultList(ResultList *pList, ExprList *pEList, int uniq){
  int i;                          /* Used to iterate through aList[] */
  ResultItem *aList[40];          /* Array of slots for merge sort */
  ResultItem *pHead;

  memset(aList, 0, sizeof(aList));
  pHead = pList->pItem;

................................................................................
    pHead = pNext;
  }

  pHead = aList[0];
  for(i=1; i<ArraySize(aList); i++){
    pHead = mergeResultItems(pEList, pHead, aList[i]);
  }

  pList->pItem = pHead;

  if( uniq && pHead ){
    ResultItem *pPrev = pHead;
    ResultItem *p;
    for(p=pPrev->pNext; p; p=p->pNext){
      if( cmpResultItem(pPrev, p, pEList)==0 ){
        pPrev->pNext = p->pNext;
      }else{
        pPrev = p;
      }
    }
  }

#if 1
  while( pHead ){
    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;
................................................................................
** 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.
................................................................................
              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 ){
................................................................................
        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);
................................................................................
    rc = selectStepGrouped(p);


  }
  return rc;
}


static int selectStepCompounded(Query *);
static int cacheQuery(ResultList *pList, Query *p){
  Pool *pPool;
  int rc;

  pList->nKey = 1;
  pPool = pList->pPool = xjd1PoolNew();
  if( !pPool ) return XJD1_NOMEM;

  while( XJD1_ROW==(rc = selectStepCompounded(p) ) ){
    JsonNode *pDoc = xjd1QueryDoc(p, 0);
    rc = addToResultList(pList, &pDoc);
    if( rc!=XJD1_OK ) break;
  }

  if( rc==XJD1_DONE ){
    sortResultList(pList, 0, 1);
    rc = XJD1_OK;
  }
  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);

    /* A non-aggregate with an ORDER BY clause. */
    if( p->ordered.pPool==0 ){
      int nKey = pOrderBy->nEItem + 1;

      Pool *pPool;
      JsonNode **apKey;

      p->ordered.nKey = nKey;
      pPool = p->ordered.pPool = xjd1PoolNew();
      if( !pPool ) return XJD1_NOMEM;
      apKey = xjd1PoolMallocZero(pPool, nKey * sizeof(JsonNode *));
      if( !apKey ) return XJD1_NOMEM;

      while( XJD1_ROW==(rc = selectStepCompounded(p) ) ){
        int i;
        for(i=0; i<pOrderBy->nEItem; i++){
          apKey[i] = xjd1ExprEval(pOrderBy->apEItem[i].pExpr);
        }
        apKey[i] = xjd1QueryDoc(p, 0);

        rc = addToResultList(&p->ordered, apKey);
        if( rc!=XJD1_OK ) break;
      }
      if( rc!=XJD1_DONE ) return rc;

      sortResultList(&p->ordered, pOrderBy, 0);
      p->eDocFrom = XJD1_FROM_ORDERED;
    }else{
      assert( p->eDocFrom==XJD1_FROM_ORDERED );
      popResultList(&p->ordered);
    }

    rc = p->ordered.pItem ? XJD1_ROW : XJD1_DONE;
  }else{
    /* No ORDER BY clause. */
    rc = selectStepCompounded(p);
  }

  return rc;
}

/*
** Advance a query to the next row.  Return XDJ1_DONE if there is no
** next row, or XJD1_ROW if the step was successful.
*/
int xjd1QueryStep(Query *p){
  int rc = XJD1_ROW;
  if( p==0 ) return XJD1_DONE;

  /* Query is either a simple SELECT, or a compound of some type. */
  assert( p->eQType==TK_SELECT || p->eQType==TK_UNION || p->eQType==TK_ALL
       || p->eQType==TK_EXCEPT || p->eQType==TK_INTERSECT
  );

  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) ){
        int nOffset;
        for(nOffset=(int)rOffset; nOffset>0; nOffset--){
          rc = selectStepOrdered(p);
          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;
      }
      xjd1JsonFree(pVal);
    }
    p->bLimitValid = 1;
  }

  if( rc==XJD1_ROW ){
    if( p->nLimit==0 ){
      rc = XJD1_DONE;
    }else{
      rc = selectStepOrdered(p);
      if( p->nLimit>0 ) p->nLimit--;
    }
  }










  return rc;
}

/*
** Return a document currently referenced by a query.  If zDocName==0 then
** return the constructed result set of the query.
**
................................................................................
          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);
    }
  }
  return pOut;
}

................................................................................
    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;
}

Changes to src/xjd1Int.h.

266
267
268
269
270
271
272
273



274
275
276
277
278
279
280
...
284
285
286
287
288
289
290
291

292
293
294

295
296
297
298
299
300
301
  int eQType;                   /* Query type */
  xjd1_stmt *pStmt;             /* Statement this query is part of */
  Query *pOuter;                /* Next outer query for a subquery */
  union {
    struct {                    /* For compound queries */
      Query *pLeft;               /* Left subquery */
      Query *pRight;              /* Right subquery */
      int doneLeft;               /* True if left is run to completion */



    } compound;
    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 *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 */
  ResultList ordered;             /* Query results in sorted order */

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

  int bStarted;                   /* Set to true after first Step() */

  int nLimit;                     /* Stop after returning this many more rows */
};

/* Candidate values for Query.eDocFrom */
#define XJD1_FROM_DATASRC    0
#define XJD1_FROM_GROUPED    1
#define XJD1_FROM_DISTINCTED 2







|
>
>
>







 







<
>


|
>







266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
...
287
288
289
290
291
292
293

294
295
296
297
298
299
300
301
302
303
304
305
  int eQType;                   /* Query type */
  xjd1_stmt *pStmt;             /* Statement this query is part of */
  Query *pOuter;                /* Next outer query for a subquery */
  union {
    struct {                    /* For compound queries */
      Query *pLeft;               /* Left subquery */
      Query *pRight;              /* Right subquery */
      int doneLeft;               /* True if left has run to completion */
      ResultList left;            /* Sorted results of pLeft */
      ResultList right;           /* Sorted results of pRight */
      JsonNode *pOut;
    } compound;
    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 *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
#define XJD1_FROM_GROUPED    1
#define XJD1_FROM_DISTINCTED 2

Changes to test/all.test.

1
2
3
4
5
6
7

-- Run all test scripts
--
.read base01.test
.read base02.test
.read base03.test
.read base04.test
.read base05.test








>
1
2
3
4
5
6
7
8
-- Run all test scripts
--
.read base01.test
.read base02.test
.read base03.test
.read base04.test
.read base05.test
.read base06.test