Skip to content

Commit

Permalink
[BACKLOG-18271] - CSV File Input: Columns with the same name in the c…
Browse files Browse the repository at this point in the history
…sv are not read properly (#4274)

* [BACKLOG-18271] - CSV File Input: Columns with the same name in the csv are not read properly

* [BACKLOG-18271] - CSV File Input: Columns with the same name in the csv are not read properly - test
  • Loading branch information
AndreyBurikhin authored and Ben Morrise committed Aug 14, 2017
1 parent 6a8fc33 commit 04cd197
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Expand Up @@ -22,9 +22,13 @@

package org.pentaho.di.trans.steps.csvinput;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;

public class NamedFieldsMapping implements FieldsMapping {

private final Map<Integer, Integer> actualToMetaFieldMapping;
Expand All @@ -45,13 +49,19 @@ public int size() {
}

public static NamedFieldsMapping mapping( String[] actualFieldNames, String[] metaFieldNames ) {
Map<String, Integer> metaNameToIndex = new HashMap<>();
MultiValuedMap<String, Integer> metaNameToIndex = new ArrayListValuedHashMap<String, Integer>();
for ( int j = 0; j < metaFieldNames.length; j++ ) {
metaNameToIndex.put( metaFieldNames[j], Integer.valueOf( j ) );
}
Map<Integer, Integer> actualToMetaFieldMapping = new HashMap<>();
for ( int i = 0; i < actualFieldNames.length; i++ ) {
actualToMetaFieldMapping.put( i, metaNameToIndex.get( actualFieldNames[i] ) );
Collection<Integer> columnIndexes = metaNameToIndex.get( actualFieldNames[i] );
if ( columnIndexes.isEmpty() ) {
continue;
}
Integer columnIndex = columnIndexes.iterator().next();
metaNameToIndex.removeMapping( actualFieldNames[i], columnIndex );
actualToMetaFieldMapping.put( i, columnIndex );
}
return new NamedFieldsMapping( actualToMetaFieldMapping );
}
Expand Down
Expand Up @@ -64,4 +64,20 @@ public void mapping() {
assertEquals( 0, mapping.fieldMetaIndex( 1 ) );
}

@Test
public void mappingWithNonUniqueColumnNames() {
NamedFieldsMapping mapping =
NamedFieldsMapping.mapping( new String[] { "Object", "Test", "Object" }, new String[] { "Object", "Test",
"Object" } );
assertEquals( 0, mapping.fieldMetaIndex( 0 ) );
assertEquals( 2, mapping.fieldMetaIndex( 2 ) );
}

@Test
public void fieldMetaIndexWithUnexistingField_nonUniqueColumnNames() {
NamedFieldsMapping mapping =
NamedFieldsMapping.mapping( new String[] { "Object", "Test", "Object" }, new String[] { "Object", "Test" } );
assertEquals( FieldsMapping.FIELD_DOES_NOT_EXIST, mapping.fieldMetaIndex( 2 ) );
}

}

0 comments on commit 04cd197

Please sign in to comment.