Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-16754: [Java] StructVector's child vectors get unexpectedly reordered after adding duplicated fields #13321

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -54,7 +54,7 @@
public class MapWithOrdinalImpl<K, V> implements MapWithOrdinal<K, V> {
private static final Logger logger = LoggerFactory.getLogger(MapWithOrdinalImpl.class);

private final Map<K, Map.Entry<Integer, V>> primary = new HashMap<>();
private final Map<K, Map.Entry<Integer, V>> primary = new LinkedHashMap<>();
private final IntObjectHashMap<V> secondary = new IntObjectHashMap<>();

private final Map<K, V> delegate = new Map<K, V>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.vector.complex.AbstractStructVector;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.complex.UnionVector;
import org.apache.arrow.vector.holders.ComplexHolder;
import org.apache.arrow.vector.types.Types.MinorType;
import org.apache.arrow.vector.types.pojo.ArrowType.Struct;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -171,13 +174,112 @@ public void testAddOrGetComplexChildVectors() {
vector.addOrGetStruct("struct");
vector.addOrGetMap("map", true);

List<FieldVector> childrens = vector.getChildrenFromFields();
assertEquals(5, childrens.size());
assertEquals(MinorType.LIST, childrens.get(0).getMinorType());
assertEquals(MinorType.FIXED_SIZE_LIST, childrens.get(1).getMinorType());
assertEquals(MinorType.UNION, childrens.get(2).getMinorType());
assertEquals(MinorType.STRUCT, childrens.get(3).getMinorType());
assertEquals(MinorType.MAP, childrens.get(4).getMinorType());
List<FieldVector> children = vector.getChildrenFromFields();
assertEquals(5, children.size());
assertEquals(MinorType.LIST, children.get(0).getMinorType());
assertEquals(MinorType.FIXED_SIZE_LIST, children.get(1).getMinorType());
assertEquals(MinorType.UNION, children.get(2).getMinorType());
assertEquals(MinorType.STRUCT, children.get(3).getMinorType());
assertEquals(MinorType.MAP, children.get(4).getMinorType());
}
}

@Test
public void testAddChildVectorsWithDuplicatedFieldNamesForConflictPolicyAppend() {
final FieldType type = new FieldType(true, Struct.INSTANCE, null, null);
try (StructVector vector = new StructVector("struct", allocator, type, null,
AbstractStructVector.ConflictPolicy.CONFLICT_APPEND, true)) {
final List<Field> initFields = new ArrayList<>();

// Add a bit more fields to test against stability of the internal field
// ordering mechanism of StructVector
initFields.add(Field.nullable("varchar1", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("int1", MinorType.INT.getType()));
initFields.add(Field.nullable("varchar2", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("int2", MinorType.INT.getType()));
initFields.add(Field.nullable("varchar3", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("int3", MinorType.INT.getType()));
initFields.add(Field.nullable("uncertain-type", MinorType.INT.getType()));

// To ensure duplicated field names don't mess up the original field order
// in the struct vector
initFields.add(Field.nullable("varchar1", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("varchar2", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("varchar3", MinorType.VARCHAR.getType()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test a duplicated field name with a different type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

initFields.add(Field.nullable("uncertain-type", MinorType.VARCHAR.getType()));

vector.initializeChildrenFromFields(initFields);

List<FieldVector> children = vector.getChildrenFromFields();
assertEquals(11, children.size());
assertEquals("varchar1", children.get(0).getName());
assertEquals("int1", children.get(1).getName());
assertEquals("varchar2", children.get(2).getName());
assertEquals("int2", children.get(3).getName());
assertEquals("varchar3", children.get(4).getName());
assertEquals("int3", children.get(5).getName());
assertEquals("uncertain-type", children.get(6).getName());
assertEquals("varchar1", children.get(7).getName());
assertEquals("varchar2", children.get(8).getName());
assertEquals("varchar3", children.get(9).getName());
assertEquals("uncertain-type", children.get(10).getName());
assertEquals(MinorType.VARCHAR, children.get(0).getMinorType());
assertEquals(MinorType.INT, children.get(1).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(2).getMinorType());
assertEquals(MinorType.INT, children.get(3).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(4).getMinorType());
assertEquals(MinorType.INT, children.get(5).getMinorType());
assertEquals(MinorType.INT, children.get(6).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(7).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(8).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(9).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(10).getMinorType());
}
}

@Test
public void testAddChildVectorsWithDuplicatedFieldNamesForConflictPolicyReplace() {
final FieldType type = new FieldType(true, Struct.INSTANCE, null, null);
try (StructVector vector = new StructVector("struct", allocator, type, null,
AbstractStructVector.ConflictPolicy.CONFLICT_REPLACE, true)) {
final List<Field> initFields = new ArrayList<>();

// Add a bit more fields to test against stability of the internal field
// ordering mechanism of StructVector
initFields.add(Field.nullable("varchar1", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("int1", MinorType.INT.getType()));
initFields.add(Field.nullable("varchar2", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("int2", MinorType.INT.getType()));
initFields.add(Field.nullable("varchar3", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("int3", MinorType.INT.getType()));
initFields.add(Field.nullable("uncertain-type", MinorType.INT.getType()));

// To ensure duplicated field names don't mess up the original field order
// in the struct vector
initFields.add(Field.nullable("varchar1", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("varchar2", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("varchar3", MinorType.VARCHAR.getType()));
initFields.add(Field.nullable("uncertain-type", MinorType.VARCHAR.getType()));

vector.initializeChildrenFromFields(initFields);

List<FieldVector> children = vector.getChildrenFromFields();
assertEquals(7, children.size());
assertEquals("varchar1", children.get(0).getName());
assertEquals("int1", children.get(1).getName());
assertEquals("varchar2", children.get(2).getName());
assertEquals("int2", children.get(3).getName());
assertEquals("varchar3", children.get(4).getName());
assertEquals("int3", children.get(5).getName());
assertEquals("uncertain-type", children.get(6).getName());
assertEquals(MinorType.VARCHAR, children.get(0).getMinorType());
assertEquals(MinorType.INT, children.get(1).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(2).getMinorType());
assertEquals(MinorType.INT, children.get(3).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(4).getMinorType());
assertEquals(MinorType.INT, children.get(5).getMinorType());
assertEquals(MinorType.VARCHAR, children.get(6).getMinorType());
}
}

}