Search in sources :

Example 1 with FunctionKind

use of org.hibernate.query.sqm.function.FunctionKind in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitGenericFunction.

@Override
public Object visitGenericFunction(HqlParser.GenericFunctionContext ctx) {
    final String originalFunctionName = visitGenericFunctionName(ctx.genericFunctionName());
    final String functionName = originalFunctionName.toLowerCase();
    if (creationOptions.useStrictJpaCompliance() && !JPA_STANDARD_FUNCTIONS.contains(functionName)) {
        throw new StrictJpaComplianceViolation("Encountered non-compliant non-standard function call [" + originalFunctionName + "], but strict JPA " + "compliance was requested; use JPA's FUNCTION(functionName[,...]) " + "syntax name instead", StrictJpaComplianceViolation.Type.FUNCTION_CALL);
    }
    final ParseTree argumentChild = ctx.getChild(2);
    final List<SqmTypedNode<?>> functionArguments;
    if (argumentChild instanceof HqlParser.GenericFunctionArgumentsContext) {
        functionArguments = (List<SqmTypedNode<?>>) argumentChild.accept(this);
    } else if ("*".equals(argumentChild.getText())) {
        functionArguments = Collections.singletonList(new SqmStar(getCreationContext().getNodeBuilder()));
    } else {
        functionArguments = emptyList();
    }
    final Boolean fromFirst = getFromFirst(ctx);
    final Boolean respectNulls = getRespectNullsClause(ctx);
    final SqmOrderByClause withinGroup = getWithinGroup(ctx);
    final SqmPredicate filterExpression = getFilterExpression(ctx);
    final boolean hasOverClause = ctx.getChild(ctx.getChildCount() - 1) instanceof HqlParser.OverClauseContext;
    SqmFunctionDescriptor functionTemplate = getFunctionDescriptor(functionName);
    if (functionTemplate == null) {
        FunctionKind functionKind = FunctionKind.NORMAL;
        if (withinGroup != null) {
            functionKind = FunctionKind.ORDERED_SET_AGGREGATE;
        } else if (hasOverClause) {
            functionKind = FunctionKind.WINDOW;
        } else if (filterExpression != null) {
            functionKind = FunctionKind.AGGREGATE;
        }
        functionTemplate = new NamedSqmFunctionDescriptor(functionName, true, null, StandardFunctionReturnTypeResolvers.invariant(resolveExpressibleTypeBasic(Object.class)), null, functionName, functionKind, null, SqlAstNodeRenderingMode.DEFAULT);
    } else {
        if (hasOverClause && functionTemplate.getFunctionKind() == FunctionKind.NORMAL) {
            throw new SemanticException("OVER clause is illegal for normal function: " + functionName);
        } else if (!hasOverClause && functionTemplate.getFunctionKind() == FunctionKind.WINDOW) {
            throw new SemanticException("OVER clause is mandatory for window-only function: " + functionName);
        }
        if (respectNulls != null) {
            switch(functionName) {
                case "lag":
                case "lead":
                case "first_value":
                case "last_value":
                case "nth_value":
                    break;
                default:
                    throw new SemanticException("RESPECT/IGNORE NULLS is illegal for function: " + functionName);
            }
        }
        if (fromFirst != null && !"nth_value".equals(functionName)) {
            throw new SemanticException("FROM FIRST/LAST is illegal for function: " + functionName);
        }
    }
    final SqmFunction<?> function;
    switch(functionTemplate.getFunctionKind()) {
        case ORDERED_SET_AGGREGATE:
            function = functionTemplate.generateOrderedSetAggregateSqmExpression(functionArguments, filterExpression, withinGroup, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
        case AGGREGATE:
            function = functionTemplate.generateAggregateSqmExpression(functionArguments, filterExpression, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
        case WINDOW:
            function = functionTemplate.generateWindowSqmExpression(functionArguments, filterExpression, null, null, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
        default:
            if (filterExpression != null) {
                throw new ParsingException("Illegal use of a FILTER clause for non-aggregate function: " + originalFunctionName);
            }
            function = functionTemplate.generateSqmExpression(functionArguments, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
    }
    return applyOverClause(ctx, function);
}
Also used : SqmStar(org.hibernate.query.sqm.tree.expression.SqmStar) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) ParsingException(org.hibernate.query.sqm.ParsingException) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) FunctionKind(org.hibernate.query.sqm.function.FunctionKind) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) SemanticException(org.hibernate.query.SemanticException)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 SemanticException (org.hibernate.query.SemanticException)1 ParsingException (org.hibernate.query.sqm.ParsingException)1 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)1 FunctionKind (org.hibernate.query.sqm.function.FunctionKind)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 SqmStar (org.hibernate.query.sqm.tree.expression.SqmStar)1 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)1 SqmOrderByClause (org.hibernate.query.sqm.tree.select.SqmOrderByClause)1