Source

htsql / src / htsql / core / tr / reduce.py

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


from ..adapter import Adapter, adapt
from ..domain import BooleanDomain, TextDomain, IntegerDomain
from .coerce import coerce
from .stitch import arrange
from .term import PermanentTerm
from .frame import (Clause, Frame, ScalarFrame, BranchFrame, NestedFrame,
        SegmentFrame, Phrase, LiteralPhrase, NullPhrase, TruePhrase,
        FalsePhrase, CastPhrase, FormulaPhrase, ExportPhrase, ReferencePhrase,
        Anchor, LeadingAnchor)
from .signature import (Signature, isformula, IsEqualSig, IsTotallyEqualSig,
        IsInSig, IsNullSig, IfNullSig, NullIfSig, AndSig, OrSig, NotSig,
        SortDirectionSig, FromPredicateSig, ToPredicateSig)


class ReducingState(object):
    """
    Encapsulates the state of the reducing and collapsing processes.

    State attributes:

    `substitutes` (a mapping `tag -> list of phrases`)
        A mapping containing the `SELECT` clause of collapsed frames.
    """

    def __init__(self):
        # `SELECT` clauses of collapsed frames by the frame tag.
        self.substitutes = {}

    def reduce(self, clause):
        """
        Reduces (simplifies) a SQL clause.

        Returns an equivalent (possibly the same) clause.

        `clause` (:class:`htsql.core.tr.frame.Clause`)
            The clause to simplify.
        """
        # Realize and apply the `Reduce` adapter.
        return Reduce.__invoke__(clause, self)

    def collapse(self, frame):
        """
        Collapses a frame.

        Returns an equivalent (possibly the same) frame.

        Note that the generated frame may contain some clauses that refer
        to subframes which no longer exist.  To fix broken references,
        apply :meth:`reduce` to the returned frame.

        `frame` (:class:`htsql.core.tr.frame.Frame`)
            The frame to collapse.
        """
        # Realize and apply the `Collapse` adapter.
        return Collapse.__invoke__(frame, self)

    def interlink(self, frame):
        return Interlink.__invoke__(frame, self)

    def to_predicate(self, phrase):
        phrase = FormulaPhrase(ToPredicateSig(), phrase.domain,
                               phrase.is_nullable, phrase.expression,
                               op=phrase)
        return self.reduce(phrase)

    def from_predicate(self, phrase):
        phrase = FormulaPhrase(FromPredicateSig(), phrase.domain,
                               phrase.is_nullable, phrase.expression,
                               op=phrase)
        return self.reduce(phrase)


class Reduce(Adapter):
    """
    Reduces (simplifies) a SQL clause.

    This is an interface adapter; see subclasses for implementations.

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

        Reduce: (Clause, ReducingState) -> Clause

    The adapter is polymorphic on the `Claim` argument.

    `clause` (:class:`htsql.core.tr.frame.Clause`)
        The clause node.

    `state` (:class:`ReducingState`)
        The current state of the reducing process.
    """

    adapt(Clause)

    def __init__(self, clause, state):
        assert isinstance(clause, Clause)
        assert isinstance(state, ReducingState)
        self.clause = clause
        self.state = state

    def __call__(self):
        # Implement in subclasses.
        raise NotImplementedError("the reduce adapter is not implemented"
                                  " for a %r node" % self.clause)


class ReduceFrame(Reduce):
    """
    Reduces a SQL frame.

    The adapter collapses the subframes of the frame node and simplifies
    its clauses.  This is an abstract adapter; see subclasses for concrete
    implementations.

    `frame` (:class:`htsql.core.tr.frame.Frame`)
        The frame node.

    `state` (:class:`ReducingState`)
        The current state of the reducing process.
    """

    adapt(Frame)

    def __init__(self, frame, state):
        super(ReduceFrame, self).__init__(frame, state)
        self.frame = frame

    def __call__(self):
        # Return the frame itself; this default implementation is used
        # only by a table frame.  The scalar, the nested and the segment
        # frames have more elaborate implementations.
        return self.frame


class ReduceScalar(ReduceFrame):
    """
    Reduces a scalar frame.
    """

    adapt(ScalarFrame)

    def __call__(self):
        # The databases we currently support have no notion of a `DUAL`
        # table, so we replace scalar frame with an empty `SELECT`
        # statement.  Databases with a native `DUAL` table may need
        # to override this implementation.
        select = [TruePhrase(self.frame.expression)]
        return NestedFrame(include=[], embed=[], select=select,
                           where=None, group=[], having=None,
                           order=[], limit=None, offset=None,
                           term=self.frame.term)


