Creating Attributes
Creating an attribute requires specifying a (optional) name and a (required) datatype. In the example below we create an int32 attribute called attr.
C
C++
Python
R
Java
Go
1
#include <tiledb/tiledb.h>
2
3
// Create context
4
tiledb_ctx_t* ctx;
5
tiledb_ctx_alloc(NULL, &ctx);
6
7
// Create attribute
8
tiledb_attribute_t* attr;
9
tiledb_attribute_alloc(ctx, "attr", TILEDB_INT32, &attr);
10
11
// ...
12
13
// Make sure to free the attribute and context
14
tiledb_attribute_free(&attr);
15
tiledb_ctx_free(&ctx);
Copied!
1
#include <tiledb/tiledb>
2
using namespace tiledb;
3
4
// Create context
5
Context ctx;
6
7
// Create attribute
8
auto attr = Attribute::create<int32>(ctx, "attr")
9
10
// Or alternatively:
11
Attribute attr(ctx, "attr", TILEDB_INT32);
Copied!
1
import tiledb
2
import numpy as np
3
4
attr = tiledb.Attr(name="attr", dtype=np.int32)
Copied!
1
# Create attribute
2
attr <- tiledb_attr("attr", "INT32")
Copied!
1
// Create context
2
try(Context ctx = new Context()) {
3
// Create attribute
4
Attribute attr = new Attribute(ctx, "attr", TILEDB_INT32);
5
}
Copied!
1
import tiledb "github.com/TileDB/TileDB-Go"
2
3
// Create context
4
ctx, _ := tiledb.NewContext(nil)
5
6
// Create attribute
7
attr, _ := tiledb.NewAttribute(ctx, "attr", tiledb.TILEDB_INT32)
Copied!
An attribute can also store a fixed number of values (of the same datatype) in a single cell, or a variable number of values. You can specify this as follows:
C
C++
Python
R
Java
Go
1
// Setting a fixed number of values
2
tiledb_attribute_set_cell_val_num(ctx, attr, 3);
3
4
// Setting a variable number of values
5
tiledb_attribute_set_cell_val_num(ctx, attr, TILEDB_VAR_NUM);
Copied!
1
// Setting a fixed number of values
2
attr.set_cell_val_num(3);
3
4
// Setting a variable number of values
5
attr.set_cell_val_num(TILEDB_VAR_NUM);
Copied!
1
# Setting a fixed number of values
2
attr = tiledb.Attr(name="attr", dtype=np.dtype('i4, i4, i4'))
3
4
# Setting a variable number of values
5
attr = tiledb.Attr(name="attr", dtype=np.int32, var=True)
Copied!
1
## Attribute value counts can be retrieved from R
2
attr <- tiledb_attr("a1", type = "INT32")
3
cell_val_num(a1)
4
5
## Attribute value counts can be set via the lower level API
6
## set int32 attribute to length three
7
attr <- tiledb_attr("a1", type = "INT32")
8
tiledb:::libtiledb_attribute_set_cell_val_num(attr@ptr, 3)
9
10
## set char attribute to variable length which is encoded as a NA
11
attr <- tiledb_attr("a1", type = "CHAR")
12
tiledb:::libtiledb_attribute_set_cell_val_num(attr@ptr, NA)
Copied!
1
// Setting a fixed number of values
2
attr.setCellValNum(3);
3
4
// Setting a variable number of values
5
attr.setCellValNum(TILEDB_VAR_NUM);
Copied!
1
// Setting a fixed number of values
2
attr.SetCellValNum(3)
3
4
// Setting a variable number of values
5
attr.SetCellValNum(tiledb.TILEDB_VAR_NUM)
Copied!
An attribute may also be nullable. This allows designating each cell as valid or null. Applicable to both fixed-sized and var-sized attributes.
C
C++
Python
R
Java
Go
1
// Setting a nullable attribute
2
tiledb_attribute_set_nullable(ctx, attr, 1);
Copied!
1
// Setting a nullable attribute
2
attr.set_nullable(true);
Copied!
1
# TODO
Copied!
1
## TODO
Copied!
1
// TODO
Copied!
1
// TODO
Copied!
Supported Attribute Datatypes:
C/C++/Java/Go
Python
R
Datatype
Description
TILEDB_INT8
8-bit integer
TILEDB_UINT8
8-bit unsigned integer
TILEDB_INT16
16-bit integer
TILEDB_UINT16
16-bit unsigned integer
TILEDB_INT32
32-bit integer
TILEDB_UINT32
32-bit unsigned integer
TILEDB_INT64
64-bit integer
TILEDB_UINT64
64-bit unsigned integer
TILEDB_FLOAT32
32-bit floating point
TILEDB_FLOAT64
64-bit floating point
TILEDB_DATETIME_YEAR
Years
TILEDB_DATETIME_MONTH
Months
TILEDB_DATETIME_WEEK
Weeks
TILEDB_DATETIME_DAY
Days
TILEDB_DATETIME_HR
Hours
TILEDB_DATETIME_MIN
Minutes
TILEDB_DATETIME_SEC
Seconds
TILEDB_DATETIME_MS
Milliseconds
TILEDB_DATETIME_US
Microseconds
TILEDB_DATETIME_NS
Nanoseconds
TILEDB_DATETIME_PS
Picoseconds
TILEDB_DATETIME_FS
Femtoseconds
TILEDB_DATETIME_AS
Attoseconds
TILEDB_DATETIME_CHAR
Single character
TILEDB_STRING_ASCII
ASCII string
TILEDB_STRING_UTF8
UTF-8 string
TILEDB_STRING_UTF16
UTF-16 string
TILEDB_STRING_UTF32
UTF-32 string
TILEDB_STRING_UCS2
UCS-2 string
TILEDB_STRING_UCS4
UCS-4 string
TILEDB_ANY
(datatype, bytelength, value)
Datatype
Description
Internal TILEDB Datatype Mapping
np.int8
8-bit integer
TILEDB_INT8
np.uint8
8-bit unsigned integer
TILEDB_UINT8
np.int16
16-bit integer
TILEDB_INT16
np.uint16
16-bit unsigned integer
TILEDB_UINT16
np.int32
32-bit integer
TILEDB_INT32
np.uint32
32-bit unsigned integer
TILEDB_UINT32
np.int64
64-bit integer
TILEDB_INT64
np.uint64
64-bit unsigned integer
TILEDB_UINT64
np.float32
32-bit floating point
TILEDB_FLOAT32
np.float64
64-bit floating point
TILEDB_FLOAT64
"ascii"
ASCII
TILEDB_STRING_ASCII
np.dtype('S1')
Character
TILEDB_CHAR
np.dtype('U1')
Unicode UTF-8
TILEDB_STRING_UTF8
"datetime64[Y]"
Years
TILEDB_DATETIME_YEAR
"datetime64[M]"
Months
TILEDB_DATETIME_MONTH
"datetime64[W]"
Weeks
TILEDB_DATETIME_WEEK
"datetime64[D]"
Days
TILEDB_DATETIME_DAY
"datetime64[h]"
Hours
TILEDB_DATETIME_HR
"datetime64[m]"
Minutes
TILEDB_DATETIME_MIN
"datetime64[s]"
Seconds
TILEDB_DATETIME_SEC
"datetime64[ms]"
Milliseconds
TILEDB_DATETIME_MS
"datetime64[us]"
Microseconds
TILEDB_DATETIME_US
"datetime64[ns]"
Nanoseconds
TILEDB_DATETIME_NS
"datetime64[ps]"
Picoseconds
TILEDB_DATETIME_PS
"datetime64[fs]"
Femtoseconds
TILEDB_DATETIME_FS
"datetime64[as]"
Attoseconds
TILEDB_DATETIME_AS
Datatype
Description
"CHAR"
Single character
"INT8"
8-bit integer
"UINT8"
8-bit unsigned integer
"INT16"
16-bit integer
"UINT16"
16-bit unsigned integer
"INT32"
32-bit integer
"UINT32"
32-bit unsigned integer
"INT64"
64-bit integer
"UINT64"
64-bit unsigned integer
"FLOAT32"
32-bit floating point
"FLOAT64"
64-bit floating point
"DATETIME_YEAR"
Years
"DATETIME_MONTH"
Months
"DATETIME_WEEK"
Weeks
"DATETIME_DAY"
Days
"DATETIME_HR"
Hours
"DATETIME_MIN"
Minutes
"DATETIME_SEC"
Seconds
"DATETIME_MS"
Milliseconds
"DATETIME_US"
Microseconds
"DATETIME_NS"
Nanoseconds
"DATETIME_PS"
Picoseconds
"DATETIME_FS"
Femtoseconds
"DATETIME_AS"
Attoseconds

