@Override public GroupBy optimize(final QueryContext qc, final VarScope scp) throws QueryException { final int pl = preExpr.length; for (int p = 0; p < pl; p++) { final SeqType it = preExpr[p].seqType(); post[p].refineType(it.withOcc(it.mayBeZero() ? Occ.ZERO_MORE : Occ.ONE_MORE), qc); } return this; }
@Override public Expr optimize(final QueryContext qc, final VarScope scp) throws QueryException { // number of predicates may change in loop for (int p = 0; p < preds.length; p++) { final Expr pred = preds[p]; if (pred instanceof CmpG || pred instanceof CmpV) { final Cmp cmp = (Cmp) pred; if (cmp.exprs[0].isFunction(Function.POSITION)) { final Expr e2 = cmp.exprs[1]; final SeqType st2 = e2.seqType(); // position() = last() -> last() // position() = $n (numeric) -> $n if (e2.isFunction(Function.LAST) || st2.one() && st2.type.isNumber()) { if (cmp instanceof CmpG && ((CmpG) cmp).op == OpG.EQ || cmp instanceof CmpV && ((CmpV) cmp).op == OpV.EQ) { qc.compInfo(OPTWRITE, pred); preds[p] = e2; } } } } else if (pred instanceof And) { if (!pred.has(Flag.FCS)) { // replace AND expression with predicates (don't swap position tests) qc.compInfo(OPTPRED, pred); final Expr[] and = ((Arr) pred).exprs; final int m = and.length - 1; final ExprList el = new ExprList(preds.length + m); for (final Expr e : Arrays.asList(preds).subList(0, p)) el.add(e); for (final Expr a : and) { // wrap test with boolean() if the result is numeric el.add(Function.BOOLEAN.get(null, info, a).optimizeEbv(qc, scp)); } for (final Expr e : Arrays.asList(preds).subList(p + 1, preds.length)) el.add(e); preds = el.finish(); } } else if (pred instanceof ANum) { final ANum it = (ANum) pred; final long i = it.itr(); if (i == it.dbl()) { preds[p] = Pos.get(i, info); } else { qc.compInfo(OPTREMOVE, this, pred); return Empty.SEQ; } } else if (pred.isValue()) { if (pred.ebv(qc, info).bool(info)) { qc.compInfo(OPTREMOVE, this, pred); preds = Array.delete(preds, p--); } else { // handle statically known predicates qc.compInfo(OPTREMOVE, this, pred); return Empty.SEQ; } } } return this; }
@Override public Expr compile(final QueryContext ctx) throws QueryException { for (int e = 0; e < expr.length; e++) expr[e] = expr[e].compile(ctx); // compute number of results size = 0; for (final Expr e : expr) { final long c = e.size(); if (c == -1) { size = c; break; } size += c; } // evaluate sequence type type = SeqType.EMP; Value[] val = new Value[expr.length]; for (int v = 0; v < expr.length; v++) { final Expr e = expr[v]; // check if all expressions are values if (val != null) { if (e.isValue()) val[v] = (Value) e; else val = null; } // skip expression that will not add any results if (e.isEmpty()) continue; // evaluate sequence type final SeqType et = e.type(); type = type == SeqType.EMP ? et : SeqType.get( et.type == type.type ? et.type : AtomType.ITEM, et.mayBeZero() && type.mayBeZero() ? Occ.ZERO_MORE : Occ.ONE_MORE); } // return cached integer sequence, cached values or self reference Expr e = this; final int s = (int) size; if (val != null && size <= Integer.MAX_VALUE) { if (type.type == AtomType.STR) e = StrSeq.get(val, s); else if (type.type == AtomType.BLN) e = BlnSeq.get(val, s); else if (type.type == AtomType.FLT) e = FltSeq.get(val, s); else if (type.type == AtomType.DBL) e = DblSeq.get(val, s); else if (type.type == AtomType.DEC) e = DecSeq.get(val, s); else if (type.type == AtomType.BYT) e = BytSeq.get(val, s); else if (type.type.instanceOf(AtomType.ITR)) e = IntSeq.get(val, s, type.type); else { final ValueBuilder vb = new ValueBuilder(s); for (final Value v : val) vb.add(v); e = vb.value(); } } return optPre(e, ctx); }
@Override public final Expr optimize(final QueryContext qc, final VarScope scp) throws QueryException { final Value v = initial(qc); if (v != null && v.isEmpty() || emptyPath(v)) return optPre(qc); // merge descendant steps Expr e = mergeSteps(qc); if (e == this && v != null && v.type == NodeType.DOC) { // check index access e = index(qc, v); // rewrite descendant to child steps if (e == this) e = children(qc, v); } // recompile path if it has changed if (e != this) return e.compile(qc, scp); // set atomic type for single attribute steps to speed up predicate tests if (root == null && steps.length == 1 && steps[0] instanceof Step) { final Step curr = (Step) steps[0]; if (curr.axis == ATTR && curr.test.kind == Kind.URI_NAME) curr.seqType(SeqType.NOD_ZO); } // choose best path implementation and set type information final Path path = get(info, root, steps); path.size = path.size(qc); path.seqType = SeqType.get(steps[steps.length - 1].seqType().type, size); return path; }