Hibernate 6.5.2.Final createCountQuery fails

Hi! I have an issue with Postgresql 15.2 (guess it is not matter) and createCountQuery method from SqmSelectStatement with big origin query and distinct:

        var builder = deps.getCriteriaBuilder();
        var query = b.createTupleQuery();
        var from = query.from(Entity.class);
        var sesJoin = from.join("relation1", JoinType.LEFT);
        var posJoin = sesJoin.join("relation2", JoinType.LEFT);
        var sels = new ArrayList<Selection<?>>();
        //here much selections (> 29) in origin query (from root or joins)
        sels.add(from.get("someFieldForSelect"));
        query.multiselect(sels);
        query.distinct(true);

        query.where(builder.equal(posJoin.get("someField"), "someValue"));
        var countQ = ((SqmSelectStatement<?>) query).createCountQuery();
        var r = getDao().createQuery(query).setMaxResults(100).list();
        var count = getDao().createQuery(countQ).uniqueResult();

this query fails at db level with:

org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL [select count(*) from (select distinct ...fields from ENTITY c1_0 left join (select * from RELATION_1 t where t.rel_type='ENTITY_TYPE') es1_0 on c1_0.id=es1_0.rel_id left join RELATION_2 p1_0 on p1_0.id=es1_0.rel_1 where p1_0.some_field=?) derived1_0(b_,c_,d_,e_,f_,g_,h_,i_,j_,k_,l_,m_,n_,o_,p_,q_,r_,s_,t_,u_,v_,w_,x_,y_,z_,{_,|_,}_,~_)] [ERROR: syntax error at or near "{"

looks like a bug in org.hibernate.query.sqm.tree.select.SqmSelectStatement#createCountQuery
at line: item.alias( Character.toString( ++c ) + ā€˜_ā€™ );

is it a bug or iā€™m doing something wrong?

Hello, that implementation of selection aliasing was unfortunately not accounting for select statements with a long list of items. In the upcoming Hibernate versions 6.5.3 and 6.6 we changed the logic around that (to fix an unrelated bug) and also changed the aliasing to use c1, c2, ā€¦, cn instead, which should work in your case.

Thanks for your answer, will wait 6.5.3