CCTEListEntry.cpp 4.1 KB
Newer Older
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
//---------------------------------------------------------------------------
//	Greenplum Database
//	Copyright (C) 2012 EMC Corp.
//
//	@filename:
//		CCTEListEntry.cpp
//
//	@doc:
//		Implementation of the class representing the list of common table
//		expression defined at a query level
//
//	@test:
//
//
//---------------------------------------------------------------------------

#include "postgres.h"
#include "gpopt/translate/CCTEListEntry.h"

#include "nodes/parsenodes.h"

#include "gpos/base.h"
#include "gpopt/gpdbwrappers.h"
using namespace gpdxl;

//---------------------------------------------------------------------------
//	@function:
//		CCTEListEntry::CCTEListEntry
//
//	@doc:
//		Ctor: single CTE
//
//---------------------------------------------------------------------------
CCTEListEntry::CCTEListEntry
	(
	IMemoryPool *pmp,
	ULONG ulQueryLevel,
	CommonTableExpr *pcte,
	CDXLNode *pdxlnCTEProducer
	)
	:
	m_ulQueryLevel(ulQueryLevel),
	m_phmszcteinfo(NULL)
{
	GPOS_ASSERT(NULL != pcte && NULL != pdxlnCTEProducer);
	
47
	m_phmszcteinfo = GPOS_NEW(pmp) HMSzCTEInfo(pmp);
48 49 50 51 52
	Query *pqueryCTE = (Query*) pcte->ctequery;
		
#ifdef GPOS_DEBUG
		BOOL fResult =
#endif
53
	m_phmszcteinfo->FInsert(pcte->ctename, GPOS_NEW(pmp) SCTEProducerInfo(pdxlnCTEProducer, pqueryCTE->targetList));
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
		
	GPOS_ASSERT(fResult);
}

//---------------------------------------------------------------------------
//	@function:
//		CCTEListEntry::CCTEListEntry
//
//	@doc:
//		Ctor: multiple CTEs
//
//---------------------------------------------------------------------------
CCTEListEntry::CCTEListEntry
	(
	IMemoryPool *pmp,
	ULONG ulQueryLevel,
	List *plCTE, 
	DrgPdxln *pdrgpdxln
	)
	:
	m_ulQueryLevel(ulQueryLevel),
	m_phmszcteinfo(NULL)
{
	GPOS_ASSERT(NULL != pdrgpdxln);
	GPOS_ASSERT(pdrgpdxln->UlLength() == gpdb::UlListLength(plCTE));
	
80
	m_phmszcteinfo = GPOS_NEW(pmp) HMSzCTEInfo(pmp);
81 82 83 84 85 86 87 88 89 90 91
	const ULONG ulCTEs = pdrgpdxln->UlLength();
	
	for (ULONG ul = 0; ul < ulCTEs; ul++)
	{
		CDXLNode *pdxlnCTEProducer = (*pdrgpdxln)[ul];
		CommonTableExpr *pcte = (CommonTableExpr*) gpdb::PvListNth(plCTE, ul);
		Query *pqueryCTE = (Query*) pcte->ctequery;
		
#ifdef GPOS_DEBUG
		BOOL fResult =
#endif
92
		m_phmszcteinfo->FInsert(pcte->ctename, GPOS_NEW(pmp) SCTEProducerInfo(pdxlnCTEProducer, pqueryCTE->targetList));
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
		
		GPOS_ASSERT(fResult);
		GPOS_ASSERT(NULL != m_phmszcteinfo->PtLookup(pcte->ctename));
	}
}

//---------------------------------------------------------------------------
//	@function:
//		CCTEListEntry::PdxlnCTEProducer
//
//	@doc:
//		Return the query of the CTE referenced in the range table entry
//
//---------------------------------------------------------------------------
const CDXLNode *
CCTEListEntry::PdxlnCTEProducer
	(
	const CHAR *szCTE
	)
	const
{
	SCTEProducerInfo *pcteinfo = m_phmszcteinfo->PtLookup(szCTE);
	if (NULL == pcteinfo)
	{
		return NULL; 
	}
	
	return pcteinfo->m_pdxlnCTEProducer;
}

//---------------------------------------------------------------------------
//	@function:
//		CCTEListEntry::PdxlnCTEProducer
//
//	@doc:
//		Return the query of the CTE referenced in the range table entry
//
//---------------------------------------------------------------------------
List *
CCTEListEntry::PlCTEProducerTL
	(
	const CHAR *szCTE
	)
	const
{
	SCTEProducerInfo *pcteinfo = m_phmszcteinfo->PtLookup(szCTE);
	if (NULL == pcteinfo)
	{
		return NULL; 
	}
	
	return pcteinfo->m_plTargetList;
}

//---------------------------------------------------------------------------
//	@function:
//		CCTEListEntry::AddCTEProducer
//
//	@doc:
//		Add a new CTE producer to this query level
//
//---------------------------------------------------------------------------
void
CCTEListEntry::AddCTEProducer
	(
	IMemoryPool *pmp,
	CommonTableExpr *pcte,
	const CDXLNode *pdxlnCTEProducer
	)
{
	GPOS_ASSERT(NULL == m_phmszcteinfo->PtLookup(pcte->ctename) && "CTE entry already exists");
	Query *pqueryCTE = (Query*) pcte->ctequery;
	
#ifdef GPOS_DEBUG
	BOOL fResult =
#endif
169
	m_phmszcteinfo->FInsert(pcte->ctename, GPOS_NEW(pmp) SCTEProducerInfo(pdxlnCTEProducer, pqueryCTE->targetList));
170 171 172 173 174
	
	GPOS_ASSERT(fResult);
}

// EOF