TableEngine and SqlHandler impl (#45)

* Impl TableEngine, bridge to storage

* Impl sql handler to process insert sql

* fix: minor changes and typo

* test: add datanode test

* test: add table-engine test

* fix: code style

* refactor: split out insert mod from sql and minor changes by CR

* refactor: replace with_context with context
This commit is contained in:
dennis zhuang
2022-06-17 11:36:49 +08:00
committed by GitHub
parent e03ac2fc2b
commit e78c015fc0
36 changed files with 1438 additions and 110 deletions

View File

@@ -1 +1,2 @@
pub use sqlparser::ast::Expr;
pub use sqlparser::ast::Value;

View File

@@ -1,12 +1,56 @@
use sqlparser::ast::Statement;
use sqlparser::ast::{SetExpr, Statement, Values};
use sqlparser::parser::ParserError;
use crate::ast::{Expr, Value};
#[derive(Debug, Clone, PartialEq)]
pub struct Insert {
// Can only be sqlparser::ast::Statement::Insert variant
pub inner: Statement,
}
impl Insert {
pub fn table_name(&self) -> String {
match &self.inner {
Statement::Insert { table_name, .. } => {
// FIXME(dennis): table_name may be in the form of "catalog.schema.table"
table_name.to_string()
}
_ => unreachable!(),
}
}
pub fn columns(&self) -> Vec<&String> {
match &self.inner {
Statement::Insert { columns, .. } => columns.iter().map(|ident| &ident.value).collect(),
_ => unreachable!(),
}
}
pub fn values(&self) -> Vec<Vec<Value>> {
match &self.inner {
Statement::Insert { source, .. } => match &source.body {
SetExpr::Values(Values(values)) => values
.iter()
.map(|v| {
v.iter()
.map(|expr| match expr {
Expr::Value(v) => v.clone(),
Expr::Identifier(ident) => {
Value::SingleQuotedString(ident.value.clone())
}
_ => unreachable!(),
})
.collect::<Vec<Value>>()
})
.collect(),
_ => unreachable!(),
},
_ => unreachable!(),
}
}
}
impl TryFrom<Statement> for Insert {
type Error = ParserError;