or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bigquery-client.mdclickhouse-client.mdcore-types.mdcredentials.mddatabricks-client.mdfactory-functions.mdindex.mdpostgres-client.mdredshift-client.mdsnowflake-client.mdsql-builders.mdssh-tunnel.mdtrino-client.md
tile.json

clickhouse-client.mddocs/

ClickHouse Client

ClickHouse client with named parameter support (NOT exported - use factory function).

Quick Start

import { warehouseClientFromCredentials } from '@lightdash/warehouses';
import { WarehouseTypes } from '@lightdash/common';

const client = warehouseClientFromCredentials({
  type: WarehouseTypes.CLICKHOUSE,
  host: 'localhost',
  port: 8123, // HTTP port
  user: 'default',
  password: 'mypassword',
  schema: 'analytics', // Database name in ClickHouse
  secure: true,
  timeoutSeconds: 30,
});

await client.test();

Key Features

  • Named Parameters: Type-safe query parameters with type hints
  • Native Streaming: JSONCompactEachRowWithNamesAndTypes format
  • Timezone Support: Session-level timezone configuration
  • Query Tags: Added as SQL comments

Common Patterns

Named Parameters

await client.streamQuery(
  `SELECT * FROM events
   WHERE timestamp > {startDate: DateTime}
     AND timestamp < {endDate: DateTime}
     AND user_id = {userId: UInt64}
     AND event_type = {eventType: String}`,
  (data) => { /* process rows */ },
  {
    queryParams: {
      startDate: '2024-01-01 00:00:00',
      endDate: '2024-01-31 23:59:59',
      userId: 12345,
      eventType: 'page_view',
    },
    timezone: 'Europe/London',
    tags: { source: 'app' },
  }
);

Array Parameters

await client.streamQuery(
  'SELECT * FROM events WHERE user_id IN {userIds: Array(UInt64)}',
  (data) => { /* process rows */ },
  { queryParams: { userIds: [100, 200, 300] }, tags: {} }
);

SQL Builder

Note: ClickhouseSqlBuilder is NOT exported. Use factory:

import { warehouseSqlBuilderFromType } from '@lightdash/warehouses';
import { SupportedDbtAdapter } from '@lightdash/common';

const builder = warehouseSqlBuilderFromType(SupportedDbtAdapter.CLICKHOUSE);
builder.getFieldQuoteChar(); // '"'
builder.escapeString("O'Reilly"); // "O''Reilly"

Percentile SQL:

// Median: median(col)
// 95th: quantile(0.95)(col)

Schema Naming

Important: In ClickHouse, "schema" = "database"

  • Credentials schema field = ClickHouse database name
  • database parameter in methods = always empty string

Query Tags

Tags are added as SQL comments:

{ tags: { dashboard: 'analytics', user_id: '123' } }
// Adds: -- {"dashboard":"analytics","user_id":"123"}

Type System

ClickhouseTypes enum is NOT exported. Types mapped automatically:

  • Int*, UInt*, Float*, Decimal* → NUMBER
  • Date, Date32 → DATE
  • DateTime, DateTime64 → TIMESTAMP
  • Bool → BOOLEAN
  • Others → STRING

Handles:

  • Nullable() wrapper (stripped before mapping)
  • LowCardinality() wrapper (stripped)
  • Precision specifiers (e.g., Decimal(18, 2))

Security

  • Use secure: true for HTTPS (port 8443)
  • Named parameters provide SQL injection protection
  • SQL comments and null bytes stripped from escaped strings

Notes

  • Default port: 8123 (HTTP), 8443 (HTTPS)
  • Streaming format: JSONCompactEachRowWithNamesAndTypes
  • Class NOT exported: must use warehouseClientFromCredentials()