-
Notifications
You must be signed in to change notification settings - Fork 121
/
query_executor.c
25215 lines (22210 loc) · 710 KB
/
query_executor.c
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
/*
* Copyright 2008 Search Solution Corporation
* Copyright 2016 CUBRID Corporation
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* query_executor.c - Query evaluator module
*/
#ident "$Id$"
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <search.h>
#include <sys/timeb.h>
#include "query_executor.h"
#include "binaryheap.h"
#include "porting.h"
#include "error_manager.h"
#include "partition_sr.h"
#include "query_aggregate.hpp"
#include "query_analytic.hpp"
#include "query_opfunc.h"
#include "fetch.h"
#include "dbtype.h"
#include "object_primitive.h"
#include "object_representation.h"
#include "list_file.h"
#include "extendible_hash.h"
#include "xasl_cache.h"
#include "stream_to_xasl.h"
#include "query_manager.h"
#include "query_reevaluation.hpp"
#include "extendible_hash.h"
#include "replication.h"
#include "elo.h"
#include "db_elo.h"
#include "locator_sr.h"
#include "log_lsa.hpp"
#include "log_volids.hpp"
#include "xserver_interface.h"
#include "tz_support.h"
#include "session.h"
#include "tz_support.h"
#include "db_date.h"
#include "btree_load.h"
#include "query_dump.h"
#if defined (SERVER_MODE)
#include "jansson.h"
#endif /* defined (SERVER_MODE) */
#if defined(ENABLE_SYSTEMTAP)
#include "probes.h"
#endif /* ENABLE_SYSTEMTAP */
#include "db_json.hpp"
#include "dbtype.h"
#include "string_regex.hpp"
#include "thread_entry.hpp"
#include "regu_var.hpp"
#include "xasl.h"
#include "xasl_aggregate.hpp"
#include "xasl_analytic.hpp"
#include "xasl_predicate.hpp"
#include <vector>
// XASL_STATE
typedef struct xasl_state XASL_STATE;
struct xasl_state
{
VAL_DESCR vd; /* Value Descriptor */
QUERY_ID query_id; /* Query associated with XASL */
int qp_xasl_line; /* Error line */
};
#define GOTO_EXIT_ON_ERROR \
do \
{ \
qexec_failure_line (__LINE__, xasl_state); \
goto exit_on_error; \
} \
while (0)
/* Page buffer int keep free ratios: for the cases of
* client/server - minimal page buffer pool
* client/server - large page buffer pool
* standalone - minimal page buffer pool
* standalone - large page buffer pool
* respectively,
*/
/* Keep the scans grouped only for that many classes scans for join queries.*/
#define QPROC_MAX_GROUPED_SCAN_CNT (4)
/* if 1, single class query scans are done in a grouped manner. */
#define QPROC_SINGLE_CLASS_GROUPED_SCAN (0)
/* used for tuple string id */
#define CONNECTBY_TUPLE_INDEX_STRING_MEM 64
/* default number of hash entries */
#define HASH_AGGREGATE_DEFAULT_TABLE_SIZE 1000
/* minimum amount of tuples that have to be hashed before deciding if
selectivity is very high */
#define HASH_AGGREGATE_VH_SELECTIVITY_TUPLE_THRESHOLD 200
/* maximum selectivity allowed for hash aggregate evaluation */
#define HASH_AGGREGATE_VH_SELECTIVITY_THRESHOLD 0.5f
#define QEXEC_CLEAR_AGG_LIST_VALUE(agg_list) \
do \
{ \
AGGREGATE_TYPE *agg_ptr; \
for (agg_ptr = (agg_list); agg_ptr; agg_ptr = agg_ptr->next) \
{ \
if (agg_ptr->function == PT_GROUPBY_NUM) \
continue; \
pr_clear_value (agg_ptr->accumulator.value); \
} \
} \
while (0)
#define QEXEC_EMPTY_ACCESS_SPEC_SCAN(specp) \
((specp)->type == TARGET_CLASS \
&& ((ACCESS_SPEC_HFID((specp)).vfid.fileid == NULL_FILEID || ACCESS_SPEC_HFID((specp)).vfid.volid == NULL_VOLID)))
#define QEXEC_IS_MULTI_TABLE_UPDATE_DELETE(xasl) \
(xasl->upd_del_class_cnt > 1 || (xasl->upd_del_class_cnt == 1 && xasl->scan_ptr != NULL))
#define QEXEC_SEL_UPD_USE_REEVALUATION(xasl) \
((xasl) && ((xasl)->spec_list) && ((xasl)->spec_list->next == NULL) \
&& ((xasl)->spec_list->pruning_type == DB_NOT_PARTITIONED_CLASS) \
&& ((xasl)->aptr_list == NULL) && ((xasl)->scan_ptr == NULL))
#if 0
/* Note: the following macro is used just for replacement of a repetitive
* text in order to improve the readability.
*/
#if !defined(SERVER_MODE)
#define pthread_mutex_init(a, b)
#define pthread_mutex_destroy(a)
#define pthread_mutex_lock(a) 0
#define pthread_mutex_unlock(a)
#endif
#endif
/* XASL scan block function */
typedef SCAN_CODE (*XSAL_SCAN_FUNC) (THREAD_ENTRY * thread_p, XASL_NODE *, XASL_STATE *, QFILE_TUPLE_RECORD *, void *);
/* pointer to XASL scan function */
typedef XSAL_SCAN_FUNC *XASL_SCAN_FNC_PTR;
enum groupby_dimension_flag
{
GROUPBY_DIM_FLAG_NONE = 0,
GROUPBY_DIM_FLAG_GROUP_BY = 1,
GROUPBY_DIM_FLAG_ROLLUP = 2,
GROUPBY_DIM_FLAG_CUBE = 4,
GROUPBY_DIM_FLAG_SET = 8
};
typedef enum groupby_dimension_flag GROUPBY_DIMENSION_FLAG;
typedef struct groupby_dimension GROUPBY_DIMENSION;
struct groupby_dimension
{
GROUPBY_DIMENSION_FLAG d_flag; /* dimension info */
AGGREGATE_TYPE *d_agg_list; /* aggregation colunms list */
};
typedef struct groupby_state GROUPBY_STATE;
struct groupby_state
{
int state;
SORTKEY_INFO key_info;
QFILE_LIST_SCAN_ID *input_scan;
#if 0 /* SortCache */
VPID fixed_vpid; /* current fixed page info of */
PAGE_PTR fixed_page; /* input list file */
#endif
QFILE_LIST_ID *output_file;
PRED_EXPR *having_pred;
PRED_EXPR *grbynum_pred;
DB_VALUE *grbynum_val;
int grbynum_flag;
XASL_NODE *eptr_list;
AGGREGATE_TYPE *g_output_agg_list;
REGU_VARIABLE_LIST g_regu_list;
REGU_VARIABLE_LIST g_hk_regu_list;
VAL_LIST *g_val_list;
OUTPTR_LIST *g_outptr_list;
XASL_NODE *xasl;
XASL_STATE *xasl_state;
RECDES current_key;
RECDES gby_rec;
QFILE_TUPLE_RECORD input_tpl;
QFILE_TUPLE_RECORD *output_tplrec;
int input_recs;
bool with_rollup;
GROUPBY_DIMENSION *g_dim; /* dimensions for Data Cube */
int g_dim_levels; /* dimensions size */
int hash_eligible;
AGGREGATE_HASH_CONTEXT *agg_hash_context;
SORT_CMP_FUNC *cmp_fn;
LK_COMPOSITE_LOCK *composite_lock;
int upd_del_class_cnt;
};
typedef struct analytic_function_state ANALYTIC_FUNCTION_STATE;
struct analytic_function_state
{
ANALYTIC_TYPE *func_p;
RECDES current_key;
/* result list files */
QFILE_LIST_ID *group_list_id; /* file containing group headers */
QFILE_LIST_ID *value_list_id; /* file containing group values */
QFILE_LIST_SCAN_ID group_scan_id; /* scan on group_list_id */
QFILE_LIST_SCAN_ID value_scan_id; /* scan on value_list_id */
QFILE_TUPLE_RECORD group_tplrec;
QFILE_TUPLE_RECORD value_tplrec;
DB_VALUE cgtc_dbval; /* linked to curr_group_tuple_count */
DB_VALUE cgtc_nn_dbval; /* linked to curr_group_tuple_count_nn */
DB_VALUE csktc_dbval; /* linked to curr_sort_key_tuple_count */
int curr_group_tuple_count; /* tuples in current group */
int curr_group_tuple_count_nn; /* tuples in current group with non-NULL values */
int curr_sort_key_tuple_count; /* tuples sharing current sort key */
int group_tuple_position; /* position of value_scan_id in current group */
int group_tuple_position_nn; /* position of value_scan_id in current group, ignoring NULL values */
int sort_key_tuple_position; /* position of value_scan_id in current sort key */
int group_consumed_tuples; /* number of consumed tuples from current group */
};
typedef struct analytic_state ANALYTIC_STATE;
struct analytic_state
{
int state;
int func_count;
SORTKEY_INFO key_info;
SORT_CMP_FUNC *cmp_fn;
XASL_NODE *xasl;
XASL_STATE *xasl_state;
ANALYTIC_FUNCTION_STATE *func_state_list;
REGU_VARIABLE_LIST a_regu_list;
OUTPTR_LIST *a_outptr_list_interm;
OUTPTR_LIST *a_outptr_list;
QFILE_LIST_SCAN_ID *input_scan;
QFILE_LIST_SCAN_ID *interm_scan;
QFILE_LIST_ID *interm_file;
QFILE_LIST_ID *output_file;
RECDES analytic_rec;
QFILE_TUPLE_RECORD input_tplrec;
QFILE_TUPLE_RECORD *output_tplrec;
struct
{
VPID vpid;
PAGE_PTR page_p;
} curr_sort_page;
int input_recs;
bool is_last_run;
bool is_output_rec;
};
/*
* Information required for processing the ORDBY_NUM() function. See
* qexec_eval_ordbynum_pred ().
*/
typedef struct ordbynum_info ORDBYNUM_INFO;
struct ordbynum_info
{
XASL_STATE *xasl_state;
PRED_EXPR *ordbynum_pred;
DB_VALUE *ordbynum_val;
int ordbynum_flag;
int ordbynum_pos_cnt;
int *ordbynum_pos;
int reserved[2];
};
/* parent pos info stack */
typedef struct parent_pos_info PARENT_POS_INFO;
struct parent_pos_info
{
QFILE_TUPLE_POSITION tpl_pos;
PARENT_POS_INFO *stack;
};
/* used for deleting lob files */
typedef struct del_lob_info DEL_LOB_INFO;
struct del_lob_info
{
OID *class_oid; /* OID of the class that has lob attributes */
HFID *class_hfid; /* class hfid */
HEAP_CACHE_ATTRINFO attr_info; /* attribute cache info */
DEL_LOB_INFO *next; /* next DEL_LOB_INFO in a list */
};
/* used for internal update/delete execution */
typedef struct upddel_class_info_internal UPDDEL_CLASS_INFO_INTERNAL;
struct upddel_class_info_internal
{
int subclass_idx; /* active subclass index */
OID *oid; /* instance oid of current class */
OID *class_oid; /* oid of current class */
HFID *class_hfid; /* hfid of current class */
HEAP_SCANCACHE *scan_cache;
OID prev_class_oid; /* previous class oid */
HEAP_CACHE_ATTRINFO attr_info; /* attribute cache info */
bool is_attr_info_inited; /* true if attr_info has valid data */
int needs_pruning; /* partition pruning information */
PRUNING_CONTEXT context; /* partition pruning context */
int num_lob_attrs; /* number of lob attributes */
int *lob_attr_ids; /* lob attribute ids */
DEL_LOB_INFO *crt_del_lob_info; /* DEL_LOB_INFO for current class_oid */
multi_index_unique_stats m_unique_stats;
HEAP_SCANCACHE m_scancache;
bool m_inited_scancache;
int extra_assign_reev_cnt; /* size of mvcc_extra_assign_reev in elements */
UPDDEL_MVCC_COND_REEVAL **mvcc_extra_assign_reev; /* classes in the select list that are referenced in
* assignments to the attributes of current class and are not
* referenced in conditions */
UPDATE_MVCC_REEV_ASSIGNMENT *mvcc_reev_assigns;
};
enum analytic_stage
{
ANALYTIC_INTERM_PROC = 1,
ANALYTIC_GROUP_PROC
};
typedef enum analytic_stage ANALYTIC_STAGE;
#define QEXEC_GET_BH_TOPN_TUPLE(heap, index) (*(TOPN_TUPLE **) BH_ELEMENT (heap, index))
typedef enum
{
TOPN_SUCCESS,
TOPN_OVERFLOW,
TOPN_FAILURE
} TOPN_STATUS;
static DB_LOGICAL qexec_eval_instnum_pred (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_add_composite_lock (THREAD_ENTRY * thread_p, REGU_VARIABLE_LIST reg_var_list, XASL_STATE * xasl_state,
LK_COMPOSITE_LOCK * composite_lock, int upd_del_cls_cnt, OID * default_cls_oid);
static QPROC_TPLDESCR_STATUS qexec_generate_tuple_descriptor (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id,
VALPTR_LIST * outptr_list, VAL_DESCR * vd);
static int qexec_upddel_add_unique_oid_to_ehid (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_end_one_iteration (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static void qexec_failure_line (int line, XASL_STATE * xasl_state);
static void qexec_reset_regu_variable (REGU_VARIABLE * var);
static void qexec_reset_regu_variable_list (REGU_VARIABLE_LIST list);
static void qexec_reset_pred_expr (PRED_EXPR * pred);
static int qexec_clear_xasl_head (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static int qexec_clear_arith_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, ARITH_TYPE * list, bool is_final);
static int qexec_clear_regu_var (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, REGU_VARIABLE * regu_var, bool is_final);
static int qexec_clear_regu_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, REGU_VARIABLE_LIST list, bool is_final);
static int qexec_clear_regu_value_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, REGU_VALUE_LIST * list,
bool is_final);
static void qexec_clear_db_val_list (QPROC_DB_VALUE_LIST list);
static void qexec_clear_sort_list (XASL_NODE * xasl_p, SORT_LIST * list, bool is_final);
static void qexec_clear_pos_desc (XASL_NODE * xasl_p, QFILE_TUPLE_VALUE_POSITION * position_descr, bool is_final);
static int qexec_clear_pred (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, PRED_EXPR * pr, bool is_final);
static int qexec_clear_access_spec_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, ACCESS_SPEC_TYPE * list,
bool is_final);
static int qexec_clear_analytic_function_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, ANALYTIC_EVAL_TYPE * list,
bool is_final);
static int qexec_clear_agg_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, AGGREGATE_TYPE * list, bool is_final);
static void qexec_clear_head_lists (THREAD_ENTRY * thread_p, XASL_NODE * xasl_list);
static void qexec_clear_scan_all_lists (THREAD_ENTRY * thread_p, XASL_NODE * xasl_list);
static void qexec_clear_all_lists (THREAD_ENTRY * thread_p, XASL_NODE * xasl_list);
static int qexec_clear_update_assignment (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, UPDATE_ASSIGNMENT * assignment,
bool is_final);
static DB_LOGICAL qexec_eval_ordbynum_pred (THREAD_ENTRY * thread_p, ORDBYNUM_INFO * ordby_info);
static int qexec_ordby_put_next (THREAD_ENTRY * thread_p, const RECDES * recdes, void *arg);
static int qexec_orderby_distinct (THREAD_ENTRY * thread_p, XASL_NODE * xasl, QUERY_OPTIONS option,
XASL_STATE * xasl_state);
static int qexec_orderby_distinct_by_sorting (THREAD_ENTRY * thread_p, XASL_NODE * xasl, QUERY_OPTIONS option,
XASL_STATE * xasl_state);
static DB_LOGICAL qexec_eval_grbynum_pred (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate);
static GROUPBY_STATE *qexec_initialize_groupby_state (GROUPBY_STATE * gbstate, SORT_LIST * groupby_list,
PRED_EXPR * having_pred, PRED_EXPR * grbynum_pred,
DB_VALUE * grbynum_val, int grbynum_flag, XASL_NODE * eptr_list,
AGGREGATE_TYPE * g_agg_list, REGU_VARIABLE_LIST g_regu_list,
VAL_LIST * g_val_list, OUTPTR_LIST * g_outptr_list,
REGU_VARIABLE_LIST g_hk_regu_list, bool with_rollup,
int hash_eligible, AGGREGATE_HASH_CONTEXT * agg_hash_context,
XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_VALUE_TYPE_LIST * type_list,
QFILE_TUPLE_RECORD * tplrec);
static void qexec_clear_groupby_state (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate);
static int qexec_clear_agg_orderby_const_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl, bool is_final);
static int qexec_gby_init_group_dim (GROUPBY_STATE * gbstate);
static void qexec_gby_clear_group_dim (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate);
static void qexec_gby_agg_tuple (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate, QFILE_TUPLE tpl, int peek);
static int qexec_hash_gby_agg_tuple (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
BUILDLIST_PROC_NODE * proc, QFILE_TUPLE_RECORD * tplrec,
QFILE_TUPLE_DESCRIPTOR * tpldesc, QFILE_LIST_ID * groupby_list,
bool * output_tuple);
static void qexec_gby_start_group_dim (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate, const RECDES * recdes);
static void qexec_gby_start_group (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate, const RECDES * recdes, int N);
static void qexec_gby_finalize_group_val_list (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate, int N);
static int qexec_gby_finalize_group_dim (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate, const RECDES * recdes);
static void qexec_gby_finalize_group (THREAD_ENTRY * thread_p, GROUPBY_STATE * gbstate, int N, bool keep_list_file);
static SORT_STATUS qexec_hash_gby_get_next (THREAD_ENTRY * thread_p, RECDES * recdes, void *arg);
static int qexec_hash_gby_put_next (THREAD_ENTRY * thread_p, const RECDES * recdes, void *arg);
static SORT_STATUS qexec_gby_get_next (THREAD_ENTRY * thread_p, RECDES * recdes, void *arg);
static int qexec_gby_put_next (THREAD_ENTRY * thread_p, const RECDES * recdes, void *arg);
static int qexec_groupby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static int qexec_groupby_index (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static int qexec_initialize_analytic_function_state (THREAD_ENTRY * thread_p, ANALYTIC_FUNCTION_STATE * func_state,
ANALYTIC_TYPE * func_p, XASL_STATE * xasl_state);
static ANALYTIC_STATE *qexec_initialize_analytic_state (THREAD_ENTRY * thread_p, ANALYTIC_STATE * analytic_state,
ANALYTIC_TYPE * a_func_list, SORT_LIST * sort_list,
REGU_VARIABLE_LIST a_regu_list, VAL_LIST * a_val_list,
OUTPTR_LIST * a_outptr_list, OUTPTR_LIST * a_outptr_list_interm,
bool is_last_run, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_VALUE_TYPE_LIST * type_list,
QFILE_TUPLE_RECORD * tplrec);
static SORT_STATUS qexec_analytic_get_next (THREAD_ENTRY * thread_p, RECDES * recdes, void *arg);
static int qexec_analytic_put_next (THREAD_ENTRY * thread_p, const RECDES * recdes, void *arg);
static int qexec_analytic_eval_instnum_pred (THREAD_ENTRY * thread_p, ANALYTIC_STATE * analytic_state,
ANALYTIC_STAGE stage);
static int qexec_analytic_start_group (THREAD_ENTRY * thread_p, XASL_STATE * xasl_state,
ANALYTIC_FUNCTION_STATE * func_state, const RECDES * key, bool reinit);
static int qexec_analytic_finalize_group (THREAD_ENTRY * thread_p, XASL_STATE * xasl_state,
ANALYTIC_FUNCTION_STATE * func_state, bool is_same_group);
static void qexec_analytic_add_tuple (THREAD_ENTRY * thread_p, ANALYTIC_STATE * analytic_state, QFILE_TUPLE tpl,
int peek);
static void qexec_clear_analytic_function_state (THREAD_ENTRY * thread_p, ANALYTIC_FUNCTION_STATE * func_state);
static void qexec_clear_analytic_state (THREAD_ENTRY * thread_p, ANALYTIC_STATE * analytic_state);
static int qexec_analytic_evaluate_ntile_function (THREAD_ENTRY * thread_p, ANALYTIC_FUNCTION_STATE * func_state);
static int qexec_analytic_evaluate_offset_function (THREAD_ENTRY * thread_p, ANALYTIC_FUNCTION_STATE * func_state,
ANALYTIC_STATE * analytic_state);
static int qexec_analytic_evaluate_interpolation_function (THREAD_ENTRY * thread_p,
ANALYTIC_FUNCTION_STATE * func_state);
static int qexec_analytic_group_header_load (ANALYTIC_FUNCTION_STATE * func_state);
static int qexec_analytic_sort_key_header_load (ANALYTIC_FUNCTION_STATE * func_state, bool load_value);
static int qexec_analytic_value_advance (THREAD_ENTRY * thread_p, ANALYTIC_FUNCTION_STATE * func_state, int amount,
int max_group_changes, bool ignore_nulls);
static int qexec_analytic_value_lookup (THREAD_ENTRY * thread_p, ANALYTIC_FUNCTION_STATE * func_state, int position,
bool ignore_nulls);
static int qexec_analytic_group_header_next (THREAD_ENTRY * thread_p, ANALYTIC_FUNCTION_STATE * func_state);
static int qexec_analytic_update_group_result (THREAD_ENTRY * thread_p, ANALYTIC_STATE * analytic_state);
static int qexec_collection_has_null (DB_VALUE * colval);
static DB_VALUE_COMPARE_RESULT qexec_cmp_tpl_vals_merge (QFILE_TUPLE * left_tval, TP_DOMAIN ** left_dom,
QFILE_TUPLE * rght_tval, TP_DOMAIN ** rght_dom, int tval_cnt);
static long qexec_size_remaining (QFILE_TUPLE_RECORD * tplrec1, QFILE_TUPLE_RECORD * tplrec2,
QFILE_LIST_MERGE_INFO * merge_info, int k);
static int qexec_merge_tuple (QFILE_TUPLE_RECORD * tplrec1, QFILE_TUPLE_RECORD * tplrec2,
QFILE_LIST_MERGE_INFO * merge_info, QFILE_TUPLE_RECORD * tplrec);
static int qexec_merge_tuple_add_list (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id, QFILE_TUPLE_RECORD * tplrec1,
QFILE_TUPLE_RECORD * tplrec2, QFILE_LIST_MERGE_INFO * merge_info,
QFILE_TUPLE_RECORD * tplrec);
static QFILE_LIST_ID *qexec_merge_list (THREAD_ENTRY * thread_p, QFILE_LIST_ID * outer_list_idp,
QFILE_LIST_ID * inner_list_idp, QFILE_LIST_MERGE_INFO * merge_infop,
int ls_flag);
static QFILE_LIST_ID *qexec_merge_list_outer (THREAD_ENTRY * thread_p, SCAN_ID * outer_sid, SCAN_ID * inner_sid,
QFILE_LIST_MERGE_INFO * merge_infop, PRED_EXPR * other_outer_join_pred,
XASL_STATE * xasl_state, int ls_flag);
static int qexec_merge_listfiles (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_open_scan (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE * curr_spec, VAL_LIST * val_list, VAL_DESCR * vd,
bool force_select_lock, int fixed, int grouped, bool iscan_oid_order, SCAN_ID * s_id,
QUERY_ID query_id, SCAN_OPERATION_TYPE scan_op_type, bool scan_immediately_stop,
bool * p_mvcc_select_lock_needed);
static void qexec_close_scan (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE * curr_spec);
static void qexec_end_scan (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE * curr_spec);
static SCAN_CODE qexec_next_merge_block (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE ** spec);
static SCAN_CODE qexec_next_scan_block (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static SCAN_CODE qexec_next_scan_block_iterations (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static SCAN_CODE qexec_execute_scan (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * ignore, XASL_SCAN_FNC_PTR next_scan_fnc);
static SCAN_CODE qexec_intprt_fnc (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec, XASL_SCAN_FNC_PTR next_scan_fnc);
static SCAN_CODE qexec_merge_fnc (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec, XASL_SCAN_FNC_PTR ignore);
static int qexec_setup_list_id (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static int qexec_init_upddel_ehash_files (THREAD_ENTRY * thread_p, XASL_NODE * buildlist);
static void qexec_destroy_upddel_ehash_files (THREAD_ENTRY * thread_p, XASL_NODE * buildlist);
static int qexec_execute_update (THREAD_ENTRY * thread_p, XASL_NODE * xasl, bool has_delete, XASL_STATE * xasl_state);
static int qexec_execute_delete (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_execute_insert (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state, bool skip_aptr);
static int qexec_execute_merge (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_execute_build_indexes (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_execute_obj_fetch (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
SCAN_OPERATION_TYPE scan_operation_type);
static int qexec_execute_increment (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid,
const HFID * class_hfid, ATTR_ID attrid, int n_increment, int pruning_type);
static int qexec_execute_selupd_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_execute_selupd_list_find_class (THREAD_ENTRY * thread_p, XASL_NODE * xasl, VAL_DESCR * vd, OID * oid,
SELUPD_LIST * selupd, OID * class_oid, HFID * class_hfid,
DB_CLASS_PARTITION_TYPE * needs_pruning, bool * found);
static int qexec_start_connect_by_lists (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_update_connect_by_lists (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static void qexec_end_connect_by_lists (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static void qexec_clear_connect_by_lists (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static int qexec_execute_connect_by (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static int qexec_iterate_connect_by_results (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static int qexec_check_for_cycle (THREAD_ENTRY * thread_p, OUTPTR_LIST * outptr_list, QFILE_TUPLE tpl,
QFILE_TUPLE_VALUE_TYPE_LIST * type_list, QFILE_LIST_ID * list_id_p, int *iscycle);
static int qexec_compare_valptr_with_tuple (OUTPTR_LIST * outptr_list, QFILE_TUPLE tpl,
QFILE_TUPLE_VALUE_TYPE_LIST * type_list, int *are_equal);
static int qexec_listfile_orderby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, QFILE_LIST_ID * list_file,
SORT_LIST * orderby_list, XASL_STATE * xasl_state, OUTPTR_LIST * outptr_list);
static int qexec_end_buildvalueblock_iterations (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static int qexec_end_mainblock_iterations (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
QFILE_TUPLE_RECORD * tplrec);
static void qexec_clear_mainblock_iterations (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static int qexec_execute_analytic (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
ANALYTIC_EVAL_TYPE * analytic_eval, QFILE_TUPLE_RECORD * tplrec, bool is_last);
static void qexec_update_btree_unique_stats_info (THREAD_ENTRY * thread_p, multi_index_unique_stats * info,
const HEAP_SCANCACHE * scan_cache);
static int qexec_prune_spec (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE * spec, VAL_DESCR * vd,
SCAN_OPERATION_TYPE scan_op_type);
static int qexec_process_partition_unique_stats (THREAD_ENTRY * thread_p, PRUNING_CONTEXT * pcontext);
static int qexec_process_unique_stats (THREAD_ENTRY * thread_p, const OID * class_oid,
UPDDEL_CLASS_INFO_INTERNAL * class_);
static SCAN_CODE qexec_init_next_partition (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE * spec);
static int qexec_check_limit_clause (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
bool * empty_result);
static int qexec_execute_mainblock_internal (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
UPDDEL_CLASS_INSTANCE_LOCK_INFO * p_class_instance_lock_info);
static DEL_LOB_INFO *qexec_create_delete_lob_info (THREAD_ENTRY * thread_p, XASL_STATE * xasl_state,
UPDDEL_CLASS_INFO_INTERNAL * class_info);
static DEL_LOB_INFO *qexec_change_delete_lob_info (THREAD_ENTRY * thread_p, XASL_STATE * xasl_state,
UPDDEL_CLASS_INFO_INTERNAL * class_info,
DEL_LOB_INFO ** del_lob_info_list_ptr);
static void qexec_free_delete_lob_info_list (THREAD_ENTRY * thread_p, DEL_LOB_INFO ** del_lob_info_list_ptr);
static const char *qexec_schema_get_type_name_from_id (DB_TYPE id);
static int qexec_schema_get_type_desc (DB_TYPE id, TP_DOMAIN * domain, DB_VALUE * result);
static int qexec_execute_build_columns (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int qexec_execute_cte (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
#if defined(SERVER_MODE)
#if defined (ENABLE_UNUSED_FUNCTION)
static int tranid_compare (const void *t1, const void *t2); /* TODO: put to header ?? */
#endif
#endif
static REGU_VARIABLE *replace_null_arith (REGU_VARIABLE * regu_var, DB_VALUE * set_dbval);
static REGU_VARIABLE *replace_null_dbval (REGU_VARIABLE * regu_var, DB_VALUE * set_dbval);
static void qexec_replace_prior_regu_vars (THREAD_ENTRY * thread_p, REGU_VARIABLE * regu, XASL_NODE * xasl);
static void qexec_replace_prior_regu_vars_pred (THREAD_ENTRY * thread_p, PRED_EXPR * pred, XASL_NODE * xasl);
static int qexec_init_index_pseudocolumn_strings (THREAD_ENTRY * thread_p, char **father_index, int *len_father_index,
char **son_index, int *len_son_index);
static int qexec_set_pseudocolumns_val_pointers (XASL_NODE * xasl, DB_VALUE ** level_valp, DB_VALUE ** isleaf_valp,
DB_VALUE ** iscycle_valp, DB_VALUE ** parent_pos_valp,
DB_VALUE ** index_valp);
static void qexec_reset_pseudocolumns_val_pointers (DB_VALUE * level_valp, DB_VALUE * isleaf_valp,
DB_VALUE * iscycle_valp, DB_VALUE * parent_pos_valp,
DB_VALUE * index_valp);
static int qexec_get_index_pseudocolumn_value_from_tuple (THREAD_ENTRY * thread_p, XASL_NODE * xasl, QFILE_TUPLE tpl,
DB_VALUE ** index_valp, char **index_value, int *index_len);
static int qexec_recalc_tuples_parent_pos_in_list (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p);
static int qexec_remove_duplicates_for_replace (THREAD_ENTRY * thread_p, HEAP_SCANCACHE * scan_cache,
HEAP_CACHE_ATTRINFO * attr_info, HEAP_CACHE_ATTRINFO * index_attr_info,
const HEAP_IDX_ELEMENTS_INFO * idx_info, int op_type, int pruning_type,
PRUNING_CONTEXT * pcontext, int *removed_count);
static int qexec_oid_of_duplicate_key_update (THREAD_ENTRY * thread_p, HEAP_SCANCACHE ** pruned_partition_scan_cache,
HEAP_SCANCACHE * scan_cache, HEAP_CACHE_ATTRINFO * attr_info,
HEAP_CACHE_ATTRINFO * index_attr_info,
const HEAP_IDX_ELEMENTS_INFO * idx_info, int needs_pruning,
PRUNING_CONTEXT * pcontext, OID * unique_oid, int op_type);
static int qexec_execute_duplicate_key_update (THREAD_ENTRY * thread_p, ODKU_INFO * odku, HFID * hfid, VAL_DESCR * vd,
int op_type, HEAP_SCANCACHE * scan_cache,
HEAP_CACHE_ATTRINFO * attr_info, HEAP_CACHE_ATTRINFO * index_attr_info,
HEAP_IDX_ELEMENTS_INFO * idx_info, int pruning_type,
PRUNING_CONTEXT * pcontext, int *force_count);
static int qexec_execute_do_stmt (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state);
static int bf2df_str_son_index (THREAD_ENTRY * thread_p, char **son_index, char *father_index, int *len_son_index,
int cnt);
static DB_VALUE_COMPARE_RESULT bf2df_str_compare (const unsigned char *s0, int l0, const unsigned char *s1, int l1);
static DB_VALUE_COMPARE_RESULT bf2df_str_cmpdisk (void *mem1, void *mem2, TP_DOMAIN * domain, int do_coercion,
int total_order, int *start_colp);
static DB_VALUE_COMPARE_RESULT bf2df_str_cmpval (DB_VALUE * value1, DB_VALUE * value2, int do_coercion, int total_order,
int *start_colp, int collation);
static void qexec_resolve_domains_on_sort_list (SORT_LIST * order_list, REGU_VARIABLE_LIST reference_regu_list);
static void qexec_resolve_domains_for_group_by (BUILDLIST_PROC_NODE * buildlist, OUTPTR_LIST * reference_out_list);
static int qexec_resolve_domains_for_aggregation (THREAD_ENTRY * thread_p, AGGREGATE_TYPE * agg_p,
XASL_STATE * xasl_state, QFILE_TUPLE_RECORD * tplrec,
REGU_VARIABLE_LIST regu_list, int *resolved);
static int query_multi_range_opt_check_set_sort_col (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static ACCESS_SPEC_TYPE *query_multi_range_opt_check_specs (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
static int qexec_init_instnum_val (XASL_NODE * xasl, THREAD_ENTRY * thread_p, XASL_STATE * xasl_state);
static int qexec_set_class_locks (THREAD_ENTRY * thread_p, XASL_NODE * aptr_list, UPDDEL_CLASS_INFO * query_classes,
int query_classes_count, UPDDEL_CLASS_INFO_INTERNAL * internal_classes);
static int qexec_for_update_set_class_locks (THREAD_ENTRY * thread_p, XASL_NODE * scan_list);
static int qexec_create_internal_classes (THREAD_ENTRY * thread_p, UPDDEL_CLASS_INFO * classes_info, int count,
UPDDEL_CLASS_INFO_INTERNAL ** classes);
static int qexec_create_mvcc_reev_assignments (THREAD_ENTRY * thread_p, XASL_NODE * aptr, bool should_delete,
UPDDEL_CLASS_INFO_INTERNAL * classes, int num_classes,
int num_assignments, UPDATE_ASSIGNMENT * assignments,
UPDATE_MVCC_REEV_ASSIGNMENT ** mvcc_reev_assigns);
static int prepare_mvcc_reev_data (THREAD_ENTRY * thread_p, XASL_NODE * aptr, XASL_STATE * xasl_state,
int num_reev_classes, int *cond_reev_indexes, MVCC_UPDDEL_REEV_DATA * reev_data,
int num_classes, UPDDEL_CLASS_INFO * classes,
UPDDEL_CLASS_INFO_INTERNAL * internal_classes, int num_assigns,
UPDATE_ASSIGNMENT * assigns, PRED_EXPR * cons_pred,
UPDDEL_MVCC_COND_REEVAL ** mvcc_reev_classes,
UPDATE_MVCC_REEV_ASSIGNMENT ** mvcc_reev_assigns, bool has_delete);
static UPDDEL_MVCC_COND_REEVAL *qexec_mvcc_cond_reev_set_scan_order (XASL_NODE * aptr,
UPDDEL_MVCC_COND_REEVAL * reev_classes,
int num_reev_classes, UPDDEL_CLASS_INFO * classes,
int num_classes);
static void qexec_clear_internal_classes (THREAD_ENTRY * thread_p, UPDDEL_CLASS_INFO_INTERNAL * classes, int count);
static int qexec_upddel_setup_current_class (THREAD_ENTRY * thread_p, UPDDEL_CLASS_INFO * class_,
UPDDEL_CLASS_INFO_INTERNAL * class_info, int op_type, OID * current_oid);
static int qexec_upddel_mvcc_set_filters (THREAD_ENTRY * thread_p, XASL_NODE * aptr_list,
UPDDEL_MVCC_COND_REEVAL * mvcc_reev_class, OID * class_oid);
static int qexec_init_agg_hierarchy_helpers (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE * spec,
AGGREGATE_TYPE * aggregate_list, HIERARCHY_AGGREGATE_HELPER ** helpers,
int *helpers_countp);
static int qexec_evaluate_aggregates_optimize (THREAD_ENTRY * thread_p, AGGREGATE_TYPE * agg_list,
ACCESS_SPEC_TYPE * spec, bool * is_scan_needed);
static int qexec_evaluate_partition_aggregates (THREAD_ENTRY * thread_p, ACCESS_SPEC_TYPE * spec,
AGGREGATE_TYPE * agg_list, bool * is_scan_needed);
static int qexec_setup_topn_proc (THREAD_ENTRY * thread_p, XASL_NODE * xasl, VAL_DESCR * vd);
static BH_CMP_RESULT qexec_topn_compare (const void *left, const void *right, BH_CMP_ARG arg);
static BH_CMP_RESULT qexec_topn_cmpval (DB_VALUE * left, DB_VALUE * right, SORT_LIST * sort_spec);
static TOPN_STATUS qexec_add_tuple_to_topn (THREAD_ENTRY * thread_p, TOPN_TUPLES * sort_stop,
QFILE_TUPLE_DESCRIPTOR * tpldescr);
static int qexec_topn_tuples_to_list_id (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state,
bool is_final);
static void qexec_clear_topn_tuple (THREAD_ENTRY * thread_p, TOPN_TUPLE * tuple, int count);
static int qexec_get_orderbynum_upper_bound (THREAD_ENTRY * tread_p, PRED_EXPR * pred, VAL_DESCR * vd,
DB_VALUE * ubound);
static int qexec_analytic_evaluate_cume_dist_percent_rank_function (THREAD_ENTRY * thread_p,
ANALYTIC_FUNCTION_STATE * func_state);
static int qexec_clear_regu_variable_list (THREAD_ENTRY * thread_p, XASL_NODE * xasl_p, REGU_VARIABLE_LIST list,
bool is_final);
static void qexec_clear_pred_xasl (THREAD_ENTRY * thread_p, PRED_EXPR * pred);
#if defined(SERVER_MODE)
static void qexec_set_xasl_trace_to_session (THREAD_ENTRY * thread_p, XASL_NODE * xasl);
#endif /* SERVER_MODE */
static int qexec_alloc_agg_hash_context (THREAD_ENTRY * thread_p, BUILDLIST_PROC_NODE * proc, XASL_STATE * xasl_state);
static void qexec_free_agg_hash_context (THREAD_ENTRY * thread_p, BUILDLIST_PROC_NODE * proc);
static int qexec_build_agg_hkey (THREAD_ENTRY * thread_p, XASL_STATE * xasl_state, REGU_VARIABLE_LIST regu_list,
QFILE_TUPLE tpl, AGGREGATE_HASH_KEY * key);
static int qexec_locate_agg_hentry_in_list (THREAD_ENTRY * thread_p, AGGREGATE_HASH_CONTEXT * context,
AGGREGATE_HASH_KEY * key, bool * found);
static int qexec_get_attr_default (THREAD_ENTRY * thread_p, OR_ATTRIBUTE * attr, DB_VALUE * default_val);
/*
* Utility routines
*/
/*
* qexec_eval_instnum_pred () -
* return:
* xasl(in) :
* xasl_state(in) :
*/
static DB_LOGICAL
qexec_eval_instnum_pred (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state)
{
DB_LOGICAL ev_res;
/* instant numbering; increase the value of inst_num() by 1 */
if (xasl->instnum_val)
{
xasl->instnum_val->data.bigint++;
}
if (xasl->save_instnum_val)
{
xasl->save_instnum_val->data.bigint++;
}
if (xasl->instnum_pred && !(xasl->instnum_flag & XASL_INSTNUM_FLAG_SCAN_STOP_AT_ANALYTIC))
{
PRED_EXPR *pr = xasl->instnum_pred;
/* this case is for: select * from table limit 3, or select * from table where rownum <= 3 and we can change
* operator <= to < and reevaluate last condition. (to stop scan at this time) */
if (pr->type == T_EVAL_TERM && pr->pe.m_eval_term.et_type == T_COMP_EVAL_TERM
&& (pr->pe.m_eval_term.et.et_comp.lhs->type == TYPE_CONSTANT
&& pr->pe.m_eval_term.et.et_comp.rhs->type == TYPE_POS_VALUE)
&& xasl->instnum_pred->pe.m_eval_term.et.et_comp.rel_op == R_LE)
{
xasl->instnum_pred->pe.m_eval_term.et.et_comp.rel_op = R_LT;
/* evaluate predicate */
ev_res = eval_pred (thread_p, xasl->instnum_pred, &xasl_state->vd, NULL);
xasl->instnum_pred->pe.m_eval_term.et.et_comp.rel_op = R_LE;
if (ev_res != V_TRUE)
{
ev_res = eval_pred (thread_p, xasl->instnum_pred, &xasl_state->vd, NULL);
if (ev_res == V_TRUE)
{
xasl->instnum_flag |= XASL_INSTNUM_FLAG_SCAN_LAST_STOP;
}
}
}
else
{
/* evaluate predicate */
ev_res = eval_pred (thread_p, xasl->instnum_pred, &xasl_state->vd, NULL);
}
switch (ev_res)
{
case V_FALSE:
/* evaluation is false; if check flag was set, stop scan */
if (xasl->instnum_flag & XASL_INSTNUM_FLAG_SCAN_CHECK)
{
xasl->instnum_flag |= XASL_INSTNUM_FLAG_SCAN_STOP;
}
break;
case V_TRUE:
/* evaluation is true; if not continue scan mode, set scan check flag */
if (!(xasl->instnum_flag & XASL_INSTNUM_FLAG_SCAN_CONTINUE)
&& !(xasl->instnum_flag & XASL_INSTNUM_FLAG_SCAN_CHECK))
{
xasl->instnum_flag |= XASL_INSTNUM_FLAG_SCAN_CHECK;
}
break;
case V_ERROR:
break;
default: /* V_UNKNOWN */
break;
}
}
else
{
/* no predicate; always true */
ev_res = V_TRUE;
}
return ev_res;
}
/*
* qexec_add_composite_lock () -
* return: NO_ERROR or ER_code
* thread_p(in) :
* reg_var_list(in/out) : list of regu variables to be fetched. First
* upd_del_cls_cnt pairs of variables will be loaded. Each pair will
* contain instance OID and class OID.
* xasl_state(in) : xasl state. Needed for fetch_peek_dbval.
* composite_lock(in/out) : structure that will be filled with composite
* locks.
* upd_del_cls_cnt(in): number of classes for which rows will be updated or
* deleted.
* default_cls_oid(in): default class oid
*/
static int
qexec_add_composite_lock (THREAD_ENTRY * thread_p, REGU_VARIABLE_LIST reg_var_list, XASL_STATE * xasl_state,
LK_COMPOSITE_LOCK * composite_lock, int upd_del_cls_cnt, OID * default_cls_oid)
{
int ret = NO_ERROR, idx;
DB_VALUE *dbval, element;
DB_TYPE typ;
OID instance_oid, class_oid;
/* By convention, the first upd_del_class_cnt pairs of values must be: instance OID - class OID */
idx = 0;
while (reg_var_list && idx < upd_del_cls_cnt)
{
if (reg_var_list->next == NULL && default_cls_oid == NULL)
{
GOTO_EXIT_ON_ERROR;
}
ret = fetch_peek_dbval (thread_p, ®_var_list->value, &xasl_state->vd, NULL, NULL, NULL, &dbval);
if (ret != NO_ERROR)
{
GOTO_EXIT_ON_ERROR;
}
reg_var_list = reg_var_list->next;
if (!DB_IS_NULL (dbval))
{
typ = DB_VALUE_DOMAIN_TYPE (dbval);
if (typ == DB_TYPE_VOBJ)
{
/* grab the real oid */
ret = db_seq_get (db_get_set (dbval), 2, &element);
if (ret != NO_ERROR)
{
GOTO_EXIT_ON_ERROR;
}
dbval = &element;
typ = DB_VALUE_DOMAIN_TYPE (dbval);
}
if (typ != DB_TYPE_OID)
{
GOTO_EXIT_ON_ERROR;
}
SAFE_COPY_OID (&instance_oid, db_get_oid (dbval));
if (default_cls_oid != NULL)
{
COPY_OID (&class_oid, default_cls_oid);
}
else
{
ret = fetch_peek_dbval (thread_p, ®_var_list->value, &xasl_state->vd, NULL, NULL, NULL, &dbval);
if (ret != NO_ERROR)
{
GOTO_EXIT_ON_ERROR;
}
typ = DB_VALUE_DOMAIN_TYPE (dbval);
if (typ == DB_TYPE_VOBJ)
{
/* grab the real oid */
ret = db_seq_get (db_get_set (dbval), 2, &element);
if (ret != NO_ERROR)
{
GOTO_EXIT_ON_ERROR;
}
dbval = &element;
typ = DB_VALUE_DOMAIN_TYPE (dbval);
}
if (typ != DB_TYPE_OID)
{
GOTO_EXIT_ON_ERROR;
}
SAFE_COPY_OID (&class_oid, db_get_oid (dbval));
}
ret = lock_add_composite_lock (thread_p, composite_lock, &instance_oid, &class_oid);
if (ret != NO_ERROR)
{
GOTO_EXIT_ON_ERROR;
}
}
idx++;
if (reg_var_list)
{
reg_var_list = reg_var_list->next;
}
}
return ret;
exit_on_error:
return (ret == NO_ERROR && (ret = er_errid ()) == NO_ERROR) ? ER_FAILED : ret;
}
/*
* qexec_generate_tuple_descriptor () -
* return: status
* thread_p(in) :
* list_id(in/out) :
* outptr_list(in) :
* vd(in) :
*
*/
static QPROC_TPLDESCR_STATUS
qexec_generate_tuple_descriptor (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id, VALPTR_LIST * outptr_list,
VAL_DESCR * vd)
{
QPROC_TPLDESCR_STATUS status;
size_t size;
int i;
status = QPROC_TPLDESCR_FAILURE; /* init */
/* make f_valp array */
if (list_id->tpl_descr.f_valp == NULL && list_id->type_list.type_cnt > 0)
{
size = list_id->type_list.type_cnt * DB_SIZEOF (DB_VALUE *);
list_id->tpl_descr.f_valp = (DB_VALUE **) malloc (size);
if (list_id->tpl_descr.f_valp == NULL)
{
er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_OUT_OF_VIRTUAL_MEMORY, 1, size);
goto exit_on_error;
}
size = list_id->type_list.type_cnt * sizeof (bool);
list_id->tpl_descr.clear_f_val_at_clone_decache = (bool *) malloc (size);
if (list_id->tpl_descr.clear_f_val_at_clone_decache == NULL)
{
er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_OUT_OF_VIRTUAL_MEMORY, 1, size);
goto exit_on_error;
}
for (i = 0; i < list_id->type_list.type_cnt; i++)
{
list_id->tpl_descr.clear_f_val_at_clone_decache[i] = false;
}
}
/* build tuple descriptor */
status = qdata_generate_tuple_desc_for_valptr_list (thread_p, outptr_list, vd, &(list_id->tpl_descr));
if (status == QPROC_TPLDESCR_FAILURE)
{
goto exit_on_error;
}
if (list_id->is_domain_resolved == false)
{
/* Resolve DB_TYPE_VARIABLE domains. It will be done when generating the first tuple. */
if (qfile_update_domains_on_type_list (thread_p, list_id, outptr_list) != NO_ERROR)
{
goto exit_on_error;
}
}
return status;
exit_on_error:
return QPROC_TPLDESCR_FAILURE;
}
/*
* qexec_upddel_add_unique_oid_to_ehid () -
* return: error code (<0) or the number of removed OIDs (>=0).
* thread_p(in) :
* xasl(in) : The XASL node of the generated SELECT statement for UPDATE or
* DELETE. It must be a BUILDLIST_PROC and have the temporary hash
* files already created (upddel_oid_locator_ehids).
* xasl_state(in) :
*
* Note: This function is used only for the SELECT queries generated for UPDATE
* or DELETE statements. It sets each instance OID from the outptr_list
* to null if the OID already exists in the hash file associated with the
* source table of the OID. (It eliminates duplicate OIDs in order to not
* UPDATE/DELETE them more than once). The function returns the number of
* removed OIDs so that the caller can remove the entire row from
* processing (SELECT list) if all OIDs were removed. Otherwise only the
* null instance OIDs will be skipped from UPDATE/DELETE processing.
*/
static int
qexec_upddel_add_unique_oid_to_ehid (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_state)
{
REGU_VARIABLE_LIST reg_var_list = NULL;
DB_VALUE *dbval = NULL, *orig_dbval = NULL, element;
DB_TYPE typ;
int ret = NO_ERROR, idx, rem_cnt = 0;
EHID *ehid = NULL;
OID oid, key_oid;
EH_SEARCH eh_search;
if (xasl == NULL || xasl->type != BUILDLIST_PROC || xasl->proc.buildlist.upddel_oid_locator_ehids == NULL)
{
return NO_ERROR;
}