Search in sources :

Example 1 with NamedSqmFunctionDescriptor

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

the class StringArrayFunctionContributor method contributeFunctions.

@Override
public void contributeFunctions(FunctionContributions functionContributions) {
    TypeConfiguration typeConfiguration = functionContributions.getTypeConfiguration();
    functionContributions.getFunctionRegistry().register("array_length", new NamedSqmFunctionDescriptor("array_length", true, StandardArgumentsValidators.exactly(1), StandardFunctionReturnTypeResolvers.invariant(typeConfiguration.getBasicTypeRegistry().resolve(StandardBasicTypes.INTEGER))));
}
Also used : NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration)

Example 2 with NamedSqmFunctionDescriptor

use of org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor 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 3 with NamedSqmFunctionDescriptor

use of org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor 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 4 with NamedSqmFunctionDescriptor

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

the class SqmCriteriaNodeBuilder method function.

@Override
public <T> SqmFunction<T> function(String name, Class<T> type, Expression<?>[] args) {
    SqmFunctionDescriptor functionTemplate = getFunctionDescriptor(name);
    final BasicType<T> resultType = getTypeConfiguration().getBasicTypeForJavaType(type);
    if (functionTemplate == null) {
        functionTemplate = new NamedSqmFunctionDescriptor(name, true, null, StandardFunctionReturnTypeResolvers.invariant(resultType), null);
    }
    return functionTemplate.generateSqmExpression(expressionList(args), resultType, getQueryEngine(), getJpaMetamodel().getTypeConfiguration());
}
Also used : NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor)

Example 5 with NamedSqmFunctionDescriptor

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

the class OracleSDOFunctionDescriptors method registerSDOFunctions.

private void registerSDOFunctions() {
    map.put(CommonSpatialFunction.ST_ASTEXT.getKey(), new NamedSqmFunctionDescriptor("SDO_UTIL.TO_WKTGEOMETRY", false, StandardArgumentsValidators.exactly(1), StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.STRING))));
    map.put(CommonSpatialFunction.ST_GEOMETRYTYPE.getKey(), new SDOGetGeometryType(typeRegistry));
    map.put(CommonSpatialFunction.ST_DIMENSION.getKey(), new SDOMethodDescriptor("Get_Dims", StandardArgumentsValidators.exactly(1), StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.INTEGER))));
    map.put(CommonSpatialFunction.ST_ENVELOPE.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_MBR", true, StandardArgumentsValidators.exactly(1), StandardFunctionReturnTypeResolvers.useFirstNonNull()));
    map.put(CommonSpatialFunction.ST_SRID.getKey(), new SDOMethodDescriptor("SDO_SRID", false, StandardArgumentsValidators.exactly(1), StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.INTEGER))));
    map.put(CommonSpatialFunction.ST_ASBINARY.getKey(), new SDOMethodDescriptor("Get_WKB", true, StandardArgumentsValidators.exactly(1), StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.BINARY))));
    map.put(CommonSpatialFunction.ST_ISSIMPLE.getKey(), new OracleSpatialSQLMMFunction("ST_ISSIMPLE", "ST_ISSIMPLE", 1, StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.BOOLEAN)), false));
    map.put(CommonSpatialFunction.ST_ISEMPTY.getKey(), new OracleSpatialSQLMMFunction("ST_ISEMPTY", "ST_ISEMPTY", 1, StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.BOOLEAN)), false));
    map.put(CommonSpatialFunction.ST_BOUNDARY.getKey(), new OracleSpatialSQLMMFunction("ST_BOUNDARY", "ST_BOUNDARY", 1, StandardFunctionReturnTypeResolvers.useFirstNonNull(), true));
    map.put(CommonSpatialFunction.ST_OVERLAPS.getKey(), new SDORelateFunction(List.of("CONTAINS"), typeRegistry));
    map.put(CommonSpatialFunction.ST_CROSSES.getKey(), new OracleSpatialSQLMMFunction("ST_CROSSES", "ST_CROSSES", 2, StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.BOOLEAN)), false));
    map.put(CommonSpatialFunction.ST_INTERSECTS.getKey(), new SDORelateFunction(List.of("OVERLAPBDYDISJOINT", "OVERLAPBDYINTERSECT"), typeRegistry));
    map.put(CommonSpatialFunction.ST_CONTAINS.getKey(), new SDORelateFunction(List.of("CONTAINS"), typeRegistry));
    map.put(CommonSpatialFunction.ST_DISJOINT.getKey(), new SDORelateFunction(List.of("DISJOINT"), typeRegistry));
    map.put(CommonSpatialFunction.ST_RELATE.getKey(), new STRelateFunction(typeRegistry));
    map.put(CommonSpatialFunction.ST_TOUCHES.getKey(), new SDORelateFunction(List.of("TOUCH"), typeRegistry));
    map.put(CommonSpatialFunction.ST_WITHIN.getKey(), new SDORelateFunction(List.of("COVERS", "CONTAINS"), typeRegistry));
    map.put(CommonSpatialFunction.ST_EQUALS.getKey(), new SDORelateFunction(List.of("EQUAL"), typeRegistry));
    map.put(CommonSpatialFunction.ST_DISTANCE.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_DISTANCE", true, StandardArgumentsValidators.exactly(2), StandardFunctionReturnTypeResolvers.invariant(typeRegistry.resolve(StandardBasicTypes.DOUBLE))));
    map.put(CommonSpatialFunction.ST_BUFFER.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_BUFFER", true, StandardArgumentsValidators.exactly(2), StandardFunctionReturnTypeResolvers.useFirstNonNull()));
    map.put(CommonSpatialFunction.ST_CONVEXHULL.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_CONVEXHULL", true, StandardArgumentsValidators.exactly(1), StandardFunctionReturnTypeResolvers.useFirstNonNull()));
    map.put(CommonSpatialFunction.ST_DIFFERENCE.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_DIFFERENCE", true, StandardArgumentsValidators.exactly(2), StandardFunctionReturnTypeResolvers.useFirstNonNull()));
    map.put(CommonSpatialFunction.ST_INTERSECTION.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_INTERSECTION", true, StandardArgumentsValidators.exactly(2), StandardFunctionReturnTypeResolvers.useFirstNonNull()));
    map.put(CommonSpatialFunction.ST_SYMDIFFERENCE.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_XOR", true, StandardArgumentsValidators.exactly(2), StandardFunctionReturnTypeResolvers.useFirstNonNull()));
    map.put(CommonSpatialFunction.ST_UNION.getKey(), new NamedSqmFunctionDescriptor("SDO_GEOM.SDO_UNION", true, StandardArgumentsValidators.exactly(2), StandardFunctionReturnTypeResolvers.useFirstNonNull()));
}
Also used : NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor)

Aggregations

NamedSqmFunctionDescriptor (org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor)5 SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)3 SqmTypedNode (org.hibernate.query.sqm.tree.SqmTypedNode)2 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 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 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)1