Working with ClickHouse databases
Warning
External connectors are an experimental feature of YDB. To work with external DBMS through connectors, you need to deploy fq-connector-go and explicitly enable the corresponding sources in the cluster configuration YDB. The functionality may change, so use in production is not recommended without thorough testing.
This section describes basic information about working with an external database ClickHouse.
To work with an external ClickHouse database, you need to follow these steps:
-
Create a secret containing the password for connecting to the database.
CREATE SECRET clickhouse_datasource_user_password WITH (value = "<password>"); -
Create an external data source that describes the target database inside the ClickHouse cluster. To connect to ClickHouse, you can use either the native TCP protocol (
PROTOCOL="NATIVE") or the HTTP protocol (PROTOCOL="HTTP"). Enable encryption of connections to the external database using theUSE_TLS="TRUE"parameter.CREATE EXTERNAL DATA SOURCE clickhouse_datasource WITH ( SOURCE_TYPE="ClickHouse", LOCATION="<host>:<port>", DATABASE_NAME="<database>", AUTH_METHOD="BASIC", LOGIN="<login>", PASSWORD_SECRET_PATH="clickhouse_datasource_user_password", PROTOCOL="NATIVE", USE_TLS="TRUE" ); -
Deploy the connector and configure the YDB dynamic nodes to interact with it. Additionally, ensure network access from the YDB dynamic nodes to the external data source (at the address specified in the
LOCATIONparameter of theCREATE EXTERNAL DATA SOURCErequest). If network connection encryption to the external source was enabled in the previous step, the connector will use the system's root certificates. More details on TLS configuration can be found in the guide on deploying the connector. -
Execute a query to the database.
Query syntax
The following SQL query form is used to work with ClickHouse:
SELECT * FROM clickhouse_datasource.<table_name>
where:
clickhouse_datasource: external data source identifier<table_name>is the name of the table inside the external data source.
Limitations
When working with ClickHouse clusters, there are a number of limitations:
-
External sources are available only for reading data through
SELECTqueries. The federated query processing engine currently does not support queries that modify tables in external sources. -
If the date value stored in the external data source is outside the allowed range for YDB (all dates used must be later than 1970-01-01 but earlier than 2105-12-31), such a value in YDB will be converted to
NULL. -
The YDB federated query processing system is capable of delegating the execution of certain parts of a query to the system acting as the data source. Query fragments are passed through YDB directly to the external system and processed by them. This optimization, known as "predicate pushdown", significantly reduces the amount of data transferred from the source to the federated query processing engine. This reduces network load and saves computational resources for the federated YDB.
A specific case of predicate pushdown, when the filtering expressions are specified after the
WHEREkeyword, are passed to the data source, is called "filter pushdown". Filter pushdown is possible when using:Description Example NULLchecksWHERE column1 IS NULLorWHERE column1 IS NOT NULLLogical conditions OR,NOT,ANDand parentheses for controlling calculation priority.WHERE column1 IS NULL OR (column2 IS NOT NULL AND column3 > 10).Comparison operators with other columns or constants. WHERE column1 > column2 OR column3 <= 10,WHERE column1 + column2 > 10,WHERE column1 = (10 + 10)When using other types of filters, pushdown to the data source is not performed: filtering of the external table rows will be executed by the federated YDB, which means that YDB will perform a full scan of the external table when processing the query.
Supported data types for filter pushdown:
Data type YDB BoolInt8Uint8Int16Uint16Int32Uint32Int64Uint64FloatDoubleString
Supported data types
By default, ClickHouse columns physically cannot contain the NULL value; however, you can create a table with columns of optional, or nullable, types. The column types displayed by YDB when retrieving data from an external ClickHouse database will depend on whether the ClickHouse table uses primitive or optional types. However, due to the limitations of YDB types used for storing dates and times discussed above, all similar ClickHouse types are displayed in YDB as optional.
Below are the tables of correspondence between ClickHouse and YDB types. All other data types, except those listed, are not supported.
Primitive data types
| ClickHouse data type | Data type YDB | Notes |
|---|---|---|
Bool |
Bool |
|
Int8 |
Int8 |
|
UInt8 |
Uint8 |
|
Int16 |
Int16 |
|
UInt16 |
Uint16 |
|
Int32 |
Int32 |
|
UInt32 |
Uint32 |
|
Int64 |
Int64 |
|
UInt64 |
Uint64 |
|
Float32 |
Float |
|
Float64 |
Double |
|
Date |
Date |
|
Date32 |
Optional<Date> |
Valid date range from 1970-01-01 to 2105-12-31. If the value goes outside the range, NULL is returned. |
DateTime |
Optional<DateTime> |
Valid time range from 1970-01-01 00:00:00 to 2105-12-31 23:59:59. If the value goes out of range, the value NULL is returned. |
DateTime64 |
Optional<Timestamp> |
Valid time range from 1970-01-01 00:00:00 to 2105-12-31 23:59:59. If the value goes out of range, the value NULL is returned. |
String |
String |
|
FixedString |
String |
Null bytes FixedString are transferred to String unchanged. |
Optional data types
| ClickHouse data type | Data type YDB | Notes |
|---|---|---|
Nullable(Bool) |
Optional<Bool> |
|
Nullable(Int8) |
Optional<Int8> |
|
Nullable(UInt8) |
Optional<Uint8> |
|
Nullable(Int16) |
Optional<Int16> |
|
Nullable(UInt16) |
Optional<Uint16> |
|
Nullable(Int32) |
Optional<Int32> |
|
Nullable(UInt32) |
Optional<Uint32> |
|
Nullable(Int64) |
Optional<Int64> |
|
Nullable(UInt64) |
Optional<Uint64> |
|
Nullable(Float32) |
Optional<Float> |
|
Nullable(Float64) |
Optional<Double> |
|
Nullable(Date) |
Optional<Date> |
|
Nullable(Date32) |
Optional<Date> |
Valid date range from 1970-01-01 to 2105-12-31. If the value goes beyond the range boundaries, NULL is returned. |
Nullable(DateTime) |
Optional<DateTime> |
Allowed time value range from 1970-01-01 00:00:00 to 2105-12-31 23:59:59. If the value goes beyond the range boundaries, the NULL value is returned. |
Nullable(DateTime64) |
Optional<Timestamp> |
Allowed time value range from 1970-01-01 00:00:00 to 2105-12-31 23:59:59. If the value goes beyond the range boundaries, the value NULL is returned. |
Nullable(String) |
Optional<String> |
|
Nullable(FixedString) |
Optional<String> |
Null bytes FixedString are transferred to String without changes. |