# Copyright (c) 2015, VMware, Inc. or its affiliates. All Rights Reserved.

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include"
                    "${PROJECT_SOURCE_DIR}/libgpdbcost/include"
                    "${PROJECT_SOURCE_DIR}/libgpopt/include"
                    "${PROJECT_SOURCE_DIR}/libnaucrates/include")
# for the generated config.h file.
include_directories(${PROJECT_BINARY_DIR}/libgpos/include/)
# for the auto generated minidump test classes
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)

# Some tests use C99 long long integer constants, but the C++ dialect may not
# explicitly include C99. We supress such errors here.
check_cxx_compiler_flag("-Wno-long-long"
                        COMPILER_HAS_WNO_LONG_LONG)
if (COMPILER_HAS_WNO_LONG_LONG)
    set_source_files_properties(src/unittest/dxl/statistics/CStatisticsTest.cpp
                                src/unittest/dxl/statistics/CBucketTest.cpp
                                src/unittest/dxl/statistics/CPointTest.cpp
                                src/unittest/dxl/statistics/CHistogramTest.cpp
                                src/unittest/dxl/statistics/CMCVTest.cpp
                                src/unittest/dxl/statistics/CJoinCardinalityTest.cpp
                                src/unittest/dxl/statistics/CFilterCardinalityTest.cpp
                                src/unittest/gpopt/base/CConstraintTest.cpp
                                PROPERTIES COMPILE_FLAGS "-Wno-long-long")
endif()

