コード例 #1
0
// ***************************************************************************
// Initialize the several hash tables that map output list elements:
//    outputByIDHash_	      - Using the ID of the output item as the key.
//    outputByColumnName_     - Using the "ID Qualified" column name as the key.
//    outputByExprText_	      - Using the expression text as the key.
//    outputByInputColumns_   - Using the name of expression input column as the key.
// ***************************************************************************
void MVDetails::initOutputs(const QRJBBPtr jbb, CollHeap* heap)
{
  // Match the output elements with their names.
  const QROutputListPtr outputList = jbb->getOutputList();

  assertLogAndThrow(CAT_MATCHTST_MVDETAILS, LL_MVQR_FAIL,
                    outputList != NULL, QRLogicException,
                    "Output list is null.");

  // For each output list element
  for (CollIndex i=0; i<outputList->entries(); i++)
  {
    QROutputPtr output = (*outputList)[i];
    // Set the ordinal number of the output element.
    output->setColPos(i+1);

    // Get the output item (column or expression).
    QRElementPtr outputItem = output->getOutputItem()->getReferencedElement();
    const NAString& id = outputItem->getID();
    // Insert the ID of the output item (whatever it is) into the ID hash.
    outputByIDHash_.insert(&id, output);

    // OK, now lets check what type of output item it is.
    switch (outputItem->getElementType())
    {
      case ET_Column:
      {
	// If its an output column, add it to the output column by name hash.
	const QRColumnPtr col = outputItem->downCastToQRColumn();

        const QRElementPtr tableElem = getElementForID(col->getTableID());
        NABoolean hasLOJParent = tableElem->downCastToQRTable()->hasLOJParent();

        if (!isFromJoin(col) || hasLOJParent)
        {
	  // Compute the "ID Qualified" column name as the key.
	  const NAString* idQualifiedColName = 
	    calcIDQualifiedColumnName(col->getTableID(),  col->getColumnName(), heap);

	  // Insert into the hash table.
	  outputByColumnName_.insert(idQualifiedColName, output);
        }
        else
        {
	  // If its an equality set, insert all the participating columns
	  // as pointing to the same MV output column.
          const QRJoinPredPtr jp = getJoinPred(col);
	  const QRJoinPredPtr joinPredElement = jp->downCastToQRJoinPred();
          addEqualitySet(joinPredElement, FALSE, output, heap);
        }
	break;
      }

      case ET_JoinPred:
      {
        assertLogAndThrow(CAT_MATCHTST_MVDETAILS, LL_MVQR_FAIL,
                          (outputItem->getElementType() == ET_JoinPred), QRLogicException, 
			  "Unexpected JoinPred element.");

	// If its an equality set, insert all the participating columns
	// as pointing to the same MV output column.
	const QRJoinPredPtr joinPredElement = outputItem->downCastToQRJoinPred();
        addEqualitySet(joinPredElement, FALSE, output, heap);
	break;
      }

      case ET_Expr:
      {
	// This is an expression. Insert the expression text into the expression hash.
	const QRExprPtr expr = outputItem->downCastToQRExpr();
        const NAString& exprText = expr->getExprText(); 
	outputByExprText_.insert(&exprText, output);
        QRLogger::log(CAT_MATCHTST_MVDETAILS, LL_DEBUG,
          "Adding output expression: %s.", exprText.data());

	// Also map the expressions's input columns to it.
	// This call to getInitColumns() also inits the input list using the 
	// right heap.
	const ElementPtrList& inputs = expr->getInputColumns(heap);
	if (inputs.entries() > 0)
	{
	  for (CollIndex j=0; j<inputs.entries(); j++)
	  {
            QRElementPtr inputElem = inputs[j]->getReferencedElement();
            if (inputElem->getElementType() == ET_Column)
            {
	      QRColumnPtr inputCol = inputElem->downCastToQRColumn();

	      // Compute the "ID Qualified" column name as the key.
	      const NAString* idQualifiedColName = 
	        calcIDQualifiedColumnName(inputCol->getTableID(),  inputCol->getColumnName(), heap);

	      outputByInputColumns_.insert(idQualifiedColName, output);
	    }
            else
            {
              QRJoinPredPtr inputJoinPred = inputElem->downCastToQRJoinPred();
              addEqualitySet(inputJoinPred, TRUE, output, heap);
            }
          }
	}

        // Check if this is the COUNT(*) function.
        QRExplicitExprPtr rootExpr = expr->getExprRoot();
        if (rootExpr->getElementType() == ET_Function)
        {
          QRFunctionPtr func = rootExpr->downCastToQRFunction();
          if (func->getAggregateFunc() == QRFunction::AFT_COUNTSTAR)
          {
            // Found it, now remember it for later use.
            // If a COUNT(*) equivalent function has already been found 
            // (see below) - overwrite it.
            countStar_ = output;
          }
          else if (countStar_ == NULL &&
                   func->isCountStarEquivalent(heap) )
          {
            // Well, we didn't find a COUNT(*) yet, but we found a COUNT(a)
            // and the input column is NOT NOLL, we can use it as a COUNT(*) column.
            countStar_ = output;
          }
        }
      } // end of case ET_Expr
      break;
    }  // switch on element type
  }  // for on output list elements
}  //  MVDetails::initOutputs()
コード例 #2
0
// ***************************************************************************
// ***************************************************************************
void JBBDetails::initRangePreds(CollHeap* heap)
{
  // Initialize the Range predicates.
  const QRRangePredListPtr rangePreds = jbbDesc_->getHub()->getRangePredList();
  if (rangePreds==NULL || rangePreds->entries() == 0)
  {
    hasNoRangePredicates_ = TRUE;
    return;
  }

  for (CollIndex k=0; k<rangePreds->entries(); k++)
  {
    const QRRangePredPtr rangePred = (*rangePreds)[k];
    const QRElementPtr rangeElem = rangePred->getRangeItem()->getReferencedElement();

    if (rangeElem->getElementType() == ET_Column && rangePred->isSingleValue())
    {
      constColumns_.insert(&rangeElem->getID());
    }

    // For query descriptors, only check for const columns.
    if (!isAnMV_)
      continue;

    if (rangeElem->getElementType() == ET_Column)
    {
      const QRColumnPtr rangeColumn = rangeElem->downCastToQRColumn();

      if (!descDetails_->isFromJoin(rangeColumn))
      {
        // This column is NOT part of a join predicate.
        BaseTableDetailsPtr rangeTable = getBaseTableByID(rangeColumn->getTableID());
        rangeTable->addRangePredicateOnColumn(rangePred, rangeColumn);
      }
      else
      {
        // This column IS part of a join predicate.
        // Insert the range pred for all the participating columns.
        const QRJoinPredPtr jp = descDetails_->getJoinPred(rangeColumn);
        const ElementPtrList& equalitySet = jp->getEqualityList();
        for (CollIndex i=0; i<equalitySet.entries(); i++)
        {
	  const QRElementPtr halfPred = equalitySet[i];

	  // Now lets look at the equality set members
	  // Ignore members that are not simple columns.
	  if (halfPred->getElementType() != ET_Column)
	    continue;

	  const QRColumnPtr eqSetColumn = halfPred->getReferencedElement()->downCastToQRColumn();
          BaseTableDetailsPtr rangeTable = getBaseTableByID(eqSetColumn->getTableID());
          rangeTable->addRangePredicateOnColumn(rangePred, eqSetColumn);
        }
      }
    }
    else if (rangeElem->getElementType() == ET_JoinPred)
    {
      assertLogAndThrow(CAT_MATCHTST_MVDETAILS, LL_MVQR_FAIL,
                        (rangeElem->getElementType() == ET_JoinPred), QRLogicException, 
			"Unexpected JoinPred element.");

      // If its an equality set, insert the range pred for all the participating columns.
      const QRJoinPredPtr joinPredElement = rangeElem->downCastToQRJoinPred();
      const ElementPtrList& equalitySet = joinPredElement->getEqualityList();
  
      for (CollIndex i=0; i<equalitySet.entries(); i++)
      {
	const QRElementPtr halfPred = equalitySet[i];

	// Now lets look at the equality set members
	// Ignore members that are not simple columns.
	if (halfPred->getElementType() != ET_Column)
	  continue;

	const QRColumnPtr eqSetColumn = halfPred->getReferencedElement()->downCastToQRColumn();
        BaseTableDetailsPtr rangeTable = getBaseTableByID(eqSetColumn->getTableID());
        rangeTable->addRangePredicateOnColumn(rangePred, eqSetColumn);
      }
    }
    else
    {
      // The range pred is on an expression. Insert it for all the expression's input columns.
      const QRExprPtr rangeExpr = rangeElem->downCastToQRExpr();

      if (rangeExpr->getExprRoot()->containsAnAggregate(heap))
      {
        hasHavingPredicates_ = TRUE;
      }

      // Verify we have a single input column
      // No input columns mean its a COUNT(*), which is OK too.
      const ElementPtrList& inputs = rangeExpr->getInputColumns(heap);
      assertLogAndThrow(CAT_MATCHTST_MVDETAILS, LL_MVQR_FAIL,
                        (inputs.entries() <= 1), QRLogicException, 
			"Range predicate expression must have a single input at most.");

      const NAString& predText = rangeExpr->getExprText();

      // Look it up in the hash table: do we already have a range pred with this text?
      RangePredPtrList* predList = getRangePredsOnExpression(predText);

      if (predList == NULL)
	{
	  // No, its the first one. Create a new list.
	  predList = new(heap) RangePredPtrList(heap);
	  // And insert it into the hash table.
	  rangePredicates_.insert(&predText, predList);
	}

      // Insert the predicate into the pred list.
      predList->insert(rangePred);
    }
  }
}  // JBBDetails::init()