-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathCTranslatorUtils.cpp
More file actions
2512 lines (2203 loc) · 71.1 KB
/
Copy pathCTranslatorUtils.cpp
File metadata and controls
2512 lines (2203 loc) · 71.1 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
//---------------------------------------------------------------------------
// Greenplum Database
// Copyright (C) 2012 EMC Corp.
//
// @filename:
// CTranslatorUtils.cpp
//
// @doc:
// Implementation of the utility methods for translating GPDB's
// Query / PlannedStmt into DXL Tree
//
// @test:
//
//
//---------------------------------------------------------------------------
extern "C" {
#include "postgres.h"
#include "access/sysattr.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "nodes/parsenodes.h"
#include "nodes/plannodes.h"
#include "optimizer/walkers.h"
#include "utils/guc.h"
#include "utils/rel.h"
}
#include "gpos/attributes.h"
#include "gpos/base.h"
#include "gpos/common/CAutoTimer.h"
#include "gpos/common/CBitSetIter.h"
#include "gpos/string/CWStringDynamic.h"
#include "gpopt/base/CUtils.h"
#include "gpopt/gpdbwrappers.h"
#include "gpopt/mdcache/CMDAccessor.h"
#include "gpopt/translate/CDXLTranslateContext.h"
#include "gpopt/translate/CTranslatorRelcacheToDXL.h"
#include "gpopt/translate/CTranslatorScalarToDXL.h"
#include "gpopt/translate/CTranslatorUtils.h"
#include "naucrates/dxl/CDXLUtils.h"
#include "naucrates/dxl/gpdb_types.h"
#include "naucrates/dxl/operators/CDXLColDescr.h"
#include "naucrates/dxl/operators/CDXLDatumBool.h"
#include "naucrates/dxl/operators/CDXLDatumInt2.h"
#include "naucrates/dxl/operators/CDXLDatumInt4.h"
#include "naucrates/dxl/operators/CDXLDatumInt8.h"
#include "naucrates/dxl/operators/CDXLDatumOid.h"
#include "naucrates/dxl/operators/CDXLNode.h"
#include "naucrates/dxl/operators/CDXLPhysicalRandomMotion.h"
#include "naucrates/dxl/operators/CDXLPhysicalRedistributeMotion.h"
#include "naucrates/dxl/operators/CDXLScalarAssertConstraint.h"
#include "naucrates/dxl/operators/CDXLScalarIdent.h"
#include "naucrates/dxl/operators/CDXLScalarProjElem.h"
#include "naucrates/dxl/operators/CDXLSpoolInfo.h"
#include "naucrates/dxl/xml/dxltokens.h"
#include "naucrates/exception.h"
#include "naucrates/md/CMDIdColStats.h"
#include "naucrates/md/CMDIdRelStats.h"
#include "naucrates/md/CMDTypeGenericGPDB.h"
#include "naucrates/md/IMDAggregate.h"
#include "naucrates/md/IMDIndex.h"
#include "naucrates/md/IMDRelation.h"
#include "naucrates/md/IMDTypeBool.h"
#include "naucrates/md/IMDTypeInt2.h"
#include "naucrates/md/IMDTypeInt4.h"
#include "naucrates/md/IMDTypeInt8.h"
#include "naucrates/md/IMDTypeOid.h"
#include "naucrates/traceflags/traceflags.h"
using namespace gpdxl;
using namespace gpmd;
using namespace gpos;
using namespace gpopt;
extern bool optimizer_enable_master_only_queries;
extern bool optimizer_multilevel_partitioning;
#define GPDB_NEXTVAL 1574
#define GPDB_CURRVAL 1575
#define GPDB_SETVAL 1576
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetIndexDescr
//
// @doc:
// Create a DXL index descriptor from an index MD id
//
//---------------------------------------------------------------------------
CDXLIndexDescr *
CTranslatorUtils::GetIndexDescr(CMemoryPool *mp, CMDAccessor *md_accessor,
IMDId *mdid)
{
const IMDIndex *index = md_accessor->RetrieveIndex(mdid);
const CWStringConst *index_name = index->Mdname().GetMDName();
CMDName *index_mdname = GPOS_NEW(mp) CMDName(mp, index_name);
return GPOS_NEW(mp) CDXLIndexDescr(mdid, index_mdname);
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetTableDescr
//
// @doc:
// Create a DXL table descriptor from a GPDB range table entry
//
//---------------------------------------------------------------------------
CDXLTableDescr *
CTranslatorUtils::GetTableDescr(CMemoryPool *mp, CMDAccessor *md_accessor,
CIdGenerator *id_generator,
const RangeTblEntry *rte,
const RTEPermissionInfo *perminfo,
ULONG assigned_query_id_for_target_rel,
BOOL *is_distributed_table // output
)
{
// generate an MDId for the table desc.
OID rel_oid = rte->relid;
CMDIdGPDB *mdid = GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidRel, rel_oid);
const IMDRelation *rel = md_accessor->RetrieveRel(mdid);
// look up table name
CMDName *table_mdname =
rte->alias
? GPOS_NEW(mp) CMDName(
GPOS_NEW(mp) CWStringConst(mp, rte->alias->aliasname), true)
: GPOS_NEW(mp) CMDName(mp, rel->Mdname().GetMDName());
ULONG required_perms = static_cast<ULONG>(perminfo->requiredPerms);
CDXLTableDescr *table_descr = GPOS_NEW(mp) CDXLTableDescr(
mp, mdid, table_mdname, perminfo->checkAsUser, rte->rellockmode,
required_perms, assigned_query_id_for_target_rel);
const ULONG len = rel->ColumnCount();
IMDRelation::Ereldistrpolicy distribution_policy =
rel->GetRelDistribution();
if (nullptr != is_distributed_table &&
(IMDRelation::EreldistrHash == distribution_policy ||
IMDRelation::EreldistrRandom == distribution_policy ||
IMDRelation::EreldistrReplicated == distribution_policy))
{
*is_distributed_table = true;
}
else if (IMDRelation::ErelstorageForeign != rel->RetrieveRelStorageType() &&
!optimizer_enable_master_only_queries &&
(IMDRelation::EreldistrMasterOnly == distribution_policy))
{
// fall back to the planner for queries on master-only table if they are disabled with Orca. This is due to
// the fact that catalog tables (master-only) are not analyzed often and will result in Orca producing
// inferior plans.
GPOS_THROW_EXCEPTION(gpdxl::ExmaDXL, // major
gpdxl::ExmiQuery2DXLUnsupportedFeature, // minor
GPOS_WSZ_LIT("Queries on master-only tables"));
}
// add columns from md cache relation object to table descriptor
for (ULONG ul = 0; ul < len; ul++)
{
const IMDColumn *md_col = rel->GetMdCol(ul);
if (md_col->IsDropped())
{
continue;
}
CMDName *col = GPOS_NEW(mp) CMDName(mp, md_col->Mdname().GetMDName());
CMDIdGPDB *col_type = CMDIdGPDB::CastMdid(md_col->MdidType());
col_type->AddRef();
// create a column descriptor for the column
CDXLColDescr *dxl_col_descr = GPOS_NEW(mp)
CDXLColDescr(col, id_generator->next_id(), md_col->AttrNum(),
col_type, md_col->TypeModifier(), /* type_modifier */
false, /* fColDropped */
md_col->Length());
table_descr->AddColumnDescr(dxl_col_descr);
}
return table_descr;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::IsSirvFunc
//
// @doc:
// Check if the given function is a SIRV (single row volatile) that reads
// or modifies SQL data
//
//---------------------------------------------------------------------------
BOOL
CTranslatorUtils::IsSirvFunc(CMemoryPool *mp, CMDAccessor *md_accessor,
OID func_oid)
{
// we exempt the following 3 functions to avoid falling back to the planner
// for DML on tables with sequences. The same exemption is also in the planner
if (GPDB_NEXTVAL == func_oid || GPDB_CURRVAL == func_oid ||
GPDB_SETVAL == func_oid)
{
return false;
}
CMDIdGPDB *mdid_func =
GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidGeneral, func_oid);
const IMDFunction *func = md_accessor->RetrieveFunc(mdid_func);
BOOL is_sirv = (!func->ReturnsSet() &&
IMDFunction::EfsVolatile == func->GetFuncStability());
mdid_func->Release();
return is_sirv;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::HasSubquery
//
// @doc:
// Check if the given tree contains a subquery
//
//---------------------------------------------------------------------------
BOOL
CTranslatorUtils::HasSubquery(Node *node)
{
List *unsupported_list = ListMake1Int(T_SubLink);
INT unsupported = gpdb::FindNodes(node, unsupported_list);
gpdb::GPDBFree(unsupported_list);
return (0 <= unsupported);
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::ConvertToCDXLLogicalTVF
//
// @doc:
// Create a DXL logical TVF from a GPDB range table entry
//
//---------------------------------------------------------------------------
CDXLLogicalTVF *
CTranslatorUtils::ConvertToCDXLLogicalTVF(CMemoryPool *mp,
CMDAccessor *md_accessor,
CIdGenerator *id_generator,
const RangeTblEntry *rte)
{
/*
* GPDB_94_MERGE_FIXME: RangeTblEntry for functions can now contain multiple function calls.
* ORCA isn't prepared for that yet. See upstream commit 784e762e88.
*/
if (list_length(rte->functions) != 1)
{
GPOS_RAISE(gpdxl::ExmaDXL, gpdxl::ExmiQuery2DXLUnsupportedFeature,
GPOS_WSZ_LIT("Multi-argument UNNEST() or TABLE()"));
}
/*
* GPDB_94_MERGE_FIXME: Does WITH ORDINALITY work? It was new in 9.4. Add a check here,
* if it doesn't, or remove this comment if it does.
*/
RangeTblFunction *rtfunc = (RangeTblFunction *) linitial(rte->functions);
// TVF evaluates to const, return const DXL node
if (IsA(rtfunc->funcexpr, Const))
{
Const *constExpr = (Const *) rtfunc->funcexpr;
CMDIdGPDB *mdid_return_type =
GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidGeneral, constExpr->consttype);
const IMDType *type = md_accessor->RetrieveType(mdid_return_type);
CDXLColDescrArray *column_descrs = GetColumnDescriptorsFromComposite(
mp, md_accessor, id_generator, type);
CMDName *func_name =
CDXLUtils::CreateMDNameFromCharArray(mp, rte->eref->aliasname);
// if TVF evaluates to const, pass invalid key as funcid
CDXLLogicalTVF *tvf_dxl = GPOS_NEW(mp)
CDXLLogicalTVF(mp, GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidGeneral, 0),
mdid_return_type, func_name, column_descrs);
return tvf_dxl;
}
FuncExpr *funcexpr = (FuncExpr *) rtfunc->funcexpr;
// In the planner, scalar functions that are volatile (SIRV) or read or modify SQL
// data get patched into an InitPlan. This is not supported in the optimizer
if (IsSirvFunc(mp, md_accessor, funcexpr->funcid))
{
GPOS_RAISE(gpdxl::ExmaDXL, gpdxl::ExmiQuery2DXLUnsupportedFeature,
GPOS_WSZ_LIT("SIRV functions"));
}
// get function id
CMDIdGPDB *mdid_func =
GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidGeneral, funcexpr->funcid);
CMDIdGPDB *mdid_return_type =
GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidGeneral, funcexpr->funcresulttype);
const IMDType *type = md_accessor->RetrieveType(mdid_return_type);
// get function from MDcache
const IMDFunction *func = md_accessor->RetrieveFunc(mdid_func);
IMdIdArray *out_arg_types = func->OutputArgTypesMdidArray();
CDXLColDescrArray *column_descrs = nullptr;
if (nullptr != rtfunc->funccoltypes)
{
// function returns record - use col names and types from query
column_descrs = GetColumnDescriptorsFromRecord(
mp, id_generator, rte->eref->colnames, rtfunc->funccoltypes,
rtfunc->funccoltypmods);
}
else if (type->IsComposite() && IMDId::IsValid(type->GetBaseRelMdid()))
{
// function returns a "table" type or a user defined type
column_descrs = GetColumnDescriptorsFromComposite(mp, md_accessor,
id_generator, type);
}
else if (nullptr != out_arg_types)
{
// function returns record - but output col types are defined in catalog
out_arg_types->AddRef();
if (ContainsPolymorphicTypes(out_arg_types))
{
// resolve polymorphic types (anyelement/anyarray) using the
// argument types from the query
List *arg_types = gpdb::GetFuncArgTypes(funcexpr->funcid);
IMdIdArray *resolved_types =
ResolvePolymorphicTypes(mp, out_arg_types, arg_types, funcexpr);
out_arg_types->Release();
out_arg_types = resolved_types;
}
column_descrs = GetColumnDescriptorsFromRecord(
mp, id_generator, rte->eref->colnames, out_arg_types);
out_arg_types->Release();
}
else
{
// function returns base type
CMDName func_mdname = func->Mdname();
// table valued functions don't describe the returned column type modifiers, hence the -1
column_descrs =
GetColumnDescriptorsFromBase(mp, id_generator, mdid_return_type,
default_type_modifier, &func_mdname);
}
CMDName *pmdfuncname = GPOS_NEW(mp) CMDName(mp, func->Mdname().GetMDName());
CDXLLogicalTVF *tvf_dxl = GPOS_NEW(mp) CDXLLogicalTVF(
mp, mdid_func, mdid_return_type, pmdfuncname, column_descrs);
return tvf_dxl;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::ResolvePolymorphicTypes
//
// @doc:
// Resolve polymorphic types in the given array of type ids, replacing
// them with the actual types obtained from the query
//
//---------------------------------------------------------------------------
IMdIdArray *
CTranslatorUtils::ResolvePolymorphicTypes(CMemoryPool *mp,
IMdIdArray *return_arg_mdids,
List *input_arg_types,
FuncExpr *funcexpr)
{
ULONG arg_index = 0;
const ULONG num_arg_types = gpdb::ListLength(input_arg_types);
const ULONG num_args_from_query = gpdb::ListLength(funcexpr->args);
const ULONG num_return_args = return_arg_mdids->Size();
const ULONG num_args = std::min(num_arg_types, num_args_from_query);
const ULONG total_args = num_args + num_return_args;
OID arg_types[total_args];
char arg_modes[total_args];
// copy the first 'num_args' function argument types
ListCell *arg_type = nullptr;
ForEach(arg_type, input_arg_types)
{
if (arg_index >= num_args)
{
break;
}
arg_types[arg_index] = lfirst_oid(arg_type);
arg_modes[arg_index++] = PROARGMODE_IN;
}
// copy function return types
for (ULONG ul = 0; ul < num_return_args; ul++)
{
IMDId *mdid = (*return_arg_mdids)[ul];
arg_types[arg_index] = CMDIdGPDB::CastMdid(mdid)->Oid();
arg_modes[arg_index++] = PROARGMODE_TABLE;
}
if (!gpdb::ResolvePolymorphicArgType(total_args, arg_types, arg_modes,
funcexpr))
{
GPOS_RAISE(
gpdxl::ExmaDXL, gpdxl::ExmiDXLUnrecognizedType,
GPOS_WSZ_LIT(
"could not determine actual argument/return type for polymorphic function"));
}
// generate a new array of mdids based on the resolved return types
IMdIdArray *resolved_types = GPOS_NEW(mp) IMdIdArray(mp);
// get the resolved return types
for (ULONG ul = num_args; ul < total_args; ul++)
{
IMDId *resolved_mdid = nullptr;
resolved_mdid =
GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidGeneral, arg_types[ul]);
resolved_types->Append(resolved_mdid);
}
return resolved_types;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::ContainsPolymorphicTypes
//
// @doc:
// Check if the given mdid array contains any of the polymorphic
// types (ANYELEMENT, ANYARRAY, ANYENUM, ANYNONARRAY)
//
//---------------------------------------------------------------------------
BOOL
CTranslatorUtils::ContainsPolymorphicTypes(IMdIdArray *mdid_array)
{
GPOS_ASSERT(nullptr != mdid_array);
const ULONG len = mdid_array->Size();
for (ULONG ul = 0; ul < len; ul++)
{
IMDId *mdid_type = (*mdid_array)[ul];
if (IsPolymorphicType(CMDIdGPDB::CastMdid(mdid_type)->Oid()))
{
return true;
}
}
return false;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetColumnDescriptorsFromRecord
//
// @doc:
// Get column descriptors from a record type
//
//---------------------------------------------------------------------------
CDXLColDescrArray *
CTranslatorUtils::GetColumnDescriptorsFromRecord(CMemoryPool *mp,
CIdGenerator *id_generator,
List *col_names,
List *col_types,
List *col_type_modifiers)
{
ListCell *col_name = nullptr;
ListCell *col_type = nullptr;
ListCell *col_type_modifier = nullptr;
ULONG ul = 0;
CDXLColDescrArray *column_descrs = GPOS_NEW(mp) CDXLColDescrArray(mp);
ForThree(col_name, col_names, col_type, col_types, col_type_modifier,
col_type_modifiers)
{
Oid coltype = lfirst_oid(col_type);
INT type_modifier = lfirst_int(col_type_modifier);
CHAR *col_name_char_array = strVal(lfirst(col_name));
CWStringDynamic *column_name =
CDXLUtils::CreateDynamicStringFromCharArray(mp,
col_name_char_array);
CMDName *col_mdname = GPOS_NEW(mp) CMDName(mp, column_name);
GPOS_DELETE(column_name);
IMDId *col_type = GPOS_NEW(mp) CMDIdGPDB(IMDId::EmdidGeneral, coltype);
CDXLColDescr *dxl_col_descr = GPOS_NEW(mp) CDXLColDescr(
col_mdname, id_generator->next_id(), INT(ul + 1) /* attno */,
col_type, type_modifier, false /* fColDropped */
);
column_descrs->Append(dxl_col_descr);
ul++;
}
return column_descrs;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetColumnDescriptorsFromRecord
//
// @doc:
// Get column descriptors from a record type
//
//---------------------------------------------------------------------------
CDXLColDescrArray *
CTranslatorUtils::GetColumnDescriptorsFromRecord(CMemoryPool *mp,
CIdGenerator *id_generator,
List *col_names,
IMdIdArray *out_arg_types)
{
GPOS_ASSERT(out_arg_types->Size() == (ULONG) gpdb::ListLength(col_names));
ListCell *col_name = nullptr;
ULONG ul = 0;
CDXLColDescrArray *column_descrs = GPOS_NEW(mp) CDXLColDescrArray(mp);
ForEach(col_name, col_names)
{
CHAR *col_name_char_array = strVal(lfirst(col_name));
CWStringDynamic *column_name =
CDXLUtils::CreateDynamicStringFromCharArray(mp,
col_name_char_array);
CMDName *col_mdname = GPOS_NEW(mp) CMDName(mp, column_name);
GPOS_DELETE(column_name);
IMDId *col_type = (*out_arg_types)[ul];
col_type->AddRef();
// This function is only called to construct column descriptors for table-valued functions
// which won't have type modifiers for columns of the returned table
CDXLColDescr *dxl_col_descr = GPOS_NEW(mp) CDXLColDescr(
col_mdname, id_generator->next_id(), INT(ul + 1) /* attno */,
col_type, default_type_modifier, false /* fColDropped */
);
column_descrs->Append(dxl_col_descr);
ul++;
}
return column_descrs;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetColumnDescriptorsFromBase
//
// @doc:
// Get column descriptor from a base type
//
//---------------------------------------------------------------------------
CDXLColDescrArray *
CTranslatorUtils::GetColumnDescriptorsFromBase(CMemoryPool *mp,
CIdGenerator *id_generator,
IMDId *mdid_return_type,
INT type_modifier,
CMDName *pmdName)
{
CDXLColDescrArray *column_descrs = GPOS_NEW(mp) CDXLColDescrArray(mp);
mdid_return_type->AddRef();
CMDName *col_mdname = GPOS_NEW(mp) CMDName(mp, pmdName->GetMDName());
CDXLColDescr *dxl_col_descr = GPOS_NEW(mp)
CDXLColDescr(col_mdname, id_generator->next_id(), INT(1) /* attno */,
mdid_return_type, type_modifier, /* type_modifier */
false /* fColDropped */
);
column_descrs->Append(dxl_col_descr);
return column_descrs;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetColumnDescriptorsFromComposite
//
// @doc:
// Get column descriptors from a composite type
//
//---------------------------------------------------------------------------
CDXLColDescrArray *
CTranslatorUtils::GetColumnDescriptorsFromComposite(CMemoryPool *mp,
CMDAccessor *md_accessor,
CIdGenerator *id_generator,
const IMDType *type)
{
CMDColumnArray *col_ptr_arr = ExpandCompositeType(mp, md_accessor, type);
CDXLColDescrArray *column_descrs = GPOS_NEW(mp) CDXLColDescrArray(mp);
for (ULONG ul = 0; ul < col_ptr_arr->Size(); ul++)
{
IMDColumn *md_col = (*col_ptr_arr)[ul];
CMDName *col_mdname =
GPOS_NEW(mp) CMDName(mp, md_col->Mdname().GetMDName());
IMDId *col_type = md_col->MdidType();
col_type->AddRef();
CDXLColDescr *dxl_col_descr = GPOS_NEW(mp) CDXLColDescr(
col_mdname, id_generator->next_id(), INT(ul + 1) /* attno */,
col_type, md_col->TypeModifier(), /* type_modifier */
false /* fColDropped */
);
column_descrs->Append(dxl_col_descr);
}
col_ptr_arr->Release();
return column_descrs;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::ExpandCompositeType
//
// @doc:
// Expand a composite type into an array of IMDColumns
//
//---------------------------------------------------------------------------
CMDColumnArray *
CTranslatorUtils::ExpandCompositeType(CMemoryPool *mp, CMDAccessor *md_accessor,
const IMDType *type)
{
GPOS_ASSERT(nullptr != type);
GPOS_ASSERT(type->IsComposite());
IMDId *rel_mdid = type->GetBaseRelMdid();
const IMDRelation *rel = md_accessor->RetrieveRel(rel_mdid);
GPOS_ASSERT(nullptr != rel);
CMDColumnArray *pdrgPmdcol = GPOS_NEW(mp) CMDColumnArray(mp);
for (ULONG ul = 0; ul < rel->ColumnCount(); ul++)
{
CMDColumn *md_col = (CMDColumn *) rel->GetMdCol(ul);
if (!md_col->IsSystemColumn())
{
md_col->AddRef();
pdrgPmdcol->Append(md_col);
}
}
return pdrgPmdcol;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::ConvertToDXLJoinType
//
// @doc:
// Translates the join type from its GPDB representation into the DXL one
//
//---------------------------------------------------------------------------
EdxlJoinType
CTranslatorUtils::ConvertToDXLJoinType(JoinType jt)
{
EdxlJoinType join_type = EdxljtSentinel;
switch (jt)
{
case JOIN_INNER:
join_type = EdxljtInner;
break;
case JOIN_LEFT:
join_type = EdxljtLeft;
break;
case JOIN_FULL:
join_type = EdxljtFull;
break;
case JOIN_RIGHT:
join_type = EdxljtRight;
break;
case JOIN_SEMI:
join_type = EdxljtIn;
break;
case JOIN_ANTI:
join_type = EdxljtLeftAntiSemijoin;
break;
case JOIN_LASJ_NOTIN:
join_type = EdxljtLeftAntiSemijoinNotIn;
break;
case JOIN_RIGHT_SEMI:
join_type = EdxljtRightSemijoin;
break;
case JOIN_RIGHT_ANTI:
join_type = EdxljtRightAntiSemijoin;
break;
default:
GPOS_ASSERT(!"Unrecognized join type");
}
GPOS_ASSERT(EdxljtSentinel > join_type);
return join_type;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::ConvertToDXLIndexScanDirection
//
// @doc:
// Translates the DXL index scan direction from GPDB representation
//
//---------------------------------------------------------------------------
EdxlIndexScanDirection
CTranslatorUtils::ConvertToDXLIndexScanDirection(ScanDirection sd)
{
EdxlIndexScanDirection idx_scan_direction = EdxlisdSentinel;
switch (sd)
{
case BackwardScanDirection:
idx_scan_direction = EdxlisdBackward;
break;
case ForwardScanDirection:
idx_scan_direction = EdxlisdForward;
break;
case NoMovementScanDirection:
idx_scan_direction = EdxlisdNoMovement;
break;
default:
GPOS_ASSERT(!"Unrecognized index scan direction");
}
GPOS_ASSERT(EdxlisdSentinel > idx_scan_direction);
return idx_scan_direction;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetColumnDescrAt
//
// @doc:
// Find the n-th col descr entry
//
//---------------------------------------------------------------------------
const CDXLColDescr *
CTranslatorUtils::GetColumnDescrAt(const CDXLTableDescr *table_descr, ULONG pos)
{
GPOS_ASSERT(0 != pos);
GPOS_ASSERT(pos < table_descr->Arity());
return table_descr->GetColumnDescrAt(pos);
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetScanDirection
//
// @doc:
// Return the GPDB specific scan direction from its corresponding DXL
// representation
//
//---------------------------------------------------------------------------
ScanDirection
CTranslatorUtils::GetScanDirection(EdxlIndexScanDirection idx_scan_direction)
{
if (EdxlisdBackward == idx_scan_direction)
{
return BackwardScanDirection;
}
if (EdxlisdForward == idx_scan_direction)
{
return ForwardScanDirection;
}
return NoMovementScanDirection;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetSetOpType
//
// @doc:
// Return the DXL representation of the set operation
//
//---------------------------------------------------------------------------
EdxlSetOpType
CTranslatorUtils::GetSetOpType(SetOperation setop, BOOL is_all)
{
if (SETOP_UNION == setop && is_all)
{
return EdxlsetopUnionAll;
}
if (SETOP_INTERSECT == setop && is_all)
{
return EdxlsetopIntersectAll;
}
if (SETOP_EXCEPT == setop && is_all)
{
return EdxlsetopDifferenceAll;
}
if (SETOP_UNION == setop)
{
return EdxlsetopUnion;
}
if (SETOP_INTERSECT == setop)
{
return EdxlsetopIntersect;
}
if (SETOP_EXCEPT == setop)
{
return EdxlsetopDifference;
}
GPOS_ASSERT(!"Unrecognized set operator type");
return EdxlsetopSentinel;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetGroupingColidArray
//
// @doc:
// Construct a dynamic array of column ids for the given set of grouping
// col attnos
//
//---------------------------------------------------------------------------
ULongPtrArray *
CTranslatorUtils::GetGroupingColidArray(
CMemoryPool *mp, CBitSet *group_by_cols,
IntToUlongMap *sort_group_cols_to_colid_map)
{
ULongPtrArray *colids = GPOS_NEW(mp) ULongPtrArray(mp);
if (nullptr != group_by_cols)
{
CBitSetIter bsi(*group_by_cols);
while (bsi.Advance())
{
const ULONG colid =
GetColId(bsi.Bit(), sort_group_cols_to_colid_map);
colids->Append(GPOS_NEW(mp) ULONG(colid));
}
}
return colids;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorUtils::GetColumnAttnosForGroupBy
//
// @doc:
// Construct a dynamic array of sets of column attnos corresponding to the
// group by clause
//
//---------------------------------------------------------------------------
CBitSetArray *
CTranslatorUtils::GetColumnAttnosForGroupBy(
CMemoryPool *mp, List *group_clause_list, List *grouping_set_list,
bool grouping_distinct,
ULONG num_cols,
UlongToUlongMap *
group_col_pos, // mapping of grouping col positions to SortGroupRef ids
CBitSet *group_cols // existing uniqueue grouping columns
)
{
GPOS_ASSERT(nullptr != group_col_pos);
if (NIL == grouping_set_list)
{
// simple group by
CBitSet *col_attnos = CreateAttnoSetForGroupingSet(
mp, group_clause_list, num_cols, group_col_pos, group_cols,
true /* use_group_clause */);
CBitSetArray *col_attnos_arr = GPOS_NEW(mp) CBitSetArray(mp);
col_attnos_arr->Append(col_attnos);
return col_attnos_arr;
}
GPOS_ASSERT(0 < gpdb::ListLength(grouping_set_list));
CBitSetArray *col_attnos_arr = GPOS_NEW(mp) CBitSetArray(mp);
ListCell *cell = nullptr;
ForEach(cell, grouping_set_list)
{
Node *node = (Node *) lfirst(cell);
GPOS_ASSERT(nullptr != node && IsA(node, GroupingSet));
GroupingSet *grouping_set = (GroupingSet *) node;
CBitSetArray *col_attnos_arr_current = nullptr;
switch (grouping_set->kind)
{
case GROUPING_SET_EMPTY:
{
col_attnos_arr_current = GPOS_NEW(mp) CBitSetArray(mp);
CBitSet *bset = GPOS_NEW(mp) CBitSet(mp);
col_attnos_arr_current->Append(bset);
break;
}
case GROUPING_SET_ROLLUP:
{
col_attnos_arr_current = CreateGroupingSetsForRollup(
mp, grouping_set, num_cols, group_cols, group_col_pos);
break;
}
case GROUPING_SET_CUBE:
{
col_attnos_arr_current = CreateGroupingSetsForCube(
mp, grouping_set, num_cols, group_cols, group_col_pos);
break;
}
case GROUPING_SET_SETS:
{
col_attnos_arr_current = CreateGroupingSetsForSets(
mp, grouping_set, num_cols, group_cols, group_col_pos);
break;
}
case GROUPING_SET_SIMPLE:
{
col_attnos_arr_current = CreateGroupingSetsForSimple(
mp, grouping_set, num_cols, group_cols, group_col_pos);
break;
}
default:
{
/* can't happen */
GPOS_RAISE(gpdxl::ExmaDXL, gpdxl::ExmiQuery2DXLError,
GPOS_WSZ_LIT("Unrecognized grouping set kind"));
}
}
// Multiple grouping set specs is implemented as the pairwise
// concatenation of the individual elements of the different grouping
// sets. Here we blend the last computed grouping set spec
// (col_attnos_arr_current) into the cumulated result (col_attnos_arr).
ULONG col_attnos_arr_size = col_attnos_arr->Size();
if (col_attnos_arr_size > 0)
{
CBitSetArray *col_attnos_arr_temp = GPOS_NEW(mp) CBitSetArray(mp);
for (ULONG ul = 0; ul < col_attnos_arr_size; ul++)
{
for (ULONG ulInner = 0;
ulInner < col_attnos_arr_current->Size(); ulInner++)
{
CBitSet *bset =
GPOS_NEW(mp) CBitSet(mp, *(*col_attnos_arr)[ul]);
bset->Union((*col_attnos_arr_current)[ulInner]);
col_attnos_arr_temp->Append(bset);
}
}
col_attnos_arr_current->Release();
col_attnos_arr->Release();
col_attnos_arr = col_attnos_arr_temp;
}
else
{
col_attnos_arr->Release();
col_attnos_arr = col_attnos_arr_current;
}
}
// Deduplicate the grouping sets result
// Can't do dedup when building the `col_attnos_arr`
if (grouping_distinct)
{
CBitSetArray *col_attnos_arr_dedup = GPOS_NEW(mp) CBitSetArray(mp);
for (ULONG ul = 0; ul < col_attnos_arr->Size(); ul++)
{