CCTEListEntry.cpp 4.0 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
//---------------------------------------------------------------------------
//	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
//
//---------------------------------------------------------------------------
J
Jesse Zhang 已提交
34 35 36
CCTEListEntry::CCTEListEntry(CMemoryPool *mp, ULONG query_level,
							 CommonTableExpr *cte, CDXLNode *cte_producer)
	: m_query_level(query_level), m_cte_info(NULL)
37
{
38
	GPOS_ASSERT(NULL != cte && NULL != cte_producer);
J
Jesse Zhang 已提交
39

40
	m_cte_info = GPOS_NEW(mp) HMSzCTEInfo(mp);
J
Jesse Zhang 已提交
41 42
	Query *cte_query = (Query *) cte->ctequery;

43
#ifdef GPOS_DEBUG
J
Jesse Zhang 已提交
44
	BOOL result =
45
#endif
J
Jesse Zhang 已提交
46 47 48 49
		m_cte_info->Insert(
			cte->ctename,
			GPOS_NEW(mp) SCTEProducerInfo(cte_producer, cte_query->targetList));

50
	GPOS_ASSERT(result);
51 52 53 54 55 56 57 58 59 60
}

//---------------------------------------------------------------------------
//	@function:
//		CCTEListEntry::CCTEListEntry
//
//	@doc:
//		Ctor: multiple CTEs
//
//---------------------------------------------------------------------------
J
Jesse Zhang 已提交
61 62 63
CCTEListEntry::CCTEListEntry(CMemoryPool *mp, ULONG query_level, List *cte_list,
							 CDXLNodeArray *cte_dxl_arr)
	: m_query_level(query_level), m_cte_info(NULL)
64
{
65 66
	GPOS_ASSERT(NULL != cte_dxl_arr);
	GPOS_ASSERT(cte_dxl_arr->Size() == gpdb::ListLength(cte_list));
J
Jesse Zhang 已提交
67

68 69
	m_cte_info = GPOS_NEW(mp) HMSzCTEInfo(mp);
	const ULONG num_cte = cte_dxl_arr->Size();
J
Jesse Zhang 已提交
70

71
	for (ULONG ul = 0; ul < num_cte; ul++)
72
	{
73
		CDXLNode *cte_producer = (*cte_dxl_arr)[ul];
J
Jesse Zhang 已提交
74 75 76
		CommonTableExpr *cte = (CommonTableExpr *) gpdb::ListNth(cte_list, ul);

		Query *cte_query = (Query *) cte->ctequery;
77

78
#ifdef GPOS_DEBUG
79
		BOOL result =
80
#endif
J
Jesse Zhang 已提交
81 82 83 84
			m_cte_info->Insert(cte->ctename,
							   GPOS_NEW(mp) SCTEProducerInfo(
								   cte_producer, cte_query->targetList));

85 86
		GPOS_ASSERT(result);
		GPOS_ASSERT(NULL != m_cte_info->Find(cte->ctename));
87 88 89 90 91
	}
}

//---------------------------------------------------------------------------
//	@function:
92
//		CCTEListEntry::GetCTEProducer
93 94 95 96 97 98
//
//	@doc:
//		Return the query of the CTE referenced in the range table entry
//
//---------------------------------------------------------------------------
const CDXLNode *
J
Jesse Zhang 已提交
99
CCTEListEntry::GetCTEProducer(const CHAR *cte_str) const
100
{
101 102
	SCTEProducerInfo *cte_info = m_cte_info->Find(cte_str);
	if (NULL == cte_info)
103
	{
J
Jesse Zhang 已提交
104
		return NULL;
105
	}
J
Jesse Zhang 已提交
106

107
	return cte_info->m_cte_producer;
108 109 110 111
}

//---------------------------------------------------------------------------
//	@function:
112
//		CCTEListEntry::GetCTEProducerTargetList
113 114
//
//	@doc:
115
//		Return the target list of the CTE referenced in the range table entry
116 117 118
//
//---------------------------------------------------------------------------
List *
J
Jesse Zhang 已提交
119
CCTEListEntry::GetCTEProducerTargetList(const CHAR *cte_str) const
120
{
121 122
	SCTEProducerInfo *cte_info = m_cte_info->Find(cte_str);
	if (NULL == cte_info)
123
	{
J
Jesse Zhang 已提交
124
		return NULL;
125
	}
J
Jesse Zhang 已提交
126

127
	return cte_info->m_target_list;
128 129 130 131 132 133 134 135 136 137 138
}

//---------------------------------------------------------------------------
//	@function:
//		CCTEListEntry::AddCTEProducer
//
//	@doc:
//		Add a new CTE producer to this query level
//
//---------------------------------------------------------------------------
void
J
Jesse Zhang 已提交
139 140
CCTEListEntry::AddCTEProducer(CMemoryPool *mp, CommonTableExpr *cte,
							  const CDXLNode *cte_producer)
141
{
J
Jesse Zhang 已提交
142 143 144 145
	GPOS_ASSERT(NULL == m_cte_info->Find(cte->ctename) &&
				"CTE entry already exists");
	Query *cte_query = (Query *) cte->ctequery;

146
#ifdef GPOS_DEBUG
147
	BOOL result =
148
#endif
J
Jesse Zhang 已提交
149 150 151 152
		m_cte_info->Insert(
			cte->ctename,
			GPOS_NEW(mp) SCTEProducerInfo(cte_producer, cte_query->targetList));

153
	GPOS_ASSERT(result);
154 155 156
}

// EOF