Skip to content

Commit 468928b

Browse files
authored
chore: simplify DataValue (#257)
* chore: simplify `DataValue` * config: version up
1 parent a7b3d5f commit 468928b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2290
-2706
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "fnck_sql"
5-
version = "0.0.9"
5+
version = "0.0.10"
66
edition = "2021"
77
authors = ["Kould <kould2333@gmail.com>", "Xwg <loloxwg@gmail.com>"]
88
description = "SQL as a Function for Rust"
@@ -43,7 +43,7 @@ csv = { version = "1" }
4343
dirs = { version = "5" }
4444
fixedbitset = { version = "0.4" }
4545
itertools = { version = "0.12" }
46-
ordered-float = { version = "4" }
46+
ordered-float = { version = "4", features = ["serde"] }
4747
paste = { version = "1" }
4848
parking_lot = { version = "0.12", features = ["arc_lock"] }
4949
petgraph = { version = "0.6" }

docs/features.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ struct MyStruct {
1414
implement_from_tuple!(
1515
MyStruct, (
1616
c1: i32 => |inner: &mut MyStruct, value| {
17-
if let DataValue::Int32(Some(val)) = value {
17+
if let DataValue::Int32(val) = value {
1818
inner.c1 = val;
1919
}
2020
},
2121
c2: String => |inner: &mut MyStruct, value| {
22-
if let DataValue::Utf8(Some(val)) = value {
23-
inner.c2 = val;
22+
if let DataValue::Utf8 { value, .. } = value {
23+
inner.c2 = value;
2424
}
2525
}
2626
)

examples/hello_world.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ struct MyStruct {
1212
implement_from_tuple!(
1313
MyStruct, (
1414
c1: i32 => |inner: &mut MyStruct, value| {
15-
if let DataValue::Int32(Some(val)) = value {
15+
if let DataValue::Int32(val) = value {
1616
inner.c1 = val;
1717
}
1818
},
1919
c2: String => |inner: &mut MyStruct, value| {
20-
if let DataValue::Utf8 { value: Some(val), .. } = value {
21-
inner.c2 = val;
20+
if let DataValue::Utf8 { value, .. } = value {
21+
inner.c2 = value;
2222
}
2323
}
2424
)

src/binder/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
8282
Expr::TypedString { data_type, value } => {
8383
let logical_type = LogicalType::try_from(data_type.clone())?;
8484
let value = DataValue::Utf8 {
85-
value: Some(value.to_string()),
85+
value: value.to_string(),
8686
ty: Utf8Type::Variable(None),
8787
unit: CharLengthUnits::Characters,
8888
}
@@ -429,7 +429,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
429429
};
430430

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

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

691691
fn wildcard_expr() -> ScalarExpression {
692692
ScalarExpression::Constant(DataValue::Utf8 {
693-
value: Some("*".to_string()),
693+
value: "*".to_string(),
694694
ty: Utf8Type::Variable(None),
695695
unit: CharLengthUnits::Characters,
696696
})

src/binder/select.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
695695
let expr = self.bind_expr(expr)?;
696696
match expr {
697697
ScalarExpression::Constant(dv) => match &dv {
698-
DataValue::Int32(Some(v)) if *v >= 0 => limit = Some(*v as usize),
699-
DataValue::Int64(Some(v)) if *v >= 0 => limit = Some(*v as usize),
698+
DataValue::Int32(v) if *v >= 0 => limit = Some(*v as usize),
699+
DataValue::Int64(v) if *v >= 0 => limit = Some(*v as usize),
700700
_ => return Err(DatabaseError::InvalidType),
701701
},
702702
_ => {
@@ -711,8 +711,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
711711
let expr = self.bind_expr(&expr.value)?;
712712
match expr {
713713
ScalarExpression::Constant(dv) => match &dv {
714-
DataValue::Int32(Some(v)) if *v > 0 => offset = Some(*v as usize),
715-
DataValue::Int64(Some(v)) if *v > 0 => offset = Some(*v as usize),
714+
DataValue::Int32(v) if *v > 0 => offset = Some(*v as usize),
715+
DataValue::Int64(v) if *v > 0 => offset = Some(*v as usize),
716716
_ => return Err(DatabaseError::InvalidType),
717717
},
718718
_ => {

src/db.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub(crate) mod test {
530530
iter.next().unwrap()?,
531531
Tuple::new(
532532
None,
533-
vec![DataValue::Date32(Some(Local::now().num_days_from_ce()))]
533+
vec![DataValue::Date32(Local::now().num_days_from_ce())]
534534
)
535535
);
536536
assert!(iter.next().is_none());
@@ -558,11 +558,11 @@ pub(crate) mod test {
558558
assert_eq!(iter.schema(), &Arc::new(vec![ColumnRef::from(column)]));
559559
assert_eq!(
560560
iter.next().unwrap()?,
561-
Tuple::new(None, vec![DataValue::Int32(Some(3))])
561+
Tuple::new(None, vec![DataValue::Int32(3)])
562562
);
563563
assert_eq!(
564564
iter.next().unwrap()?,
565-
Tuple::new(None, vec![DataValue::Int32(Some(4))])
565+
Tuple::new(None, vec![DataValue::Int32(4)])
566566
);
567567
Ok(())
568568
}
@@ -583,7 +583,7 @@ pub(crate) mod test {
583583
{
584584
let statement = fnck_sql.prepare("explain select * from t1 where b > ?1")?;
585585

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

588588
assert_eq!(
589589
iter.next().unwrap()?.values[0].utf8().unwrap(),
@@ -601,10 +601,10 @@ pub(crate) mod test {
601601
let mut iter = fnck_sql.execute(
602602
&statement,
603603
&[
604-
("?1", DataValue::Int32(Some(0))),
605-
("?2", DataValue::Int32(Some(0))),
606-
("?3", DataValue::Int32(Some(1))),
607-
("?4", DataValue::Int32(Some(0))),
604+
("?1", DataValue::Int32(0)),
605+
("?2", DataValue::Int32(0)),
606+
("?3", DataValue::Int32(1)),
607+
("?4", DataValue::Int32(0)),
608608
],
609609
)?;
610610
assert_eq!(
@@ -621,10 +621,10 @@ pub(crate) mod test {
621621
let mut iter = fnck_sql.execute(
622622
&statement,
623623
&[
624-
("?1", DataValue::Int32(Some(9))),
625-
("?2", DataValue::Int32(Some(0))),
626-
("?3", DataValue::Int32(Some(1))),
627-
("?4", DataValue::Int32(Some(0))),
624+
("?1", DataValue::Int32(9)),
625+
("?2", DataValue::Int32(0)),
626+
("?3", DataValue::Int32(1)),
627+
("?4", DataValue::Int32(0)),
628628
],
629629
)?;
630630
assert_eq!(
@@ -666,20 +666,20 @@ pub(crate) mod test {
666666

667667
assert_eq!(
668668
iter_1.next().unwrap()?.values,
669-
vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))]
669+
vec![DataValue::Int32(0), DataValue::Int32(0)]
670670
);
671671
assert_eq!(
672672
iter_1.next().unwrap()?.values,
673-
vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(1))]
673+
vec![DataValue::Int32(1), DataValue::Int32(1)]
674674
);
675675

676676
assert_eq!(
677677
iter_2.next().unwrap()?.values,
678-
vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))]
678+
vec![DataValue::Int32(0), DataValue::Int32(0)]
679679
);
680680
assert_eq!(
681681
iter_2.next().unwrap()?.values,
682-
vec![DataValue::Int32(Some(3)), DataValue::Int32(Some(3))]
682+
vec![DataValue::Int32(3), DataValue::Int32(3)]
683683
);
684684
drop(iter_1);
685685
drop(iter_2);

src/execution/dml/analyze.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {
9292
if values.len() == 1 {
9393
throw!(builder.append(&values[0]));
9494
} else {
95-
throw!(
96-
builder.append(&Arc::new(DataValue::Tuple(Some((values, false)))))
97-
);
95+
throw!(builder.append(&Arc::new(DataValue::Tuple(values, false))));
9896
}
9997
}
10098
}
@@ -121,7 +119,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {
121119

122120
throw!(meta.to_file(&temp_path));
123121
values.push(DataValue::Utf8 {
124-
value: Some(path_str.clone()),
122+
value: path_str.clone(),
125123
ty: Utf8Type::Variable(None),
126124
unit: CharLengthUnits::Characters,
127125
});

src/execution/dml/insert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Insert {
118118
if value.is_none() {
119119
value = throw!(col.default_value());
120120
}
121-
value.unwrap_or_else(|| DataValue::none(col.datatype()))
121+
value.unwrap_or(DataValue::Null)
122122
};
123123
if value.is_null() && !col.nullable() {
124124
yield Err(DatabaseError::NotNull);

src/execution/dql/aggregate/avg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ impl Accumulator for AvgAccumulator {
3838
return Ok(DataValue::init(&value_ty));
3939
}
4040
let quantity = if value_ty.is_signed_numeric() {
41-
DataValue::Int64(Some(self.count as i64))
41+
DataValue::Int64(self.count as i64)
4242
} else {
43-
DataValue::UInt32(Some(self.count as u32))
43+
DataValue::UInt32(self.count as u32)
4444
};
4545
let quantity_ty = quantity.logical_type();
4646

src/execution/dql/aggregate/count.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Accumulator for CountAccumulator {
2424
}
2525

2626
fn evaluate(&self) -> Result<DataValue, DatabaseError> {
27-
Ok(DataValue::Int32(Some(self.result)))
27+
Ok(DataValue::Int32(self.result))
2828
}
2929
}
3030

@@ -50,6 +50,6 @@ impl Accumulator for DistinctCountAccumulator {
5050
}
5151

5252
fn evaluate(&self) -> Result<DataValue, DatabaseError> {
53-
Ok(DataValue::Int32(Some(self.distinct_values.len() as i32)))
53+
Ok(DataValue::Int32(self.distinct_values.len() as i32))
5454
}
5555
}

src/execution/dql/aggregate/hash_agg.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,24 @@ mod test {
159159
operator: Operator::Values(ValuesOperator {
160160
rows: vec![
161161
vec![
162-
DataValue::Int32(Some(0)),
163-
DataValue::Int32(Some(2)),
164-
DataValue::Int32(Some(4)),
162+
DataValue::Int32(0),
163+
DataValue::Int32(2),
164+
DataValue::Int32(4),
165165
],
166166
vec![
167-
DataValue::Int32(Some(1)),
168-
DataValue::Int32(Some(3)),
169-
DataValue::Int32(Some(5)),
167+
DataValue::Int32(1),
168+
DataValue::Int32(3),
169+
DataValue::Int32(5),
170170
],
171171
vec![
172-
DataValue::Int32(Some(0)),
173-
DataValue::Int32(Some(1)),
174-
DataValue::Int32(Some(2)),
172+
DataValue::Int32(0),
173+
DataValue::Int32(1),
174+
DataValue::Int32(2),
175175
],
176176
vec![
177-
DataValue::Int32(Some(1)),
178-
DataValue::Int32(Some(2)),
179-
DataValue::Int32(Some(3)),
177+
DataValue::Int32(1),
178+
DataValue::Int32(2),
179+
DataValue::Int32(3),
180180
],
181181
],
182182
schema_ref: t1_schema.clone(),

src/execution/dql/aggregate/min_max.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@ use crate::execution::dql::aggregate::Accumulator;
33
use crate::expression::BinaryOperator;
44
use crate::types::evaluator::EvaluatorFactory;
55
use crate::types::value::DataValue;
6-
use crate::types::LogicalType;
76

87
pub struct MinMaxAccumulator {
98
inner: Option<DataValue>,
109
op: BinaryOperator,
11-
ty: LogicalType,
1210
}
1311

1412
impl MinMaxAccumulator {
15-
pub fn new(ty: &LogicalType, is_max: bool) -> Self {
13+
pub fn new(is_max: bool) -> Self {
1614
let op = if is_max {
1715
BinaryOperator::Lt
1816
} else {
1917
BinaryOperator::Gt
2018
};
2119

22-
Self {
23-
inner: None,
24-
op,
25-
ty: ty.clone(),
26-
}
20+
Self { inner: None, op }
2721
}
2822
}
2923

@@ -32,12 +26,10 @@ impl Accumulator for MinMaxAccumulator {
3226
if !value.is_null() {
3327
if let Some(inner_value) = &self.inner {
3428
let evaluator = EvaluatorFactory::binary_create(value.logical_type(), self.op)?;
35-
if let DataValue::Boolean(Some(result)) =
36-
evaluator.0.binary_eval(inner_value, value)
37-
{
29+
if let DataValue::Boolean(result) = evaluator.0.binary_eval(inner_value, value) {
3830
result
3931
} else {
40-
unreachable!()
32+
return Err(DatabaseError::InvalidType);
4133
}
4234
} else {
4335
true
@@ -49,9 +41,6 @@ impl Accumulator for MinMaxAccumulator {
4941
}
5042

5143
fn evaluate(&self) -> Result<DataValue, DatabaseError> {
52-
Ok(self
53-
.inner
54-
.clone()
55-
.unwrap_or_else(|| DataValue::none(&self.ty)))
44+
Ok(self.inner.clone().unwrap_or(DataValue::Null))
5645
}
5746
}

src/execution/dql/aggregate/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ fn create_accumulator(expr: &ScalarExpression) -> Result<Box<dyn Accumulator>, D
3636
(AggKind::Count, true) => Box::new(DistinctCountAccumulator::new()),
3737
(AggKind::Sum, false) => Box::new(SumAccumulator::new(ty)?),
3838
(AggKind::Sum, true) => Box::new(DistinctSumAccumulator::new(ty)?),
39-
(AggKind::Min, _) => Box::new(MinMaxAccumulator::new(ty, false)),
40-
(AggKind::Max, _) => Box::new(MinMaxAccumulator::new(ty, true)),
39+
(AggKind::Min, _) => Box::new(MinMaxAccumulator::new(false)),
40+
(AggKind::Max, _) => Box::new(MinMaxAccumulator::new(true)),
4141
(AggKind::Avg, _) => Box::new(AvgAccumulator::new(ty)?),
4242
})
4343
} else {

src/execution/dql/aggregate/sum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl SumAccumulator {
1717
debug_assert!(ty.is_numeric());
1818

1919
Ok(Self {
20-
result: DataValue::none(ty),
20+
result: DataValue::Null,
2121
evaluator: EvaluatorFactory::binary_create(ty.clone(), BinaryOperator::Plus)?,
2222
})
2323
}
@@ -27,7 +27,7 @@ impl Accumulator for SumAccumulator {
2727
fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> {
2828
if !value.is_null() {
2929
if self.result.is_null() {
30-
self.result = DataValue::clone(value);
30+
self.result = value.clone();
3131
} else {
3232
self.result = self.evaluator.0.binary_eval(&self.result, value);
3333
}

0 commit comments

Comments
 (0)