You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/09/10 18:24:19 UTC

[GitHub] [nifi] pgyori opened a new pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

pgyori opened a new pull request #5381:
URL: https://github.com/apache/nifi/pull/5381


   …to remove fields from records
   
   https://issues.apache.org/jira/browse/NIFI-9206
   
   <!--
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
         http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
   -->
   
   #### Description of PR
   
   Adds a new processor called RemoveRecordField and implements the ability to remove fields from records (and their schemas). The field(s) to be removed can be specified by RecordPath expressions.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [ ] Is there a JIRA ticket associated with this PR? Is it referenced 
        in the commit message?
   
   - [ ] Does your PR title start with **NIFI-XXXX** where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [ ] Has your PR been rebased against the latest commit within the target branch (typically `main`)?
   
   - [ ] Is your initial contribution a single, squashed commit? _Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not `squash` or use `--force` when pushing to allow for clean monitoring of changes._
   
   ### For code changes:
   - [ ] Have you ensured that the full suite of tests is executed via `mvn -Pcontrib-check clean install` at the root `nifi` folder?
   - [ ] Have you written or updated unit tests to verify your changes?
   - [ ] Have you verified that the full build is successful on JDK 8?
   - [ ] Have you verified that the full build is successful on JDK 11?
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
   - [ ] If applicable, have you updated the `LICENSE` file, including the main `LICENSE` file under `nifi-assembly`?
   - [ ] If applicable, have you updated the `NOTICE` file, including the main `NOTICE` file found under `nifi-assembly`?
   - [ ] If adding new Properties, have you added `.displayName` in addition to .name (programmatic access) for each of the new properties?
   
   ### For documentation related changes:
   - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check GitHub Actions CI for build issues and submit an update to your PR as soon as possible.
   


-- 
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: issues-unsubscribe@nifi.apache.org

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



[GitHub] [nifi] pgyori commented on a change in pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

Posted by GitBox <gi...@apache.org>.
pgyori commented on a change in pull request #5381:
URL: https://github.com/apache/nifi/pull/5381#discussion_r739498445



##########
File path: nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java
##########
@@ -93,21 +93,142 @@ public void testHashCodeAndEqualsWithSelfReferencingSchema() {
     }
 
     @Test
-    public void testFieldsArentCheckedInEqualsIfNameAndNamespaceMatch() {
-        final RecordField testField = new RecordField("test", RecordFieldType.STRING.getDataType());
+    public void testEqualsSimpleSchema() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertTrue(schema1.equals(schema2));
+    }
 
-        final SimpleRecordSchema schema1 = new SimpleRecordSchema(SchemaIdentifier.EMPTY);
-        schema1.setSchemaName("name");
-        schema1.setSchemaNamespace("namespace");
-        schema1.setFields(Collections.singletonList(testField));
+    @Test
+    public void testEqualsSimpleSchemaEvenIfSchemaNameAndNameSpaceAreDifferent() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName1 = "schemaName1";
+        final String schemaName2 = "schemaName2";
+        final String namespace1 = "namespace1";
+        final String namespace2 = "namespace2";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName1, namespace1);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName2, namespace2);
+
+        // WHEN, THEN
+        assertTrue(schema1.equals(schema2));
+    }
 
