UnQL

Check-in [9648d8798c]
Login

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

Overview
Comment:Add support for scalar sub-queries.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9648d8798c3a440b7ee4e108ee32a0526274bb96
User & Date: dan 2011-07-25 09:46:48
Context
2011-07-25
11:59
Change to using xjd1_malloc() etc. internally instead of using malloc() directly. check-in: 34eea27b86 user: dan tags: trunk
09:46
Add support for scalar sub-queries. check-in: 9648d8798c user: dan tags: trunk
2011-07-23
19:53
Return an error when an unknown object is referenced. i.e. for "SELECT x FROM y", return the error "no such object: x". check-in: 8b4dcb6e8a user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to main.mk.

125
126
127
128
129
130
131
132
	./lemon $(OPTS) parse.y

sqlite3.o:	$(TOP)/src/sqlite3.c $(TOP)/src/sqlite3.h
	$(TCCX) -c -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOADEXTENSION $<

clean:	
	rm -f *.o lib*.a
	rm -f lemon xjd1 parse.* parse_txt.h







|
125
126
127
128
129
130
131
132
	./lemon $(OPTS) parse.y

sqlite3.o:	$(TOP)/src/sqlite3.c $(TOP)/src/sqlite3.h
	$(TCCX) -c -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOADEXTENSION $<

clean:	
	rm -f *.o lib*.a
	rm -f lemon xjd1 parse.* parse_txt.h lempar.c

