Var-length Attributes
Writing variable-length attribute values to an array involves passing two buffers to TileDB, one for the variable-length cell values and one for the starting offset of each value in the first buffer. We illustrate this in the code block below with a sparse write, but it is also applicable to 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);
// 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_offsets_buffer(ctx, query, "a", a_off, &a_off_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);
#include <tiledb/tiledb>
using namespace tiledb;
// Create context
Context ctx;
// Prepare some data for the array
std::vector<int64_t> d1 = {1, 2, 3, 4};
std::vector<int64_t> d2 = {2, 1, 3, 4};
std::vector<int32_t> a = {1, 1, 2, 3, 3, 3, 4};
std::vector<uint64_t> a_off = {0, 8, 12, 24};
// Open array for writing
Array array(ctx, array_name, TILEDB_WRITE);
// Create the query
Query query(ctx, array);
query.set_layout(TILEDB_UNORDERED)
.set_data_buffer("d1", d1)
.set_data_buffer("d2", d2)
.set_data_buffer("a", a)
.set_offsets_buffer("a", a_off);
// Submit query
query.submit();
// Close the array
array.close();
import tiledb
import numpy as np
# note: the Attribute must be created with `var=True`
# att = tiledb.Attr(dtype=dtype, var=True, ctx=ctx)
d1 = np.array([1,2,3,4])
d2 = np.array([2,1,3,2])
a = np.array([
np.array([1,1], dtype=np.int32),
np.array([2], dtype=np.int32),
np.array([3,3,3], dtype=np.int32),
np.array([4], dtype=np.int32)
], dtype='O')
with tiledb.open(array_name, 'w') as A:
A[d1, d2] = a
arr <- tiledb_array(tmp, "WRITE")
d1 <- c(1L, 2L, 3L, 4L)
d2 <- c(2L, 1L, 3L, 4L)
data <- c(1L, 1L, 2L, 3L, 3L, 3L, 4L)
data_off <- c(0, 8, 12, 24)
qry <- tiledb_query(arr, "WRITE")
qry <- tiledb_query_set_layout(qry, "UNORDERED")
qry <- tiledb_query_set_buffer(qry, "d1", d1)
qry <- tiledb_query_set_buffer(qry, "d2", d2)
# use lower-level function to allocate and set buffers
qryptr <- qry@ptr
vecptr <- tiledb:::libtiledb_query_buffer_var_vec_create(data_off, data)
qryptr <- tiledb:::libtiledb_query_set_buffer_var_vec(qryptr, "a", vecptr)
qry <- tiledb_query_submit(qry)
res <- tiledb_array_close(arr)
#TODO Higher-level R support
// Create context, array and query
try(Context ctx = new Context(),
Array array = new Array(ctx, array_name, TILEDB_WRITE),
Query query = new Query(ctx, array);) {
// Prepare some data for the array
NativeArray d1 = new NativeArray(ctx, new long[] {1l, 2l, 3l, 4l}, Long.class);
NativeArray d2 = new NativeArray(ctx, new long[] {2l, 1l, 3l, 4l}, Long.class);
NativeArray a_off =
new NativeArray(ctx, new long[] {0l, 8l, 12l, 24l}, Long.class);
NativeArray a = new NativeArray(ctx, new int[] {1, 1, 2, 3, 3, 3, 4}, Integer.class);
// Create the query
query.setLayout(TILEDB_ROW_MAJOR)
.setBuffer("d1", d1)
.setBuffer("d2", d2)
.setBuffer("a", a_off, a);
// Submit query
query.submit();
}
import "github.com/TileDB-Inc/TileDB-Go"
// Create context
ctx, _ := tiledb.NewContext(nil)
// Prepare some data for the array
d1 := []int32{1, 2, 3, 4}
d2 := []int32{2, 1, 3, 4}
a := []int32{1, 1, 2, 3, 3, 3, 4}
a_off := []uint64{0, 8, 12, 24}
// Open array for writing
array, _ := tiledb.NewArray(ctx, array_name)
array.Open(tiledb.TILEDB_WRITE)
// Create the query
query, _ := tiledb.NewQuery(ctx, array)
query.SetLayout(tiledb.TILEDB_ROW_MAJOR)
_, err = query.SetBuffer("d1", d1)
_, err = query.SetBuffer("d2", d2)
_. err = query.SetBufferVar("a", a_off, a)
// Submit query
query.Submit();
// Close the array
array.Close();
using TileDB.CSharp;
// Create TileDB context
using Context ctx = new Context();
// Open array for writing
using Array array = new Array(ctx, array_name);
array.Open(QueryType.Write);
// Prepare some data for the array
long[] d1 = new long[] { 1, 2, 3, 4 };
long[] d2 = new long[] { 2, 1, 3, 4 };
int[] a = new int[] { 1, 1, 2, 3, 3, 3, 4 };
ulong[] aOffsets = new ulong[] { 0, 8, 12, 24 };
// Create the query
using Query query = new Query(ctx, array, QueryType.Write);
query.SetLayout(LayoutType.Unordered);
query.SetDataBuffer("d1", d1);
query.SetDataBuffer("d2", d2);
query.SetDataBuffer("a", a);
query.SetOffsetsBuffer("a", aOffsets);
// Submit query
query.Submit();
// Close array
array.Close();
The code snippet above produces the following sparse fragment.
Last updated