-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
pass1.cpp
11553 lines (9777 loc) · 310 KB
/
pass1.cpp
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
/*
* PROGRAM: Dynamic SQL runtime support
* MODULE: pass1.cpp
* DESCRIPTION: First-pass compiler for request trees.
*
* 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): ______________________________________.
*
* 2001.5.26: Claudio Valderrama: field names should be skimmed from trailing
*
* 2001.5.26: Claudio Valderrama: COMPUTED fields will be skipped if a dummy
* "insert into tbl values(...)" sentence is issued.
*
* 2001.5.26: Claudio Valderrama: field names should be skimmed from trailing
* blanks to allow reliable comparisons in pass1_field. Same for table and
* and index names in plans.
*
* 2001.5.29: Claudio Valderrama: handle DROP VIEW case in pass1_statement().
*
* 2001.6.12: Claudio Valderrama: add basic BREAK capability to procedures.
*
* 2001.6.27: Claudio Valderrama: pass1_variable() now gives the name of the
* variable it can't find in the error message.
*
* 2001.6.30: Claudio Valderrama: Enhanced again to provide (line, col), see node.h.
*
* 2001.7.28: John Bellardo: added code to handle nod_limit and associated fields.
*
* 2001.08.14 Claudio Valderrama: fixed crash with trigger and CURRENT OF <cursor> syntax.
*
* 2001.09.10 John Bellardo: fixed gen_rse to attribute skip/first nodes to the parent_rse
* if present instead of the child rse. BUG #451798
*
* 2001.09.26 Claudio Valderrama: ambiguous field names are rejected from now.
*
* 2001.10.01 Claudio Valderrama: check constraints are allowed to have ambiguous field
* names because they use OLD and NEW as aliases of the same table. However, if the
* check constraint has an embedded ambiguous SELECT statement, it won't be detected.
* The code should be revisited if check constraints' before delete triggers are used
* for whatever reason. Currently they are never generated. The code can be improved
* to not report errors for fields between NEW and OLD contexts but complain otherwise.
*
* 2001.10.05 Neil McCalden: validate udf and parameters when comparing select list and
* group by list, to detect invalid SQL statements when grouping by UDFs.
*
* 2001.10.23 Ann Harrison: allow reasonable checking of ambiguous names in unions.
* Remembering, later, that LLS_PUSH expects an object, not an LLS block. Also
* stuck in the code for handling variables in pass1 - it apparently doesn't happen
* because the code returned an uninitialized pointer.
*
* 2001.11.17 Neil McCalden: Add aggregate_in_list procedure to handle cases
* where select statement has aggregate as a parameter to a udf which does
* not have to be in a group by clause.
*
* 2001.11.21 Claudio Valderrama: don't try to detect ambiguity in pass1_field()
* if the field or output procedure parameter has been fully qualified!!!
*
* 2001.11.27 Ann Harrison: Redo the amiguity checking so as to give better
* error messages, return warnings for dialect 1, and simplify.
*
* 2001.11.28 Claudio Valderrama: allow udf arguments to be query parameters.
* Honor the code in the parser that already accepts those parameters.
* This closes SF Bug# 409769.
*
* 2001.11.29 Claudio Valderrama: make the nice new ambiguity checking code do the
* right thing instead of crashing the engine and restore fix from 2001.11.21.
*
* 2001.12.21 Claudio Valderrama: Fix SF Bug #494832 - pass1_variable() should work
* with def_proc, mod_proc, redef_proc, def_trig and mod_trig node types.
*
* 2002.07.30 Arno Brinkman: Added pass1_coalesce, pass1_simple_case, pass1_searched_case
* and pass1_put_args_on_stack
*
* 2002.08.04 Arno Brinkman: Added ignore_cast as parameter to node_match,
* Changed invalid_reference procedure for allow EXTRACT, SUBSTRING, CASE,
* COALESCE and NULLIF functions in GROUP BY and as select_items.
* Removed aggregate_in_list procedure.
*
* 2002.08.07 Dmitry Yemanov: Disabled BREAK statement in triggers
*
* 2002.08.10 Dmitry Yemanov: ALTER VIEW
*
* 2002.09.28 Dmitry Yemanov: Reworked internal_info stuff, enhanced
* exception handling in SPs/triggers,
* implemented ROWS_AFFECTED system variable
*
* 2002.09.29 Arno Brinkman: Adding more checking for aggregate functions
* and adding support for 'linking' from sub-selects to aggregate functions
* which are in an lower level.
* Modified functions pass1_field, pass1_rse, copy_field, pass1_sort.
* Functions pass1_found_aggregate and pass1_found_field added.
*
* 2002.10.21 Nickolay Samofatov: Added support for explicit pessimistic locks
*
* 2002.10.25 Dmitry Yemanov: Re-allowed plans in triggers
*
* 2002.10.29 Nickolay Samofatov: Added support for savepoints
*
* 2002.12.03 Dmitry Yemanov: Implemented ORDER BY clause in subqueries
*
* 2002.12.18 Dmitry Yemanov: Fixed bug with BREAK and partially implemented
* SQL-compliant labels and LEAVE statement
*
* 2003.01.11 Arno Brinkman: Reworked a lot of functions for bringing back backwards compatibilty
* with sub-selects and aggregates.
*
* 2003.01.14 Dmitry Yemanov: Fixed bug with cursors in triggers
*
* 2003.01.15 Dmitry Yemanov: Added support for parametrized events
*
* 2003.04.05 Dmitry Yemanov: Changed logic of ORDER BY with collations
* (because of the parser change)
*
* 2003.08.14 Arno Brinkman: Added derived table support.
*
* 2003.08.16 Arno Brinkman: Changed ambiguous column name checking.
*
* 2003.10.05 Dmitry Yemanov: Added support for explicit cursors in PSQL.
*
* 2004.01.16 Vlad Horsun: added support for default parameters and
* EXECUTE BLOCK statement
*
* Adriano dos Santos Fernandes
*
*/
#include "firebird.h"
#include <string.h>
#include <stdio.h>
#include "../jrd/ibase.h"
#include "../dsql/dsql.h"
#include "../dsql/node.h"
#include "../jrd/intl.h"
#include "../jrd/blr.h"
#include "../jrd/jrd.h"
#include "../jrd/constants.h"
#include "../jrd/intl_classes.h"
#include "../dsql/ddl_proto.h"
#include "../dsql/errd_proto.h"
#include "../dsql/hsh_proto.h"
#include "../dsql/make_proto.h"
#include "../dsql/metd_proto.h"
#include "../dsql/misc_func.h"
#include "../dsql/pass1_proto.h"
#include "../dsql/utld_proto.h"
#include "../jrd/dsc_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/thread_proto.h"
#include "../jrd/why_proto.h"
#include "../jrd/SysFunction.h"
#include "../common/classes/array.h"
#include "../common/classes/auto.h"
#include "../common/utils_proto.h"
#include "../common/config/config.h"
using namespace Jrd;
using namespace Dsql;
#ifdef DEV_BUILD
static void DSQL_pretty(const dsql_nod*, int);
#endif
class CStrCmp
{
public:
static int greaterThan(const char* s1, const char* s2)
{
return strcmp(s1, s2) > 0;
}
};
typedef Firebird::SortedArray<const char*,
Firebird::EmptyStorage<const char*>, const char*,
Firebird::DefaultKeyValue<const char*>,
CStrCmp>
StrArray;
static bool aggregate_found(const dsql_req*, const dsql_nod*);
static bool aggregate_found2(const dsql_req*, const dsql_nod*, USHORT*,
USHORT*, bool);
static dsql_nod* ambiguity_check(dsql_req*, dsql_nod*, const dsql_str*,
const DsqlContextStack&);
static void assign_fld_dtype_from_dsc(dsql_fld*, const dsc*);
static void check_unique_fields_names(StrArray& names, const dsql_nod* fields);
static dsql_nod* compose(dsql_nod*, dsql_nod*, NOD_TYPE);
static dsql_nod* explode_fields(dsql_rel*);
static dsql_nod* explode_outputs(dsql_req*, const dsql_prc*);
static void field_appears_once(const dsql_nod*, const dsql_nod*, const bool, const char*);
static void field_duplication(const TEXT*, const TEXT*, const dsql_nod*, const char*);
static void field_unknown(const TEXT*, const TEXT*, const dsql_nod*);
static dsql_par* find_dbkey(const dsql_req*, const dsql_nod*);
static dsql_par* find_record_version(const dsql_req*, const dsql_nod*);
static dsql_ctx* get_context(const dsql_nod* node);
#ifdef NOT_USED_OR_REPLACED
static bool get_object_and_field(const dsql_nod* node,
const char** obj_name, const char** fld_name, bool do_collation);
#endif
static bool invalid_reference(const dsql_ctx*, const dsql_nod*,
const dsql_nod*, bool, bool);
static bool node_match(const dsql_nod*, const dsql_nod*, bool);
static dsql_nod* nullify_returning(dsql_nod* input, bool proc_flag);
static dsql_nod* pass1_alias_list(dsql_req*, dsql_nod*);
static dsql_ctx* pass1_alias(dsql_req*, DsqlContextStack&, dsql_str*);
static dsql_str* pass1_alias_concat(const dsql_str*, const dsql_str*);
static dsql_nod* pass1_any(dsql_req*, dsql_nod*, NOD_TYPE);
static dsql_rel* pass1_base_table(dsql_req*, const dsql_rel*, const dsql_str*);
static void pass1_blob(dsql_req*, dsql_nod*);
static dsql_nod* pass1_coalesce(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_collate(dsql_req*, dsql_nod*, const dsql_str*);
static dsql_nod* pass1_constant(dsql_req*, dsql_nod*);
static dsql_ctx* pass1_cursor_context(dsql_req*, const dsql_nod*, const dsql_nod*);
static dsql_nod* pass1_cursor_name(dsql_req*, const dsql_str*, USHORT, bool);
static dsql_nod* pass1_cursor_reference(dsql_req*, const dsql_nod*, dsql_nod*);
static dsql_nod* pass1_dbkey(dsql_req*, dsql_nod*);
static dsql_nod* pass1_delete(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_derived_table(dsql_req*, dsql_nod*, bool, dsql_str*);
static dsql_nod* pass1_expand_select_list(dsql_req*, dsql_nod*, dsql_nod*);
static void pass1_expand_select_node(dsql_req*, dsql_nod*, DsqlNodStack&, bool);
static dsql_nod* pass1_field(dsql_req*, dsql_nod*, const bool, dsql_nod*);
static bool pass1_found_aggregate(const dsql_nod*, USHORT, USHORT, bool);
static bool pass1_found_field(const dsql_nod*, USHORT, USHORT, bool*);
static bool pass1_found_sub_select(const dsql_nod*);
static dsql_nod* pass1_group_by_list(dsql_req*, dsql_nod*, dsql_nod*);
static dsql_nod* pass1_insert(dsql_req*, dsql_nod*, bool, bool);
static dsql_nod* pass1_join(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_label(dsql_req*, dsql_nod*);
static dsql_nod* pass1_lookup_alias(dsql_req*, const dsql_str*, dsql_nod*, bool);
static dsql_nod* pass1_make_derived_field(dsql_req*, thread_db*, dsql_nod*);
static dsql_nod* pass1_merge(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_not(dsql_req*, const dsql_nod*, bool, bool);
static void pass1_put_args_on_stack(dsql_req*, dsql_nod*, DsqlNodStack&, bool);
static dsql_nod* pass1_relation(dsql_req*, dsql_nod*);
static dsql_nod* pass1_returning(dsql_req*, const dsql_nod*, bool);
static dsql_nod* pass1_rse(dsql_req*, dsql_nod*, dsql_nod*, dsql_nod*, dsql_nod*, USHORT);
static dsql_nod* pass1_rse_impl(dsql_req*, dsql_nod*, dsql_nod*, dsql_nod*, dsql_nod*, USHORT);
static dsql_nod* pass1_searched_case(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_sel_list(dsql_req*, dsql_nod*);
static dsql_nod* pass1_simple_case(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_sort(dsql_req*, dsql_nod*, dsql_nod*);
static dsql_nod* pass1_sys_function(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_udf(dsql_req*, dsql_nod*, bool);
static void pass1_udf_args(dsql_req*, dsql_nod*, dsql_udf*, USHORT&, DsqlNodStack&,
bool);
static dsql_nod* pass1_union(dsql_req*, dsql_nod*, dsql_nod*, dsql_nod*, USHORT);
static void pass1_union_auto_cast(dsql_nod*, const dsc&, SSHORT,
bool in_select_list = false);
static dsql_nod* pass1_update(dsql_req*, dsql_nod*, bool, bool);
static dsql_nod* pass1_update_or_insert(dsql_req*, dsql_nod*, bool);
static dsql_nod* pass1_variable(dsql_req*, dsql_nod*);
static dsql_nod* post_map(dsql_nod*, dsql_ctx*);
static dsql_nod* remap_field(dsql_req*, dsql_nod*, dsql_ctx*, USHORT);
static dsql_nod* remap_fields(dsql_req*, dsql_nod*, dsql_ctx*);
static void remap_streams_to_parent_context(dsql_nod*, dsql_ctx*);
static dsql_fld* resolve_context(dsql_req*, const dsql_str*, dsql_ctx*, bool, bool);
static dsql_nod* resolve_using_field(dsql_req* request, dsql_str* name, DsqlNodStack& stack,
const dsql_nod* flawedNode, const TEXT* side, dsql_ctx*& ctx);
static dsql_nod* resolve_variable_name(const dsql_nod* var_nodes, const dsql_str* var_name);
static bool set_parameter_type(dsql_req*, dsql_nod*, dsql_nod*, bool);
static void set_parameters_name(dsql_nod*, const dsql_nod*);
static void set_parameter_name(dsql_nod*, const dsql_nod*, const dsql_rel*);
static dsql_nod* pass1_savepoint(const dsql_req*, dsql_nod*);
static bool pass1_relproc_is_recursive(dsql_req*, dsql_nod*);
static dsql_nod* pass1_join_is_recursive(dsql_req*, dsql_nod*&);
static bool pass1_rse_is_recursive(dsql_req*, dsql_nod*);
static dsql_nod* pass1_recursive_cte(dsql_req*, dsql_nod*, bool);
static dsql_nod* process_returning(dsql_req*, dsql_nod*, bool);
// CVC: more global variables???
static const dsql_str* global_temp_collation_name = NULL;
const char* DB_KEY_STRING = "DB_KEY"; // NTX: pseudo field name
const int MAX_MEMBER_LIST = 1500; // Maximum members in "IN" list.
// For eg. SELECT * FROM T WHERE
// F IN (1, 2, 3, ...)
//
// Bug 10061, bsriram - 19-Apr-1999
const int LIKE_PARAM_LEN = 30; // CVC: This is a guess for the length of the
// parameter for LIKE and others, when the
// original dtype isn't string and force_varchar
// is true.
enum field_match_val {
FIELD_MATCH_TYPE_EQUAL = 0,
FIELD_MATCH_TYPE_LOWER = 1,
FIELD_MATCH_TYPE_LOWER_EQUAL = 2,
FIELD_MATCH_TYPE_HIGHER = 3,
FIELD_MATCH_TYPE_HIGHER_EQUAL = 4
};
/**
PASS1_make_context
@brief Generate a context for a request.
@param request
@param relation_node
**/
dsql_ctx* PASS1_make_context(dsql_req* request, const dsql_nod* relation_node)
{
DEV_BLKCHK(request, dsql_type_req);
DEV_BLKCHK(relation_node, dsql_type_nod);
thread_db* const tdbb = JRD_get_thread_data();
dsql_rel* relation = NULL;
dsql_prc* procedure = NULL;
/* figure out whether this is a relation or a procedure
and give an error if it is neither */
dsql_str* relation_name;
switch (relation_node->nod_type)
{
case nod_rel_proc_name:
relation_name = (dsql_str*) relation_node->nod_arg[e_rpn_name];
break;
case nod_derived_table:
relation_name = (dsql_str*) relation_node->nod_arg[e_derived_table_alias];
break;
default:
relation_name = (dsql_str*) relation_node->nod_arg[e_rln_name];
break;
}
// CVC: Let's skim the context, too.
if (relation_name && relation_name->str_data) {
fb_utils::exact_name((TEXT*) relation_name->str_data);
}
DEV_BLKCHK(relation_name, dsql_type_str);
dsql_nod* cte = NULL;
if (relation_node->nod_type == nod_derived_table) {
// No processing needed here for derived tables.
}
else if ((relation_node->nod_type == nod_rel_proc_name) &&
relation_node->nod_arg[e_rpn_inputs])
{
procedure = METD_get_procedure(request, relation_name);
if (!procedure)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 204,
isc_arg_gds, isc_dsql_procedure_err,
isc_arg_gds, isc_random, isc_arg_string, relation_name->str_data,
isc_arg_gds, isc_dsql_line_col_error,
isc_arg_number, (int) relation_node->nod_line,
isc_arg_number, (int) relation_node->nod_column,
0);
}
}
else if ((cte = request->findCTE(relation_name)))
{
relation_node = cte;
}
else
{
relation = METD_get_relation(request, relation_name);
if (!relation && (relation_node->nod_type == nod_rel_proc_name))
{
procedure = METD_get_procedure(request, relation_name);
}
if (!relation && !procedure)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 204,
isc_arg_gds, isc_dsql_relation_err,
isc_arg_gds, isc_random, isc_arg_string, relation_name->str_data,
isc_arg_gds, isc_dsql_line_col_error,
isc_arg_number, (int) relation_node->nod_line,
isc_arg_number, (int) relation_node->nod_column,
0);
}
}
if (procedure && !procedure->prc_out_count) {
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 84,
isc_arg_gds, isc_dsql_procedure_use_err,
isc_arg_string, relation_name->str_data,
isc_arg_gds, isc_dsql_line_col_error,
isc_arg_number, (int) relation_node->nod_line,
isc_arg_number, (int) relation_node->nod_column,
0);
}
// Set up context block.
dsql_ctx* context = FB_NEW(*tdbb->getDefaultPool())
dsql_ctx(*tdbb->getDefaultPool());
context->ctx_relation = relation;
context->ctx_procedure = procedure;
context->ctx_request = request;
context->ctx_context = request->req_context_number++;
context->ctx_scope_level = request->req_scope_level;
// When we're in a outer-join part mark context for it.
if (request->req_in_outer_join) {
context->ctx_flags |= CTX_outer_join;
}
// find the context alias name, if it exists.
dsql_str* string;
if (relation_node->nod_type == nod_rel_proc_name) {
string = (dsql_str*) relation_node->nod_arg[e_rpn_alias];
}
else if (relation_node->nod_type == nod_derived_table) {
string = (dsql_str*) relation_node->nod_arg[e_derived_table_alias];
context->ctx_rse = relation_node->nod_arg[e_derived_table_rse];
}
else {
string = (dsql_str*) relation_node->nod_arg[e_rln_alias];
}
if (string)
{
context->ctx_internal_alias = (TEXT*) string->str_data;
}
DEV_BLKCHK(string, dsql_type_str);
if (request->req_alias_relation_prefix && !(relation_node->nod_type == nod_derived_table))
{
if (string) {
string = pass1_alias_concat(request->req_alias_relation_prefix, string);
}
else {
string = pass1_alias_concat(request->req_alias_relation_prefix, relation_name);
}
}
if (string) {
context->ctx_alias = (TEXT*) string->str_data;
// check to make sure the context is not already used at this same
// query level (if there are no subqueries, this checks that the
// alias is not used twice in the request).
for (DsqlContextStack::iterator stack(*request->req_context);
stack.hasData(); ++stack)
{
const dsql_ctx* conflict = stack.object();
if (conflict->ctx_scope_level != context->ctx_scope_level) {
continue;
}
const TEXT* conflict_name;
ISC_STATUS error_code;
if (conflict->ctx_alias) {
conflict_name = conflict->ctx_alias;
error_code = isc_alias_conflict_err;
// alias %s conflicts with an alias in the same statement.
}
else if (conflict->ctx_procedure) {
conflict_name = conflict->ctx_procedure->prc_name.c_str();
error_code = isc_procedure_conflict_error;
// alias %s conflicts with a procedure in the same statement.
}
else if (conflict->ctx_relation) {
conflict_name = conflict->ctx_relation->rel_name.c_str();
error_code = isc_relation_conflict_err;
// alias %s conflicts with a relation in the same statement.
}
else {
continue;
}
if (!strcmp(conflict_name, context->ctx_alias)) {
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 204,
isc_arg_gds, error_code,
isc_arg_string, conflict_name, 0);
}
}
}
if (procedure) {
USHORT count = 0;
if (relation_node->nod_arg[e_rpn_inputs])
{
context->ctx_proc_inputs =
PASS1_node(request, relation_node->nod_arg[e_rpn_inputs], false);
count = context->ctx_proc_inputs->nod_count;
}
if (!(request->req_flags & REQ_procedure))
{
if (count > procedure->prc_in_count ||
count < procedure->prc_in_count - procedure->prc_def_count)
{
ERRD_post(isc_prcmismat, isc_arg_string,
relation_name->str_data, 0);
}
if (count)
{
// Initialize this stack variable, and make it look like a node
Firebird::AutoPtr<dsql_nod> desc_node(FB_NEW_RPT(*tdbb->getDefaultPool(), 0) dsql_nod);
dsql_nod* const* input = context->ctx_proc_inputs->nod_arg;
for (dsql_fld* field = procedure->prc_inputs;
*input; input++, field = field->fld_next)
{
DEV_BLKCHK(field, dsql_type_fld);
DEV_BLKCHK(*input, dsql_type_nod);
MAKE_desc_from_field(&desc_node->nod_desc, field);
// set_parameter_type(request, *input, &desc_node, false);
set_parameter_type(request, *input, desc_node, false);
}
}
}
}
// push the context onto the request context stack
// for matching fields against
request->req_context->push(context);
return context;
}
/**
PASS1_node
@brief Compile a parsed request into something more interesting.
@param request
@param input
@param proc_flag
**/
dsql_nod* PASS1_node(dsql_req* request, dsql_nod* input, bool proc_flag)
{
DEV_BLKCHK(request, dsql_type_req);
DEV_BLKCHK(input, dsql_type_nod);
if (input == NULL) {
return NULL;
}
dsql_nod* node;
dsql_fld* field;
dsql_nod* sub1;
// Dispatch on node type. Fall thru on easy ones
switch (input->nod_type)
{
case nod_alias:
node = MAKE_node(input->nod_type, e_alias_count);
node->nod_arg[e_alias_value] = sub1 =
PASS1_node(request, input->nod_arg[e_alias_value], proc_flag);
node->nod_arg[e_alias_alias] = input->nod_arg[e_alias_alias];
node->nod_desc = sub1->nod_desc;
return node;
case nod_cast:
node = MAKE_node(input->nod_type, e_cast_count);
node->nod_arg[e_cast_source] = sub1 =
PASS1_node(request, input->nod_arg[e_cast_source], proc_flag);
node->nod_arg[e_cast_target] = input->nod_arg[e_cast_target];
field = (dsql_fld*) node->nod_arg[e_cast_target];
DEV_BLKCHK(field, dsql_type_fld);
DDL_resolve_intl_type(request, field, NULL);
MAKE_desc_from_field(&node->nod_desc, field);
set_parameter_type(request, node, NULL, false);
// If the source is nullable, so is the target
MAKE_desc(request, &sub1->nod_desc, sub1, NULL);
if (sub1->nod_desc.dsc_flags & DSC_nullable)
node->nod_desc.dsc_flags |= DSC_nullable;
return node;
case nod_coalesce:
return pass1_coalesce(request, input, proc_flag);
case nod_derived_field:
return input;
case nod_simple_case:
return pass1_simple_case(request, input, proc_flag);
case nod_searched_case:
return pass1_searched_case(request, input, proc_flag);
case nod_gen_id:
case nod_gen_id2:
node = MAKE_node(input->nod_type, e_gen_id_count);
node->nod_arg[e_gen_id_value] =
PASS1_node(request, input->nod_arg[e_gen_id_value], proc_flag);
node->nod_arg[e_gen_id_name] = input->nod_arg[e_gen_id_name];
return node;
case nod_collate:
global_temp_collation_name = (dsql_str*) input->nod_arg[e_coll_target];
sub1 = PASS1_node(request, input->nod_arg[e_coll_source], proc_flag);
global_temp_collation_name = NULL;
node =
pass1_collate(request, sub1, (dsql_str*) input->nod_arg[e_coll_target]);
return node;
case nod_extract:
// Figure out the data type of the sub parameter, and make
// sure the requested type of information can be extracted
sub1 = PASS1_node(request, input->nod_arg[e_extract_value], proc_flag);
MAKE_desc(request, &sub1->nod_desc, sub1, NULL);
switch (input->nod_arg[e_extract_part]->getSlong())
{
case blr_extract_year:
case blr_extract_month:
case blr_extract_day:
case blr_extract_weekday:
case blr_extract_yearday:
case blr_extract_week:
if (sub1->nod_type != nod_null &&
sub1->nod_desc.dsc_dtype != dtype_sql_date &&
sub1->nod_desc.dsc_dtype != dtype_timestamp)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 105,
isc_arg_gds, isc_extract_input_mismatch, 0);
}
break;
case blr_extract_hour:
case blr_extract_minute:
case blr_extract_second:
case blr_extract_millisecond:
if (sub1->nod_type != nod_null &&
sub1->nod_desc.dsc_dtype != dtype_sql_time &&
sub1->nod_desc.dsc_dtype != dtype_timestamp)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 105,
isc_arg_gds, isc_extract_input_mismatch, 0);
}
break;
default:
fb_assert(false);
break;
}
node = MAKE_node(input->nod_type, e_extract_count);
node->nod_arg[e_extract_part] = input->nod_arg[e_extract_part];
node->nod_arg[e_extract_value] = sub1;
if (sub1->nod_desc.dsc_flags & DSC_nullable) {
node->nod_desc.dsc_flags |= DSC_nullable;
}
return node;
case nod_strlen:
node = MAKE_node(input->nod_type, e_strlen_count);
node->nod_arg[e_strlen_type] = input->nod_arg[e_strlen_type];
node->nod_arg[e_strlen_value] =
PASS1_node(request, input->nod_arg[e_strlen_value], proc_flag);
if (node->nod_arg[e_strlen_value]->nod_desc.dsc_flags & DSC_nullable)
node->nod_desc.dsc_flags |= DSC_nullable;
return node;
case nod_delete:
case nod_insert:
case nod_merge:
case nod_update_or_insert:
case nod_order:
case nod_select:
case nod_with:
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 104,
isc_arg_gds, isc_dsql_command_err, 0);
case nod_derived_table:
return pass1_derived_table(request, input, proc_flag, NULL);
case nod_select_expr:
{
if (proc_flag)
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 206,
isc_arg_gds, isc_dsql_subselect_err, 0);
const DsqlContextStack::iterator base(*request->req_context);
node = MAKE_node(nod_via, e_via_count);
dsql_nod* rse = PASS1_rse(request, input, NULL);
node->nod_arg[e_via_rse] = rse;
node->nod_arg[e_via_value_1] = rse->nod_arg[e_rse_items]->nod_arg[0];
node->nod_arg[e_via_value_2] = MAKE_node(nod_null, (int) 0);
// Finish off by cleaning up contexts
request->req_context->clear(base);
return node;
}
case nod_exists:
case nod_singular:
{
const DsqlContextStack::iterator base(*request->req_context);
node = MAKE_node(input->nod_type, 1);
input = input->nod_arg[0];
node->nod_arg[0] = PASS1_rse(request, input, NULL);
// Finish off by cleaning up contexts
request->req_context->clear(base);
return node;
}
case nod_field_name:
if (proc_flag)
return pass1_variable(request, input);
else
return pass1_field(request, input, false, NULL);
case nod_field:
// AB: nod_field is an already passed node.
// This could be done in expand_select_list.
return input;
case nod_array:
if (proc_flag)
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 104,
isc_arg_gds, isc_dsql_invalid_array, 0);
else
return pass1_field(request, input, false, NULL);
case nod_variable:
node = MAKE_node (input->nod_type, e_var_count);
node->nod_arg[0] = input->nod_arg[0];
node->nod_desc = input->nod_desc;
return node;
case nod_var_name:
return pass1_variable(request, input);
case nod_dbkey:
return pass1_dbkey(request, input);
case nod_relation_name:
case nod_rel_proc_name:
{
dsql_str* rel_name;
dsql_str* rel_alias;
if (input->nod_type == nod_rel_proc_name) {
rel_name = (dsql_str*) input->nod_arg[e_rpn_name];
rel_alias = (dsql_str*) input->nod_arg[e_rpn_alias];
}
else { // nod_relation_name
rel_name = (dsql_str*) input->nod_arg[e_rln_name];
rel_alias = (dsql_str*) input->nod_arg[e_rln_alias];
}
if (!rel_alias) {
rel_alias = rel_name;
}
dsql_nod* cte = request->findCTE(rel_name);
if (cte)
{
cte->nod_flags |= NOD_DT_CTE_USED;
if ((request->req_flags & REQ_CTE_recursive) &&
request->req_curr_ctes.hasData() &&
(request->req_curr_ctes.object() == cte))
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 104,
isc_arg_gds, isc_dsql_cte_wrong_reference, // Recursive CTE member (%s) can refer itself only in FROM clause
isc_arg_string, rel_name->str_data,
0);
}
for (DsqlNodStack::const_iterator stack(request->req_curr_ctes); stack.hasData(); ++stack)
{
dsql_nod* cte1 = stack.object();
if (cte1 == cte) {
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 104,
isc_arg_gds, isc_dsql_cte_cycle, // CTE %s has cyclic dependencies
isc_arg_string, rel_name->str_data,
0);
}
}
dsql_nod* const select_expr = cte->nod_arg[e_derived_table_rse];
dsql_nod* const query = select_expr->nod_arg[e_sel_query_spec];
const bool isRecursive = (query->nod_type == nod_list) &&
(query->nod_flags & NOD_UNION_RECURSIVE);
dsql_str* cte_name = (dsql_str*) cte->nod_arg[e_derived_table_alias];
if (!isRecursive) {
cte->nod_arg[e_derived_table_alias] = (dsql_nod*) rel_alias;
}
request->req_curr_ctes.push(cte);
dsql_nod* derived_node = pass1_derived_table(request, cte, proc_flag,
isRecursive ? rel_alias : NULL);
if (!isRecursive) {
cte->nod_arg[e_derived_table_alias] = (dsql_nod*) cte_name;
}
request->req_curr_ctes.pop();
return derived_node;
}
return pass1_relation(request, input);
}
case nod_constant:
return pass1_constant(request, input);
case nod_parameter:
if (proc_flag)
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 104,
isc_arg_gds, isc_dsql_command_err, 0);
node = MAKE_node(input->nod_type, e_par_count);
node->nod_count = 0;
node->nod_arg[e_par_parameter] = (dsql_nod*)
MAKE_parameter(
(input->nod_arg[e_par_parameter] ?
(dsql_msg*) ((dsql_par*) input->nod_arg[e_par_parameter])->par_message :
request->req_send),
true, true,
// Pass 0 here to restore older parameter
// ordering behavior unconditionally.
(USHORT)(IPTR) input->nod_arg[e_par_index],
NULL);
node->nod_arg[e_par_index] = (dsql_nod*) (IPTR) ((dsql_par*) node->nod_arg[e_par_parameter])->par_index;
return node;
case nod_param_val:
node = MAKE_node(input->nod_type, e_prm_val_count);
node->nod_arg[e_prm_val_fld] = input->nod_arg[e_prm_val_fld];
node->nod_arg[e_prm_val_val] = PASS1_node(request, input->nod_arg[e_prm_val_val], proc_flag);
field = (dsql_fld*) node->nod_arg[e_prm_val_fld]->nod_arg[e_dfl_field];
DDL_resolve_intl_type(request, field, NULL);
{ // scope
dsql_nod *temp = node->nod_arg[e_prm_val_val];
// Initialize this stack variable, and make it look like a node
Firebird::AutoPtr<dsql_nod> desc_node(FB_NEW_RPT(*getDefaultMemoryPool(), 0) dsql_nod);
DEV_BLKCHK(field, dsql_type_fld);
DEV_BLKCHK(temp, dsql_type_nod);
field->fld_flags |= FLD_nullable;
MAKE_desc_from_field(&(desc_node->nod_desc), field);
set_parameter_type(request, temp, desc_node, false);
} // end scope
return node;
case nod_udf:
return pass1_udf(request, input, proc_flag);
case nod_sys_function:
return pass1_sys_function(request, input, proc_flag);
case nod_equiv:
case nod_eql:
case nod_neq:
case nod_gtr:
case nod_geq:
case nod_lss:
case nod_leq:
case nod_eql_any:
case nod_neq_any:
case nod_gtr_any:
case nod_geq_any:
case nod_lss_any:
case nod_leq_any:
case nod_eql_all:
case nod_neq_all:
case nod_gtr_all:
case nod_geq_all:
case nod_lss_all:
case nod_leq_all:
{
dsql_nod* sub2 = input->nod_arg[1];
if (sub2->nod_type == nod_list) {
int list_item_count = 0;
node = NULL;
dsql_nod** ptr = sub2->nod_arg;
for (const dsql_nod* const* const end = ptr + sub2->nod_count;
ptr < end && list_item_count < MAX_MEMBER_LIST;
list_item_count++, ptr++)
{
DEV_BLKCHK(*ptr, dsql_type_nod);
dsql_nod* temp = MAKE_node(input->nod_type, 2);
temp->nod_arg[0] = input->nod_arg[0];
temp->nod_arg[1] = *ptr;
node = compose(node, temp, nod_or);
}
if (list_item_count >= MAX_MEMBER_LIST)
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -901,
isc_arg_gds, isc_imp_exc,
isc_arg_gds, isc_dsql_too_many_values,
isc_arg_number, MAX_MEMBER_LIST,
0);
return PASS1_node(request, node, proc_flag);
}
if (sub2->nod_type == nod_select_expr)
{
if (proc_flag)
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 206,
isc_arg_gds, isc_dsql_subselect_err, 0);
if (sub2->nod_flags & NOD_SELECT_EXPR_SINGLETON) {
const DsqlContextStack::iterator base(*request->req_context);
node = MAKE_node(input->nod_type, 2);
node->nod_arg[0] = PASS1_node(request, input->nod_arg[0], false);
dsql_nod* temp = MAKE_node(nod_via, e_via_count);
node->nod_arg[1] = temp;
dsql_nod* rse = PASS1_rse(request, sub2, NULL);
temp->nod_arg[e_via_rse] = rse;
temp->nod_arg[e_via_value_1] =
rse->nod_arg[e_rse_items]->nod_arg[0];
temp->nod_arg[e_via_value_2] = MAKE_node(nod_null, (int) 0);
// Try to force sub1 to be same type as sub2 eg: ? = (select ...) case
sub1 = node->nod_arg[0];
sub2 = node->nod_arg[1];
set_parameter_type(request, sub1, sub2, false);
// Finish off by cleaning up contexts
request->req_context->clear(base);
return node;
}
else {
switch (input->nod_type)
{
case nod_equiv:
case nod_eql:
case nod_neq:
case nod_gtr:
case nod_geq:
case nod_lss:
case nod_leq:
return pass1_any(request, input, nod_any);
case nod_eql_any:
case nod_neq_any:
case nod_gtr_any:
case nod_geq_any:
case nod_lss_any:
case nod_leq_any:
return pass1_any(request, input, nod_ansi_any);
case nod_eql_all:
case nod_neq_all:
case nod_gtr_all:
case nod_geq_all:
case nod_lss_all:
case nod_leq_all:
return pass1_any(request, input, nod_ansi_all);
default: // make compiler happy
break;
}
}
}
}
break;
case nod_agg_count:
case nod_agg_min:
case nod_agg_max:
case nod_agg_average:
case nod_agg_total:
case nod_agg_average2:
case nod_agg_total2:
case nod_agg_list:
if (proc_flag) {
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 104,
isc_arg_gds, isc_dsql_command_err, 0);
}
if (!(request->req_in_select_list || request->req_in_where_clause ||
request->req_in_group_by_clause || request->req_in_having_clause ||
request->req_in_order_by_clause))
{
/* not part of a select list, where clause, group by clause,
having clause, or order by clause */
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) - 104,
isc_arg_gds, isc_dsql_agg_ref_err, 0);