Search in sources :

Example 1 with VisitorAdapter

use of com.blazebit.persistence.parser.expression.VisitorAdapter in project blaze-persistence by Blazebit.

the class GroupByExpressionGatheringVisitor method extractGroupByExpressions.

public Set<Expression> extractGroupByExpressions(Expression expression, ClauseType clauseType) {
    // Also, grouping by just a parameter does not make sense as the value is constant
    if (expression instanceof LiteralExpression || expression instanceof ParameterExpression) {
        return Collections.emptySet();
    }
    clear();
    try {
        // When having a predicate at the top level, we have to collect
        collect = expression instanceof Predicate;
        boolean expressionWasSplit = expression.accept(this);
        // In the HAVING clause we additionally add the path expressions to the expression set for DBMS like MySQL that's don't support partial expression structure matching
        if (clauseType == ClauseType.HAVING && !dbmsDialect.supportsGroupByExpressionInHavingMatching()) {
            expression.accept(new VisitorAdapter() {

                @Override
                public void visit(FunctionExpression expression) {
                    // Skip aggregate expressions
                    if (expression instanceof AggregateExpression || (treatSizeAsAggregate && com.blazebit.persistence.parser.util.ExpressionUtils.isSizeFunction(expression))) {
                        return;
                    }
                    super.visit(expression);
                }

                @Override
                public void visit(SubqueryExpression expression) {
                    GroupByExpressionGatheringVisitor.this.visit(expression);
                }

                @Override
                public void visit(PathExpression expression) {
                    if (expression.getBaseNode() == null) {
                        ((SelectInfo) aliasManager.getAliasInfo(expression.toString())).getExpression().accept(this);
                    } else {
                        expressions.add(expression);
                    }
                }

                @Override
                public void visit(TreatExpression expression) {
                    expressions.add(expression);
                }

                @Override
                public void visit(PropertyExpression expression) {
                    expressions.add(expression);
                }

                @Override
                public void visit(ListIndexExpression expression) {
                    expressions.add(expression);
                }

                @Override
                public void visit(MapEntryExpression expression) {
                    expressions.add(expression);
                }

                @Override
                public void visit(MapKeyExpression expression) {
                    expressions.add(expression);
                }

                @Override
                public void visit(MapValueExpression expression) {
                    expressions.add(expression);
                }
            });
            return expressions;
        }
        if (expressionWasSplit) {
            return expressions;
        }
    } catch (IllegalParameterException ex) {
        throw new IllegalArgumentException("Can't use the expression '" + expression + "' as an implicit group by clause, because the parameter '" + ex.parameterExpression + "' with the value '" + ex.value + "' can't be rendered as literal which is required!", ex);
    }
    return Collections.singleton(expression);
}
Also used : TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) MapValueExpression(com.blazebit.persistence.parser.expression.MapValueExpression) LiteralExpression(com.blazebit.persistence.parser.expression.LiteralExpression) AggregateExpression(com.blazebit.persistence.parser.expression.AggregateExpression) BetweenPredicate(com.blazebit.persistence.parser.predicate.BetweenPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) BinaryExpressionPredicate(com.blazebit.persistence.parser.predicate.BinaryExpressionPredicate) IsEmptyPredicate(com.blazebit.persistence.parser.predicate.IsEmptyPredicate) IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) InPredicate(com.blazebit.persistence.parser.predicate.InPredicate) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) MapEntryExpression(com.blazebit.persistence.parser.expression.MapEntryExpression) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) ParameterExpression(com.blazebit.persistence.parser.expression.ParameterExpression) VisitorAdapter(com.blazebit.persistence.parser.expression.VisitorAdapter) AbortableVisitorAdapter(com.blazebit.persistence.parser.expression.AbortableVisitorAdapter) ListIndexExpression(com.blazebit.persistence.parser.expression.ListIndexExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Aggregations

AbortableVisitorAdapter (com.blazebit.persistence.parser.expression.AbortableVisitorAdapter)1 AggregateExpression (com.blazebit.persistence.parser.expression.AggregateExpression)1 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)1 ListIndexExpression (com.blazebit.persistence.parser.expression.ListIndexExpression)1 LiteralExpression (com.blazebit.persistence.parser.expression.LiteralExpression)1 MapEntryExpression (com.blazebit.persistence.parser.expression.MapEntryExpression)1 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)1 MapValueExpression (com.blazebit.persistence.parser.expression.MapValueExpression)1 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)1 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)1 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)1 SubqueryExpression (com.blazebit.persistence.parser.expression.SubqueryExpression)1 TreatExpression (com.blazebit.persistence.parser.expression.TreatExpression)1 VisitorAdapter (com.blazebit.persistence.parser.expression.VisitorAdapter)1 BetweenPredicate (com.blazebit.persistence.parser.predicate.BetweenPredicate)1 BinaryExpressionPredicate (com.blazebit.persistence.parser.predicate.BinaryExpressionPredicate)1 CompoundPredicate (com.blazebit.persistence.parser.predicate.CompoundPredicate)1 InPredicate (com.blazebit.persistence.parser.predicate.InPredicate)1 IsEmptyPredicate (com.blazebit.persistence.parser.predicate.IsEmptyPredicate)1 IsNullPredicate (com.blazebit.persistence.parser.predicate.IsNullPredicate)1