Changes to src/datasrc.c.

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  switch( p->eDSType ){
    case TK_COMMA: {
      xjd1DataSrcInit(p->u.join.pLeft, pQuery);
      xjd1DataSrcInit(p->u.join.pRight, pQuery);
      break;
    }
    case TK_SELECT: {
      xjd1QueryInit(p->u.subq.q, pQuery->pStmt, pQuery);
      break;
    }
    case TK_ID: {
      char *zSql = sqlite3_mprintf("SELECT x FROM \"%w\"", p->u.tab.zName);
      sqlite3_prepare_v2(pQuery->pStmt->pConn->db, zSql, -1, 
                         &p->u.tab.pStmt, 0);
      sqlite3_free(zSql);







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  switch( p->eDSType ){
    case TK_COMMA: {
      xjd1DataSrcInit(p->u.join.pLeft, pQuery);
      xjd1DataSrcInit(p->u.join.pRight, pQuery);
      break;
    }
    case TK_SELECT: {
      xjd1QueryInit(p->u.subq.q, pQuery->pStmt, 0);
      break;
    }
    case TK_ID: {
      char *zSql = sqlite3_mprintf("SELECT x FROM \"%w\"", p->u.tab.zName);
      sqlite3_prepare_v2(pQuery->pStmt->pConn->db, zSql, -1, 
                         &p->u.tab.pStmt, 0);
      sqlite3_free(zSql);

Changes to src/expr.c.

14
15
16
17
18
19
20
21

22
23


24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

163
164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
...
200
201
202
203
204
205
206
207

208
209
210
211
212
213
214

215
216
217
218
219
220
221
222
223






224
225
226
227
228
229


230
231
232
233
234
235
236

237
238


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
...
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
...
568
569
570
571
572
573
574




















575
576
577
578
579
580
581
...
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains code used to evaluate expressions at run-time.
*/
#include "xjd1Int.h"
#include <math.h>


/*
** Information passed down into the expression walker.


*/
typedef struct WalkAction WalkAction;
struct WalkAction {
  int (*xQueryAction)(Expr*,WalkAction*);  /* Call for each EXPR_Q node */
  int (*xNodeAction)(Expr*,WalkAction*);   /* Call for every node */
  xjd1_stmt *pStmt;                        /* Stmt expr belongs to */
  Query *pQuery;                           /* Query expr belongs to */
  void *pArg;                              /* Some other argument */

};

/* forward reference */
static int walkExpr(Expr*,WalkAction*);

/*
** Walk an expression list 
*/
static int walkExprList(ExprList *p, WalkAction *pAction){
  if( p ){
    int i;
    for(i=0; i<p->nEItem; i++){
      walkExpr(p->apEItem[i].pExpr, pAction);
    }
  }
  return XJD1_OK;
}

/*
** Walk an expression tree
*/
static int walkExpr(Expr *p, WalkAction *pAction){
  int rc = XJD1_OK;
  if( p==0 ) return XJD1_OK;
  if( pAction->xNodeAction ){
    rc = pAction->xNodeAction(p, pAction);
  }
  switch( p->eClass ){
    case XJD1_EXPR_BI: {
      walkExpr(p->u.bi.pLeft, pAction);
      walkExpr(p->u.bi.pRight, pAction);
      break;
    }
    case XJD1_EXPR_TRI: {
      walkExpr(p->u.tri.pTest, pAction);
      walkExpr(p->u.tri.pIfTrue, pAction);
      walkExpr(p->u.tri.pIfFalse, pAction);
      break;
    }
    case XJD1_EXPR_TK: {
      /* Nothing to do */
      break;
    }
    case XJD1_EXPR_FUNC: {
      walkExprList(p->u.func.args, pAction);
      break;
    }
    case XJD1_EXPR_Q: {
      if( pAction->xQueryAction ){
        rc = pAction->xQueryAction(p, pAction);
      }
      break;
    }
    case XJD1_EXPR_JSON: {
      /* Nothing to do */
      break;
    }
    case XJD1_EXPR_ARRAY: {
      walkExprList(p->u.ar, pAction);
      break;
    }
    case XJD1_EXPR_STRUCT: {
      walkExprList(p->u.st, pAction);
      break;
    }
    case XJD1_EXPR_LVALUE: {
      walkExpr(p->u.lvalue.pLeft, pAction);
      break;
    }
  }
  return rc;
}

static int exprResolve(Expr *p, WalkAction *pAction){
  int eExpr = *(int *)pAction->pArg;
  const char *zDoc;

  assert( eExpr>0 || (pAction->pQuery==0 && pAction->pStmt) );

  zDoc = p->u.id.zId;
  if( eExpr==0 ){
    Command *pCmd = pAction->pStmt->pCmd;
    switch( pCmd->eCmdType ){
      case TK_DELETE:
        if( 0==strcmp(zDoc, pCmd->u.del.zName) ) return XJD1_OK;
        break;
      case TK_UPDATE:
        if( 0==strcmp(zDoc, pCmd->u.update.zName) ) return XJD1_OK;
        break;

      default:
        assert( 0 );
        break;
    }
  }else{


    Query *pQuery = pAction->pQuery;
    int bFound = 0;

    assert( pQuery->eQType==TK_SELECT || eExpr==XJD1_EXPR_ORDERBY );

    /* Search the FROM clause. */
    if( pQuery->eQType==TK_SELECT && (
          eExpr==XJD1_EXPR_RESULT  || eExpr==XJD1_EXPR_WHERE ||
          eExpr==XJD1_EXPR_GROUPBY || eExpr==XJD1_EXPR_HAVING ||
          eExpr==XJD1_EXPR_ORDERBY
    )){
      int iDatasrc = xjd1DataSrcResolve(pQuery->u.simple.pFrom, zDoc);
      if( iDatasrc ){
        p->u.id.iDatasrc = iDatasrc;
        bFound = 1;
      }
    }

    /* Match against any 'AS' alias on the query result */
    if( bFound==0 && pQuery->zAs ){
      if( eExpr==XJD1_EXPR_ORDERBY 
       || eExpr==XJD1_EXPR_HAVING
       || (eExpr==XJD1_EXPR_WHERE && pQuery->u.simple.pAgg==0)
      ){
        if( 0==strcmp(zDoc, pQuery->zAs) ){
          bFound = 1;
        }
      }
    }

    if( bFound ){
      p->u.id.pQuery = pQuery;
      return XJD1_OK;
    }
  }


  xjd1StmtError(pAction->pStmt, XJD1_ERROR, "no such object: %s", zDoc);
  return XJD1_ERROR;
}

/*
** Callback for query expressions
*/
static int walkInitCallback(Expr *p, WalkAction *pAction){
  int rc = XJD1_OK;

  assert( p );
  p->pStmt = pAction->pStmt;
  p->pQuery = pAction->pQuery;
  switch( p->eClass ){
    case XJD1_EXPR_Q:
      rc = xjd1QueryInit(p->u.subq.p, pAction->pStmt, pAction->pQuery);
      break;

    case XJD1_EXPR_FUNC: {
      int eExpr = *(int *)pAction->pArg;
      rc = xjd1FunctionInit(p, pAction->pStmt, pAction->pQuery, eExpr);
      break;
    }

    case XJD1_EXPR_TK: {
      rc = exprResolve(p, pAction);
      break;
    }

    default:
      break;
  }
  
................................................................................
** Initialize an expression in preparation for evaluation of a
** statement.
*/
int xjd1ExprInit(
  Expr *p,                        /* Expression to initialize */
  xjd1_stmt *pStmt,               /* Statement expression belongs to */
  Query *pQuery,                  /* Query expression belongs to (or NULL) */
  int eExpr                       /* How the expression features in the query */

){
  WalkAction sAction;
  memset(&sAction, 0, sizeof(sAction));
  sAction.xNodeAction = walkInitCallback;
  sAction.pStmt = pStmt;
  sAction.pQuery = pQuery;
  sAction.pArg = (void *)&eExpr;

  return walkExpr(p, &sAction);
}

/*
** Initialize a list of expression in preparation for evaluation of a
** statement.
*/
int xjd1ExprListInit(ExprList *p, xjd1_stmt *pStmt, Query *pQuery, int eExpr){
  WalkAction sAction;






  assert( eExpr==0 || pQuery );
  memset(&sAction, 0, sizeof(sAction));
  sAction.xNodeAction = walkInitCallback;
  sAction.pStmt = pStmt;
  sAction.pQuery = pQuery;
  sAction.pArg = (int *)&eExpr;


  return walkExprList(p, &sAction);
}


/* Walker callback for ExprClose() */
static int walkCloseQueryCallback(Expr *p, WalkAction *pAction){
  assert( p );

  assert( p->eType==TK_SELECT );
  return xjd1QueryClose(p->u.subq.p);


}

/*
** Close all subqueries in an expression.
*/
int xjd1ExprClose(Expr *p){
  WalkAction sAction;
  memset(&sAction, 0, sizeof(sAction));
  sAction.xQueryAction = walkCloseQueryCallback;
  return walkExpr(p,&sAction);
}

/*
** Close all subqueries in an expression list.
*/
int xjd1ExprListClose(ExprList *p){
  WalkAction sAction;
  memset(&sAction, 0, sizeof(sAction));
  sAction.xQueryAction = walkCloseQueryCallback;
  return walkExprList(p,&sAction);
}

/*
** Return true if the JSON object is a string
*/
static int isStr(const JsonNode *p){
  if( p==0 ) return 0;
................................................................................
      xjd1JsonFree(pJRight);
      if( pRes==0 ) pRes = nullJson();
      return pRes;
    }

    case TK_ID: {
      if( p->pQuery ){
        return xjd1QueryDoc(p->pQuery, p->u.id.iDatasrc);
      }else{
        return xjd1StmtDoc(p->pStmt);
      }
    }

    /* The following two logical operators work in the same way as their
    ** javascript counterparts. i.e.
................................................................................
      return pRes;
    }

    case TK_FUNCTION: {
      pRes = xjd1FunctionEval(p);
      return pRes;
    }




















  }


  pRes = xjd1JsonNew(0);
  if( pRes==0 ) return 0;
  pRes->eJType = XJD1_NULL;
  switch( p->eType ){
................................................................................
    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);
  }
}








>

<
>
>

|
|
<
<
|
|
|
>



|




|



|








|


|
<
<


|
|



|
|
|







|
<
<
<
<
<
<







|



|



|






|
|


|



|













>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|






|

>

|
|


|



<
|




|







 







|
>

<
|
|
|
|
|
>
|






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




|
<
>
|
|
>
>






<
<
|
<






<
<
<
|







 







|







 







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







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
14
15
16
17
18
19
20
21
22
23

24
25
26
27
28


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75






76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

178
179
180
181
182
183
184
185
186
187
188
189
190
...
196
197
198
199
200
201
202
203
204
205

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227


228
229

230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245
246
247
248


249

250
251
252
253
254
255



256
257
258
259
260
261
262
263
...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
...
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
...
825
826
827
828
829
830
831


















**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains code used to evaluate expressions at run-time.
*/
#include "xjd1Int.h"
#include <math.h>


/*

** Instances of the following structure are used when resolving document
** references in expressions.
*/
typedef struct ResolveCtx ResolveCtx;
struct ResolveCtx {


  xjd1_stmt *pStmt;               /* Statement expressions are part of */
  Query *pQuery;                  /* Query expressions are part of */
  int eExpr;                      /* Role of expression in pQuery */
  ResolveCtx *pParent;            /* NULL or parent of pQuery */
};

/* forward reference */
static int walkExpr(Expr*, int (*)(Expr *,void *), void *);

/*
** Walk an expression list 
*/
static int walkExprList(ExprList *p, int (*xFunc)(Expr *,void *), void *pCtx){
  if( p ){
    int i;
    for(i=0; i<p->nEItem; i++){
      walkExpr(p->apEItem[i].pExpr, xFunc, pCtx);
    }
  }
  return XJD1_OK;
}

/*
** Walk an expression tree
*/
static int walkExpr(Expr *p, int (*xFunc)(Expr *,void *), void *pCtx){
  int rc = XJD1_OK;
  if( p==0 ) return XJD1_OK;
  rc = xFunc(p, pCtx);


  switch( p->eClass ){
    case XJD1_EXPR_BI: {
      walkExpr(p->u.bi.pLeft, xFunc, pCtx);
      walkExpr(p->u.bi.pRight, xFunc, pCtx);
      break;
    }
    case XJD1_EXPR_TRI: {
      walkExpr(p->u.tri.pTest, xFunc, pCtx);
      walkExpr(p->u.tri.pIfTrue, xFunc, pCtx);
      walkExpr(p->u.tri.pIfFalse, xFunc, pCtx);
      break;
    }
    case XJD1_EXPR_TK: {
      /* Nothing to do */
      break;
    }
    case XJD1_EXPR_FUNC: {
      walkExprList(p->u.func.args, xFunc, pCtx);






      break;
    }
    case XJD1_EXPR_JSON: {
      /* Nothing to do */
      break;
    }
    case XJD1_EXPR_ARRAY: {
      walkExprList(p->u.ar, xFunc, pCtx);
      break;
    }
    case XJD1_EXPR_STRUCT: {
      walkExprList(p->u.st, xFunc, pCtx);
      break;
    }
    case XJD1_EXPR_LVALUE: {
      walkExpr(p->u.lvalue.pLeft, xFunc, pCtx);
      break;
    }
  }
  return rc;
}

static int exprResolve(Expr *p, ResolveCtx *pCtx){
  int eExpr = pCtx->eExpr;
  const char *zDoc;

  assert( eExpr>0 || (pCtx->pQuery==0 && pCtx->pStmt) );

  zDoc = p->u.id.zId;
  if( eExpr==0 ){
    Command *pCmd = pCtx->pStmt->pCmd;
    switch( pCmd->eCmdType ){
      case TK_DELETE:
        if( 0==strcmp(zDoc, pCmd->u.del.zName) ) return XJD1_OK;
        break;
      case TK_UPDATE:
        if( 0==strcmp(zDoc, pCmd->u.update.zName) ) return XJD1_OK;
        break;

      default:
        assert( 0 );
        break;
    }
  }else{
    ResolveCtx *pTest;
    for(pTest=pCtx; pTest; pTest=pTest->pParent){
      Query *pQuery = pTest->pQuery;
      int bFound = 0;
  
      assert( pQuery->eQType==TK_SELECT || eExpr==XJD1_EXPR_ORDERBY );
  
      /* Search the FROM clause. */
      if( pQuery->eQType==TK_SELECT && (
            eExpr==XJD1_EXPR_RESULT  || eExpr==XJD1_EXPR_WHERE ||
            eExpr==XJD1_EXPR_GROUPBY || eExpr==XJD1_EXPR_HAVING ||
            eExpr==XJD1_EXPR_ORDERBY
      )){
        int iDatasrc = xjd1DataSrcResolve(pQuery->u.simple.pFrom, zDoc);
        if( iDatasrc ){
          p->u.id.iDatasrc = iDatasrc;
          bFound = 1;
        }
      }
  
      /* Match against any 'AS' alias on the query result */
      if( bFound==0 && pQuery->zAs ){
        if( eExpr==XJD1_EXPR_ORDERBY 
         || eExpr==XJD1_EXPR_HAVING
         || (eExpr==XJD1_EXPR_WHERE && pQuery->u.simple.pAgg==0)
        ){
          if( 0==strcmp(zDoc, pQuery->zAs) ){
            bFound = 1;
          }
        }
      }
  
      if( bFound ){
        p->u.id.pQuery = pQuery;
        return XJD1_OK;
      }
    }
  }

  xjd1StmtError(pCtx->pStmt, XJD1_ERROR, "no such object: %s", zDoc);
  return XJD1_ERROR;
}

/*
** Callback for query expressions
*/
static int walkInitCallback(Expr *p, void *pArg){
  int rc = XJD1_OK;
  ResolveCtx *pCtx = (ResolveCtx *)pArg;
  assert( p );
  p->pStmt = pCtx->pStmt;
  p->pQuery = pCtx->pQuery;
  switch( p->eClass ){
    case XJD1_EXPR_Q:
      rc = xjd1QueryInit(p->u.subq.p, pCtx->pStmt, pArg);
      break;

    case XJD1_EXPR_FUNC: {

      rc = xjd1FunctionInit(p, pCtx->pStmt, pCtx->pQuery, pCtx->eExpr);
      break;
    }

    case XJD1_EXPR_TK: {
      rc = exprResolve(p, pCtx);
      break;
    }

    default:
      break;
  }
  
................................................................................
** Initialize an expression in preparation for evaluation of a
** statement.
*/
int xjd1ExprInit(
  Expr *p,                        /* Expression to initialize */
  xjd1_stmt *pStmt,               /* Statement expression belongs to */
  Query *pQuery,                  /* Query expression belongs to (or NULL) */
  int eExpr,                      /* How the expression features in the query */
  void *pCtx                      /* Parent resolution context */
){

  ResolveCtx sCtx;
  assert( pQuery==0 || pQuery->pStmt==pStmt );
  sCtx.pStmt = pStmt;
  sCtx.pQuery = pQuery;
  sCtx.eExpr = eExpr;
  sCtx.pParent = (ResolveCtx *)pCtx;
  return walkExpr(p, walkInitCallback, (void *)&sCtx);
}

/*
** Initialize a list of expression in preparation for evaluation of a
** statement.
*/
int xjd1ExprListInit(
  ExprList *p,                    /* List of expressions to initialize */
  xjd1_stmt *pStmt,               /* Statement expressions belong to */
  Query *pQuery,                  /* Query expressions belong to (or NULL) */
  int eExpr,                      /* How the expressions feature in the query */
  void *pCtx                      /* Parent resolution context */
){
  ResolveCtx sCtx;
  assert( pQuery==0 || pQuery->pStmt==pStmt );


  sCtx.pStmt = pStmt;
  sCtx.pQuery = pQuery;

  sCtx.eExpr = eExpr;
  sCtx.pParent = (ResolveCtx *)pCtx;
  return walkExprList(p, walkInitCallback, (void *)&sCtx);
}


/* Walker callback for ExprClose() */
static int walkCloseQueryCallback(Expr *p, void *pCtx){

  int rc = XJD1_OK;
  if( p->eType==TK_SELECT ){
    rc = xjd1QueryClose(p->u.subq.p);
  }
  return rc;
}

/*
** Close all subqueries in an expression.
*/
int xjd1ExprClose(Expr *p){


  return walkExpr(p, walkCloseQueryCallback, 0);

}

/*
** Close all subqueries in an expression list.
*/
int xjd1ExprListClose(ExprList *p){



  return walkExprList(p, walkCloseQueryCallback, 0);
}

/*
** Return true if the JSON object is a string
*/
static int isStr(const JsonNode *p){
  if( p==0 ) return 0;
................................................................................
      xjd1JsonFree(pJRight);
      if( pRes==0 ) pRes = nullJson();
      return pRes;
    }

    case TK_ID: {
      if( p->pQuery ){
        return xjd1QueryDoc(p->u.id.pQuery, p->u.id.iDatasrc);
      }else{
        return xjd1StmtDoc(p->pStmt);
      }
    }

    /* The following two logical operators work in the same way as their
    ** javascript counterparts. i.e.
................................................................................
      return pRes;
    }

    case TK_FUNCTION: {
      pRes = xjd1FunctionEval(p);
      return pRes;
    }

    /* A scalar sub-query. The result of this is the first object 
    ** returned by executing the query. Or, if the query returns zero
    ** rows, a NULL value. 
    **
    ** TODO: Handle correlated and uncorrelated sub-queries differently.
    */
    case TK_SELECT: {
      Query *pQuery = p->u.subq.p;
      int rc;
      rc = xjd1QueryStep(pQuery);
      if( rc==XJD1_ROW ){
        pRes = xjd1QueryDoc(pQuery, 0);
      }else if( rc==XJD1_DONE ){
        pRes = xjd1JsonNew(0);
        if( pRes ) pRes->eJType = XJD1_NULL;
      }
      xjd1QueryRewind(pQuery);
      return pRes;
    }
  }


  pRes = xjd1JsonNew(0);
  if( pRes==0 ) return 0;
  pRes->eJType = XJD1_NULL;
  switch( p->eType ){
................................................................................
    rc = isTrue(pValue);
    assert( rc==1 || rc==0 );
    xjd1JsonFree(pValue);
  }
  return rc;
}



















Changes to src/query.c.

162
163
164
165
166
167
168
169




170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

196


197


198

199
200
201
202
203
204
205
...
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
  memset(pList, 0, sizeof(ResultList));
}

/*
** Called after statement parsing to initalize every Query object
** within the statement.
*/
int xjd1QueryInit(Query *p, xjd1_stmt *pStmt, Query *pOuter){




  int rc;
  if( p==0 ) return XJD1_OK;
  p->pStmt = pStmt;
  p->pOuter = pOuter;
  if( p->eQType==TK_SELECT ){
    rc = xjd1ExprInit(p->u.simple.pRes, pStmt, p, XJD1_EXPR_RESULT);
    if( !rc ){
      rc = xjd1DataSrcInit(p->u.simple.pFrom, p);
    }
    if( !rc ){
      rc = xjd1ExprInit(p->u.simple.pWhere, pStmt, p, XJD1_EXPR_WHERE);
    }
    if( !rc ){
      rc = xjd1ExprListInit(p->u.simple.pGroupBy, pStmt, p, XJD1_EXPR_GROUPBY);
    }
    if( !rc ){
      rc = xjd1ExprInit(p->u.simple.pHaving, pStmt, p, XJD1_EXPR_HAVING);
    }
    if( !rc && p->u.simple.pGroupBy ){ 
      rc = xjd1AggregateInit(pStmt, p, 0);
    }
  }else{
    rc = xjd1QueryInit(p->u.compound.pLeft, pStmt, pOuter);
    if( !rc ) rc = xjd1QueryInit(p->u.compound.pRight, pStmt, pOuter);
  }


  if( !rc ) rc = xjd1ExprListInit(p->pOrderBy, pStmt, p, XJD1_EXPR_ORDERBY);


  if( !rc ) rc = xjd1ExprInit(p->pLimit, pStmt, p, XJD1_EXPR_LIMIT);


  if( !rc ) rc = xjd1ExprInit(p->pOffset, pStmt, p, XJD1_EXPR_OFFSET);

  return rc;
}

/*
** Rewind a query so that it is pointing at the first row.
*/
int xjd1QueryRewind(Query *p){
................................................................................
*/
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);

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







|
>
>
>
>



<

|




|


|


|





|
|


>
|
>
>
|
>
>
|
>







 







<
<
<
<







162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
...
551
552
553
554
555
556
557




558
559
560
561
562
563
564
  memset(pList, 0, sizeof(ResultList));
}

/*
** Called after statement parsing to initalize every Query object
** within the statement.
*/
int xjd1QueryInit(
  Query *p,                       /* Query to initialize */
  xjd1_stmt *pStmt,               /* Statement this query is part of */
  void *pCtx                      /* Context for expression initialization */
){
  int rc;
  if( p==0 ) return XJD1_OK;
  p->pStmt = pStmt;

  if( p->eQType==TK_SELECT ){
    rc = xjd1ExprInit(p->u.simple.pRes, pStmt, p, XJD1_EXPR_RESULT, pCtx);
    if( !rc ){
      rc = xjd1DataSrcInit(p->u.simple.pFrom, p);
    }
    if( !rc ){
      rc = xjd1ExprInit(p->u.simple.pWhere, pStmt, p, XJD1_EXPR_WHERE, pCtx);
    }
    if( !rc ){
      rc = xjd1ExprListInit(p->u.simple.pGroupBy, pStmt, p, XJD1_EXPR_GROUPBY, pCtx);
    }
    if( !rc ){
      rc = xjd1ExprInit(p->u.simple.pHaving, pStmt, p, XJD1_EXPR_HAVING, pCtx);
    }
    if( !rc && p->u.simple.pGroupBy ){ 
      rc = xjd1AggregateInit(pStmt, p, 0);
    }
  }else{
    rc = xjd1QueryInit(p->u.compound.pLeft, pStmt, pCtx);
    if( !rc ) rc = xjd1QueryInit(p->u.compound.pRight, pStmt, pCtx);
  }

  if( !rc ){
    rc = xjd1ExprListInit(p->pOrderBy, pStmt, p, XJD1_EXPR_ORDERBY, pCtx);
  }
  if( !rc ){
    rc = xjd1ExprInit(p->pLimit, pStmt, p, XJD1_EXPR_LIMIT, pCtx);
  }
  if( !rc ){
    rc = xjd1ExprInit(p->pOffset, pStmt, p, XJD1_EXPR_OFFSET, pCtx);
  }
  return rc;
}

/*
** Rewind a query so that it is pointing at the first row.
*/
int xjd1QueryRewind(Query *p){
................................................................................
*/
static int selectStepOrdered(Query *p){
  int rc = XJD1_OK;                    /* Return Code */
  ExprList *pOrderBy = p->pOrderBy;    /* ORDER BY clause (or NULL) */

  if( pOrderBy ){





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

Changes to src/stmt.c.

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        break;
      }
      case TK_INSERT: {
        xjd1QueryInit(pCmd->u.ins.pQuery, p, 0);
        break;
      }
      case TK_DELETE: {
        xjd1ExprInit(pCmd->u.del.pWhere, p, 0, 0);
        break;
      }
      case TK_UPDATE: {
        xjd1ExprInit(pCmd->u.update.pWhere, p, 0, 0);
        xjd1ExprListInit(pCmd->u.update.pChng, p, 0, 0);
        xjd1ExprInit(pCmd->u.update.pUpsert, p, 0, 0);
        break;
      }
    }

    if( p->errCode ){
      xjd1Error(pConn, p->errCode, "%s", p->errMsg.zBuf);
      rc = p->errCode;







|



|
|
|







52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        break;
      }
      case TK_INSERT: {
        xjd1QueryInit(pCmd->u.ins.pQuery, p, 0);
        break;
      }
      case TK_DELETE: {
        xjd1ExprInit(pCmd->u.del.pWhere, p, 0, 0, 0);
        break;
      }
      case TK_UPDATE: {
        xjd1ExprInit(pCmd->u.update.pWhere, p, 0, 0, 0);
        xjd1ExprListInit(pCmd->u.update.pChng, p, 0, 0, 0);
        xjd1ExprInit(pCmd->u.update.pUpsert, p, 0, 0, 0);
        break;
      }
    }

    if( p->errCode ){
      xjd1Error(pConn, p->errCode, "%s", p->errMsg.zBuf);
      rc = p->errCode;

Changes to src/xjd1Int.h.

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
...
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  } *aAggExpr;
};

/* A query statement */
struct Query {
  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 */
................................................................................
int xjd1DataSrcResolve(DataSrc *, const char *zDocname);
JsonNode *xjd1DataSrcRead(DataSrc *, int);

/******************************** delete.c ***********************************/
int xjd1DeleteStep(xjd1_stmt*);

/******************************** expr.c *************************************/
int xjd1ExprInit(Expr*, xjd1_stmt*, Query*, int);
int xjd1ExprListInit(ExprList*, xjd1_stmt*, Query*, int);
JsonNode *xjd1ExprEval(Expr*);
int xjd1ExprTrue(Expr*);
int xjd1ExprClose(Expr*);
int xjd1ExprListClose(ExprList*);
/* Candidates for the 4th parameter to xjd1ExprInit() */
#define XJD1_EXPR_RESULT  1
#define XJD1_EXPR_WHERE   2
................................................................................
void *xjd1PoolMallocZero(Pool*, int);
char *xjd1PoolDup(Pool*, const char *, int);

/******************************** pragma.c ***********************************/
int xjd1PragmaStep(xjd1_stmt*);

/******************************** query.c ************************************/
int xjd1QueryInit(Query*,xjd1_stmt*,Query*);
int xjd1QueryRewind(Query*);
int xjd1QueryStep(Query*);
int xjd1QueryClose(Query*);
JsonNode *xjd1QueryDoc(Query*, int);

/******************************** stmt.c *************************************/
JsonNode *xjd1StmtDoc(xjd1_stmt*);







<







 







|
|







 







|







279
280
281
282
283
284
285

286
287
288
289
290
291
292
...
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
...
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
  } *aAggExpr;
};

