caishunfeng commented on code in PR #12059:
URL: https://github.com/apache/dolphinscheduler/pull/12059#discussion_r975486304


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java:
##########
@@ -261,13 +261,8 @@ private String resultProcess(ResultSet resultSet) throws 
Exception {
                 resultJSONArray.add(mapOfColValues);
                 rowCount++;
             }
-            int displayRows = sqlParameters.getDisplayRows() > 0 ? 
sqlParameters.getDisplayRows() : TaskConstants.DEFAULT_DISPLAY_ROWS;
-            displayRows = Math.min(displayRows, rowCount);
-            logger.info("display sql result {} rows as follows:", displayRows);
-            for (int i = 0; i < displayRows; i++) {
-                String row = JSONUtils.toJsonString(resultJSONArray.get(i));
-                logger.info("row {} : {}", i + 1, row);
-            }
+            // generate query results
+            generateRow(resultJSONArray, md, num, rowCount);

Review Comment:
   It will not take affect when resultSet == null.



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java:
##########
@@ -261,13 +261,8 @@ private String resultProcess(ResultSet resultSet) throws 
Exception {
                 resultJSONArray.add(mapOfColValues);
                 rowCount++;
             }
-            int displayRows = sqlParameters.getDisplayRows() > 0 ? 
sqlParameters.getDisplayRows() : TaskConstants.DEFAULT_DISPLAY_ROWS;
-            displayRows = Math.min(displayRows, rowCount);
-            logger.info("display sql result {} rows as follows:", displayRows);
-            for (int i = 0; i < displayRows; i++) {
-                String row = JSONUtils.toJsonString(resultJSONArray.get(i));
-                logger.info("row {} : {}", i + 1, row);
-            }

Review Comment:
   This is the normal logic, we should recover it.



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java:
##########
@@ -261,13 +261,8 @@ private String resultProcess(ResultSet resultSet) throws 
Exception {
                 resultJSONArray.add(mapOfColValues);
                 rowCount++;
             }
-            int displayRows = sqlParameters.getDisplayRows() > 0 ? 
sqlParameters.getDisplayRows() : TaskConstants.DEFAULT_DISPLAY_ROWS;
-            displayRows = Math.min(displayRows, rowCount);
-            logger.info("display sql result {} rows as follows:", displayRows);
-            for (int i = 0; i < displayRows; i++) {
-                String row = JSONUtils.toJsonString(resultJSONArray.get(i));
-                logger.info("row {} : {}", i + 1, row);
-            }
+            // generate query results
+            generateRow(resultJSONArray, md, num, rowCount);
         }
         String result = JSONUtils.toJsonString(resultJSONArray);
         if (sqlParameters.getSendEmail() == null || 
sqlParameters.getSendEmail()) {

Review Comment:
   Avoid changing the return result.
   ```suggestion
           if (sqlParameters.getSendEmail() == null || 
sqlParameters.getSendEmail()) {
               String attachmentContent = resultJSONArray.isEmpty()? 
JSONUtils.toJsonString(generateEmptyRow()):result;
               sendAttachment(sqlParameters.getGroupId(), 
StringUtils.isNotEmpty(sqlParameters.getTitle())
                       ? sqlParameters.getTitle()
                       : taskExecutionContext.getTaskName() + " query result 
sets", attachmentContent);
   ```



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java:
##########
@@ -279,6 +274,30 @@ private String resultProcess(ResultSet resultSet) throws 
Exception {
         return result;
     }
 
+    /**
+     * Output Query Results as JsonString
+     */
+
+    private void generateRow(ArrayNode resultJSONArray, ResultSetMetaData md, 
int metaColumnsNum, int rowCount) throws SQLException {
+        if (resultJSONArray.isEmpty()) {
+            logger.info("sql query results is empty");
+            ObjectNode emptyOfColValues = JSONUtils.createObjectNode();
+            for (int i = 1; i <= metaColumnsNum; i++) {
+                emptyOfColValues.set(md.getColumnLabel(i), 
JSONUtils.toJsonNode(""));
+            }
+            resultJSONArray.add(emptyOfColValues);
+        } else {
+            int displayRows = sqlParameters.getDisplayRows() > 0 ? 
sqlParameters.getDisplayRows() : TaskConstants.DEFAULT_DISPLAY_ROWS;
+            displayRows = Math.min(displayRows, rowCount);
+            logger.info("display sql result {} rows as follows:", displayRows);
+            for (int i = 0; i < displayRows; i++) {
+                String row = JSONUtils.toJsonString(resultJSONArray.get(i));
+                logger.info("row {} : {}", i + 1, row);
+            }
+        }
+    }

Review Comment:
   We should avoid to change the input param in the method.



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java:
##########
@@ -279,6 +274,30 @@ private String resultProcess(ResultSet resultSet) throws 
Exception {
         return result;
     }
 
+    /**
+     * Output Query Results as JsonString
+     */
+
+    private void generateRow(ArrayNode resultJSONArray, ResultSetMetaData md, 
int metaColumnsNum, int rowCount) throws SQLException {
+        if (resultJSONArray.isEmpty()) {
+            logger.info("sql query results is empty");
+            ObjectNode emptyOfColValues = JSONUtils.createObjectNode();
+            for (int i = 1; i <= metaColumnsNum; i++) {
+                emptyOfColValues.set(md.getColumnLabel(i), 
JSONUtils.toJsonNode(""));
+            }
+            resultJSONArray.add(emptyOfColValues);
+        } else {
+            int displayRows = sqlParameters.getDisplayRows() > 0 ? 
sqlParameters.getDisplayRows() : TaskConstants.DEFAULT_DISPLAY_ROWS;
+            displayRows = Math.min(displayRows, rowCount);
+            logger.info("display sql result {} rows as follows:", displayRows);
+            for (int i = 0; i < displayRows; i++) {
+                String row = JSONUtils.toJsonString(resultJSONArray.get(i));
+                logger.info("row {} : {}", i + 1, row);
+            }
+        }
+    }

Review Comment:
   ```suggestion
       private ArrayNode generateEmptyRow(ResultSetMetaData md, int 
metaColumnsNum, int rowCount) throws SQLException {
               ArrayNode resultJSONArray = JSONUtils.createArrayNode();
               ObjectNode emptyOfColValues = JSONUtils.createObjectNode();
               for (int i = 1; i <= metaColumnsNum; i++) {
                   emptyOfColValues.set(md.getColumnLabel(i), 
JSONUtils.toJsonNode(""));
               }
               resultJSONArray.add(emptyOfColValues);
              return resultJSONArray;
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to