//----------------------------------------------------------------------------
// The condition for a self-cancelling delta
// (SYS_MAV.cnts IS NULL AND SYS_DELTA.cnts = 0)
// If the MAV has no GROUP BY clause, the condition is:
// (SYS_MAV.cnts = 0 AND SYS_DELTA.cnts = 0)
//----------------------------------------------------------------------------
ItemExpr *MavRelRootBuilder::buildSelfCancelingDeltaPredicate()
{
  // Find the MAV column name for COUNT(*).
  const NAString& countStarColName = mavCols_[posOfCountStar_]->getColName();

  ItemExpr *mavCountStarCondition = NULL;
  ItemExpr *mavCountStarName = new(heap_) 
    ColReference(new(heap_) ColRefName(countStarColName, mavCorrName_));
  if (isMavWithoutGroupBy())
  {
    // (SYS_MAV.cnts = 0)
    mavCountStarCondition = new(heap_) 
      BiRelat(ITM_EQUAL, mavCountStarName, new(heap_) SystemLiteral(0));
  }
  else
  {
    // (SYS_MAV.cnts IS NULL)
    mavCountStarCondition = new(heap_) 
      UnLogic(ITM_IS_NULL, mavCountStarName);
  }

  // (mavCountStarCondition AND SYS_DELTA.cnts = 0)
  ItemExpr *selfCancelCondition = new(heap_)  
    BiLogic(ITM_AND,
	    mavCountStarCondition,
	    new(heap_) BiRelat(ITM_EQUAL, 
  			       new(heap_) ColReference(new(heap_) 
				 ColRefName(countStarColName, deltaCorrName_)),
			       new(heap_) SystemLiteral(0)));

  ItemExpr *notSelfCancelingDeltaPredicate = 
    new(heap_) UnLogic(ITM_NOT, selfCancelCondition);

  notSelfCancelingDeltaPredicate->setSelectivityFactor(1.0);

  return notSelfCancelingDeltaPredicate;
}  // MavRelRootBuilder::buildSelfCancelingDeltaPredicate()