# Convenience function to add the test specified by 'TEST_NAME' to the set of
# tests to be run by CTest.
function(add_orca_test TEST_NAME)
  # Unit tests have many hard-coded paths to test data in optimizer/data. We
  # explicitly set the WORKING_DIRECTORY so that the files can be found
  # properly.
  add_test(NAME gporca_test_${TEST_NAME}
           COMMAND gporca_test -U ${TEST_NAME}
           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endfunction()

# GPDB_12_MERGE_FIXME: casts on part column no longer supported!
# CPartTableLossyCastTest

# ORCA_FEATURE_NOT_SUPPORTED: re-add the following tests to CArrayCmpTest once the
# PexprConvert2In preprocessing step is enabled
# IN-ArrayCmp
# NOT-IN-ArrayCmp


# minidump tests must follow rule:
# "CxxxTest: file1 file2; CxxxTest: file3 file4"
# each test case should end with ";", EXCEPT LAST ONE.
# comments with format /*...*/ are allowed at any place.
set(MDP_GROUPS "

CSemiJoinTest:
SemiJoin2InnerJoin SemiJoin2Select-1 SemiJoin2Select-2 SemiJoin2Select-EnforceSubplan
InSubqWithPrjListOuterRefNoInnerRefColumnIn InSubqWithPrjListOuterRefNoInnerRefConstIn
NestedInSubqWithPrjListOuterRefNoInnerRef  InEqualityJoin Correlated-SemiJoin
CorrelatedSemiJoin-True CorrelatedIN-LeftSemiJoin-True CorrelatedIN-LeftSemiNotIn-True
InSubqWithPrjListOuterRefNoInnerRefColumnPlusConstIn CorrelatedIN-LeftSemiJoin-Limit
CorrelatedLeftSemiNLJoinWithLimit PushFilterToSemiJoinLeftChild SubqOuterReferenceInClause
SemiJoinDPE InSubqWithPrjListReturnSet LeftSemiJoinWithRepOuterTab LeftSemiJoinCE;

CAntiSemiJoinTest:
AntiSemiJoin2Select-1 AntiSemiJoin2Select-2 NOT-IN-NotNullBoth NOT-IN-NullInner NOT-IN-NullOuter
Correlated-AntiSemiJoin CorrelatedAntiSemiJoin-True Correlated-LASJ-With-Outer-Col NotInToLASJ
Correlated-LASJ-With-Outer-Const Correlated-LASJ-With-Outer-Expr LeftOuter2InnerUnionAllAntiSemiJoin-Tpcds
LASJ-Not-In-Force-Broadcast;

CPredicateTest:
ConvertBoolConstNullToConstTableFalseFilter 
TranslateOneTimeFilterConjunctQuals TranslateFilterDisjunctQuals TranslateFilterWithCTEAndTableScanIntoFilterAndOneTimeFilter
InferPredicates InferPredicatesForLimit InferPredicatesBCC-vc-vc InferPredicatesBCC-vc-txt
InferPredicatesBCC-txt-txt InferPredicatesBCC-oid-oid
OR-WithIsNullPred Int2Predicate IN-Numeric DeduplicatePredicates
AddEqualityPredicates EqualityPredicateOverDate AddPredsInSubqueries ExtractPredicateFromDisjWithComputedColumns
Join-With-Subq-Preds-1 Non-Hashjoinable-Pred Non-Hashjoinable-Pred-2 Factorized-Preds IN OR AvoidConstraintDerivationForLike
NLJ-DistCol-No-Broadcast NLJ-EqAllCol-No-Broadcast NLJ-EqDistCol-InEqNonDistCol-No-Broadcast
NLJ-InEqDistCol-EqNonDistCol-Redistribute CorrelatedNLJWithTrueCondition InferPredicatesInnerOfLOJ
InferredPredicatesConstraintSimplification NoPushdownPredicateWithCTEAnchor
InferPredicatesForPartSQ InferPredicatesForQuantifiedSQ InferPredicatesForProcessedColumn DoubleNDVCardinalityEquals
NotWellDefinedDisjunctConjunctPredicates InferPredicatesIntFromSubquery InferPredicatesFromExistsSubquery
InferPredicatesIntFromExistsSubquery InferPredicatesFromMultiSubquery InferPredicatesMultiColumns
InferPredicatesJoinSubquery JoinPredFalseFilter;

CLikeIDFTest:
LIKE-Pattern-green LIKE-Pattern-green-2 LIKE-Pattern-Empty Nested-Or-Predicates Join-IDF
LikePredStatsNotComparable PredStatsNotComparable CollapseNot
IDF-NotNullConstant IDF-NullConstant INDF-NotNullConstant INDF-NullConstant NullConstant-INDF-Col
Join-INDF Join-INDF-NoBroadcast Join-INDF-Nulls-Not-Collocated;

CLeftOuterIndexApplyTest:
IndexApply-LeftOuter-NLJoin LOJ-IndexApply-MultiIndexes LOJ-IndexApply-Negative-NonEqual-Predicate
LOJ-IndexApply-DistKey-Multiple-Predicates LOJ-IndexApply-MasterOnly-Table LOJ-IndexApply-NonDistKey
LOJ-IndexApply-CompsiteKey-Equiv LOJ-IndexApply-CompsiteKey-NoMotion LOJ-IndexApply-MultiDistKeys-IndexKeys
LOJ-IndexApply-WithComplexPredicates LOJ-IndexApply-MultiDistKeys-WithComplexPreds
LOJ-IndexApply-MultiDistKey-MultiIndexKey-NoExtraFilter LOJ-IndexApply-MultiDistKey-MultiIndexKey
LOJ-IndexApply-MultiDistKeys-Bitmap LOJ-IndexApply-MultiDistKeys-Bitmap-WithComplexPreds
LOJ-DynBitmapIndex LOJ-DynBitmapIndexWithSelect LOJ-DynBtreeIndex LOJ-DynBtreeIndexWithSelect;

CTypeModifierTest:
TypeModifierColumn TypeModifierCast TypeModifierConst TypeModifierDoubleMappableConst TypeModifierArrayRef;

CIndexScanTest:
BTreeIndex-Against-InList BTreeIndex-Against-InListLarge BTreeIndex-Against-ScalarSubquery
IndexScan-AOTable IndexScan-DroppedColumns IndexScan-BoolTrue IndexScan-BoolFalse
IndexScan-Relabel IndexGet-OuterRefs LogicalIndexGetDroppedCols
IndexScan-ORPredsNonPart IndexScan-ORPredsAOPart IndexScan-AndedIn SubqInIndexPred Hash-IndexScan
IndexScan-OrderBy-on-Single-IndexCol IndexScan-OrderBy-on-Multiple-IndexCols IndexScan-OrderBy-on-NonIndexCol
Forward-IndexScan-OrderBy-on-SingleCol-Index Backward-IndexScan-OrderBy-on-SingleCol-Index
Forward-IndexScan-OrderBy-on-MultiCol-Index Backward-IndexScan-OrderBy-on-MultiCol-Index
IndexScan-OrderBy-on-MultiCol-NonIndex Max-Aggregate-uses-IndexScan-and-Limit
Min-Aggregate-uses-IndexScan-and-Limit IndexScan-IsNullPred IndexScan-IsNotNullPred;

CBitmapScanTest:
IndexedNLJBitmap BitmapIndex-ChooseHashJoin BitmapTableScan-AO-Btree-PickOnlyHighNDV
BitmapIndex-Against-InList BitmapTableScan-AO-Btree-PickIndexWithNoGap
BitmapTableScan-ComplexConjDisj BitmapTableScan-ConjDisjWithOuterRefs
BRINScan-Or BitmapScan-Hetrogeneous-Partitioned Hash-BitmapScan Hash-BitmapScan-InArray
Hash-TableScan-AllArray;

CIndexApplyTest:
IndexNLJ-IndexGet-OuterRef IndexApply_NestLoopWithNestParamTrue IndexApply1
IndexApply1-CalibratedCostModel IndexApply2 IndexApply3 IndexApply4
IndexApply-IndexCondDisjointWithHashedDistr IndexApply-IndexCondIntersectWithHashedDistr
IndexApply-IndexCondMatchHashedDistr IndexApply-IndexCondSubsetOfHashedDistr
IndexApply-IndexCondSupersetOfHashedDistr IndexApply-IndexOnMasterOnlyTable
IndexApply-PartTable IndexApply-Heterogeneous-NoDTS
IndexApply-No-Motion-Below-Join IndexApply-Redistribute-Const-Table
IndexApply-InnerSelect-Basic IndexApply-InnerSelect-PartTable IndexApply-PartKey-Is-IndexKey
IndexApply-InnerSelect-Heterogeneous-DTS IndexApply-Heterogeneous-BothSidesPartitioned
IndexApply-InnerSelect-PartTable2 CastedScalarIf-On-Index-Key
IndexApply-MultiDistKey-WithComplexPreds IndexApply-MultiDistKeys-Bitmap
IndexApply-MultiDistKeys-Bitmap-WithComplexPreds
IndexApply-MultiDistKeys-IncompletePDS-3-DistCols
BitmapIndexNLJWithProject BitmapIndexNLOJWithProject
BitmapIndexNLJWithProjectNoFilt BtreeIndexNLJWithProjectNoPart
BitmapIndexNLOJWithProjectNonPart BtreeIndexNLOJWithProject;

CDynamicIndexScanTest:
DynamicIndexScan-Homogenous DynamicIndexScan-BoolTrue DynamicIndexScan-BoolFalse
DynamicIndexScan-Heterogenous-NoDTS DynamicIndexScan-Heterogenous-Union
DynamicIndexScan-Heterogenous-Overlapping DynamicIndexScan-Heterogenous
DynamicIndexScan-DefaultPartition DynamicIndexScan-DefaultPartition-2
DynamicIndexScan-Homogenous-UnsupportedConstraint DynamicIndexScan-Heterogenous-UnsupportedConstraint
DynamicIndexScan-Heterogenous-PartSelectEquality
DynamicIndexScan-Heterogenous-PartSelectRange DynamicIndexScan-OpenEndedPartitions
DynamicIndexScan-Relabel DynamicIndexScan-DroppedCols;

CDynamicIndexOnlyScanTest:
DynamicIndexOnlyScan-Homogenous DynamicIndexOnlyScan-InnerJoin DynamicIndexOnlyScan-LeftJoin;

CIndexScanOnLeafTest:
IndexOnLeaf-AddNewPartitionToRootTableContainingHeterogenousIndex
IndexOnLeaf-AddPartitionToRootWithHomogenousIndex IndexOnLeaf-AddPartitionToRootWithHomogenousIndex
IndexOnLeaf-IndexOnPartitionsWithDifferentStorageTypes IndexOnLeaf-NonOverlappingHeterogenousIndex-ANDPredicate-AO
IndexOnLeaf-NonOverlappingHeterogenousIndex-ANDPredicate-HEAP IndexOnLeaf-NonOverlappingHomogenousIndexesOnRoot-ANDPredicate-AO
IndexOnLeaf-NonOverlappingHomogenousIndexesOnRoot-ORPredicate-AO IndexOnLeaf-OverlappingHeterogenousIndex-ANDPredicate-AO
IndexOnLeaf-OverlappingHeterogenousIndex-ANDPredicate-HEAP IndexOnLeaf-OverlappingHeterogenousIndex-ORPredicate-AO
IndexOnLeaf-OverlappingHomogenousIndexesOnRoot-ANDPredicate-AO IndexOnLeaf-OverlappingHomogenousIndexesOnRoot-AO
IndexOnLeaf-OverlappingHomogenousIndexesOnRoot-ORPredicate-AO IndexOnLeaf-SingleColumnHeterogenousIndexOnRoot-1-AO
IndexOnLeaf-SingleColumnHeterogenousIndexOnRoot-1-HEAP IndexOnLeaf-SingleColumnHeterogenousIndexOnRoot-2-AO
IndexOnLeaf-SingleColumnHeterogenousIndexOnRoot-2-HEAP NonOverlappingHomogenousIndexesOnRoot-ANDPredicate-HEAP
OverlappingHomogenousIndexesOnRoot-ANDPredicate-HEAP OverlappingHomogenousIndexesOnRoot-HEAP
SingleColumnHomogenousIndexOnRoot-AO SingleColumnHomogenousIndexOnRoot-HEAP;

CStatsTest:
Stat-Derivation-Leaf-Pattern MissingBoolColStats JoinColWithOnlyNDV UnsupportedStatsPredicate
StatsFilter-AnyWithNewColStats EquiJoinOnExpr-Supported EquiJoinOnExpr-Unsupported NdvPreservingExprProjectionEquiJoin NdvPreservingExprProjectionNonEquiJoin;

CCorrelatedStatsTest:
Correlated-Stat-Function-Dependency Correlated-Stat-Function-Dependency-2 Correlated-Stat-Function-Dependency-3
Correlated-Stat-NDistinct Correlated-Stat-NDistinct-2 Correlated-Stat-NDistinct-3 Correlated-Stat-NDistinct-4 Correlated-Stat-NDistinct-5;

CICGMiscTest:
BroadcastSkewedHashjoin OrderByNullsFirst ConvertHashToRandomSelect ConvertHashToRandomInsert HJN-DeeperOuter CTAS CTAS-Random CheckAsUser
ProjectRepeatedColumn1 ProjectRepeatedColumn2 NLJ-BC-Outer-Spool-Inner Self-Comparison Self-Comparison-Nullable
SelectCheckConstraint ExpandJoinOrder SelectOnBpchar EqualityJoin EffectsOfJoinFilter InnerJoin-With-OuterRefs
UDA-AnyElement-1 UDA-AnyElement-2 Project-With-NonScalar-Func SixWayDPv2 Join-Varchar-Equality NLJ-Rewindability
NLJ-Rewindability-CTAS DisableBroadcastThreshold VariadicFlagWithJson VariadicFlagWithFormatQuery VariadicFlagWithUnionAll
IndexScanCosting IndexOnlyScanCosting DynamicIndexScanCosting DynamicIndexOnlyScanCosting BroadcastSkewedJoinWithNulls;

CJoinPredTest:
MultipleDampedPredJoinCardinality MultipleIndependentPredJoinCardinality MultiDistKeyJoinCardinality
MultiDistKeyWithOtherPredsJoinCardinality NoDistKeyMultiPredJoinCardinality OneDistKeyMultiPredJoinCardinality
JoinOnViewWithCastedColumn JoinOnViewWithCastedColumnAndSubqueryInPredicate JoinOnViewWithVolatileColumn
JoinOnViewWithMixOfPushableAndNonpushablePredicates JoinOnViewWithSetReturningColumn OuterJoinOnViewWithCastedColumn JoinWithSubqueryProjectColumn
AggSubqCollapseFalseFilter NestedJoinWithCastedColumn JoinOnViewWithCastedColumnAndJoinWithCastedColumnInPredicate PushConstantSelectPredicateThruJoin-1 PushConstantSelectPredicateThruJoin-2
PushConstantSelectPredicateThruJoin-3 PushConstantSelectPredicateThruJoin-4 PushConstantSelectPredicateThruJoin-5 PushConstantSelectPredicateThruJoin-6
PushConstantSelectPredicateThruJoin-7 PushConstantSelectPredicateThruJoin-8 PushConstantSelectPredicateThruJoin-9 PushConstantSelectPredicateThruJoin-10
PushConstantSelectPredicateThruJoin-11 PushConstantSelectPredicateThruJoin-12 PushConstantSelectPredicateThruJoin-13
PushConstantSelectPredicateThruJoin-14 PushConstantSelectPredicateThruJoin-15 PushConstantSelectPredicateThruJoin-16
PushConstantSelectPredicateThruJoin-17;

CArrayCmpTest:
ArrayConcat ArrayRef FoldedArrayCmp ArrayCmpAll
UDA-AnyArray InClauseWithMCV CastedInClauseWithMCV FilterScalarCast
IN-Nulls-ArrayCmpAny ArrayCmp-IN-ManyElements ArrayCmpAnyEmpty ArrayCmpAllEmpty
ArrayCmpAnyEmptyLessThan ArrayCmpAll-Disjunct;

CCTEPropertiesTest:
CTE-PushProperties CTE-NoPushProperties;

CProjectTest:
ProjectWithConstant ProjectWithTextConstant ProjectSetFunction Equivalence-class-project-over-LOJ;

CPartTbl1Test:
CorrelatedNLJ-PartSelector-Subplan
DonotPushPartConstThruLimit Select-Over-PartTbl
PartSelectorOnJoinSide PartSelectorOnJoinSide2;

CPartTbl2Test:
Part-Selection-IN Part-Selection-NOT-IN
Part-Selection-ConstArray-1 Part-Selection-ConstArray-2 PartTbl-WindowFunction
PartTbl-MultiWayJoin PartTbl-AsymmetricRangePredicate PartTbl-NEqPredicate PartTbl-SQExists;

CPartTbl3Test:
PartTbl-SQNotExists PartTbl-SQAny PartTbl-SQAll PartTbl-SQScalar PartTbl-HJ3
PartTbl-HJ4 PartTbl-HJ5 PartTbl-JoinOverGbAgg PartTbl-JoinOverGbAgg-2
PartTbl-NonConstSelect PartTbl-VolatileFunc PartTbl-DateTime
PartTbl-ComplexRangePredicate-DefaultPart
PartTbl-ComplexRangePredicate-NoDefaultPart PartTbl-LASJ;

CPartTbl4Test:
PartTbl-DisablePartSelection
PartTbl-DisablePartSelectionJoin PartTbl-JoinOverUnion-1
PartTbl-JoinOverUnion-2 PartTbl-JoinOverIntersect PartTbl-JoinOverExcept
PartTbl-ArrayIn PartTbl-Disjunction
PartTbl-ComplexPredicate1 PartTbl-ComplexPredicate2 PartTbl-ComplexPredicate3
PartTbl-ComplexPredicate4 PartTbl-ComplexPredicate5 LargeMultiColumnInList;

CPartTbl5Test:
PartTbl-IsNullPredicate PartTbl-IsNotNullPredicate PartTbl-IndexOnDefPartOnly
PartTbl-SubqueryOuterRef PartTbl-CSQ-PartKey PartTbl-CSQ-NonPartKey
PartTbl-AggWithExistentialSubquery PartPredsWithRandom DTS-Hetrogeneous-Partitioned;

CPartTbl6Test:
PartTbl-PredicateWithCast PartTbl-IDFList PartTbl-IDFNull
PartTbl-PredicateWithCast PartTbl-EqPredicateWithCastRange
PartTbl-PredicateWithCastList PartTbl-IDFWithCast;

CPartTblDPETest:
PartTbl-AvoidRangePred-DPE PartTbl-DPE-Correlated-NLOuter
PartTbl-DPE PartTbl-DPE-GroupBy PartTbl-DPE-Limit
DPE-with-unsupported-pred DPE-SemiJoin DPE-IN DPE-NOT-IN HJN-DPE-Bitmap-Outer-Child
NLJ-Broadcast-DPE-Outer-Child PartTbl-MultiWayJoinWithDPE-2
PartTbl-LeftOuterHashJoin-DPE-IsNull PartTbl-LeftOuterNLJoin-DPE-IsNull
PartTbl-List-DPE-Int-Predicates JoinOrderDPE PartTbl-DPE-Opfamily
PartTbl-DPE-DynamicIndexOnlyScan-Range;

CPartTblSPETest:
PartTbl-SPE-DynamicTableScan-Range-Cost1 PartTbl-SPE-DynamicTableScan-Range-Cost2
PartTbl-SPE-DynamicTableScan-Range-Cost3 PartTbl-SPE-DynamicTableScan-Range-Cost4
PartTbl-SPE-DynamicTableScan-Range-Cost5 PartTbl-SPE-DynamicTableScan-List-Cost1
PartTbl-SPE-DynamicTableScan-List-Cost2 PartTbl-SPE-DynamicTableScan-List-Cost3
PartTbl-SPE-DynamicTableScan-List-Cost4 PartTbl-SPE-DynamicTableScan-List-Cost5;

CPartTblSPEBoolTest:
PartTbl-SPE-Boolean1 PartTbl-SPE-Boolean2;

CSetop1Test:
ValueScanWithDuplicateAndSelfComparison PushGbBelowNaryUnionAll
PushGbBelowNaryUnion-1 PushGbBelowNaryUnion-2
ManyTextUnionsInSubquery JoinBelowUnionAll;

CSetop2Test:
Cascaded-UnionAll-Same-Cols-Order Cascaded-UnionAll-Differing-Cols
Cascaded-UnionAll-Differing-Cols-Order Union-Over-UnionAll Nested-Setops
Nested-Setops-2 Except InnerJoinOverJoinExcept InnerJoinOverJoinExceptAll IsNullUnionAllIsNotNull UnionAllWithTruncatedOutput
Union-OuterRefs-Output Union-OuterRefs-Casting-Output
Union-OuterRefs-InnerChild;

CSetop3Test:
UnionWithOuterRefs UnionAll
Union-Distributed-Table-With-Const-Table ExceptAllCompatibleDataType
UnionAllCompatibleDataType UnionOfDQAQueries Union-Volatile-Func
Intersect-Volatile-Func Except-Volatile-Func UnionWithCTE ExceptHashCompatibleRange
ExceptHashIncompatibleRange;

CSetop4Test:
PushSelectDownUnionAllOfCTG Push-Subplan-Below-Union Intersect-OuterRefs
PushSelectWithOuterRefBelowUnion PushGbBelowUnion UnionGbSubquery
AnyPredicate-Over-UnionOfConsts EquivClassesUnion EquivClassesIntersect
IndexScanWithNestedCTEAndSetOp Blocking-Spool-Parallel-Union-All ParallelUnionAllWithJson;

CEquivClassTest:
Equiv-HashedDistr-1 Equiv-HashedDistr-2 EquivClassesAndOr EquivClassesLimit IndexNLJoin_Cast_NoMotion;

COuterJoin1Test:
ExpandNAryJoinGreedyWithLOJOnly NaryWithLojAndNonLojChilds LOJ_bb_mpph LOJ-Condition-False
LeftJoin-With-Pred-On-Inner LeftJoin-With-Pred-On-Inner2
LeftJoin-With-Col-Const-Pred LeftJoin-With-Coalesce LOJWithFalsePred LeftJoin-DPv2-With-Select
DPv2GreedyOnly DPv2MinCardOnly DPv2QueryOnly LOJ-PushDown LeftJoinDPv2JoinOrder
LeftJoinNullsNotColocated InnerJoinBroadcastTableHashSpec LeftJoinBroadcastTableHashSpec InnerJoinReplicatedTableHashSpec
LeftJoinPruning LeftJoinPruningOuterQuery LeftJoinPruningInnerQuery LeftJoinPruningInOuterInnerQuery;

COuterJoin2Test:
LOJ-IsNullPred Select-Proj-OuterJoin OuterJoin-With-OuterRefs Join-Disj-Subqs
EffectOfLocalPredOnJoin EffectOfLocalPredOnJoin3
LeftJoin-UnsupportedFilter-Cardinality LeftOuter2InnerUnionAllAntiSemiJoin
LOJ-With-Single-Pred-On-Outer LOJ_NULLTEST-On-SelfCheck-Pred;

COuterJoin3Test:
LOJ_IDF_no_convert_outer_ref_predicate_with_NULL
LOJ_IDF_no_convert_outer_ref_predicate_with_inner_ref LOJ_convert_to_inner_with_inner_predicate_IDF
LOJ_dont_convert_to_inner_with_inner_predicate_IDF_const LOJ_dont_convert_to_inner_with_outer_predicate_INDF
LOJ_dont_convert_to_inner_with_outer_predicate_INDF_const LOJ-With-Agg
LOJReorderWithComplexPredicate LOJReorderComplexNestedLOJs LOJReorderWithIDF
LOJReorderWithNestedLOJAndFilter LOJReorderWithSimplePredicate LOJReorderWithOneSidedFilter
LOJ-HashJoin-MultiDistKeys-WithComplexPreds LOJ_convert_to_inner_with_and_predicate
LOJ_convert_to_inner_with_or_predicate LOJNonNullRejectingPredicates LOJNullRejectingPredicates
LOJNullRejectingZeroPlacePredicates;

CFullJoinTest:
FOJ-Condition-False FullOuterJoin-NullRejectingLHS1 FullOuterJoin-NullRejectingLHS2
FullJoin-InnerNotOnDistributionColumn FullJoin-NotOnDistributionColumn FullJoin-2PredicateOnDistColumns
FullJoin-OnDistributionColumn FullJoin-SwappedPredicates FullJoin-Replicated 
FullJoin-SubqueryWithRedistribute FullJoin-SubquerySingleton FullJoin-Subquery-CastedPredicates
FullJoin-Caps;

CJoinOrderGreedyTest:
GreedyNAryJoin GreedyNAryDelayCrossJoin GreedyNAryOnlyCrossJoins GreedyNAryJoinWithDisconnectedEdges;

CMotionHazardTest:
MotionHazard-NoMaterializeSortUnderResult  MotionHazard-NoMaterializeGatherUnderResult MotionHazard-MaterializeUnderResult
MotionHazard-NoMaterializeHashAggUnderResult CorrelatedNLJWithStreamingSpool NestedNLJWithBlockingSpool;

CConstraintNegationTest:
AggregatesConstraintDeriveNegation CTEConstraintDeriveNegation Union-NOT-Plus-OR-Constraint;

CNonRedistributableColTest:
JOIN-NonRedistributableCol DQA-NonRedistributableCol
MotionHazard-NoMaterializeHashAggUnderResult;

CInvalidCostContextTest:
SpoolShouldInvalidateUnresolvedDynamicScans DiscardRedistributeHashJoin;

CRandomDataInsertionTest:
AddRedistributeBeforeInsert-1 AddRedistributeBeforeInsert-2 AddRedistributeBeforeInsert-3
AddRedistributeBeforeInsert-4 AddRedistributeBeforeInsert-5 DontAddRedistributeBeforeInsert-1
DontAddRedistributeBeforeInsert-2 CTAS-random-distr CTAS-random-distributed-from-replicated-distributed-table;

CGistIndexTest:
Gist-AOCOTable-NonLossy-BitmapIndexPlan Gist-NonPart-Lossy-BitmapIndexPlan Gist-NonPart-Lossy-IndexPlan
Gist-PartTable-Lossy-IndexPlan Gist-PartTable-NonLossy-BitmapIndexPlan Gist-PartialIndex-TableScan
Gist-NestedLoopJoin-Lossy-IndexPlan Gist-NestedLoopJoin-Postgis-IndexPlan Gist-OrderBy-BitmapPlan Gist-OrderBy-IndexPlan;

CGinIndexTest:
GinIndex GinIndexPathOpfamily GinIndexSearchModeAll;

CReplicatedTableTest:
ReplicatedTableSelect ReplicatedTableInsert ReplicatedTable-CTAS
ReplicatedHashJoinReplicated ReplicatedJoinHashDistributedTable
ReplicatedJoinRandomDistributedTable ReplicatedLOJHashDistributedTable
ReplicatedLOJRandomDistributedTable ReplicatedLOJReplicated
ReplicatedNLJReplicated ReplicatedTableAggregate ReplicatedTableCTE
ReplicatedTableGroupBy ReplicatedJoinPartitionedTable
ReplicatedTableInClause ReplicatedTableSequenceInsert
JoinOnReplicatedUniversal;

CTaintedReplicatedTest:
InsertNonSingleton NonSingleton TaintedReplicatedAgg TaintedReplicatedWindowAgg TaintedReplicatedLimit TaintedReplicatedFilter
InsertReplicatedIntoSerialHashDistributedTable TaintedReplicatedTablesCTE ReplicatedTableWithAggNoMotion;

CDqaTest:
NonSplittableAgg DqaHavingMax DqaMax DqaMin DqaSubqueryMax DqaNoRedistribute DistinctQueryWithMotions;

CMCVCardinalityTest:
BpCharMCVCardinalityEquals BpCharMCVCardinalityGreaterThan
TextMCVCardinalityEquals TextMCVCardinalityGreaterThan
VarcharMCVCardinalityEquals VarcharMCVCardinalityGreaterThan Citext-Cardinality
Name-Cardinality Char-Cardinality;

COpfamiliesTest:
JoinCitextVarchar JoinDefaultOpfamiliesUsingNonDefaultOpfamilyOp
MultiColumnAggWithDefaultOpfamilies JoinTinterval FullJoin-NonDefaultOpfamily
JoinAbsEqWithoutOpfamilies 3WayJoinUsingOperatorsOfNonDefaultOpfamily
Distinct-LegacyOpfamily;

CSubquery2Test:
Subq2PartialDecorrelate Subq2CorrSQInLOJOn Subq2NotInWhereLOJ Subq2OuterRef2InJoin Subq2OuterRefMultiLevelInOn
Index-Join-With-Subquery-In-Pred NestedSubqLimitBindings;

CIndexOnlyScanTest:
IndexOnlyScan-NoDistKeyInIndex IndexConstraintsMDidCache IndexOnlyScan-CTE AllowIndexOnlyScanOnAppendOnlyTable
AllowIndexOnlyScanOnMixedAppendOnlyPartitionedTable AllowIndexOnlyScanOnMixedPartitionedTable
IndexOnlyScan-NoDistKeyInIndex IndexConstraintsMDidCache IndexOnlyScan-CTE
Forward-IndexOnlyScan-OrderBy-on-MultiCol-Index Backward-IndexOnlyScan-OrderBy-on-MultiCol-Index
IndexOnlyScan-OrderBy-on-MultiCol-NonIndex;

CRightJoinTest:
RightJoinHashed RightJoinRedistribute RightJoinReplicated RightJoinBothReplicated RightJoinNoDPSNonDistKey RightJoinTVF
RightJoinDPE PartTbl-RightOuterHashJoin-DPE-IsNull-V2 RightJoinDpeWithWhereClause;

CSqlFunctionTest:
SqlFuncNullReject SqlFuncPredFactorize SqlFuncDmlScalar SqlFuncDmlTvf CompositeTypeRelMdId;

CScalarFuncPushDownTest:
ScalarFuncPushedBelowGather ConstScalarFuncNotPushedBelowGather;

CScalarTest:
FieldSelect FunctionWithQueryParam BasicQueryParam;

COrderedAggTest:
CTAS_OrderedAgg_multiple_cols OrderedAgg_multiple_diffcol OrderedAgg_with_groupby OrderedAgg_computed_col
OrderedAggUsingGroupColumnInDirectArg OrderedAgg_multiple_samecol OrderedAgg_with_nonOrderedAgg OrderedAgg_single
OrderedAgg_array_fraction OrderedAgg_multiple_samecol_difforderespec OrderedAgg_with_nonconst_fraction
OrderedAgg_skewed_data;

CoveringIndexTest:
CoveringIndex-1 CoveringIndex-2 CoveringIndex-3 CoveringIndex-Cost-1 CoveringIndex-Cost-2
CoveringIndex-DoesSupport-Gist CoveringIndex-DoesNotSupport-Gin;

CPlanHintsTest:
Hint-SeqScan-Over-Table Hint-SeqScan-Over-Join
Hint-IndexScan-Over-Table Hint-IndexScan-Over-Join Hint-NoIndexScan-Over-Table
Hint-IndexOnlyScan-Over-Table Hint-NoIndexOnlyScan-Over-Table
Hint-BitmapScan-Over-Table Hint-NoBitmapScan-Over-Table
Hint-AbsoluteRows-Over-Join Hint-AddRows-Over-Join Hint-SubtractRows-Over-Join Hint-MultiplyRows-Over-Join
Hint-Leading-Multiple Hint-Leading-Directed Hint-Leading-NonDirected
Hint-FullHashJoin-JoinType Hint-FullMergeJoin-JoinType Hint-IndexHashJoin-JoinType Hint-IndexNestLoop-JoinType;

CForeignPartTest:
ForeignPartUniform PartForeignMixed PartForeignDifferentServer PartForeignDifferentExecLocation PartForeignMixedDPE PartForeignMixedSPE PartForeignUniformSPE ForeignScanExecLocAnySimpleScan ForeignScanExecLocAnyJoin ForeignPartOneTimeFilterDPE;

CCardinalityTest:
SystemColCtidStats SystemColSegIdStats
")

set(mdp_dir "../data/dxl/minidump/")
set(mdp_incl_dir "unittest/gpopt/minidump")
set(mdp_test_hdr_tpl "${CMAKE_CURRENT_SOURCE_DIR}/include/${mdp_incl_dir}/MinidumpTest.h.in")
set(mdp_test_src_tpl "${CMAKE_CURRENT_SOURCE_DIR}/src/${mdp_incl_dir}/MinidumpTest.cpp.in")
set(mdp_test_hdr_dir "${CMAKE_CURRENT_BINARY_DIR}/include/${mdp_incl_dir}")
set(mdp_test_src_dir "${CMAKE_CURRENT_BINARY_DIR}/src/${mdp_incl_dir}")
set(mdp_headers "${mdp_test_hdr_dir}/MinidumpTestHeaders")
set(mdp_tests "${mdp_test_hdr_dir}/MinidumpTestArray")
set(mdp_headers_h "${mdp_headers}.h")
set(mdp_tests_inl "${mdp_tests}.inl")

# if the old file exists and the new file is the same with old file,
# do nothing. otherwise, write the old file with new file's content.
function(update_file old_file new_file)
  if(NOT EXISTS ${old_file})
    file(RENAME ${new_file} ${old_file})
  else()
    file(READ ${old_file} old_hex HEX)
    file(READ ${new_file} new_hex HEX)
    if("${old_hex}" STREQUAL "${new_hex}")
      file(REMOVE ${new_file})
    else()
      file(RENAME ${new_file} ${old_file})
    endif()
  endif()
endfunction()

file(WRITE ${mdp_headers} "")
file(WRITE ${mdp_tests} "")

foreach(mdp_group IN LISTS MDP_GROUPS)

  # remove comments /*...*/ from minidump test group string
  string(REGEX REPLACE "\\/\\*.*\\*\\/" "" mdp_group ${mdp_group})

  string(STRIP ${mdp_group} mdp_group)

  # get minidump test name
  string(REGEX MATCH "C.+Test" test_name ${mdp_group})

  # get the length of "CxxxTest:"
  string(LENGTH "${test_name}:" name_len)

  # get minidump file names (without .mdp) concatenated by spaces
  string(SUBSTRING ${mdp_group} ${name_len} -1 mdp_names)

  string(STRIP ${mdp_names} mdp_names)

  # generate full minidump paths array
  string(REGEX REPLACE "([A-Za-z0-9_\-]+)" "\"${mdp_dir}\\1.mdp\"," mdp_names ${mdp_names})
  string(REGEX REPLACE "[ \t\n]+" "\n" mdp_files ${mdp_names})

  # generate header/src file name for current test
  set(header_file ${mdp_test_hdr_dir}/${test_name})
  set(source_file ${mdp_test_src_dir}/${test_name})

  # auto generate test class from template
  configure_file(${mdp_test_hdr_tpl} ${header_file})
  configure_file(${mdp_test_src_tpl} ${source_file})

  update_file(${header_file}.h ${header_file})
  update_file(${source_file}.cpp ${source_file})

  # add test header file into include file used in main.cpp
  file(APPEND ${mdp_headers} "#include \"${mdp_incl_dir}/${test_name}.h\"\n")

  # add unittest into test array in main.cpp
  file(APPEND ${mdp_tests} "GPOS_UNITTEST_STD(${test_name}),\n")

  add_orca_test(${test_name})
endforeach()

update_file(${mdp_headers_h} ${mdp_headers})
update_file(${mdp_tests_inl} ${mdp_tests})

# The ordering of tests and the conditions by which tests are enabled or
# disabled matches the static array of tests in "src/startup/main.cpp".

# Naucrates tests.
add_orca_test(CCostTest)
add_orca_test(CExternalTableTest)
add_orca_test(CDatumTest)
add_orca_test(CDXLMemoryManagerTest)
add_orca_test(CDXLUtilsTest)
add_orca_test(CMDAccessorTest)
add_orca_test(CMDProviderTest)
add_orca_test(CArrayExpansionTest)
add_orca_test(CJoinOrderDPTest)
add_orca_test(CMiniDumperDXLTest)
add_orca_test(CExpressionPreprocessorTest)
add_orca_test(CWindowTest)
add_orca_test(CICGTest)
#add_orca_test(CMultilevelPartitionTest)
add_orca_test(CDMLTest)
add_orca_test(CDirectDispatchTest)
add_orca_test(CTVFTest)
add_orca_test(CPullUpProjectElementTest)
add_orca_test(CAggTest)
add_orca_test(CSubqueryTest)
add_orca_test(CCollapseProjectTest)
add_orca_test(CPruneColumnsTest)
add_orca_test(CMissingStatsTest)
add_orca_test(CBitmapTest)
add_orca_test(CCTETest)
add_orca_test(CEscapeMechanismTest)
add_orca_test(CPhysicalParallelUnionAllTest)
add_orca_test(CMinidumpWithConstExprEvaluatorTest)
add_orca_test(CParseHandlerManagerTest)
add_orca_test(CParseHandlerTest)
add_orca_test(CParseHandlerCostModelTest)
add_orca_test(CParseHandlerOptimizerConfigSerializeTest)
add_orca_test(CStatisticsTest)
add_orca_test(CPointTest)
add_orca_test(CBucketTest)
add_orca_test(CHistogramTest)
add_orca_test(CMCVTest)
add_orca_test(CJoinCardinalityTest)
# FIXME: the following test was first introduced in greenplum-db/gporca#360,
# reverted in greenplum-db/gporca#442, and re-added in
# greenplum-db/gporca#506 (NOT! because only the CMakeLists.txt part was
# added back). Resurrect this.
if (FALSE)
add_orca_test(CJoinCardinalityNDVBasedEqPredTest)
endif()
add_orca_test(CFilterCardinalityTest)
add_orca_test(CTranslatorDXLToExprTest)
add_orca_test(CTranslatorExprToDXLTest)
add_orca_test(CXMLSerializerTest)

# Opt tests.
add_orca_test(CColumnDescriptorTest)
add_orca_test(CColumnFactoryTest)
add_orca_test(CColRefSetIterTest)
add_orca_test(CColRefSetTest)
add_orca_test(CConstraintTest)
add_orca_test(CContradictionTest)
add_orca_test(CLogicalGbAggTest)
add_orca_test(CCorrelatedExecutionTest)
add_orca_test(CDecorrelatorTest)
add_orca_test(CDistributionSpecTest)
add_orca_test(CCastTest)
add_orca_test(CConstTblGetTest)
add_orca_test(CScalarIsDistinctFromTest)

add_orca_test(CSubqueryHandlerTest)
add_orca_test(CXformRightOuterJoin2HashJoinTest)

add_orca_test(CBindingTest)
add_orca_test(CEngineTest)
add_orca_test(CEquivalenceClassesTest)
add_orca_test(CExpressionTest)
add_orca_test(CGroupTest)
add_orca_test(CJoinOrderTest)
add_orca_test(CKeyCollectionTest)
add_orca_test(CMaxCardTest)
add_orca_test(CFunctionalDependencyTest)
add_orca_test(CNameTest)
add_orca_test(COrderSpecTest)
add_orca_test(CRangeTest)
add_orca_test(CPredicateUtilsTest)

add_orca_test(CSearchStrategyTest)

add_orca_test(COptimizationJobsTest)
add_orca_test(CStateMachineTest)
add_orca_test(CTableDescriptorTest)
add_orca_test(CIndexDescriptorTest)
add_orca_test(CTreeMapTest)
add_orca_test(CXformFactoryTest)
add_orca_test(CXformTest)

add_orca_test(CConstExprEvaluatorDefaultTest)
add_orca_test(CConstExprEvaluatorDXLTest)

file(GLOB_RECURSE hdrs ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h
                       ${CMAKE_CURRENT_SOURCE_DIR}/include/*.inl
                       ${mdp_test_hdr_dir}/*.h
                       ${mdp_test_hdr_dir}/*.inl)
file(GLOB_RECURSE srcs ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
                       ${mdp_test_src_dir}/*.cpp)

# Add headers to make them visible in some IDEs (Clion, VS, Xcode)
list(APPEND srcs ${hdrs})

add_executable(gporca_test ${srcs})

target_link_libraries(gporca_test
                      gpdbcost
                      gpopt
                      naucrates
                      gpos)
