Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify DataValue #257

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "fnck_sql"
version = "0.0.9"
version = "0.0.10"
edition = "2021"
authors = ["Kould <[email protected]>", "Xwg <[email protected]>"]
description = "SQL as a Function for Rust"
Expand Down Expand Up @@ -43,7 +43,7 @@ csv = { version = "1" }
dirs = { version = "5" }
fixedbitset = { version = "0.4" }
itertools = { version = "0.12" }
ordered-float = { version = "4" }
ordered-float = { version = "4", features = ["serde"] }
paste = { version = "1" }
parking_lot = { version = "0.12", features = ["arc_lock"] }
petgraph = { version = "0.6" }
Expand Down
6 changes: 3 additions & 3 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ struct MyStruct {
implement_from_tuple!(
MyStruct, (
c1: i32 => |inner: &mut MyStruct, value| {
if let DataValue::Int32(Some(val)) = value {
if let DataValue::Int32(val) = value {
inner.c1 = val;
}
},
c2: String => |inner: &mut MyStruct, value| {
if let DataValue::Utf8(Some(val)) = value {
inner.c2 = val;
if let DataValue::Utf8 { value, .. } = value {
inner.c2 = value;
}
}
)
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ struct MyStruct {
implement_from_tuple!(
MyStruct, (
c1: i32 => |inner: &mut MyStruct, value| {
if let DataValue::Int32(Some(val)) = value {
if let DataValue::Int32(val) = value {
inner.c1 = val;
}
},
c2: String => |inner: &mut MyStruct, value| {
if let DataValue::Utf8 { value: Some(val), .. } = value {
inner.c2 = val;
if let DataValue::Utf8 { value, .. } = value {
inner.c2 = value;
}
}
)
Expand Down
8 changes: 4 additions & 4 deletions src/binder/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
Expr::TypedString { data_type, value } => {
let logical_type = LogicalType::try_from(data_type.clone())?;
let value = DataValue::Utf8 {
value: Some(value.to_string()),
value: value.to_string(),
ty: Utf8Type::Variable(None),
unit: CharLengthUnits::Characters,
}
Expand Down Expand Up @@ -429,7 +429,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
};

Ok(ScalarExpression::Binary {
op: (op.clone()).into(),
op: (op.clone()).try_into()?,
left_expr,
right_expr,
evaluator: None,
Expand All @@ -450,7 +450,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
};

Ok(ScalarExpression::Unary {
op: (*op).into(),
op: (*op).try_into()?,
expr,
evaluator: None,
ty,
Expand Down Expand Up @@ -690,7 +690,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T

fn wildcard_expr() -> ScalarExpression {
ScalarExpression::Constant(DataValue::Utf8 {
value: Some("*".to_string()),
value: "*".to_string(),
ty: Utf8Type::Variable(None),
unit: CharLengthUnits::Characters,
})
Expand Down
8 changes: 4 additions & 4 deletions src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
let expr = self.bind_expr(expr)?;
match expr {
ScalarExpression::Constant(dv) => match &dv {
DataValue::Int32(Some(v)) if *v >= 0 => limit = Some(*v as usize),
DataValue::Int64(Some(v)) if *v >= 0 => limit = Some(*v as usize),
DataValue::Int32(v) if *v >= 0 => limit = Some(*v as usize),
DataValue::Int64(v) if *v >= 0 => limit = Some(*v as usize),
_ => return Err(DatabaseError::InvalidType),
},
_ => {
Expand All @@ -711,8 +711,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
let expr = self.bind_expr(&expr.value)?;
match expr {
ScalarExpression::Constant(dv) => match &dv {
DataValue::Int32(Some(v)) if *v > 0 => offset = Some(*v as usize),
DataValue::Int64(Some(v)) if *v > 0 => offset = Some(*v as usize),
DataValue::Int32(v) if *v > 0 => offset = Some(*v as usize),
DataValue::Int64(v) if *v > 0 => offset = Some(*v as usize),
_ => return Err(DatabaseError::InvalidType),
},
_ => {
Expand Down
32 changes: 16 additions & 16 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ pub(crate) mod test {
iter.next().unwrap()?,
Tuple::new(
None,
vec![DataValue::Date32(Some(Local::now().num_days_from_ce()))]
vec![DataValue::Date32(Local::now().num_days_from_ce())]
)
);
assert!(iter.next().is_none());
Expand Down Expand Up @@ -558,11 +558,11 @@ pub(crate) mod test {
assert_eq!(iter.schema(), &Arc::new(vec![ColumnRef::from(column)]));
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(Some(3))])
Tuple::new(None, vec![DataValue::Int32(3)])
);
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(Some(4))])
Tuple::new(None, vec![DataValue::Int32(4)])
);
Ok(())
}
Expand All @@ -583,7 +583,7 @@ pub(crate) mod test {
{
let statement = fnck_sql.prepare("explain select * from t1 where b > ?1")?;

let mut iter = fnck_sql.execute(&statement, &[("?1", DataValue::Int32(Some(0)))])?;
let mut iter = fnck_sql.execute(&statement, &[("?1", DataValue::Int32(0))])?;

assert_eq!(
iter.next().unwrap()?.values[0].utf8().unwrap(),
Expand All @@ -601,10 +601,10 @@ pub(crate) mod test {
let mut iter = fnck_sql.execute(
&statement,
&[
("?1", DataValue::Int32(Some(0))),
("?2", DataValue::Int32(Some(0))),
("?3", DataValue::Int32(Some(1))),
("?4", DataValue::Int32(Some(0))),
("?1", DataValue::Int32(0)),
("?2", DataValue::Int32(0)),
("?3", DataValue::Int32(1)),
("?4", DataValue::Int32(0)),
],
)?;
assert_eq!(
Expand All @@ -621,10 +621,10 @@ pub(crate) mod test {
let mut iter = fnck_sql.execute(
&statement,
&[
("?1", DataValue::Int32(Some(9))),
("?2", DataValue::Int32(Some(0))),
("?3", DataValue::Int32(Some(1))),
("?4", DataValue::Int32(Some(0))),
("?1", DataValue::Int32(9)),
("?2", DataValue::Int32(0)),
("?3", DataValue::Int32(1)),
("?4", DataValue::Int32(0)),
],
)?;
assert_eq!(
Expand Down Expand Up @@ -666,20 +666,20 @@ pub(crate) mod test {

assert_eq!(
iter_1.next().unwrap()?.values,
vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))]
vec![DataValue::Int32(0), DataValue::Int32(0)]
);
assert_eq!(
iter_1.next().unwrap()?.values,
vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(1))]
vec![DataValue::Int32(1), DataValue::Int32(1)]
);

assert_eq!(
iter_2.next().unwrap()?.values,
vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))]
vec![DataValue::Int32(0), DataValue::Int32(0)]
);
assert_eq!(
iter_2.next().unwrap()?.values,
vec![DataValue::Int32(Some(3)), DataValue::Int32(Some(3))]
vec![DataValue::Int32(3), DataValue::Int32(3)]
);
drop(iter_1);
drop(iter_2);
Expand Down
6 changes: 2 additions & 4 deletions src/execution/dml/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {
if values.len() == 1 {
throw!(builder.append(&values[0]));
} else {
throw!(
builder.append(&Arc::new(DataValue::Tuple(Some((values, false)))))
);
throw!(builder.append(&Arc::new(DataValue::Tuple(values, false))));
}
}
}
Expand All @@ -121,7 +119,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {

throw!(meta.to_file(&temp_path));
values.push(DataValue::Utf8 {
value: Some(path_str.clone()),
value: path_str.clone(),
ty: Utf8Type::Variable(None),
unit: CharLengthUnits::Characters,
});
Expand Down
2 changes: 1 addition & 1 deletion src/execution/dml/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Insert {
if value.is_none() {
value = throw!(col.default_value());
}
value.unwrap_or_else(|| DataValue::none(col.datatype()))
value.unwrap_or(DataValue::Null)
};
if value.is_null() && !col.nullable() {
yield Err(DatabaseError::NotNull);
Expand Down
4 changes: 2 additions & 2 deletions src/execution/dql/aggregate/avg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ impl Accumulator for AvgAccumulator {
return Ok(DataValue::init(&value_ty));
}
let quantity = if value_ty.is_signed_numeric() {
DataValue::Int64(Some(self.count as i64))
DataValue::Int64(self.count as i64)
} else {
DataValue::UInt32(Some(self.count as u32))
DataValue::UInt32(self.count as u32)
};
let quantity_ty = quantity.logical_type();

Expand Down
4 changes: 2 additions & 2 deletions src/execution/dql/aggregate/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Accumulator for CountAccumulator {
}

fn evaluate(&self) -> Result<DataValue, DatabaseError> {
Ok(DataValue::Int32(Some(self.result)))
Ok(DataValue::Int32(self.result))
}
}

Expand All @@ -50,6 +50,6 @@ impl Accumulator for DistinctCountAccumulator {
}

fn evaluate(&self) -> Result<DataValue, DatabaseError> {
Ok(DataValue::Int32(Some(self.distinct_values.len() as i32)))
Ok(DataValue::Int32(self.distinct_values.len() as i32))
}
}
24 changes: 12 additions & 12 deletions src/execution/dql/aggregate/hash_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,24 @@ mod test {
operator: Operator::Values(ValuesOperator {
rows: vec![
vec![
DataValue::Int32(Some(0)),
DataValue::Int32(Some(2)),
DataValue::Int32(Some(4)),
DataValue::Int32(0),
DataValue::Int32(2),
DataValue::Int32(4),
],
vec![
DataValue::Int32(Some(1)),
DataValue::Int32(Some(3)),
DataValue::Int32(Some(5)),
DataValue::Int32(1),
DataValue::Int32(3),
DataValue::Int32(5),
],
vec![
DataValue::Int32(Some(0)),
DataValue::Int32(Some(1)),
DataValue::Int32(Some(2)),
DataValue::Int32(0),
DataValue::Int32(1),
DataValue::Int32(2),
],
vec![
DataValue::Int32(Some(1)),
DataValue::Int32(Some(2)),
DataValue::Int32(Some(3)),
DataValue::Int32(1),
DataValue::Int32(2),
DataValue::Int32(3),
],
],
schema_ref: t1_schema.clone(),
Expand Down
21 changes: 5 additions & 16 deletions src/execution/dql/aggregate/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ use crate::execution::dql::aggregate::Accumulator;
use crate::expression::BinaryOperator;
use crate::types::evaluator::EvaluatorFactory;
use crate::types::value::DataValue;
use crate::types::LogicalType;

pub struct MinMaxAccumulator {
inner: Option<DataValue>,
op: BinaryOperator,
ty: LogicalType,
}

impl MinMaxAccumulator {
pub fn new(ty: &LogicalType, is_max: bool) -> Self {
pub fn new(is_max: bool) -> Self {
let op = if is_max {
BinaryOperator::Lt
} else {
BinaryOperator::Gt
};

Self {
inner: None,
op,
ty: ty.clone(),
}
Self { inner: None, op }
}
}

Expand All @@ -32,12 +26,10 @@ impl Accumulator for MinMaxAccumulator {
if !value.is_null() {
if let Some(inner_value) = &self.inner {
let evaluator = EvaluatorFactory::binary_create(value.logical_type(), self.op)?;
if let DataValue::Boolean(Some(result)) =
evaluator.0.binary_eval(inner_value, value)
{
if let DataValue::Boolean(result) = evaluator.0.binary_eval(inner_value, value) {
result
} else {
unreachable!()
return Err(DatabaseError::InvalidType);
}
} else {
true
Expand All @@ -49,9 +41,6 @@ impl Accumulator for MinMaxAccumulator {
}

fn evaluate(&self) -> Result<DataValue, DatabaseError> {
Ok(self
.inner
.clone()
.unwrap_or_else(|| DataValue::none(&self.ty)))
Ok(self.inner.clone().unwrap_or(DataValue::Null))
}
}
4 changes: 2 additions & 2 deletions src/execution/dql/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ fn create_accumulator(expr: &ScalarExpression) -> Result<Box<dyn Accumulator>, D
(AggKind::Count, true) => Box::new(DistinctCountAccumulator::new()),
(AggKind::Sum, false) => Box::new(SumAccumulator::new(ty)?),
(AggKind::Sum, true) => Box::new(DistinctSumAccumulator::new(ty)?),
(AggKind::Min, _) => Box::new(MinMaxAccumulator::new(ty, false)),
(AggKind::Max, _) => Box::new(MinMaxAccumulator::new(ty, true)),
(AggKind::Min, _) => Box::new(MinMaxAccumulator::new(false)),
(AggKind::Max, _) => Box::new(MinMaxAccumulator::new(true)),
(AggKind::Avg, _) => Box::new(AvgAccumulator::new(ty)?),
})
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/execution/dql/aggregate/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl SumAccumulator {
debug_assert!(ty.is_numeric());

Ok(Self {
result: DataValue::none(ty),
result: DataValue::Null,
evaluator: EvaluatorFactory::binary_create(ty.clone(), BinaryOperator::Plus)?,
})
}
Expand All @@ -27,7 +27,7 @@ impl Accumulator for SumAccumulator {
fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> {
if !value.is_null() {
if self.result.is_null() {
self.result = DataValue::clone(value);
self.result = value.clone();
} else {
self.result = self.evaluator.0.binary_eval(&self.result, value);
}
Expand Down
Loading
Loading