Search in sources :

Example 6 with ColumnImpl

use of org.apache.jackrabbit.oak.query.ast.ColumnImpl in project jackrabbit-oak by apache.

the class QueryImpl method copyOf.

@Override
public Query copyOf() {
    if (isInit()) {
        throw new IllegalStateException("QueryImpl cannot be cloned once initialised.");
    }
    List<ColumnImpl> cols = newArrayList();
    for (ColumnImpl c : columns) {
        cols.add((ColumnImpl) copyElementAndCheckReference(c));
    }
    QueryImpl copy = new QueryImpl(this.statement, (SourceImpl) copyElementAndCheckReference(this.source), this.constraint, cols.toArray(new ColumnImpl[0]), this.namePathMapper, this.settings);
    copy.explain = this.explain;
    copy.distinct = this.distinct;
    return copy;
}
Also used : ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl)

Example 7 with ColumnImpl

use of org.apache.jackrabbit.oak.query.ast.ColumnImpl in project jackrabbit-oak by apache.

the class QueryImpl method init.

@Override
public void init() {
    final QueryImpl query = this;
    if (constraint != null) {
        // need to do this *before* the visitation below, as the
        // simplify() method does not always keep the query reference
        // passed in setQuery(). TODO: avoid that mutability concern
        constraint = constraint.simplify();
    }
    new AstVisitorBase() {

        @Override
        public boolean visit(BindVariableValueImpl node) {
            node.setQuery(query);
            bindVariableMap.put(node.getBindVariableName(), null);
            return true;
        }

        @Override
        public boolean visit(ChildNodeImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(ChildNodeJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(ColumnImpl node) {
            node.setQuery(query);
            return true;
        }

        @Override
        public boolean visit(DescendantNodeImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(DescendantNodeJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(EquiJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(FullTextSearchImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(NativeFunctionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(SimilarImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(SpellcheckImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(SuggestImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(FullTextSearchScoreImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(LiteralImpl node) {
            node.setQuery(query);
            return true;
        }

        @Override
        public boolean visit(NodeLocalNameImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(NodeNameImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(PropertyExistenceImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(PropertyInexistenceImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(PropertyValueImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(SameNodeImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(SameNodeJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(SelectorImpl node) {
            String name = node.getSelectorName();
            if (selectorIndexes.put(name, selectors.size()) != null) {
                throw new IllegalArgumentException("Two selectors with the same name: " + name);
            }
            selectors.add(node);
            node.setQuery(query);
            return true;
        }

        @Override
        public boolean visit(LengthImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(UpperCaseImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(LowerCaseImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(ComparisonImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(InImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(AndImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(OrImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(NotImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }
    }.visit(this);
    source.setQueryConstraint(constraint);
    for (ColumnImpl column : columns) {
        column.bindSelector(source);
    }
    distinctColumns = new boolean[columns.length];
    for (int i = 0; i < columns.length; i++) {
        ColumnImpl c = columns[i];
        boolean distinct = true;
        if (JCR_SCORE.equals(c.getPropertyName())) {
            distinct = false;
        }
        distinctColumns[i] = distinct;
    }
    init = true;
}
Also used : SpellcheckImpl(org.apache.jackrabbit.oak.query.ast.SpellcheckImpl) NodeLocalNameImpl(org.apache.jackrabbit.oak.query.ast.NodeLocalNameImpl) AstVisitorBase(org.apache.jackrabbit.oak.query.ast.AstVisitorBase) FullTextSearchScoreImpl(org.apache.jackrabbit.oak.query.ast.FullTextSearchScoreImpl) PropertyInexistenceImpl(org.apache.jackrabbit.oak.query.ast.PropertyInexistenceImpl) UpperCaseImpl(org.apache.jackrabbit.oak.query.ast.UpperCaseImpl) InImpl(org.apache.jackrabbit.oak.query.ast.InImpl) DescendantNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl) NodeNameImpl(org.apache.jackrabbit.oak.query.ast.NodeNameImpl) BindVariableValueImpl(org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl) SuggestImpl(org.apache.jackrabbit.oak.query.ast.SuggestImpl) ComparisonImpl(org.apache.jackrabbit.oak.query.ast.ComparisonImpl) PropertyExistenceImpl(org.apache.jackrabbit.oak.query.ast.PropertyExistenceImpl) SameNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.SameNodeJoinConditionImpl) ChildNodeImpl(org.apache.jackrabbit.oak.query.ast.ChildNodeImpl) SimilarImpl(org.apache.jackrabbit.oak.query.ast.SimilarImpl) PropertyValueImpl(org.apache.jackrabbit.oak.query.ast.PropertyValueImpl) SameNodeImpl(org.apache.jackrabbit.oak.query.ast.SameNodeImpl) NotImpl(org.apache.jackrabbit.oak.query.ast.NotImpl) DescendantNodeImpl(org.apache.jackrabbit.oak.query.ast.DescendantNodeImpl) NativeFunctionImpl(org.apache.jackrabbit.oak.query.ast.NativeFunctionImpl) ChildNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl) LiteralImpl(org.apache.jackrabbit.oak.query.ast.LiteralImpl) FullTextSearchImpl(org.apache.jackrabbit.oak.query.ast.FullTextSearchImpl) SelectorImpl(org.apache.jackrabbit.oak.query.ast.SelectorImpl) LowerCaseImpl(org.apache.jackrabbit.oak.query.ast.LowerCaseImpl) LengthImpl(org.apache.jackrabbit.oak.query.ast.LengthImpl) AndImpl(org.apache.jackrabbit.oak.query.ast.AndImpl) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl) OrImpl(org.apache.jackrabbit.oak.query.ast.OrImpl) EquiJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl)

Example 8 with ColumnImpl

use of org.apache.jackrabbit.oak.query.ast.ColumnImpl in project jackrabbit-oak by apache.

the class UnionQueryImpl method getRows.

@Override
public Iterator<ResultRowImpl> getRows() {
    prepare();
    if (explain) {
        String plan = getPlan();
        columns = new ColumnImpl[] { new ColumnImpl("explain", "plan", "plan") };
        ResultRowImpl r = new ResultRowImpl(this, Tree.EMPTY_ARRAY, new PropertyValue[] { PropertyValues.newString(plan) }, null, null);
        return Arrays.asList(r).iterator();
    }
    if (LOG.isDebugEnabled()) {
        if (isInternal) {
            LOG.trace("query union plan {}", getPlan());
        } else {
            LOG.debug("query union plan {}", getPlan());
        }
    }
    boolean distinct = !unionAll;
    Comparator<ResultRowImpl> orderBy = ResultRowImpl.getComparator(orderings);
    Iterator<ResultRowImpl> it;
    final Iterator<ResultRowImpl> leftRows = left.getRows();
    final Iterator<ResultRowImpl> rightRows = right.getRows();
    Iterator<ResultRowImpl> leftIter = leftRows;
    Iterator<ResultRowImpl> rightIter = rightRows;
    // if measure retrieve the backing delegate iterator instead
    if (measure) {
        leftIter = ((MeasuringIterator) leftRows).getDelegate();
        rightIter = ((MeasuringIterator) rightRows).getDelegate();
    }
    // Since sorted by index use a merge iterator
    if (isSortedByIndex()) {
        it = FilterIterators.newCombinedFilter(Iterators.mergeSorted(ImmutableList.of(leftIter, rightIter), orderBy), distinct, limit, offset, null, settings);
    } else {
        it = FilterIterators.newCombinedFilter(Iterators.concat(leftIter, rightIter), distinct, limit, offset, orderBy, settings);
    }
    if (measure) {
        // return the measuring iterator for the union
        it = new MeasuringIterator(this, it) {

            MeasuringIterator left = (MeasuringIterator) leftRows;

            MeasuringIterator right = (MeasuringIterator) rightRows;

            @Override
            protected void setColumns(ColumnImpl[] cols) {
                columns = cols;
                left.setColumns(cols);
                right.setColumns(cols);
            }

            @Override
            protected Map<String, Long> getSelectorScanCount() {
                // Merge the 2 maps from the left and right queries to get the selector counts
                Map<String, Long> leftSelectorScan = left.getSelectorScanCount();
                Map<String, Long> rightSelectorScan = right.getSelectorScanCount();
                Map<String, Long> unionScan = Maps.newHashMap(leftSelectorScan);
                for (String key : rightSelectorScan.keySet()) {
                    if (unionScan.containsKey(key)) {
                        unionScan.put(key, rightSelectorScan.get(key) + unionScan.get(key));
                    } else {
                        unionScan.put(key, rightSelectorScan.get(key));
                    }
                }
                return unionScan;
            }

            @Override
            protected long getReadCount() {
                return left.getReadCount() + right.getReadCount();
            }
        };
    }
    return it;
}
Also used : MeasuringIterator(org.apache.jackrabbit.oak.query.QueryImpl.MeasuringIterator) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl) Map(java.util.Map)

Example 9 with ColumnImpl

use of org.apache.jackrabbit.oak.query.ast.ColumnImpl in project jackrabbit-oak by apache.

the class QueryImpl method getRows.

@Override
public Iterator<ResultRowImpl> getRows() {
    prepare();
    if (explain) {
        String plan = getPlan();
        if (measure) {
            plan += " cost: { " + getIndexCostInfo() + " }";
        }
        columns = new ColumnImpl[] { new ColumnImpl("explain", "plan", "plan") };
        ResultRowImpl r = new ResultRowImpl(this, Tree.EMPTY_ARRAY, new PropertyValue[] { PropertyValues.newString(plan) }, null, null);
        return Arrays.asList(r).iterator();
    }
    if (LOG.isDebugEnabled()) {
        logDebug("query execute " + statement);
        logDebug("query plan " + getPlan());
    }
    final RowIterator rowIt = new RowIterator(context.getBaseState());
    Comparator<ResultRowImpl> orderBy;
    if (isSortedByIndex) {
        orderBy = null;
    } else {
        orderBy = ResultRowImpl.getComparator(orderings);
    }
    Iterator<ResultRowImpl> it = FilterIterators.newCombinedFilter(rowIt, distinct, limit, offset, orderBy, settings);
    if (orderBy != null) {
        // this will force the rows to be read, so that the size is known
        it.hasNext();
        // we need the size, and there is no other way to get it right now
        // but we also have to take limit and offset into account
        long read = rowIt.getReadCount();
        // we will ignore whatever is behind 'limit+offset'
        read = Math.min(saturatedAdd(limit, offset), read);
        // and we will skip 'offset' entries
        read = Math.max(0, read - offset);
        size = read;
    }
    if (measure) {
        // return the measuring iterator delegating the readCounts to the rowIterator
        it = new MeasuringIterator(this, it) {

            @Override
            protected void setColumns(ColumnImpl[] col) {
                columns = col;
            }

            @Override
            protected long getReadCount() {
                return rowIt.getReadCount();
            }

            @Override
            protected Map<String, Long> getSelectorScanCount() {
                Map<String, Long> selectorReadCounts = Maps.newHashMap();
                for (SelectorImpl selector : selectors) {
                    selectorReadCounts.put(selector.getSelectorName(), selector.getScanCount());
                }
                return selectorReadCounts;
            }
        };
    }
    return it;
}
Also used : SelectorImpl(org.apache.jackrabbit.oak.query.ast.SelectorImpl) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with ColumnImpl

use of org.apache.jackrabbit.oak.query.ast.ColumnImpl in project jackrabbit-oak by apache.

the class ResultImpl method getColumnSelectorNames.

@Override
public String[] getColumnSelectorNames() {
    ArrayList<String> list = new ArrayList<String>();
    for (ColumnImpl c : query.getColumns()) {
        SelectorImpl selector = c.getSelector();
        String name = selector == null ? null : selector.getSelectorName();
        if (!list.contains(name)) {
            list.add(name);
        }
    }
    return list.toArray(new String[list.size()]);
}
Also used : SelectorImpl(org.apache.jackrabbit.oak.query.ast.SelectorImpl) ArrayList(java.util.ArrayList) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl)

Aggregations

ColumnImpl (org.apache.jackrabbit.oak.query.ast.ColumnImpl)10 SelectorImpl (org.apache.jackrabbit.oak.query.ast.SelectorImpl)4 Map (java.util.Map)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PropertyValue (org.apache.jackrabbit.oak.api.PropertyValue)1 Tree (org.apache.jackrabbit.oak.api.Tree)1 MeasuringIterator (org.apache.jackrabbit.oak.query.QueryImpl.MeasuringIterator)1 AndImpl (org.apache.jackrabbit.oak.query.ast.AndImpl)1 AstVisitorBase (org.apache.jackrabbit.oak.query.ast.AstVisitorBase)1 BindVariableValueImpl (org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl)1 ChildNodeImpl (org.apache.jackrabbit.oak.query.ast.ChildNodeImpl)1 ChildNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl)1 ComparisonImpl (org.apache.jackrabbit.oak.query.ast.ComparisonImpl)1 ConstraintImpl (org.apache.jackrabbit.oak.query.ast.ConstraintImpl)1 DescendantNodeImpl (org.apache.jackrabbit.oak.query.ast.DescendantNodeImpl)1 DescendantNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl)1 EquiJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl)1 FullTextSearchImpl (org.apache.jackrabbit.oak.query.ast.FullTextSearchImpl)1 FullTextSearchScoreImpl (org.apache.jackrabbit.oak.query.ast.FullTextSearchScoreImpl)1