Package org.apache.derby.iapi.sql.compile

Examples of org.apache.derby.iapi.sql.compile.AccessPath


   * @exception StandardException    Thrown on error
   */
  public ResultSetNode changeAccessPath() throws StandardException
  {
    ResultSetNode  retval;
    AccessPath ap = getTrulyTheBestAccessPath();
    ConglomerateDescriptor trulyTheBestConglomerateDescriptor =
                         ap.getConglomerateDescriptor();
    JoinStrategy trulyTheBestJoinStrategy = ap.getJoinStrategy();
    Optimizer optimizer = ap.getOptimizer();

    optimizer.trace(Optimizer.CHANGING_ACCESS_PATH_FOR_TABLE,
            tableNumber, 0, 0.0, null);

    if (SanityManager.DEBUG)
    {
      SanityManager.ASSERT(
        trulyTheBestConglomerateDescriptor != null,
        "Should only modify access path after conglomerate has been chosen.");
    }

    /*
    ** Make sure user-specified bulk fetch is OK with the chosen join
    ** strategy.
    */
    if (bulkFetch != UNSET)
    {
      if ( ! trulyTheBestJoinStrategy.bulkFetchOK())
      {
        throw StandardException.newException(SQLState.LANG_INVALID_BULK_FETCH_WITH_JOIN_TYPE,
                      trulyTheBestJoinStrategy.getName());
      }
      // bulkFetch has no meaning for hash join, just ignore it
      else if (trulyTheBestJoinStrategy.ignoreBulkFetch())
      {
        disableBulkFetch();
      }
      // bug 4431 - ignore bulkfetch property if it's 1 row resultset
      else if (isOneRowResultSet())
      {
        disableBulkFetch();
      }
    }

    // bulkFetch = 1 is the same as no bulk fetch
    if (bulkFetch == 1)
    {
      disableBulkFetch();
    }

    /* Remove any redundant join clauses.  A redundant join clause is one
     * where there are other join clauses in the same equivalence class
     * after it in the PredicateList.
     */
    restrictionList.removeRedundantPredicates();

    /*
    ** Divide up the predicates for different processing phases of the
    ** best join strategy.
    */
    storeRestrictionList = (PredicateList) getNodeFactory().getNode(
                          C_NodeTypes.PREDICATE_LIST,
                          getContextManager());
    nonStoreRestrictionList = (PredicateList) getNodeFactory().getNode(
                          C_NodeTypes.PREDICATE_LIST,
                          getContextManager());
    requalificationRestrictionList =
                  (PredicateList) getNodeFactory().getNode(
                          C_NodeTypes.PREDICATE_LIST,
                          getContextManager());
    trulyTheBestJoinStrategy.divideUpPredicateLists(
                      this,
                      restrictionList,
                      storeRestrictionList,
                      nonStoreRestrictionList,
                      requalificationRestrictionList,
                      getDataDictionary());

    /* Check to see if we are going to do execution-time probing
     * of an index using IN-list values.  We can tell by looking
     * at the restriction list: if there is an IN-list probe
     * predicate that is also a start/stop key then we know that
     * we're going to do execution-time probing.  In that case
     * we disable bulk fetching to minimize the number of non-
     * matching rows that we read from disk.  RESOLVE: Do we
     * really need to completely disable bulk fetching here,
     * or can we do something else?
     */
    for (int i = 0; i < restrictionList.size(); i++)
    {
      Predicate pred = (Predicate)restrictionList.elementAt(i);
      if (pred.isInListProbePredicate() && pred.isStartKey())
      {
        disableBulkFetch();
        multiProbing = true;
        break;
      }
    }

    /*
    ** Consider turning on bulkFetch if it is turned
    ** off.  Only turn it on if it is a not an updatable
    ** scan and if it isn't a oneRowResultSet, and
    ** not a subquery, and it is OK to use bulk fetch
    ** with the chosen join strategy.  NOTE: the subquery logic
    ** could be more sophisticated -- we are taking
    ** the safe route in avoiding reading extra
    ** data for something like:
    **
    **  select x from t where x in (select y from t)
     **
    ** In this case we want to stop the subquery
    ** evaluation as soon as something matches.
    */
    if (trulyTheBestJoinStrategy.bulkFetchOK() &&
      !(trulyTheBestJoinStrategy.ignoreBulkFetch()) &&
      ! bulkFetchTurnedOff &&
      (bulkFetch == UNSET) &&
      !forUpdate() &&
      !isOneRowResultSet() &&
      getLevel() == 0)
    {
      bulkFetch = getDefaultBulkFetch()
    }

    /* Statement is dependent on the chosen conglomerate. */
    getCompilerContext().createDependency(
        trulyTheBestConglomerateDescriptor);

    /* No need to modify access path if conglomerate is the heap */
    if ( ! trulyTheBestConglomerateDescriptor.isIndex())
    {
      /*
      ** We need a little special logic for SYSSTATEMENTS
      ** here.  SYSSTATEMENTS has a hidden column at the
      ** end.  When someone does a select * we don't want
      ** to get that column from the store.  So we'll always
      ** generate a partial read bitSet if we are scanning
      ** SYSSTATEMENTS to ensure we don't get the hidden
      ** column.
      */
      boolean isSysstatements = tableName.equals("SYS","SYSSTATEMENTS");
      /* Template must reflect full row.
       * Compact RCL down to partial row.
       */
      templateColumns = resultColumns;
      referencedCols = resultColumns.getReferencedFormatableBitSet(cursorTargetTable, isSysstatements, false);
      resultColumns = resultColumns.compactColumns(cursorTargetTable, isSysstatements);
      return this;
    }
   
    /* No need to go to the data page if this is a covering index */
    /* Derby-1087: use data page when returning an updatable resultset */
    if (ap.getCoveringIndexScan() && (!cursorTargetTable()))
    {
      /* Massage resultColumns so that it matches the index. */
      resultColumns = newResultColumns(resultColumns,
                        trulyTheBestConglomerateDescriptor,
                       baseConglomerateDescriptor,
View Full Code Here


          indexCols[i] = isAscending[i] ? baseColPos[i] : -baseColPos[i];
        indexColItem = acb.addItem(indexCols);
      }
    }

        AccessPath ap = getTrulyTheBestAccessPath();
    JoinStrategy trulyTheBestJoinStrategy =  ap.getJoinStrategy();

    /*
    ** We can only do bulkFetch on NESTEDLOOP
    */
    if (SanityManager.DEBUG)
    {
      if ( ( ! trulyTheBestJoinStrategy.bulkFetchOK()) &&
        (bulkFetch != UNSET))
      {
        SanityManager.THROWASSERT("bulkFetch should not be set "+
                "for the join strategy " +
                trulyTheBestJoinStrategy.getName());
      }
    }

    int nargs = trulyTheBestJoinStrategy.getScanArgs(
                      getLanguageConnectionContext().getTransactionCompile(),
                      mb,
                      this,
                      storeRestrictionList,
                      nonStoreRestrictionList,
                      acb,
                      bulkFetch,
                      resultRowAllocator,
                      colRefItem,
                      indexColItem,
                      getTrulyTheBestAccessPath().
                                getLockMode(),
                      (tableDescriptor.getLockGranularity() == TableDescriptor.TABLE_LOCK_GRANULARITY),
                      getCompilerContext().getScanIsolationLevel(),
                      ap.getOptimizer().getMaxMemoryPerTable(),
                      multiProbing
                      );

    return nargs;
  }
