1
2
3
4
5
6
7
8
9
10
11
12
13
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
|
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite.
**
** $Id: expr.c,v 1.59 2002/05/15 08:30:13 danielk1977 Exp $
*/
#include "sqliteInt.h"
/*
** Construct a new expression node and return a pointer to it. Memory
** for this node is obtained from sqliteMalloc(). The calling function
** is responsible for making sure the node eventually gets freed.
*/
Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
Expr *pNew;
pNew = sqliteMalloc( sizeof(Expr) );
if( pNew==0 ){
sqliteExprDelete(pLeft);
sqliteExprDelete(pRight);
return 0;
}
pNew->op = op;
pNew->pLeft = pLeft;
pNew->pRight = pRight;
if( pToken ){
pNew->token = *pToken;
}else{
pNew->token.z = 0;
pNew->token.n = 0;
}
if( pLeft && pRight ){
sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
}else{
pNew->span = pNew->token;
}
return pNew;
}
/*
** Set the Expr.token field of the given expression to span all
** text between the two given tokens.
*/
void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
if( pExpr ){
pExpr->span.z = pLeft->z;
pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
}
}
/*
** Construct a new expression node for a function with multiple
** arguments.
*/
Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
Expr *pNew;
pNew = sqliteMalloc( sizeof(Expr) );
if( pNew==0 ){
sqliteExprListDelete(pList);
return 0;
}
pNew->op = TK_FUNCTION;
pNew->pList = pList;
if( pToken ){
pNew->token = *pToken;
}else{
pNew->token.z = 0;
pNew->token.n = 0;
}
return pNew;
}
/*
** Recursively delete an expression tree.
*/
void sqliteExprDelete(Expr *p){
if( p==0 ) return;
if( p->pLeft ) sqliteExprDelete(p->pLeft);
if( p->pRight ) sqliteExprDelete(p->pRight);
if( p->pList ) sqliteExprListDelete(p->pList);
if( p->pSelect ) sqliteSelectDelete(p->pSelect);
sqliteFree(p);
}
/*
** The following group of functions are used to translate the string
** pointers of tokens in expression from one buffer to another.
**
** Normally, the Expr.token.z and Expr.span.z fields point into the
** original input buffer of an SQL statement. This is usually OK
** since the SQL statement is executed and the expression is deleted
** before the input buffer is freed. Making the tokens point to the
** original input buffer saves many calls to malloc() and thus helps
** the library to run faster.
**
** But sometimes we need an expression to persist past the time when
** the input buffer is freed. (Example: The SELECT clause of a
** CREATE VIEW statement contains expressions that must persist for
** the life of the view.) When that happens we have to make a
** persistent copy of the input buffer and translate the Expr.token.z
** and Expr.span.z fields to point to the copy rather than the
** original input buffer. The following group of routines handle that
** translation.
**
** The "offset" parameter is the distance from the original input buffer
** to the persistent copy. These routines recursively walk the entire
** expression tree and shift all tokens by "offset" amount.
**
** The work of figuring out the appropriate "offset" and making the
** presistent copy of the input buffer is done by the calling routine.
*/
void sqliteExprMoveStrings(Expr *p, int offset){
if( p==0 ) return;
if( p->token.z ) p->token.z += offset;
if( p->span.z ) p->span.z += offset;
if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset);
if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset);
if( p->pList ) sqliteExprListMoveStrings(p->pList, offset);
if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset);
}
void sqliteExprListMoveStrings(ExprList *pList, int offset){
int i;
if( pList==0 ) return;
for(i=0; i<pList->nExpr; i++){
sqliteExprMoveStrings(pList->a[i].pExpr, offset);
}
}
void sqliteSelectMoveStrings(Select *pSelect, int offset){
if( pSelect==0 ) return;
sqliteExprListMoveStrings(pSelect->pEList, offset);
sqliteExprMoveStrings(pSelect->pWhere, offset);
sqliteExprListMoveStrings(pSelect->pGroupBy, offset);
sqliteExprMoveStrings(pSelect->pHaving, offset);
sqliteExprListMoveStrings(pSelect->pOrderBy, offset);
sqliteSelectMoveStrings(pSelect->pPrior, offset);
}
/*
** The following group of routines make deep copies of expressions,
** expression lists, ID lists, and select statements. The copies can
** be deleted (by being passed to their respective ...Delete() routines)
** without effecting the originals.
**
** Note, however, that the Expr.token.z and Expr.span.z fields point to
** string space that is allocated separately from the expression tree
** itself. These routines do NOT duplicate that string space.
**
** The expression list and ID list return by sqliteExprListDup() and
** sqliteIdListDup() can not be further expanded by subsequent calls
** to sqliteExprListAppend() or sqliteIdListAppend().
**
** Any tables that the ID list might point to are not duplicated.
*/
Expr *sqliteExprDup(Expr *p){
Expr *pNew;
if( p==0 ) return 0;
pNew = sqliteMalloc( sizeof(*p) );
if( pNew==0 ) return 0;
pNew->op = p->op;
pNew->pLeft = sqliteExprDup(p->pLeft);
pNew->pRight = sqliteExprDup(p->pRight);
pNew->pList = sqliteExprListDup(p->pList);
pNew->iTable = p->iTable;
pNew->iColumn = p->iColumn;
pNew->iAgg = p->iAgg;
pNew->token = p->token;
pNew->span = p->span;
pNew->pSelect = sqliteSelectDup(p->pSelect);
return pNew;
}
ExprList *sqliteExprListDup(ExprList *p){
ExprList *pNew;
int i;
if( p==0 ) return 0;
pNew = sqliteMalloc( sizeof(*pNew) );
if( pNew==0 ) return 0;
pNew->nExpr = p->nExpr;
pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
for(i=0; i<p->nExpr; i++){
pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr);
pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
pNew->a[i].sortOrder = p->a[i].sortOrder;
pNew->a[i].isAgg = p->a[i].isAgg;
pNew->a[i].done = 0;
}
return pNew;
}
IdList *sqliteIdListDup(IdList *p){
IdList *pNew;
int i;
if( p==0 ) return 0;
pNew = sqliteMalloc( sizeof(*pNew) );
if( pNew==0 ) return 0;
pNew->nId = p->nId;
pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
for(i=0; i<p->nId; i++){
pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
pNew->a[i].idx = p->a[i].idx;
pNew->a[i].pTab = 0;
pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
}
return pNew;
}
Select *sqliteSelectDup(Select *p){
Select *pNew;
if( p==0 ) return 0;
pNew = sqliteMalloc( sizeof(*p) );
if( pNew==0 ) return 0;
pNew->isDistinct = p->isDistinct;
pNew->pEList = sqliteExprListDup(p->pEList);
pNew->pSrc = sqliteIdListDup(p->pSrc);
pNew->pWhere = sqliteExprDup(p->pWhere);
pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
pNew->pHaving = sqliteExprDup(p->pHaving);
pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
pNew->op = p->op;
pNew->pPrior = sqliteSelectDup(p->pPrior);
pNew->nLimit = p->nLimit;
pNew->nOffset = p->nOffset;
pNew->zSelect = 0;
return pNew;
}
/*
** Add a new element to the end of an expression list. If pList is
** initially NULL, then create a new expression list.
*/
ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
int i;
if( pList==0 ){
pList = sqliteMalloc( sizeof(ExprList) );
if( pList==0 ){
sqliteExprDelete(pExpr);
return 0;
}
}
if( (pList->nExpr & 7)==0 ){
int n = pList->nExpr + 8;
struct ExprList_item *a;
a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
if( a==0 ){
sqliteExprDelete(pExpr);
return pList;
}
pList->a = a;
}
if( pExpr || pName ){
i = pList->nExpr++;
pList->a[i].pExpr = pExpr;
pList->a[i].zName = 0;
if( pName ){
sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
sqliteDequote(pList->a[i].zName);
}
}
return pList;
}
/*
** Delete an entire expression list.
*/
void sqliteExprListDelete(ExprList *pList){
int i;
if( pList==0 ) return;
for(i=0; i<pList->nExpr; i++){
sqliteExprDelete(pList->a[i].pExpr);
sqliteFree(pList->a[i].zName);
}
sqliteFree(pList->a);
sqliteFree(pList);
}
/*
** Walk an expression tree. Return 1 if the expression is constant
** and 0 if it involves variables.
*/
int sqliteExprIsConstant(Expr *p){
switch( p->op ){
case TK_ID:
case TK_COLUMN:
case TK_DOT:
return 0;
case TK_INTEGER:
case TK_FLOAT:
case TK_STRING:
return 1;
default: {
if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
if( p->pList ){
int i;
for(i=0; i<p->pList->nExpr; i++){
if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
}
}
return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
}
}
return 0;
}
/*
** Return TRUE if the given string is a row-id column name.
*/
static int sqliteIsRowid(const char *z){
if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
if( sqliteStrICmp(z, "OID")==0 ) return 1;
return 0;
}
/*
** This routine walks an expression tree and resolves references to
** table columns. Nodes of the form ID.ID or ID resolve into an
** index to the table in the table list and a column offset. The
** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
** value is changed to the index of the referenced table in pTabList
** plus the "base" value. The base value will ultimately become the
** VDBE cursor number for a cursor that is pointing into the referenced
** table. The Expr.iColumn value is changed to the index of the column
** of the referenced table. The Expr.iColumn value for the special
** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
** alias for ROWID.
**
** We also check for instances of the IN operator. IN comes in two
** forms:
**
** expr IN (exprlist)
** and
** expr IN (SELECT ...)
**
** The first form is handled by creating a set holding the list
** of allowed values. The second form causes the SELECT to generate
** a temporary table.
**
** This routine also looks for scalar SELECTs that are part of an expression.
** If it finds any, it generates code to write the value of that select
** into a memory cell.
**
** Unknown columns or tables provoke an error. The function returns
** the number of errors seen and leaves an error message on pParse->zErrMsg.
*/
int sqliteExprResolveIds(
Parse *pParse, /* The parser context */
int base, /* VDBE cursor number for first entry in pTabList */
IdList *pTabList, /* List of tables used to resolve column names */
ExprList *pEList, /* List of expressions used to resolve "AS" */
Expr *pExpr /* The expression to be analyzed. */
){
if( pExpr==0 || pTabList==0 ) return 0;
assert( base+pTabList->nId<=pParse->nTab );
switch( pExpr->op ){
/* A lone identifier. Try and match it as follows:
**
** 1. To the name of a column of one of the tables in pTabList
**
** 2. To the right side of an AS keyword in the column list of
** a SELECT statement. (For example, match against 'x' in
** "SELECT a+b AS 'x' FROM t1".)
**
** 3. One of the special names "ROWID", "OID", or "_ROWID_".
*/
case TK_ID: {
int cnt = 0; /* Number of matches */
int i; /* Loop counter */
char *z;
assert( pExpr->token.z );
z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
sqliteDequote(z);
if( z==0 ) return 1;
for(i=0; i<pTabList->nId; i++){
int j;
Table *pTab = pTabList->a[i].pTab;
if( pTab==0 ) continue;
assert( pTab->nCol>0 );
for(j=0; j<pTab->nCol; j++){
if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
cnt++;
pExpr->iTable = i + base;
if( j==pTab->iPKey ){
/* Substitute the record number for the INTEGER PRIMARY KEY */
pExpr->iColumn = -1;
}else{
pExpr->iColumn = j;
}
pExpr->op = TK_COLUMN;
}
}
}
if( cnt==0 && pEList!=0 ){
int j;
for(j=0; j<pEList->nExpr; j++){
char *zAs = pEList->a[j].zName;
if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
cnt++;
assert( pExpr->pLeft==0 && pExpr->pRight==0 );
pExpr->op = TK_AS;
pExpr->iColumn = j;
pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
}
}
}
if( cnt==0 && sqliteIsRowid(z) ){
pExpr->iColumn = -1;
pExpr->iTable = base;
cnt = 1 + (pTabList->nId>1);
pExpr->op = TK_COLUMN;
}
sqliteFree(z);
if( cnt==0 ){
sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
pExpr->token.z, pExpr->token.n, 0);
pParse->nErr++;
return 1;
}else if( cnt>1 ){
sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
pExpr->token.z, pExpr->token.n, 0);
pParse->nErr++;
return 1;
}
break;
}
/* A table name and column name: ID.ID */
case TK_DOT: {
int cnt = 0; /* Number of matches */
int cntTab = 0; /* Number of matching tables */
int i; /* Loop counter */
Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
char *zLeft, *zRight; /* Text of an identifier */
pLeft = pExpr->pLeft;
pRight = pExpr->pRight;
assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
assert( pRight && pRight->op==TK_ID && pRight->token.z );
zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
if( zLeft==0 || zRight==0 ){
sqliteFree(zLeft);
sqliteFree(zRight);
return 1;
}
sqliteDequote(zLeft);
sqliteDequote(zRight);
pExpr->iTable = -1;
for(i=0; i<pTabList->nId; i++){
int j;
char *zTab;
Table *pTab = pTabList->a[i].pTab;
if( pTab==0 ) continue;
assert( pTab->nCol>0 );
if( pTabList->a[i].zAlias ){
zTab = pTabList->a[i].zAlias;
}else{
zTab = pTab->zName;
}
if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
if( 0==(cntTab++) ) pExpr->iTable = i + base;
for(j=0; j<pTab->nCol; j++){
if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
cnt++;
pExpr->iTable = i + base;
if( j==pTab->iPKey ){
/* Substitute the record number for the INTEGER PRIMARY KEY */
pExpr->iColumn = -1;
}else{
pExpr->iColumn = j;
}
}
}
}
/* If we have not already resolved this *.* expression, then maybe
* it is a new.* or old.* trigger argument reference */
if (cnt == 0 && pParse->trigStack != 0) {
TriggerStack * tt = pParse->trigStack;
int j;
int t = 0;
if (tt->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0) {
pExpr->iTable = tt->newIdx;
cntTab++;
t = 1;
}
if (tt->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0) {
pExpr->iTable = tt->oldIdx;
cntTab++;
t = 1;
}
if (t)
for(j=0; j<tt->pTab->nCol; j++) {
if( sqliteStrICmp(tt->pTab->aCol[j].zName, zRight)==0 ){
cnt++;
pExpr->iColumn = j;
}
}
}
if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
cnt = 1;
pExpr->iColumn = -1;
}
sqliteFree(zLeft);
sqliteFree(zRight);
if( cnt==0 ){
sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
pLeft->token.z, pLeft->token.n, ".", 1,
pRight->token.z, pRight->token.n, 0);
pParse->nErr++;
return 1;
}else if( cnt>1 ){
sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
pLeft->token.z, pLeft->token.n, ".", 1,
pRight->token.z, pRight->token.n, 0);
pParse->nErr++;
return 1;
}
sqliteExprDelete(pLeft);
pExpr->pLeft = 0;
sqliteExprDelete(pRight);
pExpr->pRight = 0;
pExpr->op = TK_COLUMN;
break;
}
case TK_IN: {
Vdbe *v = sqliteGetVdbe(pParse);
if( v==0 ) return 1;
if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
return 1;
}
if( pExpr->pSelect ){
/* Case 1: expr IN (SELECT ...)
**
** Generate code to write the results of the select into a temporary
** table. The cursor number of the temporary table has already
** been put in iTable by sqliteExprResolveInSelect().
*/
pExpr->iTable = pParse->nTab++;
sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
}else if( pExpr->pList ){
/* Case 2: expr IN (exprlist)
**
** Create a set to put the exprlist values in. The Set id is stored
** in iTable.
*/
int i, iSet;
for(i=0; i<pExpr->pList->nExpr; i++){
Expr *pE2 = pExpr->pList->a[i].pExpr;
if( !sqliteExprIsConstant(pE2) ){
sqliteSetString(&pParse->zErrMsg,
"right-hand side of IN operator must be constant", 0);
pParse->nErr++;
return 1;
}
if( sqliteExprCheck(pParse, pE2, 0, 0) ){
return 1;
}
}
iSet = pExpr->iTable = pParse->nSet++;
for(i=0; i<pExpr->pList->nExpr; i++){
Expr *pE2 = pExpr->pList->a[i].pExpr;
switch( pE2->op ){
case TK_FLOAT:
case TK_INTEGER:
case TK_STRING: {
int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
assert( pE2->token.z );
sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
sqliteVdbeDequoteP3(v, addr);
break;
}
default: {
sqliteExprCode(pParse, pE2);
sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
break;
}
}
}
}
break;
}
case TK_SELECT: {
/* This has to be a scalar SELECT. Generate code to put the
** value of this select in a memory cell and record the number
** of the memory cell in iColumn.
*/
pExpr->iColumn = pParse->nMem++;
if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
return 1;
}
break;
}
/* For all else, just recursively walk the tree */
default: {
if( pExpr->pLeft
&& sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
return 1;
}
if( pExpr->pRight
&& sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
return 1;
}
if( pExpr->pList ){
int i;
ExprList *pList = pExpr->pList;
for(i=0; i<pList->nExpr; i++){
Expr *pArg = pList->a[i].pExpr;
if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
return 1;
}
}
}
}
}
return 0;
}
/*
** Error check the functions in an expression. Make sure all
** function names are recognized and all functions have the correct
** number of arguments. Leave an error message in pParse->zErrMsg
** if anything is amiss. Return the number of errors.
**
** if pIsAgg is not null and this expression is an aggregate function
** (like count(*) or max(value)) then write a 1 into *pIsAgg.
*/
int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
int nErr = 0;
if( pExpr==0 ) return 0;
switch( pExpr->op ){
case TK_FUNCTION: {
int n = pExpr->pList ? pExpr->pList->nExpr : 0;
int no_such_func = 0;
int wrong_num_args = 0;
int is_agg = 0;
int i;
FuncDef *pDef;
pDef = sqliteFindFunction(pParse->db,
pExpr->token.z, pExpr->token.n, n, 0);
if( pDef==0 ){
pDef = sqliteFindFunction(pParse->db,
pExpr->token.z, pExpr->token.n, -1, 0);
if( pDef==0 ){
no_such_func = 1;
}else{
wrong_num_args = 1;
}
}else{
is_agg = pDef->xFunc==0;
}
if( is_agg && !allowAgg ){
sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
pExpr->token.z, pExpr->token.n, "()", 2, 0);
pParse->nErr++;
nErr++;
is_agg = 0;
}else if( no_such_func ){
sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
pExpr->token.z, pExpr->token.n, 0);
pParse->nErr++;
nErr++;
}else if( wrong_num_args ){
sqliteSetNString(&pParse->zErrMsg,
"wrong number of arguments to function ",-1,
pExpr->token.z, pExpr->token.n, "()", 2, 0);
pParse->nErr++;
nErr++;
}
if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
if( is_agg && pIsAgg ) *pIsAgg = 1;
for(i=0; nErr==0 && i<n; i++){
nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
allowAgg && !is_agg, pIsAgg);
}
}
default: {
if( pExpr->pLeft ){
nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
}
if( nErr==0 && pExpr->pRight ){
nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
}
if( nErr==0 && pExpr->pList ){
int n = pExpr->pList->nExpr;
int i;
for(i=0; nErr==0 && i<n; i++){
Expr *pE2 = pExpr->pList->a[i].pExpr;
nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
}
}
break;
}
}
return nErr;
}
/*
** Generate code into the current Vdbe to evaluate the given
** expression and leave the result on the top of stack.
*/
void sqliteExprCode(Parse *pParse, Expr *pExpr){
Vdbe *v = pParse->pVdbe;
int op;
if( v==0 || pExpr==0 ) return;
switch( pExpr->op ){
case TK_PLUS: op = OP_Add; break;
case TK_MINUS: op = OP_Subtract; break;
case TK_STAR: op = OP_Multiply; break;
case TK_SLASH: op = OP_Divide; break;
case TK_AND: op = OP_And; break;
case TK_OR: op = OP_Or; break;
case TK_LT: op = OP_Lt; break;
case TK_LE: op = OP_Le; break;
case TK_GT: op = OP_Gt; break;
case TK_GE: op = OP_Ge; break;
case TK_NE: op = OP_Ne; break;
case TK_EQ: op = OP_Eq; break;
case TK_ISNULL: op = OP_IsNull; break;
case TK_NOTNULL: op = OP_NotNull; break;
case TK_NOT: op = OP_Not; break;
case TK_UMINUS: op = OP_Negative; break;
case TK_BITAND: op = OP_BitAnd; break;
case TK_BITOR: op = OP_BitOr; break;
case TK_BITNOT: op = OP_BitNot; break;
case TK_LSHIFT: op = OP_ShiftLeft; break;
case TK_RSHIFT: op = OP_ShiftRight; break;
case TK_REM: op = OP_Remainder; break;
default: break;
}
switch( pExpr->op ){
case TK_COLUMN: {
if( pParse->useAgg ){
sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
}else if( pExpr->iColumn>=0 ){
sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
}else{
sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
}
break;
}
case TK_INTEGER: {
sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
break;
}
case TK_FLOAT: {
sqliteVdbeAddOp(v, OP_String, 0, 0);
assert( pExpr->token.z );
sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
break;
}
case TK_STRING: {
int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
assert( pExpr->token.z );
sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
sqliteVdbeDequoteP3(v, addr);
break;
}
case TK_NULL: {
sqliteVdbeAddOp(v, OP_String, 0, 0);
break;
}
case TK_AND:
case TK_OR:
case TK_PLUS:
case TK_STAR:
case TK_MINUS:
case TK_REM:
case TK_BITAND:
case TK_BITOR:
case TK_SLASH: {
sqliteExprCode(pParse, pExpr->pLeft);
sqliteExprCode(pParse, pExpr->pRight);
sqliteVdbeAddOp(v, op, 0, 0);
break;
}
case TK_LSHIFT:
case TK_RSHIFT: {
sqliteExprCode(pParse, pExpr->pRight);
sqliteExprCode(pParse, pExpr->pLeft);
sqliteVdbeAddOp(v, op, 0, 0);
break;
}
case TK_CONCAT: {
sqliteExprCode(pParse, pExpr->pLeft);
sqliteExprCode(pParse, pExpr->pRight);
sqliteVdbeAddOp(v, OP_Concat, 2, 0);
break;
}
case TK_LT:
case TK_LE:
case TK_GT:
case TK_GE:
case TK_NE:
case TK_EQ: {
int dest;
sqliteVdbeAddOp(v, OP_Integer, 1, 0);
sqliteExprCode(pParse, pExpr->pLeft);
sqliteExprCode(pParse, pExpr->pRight);
dest = sqliteVdbeCurrentAddr(v) + 2;
sqliteVdbeAddOp(v, op, 0, dest);
sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
break;
}
case TK_UMINUS: {
assert( pExpr->pLeft );
if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
Token *p = &pExpr->pLeft->token;
char *z = sqliteMalloc( p->n + 2 );
sprintf(z, "-%.*s", p->n, p->z);
if( pExpr->pLeft->op==TK_INTEGER ){
sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
}else{
sqliteVdbeAddOp(v, OP_String, 0, 0);
}
sqliteVdbeChangeP3(v, -1, z, p->n+1);
sqliteFree(z);
break;
}
/* Fall through into TK_NOT */
}
case TK_BITNOT:
case TK_NOT: {
sqliteExprCode(pParse, pExpr->pLeft);
sqliteVdbeAddOp(v, op, 0, 0);
break;
}
case TK_ISNULL:
case TK_NOTNULL: {
int dest;
sqliteVdbeAddOp(v, OP_Integer, 1, 0);
sqliteExprCode(pParse, pExpr->pLeft);
dest = sqliteVdbeCurrentAddr(v) + 2;
sqliteVdbeAddOp(v, op, 0, dest);
sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
break;
}
case TK_AGG_FUNCTION: {
sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
break;
}
case TK_FUNCTION: {
int i;
ExprList *pList = pExpr->pList;
int nExpr = pList ? pList->nExpr : 0;
FuncDef *pDef;
pDef = sqliteFindFunction(pParse->db,
pExpr->token.z, pExpr->token.n, nExpr, 0);
assert( pDef!=0 );
for(i=0; i<nExpr; i++){
sqliteExprCode(pParse, pList->a[i].pExpr);
}
sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
break;
}
case TK_SELECT: {
sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
break;
}
case TK_IN: {
int addr;
sqliteVdbeAddOp(v, OP_Integer, 1, 0);
sqliteExprCode(pParse, pExpr->pLeft);
addr = sqliteVdbeCurrentAddr(v);
if( pExpr->pSelect ){
sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2);
}else{
sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2);
}
sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
break;
}
case TK_BETWEEN: {
int lbl = sqliteVdbeMakeLabel(v);
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
sqliteExprIfFalse(pParse, pExpr, lbl);
sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
sqliteVdbeResolveLabel(v, lbl);
break;
}
case TK_AS: {
sqliteExprCode(pParse, pExpr->pLeft);
break;
}
case TK_CASE: {
int expr_end_label;
int next_when_label;
int i;
assert(pExpr->pList);
assert((pExpr->pList->nExpr % 2) == 0);
assert(pExpr->pList->nExpr > 0);
expr_end_label = sqliteVdbeMakeLabel(pParse->pVdbe);
if( pExpr->pLeft ){
sqliteExprCode(pParse, pExpr->pLeft);
}
for(i=0; i<pExpr->pList->nExpr; i=i+2){
if( i!=0 ){
sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
}
next_when_label = sqliteVdbeMakeLabel(pParse->pVdbe);
if( pExpr->pLeft ){
sqliteVdbeAddOp(pParse->pVdbe, OP_Dup, 0, 1);
sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
sqliteVdbeAddOp(pParse->pVdbe, OP_Ne, 0, next_when_label);
}else{
sqliteExprIfFalse(pParse, pExpr->pList->a[i].pExpr, next_when_label);
}
if( pExpr->pLeft ){
sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
}
sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
sqliteVdbeAddOp(pParse->pVdbe, OP_Goto, 0, expr_end_label);
}
sqliteVdbeResolveLabel(pParse->pVdbe, next_when_label);
if( pExpr->pLeft ){
sqliteVdbeAddOp(pParse->pVdbe, OP_Pop, 1, 0);
}
if( pExpr->pRight ){
sqliteExprCode(pParse, pExpr->pRight);
}else{
sqliteVdbeAddOp(pParse->pVdbe, OP_String, 0, 0);
}
sqliteVdbeResolveLabel(pParse->pVdbe, expr_end_label);
}
break;
}
}
/*
** Generate code for a boolean expression such that a jump is made
** to the label "dest" if the expression is true but execution
** continues straight thru if the expression is false.
*/
void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
Vdbe *v = pParse->pVdbe;
int op = 0;
if( v==0 || pExpr==0 ) return;
switch( pExpr->op ){
case TK_LT: op = OP_Lt; break;
case TK_LE: op = OP_Le; break;
case TK_GT: op = OP_Gt; break;
case TK_GE: op = OP_Ge; break;
case TK_NE: op = OP_Ne; break;
case TK_EQ: op = OP_Eq; break;
case TK_ISNULL: op = OP_IsNull; break;
case TK_NOTNULL: op = OP_NotNull; break;
default: break;
}
switch( pExpr->op ){
case TK_AND: {
int d2 = sqliteVdbeMakeLabel(v);
sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
sqliteExprIfTrue(pParse, pExpr->pRight, dest);
sqliteVdbeResolveLabel(v, d2);
break;
}
case TK_OR: {
sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
sqliteExprIfTrue(pParse, pExpr->pRight, dest);
break;
}
case TK_NOT: {
sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
break;
}
case TK_LT:
case TK_LE:
case TK_GT:
case TK_GE:
case TK_NE:
case TK_EQ: {
sqliteExprCode(pParse, pExpr->pLeft);
sqliteExprCode(pParse, pExpr->pRight);
sqliteVdbeAddOp(v, op, 0, dest);
break;
}
case TK_ISNULL:
case TK_NOTNULL: {
sqliteExprCode(pParse, pExpr->pLeft);
sqliteVdbeAddOp(v, op, 0, dest);
break;
}
case TK_IN: {
sqliteExprCode(pParse, pExpr->pLeft);
if( pExpr->pSelect ){
sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
}else{
sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
}
break;
}
case TK_BETWEEN: {
int lbl = sqliteVdbeMakeLabel(v);
sqliteExprCode(pParse, pExpr->pLeft);
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
sqliteVdbeAddOp(v, OP_Lt, 0, lbl);
sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
sqliteVdbeAddOp(v, OP_Le, 0, dest);
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
sqliteVdbeResolveLabel(v, lbl);
sqliteVdbeAddOp(v, OP_Pop, 1, 0);
break;
}
default: {
sqliteExprCode(pParse, pExpr);
sqliteVdbeAddOp(v, OP_If, 0, dest);
break;
}
}
}
/*
** Generate code for a boolean expression such that a jump is made
** to the label "dest" if the expression is false but execution
** continues straight thru if the expression is true.
*/
void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
Vdbe *v = pParse->pVdbe;
int op = 0;
if( v==0 || pExpr==0 ) return;
switch( pExpr->op ){
case TK_LT: op = OP_Ge; break;
case TK_LE: op = OP_Gt; break;
case TK_GT: op = OP_Le; break;
case TK_GE: op = OP_Lt; break;
case TK_NE: op = OP_Eq; break;
case TK_EQ: op = OP_Ne; break;
case TK_ISNULL: op = OP_NotNull; break;
case TK_NOTNULL: op = OP_IsNull; break;
default: break;
}
switch( pExpr->op ){
case TK_AND: {
sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
sqliteExprIfFalse(pParse, pExpr->pRight, dest);
break;
}
case TK_OR: {
int d2 = sqliteVdbeMakeLabel(v);
sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
sqliteExprIfFalse(pParse, pExpr->pRight, dest);
sqliteVdbeResolveLabel(v, d2);
break;
}
case TK_NOT: {
sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
break;
}
case TK_LT:
case TK_LE:
case TK_GT:
case TK_GE:
case TK_NE:
case TK_EQ: {
sqliteExprCode(pParse, pExpr->pLeft);
sqliteExprCode(pParse, pExpr->pRight);
sqliteVdbeAddOp(v, op, 0, dest);
break;
}
case TK_ISNULL:
case TK_NOTNULL: {
sqliteExprCode(pParse, pExpr->pLeft);
sqliteVdbeAddOp(v, op, 0, dest);
break;
}
case TK_IN: {
sqliteExprCode(pParse, pExpr->pLeft);
if( pExpr->pSelect ){
sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
}else{
sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
}
break;
}
case TK_BETWEEN: {
int addr;
sqliteExprCode(pParse, pExpr->pLeft);
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
addr = sqliteVdbeCurrentAddr(v);
sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
sqliteVdbeAddOp(v, OP_Pop, 1, 0);
sqliteVdbeAddOp(v, OP_Goto, 0, dest);
sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
sqliteVdbeAddOp(v, OP_Gt, 0, dest);
break;
}
default: {
sqliteExprCode(pParse, pExpr);
sqliteVdbeAddOp(v, OP_Not, 0, 0);
sqliteVdbeAddOp(v, OP_If, 0, dest);
break;
}
}
}
/*
** Do a deep comparison of two expression trees. Return TRUE (non-zero)
** if they are identical and return FALSE if they differ in any way.
*/
int sqliteExprCompare(Expr *pA, Expr *pB){
int i;
if( pA==0 ){
return pB==0;
}else if( pB==0 ){
return 0;
}
if( pA->op!=pB->op ) return 0;
if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
if( pA->pList ){
if( pB->pList==0 ) return 0;
if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
for(i=0; i<pA->pList->nExpr; i++){
if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
return 0;
}
}
}else if( pB->pList ){
return 0;
}
if( pA->pSelect || pB->pSelect ) return 0;
if( pA->token.z ){
if( pB->token.z==0 ) return 0;
if( pB->token.n!=pA->token.n ) return 0;
if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0;
}
return 1;
}
/*
** Add a new element to the pParse->aAgg[] array and return its index.
*/
static int appendAggInfo(Parse *pParse){
if( (pParse->nAgg & 0x7)==0 ){
int amt = pParse->nAgg + 8;
AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
if( aAgg==0 ){
return -1;
}
pParse->aAgg = aAgg;
}
memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
return pParse->nAgg++;
}
/*
** Analyze the given expression looking for aggregate functions and
** for variables that need to be added to the pParse->aAgg[] array.
** Make additional entries to the pParse->aAgg[] array as necessary.
**
** This routine should only be called after the expression has been
** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
**
** If errors are seen, leave an error message in zErrMsg and return
** the number of errors.
*/
int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
int i;
AggExpr *aAgg;
int nErr = 0;
if( pExpr==0 ) return 0;
switch( pExpr->op ){
case TK_COLUMN: {
aAgg = pParse->aAgg;
for(i=0; i<pParse->nAgg; i++){
if( aAgg[i].isAgg ) continue;
if( aAgg[i].pExpr->iTable==pExpr->iTable
&& aAgg[i].pExpr->iColumn==pExpr->iColumn ){
break;
}
}
if( i>=pParse->nAgg ){
i = appendAggInfo(pParse);
if( i<0 ) return 1;
pParse->aAgg[i].isAgg = 0;
pParse->aAgg[i].pExpr = pExpr;
}
pExpr->iAgg = i;
break;
}
case TK_AGG_FUNCTION: {
aAgg = pParse->aAgg;
for(i=0; i<pParse->nAgg; i++){
if( !aAgg[i].isAgg ) continue;
if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
break;
}
}
if( i>=pParse->nAgg ){
i = appendAggInfo(pParse);
if( i<0 ) return 1;
pParse->aAgg[i].isAgg = 1;
pParse->aAgg[i].pExpr = pExpr;
pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
pExpr->token.z, pExpr->token.n,
pExpr->pList ? pExpr->pList->nExpr : 0, 0);
}
pExpr->iAgg = i;
break;
}
default: {
if( pExpr->pLeft ){
nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
}
if( nErr==0 && pExpr->pRight ){
nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
}
if( nErr==0 && pExpr->pList ){
int n = pExpr->pList->nExpr;
int i;
for(i=0; nErr==0 && i<n; i++){
nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
}
}
break;
}
}
return nErr;
}
/*
** Locate a user function given a name and a number of arguments.
** Return a pointer to the FuncDef structure that defines that
** function, or return NULL if the function does not exist.
**
** If the createFlag argument is true, then a new (blank) FuncDef
** structure is created and liked into the "db" structure if a
** no matching function previously existed. When createFlag is true
** and the nArg parameter is -1, then only a function that accepts
** any number of arguments will be returned.
**
** If createFlag is false and nArg is -1, then the first valid
** function found is returned. A function is valid if either xFunc
** or xStep is non-zero.
*/
FuncDef *sqliteFindFunction(
sqlite *db, /* An open database */
const char *zName, /* Name of the function. Not null-terminated */
int nName, /* Number of characters in the name */
int nArg, /* Number of arguments. -1 means any number */
int createFlag /* Create new entry if true and does not otherwise exist */
){
FuncDef *pFirst, *p, *pMaybe;
pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
if( p && !createFlag && nArg<0 ){
while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
return p;
}
pMaybe = 0;
while( p && p->nArg!=nArg ){
if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
p = p->pNext;
}
if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
return 0;
}
if( p==0 && pMaybe ){
assert( createFlag==0 );
return pMaybe;
}
if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
p->nArg = nArg;
p->pNext = pFirst;
sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
}
return p;
}
|