htsql-tutorial / src / htsql / core / tr / compile.py

   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
#
# Copyright (c) 2006-2012, Prometheus Research, LLC
#


"""
:mod:`htsql.core.tr.compile`
============================

This module implements the compiling process.
"""


from ..util import maybe, listof
from ..adapter import Adapter, adapt, adapt_many
from ..domain import BooleanDomain
from .error import CompileError
from .coerce import coerce
from .signature import IsNullSig, AndSig
from .flow import (Expression, QueryExpr, SegmentCode, Code, LiteralCode,
                   FormulaCode, Flow, RootFlow, ScalarFlow, TableFlow,
                   QuotientFlow, ComplementFlow, MonikerFlow,
                   ForkedFlow, LinkedFlow, FilteredFlow, OrderedFlow,
                   Unit, ScalarUnit, ColumnUnit, AggregateUnit, CorrelatedUnit,
                   KernelUnit, CoveringUnit)
from .term import (Term, ScalarTerm, TableTerm, FilterTerm, JoinTerm,
                   EmbeddingTerm, CorrelationTerm, ProjectionTerm, OrderTerm,
                   WrapperTerm, PermanentTerm, SegmentTerm, QueryTerm, Joint)
from .stitch import arrange, spread, sew, tie


class CompilingState(object):
    """
    Encapsulates the state of the compiling process.

    State attributes:

    `root` (:class:`htsql.core.tr.flow.RootFlow`)
        The root flow.

    `baseline` (:class:`htsql.core.tr.flow.Flow`)
        When compiling a new term, indicates the leftmost axis that must
        exported by the term.  Note that the baseline flow is always
        inflated.
    """

    def __init__(self):
        # The next term tag to be produced by `tag`.
        self.next_tag = 1
        # The root scalar flow.
        self.root = None
        # The stack of previous baseline flows.
        self.baseline_stack = []
        # The current baseline flow.
        self.baseline = None
        # Support for nested segments.
        self.superflow_stack = []
        self.superflow = None

    def tag(self):
        """
        Generates and returns a new unique term tag.
        """
        tag = self.next_tag
        self.next_tag += 1
        return tag

    def set_root(self, flow):
        """
        Initializes the root, baseline and mask flows.

        This function must be called before state attributes `root`,
        `baseline` and `mask` could be used.

        `flow` (:class:`htsql.core.tr.flow.RootFlow`)
            A root scalar flow.
        """
        assert isinstance(flow, RootFlow)
        # Check that the state flows are not yet initialized.
        assert self.root is None
        assert self.baseline is None
        assert self.superflow is None
        self.root = flow
        self.baseline = flow
        self.superflow = flow

    def flush(self):
        """
        Clears the state flows.
        """
        # Check that the state flows are initialized and the flow stacks
        # are exhausted.
        assert self.root is not None
        assert not self.baseline_stack
        assert self.baseline is self.root
        self.root = None
        self.baseline = None
        self.superflow = None

    def push_baseline(self, baseline):
        """
        Sets a new baseline flow.

        This function masks the current baseline flow.  To restore
        the previous baseline flow, use :meth:`pop_baseline`.

        `baseline` (:class:`htsql.core.tr.flow.Flow`)
            The new baseline flow.  Note that the baseline flow
            must be inflated.
        """
        assert isinstance(baseline, Flow) and baseline.is_inflated
        self.baseline_stack.append(self.baseline)
        self.baseline = baseline

    def pop_baseline(self):
        """
        Restores the previous baseline flow.
        """
        self.baseline = self.baseline_stack.pop()

    def push_superflow(self, flow):
        self.superflow_stack.append(self.superflow)
        self.superflow = flow

    def pop_superflow(self):
        self.superflow = self.superflow_stack.pop()

    def compile(self, expression, baseline=None):
        """
        Compiles a new term node for the given expression.

        `expression` (:class:`htsql.core.tr.flow.Expression`)
            An expression node.

        `baseline` (:class:`htsql.core.tr.flow.Flow` or ``None``)
            The baseline flow.  Specifies an axis flow that the compiled
            term must export.  If not set, the current baseline flow of
            the state is used.

            When `expression` is a flow, the generated term must
            export the flow itself as well as all inflated prefixes
            up to the `baseline` flow.  It may (but it is not required)
            export other axes as well.
        """
        # FIXME: potentially, we could implement a cache of `expression`
        # -> `term` to avoid generating the same term node more than once.
        # There are several complications though.  First, the term depends
        # not only on the expression, but also on the current baseline
        # and mask flows.  Second, each compiled term must have a unique
        # tag, therefore we'd have to replace the tags and route tables
        # of the cached term node.
        return compile(expression, self, baseline=baseline)

    def inject(self, term, expressions):
        """
        Augments a term to make it capable of producing the given expressions.

        This method takes a term node and a list of expressions.  It returns
        a term that could produce the same expressions as the given term, and,
        in addition, all the given expressions.

        Note that, technically, a term only exports unit expressions;
        we claim that a term could export an expression if it exports
        all the units of the expression.

        `term` (:class:`htsql.core.tr.term.Term`)
            A term node.

        `expressions` (a list of :class:`htsql.core.tr.flow.Expression`)
            A list of expressions to inject into the given term.
        """
        assert isinstance(term, Term)
        assert isinstance(expressions, listof(Expression))
        # Iterate over the expressions to inject.
        for expression in expressions:
            # A quick check to avoid a costly adapter call.  This
            # only works if the expression is a unit.
            if expression in term.routes:
                continue
            # Inject the expression into the term.
            term = Inject.__invoke__(expression, term, self)
        # Return the augmented term node.
        return term


