From 04473a21c8bb6024e4813c2b7fbfc166a5db4e21 Mon Sep 17 00:00:00 2001 From: Anton Polonskiy Date: Sun, 8 Feb 2026 02:01:13 +0200 Subject: [PATCH] ClickHouse: fix minor problems * ClickHouse: Fix warning on empty tables Warning: reset() expects parameter 1 to be array, null given * ClickHouse: Show correct error message * ClickHouse: add CTE support + allow whitespace at the beginning of the query * ClickHouse: set default format --- plugins/drivers/clickhouse.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/plugins/drivers/clickhouse.php b/plugins/drivers/clickhouse.php index 59e4d047..e443df45 100644 --- a/plugins/drivers/clickhouse.php +++ b/plugins/drivers/clickhouse.php @@ -15,14 +15,17 @@ if (isset($_GET["clickhouse"])) { function rootQuery($db, $query) { $file = @file_get_contents("$this->url/?database=$db", false, stream_context_create(array('http' => array( 'method' => 'POST', - 'content' => $this->isQuerySelectLike($query) ? "$query FORMAT JSONCompact" : $query, - 'header' => 'Content-type: application/x-www-form-urlencoded', + 'content' => $query, + 'header' => array( + 'Content-Type: application/x-www-form-urlencoded', + 'X-ClickHouse-Format: JSONCompact', + ), 'ignore_errors' => 1, 'follow_location' => 0, 'max_redirects' => 0, )))); - if ($file === false || !preg_match('~^HTTP/[0-9.]+ 2~i', $http_response_header[0])) { + if ($file === false || preg_match('~^HTTP/[0-9.]+ 403~i', $http_response_header[0])) { $this->error = lang('Invalid credentials.'); return false; } @@ -45,11 +48,18 @@ if (isset($_GET["clickhouse"])) { } } } + // 400 == Syntax error + // 404 == Unknown expression identifier + // 500 == Column 'x' is not under aggregate function and not in GROUP BY keys + if (preg_match('~^HTTP/[0-9.]+ [45]~i', $http_response_header[0])) { + $this->error = $return['exception']; + return false; + } return new Result($return); } function isQuerySelectLike($query) { - return (bool) preg_match('~^(select|show)~i', $query); + return (bool) preg_match('~^\s*(select|show|with)~i', $query); } function query($query, $unbuffered = false) { @@ -75,7 +85,7 @@ if (isset($_GET["clickhouse"])) { class Result { public $num_rows, $columns, $meta; - private $rows, $offset = 0; + private $rows = array(), $offset = 0; function __construct($result) { foreach ($result['data'] as $item) {