Skip to content

Commit 5a81aba

Browse files
Store object references when setting column data until write is submitted (#4350)
1 parent b8a91dd commit 5a81aba

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

apis/python/src/tiledbsoma/_dense_nd_array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ def write(
366366
mq = ManagedQuery(self, platform_config)
367367
mq._handle.set_layout(order)
368368
mq.set_coords(new_coords)
369-
mq._handle.set_column_data("soma_data", input)
370-
mq._handle.submit_write()
369+
mq.set_column_data("soma_data", input)
370+
mq.submit_write()
371371
mq._handle.finalize()
372372

373373
tiledb_write_options = TileDBWriteOptions.from_platform_config(platform_config)

apis/python/src/tiledbsoma/_managed_query.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ManagedQuery:
3030
_array: SOMAArray
3131
_platform_config: options.PlatformConfig | None = None
3232
_handle: clib.ManagedQuery = attrs.field(init=False)
33+
_ref_store: list[object] = attrs.field(init=False, default=[])
3334

3435
def __attrs_post_init__(self) -> None:
3536
array_handle = self._array._handle
@@ -218,3 +219,25 @@ def set_geometry_coord(
218219
column.set_dim_points_double_array(self._handle, [coord])
219220
else:
220221
raise ValueError(f"Unsupported spatial coordinate type. Expected slice or float, found {type(coord)}")
222+
223+
def set_column_data(self, dim_name: str, data: np.typing.NDArray) -> None:
224+
# store a reference to the data being written
225+
# libtiledbsoma will try not to copy any data to temporary buffers when writing data but the user is free
226+
# to pass temporary data objects to write. Submitting the write buffer to TileDB can happen after the
227+
# lifespan of the temporary object so ManagedQuery need to preserve a reference to each data object until
228+
# the query is submitted
229+
self._ref_store.append(data)
230+
231+
self._handle.set_column_data(dim_name, data)
232+
233+
def submit_write(self) -> None:
234+
self._handle.submit_write()
235+
236+
# clear stored data objects
237+
self._ref_store.clear()
238+
239+
def submit_and_finalize(self) -> None:
240+
self._handle.submit_and_finalize()
241+
242+
# clear stored data objects
243+
self._ref_store.clear()

apis/python/src/tiledbsoma/_sparse_nd_array.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,23 +389,23 @@ def write(
389389
mq._handle.set_layout(layout)
390390

391391
for i, c in enumerate(coords.T):
392-
mq._handle.set_column_data(
392+
mq.set_column_data(
393393
f"soma_dim_{i}",
394394
np.array(
395395
c,
396396
dtype=self.schema.field(f"soma_dim_{i}").type.to_pandas_dtype(),
397397
),
398398
)
399-
mq._handle.set_column_data(
399+
mq.set_column_data(
400400
"soma_data",
401401
np.array(data, dtype=self.schema.field("soma_data").type.to_pandas_dtype()),
402402
)
403403

404404
if layout == clib.ResultOrder.unordered:
405-
mq._handle.submit_write()
405+
mq.submit_write()
406406
mq._handle.finalize()
407407
else:
408-
mq._handle.submit_and_finalize()
408+
mq.submit_and_finalize()
409409

410410
if write_options.consolidate_and_vacuum:
411411
# Consolidate non-bulk data
@@ -425,23 +425,23 @@ def write(
425425
mq._handle.set_layout(layout)
426426

427427
for i, c in enumerate([sp.row, sp.col]):
428-
mq._handle.set_column_data(
428+
mq.set_column_data(
429429
f"soma_dim_{i}",
430430
np.array(
431431
c,
432432
dtype=self.schema.field(f"soma_dim_{i}").type.to_pandas_dtype(),
433433
),
434434
)
435-
mq._handle.set_column_data(
435+
mq.set_column_data(
436436
"soma_data",
437437
np.array(sp.data, dtype=self.schema.field("soma_data").type.to_pandas_dtype()),
438438
)
439439

440440
if layout == clib.ResultOrder.unordered:
441-
mq._handle.submit_write()
441+
mq.submit_write()
442442
mq._handle.finalize()
443443
else:
444-
mq._handle.submit_and_finalize()
444+
mq.submit_and_finalize()
445445

446446
if write_options.consolidate_and_vacuum:
447447
# Consolidate non-bulk data

apis/python/src/tiledbsoma/managed_query.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void load_managed_query(py::module& m) {
155155
py::gil_scoped_release release;
156156
try {
157157
mq.setup_write_column(
158-
name, data.size(), (const void*)data_info.ptr, static_cast<uint64_t*>(nullptr), nullptr, true);
158+
name, data.size(), (const void*)data_info.ptr, static_cast<uint64_t*>(nullptr), nullptr, false);
159159
} catch (const std::exception& e) {
160160
TPY_ERROR_LOC(e.what());
161161
}

0 commit comments

Comments
 (0)