Search in sources :

Example 1 with SqmOverflow

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

the class SemanticQueryBuilder method visitListaggFunction.

@Override
public Object visitListaggFunction(ListaggFunctionContext ctx) {
    if (creationOptions.useStrictJpaCompliance()) {
        throw new StrictJpaComplianceViolation("Encountered non-compliant non-standard function call [listagg], but strict JPA " + "compliance was requested; use JPA's FUNCTION(functionName[,...]) " + "syntax name instead", StrictJpaComplianceViolation.Type.FUNCTION_CALL);
    }
    final SqmFunctionDescriptor functionTemplate = getFunctionDescriptor("listagg");
    if (functionTemplate == null) {
        throw new SemanticException("The listagg function was not registered for the dialect!");
    }
    final int argumentStartIndex;
    final ParseTree thirdChild = ctx.getChild(2);
    final boolean distinct;
    if (thirdChild instanceof TerminalNode) {
        distinct = true;
        argumentStartIndex = 3;
    } else {
        distinct = false;
        argumentStartIndex = 2;
    }
    final SqmExpression<?> firstArgument = (SqmExpression<?>) ctx.getChild(argumentStartIndex).accept(this);
    final SqmExpression<?> secondArgument = (SqmExpression<?>) ctx.getChild(argumentStartIndex + 2).accept(this);
    final ParseTree overflowCtx = ctx.getChild(argumentStartIndex + 3);
    final List<SqmTypedNode<?>> functionArguments = new ArrayList<>(3);
    if (distinct) {
        functionArguments.add(new SqmDistinct<>(firstArgument, creationContext.getNodeBuilder()));
    } else {
        functionArguments.add(firstArgument);
    }
    if (overflowCtx instanceof OnOverflowClauseContext) {
        if (overflowCtx.getChildCount() > 3) {
            // ON OVERFLOW TRUNCATE
            final TerminalNode countNode = (TerminalNode) overflowCtx.getChild(overflowCtx.getChildCount() - 2);
            final boolean withCount = countNode.getSymbol().getType() == HqlParser.WITH;
            final SqmExpression<?> fillerExpression;
            if (overflowCtx.getChildCount() == 6) {
                fillerExpression = (SqmExpression<?>) overflowCtx.getChild(3).accept(this);
            } else {
                // The SQL standard says the default is three periods `...`
                fillerExpression = new SqmLiteral<>("...", secondArgument.getNodeType(), secondArgument.nodeBuilder());
            }
            // noinspection unchecked,rawtypes
            functionArguments.add(new SqmOverflow(secondArgument, fillerExpression, withCount));
        } else {
            // ON OVERFLOW ERROR
            functionArguments.add(new SqmOverflow<>(secondArgument, null, false));
        }
    } else {
        functionArguments.add(secondArgument);
    }
    final SqmOrderByClause withinGroup = getWithinGroup(ctx);
    final SqmPredicate filterExpression = getFilterExpression(ctx);
    return applyOverClause(ctx, functionTemplate.generateOrderedSetAggregateSqmExpression(functionArguments, filterExpression, withinGroup, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration()));
}
Also used : SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) ArrayList(java.util.ArrayList) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) SqmOverflow(org.hibernate.query.sqm.tree.expression.SqmOverflow) OnOverflowClauseContext(org.hibernate.grammars.hql.HqlParser.OnOverflowClauseContext) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) SemanticException(org.hibernate.query.SemanticException)

Aggregations

ArrayList (java.util.ArrayList)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 OnOverflowClauseContext (org.hibernate.grammars.hql.HqlParser.OnOverflowClauseContext)1 SemanticException (org.hibernate.query.SemanticException)1 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)1 NamedSqmFunctionDescriptor (org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor)1 SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)1 SqmTypedNode (org.hibernate.query.sqm.tree.SqmTypedNode)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmOverflow (org.hibernate.query.sqm.tree.expression.SqmOverflow)1 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)1 SqmOrderByClause (org.hibernate.query.sqm.tree.select.SqmOrderByClause)1