class ReduceBranch(ReduceFrame):
    """
    Reduces a top-level or a nested ``SELECT`` statement.
    """

    adapt(BranchFrame)

    def reduce_include(self):
        # Reduce the anchors of the subframes.  This will also
        # collapse and reduce the subframes themselves.
        return [self.state.reduce(anchor)
                for anchor in self.frame.include]

    def reduce_embed(self):
        # Collapse and reduce the embedded subframes.
        return [self.state.interlink(
                self.state.reduce(
                self.state.collapse(frame)))
                for frame in self.frame.embed]

    def reduce_select(self):
        # Reduce the `SELECT` clause.
        return [self.state.reduce(phrase)
                for phrase in self.frame.select]

    def reduce_where(self):
        # Reduce the `WHERE` clause.
        if self.frame.where is None:
            return None
        where = self.state.reduce(self.frame.where)
        # Eliminate a `WHERE TRUE` clause.
        if isinstance(where, TruePhrase):
            where = None
        return where

    def reduce_group(self):
        # Reduce the `GROUP BY` clause.
        # Here we reduce all the phrases in the `GROUP BY` clause and
        # also eliminate duplicates and literals.  As a result of the latter,
        # we may produce an empty `GROUP BY` clause (for instance, for scalar
        # projections), which may confuse the frame collapser or even
        # change the semantics of the `SELECT` statement.  Because of that,
        # `collapse()` should never be applied after `reduce()`.
        group = []
        duplicates = set()
        for phrase in self.frame.group:
            phrase = self.state.reduce(phrase)
            if isinstance(phrase, LiteralPhrase):
                continue
            if phrase in duplicates:
                continue
            group.append(phrase)
            duplicates.add(phrase)
        return group

    def reduce_having(self):
        # Reduce the `HAVING` clause.
        if self.frame.having is None:
            return None
        having = self.state.reduce(self.frame.having)
        # Eliminate a `HAVING TRUE` clause.
        if isinstance(having, TruePhrase):
            having = None
        return having

    def reduce_order(self):
        # Reduce the `ORDER BY` clause.
        # Here we reduce all the phrases in the `ORDER BY` clause and
        # also eliminate duplicates and literals.
        order = []
        duplicates = set()
        for phrase in self.frame.order:
            phrase = self.state.reduce(phrase)
            if isformula(phrase, SortDirectionSig):
                if isinstance(phrase.base, LiteralPhrase):
                    continue
                if phrase.base in duplicates:
                    continue
            order.append(phrase)
            duplicates.add(phrase.base)
        return order

    def __call__(self):
        # Reduce a `SELECT` statement.

        # Reduce and collapse the subframes in the `FROM` clause.
        include = self.reduce_include()
        # Reduce and collapse the embedded subframes.
        embed = self.reduce_embed()
        # Reduce the `SELECT`, `WHERE`, `GROUP BY`, `HAVING`, and
        # `ORDER BY` clauses.
        select = self.reduce_select()
        where = self.reduce_where()
        group = self.reduce_group()
        having = self.reduce_having()
        order = self.reduce_order()
        # Return a frame with reduced clauses.
        return self.frame.clone(include=include, embed=embed,
                                select=select, where=where,
                                group=group, having=having,
                                order=order)


class ReduceSegment(ReduceBranch):

    adapt(SegmentFrame)

    def __call__(self):
        frame = super(ReduceSegment, self).__call__()
        if not frame.dependents:
            return frame
        dependents = []
        for subframe in frame.dependents:
            subframe = self.state.collapse(subframe)
            subframe = self.state.reduce(subframe)
            subframe = self.state.interlink(subframe)
            dependents.append(subframe)
        return frame.clone(dependents=dependents)


class Collapse(Adapter):
    """
    Collapses nested subframes of the given frame.

    Returns an equivalent (possibly the same) frame.

    This is an auxiliary adapter used for flattening the frame structure.
    Using this adapter may remove some frames from the frame tree and thus
    invalidate any references to these frames.  Apply the :class:`Reduce`
    adapter to fix the references.

    `frame` (:class:`htsql.core.tr.frame.Frame`)
        The frame node.

    `state` (:class:`ReducingState`)
        The current state of the reducing process.
    """

    adapt(Frame)

    def __init__(self, frame, state):
        assert isinstance(frame, Frame)
        assert isinstance(state, ReducingState)
        self.frame = frame
        self.state = state

    def __call__(self):
        # The default implementation does nothing; used by leaf frames.
        return self.frame


