Nullable Attributes

Fixed-length, nullable attributes

Writing nullable, fixed-length attribute cells to an array involves populating a data buffer and a validity buffer. This is illustrated in the code block below with a sparse write, but it is applicable to dense writes as well.

#include <tiledb/tiledb.h>

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

// Open array for writing
tiledb_array_t* array;
tiledb_array_alloc(ctx, array_name, &array);
tiledb_array_open(ctx, array, TILEDB_WRITE);

// Prepare some data for the array
long long d1[] = {1, 2, 3, 4};
unsigned long long d1_size = sizeof(d1);
long long d2[] = {2, 1, 3, 4};
unsigned long long d2_size = sizeof(d2);
float a[] = {1.1f, 1.2f, 2.1f, 2.2f, 3.1f, 3.2f, 4.1f, 4.2f};
unsigned long long a_size = sizeof(a);
uint8_t a_validity[] = {1, 1, 0, 1};
unsigned long long a_validity_size = sizeof(a_validity);

// Create the query
tiledb_query_t* query;
tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
tiledb_query_set_layout(ctx, query, TILEDB_UNORDERED);
tiledb_query_set_data_buffer(ctx, query, "d1", d1, &d1_size);
tiledb_query_set_data_buffer(ctx, query, "d2", d2, &d2_size);
tiledb_query_set_data_buffer(ctx, query, "a", a, &a_size);
tiledb_query_set_validity_buffer(
    ctx, query, "a", a_validity, &a_validity_size);

// Submit query
tiledb_query_submit(ctx, query);

// Close array
tiledb_array_close(ctx, array);

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

The code snippet above produces the following sparse fragment.

Variable-length, Nullable Attributes

Writing variable-length attribute values to an array involves passing three buffers to TileDB, one for the variable-length cell values, one for the starting offset of each value in the first buffer, and one for the cell validity values. We illustrate this in the code block below with a sparse write, but it is also applicable for dense writes.

#include <tiledb/tiledb.h>

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

// Open array for writing
tiledb_array_t* array;
tiledb_array_alloc(ctx, array_name, &array);
tiledb_array_open(ctx, array, TILEDB_WRITE);

// Prepare some data for the array
long long d1[] = {1, 2, 3, 4};
unsigned long long d1_size = sizeof(d1);
long long d2[] = {2, 1, 3, 4};
unsigned long long d2_size = sizeof(d2);
int data[] = {1, 1, 2, 3, 3, 3, 4};
unsigned long long a_size = sizeof(a);
unsigned long long a_off[] = {0, 8, 12, 24};
unsigned long long a_off_size = sizeof(a_off);
uint8_t a_validity[] = {1, 1, 0, 1};
unsigned long long a_validity_size = sizeof(a_validity);

// Create the query
tiledb_query_t* query;
tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
tiledb_query_set_layout(ctx, query, TILEDB_UNORDERED);
tiledb_query_set_data_buffer(ctx, query, "d1", d1, &d1_size);
tiledb_query_set_data_buffer(ctx, query, "d2", d2, &d2_size);
tiledb_query_set_data_buffer(ctx, query, "a", a, &a_size);
tiledb_query_set_validity_buffer(ctx, query, "a", a_validity, &a_validity_size);

// Submit query
tiledb_query_submit(ctx, query);

// Close array
tiledb_array_close(ctx, array);

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

The code snippet above produces the following sparse fragment.

Last updated