-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCTranslatorDXLToExpr.cpp
More file actions
4205 lines (3584 loc) · 131 KB
/
Copy pathCTranslatorDXLToExpr.cpp
File metadata and controls
4205 lines (3584 loc) · 131 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) 2011 EMC Corp.
//
// @filename:
// CTranslatorDXLToExpr.cpp
//
// @doc:
// Implementation of the methods used to translate a DXL tree into Expr tree.
// All translator methods allocate memory in the provided memory pool, and
// the caller is responsible for freeing it.
//---------------------------------------------------------------------------
#include "gpopt/translate/CTranslatorDXLToExpr.h"
#include "gpos/common/CAutoTimer.h"
#include "gpopt/base/CAutoOptCtxt.h"
#include "gpopt/base/CColRef.h"
#include "gpopt/base/CColRefSet.h"
#include "gpopt/base/CColumnFactory.h"
#include "gpopt/base/CDistributionSpecAny.h"
#include "gpopt/base/CEnfdDistribution.h"
#include "gpopt/base/CEnfdOrder.h"
#include "gpopt/base/CUtils.h"
#include "gpopt/exception.h"
#include "gpopt/mdcache/CMDAccessorUtils.h"
#include "gpopt/metadata/CColumnDescriptor.h"
#include "gpopt/metadata/CTableDescriptor.h"
#include "gpopt/operators/CLogicalCTEAnchor.h"
#include "gpopt/operators/CLogicalCTEConsumer.h"
#include "gpopt/operators/CLogicalCTEProducer.h"
#include "gpopt/operators/CLogicalDelete.h"
#include "gpopt/operators/CLogicalDifference.h"
#include "gpopt/operators/CLogicalDifferenceAll.h"
#include "gpopt/operators/CLogicalDynamicGet.h"
#include "gpopt/operators/CLogicalForeignGet.h"
#include "gpopt/operators/CLogicalGbAgg.h"
#include "gpopt/operators/CLogicalGet.h"
#include "gpopt/operators/CLogicalInnerApply.h"
#include "gpopt/operators/CLogicalInsert.h"
#include "gpopt/operators/CLogicalLeftOuterApply.h"
#include "gpopt/operators/CLogicalIntersect.h"
#include "gpopt/operators/CLogicalIntersectAll.h"
#include "gpopt/operators/CLogicalLimit.h"
#include "gpopt/operators/CLogicalProject.h"
#include "gpopt/operators/CLogicalSelect.h"
#include "gpopt/operators/CLogicalSequenceProject.h"
#include "gpopt/operators/CLogicalSetOp.h"
#include "gpopt/operators/CLogicalTVF.h"
#include "gpopt/operators/CLogicalUnion.h"
#include "gpopt/operators/CLogicalUnionAll.h"
#include "gpopt/operators/CLogicalUpdate.h"
#include "gpopt/operators/CScalarArray.h"
#include "gpopt/operators/CScalarArrayCoerceExpr.h"
#include "gpopt/operators/CScalarArrayRef.h"
#include "gpopt/operators/CScalarBooleanTest.h"
#include "gpopt/operators/CScalarCaseTest.h"
#include "gpopt/operators/CScalarCast.h"
#include "gpopt/operators/CScalarCoalesce.h"
#include "gpopt/operators/CScalarCoerceToDomain.h"
#include "gpopt/operators/CScalarCoerceViaIO.h"
#include "gpopt/operators/CScalarFieldSelect.h"
#include "gpopt/operators/CScalarIdent.h"
#include "gpopt/operators/CScalarIf.h"
#include "gpopt/operators/CScalarIsDistinctFrom.h"
#include "gpopt/operators/CScalarMinMax.h"
#include "gpopt/operators/CScalarNullIf.h"
#include "gpopt/operators/CScalarNullTest.h"
#include "gpopt/operators/CScalarOp.h"
#include "gpopt/operators/CScalarParam.h"
#include "gpopt/operators/CScalarProjectElement.h"
#include "gpopt/operators/CScalarProjectList.h"
#include "gpopt/operators/CScalarSortGroupClause.h"
#include "gpopt/operators/CScalarSubquery.h"
#include "gpopt/operators/CScalarSubqueryAll.h"
#include "gpopt/operators/CScalarSubqueryAny.h"
#include "gpopt/operators/CScalarSubqueryExists.h"
#include "gpopt/operators/CScalarSubqueryNotExists.h"
#include "gpopt/operators/CScalarSwitch.h"
#include "gpopt/operators/CScalarSwitchCase.h"
#include "gpopt/operators/CScalarValuesList.h"
#include "gpopt/translate/CTranslatorExprToDXLUtils.h"
#include "naucrates/dxl/operators/CDXLCtasStorageOptions.h"
#include "naucrates/dxl/operators/CDXLLogicalCTAS.h"
#include "naucrates/dxl/operators/CDXLLogicalCTEAnchor.h"
#include "naucrates/dxl/operators/CDXLLogicalCTEConsumer.h"
#include "naucrates/dxl/operators/CDXLLogicalCTEProducer.h"
#include "naucrates/dxl/operators/CDXLLogicalConstTable.h"
#include "naucrates/dxl/operators/CDXLLogicalDelete.h"
#include "naucrates/dxl/operators/CDXLLogicalGet.h"
#include "naucrates/dxl/operators/CDXLLogicalGroupBy.h"
#include "naucrates/dxl/operators/CDXLLogicalInsert.h"
#include "naucrates/dxl/operators/CDXLLogicalJoin.h"
#include "naucrates/dxl/operators/CDXLLogicalLimit.h"
#include "naucrates/dxl/operators/CDXLLogicalSetOp.h"
#include "naucrates/dxl/operators/CDXLLogicalTVF.h"
#include "naucrates/dxl/operators/CDXLLogicalUpdate.h"
#include "naucrates/dxl/operators/CDXLLogicalWindow.h"
#include "naucrates/dxl/operators/CDXLScalarAggref.h"
#include "naucrates/dxl/operators/CDXLScalarArray.h"
#include "naucrates/dxl/operators/CDXLScalarArrayCoerceExpr.h"
#include "naucrates/dxl/operators/CDXLScalarArrayComp.h"
#include "naucrates/dxl/operators/CDXLScalarArrayRef.h"
#include "naucrates/dxl/operators/CDXLScalarBooleanTest.h"
#include "naucrates/dxl/operators/CDXLScalarCaseTest.h"
#include "naucrates/dxl/operators/CDXLScalarCast.h"
#include "naucrates/dxl/operators/CDXLScalarCoalesce.h"
#include "naucrates/dxl/operators/CDXLScalarCoerceToDomain.h"
#include "naucrates/dxl/operators/CDXLScalarCoerceViaIO.h"
#include "naucrates/dxl/operators/CDXLScalarDistinctComp.h"
#include "naucrates/dxl/operators/CDXLScalarFieldSelect.h"
#include "naucrates/dxl/operators/CDXLScalarFuncExpr.h"
#include "naucrates/dxl/operators/CDXLScalarIdent.h"
#include "naucrates/dxl/operators/CDXLScalarIfStmt.h"
#include "naucrates/dxl/operators/CDXLScalarMinMax.h"
#include "naucrates/dxl/operators/CDXLScalarNullIf.h"
#include "naucrates/dxl/operators/CDXLScalarNullTest.h"
#include "naucrates/dxl/operators/CDXLScalarOpExpr.h"
#include "naucrates/dxl/operators/CDXLScalarParam.h"
#include "naucrates/dxl/operators/CDXLScalarProjElem.h"
#include "naucrates/dxl/operators/CDXLScalarSortCol.h"
#include "naucrates/dxl/operators/CDXLScalarSortGroupClause.h"
#include "naucrates/dxl/operators/CDXLScalarSubquery.h"
#include "naucrates/dxl/operators/CDXLScalarSubqueryQuantified.h"
#include "naucrates/dxl/operators/CDXLScalarSwitch.h"
#include "naucrates/exception.h"
#include "naucrates/md/CMDArrayCoerceCastGPDB.h"
#include "naucrates/md/CMDProviderMemory.h"
#include "naucrates/md/CMDRelationCtasGPDB.h"
#include "naucrates/md/IMDAggregate.h"
#include "naucrates/md/IMDCast.h"
#include "naucrates/md/IMDFunction.h"
#include "naucrates/md/IMDId.h"
#include "naucrates/md/IMDRelation.h"
#include "naucrates/md/IMDScalarOp.h"
#include "naucrates/traceflags/traceflags.h"
#define GPDB_DENSE_RANK_OID 7002
#define GPDB_PERCENT_RANK_OID 7003
#define GPDB_CUME_DIST_OID 7004
#define GPDB_NTILE_INT4_OID 7005
#define GPDB_NTILE_INT8_OID 7006
#define GPDB_NTILE_NUMERIC_OID 7007
#define GPOPT_ACTION_INSERT 0
#define GPOPT_ACTION_DELETE 1
using namespace gpos;
using namespace gpnaucrates;
using namespace gpmd;
using namespace gpdxl;
using namespace gpopt;
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::CTranslatorDXLToExpr
//
// @doc:
// Ctor
//
//---------------------------------------------------------------------------
CTranslatorDXLToExpr::CTranslatorDXLToExpr(CMemoryPool *mp,
CMDAccessor *md_accessor,
BOOL fInitColumnFactory)
: m_mp(mp),
m_sysid(IMDId::EmdidGeneral, GPMD_GPDB_SYSID),
m_pmda(md_accessor),
m_phmulcr(nullptr),
m_phmululCTE(nullptr),
m_pdrgpulOutputColRefs(nullptr),
m_pdrgpmdname(nullptr),
m_phmulpdxlnCTEProducer(nullptr),
m_ulCTEId(gpos::ulong_max),
m_pcf(nullptr)
{
// initialize hash tables
m_phmulcr = GPOS_NEW(m_mp) UlongToColRefMap(m_mp);
// initialize hash tables
m_phmululCTE = GPOS_NEW(m_mp) UlongToUlongMap(m_mp);
if (fInitColumnFactory)
{
// get column factory from optimizer context object
m_pcf = COptCtxt::PoctxtFromTLS()->Pcf();
GPOS_ASSERT(nullptr != m_pcf);
}
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::~CTranslatorDXLToExpr
//
// @doc:
// Dtor
//
//---------------------------------------------------------------------------
CTranslatorDXLToExpr::~CTranslatorDXLToExpr()
{
m_phmulcr->Release();
m_phmululCTE->Release();
CRefCount::SafeRelease(m_pdrgpulOutputColRefs);
CRefCount::SafeRelease(m_pdrgpmdname);
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PdrgpulOutputColRefs
//
// @doc:
// Return the array of query output column reference id
//
//---------------------------------------------------------------------------
ULongPtrArray *
CTranslatorDXLToExpr::PdrgpulOutputColRefs()
{
GPOS_ASSERT(nullptr != m_pdrgpulOutputColRefs);
return m_pdrgpulOutputColRefs;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::Pexpr
//
// @doc:
// Translate a DXL tree into an Expr Tree
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::Pexpr(const CDXLNode *dxlnode,
const CDXLNodeArray *query_output_dxlnode_array,
const CDXLNodeArray *cte_producers)
{
GPOS_ASSERT(nullptr == m_pdrgpulOutputColRefs);
GPOS_ASSERT(nullptr == m_phmulpdxlnCTEProducer);
GPOS_ASSERT(nullptr != dxlnode && nullptr != dxlnode->GetOperator());
GPOS_ASSERT(nullptr != query_output_dxlnode_array);
m_phmulpdxlnCTEProducer = GPOS_NEW(m_mp) IdToCDXLNodeMap(m_mp);
const ULONG ulCTEs = cte_producers->Size();
for (ULONG ul = 0; ul < ulCTEs; ul++)
{
CDXLNode *pdxlnCTE = (*cte_producers)[ul];
CDXLLogicalCTEProducer *pdxlopCTEProducer =
CDXLLogicalCTEProducer::Cast(pdxlnCTE->GetOperator());
pdxlnCTE->AddRef();
BOOL fres GPOS_ASSERTS_ONLY = m_phmulpdxlnCTEProducer->Insert(
GPOS_NEW(m_mp) ULONG(pdxlopCTEProducer->Id()), pdxlnCTE);
GPOS_ASSERT(fres);
}
// translate main DXL tree
CExpression *pexpr = Pexpr(dxlnode);
GPOS_ASSERT(nullptr != pexpr);
m_phmulpdxlnCTEProducer->Release();
m_phmulpdxlnCTEProducer = nullptr;
// generate the array of output column reference ids and column names
m_pdrgpulOutputColRefs = GPOS_NEW(m_mp) ULongPtrArray(m_mp);
m_pdrgpmdname = GPOS_NEW(m_mp) CMDNameArray(m_mp);
BOOL fGenerateRequiredColumns =
COperator::EopLogicalUpdate != pexpr->Pop()->Eopid();
const ULONG length = query_output_dxlnode_array->Size();
for (ULONG ul = 0; ul < length; ul++)
{
CDXLNode *pdxlnIdent = (*query_output_dxlnode_array)[ul];
// get dxl scalar identifier
CDXLScalarIdent *pdxlopIdent =
CDXLScalarIdent::Cast(pdxlnIdent->GetOperator());
// get the dxl column reference
const CDXLColRef *dxl_colref = pdxlopIdent->GetDXLColRef();
GPOS_ASSERT(nullptr != dxl_colref);
const ULONG colid = dxl_colref->Id();
// get its column reference from the hash map
const CColRef *colref = LookupColRef(m_phmulcr, colid);
if (fGenerateRequiredColumns)
{
const ULONG ulColRefId = colref->Id();
ULONG *pulCopy = GPOS_NEW(m_mp) ULONG(ulColRefId);
// add to the array of output column reference ids
m_pdrgpulOutputColRefs->Append(pulCopy);
// get the column names and add it to the array of output column names
CMDName *mdname =
GPOS_NEW(m_mp) CMDName(m_mp, dxl_colref->MdName()->GetMDName());
m_pdrgpmdname->Append(mdname);
}
}
return pexpr;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::Pexpr
//
// @doc:
// Translates a DXL tree into a Expr Tree
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::Pexpr(const CDXLNode *dxlnode)
{
// recursive function - check stack
GPOS_CHECK_STACK_SIZE;
GPOS_ASSERT(nullptr != dxlnode && nullptr != dxlnode->GetOperator());
CDXLOperator *dxl_op = dxlnode->GetOperator();
CExpression *pexpr = nullptr;
switch (dxl_op->GetDXLOperatorType())
{
case EdxloptypeLogical:
pexpr = PexprLogical(dxlnode);
break;
case EdxloptypeScalar:
pexpr = PexprScalar(dxlnode);
break;
default:
GPOS_RAISE(gpopt::ExmaGPOPT, gpopt::ExmiUnsupportedOp,
dxlnode->GetOperator()->GetOpNameStr()->GetBuffer());
}
return pexpr;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PexprTranslateQuery
//
// @doc:
// Main driver for translating dxl query with its associated output
// columns and CTEs
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::PexprTranslateQuery(
const CDXLNode *dxlnode, const CDXLNodeArray *query_output_dxlnode_array,
const CDXLNodeArray *cte_producers)
{
CAutoTimer at("\n[OPT]: DXL To Expr Translation Time",
GPOS_FTRACE(EopttracePrintOptimizationStatistics));
CExpression *pexpr =
Pexpr(dxlnode, query_output_dxlnode_array, cte_producers);
// We need to mark all the colrefs which are not being referenced in the query as unused.
// This needs to be done here after translating since we won't know which columns are
// required until entire DXL tree has been processed
MarkUnknownColsAsUnused();
return pexpr;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PexprTranslateScalar
//
// @doc:
// Translate a dxl scalar expression
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::PexprTranslateScalar(const CDXLNode *dxlnode,
CColRefArray *colref_array,
ULongPtrArray *pdrgpul)
{
GPOS_ASSERT_IMP(nullptr != pdrgpul, nullptr != colref_array);
GPOS_ASSERT_IMP(nullptr != pdrgpul,
pdrgpul->Size() == colref_array->Size());
if (EdxloptypeScalar != dxlnode->GetOperator()->GetDXLOperatorType())
{
GPOS_RAISE(gpopt::ExmaGPOPT, gpopt::ExmiUnexpectedOp,
dxlnode->GetOperator()->GetOpNameStr()->GetBuffer());
}
if (nullptr != colref_array)
{
const ULONG length = colref_array->Size();
for (ULONG ul = 0; ul < length; ul++)
{
CColRef *colref = (*colref_array)[ul];
// copy key
ULONG *pulKey = nullptr;
if (nullptr == pdrgpul)
{
pulKey = GPOS_NEW(m_mp) ULONG(ul + 1);
}
else
{
pulKey = GPOS_NEW(m_mp) ULONG(*((*pdrgpul)[ul]) + 1);
}
#ifdef GPOS_DEBUG
BOOL fres =
#endif // GPOS_DEBUG
m_phmulcr->Insert(pulKey, colref);
GPOS_ASSERT(fres);
}
}
return PexprScalar(dxlnode);
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PexprLogical
//
// @doc:
// Translates a DXL Logical Op into a Expr
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::PexprLogical(const CDXLNode *dxlnode)
{
// recursive function - check stack
GPOS_CHECK_STACK_SIZE;
GPOS_ASSERT(nullptr != dxlnode);
GPOS_ASSERT(EdxloptypeLogical ==
dxlnode->GetOperator()->GetDXLOperatorType());
CDXLOperator *dxl_op = dxlnode->GetOperator();
switch (dxl_op->GetDXLOperator())
{
case EdxlopLogicalGet:
case EdxlopLogicalForeignGet:
return CTranslatorDXLToExpr::PexprLogicalGet(dxlnode);
case EdxlopLogicalTVF:
return CTranslatorDXLToExpr::PexprLogicalTVF(dxlnode);
case EdxlopLogicalSelect:
return CTranslatorDXLToExpr::PexprLogicalSelect(dxlnode);
case EdxlopLogicalProject:
return CTranslatorDXLToExpr::PexprLogicalProject(dxlnode);
case EdxlopLogicalCTEAnchor:
return CTranslatorDXLToExpr::PexprLogicalCTEAnchor(dxlnode);
case EdxlopLogicalCTEProducer:
return CTranslatorDXLToExpr::PexprLogicalCTEProducer(dxlnode);
case EdxlopLogicalCTEConsumer:
return CTranslatorDXLToExpr::PexprLogicalCTEConsumer(dxlnode);
case EdxlopLogicalGrpBy:
return CTranslatorDXLToExpr::PexprLogicalGroupBy(dxlnode);
case EdxlopLogicalLimit:
return CTranslatorDXLToExpr::PexprLogicalLimit(dxlnode);
case EdxlopLogicalJoin:
return CTranslatorDXLToExpr::PexprLogicalJoin(dxlnode);
case EdxlopLogicalConstTable:
return CTranslatorDXLToExpr::PexprLogicalConstTableGet(dxlnode);
case EdxlopLogicalSetOp:
return CTranslatorDXLToExpr::PexprLogicalSetOp(dxlnode);
case EdxlopLogicalWindow:
return CTranslatorDXLToExpr::PexprLogicalSeqPr(dxlnode);
case EdxlopLogicalInsert:
return CTranslatorDXLToExpr::PexprLogicalInsert(dxlnode);
case EdxlopLogicalDelete:
return CTranslatorDXLToExpr::PexprLogicalDelete(dxlnode);
case EdxlopLogicalUpdate:
return CTranslatorDXLToExpr::PexprLogicalUpdate(dxlnode);
case EdxlopLogicalCTAS:
return CTranslatorDXLToExpr::PexprLogicalCTAS(dxlnode);
default:
GPOS_RAISE(gpopt::ExmaGPOPT, gpopt::ExmiUnsupportedOp,
dxl_op->GetOpNameStr()->GetBuffer());
return nullptr;
}
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PexprLogicalTVF
//
// @doc:
// Create a logical TVF expression from its DXL representation
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::PexprLogicalTVF(const CDXLNode *dxlnode)
{
CDXLLogicalTVF *dxl_op = CDXLLogicalTVF::Cast(dxlnode->GetOperator());
GPOS_ASSERT(nullptr != dxl_op->MdName()->GetMDName());
// populate column information
const ULONG ulColumns = dxl_op->Arity();
GPOS_ASSERT(0 < ulColumns);
CColumnDescriptorArray *pdrgpcoldesc =
GPOS_NEW(m_mp) CColumnDescriptorArray(m_mp);
for (ULONG ul = 0; ul < ulColumns; ul++)
{
const CDXLColDescr *pdxlcoldesc = dxl_op->GetColumnDescrAt(ul);
GPOS_ASSERT_FIXME(pdxlcoldesc->MdidType()->IsValid());
const IMDType *pmdtype = m_pmda->RetrieveType(pdxlcoldesc->MdidType());
GPOS_ASSERT(nullptr != pdxlcoldesc->MdName()->GetMDName()->GetBuffer());
CWStringConst strColName(
m_mp, pdxlcoldesc->MdName()->GetMDName()->GetBuffer());
INT attrnum = pdxlcoldesc->AttrNum();
CColumnDescriptor *pcoldesc = GPOS_NEW(m_mp)
CColumnDescriptor(m_mp, pmdtype, pdxlcoldesc->TypeModifier(),
CName(m_mp, &strColName), attrnum,
true, // is_nullable
pdxlcoldesc->Width());
pdrgpcoldesc->Append(pcoldesc);
}
// create a logical TVF operator
IMDId *mdid_func = dxl_op->FuncMdId();
mdid_func->AddRef();
IMDId *mdid_return_type = dxl_op->ReturnTypeMdId();
mdid_return_type->AddRef();
CLogicalTVF *popTVF = GPOS_NEW(m_mp) CLogicalTVF(
m_mp, mdid_func, mdid_return_type,
GPOS_NEW(m_mp)
CWStringConst(m_mp, dxl_op->MdName()->GetMDName()->GetBuffer()),
pdrgpcoldesc);
// create expression containing the logical TVF operator
CExpression *pexpr = nullptr;
const ULONG arity = dxlnode->Arity();
if (0 < arity)
{
// translate function arguments
CExpressionArray *pdrgpexprArgs = PdrgpexprChildren(dxlnode);
pexpr = GPOS_NEW(m_mp) CExpression(m_mp, popTVF, pdrgpexprArgs);
}
else
{
// function has no arguments
pexpr = GPOS_NEW(m_mp) CExpression(m_mp, popTVF);
}
// construct the mapping between the DXL ColId and CColRef
ConstructDXLColId2ColRefMapping(dxl_op->GetDXLColumnDescrArray(),
popTVF->PdrgpcrOutput());
if (!popTVF->FuncMdId()->IsValid())
{
return pexpr;
}
const IMDFunction *pmdfunc = m_pmda->RetrieveFunc(mdid_func);
if (IMDFunction::EfsVolatile == pmdfunc->GetFuncStability())
{
COptCtxt::PoctxtFromTLS()->SetHasVolatileFunc();
}
return pexpr;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PexprLogicalGet
//
// @doc:
// Create a Expr logical get from a DXL logical get
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::PexprLogicalGet(const CDXLNode *dxlnode)
{
CDXLOperator *dxl_op = dxlnode->GetOperator();
Edxlopid edxlopid = dxl_op->GetDXLOperator();
// translate the table descriptor
CDXLTableDescr *table_descr =
CDXLLogicalGet::Cast(dxl_op)->GetDXLTableDescr();
BOOL hasSecurityQuals = CDXLLogicalGet::Cast(dxl_op)->HasSecurityQuals();
GPOS_ASSERT(nullptr != table_descr);
GPOS_ASSERT(nullptr != table_descr->MdName()->GetMDName());
CTableDescriptor *ptabdesc = Ptabdesc(table_descr);
CWStringConst strAlias(m_mp,
table_descr->MdName()->GetMDName()->GetBuffer());
// create a logical get or dynamic get operator
CName *pname = GPOS_NEW(m_mp) CName(m_mp, CName(&strAlias));
CLogical *popGet = nullptr;
CColRefArray *colref_array = nullptr;
const IMDRelation *pmdrel = m_pmda->RetrieveRel(table_descr->MDId());
if (pmdrel->IsPartitioned())
{
GPOS_ASSERT(EdxlopLogicalGet == edxlopid ||
EdxlopLogicalForeignGet == edxlopid);
IMdIdArray *partition_mdids = pmdrel->ChildPartitionMdids();
IMdIdArray *foreign_server_mdids = GPOS_NEW(m_mp) IMdIdArray(m_mp);
for (ULONG ul = 0; ul < partition_mdids->Size(); ++ul)
{
IMDId *part_mdid = (*partition_mdids)[ul];
const IMDRelation *partrel = m_pmda->RetrieveRel(part_mdid);
if (partrel->IsPartitioned())
{
// Multi-level partitioned tables are unsupported - fall back
GPOS_RAISE(gpdxl::ExmaMD, gpdxl::ExmiMDObjUnsupported,
GPOS_WSZ_LIT("Multi-level partitioned tables"));
}
// store array of foreign partitions
IMDId *foreign_server_mdid = nullptr;
if (IMDRelation::ErelstorageForeign ==
partrel->RetrieveRelStorageType())
{
foreign_server_mdid = partrel->ForeignServer();
foreign_server_mdid->AddRef();
}
else
{
// not foreign, store as invalid mdid
foreign_server_mdid =
GPOS_NEW(m_mp) CMDIdGPDB(CMDIdGPDB::m_mdid_invalid_key);
}
foreign_server_mdids->Append(foreign_server_mdid);
}
// generate a part index id
ULONG part_idx_id = COptCtxt::PoctxtFromTLS()->UlPartIndexNextVal();
partition_mdids->AddRef();
popGet = GPOS_NEW(m_mp) CLogicalDynamicGet(
m_mp, pname, ptabdesc, part_idx_id, partition_mdids,
foreign_server_mdids, hasSecurityQuals);
CLogicalDynamicGet *popDynamicGet =
CLogicalDynamicGet::PopConvert(popGet);
// get the output column references from the dynamic get
colref_array = popDynamicGet->PdrgpcrOutput();
}
else
{
if (EdxlopLogicalGet == edxlopid)
{
popGet = GPOS_NEW(m_mp)
CLogicalGet(m_mp, pname, ptabdesc, hasSecurityQuals);
}
else
{
GPOS_ASSERT(EdxlopLogicalForeignGet == edxlopid);
popGet = GPOS_NEW(m_mp) CLogicalForeignGet(m_mp, pname, ptabdesc);
}
// get the output column references
colref_array = CLogicalGet::PopConvert(popGet)->PdrgpcrOutput();
}
CExpression *pexpr = GPOS_NEW(m_mp) CExpression(m_mp, popGet);
GPOS_ASSERT(nullptr != colref_array);
GPOS_ASSERT(colref_array->Size() == table_descr->Arity());
const ULONG ulColumns = colref_array->Size();
// construct the mapping between the DXL ColId and CColRef
for (ULONG ul = 0; ul < ulColumns; ul++)
{
CColRef *colref = (*colref_array)[ul];
const CDXLColDescr *pdxlcd = table_descr->GetColumnDescrAt(ul);
GPOS_ASSERT(nullptr != colref);
GPOS_ASSERT(nullptr != pdxlcd && !pdxlcd->IsDropped());
// copy key
ULONG *pulKey = GPOS_NEW(m_mp) ULONG(pdxlcd->Id());
BOOL fres = m_phmulcr->Insert(pulKey, colref);
colref->SetMdidTable(ptabdesc->MDId());
if (!fres)
{
GPOS_DELETE(pulKey);
}
}
return pexpr;
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PexprLogicalSetOp
//
// @doc:
// Create a logical set operator from a DXL set operator
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::PexprLogicalSetOp(const CDXLNode *dxlnode)
{
GPOS_ASSERT(nullptr != dxlnode);
CDXLLogicalSetOp *dxl_op = CDXLLogicalSetOp::Cast(dxlnode->GetOperator());
#ifdef GPOS_DEBUG
const ULONG arity = dxlnode->Arity();
#endif // GPOS_DEBUG
GPOS_ASSERT(2 <= arity);
GPOS_ASSERT(arity == dxl_op->ChildCount());
// array of input column reference
CColRef2dArray *pdrgdrgpcrInput = GPOS_NEW(m_mp) CColRef2dArray(m_mp);
// array of output column descriptors
ULongPtrArray *pdrgpulOutput = GPOS_NEW(m_mp) ULongPtrArray(m_mp);
CExpressionArray *pdrgpexpr =
PdrgpexprPreprocessSetOpInputs(dxlnode, pdrgdrgpcrInput, pdrgpulOutput);
// create an array of output column references
CColRefArray *pdrgpcrOutput = CTranslatorDXLToExprUtils::Pdrgpcr(
m_mp, m_phmulcr, pdrgpulOutput /*array of colids of the first child*/);
pdrgpulOutput->Release();
CLogicalSetOp *pop = nullptr;
switch (dxl_op->GetSetOpType())
{
case EdxlsetopUnion:
{
pop = GPOS_NEW(m_mp)
CLogicalUnion(m_mp, pdrgpcrOutput, pdrgdrgpcrInput);
break;
}
case EdxlsetopUnionAll:
{
pop = GPOS_NEW(m_mp)
CLogicalUnionAll(m_mp, pdrgpcrOutput, pdrgdrgpcrInput);
break;
}
case EdxlsetopDifference:
{
pop = GPOS_NEW(m_mp)
CLogicalDifference(m_mp, pdrgpcrOutput, pdrgdrgpcrInput);
break;
}
case EdxlsetopIntersect:
{
pop = GPOS_NEW(m_mp)
CLogicalIntersect(m_mp, pdrgpcrOutput, pdrgdrgpcrInput);
break;
}
case EdxlsetopDifferenceAll:
{
pop = GPOS_NEW(m_mp)
CLogicalDifferenceAll(m_mp, pdrgpcrOutput, pdrgdrgpcrInput);
break;
}
case EdxlsetopIntersectAll:
{
pop = GPOS_NEW(m_mp)
CLogicalIntersectAll(m_mp, pdrgpcrOutput, pdrgdrgpcrInput);
break;
}
default:
GPOS_RAISE(gpopt::ExmaGPOPT, gpopt::ExmiUnsupportedOp,
dxl_op->GetOpNameStr()->GetBuffer());
}
GPOS_ASSERT(nullptr != pop);
return GPOS_NEW(m_mp) CExpression(m_mp, pop, pdrgpexpr);
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PexprCastPrjElem
//
// @doc:
// Return a project element on a cast expression
//
//---------------------------------------------------------------------------
CExpression *
CTranslatorDXLToExpr::PexprCastPrjElem(IMDId *pmdidSource, IMDId *mdid_dest,
const CColRef *pcrToCast,
CColRef *pcrToReturn)
{
const IMDCast *pmdcast = m_pmda->Pmdcast(pmdidSource, mdid_dest);
mdid_dest->AddRef();
pmdcast->GetCastFuncMdId()->AddRef();
CExpression *pexprCast;
if (pmdcast->GetMDPathType() == IMDCast::EmdtArrayCoerce)
{
CMDArrayCoerceCastGPDB *parrayCoerceCast =
(CMDArrayCoerceCastGPDB *) pmdcast;
IMDId *mdid_func = pmdcast->GetCastFuncMdId();
CExpression *pexprCastScalarFunc = CUtils::PexprFuncElemExpr(
m_mp, m_pmda, mdid_func, parrayCoerceCast->GetSrcElemTypeMdId(),
parrayCoerceCast->TypeModifier());
pexprCast = GPOS_NEW(m_mp) CExpression(
m_mp,
GPOS_NEW(m_mp) CScalarArrayCoerceExpr(
m_mp, mdid_dest, parrayCoerceCast->TypeModifier(),
(COperator::ECoercionForm) parrayCoerceCast->GetCoercionForm(),
parrayCoerceCast->Location()),
GPOS_NEW(m_mp)
CExpression(m_mp, GPOS_NEW(m_mp) CScalarIdent(m_mp, pcrToCast)),
pexprCastScalarFunc);
}
else
{
pexprCast = GPOS_NEW(m_mp) CExpression(
m_mp,
GPOS_NEW(m_mp)
CScalarCast(m_mp, mdid_dest, pmdcast->GetCastFuncMdId(),
pmdcast->IsBinaryCoercible()),
GPOS_NEW(m_mp) CExpression(
m_mp, GPOS_NEW(m_mp) CScalarIdent(m_mp, pcrToCast)));
}
return GPOS_NEW(m_mp) CExpression(
m_mp, GPOS_NEW(m_mp) CScalarProjectElement(m_mp, pcrToReturn),
pexprCast);
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::BuildSetOpChild
//
// @doc:
// Build expression and input columns of SetOp Child
//
//---------------------------------------------------------------------------
void
CTranslatorDXLToExpr::BuildSetOpChild(
const CDXLNode *pdxlnSetOp, ULONG child_index,
CExpression **ppexprChild, // output: generated child expression
CColRefArray **ppdrgpcrChild, // output: generated child input columns
CExpressionArray **
ppdrgpexprChildProjElems // output: project elements to remap child input columns
)
{
GPOS_ASSERT(nullptr != pdxlnSetOp);
GPOS_ASSERT(nullptr != ppexprChild);
GPOS_ASSERT(nullptr != ppdrgpcrChild);
GPOS_ASSERT(nullptr != ppdrgpexprChildProjElems);
GPOS_ASSERT(nullptr == *ppdrgpexprChildProjElems);
const CDXLLogicalSetOp *dxl_op =
CDXLLogicalSetOp::Cast(pdxlnSetOp->GetOperator());
const CDXLNode *child_dxlnode = (*pdxlnSetOp)[child_index];
// array of project elements to remap child input columns
*ppdrgpexprChildProjElems = GPOS_NEW(m_mp) CExpressionArray(m_mp);
// array of child input column
*ppdrgpcrChild = GPOS_NEW(m_mp) CColRefArray(m_mp);
// translate child
*ppexprChild = PexprLogical(child_dxlnode);
const ULongPtrArray *pdrgpulInput =
dxl_op->GetInputColIdArrayAt(child_index);
const ULONG ulInputCols = pdrgpulInput->Size();
CColRefSet *pcrsChildOutput = (*ppexprChild)->DeriveOutputColumns();
for (ULONG ulColPos = 0; ulColPos < ulInputCols; ulColPos++)
{
// column identifier of the input column
ULONG colid = *(*pdrgpulInput)[ulColPos];
const CColRef *colref = LookupColRef(m_phmulcr, colid);
// corresponding output column descriptor
const CDXLColDescr *pdxlcdOutput = dxl_op->GetColumnDescrAt(ulColPos);
// check if a cast function needs to be introduced
IMDId *pmdidSource = colref->RetrieveType()->MDId();
IMDId *mdid_dest = pdxlcdOutput->MdidType();
if (FCastingUnknownType(pmdidSource, mdid_dest))
{
GPOS_RAISE(gpopt::ExmaGPOPT, gpopt::ExmiUnsupportedOp,
GPOS_WSZ_LIT("Casting of columns of unknown data type"));
}
const IMDType *pmdtype = m_pmda->RetrieveType(mdid_dest);
INT type_modifier = pdxlcdOutput->TypeModifier();
BOOL fEqualTypes = IMDId::MDIdCompare(pmdidSource, mdid_dest);
BOOL fFirstChild = (0 == child_index);
if (!pcrsChildOutput->FMember(colref))
{
// input column is an outer reference, add a project element for input column
// add the colref to the hash map between DXL ColId and colref as they can used above the setop
CColRef *new_colref = PcrCreate(colref, pmdtype, type_modifier,
fFirstChild, pdxlcdOutput->Id());
(*ppdrgpcrChild)->Append(new_colref);
CExpression *pexprChildProjElem = nullptr;
if (fEqualTypes)
{
// project child input column
pexprChildProjElem = GPOS_NEW(m_mp) CExpression(
m_mp,
GPOS_NEW(m_mp) CScalarProjectElement(m_mp, new_colref),
GPOS_NEW(m_mp) CExpression(
m_mp, GPOS_NEW(m_mp) CScalarIdent(m_mp, colref)));
}
else
{
// introduce cast expression
pexprChildProjElem = PexprCastPrjElem(pmdidSource, mdid_dest,
colref, new_colref);
}
(*ppdrgpexprChildProjElems)->Append(pexprChildProjElem);
continue;
}
if (fEqualTypes)
{
// no cast function needed, add the colref to the array of input colrefs
(*ppdrgpcrChild)->Append(const_cast<CColRef *>(colref));
}
else
{
// add the colref to the hash map between DXL ColId and colref as they can used above the setop
CColRef *new_colref = PcrCreate(colref, pmdtype, type_modifier,
fFirstChild, pdxlcdOutput->Id());
(*ppdrgpcrChild)->Append(new_colref);
// introduce cast expression for input column
CExpression *pexprChildProjElem =
PexprCastPrjElem(pmdidSource, mdid_dest, colref, new_colref);
(*ppdrgpexprChildProjElems)->Append(pexprChildProjElem);
}
}
}
//---------------------------------------------------------------------------
// @function:
// CTranslatorDXLToExpr::PdrgpexprPreprocessSetOpInputs
//
// @doc:
// Pre-process inputs to the set operator and add casting when needed
//
//---------------------------------------------------------------------------
CExpressionArray *
CTranslatorDXLToExpr::PdrgpexprPreprocessSetOpInputs(
const CDXLNode *dxlnode, CColRef2dArray *pdrgdrgpcrInput,
ULongPtrArray *pdrgpulOutput)
{
GPOS_ASSERT(nullptr != dxlnode);
GPOS_ASSERT(nullptr != pdrgdrgpcrInput);
GPOS_ASSERT(nullptr != pdrgpulOutput);
// array of child expression
CExpressionArray *pdrgpexpr = GPOS_NEW(m_mp) CExpressionArray(m_mp);
CDXLLogicalSetOp *dxl_op = CDXLLogicalSetOp::Cast(dxlnode->GetOperator());
const ULONG arity = dxlnode->Arity();
GPOS_ASSERT(2 <= arity);
GPOS_ASSERT(arity == dxl_op->ChildCount());
const ULONG ulOutputCols = dxl_op->Arity();
for (ULONG ul = 0; ul < arity; ul++)
{
CExpression *pexprChild = nullptr;
CColRefArray *pdrgpcrInput = nullptr;
CExpressionArray *pdrgpexprChildProjElems = nullptr;
BuildSetOpChild(dxlnode, ul, &pexprChild, &pdrgpcrInput,
&pdrgpexprChildProjElems);
GPOS_ASSERT(ulOutputCols == pdrgpcrInput->Size());
GPOS_ASSERT(nullptr != pexprChild);
pdrgdrgpcrInput->Append(pdrgpcrInput);
if (0 < pdrgpexprChildProjElems->Size())
{
CExpression *pexprChildProject = GPOS_NEW(m_mp) CExpression(
m_mp, GPOS_NEW(m_mp) CLogicalProject(m_mp), pexprChild,
GPOS_NEW(m_mp)