View Full Code Here

              int colNum = crs[0].getColumnNumber();
             
              /* Check if we have an access path, this will be
               * null in a join case (See Beetle 4423)
               */
              AccessPath accessPath= getTrulyTheBestAccessPath();
              if (accessPath == null)
                return;
              IndexDescriptor id = accessPath.
                        getConglomerateDescriptor().
                        getIndexDescriptor();
              int[] keyColumns = id.baseColumnPositions();
              boolean[] isAscending = id.isAscending();
              for (int i = 0; i < keyColumns.length; i++)
View Full Code Here

              int colNum = crs[0].getColumnNumber();
             
              /* Check if we have an access path, this will be
               * null in a join case (See Beetle 4423,DERBY-3904)
               */
              AccessPath accessPath= getTrulyTheBestAccessPath();
              if (accessPath == null ||
                accessPath.getConglomerateDescriptor()==null||
                accessPath.getConglomerateDescriptor().
                           getIndexDescriptor() == null)
                return;
              IndexDescriptor id = accessPath.
                        getConglomerateDescriptor().
                        getIndexDescriptor();
              int[] keyColumns = id.baseColumnPositions();
              boolean[] isAscending = id.isAscending();
              for (int i = 0; i < keyColumns.length; i++)
View Full Code Here

   */
  public boolean feasibleJoinStrategy(OptimizablePredicateList predList,
                    Optimizer optimizer)
          throws StandardException
  {
    AccessPath ap;

    /* The child being an Optimizable is a special case.  In that
     * case, we want to get the current access path and join strategy
     * from the child.  Otherwise, we want to get it from this node.
     */
 
View Full Code Here

   * @exception StandardException    Thrown on error
   */
  public void pushUsefulPredicates(Optimizable optTable)
            throws StandardException
  {
    AccessPath ap = optTable.getTrulyTheBestAccessPath();

    orderUsefulPredicates(optTable,
              ap.getConglomerateDescriptor(),
              true,
              ap.getNonMatchingIndexScan(),
              ap.getCoveringIndexScan());
  }
View Full Code Here

        removeElementAt(index);
      }
    }

    // order the useful predicates on the other list
    AccessPath ap = table.getTrulyTheBestAccessPath();
    theOtherList.orderUsefulPredicates(
                  table,
                  ap.getConglomerateDescriptor(),
                  false,
                  ap.getNonMatchingIndexScan(),
                  ap.getCoveringIndexScan());

    // count the start/stop positions and qualifiers
    theOtherList.countScanFlags();
  }