class CompileBase(Adapter):

    adapt(Expression)

    # Utility functions used by implementations.

    def compile_shoot(self, flow, trunk, codes=None):
        """
        Compiles a term corresponding to the given flow.

        The compiled term is called *a shoot term* (relatively to
        the given *trunk term*).

        `flow` (:class:`htsql.core.tr.flow.Flow`)
            A flow node, for which the we compile a term.

        `trunk` (:class:`htsql.core.tr.flow.Flow` or :class:`htsql.core.tr.term.Term`)
           Expresses a promise that the compiled term will be
           (eventually) joined to a term corresponding to the
           `trunk` flow.  If `trunk` is a :class:`htsql.core.tr.term.Term`
           instance, use the term flow.

        `codes` (a list of :class:`htsql.core.tr.flow.Expression` or ``None``)
           If provided, a list of expressions to be injected
           into the compiled term.
        """

        # Sanity check on the arguments.
        assert isinstance(flow, Flow)
        assert isinstance(trunk, (Flow, Term))
        assert isinstance(codes, maybe(listof(Expression)))

        # If a term node is passed in place of a trunk flow, use
        # the flow of the term.
        if isinstance(trunk, Term):
            trunk = trunk.flow

        # This condition is enforced by unmasking process -- all
        # non-axial operations in the trunk flow are pruned from
        # the given flow.
        assert flow == flow.prune(trunk)

        # Determine the longest ancestor of the flow that contains
        # no non-axial operations.
        baseline = flow
        while not baseline.is_inflated:
            baseline = baseline.base

        # Handle the case when the given flow is not spanned by the
        # trunk flow -- it happens when we construct a plural term
        # for an aggregate unit.  In this case, before joining it
        # to the trunk term, the shoot term will be projected to some
        # singular prefix of the given flow.  To enable such projection,
        # at least the base of the shoot baseline must be spanned by
        # the trunk flow (then, we can project on the columns of
        # a foreign key that attaches the baseline to its base).
        if not trunk.spans(baseline):
            while not trunk.spans(baseline.base):
                baseline = baseline.base

        # Compile the term for the given flow up to the baseline.
        term = self.state.compile(flow, baseline=baseline)

        # If provided, inject the given expressions.
        if codes is not None:
            term = self.state.inject(term, codes)

        # Return the compiled shoot term.
        return term

    def glue_flows(self, flow, baseline, shoot, shoot_baseline):
        """
        Returns joints attaching the shoot flow to the trunk flow.

        The given flow nodes specify the shape of two term nodes:
        the trunk term and the shoot term.  The function returns
        a list of :class:`htsql.core.tr.term.Joint` objects that could
        be used to attach the shoot term to the trunk term without
        changing the cardinality of the latter.

        `flow` (:class:`htsql.core.tr.flow.Flow`)
            The flow of the trunk term.

        `baseline` (:class:`htsql.core.tr.flow.Flow`)
            The baseline of the trunk term.

        `shoot` (:class:`htsql.core.tr.flow.Flow`)
            The flow of the shoot term.

        `shoot_baseline` (:class:`htsql.core.tr.flow.Flow`)
            The baseline of the shoot term.
        """
        # Sanity check on the arguments.
        assert isinstance(flow, Flow)
        assert isinstance(baseline, Flow)
        assert baseline.is_inflated
        # The `flow` may represent not the trunk flow itself,
        # but one of its ancestors which may lie below `baseline`.
        #assert flow.concludes(baseline)
        assert isinstance(shoot, Flow)
        assert isinstance(shoot_baseline, Flow)
        assert shoot_baseline.is_inflated
        assert shoot.concludes(shoot_baseline)

        # Verify that it is possible to join the terms without
        # changing the cardinality of the trunk.
        assert (shoot_baseline.is_root or flow.spans(shoot_baseline.base))

        # There are two ways the joints are generated:
        #
        # - when the shoot baseline is an axis of the trunk flow,
        #   in this case we join the terms using parallel joints on
        #   the common axes;
        # - otherwise, join the terms using a serial joint between
        #   the shoot baseline and its base.

        # Joints to attach the shoot to the trunk.
        joints = []
        # The backbone of the trunk term.
        backbone = flow.inflate()
        # The backbone of the shoot term.
        shoot_backbone = shoot.inflate()
        # Check if the shoot baseline is an axis of the trunk flow.
        if backbone.concludes(shoot_baseline):
            # In this case, we join the terms by all axes of the trunk
            # flow that are exported by the shoot term.
            # Find the first inflated axis of the trunk exported
            # by the shoot.
            axis = backbone
            while not shoot_backbone.concludes(axis):
                axis = axis.base
            # Now the axes between `axis` and `shoot_baseline` are common
            # axes of the trunk flow and the shoot term.  For each of them,
            # generate a parallel joint.  Note that we do not verify
            # (and, in general, it is not required) that these axes
            # are exported by the trunk term.  Apply `inject_joints()` on
            # the trunk term before using the joints to join the terms.
            axes = []
            while axis != shoot_baseline.base:
                # Skip non-expanding axes (but always include the baseline).
                if not axis.is_contracting or axis == shoot_baseline:
                    axes.append(axis)
                axis = axis.base
            # We prefer (for no particular reason) the joints to go
            # from shortest to longest axes.
            axes.reverse()
            for axis in axes:
                joints.extend(sew(axis))
        else:
            # When the shoot does not touch the trunk flow, we attach it
            # using a serial joint between the shoot baseline and its base.
            # Note that we do not verify (and it is not required) that
            # the trunk term exports the base flow.  Apply `inject_joints()`
            # on the trunk term to inject any necessary flows before
            # joining the terms using the joints.
            joints = tie(shoot_baseline)

            # We can try to optimize the joints when the base of the
            # shoot baseline is an ancestor of the trunk flow, but not
            # exported by the trunk term.  It this case, we prefer to
            # avoid adding an extra axis to the trunk term from below.

            # The axis that joins the shoot term to the trunk.
            origin = shoot_baseline.base
            # Check if the axis is a part of the trunk backbone, but
            # lies below the the trunk baseline.
            if (baseline.concludes(origin) and baseline != origin):
                # Find the direct descendant of `origin` along the trunk.
                axis = baseline
                while axis.base != origin:
                    axis = axis.base
                # Ties from the shoot term to the origin flow.
                shoot_joints = joints
                # Ties from the trunk term to the origin flow.
                trunk_joints = tie(axis)
                # Check if both set of ties share the same origin
                # expressions.
                if (len(trunk_joints) == len(shoot_joints) and
                        all(trunk_joint.lop == shoot_joint.lop
                            for trunk_joint, shoot_joint
                            in zip(trunk_joints, shoot_joints))):
                    # Generate a new set of ties by merging the shoot
                    # and trunk joints.
                    joints = [Joint(trunk_joint.rop, shoot_joint.rop)
                              for trunk_joint, shoot_joint
                              in zip(trunk_joints, shoot_joints)]

        # Return the generated joints.
        return joints

    def glue_terms(self, trunk_term, shoot_term):
        """
        Returns joints to attach the shoot term to the trunk term.

        `trunk_term` (:class:`htsql.core.tr.term.Term`)
            The left (trunk) operand of the join.

        `shoot_term` (:class:`htsql.core.tr.term.Term`)
            The right (shoot) operand of the join.

        Note that the trunk term may not export all the units necessary
        to generate join conditions.  Apply :meth:`inject_joints` on the
        trunk before using the joints to join the trunk and the shoot.
        """
        # Sanity check on the arguments.
        assert isinstance(trunk_term, Term)
        assert isinstance(shoot_term, Term)
        # Delegate to an auxiliary method.
        return self.glue_flows(trunk_term.flow, trunk_term.baseline,
                               shoot_term.flow, shoot_term.baseline)

    def inject_joints(self, term, joints):
        """
        Augments the term to ensure it can export all units required
        to generate join conditions.

        `term` (:class:`htsql.core.tr.term.Term`)
            The term to update.

            It is assumed that `term` was the argument `trunk_term` of
            :meth:`glue_terms` when the joints were generated.

        `joints` (a list of :class:`htsql.core.tr.term.Joint`)
            The joints to inject.

            It is assumed the ties were generated by :meth:`glue_terms`.
        """
        # Sanity check on the arguments.
        assert isinstance(term, Term)
        assert isinstance(joints, listof(Joint))

        codes = [lop for lop, rop in joints]
        return self.state.inject(term, codes)

    def join_terms(self, trunk_term, shoot_term, extra_routes):
        """
        Attaches a shoot term to a trunk term.

        The produced join term uses the flow and the routing
        table of the trunk term, but also includes the given
        extra routes.

        `trunk_term` (:class:`htsql.core.tr.term.Term`)
            The left (trunk) operand of the join.

        `shoot_term` (:class:`htsql.core.tr.term.Term`)
            The right (shoot) operand of the term.

            The shoot term must be singular relatively to the trunk term.

        `extra_routes` (a mapping from a unit/flow to a term tag)
            Any extra routes provided by the join.
        """
        # Sanity check on the arguments.
        assert isinstance(trunk_term, Term)
        assert isinstance(shoot_term, Term)
        assert trunk_term.flow.spans(shoot_term.flow)
        assert isinstance(extra_routes, dict)

        # Join conditions that glue the terms.
        joints = self.glue_terms(trunk_term, shoot_term)
        # Make sure the trunk term could export the joints (this
        # may change the baseline of the trunk term).
        trunk_term = self.inject_joints(trunk_term, joints)
        # Determine if we could use an inner join to attach the shoot
        # to the trunk.  We could do it if the inner join does not
        # decrease cardinality of the trunk.  It is so if the shoot flow
        # dominates a closest ancestor of the trunk flow that is spanned
        # by the shoot flow.
        is_left = True
        flow = trunk_term.flow
        while not shoot_term.flow.spans(flow):
            flow = flow.base
        is_left = (not shoot_term.flow.dominates(flow))
        is_right = False
        # Use the routing table of the trunk term, but also add
        # the given extra routes.
        routes = trunk_term.routes.copy()
        routes.update(extra_routes)
        # Generate and return a join term.
        return JoinTerm(self.state.tag(), trunk_term, shoot_term,
                        joints, is_left, is_right,
                        trunk_term.flow, trunk_term.baseline, routes)


