Skip to content

Commit 552ceca

Browse files
lwhite1kou
authored andcommitted
ARROW-18014: [Java] Implement copy functions for vectors and Table (#14389)
Copy methods for the table and for individual vectors are provided with tests Authored-by: Larry White <[email protected]> Signed-off-by: David Li <[email protected]>
1 parent 787c030 commit 552ceca

File tree

6 files changed

+302
-161
lines changed

6 files changed

+302
-161
lines changed

vector/src/main/java/org/apache/arrow/vector/table/BaseTable.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public BaseTable(List<FieldVector> fieldVectors, int rowCount, DictionaryProvide
9090
this.schema = new Schema(fields);
9191
}
9292

93+
BaseTable() {
94+
this.fieldVectors = new ArrayList<>();
95+
}
96+
9397
/**
9498
* Returns a FieldReader for the vector with the given name.
9599
*
@@ -254,7 +258,7 @@ FieldVector getVector(String columnName) {
254258
return entry.getValue();
255259
}
256260
}
257-
throw new IllegalStateException(String.format("No vector named '%s' is present in the table", columnName));
261+
throw new IllegalArgumentException(String.format("No vector named '%s' is present in the table", columnName));
258262
}
259263

260264
/**
@@ -266,6 +270,48 @@ FieldVector getVector(int columnIndex) {
266270
return fieldVectors.get(columnIndex);
267271
}
268272

273+
274+
/**
275+
* Returns a copy of the vector with the given name, or throws IllegalArgumentException if the name is not found.
276+
* Names are case-sensitive.
277+
*
278+
* @param columnName The name of the vector to copy
279+
* @return A copy of the Vector with the given name
280+
* @throws IllegalArgumentException if the name is not the name of a vector in the table.
281+
*/
282+
public FieldVector getVectorCopy(String columnName) {
283+
FieldVector source;
284+
for (Map.Entry<Field, FieldVector> entry : fieldVectorsMap.entrySet()) {
285+
if (entry.getKey().getName().equals(columnName)) {
286+
source = entry.getValue();
287+
FieldVector copy = source.getField().createVector(source.getAllocator());
288+
copy.allocateNew();
289+
for (int i = 0; i < source.getValueCount(); i++) {
290+
copy.copyFromSafe(i, i, source);
291+
}
292+
copy.setValueCount(source.getValueCount());
293+
return copy;
294+
}
295+
}
296+
throw new IllegalStateException(String.format("No vector named '%s' is present in the table", columnName));
297+
}
298+
299+
/**
300+
* Returns a copy of the vector at the given position.
301+
*
302+
* @param columnIndex The 0-based position of the vector to be copied
303+
*/
304+
public FieldVector getVectorCopy(int columnIndex) {
305+
FieldVector source = fieldVectors.get(columnIndex);
306+
FieldVector copy = source.getField().createVector(source.getAllocator());
307+
copy.allocateNew();
308+
for (int i = 0; i < source.getValueCount(); i++) {
309+
copy.copyFromSafe(i, i, source);
310+
}
311+
copy.setValueCount(source.getValueCount());
312+
return copy;
313+
}
314+
269315
/**
270316
* Returns an immutable Row object holding a reference to this table. The default character
271317
* encoding used by the cursor to decode Strings will be StandardCharsets.UTF_8 as this is the only charset

0 commit comments

Comments
 (0)