UnQL

Check-in [4e6e4ec201]
Login

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

Overview
Comment:Add support for DISTINCT.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4e6e4ec2015d407118c94c8fb01ccac6a0c2b3a9
User & Date: dan 2011-07-21 18:59:14
Context
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
18:59
Add support for DISTINCT. check-in: 4e6e4ec201 user: dan tags: trunk
18:56
Add fragments to the syntax diagram page: /doc/trunk/doc/syntax/all.wiki check-in: 33c3f4b80d user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/parse.y.

353
354
355
356
357
358
359

360
361
362
363
364
365
366
367
368
369

370
371
372
373
374
375
376
...
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
    ExprList *pGroupBy;
    Expr *pHaving;
  } GroupByHaving;

  /* Construct a simple query object */
  static Query *simpleQuery(
    Parse *p,

    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.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;
................................................................................
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 expr_opt(S) from(F) where_opt(W)
                    groupby_opt(G) orderby_opt(O) limit_opt(L).
  {A = simpleQuery(p,S,F,W,&G,O,&L);}







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


// A complete FROM clause.
//
%type from {DataSrc*}
%type fromlist {DataSrc*}
%type fromitem {DataSrc*}
%include {







>










>







 







|

|


>
>
>
>
>






<







353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
...
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
    ExprList *pGroupBy;
    Expr *pHaving;
  } GroupByHaving;

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


// A complete FROM clause.
//
%type from {DataSrc*}
%type fromlist {DataSrc*}
%type fromitem {DataSrc*}
%include {

Changes to src/query.c.

48
49
50
51
52
53
54

55
56
57
58
59
60
61
62
63
64
65
...
177
178
179
180
181
182
183

184
185
186
187
188
189
190
...
335
336
337
338
339
340
341












































342
343
344
345
346
347
348
...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
...
468
469
470
471
472
473
474









475
476
477
478
479
480
481
...
512
513
514
515
516
517
518

519
520
521
522
523
524
525

  return XJD1_OK;
}

static int cmpResultItem(ResultItem *p1, ResultItem *p2, ExprList *pEList){
  int i;
  int c = 0;

  for(i=0; i<pEList->nEItem; i++){
    if( 0!=(c=xjd1JsonCompare(p1->apKey[i], p2->apKey[i])) ) break;
  }
  if( c ){
    char const *zDir = pEList->apEItem[i].zAs;
    if( zDir && zDir[0]=='D' ){
      c = c*-1;
    }
  }
  return c;
}
................................................................................
  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);

    xjd1AggregateClear(p);
  }else{
    xjd1QueryRewind(p->u.compound.pLeft);
    p->u.compound.doneLeft = 0;
    xjd1QueryRewind(p->u.compound.pRight);
  }
  return XJD1_OK;
................................................................................
  }else{
    /* Non-aggregate query */
    rc = selectStepWhered(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.
................................................................................

      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 = selectStepGrouped(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);
................................................................................
      assert( p->eDocFrom==XJD1_FROM_ORDERED );
      popResultList(&p->ordered);
    }

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

  return rc;
}

/*
** Advance a query to the next row.  Return XDJ1_DONE if there is no
................................................................................
  if( p ){
    if( p->eQType==TK_SELECT ){
      switch( p->eDocFrom ){
        case XJD1_FROM_ORDERED:
          assert( zDocName==0 && p->ordered.pItem );
          pOut = xjd1JsonRef(p->ordered.pItem->apKey[p->ordered.nKey-1]);
          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 ){
................................................................................
*/
int xjd1QueryClose(Query *pQuery){
  int rc = XJD1_OK;
  if( pQuery==0 ) return rc;
  if( pQuery->eQType==TK_SELECT ){
    clearResultList(&pQuery->ordered);
    clearResultList(&pQuery->grouped);

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







>
|


|







 







>







 







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







 







|







 







|







 







>
>
>
>
>
>
>
>
>







 







>







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
...
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
...
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
...
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
...
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581

  return XJD1_OK;
}

static int cmpResultItem(ResultItem *p1, ResultItem *p2, ExprList *pEList){
  int i;
  int c = 0;
  int nEItem = (pEList ? pEList->nEItem : 1);
  for(i=0; i<nEItem; i++){
    if( 0!=(c=xjd1JsonCompare(p1->apKey[i], p2->apKey[i])) ) break;
  }
  if( c && pEList ){
    char const *zDir = pEList->apEItem[i].zAs;
    if( zDir && zDir[0]=='D' ){
      c = c*-1;
    }
  }
  return c;
}
................................................................................
  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;
................................................................................
  }else{
    /* Non-aggregate query */
    rc = selectStepWhered(p);
  }

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

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

      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);