class Compile(CompileBase):
    """
    Translates an expression node to a term node.

    This is an interface adapter; see subclasses for implementations.

    The :class:`Compile` adapter is implemented for two classes
    of expressions:

    - top-level expressions such as the whole query and the query segment,
      for which it builds respective top-level term nodes;

    - flows, for which the adapter builds a corresponding relational
      algebraic expression.

    After a term is built, it is typically augmented using the
    :class:`Inject` adapter to have it export any exprected units.

    The :class:`Compile` adapter has the following signature::

        Compile: (Expression, CompilingState) -> Term

    The adapter is polymorphic on the `Expression` argument.

    `expression` (:class:`htsql.core.tr.flow.Expression`)
        An expression node.

    `state` (:class:`CompilingState`)
        The current state of the compiling process.
    """

    def __init__(self, expression, state):
        assert isinstance(expression, Expression)
        assert isinstance(state, CompilingState)
        self.expression = expression
        self.state = state

    def __call__(self):
        # This should never be reachable; if we are here, it indicates
        # either a bug or an incomplete implementation.  Since normally it
        # cannot be triggered by a user, we don't bother with generating
        # a user-level HTSQL exception.
        raise NotImplementedError("the compile adapter is not implemented"
                                  " for a %r node" % self.expression)


class Inject(CompileBase):
    """
    Augments a term to make it capable of producing the given expression.

    This is an interface adapter; see subclasses for implementations.

    This adapter takes a term node and an expression (usually, a unit)
    and returns a new term (an augmentation of the given term) that is
    able to produce the given expression.

    The :class:`Inject` adapter has the following signature::

        Inject: (Expression, Term, CompilingState) -> Term

    The adapter is polymorphic on the `Expression` argument.

    `expression` (:class:`htsql.core.tr.flow.Expression`)
        An expression node to inject.

    `term` (:class:`htsql.core.tr.term.Term`)
        A term node to inject into.

    `state` (:class:`CompilingState`)
        The current state of the compiling process.
    """

    def __init__(self, expression, term, state):
        assert isinstance(expression, Expression)
        assert isinstance(term, Term)
        assert isinstance(state, CompilingState)
        self.expression = expression
        self.term = term
        self.state = state

    def __call__(self):
        # Same as with `Compile`, unless it's a bug or an incomplete
        # implementation, it should never be reachable.
        raise NotImplementedError("the inject adapter is not implemented"
                                  " for a %r node" % self.expression)


class CompileQuery(Compile):

    adapt(QueryExpr)

    def __call__(self):
        # Initialize the all state flows with a root scalar flow.
        self.state.set_root(RootFlow(None, self.expression.binding))
        # Compile the segment term.
        segment = None
        if self.expression.segment is not None:
            segment = self.state.compile(self.expression.segment)
        # Shut down the state flows.
        self.state.flush()
        # Construct a query term.
        return QueryTerm(segment, self.expression)


class CompileSegment(Compile):

    adapt(SegmentCode)

    def __call__(self):
        if not self.state.superflow.spans(self.expression.root):
            raise CompileError("a singular expression is expected",
                               self.expression.root.mark)
        chain = self.state.superflow_stack + \
                [self.state.superflow, self.expression.root,
                 self.expression.flow]
        # Get the ordering of the segment flow.  We must respect the ordering
        # of the parent segment.
        order = []
        for flow in chain:
            order.extend(arrange(flow))
        # List of expressions we need the term to export.
        codes = ([self.expression.code] +
                 [code for code, direction in order])
        idx = 0
        while idx+1 < len(chain):
            parent_flow = chain[idx]
            child_flow = chain[idx+1]
            is_native = False
            while child_flow is not None:
                if parent_flow.dominates(child_flow):
                    is_native = True
                    break
                child_flow = child_flow.base
            if is_native:
                del chain[idx]
            else:
                idx += 1
        # Construct a term corresponding to the segment flow.
        trunk_term = self.state.compile(chain[0], baseline=self.state.root)
        for flow in chain[1:]:
            shoot_term = self.compile_shoot(flow, trunk_term)
            joints = self.glue_terms(trunk_term, shoot_term)
            trunk_term = self.inject_joints(trunk_term, joints)
            routes = {}
            routes.update(trunk_term.routes)
            routes.update(shoot_term.routes)
            trunk_term = JoinTerm(self.state.tag(), trunk_term, shoot_term,
                                  joints, False, False,
                                  shoot_term.flow, self.state.root, routes)
        kid = trunk_term
        # Inject the expressions into the term.
        kid = self.state.inject(kid, codes)
        # The compiler does not guarantee that the produced term respects
        # the flow ordering, so it is our responsitibity to wrap the term
        # with an order node.
        if order:
            kid = OrderTerm(self.state.tag(), kid, order, None, None,
                            kid.flow, kid.baseline, kid.routes.copy())
        # Compile nested segments.
        subtrees = {}
        for segment in self.expression.code.segments:
            if segment in subtrees:
                continue
            self.state.push_superflow(self.expression.root)
            self.state.push_superflow(self.expression.flow)
            term = self.state.compile(segment)
            self.state.pop_superflow()
            self.state.pop_superflow()
            subtrees[segment] = term
        # Construct keys for segment merging.
        superkeys = [code for code, direction in arrange(self.state.superflow,
                                                         with_strong=False)]
        keys = [code for code, direction in arrange(self.expression.flow,
                                                    with_strong=False)]
        # Construct a segment term.
        return SegmentTerm(self.state.tag(), kid, self.expression,
                           superkeys, keys, subtrees,
                           kid.flow, kid.baseline, kid.routes.copy())


class CompileFlow(Compile):
    """
    Compile a term corresponding to a flow node.

    This is an abstract class; see subclasses for implementations.

    The general algorithm for compiling a term node for the given flow
    looks as follows:

    - compile a term for the base flow;
    - inject any necessary expressions;
    - build a new term node that represents the flow operation.

    When compiling a term for a flow node, the current `baseline` flow
    denotes the leftmost axis that the term should be able to export.
    The compiler may (but does not have to) omit any axes nested under
    the `baseline` axis.

    The generated term is not required to respect the ordering of the flow.

    Constructor arguments:

    `flow` (:class:`htsql.core.tr.flow.Flow`)
        A flow node.

    `state` (:class:`CompilingState`)
        The current state of the compiling process.

    Other attributes:

    `backbone` (:class:`htsql.core.tr.flow.Flow`)
        The inflation of the given flow.

    `baseline` (:class:`htsql.core.tr.flow.Flow`)
        An alias to `state.baseline`.
    """

    adapt(Flow)

    def __init__(self, flow, state):
        assert isinstance(flow, Flow)
        # The inflation of the flow.
        backbone = flow.inflate()
        # Check that the baseline flow is an axis of the given flow.
        assert flow.concludes(state.baseline)
        super(CompileFlow, self).__init__(flow, state)
        self.flow = flow
        self.state = state
        self.backbone = backbone
        # Extract commonly used state properties.
        self.baseline = state.baseline


class InjectFlow(Inject):

    adapt(Flow)

    def __init__(self, flow, term, state):
        assert isinstance(flow, Flow)
        # It is a bug if we get the `flow` plural for the `term` here.
        # It is a responsibility of `InjectUnit` to guard against unexpected
        # plural expressions and to issue an appropriate HTSQL error.
        assert term.flow.spans(flow)
        super(InjectFlow, self).__init__(flow, term, state)
        self.flow = flow
        self.term = term
        self.state = state

    def __call__(self):
        # Note that this function works for all flow classes universally.
        # We start with checking for and handling several special cases;
        # if none of them apply, we grow a shoot term for the given flow
        # and attach it to the main term.

        # Check if the flow is already exported.
        if all(unit in self.term.routes for unit in spread(self.flow)):
            # Not reachable since we only call `InjectFlow` from
            # `InjectColumn` and `InjectKernel`, and those already
            # verified that the flow is not exported.
            return self.term

        # Check that the flow does not contain any non-axial operations
        # of the term flow -- that's enforced by unmasking process.
        assert self.flow == self.flow.prune(self.term.flow)

        # A special case when the given flow is an ancestor of the term
        # flow.  The fact that the flow is not exported by the term means
        # that the term tree is optimized by cutting all axes below some
        # baseline.  Now we need to grow these axes back.
        if self.term.flow.concludes(self.flow):
            # Verify that the flow is not in the term.
            assert self.term.baseline.base.concludes(self.flow)

            # Here we compile a term corresponding to the flow and
            # attach it to the axis directly above it using a serial joint.

            # Compile a term for the missing axes.
            lkid = self.state.compile(self.term.baseline.base,
                                       baseline=self.flow)
            rkid = self.term

            # Join the terms using a serial joint.
            joints = tie(self.term.baseline)
            lkid = self.inject_joints(lkid, joints)
            # Since we are expanding the term baseline, the join is always
            # inner.
            is_left = False
            is_right = False
            # Re-use the old routing table, but add the new axis.
            routes = {}
            routes.update(lkid.routes)
            routes.update(rkid.routes)
            # Compile and return a join term.
            return JoinTerm(self.state.tag(), lkid, rkid, joints,
                            is_left, is_right,
                            rkid.flow, lkid.baseline, routes)

        # None of the special cases apply, so we use a general method:
        # - grow a shoot term for the given flow;
        # - attach the shoot to the main term.

        # Compile a shoot term for the flow.
        flow_term = self.compile_shoot(self.flow, self.term)
        # The routes to add.
        extra_routes = {}
        for unit in spread(self.flow):
            extra_routes[unit] = flow_term.routes[unit]
        # Join the shoot to the main term.
        return self.join_terms(self.term, flow_term, extra_routes)