class CollapseBranch(Collapse):
    """
    Collapses a branch frame.
    """

    adapt(BranchFrame)

    def __call__(self):
        # Here we attempt to dismantle the first subframe in the `FROM`
        # clause and include its content to the outer frame.  We continue
        # this process until no further reductions could be done.
        # We must be especially careful to not change the semantics
        # of the query!
        # FIXME: we do not attempt to absorb the content of any other
        # subframe but the first one.  It may be even trickier because
        # we need to carry over the `JOIN` condition.  There are some
        # cases when we may want to absorb the second subframe
        # (see a special case in `InjectSpace` when we grow a missing
        # axis).

        # No subframes in the `FROM` clause -- nothing to do.
        if not self.frame.include:
            return self.frame

        # The first subframe; let us try to eliminate it.
        head = self.frame.include[0].frame
        # The rest of the `FROM` clause.
        tail = self.frame.include[1:]

        # A frame corresponding to a permanent term is never collapsed down.
        if isinstance(head.term, PermanentTerm):
            return self.frame

        # Any subframe, including the head frame, is one of theses:
        # - a scalar frame, we could just discard it;
        # - a table frame, there is nothing we can do;
        # - a nested `SELECT` frame, if possible, include the
        #   content of the subframe into the outer frame.

        # First, check if the head frame is scalar.
        if head.is_scalar:
            # We could only discard the head subframe if the subframe
            # next to it has no `JOIN` condition (or the `FROM` clause
            # contains a single subframe).
            # FIXME: Some databases (i.e., Oracle) forbid an empty
            # `FROM` clause.
            if tail and not tail[0].is_cross:
                return self.frame
            # Indicate that the first anchor in the tail is now
            # a leading anchor.
            if tail:
                tail[0] = tail[0].clone_to(LeadingAnchor)
            # Make a new frame with a reduced `FROM` clause.
            frame = self.frame.clone(include=tail)
            # Try to further collapse the frame.
            return self.state.collapse(frame)

        # Now, only two possibilities are left: the head frame is either
        # a nested frame or a table frame.  If it is a table frame,
        # we are done.
        if not head.is_nested:
            return self.frame

        # Here goes a long list of checks to ensure we could merge the
        # content of the head subframe without breaking the query.

        # If the head subframe by itself has no subframes, the subframe
        # next to the head leads the `FROM` clause.  It is only valid
        # if the subframe has no `JOIN` condition.
        if not head.include:
            if tail and not tail[0].is_cross:
                return self.frame

        # We cannot safely collapse the head subframe if some other
        # subframe is attached using a `RIGHT OUTER` join.  Currently,
        # the compiler never generates right joins, so this check
        # is effectively no-op.
        if any(anchor.is_right for anchor in tail):
            return self.frame

        # A tricky case, the head subframe contains a `GROUP BY` clause.
        # We collapse frames like this only when they correspond to a
        # scalar projection, like in `/count(table)`.
        if head.group:
            # Verify that the kernel of the projection contains only
            # literal phrases, and thus will be eliminated by the reducer
            # (technically, it should contain a single `TRUE` literal,
            # but checking for arbitrary literals does not hurt).
            #if not all(isinstance(phrase, LiteralPhrase)
            #           for phrase in head.group):
            #    return self.frame

            # We only collapse a projection subframe if it is the only
            # subframe in the `FROM` clause.
            if tail:
                return self.frame
            # Ensure that the outer frame has no projection clauses, i.e.,
            # `GROUP BY` and `HAVING`.
            # The outer frame could still have `WHERE` clause (to be
            # moved to `HAVING`), as well as `ORDER BY`, `LIMIT`, and `OFFSET`.
            if self.frame.group or self.frame.having is not None:
                return self.frame
            # Ensure that the subframe has no `HAVING` clause.
            # The subframe could still have `WHERE`, `ORDER BY`, `LIMIT`,
            # and `OFFSET` clauses.
            if head.having is not None:
                return self.frame
            # If the outer frame has a `WHERE` clause, make sure the
            # `GROUP BY` clause of the subframe is not trivial since
            # not every database engine can handle `HAVING` without
            # `GROUP BY` (PostgreSQL can, SQLite cannot).
            if (self.frame.where is not None and
                all(isinstance(phrase, LiteralPhrase)
                               for phrase in head.group)):
                return self.frame

        # If we reached this point, the `HAVING` clause of the head subframe
        # must be empty.  This check is no-op though since we never generate
        # `HAVING` in the first place.
        assert head.having is None

        # Another tricky case: the subframe has a `LIMIT` or an `OFFSET`
        # clause.
        if not (head.limit is None and
                head.offset is None):
            # Now we need to answer the question: could we safely merge
            # the head frame to the outer frame.  It is only safe when
            # both the outer and the inner frames produce the same
            # number of rows.  However we cannot deduce that from the
            # frame structure only, so we analyze the spaces from which
            # the frames were compiled.
            # The inner and the outer frames would produce the same number
            # of rows if they are compiled from the same space, or, at
            # least, the spaces they are compiled from conform to each
            # other.  We also verify that their baseline spaces are equal,
            # but this is a redundant check -- precense of a non-trivial
            # `LIMIT` or `OFFSET` implies that the baseline is the scalar
            # space (see `CompileOrdered` in `htsql.core.tr.compile`).
            # Other than that, we also need to check that the `ORDER BY`
            # clauses of the inner and outer frames coincide (if they
            # both are non-empty).  We cannot compare the clauses directly
            # since they contain different export references, but we
            # can compare the ordering of the underlying spaces.
            if not (head.space.conforms(self.frame.space) and
                    head.baseline == self.frame.baseline and
                    arrange(head.space) == arrange(self.frame.space)):
                # Another safe case is when the outer frame contains
                # no clauses that may change the cardinality of the inner
                # frame, including no other `FROM` subframes.
                if not (self.frame.where is None and
                        not self.frame.group and
                        self.frame.having is None and
                        not self.frame.order and
                        self.frame.limit is None and
                        self.frame.offset is None and
                        not tail):
                    return self.frame
            # Since the inner and the outer spaces conform to each other,
            # the outer frame cannot contain non-trivial `LIMIT` and `OFFSET`
            # clauses.
            assert self.frame.limit is None and self.frame.offset is None

        # All checks passed, now we merge the head subframe to the outer
        # frame.

        # Merge the `FROM` clause of the head with the rest of the `FROM`
        # clause of the frame.
        if not head.include and tail:
            tail[0] = tail[0].clone_to(LeadingAnchor)
        include = head.include+tail

        # Merge the embedded subframes.
        embed = head.embed+self.frame.embed

        # Now that we merged the head subframe, all references to it
        # are broken and have to be replaced with the referenced phrases.
        # It is done by `reduce()`, but here we need to save the referenced
        # phrases.
        assert head.tag not in self.state.substitutes
        self.state.substitutes[head.tag] = head.select

        # Merge the `WHERE` clauses; both the inner and the outer `WHERE`
        # clauses could be non-empty, in which case we join them with `AND`.
        where = None
        if not head.group:
            where = self.frame.where
        if head.where is not None:
            if where is None:
                where = head.where
            else:
                is_nullable = (where.is_nullable or head.where.is_nullable)
                where = FormulaPhrase(AndSig(), coerce(BooleanDomain()),
                                      is_nullable, where.expression,
                                      ops=[where, head.where])

        # Pull the `GROUP BY` clause.
        group = self.frame.group
        if head.group:
            assert not group
            group = head.group

        # If the subframe contains a `GROUP BY` clause, move the `WHERE`
        # clause of the outer frame to `HAVING`.
        having = self.frame.having
        if head.group:
            assert having is None
            having = self.frame.where

        # Merge the `ORDER BY` clauses.  There are several possibilities:
        # - the inner `ORDER BY` is empty;
        # - the outer `ORDER BY` is empty;
        # - both the inner and the outer `ORDER BY` clauses are non-empty;
        #   in this case, they must be identical (but not necessarily equal
        #   by-value).
        assert (not head.order or not self.frame.order or
                len(head.order) == len(self.frame.order))
        order = head.order
        if self.frame.order:
            order = self.frame.order

        # Merge the `LIMIT` and the `OFFSET` clauses.  Here either the
        # inner `LIMIT/OFFSET` or the outer `LIMIT/OFFSET` must be empty.
        assert ((head.limit is None and head.offset is None) or
                (self.frame.limit is None and self.frame.offset is None))
        limit = head.limit
        if self.frame.limit is not None:
            limit = self.frame.limit
        offset = head.offset
        if self.frame.offset is not None:
            offset = self.frame.offset

        # Update the frame node.
        frame = self.frame.clone(include=include, embed=embed, where=where,
                                 group=group, having=having,
                                 order=order, limit=limit, offset=offset)
        # Try to collapse the frame once again.
        return self.state.collapse(frame)


