Skip to content

Commit 6e5fa51

Browse files
Copilotantonkomarev
andcommitted
Add support for ClickHouse settings configuration
Co-authored-by: antonkomarev <1849174+antonkomarev@users.noreply.github.com>
1 parent 634a049 commit 6e5fa51

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ php artisan vendor:publish --provider="Cog\Laravel\Clickhouse\ClickhouseServiceP
4949

5050
Edit `config/clickhouse.php` file.
5151

52+
### ClickHouse settings
53+
54+
You can configure ClickHouse session settings in the configuration file. These settings will be applied to all queries made through the client.
55+
56+
For example, to enable nested JSON storage using the experimental Object type:
57+
58+
```php
59+
'connection' => [
60+
// ... other connection settings
61+
'settings' => [
62+
'allow_experimental_object_type' => 1,
63+
],
64+
],
65+
```
66+
67+
Common settings you might want to configure:
68+
- `allow_experimental_object_type` - Enable JSON/Object type for nested data structures
69+
- `max_execution_time` - Maximum query execution time in seconds
70+
- `max_memory_usage` - Maximum memory usage for query execution
71+
72+
See [ClickHouse settings documentation](https://clickhouse.com/docs/en/operations/settings/settings) for all available settings.
73+
5274
## Usage
5375

5476
### ClickHouse client

config/clickhouse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
'timeout' => 1,
3434
'connectTimeOut' => 2,
3535
],
36+
'settings' => [
37+
// ClickHouse session settings
38+
// Example: 'allow_experimental_object_type' => 1,
39+
],
3640
],
3741

3842
/*

src/Factory/ClickhouseClientFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ public function create(
4242
}
4343

4444
$options = [];
45+
$settings = [];
4546

4647
if (isset($config['options'])) {
4748
$options = $config['options'];
4849

4950
unset($config['options']);
5051
}
5152

53+
if (isset($config['settings'])) {
54+
$settings = $config['settings'];
55+
56+
unset($config['settings']);
57+
}
58+
5259
$client = new Client($config);
5360

5461
foreach ($options as $option => $value) {
@@ -57,6 +64,10 @@ public function create(
5764
$client->$method($value);
5865
}
5966

67+
foreach ($settings as $setting => $value) {
68+
$client->settings()->set($setting, $value);
69+
}
70+
6071
return $client;
6172
}
6273

tests/Factory/ClickhouseClientFactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,33 @@ public function testInitializationWithNonExistsOption(): void
7272
self::assertSame(ClickhouseConfigException::class, get_class($exception));
7373
}
7474
}
75+
76+
public function testInitializationWithSettings(): void
77+
{
78+
$clickhouse = new ClickhouseClientFactory(
79+
[
80+
'host' => 'example.com',
81+
'port' => '9000',
82+
'username' => 'test_user',
83+
'password' => 'secret',
84+
'options' => [
85+
'database' => 'test_database',
86+
],
87+
'settings' => [
88+
'max_execution_time' => 60,
89+
'allow_experimental_object_type' => 1,
90+
],
91+
]
92+
);
93+
94+
$client = $clickhouse->create();
95+
96+
self::assertSame('example.com', $client->getConnectHost());
97+
self::assertSame('9000', $client->getConnectPort());
98+
self::assertSame('test_user', $client->getConnectUsername());
99+
self::assertSame('secret', $client->getConnectPassword());
100+
self::assertSame('test_database', $client->settings()->getDatabase());
101+
self::assertSame(60, $client->settings()->getSetting('max_execution_time'));
102+
self::assertSame(1, $client->settings()->getSetting('allow_experimental_object_type'));
103+
}
75104
}

0 commit comments

Comments
 (0)