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
30 changes: 26 additions & 4 deletions src/Audit/Adapter/ClickHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class ClickHouse extends SQL

private const DEFAULT_DATABASE = 'default';

/**
* @var list<string>
*/
private const LOW_CARDINALITY_COLUMNS = [
'event',
'actorType',
'resourceType',
'country',
];

/**
* Filter methods that must be supplied at least one value. Empty `values`
* arrays for these methods are rejected up front so they can't silently
Expand Down Expand Up @@ -766,16 +776,18 @@ public function setup(): void
$tableName = $this->getTableName();
$escapedDatabaseAndTable = $this->escapeIdentifier($this->database) . '.' . $this->escapeIdentifier($tableName);

$orderByExpr = $this->sharedTables ? '(tenant, time, id)' : '(time, id)';

// Create table with MergeTree engine for optimal performance
$createTableSql = "
CREATE TABLE IF NOT EXISTS {$escapedDatabaseAndTable} (
" . implode(",\n ", $columns) . ",
" . implode(",\n ", $indexes) . "
)
ENGINE = MergeTree()
ORDER BY (time, id)
ORDER BY {$orderByExpr}
PARTITION BY toYYYYMM(time)
SETTINGS index_granularity = 8192
SETTINGS index_granularity = 8192" . ($this->sharedTables ? ', allow_nullable_key = 1' : '') . "
";

$this->query($createTableSql);
Expand Down Expand Up @@ -1962,9 +1974,19 @@ protected function getColumnDefinition(string $id): string
? 'DateTime64(3)'
: 'String';

$nullable = !$attribute['required'] ? 'Nullable(' . $type . ')' : $type;
$required = (bool) $attribute['required'];

if ($type === 'String' && \in_array($id, self::LOW_CARDINALITY_COLUMNS, true)) {
$columnType = $required
? 'LowCardinality(String)'
: 'LowCardinality(Nullable(String))';

return "{$id} {$columnType}";
}

$columnType = !$required ? 'Nullable(' . $type . ')' : $type;

return "{$id} {$nullable}";
return "{$id} {$columnType}";
}

/**
Expand Down
66 changes: 65 additions & 1 deletion tests/Audit/Adapter/ClickHouseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function initializeAudit(): void
$username = getenv('CLICKHOUSE_USER') ?: 'default';
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
$secure = (bool) (getenv('CLICKHOUSE_SECURE') ?: false);
$secure = filter_var(getenv('CLICKHOUSE_SECURE') ?: false, FILTER_VALIDATE_BOOLEAN);

$clickHouse = new ClickHouse(
host: $host,
Expand Down Expand Up @@ -934,4 +934,68 @@ public function testOrderRandomRejectedWithColumnOrder(): void
Query::orderDesc('time'),
]);
}

public function testSharedTableSortKeyLeadsWithTenant(): void
{
$host = getenv('CLICKHOUSE_HOST') ?: 'clickhouse';
$username = getenv('CLICKHOUSE_USER') ?: 'default';
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
$secure = filter_var(getenv('CLICKHOUSE_SECURE') ?: false, FILTER_VALIDATE_BOOLEAN);
$database = getenv('CLICKHOUSE_DATABASE') ?: 'default';

$namespace = 'projtest_' . uniqid();

$adapter = new ClickHouse(
host: $host,
username: $username,
password: $password,
port: $port,
secure: $secure
);
$adapter->setDatabase($database);
$adapter->setNamespace($namespace);
$adapter->setSharedTables(true);
$adapter->setTenant(1);

$table = $namespace . '_audits';

$http = function (string $sql, array $params = []) use ($host, $port, $username, $password, $secure, $database): string {
$scheme = $secure ? 'https' : 'http';
$url = "{$scheme}://{$host}:{$port}/?database=" . rawurlencode($database)
. '&user=' . rawurlencode($username)
. '&password=' . rawurlencode($password);
foreach ($params as $key => $value) {
$url .= '&param_' . rawurlencode((string) $key) . '=' . rawurlencode((string) $value);
}
$ctx = stream_context_create(['http' => [
'method' => 'POST',
'header' => "Content-Type: text/plain\r\n",
'content' => $sql,
'timeout' => 15,
'ignore_errors' => true,
]]);
$out = @file_get_contents($url, false, $ctx);

return $out === false ? '' : trim((string) $out);
};

try {
(new Audit($adapter))->setup();

$sortingKey = $http(
'SELECT sorting_key FROM system.tables WHERE database = {db:String} AND name = {tbl:String}',
['db' => $database, 'tbl' => $table]
);

$this->assertTrue(
str_starts_with(trim($sortingKey), 'tenant'),
"Expected sorting key to lead with 'tenant', got: {$sortingKey}"
);
Comment on lines +991 to +994

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Normalize sorting key

ClickHouse reports multi-column system.tables.sorting_key values in tuple form, such as (tenant, time, id). With that format, this assertion fails even when setup() created the correct tenant-leading key, so the new live test can reject a valid schema.

} finally {
$escDb = '`' . str_replace('`', '``', $database) . '`';
$escTbl = '`' . str_replace('`', '``', $table) . '`';
$http("DROP TABLE IF EXISTS {$escDb}.{$escTbl}");
}
}
}
Loading