summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/expression.cpp
blob: 12f700a290e1f4e5284f83942b88de140954957f (plain)
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
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
/**
 * Copyright (c) 2011 10gen Inc.
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * As a special exception, the copyright holders give permission to link the
 * code of portions of this program with the OpenSSL library under certain
 * conditions as described in each individual source file and distribute
 * linked combinations including the program with the OpenSSL library. You
 * must comply with the GNU Affero General Public License in all respects for
 * all of the code used other than as permitted herein. If you modify file(s)
 * with this exception, you may extend this exception to your version of the
 * file(s), but you are not obligated to do so. If you do not wish to do so,
 * delete this exception statement from your version. If you delete this
 * exception statement from all source files in the program, then also delete
 * it in the license file.
 */

#include "pch.h"

#include "db/pipeline/expression.h"

#include <boost/algorithm/string.hpp>
#include <boost/preprocessor/cat.hpp> // like the ## operator but works with __LINE__
#include <cstdio>

#include "mongo/base/init.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/pipeline/document.h"
#include "mongo/db/pipeline/expression_context.h"
#include "mongo/db/pipeline/value.h"
#include "mongo/stdx/functional.h"
#include "mongo/util/string_map.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {
    using namespace mongoutils;

    /// Helper function to easily wrap constants with $const.
    static Value serializeConstant(Value val) {
        return Value(DOC("$const" << val));
    }

    void Variables::uassertValidNameForUserWrite(StringData varName) {
        // System variables users allowed to write to (currently just one)
        if (varName == "CURRENT") {
            return;
        }

        uassert(16866, "empty variable names are not allowed",
                !varName.empty());

        const bool firstCharIsValid = (varName[0] >= 'a' &&  varName[0] <= 'z')
                                   || (varName[0] & '\x80') // non-ascii
                                   ;

        uassert(16867, str::stream() <<
                   "'" << varName << "' starts with an invalid character for a user variable name",
                firstCharIsValid);

        for (size_t i = 1; i < varName.size(); i++) {
            const bool charIsValid = (varName[i] >= 'a' &&  varName[i] <= 'z')
                                  || (varName[i] >= 'A' &&  varName[i] <= 'Z')
                                  || (varName[i] >= '0' &&  varName[i] <= '9')
                                  || (varName[i] == '_')
                                  || (varName[i] & '\x80') // non-ascii
                                  ;

            uassert(16868, str::stream() << "'" << varName << "' contains an invalid character "
                                         << "for a variable name: '" << varName[i] << "'",
                    charIsValid);
        }
    }

    void Variables::uassertValidNameForUserRead(StringData varName) {
        uassert(16869, "empty variable names are not allowed",
                !varName.empty());

        const bool firstCharIsValid = (varName[0] >= 'a' &&  varName[0] <= 'z')
                                   || (varName[0] >= 'A' &&  varName[0] <= 'Z')
                                   || (varName[0] & '\x80') // non-ascii
                                   ;

        uassert(16870, str::stream() <<
                   "'" << varName << "' starts with an invalid character for a variable name",
                firstCharIsValid);

        for (size_t i = 1; i < varName.size(); i++) {
            const bool charIsValid = (varName[i] >= 'a' &&  varName[i] <= 'z')
                                  || (varName[i] >= 'A' &&  varName[i] <= 'Z')
                                  || (varName[i] >= '0' &&  varName[i] <= '9')
                                  || (varName[i] == '_')
                                  || (varName[i] & '\x80') // non-ascii
                                  ;

            uassert(16871, str::stream() << "'" << varName << "' contains an invalid character "
                                         << "for a variable name: '" << varName[i] << "'",
                    charIsValid);
        }
    }

    void Variables::setValue(Id id, const Value& value) {
        massert(17199, "can't use Variables::setValue to set ROOT",
                id != ROOT_ID);

        verify(id < _numVars);
        _rest[id] = value;
    }

    Value Variables::getValue(Id id) const {
        if (id == ROOT_ID)
            return Value(_root);

        verify(id < _numVars);
        return  _rest[id];
    }

    Document Variables::getDocument(Id id) const {
        if (id == ROOT_ID)
            return _root;

        verify(id < _numVars);
        const Value var = _rest[id];
        if (var.getType() == Object)
            return var.getDocument();

        return Document();
    }

    Variables::Id VariablesParseState::defineVariable(const StringData& name) {
        // caller should have validated before hand by using Variables::uassertValidNameForUserWrite
        massert(17275, "Can't redefine ROOT",
                name != "ROOT");

        Variables::Id id = _idGenerator->generateId();
        _variables[name] = id;
        return id;
    }

    Variables::Id VariablesParseState::getVariable(const StringData& name) const {
        StringMap<Variables::Id>::const_iterator it = _variables.find(name);
        if (it != _variables.end())
            return it->second;

        uassert(17276, str::stream() << "Use of undefined variable: " << name,
                name == "ROOT" || name == "CURRENT");

        return Variables::ROOT_ID;
    }

    /* --------------------------- Expression ------------------------------ */

    Expression::ObjectCtx::ObjectCtx(int theOptions)
        : options(theOptions)
    {}

    bool Expression::ObjectCtx::documentOk() const {
        return ((options & DOCUMENT_OK) != 0);
    }

    bool Expression::ObjectCtx::topLevel() const {
        return ((options & TOP_LEVEL) != 0);
    }

    bool Expression::ObjectCtx::inclusionOk() const {
        return ((options & INCLUSION_OK) != 0);
    }

    string Expression::removeFieldPrefix(const string &prefixedField) {
        uassert(16419, str::stream()<<"field path must not contain embedded null characters" << prefixedField.find("\0") << "," ,
                prefixedField.find('\0') == string::npos);

        const char *pPrefixedField = prefixedField.c_str();
        uassert(15982, str::stream() <<
                "field path references must be prefixed with a '$' ('" <<
                prefixedField << "'", pPrefixedField[0] == '$');

        return string(pPrefixedField + 1);
    }

    intrusive_ptr<Expression> Expression::parseObject(
            BSONObj obj,
            ObjectCtx* pCtx,
            const VariablesParseState& vps) {
        /*
          An object expression can take any of the following forms:

          f0: {f1: ..., f2: ..., f3: ...}
          f0: {$operator:[operand1, operand2, ...]}
        */

        intrusive_ptr<Expression> pExpression; // the result
        intrusive_ptr<ExpressionObject> pExpressionObject; // alt result
        enum { UNKNOWN, NOTOPERATOR, OPERATOR } kind = UNKNOWN;

        if (obj.isEmpty())
            return ExpressionObject::create();
        BSONObjIterator iter(obj);

        for(size_t fieldCount = 0; iter.more(); ++fieldCount) {
            BSONElement fieldElement(iter.next());
            const char *pFieldName = fieldElement.fieldName();

            if (pFieldName[0] == '$') {
                uassert(15983, str::stream() <<
                        "the operator must be the only field in a pipeline object (at '"
                        << pFieldName << "'",
                        fieldCount == 0);

                uassert(16404, "$expressions are not allowed at the top-level of $project",
                        !pCtx->topLevel());

                /* we've determined this "object" is an operator expression */
                kind = OPERATOR;

                pExpression = parseExpression(fieldElement, vps);
            }
            else {
                uassert(15990, str::stream() << "this object is already an operator expression, and can't be used as a document expression (at '" <<
                        pFieldName << "')",
                        kind != OPERATOR);

                uassert(16405, "dotted field names are only allowed at the top level",
                        pCtx->topLevel() || !str::contains(pFieldName, '.'));

                /* if it's our first time, create the document expression */
                if (!pExpression.get()) {
                    verify(pCtx->documentOk());
                    // CW TODO error: document not allowed in this context

                    pExpressionObject = pCtx->topLevel() ? ExpressionObject::createRoot()
                                                         : ExpressionObject::create();
                    pExpression = pExpressionObject;

                    /* this "object" is not an operator expression */
                    kind = NOTOPERATOR;
                }

                BSONType fieldType = fieldElement.type();
                string fieldName(pFieldName);
                switch (fieldType){
                    case Object: {
                        /* it's a nested document */
                        ObjectCtx oCtx(
                            (pCtx->documentOk() ? ObjectCtx::DOCUMENT_OK : 0)
                             | (pCtx->inclusionOk() ? ObjectCtx::INCLUSION_OK : 0));
                            
                        pExpressionObject->addField(fieldName,
                                                    parseObject(fieldElement.Obj(), &oCtx, vps));
                        break;
                    }
                    case String: {
                        /* it's a renamed field */
                        // CW TODO could also be a constant
                        pExpressionObject->addField(fieldName,
                                                    ExpressionFieldPath::parse(fieldElement.str(),
                                                                               vps));
                        break;
                    }
                    case Bool:
                    case NumberDouble:
                    case NumberLong:
                    case NumberInt: {
                        /* it's an inclusion specification */
                        if (fieldElement.trueValue()) {
                            uassert(16420, "field inclusion is not allowed inside of $expressions",
                                    pCtx->inclusionOk());
                            pExpressionObject->includePath(fieldName);
                        }
                        else {
                            uassert(16406,
                                    "The top-level _id field is the only field currently supported for exclusion",
                                    pCtx->topLevel() && fieldName == "_id");
                            pExpressionObject->excludeId(true);
                        }
                        break;
                    }
                    default:
                        uassert(15992, str::stream() <<
                                "disallowed field type " << typeName(fieldType) <<
                                " in object expression (at '" <<
                                fieldName << "')", false);
                }
            }
        }

        return pExpression;
    }

namespace {
    typedef stdx::function<intrusive_ptr<Expression>(BSONElement, const VariablesParseState&)>
            ExpressionParser;
    StringMap<ExpressionParser> expressionParserMap;
}