View Full Code Here

                  RowOrdering rowOrdering)
          throws StandardException
  {
    int  numStrat = optimizer.getNumberOfJoinStrategies();
    boolean found = false;
    AccessPath ap = getCurrentAccessPath();

    /*
    ** Most Optimizables have no ordering, so tell the rowOrdering that
    ** this Optimizable is unordered, if appropriate.
    */
    if (userSpecifiedJoinStrategy != null)
    {
      /*
      ** User specified a join strategy, so we should look at only one
      ** strategy.  If there is a current strategy, we have already
      ** looked at the strategy, so go back to null.
      */
      if (ap.getJoinStrategy() != null)
      {
          ap.setJoinStrategy((JoinStrategy) null);

        found = false;
      }
      else
      {
        ap.setJoinStrategy(
                optimizer.getJoinStrategy(userSpecifiedJoinStrategy));

        if (ap.getJoinStrategy() == null)
        {
          throw StandardException.newException(SQLState.LANG_INVALID_JOIN_STRATEGY,
            userSpecifiedJoinStrategy, getBaseTableName());
        }

        found = true;
      }
    }
    else if (joinStrategyNumber < numStrat)
    {
      /* Step through the join strategies. */
      ap.setJoinStrategy(optimizer.getJoinStrategy(joinStrategyNumber));

      joinStrategyNumber++;

      found = true;

      optimizer.trace(Optimizer.CONSIDERING_JOIN_STRATEGY, tableNumber, 0, 0.0,
              ap.getJoinStrategy());
    }

    /*
    ** Tell the RowOrdering about columns that are equal to constant
    ** expressions.
 
View Full Code Here

      }

      return;
    }

    AccessPath bestPath = getTrulyTheBestAccessPath();
    AccessPathImpl ap = null;
    if (action == ADD_PLAN)
    {
      // If we get to this method before ever optimizing this node, then
      // there will be no best path--so there's nothing to do.
      if (bestPath == null)
        return;

      // If the bestPlanMap already exists, search for an
      // AccessPath for the received key and use that if we can.
      if (bestPlanMap == null)
        bestPlanMap = new HashMap();
      else
        ap = (AccessPathImpl)bestPlanMap.get(planKey);

      // If we don't already have an AccessPath for the key,
      // create a new one.  If the key is an OptimizerImpl then
      // we might as well pass it in to the AccessPath constructor;
      // otherwise just pass null.
      if (ap == null)
      {
        if (planKey instanceof Optimizer)
          ap = new AccessPathImpl((Optimizer)planKey);
        else
          ap = new AccessPathImpl((Optimizer)null);
      }

      ap.copy(bestPath);
      bestPlanMap.put(planKey, ap);
      return;
    }

    // If we get here, we want to load the best plan from our map
    // into this Optimizable's trulyTheBestAccessPath field.

    // If we don't have any plans saved, then there's nothing to load.
    // This can happen if the key is an OptimizerImpl that tried some
    // join order for which there was no valid plan.
    if (bestPlanMap == null)
      return;

    ap = (AccessPathImpl)bestPlanMap.get(planKey);

    // It might be the case that there is no plan stored for
    // the key, in which case there's nothing to load.
    if ((ap == null) || (ap.getCostEstimate() == null))
      return;

    // We found a best plan in our map, so load it into this Optimizable's
    // trulyTheBestAccessPath field.
    bestPath.copy(ap);
    return;
  }
View Full Code Here

  /** @see Optimizable#rememberAsBest */
  public void rememberAsBest(int planType, Optimizer optimizer)
    throws StandardException
  {
    AccessPath bestPath = null;

    switch (planType)
    {
      case Optimizer.NORMAL_PLAN:
      bestPath = getBestAccessPath();
      break;

      case Optimizer.SORT_AVOIDANCE_PLAN:
      bestPath = getBestSortAvoidancePath();
      break;

      default:
      if (SanityManager.DEBUG)
      {
        SanityManager.THROWASSERT(
          "Invalid plan type " + planType);
      }
    }

    getTrulyTheBestAccessPath().copy(bestPath);

    // Since we just set trulyTheBestAccessPath for the current
    // join order of the received optimizer, take note of what
    // that path is, in case we need to "revert" back to this
    // path later.  See Optimizable.updateBestPlanMap().
    // Note: Since this call descends all the way down to base
    // tables, it can be relatively expensive when we have deeply
    // nested subqueries.  So in an attempt to save some work, we
    // skip the call if this node is a ProjectRestrictNode whose
    // child is an Optimizable--in that case the ProjectRestrictNode
    // will in turn call "rememberAsBest" on its child and so
    // the required call to updateBestPlanMap() will be
    // made at that time.  If we did it here, too, then we would
    // just end up duplicating the work.
    if (!(this instanceof ProjectRestrictNode))
      updateBestPlanMap(ADD_PLAN, optimizer);
    else
    {
      ProjectRestrictNode prn = (ProjectRestrictNode)this;
      if (!(prn.getChildResult() instanceof Optimizable))
        updateBestPlanMap(ADD_PLAN, optimizer);
    }
    
    /* also store the name of the access path; i.e index name/constraint
     * name if we're using an index to access the base table.
     */
    ConglomerateDescriptor cd =  bestPath.getConglomerateDescriptor();

    if (isBaseTable())
    {
      DataDictionary dd = getDataDictionary();
      TableDescriptor td = getTableDescriptor();
      getTrulyTheBestAccessPath().initializeAccessPathName(dd, td);
    }

    setCostEstimate(bestPath.getCostEstimate());

    bestPath.getOptimizer().trace(Optimizer.REMEMBERING_BEST_ACCESS_PATH,
              tableNumber, planType, 0.0, bestPath);
  }
View Full Code Here

TOP

Related Classes of org.apache.derby.iapi.sql.compile.AccessPath

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.