/* A query statement */
struct Query {
  int eQType;                   /* Query type */
  xjd1_stmt *pStmt;             /* Statement this query is part of */

  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 */
................................................................................
int xjd1DataSrcResolve(DataSrc *, const char *zDocname);
JsonNode *xjd1DataSrcRead(DataSrc *, int);

/******************************** delete.c ***********************************/
int xjd1DeleteStep(xjd1_stmt*);

/******************************** expr.c *************************************/
int xjd1ExprInit(Expr*, xjd1_stmt*, Query*, int, void *);
int xjd1ExprListInit(ExprList*, xjd1_stmt*, Query*, int, void *);
JsonNode *xjd1ExprEval(Expr*);
int xjd1ExprTrue(Expr*);
int xjd1ExprClose(Expr*);
int xjd1ExprListClose(ExprList*);
/* Candidates for the 4th parameter to xjd1ExprInit() */
#define XJD1_EXPR_RESULT  1
#define XJD1_EXPR_WHERE   2
................................................................................
void *xjd1PoolMallocZero(Pool*, int);
char *xjd1PoolDup(Pool*, const char *, int);

/******************************** pragma.c ***********************************/
int xjd1PragmaStep(xjd1_stmt*);

/******************************** query.c ************************************/
int xjd1QueryInit(Query*,xjd1_stmt*,void*);
int xjd1QueryRewind(Query*);
int xjd1QueryStep(Query*);
int xjd1QueryClose(Query*);
JsonNode *xjd1QueryDoc(Query*, int);

/******************************** stmt.c *************************************/
JsonNode *xjd1StmtDoc(xjd1_stmt*);

Changes to test/base07.test.

18
19
20
21
22
23
24



25



26
















SELECT count(c2) FROM c1 AS c2;
.error 4

.testcase 4
SELECT count(c1) FROM c1 AS c2;
.error ERROR no such object: c1
































>
>
>

>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
SELECT count(c2) FROM c1 AS c2;
.error 4

.testcase 4
SELECT count(c1) FROM c1 AS c2;
.error ERROR no such object: c1

.testcase 5
SELECT (SELECT 1);
.result 1

.testcase 6
SELECT (SELECT c1.i FROM c1);
.result 3

.testcase 7
SELECT (SELECT c1.i FROM c1) FROM c1;
.result 3 3 3 3

.testcase 8
SELECT (SELECT c1.i) FROM c1;
.result 3 1 2 4

.testcase 7
SELECT 
  (SELECT inner.z FROM c1 AS inner WHERE inner.i==c1.i+1) 
FROM c1 ORDER BY c1.i;
.result "two" "three" "four" null