| 66 | * Handles both clustered and non-clustered scenarios |
| 67 | */ |
| 68 | export function createTable({ |
| 69 | name: tableName, |
| 70 | columns, |
| 71 | indices = [], |
| 72 | engine = 'MergeTree()', |
| 73 | orderBy = ['tuple()'], |
| 74 | partitionBy, |
| 75 | settings = {}, |
| 76 | distributionHash, |
| 77 | replicatedVersion, |
| 78 | isClustered, |
| 79 | }: CreateTableOptions): string[] { |
| 80 | const columnDefinitions = [...columns, ...indices].join(',\n '); |
| 81 | |
| 82 | const settingsClause = Object.entries(settings).length |
| 83 | ? `SETTINGS ${Object.entries(settings) |
| 84 | .map(([key, value]) => `${key} = ${value}`) |
| 85 | .join(', ')}` |
| 86 | : ''; |
| 87 | |
| 88 | const partitionByClause = partitionBy ? `PARTITION BY ${partitionBy}` : ''; |
| 89 | |
| 90 | if (!isClustered) { |
| 91 | // Non-clustered scenario: single table |
| 92 | return [ |
| 93 | `CREATE TABLE IF NOT EXISTS ${tableName} ( |
| 94 | ${columnDefinitions} |
| 95 | ) |
| 96 | ENGINE = ${engine} |
| 97 | ${partitionByClause} |
| 98 | ORDER BY (${orderBy.join(', ')}) |
| 99 | ${settingsClause}`.trim(), |
| 100 | ]; |
| 101 | } |
| 102 | |
| 103 | return [ |
| 104 | // Local replicated table |
| 105 | `CREATE TABLE IF NOT EXISTS ${replicated(tableName)} ON CLUSTER '{cluster}' ( |
| 106 | ${columnDefinitions} |
| 107 | ) |
| 108 | ENGINE = Replicated${engine.replace(/^(.+?)\((.+?)?\)/, `$1('${CLUSTER_REPLICA_PATH.replace('{replicatedVersion}', replicatedVersion)}', '{replica}', $2)`).replace(/, \)$/, ')')} |
| 109 | ${partitionByClause} |
| 110 | ORDER BY (${orderBy.join(', ')}) |
| 111 | ${settingsClause}`.trim(), |
| 112 | // Distributed table |
| 113 | `CREATE TABLE IF NOT EXISTS ${tableName} ON CLUSTER '{cluster}' AS ${replicated(tableName)} |
| 114 | ENGINE = Distributed('{cluster}', currentDatabase(), ${replicated(tableName)}, ${distributionHash})`, |
| 115 | ]; |
| 116 | } |
| 117 | |
| 118 | export const modifyTTL = ({ |
| 119 | tableName, |