|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Clickhouse; |
|
6
|
|
|
use Basis\Test; |
|
7
|
|
|
use ClickHouseDB\Client; |
|
8
|
|
|
|
|
9
|
|
|
class ClickhouseTest extends Test |
|
10
|
|
|
{ |
|
11
|
|
|
public function setUp(): void |
|
12
|
|
|
{ |
|
13
|
|
|
parent::setup(); |
|
14
|
|
|
$client = $this->get(Client::class); |
|
15
|
|
|
|
|
16
|
|
|
// recreate test database |
|
17
|
|
|
$client->database('default'); |
|
18
|
|
|
$client->write('drop database test'); |
|
19
|
|
|
$client->write('create database if not exists test'); |
|
20
|
|
|
$client->database('test'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testConnectionConfiguration() |
|
24
|
|
|
{ |
|
25
|
|
|
$clickhouse = $this->get(Client::class); |
|
26
|
|
|
$this->assertSame($clickhouse->getConnectHost(), '127.0.0.1'); |
|
27
|
|
|
$this->assertSame($this->get(Client::class), $clickhouse); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testBulkInsert() |
|
31
|
|
|
{ |
|
32
|
|
|
$client = $this->get(Client::class); |
|
33
|
|
|
$query = "create table if not exists test.tester ( |
|
34
|
|
|
date Date, |
|
35
|
|
|
time DateTime, |
|
36
|
|
|
id String, |
|
37
|
|
|
value String |
|
38
|
|
|
) |
|
39
|
|
|
engine=MergeTree(date, (id), 8192)"; |
|
40
|
|
|
|
|
41
|
|
|
$client->write($query); |
|
42
|
|
|
|
|
43
|
|
|
$headers = ['date', 'time', 'id', 'value']; |
|
44
|
|
|
|
|
45
|
|
|
$data = []; |
|
46
|
|
|
foreach (range(1, 20) as $i) { |
|
47
|
|
|
$data[] = [date('Y-m-d'), time(), "$i", "the $i"]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$clickhouse = $this->get(Clickhouse::class); |
|
51
|
|
|
$clickhouse->bucketSize = 10; |
|
52
|
|
|
|
|
53
|
|
|
$buckets = $this->insert('tester', $data, $headers); |
|
54
|
|
|
$this->assertSame(2, $buckets); |
|
55
|
|
|
$select = $this->select('*', 'tester', []); |
|
56
|
|
|
$this->assertCount(20, $select->rows()); |
|
57
|
|
|
|
|
58
|
|
|
$clickhouse->bucketSize = 100; |
|
59
|
|
|
|
|
60
|
|
|
$buckets = $this->insert('tester', $data, $headers); |
|
61
|
|
|
$this->assertSame(1, $buckets); |
|
62
|
|
|
|
|
63
|
|
|
$select = $this->select('*', 'tester', []); |
|
64
|
|
|
$this->assertCount(40, $select->rows()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testSelect() |
|
68
|
|
|
{ |
|
69
|
|
|
$client = $this->get(Client::class); |
|
70
|
|
|
$query = "create table if not exists test.tester ( |
|
71
|
|
|
date Date, |
|
72
|
|
|
time DateTime, |
|
73
|
|
|
id String, |
|
74
|
|
|
value String |
|
75
|
|
|
) |
|
76
|
|
|
engine=MergeTree(date, (id), 8192)"; |
|
77
|
|
|
|
|
78
|
|
|
$client->write($query); |
|
79
|
|
|
|
|
80
|
|
|
$headers = ['date', 'time', 'id', 'value']; |
|
81
|
|
|
|
|
82
|
|
|
$data = [ |
|
83
|
|
|
[date('Y-m-d'), time(), "1", "nekufa"], |
|
84
|
|
|
[date('Y-m-d'), time(), "2", "petya"], |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
$this->insert('tester', $data, $headers); |
|
88
|
|
|
|
|
89
|
|
|
$select = $this->select('*', 'tester', ['id' => "1"]); |
|
90
|
|
|
$this->assertCount(1, $select->rows()); |
|
91
|
|
|
|
|
92
|
|
|
$select = $this->select('*', 'tester', ['id' => "2"]); |
|
93
|
|
|
$this->assertCount(1, $select->rows()); |
|
94
|
|
|
|
|
95
|
|
|
$select = $this->select('*', 'tester', []); |
|
96
|
|
|
$this->assertCount(2, $select->rows()); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|