From 1b3209f38a7234864fa7aeaaf353f7230be118c5 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 5 Apr 2023 12:11:22 +0200 Subject: [PATCH] HHH-16347 Add test for issue --- .../test/query/hql/WindowFunctionTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/WindowFunctionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/WindowFunctionTest.java index 6509601277..d579af3ed7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/WindowFunctionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/WindowFunctionTest.java @@ -9,10 +9,14 @@ package org.hibernate.orm.test.query.hql; import java.util.Date; import java.util.List; +import org.hibernate.internal.util.ExceptionHelper; +import org.hibernate.query.sqm.ParsingException; + 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; @@ -24,7 +28,10 @@ import org.junit.jupiter.api.Test; import jakarta.persistence.TypedQuery; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Christian Beikov @@ -165,4 +172,43 @@ public class WindowFunctionTest { } ); } + + @Test + @RequiresDialectFeature( feature = DialectFeatureChecks.SupportsWindowFunctions.class ) + @Jira( "https://hibernate.atlassian.net/browse/HHH-16347" ) + public void testOrderByAndAlias(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + final TypedQuery q = session.createQuery( + "select id from (select id as id, dense_rank() over (order by theInt, id ASC) as ranking" + + " from EntityOfBasics) entity_rank where ranking = :rank", + Integer.class + ).setParameter( "rank", 5 ); + assertEquals( 4, q.getSingleResult() ); + } + ); + } + + @Test + @RequiresDialectFeature( feature = DialectFeatureChecks.SupportsWindowFunctions.class ) + @Jira( "https://hibernate.atlassian.net/browse/HHH-16347" ) + public void testOrderByAndPositional(SessionFactoryScope scope) { + scope.inTransaction( session -> { + try { + session.createQuery( + "select id from (select id, dense_rank() over (order by theInt, 1 ASC) as ranking from EntityOfBasics) ", + Integer.class + ); + fail( "Order-by positional '1' should not be allowed in OVER clause" ); + } + catch (Exception e) { + final Throwable rootCause = ExceptionHelper.getRootCause( e ); + assertInstanceOf( ParsingException.class, rootCause ); + assertEquals( + "Position based order-by is not allowed in OVER or WITHIN GROUP clauses", + rootCause.getMessage() + ); + } + } ); + } }