Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SQL Server error code 145 for select distinct with result limited queries. #964

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kaizimmerm
Copy link

Fixes #963 with the proposed solution to step out.

I did not create a dedicated test as the current test suite finds this one already when run against SQL Server, e.g.:

org.eclipse.persistence.testing.tests.jpa.jpql.JUnitJPQLSimpleTestSuite.selectPhoneUsingALLTest  Time elapsed: 0.023 s  <<< ERROR!
jakarta.persistence.PersistenceException: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 3.0.0.v202011171834): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Error Code: 145
Call: SELECT DISTINCT t1.EMP_ID, t2.EMP_ID, t1.F_NAME, t1.GENDER, t1.L_NAME, t1.PAY_SCALE, t1.ROOM_NUM, t2.SALARY, t1.STATUS, t1.VERSION, t1.START_TIME, t1.END_TIME, t1.START_OVERTIME, t1.END_OVERTIME, t1.FORMER_COMPANY, t1.FORMER_END_DATE, t1.FORMER_START_DATE, t1.FORMER_COMPANY_ADDRESS_ID, t1.END_DATE, t1.START_DATE, t1.COMPANYADDRESS_ADDRESS_ID, t1.ADDR_ID, t1.DEPT_ID, t1.MANAGER_EMP_ID, t1.HUGE_PROJ_ID, t0.ID, t0.NAME, t0.DEPT_HEAD FROM CMP3_EMPLOYEE t1 LEFT OUTER JOIN CMP3_DEPT t0 ON (t0.ID = t1.DEPT_ID), CMP3_PHONENUMBER t3, CMP3_SALARY t2 WHERE (((t3.NUMB = ALL(SELECT MIN(t4.NUMB) FROM CMP3_PHONENUMBER t4)) AND (t2.EMP_ID = t1.EMP_ID)) AND (t3.OWNER_ID = t1.EMP_ID)) ORDER BY ROW_NUMBER() OVER (ORDER BY (SELECT null)) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY
	bind => [2 parameters bound]
Query: ReadAllQuery(referenceClass=Employee sql="SELECT DISTINCT t1.EMP_ID, t2.EMP_ID, t1.F_NAME, t1.GENDER, t1.L_NAME, t1.PAY_SCALE, t1.ROOM_NUM, t2.SALARY, t1.STATUS, t1.VERSION, t1.START_TIME, t1.END_TIME, t1.START_OVERTIME, t1.END_OVERTIME, t1.FORMER_COMPANY, t1.FORMER_END_DATE, t1.FORMER_START_DATE, t1.FORMER_COMPANY_ADDRESS_ID, t1.END_DATE, t1.START_DATE, t1.COMPANYADDRESS_ADDRESS_ID, t1.ADDR_ID, t1.DEPT_ID, t1.MANAGER_EMP_ID, t1.HUGE_PROJ_ID, t0.ID, t0.NAME, t0.DEPT_HEAD FROM CMP3_EMPLOYEE t1 LEFT OUTER JOIN CMP3_DEPT t0 ON (t0.ID = t1.DEPT_ID), CMP3_PHONENUMBER t3, CMP3_SALARY t2 WHERE (((t3.NUMB = ALL(SELECT MIN(t4.NUMB) FROM CMP3_PHONENUMBER t4)) AND (t2.EMP_ID = t1.EMP_ID)) AND (t3.OWNER_ID = t1.EMP_ID)) ORDER BY ROW_NUMBER() OVER (ORDER BY (SELECT null)) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY")
	at org.eclipse.persistence.testing.tests.jpa.jpql.JUnitJPQLSimpleTestSuite.selectPhoneUsingALLTest(JUnitJPQLSimpleTestSuite.java:1946)