class Interlink(Adapter):

    adapt(Frame)

    def __init__(self, frame, state):
        assert isinstance(frame, Frame)
        assert isinstance(state, ReducingState)
        self.frame = frame
        self.state = state

    def __call__(self):
        return self.frame


class InterlinkBranch(Interlink):

    adapt(BranchFrame)

    def interlink_group(self):
        group = []
        for index, phrase in enumerate(self.frame.group):
            if phrase in self.frame.select:
                position = self.frame.select.index(phrase)+1
                phrase = LiteralPhrase(position, coerce(IntegerDomain()),
                                       phrase.expression)
            group.append(phrase)
        return group

    def interlink_order(self):
        order = []
        for index, phrase in enumerate(self.frame.order):
            if isformula(phrase, SortDirectionSig):
                if phrase.base in self.frame.select:
                    position = self.frame.select.index(phrase.base)+1
                    base = LiteralPhrase(position, coerce(IntegerDomain()),
                                         phrase.base.expression)
                    phrase = phrase.clone(base=base)
            order.append(phrase)
        return order

    def __call__(self):
        group = self.interlink_group()
        order = self.interlink_order()
        return self.frame.clone(group=group, order=order)


class ReduceAnchor(Reduce):
    """
    Reduces a ``JOIN`` clause.
    """

    adapt(Anchor)

    def __call__(self):
        # Collapse and reduce (in that order!) the subframe.
        frame = self.state.interlink(
                self.state.reduce(
                self.state.collapse(self.clause.frame)))
        # Reduce the join condition.
        condition = (self.state.reduce(self.clause.condition)
                     if self.clause.condition is not None else None)
        # Update the anchor.
        return self.clause.clone(frame=frame, condition=condition)


class ReducePhrase(Reduce):
    """
    Reduces a SQL phrase.
    """

    adapt(Phrase)

    # Note that we do not provide a default no-op implementation: every
    # non-leaf phrase node must at least apply `reduce()` to its subnodes
    # in order to fix broken references.

    def __init__(self, phrase, state):
        super(ReducePhrase, self).__init__(phrase, state)
        self.phrase = phrase


class ReduceLiteral(Reduce):
    """
    Reduces a literal phrase.
    """

    adapt(LiteralPhrase)

    def __call__(self):
        # We cannot really simplify a literal value; instead, we encode
        # some common literals: `NULL`, `TRUE`, `FALSE` so that they
        # could be recognized with a single `isinstance()` check.
        if self.phrase.value is None:
            return NullPhrase(self.phrase.domain, self.phrase.expression)
        if isinstance(self.phrase.domain, BooleanDomain):
            if self.phrase.value is True:
                return TruePhrase(self.phrase.expression)
            if self.phrase.value is False:
                return FalsePhrase(self.phrase.expression)
        return self.phrase


class ReduceCast(Reduce):
    """
    Reduces a ``CAST`` operator.
    """

    adapt(CastPhrase)

    def __call__(self):
        # Reduce the operand of the cast.  We do not specialize
        # on the domains here because we assume that any domain
        # specific conversion is already done by the encoder.
        return self.phrase.clone(base=self.state.reduce(self.phrase.base))


class ReduceFormula(Reduce):
    """
    Reduces a formula node.

    Reducing a formula is specific to the formula signature and is
    implemented by the :class:`ReduceBySignature` adapter.
    """

    adapt(FormulaPhrase)

    def __call__(self):
        # Delegate the reduction to the `ReduceBySignature` adapter.
        return ReduceBySignature.__invoke__(self.phrase, self.state)