................................................................................
      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
................................................................................
  if( p ){
    if( p->eQType==TK_SELECT ){
      switch( p->eDocFrom ){
        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 ){
................................................................................
*/
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);

Changes to src/tokenize.c.

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
};

/**********************************************************************
** The following code is automatically generated
** by ../tool/mkkeywordhash.c
*/
/* Hash score: 51 */
static int keywordCode(const char *z, int n){
  /* zText[] encodes 282 bytes of keywords in 202 bytes */
  /*   BEGINTORDEROLLBACKELSELECTGROUPDATEACHAVINGLOBYWITHINSERTALL       */
  /*   IKEXISTSASCENDINGCOLLATEXCEPTCOLLECTIONULLIMITCREATEDELETE         */
  /*   DESCENDINGDROPRAGMAFLATTENOTIFROMUNIONVALUEWHEREinullCOMMIT        */
  /*   INTERSECTOFFSETfalsetrue                                           */
  static const char zText[201] = {
    'B','E','G','I','N','T','O','R','D','E','R','O','L','L','B','A','C','K',
    'E','L','S','E','L','E','C','T','G','R','O','U','P','D','A','T','E','A',
    'C','H','A','V','I','N','G','L','O','B','Y','W','I','T','H','I','N','S',
    'E','R','T','A','L','L','I','K','E','X','I','S','T','S','A','S','C','E',
    'N','D','I','N','G','C','O','L','L','A','T','E','X','C','E','P','T','C',
    'O','L','L','E','C','T','I','O','N','U','L','L','I','M','I','T','C','R',
    'E','A','T','E','D','E','L','E','T','E','D','E','S','C','E','N','D','I',
    'N','G','D','R','O','P','R','A','G','M','A','F','L','A','T','T','E','N',
    'O','T','I','F','R','O','M','U','N','I','O','N','V','A','L','U','E','W',
    'H','E','R','E','i','n','u','l','l','C','O','M','M','I','T','I','N','T',
    'E','R','S','E','C','T','O','F','F','S','E','T','f','a','l','s','e','t',
    'r','u','e',
  };
  static const unsigned char aHash[80] = {
       0,  12,  37,  15,  30,   0,  38,   1,  40,   7,   0,  46,  23,
       8,  22,   0,   0,   4,  45,  10,  34,  32,  41,   0,   0,   0,
       0,  35,   0,   0,   0,  18,  13,   0,   0,  44,   0,   0,   6,
       0,   0,   0,   0,  36,   0,   0,   0,   0,   0,   0,   0,   0,
      21,  26,  43,  33,   9,  25,   0,   0,   0,   2,  19,  29,   0,
      42,   0,   0,   0,  31,   0,   0,  24,  27,   0,   0,  39,  28,
      14,   5,
  };
  static const unsigned char aNext[46] = {
       0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      11,   0,   0,   0,   0,   0,   0,   0,   0,   0,  20,   0,   0,
       0,   0,   3,   0,   0,   0,   0,   0,  16,   0,   0,   0,   0,
       0,   0,   0,   0,   0,   0,  17,
  };
  static const unsigned char aLen[46] = {
       5,   4,   5,   8,   4,   6,   5,   6,   4,   6,   4,   2,   6,
       6,   3,   4,   6,   2,   3,   9,   7,   6,  10,   4,   5,   6,
       6,   4,  10,   4,   6,   7,   3,   2,   4,   5,   5,   5,   2,
       4,   6,   9,   6,   3,   5,   4,
  };
  static const unsigned short int aOffset[46] = {
       0,   3,   6,  10,  18,  20,  26,  29,  34,  37,  42,  45,  47,
      51,  57,  59,  62,  68,  68,  68,  77,  83,  89,  98, 101, 106,
     112, 118, 118, 128, 131, 137, 143, 146, 147, 151, 156, 161, 166,
     167, 171, 177, 186, 189, 192, 197,
  };
  static const unsigned char aCode[46] = {
    TK_BEGIN,      TK_INTO,       TK_ORDER,      TK_ROLLBACK,   TK_ELSE,       
    TK_SELECT,     TK_GROUP,      TK_UPDATE,     TK_FLATTENOP,  TK_HAVING,     
    TK_LIKEOP,     TK_BY,         TK_WITHIN,     TK_INSERT,     TK_ALL,        
    TK_LIKEOP,     TK_EXISTS,     TK_AS,         TK_ASCENDING,  TK_ASCENDING,  
    TK_COLLATE,    TK_EXCEPT,     TK_COLLECTION, TK_NULL,       TK_LIMIT,      
    TK_CREATE,     TK_DELETE,     TK_DESCENDING, TK_DESCENDING, TK_DROP,       
    TK_PRAGMA,     TK_FLATTENOP,  TK_NOT,        TK_IF,         TK_FROM,       
    TK_UNION,      TK_VALUE,      TK_WHERE,      TK_IN,         TK_NULL,       
    TK_COMMIT,     TK_INTERSECT,  TK_OFFSET,     TK_SET,        TK_FALSE,      
    TK_TRUE,       
  };
  int h, i;
  if( n<2 ) return TK_ID;
  h = (z[0]*4 ^ z[n-1]*3 ^ n) % 80;
  for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
    if( aLen[i]==n && memcmp(&zText[aOffset[i]],z,n)==0 ){
      return aCode[i];
    }
  }
  return TK_ID;
}
#define XJD1_N_KEYWORD 46

