Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for query pass-through table function in ClickHouse connector #22545

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/src/main/sphinx/connector/clickhouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,42 @@ statements, the connector supports the following features:
```{include} alter-schema-limitation.fragment
```

## Table functions

The connector provides specific {doc}`table functions </functions/table>` to
access ClickHouse.

(clickhouse-query-function)=
### `query(varchar) -> table`

The `query` function allows you to query the underlying database directly. It
requires syntax native to ClickHouse, because the full query is pushed down and
processed in ClickHouse. This can be useful for accessing native features which
are not available in Trino or for improving query performance in situations
where running a query natively may be faster.

```{include} query-passthrough-warning.fragment
```

As a simple example, query the `example` catalog and select an entire table:

```
SELECT
*
FROM
TABLE(
example.system.query(
query => 'SELECT
*
FROM
tpch.nation'
)
);
```

```{include} query-table-function-ordering.fragment
```

## Performance

The connector includes a number of performance improvements, detailed in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import io.trino.plugin.jdbc.JdbcClient;
import io.trino.plugin.jdbc.JdbcMetadataConfig;
import io.trino.plugin.jdbc.credential.CredentialProvider;
import io.trino.plugin.jdbc.ptf.Query;
import io.trino.spi.function.table.ConnectorTableFunction;

import java.util.Properties;

import static com.clickhouse.client.config.ClickHouseClientOption.USE_BINARY_STRING;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static io.airlift.configuration.ConfigBinder.configBinder;
import static io.trino.plugin.clickhouse.ClickHouseClient.DEFAULT_DOMAIN_COMPACTION_THRESHOLD;
import static io.trino.plugin.jdbc.JdbcModule.bindSessionPropertiesProvider;
Expand All @@ -50,6 +53,7 @@ public void configure(Binder binder)
bindTablePropertiesProvider(binder, ClickHouseTableProperties.class);
configBinder(binder).bindConfigDefaults(JdbcMetadataConfig.class, config -> config.setDomainCompactionThreshold(DEFAULT_DOMAIN_COMPACTION_THRESHOLD));
binder.install(new DecimalModule());
newSetBinder(binder, ConnectorTableFunction.class).addBinding().toProvider(Query.class).in(Scopes.SINGLETON);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
SUPPORTS_ARRAY,
SUPPORTS_DELETE,
SUPPORTS_DROP_NOT_NULL_CONSTRAINT,
SUPPORTS_NATIVE_QUERY,
SUPPORTS_NEGATIVE_DATE,
SUPPORTS_PREDICATE_PUSHDOWN_WITH_VARCHAR_INEQUALITY,
SUPPORTS_ROW_TYPE,
Expand Down Expand Up @@ -788,6 +787,23 @@ public void testRenameTableToLongTableName()
assertThat(getQueryRunner().tableExists(getSession(), invalidTargetTableName)).isFalse();
}

@Test
@Override // Override because the failure message differs
public void testNativeQueryIncorrectSyntax()
{
assertThat(query("SELECT * FROM TABLE(system.query(query => 'some wrong syntax'))"))
.failure().hasMessage("Query not supported: ResultSetMetaData not available for query: some wrong syntax");
}

@Test
@Override // Override because the failure message differs
public void testNativeQueryInsertStatementTableDoesNotExist()
{
assertThat(getQueryRunner().tableExists(getSession(), "non_existent_table")).isFalse();
assertThat(query("SELECT * FROM TABLE(system.query(query => 'INSERT INTO non_existent_table VALUES (1)'))"))
.failure().hasMessage("Query not supported: ResultSetMetaData not available for query: INSERT INTO non_existent_table VALUES (1)");
}

@Test
public void testLargeDefaultDomainCompactionThreshold()
{
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
<classifier>all</classifier>
</dependency>

Expand Down
Loading