/** Registers an ExpressionParser so it can be called from parseExpression and friends.
 *
 *  As an example, if your expression looks like {"$foo": [1,2,3]} you would add this line:
 *  REGISTER_EXPRESSION("$foo", ExpressionFoo::parse);
 */
#define REGISTER_EXPRESSION(key, parserFunc) \
    MONGO_INITIALIZER(BOOST_PP_CAT(addToExpressionParserMap, __LINE__))(InitializerContext*) { \
        /* prevent duplicate expressions */ \
        StringMap<ExpressionParser>::const_iterator op = expressionParserMap.find(key); \
        massert(17064, str::stream() << "Duplicate expression (" << key << ") detected at " \
                                     << __FILE__ << ":" << __LINE__, \
                op == expressionParserMap.end()); \
        /* register expression */ \
        expressionParserMap[key] = (parserFunc); \
        return Status::OK(); \
    }

    intrusive_ptr<Expression> Expression::parseExpression(
            BSONElement exprElement,
            const VariablesParseState& vps) {

        /* look for the specified operator */
        const char* opName = exprElement.fieldName();
        StringMap<ExpressionParser>::const_iterator op = expressionParserMap.find(opName);
        uassert(15999, str::stream() << "invalid operator '" << opName << "'",
                op != expressionParserMap.end());

        /* make the expression node */
        return op->second(exprElement, vps);
    }

    Expression::ExpressionVector ExpressionNary::parseArguments(
            BSONElement exprElement,
            const VariablesParseState& vps) {

        ExpressionVector out;
        if (exprElement.type() == Array) {
            BSONForEach(elem, exprElement.Obj()) {
                out.push_back(Expression::parseOperand(elem, vps));
            }
        }
        else { // assume it's an atomic operand
            out.push_back(Expression::parseOperand(exprElement, vps));
        }

        return out;
    }

    intrusive_ptr<Expression> Expression::parseOperand(
            BSONElement exprElement,
            const VariablesParseState& vps) {

        BSONType type = exprElement.type();

        if (type == String && exprElement.valuestr()[0] == '$') {
            /* if we got here, this is a field path expression */
            return ExpressionFieldPath::parse(exprElement.str(), vps);
        }
        else if (type == Object) {
            ObjectCtx oCtx(ObjectCtx::DOCUMENT_OK);
            return Expression::parseObject(exprElement.Obj(), &oCtx, vps);
        }
        else {
            return ExpressionConstant::parse(exprElement, vps);
        }
    }

    /* ------------------------- ExpressionAdd ----------------------------- */

    Value ExpressionAdd::evaluateInternal(Variables* vars) const {

        /*
          We'll try to return the narrowest possible result value.  To do that
          without creating intermediate Values, do the arithmetic for double
          and integral types in parallel, tracking the current narrowest
          type.
         */
        double doubleTotal = 0;
        long long longTotal = 0;
        BSONType totalType = NumberInt;
        bool haveDate = false;

        const size_t n = vpOperand.size();
        for (size_t i = 0; i < n; ++i) {
            Value val = vpOperand[i]->evaluateInternal(vars);

            if (val.numeric()) {
                totalType = Value::getWidestNumeric(totalType, val.getType());

                doubleTotal += val.coerceToDouble();
                longTotal += val.coerceToLong();
            }
            else if (val.getType() == Date) {
                uassert(16612, "only one Date allowed in an $add expression",
                        !haveDate);
                haveDate = true;

                // We don't manipulate totalType here.

                longTotal += val.getDate();
                doubleTotal += val.getDate();
            }
            else if (val.nullish()) {
                return Value(BSONNULL);
            }
            else {
                uasserted(16554, str::stream() << "$add only supports numeric or date types, not "
                                               << typeName(val.getType()));
            }
        }

        if (haveDate) {
            if (totalType == NumberDouble)
                longTotal = static_cast<long long>(doubleTotal);
            return Value(Date_t(longTotal));
        }
        else if (totalType == NumberLong) {
            return Value(longTotal);
        }
        else if (totalType == NumberDouble) {
            return Value(doubleTotal);
        }
        else if (totalType == NumberInt) {
            return Value::createIntOrLong(longTotal);
        }
        else {
            massert(16417, "$add resulted in a non-numeric type", false);
        }
    }

    REGISTER_EXPRESSION("$add", ExpressionAdd::parse);
    const char *ExpressionAdd::getOpName() const {
        return "$add";
    }

    /* ------------------------- ExpressionAllElementsTrue -------------------------- */

    Value ExpressionAllElementsTrue::evaluateInternal(Variables* vars) const {
        const Value arr = vpOperand[0]->evaluateInternal(vars);
        uassert(17040, str::stream() << getOpName() << "'s argument must be an array, but is "
                                     << typeName(arr.getType()),
                arr.getType() == Array);
        const vector<Value>& array = arr.getArray();
        for (vector<Value>::const_iterator it = array.begin(); it != array.end(); ++it) {
            if (!it->coerceToBool()) {
                return Value(false);
            }
        }
        return Value(true);
    }

    REGISTER_EXPRESSION("$allElementsTrue", ExpressionAllElementsTrue::parse);
    const char *ExpressionAllElementsTrue::getOpName() const {
        return "$allElementsTrue";
    }

    /* ------------------------- ExpressionAnd ----------------------------- */

    intrusive_ptr<Expression> ExpressionAnd::optimize() {
        /* optimize the conjunction as much as possible */
        intrusive_ptr<Expression> pE(ExpressionNary::optimize());

        /* if the result isn't a conjunction, we can't do anything */
        ExpressionAnd *pAnd = dynamic_cast<ExpressionAnd *>(pE.get());
        if (!pAnd)
            return pE;

        /*
          Check the last argument on the result; if it's not constant (as
          promised by ExpressionNary::optimize(),) then there's nothing
          we can do.
        */
        const size_t n = pAnd->vpOperand.size();
        // ExpressionNary::optimize() generates an ExpressionConstant for {$and:[]}.
        verify(n > 0);
        intrusive_ptr<Expression> pLast(pAnd->vpOperand[n - 1]);
        const ExpressionConstant *pConst =
            dynamic_cast<ExpressionConstant *>(pLast.get());
        if (!pConst)
            return pE;

        /*
          Evaluate and coerce the last argument to a boolean.  If it's false,
          then we can replace this entire expression.
         */
        bool last = pConst->getValue().coerceToBool();
        if (!last) {
            intrusive_ptr<ExpressionConstant> pFinal(
                ExpressionConstant::create(Value(false)));
            return pFinal;
        }

        /*
          If we got here, the final operand was true, so we don't need it
          anymore.  If there was only one other operand, we don't need the
          conjunction either.  Note we still need to keep the promise that
          the result will be a boolean.
         */
        if (n == 2) {
            intrusive_ptr<Expression> pFinal(
                ExpressionCoerceToBool::create(pAnd->vpOperand[0]));
            return pFinal;
        }

        /*
          Remove the final "true" value, and return the new expression.

          CW TODO:
          Note that because of any implicit conversions, we may need to
          apply an implicit boolean conversion.
        */
        pAnd->vpOperand.resize(n - 1);
        return pE;
    }

    Value ExpressionAnd::evaluateInternal(Variables* vars) const {
        const size_t n = vpOperand.size();
        for(size_t i = 0; i < n; ++i) {
            Value pValue(vpOperand[i]->evaluateInternal(vars));
            if (!pValue.coerceToBool())
                return Value(false);
        }

        return Value(true);
    }

    REGISTER_EXPRESSION("$and", ExpressionAnd::parse);
    const char *ExpressionAnd::getOpName() const {
        return "$and";
    }

    /* ------------------------- ExpressionAnyElementTrue -------------------------- */

    Value ExpressionAnyElementTrue::evaluateInternal(Variables* vars) const {
        const Value arr = vpOperand[0]->evaluateInternal(vars);
        uassert(17041, str::stream() << getOpName() << "'s argument must be an array, but is "
                                     << typeName(arr.getType()),
                arr.getType() == Array);
        const vector<Value>& array = arr.getArray();
        for (vector<Value>::const_iterator it = array.begin(); it != array.end(); ++it) {
            if (it->coerceToBool()) {
                return Value(true);
            }
        }
        return Value(false);
    }

    REGISTER_EXPRESSION("$anyElementTrue", ExpressionAnyElementTrue::parse);
    const char *ExpressionAnyElementTrue::getOpName() const {
        return "$anyElementTrue";
    }

    /* -------------------- ExpressionCoerceToBool ------------------------- */

    intrusive_ptr<ExpressionCoerceToBool> ExpressionCoerceToBool::create(
        const intrusive_ptr<Expression> &pExpression) {
        intrusive_ptr<ExpressionCoerceToBool> pNew(
            new ExpressionCoerceToBool(pExpression));
        return pNew;
    }

    ExpressionCoerceToBool::ExpressionCoerceToBool(
        const intrusive_ptr<Expression> &pTheExpression):
        Expression(),
        pExpression(pTheExpression) {
    }

    intrusive_ptr<Expression> ExpressionCoerceToBool::optimize() {
        /* optimize the operand */
        pExpression = pExpression->optimize();

        /* if the operand already produces a boolean, then we don't need this */
        /* LATER - Expression to support a "typeof" query? */
        Expression *pE = pExpression.get();
        if (dynamic_cast<ExpressionAnd *>(pE) ||
            dynamic_cast<ExpressionOr *>(pE) ||
            dynamic_cast<ExpressionNot *>(pE) ||
            dynamic_cast<ExpressionCoerceToBool *>(pE))
            return pExpression;

        return intrusive_ptr<Expression>(this);
    }

    void ExpressionCoerceToBool::addDependencies(DepsTracker* deps, vector<string>* path) const {
        pExpression->addDependencies(deps);
    }

    Value ExpressionCoerceToBool::evaluateInternal(Variables* vars) const {
        Value pResult(pExpression->evaluateInternal(vars));
        bool b = pResult.coerceToBool();
        if (b)
            return Value(true);
        return Value(false);
    }

    Value ExpressionCoerceToBool::serialize(bool explain) const {
        // When not explaining, serialize to an $and expression. When parsed, the $and expression
        // will be optimized back into a ExpressionCoerceToBool.
        const char* name = explain ? "$coerceToBool" : "$and";
        return Value(DOC(name << DOC_ARRAY(pExpression->serialize(explain))));
    }

    /* ----------------------- ExpressionCompare --------------------------- */

    REGISTER_EXPRESSION("$cmp",
                        stdx::bind(ExpressionCompare::parse, stdx::placeholders::_1, stdx::placeholders::_2, ExpressionCompare::CMP));
    REGISTER_EXPRESSION("$eq",
                        stdx::bind(ExpressionCompare::parse, stdx::placeholders::_1, stdx::placeholders::_2, ExpressionCompare::EQ));
    REGISTER_EXPRESSION("$gt",
                        stdx::bind(ExpressionCompare::parse, stdx::placeholders::_1, stdx::placeholders::_2, ExpressionCompare::GT));
    REGISTER_EXPRESSION("$gte",
                        stdx::bind(ExpressionCompare::parse, stdx::placeholders::_1, stdx::placeholders::_2, ExpressionCompare::GTE));
    REGISTER_EXPRESSION("$lt",
                        stdx::bind(ExpressionCompare::parse, stdx::placeholders::_1, stdx::placeholders::_2, ExpressionCompare::LT));
    REGISTER_EXPRESSION("$lte",
                        stdx::bind(ExpressionCompare::parse, stdx::placeholders::_1, stdx::placeholders::_2, ExpressionCompare::LTE));
    REGISTER_EXPRESSION("$ne",
                        stdx::bind(ExpressionCompare::parse, stdx::placeholders::_1, stdx::placeholders::_2, ExpressionCompare::NE));
    intrusive_ptr<Expression> ExpressionCompare::parse(
            BSONElement bsonExpr,
            const VariablesParseState& vps,
            CmpOp op) {

        intrusive_ptr<ExpressionCompare> expr = new ExpressionCompare(op);
        ExpressionVector args = parseArguments(bsonExpr, vps);
        expr->validateArguments(args);
        expr->vpOperand = args;
        return expr;
    }

    ExpressionCompare::ExpressionCompare(CmpOp theCmpOp)
        : cmpOp(theCmpOp)
    {}