/* End of the automatically generated hash code
*********************************************************************/

/*
** Return the length of the token that begins at z[0]. 
** Store the token type in *tokenType before returning.







|

|



|
|









|
|
|


|
|
|

|
|


|



|

|



|

|



|

|








|
|











|







82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
};

/**********************************************************************
** The following code is automatically generated
** by ../tool/mkkeywordhash.c
*/
/* Hash score: 53 */
static int keywordCode(const char *z, int n){
  /* zText[] encodes 291 bytes of keywords in 210 bytes */
  /*   BEGINTORDEROLLBACKELSELECTGROUPDATEACHAVINGLOBYWITHINSERTALL       */
  /*   IKEXISTSASCENDINGCOLLATEXCEPTCOLLECTIONULLIMITCREATEDELETE         */
  /*   DESCENDINGDROPRAGMAFLATTENOTIFROMUNIONVALUEWHEREinullCOMMIT        */
  /*   DISTINCTINTERSECTOFFSETfalsetrue                                   */
  static const char zText[209] = {
    'B','E','G','I','N','T','O','R','D','E','R','O','L','L','B','A','C','K',
    'E','L','S','E','L','E','C','T','G','R','O','U','P','D','A','T','E','A',
    'C','H','A','V','I','N','G','L','O','B','Y','W','I','T','H','I','N','S',
    'E','R','T','A','L','L','I','K','E','X','I','S','T','S','A','S','C','E',
    'N','D','I','N','G','C','O','L','L','A','T','E','X','C','E','P','T','C',
    'O','L','L','E','C','T','I','O','N','U','L','L','I','M','I','T','C','R',
    'E','A','T','E','D','E','L','E','T','E','D','E','S','C','E','N','D','I',
    'N','G','D','R','O','P','R','A','G','M','A','F','L','A','T','T','E','N',
    'O','T','I','F','R','O','M','U','N','I','O','N','V','A','L','U','E','W',
    'H','E','R','E','i','n','u','l','l','C','O','M','M','I','T','D','I','S',
    'T','I','N','C','T','I','N','T','E','R','S','E','C','T','O','F','F','S',
    'E','T','f','a','l','s','e','t','r','u','e',
  };
  static const unsigned char aHash[80] = {
       0,  12,  37,  15,  42,   0,  38,   1,  40,   7,   0,  47,  23,
       8,  22,   0,   0,   4,  46,  10,  34,  32,  41,   0,   0,   0,
       0,  35,   0,   0,   0,  18,  13,   0,   0,  45,   0,   0,   6,
       0,   0,   0,   0,  36,   0,   0,   0,   0,   0,   0,   0,   0,
      21,  26,  44,  33,   9,  25,   0,   0,   0,   2,  19,  29,   0,
      43,   0,   0,   0,  31,   0,   0,  24,  27,   0,   0,  39,  28,
      14,   5,
  };
  static const unsigned char aNext[47] = {
       0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
      11,   0,   0,   0,   0,   0,   0,   0,   0,   0,  20,   0,   0,
       0,   0,   3,   0,   0,   0,   0,   0,  16,   0,   0,   0,   0,
       0,   0,  30,   0,   0,   0,   0,  17,
  };
  static const unsigned char aLen[47] = {
       5,   4,   5,   8,   4,   6,   5,   6,   4,   6,   4,   2,   6,
       6,   3,   4,   6,   2,   3,   9,   7,   6,  10,   4,   5,   6,
       6,   4,  10,   4,   6,   7,   3,   2,   4,   5,   5,   5,   2,
       4,   6,   8,   9,   6,   3,   5,   4,
  };
  static const unsigned short int aOffset[47] = {
       0,   3,   6,  10,  18,  20,  26,  29,  34,  37,  42,  45,  47,
      51,  57,  59,  62,  68,  68,  68,  77,  83,  89,  98, 101, 106,
     112, 118, 118, 128, 131, 137, 143, 146, 147, 151, 156, 161, 166,
     167, 171, 177, 185, 194, 197, 200, 205,
  };
  static const unsigned char aCode[47] = {
    TK_BEGIN,      TK_INTO,       TK_ORDER,      TK_ROLLBACK,   TK_ELSE,       
    TK_SELECT,     TK_GROUP,      TK_UPDATE,     TK_FLATTENOP,  TK_HAVING,     
    TK_LIKEOP,     TK_BY,         TK_WITHIN,     TK_INSERT,     TK_ALL,        
    TK_LIKEOP,     TK_EXISTS,     TK_AS,         TK_ASCENDING,  TK_ASCENDING,  
    TK_COLLATE,    TK_EXCEPT,     TK_COLLECTION, TK_NULL,       TK_LIMIT,      
    TK_CREATE,     TK_DELETE,     TK_DESCENDING, TK_DESCENDING, TK_DROP,       
    TK_PRAGMA,     TK_FLATTENOP,  TK_NOT,        TK_IF,         TK_FROM,       
    TK_UNION,      TK_VALUE,      TK_WHERE,      TK_IN,         TK_NULL,       
    TK_COMMIT,     TK_DISTINCT,   TK_INTERSECT,  TK_OFFSET,     TK_SET,        
    TK_FALSE,      TK_TRUE,       
  };
  int h, i;
  if( n<2 ) return TK_ID;
  h = (z[0]*4 ^ z[n-1]*3 ^ n) % 80;
  for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
    if( aLen[i]==n && memcmp(&zText[aOffset[i]],z,n)==0 ){
      return aCode[i];
    }
  }
  return TK_ID;
}
#define XJD1_N_KEYWORD 47

