-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTableSizes.php
More file actions
58 lines (53 loc) · 1.91 KB
/
TableSizes.php
File metadata and controls
58 lines (53 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
namespace SimPod\ClickHouseClient\Snippet;
use Psr\Http\Client\ClientExceptionInterface;
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Exception\UnsupportedParamType;
use SimPod\ClickHouseClient\Exception\UnsupportedParamValue;
use SimPod\ClickHouseClient\Format\JsonEachRow;
use SimPod\ClickHouseClient\Sql\Expression;
/** @phpstan-type Entry array{table: string, database: string, size: string, min_date: string, max_date: string} */
final readonly class TableSizes
{
/**
* @return array<Entry>
*
* @throws ClientExceptionInterface
* @throws ServerError
* @throws UnsupportedParamType
* @throws UnsupportedParamValue
*/
public static function run(ClickHouseClient $clickHouseClient, string|null $databaseName = null): array
{
/** @var JsonEachRow<Entry> $format */
$format = new JsonEachRow();
return $clickHouseClient->selectWithParams(
<<<'CLICKHOUSE'
SELECT
name AS table,
database,
max(size) AS size,
min(min_date) AS min_date,
max(max_date) AS max_date
FROM system.tables
ANY LEFT JOIN (
SELECT
table,
database,
sum(bytes) AS size,
min(min_date) AS min_date,
max(max_date) AS max_date
FROM system.parts
WHERE active AND database = :database
GROUP BY table,database
) parts USING ( table, database )
WHERE database = :database AND storage_policy <> ''
GROUP BY table, database
CLICKHOUSE,
['database' => $databaseName ?? Expression::new('currentDatabase()')],
$format,
)->data;
}
}