namespace {
    // Lookup table for truth value returns
    struct CmpLookup {
        const bool truthValue[3]; // truth value for -1, 0, 1
        const ExpressionCompare::CmpOp reverse; // reverse(b,a) returns the same as op(a,b)
        const char name[5]; // string name with trailing '\0'
    };
    static const CmpLookup cmpLookup[7] = {
        /*             -1      0      1      reverse                  name   */
        /* EQ  */ { { false, true,  false }, ExpressionCompare::EQ,  "$eq"  },
        /* NE  */ { { true,  false, true },  ExpressionCompare::NE,  "$ne"  },
        /* GT  */ { { false, false, true },  ExpressionCompare::LT,  "$gt"  },
        /* GTE */ { { false, true,  true },  ExpressionCompare::LTE, "$gte" },
        /* LT  */ { { true,  false, false }, ExpressionCompare::GT,  "$lt"  },
        /* LTE */ { { true,  true,  false }, ExpressionCompare::GTE, "$lte" },

        // CMP is special. Only name is used.
        /* CMP */ { { false, false, false }, ExpressionCompare::CMP, "$cmp" },
    };
}

    Value ExpressionCompare::evaluateInternal(Variables* vars) const {
        Value pLeft(vpOperand[0]->evaluateInternal(vars));
        Value pRight(vpOperand[1]->evaluateInternal(vars));

        int cmp = Value::compare(pLeft, pRight);

        // Make cmp one of 1, 0, or -1.
        if (cmp == 0) {
            // leave as 0
        } else if (cmp < 0) {
            cmp = -1;
        } else if (cmp > 0) {
            cmp = 1;
        }

        if (cmpOp == CMP)
            return Value(cmp);

        bool returnValue = cmpLookup[cmpOp].truthValue[cmp + 1];
        return Value(returnValue);
    }

    const char *ExpressionCompare::getOpName() const {
        return cmpLookup[cmpOp].name;
    }

    /* ------------------------- ExpressionConcat ----------------------------- */

    Value ExpressionConcat::evaluateInternal(Variables* vars) const {
        const size_t n = vpOperand.size();

        StringBuilder result;
        for (size_t i = 0; i < n; ++i) {
            Value val = vpOperand[i]->evaluateInternal(vars);
            if (val.nullish())
                return Value(BSONNULL);

            uassert(16702, str::stream() << "$concat only supports strings, not "
                                         << typeName(val.getType()),
                    val.getType() == String);

            result << val.coerceToString();
        }

        return Value(result.str());
    }

    REGISTER_EXPRESSION("$concat", ExpressionConcat::parse);
    const char *ExpressionConcat::getOpName() const {
        return "$concat";
    }

    /* ----------------------- ExpressionCond ------------------------------ */

    Value ExpressionCond::evaluateInternal(Variables* vars) const {
        Value pCond(vpOperand[0]->evaluateInternal(vars));
        int idx = pCond.coerceToBool() ? 1 : 2;
        return vpOperand[idx]->evaluateInternal(vars);
    }

    intrusive_ptr<Expression> ExpressionCond::parse(
            BSONElement expr,
            const VariablesParseState& vps) {

        if (expr.type() != Object) {
            return Base::parse(expr, vps);
        }
        verify(str::equals(expr.fieldName(), "$cond"));

        intrusive_ptr<ExpressionCond> ret = new ExpressionCond();
        ret->vpOperand.resize(3);

        const BSONObj args = expr.embeddedObject();
        BSONForEach(arg, args) {
            if (str::equals(arg.fieldName(), "if")) {
                ret->vpOperand[0] = parseOperand(arg, vps);
            } else if (str::equals(arg.fieldName(), "then")) {
                ret->vpOperand[1] = parseOperand(arg, vps);
            } else if (str::equals(arg.fieldName(), "else")) {
                ret->vpOperand[2] = parseOperand(arg, vps);
            } else {
                uasserted(17083, str::stream()
                        << "Unrecognized parameter to $cond: " << arg.fieldName());
            }
        }

        uassert(17080, "Missing 'if' parameter to $cond",
                ret->vpOperand[0]);
        uassert(17081, "Missing 'then' parameter to $cond",
                ret->vpOperand[1]);
        uassert(17082, "Missing 'else' parameter to $cond",
                ret->vpOperand[2]);

        return ret;
    }

    REGISTER_EXPRESSION("$cond", ExpressionCond::parse);
    const char *ExpressionCond::getOpName() const {
        return "$cond";
    }

    /* ---------------------- ExpressionConstant --------------------------- */

    intrusive_ptr<Expression> ExpressionConstant::parse(
            BSONElement exprElement,
            const VariablesParseState& vps) {
        return new ExpressionConstant(Value(exprElement));
    }


    intrusive_ptr<ExpressionConstant> ExpressionConstant::create(const Value& pValue) {
        intrusive_ptr<ExpressionConstant> pEC(new ExpressionConstant(pValue));
        return pEC;
    }

    ExpressionConstant::ExpressionConstant(const Value& pTheValue): pValue(pTheValue) {}


    intrusive_ptr<Expression> ExpressionConstant::optimize() {
        /* nothing to do */
        return intrusive_ptr<Expression>(this);
    }

    void ExpressionConstant::addDependencies(DepsTracker* deps, vector<string>* path) const {
        /* nothing to do */
    }

    Value ExpressionConstant::evaluateInternal(Variables* vars) const {
        return pValue;
    }

    Value ExpressionConstant::serialize(bool explain) const {
        return serializeConstant(pValue);
    }

    REGISTER_EXPRESSION("$const", ExpressionConstant::parse);
    REGISTER_EXPRESSION("$literal", ExpressionConstant::parse); // alias
    const char *ExpressionConstant::getOpName() const {
        return "$const";
    }

    /* ---------------------- ExpressionDayOfMonth ------------------------- */

    Value ExpressionDayOfMonth::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_mday);
    }

    REGISTER_EXPRESSION("$dayOfMonth", ExpressionDayOfMonth::parse);
    const char *ExpressionDayOfMonth::getOpName() const {
        return "$dayOfMonth";
    }

    /* ------------------------- ExpressionDayOfWeek ----------------------------- */

    Value ExpressionDayOfWeek::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_wday+1); // MySQL uses 1-7 tm uses 0-6
    }

    REGISTER_EXPRESSION("$dayOfWeek", ExpressionDayOfWeek::parse);
    const char *ExpressionDayOfWeek::getOpName() const {
        return "$dayOfWeek";
    }

    /* ------------------------- ExpressionDayOfYear ----------------------------- */

    Value ExpressionDayOfYear::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_yday+1); // MySQL uses 1-366 tm uses 0-365
    }

    REGISTER_EXPRESSION("$dayOfYear", ExpressionDayOfYear::parse);
    const char *ExpressionDayOfYear::getOpName() const {
        return "$dayOfYear";
    }

    /* ----------------------- ExpressionDivide ---------------------------- */

    Value ExpressionDivide::evaluateInternal(Variables* vars) const {
        Value lhs = vpOperand[0]->evaluateInternal(vars);
        Value rhs = vpOperand[1]->evaluateInternal(vars);

        if (lhs.numeric() && rhs.numeric()) {
            double numer = lhs.coerceToDouble();
            double denom = rhs.coerceToDouble();
            uassert(16608, "can't $divide by zero",
                    denom != 0);

            return Value(numer / denom);
        }
        else if (lhs.nullish() || rhs.nullish()) {
            return Value(BSONNULL);
        }
        else {
            uasserted(16609, str::stream() << "$divide only supports numeric types, not "
                                           << typeName(lhs.getType())
                                           << " and "
                                           << typeName(rhs.getType()));
        }
    }

    REGISTER_EXPRESSION("$divide", ExpressionDivide::parse);
    const char *ExpressionDivide::getOpName() const {
        return "$divide";
    }

    /* ---------------------- ExpressionObject --------------------------- */

    intrusive_ptr<ExpressionObject> ExpressionObject::create() {
        return new ExpressionObject(false);
    }

    intrusive_ptr<ExpressionObject> ExpressionObject::createRoot() {
        return new ExpressionObject(true);
    }

    ExpressionObject::ExpressionObject(bool atRoot)
        : _excludeId(false)
        , _atRoot(atRoot)
    {}

    intrusive_ptr<Expression> ExpressionObject::optimize() {
        for (FieldMap::iterator it(_expressions.begin()); it!=_expressions.end(); ++it) {
            if (it->second)
                it->second = it->second->optimize();
        }

        return intrusive_ptr<Expression>(this);
    }

    bool ExpressionObject::isSimple() {
        for (FieldMap::iterator it(_expressions.begin()); it!=_expressions.end(); ++it) {
            if (it->second && !it->second->isSimple())
                return false;
        }
        return true;
    }

    void ExpressionObject::addDependencies(DepsTracker* deps, vector<string>* path) const {
        string pathStr;
        if (path) {
            if (path->empty()) {
                // we are in the top level of a projection so _id is implicit
                if (!_excludeId)
                    deps->fields.insert("_id");
            }
            else {
                FieldPath f (*path);
                pathStr = f.getPath(false);
                pathStr += '.';
            }
        }
        else {
            verify(!_excludeId);
        }
        

        for (FieldMap::const_iterator it(_expressions.begin()); it!=_expressions.end(); ++it) {
            if (it->second) {
                if (path) path->push_back(it->first);
                it->second->addDependencies(deps, path);
                if (path) path->pop_back();
            }
            else { // inclusion
                uassert(16407, "inclusion not supported in objects nested in $expressions",
                        path);

                deps->fields.insert(pathStr + it->first);
            }
        }
    }

    void ExpressionObject::addToDocument(
        MutableDocument& out,
        const Document& currentDoc,
        Variables* vars
        ) const
    {
        FieldMap::const_iterator end = _expressions.end();

        // This is used to mark fields we've done so that we can add the ones we haven't
        set<string> doneFields;

        FieldIterator fields(currentDoc);
        while(fields.more()) {
            Document::FieldPair field (fields.next());

            // TODO don't make a new string here
            const string fieldName = field.first.toString();
            FieldMap::const_iterator exprIter = _expressions.find(fieldName);

            // This field is not supposed to be in the output (unless it is _id)
            if (exprIter == end) {
                if (!_excludeId && _atRoot && field.first == "_id") {
                    // _id from the root doc is always included (until exclusion is supported)
                    // not updating doneFields since "_id" isn't in _expressions
                    out.addField(field.first, field.second);
                }
                continue;
            }

            // make sure we don't add this field again
            doneFields.insert(exprIter->first);

            Expression* expr = exprIter->second.get();

            if (!expr) {
                // This means pull the matching field from the input document
                out.addField(field.first, field.second);
                continue;
            }

            ExpressionObject* exprObj = dynamic_cast<ExpressionObject*>(expr);
            BSONType valueType = field.second.getType();
            if ((valueType != Object && valueType != Array) || !exprObj ) {
                // This expression replace the whole field
                
                Value pValue(expr->evaluateInternal(vars));

                // don't add field if nothing was found in the subobject
                if (exprObj && pValue.getDocument().empty())
                    continue;

                /*
                   Don't add non-existent values (note:  different from NULL or Undefined);
                   this is consistent with existing selection syntax which doesn't
                   force the appearance of non-existent fields.
                   */
                if (!pValue.missing())
                    out.addField(field.first, pValue);

                continue;
            }

            /*
                Check on the type of the input value.  If it's an
                object, just walk down into that recursively, and
                add it to the result.
            */
            if (valueType == Object) {
                MutableDocument sub (exprObj->getSizeHint());
                exprObj->addToDocument(sub, field.second.getDocument(), vars);
                out.addField(field.first, sub.freezeToValue());
            }
            else if (valueType == Array) {
                /*
                    If it's an array, we have to do the same thing,
                    but to each array element.  Then, add the array
                    of results to the current document.
                */
                vector<Value> result;
                const vector<Value>& input = field.second.getArray();
                for (size_t i=0; i < input.size(); i++) {
                    // can't look for a subfield in a non-object value.
                    if (input[i].getType() != Object)
                        continue;

                    MutableDocument doc (exprObj->getSizeHint());
                    exprObj->addToDocument(doc, input[i].getDocument(), vars);
                    result.push_back(doc.freezeToValue());
                }

                out.addField(field.first, Value::consume(result));
            }
            else {
                verify( false );
            }
        }

        if (doneFields.size() == _expressions.size())
            return;

        /* add any remaining fields we haven't already taken care of */
        for (vector<string>::const_iterator i(_order.begin()); i!=_order.end(); ++i) {
            FieldMap::const_iterator it = _expressions.find(*i);
            string fieldName(it->first);

            /* if we've already dealt with this field, above, do nothing */
            if (doneFields.count(fieldName))
                continue;

            // this is a missing inclusion field
            if (!it->second)
                continue;

            Value pValue(it->second->evaluateInternal(vars));

            /*
              Don't add non-existent values (note:  different from NULL or Undefined);
              this is consistent with existing selection syntax which doesn't
              force the appearnance of non-existent fields.
            */
            if (pValue.missing())
                continue;

            // don't add field if nothing was found in the subobject
            if (dynamic_cast<ExpressionObject*>(it->second.get())
                    && pValue.getDocument().empty())
                continue;


            out.addField(fieldName, pValue);
        }
    }

    size_t ExpressionObject::getSizeHint() const {
        // Note: this can overestimate, but that is better than underestimating
        return _expressions.size() + (_excludeId ? 0 : 1);
    }

    Document ExpressionObject::evaluateDocument(Variables* vars) const {
        /* create and populate the result */
        MutableDocument out (getSizeHint());
        
        addToDocument(out,
                      Document(), // No inclusion field matching.
                      vars);
        return out.freeze();
    }

    Value ExpressionObject::evaluateInternal(Variables* vars) const {
        return Value(evaluateDocument(vars));
    }

    void ExpressionObject::addField(const FieldPath &fieldPath,
                                    const intrusive_ptr<Expression> &pExpression) {
        const string fieldPart = fieldPath.getFieldName(0);
        const bool haveExpr = _expressions.count(fieldPart);

        intrusive_ptr<Expression>& expr = _expressions[fieldPart]; // inserts if !haveExpr
        intrusive_ptr<ExpressionObject> subObj = dynamic_cast<ExpressionObject*>(expr.get());

        if (!haveExpr) {
            _order.push_back(fieldPart);
        }
        else { // we already have an expression or inclusion for this field
            if (fieldPath.getPathLength() == 1) {
                // This expression is for right here

                ExpressionObject* newSubObj = dynamic_cast<ExpressionObject*>(pExpression.get());
                uassert(16400, str::stream()
                             << "can't add an expression for field " << fieldPart
                             << " because there is already an expression for that field"
                             << " or one of its sub-fields.",
                        subObj && newSubObj); // we can merge them

                // Copy everything from the newSubObj to the existing subObj
                // This is for cases like { $project:{ 'b.c':1, b:{ a:1 } } }
                for (vector<string>::const_iterator it (newSubObj->_order.begin());
                                                    it != newSubObj->_order.end();
                                                    ++it) {
                    // asserts if any fields are dupes
                    subObj->addField(*it, newSubObj->_expressions[*it]);
                }
                return;
            }
            else {
                // This expression is for a subfield
                uassert(16401, str::stream()
                           << "can't add an expression for a subfield of " << fieldPart
                           << " because there is already an expression that applies to"
                           << " the whole field",
                        subObj);
            }
        }

        if (fieldPath.getPathLength() == 1) {
            verify(!haveExpr); // haveExpr case handled above.
            expr = pExpression;
            return;
        }

        if (!haveExpr)
            expr = subObj = ExpressionObject::create();

        subObj->addField(fieldPath.tail(), pExpression);
    }

    void ExpressionObject::includePath(const string &theFieldPath) {
        addField(theFieldPath, NULL);
    }

    Value ExpressionObject::serialize(bool explain) const {
        MutableDocument valBuilder;
        if (_excludeId)
            valBuilder["_id"] = Value(false);

        for (vector<string>::const_iterator it(_order.begin()); it!=_order.end(); ++it) {
            string fieldName = *it;
            verify(_expressions.find(fieldName) != _expressions.end());
            intrusive_ptr<Expression> expr = _expressions.find(fieldName)->second;

            if (!expr) {
                // this is inclusion, not an expression
                valBuilder[fieldName] = Value(true);
            }
            else {
                valBuilder[fieldName] = expr->serialize(explain);
            }
        }
        return valBuilder.freezeToValue();
    }

    /* --------------------- ExpressionFieldPath --------------------------- */

    // this is the old deprecated version only used by tests not using variables
    intrusive_ptr<ExpressionFieldPath> ExpressionFieldPath::create(const string& fieldPath) {
        return new ExpressionFieldPath("CURRENT." + fieldPath, Variables::ROOT_ID);
    }

    // this is the new version that supports every syntax
    intrusive_ptr<ExpressionFieldPath> ExpressionFieldPath::parse(
            const string& raw,
            const VariablesParseState& vps) {

        uassert(16873, str::stream() << "FieldPath '" << raw << "' doesn't start with $",
                raw.c_str()[0] == '$'); // c_str()[0] is always a valid reference.

        uassert(16872, str::stream() << "'$' by itself is not a valid FieldPath",
                raw.size() >= 2); // need at least "$" and either "$" or a field name

        if (raw[1] == '$') {
            const StringData rawSD = raw;
            const StringData fieldPath = rawSD.substr(2); // strip off $$
            const StringData varName = fieldPath.substr(0, fieldPath.find('.'));
            Variables::uassertValidNameForUserRead(varName);
            return new ExpressionFieldPath(fieldPath.toString(), vps.getVariable(varName));
        }
        else {
            return new ExpressionFieldPath("CURRENT." + raw.substr(1), // strip the "$" prefix
                                           vps.getVariable("CURRENT"));
        }
    }


    ExpressionFieldPath::ExpressionFieldPath(const string& theFieldPath, Variables::Id variable)
        : _fieldPath(theFieldPath)
        , _variable(variable)
    {}

    intrusive_ptr<Expression> ExpressionFieldPath::optimize() {
        /* nothing can be done for these */
        return intrusive_ptr<Expression>(this);
    }

    void ExpressionFieldPath::addDependencies(DepsTracker* deps, vector<string>* path) const {
        if (_variable == Variables::ROOT_ID) { // includes CURRENT when it is equivalent to ROOT.
            if (_fieldPath.getPathLength() == 1) {
                deps->needWholeDocument = true; // need full doc if just "$$ROOT"
            } else {
                deps->fields.insert(_fieldPath.tail().getPath(false));
            }
        }
    }

    Value ExpressionFieldPath::evaluatePathArray(size_t index, const Value& input) const {
        dassert(input.getType() == Array);

        // Check for remaining path in each element of array
        vector<Value> result;
        const vector<Value>& array = input.getArray();
        for (size_t i=0; i < array.size(); i++) {
            if (array[i].getType() != Object)
                continue;

            const Value nested = evaluatePath(index, array[i].getDocument());
            if (!nested.missing())
                result.push_back(nested);
        }

        return Value::consume(result);
    }
    Value ExpressionFieldPath::evaluatePath(size_t index, const Document& input) const {
        // Note this function is very hot so it is important that is is well optimized.
        // In particular, all return paths should support RVO.

        /* if we've hit the end of the path, stop */
        if (index == _fieldPath.getPathLength() - 1)
            return input[_fieldPath.getFieldName(index)];

        // Try to dive deeper
        const Value val = input[_fieldPath.getFieldName(index)];
        switch (val.getType()) {
        case Object:
            return evaluatePath(index+1, val.getDocument());

        case Array:
            return evaluatePathArray(index+1, val);

        default:
            return Value();
        }
    }

    Value ExpressionFieldPath::evaluateInternal(Variables* vars) const {
        if (_fieldPath.getPathLength() == 1) // get the whole variable
            return vars->getValue(_variable);

        if (_variable == Variables::ROOT_ID) {
            // ROOT is always a document so use optimized code path
            return evaluatePath(1, vars->getRoot());
        }

        Value var = vars->getValue(_variable);
        switch (var.getType()) {
        case Object: return evaluatePath(1, var.getDocument());
        case Array: return evaluatePathArray(1, var);
        default: return Value();
        }
    }

    Value ExpressionFieldPath::serialize(bool explain) const {
        if (_fieldPath.getFieldName(0) == "CURRENT" && _fieldPath.getPathLength() > 1) {
            // use short form for "$$CURRENT.foo" but not just "$$CURRENT"
            return Value("$" + _fieldPath.tail().getPath(false));
        }
        else {
            return Value("$$" + _fieldPath.getPath(false));
        }
    }

    /* ------------------------- ExpressionLet ----------------------------- */

    REGISTER_EXPRESSION("$let", ExpressionLet::parse);
    intrusive_ptr<Expression> ExpressionLet::parse(
            BSONElement expr,
            const VariablesParseState& vpsIn) {

        verify(str::equals(expr.fieldName(), "$let"));

        uassert(16874, "$let only supports an object as it's argument",
                expr.type() == Object);
        const BSONObj args = expr.embeddedObject();

        // varsElem must be parsed before inElem regardless of BSON order.
        BSONElement varsElem;
        BSONElement inElem;
        BSONForEach(arg, args) {
            if (str::equals(arg.fieldName(), "vars")) {
                varsElem = arg;
            } else if (str::equals(arg.fieldName(), "in")) {
                inElem = arg;
            } else {
                uasserted(16875, str::stream()
                        << "Unrecognized parameter to $let: " << arg.fieldName());
            }
        }

        uassert(16876, "Missing 'vars' parameter to $let",
                !varsElem.eoo());
        uassert(16877, "Missing 'in' parameter to $let",
                !inElem.eoo());

        // parse "vars"
        VariablesParseState vpsSub(vpsIn); // vpsSub gets our vars, vpsIn doesn't.
        VariableMap vars;
        BSONForEach(varElem, varsElem.embeddedObjectUserCheck()) {
            const string varName = varElem.fieldName();
            Variables::uassertValidNameForUserWrite(varName);
            Variables::Id id = vpsSub.defineVariable(varName);

            vars[id] = NameAndExpression(varName,
                                         parseOperand(varElem, vpsIn)); // only has outer vars
        }

        // parse "in"
        intrusive_ptr<Expression> subExpression = parseOperand(inElem, vpsSub); // has our vars

        return new ExpressionLet(vars, subExpression);
    }

    ExpressionLet::ExpressionLet(const VariableMap& vars, intrusive_ptr<Expression> subExpression)
        : _variables(vars)
        , _subExpression(subExpression)
    {}

    intrusive_ptr<Expression> ExpressionLet::optimize() {
        if (_variables.empty()) {
            // we aren't binding any variables so just return the subexpression
            return _subExpression->optimize();
        }

        for (VariableMap::iterator it=_variables.begin(), end=_variables.end(); it != end; ++it) {
            it->second.expression = it->second.expression->optimize();
        }

        // TODO be smarter with constant "variables"
        _subExpression = _subExpression->optimize();

        return this;
    }

    Value ExpressionLet::serialize(bool explain) const {
        MutableDocument vars;
        for (VariableMap::const_iterator it=_variables.begin(), end=_variables.end();
                it != end; ++it) {
            vars[it->second.name] = it->second.expression->serialize(explain);
        }

        return Value(DOC("$let" << DOC("vars" << vars.freeze()
                                    << "in" << _subExpression->serialize(explain))
                                    ));
    }

    Value ExpressionLet::evaluateInternal(Variables* vars) const {
        for (VariableMap::const_iterator it=_variables.begin(), end=_variables.end();
                it != end; ++it) {
            // It is guaranteed at parse-time that these expressions don't use the variable ids we
            // are setting
            vars->setValue(it->first,
                           it->second.expression->evaluateInternal(vars));
        }

        return _subExpression->evaluateInternal(vars);
    }

    void ExpressionLet::addDependencies(DepsTracker* deps, vector<string>* path) const {
        for (VariableMap::const_iterator it=_variables.begin(), end=_variables.end();
                it != end; ++it) {
            it->second.expression->addDependencies(deps);
        }

        // TODO be smarter when CURRENT is a bound variable
        _subExpression->addDependencies(deps);
    }


    /* ------------------------- ExpressionMap ----------------------------- */

    REGISTER_EXPRESSION("$map", ExpressionMap::parse);
    intrusive_ptr<Expression> ExpressionMap::parse(
            BSONElement expr,
            const VariablesParseState& vpsIn) {

        verify(str::equals(expr.fieldName(), "$map"));

        uassert(16878, "$map only supports an object as it's argument",
                expr.type() == Object);

        // "in" must be parsed after "as" regardless of BSON order
        BSONElement inputElem;
        BSONElement asElem;
        BSONElement inElem;
        const BSONObj args = expr.embeddedObject();
        BSONForEach(arg, args) {
            if (str::equals(arg.fieldName(), "input")) {
                inputElem = arg;
            } else if (str::equals(arg.fieldName(), "as")) {
                asElem = arg;
            } else if (str::equals(arg.fieldName(), "in")) {
                inElem = arg;
            } else {
                uasserted(16879, str::stream()
                        << "Unrecognized parameter to $map: " << arg.fieldName());
            }
        }

        uassert(16880, "Missing 'input' parameter to $map",
                !inputElem.eoo());
        uassert(16881, "Missing 'as' parameter to $map",
                !asElem.eoo());
        uassert(16882, "Missing 'in' parameter to $map",
                !inElem.eoo());

        // parse "input"
        intrusive_ptr<Expression> input = parseOperand(inputElem, vpsIn); // only has outer vars

        // parse "as"
        VariablesParseState vpsSub(vpsIn); // vpsSub gets our vars, vpsIn doesn't.
        string varName = asElem.str();
        Variables::uassertValidNameForUserWrite(varName);
        Variables::Id varId = vpsSub.defineVariable(varName);

        // parse "in"
        intrusive_ptr<Expression> in = parseOperand(inElem, vpsSub); // has access to map variable

        return new ExpressionMap(varName, varId, input, in);
    }

    ExpressionMap::ExpressionMap(const string& varName,
                                 Variables::Id varId,
                                 intrusive_ptr<Expression> input,
                                 intrusive_ptr<Expression> each)
        : _varName(varName)
        , _varId(varId)
        , _input(input)
        , _each(each)
    {}

    intrusive_ptr<Expression> ExpressionMap::optimize() {
        // TODO handle when _input is constant
        _input = _input->optimize();
        _each = _each->optimize();
        return this;
    }

    Value ExpressionMap::serialize(bool explain) const {
        return Value(DOC("$map" << DOC("input" << _input->serialize(explain)
                                    << "as" << _varName
                                    << "in" << _each->serialize(explain)
                                    )));
    }

    Value ExpressionMap::evaluateInternal(Variables* vars) const {
        // guaranteed at parse time that this isn't using our _varId
        const Value inputVal = _input->evaluateInternal(vars);
        if (inputVal.nullish())
            return Value(BSONNULL);

        uassert(16883, str::stream() << "input to $map must be an Array not "
                                     << typeName(inputVal.getType()),
                inputVal.getType() == Array);

        const vector<Value>& input = inputVal.getArray();

        if (input.empty())
            return inputVal;

        vector<Value> output;
        output.reserve(input.size());
        for (size_t i=0; i < input.size(); i++) {
            vars->setValue(_varId, input[i]);

            Value toInsert = _each->evaluateInternal(vars);
            if (toInsert.missing())
                toInsert = Value(BSONNULL); // can't insert missing values into array

            output.push_back(toInsert);
        }

        return Value::consume(output);
    }

    void ExpressionMap::addDependencies(DepsTracker* deps, vector<string>* path) const {
        _input->addDependencies(deps);
        _each->addDependencies(deps);
    }

    /* ------------------------- ExpressionMeta ----------------------------- */

    REGISTER_EXPRESSION("$meta", ExpressionMeta::parse);
    intrusive_ptr<Expression> ExpressionMeta::parse(
            BSONElement expr,
            const VariablesParseState& vpsIn) {

        uassert(17307, "$meta only supports String arguments",
                expr.type() == String);
        uassert(17308, "Unsupported argument to $meta: " + expr.String(),
                expr.String() == "textScore");

        return new ExpressionMeta();
    }

    Value ExpressionMeta::serialize(bool explain) const {
        return Value(DOC("$meta" << "textScore"));
    }

    Value ExpressionMeta::evaluateInternal(Variables* vars) const {
        const Document& root = vars->getRoot();
        return root.hasTextScore()
                ? Value(root.getTextScore())
                : Value();
    }

    void ExpressionMeta::addDependencies(DepsTracker* deps, vector<string>* path) const {
        deps->needTextScore = true;
    }

    /* ------------------------- ExpressionMillisecond ----------------------------- */

    Value ExpressionMillisecond::evaluateInternal(Variables* vars) const {
        Value date(vpOperand[0]->evaluateInternal(vars));
        const int ms = date.coerceToDate() % 1000LL;
        // adding 1000 since dates before 1970 would have negative ms
        return Value(ms >= 0 ? ms : 1000 + ms);
    }

    REGISTER_EXPRESSION("$millisecond", ExpressionMillisecond::parse);
    const char *ExpressionMillisecond::getOpName() const {
        return "$millisecond";
    }

    /* ------------------------- ExpressionMinute -------------------------- */

    Value ExpressionMinute::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_min);
    }

    REGISTER_EXPRESSION("$minute", ExpressionMinute::parse);
    const char *ExpressionMinute::getOpName() const {
        return "$minute";
    }

    /* ----------------------- ExpressionMod ---------------------------- */

    Value ExpressionMod::evaluateInternal(Variables* vars) const {
        Value lhs = vpOperand[0]->evaluateInternal(vars);
        Value rhs = vpOperand[1]->evaluateInternal(vars);

        BSONType leftType = lhs.getType();
        BSONType rightType = rhs.getType();

        if (lhs.numeric() && rhs.numeric()) {
            // ensure we aren't modding by 0
            double right = rhs.coerceToDouble();

            uassert(16610, "can't $mod by 0",
                    right != 0);

            if (leftType == NumberDouble
                || (rightType == NumberDouble && rhs.coerceToInt() != right)) {
                // the shell converts ints to doubles so if right is larger than int max or
                // if right truncates to something other than itself, it is a real double.
                // Integer-valued double case is handled below

                double left = lhs.coerceToDouble();
                return Value(fmod(left, right));
            }
            else if (leftType == NumberLong || rightType == NumberLong) {
                // if either is long, return long
                long long left = lhs.coerceToLong();
                long long rightLong = rhs.coerceToLong();
                return Value(left % rightLong);
            }

            // lastly they must both be ints, return int
            int left = lhs.coerceToInt();
            int rightInt = rhs.coerceToInt();
            return Value(left % rightInt);
        }
        else if (lhs.nullish() || rhs.nullish()) {
            return Value(BSONNULL);
        }
        else {
            uasserted(16611, str::stream() << "$mod only supports numeric types, not "
                                           << typeName(lhs.getType())
                                           << " and "
                                           << typeName(rhs.getType()));
        }
    }

    REGISTER_EXPRESSION("$mod", ExpressionMod::parse);
    const char *ExpressionMod::getOpName() const {
        return "$mod";
    }

    /* ------------------------ ExpressionMonth ----------------------------- */

    Value ExpressionMonth::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_mon + 1); // MySQL uses 1-12 tm uses 0-11
    }

    REGISTER_EXPRESSION("$month", ExpressionMonth::parse);
    const char *ExpressionMonth::getOpName() const {
        return "$month";
    }

    /* ------------------------- ExpressionMultiply ----------------------------- */

    Value ExpressionMultiply::evaluateInternal(Variables* vars) const {
        /*
          We'll try to return the narrowest possible result value.  To do that
          without creating intermediate Values, do the arithmetic for double
          and integral types in parallel, tracking the current narrowest
          type.
         */
        double doubleProduct = 1;
        long long longProduct = 1;
        BSONType productType = NumberInt;

        const size_t n = vpOperand.size();
        for(size_t i = 0; i < n; ++i) {
            Value val = vpOperand[i]->evaluateInternal(vars);

            if (val.numeric()) {
                productType = Value::getWidestNumeric(productType, val.getType());

                doubleProduct *= val.coerceToDouble();
                longProduct *= val.coerceToLong();
            }
            else if (val.nullish()) {
                return Value(BSONNULL);
            }
            else {
                uasserted(16555, str::stream() << "$multiply only supports numeric types, not "
                                               << typeName(val.getType()));
            }
        }

        if (productType == NumberDouble)
            return Value(doubleProduct);
        else if (productType == NumberLong)
            return Value(longProduct);
        else if (productType == NumberInt)
            return Value::createIntOrLong(longProduct);
        else
            massert(16418, "$multiply resulted in a non-numeric type", false);
    }

    REGISTER_EXPRESSION("$multiply", ExpressionMultiply::parse);
    const char *ExpressionMultiply::getOpName() const {
        return "$multiply";
    }

    /* ------------------------- ExpressionHour ----------------------------- */

    Value ExpressionHour::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_hour);
    }

    REGISTER_EXPRESSION("$hour", ExpressionHour::parse);
    const char *ExpressionHour::getOpName() const {
        return "$hour";
    }

    /* ----------------------- ExpressionIfNull ---------------------------- */

    Value ExpressionIfNull::evaluateInternal(Variables* vars) const {
        Value pLeft(vpOperand[0]->evaluateInternal(vars));
        if (!pLeft.nullish())
            return pLeft;

        Value pRight(vpOperand[1]->evaluateInternal(vars));
        return pRight;
    }

    REGISTER_EXPRESSION("$ifNull", ExpressionIfNull::parse);
    const char *ExpressionIfNull::getOpName() const {
        return "$ifNull";
    }

    /* ------------------------ ExpressionNary ----------------------------- */

    intrusive_ptr<Expression> ExpressionNary::optimize() {
        const size_t n = vpOperand.size();

        // optimize sub-expressions and count constants
        unsigned constCount = 0;
        for(size_t i = 0; i < n; ++i) {
            intrusive_ptr<Expression> optimized = vpOperand[i]->optimize();

            // substitute the optimized expression
            vpOperand[i] = optimized;

            // check to see if the result was a constant
            if (dynamic_cast<ExpressionConstant*>(optimized.get())) {
                constCount++;
            }
        }

        // If all the operands are constant, we can replace this expression with a constant. Using
        // an empty Variables since it will never be accessed.  
        if (constCount == n) {
            Variables emptyVars;
            Value pResult(evaluateInternal(&emptyVars));
            intrusive_ptr<Expression> pReplacement(
                ExpressionConstant::create(pResult));
            return pReplacement;
        }

        // Remaining optimizations are only for associative and commutative expressions.
        if (!isAssociativeAndCommutative())
            return this;

        // Process vpOperand to split it into constant and nonconstant vectors.
        // This can leave vpOperand in an invalid state that is cleaned up after the loop.
        ExpressionVector constExprs;
        ExpressionVector nonConstExprs;
        for(size_t i = 0; i < vpOperand.size(); ++i) { // NOTE: vpOperand grows in loop
            intrusive_ptr<Expression> expr = vpOperand[i];
            if (dynamic_cast<ExpressionConstant*>(expr.get())) {
                constExprs.push_back(expr);
            }
            else {
                // If the child operand is the same type as this, then we can
                // extract its operands and inline them here because we know
                // this is commutative and associative.  We detect sameness of
                // the child operator by checking for equality of the opNames
                ExpressionNary* nary = dynamic_cast<ExpressionNary*>(expr.get());
                if (!nary || !str::equals(nary->getOpName(), getOpName())) {
                    nonConstExprs.push_back(expr);
                }
                else {
                    // same expression, so flatten by adding to vpOperand which
                    // will be processed later in this loop.
                    vpOperand.insert(vpOperand.end(),
                                     nary->vpOperand.begin(),
                                     nary->vpOperand.end());
                }
            }
        }

        // collapse all constant expressions (if any)
        Value constValue;
        if (!constExprs.empty()) {
            vpOperand = constExprs;
            Variables emptyVars;
            constValue = evaluateInternal(&emptyVars);
        }

        // now set the final expression list with constant (if any) at the end
        vpOperand = nonConstExprs;
        if (!constExprs.empty()) {
            vpOperand.push_back(ExpressionConstant::create(constValue));
        }

        return this;
    }

    void ExpressionNary::addDependencies(DepsTracker* deps, vector<string>* path) const {
        for(ExpressionVector::const_iterator i(vpOperand.begin());
            i != vpOperand.end(); ++i) {
            (*i)->addDependencies(deps);
        }
    }

    void ExpressionNary::addOperand(const intrusive_ptr<Expression>& pExpression) {
        vpOperand.push_back(pExpression);
    }

    Value ExpressionNary::serialize(bool explain) const {
        const size_t nOperand = vpOperand.size();
        vector<Value> array;
        /* build up the array */
        for(size_t i = 0; i < nOperand; i++)
            array.push_back(vpOperand[i]->serialize(explain));

        return Value(DOC(getOpName() << array));
    }

    /* ------------------------- ExpressionNot ----------------------------- */

    Value ExpressionNot::evaluateInternal(Variables* vars) const {
        Value pOp(vpOperand[0]->evaluateInternal(vars));

        bool b = pOp.coerceToBool();
        return Value(!b);
    }

    REGISTER_EXPRESSION("$not", ExpressionNot::parse);
    const char *ExpressionNot::getOpName() const {
        return "$not";
    }

    /* -------------------------- ExpressionOr ----------------------------- */

    Value ExpressionOr::evaluateInternal(Variables* vars) const {
        const size_t n = vpOperand.size();
        for(size_t i = 0; i < n; ++i) {
            Value pValue(vpOperand[i]->evaluateInternal(vars));
            if (pValue.coerceToBool())
                return Value(true);
        }

        return Value(false);
    }

    intrusive_ptr<Expression> ExpressionOr::optimize() {
        /* optimize the disjunction as much as possible */
        intrusive_ptr<Expression> pE(ExpressionNary::optimize());

        /* if the result isn't a disjunction, we can't do anything */
        ExpressionOr *pOr = dynamic_cast<ExpressionOr *>(pE.get());
        if (!pOr)
            return pE;

        /*
          Check the last argument on the result; if it's not constant (as
          promised by ExpressionNary::optimize(),) then there's nothing
          we can do.
        */
        const size_t n = pOr->vpOperand.size();
        // ExpressionNary::optimize() generates an ExpressionConstant for {$or:[]}.
        verify(n > 0);
        intrusive_ptr<Expression> pLast(pOr->vpOperand[n - 1]);
        const ExpressionConstant *pConst =
            dynamic_cast<ExpressionConstant *>(pLast.get());
        if (!pConst)
            return pE;

        /*
          Evaluate and coerce the last argument to a boolean.  If it's true,
          then we can replace this entire expression.
         */
        bool last = pConst->getValue().coerceToBool();
        if (last) {
            intrusive_ptr<ExpressionConstant> pFinal(
                ExpressionConstant::create(Value(true)));
            return pFinal;
        }

        /*
          If we got here, the final operand was false, so we don't need it
          anymore.  If there was only one other operand, we don't need the
          conjunction either.  Note we still need to keep the promise that
          the result will be a boolean.
         */
        if (n == 2) {
            intrusive_ptr<Expression> pFinal(
                ExpressionCoerceToBool::create(pOr->vpOperand[0]));
            return pFinal;
        }

        /*
          Remove the final "false" value, and return the new expression.
        */
        pOr->vpOperand.resize(n - 1);
        return pE;
    }

    REGISTER_EXPRESSION("$or", ExpressionOr::parse);
    const char *ExpressionOr::getOpName() const {
        return "$or";
    }

    /* ------------------------- ExpressionSecond ----------------------------- */

    Value ExpressionSecond::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_sec);
    }

    REGISTER_EXPRESSION("$second", ExpressionSecond::parse);
    const char *ExpressionSecond::getOpName() const {
        return "$second";
    }



    namespace {
        ValueSet arrayToSet(const Value& val) {
            const vector<Value>& array = val.getArray();
            return ValueSet(array.begin(), array.end());
        }
    }

    /* ----------------------- ExpressionSetDifference ---------------------------- */

    Value ExpressionSetDifference::evaluateInternal(Variables* vars) const {
        const Value lhs = vpOperand[0]->evaluateInternal(vars);
        const Value rhs = vpOperand[1]->evaluateInternal(vars);

        if (lhs.nullish() || rhs.nullish()) {
            return Value(BSONNULL);
        }

        uassert(17048, str::stream() << "both operands of $setDifference must be arrays. First "
                                     << "argument is of type: " << typeName(lhs.getType()),
                lhs.getType() == Array);
        uassert(17049, str::stream() << "both operands of $setDifference must be arrays. Second "
                                     << "argument is of type: " << typeName(rhs.getType()),
                rhs.getType() == Array);

        ValueSet rhsSet = arrayToSet(rhs);
        const vector<Value>& lhsArray = lhs.getArray();
        vector<Value> returnVec;

        for (vector<Value>::const_iterator it = lhsArray.begin(); it != lhsArray.end(); ++it) {
            // rhsSet serves the dual role of filtering out elements that were originally present
            // in RHS and of eleminating duplicates from LHS
            if (rhsSet.insert(*it).second) {
                returnVec.push_back(*it);
            }
        }
        return Value::consume(returnVec);
    }

    REGISTER_EXPRESSION("$setDifference", ExpressionSetDifference::parse);
    const char *ExpressionSetDifference::getOpName() const {
        return "$setDifference";
    }

    /* ----------------------- ExpressionSetEquals ---------------------------- */

    void ExpressionSetEquals::validateArguments(const ExpressionVector& args) const {
        uassert(17045, str::stream() << "$setEquals needs at least two arguments had: "
                                     << args.size(),
                args.size() >= 2);
    }

    Value ExpressionSetEquals::evaluateInternal(Variables* vars) const {
        const size_t n = vpOperand.size();
        std::set<Value> lhs;

        for (size_t i = 0; i < n; i++) {
            const Value nextEntry = vpOperand[i]->evaluateInternal(vars);
            uassert(17044, str::stream() << "All operands of $setEquals must be arrays. One "
                                         << "argument is of type: "
                                         << typeName(nextEntry.getType()),
                    nextEntry.getType() == Array);

            if (i == 0) {
                lhs.insert(nextEntry.getArray().begin(), nextEntry.getArray().end());
            }
            else {
                const std::set<Value> rhs(nextEntry.getArray().begin(), nextEntry.getArray().end());
                if (lhs != rhs) {
                    return Value(false);
                }
            }
        }
        return Value(true);
    }

    REGISTER_EXPRESSION("$setEquals", ExpressionSetEquals::parse);
    const char *ExpressionSetEquals::getOpName() const {
        return "$setEquals";
    }

    /* ----------------------- ExpressionSetIntersection ---------------------------- */

    Value ExpressionSetIntersection::evaluateInternal(Variables* vars) const {
        const size_t n = vpOperand.size();
        ValueSet currentIntersection;
        for (size_t i = 0; i < n; i++) {
            const Value nextEntry = vpOperand[i]->evaluateInternal(vars);
            if (nextEntry.nullish()) {
                return Value(BSONNULL);
            }
            uassert(17047, str::stream() << "All operands of $setIntersection must be arrays. One "
                                         << "argument is of type: "
                                         << typeName(nextEntry.getType()),
                    nextEntry.getType() == Array);

            if (i == 0) {
                currentIntersection.insert(nextEntry.getArray().begin(),
                                           nextEntry.getArray().end());
            }
            else {
                ValueSet nextSet = arrayToSet(nextEntry);
                if (currentIntersection.size() > nextSet.size()) {
                    // to iterate over whichever is the smaller set
                    nextSet.swap(currentIntersection);
                }
                ValueSet::iterator it = currentIntersection.begin();
                while (it != currentIntersection.end()) {
                    if (!nextSet.count(*it)) {
                        ValueSet::iterator del = it;
                        ++it;
                        currentIntersection.erase(del);
                    }
                    else {
                        ++it;
                    }
                }
            }
            if (currentIntersection.empty()) {
                break;
            }
        }
        vector<Value> result = vector<Value>(currentIntersection.begin(),
                                             currentIntersection.end());
        return Value::consume(result);
    }

    REGISTER_EXPRESSION("$setIntersection", ExpressionSetIntersection::parse);
    const char *ExpressionSetIntersection::getOpName() const {
        return "$setIntersection";
    }

    /* ----------------------- ExpressionSetIsSubset ---------------------------- */

