Skip to content

Commit 58dbc08

Browse files
authored
Add support for getting a dtype of ndrectangle, (#5229)
Users wrapping the current domain APIs are requesting: `tiledb_datatype_t NDRectangle::range_dtype(const std::string& dim_name)` and `tiledb_datatype_t NDRectangle::range_dtype(uint32_t dim_idx)` (and the CAPI counterparts) so that the underlying TileDB datatype of a range/dimension can be queried closer to the call site of functions like e.g. range<T> where a conversion from tiledb_datatype_t to static c++ type is most of the time needed. [sc-52012] --- TYPE: FEATURE DESC: Add support for getting a dtype of ndrectangle,
1 parent ab707f5 commit 58dbc08

File tree

9 files changed

+212
-0
lines changed

9 files changed

+212
-0
lines changed

examples/c_api/current_domain.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ void print_current_domain() {
143143
*(int*)range.min,
144144
*(int*)range.max);
145145

146+
// Get datatype of range
147+
tiledb_datatype_t dtype;
148+
tiledb_ndrectangle_get_dtype(ctx, ndrect, 0, &dtype);
149+
const char* dtype_str;
150+
tiledb_datatype_to_str(dtype, &dtype_str);
151+
printf("Range 0 dtype: %s\n", dtype_str);
152+
153+
// Get datatype of range by name
154+
tiledb_ndrectangle_get_dtype_from_name(ctx, ndrect, "d1", &dtype);
155+
tiledb_datatype_to_str(dtype, &dtype_str);
156+
printf("Range 0 dtype by name: %s\n", dtype_str);
157+
146158
// Clean up
147159
tiledb_ndrectangle_free(&ndrect);
148160
} else {

examples/cpp_api/current_domain.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ void print_current_domain(Context& ctx) {
113113
// Print the range
114114
std::cout << "Current domain range: [" << range[0] << ", " << range[1] << "]"
115115
<< std::endl;
116+
117+
// Print datatype of range 0
118+
std::cout << "Current domain range 0 datatype: "
119+
<< tiledb::impl::type_to_str(ndrect.range_dtype(0)) << std::endl;
120+
121+
// Print datatype of range d1
122+
std::cout << "Current domain range 0 datatype: "
123+
<< tiledb::impl::type_to_str(ndrect.range_dtype("d1")) << std::endl;
116124
}
117125

118126
void expand_current_domain(Context& ctx) {

test/src/test-cppapi-current-domain.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ TEST_CASE_METHOD(
9797
range = rect.range<int>(1);
9898
CHECK(range[0] == 30);
9999
CHECK(range[1] == 40);
100+
101+
// CHECK range dtype
102+
CHECK(ndrect.range_dtype(0) == TILEDB_INT32);
103+
CHECK(ndrect.range_dtype("x") == TILEDB_INT32);
100104
}
101105

102106
TEST_CASE_METHOD(

tiledb/api/c_api/ndrectangle/ndrectangle_api.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,36 @@ capi_return_t tiledb_ndrectangle_set_range(
177177
return TILEDB_OK;
178178
}
179179

180+
capi_return_t tiledb_ndrectangle_get_dtype(
181+
tiledb_ctx_t* ctx,
182+
tiledb_ndrectangle_t* ndr,
183+
uint32_t idx,
184+
tiledb_datatype_t* type) {
185+
ensure_context_is_valid(ctx);
186+
ensure_handle_is_valid(ndr);
187+
ensure_output_pointer_is_valid(type);
188+
189+
*type = static_cast<tiledb_datatype_t>(ndr->ndrectangle()->range_dtype(idx));
190+
191+
return TILEDB_OK;
192+
}
193+
194+
capi_return_t tiledb_ndrectangle_get_dtype_from_name(
195+
tiledb_ctx_t* ctx,
196+
tiledb_ndrectangle_t* ndr,
197+
const char* name,
198+
tiledb_datatype_t* type) {
199+
ensure_context_is_valid(ctx);
200+
ensure_handle_is_valid(ndr);
201+
ensure_dim_name_is_valid(name);
202+
ensure_output_pointer_is_valid(type);
203+
204+
*type = static_cast<tiledb_datatype_t>(
205+
ndr->ndrectangle()->range_dtype_for_name(name));
206+
207+
return TILEDB_OK;
208+
}
209+
180210
} // namespace tiledb::api
181211

182212
using tiledb::api::api_entry_plain;
@@ -236,3 +266,24 @@ CAPI_INTERFACE(
236266
return api_entry_with_context<tiledb::api::tiledb_ndrectangle_set_range>(
237267
ctx, ndr, idx, range);
238268
}
269+
270+
CAPI_INTERFACE(
271+
ndrectangle_get_dtype,
272+
tiledb_ctx_t* ctx,
273+
tiledb_ndrectangle_t* ndr,
274+
uint32_t idx,
275+
tiledb_datatype_t* type) {
276+
return api_entry_with_context<tiledb::api::tiledb_ndrectangle_get_dtype>(
277+
ctx, ndr, idx, type);
278+
}
279+
280+
CAPI_INTERFACE(
281+
ndrectangle_get_dtype_from_name,
282+
tiledb_ctx_t* ctx,
283+
tiledb_ndrectangle_t* ndr,
284+
const char* name,
285+
tiledb_datatype_t* type) {
286+
return api_entry_with_context<
287+
tiledb::api::tiledb_ndrectangle_get_dtype_from_name>(
288+
ctx, ndr, name, type);
289+
}

tiledb/api/c_api/ndrectangle/ndrectangle_api_external_experimental.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,52 @@ TILEDB_EXPORT capi_return_t tiledb_ndrectangle_set_range(
198198
uint32_t idx,
199199
tiledb_range_t* range) TILEDB_NOEXCEPT;
200200

201+
/**
202+
* Get the TileDB datatype for dimension at idx from
203+
* the N-dimensional rectangle passed as argument
204+
*
205+
* **Example:**
206+
*
207+
* @code{.c}
208+
* tiledb_datatype_t type;
209+
* tiledb_ndrectangle_get_dtype(ctx, ndr, 1, &type);
210+
* @endcode
211+
*
212+
* @param ctx The TileDB context
213+
* @param ndr The n-dimensional rectangle to be queried
214+
* @param idx The index of the dimension
215+
* @param type The datatype to be returned
216+
* @return `TILEDB_OK` for success and `TILEDB_ERR` for error.
217+
*/
218+
TILEDB_EXPORT capi_return_t tiledb_ndrectangle_get_dtype(
219+
tiledb_ctx_t* ctx,
220+
tiledb_ndrectangle_t* ndr,
221+
uint32_t idx,
222+
tiledb_datatype_t* type) TILEDB_NOEXCEPT;
223+
224+
/**
225+
* Get the TileDB datatype for dimension name from
226+
* the N-dimensional rectangle passed as argument
227+
*
228+
* **Example:**
229+
*
230+
* @code{.c}
231+
* tiledb_datatype_t type;
232+
* tiledb_ndrectangle_get_dtype_from_name(ctx, ndr, "dim1", &type);
233+
* @endcode
234+
*
235+
* @param ctx The TileDB context
236+
* @param ndr The n-dimensional rectangle to be queried
237+
* @param name The dimension name
238+
* @param type The datatype to be returned
239+
* @return `TILEDB_OK` for success and `TILEDB_ERR` for error.
240+
*/
241+
TILEDB_EXPORT capi_return_t tiledb_ndrectangle_get_dtype_from_name(
242+
tiledb_ctx_t* ctx,
243+
tiledb_ndrectangle_t* ndr,
244+
const char* name,
245+
tiledb_datatype_t* type) TILEDB_NOEXCEPT;
246+
201247
#ifdef __cplusplus
202248
}
203249
#endif

tiledb/api/c_api/ndrectangle/test/unit_capi_ndrectangle.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,27 @@ TEST_CASE_METHOD(
144144
tiledb_ndrectangle_set_range_for_name(ctx, ndr, "doesntexist", &range) ==
145145
TILEDB_ERR);
146146

147+
CHECK(
148+
tiledb_ndrectangle_get_dtype(nullptr, nullptr, 0, nullptr) ==
149+
TILEDB_INVALID_CONTEXT);
150+
CHECK(tiledb_ndrectangle_get_dtype(ctx, nullptr, 0, nullptr) == TILEDB_ERR);
151+
CHECK(tiledb_ndrectangle_get_dtype(ctx, ndr, 0, nullptr) == TILEDB_ERR);
152+
tiledb_datatype_t dtype;
153+
CHECK(tiledb_ndrectangle_get_dtype(ctx, ndr, 2, &dtype) == TILEDB_ERR);
154+
155+
CHECK(
156+
tiledb_ndrectangle_get_dtype_from_name(
157+
nullptr, nullptr, "dim1", nullptr) == TILEDB_INVALID_CONTEXT);
158+
CHECK(
159+
tiledb_ndrectangle_get_dtype_from_name(ctx, nullptr, "dim1", nullptr) ==
160+
TILEDB_ERR);
161+
CHECK(
162+
tiledb_ndrectangle_get_dtype_from_name(ctx, ndr, "dim1", nullptr) ==
163+
TILEDB_ERR);
164+
CHECK(
165+
tiledb_ndrectangle_get_dtype_from_name(ctx, ndr, "doesntexist", &dtype) ==
166+
TILEDB_ERR);
167+
147168
REQUIRE(tiledb_ndrectangle_free(&ndr) == TILEDB_OK);
148169
}
149170

