Skip to content

test: add sqllogictest module #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ wasm-opt = false
[patch.crates-io.wasm_thread]
git = "https://github.com/Twey/wasm_thread"
branch = "post-message"

[workspace]
members = [
"tests/sqllogictest",
]
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::tokio::{DbState, TonboTable};
#[cfg(feature = "wasm")]
use crate::wasm::{DbState, TonboTable};

pub(crate) fn load_module(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
pub fn load_module(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
let _ = fs::create_dir_all("./db_path/tonbo");

let aux = Some(Arc::new(DbState::new()));
Expand Down
6 changes: 4 additions & 2 deletions src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use fusio::path::Path;
use fusio_dispatch::FsOptions;
use futures_util::StreamExt;
use rusqlite::types::ValueRef;
use rusqlite::vtab::{parse_boolean, update_module, Context, CreateVTab, IndexInfo, UpdateVTab, VTab, VTabConnection, VTabCursor, VTabKind, ValueIter, Values};
use rusqlite::vtab::{
parse_boolean, update_module, Context, CreateVTab, IndexInfo, UpdateVTab, VTab, VTabConnection,
VTabCursor, VTabKind, ValueIter, Values,
};
use rusqlite::{ffi, vtab, Connection, Error};
use sqlparser::ast::{ColumnOption, DataType, Statement};
use sqlparser::dialect::MySqlDialect;
use sqlparser::parser::Parser;
use std::any::Any;
use std::collections::Bound;
use std::ffi::c_int;
use std::marker::PhantomData;
Expand Down
18 changes: 18 additions & 0 deletions tests/sqllogictest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "sqllogictest"
version = "0.1.0"
edition = "2021"

[build]
target = "wasm32-unknown-unknown"

[dependencies]
clap = { version = "4" }
glob = { version = "0.3" }
sqlite-tonbo = { path = "../.." }
sqllogictest = { version = "0.14" }
tempfile = { version = "3.10" }
rusqlite = { git = "https://github.com/tonbo-io/rusqlite", branch = "wasm32-unknown-unknown", features = [
"vtab",
"bundled",
] }
92 changes: 92 additions & 0 deletions tests/sqllogictest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use clap::Parser;
use rusqlite::types::ValueRef;
use rusqlite::Connection;
use sqlite_tonbo::load_module;
use sqllogictest::{DBOutput, DefaultColumnType, Runner, DB};
use std::fs::create_dir_all;
use std::path::Path;
use tempfile::TempDir;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[clap(long, default_value = "tests/test_files/**/*.test")]
path: String,
}

fn main() {
let args = Args::parse();

let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("..");
std::env::set_current_dir(path.clone()).unwrap();

for slt_file in glob::glob(&args.path).expect("failed to find slt files") {
let temp_dir = TempDir::new().expect("unable to create temporary working directory");
std::env::set_current_dir(temp_dir.path()).unwrap();

create_dir_all("./tonbo_test").unwrap();
// for select 4
for i in 1..10 {
create_dir_all(format!("./tonbo_test/t{i}",)).unwrap();
}

let filepath = slt_file.expect("failed to read test file");
let mut tester = Runner::new(
SQLBase::new(Connection::open_in_memory().unwrap()).expect("init db error"),
);
tester.with_hash_threshold(8);

if let Err(err) = tester.run_file(path.join(&filepath)) {
panic!("test error: {}", err);
}
println!("- '{}' Pass", filepath.display());
}
}

struct SQLBase {
db: Connection,
}

impl SQLBase {
fn new(db: Connection) -> rusqlite::Result<Self> {
load_module(&db)?;

Ok(Self { db })
}
}

impl DB for SQLBase {
type Error = rusqlite::Error;
type ColumnType = DefaultColumnType;

fn run(&mut self, sql: &str) -> Result<DBOutput<Self::ColumnType>, Self::Error> {
let mut stmt = self.db.prepare(sql)?;
let column_count = stmt.column_count();
let mut rows = stmt.query([])?;

let mut result_rows = Vec::new();
while let Some(row) = rows.next()? {
let mut result_row = Vec::with_capacity(column_count);

for i in 0..column_count {
result_row.push(value_display(&row.get_ref_unwrap(i)));
}
result_rows.push(result_row);
}

Ok(DBOutput::Rows {
types: vec![DefaultColumnType::Any; column_count],
rows: result_rows,
})
}
}

fn value_display(value: &ValueRef<'_>) -> String {
match value {
ValueRef::Null => "NULL".to_string(),
ValueRef::Integer(val) => val.to_string(),
ValueRef::Real(val) => val.to_string(),
ValueRef::Text(val) => String::from_utf8(val.to_vec()).unwrap(),
ValueRef::Blob(val) => format!("{:?}", val),
}
}
Loading