/* End of the automatically generated hash code
*********************************************************************/

/*
** Return the length of the token that begins at z[0]. 
** Store the token type in *tokenType before returning.

Changes to src/trace.c.

40
41
42
43
44
45
46

47
48
49
50
51
52
53
..
71
72
73
74
75
76
77

78
79
80
81
82
83
84
  { TK_BITXOR,           "TK_BITXOR"          },
  { TK_BITAND,           "TK_BITAND"          },
  { TK_LIKEOP,           "TK_LIKEOP"          },
  { TK_NE,               "TK_NE"              },
  { TK_EQEQ,             "TK_EQEQ"            },
  { TK_EQ3,              "TK_EQ3"             },
  { TK_NE3,              "TK_NE3"             },

  { TK_IN,               "TK_IN"              },
  { TK_GT,               "TK_GT"              },
  { TK_LE,               "TK_LE"              },
  { TK_LT,               "TK_LT"              },
  { TK_GE,               "TK_GE"              },
  { TK_LSHIFT,           "TK_LSHIFT"          },
  { TK_RSHIFT,           "TK_RSHIFT"          },
................................................................................
  { TK_LP,               "TK_LP"              },
  { TK_RP,               "TK_RP"              },
  { TK_UNION,            "TK_UNION"           },
  { TK_EXCEPT,           "TK_EXCEPT"          },
  { TK_INTERSECT,        "TK_INTERSECT"       },
  { TK_ALL,              "TK_ALL"             },
  { TK_SELECT,           "TK_SELECT"          },

  { TK_FROM,             "TK_FROM"            },
  { TK_AS,               "TK_AS"              },
  { TK_FLATTENOP,        "TK_FLATTENOP"       },
  { TK_GROUP,            "TK_GROUP"           },
  { TK_BY,               "TK_BY"              },
  { TK_HAVING,           "TK_HAVING"          },
  { TK_ORDER,            "TK_ORDER"           },







>







 







>







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  { TK_BITXOR,           "TK_BITXOR"          },
  { TK_BITAND,           "TK_BITAND"          },
  { TK_LIKEOP,           "TK_LIKEOP"          },
  { TK_NE,               "TK_NE"              },
  { TK_EQEQ,             "TK_EQEQ"            },
  { TK_EQ3,              "TK_EQ3"             },
  { TK_NE3,              "TK_NE3"             },
  { TK_WITHIN,           "TK_WITHIN"          },
  { TK_IN,               "TK_IN"              },
  { TK_GT,               "TK_GT"              },
  { TK_LE,               "TK_LE"              },
  { TK_LT,               "TK_LT"              },
  { TK_GE,               "TK_GE"              },
  { TK_LSHIFT,           "TK_LSHIFT"          },
  { TK_RSHIFT,           "TK_RSHIFT"          },
................................................................................
  { TK_LP,               "TK_LP"              },
  { TK_RP,               "TK_RP"              },
  { TK_UNION,            "TK_UNION"           },
  { TK_EXCEPT,           "TK_EXCEPT"          },
  { TK_INTERSECT,        "TK_INTERSECT"       },
  { TK_ALL,              "TK_ALL"             },
  { TK_SELECT,           "TK_SELECT"          },
  { TK_DISTINCT,         "TK_DISTINCT"        },
  { TK_FROM,             "TK_FROM"            },
  { TK_AS,               "TK_AS"              },
  { TK_FLATTENOP,        "TK_FLATTENOP"       },
  { TK_GROUP,            "TK_GROUP"           },
  { TK_BY,               "TK_BY"              },
  { TK_HAVING,           "TK_HAVING"          },
  { TK_ORDER,            "TK_ORDER"           },

Changes to src/xjd1Int.h.

269
270
271
272
273
274
275

276
277
278
279
280
281
282
...
291
292
293
294
295
296
297
298
299

300
301
302
303
304
305
306
307
  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 */

      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 */
