Skip to content

Commit

Permalink
Remove redundant columns uses,
Browse files Browse the repository at this point in the history
Remove columns field in BlendContext and FilterContext.
Update Join module not to take columns as init param.
Move get_labels in select/mod.rs to fetch.rs and rename to fetch_lables by merging column fetching codes.
  • Loading branch information
panarch committed Dec 1, 2022
1 parent f08fb22 commit 5c37b7b
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 345 deletions.
42 changes: 14 additions & 28 deletions core/src/data/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
result::Result,
},
serde::Serialize,
std::{fmt::Debug, rc::Rc, slice::Iter, vec::IntoIter},
std::{fmt::Debug, rc::Rc},
thiserror::Error,
};

Expand Down Expand Up @@ -48,8 +48,8 @@ impl Row {
self.values.get(index)
}

pub fn get_value(&self, columns: &[String], ident: &str) -> Option<&Value> {
columns
pub fn get_value(&self, ident: &str) -> Option<&Value> {
self.columns
.iter()
.position(|column| column == ident)
.and_then(|index| self.values.get(index))
Expand All @@ -62,7 +62,12 @@ impl Row {
.ok_or_else(|| RowError::ConflictOnEmptyRow.into())
}

pub fn new(column_defs: &[ColumnDef], columns: &[String], values: &[Expr]) -> Result<Self> {
pub fn new(
column_defs: &[ColumnDef],
labels: Rc<[String]>,
columns: &[String],
values: &[Expr],
) -> Result<Self> {
if !columns.is_empty() && values.len() != columns.len() {
return Err(RowError::ColumnAndValuesNotMatched.into());
} else if values.len() > column_defs.len() {
Expand Down Expand Up @@ -102,10 +107,7 @@ impl Row {

match (value, column_def.get_default(), nullable) {
(Some(&expr), _, _) | (None, Some(expr), _) => {
let value =
evaluate_stateless(None, expr)?.try_into_value(data_type, *nullable)?;

Ok(value)
evaluate_stateless(None, expr)?.try_into_value(data_type, *nullable)
}
(None, None, true) => Ok(Value::Null),
(None, None, false) => {
Expand All @@ -115,13 +117,10 @@ impl Row {
})
.collect::<Result<Vec<Value>>>()?;

// TEMP
let columns = column_defs
.iter()
.map(|column_def| column_def.name.to_owned())
.collect();

Ok(Row { columns, values })
Ok(Row {
columns: labels,
values,
})
}

pub fn validate(&self, column_defs: &[ColumnDef]) -> Result<()> {
Expand All @@ -148,10 +147,6 @@ impl Row {
Ok(())
}

pub fn iter(&self) -> Iter<'_, Value> {
self.values.iter()
}

pub fn len(&self) -> usize {
self.values.len()
}
Expand All @@ -161,15 +156,6 @@ impl Row {
}
}

impl IntoIterator for Row {
type Item = Value;
type IntoIter = IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.values.into_iter()
}
}

impl From<Row> for Vec<Value> {
fn from(row: Row) -> Self {
row.values
Expand Down
21 changes: 7 additions & 14 deletions core/src/executor/context/blend_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,34 @@ use {

#[derive(Debug)]
pub enum BlendContextRow {
Single(Option<Row>),
Single(Row),
Shared(Rc<Row>),
}

#[derive(Debug)]
pub struct BlendContext<'a> {
table_alias: &'a str,
pub columns: Rc<[String]>,
row: BlendContextRow,
next: Option<Rc<BlendContext<'a>>>,
}

impl<'a> BlendContext<'a> {
pub fn new(
table_alias: &'a str,
columns: Rc<[String]>,
row: BlendContextRow,
next: Option<Rc<BlendContext<'a>>>,
) -> Self {
Self {
table_alias,
columns,
row,
next,
}
}

pub fn get_value(&'a self, target: &str) -> Option<&'a Value> {
let value = match &self.row {
BlendContextRow::Shared(row) => row.get_value(&self.columns, target),
BlendContextRow::Single(Some(row)) => row.get_value(&self.columns, target),
BlendContextRow::Single(None) => Some(&Value::Null),
BlendContextRow::Shared(row) => row.get_value(target),
BlendContextRow::Single(row) => row.get_value(target),
};

if value.is_some() {
Expand All @@ -55,9 +51,8 @@ impl<'a> BlendContext<'a> {
}

match &self.row {
BlendContextRow::Shared(row) => row.get_value(&self.columns, target),
BlendContextRow::Single(Some(row)) => row.get_value(&self.columns, target),
BlendContextRow::Single(None) => Some(&Value::Null),
BlendContextRow::Shared(row) => row.get_value(target),
BlendContextRow::Single(row) => row.get_value(target),
}
})();

Expand All @@ -74,8 +69,7 @@ impl<'a> BlendContext<'a> {
if self.table_alias == alias {
let values = match &self.row {
BlendContextRow::Shared(row) => row.values.clone(),
BlendContextRow::Single(Some(row)) => row.values.clone(),
BlendContextRow::Single(None) => self.columns.iter().map(|_| Value::Null).collect(),
BlendContextRow::Single(row) => row.values.clone(),
};

Some(values)
Expand All @@ -89,8 +83,7 @@ impl<'a> BlendContext<'a> {
pub fn get_all_values(&'a self) -> Vec<Value> {
let values: Vec<Value> = match &self.row {
BlendContextRow::Shared(row) => row.values.clone(),
BlendContextRow::Single(Some(row)) => row.values.clone(),
BlendContextRow::Single(None) => self.columns.iter().map(|_| Value::Null).collect(),
BlendContextRow::Single(row) => row.values.clone(),
};

match &self.next {
Expand Down
31 changes: 7 additions & 24 deletions core/src/executor/context/filter_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use {

#[derive(Debug)]
enum Content<'a> {
Some {
table_alias: &'a str,
columns: Rc<[String]>,
row: &'a Row,
},
Some { table_alias: &'a str, row: &'a Row },
None,
}

Expand All @@ -22,18 +18,9 @@ pub struct FilterContext<'a> {
}

impl<'a> FilterContext<'a> {
pub fn new(
table_alias: &'a str,
columns: Rc<[String]>,
row: &'a Row,
next: Option<Rc<FilterContext<'a>>>,
) -> Self {
pub fn new(table_alias: &'a str, row: &'a Row, next: Option<Rc<FilterContext<'a>>>) -> Self {
Self {
content: Content::Some {
table_alias,
columns,
row,
},
content: Content::Some { table_alias, row },
next,
next2: None,
}
Expand All @@ -51,8 +38,8 @@ impl<'a> FilterContext<'a> {
}

pub fn get_value(&'a self, target: &str) -> Option<&'a Value> {
if let Content::Some { columns, row, .. } = &self.content {
let value = row.get_value(columns, target);
if let Content::Some { row, .. } = &self.content {
let value = row.get_value(target);

if value.is_some() {
return value;
Expand All @@ -72,12 +59,8 @@ impl<'a> FilterContext<'a> {

pub fn get_alias_value(&'a self, target_alias: &str, target: &str) -> Option<&'a Value> {
match &self.content {
Content::Some {
table_alias,
columns,
row,
} if table_alias == &target_alias => {
let value = row.get_value(columns, target);
Content::Some { table_alias, row } if table_alias == &target_alias => {
let value = row.get_value(target);

if value.is_some() {
return value;
Expand Down
23 changes: 14 additions & 9 deletions core/src/executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ pub async fn execute<T: GStore + GStoreMut>(
.fetch_schema(table_name)
.await?
.ok_or_else(|| ExecuteError::TableNotFound(table_name.to_owned()))?;
let labels = Rc::from(
column_defs
.iter()
.map(|column_def| column_def.name.to_owned())
.collect::<Vec<_>>(),
);
let column_defs = Rc::from(column_defs);
let column_validation = ColumnValidation::All(Rc::clone(&column_defs));

Expand All @@ -197,9 +203,9 @@ pub async fn execute<T: GStore + GStoreMut>(
let rows = match &source.body {
SetExpr::Values(Values(values_list)) => {
let limit = Limit::new(source.limit.as_ref(), source.offset.as_ref())?;
let rows = values_list
.iter()
.map(|values| Row::new(&column_defs, columns, values));
let rows = values_list.iter().map(|values| {
Row::new(&column_defs, Rc::clone(&labels), columns, values)
});
let rows = stream::iter(rows);
let rows = limit.apply(rows);
let rows = rows.map_ok(Into::into);
Expand Down Expand Up @@ -282,7 +288,7 @@ pub async fn execute<T: GStore + GStoreMut>(
.await?
.and_then(|item| {
let update = &update;
let (_, key, row) = item;
let (key, row) = item;

async move {
let row = update.apply(row).await?;
Expand Down Expand Up @@ -317,11 +323,10 @@ pub async fn execute<T: GStore + GStoreMut>(
selection,
} => {
let (table_name, keys) = try_block!(storage, {
let columns = Rc::from(fetch_columns(&storage, table_name).await?);

let columns = fetch_columns(&storage, table_name).await.map(Rc::from)?;
let keys = fetch(&storage, table_name, columns, selection.as_ref())
.await?
.map_ok(|(_, key, _)| key)
.map_ok(|(key, _)| key)
.try_collect::<Vec<_>>()
.await?;

Expand All @@ -339,7 +344,7 @@ pub async fn execute<T: GStore + GStoreMut>(
//- Selection
Statement::Query(query) => {
let (labels, rows) = try_block!(storage, {
let (labels, rows) = select_with_labels(&storage, query, None, true).await?;
let (labels, rows) = select_with_labels(&storage, query, None).await?;
let rows = rows.map_ok(Into::into).try_collect::<Vec<_>>().await?;
Ok((labels, rows))
});
Expand Down Expand Up @@ -393,7 +398,7 @@ pub async fn execute<T: GStore + GStoreMut>(
};

let (labels, rows) = try_block!(storage, {
let (labels, rows) = select_with_labels(&storage, &query, None, true).await?;
let (labels, rows) = select_with_labels(&storage, &query, None).await?;
let rows = rows.map_ok(Into::into).try_collect::<Vec<_>>().await?;
Ok((labels, rows))
});
Expand Down

0 comments on commit 5c37b7b

Please sign in to comment.