-        SimpleRecordSchema schema2 = Mockito.spy(new SimpleRecordSchema(SchemaIdentifier.EMPTY));
-        schema2.setSchemaName("name");
-        schema2.setSchemaNamespace("namespace");
-        schema2.setFields(Collections.singletonList(testField));
+    @Test
+    public void testNotEqualsSimpleSchemaDifferentTypes() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField1, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertFalse(schema1.equals(schema2));
+    }
+
+    @Test
+    public void testNotEqualsSimpleSchemaDifferentFieldNames() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final String nameOfField3 = "field3";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField3, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertFalse(schema1.equals(schema2));
+    }
+
+    @Test
+    public void testEqualsRecursiveSchema() {
+        final String field1 = "field1";
+        final String field2 = "field2";
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createRecursiveSchema(field1, field2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createRecursiveSchema(field1, field2, schemaName, namespace);
 
         assertTrue(schema1.equals(schema2));
-        Mockito.verify(schema2, Mockito.never()).getFields();
+        assertTrue(schema2.equals(schema1));
+    }
+
+    @Test(expected = StackOverflowError.class)
+    public void testNotEqualsRecursiveSchemaIfSchemaNameIsDifferent() {
+        final String field1 = "field1";
+        final String field2 = "field2";
+        final String schemaName1 = "schemaName1";
+        final String schemaName2 = "schemaName2";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createRecursiveSchema(field1, field2, schemaName1, namespace);
+        final SimpleRecordSchema schema2 = createRecursiveSchema(field1, field2, schemaName2, namespace);
+
+        assertFalse(schema1.equals(schema2)); // Causes StackOverflowError
+    }
+
+    @Test(expected = StackOverflowError.class)

Review comment:
       I know that this is super weird. This was the behavior even before I started modifying the code and I want to draw attention to this by creating this test case.




-- 
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: issues-unsubscribe@nifi.apache.org

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



[GitHub] [nifi] github-actions[bot] commented on pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #5381:
URL: https://github.com/apache/nifi/pull/5381#issuecomment-1062416196


   We're marking this PR as stale due to lack of updates in the past few months. If after another couple of weeks the stale label has not been removed this PR will be closed. This stale marker and eventual auto close does not indicate a judgement of the PR just lack of reviewer bandwidth and helps us keep the PR queue more manageable.  If you would like this PR re-opened you can do so and a committer can remove the stale tag.  Or you can open a new PR.  Try to help review other PRs to increase PR review bandwidth which in turn helps yours.


-- 
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: issues-unsubscribe@nifi.apache.org

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



[GitHub] [nifi] tpalfy commented on a change in pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

Posted by GitBox <gi...@apache.org>.
tpalfy commented on a change in pull request #5381:
URL: https://github.com/apache/nifi/pull/5381#discussion_r744962782



##########
File path: nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/RecordFieldRemover.java
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.record.path;
+
+import org.apache.nifi.serialization.record.Record;
+import org.apache.nifi.serialization.record.RecordFieldRemovalPath;
+import org.apache.nifi.serialization.record.RecordSchema;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public class RecordFieldRemover {
+    private Record record;
+
+    public RecordFieldRemover(Record record) {
+        this.record = record;
+    }
+
+    public Record remove(String path) {
+        RecordPath recordPath = new RecordPath(path);
+        RecordPathResult recordPathResult = org.apache.nifi.record.path.RecordPath.compile(recordPath.toString()).evaluate(record);
+        List<FieldValue> selectedFields = recordPathResult.getSelectedFields().collect(Collectors.toList());
+
+        if (recordPath.isAppliedToAllElementsInCollection()) {
+            removeAllElementsFromCollection(selectedFields);
+        } else {
+            selectedFields.forEach(field -> field.remove());
+        }
+
+        if (recordPath.pathRemovalRequiresSchemaModification()) {
+            modifySchema(selectedFields);
+        }
+
+        record.regenerateSchema();
+        return record;
+    }
+
+    private void removeAllElementsFromCollection(List<FieldValue> selectedFields) {
+        if (!selectedFields.isEmpty()) {
+            Optional<FieldValue> parentOptional = selectedFields.get(0).getParent();
+            if (parentOptional.isPresent()) {
+                FieldValue parent = parentOptional.get();
+                parent.removeContent();
+            }
+        }
+    }
+
+    private void modifySchema(List<FieldValue> selectedFields) {
+        List<RecordFieldRemovalPath> concretePaths = getConcretePaths(selectedFields);
+        removePathsFromSchema(concretePaths);
+    }
+
+    private List<RecordFieldRemovalPath> getConcretePaths(List<FieldValue> selectedFields) {
+        List<RecordFieldRemovalPath> paths = new ArrayList<>(selectedFields.size());
+        for (FieldValue field : selectedFields) {
+            RecordFieldRemovalPath path = new RecordFieldRemovalPath();
+            addToPathIfNotRoot(field, path);
+
+            Optional<FieldValue> parentOptional = field.getParent();
+            while (parentOptional.isPresent()) {
+                FieldValue parent = parentOptional.get();
+                addToPathIfNotRoot(parent, path);
+                parentOptional = parent.getParent();
+            }
+
+            paths.add(path);
+        }
+        return paths;
+    }
+
+    private void removePathsFromSchema(List<RecordFieldRemovalPath> paths) {
+        for (RecordFieldRemovalPath path : paths) {
+            RecordSchema schema = record.getSchema();
+            schema.removePath(path);
+        }
+    }
+
+    private void addToPathIfNotRoot(FieldValue field, RecordFieldRemovalPath path) {
+        if (field.getParent().isPresent()) {
+            path.add(field.getField().getFieldName());
+        }
+    }
+
+    private static class RecordPath {
+        private String recordPath;
+
+        public RecordPath(final String recordPath) {
+            this.recordPath = preprocessRecordPath(recordPath);
+        }
+
+        public boolean isAppliedToAllElementsInCollection() {
+            return recordPath.endsWith("[*]") || recordPath.endsWith("[0..-1]");
+        }
+
+        @Override
+        public String toString() {
+            return recordPath;
+        }
+
+        private String preprocessRecordPath(final String recordPath) {
+            if (recordPath.endsWith("]")) {
+                return unifyRecordPathEnd(recordPath);
+            }
+            return recordPath;
+        }
+
+        private String unifyRecordPathEnd(final String recordPath) {
+            String lastSquareBracketsOperator = getLastSquareBracketsOperator(recordPath);
+            if (lastSquareBracketsOperator.equals("[*]")) {
+                return recordPath.substring(0, recordPath.lastIndexOf('[')) + "[*]";
+            } else if (lastSquareBracketsOperator.equals("[0..-1]")) {
+                return recordPath.substring(0, recordPath.lastIndexOf('[')) + "[0..-1]";
+            } else {
+                return recordPath;
+            }
+        }
+
+        private String getLastSquareBracketsOperator(final String recordPath) {
+            int beginIndex = recordPath.lastIndexOf('[');
+            return recordPath.substring(beginIndex).replaceAll("\\s","");
+        }
+
+        public boolean pathRemovalRequiresSchemaModification() {
+            return allSquareBracketsContainAsteriskOnly(recordPath);
+        }
+
+        private boolean allSquareBracketsContainAsteriskOnly(String recordPath) {
+            boolean allSquareBracketsContainAsteriskOnly = true;
+            boolean inSquareBrackets = false;
+            for (int i = 0; i < recordPath.length() && allSquareBracketsContainAsteriskOnly; ++i) {
+                char character = recordPath.charAt(i);
+                if (inSquareBrackets) {
+                    switch (character) {
+                        case ' ':
+                        case '*':
+                            break;
+                        case ']':
+                            inSquareBrackets = false;
+                            break;
+                        default:
+                            allSquareBracketsContainAsteriskOnly = false;
+                    }
+                } else {
+                    if (character == '[') {
+                        inSquareBrackets = true;
+                    }
+                }
+            }
+            return allSquareBracketsContainAsteriskOnly;
+        }
+    }

Review comment:
       This could be significantly simplified:
   
   ```suggestion
       private boolean isAppliedToAllElementsInCollection(String recordPath) {
           // ends with [*] or [0..-1]
           return recordPath.matches(".*\\[\\s*\\*\\s*\\]$") || recordPath.matches(".*\\[\\s*0\\s*\\.\\.\\s*\\-1\\s*\\]$");
       }
   
       private boolean pathRemovalRequiresSchemaModification(String recordPath) {
           boolean pathNotReferencesIndividualArrayElements = !recordPath.matches(".*\\[(\\s*\\d+(\\s*,\\s*)?)+\\].*");
           boolean pathNotReferencesIndividualMapElements = !recordPath.matches(".*\\[\\s*'.*'\\s*\\].*");
   
           return pathNotReferencesIndividualArrayElements && pathNotReferencesIndividualMapElements;
       }
   ```

##########
File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestRemoveRecordField.java
##########
@@ -0,0 +1,424 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.processors.standard;
+
+import org.apache.nifi.json.JsonRecordSetWriter;
+import org.apache.nifi.json.JsonTreeReader;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.schema.access.SchemaAccessUtils;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+
+public class TestRemoveRecordField {
+
+    private TestRunner runner;
+
+    @Before
+    public void setup() throws InitializationException {
+        runner = TestRunners.newTestRunner(RemoveRecordField.class);
+    }
+
+    @Test
+    public void testRemoveSimpleFieldThatIsMissingFromOneRecord() throws InitializationException, IOException {
+        // GIVEN
+        String inputSchema = "src/test/resources/TestRemoveRecordField/input_schema/complex-person.avsc";
+        String inputFlowFile = "src/test/resources/TestRemoveRecordField/input/complex-person.json";
+        String outPutSchema = "src/test/resources/TestRemoveRecordField/output_schema/complex-person-no-dateOfBirth.avsc";
+        String outPutFlowFile = "src/test/resources/TestRemoveRecordField/output/complex-person-no-dateOfBirth.json";
+        String fieldToRemove = "/dateOfBirth";
+
+        executeRemovalTest(inputSchema, inputFlowFile, outPutSchema, outPutFlowFile, fieldToRemove);
+    }
+
+    @Test
+    public void testRemoveComplexFieldThatIsMissingFromOneRecord() throws InitializationException, IOException {
+        // GIVEN
+        String inputSchema = "src/test/resources/TestRemoveRecordField/input_schema/complex-person.avsc";
+        String inputFlowFile = "src/test/resources/TestRemoveRecordField/input/complex-person.json";
+        String outPutSchema = "src/test/resources/TestRemoveRecordField/output_schema/complex-person-no-workAddress.avsc";
+        String outPutFlowFile = "src/test/resources/TestRemoveRecordField/output/complex-person-no-workAddress.json";
+        String fieldToRemove = "/workAddress";
+
+        executeRemovalTest(inputSchema, inputFlowFile, outPutSchema, outPutFlowFile, fieldToRemove);
+    }
+
+    @Test
+    public void testRemoveFieldFromDeepStructure() throws InitializationException, IOException {
+        // GIVEN
+        String inputSchema = "src/test/resources/TestRemoveRecordField/input_schema/complex-person.avsc";
+        String inputFlowFile = "src/test/resources/TestRemoveRecordField/input/complex-person.json";
+        String outPutSchema = "src/test/resources/TestRemoveRecordField/output_schema/complex-person-no-workAddress-zip.avsc";
+        String outPutFlowFile = "src/test/resources/TestRemoveRecordField/output/complex-person-no-workAddress-zip.json";
+        String fieldToRemove = "/workAddress/zip";
+
+        executeRemovalTest(inputSchema, inputFlowFile, outPutSchema, outPutFlowFile, fieldToRemove);
+    }
+
+    @Test
+    public void testRemoveFieldFromDeepStructureWithRelativePath() throws InitializationException, IOException {
+        // GIVEN
+        String inputSchema = "src/test/resources/TestRemoveRecordField/input_schema/complex-person.avsc";
+        String inputFlowFile = "src/test/resources/TestRemoveRecordField/input/complex-person.json";
+        String outPutSchema = "src/test/resources/TestRemoveRecordField/output_schema/complex-person-no-zip.avsc";
+        String outPutFlowFile = "src/test/resources/TestRemoveRecordField/output/complex-person-no-zip.json";
+        String fieldToRemove = "//zip";
+
+        executeRemovalTest(inputSchema, inputFlowFile, outPutSchema, outPutFlowFile, fieldToRemove);
+    }
+
+    @Test
+    public void testRemoveFieldFrom3LevelDeepStructure() throws InitializationException, IOException {
+        // GIVEN
+        String inputSchema = "src/test/resources/TestRemoveRecordField/input_schema/complex-person.avsc";
+        String inputFlowFile = "src/test/resources/TestRemoveRecordField/input/complex-person.json";
+        String outPutSchema = "src/test/resources/TestRemoveRecordField/output_schema/complex-person-no-workAddress-building-letter.avsc";
+        String outPutFlowFile = "src/test/resources/TestRemoveRecordField/output/complex-person-no-workAddress-building-letter.json";
+        String fieldToRemove = "/workAddress/building/letter";
+
+        executeRemovalTest(inputSchema, inputFlowFile, outPutSchema, outPutFlowFile, fieldToRemove);
+    }
+

Review comment:
       I'd add two more tests around here:
   ```java
       @Test
       public void betterNamePending() throws InitializationException, IOException {
           // GIVEN
           String fieldToRemove = "/workAddress//letter";
   
           String inputSchema = "src/test/resources/TestRemoveRecordField/input_schema/complex-person.avsc";
           String outPutSchema = "src/test/resources/TestRemoveRecordField/output_schema/complex-person-no-workAddress-building-letter.avsc";
           
           String inputFlowFile = "src/test/resources/TestRemoveRecordField/input/complex-person.json";
           String outPutFlowFile = "src/test/resources/TestRemoveRecordField/output/complex-person-no-workAddress-building-letter.json";
   
           executeRemovalTest(inputSchema, inputFlowFile, outPutSchema, outPutFlowFile, fieldToRemove);
       }
   
       @Test
       public void betterNamePending() throws InitializationException, IOException {
           // GIVEN
           String fieldToRemove = "/workAddress/nonExistent/letter";
           
           String inputSchema = "src/test/resources/TestRemoveRecordField/input_schema/complex-person.avsc";
           String outPutSchema = "src/test/resources/TestRemoveRecordField/output_schema/complex-person.avsc";
           
           String inputFlowFile = "src/test/resources/TestRemoveRecordField/input/complex-person.json";
           String outPutFlowFile = "src/test/resources/TestRemoveRecordField/output/complex-person.json";
   
           executeRemovalTest(inputSchema, inputFlowFile, outPutSchema, outPutFlowFile, fieldToRemove);
       }
   ```




-- 
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: issues-unsubscribe@nifi.apache.org

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



[GitHub] [nifi] pgyori commented on a change in pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

Posted by GitBox <gi...@apache.org>.
pgyori commented on a change in pull request #5381:
URL: https://github.com/apache/nifi/pull/5381#discussion_r739498158



##########
File path: nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java
##########
@@ -93,21 +93,142 @@ public void testHashCodeAndEqualsWithSelfReferencingSchema() {
     }
 
     @Test
-    public void testFieldsArentCheckedInEqualsIfNameAndNamespaceMatch() {
-        final RecordField testField = new RecordField("test", RecordFieldType.STRING.getDataType());
+    public void testEqualsSimpleSchema() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertTrue(schema1.equals(schema2));
+    }
 
-        final SimpleRecordSchema schema1 = new SimpleRecordSchema(SchemaIdentifier.EMPTY);
-        schema1.setSchemaName("name");
-        schema1.setSchemaNamespace("namespace");
-        schema1.setFields(Collections.singletonList(testField));
+    @Test
+    public void testEqualsSimpleSchemaEvenIfSchemaNameAndNameSpaceAreDifferent() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName1 = "schemaName1";
+        final String schemaName2 = "schemaName2";
+        final String namespace1 = "namespace1";
+        final String namespace2 = "namespace2";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName1, namespace1);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName2, namespace2);
+
+        // WHEN, THEN
+        assertTrue(schema1.equals(schema2));
+    }
 
