コード例 #1
0
//---------------------------------------------------------------------------
//	@function:
//		CGroupExpression::PccComputeCost
//
//	@doc:
//		Compute and store expression's cost under a given context;
//		the function returns the cost context containing the computed cost
//
//---------------------------------------------------------------------------
CCostContext *
CGroupExpression::PccComputeCost
	(
	IMemoryPool *pmp,
	COptimizationContext *poc,
	ULONG ulOptReq,
	DrgPoc *pdrgpoc, // array of child contexts
	BOOL fPruned,	// is created cost context pruned based on cost bound
	CCost costLowerBound	// lower bound on the cost of plan carried by cost context
	)
{
	GPOS_ASSERT(NULL != poc);
	GPOS_ASSERT_IMP(!fPruned, NULL != pdrgpoc);

	if (!fPruned && !FValid(pmp, poc, pdrgpoc))
	{
		return NULL;
	}
	
	// check if the same cost context is already created for current group expression
	if (FCostContextExists(poc, pdrgpoc))
	{
		return NULL;
	}

	poc->AddRef();
	this->AddRef();
	CCostContext *pcc = GPOS_NEW(pmp) CCostContext(pmp, poc, ulOptReq, this);
	BOOL fValid = true;

	// computing cost
	pcc->SetState(CCostContext::estCosting);

	if (!fPruned)
	{
		if (NULL != pdrgpoc)
		{
			pdrgpoc->AddRef();
		}
		pcc->SetChildContexts(pdrgpoc);

		fValid = pcc->FValid(pmp);
		if (fValid)
		{
			CCost cost = CostCompute(pmp, pcc);
			pcc->SetCost(cost);
		}
		GPOS_ASSERT_IMP(COptCtxt::FAllEnforcersEnabled(), fValid &&
				"Cost context carries an invalid plan");
	}
	else
	{
		pcc->SetPruned();
		pcc->SetCost(costLowerBound);
	}

	pcc->SetState(CCostContext::estCosted);
	if (fValid)
	{
		return PccInsertBest(pcc);
	}

	pcc->Release();

	// invalid cost context
	return NULL;
}