Search in sources :

Example 1 with SqmTypedNode

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

the class SqmCriteriaNodeBuilder method createTrimNode.

private SqmFunction<String> createTrimNode(TrimSpec trimSpecification, SqmExpression<Character> trimCharacter, SqmExpression<String> source) {
    if (trimSpecification == null) {
        trimSpecification = TrimSpec.BOTH;
    }
    if (trimCharacter == null) {
        trimCharacter = new SqmLiteral<>(' ', getTypeConfiguration().standardBasicTypeForJavaType(Character.class), this);
    }
    final ArrayList<SqmTypedNode<?>> arguments = new ArrayList<>(3);
    arguments.add(new SqmTrimSpecification(trimSpecification, this));
    arguments.add(trimCharacter);
    arguments.add(source);
    return getFunctionDescriptor("trim").generateSqmExpression(arguments, null, getQueryEngine(), getJpaMetamodel().getTypeConfiguration());
}
Also used : SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) SqmTrimSpecification(org.hibernate.query.sqm.tree.expression.SqmTrimSpecification) ArrayList(java.util.ArrayList)

Example 2 with SqmTypedNode

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

the class SemanticQueryBuilder method visitGenericFunctionArguments.

@Override
public List<SqmTypedNode<?>> visitGenericFunctionArguments(HqlParser.GenericFunctionArgumentsContext ctx) {
    final int size = ctx.getChildCount();
    final int lastIndex = size - 1;
    // Shift 1 bit instead of division by 2
    final int estimateArgumentCount = size >> 1;
    final List<SqmTypedNode<?>> arguments = new ArrayList<>(estimateArgumentCount);
    int i = 0;
    boolean distinct = false;
    final ParseTree firstChild = ctx.getChild(0);
    if (firstChild instanceof HqlParser.DatetimeFieldContext) {
        arguments.add(toDurationUnit((SqmExtractUnit<?>) firstChild.accept(this)));
        i += 2;
    } else if (firstChild instanceof TerminalNode) {
        distinct = true;
        i++;
    }
    for (; i < size; i += 2) {
        // we handle the final argument differently...
        if (i == lastIndex) {
            arguments.add(visitFinalFunctionArgument(ctx.getChild(i)));
        } else {
            arguments.add((SqmTypedNode<?>) ctx.getChild(i).accept(this));
        }
    }
    if (distinct) {
        final NodeBuilder nodeBuilder = getCreationContext().getNodeBuilder();
        if (arguments.size() == 1) {
            arguments.set(0, new SqmDistinct<>((SqmExpression<?>) arguments.get(0), nodeBuilder));
        } else {
            final List<SqmTypedNode<?>> newArguments = new ArrayList<>(1);
            // noinspection unchecked
            newArguments.add(new SqmDistinct<>(new SqmTuple<>((List<SqmExpression<?>>) (List<?>) arguments, nodeBuilder), nodeBuilder));
            return newArguments;
        }
    }
    return arguments;
}
Also used : SqmTuple(org.hibernate.query.sqm.tree.expression.SqmTuple) SqmExtractUnit(org.hibernate.query.sqm.tree.expression.SqmExtractUnit) ArrayList(java.util.ArrayList) NodeBuilder(org.hibernate.query.sqm.NodeBuilder) SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with SqmTypedNode

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

Example 4 with SqmTypedNode

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

the class SemanticQueryBuilder method visitJpaNonstandardFunction.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Functions
@Override
public SqmExpression<?> visitJpaNonstandardFunction(HqlParser.JpaNonstandardFunctionContext ctx) {
    final String functionName = QuotingHelper.unquoteStringLiteral(ctx.getChild(2).getText()).toLowerCase();
    final List<SqmTypedNode<?>> functionArguments;
    if (ctx.getChildCount() > 4) {
        // noinspection unchecked
        functionArguments = (List<SqmTypedNode<?>>) ctx.getChild(4).accept(this);
    } else {
        functionArguments = emptyList();
    }
    SqmFunctionDescriptor functionTemplate = getFunctionDescriptor(functionName);
    if (functionTemplate == null) {
        functionTemplate = new NamedSqmFunctionDescriptor(functionName, true, null, StandardFunctionReturnTypeResolvers.invariant(resolveExpressibleTypeBasic(Object.class)), null);
    }
    return functionTemplate.generateSqmExpression(functionArguments, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
}
Also used : SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor)

Example 5 with SqmTypedNode

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

SqmTypedNode (org.hibernate.query.sqm.tree.SqmTypedNode)5 ArrayList (java.util.ArrayList)3 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 NamedSqmFunctionDescriptor (org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor)3 SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)3 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 SemanticException (org.hibernate.query.SemanticException)2 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)2 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2 SqmOrderByClause (org.hibernate.query.sqm.tree.select.SqmOrderByClause)2 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 OnOverflowClauseContext (org.hibernate.grammars.hql.HqlParser.OnOverflowClauseContext)1 NodeBuilder (org.hibernate.query.sqm.NodeBuilder)1 ParsingException (org.hibernate.query.sqm.ParsingException)1 FunctionKind (org.hibernate.query.sqm.function.FunctionKind)1 SqmExtractUnit (org.hibernate.query.sqm.tree.expression.SqmExtractUnit)1