Deleting

TileDB supports deletion using Query Conditions to specify which cells to remove. When a delete query is committed, any cells matching the given condition are ignored on subsequent reads.

#include <tiledb/tiledb.h>

const char* array_name = "deletes_array";

// Create TileDB context.
tiledb_ctx_t* ctx;
tiledb_ctx_alloc(NULL, &ctx);

// Create the array.
int32_t rows_domain[] = {1, 6};
int32_t tile_extents[] = {2};
tiledb_dimension_t* d;
tiledb_dimension_alloc(
    ctx, "cols", TILEDB_INT32, &rows_domain[0], &tile_extents[0], &d);
tiledb_domain_t* domain;
tiledb_domain_alloc(ctx, &domain);
tiledb_domain_add_dimension(ctx, domain, d);
tiledb_attribute_t* a;
tiledb_attribute_alloc(ctx, "a1", TILEDB_INT32, &a);
tiledb_array_schema_t* array_schema;
tiledb_array_schema_alloc(ctx, TILEDB_SPARSE, &array_schema);
tiledb_array_schema_set_domain(ctx, array_schema, domain);
tiledb_array_schema_add_attribute(ctx, array_schema, a);
tiledb_array_create(ctx, array_name, array_schema);

// Write initial data to the array.
tiledb_array_t* array;
tiledb_array_alloc(ctx, array_name, &array);
tiledb_array_open(ctx, array, TILEDB_WRITE);
tiledb_query_t* write_query;
tiledb_query_alloc(ctx, array, TILEDB_WRITE, &write_query);
int32_t cols_data[] = {1, 2, 3, 4, 5, 6};
uint64_t cols_size = sizeof(cols_data);
int32_t a1_data[] = {1, 2, 3, 4, 5, 6};
uint64_t a1_size = sizeof(a1_data);
tiledb_query_set_layout(ctx, write_query, TILEDB_UNORDERED);
tiledb_query_set_data_buffer(ctx, write_query, "cols", cols_data, &cols_size);
tiledb_query_set_data_buffer(ctx, write_query, "a1", a1_data, &a1_size);
tiledb_query_submit(ctx, write_query);
tiledb_array_close(ctx, array);

// Delete cells where a1 attribute value is < 4
tiledb_array_open(ctx, array, TILEDB_DELETE);
tiledb_query_t* delete_query;
tiledb_query_alloc(ctx, array, TILEDB_DELETE, &delete_query);
tiledb_query_condition_t* qc;
tiledb_query_condition_alloc(ctx, &qc);
int32_t val = 4;
tiledb_query_condition_init(ctx, qc, "a1", &val, sizeof(val), TILEDB_LT);
tiledb_query_set_condition(ctx, delete_query, qc);
tiledb_query_submit(ctx, delete_query);
tiledb_array_close(ctx, array);

// Read data back from the array.
tiledb_array_open(ctx, array, TILEDB_READ);
tiledb_query_t* read_query;
tiledb_query_alloc(ctx, array, TILEDB_READ, &read_query);
int32_t cols_data_read[6] = {0, 0, 0, 0, 0, 0};
uint64_t cols_size_read = sizeof(int32_t) * 6;
int32_t a1_data_read[6] = {0, 0, 0, 0, 0, 0};
uint64_t a1_size_read = sizeof(int32_t) * 6;
int32_t range[] = {1, 6};
tiledb_subarray_t* subarray;
tiledb_subarray_alloc(ctx, array, &subarray);
tiledb_subarray_add_range(ctx, subarray, 0, &range[0], &range[1], NULL);
tiledb_query_set_layout(ctx, read_query, TILEDB_UNORDERED);
tiledb_query_set_subarray_t(ctx, read_query, subarray);
tiledb_query_set_data_buffer(
    ctx, read_query, "cols", cols_data_read, &cols_size_read);
tiledb_query_set_data_buffer(
    ctx, read_query, "a1", a1_data_read, &a1_size_read);
tiledb_query_submit(ctx, read_query);
tiledb_array_close(ctx, array);

// Output: 4, 5, 6, 0, 0, 0
for (size_t i = 0; i < 5; i++) {
  printf("%d, ", a1_data_read[i]);
}
printf("%d\n", a1_data_read[5]);

// Clean up allocated objects
tiledb_attribute_free(&a);
tiledb_dimension_free(&d);
tiledb_domain_free(&domain);
tiledb_array_schema_free(&array_schema);
tiledb_array_free(&array);
tiledb_query_free(&write_query);
tiledb_query_free(&delete_query);
tiledb_query_free(&read_query);
tiledb_subarray_free(&subarray);

Last updated