-        SimpleRecordSchema schema2 = Mockito.spy(new SimpleRecordSchema(SchemaIdentifier.EMPTY));
-        schema2.setSchemaName("name");
-        schema2.setSchemaNamespace("namespace");
-        schema2.setFields(Collections.singletonList(testField));
+    @Test
+    public void testNotEqualsSimpleSchemaDifferentTypes() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField1, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertFalse(schema1.equals(schema2));
+    }
+
+    @Test
+    public void testNotEqualsSimpleSchemaDifferentFieldNames() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final String nameOfField3 = "field3";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField3, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertFalse(schema1.equals(schema2));
+    }
+
+    @Test
+    public void testEqualsRecursiveSchema() {
+        final String field1 = "field1";
+        final String field2 = "field2";
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createRecursiveSchema(field1, field2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createRecursiveSchema(field1, field2, schemaName, namespace);
 
         assertTrue(schema1.equals(schema2));
-        Mockito.verify(schema2, Mockito.never()).getFields();
+        assertTrue(schema2.equals(schema1));
+    }
+
+    @Test(expected = StackOverflowError.class)

Review comment:
       I know that this is super weird. This was the behavior even before I started modifying the code and I want to draw attention to this by creating this test case. Fixing this in SimpleRecordSchema.equals() alters the behavior heavily and still leaves open questions. I'm open to ideas and suggestions.




-- 
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: issues-unsubscribe@nifi.apache.org

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



[GitHub] [nifi] github-actions[bot] closed pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed pull request #5381:
URL: https://github.com/apache/nifi/pull/5381


   


-- 
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: issues-unsubscribe@nifi.apache.org

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



[GitHub] [nifi] pgyori commented on a change in pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

Posted by GitBox <gi...@apache.org>.
pgyori commented on a change in pull request #5381:
URL: https://github.com/apache/nifi/pull/5381#discussion_r739498158



##########
File path: nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java
##########
@@ -93,21 +93,142 @@ public void testHashCodeAndEqualsWithSelfReferencingSchema() {
     }
 
     @Test
-    public void testFieldsArentCheckedInEqualsIfNameAndNamespaceMatch() {
-        final RecordField testField = new RecordField("test", RecordFieldType.STRING.getDataType());
+    public void testEqualsSimpleSchema() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertTrue(schema1.equals(schema2));
+    }
 
-        final SimpleRecordSchema schema1 = new SimpleRecordSchema(SchemaIdentifier.EMPTY);
-        schema1.setSchemaName("name");
-        schema1.setSchemaNamespace("namespace");
-        schema1.setFields(Collections.singletonList(testField));
+    @Test
+    public void testEqualsSimpleSchemaEvenIfSchemaNameAndNameSpaceAreDifferent() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName1 = "schemaName1";
+        final String schemaName2 = "schemaName2";
+        final String namespace1 = "namespace1";
+        final String namespace2 = "namespace2";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName1, namespace1);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName2, namespace2);
+
+        // WHEN, THEN
+        assertTrue(schema1.equals(schema2));
+    }
 
