NIFI-5855: Remove unnecessary ORDER BY clause in GenerateTableFetch when Partition Size is zero

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #3191.
This commit is contained in:
Matthew Burgess 2018-11-29 17:46:24 -05:00 committed by Pierre Villard
parent 72662f0b2c
commit 8a12307d1a
No known key found for this signature in database
GPG Key ID: BEE1599F0726E9CD
2 changed files with 6 additions and 4 deletions

View File

@ -315,8 +315,8 @@ public class GenerateTableFetch extends AbstractDatabaseFetchProcessor {
String columnsClause = null;
List<String> maxValueSelectColumns = new ArrayList<>(numMaxValueColumns + 1);
// replace unnecessary row count with -1 stub value when paging is used
if (useColumnValsForPaging) {
// replace unnecessary row count with -1 stub value when column values for paging is used, or when partition size is zero.
if (useColumnValsForPaging || partitionSize == 0) {
maxValueSelectColumns.add("-1");
} else {
maxValueSelectColumns.add("COUNT(*)");
@ -496,8 +496,10 @@ public class GenerateTableFetch extends AbstractDatabaseFetchProcessor {
//Update WHERE list to include new right hand boundaries
whereClause = maxValueClauses.isEmpty() ? "1=1" : StringUtils.join(maxValueClauses, " AND ");
Long offset = partitionSize == 0 ? null : i * partitionSize + (useColumnValsForPaging ? minValueForPartitioning : 0);
// Don't use an ORDER BY clause if there's only one partition
final String orderByClause = partitionSize == 0 ? null : maxColumnNames;
final String query = dbAdapter.getSelectStatement(tableName, columnNames, whereClause, maxColumnNames, limit, offset, columnForPartitioning);
final String query = dbAdapter.getSelectStatement(tableName, columnNames, whereClause, orderByClause, limit, offset, columnForPartitioning);
FlowFile sqlFlowFile = (fileToProcess == null) ? session.create() : session.create(fileToProcess);
sqlFlowFile = session.write(sqlFlowFile, out -> out.write(query.getBytes()));
Map<String,String> attributesToAdd = new HashMap<>();

View File

@ -541,7 +541,7 @@ public class TestGenerateTableFetch {
runner.run();
runner.assertAllFlowFilesTransferred(GenerateTableFetch.REL_SUCCESS, 1);
MockFlowFile flowFile = runner.getFlowFilesForRelationship(GenerateTableFetch.REL_SUCCESS).get(0);
flowFile.assertContentEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE ID <= 2 ORDER BY ID");
flowFile.assertContentEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE ID <= 2");
flowFile.assertAttributeExists("generatetablefetch.limit");
flowFile.assertAttributeEquals("generatetablefetch.limit", null);
runner.clearTransferState();