class CompileScalar(CompileFlow):

    # The root flow is a special case of the scalar flow.
    adapt_many(ScalarFlow, RootFlow)

    def __call__(self):
        # If we are at the baseline (always the case for the root flow),
        # generate a scalar term.
        if self.flow == self.baseline:
            return ScalarTerm(self.state.tag(), self.flow, self.flow, {})
        # Otherwise, compile a term for the parent flow and reuse
        # it for the scalar flow.
        term = self.state.compile(self.flow.base)
        return WrapperTerm(self.state.tag(), term,
                           self.flow, term.baseline, term.routes)


class CompileTable(CompileFlow):

    # Used for both direct and fiber table flows.
    adapt(TableFlow)

    def __call__(self):
        # We start with identifying and handling special cases, where
        # we able to generate a more optimal, less compex term tree than
        # in the regular case.  If none of the special cases are applicable,
        # we use the generic algorithm.

        # The first special case: we are at the baseline flow.
        if self.flow == self.baseline:
            # Generate a single table term.
            tag = self.state.tag()
            # The routing table includes all the columns of the table.
            routes = {}
            for unit in spread(self.flow):
                routes[unit] = tag
            return TableTerm(tag, self.flow, self.baseline, routes)

        # Otherwise, we need a term corresponding to the parent flow.
        term = self.state.compile(self.flow.base)

        # The second special case, when the term of the parent flow could also
        # serve as a term for the flow itself.  It is possible if the
        # following two conditions are met:
        # - the term exports the inflation of the given flow (`backbone`),
        # - the given flow conforms (has the same cardinality as) its base.
        # This case usually corresponds to an HTSQL expression of the form:
        #   (A?p(B)).B,
        # where `B` is a singular, non-nullable link from `A` and `p(B)` is
        # a predicate expression on `B`.
        if (self.flow.conforms(term.flow) and
            all(unit in term.routes for unit in spread(self.backbone))):
            # We need to add the given flow to the routing table and
            # replace the term flow.
            routes = term.routes.copy()
            for unit in spread(self.flow):
                routes[unit] = routes[unit.clone(flow=self.backbone)]
            return WrapperTerm(self.state.tag(), term,
                               self.flow, term.baseline, routes)

        # Now the general case.  We take two terms:
        # - the term compiled for the parent flow
        # - and a table term corresponding to the flow table,
        # and join them using the tie between the flow and its parent.

        # This is the term for the flow base, we already generated it.
        lkid = term
        # This is a table term corresponding to the flow table.
        # Instead of generating it directly, we call `compile`
        # on the same flow, but with a different baseline, so that it
        # will hit the first special case and produce a table term.
        rkid = self.state.compile(self.backbone, baseline=self.backbone)
        # The connections between the flow to its base.
        joints = tie(self.flow)
        is_left = False
        is_right = False
        # We use the routing table of the base term with extra routes
        # corresponding to the given flow and its inflation which we
        # export from the table term.
        routes = lkid.routes.copy()
        routes = {}
        routes.update(lkid.routes)
        routes.update(rkid.routes)
        for unit in spread(self.flow):
            routes[unit] = routes[unit.clone(flow=self.backbone)]
        # Generate a join term node.
        return JoinTerm(self.state.tag(), lkid, rkid, joints,
                        is_left, is_right, self.flow, lkid.baseline, routes)


class CompileQuotient(CompileFlow):

    adapt(QuotientFlow)

    def __call__(self):
        # Normally, a quotient flow is represented by a seed term with
        # the baseline at the ground term.  If we can generate a term
        # with this shape, it is wrapped by a filter term to eliminate
        # `NULL` from the kernel and then by a projection term to
        # generate a proper quotient term.

        # However it may happen that the seed term has the baseline
        # shorter than the ground.  In this case, the term has irregular
        # parallel and serial ties and therefore cannot represent
        # the quotient axis.  To hide the irregular structure, we are
        # forced to generate a trunk term from the parent flow and
        # manually project and attach the seed term to the trunk term.

        # In addition, we may be asked to export some aggregates
        # over the complement flow.  We generate aggregate expressions
        # by pretending that the seed term actually represents
        # the complement flow and injecting the expressions into it.

        # Start with generating a term for the seed flow.

        # The ground flow is expected to be the baseline of the seed term.
        baseline = self.flow.ground
        # However, the ground may not be inflated, so we need to find
        # an inflated ancestor.
        while not baseline.is_inflated:
            baseline = baseline.base
        # The seed term.
        seed_term = self.state.compile(self.flow.seed, baseline=baseline)
        # Inject the kernel and filter out `NULL` kernel values.
        if self.flow.kernels:
            # Make sure the kernel expressions are exportable.
            seed_term = self.state.inject(seed_term, self.flow.kernels)
            # Generate filters:
            #   !is_null(kernel)&...
            filters = []
            for code in self.flow.kernels:
                filter = FormulaCode(IsNullSig(-1), coerce(BooleanDomain()),
                                     code.binding, op=code)
                filters.append(filter)
            if len(filters) == 1:
                [filter] = filters
            else:
                filter = FormulaCode(AndSig(), coerce(BooleanDomain()),
                                     self.flow.binding, ops=filters)
            # The final seed term.
            seed_term = FilterTerm(self.state.tag(), seed_term, filter,
                                   seed_term.flow, seed_term.baseline,
                                   seed_term.routes.copy())

        # Wrap the term to have a target for composite units.
        seed_term = WrapperTerm(self.state.tag(), seed_term,
                                seed_term.flow, seed_term.baseline,
                                seed_term.routes.copy())

        # Indicates that the seed term has the regular shape.
        is_regular = (seed_term.baseline == self.flow.ground)

        # Inject aggregates suggested by the rewriter.

        # List of injected aggregate expressions.
        aggregates = []
        # Clear out companions to avoid infinite recursion.
        quotient = self.backbone.clone(companions=[])
        # The plural space for the aggregates.
        complement = ComplementFlow(quotient, self.flow.binding)
        # We can only inject aggregates if the seed term has the regular shape.
        if self.flow.companions and is_regular:
            # We are going to disguise the seed term as a complement.
            # The routing table for the complement term.
            routes = {}
            for code in seed_term.routes:
                unit = CoveringUnit(code, complement, code.binding)
                routes[unit] = seed_term.tag
            for code in self.flow.kernels:
                unit = CoveringUnit(code, complement, code.binding)
                routes[unit] = seed_term.tag
            for unit in spread(self.flow.seed.inflate()):
                routes[unit.clone(flow=complement)] = seed_term.routes[unit]
            # Disguise the seed term as a complement term.
            complement_term = WrapperTerm(self.state.tag(), seed_term,
                                          complement, complement, routes)
            # Inject aggregate expressions.
            complement_term = self.state.inject(complement_term,
                                                self.flow.companions)
            # Abort if the shape of the term changed.
            if complement_term.baseline == complement:
                # Remember what we just injected.
                aggregates = self.flow.companions
                # Convert the complement term back to the seed term.
                # The routing table of the seed term will now have
                # extra aggregate expressions.
                routes = {}
                for code in aggregates:
                    for unit in code.units:
                        routes[unit] = complement_term.routes[unit]
                routes.update(seed_term.routes)
                # Back to the seed term.
                seed_term = WrapperTerm(self.state.tag(), complement_term,
                                        seed_term.flow, seed_term.baseline,
                                        routes)

        # Prepare for generating the quotient term.

        # The term for the parent flow (may remain `None` if the baseline
        # is at the quotient).
        trunk_term = None
        # The basis of the projection.
        basis = []
        # The units exported by the projection (against the inflated flow).
        units = []
        # The join conditions attaching the quotient term to the parent term.
        joints = []

        # Generate the trunk term and the join conditions.

        # Handle the regular case first.
        if is_regular:
            # Check if the term for the parent flow is necessary.
            if self.flow != self.baseline:
                # Generate the parent flow and the ties.
                trunk_term = self.state.compile(self.flow.base)
                joints = tie(self.flow)

        # The irregular case, the seed baseline is below the ground.
        else:
            # The trunk term is a must, even if the baseline is at
            # the current flow.  In that case, we need to lower the baseline.
            baseline = self.baseline
            if baseline == self.flow:
                baseline = baseline.base
            # Generate the trunk term.
            trunk_term = self.state.compile(self.flow.base, baseline=baseline)
            # Join conditions between the trunk and the seed terms.
            seed_joints = self.glue_terms(trunk_term, seed_term)
            # Convert the join conditions to joints between the trunk
            # and the projection terms.  Also prepopulate the basis
            # and the list of units.
            for joint in seed_joints:
                basis.append(joint.rop)
                unit = KernelUnit(joint.rop, self.backbone, joint.rop.binding)
                units.append(unit)
                joints.append(joint.clone(rop=unit))

        # Generate the the projection basis and a list of exported units.
        # Note that in the irregular case, those are already prepopulated
        # from the join conditions.
        # The units attaching the seed ground to the parent flow.
        for lop, rop in tie(self.flow.ground):
            basis.append(rop)
            unit = KernelUnit(rop, self.backbone, rop.binding)
            units.append(unit)
        # The kernel expressions.
        for code in self.flow.kernels:
            basis.append(code)
            unit = KernelUnit(code, self.backbone, code.binding)
            units.append(unit)
        # Injected complement aggregates (regular case only).
        for code in aggregates:
            unit = AggregateUnit(code, complement, self.backbone,
                                 code.binding)
            units.append(unit)

        # FIXME: incomplete; not reachable because we raise an error
        # on a scalar kernel.

        # When the kernel is scalar, to ensure proper conversion to SQL,
        # force `GROUP BY` to contain a reference from a subframe.  For
        # that, we create a permanent wrapper around the seed flow and
        # create a scalar unit pointing to that wrapper.  The unit
        # is added to the projection basis.
        if all(not code.units for code in self.flow.kernels):
            basis_code = LiteralCode(True, coerce(BooleanDomain()),
                                     self.flow.binding)
            basis_unit = ScalarUnit(basis_code, self.flow.seed,
                                    basis_code.binding)
            basis.append(basis_unit)
            routes = seed_term.routes.copy()
            routes[basis_unit] = seed_term.tag
            seed_term = PermanentTerm(self.state.tag(), seed_term,
                                      seed_term.flow, seed_term.baseline,
                                      routes)

        # Generate the projection term.
        tag = self.state.tag()
        # Convert the list of units to a routing table.
        routes = {}
        for unit in units:
            routes[unit] = tag
        # Generate a term node.
        term = ProjectionTerm(tag, seed_term, basis,
                              self.backbone, self.backbone, routes)

        # If there is no parent term, we are done.
        if trunk_term is None:
            return term

        # Otherwise, join the terms.
        lkid = self.inject_joints(trunk_term, joints)
        rkid = term
        # The joined routing table.
        routes = {}
        routes.update(lkid.routes)
        routes.update(rkid.routes)
        # Reparent exported units from the backbone to the original flow.
        for unit in units:
            routes[unit.clone(flow=self.flow)] = rkid.tag
        # Generate and return a join node.
        is_left = False
        is_right = False
        return JoinTerm(self.state.tag(), lkid, rkid, joints,
                        is_left, is_right, self.flow, lkid.baseline, routes)


