NIFI-6244: Provide default ORDER BY clause in MSSQL adapters

This closes #3454.

Signed-off-by: Bryan Bende <bbende@apache.org>
This commit is contained in:
Matthew Burgess 2019-04-25 15:45:26 -04:00 committed by Bryan Bende
parent 7d02ab1e2f
commit 7fc49c2cb8
No known key found for this signature in database
GPG Key ID: A0DDA9ED50711C39
4 changed files with 21 additions and 5 deletions

View File

@ -65,9 +65,14 @@ public class MSSQL2008DatabaseAdapter extends MSSQLDatabaseAdapter {
query.append(columnNames);
}
if (limit != null && offset != null && orderByClause != null && !useColumnForPartitioning) {
if (limit != null && offset != null && !useColumnForPartitioning) {
query.append(", ROW_NUMBER() OVER(ORDER BY ");
query.append(orderByClause);
if (orderByClause != null && !orderByClause.isEmpty()) {
query.append(orderByClause);
} else {
// Add a default ORDER BY clause using the newid() function
query.append("newid()");
}
query.append(" asc) rnum");
}
query.append(" FROM ");

View File

@ -83,7 +83,8 @@ public class MSSQLDatabaseAdapter implements DatabaseAdapter {
if (StringUtils.isEmpty(columnForPartitioning)) {
if (offset != null && limit != null && limit > 0) {
if (StringUtils.isEmpty(orderByClause)) {
throw new IllegalArgumentException("Order by clause cannot be null or empty when using row paging");
// Add a default ORDER BY clause using the newid() function
query.append(" ORDER BY newid()");
}
query.append(" OFFSET ");

View File

@ -47,6 +47,14 @@ public class TestMSSQL2008DatabaseAdapter {
db.getSelectStatement("", "some(set),of(columns),that,might,contain,methods,a.*", "", "", null, null);
}
@Test
public void testPagingNoOrderBy() throws Exception {
String sql1 = db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*","",null,10L,0L);
String expected1 = "SELECT * FROM (SELECT TOP 10 some(set),of(columns),that,might,contain,methods,a.*, ROW_NUMBER() OVER(ORDER BY newid() asc) rnum "
+ "FROM database.tablename) A WHERE rnum > 0 AND rnum <= 10";
Assert.assertEquals(sql1,expected1);
}
@Test
public void testTOPQuery() throws Exception {
String sql = db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*", "", "", 100L, null);

View File

@ -47,9 +47,11 @@ public class TestMSSQLDatabaseAdapter {
db.getSelectStatement("", "some(set),of(columns),that,might,contain,methods,a.*","","",null,null);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testPagingNoOrderBy() throws Exception {
db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*","","",10L,0L);
String sql1 = db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*","","",10L,0L);
String expected1 = "SELECT some(set),of(columns),that,might,contain,methods,a.* FROM database.tablename ORDER BY newid() OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY";
Assert.assertEquals(sql1,expected1);
}
@Test