Skip to content

Commit

Permalink
chore: simplify parameter on execute sql
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Dec 16, 2024
1 parent 10f1eac commit 753ded5
Show file tree
Hide file tree
Showing 29 changed files with 102 additions and 103 deletions.
6 changes: 3 additions & 3 deletions src/binder/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use itertools::Itertools;
use sqlparser::ast::{Expr, OrderByExpr};
use std::collections::HashSet;

use super::{Binder, QueryBindStep};
use crate::errors::DatabaseError;
use crate::expression::function::scala::ScalarFunction;
use crate::planner::LogicalPlan;
use crate::storage::Transaction;
use crate::types::value::DataValue;
use crate::{
expression::ScalarExpression,
planner::operator::{aggregate::AggregateOperator, sort::SortField},
};

use super::{Binder, QueryBindStep};

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub fn bind_aggregate(
&mut self,
children: LogicalPlan,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/alter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_alter_table(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_analyze(&mut self, name: &ObjectName) -> Result<LogicalPlan, DatabaseError> {
let table_name = Arc::new(lower_case_name(name)?);

Expand Down
2 changes: 1 addition & 1 deletion src/binder/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl FromStr for ExtSource {
}
}

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(super) fn bind_copy(
&mut self,
source: CopySource,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/create_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::index::IndexType;
use crate::types::value::DataValue;
use sqlparser::ast::{ObjectName, OrderByExpr};
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_create_index(
&mut self,
table_name: &ObjectName,
Expand Down
7 changes: 3 additions & 4 deletions src/binder/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use crate::planner::operator::create_table::CreateTableOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use crate::types::LogicalType;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
// TODO: TableConstraint
pub(crate) fn bind_create_table(
&mut self,
Expand Down Expand Up @@ -157,7 +158,6 @@ mod tests {
use crate::types::LogicalType;
use crate::utils::lru::SharedLruCache;
use sqlparser::ast::CharLengthUnits;
use std::cell::RefCell;
use std::hash::RandomState;
use std::sync::atomic::AtomicUsize;
use tempfile::TempDir;
Expand All @@ -173,7 +173,6 @@ mod tests {
let table_functions = Default::default();

let sql = "create table t1 (id int primary key, name varchar(10) null)";
let args = RefCell::new(Vec::new());
let mut binder = Binder::new(
BinderContext::new(
&table_cache,
Expand All @@ -183,7 +182,7 @@ mod tests {
&table_functions,
Arc::new(AtomicUsize::new(0)),
),
&args,
&[],
None,
);
let stmt = crate::parser::parse_sql(sql).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/binder/create_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use crate::planner::operator::create_view::CreateViewOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use itertools::Itertools;
use sqlparser::ast::{Ident, ObjectName, Query};
use std::sync::Arc;
use ulid::Ulid;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_create_view(
&mut self,
or_replace: &bool,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use itertools::Itertools;
use sqlparser::ast::{Expr, TableAlias, TableFactor, TableWithJoins};
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_delete(
&mut self,
from: &TableWithJoins,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::planner::operator::describe::DescribeOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_describe(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::expression::ScalarExpression;
use crate::planner::operator::aggregate::AggregateOperator;
use crate::planner::LogicalPlan;
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub fn bind_distinct(
&mut self,
children: LogicalPlan,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::planner::operator::drop_table::DropTableOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_drop_table(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/drop_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::planner::operator::drop_view::DropViewOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_drop_view(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::errors::DatabaseError;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_explain(&mut self, plan: LogicalPlan) -> Result<LogicalPlan, DatabaseError> {
Ok(LogicalPlan::new(Operator::Explain, Childrens::Only(plan)))
}
Expand Down
13 changes: 5 additions & 8 deletions src/binder/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ macro_rules! try_default {
};
}

impl<'a, T: Transaction> Binder<'a, '_, T> {
impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T, A> {
pub(crate) fn bind_expr(&mut self, expr: &Expr) -> Result<ScalarExpression, DatabaseError> {
match expr {
Expr::Identifier(ident) => {
Expand All @@ -50,14 +50,11 @@ impl<'a, T: Transaction> Binder<'a, '_, T> {
Expr::BinaryOp { left, right, op } => self.bind_binary_op_internal(left, right, op),
Expr::Value(v) => {
let value = if let Value::Placeholder(name) = v {
let (i, _) = self
.args
.borrow()
self.args
.as_ref()
.iter()
.enumerate()
.find(|(_, (key, _))| key == name)
.ok_or_else(|| DatabaseError::ParametersNotFound(name.to_string()))?;
self.args.borrow_mut().remove(i).1
.find_map(|(key, value)| (key == name).then(|| value.clone()))
.ok_or_else(|| DatabaseError::ParametersNotFound(name.to_string()))?
} else {
v.into()
};
Expand Down
2 changes: 1 addition & 1 deletion src/binder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sqlparser::ast::{Expr, Ident, ObjectName};
use std::slice;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_insert(
&mut self,
name: &ObjectName,
Expand Down
20 changes: 9 additions & 11 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ mod truncate;
mod update;

use sqlparser::ast::{Ident, ObjectName, ObjectType, SetExpr, Statement};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use crate::catalog::view::View;
use crate::catalog::{ColumnRef, TableCatalog, TableName};
use crate::db::{Args, ScalaFunctions, TableFunctions};
use crate::db::{ScalaFunctions, TableFunctions};
use crate::errors::DatabaseError;
use crate::expression::ScalarExpression;
use crate::planner::operator::join::JoinType;
use crate::planner::{LogicalPlan, SchemaOutput};
use crate::storage::{TableCache, Transaction, ViewCache};
use crate::types::tuple::SchemaRef;
use crate::types::value::DataValue;

pub enum InputRefType {
AggCall,
Expand Down Expand Up @@ -313,18 +313,18 @@ impl<'a, T: Transaction> BinderContext<'a, T> {
}
}

pub struct Binder<'a, 'b, T: Transaction> {
pub struct Binder<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> {
context: BinderContext<'a, T>,
table_schema_buf: HashMap<TableName, Option<SchemaOutput>>,
args: &'a RefCell<Args>,
pub(crate) parent: Option<&'b Binder<'a, 'b, T>>,
args: &'a A,
pub(crate) parent: Option<&'b Binder<'a, 'b, T, A>>,
}

impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, 'b, T, A> {
pub fn new(
context: BinderContext<'a, T>,
args: &'a RefCell<Args>,
parent: Option<&'b Binder<'a, 'b, T>>,
args: &'a A,
parent: Option<&'b Binder<'a, 'b, T, A>>,
) -> Self {
Binder {
context,
Expand Down Expand Up @@ -488,7 +488,6 @@ pub mod test {
use crate::types::ColumnId;
use crate::types::LogicalType::Integer;
use crate::utils::lru::SharedLruCache;
use std::cell::RefCell;
use std::hash::RandomState;
use std::path::PathBuf;
use std::sync::atomic::AtomicUsize;
Expand All @@ -507,7 +506,6 @@ pub mod test {
let scala_functions = Default::default();
let table_functions = Default::default();
let transaction = self.storage.transaction()?;
let args = RefCell::new(Vec::new());
let mut binder = Binder::new(
BinderContext::new(
&self.table_cache,
Expand All @@ -517,7 +515,7 @@ pub mod test {
&table_functions,
Arc::new(AtomicUsize::new(0)),
),
&args,
&[],
None,
);
let stmt = crate::parser::parse_sql(sql)?;
Expand Down
2 changes: 1 addition & 1 deletion src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use sqlparser::ast::{
TableWithJoins,
};

impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, 'b, T, A> {
pub(crate) fn bind_query(&mut self, query: &Query) -> Result<LogicalPlan, DatabaseError> {
let origin_step = self.context.step_now();

Expand Down
3 changes: 2 additions & 1 deletion src/binder/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::errors::DatabaseError;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_show_tables(&mut self) -> Result<LogicalPlan, DatabaseError> {
Ok(LogicalPlan::new(Operator::Show, Childrens::None))
}
Expand Down
3 changes: 2 additions & 1 deletion src/binder/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::planner::operator::truncate::TruncateOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_truncate(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use crate::planner::operator::update::UpdateOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::{Assignment, Expr, TableFactor, TableWithJoins};
use std::slice;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_update(
&mut self,
to: &TableWithJoins,
Expand Down
Loading

0 comments on commit 753ded5

Please sign in to comment.