Skip to content

[Java] VectorSchemaRoot#addVector() cannot add a vector to the end of the current vector collection #301

@asfimport

Description

@asfimport

The current implementation of Java VectorSchemaRoot cannot add a vector at the end of the current list (which is the generally understood meaning of "add").

The Precondition check in the method's second line prevents providing an appropriate index for adding at the end:

public VectorSchemaRoot addVector(int index, FieldVector vector) {
  Preconditions.checkNotNull(vector);
  Preconditions.checkArgument(index >= 0 && index < fieldVectors.size());
  List<FieldVector> newVectors = new ArrayList<>();
  for (int i = 0; i < fieldVectors.size(); i++) {
    if (i == index) {
      newVectors.add(vector);
    }
    newVectors.add(fieldVectors.get(i));
  }
  return new VectorSchemaRoot(newVectors);
}
 

One possible implementation resolving the issue is shown below.

public VectorSchemaRoot addVector(int index, FieldVector vector) {
  Preconditions.checkNotNull(vector);
  Preconditions.checkArgument(index >= 0 && index <= fieldVectors.size());
  List<FieldVector> newVectors = new ArrayList<>();
  if (index == fieldVectors.size()) {
    newVectors.addAll(fieldVectors);
    newVectors.add(vector); 
  } else {
    for (int i = 0; i < fieldVectors.size(); i++) {
      if (i == index) {
        newVectors.add(vector);
      }
      newVectors.add(fieldVectors.get(i));
    }
  }
  return new VectorSchemaRoot(newVectors);
}

 

 

 

 

 

Reporter: Larry White / @lwhite1

Note: This issue was originally created as ARROW-17530. Please see the migration documentation for further details.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions