-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathRecordSourceNodes.h
More file actions
1100 lines (913 loc) · 31.2 KB
/
RecordSourceNodes.h
File metadata and controls
1100 lines (913 loc) · 31.2 KB
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
/*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
* Adriano dos Santos Fernandes
*/
#ifndef JRD_RECORD_SOURCE_NODES_H
#define JRD_RECORD_SOURCE_NODES_H
#include "../common/classes/alloc.h"
#include "../common/classes/array.h"
#include "../common/classes/objects_array.h"
#include "../common/classes/NestConst.h"
#include "../jrd/QualifiedName.h"
#include "../dsql/ExprNodes.h"
#include "../jrd/jrd.h"
#include "../jrd/exe.h"
#include "../dsql/Visitors.h"
#include "../dsql/pass1_proto.h"
namespace Jrd {
class IndexRetrieval;
class OptimizerRetrieval;
class ProcedureScan;
class BoolExprNode;
class MessageNode;
class RecSourceListNode;
class RelationSourceNode;
class RseNode;
class SelectExprNode;
class ValueListNode;
class SortNode : public Firebird::PermanentStorage, public Printable
{
public:
explicit SortNode(MemoryPool& pool)
: PermanentStorage(pool),
unique(false),
expressions(pool),
direction(pool),
nullOrder(pool)
{
}
public:
virtual Firebird::string internalPrint(NodePrinter& printer) const
{
//// FIXME-PRINT:
return "SortNode";
}
SortNode* copy(thread_db* tdbb, NodeCopier& copier) const;
SortNode* pass1(thread_db* tdbb, CompilerScratch* csb);
SortNode* pass2(thread_db* tdbb, CompilerScratch* csb);
bool computable(CompilerScratch* csb, StreamType stream, bool allowOnlyCurrentStream);
void findDependentFromStreams(const CompilerScratch* csb,
StreamType currentStream, SortedStreamList* streamList);
NullsPlacement getEffectiveNullOrder(unsigned index) const
{
if (direction[index] == ORDER_ASC)
return (nullOrder[index] == NULLS_DEFAULT) ? NULLS_FIRST : nullOrder[index];
else if (direction[index] == ORDER_DESC)
return (nullOrder[index] == NULLS_DEFAULT) ? NULLS_LAST : nullOrder[index];
fb_assert(false);
return NULLS_DEFAULT;
}
public:
bool unique; // sort uses unique key - for DISTINCT and GROUP BY
NestValueArray expressions; // sort expressions
Firebird::Array<SortDirection> direction; // rse_order_*
Firebird::Array<NullsPlacement> nullOrder; // rse_nulls_*
};
class MapNode : public Firebird::PermanentStorage, public Printable
{
public:
explicit MapNode(MemoryPool& pool)
: PermanentStorage(pool),
sourceList(pool),
targetList(pool)
{
}
MapNode* copy(thread_db* tdbb, NodeCopier& copier) const;
MapNode* pass1(thread_db* tdbb, CompilerScratch* csb);
MapNode* pass2(thread_db* tdbb, CompilerScratch* csb);
public:
virtual Firebird::string internalPrint(NodePrinter& printer) const
{
/*** FIXME-PRINT:
NODE_PRINT(printer, sourceList);
NODE_PRINT(printer, targetList);
***/
return "MapNode";
}
public:
NestValueArray sourceList;
NestValueArray targetList;
};
class PlanNode : public Firebird::PermanentStorage, public Printable
{
public:
enum Type : UCHAR
{
TYPE_JOIN,
TYPE_RETRIEVE
};
struct AccessItem
{
explicit AccessItem(MemoryPool& pool)
: relationId(0),
indexId(0),
indexName(pool)
{
}
explicit AccessItem(MemoryPool& pool, const AccessItem& o)
: relationId(o.relationId),
indexId(o.indexId),
indexName(pool, o.indexName)
{
}
SLONG relationId;
SLONG indexId;
QualifiedName indexName;
};
struct AccessType
{
enum Type : UCHAR
{
TYPE_SEQUENTIAL,
TYPE_NAVIGATIONAL,
TYPE_INDICES
};
AccessType(MemoryPool& pool, Type aType)
: type(aType),
items(pool)
{
}
Type const type;
Firebird::ObjectsArray<AccessItem> items;
};
public:
PlanNode(MemoryPool& pool, Type aType)
: PermanentStorage(pool),
type(aType),
accessType(NULL),
recordSourceNode(NULL),
subNodes(pool),
dsqlNames(NULL)
{
}
public:
virtual Firebird::string internalPrint(NodePrinter& printer) const
{
//// FIXME-PRINT:
return "PlanNode";
}
PlanNode* dsqlPass(DsqlCompilerScratch* dsqlScratch);
private:
dsql_ctx* dsqlPassAliasList(DsqlCompilerScratch* dsqlScratch);
static dsql_ctx* dsqlPassAlias(DsqlCompilerScratch* dsqlScratch, DsqlContextStack& stack,
const QualifiedName& alias);
public:
Type const type;
AccessType* accessType;
RecordSourceNode* recordSourceNode;
Firebird::Array<NestConst<PlanNode> > subNodes;
Firebird::ObjectsArray<QualifiedName>* dsqlNames;
};
class InversionNode
{
public:
enum Type : UCHAR
{
TYPE_AND,
TYPE_OR,
TYPE_IN,
TYPE_DBKEY,
TYPE_INDEX
};
InversionNode(Type aType, InversionNode* aNode1, InversionNode* aNode2)
: impure(0),
id(0),
type(aType),
retrieval(NULL),
node1(aNode1),
node2(aNode2),
value(NULL)
{
}
InversionNode(IndexRetrieval* aRetrieval, ULONG anImpure)
: impure(anImpure),
id(0),
type(TYPE_INDEX),
retrieval(aRetrieval),
node1(NULL),
node2(NULL),
value(NULL)
{
}
InversionNode(ValueExprNode* aValue, USHORT aId)
: impure(0),
id(aId),
type(TYPE_DBKEY),
retrieval(NULL),
node1(NULL),
node2(NULL),
value(aValue)
{
}
ULONG impure;
USHORT id;
Type type;
NestConst<IndexRetrieval> retrieval;
NestConst<InversionNode> node1;
NestConst<InversionNode> node2;
NestConst<ValueExprNode> value;
};
class DbKeyRangeNode
{
public:
DbKeyRangeNode(ValueExprNode* aLower, ValueExprNode* aUpper)
: lower(aLower), upper(aUpper)
{
}
NestConst<ValueExprNode> lower;
NestConst<ValueExprNode> upper;
};
class WithClause : public Firebird::Array<SelectExprNode*>
{
public:
explicit WithClause(Firebird::MemoryPool& pool)
: Firebird::Array<SelectExprNode*>(pool),
recursive(false)
{
}
public:
bool recursive;
};
class LocalTableSourceNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_LOCAL_TABLE>
{
public:
explicit LocalTableSourceNode(MemoryPool& pool, const MetaName& aDsqlName = NULL)
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_LOCAL_TABLE>(pool),
alias(pool)
{
}
static LocalTableSourceNode* parse(thread_db* tdbb, CompilerScratch* csb, const SSHORT blrOp,
bool parseContext);
Firebird::string internalPrint(NodePrinter& printer) const override;
RecordSourceNode* dsqlPass(DsqlCompilerScratch* dsqlScratch) override;
bool dsqlSubSelectFinder(SubSelectFinder& /*visitor*/) override
{
return false;
}
bool dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const override;
void genBlr(DsqlCompilerScratch* dsqlScratch) override;
LocalTableSourceNode* copy(thread_db* tdbb, NodeCopier& copier) const override;
RecordSourceNode* pass1(thread_db* tdbb, CompilerScratch* csb) override
{
return this;
}
void pass1Source(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
BoolExprNode** boolean, RecordSourceNodeStack& stack) override;
RecordSourceNode* pass2(thread_db* /*tdbb*/, CompilerScratch* /*csb*/) override
{
return this;
}
void pass2Rse(thread_db* tdbb, CompilerScratch* csb) override;
bool containsStream(StreamType checkStream) const override
{
return checkStream == stream;
}
void computeDbKeyStreams(StreamList& streamList) const override
{
streamList.add(getStream());
}
bool computable(CompilerScratch* /*csb*/, StreamType /*stream*/,
bool /*allowOnlyCurrentStream*/, ValueExprNode* /*value*/) override
{
return true;
}
void findDependentFromStreams(const CompilerScratch* /*csb*/,
StreamType /*currentStream*/, SortedStreamList* /*streamList*/) override
{
}
RecordSource* compile(thread_db* tdbb, Optimizer* opt, bool innerSubStream) override;
public:
Firebird::string alias;
USHORT tableNumber = 0;
SSHORT context = 0; // user-specified context number for the local table reference
};
class RelationSourceNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_RELATION>
{
public:
explicit RelationSourceNode(MemoryPool& pool, const QualifiedName& aDsqlName = {})
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_RELATION>(pool),
dsqlName(pool, aDsqlName),
alias(pool),
context(0)
{
}
static RelationSourceNode* parse(thread_db* tdbb, CompilerScratch* csb, const SSHORT blrOp,
bool parseContext);
virtual Firebird::string internalPrint(NodePrinter& printer) const;
virtual RecordSourceNode* dsqlPass(DsqlCompilerScratch* dsqlScratch);
virtual bool dsqlSubSelectFinder(SubSelectFinder& /*visitor*/)
{
return false;
}
virtual bool dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const;
virtual void genBlr(DsqlCompilerScratch* dsqlScratch);
virtual RelationSourceNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual RecordSourceNode* pass1(thread_db* tdbb, CompilerScratch* csb);
virtual void pass1Source(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
BoolExprNode** boolean, RecordSourceNodeStack& stack);
virtual RecordSourceNode* pass2(thread_db* /*tdbb*/, CompilerScratch* /*csb*/)
{
return this;
}
virtual void pass2Rse(thread_db* tdbb, CompilerScratch* csb);
virtual bool containsStream(StreamType checkStream) const
{
return checkStream == stream;
}
virtual void computeDbKeyStreams(StreamList& streamList) const
{
streamList.add(getStream());
}
virtual bool computable(CompilerScratch* /*csb*/, StreamType /*stream*/,
bool /*allowOnlyCurrentStream*/, ValueExprNode* /*value*/)
{
return true;
}
virtual void findDependentFromStreams(const CompilerScratch* /*csb*/,
StreamType /*currentStream*/, SortedStreamList* /*streamList*/)
{
}
virtual RecordSource* compile(thread_db* tdbb, Optimizer* opt, bool innerSubStream);
public:
QualifiedName dsqlName;
Firebird::string alias; // SQL alias for the relation
Rsc::Rel relation;
private:
Rsc::Rel view; // parent view for posting access
public:
SSHORT context; // user-specified context number for the relation reference
};
class ProcedureSourceNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_PROCEDURE>
{
public:
explicit ProcedureSourceNode(MemoryPool& pool,
const QualifiedName& aDsqlName = {})
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_PROCEDURE>(pool),
dsqlName(pool, aDsqlName),
alias(pool)
{
}
static ProcedureSourceNode* parse(thread_db* tdbb, CompilerScratch* csb, const SSHORT blrOp,
bool parseContext);
public:
Firebird::string internalPrint(NodePrinter& printer) const override;
RecordSourceNode* dsqlPass(DsqlCompilerScratch* dsqlScratch) override;
bool dsqlAggregateFinder(AggregateFinder& visitor) override;
bool dsqlAggregate2Finder(Aggregate2Finder& visitor) override;
bool dsqlInvalidReferenceFinder(InvalidReferenceFinder& visitor) override;
bool dsqlSubSelectFinder(SubSelectFinder& visitor) override;
bool dsqlFieldFinder(FieldFinder& visitor) override;
RecordSourceNode* dsqlFieldRemapper(FieldRemapper& visitor) override;
bool dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const override;
void genBlr(DsqlCompilerScratch* dsqlScratch) override;
ProcedureSourceNode* copy(thread_db* tdbb, NodeCopier& copier) const override;
RecordSourceNode* pass1(thread_db* tdbb, CompilerScratch* csb) override;
void pass1Source(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
BoolExprNode** boolean, RecordSourceNodeStack& stack) override;
RecordSourceNode* pass2(thread_db* tdbb, CompilerScratch* csb) override;
void pass2Rse(thread_db* tdbb, CompilerScratch* csb) override;
bool containsStream(StreamType checkStream) const override
{
return checkStream == stream;
}
void computeDbKeyStreams(StreamList& /*streamList*/) const override
{
}
bool deterministic(thread_db* /*tdbb*/) const override
{
return false;
}
bool computable(CompilerScratch* csb, StreamType stream,
bool allowOnlyCurrentStream, ValueExprNode* value) override;
void findDependentFromStreams(const CompilerScratch* csb,
StreamType currentStream, SortedStreamList* streamList) override;
void collectStreams(SortedStreamList& streamList) const override;
RecordSource* compile(thread_db* tdbb, Optimizer* opt, bool innerSubStream) override;
public:
QualifiedName dsqlName;
Firebird::string alias;
/***
dimitr: Referencing procedures via a pointer is not currently reliable, because
procedures can be removed from the metadata cache after ALTER/DROP.
Usually, this is prevented via the reference counting, but it's incremented
only for compiled requests. Node trees without requests (e.g. computed fields)
are not protected and may end with dead procedure pointers, causing problems
(up to crashing) when they're copied the next time. See CORE-5456 / CORE-5457.
ExecProcedureNode is a lucky exception because it's never (directly) used in
expressions. Sub-procedures are safe too. In other cases the procedure object
must be refetched from the metadata cache while copying the node.
A better (IMO) solution would be to add a second-level reference counting for
metadata objects since the parsing stage till either request creation or
explicit unload from the metadata cache. But we don't have clearly established
cache management policies yet, so I leave it for the other day.
***/
SubRoutine<jrd_prc> procedure;
NestConst<ValueListNode> inputSources;
NestConst<ValueListNode> inputTargets;
NestConst<Firebird::ObjectsArray<MetaName>> dsqlInputArgNames;
private:
NestConst<MessageNode> inputMessage;
Rsc::Rel view;
USHORT procedureId = 0;
SSHORT context = 0;
};
class AggregateSourceNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_AGGREGATE_SOURCE>
{
public:
explicit AggregateSourceNode(MemoryPool& pool)
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_AGGREGATE_SOURCE>(pool),
dsqlGroup(NULL),
dsqlRse(NULL),
group(NULL),
map(NULL),
rse(NULL),
dsqlWindow(false)
{
}
static AggregateSourceNode* parse(thread_db* tdbb, CompilerScratch* csb);
virtual Firebird::string internalPrint(NodePrinter& printer) const;
virtual bool dsqlAggregateFinder(AggregateFinder& visitor);
virtual bool dsqlAggregate2Finder(Aggregate2Finder& visitor);
virtual bool dsqlInvalidReferenceFinder(InvalidReferenceFinder& visitor);
virtual bool dsqlSubSelectFinder(SubSelectFinder& visitor);
virtual bool dsqlFieldFinder(FieldFinder& visitor);
virtual RecordSourceNode* dsqlFieldRemapper(FieldRemapper& visitor);
virtual bool dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const;
virtual void genBlr(DsqlCompilerScratch* dsqlScratch);
virtual AggregateSourceNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual RecordSourceNode* pass1(thread_db* tdbb, CompilerScratch* csb);
virtual void pass1Source(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
BoolExprNode** boolean, RecordSourceNodeStack& stack);
virtual RecordSourceNode* pass2(thread_db* tdbb, CompilerScratch* csb);
virtual void pass2Rse(thread_db* tdbb, CompilerScratch* csb);
virtual bool containsStream(StreamType checkStream) const;
virtual void computeDbKeyStreams(StreamList& /*streamList*/) const
{
}
virtual bool computable(CompilerScratch* csb, StreamType stream,
bool allowOnlyCurrentStream, ValueExprNode* value);
virtual void findDependentFromStreams(const CompilerScratch* csb,
StreamType currentStream, SortedStreamList* streamList);
virtual RecordSource* compile(thread_db* tdbb, Optimizer* opt, bool innerSubStream);
private:
void genMap(DsqlCompilerScratch* dsqlScratch, UCHAR blrVerb, dsql_map* map);
public:
NestConst<ValueListNode> dsqlGroup;
NestConst<RseNode> dsqlRse;
NestConst<SortNode> group;
NestConst<MapNode> map;
private:
NestConst<RseNode> rse;
public:
bool dsqlWindow;
};
class UnionSourceNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_UNION>
{
public:
explicit UnionSourceNode(MemoryPool& pool)
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_UNION>(pool),
dsqlClauses(NULL),
dsqlParentRse(NULL),
clauses(pool),
maps(pool),
mapStream(0),
dsqlAll(false),
recursive(false)
{
}
static UnionSourceNode* parse(thread_db* tdbb, CompilerScratch* csb, const SSHORT blrOp);
virtual bool dsqlAggregateFinder(AggregateFinder& visitor);
virtual bool dsqlAggregate2Finder(Aggregate2Finder& visitor);
virtual bool dsqlInvalidReferenceFinder(InvalidReferenceFinder& visitor);
virtual bool dsqlSubSelectFinder(SubSelectFinder& visitor);
virtual bool dsqlFieldFinder(FieldFinder& visitor);
virtual RecordSourceNode* dsqlFieldRemapper(FieldRemapper& visitor);
virtual Firebird::string internalPrint(NodePrinter& printer) const;
virtual void genBlr(DsqlCompilerScratch* dsqlScratch);
virtual UnionSourceNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual RecordSourceNode* pass1(thread_db* /*tdbb*/, CompilerScratch* /*csb*/)
{
return this;
}
virtual void pass1Source(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
BoolExprNode** boolean, RecordSourceNodeStack& stack);
virtual RecordSourceNode* pass2(thread_db* tdbb, CompilerScratch* csb);
virtual void pass2Rse(thread_db* tdbb, CompilerScratch* csb);
virtual bool containsStream(StreamType checkStream) const;
virtual void computeDbKeyStreams(StreamList& streamList) const;
virtual bool computable(CompilerScratch* csb, StreamType stream,
bool allowOnlyCurrentStream, ValueExprNode* value);
virtual void findDependentFromStreams(const CompilerScratch* csb,
StreamType currentStream, SortedStreamList* streamList);
virtual RecordSource* compile(thread_db* tdbb, Optimizer* opt, bool innerSubStream);
public:
RecSourceListNode* dsqlClauses;
RseNode* dsqlParentRse;
private:
Firebird::Array<NestConst<RseNode> > clauses; // RseNode's for union
Firebird::Array<NestConst<MapNode> > maps; // RseNode's maps
StreamType mapStream; // stream for next level record of recursive union
public:
bool dsqlAll; // UNION ALL
bool recursive; // union node is a recursive union
};
class WindowSourceNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_WINDOW>
{
public:
struct Window
{
explicit Window(MemoryPool&)
: stream(INVALID_STREAM),
exclusion(WindowClause::Exclusion::NO_OTHERS)
{
}
StreamType stream;
NestConst<SortNode> group;
NestConst<SortNode> regroup;
NestConst<SortNode> order;
NestConst<MapNode> map;
NestConst<WindowClause::FrameExtent> frameExtent;
WindowClause::Exclusion exclusion;
};
explicit WindowSourceNode(MemoryPool& pool)
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_WINDOW>(pool),
rse(NULL),
windows(pool)
{
}
static WindowSourceNode* parse(thread_db* tdbb, CompilerScratch* csb);
private:
void parseLegacyPartitionBy(thread_db* tdbb, CompilerScratch* csb);
void parseWindow(thread_db* tdbb, CompilerScratch* csb);
public:
virtual StreamType getStream() const
{
fb_assert(false);
return 0;
}
virtual Firebird::string internalPrint(NodePrinter& printer) const;
virtual WindowSourceNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual RecordSourceNode* pass1(thread_db* tdbb, CompilerScratch* csb);
virtual void pass1Source(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
BoolExprNode** boolean, RecordSourceNodeStack& stack);
virtual RecordSourceNode* pass2(thread_db* tdbb, CompilerScratch* csb);
virtual void pass2Rse(thread_db* tdbb, CompilerScratch* csb);
virtual bool containsStream(StreamType checkStream) const;
virtual void collectStreams(SortedStreamList& streamList) const;
virtual void computeDbKeyStreams(StreamList& /*streamList*/) const
{
}
virtual void computeRseStreams(StreamList& streamList) const;
virtual bool computable(CompilerScratch* csb, StreamType stream,
bool allowOnlyCurrentStream, ValueExprNode* value);
virtual void findDependentFromStreams(const CompilerScratch* csb,
StreamType currentStream, SortedStreamList* streamList);
virtual RecordSource* compile(thread_db* tdbb, Optimizer* opt, bool innerSubStream);
private:
NestConst<RseNode> rse;
Firebird::ObjectsArray<Window> windows;
};
class RseNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_RSE>
{
enum : UCHAR // storage is BLR-compatible
{
INNER_JOIN = blr_inner,
LEFT_JOIN = blr_left,
RIGHT_JOIN = blr_right,
FULL_JOIN = blr_full,
SEMI_JOIN,
ANTI_JOIN
};
public:
enum : USHORT
{
FLAG_VARIANT = 0x01, // variant (not invariant?)
FLAG_SINGULAR = 0x02, // singleton select
FLAG_WRITELOCK = 0x04, // locked for write
FLAG_SCROLLABLE = 0x08, // scrollable cursor
FLAG_DSQL_COMPARATIVE = 0x10, // transformed from DSQL ComparativeBoolNode
FLAG_LATERAL = 0x20, // lateral derived table
FLAG_SKIP_LOCKED = 0x40, // skip locked
FLAG_SUB_QUERY = 0x80 // sub-query
};
bool isInnerJoin() const
{
return (rse_jointype == INNER_JOIN);
}
bool isOuterJoin() const
{
return (rse_jointype == LEFT_JOIN || rse_jointype == RIGHT_JOIN || rse_jointype == FULL_JOIN);
}
bool isFullJoin() const
{
return (rse_jointype == FULL_JOIN);
}
bool isSpecialJoin() const
{
return (rse_jointype == SEMI_JOIN || rse_jointype == ANTI_JOIN);
}
bool isSemiJoin() const
{
fb_assert(isSpecialJoin());
return (rse_jointype == SEMI_JOIN);
}
bool isInvariant() const
{
return (flags & FLAG_VARIANT) == 0;
}
bool isSingular() const
{
return (flags & FLAG_SINGULAR) != 0;
}
bool isScrollable() const
{
return (flags & FLAG_SCROLLABLE) != 0;
}
bool isLateral() const
{
return (flags & FLAG_LATERAL) != 0;
}
bool isSubQuery() const
{
return (flags & FLAG_SUB_QUERY) != 0;
}
bool hasWriteLock() const
{
return (flags & FLAG_WRITELOCK) != 0;
}
bool hasSkipLocked() const
{
return (flags & FLAG_SKIP_LOCKED) != 0;
}
explicit RseNode(MemoryPool& pool)
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_RSE>(pool),
rse_relations(pool)
{}
RseNode* clone(MemoryPool& pool)
{
RseNode* obj = FB_NEW_POOL(pool) RseNode(pool);
obj->dsqlFirst = dsqlFirst;
obj->dsqlSkip = dsqlSkip;
obj->dsqlDistinct = dsqlDistinct;
obj->dsqlSelectList = dsqlSelectList;
obj->dsqlFrom = dsqlFrom;
obj->dsqlWhere = dsqlWhere;
obj->dsqlJoinUsing = dsqlJoinUsing;
obj->dsqlGroup = dsqlGroup;
obj->dsqlHaving = dsqlHaving;
obj->dsqlNamedWindows = dsqlNamedWindows;
obj->dsqlOrder = dsqlOrder;
obj->dsqlStreams = dsqlStreams;
obj->dsqlContext = dsqlContext;
obj->dsqlExplicitJoin = dsqlExplicitJoin;
obj->rse_jointype = rse_jointype;
obj->rse_first = rse_first;
obj->rse_skip = rse_skip;
obj->rse_boolean = rse_boolean;
obj->rse_sorted = rse_sorted;
obj->rse_projection = rse_projection;
obj->rse_aggregate = rse_aggregate;
obj->rse_plan = rse_plan;
obj->rse_invariants = rse_invariants;
obj->flags = flags;
obj->rse_relations = rse_relations;
obj->firstRows = firstRows;
return obj;
}
virtual void getChildren(NodeRefsHolder& holder, bool dsql) const
{
RecordSourceNode::getChildren(holder, dsql);
if (dsql)
{
holder.add(dsqlStreams);
holder.add(dsqlWhere);
holder.add(dsqlJoinUsing);
holder.add(dsqlOrder);
holder.add(dsqlDistinct);
holder.add(dsqlSelectList);
holder.add(dsqlFirst);
holder.add(dsqlSkip);
}
}
virtual Firebird::string internalPrint(NodePrinter& printer) const;
virtual bool dsqlAggregateFinder(AggregateFinder& visitor);
virtual bool dsqlAggregate2Finder(Aggregate2Finder& visitor);
virtual bool dsqlInvalidReferenceFinder(InvalidReferenceFinder& visitor);
virtual bool dsqlSubSelectFinder(SubSelectFinder& visitor);
virtual bool dsqlFieldFinder(FieldFinder& visitor);
virtual RseNode* dsqlFieldRemapper(FieldRemapper& visitor);
virtual bool dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const;
virtual RseNode* dsqlPass(DsqlCompilerScratch* dsqlScratch);
virtual RseNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual RseNode* pass1(thread_db* tdbb, CompilerScratch* csb);
virtual void pass1Source(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
BoolExprNode** boolean, RecordSourceNodeStack& stack);
virtual RseNode* pass2(thread_db* /*tdbb*/, CompilerScratch* /*csb*/)
{
return this;
}
virtual void pass2Rse(thread_db* tdbb, CompilerScratch* csb);
virtual bool containsStream(StreamType checkStream) const;
virtual void computeDbKeyStreams(StreamList& streamList) const;
virtual void computeRseStreams(StreamList& streamList) const;
virtual bool computable(CompilerScratch* csb, StreamType stream,
bool allowOnlyCurrentStream, ValueExprNode* value);
virtual void findDependentFromStreams(const CompilerScratch* csb,
StreamType currentStream, SortedStreamList* streamList);
virtual void collectStreams(SortedStreamList& streamList) const;
virtual RecordSource* compile(thread_db* tdbb, Optimizer* opt, bool innerSubStream);
private:
void planCheck(thread_db* tdbb, const CompilerScratch* csb) const;
static void planSet(thread_db* tdbb, CompilerScratch* csb, PlanNode* plan);
RseNode* processPossibleJoins(thread_db* tdbb, CompilerScratch* csb);
public:
NestConst<ValueExprNode> dsqlFirst;
NestConst<ValueExprNode> dsqlSkip;
NestConst<ValueListNode> dsqlDistinct;
NestConst<ValueListNode> dsqlSelectList;
NestConst<RecSourceListNode> dsqlFrom;
NestConst<BoolExprNode> dsqlWhere;
NestConst<ValueListNode> dsqlJoinUsing;
NestConst<ValueListNode> dsqlGroup;
NestConst<BoolExprNode> dsqlHaving;
NestConst<ValueListNode> dsqlOrder;
NestConst<RecSourceListNode> dsqlStreams;
NamedWindowsClause* dsqlNamedWindows = nullptr;
bool dsqlExplicitJoin = false;
NestConst<ValueExprNode> rse_first;
NestConst<ValueExprNode> rse_skip;
NestConst<BoolExprNode> rse_boolean;
NestConst<SortNode> rse_sorted;
NestConst<SortNode> rse_projection;
NestConst<SortNode> rse_aggregate; // singleton aggregate for optimizing to index
NestConst<PlanNode> rse_plan; // user-specified access plan
NestConst<VarInvariantArray> rse_invariants; // Invariant nodes bound to top-level RSE
Firebird::Array<NestConst<RecordSourceNode> > rse_relations;
USHORT flags = 0;
UCHAR rse_jointype = INNER_JOIN;
Firebird::TriState firstRows; // optimize for first rows
};
class SelectExprNode final : public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_SELECT_EXPR>
{
public:
explicit SelectExprNode(MemoryPool& pool)
: TypedNode<RecordSourceNode, RecordSourceNode::TYPE_SELECT_EXPR>(pool),
querySpec(NULL),
orderClause(NULL),
rowsClause(NULL),
withClause(NULL),
alias(pool),
columns(NULL)
{
}
virtual Firebird::string internalPrint(NodePrinter& printer) const;
virtual RseNode* dsqlPass(DsqlCompilerScratch* dsqlScratch);
virtual bool dsqlSubSelectFinder(SubSelectFinder& visitor)
{
return true;
}
virtual RseNode* copy(thread_db* /*tdbb*/, NodeCopier& /*copier*/) const
{
fb_assert(false);
return NULL;
}
virtual RseNode* pass1(thread_db* /*tdbb*/, CompilerScratch* /*csb*/)
{
fb_assert(false);
return NULL;
}
virtual void pass1Source(thread_db* /*tdbb*/, CompilerScratch* /*csb*/, RseNode* /*rse*/,
BoolExprNode** /*boolean*/, RecordSourceNodeStack& /*stack*/)
{
fb_assert(false);
}
virtual RseNode* pass2(thread_db* /*tdbb*/, CompilerScratch* /*csb*/)
{
fb_assert(false);
return NULL;
}
virtual void pass2Rse(thread_db* /*tdbb*/, CompilerScratch* /*csb*/)
{
fb_assert(false);
}
virtual bool containsStream(StreamType /*checkStream*/) const
{
fb_assert(false);
return false;
}
virtual void computeDbKeyStreams(StreamList& /*streamList*/) const
{
fb_assert(false);
}
virtual RecordSource* compile(thread_db* /*tdbb*/, Optimizer* /*opt*/, bool /*innerSubStream*/)
{
fb_assert(false);
return NULL;
}
public:
NestConst<RecordSourceNode> querySpec;
NestConst<ValueListNode> orderClause;
NestConst<RowsClause> rowsClause;
NestConst<WithClause> withClause;
Firebird::string alias;
Firebird::ObjectsArray<MetaName>* columns;
};
class TableValueFunctionSourceNode
: public TypedNode<RecordSourceNode, RecordSourceNode::TYPE_TABLE_VALUE_FUNCTION>