Skip to content

Commit 5bce781

Browse files
committed
GH-765: Add Data#import... overloads that do not close/free imported BaseStruct objects
1 parent 7c25ce5 commit 5bce781

File tree

3 files changed

+190
-31
lines changed

3 files changed

+190
-31
lines changed

c/src/main/java/org/apache/arrow/c/ArrayImporter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ void importArray(ArrowArray src) {
5858
ArrowArray ownedArray = ArrowArray.allocateNew(allocator);
5959
ownedArray.save(snapshot);
6060
src.markReleased();
61-
src.close();
6261

6362
recursionLevel = 0;
6463

c/src/main/java/org/apache/arrow/c/ArrowArrayStreamReader.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ final class ArrowArrayStreamReader extends ArrowReader {
4444
this.ownedStream = ArrowArrayStream.allocateNew(allocator);
4545
this.ownedStream.save(snapshot);
4646
stream.markReleased();
47-
stream.close();
4847
}
4948

5049
@Override

c/src/main/java/org/apache/arrow/c/Data.java

Lines changed: 190 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ public static void exportArrayStream(
231231
new ArrayStreamExporter(allocator).export(out, reader);
232232
}
233233

234+
/**
235+
* Equivalent to calling {@link #importField(BufferAllocator, ArrowSchema, CDataDictionaryProvider, boolean) importField(allocator, schema, provider, true)}.
236+
*
237+
* @param allocator Buffer allocator for allocating dictionary vectors
238+
* @param schema C data interface struct representing the field [inout]
239+
* @param provider A dictionary provider will be initialized with empty dictionary vectors
240+
* (optional)
241+
* @return Imported field object
242+
* @see #importField(BufferAllocator, ArrowSchema, CDataDictionaryProvider, boolean)
243+
*/
244+
public static Field importField(
245+
BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) {
246+
return importField(allocator, schema, provider, true);
247+
}
248+
234249
/**
235250
* Import Java Field from the C data interface.
236251
*
@@ -241,19 +256,40 @@ public static void exportArrayStream(
241256
* @param schema C data interface struct representing the field [inout]
242257
* @param provider A dictionary provider will be initialized with empty dictionary vectors
243258
* (optional)
259+
* @param closeImportedStructs if true, the ArrowSchema struct will be closed when this method completes.
244260
* @return Imported field object
245261
*/
246262
public static Field importField(
247-
BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) {
263+
BufferAllocator allocator,
264+
ArrowSchema schema,
265+
CDataDictionaryProvider provider,
266+
boolean closeImportedStructs) {
248267
try {
249268
SchemaImporter importer = new SchemaImporter(allocator);
250269
return importer.importField(schema, provider);
251270
} finally {
252271
schema.release();
253-
schema.close();
272+
if (closeImportedStructs) {
273+
schema.close();
274+
}
254275
}
255276
}
256277

278+
/**
279+
* Equivalent to calling {@link #importSchema(BufferAllocator, ArrowSchema, CDataDictionaryProvider, boolean) importSchema(allocator, schema, provider, true)}.
280+
*
281+
* @param allocator Buffer allocator for allocating dictionary vectors
282+
* @param schema C data interface struct representing the field
283+
* @param provider A dictionary provider will be initialized with empty dictionary vectors
284+
* (optional)
285+
* @return Imported schema object
286+
* @see #importSchema(BufferAllocator, ArrowSchema, CDataDictionaryProvider, boolean)
287+
*/
288+
public static Schema importSchema(
289+
BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) {
290+
return importSchema(allocator, schema, provider, true);
291+
}
292+
257293
/**
258294
* Import Java Schema from the C data interface.
259295
*
@@ -264,11 +300,15 @@ public static Field importField(
264300
* @param schema C data interface struct representing the field
265301
* @param provider A dictionary provider will be initialized with empty dictionary vectors
266302
* (optional)
303+
* @param closeImportedStructs if true, the ArrowSchema struct will be closed when this method completes.
267304
* @return Imported schema object
268305
*/
269306
public static Schema importSchema(
270-
BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) {
271-
Field structField = importField(allocator, schema, provider);
307+
BufferAllocator allocator,
308+
ArrowSchema schema,
309+
CDataDictionaryProvider provider,
310+
boolean closeImportedStructs) {
311+
Field structField = importField(allocator, schema, provider, closeImportedStructs);
272312
if (structField.getType().getTypeID() != ArrowTypeID.Struct) {
273313
throw new IllegalArgumentException(
274314
"Cannot import schema: ArrowSchema describes non-struct type");
@@ -277,23 +317,63 @@ public static Schema importSchema(
277317
}
278318

279319
/**
280-
* Import Java vector from the C data interface.
281-
*
282-
* <p>The ArrowArray struct has its contents moved (as per the C data interface specification) to
283-
* a private object held alive by the resulting array.
320+
* Equivalent to calling {@link #importIntoVector(BufferAllocator, ArrowArray, FieldVector, DictionaryProvider, boolean)} importIntoVector(allocator, array, vector, provider, true)}.
284321
*
285322
* @param allocator Buffer allocator
286323
* @param array C data interface struct holding the array data
287324
* @param vector Imported vector object [out]
288325
* @param provider Dictionary provider to load dictionary vectors to (optional)
326+
* @see #importIntoVector(BufferAllocator, ArrowArray, FieldVector, DictionaryProvider, boolean)
289327
*/
290328
public static void importIntoVector(
291329
BufferAllocator allocator,
292330
ArrowArray array,
293331
FieldVector vector,
294332
DictionaryProvider provider) {
333+
importIntoVector(allocator, array, vector, provider, true);
334+
}
335+
336+
/**
337+
* Import Java vector from the C data interface.
338+
*
339+
* <p>On successful completion, the ArrowArray struct will have been moved (as per the C data interface specification)
340+
* to a private object held alive by the resulting array.
341+
*
342+
* @param allocator Buffer allocator
343+
* @param array C data interface struct holding the array data
344+
* @param vector Imported vector object [out]
345+
* @param provider Dictionary provider to load dictionary vectors to (optional)
346+
* @param closeImportedStructs if true, the ArrowArray struct will be closed when this method completes successfully.
347+
*/
348+
public static void importIntoVector(
349+
BufferAllocator allocator,
350+
ArrowArray array,
351+
FieldVector vector,
352+
DictionaryProvider provider,
353+
boolean closeImportedStructs) {
295354
ArrayImporter importer = new ArrayImporter(allocator, vector, provider);
296355
importer.importArray(array);
356+
if (closeImportedStructs) {
357+
array.close();
358+
}
359+
}
360+
361+
/**
362+
* Equivalent to calling {@link #importVector(BufferAllocator, ArrowArray, ArrowSchema, CDataDictionaryProvider, boolean) importVector(allocator, array, schema, provider, true)}
363+
*
364+
* @param allocator Buffer allocator for allocating the output FieldVector
365+
* @param array C data interface struct holding the array data
366+
* @param schema C data interface struct holding the array type
367+
* @param provider Dictionary provider to load dictionary vectors to (optional)
368+
* @return Imported vector object
369+
* @see #importVector(BufferAllocator, ArrowArray, ArrowSchema, CDataDictionaryProvider, boolean)
370+
*/
371+
public static FieldVector importVector(
372+
BufferAllocator allocator,
373+
ArrowArray array,
374+
ArrowSchema schema,
375+
CDataDictionaryProvider provider) {
376+
return importVector(allocator, array, schema, provider, true);
297377
}
298378

299379
/**
@@ -307,19 +387,39 @@ public static void importIntoVector(
307387
* @param array C data interface struct holding the array data
308388
* @param schema C data interface struct holding the array type
309389
* @param provider Dictionary provider to load dictionary vectors to (optional)
390+
* @param closeImportedStructs if true, the ArrowArray struct will be closed when this method completes successfully and
391+
* the ArrowSchema struct will be always be closed.
310392
* @return Imported vector object
311393
*/
312394
public static FieldVector importVector(
313-
BufferAllocator allocator,
314-
ArrowArray array,
315-
ArrowSchema schema,
316-
CDataDictionaryProvider provider) {
317-
Field field = importField(allocator, schema, provider);
395+
BufferAllocator allocator,
396+
ArrowArray array,
397+
ArrowSchema schema,
398+
CDataDictionaryProvider provider,
399+
boolean closeImportedStructs) {
400+
Field field = importField(allocator, schema, provider, closeImportedStructs);
318401
FieldVector vector = field.createVector(allocator);
319-
importIntoVector(allocator, array, vector, provider);
402+
importIntoVector(allocator, array, vector, provider, closeImportedStructs);
320403
return vector;
321404
}
322405

406+
/**
407+
* Equivalent to calling {@link #importIntoVectorSchemaRoot(BufferAllocator, ArrowArray, VectorSchemaRoot, DictionaryProvider, boolean) importIntoVectorSchemaRoot(allocator, array, root, provider, true)}.
408+
*
409+
* @param allocator Buffer allocator
410+
* @param array C data interface struct holding the record batch data
411+
* @param root vector schema root to load into
412+
* @param provider Dictionary provider to load dictionary vectors to (optional)
413+
* @see #importIntoVectorSchemaRoot(BufferAllocator, ArrowArray, VectorSchemaRoot, DictionaryProvider, boolean)
414+
*/
415+
public static void importIntoVectorSchemaRoot(
416+
BufferAllocator allocator,
417+
ArrowArray array,
418+
VectorSchemaRoot root,
419+
DictionaryProvider provider) {
420+
importIntoVectorSchemaRoot(allocator, array, root, provider, true);
421+
}
422+
323423
/**
324424
* Import record batch from the C data interface into vector schema root.
325425
*
@@ -333,15 +433,17 @@ public static FieldVector importVector(
333433
* @param array C data interface struct holding the record batch data
334434
* @param root vector schema root to load into
335435
* @param provider Dictionary provider to load dictionary vectors to (optional)
436+
* @param closeImportedStructs if true, the ArrowArray struct will be closed when this method completes successfully
336437
*/
337438
public static void importIntoVectorSchemaRoot(
338-
BufferAllocator allocator,
339-
ArrowArray array,
340-
VectorSchemaRoot root,
341-
DictionaryProvider provider) {
439+
BufferAllocator allocator,
440+
ArrowArray array,
441+
VectorSchemaRoot root,
442+
DictionaryProvider provider,
443+
boolean closeImportedStructs) {
342444
try (StructVector structVector = StructVector.emptyWithDuplicates("", allocator)) {
343445
structVector.initializeChildrenFromFields(root.getSchema().getFields());
344-
importIntoVector(allocator, array, structVector, provider);
446+
importIntoVector(allocator, array, structVector, provider, closeImportedStructs);
345447
StructVectorUnloader unloader = new StructVectorUnloader(structVector);
346448
VectorLoader loader = new VectorLoader(root);
347449
try (ArrowRecordBatch recordBatch = unloader.getRecordBatch()) {
@@ -350,6 +452,20 @@ public static void importIntoVectorSchemaRoot(
350452
}
351453
}
352454

455+
/**
456+
* Equivalent to calling {@link #importVectorSchemaRoot(BufferAllocator, ArrowSchema, CDataDictionaryProvider, boolean) importVectorSchemaRoot(allocator, schema, provider, true)}.
457+
*
458+
* @param allocator Buffer allocator for allocating the output VectorSchemaRoot
459+
* @param schema C data interface struct holding the record batch schema
460+
* @param provider Dictionary provider to load dictionary vectors to (optional)
461+
* @return Imported vector schema root
462+
* @see #importVectorSchemaRoot(BufferAllocator, ArrowSchema, CDataDictionaryProvider, boolean)
463+
*/
464+
public static VectorSchemaRoot importVectorSchemaRoot(
465+
BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) {
466+
return importVectorSchemaRoot(allocator, schema, provider, true);
467+
}
468+
353469
/**
354470
* Import Java vector schema root from a C data interface Schema.
355471
*
@@ -360,11 +476,33 @@ public static void importIntoVectorSchemaRoot(
360476
* @param allocator Buffer allocator for allocating the output VectorSchemaRoot
361477
* @param schema C data interface struct holding the record batch schema
362478
* @param provider Dictionary provider to load dictionary vectors to (optional)
479+
* @param closeImportedStructs if true, the ArrowSchema struct will be closed when this method completes
363480
* @return Imported vector schema root
364481
*/
365482
public static VectorSchemaRoot importVectorSchemaRoot(
366-
BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) {
367-
return importVectorSchemaRoot(allocator, null, schema, provider);
483+
BufferAllocator allocator,
484+
ArrowSchema schema,
485+
CDataDictionaryProvider provider,
486+
boolean closeImportedStructs) {
487+
return importVectorSchemaRoot(allocator, null, schema, provider, closeImportedStructs);
488+
}
489+
490+
/**
491+
* Equivalent to calling {@link #importVectorSchemaRoot(BufferAllocator, ArrowArray, ArrowSchema, CDataDictionaryProvider, boolean) importVectorSchemaRoot(allocator, array, schema, provider, true)}.
492+
*
493+
* @param allocator Buffer allocator for allocating the output VectorSchemaRoot
494+
* @param array C data interface struct holding the record batch data (optional)
495+
* @param schema C data interface struct holding the record batch schema
496+
* @param provider Dictionary provider to load dictionary vectors to (optional)
497+
* @return Imported vector schema root
498+
* @see #importVectorSchemaRoot(BufferAllocator, ArrowArray, ArrowSchema, CDataDictionaryProvider, boolean)
499+
*/
500+
public static VectorSchemaRoot importVectorSchemaRoot(
501+
BufferAllocator allocator,
502+
ArrowArray array,
503+
ArrowSchema schema,
504+
CDataDictionaryProvider provider) {
505+
return importVectorSchemaRoot(allocator, array, schema, provider, true);
368506
}
369507

370508
/**
@@ -383,29 +521,52 @@ public static VectorSchemaRoot importVectorSchemaRoot(
383521
* @param array C data interface struct holding the record batch data (optional)
384522
* @param schema C data interface struct holding the record batch schema
385523
* @param provider Dictionary provider to load dictionary vectors to (optional)
524+
* @param closeImportedStructs if true, the ArrowArray struct will be closed when this method completes successfully and
525+
* the ArrowSchema struct will be always be closed.
386526
* @return Imported vector schema root
387527
*/
388528
public static VectorSchemaRoot importVectorSchemaRoot(
389-
BufferAllocator allocator,
390-
ArrowArray array,
391-
ArrowSchema schema,
392-
CDataDictionaryProvider provider) {
529+
BufferAllocator allocator,
530+
ArrowArray array,
531+
ArrowSchema schema,
532+
CDataDictionaryProvider provider,
533+
boolean closeImportedStructs) {
393534
VectorSchemaRoot vsr =
394-
VectorSchemaRoot.create(importSchema(allocator, schema, provider), allocator);
535+
VectorSchemaRoot.create(importSchema(allocator, schema, provider, closeImportedStructs), allocator);
395536
if (array != null) {
396-
importIntoVectorSchemaRoot(allocator, array, vsr, provider);
537+
importIntoVectorSchemaRoot(allocator, array, vsr, provider, closeImportedStructs);
397538
}
398539
return vsr;
399540
}
400541

401542
/**
402-
* Import an ArrowArrayStream as an {@link ArrowReader}.
543+
* Equivalent to calling {@link ##importArrayStream(BufferAllocator, ArrowArrayStream, boolean) importArrayStream(allocator, stream, true)}.
403544
*
404545
* @param allocator Buffer allocator for allocating the output data.
405546
* @param stream C stream interface struct to import.
406547
* @return Imported reader
548+
* @see #importArrayStream(BufferAllocator, ArrowArrayStream, boolean)
407549
*/
408550
public static ArrowReader importArrayStream(BufferAllocator allocator, ArrowArrayStream stream) {
409-
return new ArrowArrayStreamReader(allocator, stream);
551+
return importArrayStream(allocator, stream, true);
552+
}
553+
554+
/**
555+
* Import an ArrowArrayStream as an {@link ArrowReader}.
556+
*
557+
* <p>On successful completion, the ArrowArrayStream struct will have been moved (as per the C data interface specification)
558+
* to a private object held alive by the resulting ArrowReader.
559+
*
560+
* @param allocator Buffer allocator for allocating the output data.
561+
* @param stream C stream interface struct to import.
562+
* @param closeImportedStructs if true, the ArrowArrayStream struct will be closed when this method completes successfully
563+
* @return Imported reader
564+
*/
565+
public static ArrowReader importArrayStream(BufferAllocator allocator, ArrowArrayStream stream, boolean closeImportedStructs) {
566+
ArrowArrayStreamReader reader = new ArrowArrayStreamReader(allocator, stream);
567+
if (closeImportedStructs) {
568+
stream.close();
569+
}
570+
return reader;
410571
}
411572
}

0 commit comments

Comments
 (0)