Search in sources :

Example 66 with Expression

use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.

the class JavaQueryInMemoryEvaluator method handleResult.

/**
 * Checks if there are aggregates and handles it.
 * @param resultSet The resultSet containing all elements
 * @return A list with aggregated elements
 */
private List handleResult(List resultSet) {
    List result = new ArrayList();
    final Expression[] grouping = compilation.getExprGrouping();
    if (grouping != null) {
        Comparator c = new Comparator() {

            public int compare(Object arg0, Object arg1) {
                for (int i = 0; i < grouping.length; i++) {
                    state.put(candidateAlias, arg0);
                    Object a = grouping[i].evaluate(evaluator);
                    state.put(candidateAlias, arg1);
                    Object b = grouping[i].evaluate(evaluator);
                    // Put any null values at the end
                    if (a == null && b == null) {
                        return 0;
                    } else if (a == null) {
                        return -1;
                    } else if (b == null) {
                        return 1;
                    } else {
                        int result = ((Comparable) a).compareTo(b);
                        if (result != 0) {
                            return result;
                        }
                    }
                }
                return 0;
            }
        };
        List groups = new ArrayList();
        List group = new ArrayList();
        if (!resultSet.isEmpty()) {
            groups.add(group);
        }
        for (int i = 0; i < resultSet.size(); i++) {
            if (i > 0) {
                if (c.compare(resultSet.get(i - 1), resultSet.get(i)) != 0) {
                    group = new ArrayList();
                    groups.add(group);
                }
            }
            group.add(resultSet.get(i));
        }
        // Apply the result to the generated groups
        for (int i = 0; i < groups.size(); i++) {
            group = (List) groups.get(i);
            result.add(result(group));
        }
    } else {
        boolean aggregates = false;
        Expression[] resultExprs = compilation.getExprResult();
        if (resultExprs.length > 0 && resultExprs[0] instanceof CreatorExpression) {
            Expression[] resExpr = ((CreatorExpression) resultExprs[0]).getArguments().toArray(new Expression[((CreatorExpression) resultExprs[0]).getArguments().size()]);
            for (int i = 0; i < resExpr.length; i++) {
                if (resExpr[i] instanceof InvokeExpression) {
                    String method = ((InvokeExpression) resExpr[i]).getOperation().toLowerCase();
                    if (method.equals("count") || method.equals("sum") || method.equals("avg") || method.equals("min") || method.equals("max")) {
                        aggregates = true;
                    }
                }
            }
        } else {
            for (int i = 0; i < resultExprs.length; i++) {
                if (resultExprs[i] instanceof InvokeExpression) {
                    String method = ((InvokeExpression) resultExprs[i]).getOperation().toLowerCase();
                    if (method.equals("count") || method.equals("sum") || method.equals("avg") || method.equals("min") || method.equals("max")) {
                        aggregates = true;
                    }
                }
            }
        }
        if (aggregates) {
            result.add(result(resultSet));
        } else {
            for (int i = 0; i < resultSet.size(); i++) {
                result.add(result(resultSet.get(i)));
            }
        }
    }
    if (!result.isEmpty() && ((Object[]) result.get(0)).length == 1) {
        List r = result;
        result = new ArrayList();
        for (int i = 0; i < r.size(); i++) {
            result.add(((Object[]) r.get(i))[0]);
        }
    }
    return result;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) Comparator(java.util.Comparator)

Example 67 with Expression

use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.

the class JavaQueryInMemoryEvaluator method sortByGrouping.

private List sortByGrouping(List set) {
    Object[] o = set.toArray();
    // TODO Shouldn't we handle "having" within this?
    final Expression[] grouping = compilation.getExprGrouping();
    Arrays.sort(o, new Comparator() {

        public int compare(Object arg0, Object arg1) {
            for (int i = 0; i < grouping.length; i++) {
                state.put(candidateAlias, arg0);
                Object a = grouping[i].evaluate(evaluator);
                state.put(candidateAlias, arg1);
                Object b = grouping[i].evaluate(evaluator);
                int result = 0;
                if (a == null && b == null) {
                    result = 0;
                } else {
                    if (a == null) {
                        result = -1;
                    } else {
                        result = ((Comparable) a).compareTo(b);
                    }
                }
                if (result != 0) {
                    return result;
                }
            }
            return 0;
        }
    });
    return Arrays.asList(o);
}
Also used : Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) Comparator(java.util.Comparator)

