Skip to content

Commit

Permalink
Merge branch 'main' into custom_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
KyGost committed Apr 4, 2022
2 parents c973f53 + d72f785 commit a79bdea
Show file tree
Hide file tree
Showing 37 changed files with 250 additions and 280 deletions.
2 changes: 1 addition & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn setup_glue() -> Glue {
}
}

let storage = SledStorage::new(&path)
let storage = SledStorage::new(path)
.map(Storage::new_sled)
.expect("Create Storage");

Expand Down
2 changes: 1 addition & 1 deletion src/data/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Index {
.map(|(key, mut values)| (values.swap_remove(column_index), key))
.collect();

storage.update_index(&table, &self.name, keys).await
storage.update_index(table, &self.name, keys).await
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/data/value/big_endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl BigEndian for Value {
]
.concat(),
F64(v) => [SEP.as_slice(), &v.to_be_bytes()].concat(),
Str(v) => [SEP.as_slice(), &v.as_bytes()].concat(),
Str(v) => [SEP.as_slice(), v.as_bytes()].concat(),
_ => unimplemented!(),
}
}
Expand Down
146 changes: 79 additions & 67 deletions src/data/value/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,82 +16,92 @@ pub trait CastWithRules<Output> {
// Cores
impl Cast<bool> for Value {
fn cast(self) -> Result<bool> {
self.clone().convert().or(Ok(match self {
Value::Bool(value) => value,
Value::I64(value) => match value {
1 => true,
0 => false,
_ => return Err(ValueError::ImpossibleCast.into()),
},
Value::F64(value) => {
if value.eq(&1.0) {
true
} else if value.eq(&0.0) {
false
} else {
return Err(ValueError::ImpossibleCast.into());
self.clone().convert().or_else(|_| {
Ok(match self {
Value::Bool(value) => value,
Value::I64(value) => match value {
1 => true,
0 => false,
_ => return Err(ValueError::ImpossibleCast.into()),
},
Value::F64(value) => {
if value.eq(&1.0) {
true
} else if value.eq(&0.0) {
false
} else {
return Err(ValueError::ImpossibleCast.into());
}
}
}
Value::Str(value) => match value.to_uppercase().as_str() {
"TRUE" => true,
"FALSE" => false,
_ => return Err(ValueError::ImpossibleCast.into()),
},
Value::Null => return Err(ValueError::ImpossibleCast.into()),
_ => unimplemented!(),
}))
Value::Str(value) => match value.to_uppercase().as_str() {
"TRUE" => true,
"FALSE" => false,
_ => return Err(ValueError::ImpossibleCast.into()),
},
Value::Null => return Err(ValueError::ImpossibleCast.into()),
_ => unimplemented!(),
})
})
}
}

impl Cast<i64> for Value {
fn cast(self) -> Result<i64> {
self.clone().convert().or(Ok(match self {
Value::Bool(value) => {
if value {
1
} else {
0
self.clone().convert().or_else(|_| {
Ok(match self {
Value::Bool(value) => {
if value {
1
} else {
0
}
}
}
Value::I64(value) => value,
Value::F64(value) => value.trunc() as i64,
Value::Str(value) => lexical::parse(value).map_err(|_| ValueError::ImpossibleCast)?,
Value::Null => return Err(ValueError::ImpossibleCast.into()),
_ => unimplemented!(),
}))
Value::I64(value) => value,
Value::F64(value) => value.trunc() as i64,
Value::Str(value) => {
lexical::parse(value).map_err(|_| ValueError::ImpossibleCast)?
}
Value::Null => return Err(ValueError::ImpossibleCast.into()),
_ => unimplemented!(),
})
})
}
}

impl Cast<f64> for Value {
fn cast(self) -> Result<f64> {
self.clone().convert().or(Ok(match self {
Value::Bool(value) => {
if value {
1.0
} else {
0.0
self.clone().convert().or_else(|_| {
Ok(match self {
Value::Bool(value) => {
if value {
1.0
} else {
0.0
}
}
}
Value::I64(value) => (value as f64).trunc(),
Value::F64(value) => value,
Value::Str(value) => {
fast_float::parse(value).map_err(|_| ValueError::ImpossibleCast)?
}
Value::Null => return Err(ValueError::ImpossibleCast.into()),
_ => unimplemented!(),
}))
Value::I64(value) => (value as f64).trunc(),
Value::F64(value) => value,
Value::Str(value) => {
fast_float::parse(value).map_err(|_| ValueError::ImpossibleCast)?
}
Value::Null => return Err(ValueError::ImpossibleCast.into()),
_ => unimplemented!(),
})
})
}
}
impl Cast<String> for Value {
fn cast(self) -> Result<String> {
self.clone().convert().or(Ok(match self {
Value::Bool(value) => (if value { "TRUE" } else { "FALSE" }).to_string(),
Value::I64(value) => lexical::to_string(value),
Value::F64(value) => lexical::to_string(value),
Value::Str(value) => value,
Value::Null => String::from("NULL"),
_ => unimplemented!(),
}))
self.clone().convert().or_else(|_| {
Ok(match self {
Value::Bool(value) => (if value { "TRUE" } else { "FALSE" }).to_string(),
Value::I64(value) => lexical::to_string(value),
Value::F64(value) => lexical::to_string(value),
Value::Str(value) => value,
Value::Null => String::from("NULL"),
_ => unimplemented!(),
})
})
}
}

Expand Down Expand Up @@ -133,22 +143,22 @@ impl CastWithRules<String> for Value {
fn cast_with_rule(self, rule: Self) -> Result<String> {
match rule {
Value::I64(000) | Value::Bool(true) => self.cast(),
Value::Str(specified) if specified == String::from("DATETIME") => {
Value::Str(specified) if specified == *"DATETIME" => {
Ok(NaiveDateTime::from_timestamp(self.convert()?, 0)
.format("%F %T")
.to_string())
}
Value::Str(specified) if specified == String::from("MONEY") => {
Value::Str(specified) if specified == *"MONEY" => {
let value: f64 = self.convert()?;
let value = (value * 100.0).round() / 100.0;
let value = value.separate_with_commas();
Ok(format!("${}", value))
}
Value::Str(specified) if specified == String::from("SEPARATED") => {
Value::Str(specified) if specified == *"SEPARATED" => {
let value: f64 = self.convert()?;
let value = (value * 100.0).round() / 100.0;
let value = value.separate_with_commas();
Ok(format!("{}", value))
Ok(value)
}
Value::Str(format) if matches!(self, Value::I64(..)) => {
// TODO: TIMESTAMP type
Expand All @@ -170,9 +180,11 @@ impl Cast<NaiveDateTime> for Value {
// Default (from Timestamp)
fn cast(self) -> Result<NaiveDateTime> {
let timestamp: i64 = self.cast()?;
NaiveDateTime::from_timestamp_opt(timestamp, 0).ok_or(ValueError::ImpossibleCast.into())
NaiveDateTime::from_timestamp_opt(timestamp, 0)
.ok_or_else(|| ValueError::ImpossibleCast.into())
}
}
#[allow(clippy::zero_prefixed_literal)]
impl CastWithRules<NaiveDateTime> for Value {
fn cast_with_rule(self, rule: Self) -> Result<NaiveDateTime> {
fn for_format_datetime(string: Value, format: &str) -> Result<NaiveDateTime> {
Expand All @@ -198,7 +210,7 @@ impl CastWithRules<NaiveDateTime> for Value {
rules
.iter()
.find_map(|try_rule| try_value.clone().cast_with_rule((*try_rule).into()).ok())
.ok_or(ValueError::ParseError(try_value.clone(), "TIMESTAMP").into())
.ok_or_else(|| ValueError::ParseError(try_value.clone(), "TIMESTAMP").into())
}
const TRY_RULES_TIMESTAMP: [i64; 1] = [000];
const TRY_RULES_DATETIME: [i64; 7] = [010, 011, 020, 021, 030, 031, 060];
Expand All @@ -213,8 +225,8 @@ impl CastWithRules<NaiveDateTime> for Value {
"DATE" => try_rules(&self, &TRY_RULES_DATE),
"TIME" => try_rules(&self, &TRY_RULES_TIME),
custom_format => for_format_datetime(self.clone(), custom_format)
.or(for_format_date(self.clone(), custom_format))
.or(for_format_time(self, custom_format)),
.or_else(|_| for_format_date(self.clone(), custom_format))
.or_else(|_| for_format_time(self, custom_format)),
},
Value::I64(000) => {
// From Timestamp (Default)
Expand Down
2 changes: 1 addition & 1 deletion src/data/value/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl<'a> TryFrom<&'a AstValue> for Value {

fn try_from(ast_value: &'a AstValue) -> Result<Self> {
match ast_value {
AstValue::Boolean(value) => Ok(Value::Bool(value.clone())),
AstValue::Boolean(value) => Ok(Value::Bool(*value)),
AstValue::Number(value, false) => value
.parse::<i64>()
.map_or_else(
Expand Down
2 changes: 2 additions & 0 deletions src/data/value/methods/binary.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::should_implement_trait)] // TODO

use {
super::ValueCore,
crate::{Convert, ConvertFrom, Result, Value, ValueError},
Expand Down
2 changes: 1 addition & 1 deletion src/data/value/methods/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro_rules! expect_arguments {
macro_rules! optional_expect_arguments {
($arguments: expr, $min: expr, $max: expr) => {
match $arguments.len() {
len if len >= $min && len <= $max => (),
len if ($min..=$max).contains(&len) => (),
found => {
return Err(ValueError::NumberOfFunctionParamsNotMatching {
expected: $min,
Expand Down
51 changes: 17 additions & 34 deletions src/data/value/methods/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! expect_arguments {
macro_rules! optional_expect_arguments {
($arguments: expr, $min: expr, $max: expr) => {
match $arguments.len() {
len if len >= $min && len <= $max => (),
len if ($min..=$max).contains(&len) => (),
found => {
return Err(ValueError::NumberOfFunctionParamsNotMatching {
expected: $min,
Expand Down Expand Up @@ -88,31 +88,12 @@ impl Value {
}
pub fn function_timestamp_from_parts(arguments: Vec<Self>) -> Result<Self> {
optional_expect_arguments!(arguments, 1, 6);
protect_null!(arguments
.get(0)
.map(|value| value.clone())
.unwrap_or(Value::I64(1)))
.date_from_parts(
protect_null!(arguments
.get(1)
.map(|value| value.clone())
.unwrap_or(Value::I64(1))),
protect_null!(arguments
.get(2)
.map(|value| value.clone())
.unwrap_or(Value::I64(1))),
protect_null!(arguments
.get(3)
.map(|value| value.clone())
.unwrap_or(Value::I64(0))),
protect_null!(arguments
.get(4)
.map(|value| value.clone())
.unwrap_or(Value::I64(0))),
protect_null!(arguments
.get(5)
.map(|value| value.clone())
.unwrap_or(Value::I64(0))),
protect_null!(arguments.get(0).cloned().unwrap_or(Value::I64(1))).date_from_parts(
protect_null!(arguments.get(1).cloned().unwrap_or(Value::I64(1))),
protect_null!(arguments.get(2).cloned().unwrap_or(Value::I64(1))),
protect_null!(arguments.get(3).cloned().unwrap_or(Value::I64(0))),
protect_null!(arguments.get(4).cloned().unwrap_or(Value::I64(0))),
protect_null!(arguments.get(5).cloned().unwrap_or(Value::I64(0))),
)
}
}
Expand Down Expand Up @@ -174,14 +155,16 @@ impl Value {
match self {
Value::Str(string) if string == "YEAR" => {
let years = datetime.year() + amount as i32;
let calculated = datetime.with_year(years).unwrap_or(
datetime
.with_day(28)
.ok_or(ValueError::DateError)
.unwrap() //?
.with_year(years)
.ok_or(ValueError::DateError)
.unwrap(), //?,
let calculated = datetime.with_year(years).unwrap_or_else(
|| {
datetime
.with_day(28)
.ok_or(ValueError::DateError)
.unwrap() //?
.with_year(years)
.ok_or(ValueError::DateError)
.unwrap()
}, //?,
);
Ok(Value::I64(calculated.timestamp()))
}
Expand Down
3 changes: 2 additions & 1 deletion src/data/value/methods/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ macro_rules! generic {
};
}

#[allow(clippy::should_implement_trait)] // TODO
impl Value {
pub fn unary_plus<Core>(self) -> Result<Self>
where
Core: ValueCore + Clone,
{
let core = Core::convert_from(self)?;
let result = core.clone();
let result = core;
Ok(result.into())
}
pub fn unary_minus<Core>(self) -> Result<Self>
Expand Down
15 changes: 5 additions & 10 deletions src/executor/alter_row/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn delete(
table_name: &ObjectName,
selection: &Option<Expr>,
) -> Result<Payload> {
let table_name = get_name(&table_name)?;
let table_name = get_name(table_name)?;
let Schema {
column_defs,
indexes,
Expand All @@ -27,11 +27,8 @@ pub async fn delete(
let columns = column_defs
.clone()
.into_iter()
.map(|column_def| {
let ColumnDef { name, .. } = column_def;
ColumnInfo::of_name(name.value)
})
.collect();
.map(|ColumnDef { name, .. }| ColumnInfo::of_name(name.value))
.collect::<Vec<ColumnInfo>>();
let filter = selection
.clone()
.map(|selection| {
Expand All @@ -50,7 +47,7 @@ pub async fn delete(
.filter_map(|(key, row)| {
let row = row.0;

let confirm_constraint = filter.confirm_constraint(&row.clone());
let confirm_constraint = filter.confirm_constraint(&row);
match confirm_constraint {
Ok(true) => Some(Ok(key)),
Ok(false) => None,
Expand All @@ -68,9 +65,7 @@ pub async fn delete(
.map(|_| Payload::Delete(num_keys))?;

for index in indexes.iter() {
index
.reset(storages[0].1, &table_name, &column_defs)
.await?; // TODO: Not this; optimise
index.reset(storages[0].1, table_name, &column_defs).await?; // TODO: Not this; optimise
}
Ok(result)
}
Loading

0 comments on commit a79bdea

Please sign in to comment.