Skip to content

Commit

Permalink
impl display for scalar value & sqllogictest
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 29, 2024
1 parent d18f96c commit 601e33e
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 32 deletions.
100 changes: 100 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion bustubx-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bustubx::database::Database;
use bustubx::Database;
use rustyline::error::ReadlineError;
use rustyline::{DefaultEditor, Result};
use std::io::Write;
use tracing::info;
use tracing_chrome::ChromeLayerBuilder;
use tracing_subscriber::fmt;
Expand Down
33 changes: 17 additions & 16 deletions bustubx/src/common/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Formatter;

use crate::catalog::DataType;

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -10,6 +8,7 @@ pub enum ScalarValue {
Int32(Option<i32>),
Int64(Option<i64>),
}

impl ScalarValue {
pub fn new_empty(data_type: DataType) -> Self {
match data_type {
Expand Down Expand Up @@ -55,7 +54,6 @@ impl ScalarValue {
DataType::Int64 => Self::Int64(Some(v.parse::<i64>().unwrap())),
_ => panic!("Not implemented"),
},
// sqlparser::ast::Value::SingleQuotedString(_) => {}
sqlparser::ast::Value::Boolean(b) => ScalarValue::Boolean(Some(*b)),
_ => unreachable!(),
}
Expand Down Expand Up @@ -109,16 +107,19 @@ impl ScalarValue {
}
}

// TODO delete
// impl std::fmt::Display for ScalarValue {
// fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
// match self {
// ScalarValue::Boolean(e) => write!(f, "{}", e)?,
// ScalarValue::Int8(e) => write!(f, "{}", e)?,
// ScalarValue::Int16(e) => write!(f, "{}", e)?,
// ScalarValue::Int32(e) => write!(f, "{}", e)?,
// ScalarValue::Int64(e) => write!(f, "{}", e)?,
// };
// Ok(())
// }
// }
impl std::fmt::Display for ScalarValue {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ScalarValue::Boolean(None) => write!(f, "NULL"),
ScalarValue::Boolean(Some(v)) => write!(f, "{v}"),
ScalarValue::Int8(None) => write!(f, "NULL"),
ScalarValue::Int8(Some(v)) => write!(f, "{v}"),
ScalarValue::Int16(None) => write!(f, "NULL"),
ScalarValue::Int16(Some(v)) => write!(f, "{v}"),
ScalarValue::Int32(None) => write!(f, "NULL"),
ScalarValue::Int32(Some(v)) => write!(f, "{v}"),
ScalarValue::Int64(None) => write!(f, "NULL"),
ScalarValue::Int64(Some(v)) => write!(f, "{v}"),
}
}
}
8 changes: 6 additions & 2 deletions bustubx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
mod buffer;
mod catalog;
mod common;
pub mod database;
pub mod error;
mod database;
mod error;
mod execution;
mod expression;
mod optimizer;
mod parser;
mod planner;
mod storage;

pub use database::Database;
pub use error::{BustubxError, BustubxResult};
pub use storage::Tuple;
44 changes: 31 additions & 13 deletions tests/sqllogictest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
use bustubx::database::Database;
use bustubx::error::BustubxError;
use bustubx::Database;
use bustubx::{BustubxError, Tuple};
use sqllogictest::{DBOutput, DefaultColumnType};
use tempfile::TempDir;

pub struct Bustubx {
pub struct BustubxDB {
db: Database,
tmp_dir: TempDir,
}

impl Bustubx {
impl BustubxDB {
pub fn new() -> Self {
let tmp_dir = TempDir::new().unwrap();
let db = Database::new_on_disk(tmp_dir.path().to_str().unwrap());
Self { db, tmp_dir }
let db = Database::new_temp();
Self { db }
}
}

#[async_trait::async_trait]
impl sqllogictest::AsyncDB for &Bustubx {
fn tuples_to_sqllogictest_string(tuples: Vec<Tuple>) -> Vec<Vec<String>> {
todo!()
}

impl sqllogictest::DB for BustubxDB {
type Error = BustubxError;
type ColumnType = DefaultColumnType;

async fn run(&mut self, sql: &str) -> Result<DBOutput<Self::ColumnType>, Self::Error> {
todo!()
fn run(&mut self, sql: &str) -> Result<DBOutput<Self::ColumnType>, Self::Error> {
let is_query_sql = {
let lower_sql = sql.trim_start().to_ascii_lowercase();
lower_sql.starts_with("select")
};
let tuples = self.db.run(sql)?;
if tuples.is_empty() {
if is_query_sql {
return Ok(DBOutput::Rows {
types: vec![],
rows: vec![],
});
} else {
return Ok(DBOutput::StatementComplete(0));
}
}
// TODO fix type count
let types = vec![DefaultColumnType::Any; 3];
let rows = tuples_to_sqllogictest_string(tuples);
Ok(DBOutput::Rows { types, rows })
}
}

0 comments on commit 601e33e

Please sign in to comment.