-        SimpleRecordSchema schema2 = Mockito.spy(new SimpleRecordSchema(SchemaIdentifier.EMPTY));
-        schema2.setSchemaName("name");
-        schema2.setSchemaNamespace("namespace");
-        schema2.setFields(Collections.singletonList(testField));
+    @Test
+    public void testNotEqualsSimpleSchemaDifferentTypes() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField1, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertFalse(schema1.equals(schema2));
+    }
+
+    @Test
+    public void testNotEqualsSimpleSchemaDifferentFieldNames() {
+        // GIVEN
+        final String nameOfField1 = "field1";
+        final String nameOfField2 = "field2";
+        final String nameOfField3 = "field3";
+        final DataType typeOfField1 = RecordFieldType.INT.getDataType();
+        final DataType typeOfField2 = RecordFieldType.STRING.getDataType();
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createSchemaWithTwoFields(nameOfField1, nameOfField2, typeOfField1, typeOfField2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createSchemaWithTwoFields(nameOfField1, nameOfField3, typeOfField1, typeOfField2, schemaName, namespace);
+
+        // WHEN, THEN
+        assertFalse(schema1.equals(schema2));
+    }
+
+    @Test
+    public void testEqualsRecursiveSchema() {
+        final String field1 = "field1";
+        final String field2 = "field2";
+        final String schemaName = "schemaName";
+        final String namespace = "namespace";
+
+        final SimpleRecordSchema schema1 = createRecursiveSchema(field1, field2, schemaName, namespace);
+        final SimpleRecordSchema schema2 = createRecursiveSchema(field1, field2, schemaName, namespace);
 
         assertTrue(schema1.equals(schema2));
-        Mockito.verify(schema2, Mockito.never()).getFields();
+        assertTrue(schema2.equals(schema1));
+    }
+
+    @Test(expected = StackOverflowError.class)

Review comment:
       I know that this is super weird. This was the behavior even before I started modifying the code and I want to draw attention to this. Fixing this in SimpleRecordSchema.equals() alters the behavior heavily and still leaves open questions. I'm open to ideas and suggestions.




-- 
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: issues-unsubscribe@nifi.apache.org

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



[GitHub] [nifi] pgyori commented on pull request #5381: NIFI-9206: Add RemoveRecordField processor and implement the ability …

Posted by GitBox <gi...@apache.org>.
pgyori commented on pull request #5381:
URL: https://github.com/apache/nifi/pull/5381#issuecomment-917133183


   This is a draft pull request at the moment. There are a few things that are going to get refactored, some optimized. There are some TODOs that I still need to work on (e.g. the processor's property validation is missing). The documentation is far from being complete.
   However, the field removal logic can now be reviewed and tested. For anyone reviewing this: a way to understand this logic is to start with the easiest unit test (the unit tests get more complicated from top to bottom in the order they are present in the TestRemoveRecordField class), and debug its execution all the way. The unit tests cover many complex cases of record handling and going through them will explain why certain parts are implemented as they are.
   Feedback is highly appreciated. If you find examples that cover cases for which the field removal logic does not work properly, let me know!


-- 
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: issues-unsubscribe@nifi.apache.org

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