[JIRA] (HHH-15531) Use dense_rank instead of row_number when query uses dinstinct
by Christian Beikov (JIRA)
Christian Beikov ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNWQ2YjVmMzQ2... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-15531?atlOrigin=eyJpIjoiNWQ2Yj... ) HHH-15531 ( https://hibernate.atlassian.net/browse/HHH-15531?atlOrigin=eyJpIjoiNWQ2Yj... ) Use dense_rank instead of row_number when query uses dinstinct ( https://hibernate.atlassian.net/browse/HHH-15531?atlOrigin=eyJpIjoiNWQ2Yj... )
Change By: Christian Beikov ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
We have some code that adds {{row_number}} expressions like in the table based insert handler, which should rather use {{dense_rank}} or wrap a query in certain circumstances. See [https://hibernate.atlassian.net/browse/HHH-15517|https://hibernate.atlass...] for an example.
Paraphrasing:
If I rewrite the HQL to this (don’t select {{id}}, use {{distinct}} instead of {{group by}}):
{code:sql}insert into TabData (col1, col2, col3)
select distinct col1, col2, col3
from TabDataStaging{code}
Hibernate 6 does the following:
{code:sql}insert into HTE_tab_data(col1, col2, col3, rn_)
(select distinct p2_0.col1,
p2_0.col2,
p2_0.col3,
row_number() over () -- adding this column effectively disables the `distinct`
from tab_data_staging p2_0);{code}
Followed by the {{updates}} on the HTE table you were suspecting earlier:
{code:sql}update HTE_tab_data set id=? where rn_=?
update HTE_tab_data set id=? where rn_=?
...{code}
Followed by
{noformat}insert into tab_data(col1,col2,col3,id)
select p1_0.col1,p1_0.col2,p1_0.col3,p1_0.id
from HTE_tab_data p1_0{noformat}
The final {{insert}} into {{tab_data}} can then fail due to unique constraint violation (I have a composite unique constraint on {{col1, col2, col3}}), because even though I’m using {{distinct}}, the {{row_number()}} clause added by Hibernate effectively disabled it…
( https://hibernate.atlassian.net/browse/HHH-15531#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15531#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100207- sha1:866f7d8 )