Skip to content

Commit

Permalink
chore: codefmt
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Oct 7, 2024
1 parent 4af0559 commit 5e00745
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 92 deletions.
42 changes: 5 additions & 37 deletions src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,8 @@ impl ScalarExpression {
}

match self {
ScalarExpression::Alias { expr, alias } => {
ScalarExpression::Alias { expr, .. } => {
expr.try_reference(output_exprs);

match alias {
AliasType::Name(_) => (),
AliasType::Expr(alias_expr) => {
alias_expr.try_reference(output_exprs);
}
}
}
ScalarExpression::TypeCast { expr, .. } => {
expr.try_reference(output_exprs);
Expand Down Expand Up @@ -478,13 +471,7 @@ impl ScalarExpression {

pub fn has_count_star(&self) -> bool {
match self {
ScalarExpression::Alias { expr, alias } => {
expr.has_count_star()
|| match alias {
AliasType::Name(_) => false,
AliasType::Expr(alias_expr) => alias_expr.has_count_star(),
}
}
ScalarExpression::Alias { expr, .. } => expr.has_count_star(),
ScalarExpression::TypeCast { expr, .. } => expr.has_count_star(),
ScalarExpression::IsNull { expr, .. } => expr.has_count_star(),
ScalarExpression::Unary { expr, .. } => expr.has_count_star(),
Expand Down Expand Up @@ -642,15 +629,8 @@ impl ScalarExpression {
ScalarExpression::ColumnRef(col) => {
vec.push(col.clone());
}
ScalarExpression::Alias { expr, alias } => {
ScalarExpression::Alias { expr, .. } => {
columns_collect(expr, vec, only_column_ref);

match alias {
AliasType::Name(_) => (),
AliasType::Expr(alias_expr) => {
columns_collect(alias_expr, vec, only_column_ref);
}
}
}
ScalarExpression::TypeCast { expr, .. } => {
columns_collect(expr, vec, only_column_ref)
Expand Down Expand Up @@ -776,13 +756,7 @@ impl ScalarExpression {
ScalarExpression::ColumnRef(column) => {
column.table_name().is_some() && column.id().is_some()
}
ScalarExpression::Alias { expr, alias } => {
expr.has_table_ref_column()
|| match alias {
AliasType::Name(_) => false,
AliasType::Expr(expr) => expr.has_table_ref_column(),
}
}
ScalarExpression::Alias { expr, .. } => expr.has_table_ref_column(),
ScalarExpression::TypeCast { expr, .. } | ScalarExpression::IsNull { expr, .. } => {
expr.has_table_ref_column()
}
Expand Down Expand Up @@ -900,13 +874,7 @@ impl ScalarExpression {
ScalarExpression::AggCall { .. } => true,
ScalarExpression::Constant(_) => false,
ScalarExpression::ColumnRef(_) => false,
ScalarExpression::Alias { expr, alias } => {
expr.has_agg_call()
|| match alias {
AliasType::Name(_) => false,
AliasType::Expr(alias_expr) => alias_expr.has_agg_call(),
}
}
ScalarExpression::Alias { expr, .. } => expr.has_agg_call(),
ScalarExpression::TypeCast { expr, .. } => expr.has_agg_call(),
ScalarExpression::IsNull { expr, .. } => expr.has_agg_call(),
ScalarExpression::Unary { expr, .. } => expr.has_agg_call(),
Expand Down
18 changes: 13 additions & 5 deletions src/serdes/alias_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ReferenceSerialization for AliasType {
#[cfg(test)]
pub(crate) mod test {
use crate::errors::DatabaseError;
use crate::expression::AliasType;
use crate::expression::{AliasType, ScalarExpression};
use crate::serdes::{ReferenceSerialization, ReferenceTables};
use crate::storage::rocksdb::RocksTransaction;
use std::io::{Cursor, Seek, SeekFrom};
Expand All @@ -59,14 +59,22 @@ pub(crate) mod test {
fn test_serialization() -> Result<(), DatabaseError> {
let mut bytes = Vec::new();
let mut cursor = Cursor::new(&mut bytes);
let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();

let source = AliasType::Name("hello".to_string());
source.encode(&mut cursor, false, &mut reference_tables)?;
let name_source = AliasType::Name("hello".to_string());
name_source.encode(&mut cursor, false, &mut reference_tables)?;
cursor.seek(SeekFrom::Start(0))?;
assert_eq!(
AliasType::decode::<RocksTransaction, _>(&mut cursor, None, &reference_tables)?,
source
name_source
);
cursor.seek(SeekFrom::Start(0))?;
let expr_source = AliasType::Expr(Box::new(ScalarExpression::Empty));
expr_source.encode(&mut cursor, false, &mut reference_tables)?;
cursor.seek(SeekFrom::Start(0))?;
assert_eq!(
AliasType::decode::<RocksTransaction, _>(&mut cursor, None, &reference_tables)?,
expr_source
);

Ok(())
Expand Down
20 changes: 7 additions & 13 deletions src/serdes/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ impl ReferenceSerialization for ColumnRelation {
0 => ColumnRelation::None,
1 => {
let column_id = ColumnId::decode(reader)?;
let table_name = reference_tables
.get(u32::decode(reader)? as usize)
.clone();
let table_name = reference_tables.get(u32::decode(reader)? as usize).clone();

ColumnRelation::Table {
column_id,
Expand Down Expand Up @@ -162,12 +160,7 @@ impl ReferenceSerialization for ColumnDesc {
let column_datatype = LogicalType::decode(reader)?;
let default = Option::<ScalarExpression>::decode(reader, drive, reference_tables)?;

ColumnDesc::new(
column_datatype,
is_primary,
is_unique,
default,
)
ColumnDesc::new(column_datatype, is_primary, is_unique, default)
}
}

Expand Down Expand Up @@ -201,7 +194,7 @@ pub(crate) mod test {
build_table(&table_cache, &mut transaction)?;

let mut cursor = Cursor::new(Vec::new());
let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();

{
let ref_column = Arc::new(ColumnCatalog {
Expand Down Expand Up @@ -278,7 +271,7 @@ pub(crate) mod test {
#[test]
fn test_column_summary_serialization() -> Result<(), DatabaseError> {
let mut cursor = Cursor::new(Vec::new());
let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();
let summary = ColumnSummary {
name: "c1".to_string(),
relation: ColumnRelation::Table {
Expand All @@ -304,7 +297,7 @@ pub(crate) mod test {
#[test]
fn test_column_relation_serialization() -> Result<(), DatabaseError> {
let mut cursor = Cursor::new(Vec::new());
let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();
let none_relation = ColumnRelation::None;
none_relation.encode(&mut cursor, false, &mut reference_tables)?;
cursor.seek(SeekFrom::Start(0))?;
Expand All @@ -328,14 +321,15 @@ pub(crate) mod test {
None,
&reference_tables,
)?;
assert_eq!(table_relation, decode_relation);

Ok(())
}

#[test]
fn test_column_desc_serialization() -> Result<(), DatabaseError> {
let mut cursor = Cursor::new(Vec::new());
let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();
let desc = ColumnDesc {
column_datatype: LogicalType::Integer,
is_primary: false,
Expand Down
5 changes: 2 additions & 3 deletions src/serdes/logic_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,10 @@ impl Serialization for LogicalType {
#[cfg(test)]
pub(crate) mod test {
use crate::errors::DatabaseError;
use crate::serdes::{ReferenceTables, Serialization};
use crate::serdes::Serialization;
use crate::types::LogicalType;
use sqlparser::ast::CharLengthUnits;
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use std::sync::Arc;
use std::io::{Cursor, Seek, SeekFrom};

#[test]
fn test_logic_type_serialization() -> Result<(), DatabaseError> {
Expand Down
39 changes: 12 additions & 27 deletions src/serdes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ mod unary_operator;
use crate::catalog::TableName;
use crate::errors::DatabaseError;
use crate::storage::{TableCache, Transaction};
use std::io;
use std::io::{Read, Write};
use std::{io, mem};

pub trait ReferenceSerialization: Sized {
fn encode<W: Write>(
Expand All @@ -45,42 +45,27 @@ pub trait Serialization: Sized {
fn decode<R: Read>(reader: &mut R) -> Result<Self, Self::Error>;
}

pub enum ReferenceTables {
Single(Option<TableName>),
Multiple(Vec<TableName>),
#[derive(Default)]
pub struct ReferenceTables {
tables: Vec<TableName>,
}

impl ReferenceTables {
pub fn single() -> Self {
ReferenceTables::Single(None)
}

pub fn multiple() -> Self {
ReferenceTables::Multiple(Vec::new())
pub fn new() -> Self {
ReferenceTables { tables: vec![] }
}

pub fn get(&self, i: usize) -> &TableName {
match self {
ReferenceTables::Single(table_name) => table_name.as_ref().unwrap(),
ReferenceTables::Multiple(tables) => &tables[i],
}
&self.tables[i]
}

pub fn push_or_replace(&mut self, table_name: &TableName) -> usize {
match self {
ReferenceTables::Single(table) => {
let _ = mem::replace(table, Some(table_name.clone()));
0
}
ReferenceTables::Multiple(tables) => {
for (i, item) in tables.iter().enumerate() {
if item == table_name {
return i;
}
}
tables.push(table_name.clone());
tables.len() - 1
for (i, item) in self.tables.iter().enumerate() {
if item == table_name {
return i;
}
}
self.tables.push(table_name.clone());
self.tables.len() - 1
}
}
2 changes: 1 addition & 1 deletion src/serdes/scala_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ pub(crate) mod test {
build_table(&table_cache, &mut transaction)?;

let mut cursor = Cursor::new(Vec::new());
let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();

fn_assert(
&mut cursor,
Expand Down
8 changes: 4 additions & 4 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub trait Transaction: Sized {
}

let column = table.get_column_by_id(&col_id).unwrap();
let (key, value) = TableCodec::encode_column(column, &mut ReferenceTables::single())?;
let (key, value) = TableCodec::encode_column(column, &mut ReferenceTables::new())?;
self.set(key, value)?;
table_cache.remove(table_name);

Expand All @@ -261,7 +261,7 @@ pub trait Transaction: Sized {
if let Some(table_catalog) = self.table(table_cache, table_name.clone()).cloned() {
let column = table_catalog.get_column_by_name(column_name).unwrap();

let (key, _) = TableCodec::encode_column(column, &mut ReferenceTables::single())?;
let (key, _) = TableCodec::encode_column(column, &mut ReferenceTables::new())?;
self.remove(&key)?;

for index_meta in table_catalog.indexes.iter() {
Expand Down Expand Up @@ -307,7 +307,7 @@ pub trait Transaction: Sized {
self.create_index_meta_from_column(&mut table_catalog)?;
self.set(table_key, value)?;

let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();
for column in table_catalog.columns() {
let (key, value) = TableCodec::encode_column(column, &mut reference_tables)?;
self.set(key, value)?;
Expand Down Expand Up @@ -444,7 +444,7 @@ pub trait Transaction: Sized {

let mut columns = Vec::new();
let mut index_metas = Vec::new();
let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();
let _ = reference_tables.push_or_replace(&table_name);

// Tips: only `Column`, `IndexMeta`, `TableMeta`
Expand Down
4 changes: 2 additions & 2 deletions src/storage/table_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ mod tests {
};
let col = Arc::new(col);

let mut reference_tables = ReferenceTables::single();
let mut reference_tables = ReferenceTables::new();

let (_, bytes) = TableCodec::encode_column(&col, &mut reference_tables).unwrap();
let mut cursor = Cursor::new(bytes.as_ref());
Expand Down Expand Up @@ -558,7 +558,7 @@ mod tests {
};

let (key, _) =
TableCodec::encode_column(&Arc::new(col), &mut ReferenceTables::single()).unwrap();
TableCodec::encode_column(&Arc::new(col), &mut ReferenceTables::new()).unwrap();
key
};

Expand Down

0 comments on commit 5e00745

Please sign in to comment.