pub mod clickhouse;
use std::collections::HashMap;
use crate::query::SqlDialect;
#[derive(Debug)]
pub struct Preprocessor {
dialect: SqlDialect
}
#[derive(Debug, Default, Clone)]
pub struct PreprocessorMetadata {
pub codecs: HashMap<String, String>,
pub ttl_expressions: Vec<String>,
pub settings: HashMap<String, String>,
pub partition_by: Vec<String>
}
#[derive(Debug)]
pub struct PreprocessorResult {
pub sql: String,
pub metadata: PreprocessorMetadata
}
impl Preprocessor {
#[must_use]
pub fn new(dialect: SqlDialect) -> Self {
Self {
dialect
}
}
#[must_use]
pub fn process(&self, sql: &str) -> PreprocessorResult {
match self.dialect {
SqlDialect::ClickHouse => clickhouse::preprocess(sql),
_ => PreprocessorResult {
sql: sql.to_string(),
metadata: PreprocessorMetadata::default()
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_preprocessor_generic_passthrough() {
let sql = "CREATE TABLE users (id INT PRIMARY KEY)";
let result = Preprocessor::new(SqlDialect::Generic).process(sql);
assert_eq!(result.sql, sql);
assert!(result.metadata.codecs.is_empty());
}
#[test]
fn test_preprocessor_clickhouse_removes_codec() {
let sql = "CREATE TABLE logs (data String CODEC(ZSTD)) ENGINE = MergeTree ORDER BY id";
let result = Preprocessor::new(SqlDialect::ClickHouse).process(sql);
assert!(!result.sql.contains("CODEC"));
}
#[test]
fn test_preprocessor_metadata_extraction() {
let sql = "CREATE TABLE t (col String CODEC(LZ4)) ENGINE = MergeTree ORDER BY col";
let result = Preprocessor::new(SqlDialect::ClickHouse).process(sql);
assert!(result.metadata.codecs.contains_key("col"));
assert_eq!(result.metadata.codecs.get("col"), Some(&"LZ4".to_string()));
}
}