Пример #1
0
 @Override
 public boolean indexAccessible(final IndexInfo ii) throws QueryException {
   int costs = 0;
   final ExprList el = new ExprList(exprs.length);
   for (final Expr expr : exprs) {
     // check if expression can be rewritten, and if access is not sequential
     if (!expr.indexAccessible(ii)) return false;
     // skip expressions without results
     if (ii.costs == 0) continue;
     costs += ii.costs;
     el.add(ii.expr);
   }
   // use summarized costs for estimation
   ii.costs = costs;
   // no expressions means no costs: expression will later be ignored
   ii.expr = el.size() == 1 ? el.get(0) : new Union(info, el.finish());
   return true;
 }
Пример #2
0
  @Override
  public Expr optimize(final QueryContext qc, final VarScope scp) throws QueryException {
    super.optimize(qc, scp);

    final ExprList el = new ExprList(exprs.length);
    for (final Expr ex : exprs) {
      if (ex.isEmpty()) {
        // remove empty operands (return empty sequence if first value is empty)
        if (el.isEmpty()) return optPre(qc);
        qc.compInfo(OPTREMOVE_X_X, this, ex);
      } else {
        el.add(ex);
      }
    }
    // ensure that results are always sorted
    if (el.size() == 1 && iterable) return el.get(0);
    // replace expressions with optimized list
    exprs = el.finish();
    return this;
  }
Пример #3
0
  /**
   * Returns an equivalent expression which accesses an index. If the expression cannot be
   * rewritten, the original expression is returned.
   *
   * <p>The following types of queries can be rewritten (in the examples, the equality comparison is
   * used, which will be rewritten to {@link ValueAccess} instances):
   *
   * <pre>
   * 1. A[text() = '...']    -> IA('...')
   * 2. A[. = '...']         -> IA('...', A)
   * 3. text()[. = '...']    -> IA('...')
   * 4. A[B = '...']         -> IA('...', B)/parent::A
   * 1. A[B/text() = '...']  -> IA('...')/parent::B/parent::A
   * 2. A[B/C = '...']       -> IA('...', C)/parent::B/parent::A
   * 7. A[@a = '...']        -> IA('...', @a)/parent::A
   * 8. @a[. = '...']        -> IA('...', @a)</pre>
   *
   * Queries of type 1, 3, 5 will not yield any results if the string to be compared is empty.
   *
   * @param qc query context
   * @param rt root value
   * @return original or new expression
   * @throws QueryException query exception
   */
  private Expr index(final QueryContext qc, final Value rt) throws QueryException {
    // only rewrite paths with data reference
    final Data data = rt.data();
    if (data == null) return this;

    // cache index access costs
    IndexInfo index = null;
    // cheapest predicate and step
    int iPred = 0, iStep = 0;

    // check if path can be converted to an index access
    final int sl = steps.length;
    for (int s = 0; s < sl; s++) {
      // only accept descendant steps without positional predicates
      final Step step = axisStep(s);
      if (step == null || !step.axis.down || step.has(Flag.FCS)) break;

      // check if path is iterable (i.e., will be duplicate-free)
      final boolean iter = pathNodes(data, s) != null;
      final IndexContext ictx = new IndexContext(data, iter);

      // choose cheapest index access
      final int pl = step.preds.length;
      for (int p = 0; p < pl; p++) {
        final IndexInfo ii = new IndexInfo(ictx, qc, step);
        if (!step.preds[p].indexAccessible(ii)) continue;

        if (ii.costs == 0) {
          // no results...
          qc.compInfo(OPTNOINDEX, this);
          return Empty.SEQ;
        }
        if (index == null || index.costs > ii.costs) {
          index = ii;
          iPred = p;
          iStep = s;
        }
      }
    }

    // skip rewriting if no index access is possible, or if it is too expensive
    if (index == null || index.costs > data.meta.size) return this;

    // rewrite for index access
    qc.compInfo(index.info);

    // replace expressions for index access
    final Step indexStep = index.step;

    // collect remaining predicates
    final int pl = indexStep.preds.length;
    final ExprList newPreds = new ExprList(pl - 1);
    for (int p = 0; p < pl; p++) {
      if (p != iPred) newPreds.add(indexStep.preds[p]);
    }

    // invert steps that occur before index step and add them as predicate
    final Test test = InvDocTest.get(rt);
    final ExprList invSteps = new ExprList();
    if (test != Test.DOC || !data.meta.uptodate || predSteps(data, iStep)) {
      for (int s = iStep; s >= 0; s--) {
        final Axis ax = axisStep(s).axis.invert();
        if (s == 0) {
          // add document test for collections and axes other than ancestors
          if (test != Test.DOC || ax != Axis.ANC && ax != Axis.ANCORSELF)
            invSteps.add(Step.get(info, ax, test));
        } else {
          final Step prev = axisStep(s - 1);
          invSteps.add(Step.get(info, ax, prev.test, prev.preds));
        }
      }
    }
    if (!invSteps.isEmpty()) newPreds.add(get(info, null, invSteps.finish()));

    // create resulting expression
    final ExprList resultSteps = new ExprList();
    final Expr resultRoot;
    if (index.expr instanceof Path) {
      final Path p = (Path) index.expr;
      resultRoot = p.root;
      resultSteps.add(p.steps);
    } else {
      resultRoot = index.expr;
    }

    if (!newPreds.isEmpty()) {
      int ls = resultSteps.size() - 1;
      Step step;
      if (ls < 0 || !(resultSteps.get(ls) instanceof Step)) {
        // add at least one self axis step
        step = Step.get(info, Axis.SELF, Test.NOD);
        ls++;
      } else {
        step = (Step) resultSteps.get(ls);
      }
      // add remaining predicates to last step
      resultSteps.set(ls, step.addPreds(newPreds.finish()));
    }

    // add remaining steps
    for (int s = iStep + 1; s < sl; s++) resultSteps.add(steps[s]);
    return get(info, resultRoot, resultSteps.finish());
  }