Search in sources :

Example 6 with SqmSortSpecification

use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.

the class HypotheticalSetWindowEmulation method generateSqmOrderedSetAggregateFunctionExpression.

@Override
public <T> SelfRenderingSqmOrderedSetAggregateFunction<T> generateSqmOrderedSetAggregateFunctionExpression(List<? extends SqmTypedNode<?>> arguments, SqmPredicate filter, SqmOrderByClause withinGroupClause, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
    return new SelfRenderingSqmOrderedSetAggregateFunction<>(this, this, arguments, filter, withinGroupClause, impliedResultType, getArgumentsValidator(), getReturnTypeResolver(), queryEngine.getCriteriaBuilder(), getName()) {

        @Override
        public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
            final Clause currentClause = walker.getCurrentClauseStack().getCurrent();
            if (currentClause == Clause.OVER) {
                return super.convertToSqlAst(walker);
            } else if (currentClause != Clause.SELECT) {
                throw new IllegalArgumentException("Can't emulate [" + getName() + "] in clause " + currentClause + ". Only the SELECT clause is supported!");
            }
            final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
            List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
            ArgumentsValidator argumentsValidator = getArgumentsValidator();
            if (argumentsValidator != null) {
                argumentsValidator.validateSqlTypes(arguments, getFunctionName());
            }
            List<SortSpecification> withinGroup;
            if (this.getWithinGroup() == null) {
                withinGroup = Collections.emptyList();
            } else {
                walker.getCurrentClauseStack().push(Clause.ORDER);
                try {
                    final List<SqmSortSpecification> sortSpecifications = this.getWithinGroup().getSortSpecifications();
                    withinGroup = new ArrayList<>(sortSpecifications.size());
                    for (SqmSortSpecification sortSpecification : sortSpecifications) {
                        final SortSpecification specification = (SortSpecification) walker.visitSortSpecification(sortSpecification);
                        if (specification != null) {
                            withinGroup.add(specification);
                        }
                    }
                } finally {
                    walker.getCurrentClauseStack().pop();
                }
            }
            final SelfRenderingFunctionSqlAstExpression function = new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), Collections.emptyList(), getFilter() == null ? null : (Predicate) getFilter().accept(walker), Collections.emptyList(), resultType, getMappingModelExpressible(walker, resultType));
            final Over<Object> windowFunction = new Over<>(function, new ArrayList<>(), withinGroup);
            walker.registerQueryTransformer(new AggregateWindowEmulationQueryTransformer(windowFunction, withinGroup, arguments));
            return windowFunction;
        }
    };
}
Also used : SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) Over(org.hibernate.sql.ast.tree.expression.Over) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression) Clause(org.hibernate.sql.ast.Clause) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) ArgumentsValidator(org.hibernate.query.sqm.produce.function.ArgumentsValidator) SelfRenderingSqmOrderedSetAggregateFunction(org.hibernate.query.sqm.function.SelfRenderingSqmOrderedSetAggregateFunction)

Example 7 with SqmSortSpecification

use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitSortSpecification.

@Override
public SqmSortSpecification visitSortSpecification(HqlParser.SortSpecificationContext ctx) {
    final SqmExpression<?> sortExpression = visitSortExpression((HqlParser.SortExpressionContext) ctx.getChild(0));
    if (sortExpression == null) {
        throw new ParsingException("Could not resolve sort-expression : " + ctx.getChild(0).getText());
    }
    if (sortExpression instanceof SqmLiteral || sortExpression instanceof SqmParameter) {
        HqlLogging.QUERY_LOGGER.debugf("Questionable sorting by constant value : %s", sortExpression);
    }
    final SortOrder sortOrder;
    final NullPrecedence nullPrecedence;
    int nextIndex = 1;
    if (nextIndex < ctx.getChildCount()) {
        ParseTree parseTree = ctx.getChild(nextIndex);
        if (parseTree instanceof HqlParser.SortDirectionContext) {
            switch(((TerminalNode) parseTree.getChild(0)).getSymbol().getType()) {
                case HqlParser.ASC:
                    sortOrder = SortOrder.ASCENDING;
                    break;
                case HqlParser.DESC:
                    sortOrder = SortOrder.DESCENDING;
                    break;
                default:
                    throw new SemanticException("Unrecognized sort ordering: " + parseTree.getText());
            }
            nextIndex++;
        } else {
            sortOrder = null;
        }
        parseTree = ctx.getChild(nextIndex);
        if (parseTree instanceof HqlParser.NullsPrecedenceContext) {
            switch(((TerminalNode) parseTree.getChild(1)).getSymbol().getType()) {
                case HqlParser.FIRST:
                    nullPrecedence = NullPrecedence.FIRST;
                    break;
                case HqlParser.LAST:
                    nullPrecedence = NullPrecedence.LAST;
                    break;
                default:
                    throw new SemanticException("Unrecognized null precedence: " + parseTree.getText());
            }
        } else {
            nullPrecedence = null;
        }
    } else {
        sortOrder = null;
        nullPrecedence = null;
    }
    return new SqmSortSpecification(sortExpression, sortOrder, nullPrecedence);
}
Also used : NullPrecedence(org.hibernate.query.sqm.NullPrecedence) SortOrder(org.hibernate.query.sqm.SortOrder) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) HqlParser(org.hibernate.grammars.hql.HqlParser) ParsingException(org.hibernate.query.sqm.ParsingException) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Example 8 with SqmSortSpecification

