use of org.hibernate.query.sqm.tree.predicate.SqmPredicate in project hibernate-orm by hibernate.
the class InverseDistributionWindowEmulation method generateSqmOrderedSetAggregateFunctionExpression.
@Override
public <T> SelfRenderingSqmOrderedSetAggregateFunction<T> generateSqmOrderedSetAggregateFunctionExpression(List<? extends SqmTypedNode<?>> arguments, SqmPredicate filter, SqmOrderByClause withinGroupClause, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
return new SelfRenderingInverseDistributionFunction<>(arguments, filter, withinGroupClause, impliedResultType, queryEngine) {
@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
final Clause currentClause = walker.getCurrentClauseStack().getCurrent();
if (currentClause == Clause.OVER) {
return super.convertToSqlAst(walker);
} else if (currentClause != Clause.SELECT) {
throw new IllegalArgumentException("Can't emulate [" + getName() + "] in clause " + currentClause + ". Only the SELECT clause is supported!");
}
final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
ArgumentsValidator argumentsValidator = getArgumentsValidator();
if (argumentsValidator != null) {
argumentsValidator.validateSqlTypes(arguments, getFunctionName());
}
List<SortSpecification> withinGroup;
if (this.getWithinGroup() == null) {
withinGroup = Collections.emptyList();
} else {
walker.getCurrentClauseStack().push(Clause.ORDER);
try {
final List<SqmSortSpecification> sortSpecifications = this.getWithinGroup().getSortSpecifications();
withinGroup = new ArrayList<>(sortSpecifications.size());
for (SqmSortSpecification sortSpecification : sortSpecifications) {
final SortSpecification specification = (SortSpecification) walker.visitSortSpecification(sortSpecification);
if (specification != null) {
withinGroup.add(specification);
}
}
} finally {
walker.getCurrentClauseStack().pop();
}
}
final SelfRenderingFunctionSqlAstExpression function = new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), arguments, getFilter() == null ? null : (Predicate) getFilter().accept(walker), withinGroup, resultType, getMappingModelExpressible(walker, resultType));
final Over<Object> windowFunction = new Over<>(function, new ArrayList<>(), Collections.emptyList());
walker.registerQueryTransformer(new AggregateWindowEmulationQueryTransformer(windowFunction, withinGroup, null));
return windowFunction;
}
};
}
use of org.hibernate.query.sqm.tree.predicate.SqmPredicate in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitNestedTopLevelPredicate.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Predicates
@Override
public Predicate visitNestedTopLevelPredicate(SqmPredicate predicate) {
final Map<SqmPath<?>, Set<String>> originalConjunctTableGroupTreatUsages;
if (conjunctTreatUsages.isEmpty()) {
originalConjunctTableGroupTreatUsages = null;
} else {
originalConjunctTableGroupTreatUsages = new IdentityHashMap<>(conjunctTreatUsages);
}
conjunctTreatUsages.clear();
final Predicate result = (Predicate) predicate.accept(this);
final Predicate finalPredicate = SqlAstTreeHelper.combinePredicates(result, consumeConjunctTreatTypeRestrictions());
if (originalConjunctTableGroupTreatUsages != null) {
conjunctTreatUsages.putAll(originalConjunctTableGroupTreatUsages);
}
return finalPredicate;
}
use of org.hibernate.query.sqm.tree.predicate.SqmPredicate in project hibernate-orm by hibernate.
the class HypotheticalSetWindowEmulation method generateSqmOrderedSetAggregateFunctionExpression.
@Override
public <T> SelfRenderingSqmOrderedSetAggregateFunction<T> generateSqmOrderedSetAggregateFunctionExpression(List<? extends SqmTypedNode<?>> arguments, SqmPredicate filter, SqmOrderByClause withinGroupClause, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
return new SelfRenderingSqmOrderedSetAggregateFunction<>(this, this, arguments, filter, withinGroupClause, impliedResultType, getArgumentsValidator(), getReturnTypeResolver(), queryEngine.getCriteriaBuilder(), getName()) {
@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
final Clause currentClause = walker.getCurrentClauseStack().getCurrent();
if (currentClause == Clause.OVER) {
return super.convertToSqlAst(walker);
} else if (currentClause != Clause.SELECT) {
throw new IllegalArgumentException("Can't emulate [" + getName() + "] in clause " + currentClause + ". Only the SELECT clause is supported!");
}
final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
ArgumentsValidator argumentsValidator = getArgumentsValidator();
if (argumentsValidator != null) {
argumentsValidator.validateSqlTypes(arguments, getFunctionName());
}
List<SortSpecification> withinGroup;
if (this.getWithinGroup() == null) {
withinGroup = Collections.emptyList();
} else {
walker.getCurrentClauseStack().push(Clause.ORDER);
try {
final List<SqmSortSpecification> sortSpecifications = this.getWithinGroup().getSortSpecifications();
withinGroup = new ArrayList<>(sortSpecifications.size());
for (SqmSortSpecification sortSpecification : sortSpecifications) {
final SortSpecification specification = (SortSpecification) walker.visitSortSpecification(sortSpecification);
if (specification != null) {
withinGroup.add(specification);
}
}
} finally {
walker.getCurrentClauseStack().pop();
}
}
final SelfRenderingFunctionSqlAstExpression function = new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), Collections.emptyList(), getFilter() == null ? null : (Predicate) getFilter().accept(walker), Collections.emptyList(), resultType, getMappingModelExpressible(walker, resultType));
final Over<Object> windowFunction = new Over<>(function, new ArrayList<>(), withinGroup);
walker.registerQueryTransformer(new AggregateWindowEmulationQueryTransformer(windowFunction, withinGroup, arguments));
return windowFunction;
}
};
}
use of org.hibernate.query.sqm.tree.predicate.SqmPredicate in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitAnyFunction.
@Override
public SqmExpression<?> visitAnyFunction(HqlParser.AnyFunctionContext ctx) {
final SqmPredicate filterExpression = getFilterExpression(ctx);
final ParseTree argumentChild = ctx.getChild(2);
if (argumentChild instanceof HqlParser.SubqueryContext) {
final SqmSubQuery<?> subquery = (SqmSubQuery<?>) argumentChild.accept(this);
return new SqmAny<>(subquery, creationContext.getNodeBuilder());
} else if (argumentChild instanceof HqlParser.PredicateContext) {
if (getCreationOptions().useStrictJpaCompliance()) {
throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.FUNCTION_CALL);
}
final SqmExpression<?> argument = (SqmExpression<?>) argumentChild.accept(this);
return applyOverClause(ctx, getFunctionDescriptor("any").generateAggregateSqmExpression(singletonList(argument), filterExpression, resolveExpressibleTypeBasic(Boolean.class), creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration()));
} else {
if (getCreationOptions().useStrictJpaCompliance()) {
throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.HQL_COLLECTION_FUNCTION);
}
return new SqmAny<>(createCollectionReferenceSubQuery((HqlParser.SimplePathContext) ctx.getChild(3), (TerminalNode) ctx.getChild(1)), creationContext.getNodeBuilder());
}
}
use of org.hibernate.query.sqm.tree.predicate.SqmPredicate 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);
}
Aggregations