Caused by: org.eclipse.persistence.exceptions.DatabaseException: 

Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Error Code: 145
Call: SELECT DISTINCT t1.EMP_ID, t2.EMP_ID, t1.F_NAME, t1.GENDER, t1.L_NAME, t1.PAY_SCALE, t1.ROOM_NUM, t2.SALARY, t1.STATUS, t1.VERSION, t1.START_TIME, t1.END_TIME, t1.START_OVERTIME, t1.END_OVERTIME, t1.FORMER_COMPANY, t1.FORMER_END_DATE, t1.FORMER_START_DATE, t1.FORMER_COMPANY_ADDRESS_ID, t1.END_DATE, t1.START_DATE, t1.COMPANYADDRESS_ADDRESS_ID, t1.ADDR_ID, t1.DEPT_ID, t1.MANAGER_EMP_ID, t1.HUGE_PROJ_ID, t0.ID, t0.NAME, t0.DEPT_HEAD FROM CMP3_EMPLOYEE t1 LEFT OUTER JOIN CMP3_DEPT t0 ON (t0.ID = t1.DEPT_ID), CMP3_PHONENUMBER t3, CMP3_SALARY t2 WHERE (((t3.NUMB = ALL(SELECT MIN(t4.NUMB) FROM CMP3_PHONENUMBER t4)) AND (t2.EMP_ID = t1.EMP_ID)) AND (t3.OWNER_ID = t1.EMP_ID)) ORDER BY ROW_NUMBER() OVER (ORDER BY (SELECT null)) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY
	bind => [2 parameters bound]
Query: ReadAllQuery(referenceClass=Employee sql="SELECT DISTINCT t1.EMP_ID, t2.EMP_ID, t1.F_NAME, t1.GENDER, t1.L_NAME, t1.PAY_SCALE, t1.ROOM_NUM, t2.SALARY, t1.STATUS, t1.VERSION, t1.START_TIME, t1.END_TIME, t1.START_OVERTIME, t1.END_OVERTIME, t1.FORMER_COMPANY, t1.FORMER_END_DATE, t1.FORMER_START_DATE, t1.FORMER_COMPANY_ADDRESS_ID, t1.END_DATE, t1.START_DATE, t1.COMPANYADDRESS_ADDRESS_ID, t1.ADDR_ID, t1.DEPT_ID, t1.MANAGER_EMP_ID, t1.HUGE_PROJ_ID, t0.ID, t0.NAME, t0.DEPT_HEAD FROM CMP3_EMPLOYEE t1 LEFT OUTER JOIN CMP3_DEPT t0 ON (t0.ID = t1.DEPT_ID), CMP3_PHONENUMBER t3, CMP3_SALARY t2 WHERE (((t3.NUMB = ALL(SELECT MIN(t4.NUMB) FROM CMP3_PHONENUMBER t4)) AND (t2.EMP_ID = t1.EMP_ID)) AND (t3.OWNER_ID = t1.EMP_ID)) ORDER BY ROW_NUMBER() OVER (ORDER BY (SELECT null)) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY")
	at org.eclipse.persistence.testing.tests.jpa.jpql.JUnitJPQLSimpleTestSuite.selectPhoneUsingALLTest(JUnitJPQLSimpleTestSuite.java:1946)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
	at org.eclipse.persistence.testing.tests.jpa.jpql.JUnitJPQLSimpleTestSuite.selectPhoneUsingALLTest(JUnitJPQLSimpleTestSuite.java:1946)

Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>
List<Expression> orderBy = statement.getOrderByExpressions();
if (orderBy.isEmpty()) {
orderBy.add(statement.getBuilder().literal("ROW_NUMBER() OVER (ORDER BY (SELECT null))"));
super.printSQLSelectStatement(call, printer, statement);
return;
}
Copy link
Contributor

@rfelcman rfelcman Jan 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I'm not specialist for MS SQL, but could You please provide some example and simple SQL query with ROW_NUMBER() OVER
As I understand previous change in #300 did transformation like
origin query SELECT ... FROM table
into SELECT ... FROM table ORDER BY ROW_NUMBER() OVER (ORDER BY (SELECT null)) OFFSET ? ROWS
or SELECT ... FROM table ORDER BY ROW_NUMBER() OVER (ORDER BY (SELECT null)) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY
in SQL Server 2012 and higher.
Your change will keep query without order SELECT ... FROM table untouched and
query like SELECT ... FROM table ORDER BY column1 will be transformed
into SELECT ... FROM table ORDER BY column1 ? OFFSET ? ROWS or
SELECT ... FROM table ORDER BY column1 ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY
and fix #300 was workaround for microsoft/mssql-jdbc#176
Is it this change safe for issue with JDBC Statement#setMaxRows() mentioned in #300?

@schabdo schabdo mentioned this pull request Jan 28, 2021
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQL Server: Exception when DISTINCT is used with result limited queries
2 participants