smi2/phpclickhouse
PHP client for ClickHouse with sync and async queries, native parameter binding, bulk inserts, generators, gzip compression, cluster support, streaming, and progress callbacks. Requires PHP 8.1+, ext-curl, ext-json.
ClickHouse facade over the client.StringType, FixedString(N), Date, DateTime, Enum8, Enum16, Int8–Int256, UInt8–UInt256, Float32/64, and Decimal32–Decimal256 can be used at the application boundary before passing values into queries.StringableType values are escaped by ValueFormatter, bindings, and native parameters; numeric fromString() validation closes an SQL-injection vector for untrusted input when these wrappers are adopted.DatabaseException::getServerVersion() and getServerStackTrace() improve production diagnostics, but the server stack trace is not included in getMessage() and requires per-query ['stacktrace' => 1].DateTime64, UUID, decimals, and enum types.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| PHP runtime breaking change | 1.26.925 requires PHP ^8.1; PHP 8.0 users must pin 1.26.4. |
Confirm all app/CI/container runtimes are PHP 8.1+ before upgrade; align with Laravel 10+ / Laravel 9 on PHP 8.1+. |
| Type Mismatch | Laravel/Eloquent expects Carbon and native PHP scalars; ClickHouse uses DateTime64, Decimal, Enum, etc. |
Use custom casts/accessors and the new type wrappers at query boundaries. |
Legacy Type escaping |
Release notes indicate legacy Type values are still interpolated into SQL unescaped; new wrappers are the safe path. |
Audit existing raw Type usage; migrate untrusted inputs to StringableType / ScalarNumericType wrappers. |
| Decimal precision | json_decode() precision trap applies for >16-digit decimals; exact reads need toString(). |
Identify financial/high-precision columns; read as strings where exactness matters. |
| Connection Pooling | No built-in Laravel connection pooling for ClickHouse. | Use PHP-CLI workers, queue-based writes, and Redis/cache for hot reads where appropriate. |
| Async Query Handling | Laravel’s synchronous request model may block on slow ClickHouse queries. | Offload heavy inserts/queries to Laravel Queues; set per-query timeouts such as max_execution_time. |
| Schema Migrations | ClickHouse DDL differs from Laravel migrations. | Use custom Artisan commands or explicit schema-management scripts. |
| Error Handling | Improved, but server stack traces require opt-in and explicit getter use. | Log getServerVersion() / getServerStackTrace(); avoid enabling stack traces globally unless needed. |
| Performance Overhead | HTTP client may add latency versus native drivers. | Benchmark against expected QPS; cache frequent analytics responses. |
1.26.4?Type objects rather than the new validated/escaped wrappers?getServerVersion() and getServerStackTrace() in logs? Will per-query stacktrace be enabled selectively?| Laravel Component | Integration Strategy |
|---|---|
| Database Layer | Register ClickHouse as a secondary connection in config/database.php. |
| Query Builder | Extend with macros for ClickHouse-specific syntax and native parameters. |
| Eloquent | Use custom casts/accessors for DateTime64, Decimal, Enum, and UUID; consider repositories for complex analytical queries. |
| Migrations | Use custom Artisan commands or schema scripts; do not assume Laravel migrations cover ClickHouse DDL. |
| Queues/Jobs | Offload bulk inserts and slow analytical queries to Laravel Queues. |
| API Layer | Use ClickHouse for analytics endpoints such as /metrics and /reports; cache frequent responses. |
| Caching | Cache high-cost ClickHouse reads in Redis or Laravel Cache. |
| Validation / Formatting | Adopt new type wrappers for untrusted strings, numerics, dates, and enums before query execution. |
1.26.4 only for services still on PHP 8.0.Type usage and identify untrusted inputs that should move to StringableType / ScalarNumericType.DB::connection('clickhouse') or a facade for read queries.toString() where precision matters.| Laravel / PHP Feature | Compatibility Notes |
|---|---|
| PHP 8.1+ | Required by 1.26.925; PHP 8.0 users must pin older release. |
| Laravel 10+ | Strong fit on PHP 8.1+; package remains framework-agnostic but integrable. |
| Laravel 9 | Feasible where runtime is PHP 8.1+, but validate package CI coverage and app dependencies. |
| Eloquent Models | Works via custom casts/accessors, not as a full native Eloquent driver. |
| Migrations | Requires custom Artisan commands or external schema management. |
| Query Builder | Extendable via macros; use native parameters where possible. |
| Queues | Use Laravel Queues for async inserts and slow analytical queries. |
| Caching | Cache ClickHouse results in Redis/Laravel Cache for hot analytics. |
| Observability | Structured exceptions improve diagnostics; server stack traces require opt-in per query. |
Type interpolation where possible.getServerVersion() and getServerStackTrace().| Task | Effort Level | Notes |
|---|---|---|
| Dependency Updates | Medium | PHP 8.1 is now the floor; monitor compatibility with Laravel and PHP 8.2–8.4. |
| Schema Changes | High | ClickHouse lacks native Laravel migrations; maintain explicit DDL scripts. |
| Type Wrapper Adoption | Medium | New wrappers improve safety but require auditing existing Type usage. |
| Exception Handling | Low–Medium | Structured exceptions improve support; ensure logs include server version/stack trace getters. |
| ClickHouse Version Testing | Medium | Package tests CH 21.9 / 26.3 / 26.9; production version must be in CI or staging. |
1.26.4.Type usage remains a risk.| Failure Mode | Impact | Mitigation |
|---|---|---|
| PHP version mismatch | Composer install/upgrade failure | Enforce PHP 8.1+ in CI/containers; pin 1.26.4 where needed. |
Legacy Type injection risk |
SQL injection if untrusted values are interpolated | Migrate to StringableType / ScalarNumericType wrappers. |
| Decimal precision loss | Incorrect financial/high-precision analytics | Use toString() for exact decimal reads. |
| Slow analytical query | Request latency or timeout | Queue heavy queries; set max_execution_time; cache results. |
| ClickHouse schema drift | Query failures after deployment | Use explicit DDL scripts and staged schema validation. |
| Missing server stack trace | Slower production debugging | Enable per-query stacktrace where needed and log getServerStackTrace(). |
| ClickHouse version mismatch | Untested SQL/type behavior | Run CI/staging against the production ClickHouse version. |
How can I help you explore Laravel packages today?