Skip to content

Commit

Permalink
Update data::Row to have columns: Rc<[String]> field
Browse files Browse the repository at this point in the history
  • Loading branch information
panarch committed Nov 27, 2022
1 parent bc68a53 commit f08fb22
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 82 deletions.
54 changes: 32 additions & 22 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, slice::Iter, vec::IntoIter},
std::{fmt::Debug, rc::Rc, slice::Iter, vec::IntoIter},
thiserror::Error,
};

Expand Down Expand Up @@ -38,22 +38,25 @@ enum Columns<I1, I2> {
}

#[derive(Clone, Debug, PartialEq)]
pub struct Row(pub Vec<Value>);
pub struct Row {
pub columns: Rc<[String]>,
pub values: Vec<Value>,
}

impl Row {
pub fn get_value_by_index(&self, index: usize) -> Option<&Value> {
self.0.get(index)
self.values.get(index)
}

pub fn get_value(&self, columns: &[String], ident: &str) -> Option<&Value> {
columns
.iter()
.position(|column| column == ident)
.and_then(|index| self.0.get(index))
.and_then(|index| self.values.get(index))
}

pub fn take_first_value(self) -> Result<Value> {
self.0
self.values
.into_iter()
.next()
.ok_or_else(|| RowError::ConflictOnEmptyRow.into())
Expand Down Expand Up @@ -82,7 +85,7 @@ impl Row {

let column_name_value_list = columns.zip(values.iter()).collect::<Vec<(_, _)>>();

column_defs
let values = column_defs
.iter()
.map(|column_def| {
let ColumnDef {
Expand All @@ -99,16 +102,26 @@ impl Row {

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

Ok(value)
}
(None, None, true) => Ok(Value::Null),
(None, None, false) => {
Err(RowError::LackOfRequiredColumn(def_name.to_owned()).into())
}
}
})
.collect::<Result<_>>()
.map(Self)
.collect::<Result<Vec<Value>>>()?;

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

Ok(Row { columns, values })
}

pub fn validate(&self, column_defs: &[ColumnDef]) -> Result<()> {
Expand Down Expand Up @@ -136,15 +149,15 @@ impl Row {
}

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

pub fn len(&self) -> usize {
self.0.len()
self.values.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
self.values.is_empty()
}
}

Expand All @@ -153,29 +166,26 @@ impl IntoIterator for Row {
type IntoIter = IntoIter<Self::Item>;

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

impl From<Row> for Vec<Value> {
fn from(row: Row) -> Self {
row.0
}
}

impl From<Vec<Value>> for Row {
fn from(values: Vec<Value>) -> Self {
Row(values)
row.values
}
}

#[cfg(test)]
mod tests {
use {super::Row, crate::data::Value};
use {super::Row, crate::data::Value, std::rc::Rc};

#[test]
fn len() {
let row: Row = vec![Value::Bool(true), Value::I64(100)].into();
let row = Row {
columns: Rc::from(vec!["T".to_owned()]),
values: vec![Value::Bool(true), Value::I64(100)],
};

assert_eq!(row.len(), 2);
assert!(!row.is_empty());
Expand Down
10 changes: 5 additions & 5 deletions core/src/executor/context/blend_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum BlendContextRow {
#[derive(Debug)]
pub struct BlendContext<'a> {
table_alias: &'a str,
columns: Rc<[String]>,
pub columns: Rc<[String]>,
row: BlendContextRow,
next: Option<Rc<BlendContext<'a>>>,
}
Expand Down Expand Up @@ -73,8 +73,8 @@ impl<'a> BlendContext<'a> {
pub fn get_alias_values(&self, alias: &str) -> Option<Vec<Value>> {
if self.table_alias == alias {
let values = match &self.row {
BlendContextRow::Shared(row) => row.0.clone(),
BlendContextRow::Single(Some(row)) => row.0.clone(),
BlendContextRow::Shared(row) => row.values.clone(),
BlendContextRow::Single(Some(row)) => row.values.clone(),
BlendContextRow::Single(None) => self.columns.iter().map(|_| Value::Null).collect(),
};

Expand All @@ -88,8 +88,8 @@ impl<'a> BlendContext<'a> {

pub fn get_all_values(&'a self) -> Vec<Value> {
let values: Vec<Value> = match &self.row {
BlendContextRow::Shared(row) => row.0.clone(),
BlendContextRow::Single(Some(row)) => row.0.clone(),
BlendContextRow::Shared(row) => row.values.clone(),
BlendContextRow::Single(Some(row)) => row.values.clone(),
BlendContextRow::Single(None) => self.columns.iter().map(|_| Value::Null).collect(),
};

Expand Down
2 changes: 1 addition & 1 deletion core/src/executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ pub async fn execute<T: GStore + GStoreMut>(
let rows = try_block!(storage, {
let rows = select(&storage, &query, None).await?;
let rows = rows
.map_ok(|Row(values)| values)
.map_ok(|row| row.values)
.try_collect::<Vec<_>>()
.await?;
Ok(rows)
Expand Down

0 comments on commit f08fb22

Please sign in to comment.