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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -813,11 +813,11 @@ public void printSQLSelectStatement(DatabaseCall call, ExpressionSQLPrinter prin
return;
}

// OFFSET + FETCH NEXT requires ORDER BY, so add an ordering if there are none
// this SQL will satisfy the query parser without actually changing the ordering of the rows
// OFFSET + FETCH NEXT requires ORDER BY
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?


// decide exact syntax to use, depending on whether a limit is specified (could just have an offset)
Expand Down