class ReduceBySignature(Adapter):
    """
    Reduces a formula node.

    This is an auxiliary adapter used to reduce
    :class:`htsql.core.tr.frame.FormulaPhrase` nodes.  The adapter is polymorphic
    on the formula signature.

    Unless overridden, the adapter reduces the arguments of the formula
    and generates a new formula with the same signature.

    `phrase` (:class:`htsql.core.tr.frame.FormulaPhrase`)
        The formula node to reduce.

    `state` (:class:`ReducingState`)
        The current state of the reducing process.

    Aliases:

    `signature` (:class:`htsql.core.tr.signature.Signature`)
        The signature of the formula.

    `domain` (:class:`htsql.core.tr.domain.Domain`)
        The co-domain of the formula.

    `arguments` (:class:`htsql.core.tr.signature.Bag`)
        The arguments of the formula.

    `is_nullable` (Boolean)
        Indicates that the formula may produce a ``NULL`` value.
    """

    adapt(Signature)

    @classmethod
    def __dispatch__(interface, phrase, *args, **kwds):
        # Override the default dispatch since the adapter is polymorphic
        # not on the type of the formula, but on the type of the formula
        # signature.
        assert isinstance(phrase, FormulaPhrase)
        return (type(phrase.signature),)

    def __init__(self, phrase, state):
        assert isinstance(phrase, FormulaPhrase)
        assert isinstance(state, ReducingState)
        self.phrase = phrase
        self.state = state
        self.signature = phrase.signature
        self.domain = phrase.domain
        self.arguments = phrase.arguments
        self.is_nullable = phrase.is_nullable

    def __call__(self):
        # By default, just reduce the arguments of the formula.
        arguments = self.arguments.map(self.state.reduce)
        return FormulaPhrase(self.signature,
                             self.domain,
                             self.is_nullable,
                             self.phrase.expression,
                             **arguments)


class ReduceIsEqual(ReduceBySignature):
    """
    Reduces an (in)equality operator.
    """

    adapt(IsEqualSig)

    def __call__(self):
        # Start with reducing the operands.
        lop = self.state.reduce(self.phrase.lop)
        rop = self.state.reduce(self.phrase.rop)

        # Check if both operands are `NULL`.
        if isinstance(lop, NullPhrase) and isinstance(rop, NullPhrase):
            # Reduce:
            #   null()=null(), null()!=null() => null()
            return self.state.to_predicate(
                    NullPhrase(self.phrase.domain, self.phrase.expression))

        # Now suppose one of the operands (`rop`) is `NULL`.
        if isinstance(lop, NullPhrase):
            lop, rop = rop, lop
        if isinstance(rop, NullPhrase):
            # We could reduce:
            #   lop=null(), lop!=null() => null(),
            # but that completely removes `lop` from the clause tree, and thus
            # may change the semantics of SQL (for instance, when `lop` is an
            # aggregate expression).  So we only do that when `lop` is a
            # literal.
            if isinstance(lop, LiteralPhrase):
                return self.state.to_predicate(
                        NullPhrase(self.phrase.domain, self.phrase.expression))

        # Check if both arguments are boolean literals.
        if (isinstance(lop, (TruePhrase, FalsePhrase)) and
            isinstance(rop, (TruePhrase, FalsePhrase))):
            # Reduce:
            #   true()=true() => true()
            #   ...
            # Note: we do not apply the same optimization for literals of
            # arbitrary type because we cannot precisely replicate the
            # database equality operator.
            polarity = self.signature.polarity
            if lop.value != rop.value:
                polarity = -polarity
            if polarity > 0:
                return self.state.to_predicate(
                        TruePhrase(self.phrase.expression))
            else:
                return self.state.to_predicate(
                        FalsePhrase(self.phrase.expression))

        # None of specific optimizations were applied, just return
        # the same operator with reduced operands.  Update the `is_nullable`
        # status since it may change after reducing the arguments.
        is_nullable = (lop.is_nullable or rop.is_nullable)
        return self.phrase.clone(is_nullable=is_nullable, lop=lop, rop=rop)


