Skip to content

Commit ca19c7f

Browse files
committed
Use ORDER BY tuple() by default in ClickHouse MergeTree
1 parent df2277f commit ca19c7f

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

docs/src/main/sphinx/connector/clickhouse.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ WITH (
154154

155155
The following are supported ClickHouse table properties from [https://clickhouse.tech/docs/en/engines/table-engines/mergetree-family/mergetree/](https://clickhouse.tech/docs/en/engines/table-engines/mergetree-family/mergetree/)
156156

157-
| Property name | Default value | Description |
158-
| -------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------- |
159-
| `engine` | `Log` | Name and parameters of the engine. |
160-
| `order_by` | (none) | Array of columns or expressions to concatenate to create the sorting key. Required if `engine` is `MergeTree`. |
161-
| `partition_by` | (none) | Array of columns or expressions to use as nested partition keys. Optional. |
162-
| `primary_key` | (none) | Array of columns or expressions to concatenate to create the primary key. Optional. |
163-
| `sample_by` | (none) | An expression to use for [sampling](https://clickhouse.tech/docs/en/sql-reference/statements/select/sample/). Optional. |
157+
| Property name | Default value | Description |
158+
| -------------- | ------------- |----------------------------------------------------------------------------------------------------------------------------------------|
159+
| `engine` | `Log` | Name and parameters of the engine. |
160+
| `order_by` | (none) | Array of columns or expressions to concatenate to create the sorting key. `tuple()` is used by default if `order_by is` not specified. |
161+
| `partition_by` | (none) | Array of columns or expressions to use as nested partition keys. Optional. |
162+
| `primary_key` | (none) | Array of columns or expressions to concatenate to create the primary key. Optional. |
163+
| `sample_by` | (none) | An expression to use for [sampling](https://clickhouse.tech/docs/en/sql-reference/statements/select/sample/). Optional. |
164164

165165
Currently the connector only supports `Log` and `MergeTree` table engines
166166
in create table statement. `ReplicatedMergeTree` engine is not yet supported.

plugin/trino-clickhouse/src/main/java/io/trino/plugin/clickhouse/ClickHouseClient.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@
157157
import static io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling;
158158
import static io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR;
159159
import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
160-
import static io.trino.spi.StandardErrorCode.INVALID_TABLE_PROPERTY;
161160
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
162161
import static io.trino.spi.StandardErrorCode.SCHEMA_NOT_EMPTY;
163162
import static io.trino.spi.connector.ConnectorMetadata.MODIFYING_ROWS_MESSAGE;
@@ -401,9 +400,8 @@ protected List<String> createTableSqls(RemoteTableName remoteTableName, List<Str
401400
ClickHouseEngineType engine = ClickHouseTableProperties.getEngine(tableProperties);
402401
tableOptions.add("ENGINE = " + engine.getEngineType());
403402
if (engine == ClickHouseEngineType.MERGETREE && formatProperty(ClickHouseTableProperties.getOrderBy(tableProperties)).isEmpty()) {
404-
// order_by property is required
405-
throw new TrinoException(INVALID_TABLE_PROPERTY,
406-
format("The property of %s is required for table engine %s", ClickHouseTableProperties.ORDER_BY_PROPERTY, engine.getEngineType()));
403+
// https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#order_by
404+
tableOptions.add("ORDER BY tuple()");
407405
}
408406
formatProperty(ClickHouseTableProperties.getOrderBy(tableProperties)).ifPresent(value -> tableOptions.add("ORDER BY " + value));
409407
formatProperty(ClickHouseTableProperties.getPrimaryKey(tableProperties)).ifPresent(value -> tableOptions.add("PRIMARY KEY " + value));

plugin/trino-clickhouse/src/test/java/io/trino/plugin/clickhouse/TestClickHouseConnectorTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ public void testDifferentEngine()
355355
assertThat(getQueryRunner().tableExists(getSession(), tableName)).isTrue();
356356
assertUpdate("DROP TABLE " + tableName);
357357
// MergeTree without order by
358-
assertQueryFails("CREATE TABLE " + tableName + " (id int NOT NULL, x VARCHAR) WITH (engine = 'MergeTree')", "The property of order_by is required for table engine MergeTree\\(\\)");
358+
assertUpdate("CREATE TABLE " + tableName + " (id int NOT NULL, x VARCHAR) WITH (engine = 'MergeTree')");
359+
assertThat(getQueryRunner().tableExists(getSession(), tableName)).isTrue();
360+
assertUpdate("DROP TABLE " + tableName);
359361

360362
// MergeTree with optional
361363
assertUpdate("CREATE TABLE " + tableName + " (id int NOT NULL, x VARCHAR, logdate DATE NOT NULL) WITH " +
@@ -373,7 +375,7 @@ public void testDifferentEngine()
373375

374376
//NOT support engine
375377
assertQueryFails("CREATE TABLE " + tableName + " (id int NOT NULL, x VARCHAR) WITH (engine = 'bad_engine')",
376-
"line 1:76: Unable to set catalog 'clickhouse' table property 'engine' to.*");
378+
".* Unable to set catalog 'clickhouse' table property 'engine' to.*");
377379
}
378380

379381
@Test
@@ -509,15 +511,15 @@ public void testTableProperty()
509511

510512
// Primary key must be a prefix of the sorting key,
511513
assertQueryFails("CREATE TABLE " + tableName + " (id int NOT NULL, x boolean NOT NULL, y boolean NOT NULL) WITH (engine = 'MergeTree', order_by = ARRAY['id'], sample_by = ARRAY['x', 'y'])",
512-
"line 1:151: Invalid value for catalog 'clickhouse' table property 'sample_by': .*");
514+
".* Invalid value for catalog 'clickhouse' table property 'sample_by': .*");
513515

514516
// wrong property type
515517
assertQueryFails("CREATE TABLE " + tableName + " (id int NOT NULL) WITH (engine = 'MergeTree', order_by = 'id')",
516-
"line 1:87: Invalid value for catalog 'clickhouse' table property 'order_by': .*");
518+
".* Invalid value for catalog 'clickhouse' table property 'order_by': .*");
517519
assertQueryFails("CREATE TABLE " + tableName + " (id int NOT NULL) WITH (engine = 'MergeTree', order_by = ARRAY['id'], primary_key = 'id')",
518-
"line 1:111: Invalid value for catalog 'clickhouse' table property 'primary_key': .*");
520+
".* Invalid value for catalog 'clickhouse' table property 'primary_key': .*");
519521
assertQueryFails("CREATE TABLE " + tableName + " (id int NOT NULL) WITH (engine = 'MergeTree', order_by = ARRAY['id'], primary_key = ARRAY['id'], partition_by = 'id')",
520-
"line 1:138: Invalid value for catalog 'clickhouse' table property 'partition_by': .*");
522+
".* Invalid value for catalog 'clickhouse' table property 'partition_by': .*");
521523
}
522524

523525
@Test

0 commit comments

Comments
 (0)