Skip to content

Commit 9d09ca1

Browse files
authored
Added query stats
Added query stats
2 parents c5ecca6 + ad41502 commit 9d09ca1

11 files changed

+463
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* added query stats
12
* added ReadTokenFromFile
23
* added lambda on exception in retryTransaction
34

src/Enums/CollectQueryStatsMode.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace YdbPlatform\Ydb\Enums;
3+
use UnexpectedValueException;
4+
use Ydb\Table\QueryStatsCollection\Mode;
5+
6+
class CollectQueryStatsMode
7+
{
8+
9+
const STATS_COLLECTION_UNSPECIFIED = Mode::STATS_COLLECTION_UNSPECIFIED;
10+
11+
const STATS_COLLECTION_NONE = Mode::STATS_COLLECTION_NONE;
12+
13+
const STATS_COLLECTION_BASIC = Mode::STATS_COLLECTION_BASIC;
14+
15+
const STATS_COLLECTION_FULL = Mode::STATS_COLLECTION_FULL;
16+
17+
const STATS_COLLECTION_PROFILE = Mode::STATS_COLLECTION_PROFILE;
18+
19+
private static $valueToName = [
20+
self::STATS_COLLECTION_UNSPECIFIED => 'STATS_COLLECTION_UNSPECIFIED',
21+
self::STATS_COLLECTION_NONE => 'STATS_COLLECTION_NONE',
22+
self::STATS_COLLECTION_BASIC => 'STATS_COLLECTION_BASIC',
23+
self::STATS_COLLECTION_FULL => 'STATS_COLLECTION_FULL',
24+
self::STATS_COLLECTION_PROFILE => 'STATS_COLLECTION_PROFILE',
25+
];
26+
27+
public static function name($value)
28+
{
29+
if (!isset(self::$valueToName[$value])) {
30+
throw new UnexpectedValueException(sprintf(
31+
'Enum %s has no name defined for value %s', __CLASS__, $value));
32+
}
33+
return self::$valueToName[$value];
34+
}
35+
36+
37+
public static function value($name)
38+
{
39+
$const = __CLASS__ . '::' . strtoupper($name);
40+
if (!defined($const)) {
41+
throw new UnexpectedValueException(sprintf(
42+
'Enum %s has no value defined for name %s', __CLASS__, $name));
43+
}
44+
return constant($const);
45+
}
46+
}

src/QueryResult.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
namespace YdbPlatform\Ydb;
44

55
use DateTime;
6+
use YdbPlatform\Ydb\QueryStats\QueryStats;
67

78
class QueryResult
89
{
910
protected $columns = [];
1011
protected $rows = [];
1112
protected $truncated = false;
1213

14+
/**
15+
* @var QueryStats|null
16+
*/
17+
protected $queryStats = null;
18+
1319
public function __construct($result)
1420
{
1521
if (method_exists($result, 'getResultSets'))
@@ -42,6 +48,11 @@ public function __construct($result)
4248
{
4349
throw new Exception('Unknown result');
4450
}
51+
52+
if (method_exists($result, 'getQueryStats') && $result->getQueryStats())
53+
{
54+
$this->queryStats = new QueryStats($result->getQueryStats());
55+
}
4556
}
4657

4758
public function isTruncated()
@@ -233,4 +244,13 @@ protected function fillRows($rows)
233244
$this->rows[] = $_row;
234245
}
235246
}
247+
248+
/**
249+
* @return QueryStats|null
250+
*/
251+
public function getQueryStats(): ?QueryStats
252+
{
253+
return $this->queryStats;
254+
}
255+
236256
}

src/QueryStats/CompilationStats.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\QueryStats;
4+
5+
class CompilationStats
6+
{
7+
/**
8+
* @var \Ydb\TableStats\CompilationStats
9+
*/
10+
protected $compilationStats;
11+
12+
public function __construct(\Ydb\TableStats\CompilationStats $compilationStats)
13+
{
14+
$this->compilationStats = $compilationStats;
15+
}
16+
17+
/**
18+
* @return bool
19+
*/
20+
public function getFromCache(): bool
21+
{
22+
return $this->compilationStats->getFromCache();
23+
}
24+
25+
/**
26+
* @return int|string
27+
*/
28+
public function getDurationUs()
29+
{
30+
return $this->compilationStats->getDurationUs();
31+
}
32+
33+
/**
34+
* @return int|string
35+
*/
36+
public function getCpuTimeUs()
37+
{
38+
return $this->compilationStats->getCpuTimeUs();
39+
}
40+
}