class ReduceIsTotallyEqual(ReduceBySignature):
    """
    Reduces a total (in)equality operator.
    """

    adapt(IsTotallyEqualSig)

    def __call__(self):
        # The polarity of the operator: +1 for `==`, -1 for `!==`.
        polarity = self.signature.polarity

        # Start with reducing the operands.
        lop = self.state.reduce(self.phrase.lop)
        rop = self.state.reduce(self.phrase.rop)

        # Check if both operands are `NULL`.
        if isinstance(lop, NullPhrase) and isinstance(rop, NullPhrase):
            # Reduce:
            #   null()==null() => true, null()!==null() => false()
            if polarity > 0:
                return self.state.to_predicate(
                        TruePhrase(self.phrase.expression))
            else:
                return self.state.to_predicate(
                        FalsePhrase(self.phrase.expression))

        # Now suppose one of the operands (`rop`) is `NULL`.
        if isinstance(lop, NullPhrase):
            lop, rop = rop, lop
        if isinstance(rop, NullPhrase):
            # We could always reduce:
            #   lop==null() => is_null(lop)
            #   lop!==null() => !is_null(lop)
            # In addition, if we know that `lop` is not nullable, we could have
            # reduced:
            #   lop==null() => false()
            #   lop!==null() => true()
            # However that completely removes `lop` from the clause tree, which,
            # in some cases, may change the semantics of SQL (for instance, when
            # `lop` is an aggregate expression).  Therefore, we only apply this
            # optimization when `lop` is a literal.
            if isinstance(lop, LiteralPhrase):
                if polarity > 0:
                    return self.state.to_predicate(
                            FalsePhrase(self.phrase.expression))
                else:
                    return self.state.to_predicate(
                            TruePhrase(self.phrase.expression))
            return FormulaPhrase(IsNullSig(polarity), self.domain,
                                 False, self.phrase.expression, op=lop)

        # Check if both arguments are boolean literals.
        if (isinstance(lop, (TruePhrase, FalsePhrase)) and
            isinstance(rop, (TruePhrase, FalsePhrase))):
            # Reduce:
            #   true()==true() => true()
            #   ...
            # Note: we do not apply the same optimization for literals of
            # arbitrary type because we cannot precisely replicate the
            # database equality operator.
            if polarity * (-1 if (lop.value != rop.value) else +1):
                return self.state.to_predicate(
                        TruePhrase(self.phrase.expression))
            else:
                return self.state.to_predicate(
                        FalsePhrase(self.phrase.expression))

        # When both operands are not nullable, we could replace a total
        # comparison operator with a regular one.
        if not (lop.is_nullable or rop.is_nullable):
            return self.phrase.clone(signature=IsEqualSig(polarity),
                                     lop=lop, rop=rop)

        # FIXME: we also need to replace a total comparison operator
        # with a regular one for databases that do not have a native
        # total comparison operator.

        # None of specific optimizations were applied, just return
        # the same operator with reduced operands.
        return self.phrase.clone(lop=lop, rop=rop)


class ReduceIsIn(ReduceBySignature):
    """
    Reduces the ``IN`` and ``NOT IN`` clauses.
    """

    adapt(IsInSig)

    def __call__(self):
        # Reduce the left operand.
        lop = self.state.reduce(self.phrase.lop)
        # Reduce the right operands, eliminating duplicates.
        rops = []
        duplicates = set()
        for rop in self.phrase.rops:
            rop = self.state.reduce(rop)
            if rop in duplicates:
                continue
            rops.append(rop)
            duplicates.add(rop)

        # Reduce:
        #   null()={...} => null()
        # We could do this substitution safely only when all operands
        # on the right are literals.
        if isinstance(lop, NullPhrase):
            if all(isinstance(rop, LiteralPhrase) for rop in rops):
                return self.state.to_predicate(
                        NullPhrase(self.domain, self.phrase.expression))
        # Similarly, reduce:
        #   x={null(),null(),...}
        if all(isinstance(rop, NullPhrase) for rop in rops):
            if isinstance(lop, LiteralPhrase):
                return self.state.to_predicate(
                        NullPhrase(self.domain, self.phrase.expression))

        # Reduce:
        #   x={y} => x=y
        if len(rops) == 1:
            [rop] = rops
            signature = IsEqualSig(self.signature.polarity)
            is_nullable = (lop.is_nullable or rop.is_nullable)
            return FormulaPhrase(signature, self.domain, is_nullable,
                                 self.phrase.expression, lop=lop, rop=rop)

        # None of specific optimizations were applied, just return
        # the same operator with reduced operands.  Update the `is_nullable`
        # status since it may change after reducing the arguments.
        is_nullable = (lop.is_nullable or any(rop.is_nullable for rop in rops))
        return self.phrase.clone(is_nullable=is_nullable, lop=lop, rops=rops)


class ReduceIsNull(ReduceBySignature):
    """
    Reduces the ``IS NULL`` and ``IS NOT NULL`` clauses.
    """

    adapt(IsNullSig)

    def __call__(self):
        # Start with reducing the operand.
        op = self.state.reduce(self.phrase.op)

        # Reduce:
        #   is_null(null()) => true()
        #   !is_null(null()) => false()
        if isinstance(op, NullPhrase):
            if self.signature.polarity > 0:
                return self.state.to_predicate(
                        TruePhrase(self.phrase.expression))
            else:
                return self.state.to_predicate(
                        FalsePhrase(self.phrase.expression))
        # If the operand is not nullable, we could reduce the operator
        # to a `TRUE` or a `FALSE` clause.  However it is only safe
        # to do for a literal operand.
        if isinstance(op, LiteralPhrase):
            if self.signature.polarity > 0:
                return self.state.to_predicate(
                        FalsePhrase(self.phrase.expression))
            else:
                return self.state.to_predicate(
                        TruePhrase(self.phrase.expression))

        # Return the same operator with a reduced operand.
        return self.phrase.clone(op=op)


class ReduceIfNull(ReduceBySignature):
    """
    Reduces the ``IFNULL`` clause.
    """

    adapt(IfNullSig)

    def __call__(self):
        # Reduce the operands.
        lop = self.state.reduce(self.phrase.lop)
        rop = self.state.reduce(self.phrase.rop)

        # If the first operand is not nullable, then the operation is no-op,
        # and we could just return the first operand discarding the second
        # one.  However discarding a clause is not safe in general, so we
        # only do that when the second operand is a literal.
        if not lop.is_nullable and isinstance(rop, LiteralPhrase):
            return lop
        # Reduce:
        #   if_null(lop,null()) => lop
        if isinstance(rop, NullPhrase):
            return lop
        # Reduce:
        #   if_null(null(),rop) => rop
        if isinstance(lop, NullPhrase):
            return rop

        # Return the same operator with reduced operands.
        is_nullable = (lop.is_nullable and rop.is_nullable)
        return self.phrase.clone(is_nullable=is_nullable, lop=lop, rop=rop)


