You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by "featzhang (via GitHub)" <gi...@apache.org> on 2023/04/17 09:46:32 UTC

[GitHub] [inlong] featzhang opened a new pull request, #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

featzhang opened a new pull request, #7869:
URL: https://github.com/apache/inlong/pull/7869

   ### Prepare a Pull Request
   *(Change the title refer to the following example)*
   
   - Title Example: [INLONG-XYZ][Component] Title of the pull request
   
   *(The following *XYZ* should be replaced by the actual [GitHub Issue](https://github.com/apache/inlong/issues) number)*
   
   - Fixes #XYZ
   
   ### Motivation
   
   *Explain here the context, and why you're making that change. What is the problem you're trying to solve?*
   
   ### Modifications
   
   *Describe the modifications you've done.*
   
   ### Verifying this change
   
   *(Please pick either of the following options)*
   
   - [ ] This change is a trivial rework/code cleanup without any test coverage.
   
   - [ ] This change is already covered by existing tests, such as:
     *(please describe tests)*
   
   - [ ] This change added tests and can be verified as follows:
   
     *(example:)*
     - *Added integration tests for end-to-end deployment with large payloads (10MB)*
     - *Extended integration test for recovery after broker failure*
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
     - If a feature is not applicable for documentation, explain why?
     - If a feature is not documented yet in this PR, please create a follow-up issue for adding the documentation
   


-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [inlong] dockerzhang merged pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

Posted by "dockerzhang (via GitHub)" <gi...@apache.org>.
dockerzhang merged PR #7869:
URL: https://github.com/apache/inlong/pull/7869


-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [inlong] featzhang commented on a diff in pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7869:
URL: https://github.com/apache/inlong/pull/7869#discussion_r1168567241


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -372,35 +373,54 @@ public static <E> List<E> read(InputStream is, Class<E> clazz)
 
                 int lastRowNum = sheet.getLastRowNum();
                 List<E> currentResult = new ArrayList<>(lastRowNum);
-
+                List<String> validateResult = new ArrayList<>(lastRowNum);
                 for (int rowNum = 1; rowNum <= lastRowNum; ++rowNum) {
                     XSSFRow row = sheet.getRow(rowNum);
                     if (row == null) {
                         continue;
                     }
+                    // Mark the row only if all cells are not null
+                    Map<Integer, FieldMeta> positionFieldMetaMap = classMeta.positionFieldMetaMap;
+                    boolean rowNotNull = positionFieldMetaMap.keySet()
+                            .stream()
+                            .map(colIndex -> row.getCell(colIndex,
+                                    Row.MissingCellPolicy.RETURN_BLANK_AS_NULL) != null)
+                            .reduce(false, (left, right) -> left || right);
+
+                    // Skip if all columns are null
+                    if (!rowNotNull) {
+                        continue;
+                    }
+
                     E instance = null;
+                    StringBuilder colValidateResult = new StringBuilder();
                     boolean hasValueInRow = false;
-                    for (Map.Entry<Integer, FieldMeta> entry : classMeta.positionFieldMetaMap.entrySet()) {
+                    for (Map.Entry<Integer, FieldMeta> entry : positionFieldMetaMap.entrySet()) {
                         Integer colIndex = entry.getKey();
                         FieldMeta fieldMeta = entry.getValue();
                         XSSFCell cell = row.getCell(colIndex, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
-                        if (cell == null) {
-                            continue;
-                        }
+
                         hasValueInRow = true;
                         ExcelCellDataTransfer cellDataTransfer = fieldMeta.getCellDataTransfer();
                         Object value = parseCellValue(cellDataTransfer, cell);
                         if (instance == null) {
                             instance = clazz.newInstance();
                         }
+                        validateCellValue(fieldMeta, value).ifPresent(info -> colValidateResult.append("Column ")
+                                .append(colIndex + 1).append(":").append(info).append(";"));
                         fieldMeta.getField().setAccessible(true);
                         fieldMeta.getField().set(instance, value);
-
                     }
                     if (hasValueInRow) {
                         currentResult.add(instance);
                     }
+                    if (colValidateResult.length() > 0) {
+                        String lineValidateResult = "Error in Row " + (rowNum + 1) + ", " + colValidateResult;

Review Comment:
   Fixed



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [inlong] featzhang commented on a diff in pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7869:
URL: https://github.com/apache/inlong/pull/7869#discussion_r1168565556


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/util/Preconditions.java:
##########
@@ -153,6 +155,17 @@ public static void expectNotBlank(String obj, ErrorCodeEnum errorCodeEnum, Strin
         }
     }
 
+    public static void expectBlank(String obj, ErrorCodeEnum errorCodeEnum, String errMsg) {
+        if (StringUtils.isNotBlank(obj)) {
+            throw new BusinessException(errorCodeEnum, errMsg);
+        }
+    }
+    public static void expectBlank(List<String> obj, ErrorCodeEnum errorCodeEnum, String errMsg) {

Review Comment:
   Fixed



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -457,6 +477,27 @@ private static String parseCellValue(Cell cell) {
         return cellValue;
     }
 
+    /**
+     * Validate the cell value of a given field in the Excel sheet
+     *
+     * @param fieldMeta the meta information of the field to validate
+     * @param value     the value of the field to validate
+     */
+    private static Optional<String> validateCellValue(
+            FieldMeta fieldMeta,
+            Object value) {
+        ExcelCellValidator cellValidator = fieldMeta.getCellValidator();
+        if (cellValidator != null) {

Review Comment:
   Fixed



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [inlong] featzhang commented on a diff in pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7869:
URL: https://github.com/apache/inlong/pull/7869#discussion_r1168568103


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -553,17 +599,22 @@ public static <T> ClassMeta<T> of(Class<T> clazz)
             for (Field field : fields) {
                 ExcelField excelField = field.getAnnotation(ExcelField.class);
                 if (excelField != null) {
+                    Class<? extends ExcelCellValidator> validatorClass = excelField.validator();
                     ExcelCellDataTransfer excelCellDataTransfer = excelField.x2oTransfer();
+                    ExcelCellValidator excelCellValidator = null;
+                    if (validatorClass != ExcelCellValidator.class) {
+                        excelCellValidator = validatorClass.newInstance();
+                    }
                     meta.addField(field.getName(), excelField.name(), field, field.getType(),
-                            excelCellDataTransfer);
+                            excelCellDataTransfer, excelCellValidator);

Review Comment:
   The ExcelCellValidator has no effect when it is null, and it will check if it is null when used.



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [inlong] fuweng11 commented on a diff in pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
fuweng11 commented on code in PR #7869:
URL: https://github.com/apache/inlong/pull/7869#discussion_r1168537587


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/util/Preconditions.java:
##########
@@ -153,6 +155,17 @@ public static void expectNotBlank(String obj, ErrorCodeEnum errorCodeEnum, Strin
         }
     }
 
+    public static void expectBlank(String obj, ErrorCodeEnum errorCodeEnum, String errMsg) {
+        if (StringUtils.isNotBlank(obj)) {
+            throw new BusinessException(errorCodeEnum, errMsg);
+        }
+    }
+    public static void expectBlank(List<String> obj, ErrorCodeEnum errorCodeEnum, String errMsg) {

Review Comment:
   expectEmpty.



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -553,17 +599,22 @@ public static <T> ClassMeta<T> of(Class<T> clazz)
             for (Field field : fields) {
                 ExcelField excelField = field.getAnnotation(ExcelField.class);
                 if (excelField != null) {
+                    Class<? extends ExcelCellValidator> validatorClass = excelField.validator();
                     ExcelCellDataTransfer excelCellDataTransfer = excelField.x2oTransfer();
+                    ExcelCellValidator excelCellValidator = null;
+                    if (validatorClass != ExcelCellValidator.class) {
+                        excelCellValidator = validatorClass.newInstance();
+                    }
                     meta.addField(field.getName(), excelField.name(), field, field.getType(),
-                            excelCellDataTransfer);
+                            excelCellDataTransfer, excelCellValidator);

Review Comment:
   `excelCellValidator` maybe is null.



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -372,35 +373,54 @@ public static <E> List<E> read(InputStream is, Class<E> clazz)
 
                 int lastRowNum = sheet.getLastRowNum();
                 List<E> currentResult = new ArrayList<>(lastRowNum);
-
+                List<String> validateResult = new ArrayList<>(lastRowNum);
                 for (int rowNum = 1; rowNum <= lastRowNum; ++rowNum) {
                     XSSFRow row = sheet.getRow(rowNum);
                     if (row == null) {
                         continue;
                     }
+                    // Mark the row only if all cells are not null
+                    Map<Integer, FieldMeta> positionFieldMetaMap = classMeta.positionFieldMetaMap;
+                    boolean rowNotNull = positionFieldMetaMap.keySet()
+                            .stream()
+                            .map(colIndex -> row.getCell(colIndex,
+                                    Row.MissingCellPolicy.RETURN_BLANK_AS_NULL) != null)
+                            .reduce(false, (left, right) -> left || right);
+
+                    // Skip if all columns are null
+                    if (!rowNotNull) {
+                        continue;
+                    }
+
                     E instance = null;
+                    StringBuilder colValidateResult = new StringBuilder();
                     boolean hasValueInRow = false;
-                    for (Map.Entry<Integer, FieldMeta> entry : classMeta.positionFieldMetaMap.entrySet()) {
+                    for (Map.Entry<Integer, FieldMeta> entry : positionFieldMetaMap.entrySet()) {
                         Integer colIndex = entry.getKey();
                         FieldMeta fieldMeta = entry.getValue();
                         XSSFCell cell = row.getCell(colIndex, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
-                        if (cell == null) {
-                            continue;
-                        }
+
                         hasValueInRow = true;
                         ExcelCellDataTransfer cellDataTransfer = fieldMeta.getCellDataTransfer();
                         Object value = parseCellValue(cellDataTransfer, cell);
                         if (instance == null) {
                             instance = clazz.newInstance();
                         }
+                        validateCellValue(fieldMeta, value).ifPresent(info -> colValidateResult.append("Column ")
+                                .append(colIndex + 1).append(":").append(info).append(";"));
                         fieldMeta.getField().setAccessible(true);
                         fieldMeta.getField().set(instance, value);
-
                     }
                     if (hasValueInRow) {
                         currentResult.add(instance);
                     }
+                    if (colValidateResult.length() > 0) {
+                        String lineValidateResult = "Error in Row " + (rowNum + 1) + ", " + colValidateResult;

Review Comment:
   Suggest use `String.format()`.



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -457,6 +477,27 @@ private static String parseCellValue(Cell cell) {
         return cellValue;
     }
 
+    /**
+     * Validate the cell value of a given field in the Excel sheet
+     *
+     * @param fieldMeta the meta information of the field to validate
+     * @param value     the value of the field to validate
+     */
+    private static Optional<String> validateCellValue(
+            FieldMeta fieldMeta,
+            Object value) {
+        ExcelCellValidator cellValidator = fieldMeta.getCellValidator();
+        if (cellValidator != null) {

Review Comment:
   ```
     if (cellValidator != null&&!cellValidator.validate(value)) {
          return Optional.of(cellValidator.getInvalidTip());
     } else {
           return Optional.empty();
     }
   ```



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [inlong] healchow commented on a diff in pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

Posted by "healchow (via GitHub)" <gi...@apache.org>.
healchow commented on code in PR #7869:
URL: https://github.com/apache/inlong/pull/7869#discussion_r1168630631


##########
inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/stream/StreamFieldTypeCellValidator.java:
##########
@@ -38,4 +38,14 @@ public List<String> constraint() {
         return new ArrayList<>(STREAM_FIELD_TYPES);
     }
 
+    @Override
+    public boolean validate(String value) {
+        return STREAM_FIELD_TYPES.contains(value);
+    }
+
+    @Override
+    public String getInvalidTip() {
+        return "StreamField type must one of " + STREAM_FIELD_TYPES;

Review Comment:
   `must one of` -> `must be one of`.
   
   And also, extracting a String constant, then using `String.format()` to append the `STREAM_FIELD_TYPES` maybe is a good practice.



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [inlong] featzhang commented on a diff in pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7869:
URL: https://github.com/apache/inlong/pull/7869#discussion_r1168736795


##########
inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/stream/StreamFieldTypeCellValidator.java:
##########
@@ -38,4 +38,14 @@ public List<String> constraint() {
         return new ArrayList<>(STREAM_FIELD_TYPES);
     }
 
+    @Override
+    public boolean validate(String value) {
+        return STREAM_FIELD_TYPES.contains(value);
+    }
+
+    @Override
+    public String getInvalidTip() {
+        return "StreamField type must one of " + STREAM_FIELD_TYPES;

Review Comment:
   Fixed



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org