Skip to content

Commit a7f55b8

Browse files
author
Evgeny Maruschenko
committed
Refactor ScalarValue::new_primitive to return Result
1 parent cb2d03c commit a7f55b8

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

datafusion/common/src/scalar.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,21 +744,21 @@ macro_rules! eq_array_primitive {
744744
}
745745

746746
impl ScalarValue {
747-
/// Create a [`ScalarValue`] with the provided value and datatype
747+
/// Create a [`Result<ScalarValue>`] with the provided value and datatype
748748
///
749749
/// # Panics
750750
///
751751
/// Panics if d is not compatible with T
752752
pub fn new_primitive<T: ArrowPrimitiveType>(
753753
a: Option<T::Native>,
754754
d: &DataType,
755-
) -> Self {
755+
) -> Result<Self> {
756756
match a {
757-
None => d.try_into().unwrap(),
757+
None => d.try_into(),
758758
Some(v) => {
759759
let array = PrimitiveArray::<T>::new(vec![v].into(), None)
760760
.with_data_type(d.clone());
761-
Self::try_from_array(&array, 0).unwrap()
761+
Self::try_from_array(&array, 0)
762762
}
763763
}
764764
}

datafusion/physical-expr/src/aggregate/bit_and_or_xor.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ where
195195
}
196196

197197
fn evaluate(&self) -> Result<ScalarValue> {
198-
Ok(ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE))
198+
ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE)
199199
}
200200

201201
fn size(&self) -> usize {
@@ -356,7 +356,7 @@ where
356356
}
357357

358358
fn evaluate(&self) -> Result<ScalarValue> {
359-
Ok(ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE))
359+
ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE)
360360
}
361361

362362
fn size(&self) -> usize {
@@ -517,7 +517,7 @@ where
517517
}
518518

519519
fn evaluate(&self) -> Result<ScalarValue> {
520-
Ok(ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE))
520+
ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE)
521521
}
522522

523523
fn size(&self) -> usize {
@@ -638,13 +638,13 @@ where
638638
// 1. Stores aggregate state in `ScalarValue::List`
639639
// 2. Constructs `ScalarValue::List` state from distinct numeric stored in hash set
640640
let state_out = {
641-
let values: Vec<ScalarValue> = self
641+
let values = self
642642
.values
643643
.iter()
644644
.map(|x| ScalarValue::new_primitive::<T>(Some(*x), &T::DATA_TYPE))
645-
.collect();
645+
.collect::<Result<Vec<_>>>();
646646

647-
let arr = ScalarValue::new_list(&values, &T::DATA_TYPE);
647+
let arr = ScalarValue::new_list(&values?, &T::DATA_TYPE);
648648
vec![ScalarValue::List(arr)]
649649
};
650650
Ok(state_out)
@@ -685,7 +685,7 @@ where
685685
acc = acc ^ *distinct_value;
686686
}
687687
let v = (!self.values.is_empty()).then_some(acc);
688-
Ok(ScalarValue::new_primitive::<T>(v, &T::DATA_TYPE))
688+
ScalarValue::new_primitive::<T>(v, &T::DATA_TYPE)
689689
}
690690

691691
fn size(&self) -> usize {

datafusion/physical-expr/src/aggregate/median.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ impl<T: ArrowNumericType> std::fmt::Debug for MedianAccumulator<T> {
146146

147147
impl<T: ArrowNumericType> Accumulator for MedianAccumulator<T> {
148148
fn state(&self) -> Result<Vec<ScalarValue>> {
149-
let all_values: Vec<ScalarValue> = self
149+
let all_values = self
150150
.all_values
151151
.iter()
152152
.map(|x| ScalarValue::new_primitive::<T>(Some(*x), &self.data_type))
153-
.collect();
153+
.collect::<Result<Vec<_>>>();
154154

155-
let arr = ScalarValue::new_list(&all_values, &self.data_type);
155+
let arr = ScalarValue::new_list(&all_values?, &self.data_type);
156156
Ok(vec![ScalarValue::List(arr)])
157157
}
158158

@@ -188,7 +188,7 @@ impl<T: ArrowNumericType> Accumulator for MedianAccumulator<T> {
188188
let (_, median, _) = d.select_nth_unstable_by(len / 2, cmp);
189189
Some(*median)
190190
};
191-
Ok(ScalarValue::new_primitive::<T>(median, &self.data_type))
191+
ScalarValue::new_primitive::<T>(median, &self.data_type)
192192
}
193193

194194
fn size(&self) -> usize {

datafusion/physical-expr/src/aggregate/sum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<T: ArrowNumericType> Accumulator for SumAccumulator<T> {
205205
}
206206

207207
fn evaluate(&self) -> Result<ScalarValue> {
208-
Ok(ScalarValue::new_primitive::<T>(self.sum, &self.data_type))
208+
ScalarValue::new_primitive::<T>(self.sum, &self.data_type)
209209
}
210210

211211
fn size(&self) -> usize {
@@ -265,7 +265,7 @@ impl<T: ArrowNumericType> Accumulator for SlidingSumAccumulator<T> {
265265

266266
fn evaluate(&self) -> Result<ScalarValue> {
267267
let v = (self.count != 0).then_some(self.sum);
268-
Ok(ScalarValue::new_primitive::<T>(v, &self.data_type))
268+
ScalarValue::new_primitive::<T>(v, &self.data_type)
269269
}
270270

271271
fn size(&self) -> usize {

datafusion/physical-expr/src/aggregate/sum_distinct.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,16 @@ impl<T: ArrowPrimitiveType> Accumulator for DistinctSumAccumulator<T> {
159159
// 1. Stores aggregate state in `ScalarValue::List`
160160
// 2. Constructs `ScalarValue::List` state from distinct numeric stored in hash set
161161
let state_out = {
162-
let mut distinct_values = Vec::new();
163-
self.values.iter().for_each(|distinct_value| {
164-
distinct_values.push(ScalarValue::new_primitive::<T>(
165-
Some(distinct_value.0),
166-
&self.data_type,
167-
))
168-
});
162+
let distinct_values = self
163+
.values
164+
.iter()
165+
.map(|value| {
166+
ScalarValue::new_primitive::<T>(Some(value.0), &self.data_type)
167+
})
168+
.collect::<Result<Vec<_>>>();
169+
169170
vec![ScalarValue::List(ScalarValue::new_list(
170-
&distinct_values,
171+
&distinct_values?,
171172
&self.data_type,
172173
))]
173174
};
@@ -206,7 +207,7 @@ impl<T: ArrowPrimitiveType> Accumulator for DistinctSumAccumulator<T> {
206207
acc = acc.add_wrapping(distinct_value.0)
207208
}
208209
let v = (!self.values.is_empty()).then_some(acc);
209-
Ok(ScalarValue::new_primitive::<T>(v, &self.data_type))
210+
ScalarValue::new_primitive::<T>(v, &self.data_type)
210211
}
211212

212213
fn size(&self) -> usize {

0 commit comments

Comments
 (0)