use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitOver.

@Override
public Object visitOver(SqmOver<?> over) {
    currentClauseStack.push(Clause.OVER);
    final Expression expression = (Expression) over.getExpression().accept(this);
    final List<Expression> partitions = new ArrayList<>(over.getPartitions().size());
    for (SqmExpression<?> partition : over.getPartitions()) {
        partitions.add((Expression) partition.accept(this));
    }
    final List<SortSpecification> orderList = new ArrayList<>(over.getOrderList().size());
    for (SqmSortSpecification sortSpecification : over.getOrderList()) {
        orderList.add(visitSortSpecification(sortSpecification));
    }
    final Over<Object> overExpression = new Over<>(expression, partitions, orderList, over.getMode(), over.getStartKind(), over.getStartExpression() == null ? null : (Expression) over.getStartExpression().accept(this), over.getEndKind(), over.getEndExpression() == null ? null : (Expression) over.getEndExpression().accept(this), over.getExclusion());
    currentClauseStack.pop();
    return overExpression;
}
Also used : SqmOver(org.hibernate.query.sqm.tree.expression.SqmOver) Over(org.hibernate.sql.ast.tree.expression.Over) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) ArrayList(java.util.ArrayList) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification)

Example 9 with SqmSortSpecification

use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.

the class SelfRenderingSqmOrderedSetAggregateFunction method convertToSqlAst.

@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
    final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
    List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
    ArgumentsValidator argumentsValidator = getArgumentsValidator();
    if (argumentsValidator != null) {
        argumentsValidator.validateSqlTypes(arguments, getFunctionName());
    }
    List<SortSpecification> withinGroup;
    if (this.withinGroup == null) {
        withinGroup = Collections.emptyList();
    } else {
        walker.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
        try {
            final List<SqmSortSpecification> sortSpecifications = this.withinGroup.getSortSpecifications();
            withinGroup = new ArrayList<>(sortSpecifications.size());
            for (SqmSortSpecification sortSpecification : sortSpecifications) {
                final SortSpecification specification = (SortSpecification) walker.visitSortSpecification(sortSpecification);
                if (specification != null) {
                    withinGroup.add(specification);
                }
            }
        } finally {
            walker.getCurrentClauseStack().pop();
        }
    }
    return new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), arguments, getFilter() == null ? null : walker.visitNestedTopLevelPredicate(getFilter()), withinGroup, resultType, getMappingModelExpressible(walker, resultType));
}
Also used : SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) ArgumentsValidator(org.hibernate.query.sqm.produce.function.ArgumentsValidator) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification)

Example 10 with SqmSortSpecification

use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.

the class SortSpecificationReversalTests method testDefaultSqmSortSpecificationReverse.

@Test
public void testDefaultSqmSortSpecificationReverse() {
    SqmExpression sortExpression = mock(SqmExpression.class);
    SqmSortSpecification order = new SqmSortSpecification(sortExpression, ASCENDING, FIRST);
    assertEquals(sortExpression, order.getSortExpression());
    assertEquals(ASCENDING, order.getSortOrder());
    assertEquals(FIRST, order.getNullPrecedence());
    JpaOrder reversed = order.reverse();
    assertEquals(DESCENDING, reversed.getSortOrder());
    assertEquals(FIRST, reversed.getNullPrecedence());
    assertNotSame("Order.reverse() should create new instance", order, reversed);
}
Also used : SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) JpaOrder(org.hibernate.query.criteria.JpaOrder) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) Test(org.junit.Test)

Aggregations

SqmSortSpecification (org.hibernate.query.sqm.tree.select.SqmSortSpecification)11 SortSpecification (org.hibernate.sql.ast.tree.select.SortSpecification)5 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)3 ArgumentsValidator (org.hibernate.query.sqm.produce.function.ArgumentsValidator)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)3 SqlAstNode (org.hibernate.sql.ast.tree.SqlAstNode)3 Over (org.hibernate.sql.ast.tree.expression.Over)3 ArrayList (java.util.ArrayList)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 HqlParser (org.hibernate.grammars.hql.HqlParser)2 SelfRenderingOrderedSetAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression)2 SqmToSqlAstConverter (org.hibernate.query.sqm.sql.SqmToSqlAstConverter)2 SqmOver (org.hibernate.query.sqm.tree.expression.SqmOver)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2 SqmOrderByClause (org.hibernate.query.sqm.tree.select.SqmOrderByClause)2 Clause (org.hibernate.sql.ast.Clause)2 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)2 Test (org.junit.Test)2 Order (jakarta.persistence.criteria.Order)1 BigInteger (java.math.BigInteger)1