@@ -186,5 +207,13 @@ TEST_CASE_METHOD(
186207
CHECK(std::memcmp(range.min, out_range_d2.min, range.min_size) == 0);
187208
CHECK(std::memcmp(range.max, out_range_d2.max, range.max_size) == 0);
188209

210+
tiledb_datatype_t dtype;
211+
REQUIRE(tiledb_ndrectangle_get_dtype(ctx, ndr, 0, &dtype) == TILEDB_OK);
212+
CHECK(dtype == TILEDB_UINT64);
213+
REQUIRE(
214+
tiledb_ndrectangle_get_dtype_from_name(ctx, ndr, "d1", &dtype) ==
215+
TILEDB_OK);
216+
CHECK(dtype == TILEDB_UINT64);
217+
189218
REQUIRE(tiledb_ndrectangle_free(&ndr) == TILEDB_OK);
190219
}

tiledb/sm/array_schema/ndrectangle.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ const Range& NDRectangle::get_range_for_name(const std::string& name) const {
167167
return get_range(idx);
168168
}
169169

170+
Datatype NDRectangle::range_dtype(uint32_t idx) const {
171+
if (idx >= range_data_.size()) {
172+
throw std::logic_error(
173+
"The index does not correspond to a valid dimension in the "
174+
"NDRectangle");
175+
}
176+
return domain()->dimension_ptr(idx)->type();
177+
}
178+
179+
Datatype NDRectangle::range_dtype_for_name(const std::string& name) const {
180+
auto idx = domain()->get_dimension_index(name);
181+
return range_dtype(idx);
182+
}
183+
170184
} // namespace tiledb::sm
171185