class CompileComplement(CompileFlow):

    adapt(ComplementFlow)

    def __call__(self):
        # A complement term, just like a quotient term is represented
        # by a seed term with a baseline at the seed ground.  As opposed
        # to the quotient term, we don't have to filter out `NULL` kernel
        # values as this filter is enforced by the quotient anyway.

        # Since the quotient and the complement terms share the same
        # shape, we could reuse the complement term to export the respective
        # quotient flow.  In this case, we need to apply kernel filters.

        # As in the quotient case, the seed term may have an irregular
        # shape, that is, the term baseline lies below the seed ground.
        # In this case, we manually attach the seed term to the trunk.

        # The flow node may contain extra code objects -- `companions`,
        # which indicate that the generated term should export covering
        # units wrapping the companions.

        # Generate the seed term.

        # The baseline of the seed term is expected to be the seed ground flow.
        baseline = self.flow.ground
        # However it may be not inflated, in which case we find the closest
        # inflated axis.
        while not baseline.is_inflated:
            baseline = baseline.base
        # Create the seed term.
        seed_term = self.state.compile(self.flow.seed, baseline=baseline)
        # Make sure the seed term can export the quotient kernel and the
        # extra companion expressions.
        seed_term = self.state.inject(seed_term,
                                      self.flow.kernels + self.flow.companions)

        # Indicates whether the seed term has a regular shape.
        is_regular = (seed_term.baseline == self.flow.ground)

        # Indicates that the generated term can export the quotient flow:
        # - we cannot omit generating the parent term because the baseline
        #   is below the current flow or the seed term is irregular.
        # - there are no filters or other non-axial operations between
        #   the complement and its quotient;
        # - the quotient flow does not have to export any aggregates.
        # Note that the seed term may have an irregular shape.
        has_quotient = ((self.baseline != self.flow or not is_regular) and
                        isinstance(self.flow.base, QuotientFlow) and
                        not self.flow.base.companions)

        # If the term exports the quotient flow, we need to enforce the
        # condition: `!is_null(kernel)`.
        if has_quotient and self.flow.kernels:
            # Generate a filter around the seed term.
            filters = []
            for code in self.flow.kernels:
                filter = FormulaCode(IsNullSig(-1), coerce(BooleanDomain()),
                                     code.binding, op=code)
                filters.append(filter)
            if len(filters) == 1:
                [filter] = filters
            else:
                filter = FormulaCode(AndSig(), coerce(BooleanDomain()),
                                     self.flow.binding, ops=filters)
            seed_term = FilterTerm(self.state.tag(), seed_term, filter,
                                   seed_term.flow, seed_term.baseline,
                                   seed_term.routes.copy())

        # Wrap the term to have a target for covering units.
        seed_term = WrapperTerm(self.state.tag(), seed_term,
                                seed_term.flow, seed_term.baseline,
                                seed_term.routes.copy())

        # Prepare for generating the complement term.

        # The term for the parent (or grandparent if `has_quotient`) flow.
        # May remain unset if the baseline at the current or the parent flow.
        trunk_term = None
        # Flow units exported by the term.
        covering_units = []
        # Units from the parent quotient flow exported by the term.
        quotient_units = []
        # Join conditions attaching the term to the trunk.
        joints = []

        # Generate the trunk term if needed.

        # The trunk flow.
        axis = self.flow.base
        # Use the grandparent flow if the quotient is already included
        # in the complement flow.
        if has_quotient:
            axis = axis.base
        # Determine the baseline.
        baseline = self.baseline
        # If the baseline is above the trunk flow, we can avoid generating
        # the trunk term, but only if the seed term has the regular shape.
        # Otherwise, lower the baseline till it reaches the trunk flow.
        if not is_regular:
            while not axis.concludes(baseline):
                baseline = baseline.base
        # Generate the trunk term if needed.
        if axis.concludes(baseline):
            trunk_term = self.state.compile(axis, baseline=baseline)

        # Generate the links to the trunk.
        if trunk_term is not None:
            # Add custom joints for the irregular case.
            if not is_regular:
                seed_joints = self.glue_terms(trunk_term, seed_term)
                for joint in seed_joints:
                    unit = CoveringUnit(joint.rop, self.backbone,
                                        joint.rop.binding)
                    joints.append(joint.clone(rop=unit))
                    # Make sure the joint is exported by the complement term.
                    covering_units.append(unit)

            # Add regular joints: the serial joints from the complement
            # flow (or the parent flow if it is included).
            if has_quotient:
                joints += tie(self.flow.base)
            else:
                joints += tie(self.flow)

        # Populate units exported by the complement.

        # Add units from the parent quotient flow if needed.
        if has_quotient:
            quotient_backbone = self.flow.base.inflate()
            quotient_units = spread(quotient_backbone)

        # Wrap everything produced by the seed term.
        for code in seed_term.routes:
            unit = CoveringUnit(code, self.backbone, code.binding)
            covering_units.append(unit)
        # Ensure we export serial ties.
        for lop, rop in tie(self.flow.ground):
            unit = CoveringUnit(rop, self.backbone, rop.binding)
            covering_units.append(unit)
        # Export the kernel and any requested companion units.
        for code in self.flow.kernels + self.flow.companions:
            unit = CoveringUnit(code, self.backbone, code.binding)
            covering_units.append(unit)

        # Generate the routing table and the complement term.
        routes = {}
        # Export units from the quotient flow, if any.
        for unit in quotient_units:
            routes[unit] = seed_term.tag
        # Export complement units.
        for unit in covering_units:
            routes[unit] = seed_term.tag
        # Export native units.
        for unit in spread(self.flow.seed):
            routes[unit.clone(flow=self.backbone)] = seed_term.routes[unit]
        # The baseline for the complement term.
        baseline = self.backbone
        if has_quotient:
            baseline = baseline.base
        # The complement term.
        term = WrapperTerm(self.state.tag(), seed_term,
                           self.backbone, baseline, routes)

        # If there is no parental term, we are done.
        if trunk_term is None:
            return term

        # Attach the complement term to the trunk.
        lkid = self.inject_joints(trunk_term, joints)
        rkid = term
        # Merge the routing table.
        routes = {}
        routes.update(lkid.routes)
        routes.update(rkid.routes)
        # Now reparent the exported units to the given flow
        # (rather than the backbone).
        for unit in quotient_units:
            routes[unit.clone(flow=self.flow.base)] = seed_term.tag
        for unit in covering_units:
            routes[unit.clone(flow=self.flow)] = seed_term.tag
        for unit in spread(self.flow.seed):
            routes[unit.clone(flow=self.flow)] = seed_term.routes[unit]
        is_left = False
        is_right = False
        # Generate and return the join term node.
        return JoinTerm(self.state.tag(), lkid, rkid, joints,
                        is_left, is_right, self.flow, lkid.baseline, routes)