class ReduceNullIf(ReduceBySignature):
    """
    Reduces the ``NULLIF`` clause.
    """

    adapt(NullIfSig)

    def __call__(self):
        # Reduce the operands.
        lop = self.state.reduce(self.phrase.lop)
        rop = self.state.reduce(self.phrase.rop)
        # Reduce (when it is safe, i.e., when `rop` is a literal):
        #   null_if(null(),rop) => null()
        if isinstance(lop, NullPhrase):
            if isinstance(rop, LiteralPhrase):
                return lop
        # Reduce:
        #   null_if(lop,null()) => lop
        if isinstance(rop, NullPhrase):
            return lop
        # When both operands are literals, we could determine the result
        # immediately.  We should be careful though since we cannot precisely
        # mimic the equality operator of the database.
        if isinstance(lop, LiteralPhrase) and isinstance(rop, LiteralPhrase):
            # Assume that if the literals are equal in Python, they would
            # be equal for the database too.  The reverse is not valid in
            # general, but still valid for some literals.
            if lop.value == rop.value:
                return NullPhrase(self.phrase.domain, self.phrase.expression)
            # We could safely rely on comparison for Boolean values.
            elif isinstance(self.phrase.domain, BooleanDomain):
                return lop
            # In general, we can't rely on comparison for string values,
            # but we could assume that an empty string is only equal to itself.
            elif isinstance(self.phrase.domain, TextDomain):
                if len(lop.value) > 0 and len(rop.value) == 0:
                    return lop

        # Return the same operator with reduced operands.
        return self.phrase.clone(lop=lop, rop=rop)


class ReduceAnd(ReduceBySignature):
    """
    Reduces "AND" (``&``) operator.
    """

    adapt(AndSig)

    def __call__(self):
        # Start with reducing the operands.
        ops = []
        duplicates = set()
        for op in self.phrase.ops:
            # Reduce the operand.
            op = self.state.reduce(op)
            # Weed out duplicates.
            if op in duplicates:
                continue
            # Weed out `TRUE` literals.
            if isinstance(op, TruePhrase):
                continue
            # Expand a nested `AND` operator.
            if isformula(op, AndSig):
                # The operands of the nested `AND` are already reduced,
                # but we still need to weed out duplicates.
                for nop in op.ops:
                    if nop in duplicates:
                        continue
                    ops.append(nop)
                    duplicates.add(nop)
                continue
            ops.append(op)
            duplicates.add(op)

        # Reduce:
        #   "&"() => true()
        if not ops:
            return self.state.to_predicate(TruePhrase(self.phrase.expression))
        # Reduce:
        #   "&"(op) => op
        if len(ops) == 1:
            return ops[0]
        # We could reduce:
        #   "&"(...,false(),...) => false()
        # but that means we discard the rest of the operands
        # from the clause tree, which is unsafe.  So we only
        # do that when all operands are literals.
        if any(isinstance(op, FalsePhrase) for op in ops):
            if all(isinstance(op, LiteralPhrase) for op in ops):
                return self.state.to_predicate(
                        FalsePhrase(self.phrase.expression))

        # Reduce:
        #   x!=a&x!=b&... => x!={a,b,...}
        if all((isformula(op, IsEqualSig) or isformula(op, IsInSig))
               and op.lop == ops[0].lop and op.signature.polarity == -1
               for op in ops):
            lop = ops[0].lop
            rops = []
            duplicates = set()
            for op in ops:
                if isformula(op, IsEqualSig):
                    if op.rop not in duplicates:
                        rops.append(op.rop)
                        duplicates.add(op.rop)
                elif isformula(op, IsInSig):
                    for rop in op.rops:
                        if rop not in duplicates:
                            rops.append(rop)
                            duplicates.add(rop)
            if len(rops) > 1:
                is_nullable = (lop.is_nullable or any(rop.is_nullable
                                                      for rop in rops))
                return FormulaPhrase(IsInSig(-1), self.domain, is_nullable,
                                     self.phrase.expression,
                                     lop=lop, rops=rops)
            else:
                [rop] = rops
                is_nullable = (lop.is_nullable or rop.is_nullable)
                return FormulaPhrase(IsEqualSig(-1), self.domain, is_nullable,
                                     self.phrase.expression,
                                     lop=lop, rop=rop)

        # Return the same operator with reduced operands.  Update
        # the `is_nullable` status since it could change after reducing
        # the arguments.
        is_nullable = any(op.is_nullable for op in ops)
        if any(isinstance(op, FalsePhrase) for op in ops):
            is_nullable = False
        return self.phrase.clone(is_nullable=is_nullable, ops=ops)