namespace {
    Value setIsSubsetHelper(const vector<Value>& lhs, const ValueSet& rhs) {
        // do not shortcircuit when lhs.size() > rhs.size()
        // because lhs can have redundant entries
        for (vector<Value>::const_iterator it = lhs.begin(); it != lhs.end(); ++it) {
            if (!rhs.count(*it)) {
                return Value(false);
            }
        }
        return Value(true);
    }
}

    Value ExpressionSetIsSubset::evaluateInternal(Variables* vars) const {
        const Value lhs = vpOperand[0]->evaluateInternal(vars);
        const Value rhs = vpOperand[1]->evaluateInternal(vars);

        uassert(17046, str::stream() << "both operands of $setIsSubset must be arrays. First "
                                     << "argument is of type: " << typeName(lhs.getType()),
                lhs.getType() == Array);
        uassert(17042, str::stream() << "both operands of $setIsSubset must be arrays. Second "
                                     << "argument is of type: " << typeName(rhs.getType()),
                rhs.getType() == Array);

        return setIsSubsetHelper(lhs.getArray(), arrayToSet(rhs));
    }

    /**
     * This class handles the case where the RHS set is constant.
     *
     * Since it is constant we can construct the hashset once which makes the runtime performance
     * effectively constant with respect to the size of RHS. Large, constant RHS is expected to be a
     * major use case for $redact and this has been verified to improve performance significantly.
     */
    class ExpressionSetIsSubset::Optimized : public ExpressionSetIsSubset {
    public:
        Optimized(const ValueSet& cachedRhsSet, const ExpressionVector& operands)
            : _cachedRhsSet(cachedRhsSet)
        {
            vpOperand = operands;
        }

        virtual Value evaluateInternal(Variables* vars) const {
            const Value lhs = vpOperand[0]->evaluateInternal(vars);

            uassert(17310, str::stream() << "both operands of $setIsSubset must be arrays. First "
                                         << "argument is of type: " << typeName(lhs.getType()),
                    lhs.getType() == Array);

            return setIsSubsetHelper(lhs.getArray(), _cachedRhsSet);
        }

    private:
        const ValueSet _cachedRhsSet;
    };

    intrusive_ptr<Expression> ExpressionSetIsSubset::optimize() {
        // perfore basic optimizations
        intrusive_ptr<Expression> optimized = ExpressionNary::optimize();

        // if ExpressionNary::optimize() created a new value, return it directly
        if (optimized.get() != this)
            return optimized;

        if (ExpressionConstant* ec = dynamic_cast<ExpressionConstant*>(vpOperand[1].get())) {
            const Value rhs = ec->getValue();
            uassert(17311, str::stream() << "both operands of $setIsSubset must be arrays. Second "
                                         << "argument is of type: " << typeName(rhs.getType()),
                    rhs.getType() == Array);

            return new Optimized(arrayToSet(rhs), vpOperand);
        }

        return optimized;
    }

    REGISTER_EXPRESSION("$setIsSubset", ExpressionSetIsSubset::parse);
    const char *ExpressionSetIsSubset::getOpName() const {
        return "$setIsSubset";
    }

    /* ----------------------- ExpressionSetUnion ---------------------------- */

    Value ExpressionSetUnion::evaluateInternal(Variables* vars) const {
        ValueSet unionedSet;
        const size_t n = vpOperand.size();
        for (size_t i = 0; i < n; i++) {
            const Value newEntries = vpOperand[i]->evaluateInternal(vars);
            if (newEntries.nullish()) {
                return Value(BSONNULL);
            }
            uassert(17043, str::stream() << "All operands of $setUnion must be arrays. One argument"
                                         << " is of type: " << typeName(newEntries.getType()),
                    newEntries.getType() == Array);

            unionedSet.insert(newEntries.getArray().begin(), newEntries.getArray().end());
        }
        vector<Value> result = vector<Value>(unionedSet.begin(), unionedSet.end());
        return Value::consume(result);
    }

    REGISTER_EXPRESSION("$setUnion", ExpressionSetUnion::parse);
    const char *ExpressionSetUnion::getOpName() const {
        return "$setUnion";
    }

    /* ----------------------- ExpressionSize ---------------------------- */

    Value ExpressionSize::evaluateInternal(Variables* vars) const {
        Value array = vpOperand[0]->evaluateInternal(vars);

        uassert(17124, str::stream() << "The argument to $size must be an Array, but was of type: "
                                     << typeName(array.getType()),
                array.getType() == Array);
        return Value::createIntOrLong(array.getArray().size());
    }

    REGISTER_EXPRESSION("$size", ExpressionSize::parse);
    const char *ExpressionSize::getOpName() const {
        return "$size";
    }

    /* ----------------------- ExpressionStrcasecmp ---------------------------- */

    Value ExpressionStrcasecmp::evaluateInternal(Variables* vars) const {
        Value pString1(vpOperand[0]->evaluateInternal(vars));
        Value pString2(vpOperand[1]->evaluateInternal(vars));

        /* boost::iequals returns a bool not an int so strings must actually be allocated */
        string str1 = boost::to_upper_copy( pString1.coerceToString() );
        string str2 = boost::to_upper_copy( pString2.coerceToString() );
        int result = str1.compare(str2);

        if (result == 0)
            return Value(0);
        else if (result > 0)
            return Value(1);
        else
            return Value(-1);
    }

    REGISTER_EXPRESSION("$strcasecmp", ExpressionStrcasecmp::parse);
    const char *ExpressionStrcasecmp::getOpName() const {
        return "$strcasecmp";
    }

    /* ----------------------- ExpressionSubstr ---------------------------- */

    Value ExpressionSubstr::evaluateInternal(Variables* vars) const {
        Value pString(vpOperand[0]->evaluateInternal(vars));
        Value pLower(vpOperand[1]->evaluateInternal(vars));
        Value pLength(vpOperand[2]->evaluateInternal(vars));

        string str = pString.coerceToString();
        uassert(16034, str::stream() << getOpName() <<
                ":  starting index must be a numeric type (is BSON type " <<
                typeName(pLower.getType()) << ")",
                (pLower.getType() == NumberInt 
                 || pLower.getType() == NumberLong 
                 || pLower.getType() == NumberDouble));
        uassert(16035, str::stream() << getOpName() <<
                ":  length must be a numeric type (is BSON type " <<
                typeName(pLength.getType() )<< ")",
                (pLength.getType() == NumberInt 
                 || pLength.getType() == NumberLong 
                 || pLength.getType() == NumberDouble));
        string::size_type lower = static_cast< string::size_type >( pLower.coerceToLong() );
        string::size_type length = static_cast< string::size_type >( pLength.coerceToLong() );
        if ( lower >= str.length() ) {
            // If lower > str.length() then string::substr() will throw out_of_range, so return an
            // empty string if lower is not a valid string index.
            return Value("");
        }
        return Value(str.substr(lower, length));
    }

    REGISTER_EXPRESSION("$substr", ExpressionSubstr::parse);
    const char *ExpressionSubstr::getOpName() const {
        return "$substr";
    }

    /* ----------------------- ExpressionSubtract ---------------------------- */

    Value ExpressionSubtract::evaluateInternal(Variables* vars) const {
        Value lhs = vpOperand[0]->evaluateInternal(vars);
        Value rhs = vpOperand[1]->evaluateInternal(vars);
            
        BSONType diffType = Value::getWidestNumeric(rhs.getType(), lhs.getType());

        if (diffType == NumberDouble) {
            double right = rhs.coerceToDouble();
            double left = lhs.coerceToDouble();
            return Value(left - right);
        } 
        else if (diffType == NumberLong) {
            long long right = rhs.coerceToLong();
            long long left = lhs.coerceToLong();
            return Value(left - right);
        }
        else if (diffType == NumberInt) {
            long long right = rhs.coerceToLong();
            long long left = lhs.coerceToLong();
            return Value::createIntOrLong(left - right);
        }
        else if (lhs.nullish() || rhs.nullish()) {
            return Value(BSONNULL);
        }
        else if (lhs.getType() == Date) {
            if (rhs.getType() == Date) {
                long long timeDelta = lhs.getDate() - rhs.getDate();
                return Value(timeDelta);
            }
            else if (rhs.numeric()) {
                long long millisSinceEpoch = lhs.getDate() - rhs.coerceToLong();
                return Value(Date_t(millisSinceEpoch));
            }
            else {
                uasserted(16613, str::stream() << "cant $subtract a "
                                               << typeName(rhs.getType())
                                               << " from a Date");
            }
        }
        else {
            uasserted(16556, str::stream() << "cant $subtract a"
                                           << typeName(rhs.getType())
                                           << " from a "
                                           << typeName(lhs.getType()));
        }
    }

    REGISTER_EXPRESSION("$subtract", ExpressionSubtract::parse);
    const char *ExpressionSubtract::getOpName() const {
        return "$subtract";
    }

    /* ------------------------- ExpressionToLower ----------------------------- */

    Value ExpressionToLower::evaluateInternal(Variables* vars) const {
        Value pString(vpOperand[0]->evaluateInternal(vars));
        string str = pString.coerceToString();
        boost::to_lower(str);
        return Value(str);
    }

    REGISTER_EXPRESSION("$toLower", ExpressionToLower::parse);
    const char *ExpressionToLower::getOpName() const {
        return "$toLower";
    }

    /* ------------------------- ExpressionToUpper -------------------------- */

    Value ExpressionToUpper::evaluateInternal(Variables* vars) const {
        Value pString(vpOperand[0]->evaluateInternal(vars));
        string str(pString.coerceToString());
        boost::to_upper(str);
        return Value(str);
    }

    REGISTER_EXPRESSION("$toUpper", ExpressionToUpper::parse);
    const char *ExpressionToUpper::getOpName() const {
        return "$toUpper";
    }

    /* ------------------------- ExpressionWeek ----------------------------- */

    Value ExpressionWeek::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        int dayOfWeek = date.tm_wday;
        int dayOfYear = date.tm_yday;
        int prevSundayDayOfYear = dayOfYear - dayOfWeek; // may be negative
        int nextSundayDayOfYear = prevSundayDayOfYear + 7; // must be positive

        // Return the zero based index of the week of the next sunday, equal to the one based index
        // of the week of the previous sunday, which is to be returned.
        int nextSundayWeek = nextSundayDayOfYear / 7;

        // Verify that the week calculation is consistent with strftime "%U".
        DEV{
            char buf[3];
            verify(strftime(buf,3,"%U",&date));
            verify(int(str::toUnsigned(buf))==nextSundayWeek);
        }

        return Value(nextSundayWeek);
    }

    REGISTER_EXPRESSION("$week", ExpressionWeek::parse);
    const char *ExpressionWeek::getOpName() const {
        return "$week";
    }

    /* ------------------------- ExpressionYear ----------------------------- */

    Value ExpressionYear::evaluateInternal(Variables* vars) const {
        Value pDate(vpOperand[0]->evaluateInternal(vars));
        tm date = pDate.coerceToTm();
        return Value(date.tm_year + 1900); // tm_year is years since 1900
    }

    REGISTER_EXPRESSION("$year", ExpressionYear::parse);
    const char *ExpressionYear::getOpName() const {
        return "$year";
    }

}