| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Expr optimize(final CompileContext cc) throws QueryException { |
| 54 | flatten(cc); |
| 55 | removeEmpty(cc); |
| 56 | toRange(cc); |
| 57 | toReplicate(cc); |
| 58 | |
| 59 | final int el = exprs.length; |
| 60 | if(el == 0) return cc.emptySeq(this); |
| 61 | if(el == 1) return exprs[0]; |
| 62 | |
| 63 | // determine result type, compute number of results, set expression type |
| 64 | final SeqType st = SeqType.union(exprs, false); |
| 65 | Occ occ = Occ.ZERO; |
| 66 | long size = 0; |
| 67 | for(final Expr expr : exprs) { |
| 68 | final long sz = expr.size(); |
| 69 | if(size != -1) size = sz == -1 ? -1 : size + sz; |
| 70 | occ = occ.add(expr.seqType().occ); |
| 71 | } |
| 72 | exprType.assign(st != null ? st : Types.EMPTY_SEQUENCE_Z, occ, size).data(exprs); |
| 73 | |
| 74 | // pre-evaluate list; skip expressions with large result sizes |
| 75 | return values(true, cc) ? cc.preEval(this).shrink(cc.qc) : this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tries to rewrite identical expressions to replicate. |