Tile filters allow you to compress and encrypt attribute and dimension data for your arrays.
How to run this tutorial
You can run this tutorial in two ways:
Locally on your machine.
On TileDB Cloud.
However, since TileDB Cloud has a free tier, we strongly recommend that you sign up and run everything there, as that requires no installations or deployment.
This tutorial describes how to use tile filters for attributes and dimensions. Two types of unique filters exist in TileDB: compression and encryption. For tutorials on each, visit the Compress Array Data and Encrypt an Array with an AES-256-GCM Key tutorials. This tutorial is very similar to the one on compression. For more information on all the supported tiled filters, visit the Key Concepts: Tile Filters section.
Note
In this context, a tile filter is a transformation (that is, a processing step) applied to the data in a tile before TileDB writes the tile to storage. TileDB applies the filter to each tile individually, and the transformation is reversible.
First, import the necessary libraries, set the array URI (that is, its path, which in this tutorial will be on local storage), and delete any previously created arrays with the same name.
# Set up some filter lists to pass to dimensions and attributes# tiledb.FilterList accepts an iterable of zero or more filtersfilter_list_1 = tiledb.FilterList( [tiledb.PositiveDeltaFilter(window=1000)], chunksize=10000)filter_list_2 = tiledb.FilterList([tiledb.BitWidthReductionFilter(window=10)])
# Set up some filter lists to pass to dimensions and attributes# tiledb_filter_list() accepts a vector of zero or more filterspd_filter <-tiledb_filter("POSITIVE_DELTA")pd_filter <-tiledb_filter_set_option( pd_filter,"POSITIVE_DELTA_MAX_WINDOW",1000)filter_list_1 <-tiledb_filter_list(pd_filter)set_max_chunk_size(filter_list_1, 10000)bwr_filter <-tiledb_filter("BIT_WIDTH_REDUCTION")bwr_filter <-tiledb_filter_set_option( bwr_filter,"BIT_WIDTH_MAX_WINDOW",10)filter_list_2 <-tiledb_filter_list(bwr_filter)
Then create an array and pass the desired filters as arguments in each dimension and attribute.
# Create the two dimensions# The filter list we created above is passed into the `filters` parameterd1 = tiledb.Dim(name="d1", domain=(0, 3), tile=2, dtype=np.int32, filters=filter_list_1)d2 = tiledb.Dim(name="d2", domain=(0, 3), tile=2, dtype=np.int32, filters=filter_list_1)# Create a domain using the two dimensionsdom = tiledb.Domain(d1, d2)# Create an attribute# The filter list we created above is passed into the `filters` parametera = tiledb.Attr(name="a", dtype=np.int32, filters=filter_list_2)# Create the array schema with `sparse=True`sch = tiledb.ArraySchema(domain=dom, sparse=True, attrs=[a])# Create the array on disk (it will initially be empty)tiledb.Array.create(array_uri, sch)
# Create the two dimensionsd1 <-tiledb_dim("d1", c(0L, 3L), 2L, "INT32", filter_list = filter_list_1)d2 <-tiledb_dim("d2", c(0L, 3L), 2L, "INT32", filter_list = filter_list_1)# Create a domain using the two dimensionsdom <-tiledb_domain(dims =c(d1, d2))# Create an attributea <-tiledb_attr("a", type ="INT32", filter_list = filter_list_2)# Create the array schema with `sparse = TRUE`sch <-tiledb_array_schema(dom, a, sparse =TRUE)# Create the array on disk (it will initially be empty)arr <-tiledb_array_create(array_uri, sch)
Inspect the schema of the array to see how TileDB set the filters you passed.
# ... create domain dom# ... create attribute a# ... create filter list filter_list_1# Create the schema setting the coordinates filter list.# This is applicable only to sparse arrays.schema = tiledb.ArraySchema( domain=dom, sparse=False, attrs=[a], coords_filters=filter_list_1)
# ... create (or retrieve) array schema sch# assign filter list to an existing schemasch <-tiledb_array_schema_set_coords_filter_list(sch, filter_list_1)# Alternatively create a new schema and set the coordinates filter listsch <-tiledb_array_schema(dom, a, coords_filter_list = filter_list_1)
You can also set filters for the variable-length attribute and dimension offsets.
# Create the schema setting the offsets filter list# ... create domain dom# ... create attribute a# ... create filter list filter_list_2# Create the schema setting the offsets filter listschema = tiledb.ArraySchema( domain=dom, sparse=False, attrs=[a], offsets_filters=filter_list_2)
# ... create (or retrieve) array schema sch# assign filter list to an existing schemasch <-tiledb_array_schema_set_offsets_filter_list(sch, filter_list_2)# Alternatively create a new schema and set the offsets filter listsch <-tiledb_array_schema(dom, a, offsets_filter_list = filter_list_2)
The same applies to the validity vectors for fixed-length or variable-length attributes.
# Create the schema setting the offsets filter list# ... create domain dom# ... create attribute a# ... create filter list filter_list_3# Create the schema setting the offsets filter listschema = tiledb.ArraySchema( domain=dom, sparse=False, attrs=[a], validity_filters=filter_list_1)
# ... create (or retrieve) array schema sch# assign filter list to an existing schemasch <-tiledb_array_schema_set_validity_filter_list(sch, filter_list_1)# Alternatively create a new schema and set the validity filter listsch <-tiledb_array_schema(dom, a, validity_filter_list = filter_list_1)