Skip to content

Commit

Permalink
Added checks when setting the subarray in sparse writes. Closes #812
Browse files Browse the repository at this point in the history
PR #843

(cherry picked from commit df6338d)
  • Loading branch information
stavrospapadopoulos authored and jakebolewski committed Jul 25, 2018
1 parent 106a308 commit 4071680
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
49 changes: 49 additions & 0 deletions test/src/unit-capi-dense_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3440,3 +3440,52 @@ TEST_CASE_METHOD(

remove_temp_dir(temp_dir);
}

TEST_CASE_METHOD(
DenseArrayFx,
"C API: Test sparse array, set subarray in sparse writes should error",
"[capi], [dense], [dense-set-subarray-sparse]") {
std::string array_name =
FILE_URI_PREFIX + FILE_TEMP_DIR + "dense_set_subarray_sparse";
std::string temp_dir = FILE_URI_PREFIX + FILE_TEMP_DIR;
create_temp_dir(temp_dir);
create_dense_array(array_name);

// Create TileDB context
tiledb_ctx_t* ctx = nullptr;
REQUIRE(tiledb_ctx_alloc(nullptr, &ctx) == TILEDB_OK);

// Open array
tiledb_array_t* array;
int rc = tiledb_array_alloc(ctx, array_name.c_str(), &array);
CHECK(rc == TILEDB_OK);
rc = tiledb_array_open(ctx, array, TILEDB_WRITE);
CHECK(rc == TILEDB_OK);

// Create WRITE query
tiledb_query_t* query;
rc = tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
CHECK(rc == TILEDB_OK);

uint64_t subarray[] = {1, 1, 1, 1};

// Set some subarray BEFORE setting the layout to UNORDERED
rc = tiledb_query_set_subarray(ctx, query, subarray);
CHECK(rc == TILEDB_OK);

// Set some subarray AFTER setting the layout to UNORDERED
rc = tiledb_query_set_layout(ctx, query, TILEDB_UNORDERED);
CHECK(rc == TILEDB_OK);
rc = tiledb_query_set_subarray(ctx, query, subarray);
CHECK(rc == TILEDB_ERR);

// Close array
CHECK(tiledb_array_close(ctx, array) == TILEDB_OK);

// Clean up
tiledb_query_free(&query);
tiledb_array_free(&array);
tiledb_ctx_free(&ctx);

remove_temp_dir(temp_dir);
}
40 changes: 39 additions & 1 deletion test/src/unit-capi-sparse_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ void SparseArrayFx::check_sparse_array_unordered_with_duplicates_error(

void SparseArrayFx::check_sparse_array_unordered_with_duplicates_no_check(
const std::string& array_name) {
// Create TileDB context, setting
// Create TileDB context with config
tiledb_config_t* config = nullptr;
tiledb_error_t* error = nullptr;
REQUIRE(tiledb_config_alloc(&config, &error) == TILEDB_OK);
Expand Down Expand Up @@ -2335,3 +2335,41 @@ TEST_CASE_METHOD(
write_sparse_array_missing_attributes(array_name);
check_sparse_array_no_results(array_name);
}

TEST_CASE_METHOD(
SparseArrayFx,
"C API: Test sparse array, set subarray should error",
"[capi], [sparse], [sparse-set-subarray]") {
std::string array_name =
FILE_URI_PREFIX + FILE_TEMP_DIR + "sparse_set_subarray";
create_sparse_array(array_name);

// Create TileDB context
tiledb_ctx_t* ctx = nullptr;
REQUIRE(tiledb_ctx_alloc(nullptr, &ctx) == TILEDB_OK);

// Open array
tiledb_array_t* array;
int rc = tiledb_array_alloc(ctx, array_name.c_str(), &array);
CHECK(rc == TILEDB_OK);
rc = tiledb_array_open(ctx, array, TILEDB_WRITE);
CHECK(rc == TILEDB_OK);

// Create WRITE query
tiledb_query_t* query;
rc = tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
CHECK(rc == TILEDB_OK);

// Set some subarray
uint64_t subarray[] = {1, 1, 1, 1};
rc = tiledb_query_set_subarray(ctx, query, subarray);
CHECK(rc == TILEDB_ERR);

// Close array
CHECK(tiledb_array_close(ctx, array) == TILEDB_OK);

// Clean up
tiledb_query_free(&query);
tiledb_array_free(&array);
tiledb_ctx_free(&ctx);
}
8 changes: 8 additions & 0 deletions tiledb/sm/c_api/tiledb.h
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,14 @@ TILEDB_EXPORT int tiledb_query_alloc(
* fix the attributes and layout, but explore different subarrays with
* the same `tiledb_query_t` object (i.e., without having to created
* a new object).
*
* @note Setting the subarray in sparse writes is meaningless and, thus,
* this function will error in the following two cases, provided that
* this is a write query:
* (i) the array is sparse, and (ii) the array is dense and the
* layout has been set to `TILEDB_UNORDERED`. In the second case,
* if the user sets the layout to `TILEDB_UNORDERED` **after**
* the subarray has been set, the subarray will simply be ignored.
*/
TILEDB_EXPORT int tiledb_query_set_subarray(
tiledb_ctx_t* ctx, tiledb_query_t* query, const void* subarray);
Expand Down
11 changes: 11 additions & 0 deletions tiledb/sm/query/writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ void Writer::set_storage_manager(StorageManager* storage_manager) {
}

Status Writer::set_subarray(const void* subarray) {
// Check
if (subarray != nullptr) {
if (!array_schema_->dense()) // Sparse arrays
return LOG_STATUS(Status::WriterError(
"Cannot set subarray when writing to sparse arrays"));
else if (layout_ == Layout::UNORDERED) // Dense arrays
return LOG_STATUS(Status::WriterError(
"Cannot set subarray when performing sparse writes to dense arrays "
"(i.e., when writing in UNORDERED mode)"));
}

// Reset the writer (this will nuke the global write state)
reset();

Expand Down

0 comments on commit 4071680

Please sign in to comment.