Setting Filters

Attributes accept filters such as compressors. This is described in detail here.

Setting Fill Values

There are situations where you may read "empty spaces" from TileDB arrays. For those empty spaces, a read query will return some default fill values for the selected attributes. You can set your own fill values for these cases as follows:
C
C++
Python
R
Java
Go
1
// Assumming a int32 attribute
2
int value = 0;
3
unsigned long long size = sizeof(value);
4
tiledb_attribute_set_fill_value(ctx, attr, &value, size);
5
6
// Assumming a var-sized attribute
7
const char* value = "null";
8
unsigned long long size = strlen(value);
9
tiledb_attribute_set_fill_value(ctx, attr, value, size);
Copied!
1
// Fixed-sized attribute
2
auto a1 = tiledb::Attribute::create<int>(ctx, "a1");
3
int32_t value = 0;
4
uint64_t size = sizeof(value);
5
a1.set_fill_value(&value, size);
6
7
// Var-sized attribute
8
auto a2 = tiledb::Attribute::create<std::string>(ctx, "a2");
9
std::string value("null");
10
a2.set_fill_value(value.c_str(), value.size());
Copied!
1
# Setting a fixed-size fill value
2
attr = tiledb.Attr(name="attr", dtype=np.int32, fill=123)
3
4
# Setting a var-sized (string attribute) fill value
5
attr = tiledb.Attr(name="attr", dtype=np.str, fill="filler")
6
7
# Retrieving the value
8
attr.fill # -> "filler"
Copied!
1
# ... create int attribute attr
2
# set fill value to 42L
3
tiledb_attribute_set_fill_value(attr, 42L)
4
5
# ... create variable-sized attributte attr
6
# set fill value to "..."
7
tiledb_attribute_set_fill_value(attr, "...")
Copied!
1
// TODO
Copied!
1
// Assumming a int32 attribute
2
attribute.SetFillValue(0)
3
4
// Assumming a var-sized attribute
5
attribute.SetFillValue("null")
Copied!
A call to setting the number of cells for an attribute (see above) sets the fill value of the attribute to its default. Therefore, make sure you set the fill values after deciding on the number of values this attribute will hold in each cell.
For fixed-sized attributes, the input fill value size should be equal to the cell size.
Last modified 1mo ago