172186
std::ostream& operator<<(std::ostream& os, const tiledb::sm::NDRectangle& ndr) {

tiledb/sm/array_schema/ndrectangle.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,35 @@ class NDRectangle {
169169
/**
170170
* Get the range for the dimension at idx
171171
*
172+
* @param name The dimension index
172173
* @return The range of the dimension
173174
*/
174175
const Range& get_range(uint32_t idx) const;
175176

176177
/**
177178
* Get the range for the dimension specified by name
178179
*
180+
* @param name The name of the dimension
179181
* @return The range of the dimension
180182
*/
181183
const Range& get_range_for_name(const std::string& name) const;
182184

185+
/**
186+
* Get the data type of the range at idx
187+
*
188+
* @param idx The index of the range
189+
* @return The range datatype
190+
*/
191+
Datatype range_dtype(uint32_t idx) const;
192+
193+
/**
194+
* Get the data type of the range at idx
195+
*
196+
* @param name The dimension name
197+
* @return The range datatype
198+
*/
199+
Datatype range_dtype_for_name(const std::string& name) const;
200+
183201
private:
184202
/* ********************************* */
185203
/* PRIVATE ATTRIBUTES */

tiledb/sm/cpp_api/ndrectangle.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,36 @@ class NDRectangle {
269269
std::shared_ptr<tiledb_ndrectangle_t> ptr() const {
270270
return ndrect_;
271271
}
272+
/**
273+
* Get the data type of the range at idx
274+
*
275+
* @param dim_idx The dimension index.
276+
* @return The datatype of the range.
277+
*/
278+
tiledb_datatype_t range_dtype(unsigned dim_idx) {
279+
auto& ctx = ctx_.get();
280+
281+
tiledb_datatype_t dtype;
282+
ctx.handle_error(tiledb_ndrectangle_get_dtype(
283+
ctx.ptr().get(), ndrect_.get(), dim_idx, &dtype));
284+
285+
return dtype;
286+
}
287+
/**
288+
* Get the data type of the range by name
289+
*
290+
* @param dim_name The dimension name.
291+
* @return The datatype of the range.
292+
*/
293+
tiledb_datatype_t range_dtype(const std::string& dim_name) {
294+
auto& ctx = ctx_.get();
295+
296+
tiledb_datatype_t dtype;
297+
ctx.handle_error(tiledb_ndrectangle_get_dtype_from_name(
298+
ctx.ptr().get(), ndrect_.get(), dim_name.c_str(), &dtype));
299+
300+
return dtype;
301+
}
272302

273303
private:
274304
/* ********************************* */

0 commit comments

Comments
 (0)