# DynamoDB
source: https://docs.chalk.ai/docs/dynamodb

## Integrate with DynamoDB.

Chalk supports [DynamoDB](https://aws.amazon.com/dynamodb/) as a natively accelerated [SQL source](/docs/sql). After establishing the integration,
you will be able to query your DynamoDB instance through Chalk with [PartiQL](https://partiql.org/).

### Adding DynamoDB

By navigating to `Integrations > Add a data source` and selecting `DynamoDB`, you'll find a form where you can input information about your DynamoDB integration. 
Note that the data source must be accessible by the IAM Role defined in your cluster deployment.

### Integration

Add your DynamoDB data source in a Python file and give it a `name`. This line is required for all DynamoDB
integrations. In this file, we'll also define a `User` feature class with a `transaction_volume` feature which will be
resolved with DynamoDB.

```python
from chalk.features import features
from chalk.sql import DynamoDBSource

DynamoDBSource(name="user_data")  # required for Chalk to be aware of this source

@features
class User:
    id: str
    transaction_volume: float
    high_volume: bool = _.transaction_volume > 1000
```

Now, you are all set to use DynamoDB with [SQL file resolvers](/docs/sql#sql-file-resolvers):

```sql
-- type: online
-- resolves: User
-- source: user_data
SELECT
    id,
    TransactionVolume AS transaction_volume
FROM UserTable
```

This file resolves two features of the feature class `User`: `User.transaction_volume` and `User.id`.
When the `User.transaction_volume` feature is required (or if it's needed for computing a downstream feature
like `User.high_volume`), Chalk will execute the SQL query on your DynamoDB instance and
return the result.
The `-- source: user_data` comment tells Chalk to use the `DynamoDBSource` with the name `user_data` to resolve this
query.

### Chalk's DynamoDB Query Language

Chalk uses [PartiQL](https://partiql.org/) to execute SQL queries on your DynamoDB connection. Chalk extends PartiQL to
support aliasing and limits: you can rename your result columns to match your feature names
and limit results. The above example demonstrates aliasing with `TransactionVolume AS transaction_volume`.

Chalk extends PartiQL with the following syntax:

```
--- Amazon PartiQL
SELECT expression  [, ...]
FROM table[.index]
[ WHERE condition ] [ ORDER BY key [DESC|ASC] , ...]

--- Chalk PartiQL
SELECT expression [AS alias] [, ...]
FROM table[.index]
[ WHERE condition ] [ ORDER BY key [DESC|ASC] , ...]
[ LIMIT limit ]
```

Chalk sends `LIMIT` to DynamoDB as the request's
[`Limit`](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ExecuteStatement.html#DDB-ExecuteStatement-request-Limit),
which caps the items DynamoDB _evaluates_, not the items it returns. When the `WHERE` clause only constrains the partition
key, every evaluated item matches, so the query returns up to `limit` rows and reads no more than it returns. When the
`WHERE` clause also filters on other attributes, DynamoDB may evaluate items that the filter then discards, and the
query can return fewer than `limit` rows even though more matching items exist.

Queries with an `ORDER BY` get the same pushdown, because DynamoDB counts `Limit` in the requested key order:
`WHERE pk = ? ORDER BY sk DESC LIMIT 10` reads only the 10 items with the highest sort keys in that partition.

You can also use [batch execution with
PartiQL](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.multiplestatements.batching.html)
to retrieve multiple records in a single request via [`ChalkClient.query_bulk`](/api-docs#ChalkClient.query_bulk). Batch
execution is only supported for single table queries.





