apache / arrow-java

Official Java implementation of Apache Arrow
https://arrow.apache.org/
Apache License 2.0
4 stars 5 forks source link

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

Open asfimport opened 2 years ago

asfimport commented 2 years ago

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.

asfimport commented 2 years ago

Apache Arrow JIRA Bot: This issue was last updated over 90 days ago, which may be an indication it is no longer being actively worked. To better reflect the current state, the issue is being unassigned per project policy. Please feel free to re-take assignment of the issue if it is being actively worked, or if you plan to start that work soon.