HHH-17391 HHH-17392 Skip checking existing selection for OVER clause

This commit is contained in:
Marco Belladelli 2023-11-07 12:17:29 +01:00
parent 4e73ffca20
commit ef4609baad
2 changed files with 44 additions and 1 deletions

View File

@ -2338,7 +2338,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
sqmPosition = aliasedNodeOrdinal - 1;
path = aliasedNodeRef.getNavigablePath();
}
else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA ) {
else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA && currentClauseStack.getCurrent() != Clause.OVER ) {
// In JPA Criteria we could be using the same expression object for the group/order by and select item
// We try to find the select item position for this expression here which is not necessarily just an optimization.
// This is vital to enable the support for parameters in these expressions.

View File

@ -21,6 +21,7 @@ import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.domain.gambit.EntityOfBasics;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
@ -134,6 +135,48 @@ public class CriteriaWindowFunctionTest {
);
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-17391" )
public void testRowNumberMultiSelectGroupBy(final SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final HibernateCriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Tuple> cr = cb.createQuery( Tuple.class );
final Root<EntityOfBasics> root = cr.from( EntityOfBasics.class );
final JpaWindow window = cb.createWindow();
window.partitionBy( root.get( "id" ) ).orderBy( cb.asc( root.get( "id" ) ) );
final JpaExpression<Long> rowNumber = cb.rowNumber( window );
cr.multiselect( root.get( "id" ), rowNumber ).groupBy( root.get( "id" ) );
final List<Tuple> resultList = session.createQuery( cr ).getResultList();
assertEquals( 5, resultList.size() );
resultList.forEach( tuple -> assertEquals( 1L, tuple.get( 1, Long.class ) ) );
}
);
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-17392" )
public void testRowNumberMultiSelect(final SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final HibernateCriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Tuple> cr = cb.createQuery( Tuple.class );
final Root<EntityOfBasics> root = cr.from( EntityOfBasics.class );
final JpaWindow window = cb.createWindow();
window.partitionBy( root.get( "id" ) ).orderBy( cb.asc( root.get( "id" ) ) );
final JpaExpression<Long> rowNumber = cb.rowNumber( window );
cr.multiselect( root.get( "id" ), rowNumber );
final List<Tuple> resultList = session.createQuery( cr ).getResultList();
assertEquals( 5, resultList.size() );
resultList.forEach( tuple -> assertEquals( 1L, tuple.get( 1, Long.class ) ) );
}
);
}
@Test
public void testFirstValue(SessionFactoryScope scope) {
scope.inTransaction(