> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How DirectQuery behavior differs between external databases

> Native and emulated time travel, vector representations, and known limitations of DirectQuery on Snowflake, Databricks, BigQuery, ClickHouse, Redshift, SQL Server, Azure Synapse, and Generic JDBC

Atoti DirectQuery works the same way on every supported external database, but each database has its own features and limits.
Some features, such as time travel and vectors, depend on what the database itself supports.
This page compares these differences feature by feature, then lists what to check for specific databases.
For an overview of DirectQuery, read [Introduction to DirectQuery](https://docs.activeviam.com/concepts/directquery/directquery.md).
For the list of supported databases and versions, read [DirectQuery database compatibility](https://docs.activeviam.com/concepts/directquery/database-compatibility.md).

## Which aggregation functions differ between databases?

Most aggregation functions work the same way on every database.
Some aggregations behave differently depending on the database:

* **DistinctCount**: some databases do not support it.
* **Multiply**: most databases have no product aggregation function, so DirectQuery emulates it with logarithms.
  The emulated result can differ slightly from an exact product.
* **Vectors in native arrays**: only some databases aggregate them, with the Sum, GrossSum, Long, Short, and SumProduct functions.
  On other databases, DirectQuery rejects the aggregation.
* **Vectors with multi-row emulation**: DirectQuery rebuilds each vector with the database's array aggregation function.
  Only some databases support it.

Vectors with multi-column emulation use standard SQL, on every database that supports the representation.
For the vector representations, read [Which databases support vectors?](#which-databases-support-vectors)

| Database | DistinctCount | Multiply | Vectors in native arrays | Vectors with multi-row emulation |
| - | - | - | - | - |
| Amazon Redshift | Yes | Emulated | No | Yes |
| ClickHouse | Yes | Native | Yes | No |
| Databricks<sup>1</sup> | Yes | Emulated | Depends on the Databricks version | Yes |
| Generic JDBC<sup>2</sup> | Depends on the SQL dialect | Emulated | Depends on the SQL dialect | Depends on the SQL dialect |
| Google BigQuery | Yes | Emulated | No | Yes |
| Microsoft Azure Synapse | No | Emulated | No | Yes |
| Microsoft SQL Server<sup>3</sup> | No | Emulated | No | Yes |
| Snowflake | Yes | Emulated | With user-defined aggregate functions | Yes |

1. DirectQuery aggregates native arrays with the Databricks [`vector_sum`](https://docs.databricks.com/aws/en/sql/language-manual/functions/vector_sum) function, available in recent Databricks versions.
   This function aggregates `FLOAT` arrays only, so DirectQuery rejects the aggregation of other arrays.
   For the supported versions, read [Compatibility matrix](https://docs.activeviam.com/engine/java-sdk/latest/directquery/compatibility.md).
2. The team that builds the application writes the SQL dialect for a specific database in Java.
   For more information, read [What is the Generic JDBC connector?](https://docs.activeviam.com/concepts/directquery/database-compatibility.md#what-is-the-generic-jdbc-connector)
3. SQL Server 2019 does not provide the `GREATEST` and `LEAST` functions.
   DirectQuery detects the SQL Server version at startup and emulates them for the Long and Short aggregation functions.

## Which databases support time travel?

Time travel lets DirectQuery read the external database as it was at a given point in time.
Native time travel is built into the database.
Emulated time travel uses validity columns added to each table.

| Database | Native time travel | Emulated time travel |
| - | - | - |
| Amazon Redshift | No | Yes |
| ClickHouse | No | Yes |
| Databricks<sup>1</sup> | Yes, tables only | Yes |
| Generic JDBC | No | Yes |
| Google BigQuery | Yes | Yes |
| Microsoft Azure Synapse | No | Yes |
| Microsoft SQL Server | No | Yes |
| Snowflake<sup>2</sup> | Yes | Yes |

1. With Databricks, native time travel does not work on views.
   The time travel policy sets what DirectQuery does with views:

   * **Strict** (default): DirectQuery raises an error if the model contains a view.
   * **Lax**: DirectQuery queries views without time travel, so their data can be out of sync with the tables.
   * **Disabled**: DirectQuery does not use time travel.

   Find out how to set the Databricks time travel policy with:

   * Atoti Java SDK: `timeTravelPolicy` in `DatabricksDatabaseSettings`
   * Atoti Python SDK: [`time_travel`](https://docs.activeviam.com/engine/python-sdk/latest/api/atoti_directquery_databricks.ConnectionConfig#atoti_directquery_databricks.ConnectionConfig.time_travel)

2. With Snowflake, native time travel also works on views.
   Queries on a view return the data as of the current version, like queries on a table.

## Which databases support vectors?

DirectQuery can represent a vector in three ways:

* **Native arrays**: the database stores the vector in a single array column.
* **Multi-row emulation**: each element of the vector is stored in its own row.
* **Multi-column emulation**: each element of the vector is stored in its own column.

Native arrays are the most efficient representation, but not every database can aggregate them.
For vector aggregation support by database, read [Which aggregation functions differ between databases?](#which-aggregation-functions-differ-between-databases)

| Database | Native arrays | Multi-row emulation | Multi-column emulation |
| - | - | - | - |
| Amazon Redshift | Yes | Yes | Yes |
| ClickHouse<sup>1</sup> | Yes | No | No |
| Databricks | Yes | Yes | Yes |
| Generic JDBC<sup>2</sup> | Depends on the SQL dialect | Depends on the SQL dialect | Yes |
| Google BigQuery | Yes | Yes | Yes |
| Microsoft Azure Synapse | No | Yes | Yes |
| Microsoft SQL Server | No | Yes | Yes |
| Snowflake | Yes | Yes | Yes |

1. ClickHouse supports arrays of scalar values only, not arrays of arrays.
2. The team that builds the application writes the SQL dialect for a specific database in Java.
   ActiveViam provides a dialect for Dremio.
   For more information, read [What is the Generic JDBC connector?](https://docs.activeviam.com/concepts/directquery/database-compatibility.md#what-is-the-generic-jdbc-connector)

Find out how to define vector fields with:

* Atoti Java SDK: [Use vectors in a table](https://docs.activeviam.com/engine/java-sdk/latest/directquery/how_to/use_vector_in_table.md)
* Atoti Python SDK:
  * [`MultiColumnArrayConversion`](https://docs.activeviam.com/engine/python-sdk/latest/api/atoti.directquery.array_conversion.MultiColumnArrayConversion)
  * [`MultiRowArrayConversion`](https://docs.activeviam.com/engine/python-sdk/latest/api/atoti.directquery.array_conversion.MultiRowArrayConversion)
  * [`AutoMultiColumnArrayConversion`](https://docs.activeviam.com/engine/python-sdk/latest/api/atoti.directquery.array_conversion.AutoMultiColumnArrayConversion)

## What should be checked with ClickHouse?

* Within the [MergeTree family](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family), DirectQuery supports only `MergeTree`, `ReplicatedMergeTree`, and `SharedMergeTree`.
  Engines outside the family are accepted.
* Left joins return null values for missing keys, following the SQL standard.
  By default, ClickHouse returns type-based default values instead.
  To get the same results as DirectQuery in another SQL client, add `SETTINGS join_use_nulls=1` to the query.
* DirectQuery presents ClickHouse dictionaries as standard tables.
* DirectQuery reads most [ClickHouse data types](https://clickhouse.com/docs/en/sql-reference/data-types) automatically.
  Some types are converted or can overflow:
  * `UInt64`, `UInt128`, `UInt256`, `Int128`, and `Int256` are read as Java `long` values, so values outside the `long` range overflow.
  * `String`, `FixedString`, `Enum8`, `Enum16`, `UUID`, `IPv4`, and `IPv6` are read as strings.
  * Other types, such as `Map`, `Tuple`, and `Nested`, are read as generic Java objects.
    To read them as a specific type, use a custom type reader.

Find out how to use a custom type reader with:

* Atoti Java SDK: [Use custom types](https://docs.activeviam.com/engine/java-sdk/latest/directquery/how_to/use_custom_types.md)

## What should be checked with Generic JDBC?

Databases and JDBC drivers can handle data types differently, for example time zones in dates and timestamps.
The SQL dialect can customize the type mapping.
