Skip to content

Commit

Permalink
Remove ast::ColumnOptionDef (#983)
Browse files Browse the repository at this point in the history
Remove `ast::ColumnOptionDef` and make it only use `ast::ColumnOption`
  • Loading branch information
MRGRAVITY817 authored Nov 4, 2022
1 parent 189dc01 commit 6fc167e
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 132 deletions.
39 changes: 10 additions & 29 deletions core/src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ pub enum AlterTableOperation {
pub struct ColumnDef {
pub name: String,
pub data_type: DataType,
pub options: Vec<ColumnOptionDef>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ColumnOptionDef {
pub name: Option<String>,
pub option: ColumnOption,
pub options: Vec<ColumnOption>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -81,7 +75,7 @@ impl ToSql for ColumnDef {
{
let options = options
.iter()
.map(|ColumnOptionDef { option, .. }| option.to_sql())
.map(|option| option.to_sql())
.collect::<Vec<_>>()
.join(" ");
format!("{name} {data_type} {options}")
Expand All @@ -107,7 +101,7 @@ impl ToSql for ColumnOption {

#[cfg(test)]
mod tests {
use crate::ast::{AstLiteral, ColumnDef, ColumnOption, ColumnOptionDef, DataType, Expr, ToSql};
use crate::ast::{AstLiteral, ColumnDef, ColumnOption, DataType, Expr, ToSql};

#[test]
fn to_sql_column_def() {
Expand All @@ -116,10 +110,7 @@ mod tests {
ColumnDef {
name: "name".to_owned(),
data_type: DataType::Text,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Unique { is_primary: false }
}]
options: vec![ColumnOption::Unique { is_primary: false }]
}
.to_sql()
);
Expand All @@ -129,10 +120,7 @@ mod tests {
ColumnDef {
name: "accepted".to_owned(),
data_type: DataType::Boolean,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Null
}]
options: vec![ColumnOption::Null]
}
.to_sql()
);
Expand All @@ -143,14 +131,8 @@ mod tests {
name: "id".to_owned(),
data_type: DataType::Int,
options: vec![
ColumnOptionDef {
name: None,
option: ColumnOption::NotNull
},
ColumnOptionDef {
name: None,
option: ColumnOption::Unique { is_primary: true }
}
ColumnOption::NotNull,
ColumnOption::Unique { is_primary: true }
]
}
.to_sql()
Expand All @@ -161,10 +143,9 @@ mod tests {
ColumnDef {
name: "accepted".to_owned(),
data_type: DataType::Boolean,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Default(Expr::Literal(AstLiteral::Boolean(false)))
}]
options: vec![ColumnOption::Default(Expr::Literal(AstLiteral::Boolean(
false
)))]
}
.to_sql()
);
Expand Down
20 changes: 7 additions & 13 deletions core/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ mod tests {

use {
crate::ast::{
Assignment, AstLiteral, BinaryOperator, ColumnDef, ColumnOption, ColumnOptionDef,
DataType, Expr, Query, Select, SelectItem, SetExpr, Statement, TableFactor,
TableWithJoins, ToSql, Values, Variable,
Assignment, AstLiteral, BinaryOperator, ColumnDef, ColumnOption, DataType, Expr, Query,
Select, SelectItem, SetExpr, Statement, TableFactor, TableWithJoins, ToSql, Values,
Variable,
},
bigdecimal::BigDecimal,
std::str::FromStr,
Expand Down Expand Up @@ -370,10 +370,7 @@ mod tests {
ColumnDef {
name: "num".to_owned(),
data_type: DataType::Int,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Null
}]
options: vec![ColumnOption::Null]
},
ColumnDef {
name: "name".to_owned(),
Expand Down Expand Up @@ -457,12 +454,9 @@ mod tests {
column_def: ColumnDef {
name: "amount".to_owned(),
data_type: DataType::Int,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Default(Expr::Literal(AstLiteral::Number(
BigDecimal::from_str("10").unwrap()
)))
}]
options: vec![ColumnOption::Default(Expr::Literal(AstLiteral::Number(
BigDecimal::from_str("10").unwrap()
)))]
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions core/src/data/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::ast::{ColumnDef, ColumnOption, ColumnOptionDef, Expr},
crate::ast::{ColumnDef, ColumnOption, Expr},
chrono::NaiveDateTime,
serde::{Deserialize, Serialize},
std::fmt::Debug,
Expand Down Expand Up @@ -40,15 +40,13 @@ impl ColumnDefExt for ColumnDef {
fn is_nullable(&self) -> bool {
self.options
.iter()
.any(|ColumnOptionDef { option, .. }| option == &ColumnOption::Null)
.any(|option| option == &ColumnOption::Null)
}

fn get_default(&self) -> Option<&Expr> {
self.options
.iter()
.find_map(|ColumnOptionDef { option, .. }| match option {
ColumnOption::Default(expr) => Some(expr),
_ => None,
})
self.options.iter().find_map(|option| match option {
ColumnOption::Default(expr) => Some(expr),
_ => None,
})
}
}
12 changes: 3 additions & 9 deletions core/src/executor/alter/table.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
super::{validate, AlterError},
crate::{
ast::{ColumnDef, ColumnOption, ColumnOptionDef, Query, SetExpr, TableFactor, Values},
ast::{ColumnDef, ColumnOption, Query, SetExpr, TableFactor, Values},
data::{Schema, TableError},
executor::{evaluate_stateless, select::select},
prelude::{DataType, Value},
Expand Down Expand Up @@ -42,10 +42,7 @@ pub async fn create_table<T: GStore + GStoreMut>(
let column_def = ColumnDef {
name: "N".into(),
data_type: DataType::Int,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::NotNull,
}],
options: vec![ColumnOption::NotNull],
};

vec![column_def]
Expand Down Expand Up @@ -94,10 +91,7 @@ pub async fn create_table<T: GStore + GStoreMut>(
.map(|(i, data_type)| ColumnDef {
name: format!("column{}", i + 1),
data_type,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Null,
}],
options: vec![ColumnOption::Null],
})
.collect::<Vec<_>>();

Expand Down
14 changes: 6 additions & 8 deletions core/src/executor/alter/validate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
super::AlterError,
crate::{
ast::{ColumnDef, ColumnOption, ColumnOptionDef, DataType},
ast::{ColumnDef, ColumnOption, DataType},
executor::evaluate_stateless,
result::Result,
},
Expand All @@ -19,7 +19,7 @@ pub fn validate(column_def: &ColumnDef) -> Result<()> {
if matches!(data_type, DataType::Float | DataType::Map)
&& options
.iter()
.any(|ColumnOptionDef { option, .. }| matches!(option, ColumnOption::Unique { .. }))
.any(|option| matches!(option, ColumnOption::Unique { .. }))
{
return Err(AlterError::UnsupportedDataTypeForUniqueColumn(
name.to_owned(),
Expand All @@ -28,12 +28,10 @@ pub fn validate(column_def: &ColumnDef) -> Result<()> {
.into());
}

let default = options
.iter()
.find_map(|ColumnOptionDef { option, .. }| match option {
ColumnOption::Default(expr) => Some(expr),
_ => None,
});
let default = options.iter().find_map(|option| match option {
ColumnOption::Default(expr) => Some(expr),
_ => None,
});

if let Some(expr) = default {
evaluate_stateless(None, expr)?;
Expand Down
11 changes: 5 additions & 6 deletions core/src/executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use {
},
crate::{
ast::{
ColumnDef, ColumnOption, ColumnOptionDef, DataType, Dictionary, Expr, Query,
SelectItem, SetExpr, Statement, TableAlias, TableFactor, TableWithJoins, Values,
Variable,
ColumnDef, ColumnOption, DataType, Dictionary, Expr, Query, SelectItem, SetExpr,
Statement, TableAlias, TableFactor, TableWithJoins, Values, Variable,
},
data::{Key, Row, Schema},
executor::limit::Limit,
Expand Down Expand Up @@ -229,9 +228,9 @@ pub async fn execute<T: GStore + GStoreMut>(
.iter()
.enumerate()
.find(|(_, ColumnDef { options, .. })| {
options.iter().any(|ColumnOptionDef { option, .. }| {
option == &ColumnOption::Unique { is_primary: true }
})
options
.iter()
.any(|option| option == &ColumnOption::Unique { is_primary: true })
})
.map(|(i, _)| i);

Expand Down
5 changes: 2 additions & 3 deletions core/src/executor/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ pub async fn fetch_relation_rows<'a>(
|ColumnDef { name, options, .. }| {
options
.iter()
.any(|column_option_def| {
column_option_def.option
== ColumnOption::Unique { is_primary: true }
.any(|option| {
option == &ColumnOption::Unique { is_primary: true }
})
.then_some(name)
},
Expand Down
8 changes: 4 additions & 4 deletions core/src/executor/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
evaluate::{evaluate, Evaluated},
},
crate::{
ast::{Assignment, ColumnDef, ColumnOption, ColumnOptionDef},
ast::{Assignment, ColumnDef, ColumnOption},
data::{schema::ColumnDefExt, Row, Value},
result::Result,
store::GStore,
Expand Down Expand Up @@ -51,9 +51,9 @@ impl<'a> Update<'a> {
return false;
}

options.iter().any(|ColumnOptionDef { option, .. }| {
option == &ColumnOption::Unique { is_primary: true }
})
options
.iter()
.any(|option| option == &ColumnOption::Unique { is_primary: true })
}) {
return Err(UpdateError::UpdateOnPrimaryKeyNotSupported(id.to_owned()).into());
}
Expand Down
59 changes: 27 additions & 32 deletions core/src/executor/validate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
ast::{ColumnDef, ColumnOption, ColumnOptionDef},
ast::{ColumnDef, ColumnOption},
data::{Key, Row, Value},
result::Result,
store::Store,
Expand Down Expand Up @@ -91,41 +91,36 @@ pub async fn validate_unique(
All(Vec<(usize, String)>),
}

let columns = match &column_validation {
ColumnValidation::All(column_defs) => {
let primary_key_index = column_defs
.iter()
.enumerate()
.find(|(_, column_def)| {
column_def
.options
.iter()
.any(|ColumnOptionDef { option, .. }| {
let columns =
match &column_validation {
ColumnValidation::All(column_defs) => {
let primary_key_index = column_defs
.iter()
.enumerate()
.find(|(_, column_def)| {
column_def.options.iter().any(|option| {
matches!(option, ColumnOption::Unique { is_primary: true })
})
})
.map(|(i, _)| i);
let other_unique_column_def_count = column_defs
.iter()
.filter(|column_def| {
column_def
.options
.iter()
.any(|ColumnOptionDef { option, .. }| {
})
.map(|(i, _)| i);
let other_unique_column_def_count = column_defs
.iter()
.filter(|column_def| {
column_def.options.iter().any(|option| {
matches!(option, ColumnOption::Unique { is_primary: false })
})
})
.count();
})
.count();

match (primary_key_index, other_unique_column_def_count) {
(Some(primary_key_index), 0) => Columns::PrimaryKeyOnly(primary_key_index),
_ => Columns::All(fetch_all_unique_columns(column_defs)),
match (primary_key_index, other_unique_column_def_count) {
(Some(primary_key_index), 0) => Columns::PrimaryKeyOnly(primary_key_index),
_ => Columns::All(fetch_all_unique_columns(column_defs)),
}
}
}
ColumnValidation::SpecifiedColumns(column_defs, specified_columns) => Columns::All(
fetch_specified_unique_columns(column_defs, specified_columns),
),
};
ColumnValidation::SpecifiedColumns(column_defs, specified_columns) => Columns::All(
fetch_specified_unique_columns(column_defs, specified_columns),
),
};

match columns {
Columns::PrimaryKeyOnly(primary_key_index) => {
Expand Down Expand Up @@ -197,7 +192,7 @@ fn fetch_all_unique_columns(column_defs: &[ColumnDef]) -> Vec<(usize, String)> {
table_col
.options
.iter()
.any(|opt_def| matches!(opt_def.option, ColumnOption::Unique { .. }))
.any(|option| matches!(option, ColumnOption::Unique { .. }))
.then_some((i, table_col.name.to_owned()))
})
.collect()
Expand All @@ -214,7 +209,7 @@ fn fetch_specified_unique_columns(
table_col
.options
.iter()
.any(|ColumnOptionDef { option, .. }| match option {
.any(|option| match option {
ColumnOption::Unique { .. } => specified_columns
.iter()
.any(|specified_col| specified_col == &table_col.name),
Expand Down
9 changes: 2 additions & 7 deletions core/src/plan/planner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use {
super::context::Context,
crate::{
ast::{
ColumnDef, ColumnOption, ColumnOptionDef, Expr, Function, Query, TableAlias,
TableFactor,
},
ast::{ColumnDef, ColumnOption, Expr, Function, Query, TableAlias, TableFactor},
data::Schema,
},
std::rc::Rc,
Expand Down Expand Up @@ -211,9 +208,7 @@ pub trait Planner<'a> {
.find_map(|ColumnDef { name, options, .. }| {
options
.iter()
.any(|ColumnOptionDef { option, .. }| {
matches!(option, ColumnOption::Unique { is_primary: true })
})
.any(|option| matches!(option, ColumnOption::Unique { is_primary: true }))
.then(|| name.as_str())
});

Expand Down
Loading

0 comments on commit 6fc167e

Please sign in to comment.