use of org.hibernate.query.sqm.tree.select.SqmSortSpecification 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.select.SqmSortSpecification in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitSortSpecification.
@Override
public SqmSortSpecification visitSortSpecification(HqlParser.SortSpecificationContext ctx) {
final SqmExpression<?> sortExpression = visitSortExpression((HqlParser.SortExpressionContext) ctx.getChild(0));
if (sortExpression == null) {
throw new ParsingException("Could not resolve sort-expression : " + ctx.getChild(0).getText());
}
if (sortExpression instanceof SqmLiteral || sortExpression instanceof SqmParameter) {
HqlLogging.QUERY_LOGGER.debugf("Questionable sorting by constant value : %s", sortExpression);
}
final SortOrder sortOrder;
final NullPrecedence nullPrecedence;
int nextIndex = 1;
if (nextIndex < ctx.getChildCount()) {
ParseTree parseTree = ctx.getChild(nextIndex);
if (parseTree instanceof HqlParser.SortDirectionContext) {
switch(((TerminalNode) parseTree.getChild(0)).getSymbol().getType()) {
case HqlParser.ASC:
sortOrder = SortOrder.ASCENDING;
break;
case HqlParser.DESC:
sortOrder = SortOrder.DESCENDING;
break;
default:
throw new SemanticException("Unrecognized sort ordering: " + parseTree.getText());
}
nextIndex++;
} else {
sortOrder = null;
}
parseTree = ctx.getChild(nextIndex);
if (parseTree instanceof HqlParser.NullsPrecedenceContext) {
switch(((TerminalNode) parseTree.getChild(1)).getSymbol().getType()) {
case HqlParser.FIRST:
nullPrecedence = NullPrecedence.FIRST;
break;
case HqlParser.LAST:
nullPrecedence = NullPrecedence.LAST;
break;
default:
throw new SemanticException("Unrecognized null precedence: " + parseTree.getText());
}
} else {
nullPrecedence = null;
}
} else {
sortOrder = null;
nullPrecedence = null;
}
return new SqmSortSpecification(sortExpression, sortOrder, nullPrecedence);
}
use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitOver.
@Override
public Object visitOver(SqmOver<?> over) {
currentClauseStack.push(Clause.OVER);
final Expression expression = (Expression) over.getExpression().accept(this);
final List<Expression> partitions = new ArrayList<>(over.getPartitions().size());
for (SqmExpression<?> partition : over.getPartitions()) {
partitions.add((Expression) partition.accept(this));
}
final List<SortSpecification> orderList = new ArrayList<>(over.getOrderList().size());
for (SqmSortSpecification sortSpecification : over.getOrderList()) {
orderList.add(visitSortSpecification(sortSpecification));
}
final Over<Object> overExpression = new Over<>(expression, partitions, orderList, over.getMode(), over.getStartKind(), over.getStartExpression() == null ? null : (Expression) over.getStartExpression().accept(this), over.getEndKind(), over.getEndExpression() == null ? null : (Expression) over.getEndExpression().accept(this), over.getExclusion());
currentClauseStack.pop();
return overExpression;
}
use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.
the class SelfRenderingSqmOrderedSetAggregateFunction method convertToSqlAst.
@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
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.withinGroup == null) {
withinGroup = Collections.emptyList();
} else {
walker.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
try {
final List<SqmSortSpecification> sortSpecifications = this.withinGroup.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();
}
}
return new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), arguments, getFilter() == null ? null : walker.visitNestedTopLevelPredicate(getFilter()), withinGroup, resultType, getMappingModelExpressible(walker, resultType));
}
use of org.hibernate.query.sqm.tree.select.SqmSortSpecification in project hibernate-orm by hibernate.
the class SortSpecificationReversalTests method testDefaultSqmSortSpecificationReverse.
@Test
public void testDefaultSqmSortSpecificationReverse() {
SqmExpression sortExpression = mock(SqmExpression.class);
SqmSortSpecification order = new SqmSortSpecification(sortExpression, ASCENDING, FIRST);
assertEquals(sortExpression, order.getSortExpression());
assertEquals(ASCENDING, order.getSortOrder());
assertEquals(FIRST, order.getNullPrecedence());
JpaOrder reversed = order.reverse();
assertEquals(DESCENDING, reversed.getSortOrder());
assertEquals(FIRST, reversed.getNullPrecedence());
assertNotSame("Order.reverse() should create new instance", order, reversed);
}
Aggregations