class CompileCovering(CompileFlow):

    # The implementation is shared by these three covering flows.
    adapt_many(MonikerFlow, ForkedFlow, LinkedFlow)

    def __call__(self):
        # Moniker, forked and linked flows are represented as a seed term
        # with the baseline at the seed ground.  The compilation processes
        # for these types of flows are almost identical.

        # If the seed term has an irregular shape, we must generate a term
        # for the parent flow and add custom joints between the seed
        # and the parent terms.  If the seed term is regular and the
        # baseline is at the current flow, we avoid generating a parent term.

        # The flow node may contain extra code objects -- `companions`,
        # which indicate that the generated term should export covering
        # units wrapping the companions.

        # Generate the seed term.

        # The baseline of the seed term is expected to be the seed ground flow.
        baseline = self.flow.ground
        # However it may be not inflated, in which case we find the closest
        # inflated axis.
        while not baseline.is_inflated:
            baseline = baseline.base
        # Create the seed term.
        seed_term = self.state.compile(self.flow.seed, baseline=baseline)
        # The seed term may need to export some extra expressions.
        codes = []
        # For the forked flow, it must export the kernel expressions.
        if isinstance(self.flow, ForkedFlow):
            codes += self.flow.kernels
        # For the linked flow, it must export the linking expressions.
        if isinstance(self.flow, LinkedFlow):
            codes += [rop for lop, rop in self.flow.images]
        # Any companion expressions must also be included.
        codes += self.flow.companions
        seed_term = self.state.inject(seed_term, codes)

        # Indicates whether the seed term has a regular shape.
        is_regular = (seed_term.baseline == self.flow.ground)

        # Wrap the term to have a target for covering units.
        seed_term = WrapperTerm(self.state.tag(), seed_term,
                                seed_term.flow, seed_term.baseline,
                                seed_term.routes.copy())

        # Generate the trunk term and join conditions (if needed).

        # The term for the parent flow.  May remain `None` if we already
        # reached the baseline.
        trunk_term = None
        # Join conditions attaching the term to the trunk.
        joints = []

        # The regular case: make the parent term only if the
        # baseline is below the given flow.
        if is_regular:
            if self.baseline != self.flow:
                trunk_term = self.state.compile(self.flow.base)
            # We need the joints to produce covering units, so generate
            # them even when we do not use them for joining.
            joints = tie(self.flow)

        # The irregular case: we must create the parent term
        # even if the baseline is above the parent flow.
        else:
            # Lower the baseline if needed.
            baseline = self.baseline
            if baseline == self.flow:
                baseline = baseline.base
            # Compile a term for the parent flow.
            trunk_term = self.state.compile(self.flow.base, baseline=baseline)
            # Generate custom joints.
            shoot_term = seed_term
            # For the forked flow, this is tricky as we can't join the trunk
            # to the seed term as usual -- we must leave the seed axis
            # free of joints.  Note that the seed baseline lies below
            # `ground.base` since the seed term is irregular.
            if isinstance(self.flow, ForkedFlow):
                seed_joints = self.glue_flows(trunk_term.flow,
                                              trunk_term.baseline,
                                              self.flow.ground.base,
                                              seed_term.baseline)
            # Otherwise, just attach the shoot term to the trunk term.
            else:
                seed_joints = self.glue_terms(trunk_term, shoot_term)
            for joint in seed_joints:
                unit = CoveringUnit(joint.rop, self.backbone,
                                    joint.rop.binding)
                joints.append(joint.clone(rop=unit))
            # Append regular joints.
            joints += tie(self.flow)

        # Populate units exported by the covering term.
        units = []

        # Wrap everything produced by the seed term.
        for code in seed_term.routes:
            unit = CoveringUnit(code, self.backbone, code.binding)
            units.append(unit)
        # Ensure we can satisfy the joints.
        for joint in joints:
            units.append(joint.rop)
        # Export any requested companion units.
        for code in self.flow.companions:
            unit = CoveringUnit(code, self.backbone, code.binding)
            units.append(unit)

        # Generate the routing table and the covering term.
        routes = {}
        # Export covering units.
        for unit in units:
            routes[unit] = seed_term.tag
        # Export native units.
        for unit in spread(self.flow.seed):
            routes[unit.clone(flow=self.backbone)] = seed_term.routes[unit]
        # The covering term.
        term = WrapperTerm(self.state.tag(), seed_term,
                           self.backbone, self.backbone, routes)

        # If there is no parental term, we are done.
        if trunk_term is None:
            return term

        # Attach the covering term to the trunk.
        lkid = self.inject_joints(trunk_term, joints)
        rkid = term
        # Merge the routing table.
        routes = {}
        routes.update(lkid.routes)
        routes.update(rkid.routes)
        # Reparent the exported units from the flow backbone to the flow itself.
        for unit in units:
            routes[unit.clone(flow=self.flow)] = seed_term.tag
        for unit in spread(self.flow.seed):
            routes[unit.clone(flow=self.flow)] = seed_term.routes[unit]
        is_left = False
        is_right = False
        # Join the terms.
        return JoinTerm(self.state.tag(), lkid, rkid, joints,
                        is_left, is_right, self.flow, lkid.baseline, routes)


class CompileFiltered(CompileFlow):

    adapt(FilteredFlow)

    def __call__(self):
        # The term corresponding to the parent flow.
        term = self.state.compile(self.flow.base)
        # Make sure the base term is able to produce the filter expression.
        kid = self.state.inject(term, [self.flow.filter])
        # Inherit the routing table from the base term, but add native
        # units of the given flow.
        routes = kid.routes.copy()
        for unit in spread(self.flow):
            routes[unit] = routes[unit.clone(flow=self.backbone)]
        # Generate a filter term node.
        return FilterTerm(self.state.tag(), kid, self.flow.filter,
                          self.flow, kid.baseline, routes)