................................................................................
  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_ORDERED 2

/* A Data Source is a representation of a term out of the FROM clause. */
struct DataSrc {
  int eDSType;              /* Source type */
  char *zAs;                /* The identifier after the AS keyword */
  Query *pQuery;            /* Query this data source services */
  JsonNode *pValue;         /* Current value for this data source */







>







 







|
|
>
|







269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  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 *pHaving;              /* The HAVING clause */
      ExprList *pOrderBy;         /* The ORDER BY clause */
      Expr *pLimit;               /* The LIMIT clause */
................................................................................
  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
#define XJD1_FROM_ORDERED    3

/* A Data Source is a representation of a term out of the FROM clause. */
struct DataSrc {
  int eDSType;              /* Source type */
  char *zAs;                /* The identifier after the AS keyword */
  Query *pQuery;            /* Query this data source services */
  JsonNode *pValue;         /* Current value for this data source */

Changes to test/base05.test.

104
105
106
107
108
109
110








SELECT {x:count(c2.b),y:c2.a} FROM c2 GROUP BY c2.a ORDER BY count(c2.b) ASC;
.result {"x":1,"y":"c"} {"x":2,"y":"b"} {"x":3,"y":"a"} 

.testcase 18
SELECT {x:count(c2.b),y:c2.a} FROM c2 GROUP BY c2.a HAVING count(c2.b)>1;
.result {"x":3,"y":"a"} {"x":2,"y":"b"} 
















>
>
>
>
>
>
>
>
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
SELECT {x:count(c2.b),y:c2.a} FROM c2 GROUP BY c2.a ORDER BY count(c2.b) ASC;
.result {"x":1,"y":"c"} {"x":2,"y":"b"} {"x":3,"y":"a"} 

.testcase 18
SELECT {x:count(c2.b),y:c2.a} FROM c2 GROUP BY c2.a HAVING count(c2.b)>1;
.result {"x":3,"y":"a"} {"x":2,"y":"b"} 

.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"

Changes to tool/mkkeywordhash.c.

63
64
65
66
67
68
69

70
71
72
73
74
75
76
  { "AS",           "TK_AS",         },
  { "BEGIN",        "TK_BEGIN",      },
  { "BY",           "TK_BY",         },
  { "COLLATE",      "TK_COLLATE",    },
  { "COLLECTION",   "TK_COLLECTION", },
  { "COMMIT",       "TK_COMMIT",     },
  { "CREATE",       "TK_CREATE",     },

  { "DELETE",       "TK_DELETE",     },
  { "DESCENDING",   "TK_DESCENDING", },
  { "DESC",         "TK_DESCENDING", },
  { "DROP",         "TK_DROP",       },
  { "EACH",         "TK_FLATTENOP",  },
  { "ELSE",         "TK_ELSE",       },
  { "EXCEPT",       "TK_EXCEPT",     },







>







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  { "AS",           "TK_AS",         },
  { "BEGIN",        "TK_BEGIN",      },
  { "BY",           "TK_BY",         },
  { "COLLATE",      "TK_COLLATE",    },
  { "COLLECTION",   "TK_COLLECTION", },
  { "COMMIT",       "TK_COMMIT",     },
  { "CREATE",       "TK_CREATE",     },
  { "DISTINCT",     "TK_DISTINCT",   },
  { "DELETE",       "TK_DELETE",     },
  { "DESCENDING",   "TK_DESCENDING", },
  { "DESC",         "TK_DESCENDING", },
  { "DROP",         "TK_DROP",       },
  { "EACH",         "TK_FLATTENOP",  },
  { "ELSE",         "TK_ELSE",       },
  { "EXCEPT",       "TK_EXCEPT",     },