Search in sources :

Example 1 with SqmOver

use of org.hibernate.query.sqm.tree.expression.SqmOver in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method applyOverClause.

private SqmExpression<?> applyOverClause(HqlParser.OverClauseContext ctx, SqmFunction<?> function) {
    final List<SqmExpression<?>> partitions;
    final List<SqmSortSpecification> orderList;
    final FrameMode mode;
    final FrameKind startKind;
    final SqmExpression<?> startExpression;
    final FrameKind endKind;
    final SqmExpression<?> endExpression;
    final FrameExclusion exclusion;
    int index = 2;
    if (ctx.getChild(index) instanceof HqlParser.PartitionClauseContext) {
        final ParseTree subCtx = ctx.getChild(index);
        partitions = new ArrayList<>((subCtx.getChildCount() >> 1) - 1);
        for (int i = 2; i < subCtx.getChildCount(); i += 2) {
            partitions.add((SqmExpression<?>) subCtx.getChild(i).accept(this));
        }
        index++;
    } else {
        partitions = Collections.emptyList();
    }
    if (index < ctx.getChildCount() && ctx.getChild(index) instanceof HqlParser.OrderByClauseContext) {
        orderList = visitOrderByClause((HqlParser.OrderByClauseContext) ctx.getChild(index)).getSortSpecifications();
        index++;
    } else {
        orderList = Collections.emptyList();
    }
    if (index < ctx.getChildCount() && ctx.getChild(index) instanceof HqlParser.FrameClauseContext) {
        final ParseTree frameCtx = ctx.getChild(index);
        switch(((TerminalNode) frameCtx.getChild(0)).getSymbol().getType()) {
            case HqlParser.RANGE:
                mode = FrameMode.RANGE;
                break;
            case HqlParser.ROWS:
                mode = FrameMode.ROWS;
                break;
            case HqlParser.GROUPS:
                mode = FrameMode.GROUPS;
                break;
            default:
                throw new IllegalArgumentException("Unexpected frame mode: " + frameCtx.getChild(0));
        }
        final int frameStartIndex;
        if (frameCtx.getChild(1) instanceof TerminalNode) {
            frameStartIndex = 2;
            endKind = getFrameKind(frameCtx.getChild(4));
            endExpression = endKind == FrameKind.OFFSET_FOLLOWING || endKind == FrameKind.OFFSET_PRECEDING ? (SqmExpression<?>) frameCtx.getChild(4).getChild(0).accept(this) : null;
        } else {
            frameStartIndex = 1;
            endKind = FrameKind.CURRENT_ROW;
            endExpression = null;
        }
        startKind = getFrameKind(frameCtx.getChild(frameStartIndex));
        startExpression = startKind == FrameKind.OFFSET_FOLLOWING || startKind == FrameKind.OFFSET_PRECEDING ? (SqmExpression<?>) frameCtx.getChild(frameStartIndex).getChild(0).accept(this) : null;
        final ParseTree lastChild = frameCtx.getChild(frameCtx.getChildCount() - 1);
        if (lastChild instanceof HqlParser.FrameExclusionContext) {
            switch(((TerminalNode) lastChild.getChild(1)).getSymbol().getType()) {
                case HqlParser.CURRENT:
                    exclusion = FrameExclusion.CURRENT_ROW;
                    break;
                case HqlParser.GROUP:
                    exclusion = FrameExclusion.GROUP;
                    break;
                case HqlParser.TIES:
                    exclusion = FrameExclusion.TIES;
                    break;
                case HqlParser.NO:
                    exclusion = FrameExclusion.NO_OTHERS;
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected frame exclusion: " + lastChild);
            }
        } else {
            exclusion = FrameExclusion.NO_OTHERS;
        }
    } else {
        mode = FrameMode.ROWS;
        startKind = FrameKind.UNBOUNDED_PRECEDING;
        startExpression = null;
        endKind = FrameKind.CURRENT_ROW;
        endExpression = null;
        exclusion = FrameExclusion.NO_OTHERS;
    }
    return new SqmOver<>(function, partitions, orderList, mode, startKind, startExpression, endKind, endExpression, exclusion);
}
Also used : FrameExclusion(org.hibernate.query.sqm.FrameExclusion) FrameKind(org.hibernate.query.sqm.FrameKind) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) HqlParser(org.hibernate.grammars.hql.HqlParser) FrameMode(org.hibernate.query.sqm.FrameMode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) SqmOver(org.hibernate.query.sqm.tree.expression.SqmOver) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with SqmOver

use of org.hibernate.query.sqm.tree.expression.SqmOver 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)

Aggregations

SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 SqmOver (org.hibernate.query.sqm.tree.expression.SqmOver)2 SqmSortSpecification (org.hibernate.query.sqm.tree.select.SqmSortSpecification)2 ArrayList (java.util.ArrayList)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 FrameExclusion (org.hibernate.query.sqm.FrameExclusion)1 FrameKind (org.hibernate.query.sqm.FrameKind)1 FrameMode (org.hibernate.query.sqm.FrameMode)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)1 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)1 Over (org.hibernate.sql.ast.tree.expression.Over)1 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)1