Skip to content

Commit eb95922

Browse files
author
Vivian Nguyen
authored
ArraySchemaEvolution Check Context Last Error (#1335)
1 parent 96bb287 commit eb95922

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## API Changes
44
* Addition of `Array.upgrade_version` to upgrade array to latest version [#1334](https://github.com/TileDB-Inc/TileDB-Py/pull/1334)
55
* Attributes in query conditions no longer need to be passed to `Array.query`'s `attr` arg [#1333](https://github.com/TileDB-Inc/TileDB-Py/pull/1333)
6+
* `ArraySchemaEvolution` checks context's last error for error message [#1335](https://github.com/TileDB-Inc/TileDB-Py/pull/1335)
67

78
# TileDB-Py 0.17.4 Release Notes
89

tiledb/schema_evolution.cc

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <pybind11/pybind11.h>
2-
//#include <tiledb/tiledb_experimental>
32
#include <tiledb/tiledb.h>
43
#include <tiledb/tiledb_experimental.h>
54

@@ -17,6 +16,37 @@ typedef struct {
1716

1817
using ArraySchemaEvolution = PyArraySchemaEvolution;
1918

19+
void _throw_tiledb_error(tiledb_error_t *err_ptr) {
20+
const char *err_msg_ptr = NULL;
21+
int ret = tiledb_error_message(err_ptr, &err_msg_ptr);
22+
if (ret != TILEDB_OK) {
23+
tiledb_error_free(&err_ptr);
24+
if (ret == TILEDB_OOM) {
25+
throw std::bad_alloc();
26+
}
27+
TPY_ERROR_LOC("error retrieving error message");
28+
}
29+
30+
TPY_ERROR_LOC(std::string(err_msg_ptr));
31+
}
32+
33+
void _throw_ctx_err(tiledb_ctx_t *ctx_ptr, int rc) {
34+
if (rc == TILEDB_OK)
35+
return;
36+
if (rc == TILEDB_OOM)
37+
throw std::bad_alloc();
38+
39+
tiledb_error_t *err_ptr = NULL;
40+
int ret = tiledb_ctx_get_last_error(ctx_ptr, &err_ptr);
41+
if (ret != TILEDB_OK) {
42+
tiledb_error_free(&err_ptr);
43+
if (ret == TILEDB_OOM)
44+
throw std::bad_alloc();
45+
TPY_ERROR_LOC("error retrieving error object from ctx");
46+
}
47+
_throw_tiledb_error(err_ptr);
48+
}
49+
2050
void init_schema_evolution(py::module &m) {
2151
py::class_<ArraySchemaEvolution>(m, "ArraySchemaEvolution")
2252
.def(py::init([](py::object ctx_py) {
@@ -27,7 +57,7 @@ void init_schema_evolution(py::module &m) {
2757
tiledb_array_schema_evolution_t *evol_p;
2858
int rc = tiledb_array_schema_evolution_alloc(ctx_c, &evol_p);
2959
if (rc != TILEDB_OK) {
30-
TPY_ERROR_LOC("Failed to allocate ArraySchemaEvolution");
60+
_throw_ctx_err(ctx_c, rc);
3161
}
3262

3363
return new PyArraySchemaEvolution({ctx_c, evol_p});
@@ -42,22 +72,21 @@ void init_schema_evolution(py::module &m) {
4272
int rc = tiledb_array_schema_evolution_add_attribute(
4373
inst.ctx_, inst.evol_, attr_c);
4474
if (rc != TILEDB_OK) {
45-
TPY_ERROR_LOC("Failed to add attribute to ArraySchemaEvolution");
75+
_throw_ctx_err(inst.ctx_, rc);
4676
}
4777
})
4878
.def("drop_attribute",
4979
[](ArraySchemaEvolution &inst, std::string attr_name) {
5080
int rc = tiledb_array_schema_evolution_drop_attribute(
5181
inst.ctx_, inst.evol_, attr_name.c_str());
5282
if (rc != TILEDB_OK) {
53-
TPY_ERROR_LOC(
54-
"Failed to drop attribute from ArraySchemaEvolution");
83+
_throw_ctx_err(inst.ctx_, rc);
5584
}
5685
})
5786
.def("array_evolve", [](ArraySchemaEvolution &inst, std::string uri) {
5887
int rc = tiledb_array_evolve(inst.ctx_, uri.c_str(), inst.evol_);
5988
if (rc != TILEDB_OK) {
60-
TPY_ERROR_LOC("Failed to drop attribute from ArraySchemaEvolution");
89+
_throw_ctx_err(inst.ctx_, rc);
6190
}
6291
});
6392
}

tiledb/tests/test_schema_evolution.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def test_schema_evolution(tmp_path):
3636

3737
newattr = tiledb.Attr("a3", dtype=np.int8)
3838
se.add_attribute(newattr)
39+
40+
with pytest.raises(tiledb.TileDBError) as excinfo:
41+
se.add_attribute(newattr)
42+
assert "Input attribute name is already there" in str(excinfo.value)
43+
3944
se.array_evolve(uri)
4045

4146
data2 = {

0 commit comments

Comments
 (0)