Example 68 with Expression

use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.

the class InMemoryExpressionEvaluator method processCaseExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processCaseExpression(org.datanucleus.query.expression.CaseExpression)
     */
@Override
protected Object processCaseExpression(CaseExpression expr) {
    List<ExpressionPair> exprs = expr.getConditions();
    Iterator<ExpressionPair> exprCondIter = exprs.iterator();
    while (exprCondIter.hasNext()) {
        ExpressionPair pair = exprCondIter.next();
        Expression whenExpr = pair.getWhenExpression();
        Expression actionExpr = pair.getActionExpression();
        Object keyResult = whenExpr.evaluate(this);
        if (keyResult instanceof Boolean) {
            if ((Boolean) keyResult) {
                // This case clause resolves to true, so return its result
                Object value = actionExpr.evaluate(this);
                stack.push(value);
                return value;
            }
        } else {
            NucleusLogger.QUERY.error("Case expression " + expr + " clause " + whenExpr + " did not return boolean");
            Object value = new InMemoryFailure();
            stack.push(value);
            return value;
        }
    }
    // No case clause resolves to true, so return the else result
    Object value = expr.getElseExpression().evaluate(this);
    stack.push(value);
    return value;
}
Also used : CaseExpression(org.datanucleus.query.expression.CaseExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ExpressionPair(org.datanucleus.query.expression.CaseExpression.ExpressionPair)

Example 69 with Expression

use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.

the class InMemoryExpressionEvaluator method processInExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processInExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processInExpression(Expression expr) {
    if (expr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) expr;
        Expression left = dyExpr.getLeft();
        Expression right = dyExpr.getRight();
        Object leftVal = getValueForExpression(left);
        Object rightVal = getValueForExpression(right);
        if (rightVal instanceof Collection) {
            Boolean result = Boolean.FALSE;
            Iterator rightValIter = ((Collection) rightVal).iterator();
            while (rightValIter.hasNext()) {
                Object rightElem = rightValIter.next();
                if (leftVal.equals(rightElem)) {
                    result = Boolean.TRUE;
                    break;
                }
            }
            stack.push(result);
            return result;
        }
        Boolean result = leftVal.equals(rightVal) ? Boolean.TRUE : Boolean.FALSE;
        stack.push(result);
        return result;
    }
    return super.processInExpression(expr);
}
Also used : CaseExpression(org.datanucleus.query.expression.CaseExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Iterator(java.util.Iterator) Collection(java.util.Collection) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 70 with Expression

use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.

the class InMemoryExpressionEvaluator method getValueForPrimaryExpression.

/**
 * Convenience method to get the value for a PrimaryExpression.
 * @param primExpr Expression
 * @return The value in the object for this expression
 */
public Object getValueForPrimaryExpression(PrimaryExpression primExpr) {
    Object value = null;
    if (primExpr.getLeft() != null) {
        // Get value of left expression
        if (primExpr.getLeft() instanceof DyadicExpression) {
            DyadicExpression dyExpr = (DyadicExpression) primExpr.getLeft();
            if (dyExpr.getOperator() == Expression.OP_CAST) {
                Expression castLeftExpr = dyExpr.getLeft();
                if (castLeftExpr instanceof PrimaryExpression) {
                    value = getValueForPrimaryExpression((PrimaryExpression) castLeftExpr);
                    String castClassName = (String) ((Literal) dyExpr.getRight()).getLiteral();
                    if (value != null) {
                        // TODO Do this in the compilation stage, and check for ClassNotResolvedException
                        Class castClass = imports.resolveClassDeclaration(castClassName, clr, null);
                        if (!castClass.isAssignableFrom(value.getClass())) {
                            NucleusLogger.QUERY.debug("In-memory query requires cast of " + StringUtils.toJVMIDString(value) + " to " + castClass.getName() + " which is impossible, so exiting for this candidate");
                            return new InMemoryFailure();
                        }
                    }
                } else if (castLeftExpr instanceof VariableExpression) {
                    value = getValueForVariableExpression((VariableExpression) castLeftExpr);
                    String castClassName = (String) ((Literal) dyExpr.getRight()).getLiteral();
                    if (value != null) {
                        // TODO Do this in the compilation stage, and check for ClassNotResolvedException
                        Class castClass = imports.resolveClassDeclaration(castClassName, clr, null);
                        if (!castClass.isAssignableFrom(value.getClass())) {
                            NucleusLogger.QUERY.debug("In-memory query requires cast of " + StringUtils.toJVMIDString(value) + " to " + castClass.getName() + " which is impossible, so exiting for this candidate");
                            return new InMemoryFailure();
                        }
                    }
                } else {
                    // TODO Allow for cast of ParameterExpression
                    NucleusLogger.QUERY.warn("Dont currently support CastExpression of " + castLeftExpr);
                    return new InMemoryFailure();
                }
            } else {
                NucleusLogger.QUERY.error("Dont currently support PrimaryExpression starting with DyadicExpression of " + dyExpr);
                return new InMemoryFailure();
            }
        } else if (primExpr.getLeft() instanceof ParameterExpression) {
            value = QueryUtils.getValueForParameterExpression(parameterValues, (ParameterExpression) primExpr.getLeft());
        } else if (primExpr.getLeft() instanceof InvokeExpression) {
            InvokeExpression invokeExpr = (InvokeExpression) primExpr.getLeft();
            value = getValueForInvokeExpression(invokeExpr);
        } else if (primExpr.getLeft() instanceof VariableExpression) {
            VariableExpression varExpr = (VariableExpression) primExpr.getLeft();
            try {
                value = getValueForVariableExpression(varExpr);
            } catch (VariableNotSetException vnse) {
                // We don't know the possible values here!
                NucleusLogger.QUERY.error("Attempt to access variable " + varExpr.getId() + " as part of primaryExpression " + primExpr + " : variable is not yet set!");
                return new InMemoryFailure();
            }
        } else {
            NucleusLogger.QUERY.warn("Dont currently support PrimaryExpression with left-side of " + primExpr.getLeft());
            return new InMemoryFailure();
        }
    }
    int firstTupleToProcess = 0;
    if (value == null) {
        if (state.containsKey(primExpr.getTuples().get(0))) {
            // Get value from the first node
            value = state.get(primExpr.getTuples().get(0));
            firstTupleToProcess = 1;
        } else if (state.containsKey(candidateAlias)) {
            // Get value from the candidate
            value = state.get(candidateAlias);
        }
    }
    // Evaluate the field of this value
    for (int i = firstTupleToProcess; i < primExpr.getTuples().size(); i++) {
        String fieldName = primExpr.getTuples().get(i);
        if (value instanceof Optional) {
            // Treat Optional as the wrapped value
            Optional opt = (Optional) value;
            value = opt.isPresent() ? opt.get() : null;
        }
        if (!fieldName.equals(candidateAlias)) {
            boolean getValueByReflection = true;
            if (ec.getApiAdapter().isPersistent(value)) {
                // Make sure this field is loaded
                ObjectProvider valueOP = ec.findObjectProvider(value);
                if (valueOP != null) {
                    AbstractMemberMetaData mmd = valueOP.getClassMetaData().getMetaDataForMember(fieldName);
                    if (mmd == null) {
                        NucleusLogger.QUERY.error("Cannot find " + fieldName + " member of " + valueOP.getClassMetaData().getFullClassName());
                        return new InMemoryFailure();
                    }
                    if (mmd.getAbsoluteFieldNumber() >= 0) {
                        // Field is managed so make sure it is loaded, and get its value
                        valueOP.isLoaded(mmd.getAbsoluteFieldNumber());
                        value = valueOP.provideField(mmd.getAbsoluteFieldNumber());
                        getValueByReflection = false;
                    }
                }
            }
            if (getValueByReflection) {
                value = ClassUtils.getValueOfFieldByReflection(value, fieldName);
            }
        }
    }
    return value;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Optional(java.util.Optional) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ObjectProvider(org.datanucleus.state.ObjectProvider) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

Expression (org.datanucleus.query.expression.Expression)93 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)81 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)66 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)65 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)65 VariableExpression (org.datanucleus.query.expression.VariableExpression)57 NucleusException (org.datanucleus.exceptions.NucleusException)41 Literal (org.datanucleus.query.expression.Literal)37 OrderExpression (org.datanucleus.query.expression.OrderExpression)31 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)28 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)25 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)24 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)23 ClassExpression (org.datanucleus.query.expression.ClassExpression)22 JoinExpression (org.datanucleus.query.expression.JoinExpression)22 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)22 HashMap (java.util.HashMap)17 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)17 CaseExpression (org.datanucleus.query.expression.CaseExpression)17 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)16