src/QueryStats/OperationStats.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\QueryStats;
4+
5+
class OperationStats
6+
{
7+
8+
public function __construct(\Ydb\TableStats\OperationStats $operationStats)
9+
{
10+
$this->operationStats = $operationStats;
11+
}
12+
13+
/**
14+
* @return int|string
15+
*/
16+
public function getRows()
17+
{
18+
return $this->operationStats->getRows();
19+
}
20+
21+
/**
22+
* @return int|string
23+
*/
24+
public function getBytes()
25+
{
26+
return $this->operationStats->getBytes();
27+
}
28+
}

src/QueryStats/QueryPhaseStats.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\QueryStats;
4+
5+
class QueryPhaseStats
6+
{
7+
/**
8+
* @var \Ydb\TableStats\QueryPhaseStats
9+
*/
10+
protected $phaseStats;
11+
12+
public function __construct(\Ydb\TableStats\QueryPhaseStats $phaseStats)
13+
{
14+
$this->phaseStats = $phaseStats;
15+
}
16+
17+
18+
/**
19+
* @return int|string
20+
*/
21+
public function getDurationUs()
22+
{
23+
return $this->phaseStats->getDurationUs();
24+
}
25+
26+
/**
27+
* @return TableAccessStats[]
28+
*/
29+
public function getTableAccess()
30+
{
31+
$result = iterator_to_array($this->phaseStats->getTableAccess());
32+
foreach ($result as $key=>$item) {
33+
$result[$key] = new TableAccessStats($item);
34+
}
35+
return $result;
36+
}
37+
38+
/**
39+
* @return int|string
40+
*/
41+
public function getCpuTimeUs()
42+
{
43+
return $this->phaseStats->getCpuTimeUs();
44+
}
45+
46+
/**
47+
* @return int|string
48+
*/
49+
public function getAffectedShards()
50+
{
51+
return $this->phaseStats->getAffectedShards();
52+
}
53+
54+
/**
55+
* @return bool
56+
*/
57+
public function getLiteralPhase()
58+
{
59+
return $this->phaseStats->getLiteralPhase();
60+
}
61+
}

src/QueryStats/QueryStats.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\QueryStats;
4+
5+
class QueryStats
6+
{
7+
8+
protected $queryStats;
9+
public function __construct(\Ydb\TableStats\QueryStats $queryStats)
10+
{
11+
$this->queryStats = $queryStats;
12+
}
13+
14+
/**
15+
* @return QueryPhaseStats[]
16+
*/
17+
public function getQueryPhases()
18+
{
19+
$result = iterator_to_array($this->queryStats->getQueryPhases()->getIterator());
20+
foreach ($result as $key=>$item) {
21+
$result[$key] = new QueryPhaseStats($item);
22+
}
23+
return $result;
24+
}
25+
26+
/**
27+
* @return CompilationStats|null
28+
*/
29+
public function getCompilation(): ?CompilationStats
30+
{
31+
if($this->queryStats->getQueryPhases()){
32+
return new CompilationStats($this->queryStats->getCompilation());
33+
} else {
34+
return null;
35+
}
36+
}
37+
38+
/**
39+
* @return int|string
40+
*/
41+
public function getProcessCpuTimeUs()
42+
{
43+
return $this->queryStats->getProcessCpuTimeUs();
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getQueryPlan(): string
50+
{
51+
return $this->queryStats->getQueryPlan();
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getQueryAst(): string
58+
{
59+
return $this->queryStats->getQueryAst();
60+
}
61+
62+
/**
63+
* @return int|string
64+
*/
65+
public function getTotalDurationUs()
66+
{
67+
return $this->queryStats->getTotalDurationUs();
68+
}
69+
70+
/**
71+
* @return int|string
72+
*/
73+
public function getTotalCpuTimeUs()
74+
{
75+
return $this->queryStats->getTotalCpuTimeUs();
76+
}
77+
78+
}

0 commit comments

Comments
 (0)