class CompileOrdered(CompileFlow):

    adapt(OrderedFlow)

    def __call__(self):
        # An ordered flow has two functions:
        # - adding explicit row ordering;
        # - extracting a slice from the row set.
        # Note the first function could be ignored since the compiled terms
        # are not required to respect the ordering of the underlying flow.

        # When the order flow does not apply limit/offset, we could simply
        # reuse the base term.
        if self.flow.is_expanding:
            # Generate a term for the flow base.
            term = self.state.compile(self.flow.base)
            # Update its routing table to include the given flow and
            # return the node.
            routes = term.routes.copy()
            for unit in spread(self.flow):
                routes[unit] = routes[unit.clone(flow=self.backbone)]
            return WrapperTerm(self.state.tag(), term,
                               self.flow, term.baseline, routes)

        # Applying limit/offset requires special care.  Since slicing
        # relies on precise row numbering, the base term must produce
        # exactly the rows of the base.  Therefore we cannot use any
        # baseline or unmask non-axial operations.

        # Extract the flow ordering and make sure the base term is able
        # to produce the order expressions.
        order = arrange(self.flow)
        codes = [code for code, direction in order]
        kid = self.state.compile(self.flow.base,
                                  baseline=self.state.root)
        kid = self.state.inject(kid, codes)
        # Add the given flow to the routing table.
        routes = kid.routes.copy()
        for unit in spread(self.flow):
            routes[unit] = routes[unit.clone(flow=self.backbone)]
        # Generate an order term.
        return OrderTerm(self.state.tag(), kid, order,
                         self.flow.limit, self.flow.offset,
                         self.flow, kid.baseline, routes)


class InjectCode(Inject):

    adapt(Code)

    def __call__(self):
        # Inject all the units that compose the expression.
        return self.state.inject(self.term, self.expression.units)


class InjectUnit(Inject):

    adapt(Unit)

    def __init__(self, unit, term, state):
        assert isinstance(unit, Unit)
        super(InjectUnit, self).__init__(unit, term, state)
        self.unit = unit
        # Extract the unit attributes.
        self.flow = unit.flow

    def __call__(self):
        # Normally, this should never be reachable.  We raise an error here
        # to prevent an infinite recursion via `InjectCode` in cases when
        # `Inject` is not implemented for some unit type.
        raise NotImplementedError("the inject adapter is not implemented"
                                  " for a %r node" % self.unit)


class InjectColumn(Inject):

    adapt(ColumnUnit)

    def __call__(self):
        # To avoid an extra `inject()` call, check if the unit flow
        # is already exported by the term.
        if self.unit in self.term.routes:
            # Not reachable since already checked in `state.inject()`.
            return self.term
        # Verify that the unit is singular on the term flow.
        if not self.term.flow.spans(self.flow):
            raise CompileError("a singular expression is expected",
                               self.unit.mark)
        # Inject the unit flow into the term.
        return self.state.inject(self.term, [self.unit.flow])


class InjectScalar(Inject):

    adapt(ScalarUnit)

    def __call__(self):
        # Injects a batch of scalar units sharing the same flow.

        # To inject a scalar unit into a term, we need to do the following:
        # - compile a term for the unit flow;
        # - inject the unit into the unit term;
        # - attach the unit term to the main term.

        # If we compile a unit term for each unit individually, we may
        # end up with a lot of identical unit terms in the term tree.
        # To optimize the structure of the term tree, the rewriter
        # collects all scalar units sharing the same flow and groups
        # them together so that the compiler could reuse the same term
        # for the whole group.

        # Check if the unit is already exported by the term.
        if self.unit in self.term.routes:
            # Not reachable since already checked in `state.inject()`.
            return self.term

        # List of units to inject.  This includes the given unit itself
        # and the units suggested be injected together with it.
        units = [self.unit]
        for code in self.unit.companions:
            companion_unit = ScalarUnit(code, self.flow, code.binding)
            # This test rarely fails since injecting any of the companions
            # injects the whole group.
            if companion_unit not in self.term.routes:
                units.append(companion_unit)

        # Verify that the unit is singular relative to the term.
        if not self.term.flow.spans(self.flow):
            raise CompileError("a singular expression is expected",
                               self.unit.mark)
        # Extract the unit expressions.
        codes = [unit.code for unit in units]

        # Handle the special case when the unit flow is equal to the
        # term flow or dominates it.  In this case, we could inject
        # the units directly to the main term and avoid creating
        # a separate unit term.
        if self.flow.dominates(self.term.flow):
            # This is no longer reachable since unmasking removes
            # scalar units that dominate their mask flow.
            # Make sure the term could export all the units.
            term = self.state.inject(self.term, codes)
            # Add all the units to the routing table.  Note that we point
            # the units to the wrapper because the given term could be
            # terminal (i.e., a table) and SQL syntax does not permit
            # exporting arbitrary expressions from tables.
            tag = self.state.tag()
            routes = term.routes.copy()
            for unit in units:
                routes[unit] = tag
            # Wrap the term with the updated routing table.
            return WrapperTerm(tag, term, term.flow, term.baseline, routes)

        # The general case: compile a term for the unit flow.
        unit_term = self.compile_shoot(self.flow, self.term, codes)
        # SQL syntax does not permit us evaluating arbitrary
        # expressions in terminal terms, so we wrap such terms with
        # a no-op wrapper.
        if unit_term.is_nullary:
            unit_term = WrapperTerm(self.state.tag(), unit_term,
                                    unit_term.flow, unit_term.baseline,
                                    unit_term.routes.copy())
        # And join it to the main term.
        extra_routes = dict((unit, unit_term.tag) for unit in units)
        return self.join_terms(self.term, unit_term, extra_routes)


class InjectAggregate(Inject):

    adapt(AggregateUnit)

    def __init__(self, unit, term, state):
        super(InjectAggregate, self).__init__(unit, term, state)
        # Extract attributes of the unit.
        self.plural_flow = unit.plural_flow

    def __call__(self):
        # Injects a batch of aggregate units sharing the same plural
        # and unit flows.

        # To inject an aggregate unit into a term, we do the following:
        # - compile a term for the unit flow;
        # - compile a term for the plural flow relative to the unit term;
        # - inject the unit expression into the plural term;
        # - project plural term into the unit flow;
        # - attach the projected term to the unit term;
        # - attach the unit term to the main term.

        # When the unit flow coincides with the main term flow, we could
        # avoid compiling a separate unit term, and instead attach the
        # projected term directly to the main term.

        # Check if the unit is already exported by the term.
        if self.unit in self.term.routes:
            # Not reachable since already checked in `state.inject()`.
            return self.term

        # When we inject many aggregates to the main term individually,
        # we may end up with a lot of identical subtrees in the final
        # term tree.  Therefore, the rewritter collects aggregates
        # sharing the same plural and unit flows and groups them together
        # so that the compiler could reuse the same term subtree for
        # the whole group.

        # Get the list of units to inject.
        units = [self.unit]
        for code in self.unit.companions:
            companion_unit = AggregateUnit(code, self.plural_flow,
                                           self.flow, code.binding)
            # This test rarely fails since injecting any of the companions
            # injects the whole group.
            if companion_unit not in self.term.routes:
                units.append(companion_unit)

        # Verify that the units are singular relative to the term.
        if not self.term.flow.spans(self.flow):
            raise CompileError("a singular expression is expected",
                               self.unit.mark)
        # Extract the aggregate expressions.
        codes = [unit.code for unit in units]

        # Check if the unit can be attached directly to the trunk term.
        # It is possible only if the unit flow coincides with or dominates
        # the trunk flow or one of its ancestors.  In this case, we could
        # avoid compiling a separate unit term and instead attach the
        # projected term directly to the main term.
        is_native = False

        # The attachment point.
        unit_flow = self.term.flow
        # Iterate over all ancestors of the term flow till (if) we find
        # the attachment point.
        while  unit_flow is not None:
            if self.flow.dominates(unit_flow):
                is_native = True
                break
            unit_flow = unit_flow.base
        # The trunk term can serve as the unit term.
        if is_native:
            unit_term = self.term
            # Note that the attachment point may be below the term flow.
            unit_baseline = unit_term.baseline
        else:
            # Compile a separate term for the unit flow.
            unit_term = self.compile_shoot(self.flow, self.term)
            unit_flow = unit_term.flow
            unit_baseline = unit_term.baseline

        # Compile a term for the plural flow against the unit flow,
        # and inject all the aggregate expressions into it.
        plural_term = self.compile_shoot(self.plural_flow,
                                         unit_flow, codes)
        # Generate joints to attach the projected term to the unit term.
        # Note that we attaching not to the term itself, but to
        # the attaching point we determined before.  The attachment
        # point may lie below the term flow.
        unit_joints = self.glue_flows(unit_flow, unit_baseline,
                                      plural_term.flow, plural_term.baseline)
        # Make sure the unit term could export the join conditions.
        unit_term = self.inject_joints(unit_term, unit_joints)

        # Now we are going to project the plural term onto the unit
        # flow.  As the projection basis, we are using the joints
        # generated by `glue_terms()`.  The flow corresponding to
        # the projection term is a quotient with the kernel formed
        # from the projection basis.
        basis = [runit for lunit, runit in unit_joints]

        # Determine the flow of the projected term (not necessarily
        # accurate, but we don't care).
        # FIXME: should the kernel of the quotient be `basis`?
        projected_flow = QuotientFlow(self.flow.inflate(),
                                      self.plural_flow, [],
                                      self.expression.binding)
        # The routing table of the projected term and join conditions
        # connecting the projected term to the unit term.
        tag = self.state.tag()
        joints = []
        routes = {}
        for joint in unit_joints:
            rop = KernelUnit(joint.rop, projected_flow, joint.rop.binding)
            routes[rop] = tag
            joints.append(joint.clone(rop=rop))

        # The term that computes aggregate expressions.
        projected_term = ProjectionTerm(tag, plural_term, basis,
                                        projected_flow, projected_flow,
                                        routes)
        # Attach the projected term to the unit term, add extra entries
        # to the routing table for each of the unit in the collection.
        is_left = (not projected_flow.dominates(unit_term.flow))
        is_right = False
        # Use the routing table of the trunk term, but also add
        # the given extra routes.
        routes = unit_term.routes.copy()
        for unit in units:
            routes[unit] = projected_term.tag
        # Generate and return a join term.
        unit_term = JoinTerm(self.state.tag(), unit_term, projected_term,
                             joints, is_left, is_right,
                             unit_term.flow, unit_term.baseline, routes)
        # For native units, we are done since we use the main term as
        # the unit term.  Note: currently this condition always holds.
        if is_native:
            return unit_term
        # Otherwise, attach the unit term to the main term.
        extra_routes = dict((unit, projected_term.tag) for unit in units)
        return self.join_terms(self.term, unit_term, extra_routes)


