Skip to content
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
3 changes: 3 additions & 0 deletions packages/db/src/clickhouse/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ function getClickhouseSettings(): ClickHouseSettings {
return {
distributed_product_mode: 'allow',
date_time_input_format: 'best_effort',
// ClickHouse 26.3+ defaults async_insert to 1. The buffers already batch
// client-side, so inserts stay synchronous; a buffer can still opt in per query.
async_insert: 0,
...(process.env.CLICKHOUSE_SETTINGS_REMOVE_CONVERT_ANY_JOIN
? {}
: {
Expand Down
13 changes: 13 additions & 0 deletions packages/db/src/clickhouse/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ export async function getExistingTables() {
}
}

// A MergeTree setting that is unknown to the server fails the CREATE, so a
// migration checks before it sets one that only newer releases know.
export async function hasMergeTreeSetting(name: string): Promise<boolean> {
const result = await chMigrationClient.query({
query:
'SELECT count() AS total FROM system.merge_tree_settings WHERE name = {name:String}',
query_params: { name },
format: 'JSONEachRow',
});
const [row] = await result.json<{ total: string }>();
return Number(row?.total ?? 0) > 0;
}

export interface TableKeys {
sortingKey: string[];
partitionKey: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createMaterializedView,
dropTable,
getExistingTables,
hasMergeTreeSetting,
runClickhouseMigrationCommands,
} from '../clickhouse/migration';
import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers';
Expand All @@ -14,6 +15,12 @@ export async function up(env: CodeMigrationEnv) {
const replicatedVersion = '1';
const existingTables = await getExistingTables();
const isClustered = getIsCluster(env);
// property_value is grouped on but sits outside the sorting key. ClickHouse
// 26.7+ rejects that for AggregatingMergeTree unless allowed; older servers
// do not know the setting and would reject the CREATE for naming it.
const allowDimensionsOutsideSortingKey = await hasMergeTreeSetting(
'allow_dimensions_outside_sorting_key'
);

const sqls: string[] = [];

Expand All @@ -38,6 +45,9 @@ export async function up(env: CodeMigrationEnv) {
'event_date',
],
partitionBy: 'toYYYYMM(event_date)',
settings: allowDimensionsOutsideSortingKey
? { allow_dimensions_outside_sorting_key: 1 }
: {},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
query: `SELECT
project_id,
profile_id,
Expand Down
Loading