PHPackages                             tangwei/clickhouse-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tangwei/clickhouse-builder

ActiveLibrary

tangwei/clickhouse-builder
==========================

Clickhouse sql query builder

v1.0.1(1y ago)12.7k—0%PHPPHP ^8.0

Since Jul 20Pushed 1y agoCompare

[ Source](https://github.com/tw2066/ClickhouseBuilder)[ Packagist](https://packagist.org/packages/tangwei/clickhouse-builder)[ RSS](/packages/tangwei-clickhouse-builder/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (5)Versions (7)Used By (0)

Clickhouse Query Builder
========================

[](#clickhouse-query-builder)

[![Build Status](https://camo.githubusercontent.com/5aaf9f6d06ae733260648d8bcc660706e5d4ff110f6e2919892e0dfdc5366fb1/68747470733a2f2f7472617669732d63692e6f72672f7468652d74696e646572626f782f436c69636b686f7573654275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/the-tinderbox/ClickhouseBuilder) [![Coverage Status](https://camo.githubusercontent.com/fd6a2b1a782a1158d764f3caa70e67d7459a81394c03729b212da4afe56d2c52/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7468652d74696e646572626f782f436c69636b686f7573654275696c6465722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/the-tinderbox/ClickhouseBuilder?branch=master)

Requirements
============

[](#requirements)

`php 8.0+`

Install
=======

[](#install)

Via composer

```
composer require tangwei/clickhouse-builder
```

Usage
=====

[](#usage)

For working query builder we must previously instantiate and pass in constructor `smi2/phpclickhouse`.

```
$config = [
    'host' => '127.0.0.1',
    'port' => '8123',
    'username' => 'user',
    'password' => 'pass',
];
$client = new \ClickHouseDB\Client($config);
$client->database('default');
$builder = new \Tinderbox\ClickhouseBuilder\Query\Builder($client);
```

After that we can build and perform sql queries.

### Select columns

[](#select-columns)

```
$builder->select('column', 'column2', 'column3 as alias');
$builder->select(['column', 'column2', 'column3 as alias']);
$builder->select(['column', 'column2', 'column3' => 'alias']);
```

All this calls will be transformed into next sql:

```
SELECT `column`, `column2`, `column3` AS `alias`
```

Also, as a column we can pass closure. In this case in closure will be passed instance of Column class, inside which we can setup column how we want. This can be useful for difficult expressions with many functions, subqueries and etc.

```
$builder->select(function ($column) {
    $column->name('time')->sumIf('time', '>', 10);
});
```

Will be compiled in:

```
SELECT sumIf(`time`, time > 10)
```

```
$builder->select(function ($column) {
    $column->as('alias') //or ->name('alias') in this case
    ->query()
    ->select('column')
    ->from('table');
});
```

Will be compiled in:

```
SELECT  (SELECT `column` FROM `table) as `alias`
```

Same behavior can be also achieved by any of the following approaches:

```
$1 = $builder->select(function ($column) {
         $column->as('alias') //or ->name('alias') in this case
            ->query(function ($query) {
                $query->select('column')->from('table');
            })
});
$2 = $builder->select(function ($column) {
         $column->as('alias') //or ->name('alias') in this case
            ->query($builder->select('column')->from('table'));
});
```

**Notice! Functions on columns is not stable and under development.**

### From

[](#from)

```
$builder->select('column')->from('table', 'alias');
```

Produce the following query:

```
SELECT `column` FROM `table` as `alias`
```

Also can be passed closure or builder as argument for performing sub query.

```
$builder->from(function ($from) {
    $from->query()->select('column')->from('table');
});
```

```
SELECT * FROM (SELECT `column` FROM `table`)
```

or

```
$builder->from(function ($from) {
    $from->query(function ($query) {
        $query->select('column')->from('table');
    });
});
```

or

```
$builder->from(function ($from) {
    $from->query($builder->select('column')->from('table'));
});
```

or

```
$builder->from($builder->select('column')->from('table'));
```

It is all variants of the same sql query which was listed above.

### Sample coefficient

[](#sample-coefficient)

```
$builder->select('column')->from('table')->sample(0.1);
```

```
SELECT `column` FROM `table` SAMPLE 0.1
```

I think there no need for additional words)

### Joins

[](#joins)

```
$builder->from('table')->join('another_table', 'any', 'left', ['column1', 'column2'], true, 'alias');
```

```
SELECT * FROM `table` GLOBAL ANY LEFT JOIN `another_table` AS `alias` USING `column1`, `column2`
```

For performing subquery as first argument you can pass closure or builder.

```
$builder->from('table')->join(function ($join) {
    $join->query()->select('column1', 'column2')->from('table2');
}, 'any', 'left', ['column1', 'column2']);

$builder->from('table')->join($builder->select('column1', 'column2')->from('table2'), 'any', 'left', ['column1', 'column2']);
```

```
SELECT * FROM `table` ANY LEFT JOIN (SELECT `column1`, `column2` FROM `table2`) USING `column1`, `column2`
```

Also there are many helper functions with hardcoded arguments, like strict or type and they combinations.

```
$builder->from('table')->anyLeftJoin('table', ['column']);
$builder->from('table')->allLeftJoin('table', ['column']);
$builder->from('table')->allInnerJoin('table', ['column']);
$builder->from('table')->anyInnerJoin('table', ['column']);

$buulder->from('table')->leftJoin('table', 'any', ['column']);
$buulder->from('table')->innerJoin('table', 'all', ['column']);
```

You can use array join as well.

```
$builder->from('test')->arrayJoin('someArr');
$builder->from('test')->leftArrayJoin('someArr');
```

```
SELECT * FROM `test` ARRAY JOIN `someArr`
SELECT * FROM `test` LEFT ARRAY JOIN `someArr`
```

### Temporary tables usage

[](#temporary-tables-usage)

There are some cases when you need to filter f.e. users by their ids, but amount of ids is huge. You can store users ids in local file, upload it to server and use it as temporary table.

Read more about local files [here](https://github.com/smi2/phpClickHouse) in section `Select WHERE IN ( local csv file )`.

#### Select

[](#select)

```
$file_name_data1 = '/tmp/temp_csv.txt';
$builder->table(raw('numbers(0,1000)')->attachFile($file_name_data1,'temp_tale', ['site_id' => 'Int32', 'site_hash' => 'String'], \ClickHouseDB\Query\WhereInFile::FORMAT_CSV)->get();
```

**If you want tables to be detected automatically, call `addFile` method before calling `whereIn`.**

You can use local files in `whereIn`, `prewhereIn`, `havingIn` and `join` statements of query builder.

#### Insert

[](#insert)

If you want to insert file or files into Clickhouse, you could use `insertBatchFiles` methods.

```
$builder->table('test')->insertBatchFiles('test.tsv',['event_time', 'site_key'], Format::TSV);

```

### Prewhere, where, having

[](#prewhere-where-having)

All example will be about where, but same behavior also is for prewhere and having.

```
$builder->from('table')->where('column', '=', 'value');
$builder->from('table')->where('column', 'value');
```

```
SELECT * FROM `table` WHERE `column` = 'value'
```

All string values will be wrapped with single quotes. If operator is not provided `=` will be used. If operator is not provided and value is an array, then `IN` will be used.

```
$builder->from('table')->where(function ($query) {
    $query->where('column1', 'value')->where('column2', 'value');
});
```

```
SELECT * FROM `table` WHERE (`column1` = 'value' AND `column2` = 'value')
```

If in the first argument was passed closure, then all wheres statements from inside will be wrapped with parenthesis. But if on that builder (inside closure) will be specified `from` then it will be transformed into subquery.

```
$builder->from('table')->where(function ($query) {
    $query->select('column')->from('table');
})
```

```
SELECT * FROM `table` WHERE (SELECT `column` FROM `table`)
```

Almost same is for value parameter, except wrapping into parenthesis. Any closure or builder instance passed as value will be converted into subquery.

```
$builder->from('table')->where('column', 'IN', function ($query) {
    $query->select('column')->from('table');
});
```

```
SELECT * FROM `table` WHERE `column` IN (SELECT `column` FROM `table`)
```

Also you can pass internal representation of this statement and it will be used. I will no talk about this with deeper explanation because its not preferable way to use this.

Like joins there are many helpers with hardcoded parameters.

```
$builder->where();
$builder->orWhere();

$builder->whereRaw();
$builer->orWhereRaw();

$builder->whereIn();
$builder->orWhereIn();

$builder->whereGlobalIn();
$builder->orWhereGlobalIn();

$builder->whereGlobalNotIn();
$builder->orWhereGlobalNotIn();

$builder->whereNotIn();
$builder->orWhereNotIn();

$builder->whereBetween();
$builder->orWhereBetween();

$builder->whereNotBetween();
$builder->orWhereNotBetween();

$builder->whereBetweenColumns();
$builder->orWhereBetweenColumns();

$builder->whereNotBetweenColumns();
$builder->orWhereNotBetweenColumns();
```

Also there is method to make where by dictionary:

```
$builder->whereDict('dict', 'attribute', 'key', '=', 'value');
```

```
SELECT dictGetString('dict', 'attribute', 'key') as `attribute` WHERE `attribute` = 'value'
```

If you want to use complex key, you may pass an array as `$key`, then array will be converted to tuple. By default all strings will be escaped by single quotes, but you may pass an `Identifier` instance to pass for example column name:

```
$builder->whereDict('dict', 'attribute', [new Identifier('column'), 'string value'], '=', 'value');
```

Will produce:

```
SELECT dictGetString('dict', 'attribute', tuple(`column`, 'string value')) as `attribute` WHERE `attribute` = 'value'
```

### Group By

[](#group-by)

Works like select.

```
$builder->from('table')->select('column', raw('count()'))->groupBy('attribute');
```

Final query will be like:

```
SELECT `column`, count() FROM `table` GROUP BY `attribute`
```

### Order By

[](#order-by)

```
$builder->from('table')->orderBy('column', 'asc', 'fr');
```

*In the example above, third argument is optional*

```
SELECT *  FROM `table` ORDER BY `column` ASC COLLATE 'fr'
```

Aliases:

```
$builder->orderByAsc('column');
$builder->orderByDesc('column');
```

For column there are same behaviour like in select method.

### Limit

[](#limit)

There are two types of limit. Limit and limit n by.

Limit n by:

```
$builder->from('table')->limitBy(1, 'column1', 'column2');
```

Will produce:

```
SELECT * FROM `table` LIMIT 1 BY `column1`, `column2`
```

Simple limit:

```
$builder->from('table')->limit(10, 100);
```

Will produce:

```
SELECT * FROM `table` LIMIT 100, 10
```

### Union ALL

[](#union-all)

In `unionAll` method can be passed closure or builder instance. In case of closure inside will be passed builder instance.

```
$builder->from('table')->unionAll(function($query) {
    $query->select('column1')->from('table');
})->unionAll($builder->select('column2')->from('table'));
```

```
SELECT * FROM `table` UNION ALL SELECT `column1` FROM `table` UNION ALL SELECT `column2` FROM `table`
```

### Performing request and getting result.

[](#performing-request-and-getting-result)

After building request you must call `get()` method for sending request to the server. Also there has opportunity to make asynchronous requests. Its works almost like `unionAll`.

```
$builder->from('table')->asyncWithQuery(function($query) {
    $query->from('table');
});
$builder->from('table')->asyncWithQuery($builder->from('table'));
$builder->from('table')->asyncWithQuery()->from('table');
```

This callings will produce the same behavior. Two queries which will be executed asynchronous. Now, if you call `get()` method, as result will be returned array, where numeric index correspond to the result of request with this number.

###  Health Score

33

—

LowBetter than 77% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

4

Last Release

627d ago

Major Versions

v0.1.0 → v1.0.02024-06-27

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/24579418?v=4)[Mr.tang](/maintainers/tw2066)[@tw2066](https://github.com/tw2066)

---

Top Contributors

[![FacedSID](https://avatars.githubusercontent.com/u/11896844?v=4)](https://github.com/FacedSID "FacedSID (24 commits)")[![evsign](https://avatars.githubusercontent.com/u/7965780?v=4)](https://github.com/evsign "evsign (19 commits)")[![tw2066](https://avatars.githubusercontent.com/u/24579418?v=4)](https://github.com/tw2066 "tw2066 (15 commits)")[![vitalcrazz](https://avatars.githubusercontent.com/u/6044393?v=4)](https://github.com/vitalcrazz "vitalcrazz (4 commits)")[![astro2049](https://avatars.githubusercontent.com/u/45759373?v=4)](https://github.com/astro2049 "astro2049 (2 commits)")[![rez1dent3](https://avatars.githubusercontent.com/u/5111255?v=4)](https://github.com/rez1dent3 "rez1dent3 (2 commits)")[![romanpravda](https://avatars.githubusercontent.com/u/59819062?v=4)](https://github.com/romanpravda "romanpravda (2 commits)")[![tianhe1986](https://avatars.githubusercontent.com/u/19562319?v=4)](https://github.com/tianhe1986 "tianhe1986 (1 commits)")[![pavemaksim](https://avatars.githubusercontent.com/u/29757628?v=4)](https://github.com/pavemaksim "pavemaksim (1 commits)")[![dikopylov](https://avatars.githubusercontent.com/u/38421064?v=4)](https://github.com/dikopylov "dikopylov (1 commits)")[![gitlog](https://avatars.githubusercontent.com/u/1937028?v=4)](https://github.com/gitlog "gitlog (1 commits)")[![leo108](https://avatars.githubusercontent.com/u/1551716?v=4)](https://github.com/leo108 "leo108 (1 commits)")[![agolovenkin](https://avatars.githubusercontent.com/u/2268899?v=4)](https://github.com/agolovenkin "agolovenkin (1 commits)")[![suvorovis](https://avatars.githubusercontent.com/u/34533360?v=4)](https://github.com/suvorovis "suvorovis (1 commits)")[![tachigami](https://avatars.githubusercontent.com/u/4858247?v=4)](https://github.com/tachigami "tachigami (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tangwei-clickhouse-builder/health.svg)

```
[![Health](https://phpackages.com/badges/tangwei-clickhouse-builder/health.svg)](https://phpackages.com/packages/tangwei-clickhouse-builder)
```

###  Alternatives

[paquettg/php-html-parser

An HTML DOM parser. It allows you to manipulate HTML. Find tags on an HTML page with selectors just like jQuery.

2.5k7.8M115](/packages/paquettg-php-html-parser)[area17/twill

Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible.

3.9k436.7k15](/packages/area17-twill)[qiniu/php-sdk

Qiniu Resource (Cloud) Storage SDK for PHP

8473.0M219](/packages/qiniu-php-sdk)[php-mqtt/client

An MQTT client written in and for PHP.

4502.0M20](/packages/php-mqtt-client)[netflie/whatsapp-cloud-api

The first PHP SDK to send and receive messages using a cloud-hosted version of the WhatsApp Business Platform

633402.7k4](/packages/netflie-whatsapp-cloud-api)[the-tinderbox/clickhouse-builder

Clickhouse sql query builder

1991.8M20](/packages/the-tinderbox-clickhouse-builder)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
