Serverless SQL

Basic Usage

SQL queries can be executed with the TileDB Cloud client as follows.

Python
Python
import tiledb.cloud
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
# res is a Pandas dataframe
res = tiledb.cloud.sql.exec(
query = "select `rows`, AVG(a) as avg_a from `tiledb://TileDB-Inc/quickstart_dense` GROUP BY `rows`")

Supposing that there is an array tiledb://user/array_name that you have write permissions for, you can run a SQL query that writes the results to that array as follows:

Python
Python
import tiledb.cloud
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
tiledb.cloud.sql.exec(
query = "select `rows`, AVG(a) as avg_a from `tiledb://TileDB-Inc/quickstart_dense` GROUP BY `rows`",
output_uri = "tiledb://user/array_name")

You can run any SQL statement, including CTEs.

Python
Python
import tiledb.cloud
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
# INSERT INTO ...
tiledb.cloud.sql.exec(query = "insert into `tiledb://user1/output1` select AVG(a) from `tiledb://user1/myarray1`")
# CTE
tiledb.cloud.sql.exec(query =
"""
WITH t1 AS (select `dim`, `dim2`, AVG(a), AVG(a) OVER (partition by dim2) FROM `tiledb://user1/array1`)
insert into `tiledb://user/array_name`
select * from t1
""")

If the array does not exist, you will just need to create and pass an array schema that complies with the result schema.

Python
Python
import tiledb.cloud
import numpy as np
import pandas
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
# The output array has a single dimension
dom = tiledb.Domain(tiledb.Dim(name="rows", domain=(1, 4), tile=4, dtype=np.int32))
# The output array has a single attribute
attr = tiledb.Attr(name="avg_a", dtype=np.float32)
# The output array is sparse
schema = tiledb.ArraySchema(domain=dom, sparse=True, attrs=[attr])
# The following will create the output array and write the results to it
tiledb.cloud.sql.exec(
query = "select `rows`, AVG(a) as avg_a from `tiledb://TileDB-Inc/quickstart_dense` GROUP BY `rows`",
output_schema = schema,
output_uri = "tiledb://user/s3://my_bucket/my_array") # Notice the full path here

We also provide an auxiliary function exec_and_fetch, which executes a SQL query as above, but then opens the resulting output array so that it is ready for use.

Python
Python
import tiledb.cloud
import pandas
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
A = tiledb.cloud.sql.exec_and_fetch(
query = "select `rows`, AVG(a) as avg_a from `tiledb://TileDB-Inc/quickstart_dense` GROUP BY `rows`",
output_uri="tiledb://user/array_name")
# `A` is an opened array for reading, ready for use
print(A.df[:])

Selecting Who to Charge

If you are a member of an organization, then by default the organization is charged for your SQL query. If you would like to charge the SQL query to yourself, you just need to add one extra argument namespace.

Python
Python
import tiledb.cloud
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
tiledb.cloud.sql.exec(
namespace ="my_username", # Who to charge the query to
query = "select `rows`, AVG(a) as avg_a from `tiledb://TileDB-Inc/quickstart_dense` GROUP BY `rows`",
output_uri = "tiledb://user/array_name")

Async Execution

An asynchronous version of severless SQL is available. The _async version returns a future.

Python
Python
import tiledb.cloud
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
# res is a future
res = tiledb.cloud.sql.exec_async(
query = "select `rows`, AVG(a) as avg_a from `tiledb://TileDB-Inc/quickstart_dense` GROUP BY `rows`")
# Call res.get() to block on the results
print(res.get())

Creating New Arrays

It is also possible to use SQL to create a new array.

Python
Python
import tiledb.cloud
tiledb.cloud.login(username="my_username", password="my_password")
# or tiledb.cloud.login(token="my_token")
# Create an array that will be registered in TileDB Cloud and stored in the
# S3 bucket and location specied in the `uri` parameter. The `uri` parameter
# overrides the normal use of the table name `example`.
# The array is automatically registered in TileDB cloud
# under `my_username`/`create_example_1`
tiledb.cloud.sql.exec("""
CREATE TABLE `example` (
dim0 integer DIMENSION=1 lower_bound="0" upper_bound="100" tile_extent="10",
dim1 integer DIMENSION=1 lower_bound="0" upper_bound="100" tile_extent="10",
attr1 varchar(255) filters="GZIP=-1",
attr2 FLOAT filters="GZIP=-1",
attr3 DATETIME filters="GZIP=-1"
) engine=MyTile
array_type='SPARSE'
coordinate_filters="NONE"
offset_filters="POSITIVE_DELTA=128"
uri='tiledb://my_username/s3://my_bucket/create_example_1';
""")
# You can also use `Primary Key` to specify which fields are the dimensions
# This replaces the `DIMENSION=1` parameter
# The array is automatically registered in TileDB cloud
# under `my_username`/`create_example_2`
tiledb.cloud.sql.exec("""
CREATE TABLE `example` (
dim0 integer lower_bound="0" upper_bound="100" tile_extent="10" NOT NULL,
dim1 bigint lower_bound="0" upper_bound="100" tile_extent="10" NOT NULL,
attr1 varchar(255) filters="GZIP=-1",
PRIMARY KEY(dim0, dim1)
) engine=MyTile
array_type='SPARSE'
coordinate_filters="NONE"
offset_filters="POSITIVE_DELTA=128"
uri='tiledb://my_username/s3://my_bucket/create_example_2';
""")
# Lastly you can also use `INDEX()` to set dimensions and allow duplicates
# By default duplicate coordinates for dimensions are not allowed,
# uniqueness is enforced just like a primary key. Using `Index()`
# instead triggers the array to be created allowing duplicates coordinate values.
# The array is automatically registered in TileDB cloud
# under `my_username`/`create_example_3`
tiledb.cloud.sql.exec("""
CREATE TABLE `example` (
dim0 integer lower_bound="0" upper_bound="100" tile_extent="10" NOT NULL,
dim1 varchar(255) NOT NULL,
attr1 varchar(255) filters="GZIP=-1",
INDEX(dim0, dim1)
) engine=MyTile
array_type='SPARSE'
coordinate_filters="NONE"
offset_filters="POSITIVE_DELTA=128"
uri='tiledb://my_username/s3://my_bucket/create_example_3';
""")

Retry Settings

See Retry Settings for more information on this topic.