Search in sources :

Example 1 with PropertyValueImpl

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

the class QueryImpl method canSortByIndex.

private boolean canSortByIndex() {
    boolean canSortByIndex = false;
    // TODO add issue about order by optimization for multiple selectors
    if (orderings != null && selectors.size() == 1) {
        IndexPlan plan = selectors.get(0).getExecutionPlan().getIndexPlan();
        if (plan != null) {
            List<OrderEntry> list = plan.getSortOrder();
            if (list != null && list.size() == orderings.length) {
                canSortByIndex = true;
                for (int i = 0; i < list.size(); i++) {
                    OrderEntry e = list.get(i);
                    OrderingImpl o = orderings[i];
                    DynamicOperandImpl op = o.getOperand();
                    if (!(op instanceof PropertyValueImpl)) {
                        // ordered by a function: currently not supported
                        canSortByIndex = false;
                        break;
                    }
                    // we only have one selector, so no need to check that
                    // TODO support joins
                    String pn = ((PropertyValueImpl) op).getPropertyName();
                    if (!pn.equals(e.getPropertyName())) {
                        // ordered by another property
                        canSortByIndex = false;
                        break;
                    }
                    if (o.isDescending() != (e.getOrder() == Order.DESCENDING)) {
                        // ordered ascending versus descending
                        canSortByIndex = false;
                        break;
                    }
                }
            }
        }
    }
    return canSortByIndex;
}
Also used : DynamicOperandImpl(org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl) OrderEntry(org.apache.jackrabbit.oak.spi.query.QueryIndex.OrderEntry) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) OrderingImpl(org.apache.jackrabbit.oak.query.ast.OrderingImpl) PropertyValueImpl(org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)

Example 2 with PropertyValueImpl

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

the class SQL2Parser method parseExpressionFunction.

private DynamicOperandImpl parseExpressionFunction(String functionName) throws ParseException {
    DynamicOperandImpl op;
    if ("LENGTH".equalsIgnoreCase(functionName)) {
        op = factory.length(parseDynamicOperand());
    } else if ("NAME".equalsIgnoreCase(functionName)) {
        if (isToken(")")) {
            op = factory.nodeName(getOnlySelectorName());
        } else {
            op = factory.nodeName(readName());
        }
    } else if ("LOCALNAME".equalsIgnoreCase(functionName)) {
        if (isToken(")")) {
            op = factory.nodeLocalName(getOnlySelectorName());
        } else {
            op = factory.nodeLocalName(readName());
        }
    } else if ("SCORE".equalsIgnoreCase(functionName)) {
        if (isToken(")")) {
            op = factory.fullTextSearchScore(getOnlySelectorName());
        } else {
            op = factory.fullTextSearchScore(readName());
        }
    } else if ("LOWER".equalsIgnoreCase(functionName)) {
        op = factory.lowerCase(parseDynamicOperand());
    } else if ("UPPER".equalsIgnoreCase(functionName)) {
        op = factory.upperCase(parseDynamicOperand());
    } else if ("PROPERTY".equalsIgnoreCase(functionName)) {
        PropertyValueImpl pv = parsePropertyValue(readName());
        read(",");
        op = factory.propertyValue(pv.getSelectorName(), pv.getPropertyName(), readString().getValue(Type.STRING));
    } else {
        throw getSyntaxError("LENGTH, NAME, LOCALNAME, SCORE, LOWER, UPPER, or PROPERTY");
    }
    read(")");
    return op;
}
Also used : DynamicOperandImpl(org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl) PropertyValueImpl(org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)

Example 3 with PropertyValueImpl

use of org.apache.jackrabbit.oak.query.ast.PropertyValueImpl 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 4 with PropertyValueImpl

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

the class SQL2Parser method parseCondition.

private ConstraintImpl parseCondition(DynamicOperandImpl left) throws ParseException {
    ConstraintImpl c;
    if (readIf("=")) {
        c = factory.comparison(left, Operator.EQUAL, parseStaticOperand());
    } else if (readIf("<>")) {
        c = factory.comparison(left, Operator.NOT_EQUAL, parseStaticOperand());
    } else if (readIf("<")) {
        c = factory.comparison(left, Operator.LESS_THAN, parseStaticOperand());
    } else if (readIf(">")) {
        c = factory.comparison(left, Operator.GREATER_THAN, parseStaticOperand());
    } else if (readIf("<=")) {
        c = factory.comparison(left, Operator.LESS_OR_EQUAL, parseStaticOperand());
    } else if (readIf(">=")) {
        c = factory.comparison(left, Operator.GREATER_OR_EQUAL, parseStaticOperand());
    } else if (readIf("LIKE")) {
        c = factory.comparison(left, Operator.LIKE, parseStaticOperand());
        if (supportSQL1) {
            if (readIf("ESCAPE")) {
                StaticOperandImpl esc = parseStaticOperand();
                if (!(esc instanceof LiteralImpl)) {
                    throw getSyntaxError("only ESCAPE '\' is supported");
                }
                PropertyValue v = ((LiteralImpl) esc).getLiteralValue();
                if (!v.getValue(Type.STRING).equals("\\")) {
                    throw getSyntaxError("only ESCAPE '\' is supported");
                }
            }
        }
    } else if (readIf("IN")) {
        read("(");
        ArrayList<StaticOperandImpl> list = new ArrayList<StaticOperandImpl>();
        do {
            StaticOperandImpl x = parseStaticOperand();
            list.add(x);
        } while (readIf(","));
        read(")");
        c = factory.in(left, list);
    } else if (readIf("IS")) {
        boolean not = readIf("NOT");
        read("NULL");
        if (!(left instanceof PropertyValueImpl)) {
            throw getSyntaxError("propertyName (NOT NULL is only supported for properties)");
        }
        PropertyValueImpl p = (PropertyValueImpl) left;
        if (not) {
            c = getPropertyExistence(p);
        } else {
            c = getPropertyInexistence(p);
        }
    } else if (readIf("NOT")) {
        if (readIf("IS")) {
            read("NULL");
            if (!(left instanceof PropertyValueImpl)) {
                throw new ParseException("Only property values can be tested for NOT IS NULL; got: " + left.getClass().getName(), parseIndex);
            }
            PropertyValueImpl pv = (PropertyValueImpl) left;
            c = getPropertyExistence(pv);
        } else {
            read("LIKE");
            c = factory.comparison(left, Operator.LIKE, parseStaticOperand());
            c = factory.not(c);
        }
    } else {
        throw getSyntaxError();
    }
    return c;
}
Also used : LiteralImpl(org.apache.jackrabbit.oak.query.ast.LiteralImpl) ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl) StaticOperandImpl(org.apache.jackrabbit.oak.query.ast.StaticOperandImpl) ArrayList(java.util.ArrayList) PropertyValue(org.apache.jackrabbit.oak.api.PropertyValue) ParseException(java.text.ParseException) PropertyValueImpl(org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)

Aggregations

PropertyValueImpl (org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)4 DynamicOperandImpl (org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl)2 LiteralImpl (org.apache.jackrabbit.oak.query.ast.LiteralImpl)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 PropertyValue (org.apache.jackrabbit.oak.api.PropertyValue)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 ColumnImpl (org.apache.jackrabbit.oak.query.ast.ColumnImpl)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 InImpl (org.apache.jackrabbit.oak.query.ast.InImpl)1