class ReduceOr(ReduceBySignature):
    """
    Reduces "OR" (``|``) operator.
    """

    adapt(OrSig)

    def __call__(self):
        # Start with reducing the operands.
        ops = []
        duplicates = set()
        for op in self.phrase.ops:
            # Reduce the operand.
            op = self.state.reduce(op)
            # Weed out duplicates.
            if op in duplicates:
                continue
            # Weed out `FALSE` literals.
            if isinstance(op, FalsePhrase):
                continue
            # Expand a nested `OR` operator.
            if isformula(op, OrSig):
                # The operands of the nested `OR` are already reduced,
                # but we still need to weed out duplicates.
                for nop in op.ops:
                    if nop in duplicates:
                        continue
                    ops.append(nop)
                    duplicates.add(nop)
                continue
            ops.append(op)
            duplicates.add(op)

        # Reduce:
        #   "|"() => false()
        if not ops:
            return self.state.to_predicate(FalsePhrase(self.phrase.expression))
        # Reduce:
        #   "|"(op) => op
        if len(ops) == 1:
            return ops[0]
        # We could reduce:
        #   "|"(...,true(),...) => true()
        # but that means we discard the rest of the operands
        # from the clause tree, which is unsafe.  So we only
        # do that when all operands are literals.
        if any(isinstance(op, TruePhrase) for op in ops):
            if all(isinstance(op, LiteralPhrase) for op in ops):
                return self.state.to_predicate(
                        TruePhrase(self.phrase.expression))

        # Reduce:
        #   x=a|x=b|... => x={a,b,...}
        if all((isformula(op, IsEqualSig) or isformula(op, IsInSig))
               and op.lop == ops[0].lop and op.signature.polarity == +1
               for op in ops):
            lop = ops[0].lop
            rops = []
            duplicates = set()
            for op in ops:
                if isformula(op, IsEqualSig):
                    if op.rop not in duplicates:
                        rops.append(op.rop)
                        duplicates.add(op.rop)
                elif isformula(op, IsInSig):
                    for rop in op.rops:
                        if rop not in duplicates:
                            rops.append(rop)
                            duplicates.add(rop)
            if len(rops) > 1:
                is_nullable = (lop.is_nullable or any(rop.is_nullable
                                                      for rop in rops))
                return FormulaPhrase(IsInSig(+1), self.domain, is_nullable,
                                     self.phrase.expression,
                                     lop=lop, rops=rops)
            else:
                [rop] = rops
                is_nullable = (lop.is_nullable or rop.is_nullable)
                return FormulaPhrase(IsEqualSig(+1), self.domain, is_nullable,
                                     self.phrase.expression,
                                     lop=lop, rop=rop)

        # Return the same operator with reduced operands.  Update
        # the `is_nullable` status since it could change after reducing
        # the arguments.
        is_nullable = any(op.is_nullable for op in ops)
        if any(isinstance(op, TruePhrase) for op in ops):
            is_nullable = False
        return self.phrase.clone(is_nullable=is_nullable, ops=ops)


class ReduceNot(ReduceBySignature):
    """
    Reduces a "NOT" (``!``) operator.
    """

    adapt(NotSig)

    def __call__(self):
        # Start with reducing the operand.
        op = self.state.reduce(self.phrase.op)

        # Reduce:
        #   !null() => null()
        #   !true() => false()
        #   !false() => true()
        if isinstance(op, NullPhrase):
            return self.state.to_predicate(
                    NullPhrase(self.phrase.domain, self.phrase.expression))
        if isinstance(op, TruePhrase):
            return self.state.to_predicate(FalsePhrase(self.phrase.expression))
        if isinstance(op, FalsePhrase):
            return self.state.to_predicate(TruePhrase(self.phrase.expression))
        # Reverse polarity of equality operators:
        #   !(lop=rop) => lop!=rop
        #   ...
        if isformula(op, (IsEqualSig, IsTotallyEqualSig, IsInSig, IsNullSig)):
            return op.clone(signature=op.signature.reverse())

        # Return the same operator with a reduced operand.
        return self.phrase.clone(is_nullable=op.is_nullable, op=op)


class ReduceFromPredicate(ReduceBySignature):

    adapt(FromPredicateSig)

    def __call__(self):
        #op = self.state.reduce(self.phrase.op)
        #if isformula(op, ToPredicateSig):
        #    return op.op
        #return self.phrase.clone(is_nullable=op.is_nullable, op=op)
        return self.state.reduce(self.phrase.op)


class ReduceToPredicate(ReduceBySignature):

    adapt(ToPredicateSig)

    def __call__(self):
        #op = self.state.reduce(self.phrase.op)
        #if isformula(op, FromPredicateSig):
        #    return op.op
        #return self.phrase.clone(is_nullable=op.is_nullable, op=op)
        return self.state.reduce(self.phrase.op)


class ReduceExport(Reduce):
    """
    Reduces an export phrase.
    """

    adapt(ExportPhrase)

    def __call__(self):
        # The default implementation (used for columns and embeddings)
        # is no-op.
        return self.phrase


class ReduceReference(Reduce):
    """
    Reduce a reference phrase.
    """

    adapt(ReferencePhrase)

    def __call__(self):
        # Reduce an export reference: if the reference points to
        # a collapsed frame, replace the reference with the referenced
        # phrase.
        if self.phrase.tag not in self.state.substitutes:
            return self.phrase
        select = self.state.substitutes[self.phrase.tag]
        phrase = select[self.phrase.index]
        # Return a (reduced) referenced phrase.
        return self.state.reduce(phrase)


def reduce(segment):
    """
    Reduces (simplifies) a SQL clause.

    Returns an equivalent (possibly the same) clause.

    `clause` (:class:`htsql.core.tr.frame.Clause`)
        The clause to simplify.

    `state` (:class:`ReducingState` or ``None``)
        The reducing state to use.  If not set, a new reducing state
        is instantiated.
    """
    # Instantiate a new reducing state if necessary.
    state = ReducingState()
    segment = state.collapse(segment)
    segment = state.reduce(segment)
    segment = state.interlink(segment)
    return segment