From 8e2598bca8daec0f38be5b5b9589e3c7c48aed3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Tue, 19 Aug 2025 12:42:26 +0200 Subject: [PATCH 1/2] HHH-19719 Test case - when SelfRenderingSqmWindowFunction has no arguments, appendHqlString throws IndexOutOfBoundsException --- ...deringSqmFunctionWithoutArgumentsTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/SelfRenderingSqmFunctionWithoutArgumentsTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/SelfRenderingSqmFunctionWithoutArgumentsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/SelfRenderingSqmFunctionWithoutArgumentsTest.java new file mode 100644 index 000000000000..f3ddd59178b1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/SelfRenderingSqmFunctionWithoutArgumentsTest.java @@ -0,0 +1,64 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.query.sqm; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportPartitionBy; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +@DomainModel( + annotatedClasses = SelfRenderingSqmFunctionWithoutArgumentsTest.Dummy.class +) +@SessionFactory +@JiraKey("HHH-19719") +@RequiresDialectFeature(feature = SupportPartitionBy.class) +public class SelfRenderingSqmFunctionWithoutArgumentsTest { + + @BeforeAll + static void init(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.persist( new Dummy(1, "John Doe") ); + session.persist( new Dummy(2, "Dave Default") ); + } ); + } + + @Test + void test(SessionFactoryScope scope) { + scope.inSession( session -> { + session.createQuery( """ + with tmp as ( + select id id, name name, row_number() over (order by name) pos + from Dummy + ) + select id, name, pos from tmp + """ ).getResultList(); + } ); + + } + + @Entity(name = "Dummy") + static class Dummy { + @Id + private Integer id; + + private String name; + + private Dummy() { + // for use by Hibernate + } + + public Dummy(Integer id, String name) { + this.id = id; + this.name = name; + } + } +} From 77d978f5d6d6d763fceddc4c6ec13f7209b47b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Tue, 19 Aug 2025 12:46:47 +0200 Subject: [PATCH 2/2] HHH-19719 Prevent IndexOutOfBoundsException when no arguments --- .../query/sqm/function/SelfRenderingSqmWindowFunction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/function/SelfRenderingSqmWindowFunction.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/function/SelfRenderingSqmWindowFunction.java index a9b474046fdc..29caa8a2ae88 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/function/SelfRenderingSqmWindowFunction.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/function/SelfRenderingSqmWindowFunction.java @@ -121,7 +121,7 @@ public void appendHqlString(StringBuilder hql, SqmRenderContext context) { hql.append( getFunctionName() ); hql.append( '(' ); int i = 1; - if ( arguments.get( 0 ) instanceof SqmDistinct ) { + if ( !arguments.isEmpty() && arguments.get( 0 ) instanceof SqmDistinct ) { arguments.get( 0 ).appendHqlString( hql, context ); if ( arguments.size() > 1 ) { hql.append( ' ' );