use std::collections::HashMap;
use std::ops::Deref;
use php_std::ArrayKey;
use php_std::json::Flags;
use crate::builder::core::{Builder, Dialect, first, key_string};
use crate::builder::feature::{
BitwiseAggregates, ConditionalAggregates, CrossJoins, Cube, FullOuterJoins, Hints, RawSql, Rollup,
StringAggregates, TableSampling, Totals, is_plain_identifier,
};
use crate::builder::types::{Condition, Executor, Statement};
use crate::error::{Error, Result};
use crate::hook::Placement;
use crate::method::backed_enum;
use crate::query::{ParsedQuery, Query};
use crate::value::{self, Array, Value};
pub type ClickHouse = Builder<ClickHouseDialect>;
#[derive(Debug, Clone, PartialEq, Eq)]
struct ArrayJoin {
left: bool,
column: String,
alias: String,
}
#[derive(Debug, Clone, Default)]
pub struct ClickHouseDialect {
prewhere: Vec<Query>,
final_: bool,
sample_fraction: Option<f64>,
limit_by: Option<(i64, Vec<String>)>,
array_joins: Vec<ArrayJoin>,
raw_joins: Vec<String>,
insert_format: Option<String>,
insert_format_columns: Vec<String>,
param_types: HashMap<String, String>,
named_bindings: bool,
delete_mode: DeleteMode,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum DeleteMode {
#[default]
Lightweight,
Mutation,
}
impl DeleteMode {
pub const fn as_str(self) -> &'static str {
match self {
DeleteMode::Lightweight => "lightweight",
DeleteMode::Mutation => "mutation",
}
}
pub fn parse(mode: &str) -> Result<DeleteMode> {
match mode {
"lightweight" => Ok(DeleteMode::Lightweight),
"mutation" => Ok(DeleteMode::Mutation),
other => Err(Error::Validation(format!(
"Invalid ClickHouse delete mode: {other}. Allowed: lightweight, mutation"
))),
}
}
}
backed_enum! {
pub enum AsofOperator: "Utopia\\Query\\Builder\\ClickHouse\\AsofOperator" {
LessThan = "<",
LessThanEqual = "<=",
GreaterThan = ">",
GreaterThanEqual = ">=",
}
}
backed_enum! {
pub enum Format: "Utopia\\Query\\Builder\\ClickHouse\\Format" {
JSONEachRow = "JSONEachRow",
TabSeparated = "TabSeparated",
}
}
impl Format {
pub fn serialize(self, rows: &[Array], columns: Option<&[String]>) -> Result<String> {
let mut lines = Vec::with_capacity(rows.len());
for row in rows {
lines.push(match self {
Format::JSONEachRow => json_each_row_line(row, columns)?,
Format::TabSeparated => tab_separated_line(row, columns)?,
});
}
Ok(lines.join("\n"))
}
}
fn column_value(row: &Array, column: &str) -> Value {
row.get(&ArrayKey::from(column)).cloned().unwrap_or(Value::Null)
}
fn json_each_row_line(row: &Array, columns: Option<&[String]>) -> Result<String> {
let object = match columns {
None => row.clone(),
Some(columns) => {
let mut ordered = Array::with_capacity(columns.len());
for column in columns {
ordered.set(ArrayKey::from(column.as_str()), column_value(row, column));
}
ordered
}
};
let flags = Flags::THROW_ON_ERROR | Flags::UNESCAPED_SLASHES | Flags::UNESCAPED_UNICODE;
value::json_encode(&Value::Object(object), flags)
.map_err(|e| Error::Json { message: e.message().to_owned(), code: e.code().code() })
}
fn tab_separated_line(row: &Array, columns: Option<&[String]>) -> Result<String> {
let mut values = Vec::new();
match columns {
None => {
for v in row.values() {
values.push(escape_tab_separated(v)?);
}
}
Some(columns) => {
for column in columns {
values.push(escape_tab_separated(&column_value(row, column))?);
}
}
}
Ok(values.join("\t"))
}
fn escape_tab_separated(value: &Value) -> Result<String> {
let text = match value {
Value::Null => return Ok("\\N".into()),
Value::Bool(b) => return Ok(if *b { "1" } else { "0" }.into()),
Value::Int(_) | Value::Float(_) => return Ok(value::to_php_string(value)),
Value::Str(s) => String::from_utf8_lossy(s.as_bytes()).into_owned(),
other => {
return Err(Error::Validation(format!(
"TabSeparated values must be scalar, null, or stringable. Received: {}",
other.debug_type()
)));
}
};
let mut out = String::with_capacity(text.len());
for c in text.chars() {
match c {
'\\' => out.push_str("\\\\"),
'\t' => out.push_str("\\t"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
c => out.push(c),
}
}
Ok(out)
}
#[derive(Debug, Clone, PartialEq)]
pub struct FormattedInsertStatement {
pub statement: Statement,
pub columns: Vec<String>,
pub format: String,
pub body: Option<String>,
}
impl Deref for FormattedInsertStatement {
type Target = Statement;
fn deref(&self) -> &Statement {
&self.statement
}
}
impl FormattedInsertStatement {
pub fn with_executor(&self, executor: Executor) -> FormattedInsertStatement {
FormattedInsertStatement { statement: self.statement.with_executor(executor), ..self.clone() }
}
}
fn is_hint(hint: &str) -> bool {
let bytes = hint.as_bytes();
!bytes.is_empty()
&& bytes.iter().all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'=' | b'.' | b',' | b' '))
}
fn is_setting_value(text: &str) -> bool {
let bytes = text.as_bytes();
!bytes.is_empty() && bytes.iter().all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.'))
}
fn is_format_name(text: &str) -> bool {
let bytes = text.as_bytes();
bytes.first().is_some_and(u8::is_ascii_alphabetic) && bytes.iter().all(|&b| b.is_ascii_alphanumeric() || b == b'_')
}
fn is_clickhouse_type(text: &str) -> bool {
fn full(bytes: &[u8]) -> bool {
if !bytes.first().is_some_and(u8::is_ascii_alphabetic) {
return false;
}
let mut i = 1;
while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
i += 1;
}
if i == bytes.len() {
return true;
}
if bytes[i] != b'(' {
return false;
}
i += 1;
while i < bytes.len() {
match bytes[i] {
b')' => return i == bytes.len() - 1,
b'(' => {
let mut j = i + 1;
while j < bytes.len() && bytes[j] != b'(' && bytes[j] != b')' {
j += 1;
}
if j == bytes.len() || bytes[j] == b'(' {
return false;
}
i = j + 1;
}
_ => i += 1,
}
}
false
}
full(text.as_bytes())
}
fn infer_type(value: Option<&Value>) -> &'static str {
match value {
Some(Value::Int(_)) => "Int64",
Some(Value::Float(_)) => "Float64",
Some(Value::Bool(_)) => "UInt8",
None | Some(Value::Null) => "Nullable(String)",
Some(_) => "String",
}
}
fn named(b: &mut ClickHouse, statement: Statement) -> Result<Statement> {
use crate::builder::{Arg, Ret, Step};
if !b.overrides(Step::ApplyNamedTypedBindings) {
return Ok(apply_named_typed_bindings(b, statement));
}
match b.step(Step::ApplyNamedTypedBindings, &[Arg::Statement(&statement)])? {
Ret::Statement(statement) => Ok(statement),
_ => Ok(statement),
}
}
impl ClickHouse {
pub fn apply_named_typed_bindings(&self, statement: Statement) -> Statement {
apply_named_typed_bindings(self, statement)
}
}
fn apply_named_typed_bindings(b: &ClickHouse, statement: Statement) -> Statement {
if !b.dialect.named_bindings || statement.bindings.is_empty() {
return statement;
}
let sql = statement.query.as_bytes();
let mut rewritten = Vec::with_capacity(sql.len() + statement.bindings.len() * 16);
let mut named = Array::with_capacity(statement.bindings.len());
let mut index = 0usize;
for (i, &byte) in sql.iter().enumerate() {
let placeholder =
byte == b'?' && (i == 0 || sql[i - 1] != b'?') && !matches!(sql.get(i + 1), Some(b'|' | b'&' | b'?'));
if !placeholder {
rewritten.push(byte);
continue;
}
let binding = b.core.bindings.get(index);
let type_name = match binding.and_then(|binding| binding.column.as_ref()) {
Some(column) if b.dialect.param_types.contains_key(column) => b.dialect.param_types[column].as_str(),
_ => infer_type(binding.map(|binding| &binding.value)),
};
let name = format!("param{index}");
named.set(ArrayKey::from(name.as_str()), statement.bindings.get(index).cloned().unwrap_or(Value::Null));
rewritten.extend_from_slice(format!("{{{name}:{type_name}}}").as_bytes());
index += 1;
}
Statement::with_parts(
String::from_utf8_lossy(&rewritten).into_owned(),
statement.bindings,
statement.read_only,
b.core.executor.clone(),
Some(named),
)
}
fn select_function(b: &mut ClickHouse, expression: String, alias: &str) -> Result<()> {
let mut expression = expression;
if !alias.is_empty() {
expression.push_str(" AS ");
expression.push_str(&b.quote(alias)?);
}
b.select_raw(expression, Array::default());
Ok(())
}
impl Dialect for ClickHouseDialect {
const CLASS: &'static str = "Utopia\\Query\\Builder\\ClickHouse";
fn base_step(
_b: &mut Builder<Self>,
step: crate::builder::Step,
arguments: &[crate::builder::Arg<'_>],
) -> Option<Result<crate::builder::Ret>> {
if let (crate::builder::Step::ApplyNamedTypedBindings, [crate::builder::Arg::Statement(statement)]) =
(step, arguments)
{
return Some(Ok(crate::builder::Ret::Statement(apply_named_typed_bindings(_b, (*statement).clone()))));
}
crate::builder::feature::aggregates_base_step::<Self>(step, arguments)
}
const LITERAL: crate::StringLiteral = crate::StringLiteral::BackslashUtf8;
fn compile_random(_b: &Builder<Self>) -> String {
"rand()".into()
}
fn compile_regex(b: &mut Builder<Self>, attribute: &str, values: &Array, column: Option<&str>) -> Result<String> {
b.add_binding(first(values), column);
Ok(format!("match({attribute}, ?)"))
}
fn compile_like(
b: &mut Builder<Self>,
attribute: &str,
values: &Array,
prefix: &str,
suffix: &str,
not: bool,
column: Option<&str>,
) -> Result<String> {
let raw = first(values);
let function = match (prefix, suffix) {
("", "%") => Some("startsWith"),
("%", "") => Some("endsWith"),
_ => None,
};
if let Some(function) = function {
b.add_binding(raw, column);
let not = if not { "NOT " } else { "" };
return Ok(format!("{not}{function}({attribute}, ?)"));
}
let pattern = b.like_value(prefix, &raw, suffix)?;
b.add_binding(pattern, column);
b.like_predicate(attribute, not)
}
fn compile_contains(
b: &mut Builder<Self>,
attribute: &str,
values: &Array,
column: Option<&str>,
) -> Result<String> {
if values.len() == 1 {
b.add_binding(first(values), column);
return Ok(format!("position({attribute}, ?) > 0"));
}
let mut parts = Vec::with_capacity(values.len());
for v in values.values() {
b.add_binding(v.clone(), column);
parts.push(format!("position({attribute}, ?) > 0"));
}
Ok(format!("({})", parts.join(" OR ")))
}
fn compile_contains_all(
b: &mut Builder<Self>,
attribute: &str,
values: &Array,
column: Option<&str>,
) -> Result<String> {
let mut parts = Vec::with_capacity(values.len());
for v in values.values() {
b.add_binding(v.clone(), column);
parts.push(format!("position({attribute}, ?) > 0"));
}
Ok(format!("({})", parts.join(" AND ")))
}
fn compile_not_contains(
b: &mut Builder<Self>,
attribute: &str,
values: &Array,
column: Option<&str>,
) -> Result<String> {
if values.len() == 1 {
b.add_binding(first(values), column);
return Ok(format!("position({attribute}, ?) = 0"));
}
let mut parts = Vec::with_capacity(values.len());
for v in values.values() {
b.add_binding(v.clone(), column);
parts.push(format!("position({attribute}, ?) = 0"));
}
Ok(format!("({})", parts.join(" AND ")))
}
fn build_table_clause(b: &mut Builder<Self>) -> Result<String> {
if let Some(sub) = b.core.from_subquery.clone() {
let mut subquery = sub.subquery;
let result = subquery.build()?;
b.add_bindings(result.bindings);
return Ok(format!("FROM ({}) AS {}", result.query, b.quote(&sub.alias)?));
}
let mut sql = format!("FROM {}", b.quote(&b.core.table.clone())?);
if b.dialect.final_ {
sql.push_str(" FINAL");
}
if let Some(fraction) = b.dialect.sample_fraction {
let formatted =
php_std::format::sprintf(b"%.10g", &[php_std::format::Arg::Float(fraction)]).unwrap_or_default();
sql.push_str(" SAMPLE ");
sql.push_str(&String::from_utf8_lossy(&formatted));
}
if !b.core.alias.is_empty() {
sql.push_str(" AS ");
sql.push_str(&b.quote(&b.core.alias.clone())?);
}
Ok(sql)
}
fn build_after_joins_clause(b: &mut Builder<Self>, _grouped: &ParsedQuery) -> Result<String> {
let mut parts = Vec::new();
if !b.dialect.array_joins.is_empty() {
let mut joins = Vec::with_capacity(b.dialect.array_joins.len());
for join in b.dialect.array_joins.clone() {
let keyword = if join.left { "LEFT ARRAY JOIN" } else { "ARRAY JOIN" };
let mut clause = format!("{keyword} {}", b.resolve_and_wrap(&join.column)?);
if !join.alias.is_empty() {
clause.push_str(" AS ");
clause.push_str(&b.quote(&join.alias)?);
}
joins.push(clause);
}
parts.push(joins.join(" "));
}
if !b.dialect.raw_joins.is_empty() {
parts.push(b.dialect.raw_joins.join(" "));
}
if !b.dialect.prewhere.is_empty() {
let mut clauses = Vec::with_capacity(b.dialect.prewhere.len());
for query in b.dialect.prewhere.clone() {
clauses.push(Self::compile_filter(b, &query)?);
}
parts.push(format!("PREWHERE {}", clauses.join(" AND ")));
}
Ok(parts.join(" "))
}
fn build_after_group_by_clause(b: &mut Builder<Self>) -> Result<String> {
Ok(b.core.group_by_modifier.clone().unwrap_or_default())
}
fn build_after_order_by_clause(b: &mut Builder<Self>) -> Result<String> {
let Some((count, columns)) = b.dialect.limit_by.clone() else {
return Ok(String::new());
};
let columns = columns.iter().map(|c| b.resolve_and_wrap(c)).collect::<Result<Vec<_>>>()?;
b.add_binding(Value::Int(count), None);
Ok(format!("LIMIT ? BY {}", columns.join(", ")))
}
fn build_settings_clause(b: &mut Builder<Self>) -> Result<String> {
if b.core.hints.is_empty() {
return Ok(String::new());
}
Ok(format!("SETTINGS {}", b.core.hints.join(", ")))
}
fn compile_group_by_time_bucket(b: &mut Builder<Self>, attribute: &str, interval: &str) -> Result<String> {
let function = match interval {
"1m" => "toStartOfMinute",
"5m" => "toStartOfFiveMinutes",
"15m" => "toStartOfFifteenMinutes",
"1h" => "toStartOfHour",
"1d" => "toStartOfDay",
"1w" => "toStartOfWeek",
"1M" => "toStartOfMonth",
_ => {
return Err(Error::Validation(format!(
"Invalid groupByTimeBucket interval for ClickHouse: {interval}"
)));
}
};
Ok(format!("{function}({})", b.resolve_and_wrap(attribute)?))
}
fn resolve_join_filter_placement(_b: &Builder<Self>, _requested: Placement, _is_cross_join: bool) -> Placement {
Placement::Where
}
fn build(b: &mut Builder<Self>) -> Result<Statement> {
let result = b.base_build()?;
named(b, result)
}
fn insert(b: &mut Builder<Self>) -> Result<Statement> {
match b.formatted_insert()? {
Some(formatted) => Ok(formatted.statement),
None => {
let result = b.base_insert()?;
named(b, result)
}
}
}
fn update(b: &mut Builder<Self>) -> Result<Statement> {
b.core.bindings.clear();
b.validate_table()?;
let assignments = b.compile_assignments()?;
if assignments.is_empty() {
return Err(Error::Validation("No assignments for UPDATE. Call set() or setRaw() before update().".into()));
}
let mut parts = Vec::new();
b.compile_where_clauses(&mut parts, None)?;
if parts.is_empty() {
return Err(Error::Validation("ClickHouse UPDATE requires a WHERE clause.".into()));
}
let sql = format!(
"ALTER TABLE {} UPDATE {} {}",
b.quote(&b.core.table.clone())?,
assignments.join(", "),
parts.join(" ")
);
let statement = b.statement(sql);
named(b, statement)
}
fn delete(b: &mut Builder<Self>) -> Result<Statement> {
b.core.bindings.clear();
b.validate_table()?;
let mut parts = Vec::new();
b.compile_where_clauses(&mut parts, None)?;
if parts.is_empty() {
return Err(Error::Validation("ClickHouse DELETE requires a WHERE clause.".into()));
}
let table = b.quote(&b.core.table.clone())?;
let mut sql = match b.dialect.delete_mode {
DeleteMode::Lightweight => format!("DELETE FROM {table} {}", parts.join(" ")),
DeleteMode::Mutation => format!("ALTER TABLE {table} DELETE {}", parts.join(" ")),
};
let settings = b.step_string(crate::builder::Step::BuildSettingsClause, &[])?;
if !settings.is_empty() {
sql.push(' ');
sql.push_str(&settings);
}
let statement = b.statement(sql);
named(b, statement)
}
fn reset(b: &mut Builder<Self>) {
b.dialect = ClickHouseDialect::default();
b.core.hints.clear();
b.reset_group_by_modifier();
}
fn ast_dialect() -> crate::builder::AstDialect {
crate::builder::AstDialect::ClickHouse
}
}
impl Hints for ClickHouseDialect {
fn hint(b: &mut Builder<Self>, hint: &str) -> Result<()> {
if !is_hint(hint) {
return Err(Error::Validation(format!("Invalid hint: {hint}")));
}
b.core.hints.push(hint.to_owned());
Ok(())
}
}
impl ConditionalAggregates for ClickHouseDialect {
fn aggregate_filter(
b: &mut Builder<Self>,
aggregate: &str,
column: Option<&str>,
condition: &str,
alias: &str,
) -> Result<String> {
let arguments = match column {
None => condition.to_owned(),
Some(column) => format!("{}, {condition}", b.resolve_and_wrap(column)?),
};
let mut expression = format!("{}If({arguments})", aggregate.to_ascii_lowercase());
if !alias.is_empty() {
expression.push_str(" AS ");
expression.push_str(&b.quote(alias)?);
}
Ok(expression)
}
}
impl TableSampling for ClickHouseDialect {
fn tablesample(b: &mut Builder<Self>, percent: f64, _method: &str) -> Result<()> {
b.sample(percent / 100.0)?;
Ok(())
}
}
impl StringAggregates for ClickHouseDialect {
fn group_concat_expr(column: &str, _order_by: &str) -> String {
format!("arrayStringConcat(groupArray({column}), ?)")
}
fn json_array_agg_expr(column: &str) -> String {
format!("toJSONString(groupArray({column}))")
}
fn json_object_agg_expr(key: &str, value: &str) -> String {
format!("toJSONString(CAST((groupArray({key}), groupArray({value})) AS Map(String, String)))")
}
}
impl Totals for ClickHouseDialect {
fn with_totals(b: &mut Builder<Self>) {
b.core.group_by_modifier = Some("WITH TOTALS".into());
}
}
impl Rollup for ClickHouseDialect {
fn with_rollup(b: &mut Builder<Self>) {
b.core.group_by_modifier = Some("WITH ROLLUP".into());
}
}
impl Cube for ClickHouseDialect {
fn with_cube(b: &mut Builder<Self>) {
b.core.group_by_modifier = Some("WITH CUBE".into());
}
}
impl FullOuterJoins for ClickHouseDialect {}
impl BitwiseAggregates for ClickHouseDialect {}
impl RawSql for ClickHouseDialect {}
impl CrossJoins for ClickHouseDialect {}
impl Builder<ClickHouseDialect> {
pub fn prewhere(&mut self, queries: impl IntoIterator<Item = Query>) -> &mut Self {
self.dialect.prewhere.extend(queries);
self
}
pub fn final_(&mut self) -> &mut Self {
self.dialect.final_ = true;
self
}
pub fn sample(&mut self, fraction: f64) -> Result<&mut Self> {
if fraction <= 0.0 || fraction >= 1.0 {
return Err(Error::Validation("Sample fraction must be between 0 and 1 exclusive".into()));
}
self.dialect.sample_fraction = Some(fraction);
Ok(self)
}
pub fn settings<K: AsRef<str>, V: AsRef<str>>(
&mut self,
settings: impl IntoIterator<Item = (K, V)>,
) -> Result<&mut Self> {
for (key, value) in settings {
let (key, value) = (key.as_ref(), value.as_ref());
if !is_plain_identifier(key) {
return Err(Error::Validation(format!("Invalid ClickHouse setting key: {key}")));
}
if !is_setting_value(value) {
return Err(Error::Validation(format!("Invalid ClickHouse setting value: {value}")));
}
self.core.hints.push(format!("{key}={value}"));
}
Ok(self)
}
pub fn bulk_insert(
&mut self,
format: Format,
rows: impl IntoIterator<Item = Value>,
columns: Vec<String>,
) -> Result<FormattedInsertStatement> {
let mut materialized = Vec::new();
for row in rows {
match row {
Value::Array(row) => materialized.push(row),
_ => return Err(Error::Validation("bulkInsert() rows must be associative arrays.".into())),
}
}
let mut columns = columns;
if columns.is_empty()
&& let Some(row) = materialized.first()
{
columns = row.keys().map(|k| key_string(&k.to_owned())).collect();
}
let sql = self.format_insert_envelope(format.as_str(), &columns)?;
let body = format.serialize(&materialized, if columns.is_empty() { None } else { Some(&columns) })?;
Ok(FormattedInsertStatement {
statement: Statement::with_parts(sql, Vec::new(), false, self.core.executor.clone(), None),
columns,
format: format.as_str().to_owned(),
body: Some(body),
})
}
pub fn bulk_insert_sparse(
&mut self,
format: Format,
rows: impl IntoIterator<Item = Value>,
) -> Result<FormattedInsertStatement> {
if !matches!(format, Format::JSONEachRow) {
return Err(Error::Validation(format!(
"bulkInsertSparse() requires a self-describing format; {} is positional.",
format.as_str()
)));
}
let mut materialized = Vec::new();
for row in rows {
match row {
Value::Array(row) => materialized.push(row),
_ => return Err(Error::Validation("bulkInsertSparse() rows must be associative arrays.".into())),
}
}
let sql = self.format_insert_envelope(format.as_str(), &[])?;
let body = format.serialize(&materialized, None)?;
Ok(FormattedInsertStatement {
statement: Statement::with_parts(sql, Vec::new(), false, self.core.executor.clone(), None),
columns: Vec::new(),
format: format.as_str().to_owned(),
body: Some(body),
})
}
pub fn insert_format<S: Into<String>>(
&mut self,
format: &str,
columns: impl IntoIterator<Item = S>,
) -> Result<&mut Self> {
if !is_format_name(format) {
return Err(Error::Validation(format!("Invalid ClickHouse INSERT format: {format}")));
}
self.dialect.insert_format = Some(format.to_owned());
self.dialect.insert_format_columns = columns.into_iter().map(Into::into).collect();
Ok(self)
}
pub fn formatted_insert(&mut self) -> Result<Option<FormattedInsertStatement>> {
let Some(format) = self.dialect.insert_format.clone() else {
return Ok(None);
};
let columns = if !self.dialect.insert_format_columns.is_empty() {
self.dialect.insert_format_columns.clone()
} else {
match self.core.rows.first() {
Some(row) => row.keys().map(|k| key_string(&k.to_owned())).collect(),
None => Vec::new(),
}
};
let sql = self.format_insert_envelope(&format, &columns)?;
Ok(Some(FormattedInsertStatement {
statement: Statement::with_parts(sql, Vec::new(), false, self.core.executor.clone(), None),
columns,
format,
body: None,
}))
}
fn format_insert_envelope(&mut self, format: &str, columns: &[String]) -> Result<String> {
self.core.bindings.clear();
self.validate_table()?;
if columns.iter().any(String::is_empty) {
return Err(Error::Validation("Column names for FORMAT INSERT must be non-empty strings.".into()));
}
let mut sql = format!("INSERT INTO {}", self.quote(&self.core.table.clone())?);
if !columns.is_empty() {
let wrapped = columns.iter().map(|c| self.resolve_and_wrap(c)).collect::<Result<Vec<_>>>()?;
sql.push_str(" (");
sql.push_str(&wrapped.join(", "));
sql.push(')');
}
sql.push_str(" FORMAT ");
sql.push_str(format);
Ok(sql)
}
pub fn use_named_bindings(&mut self, enabled: bool) -> &mut Self {
self.dialect.named_bindings = enabled;
self
}
pub fn with_param_type(&mut self, column: impl Into<String>, type_name: &str) -> Result<&mut Self> {
if !is_clickhouse_type(type_name) {
return Err(Error::Validation(format!("Invalid ClickHouse type: {type_name}")));
}
self.dialect.param_types.insert(column.into(), type_name.to_owned());
Ok(self)
}
pub fn with_param_types<K: Into<String>, V: AsRef<str>>(
&mut self,
types: impl IntoIterator<Item = (K, V)>,
) -> Result<&mut Self> {
for (column, type_name) in types {
self.with_param_type(column, type_name.as_ref())?;
}
Ok(self)
}
pub fn delete_mode(&mut self, mode: DeleteMode) -> &mut Self {
self.dialect.delete_mode = mode;
self
}
pub fn stddev(&mut self, attribute: &str, alias: &str) -> Result<&mut Self> {
let expression = format!("stddevPop({})", self.resolve_and_wrap(attribute)?);
select_function(self, expression, alias)?;
Ok(self)
}
pub fn stddev_pop(&mut self, attribute: impl Into<String>, alias: impl Into<String>) -> &mut Self {
self.core.pending.push(Query::stddev_pop(attribute, alias));
self
}
pub fn stddev_samp(&mut self, attribute: impl Into<String>, alias: impl Into<String>) -> &mut Self {
self.core.pending.push(Query::stddev_samp(attribute, alias));
self
}
pub fn variance(&mut self, attribute: &str, alias: &str) -> Result<&mut Self> {
let expression = format!("varPop({})", self.resolve_and_wrap(attribute)?);
select_function(self, expression, alias)?;
Ok(self)
}
pub fn var_pop(&mut self, attribute: impl Into<String>, alias: impl Into<String>) -> &mut Self {
self.core.pending.push(Query::var_pop(attribute, alias));
self
}
pub fn var_samp(&mut self, attribute: impl Into<String>, alias: impl Into<String>) -> &mut Self {
self.core.pending.push(Query::var_samp(attribute, alias));
self
}
fn aggregate_function(&mut self, function: &str, columns: &[&str], alias: &str) -> Result<&mut Self> {
let columns = columns.iter().map(|c| self.resolve_and_wrap(c)).collect::<Result<Vec<_>>>()?;
select_function(self, format!("{function}({})", columns.join(", ")), alias)?;
Ok(self)
}
pub fn quantile(&mut self, level: f64, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function(&format!("quantile({})", php_std::number::to_string(level)), &[column], alias)
}
pub fn quantiles(&mut self, levels: &[f64], column: &str, alias: &str) -> Result<&mut Self> {
if levels.is_empty() {
return Err(Error::Validation("quantiles() requires at least one level.".into()));
}
if levels.iter().any(|&level| !(0.0..=1.0).contains(&level)) {
return Err(Error::Validation("quantiles() levels must be in the range [0, 1].".into()));
}
let levels: Vec<String> = levels.iter().map(|&level| php_std::number::to_string(level)).collect();
self.aggregate_function(&format!("quantiles({})", levels.join(", ")), &[column], alias)
}
pub fn quantile_exact(&mut self, level: f64, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function(&format!("quantileExact({})", php_std::number::to_string(level)), &[column], alias)
}
pub fn median(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("median", &[column], alias)
}
pub fn uniq(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("uniq", &[column], alias)
}
pub fn uniq_exact(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("uniqExact", &[column], alias)
}
pub fn uniq_combined(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("uniqCombined", &[column], alias)
}
pub fn arg_min(&mut self, value_column: &str, arg_column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("argMin", &[value_column, arg_column], alias)
}
pub fn arg_max(&mut self, value_column: &str, arg_column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("argMax", &[value_column, arg_column], alias)
}
pub fn top_k(&mut self, k: i64, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function(&format!("topK({k})"), &[column], alias)
}
pub fn top_k_weighted(&mut self, k: i64, column: &str, weight_column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function(&format!("topKWeighted({k})"), &[column, weight_column], alias)
}
pub fn any_value(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("any", &[column], alias)
}
pub fn any_last_value(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("anyLast", &[column], alias)
}
pub fn group_uniq_array(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("groupUniqArray", &[column], alias)
}
pub fn group_array_moving_avg(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("groupArrayMovingAvg", &[column], alias)
}
pub fn group_array_moving_sum(&mut self, column: &str, alias: &str) -> Result<&mut Self> {
self.aggregate_function("groupArrayMovingSum", &[column], alias)
}
pub fn array_join(&mut self, column: impl Into<String>, alias: impl Into<String>) -> &mut Self {
self.dialect.array_joins.push(ArrayJoin { left: false, column: column.into(), alias: alias.into() });
self
}
pub fn left_array_join(&mut self, column: impl Into<String>, alias: impl Into<String>) -> &mut Self {
self.dialect.array_joins.push(ArrayJoin { left: true, column: column.into(), alias: alias.into() });
self
}
pub fn asof_join<L: AsRef<str>, R: AsRef<str>>(
&mut self,
table: &str,
equi_pairs: impl IntoIterator<Item = (L, R)>,
left_inequality: &str,
operator: AsofOperator,
right_inequality: &str,
alias: &str,
) -> Result<&mut Self> {
let clause =
self.asof_clause("ASOF JOIN", table, equi_pairs, left_inequality, operator, right_inequality, alias)?;
self.dialect.raw_joins.push(clause);
Ok(self)
}
pub fn asof_left_join<L: AsRef<str>, R: AsRef<str>>(
&mut self,
table: &str,
equi_pairs: impl IntoIterator<Item = (L, R)>,
left_inequality: &str,
operator: AsofOperator,
right_inequality: &str,
alias: &str,
) -> Result<&mut Self> {
let clause =
self.asof_clause("ASOF LEFT JOIN", table, equi_pairs, left_inequality, operator, right_inequality, alias)?;
self.dialect.raw_joins.push(clause);
Ok(self)
}
#[allow(clippy::too_many_arguments)]
fn asof_clause<L: AsRef<str>, R: AsRef<str>>(
&mut self,
keyword: &str,
table: &str,
equi_pairs: impl IntoIterator<Item = (L, R)>,
left_inequality: &str,
operator: AsofOperator,
right_inequality: &str,
alias: &str,
) -> Result<String> {
let pairs: Vec<(L, R)> = equi_pairs.into_iter().collect();
if pairs.is_empty() {
return Err(Error::Validation("ASOF JOIN requires at least one equi-join column pair.".into()));
}
let mut table = self.quote(table)?;
if !alias.is_empty() {
table.push_str(" AS ");
table.push_str(&self.quote(alias)?);
}
let mut conditions = Vec::with_capacity(pairs.len() + 1);
for (left, right) in &pairs {
conditions.push(format!(
"{} = {}",
self.resolve_and_wrap(left.as_ref())?,
self.resolve_and_wrap(right.as_ref())?
));
}
conditions.push(format!(
"{} {} {}",
self.resolve_and_wrap(left_inequality)?,
operator.as_str(),
self.resolve_and_wrap(right_inequality)?
));
Ok(format!("{keyword} {table} ON {}", conditions.join(" AND ")))
}
pub fn limit_by<S: Into<String>>(&mut self, count: i64, columns: impl IntoIterator<Item = S>) -> &mut Self {
self.dialect.limit_by = Some((count, columns.into_iter().map(Into::into).collect()));
self
}
pub fn order_with_fill(
&mut self,
column: &str,
direction: &str,
from: Option<Value>,
to: Option<Value>,
step: Option<Value>,
) -> Result<&mut Self> {
let normalized = direction.to_ascii_uppercase();
if normalized != "ASC" && normalized != "DESC" {
return Err(Error::Validation(format!("Invalid direction for orderWithFill: {direction}")));
}
let mut expression = format!("{} {normalized} WITH FILL", self.resolve_and_wrap(column)?);
let mut bindings = Vec::new();
for (keyword, value) in [(" FROM ?", from), (" TO ?", to), (" STEP ?", step)] {
if let Some(value) = value.filter(|v| !matches!(v, Value::Null)) {
expression.push_str(keyword);
bindings.push(value);
}
}
self.core.raw_orders.push(Condition::new(expression, bindings));
Ok(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn type_names() {
assert!(is_clickhouse_type("Int64"));
assert!(is_clickhouse_type("Nullable(DateTime64(3))"));
assert!(is_clickhouse_type("Decimal(10, 2)"));
assert!(!is_clickhouse_type("Int64\n"));
assert!(!is_clickhouse_type("DROP TABLE x; --"));
assert!(!is_clickhouse_type("A(B(C(1)))"));
assert!(!is_clickhouse_type("A(1))"));
}
#[test]
fn bulk_insert_sparse_keeps_each_rows_own_keys_without_column_list() {
let mut first = Array::default();
first.set("id".into(), Value::Int(1));
first.set("is_bot".into(), Value::Int(0));
let mut second = Array::default();
second.set("id".into(), Value::Int(2));
second.set("city".into(), "Berlin".into());
let mut third = Array::default();
third.set("id".into(), Value::Int(3));
let insert = ClickHouse::new()
.from("analytics.events", "")
.bulk_insert_sparse(Format::JSONEachRow, [Value::Array(first), Value::Array(second), Value::Array(third)])
.unwrap();
assert_eq!(insert.query, "INSERT INTO `analytics`.`events` FORMAT JSONEachRow");
assert!(insert.columns.is_empty());
assert_eq!(insert.format, "JSONEachRow");
assert!(insert.bindings.is_empty());
assert_eq!(insert.body.as_deref(), Some("{\"id\":1,\"is_bot\":0}\n{\"id\":2,\"city\":\"Berlin\"}\n{\"id\":3}"));
}
#[test]
fn bulk_insert_sparse_empty_rows_emits_bare_envelope_and_empty_body() {
let insert =
ClickHouse::new().from("events", "").bulk_insert_sparse(Format::JSONEachRow, Vec::<Value>::new()).unwrap();
assert_eq!(insert.query, "INSERT INTO `events` FORMAT JSONEachRow");
assert!(insert.columns.is_empty());
assert_eq!(insert.body.as_deref(), Some(""));
}
#[test]
fn bulk_insert_sparse_rejects_positional_formats_and_non_array_rows() {
let mut row = Array::default();
row.set("id".into(), Value::Int(1));
let positional = ClickHouse::new()
.from("events", "")
.bulk_insert_sparse(Format::TabSeparated, [Value::Array(row)])
.unwrap_err();
assert!(matches!(positional, Error::Validation(_)));
let scalar =
ClickHouse::new().from("events", "").bulk_insert_sparse(Format::JSONEachRow, [Value::Int(1)]).unwrap_err();
assert!(matches!(scalar, Error::Validation(_)));
}
}