class InjectCorrelated(Inject):

    adapt(CorrelatedUnit)

    def __call__(self):
        # In the term tree, correlated subqueries are represented using
        # a pair of correlation and embedding term nodes.  A correlation
        # term connects its operand to an external *link* term.  An embedding
        # term implants the correlation term into the term tree.

        # Check if the unit is already exported by the term.
        if self.unit in self.term.routes:
            # Not reachable since already checked in `state.inject()`.
            return self.term
        # Verify that the unit is singular on the term flow.
        if not self.term.flow.spans(self.flow):
            # This is not reachable: the error is already reported by
            # the wrapping scalar unit.
            raise CompileError("a singular expression is expected",
                               self.unit.mark)

        # The general chain of operations is as follows:
        #   - compile a term for the unit flow;
        #   - inject the unit into the unit term;
        #   - attach the unit term to the main term.
        # However, when the unit flow coincides with the term flow,
        # it could be reduced to:
        #   - inject the unit directly into the main term.
        # We say that the unit is *native* to the term if the unit
        # flow coincides with the term flow (or dominates over it).

        # Note that currently the latter is always the case because
        # all correlated units are wrapped with a scalar unit sharing
        # the same unit flow.

        # Check if the unit is native to the term.
        is_native = self.flow.dominates(self.term.flow)
        if is_native:
            # If so, we are going to inject the unit directly into the term.
            unit_term = self.term
        else:
            # Otherwise, compile a separate term for the unit flow.
            # Note: not reachable as currently correlated units are always
            # wrapped by a scalar unit with the same base flow.
            unit_term = self.compile_shoot(self.flow, self.term)

        # Compile a term for the correlated subquery.
        plural_term = self.compile_shoot(self.unit.plural_flow,
                                         unit_term, [self.unit.code])
        # The ties connecting the correlated subquery to the main query.
        joints = self.glue_terms(unit_term, plural_term)
        # Make sure that the unit term could export tie conditions.
        unit_term = self.inject_joints(unit_term, joints)
        # Connect the plural term to the unit term.
        plural_term = CorrelationTerm(self.state.tag(), plural_term,
                                      unit_term, joints, plural_term.flow,
                                      plural_term.baseline, plural_term.routes)
        # Implant the correlation term into the term tree.
        routes = unit_term.routes.copy()
        routes[self.unit] = plural_term.tag
        unit_term = EmbeddingTerm(self.state.tag(), unit_term, plural_term,
                                  unit_term.flow, unit_term.baseline, routes)
        # If we attached the unit directly to the main term, we are done.
        if is_native:
            return unit_term
        # Otherwise, we need to attach the unit term to the main term.
        # Not reachable.
        extra_routes = { self.unit: plural_term.tag }
        return self.join_terms(self.term, unit_term, extra_routes)


class InjectKernel(Inject):

    adapt(KernelUnit)

    def __call__(self):
        # Check if the unit is already exported by the term.
        if self.unit in self.term.routes:
            # Not reachable since already checked in `state.inject()`.
            return self.term
        # Check if the unit is singular against the term flow.
        if not self.term.flow.spans(self.flow):
            raise CompileError("a singular expression is expected",
                               self.unit.mark)
        # Inject the quotient space -- this should automatically
        # provide the unit.
        term = self.state.inject(self.term, [self.flow])
        # Verify that the unit is injected.
        assert self.unit in term.routes
        # Return an augmented term.
        return term


class InjectCovering(Inject):

    adapt(CoveringUnit)

    def __call__(self):
        # Check if the unit is already exported by the term.
        if self.unit in self.term.routes:
            # Not reachable since already checked in `state.inject()`.
            return self.term
        # Ensure that the unit is singular against the term flow.
        if not self.term.flow.spans(self.flow):
            # Not reachable since covering units are never generated
            # by the user directly, only by the compiler.
            raise CompileError("a singular expression is expected",
                               self.unit.mark)
        # FIXME: the rewritter should optimize the flow graph
        # so that this code is not reachable.
        # Add a hint to the flow node to ask the compiler generate
        # the unit when compiling the flow term.
        companions = self.flow.companions+[self.unit.code]
        flow = self.flow.clone(companions=companions)

        # In general, we can't inject the flow into the term
        # directly as we could hit the special case when the
        # flow is an ancestor of the term flow.  Instead, we
        # inject the flow manually.

        # Compile a shoot term for the flow.
        flow_term = self.compile_shoot(flow, self.term)
        # Add the route to the new unit.
        extra_routes = {}
        extra_routes[self.unit] = flow_term.routes[self.unit]
        # Join the shoot to the main term.
        term = self.join_terms(self.term, flow_term, extra_routes)
        # Verify that the unit is injected.
        assert self.unit in term.routes
        # Return the augmented term.
        return term


def compile(expression, state=None, baseline=None):
    """
    Compiles a new term node for the given expression.

    Returns a :class:`htsql.core.tr.term.Term` instance.

    `expression` (:class:`htsql.core.tr.flow.Expression`)
        An expression node.

    `state` (:class:`CompilingState` or ``None``)
        The compiling state to use.  If not set, a new compiling state
        is instantiated.

    `baseline` (:class:`htsql.core.tr.flow.Flow` or ``None``)
        The baseline flow.  Specifies an axis that the compiled
        term must export.  If not set, the current baseline flow of
        the state is used.
    """
    # Instantiate a new compiling state if not given one.
    if state is None:
        state = CompilingState()
    # If passed, assign new baseline and mask flows.
    if baseline is not None:
        state.push_baseline(baseline)
    # Realize and apply the `Compile` adapter.
    term = Compile.__invoke__(expression, state)
    # Restore old baseline and mask flows.
    if baseline is not None:
        state.pop_baseline()
    # Return the compiled term.
    return term
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.