Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ decimal = ["dep:rust_decimal"]
macros = []
orm = ["macros"]
parser = ["dep:sqlparser"]
sqlite-parser = ["dep:sqlite3-parser"]
rocksdb = ["dep:rocksdb"]
unsafe_txdb_checkpoint = ["rocksdb"]
lmdb = ["dep:lmdb", "dep:lmdb-sys"]
Expand Down Expand Up @@ -86,6 +87,7 @@ csv = { version = "1", optional = true }
pyo3 = { version = "0.23", features = ["auto-initialize"], optional = true }
rust_decimal = { version = "1", default-features = false, features = ["std"], optional = true }
sqlparser = { version = "0.61", default-features = false, features = ["std"], optional = true }
sqlite3-parser = { version = "0.17", optional = true }

[target.'cfg(unix)'.dev-dependencies]
pprof = { version = "0.15", features = ["flamegraph", "criterion"] }
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ WASM_PACK ?= wasm-pack
SQLLOGIC_PATH ?= tests/slt/**/*.slt
SQLITE_LOGIC_PATH ?= tests/kitesql-testing/data/sqlite/**/*.slt
# Every feature except `async`, which excludes the sync storages and bindings.
SYNC_FEATURES ?= time,copy,decimal,macros,orm,parser,rocksdb,unsafe_txdb_checkpoint,lmdb,pprof,python,shell,spill,wasm
SYNC_FEATURES ?= time,copy,decimal,macros,orm,parser,sqlite-parser,rocksdb,unsafe_txdb_checkpoint,lmdb,pprof,python,shell,spill,wasm
# The async path with every feature it supports.
ASYNC_FEATURES ?= async,time,copy,decimal,macros,orm,parser,spill
ASYNC_FEATURES ?= async,time,copy,decimal,macros,orm,parser,sqlite-parser,spill
SQLITE_LOGIC_ARGS ?=
PYO3_PYTHON ?= /usr/bin/python3.12
TPCC_MEASURE_TIME ?= 15
Expand Down Expand Up @@ -48,7 +48,7 @@ COVERAGE_REPORT_ARGS ?= --llvm --ignore-not-existing --keep-only 'src/**' --igno

## Run default Rust tests in the current environment (non-WASM).
test:
$(CARGO) test --all --features spill
$(CARGO) test --all --features spill,sqlite-parser

## Run the same unit tests through the async path (`async` feature, SlateDB on an in-memory object store).
test-async:
Expand Down
56 changes: 27 additions & 29 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,12 @@ If `unsafe_txdb_checkpoint` is not enabled, `build_rocksdb()` returns an explici

### Custom SQL Dialects

KiteSQL supports pluggable SQL dialects through the `SqlDialect` trait. This allows you to customize parsing, data type mapping, and SQL rewriting. A `Database` parses and binds statements with its current dialect. The default is `GenericSqlDialect`, the standard behaviour.
KiteSQL supports pluggable SQL dialects through the `SqlDialect` trait. A dialect parses SQL text into KiteSQL's own syntax tree (`kite_sql::ast`), so it may use any parser, and customizes how statements are typed. A `Database` parses and binds statements with its current dialect. The default is `GenericSqlDialect`, the standard behaviour, which parses with sqlparser and needs the `parser` feature.

#### Using a Custom Dialect

With the `sqlite-parser` feature, `SqliteDialect` parses SQLite syntax with SQLite's own grammar (`sqlite3-parser`); statements still run with KiteSQL's semantics, and SQLite syntax KiteSQL does not support is rejected.

Pass a dialect type to the builder when it is fixed at compile time:

```rust
Expand Down Expand Up @@ -387,58 +389,54 @@ After opening a database with `DynDialect`, use `db.switch_dialect(name)` with m

#### Implementing a Custom Dialect

Implement the `SqlDialect` trait to customize behavior:
Implement the `SqlDialect` trait to customize behavior. A dialect built on sqlparser can parse with `kite_sql::parser::generic::SqlParser`, map its own data types through `DataTypeMapper`, and rewrite the returned statements:

```rust
use kite_sql::ast::Statement;
use kite_sql::dialect::SqlDialect;
use kite_sql::errors::DatabaseError;
use kite_sql::parser::generic::{DataTypeMapper, SqlParser};
use kite_sql::types::LogicalType;
use sqlparser::ast::{DataType, Statement};
use sqlparser::dialect::{Dialect as SqlParserDialect, GenericDialect};
use sqlparser::ast::DataType;
use sqlparser::dialect::GenericDialect;

#[derive(Debug)]
struct MyDialect {
parser: GenericDialect,
}

impl SqlDialect for MyDialect {
fn name(&self) -> &str {
"my_dialect"
}

fn parser_dialect(&self) -> &dyn SqlParserDialect {
&self.parser
}
struct MyDialect;

// Optional: map custom data types
fn map_data_type(
&self,
data_type: &DataType,
) -> Option<Result<LogicalType, DatabaseError>> {
// Optional: map custom data types
impl DataTypeMapper for MyDialect {
fn map_data_type(&self, data_type: &DataType) -> Option<Result<LogicalType, DatabaseError>> {
match data_type {
DataType::Custom(name, _) if name.to_string() == "MY_TYPE" => {
Some(Ok(LogicalType::Integer))
}
_ => None,
}
}
}

impl SqlDialect for MyDialect {
fn name(&self) -> &str {
"my_dialect"
}

// Optional: rewrite SQL statements before execution
fn rewrite(&self, stmt: &mut Statement) -> Result<(), DatabaseError> {
// Modify the statement if needed
Ok(())
fn parse(&self, sql: &str) -> Result<Vec<Statement>, DatabaseError> {
let statements = SqlParser::new(&GenericDialect {})
.with_type_mapper(self)
.parse(sql)?;
// Optional: rewrite the statements before they are bound
Ok(statements)
}
}
```

The `SqlDialect` trait methods:

- `name()` — Returns the dialect's unique identifier
- `parser_dialect()` — Returns the sqlparser dialect for parsing SQL
- `map_data_type()` — Optional hook to map SQL data types in expressions (`CAST`, typed literals) to KiteSQL's `LogicalType`
- `map_column_type()` — Optional hook to map the data types of column definitions; defaults to `map_data_type()`
- `rewrite()` — Optional hook to transform each parsed statement before it is bound
- `parse()` — Parses SQL text into KiteSQL statements; the dialect rejects syntax it does not support and may rewrite the statements
- `comparison_type()` — Optional hook to pick the type both operands of a comparison (`=`, `<>`, `<`, `<=`, `>`, `>=`, `IN`, `BETWEEN`) are cast to
- `boolean_text()` — Optional hook to choose the text of a boolean in `CAST(.. AS CHAR)` or `CAST(.. AS VARCHAR)` (default `'true'`/`'false'`)

`DataTypeMapper::map_data_type()` maps types inside expressions (`CAST`, typed literals); `map_column_type()` maps the types of column definitions and defaults to `map_data_type()`.

By default, the `GenericSqlDialect` is used, which provides standard SQL behavior with no custom transformations.
6 changes: 2 additions & 4 deletions fuzz/fuzz_targets/sql_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ fn run_one(db: &mut Database<LmdbStorage>, sql: &str) {
let _ = db.ddl(sql);
}
CommandType::Analyze => {
if let Statement::Analyze(analyze) = statement {
if let Some(table_name) = analyze.table_name.as_ref() {
let _ = db.analyze(table_name.to_string());
}
if let Statement::Analyze { table_name } = statement {
let _ = db.analyze(table_name.to_string());
}
}
_ => {
Expand Down
Loading
Loading