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()));
}
Aggregations