Skip to content

Commit 6de516d

Browse files
author
Evgeny Maruschenko
committed
Refactor ScalarValue::new_primitive to return Result
1 parent fa2bb6c commit 6de516d

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

datafusion/common/src/scalar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,13 @@ impl ScalarValue {
744744
pub fn new_primitive<T: ArrowPrimitiveType>(
745745
a: Option<T::Native>,
746746
d: &DataType,
747-
) -> Self {
747+
) -> Result<Self> {
748748
match a {
749-
None => d.try_into().unwrap(),
749+
None => d.try_into(),
750750
Some(v) => {
751751
let array = PrimitiveArray::<T>::new(vec![v].into(), None)
752752
.with_data_type(d.clone());
753-
Self::try_from_array(&array, 0).unwrap()
753+
Self::try_from_array(&array, 0)
754754
}
755755
}
756756
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ where
194194
}
195195

196196
fn evaluate(&self) -> Result<ScalarValue> {
197-
Ok(ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE))
197+
ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE)
198198
}
199199

200200
fn size(&self) -> usize {
@@ -355,7 +355,7 @@ where
355355
}
356356

357357
fn evaluate(&self) -> Result<ScalarValue> {
358-
Ok(ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE))
358+
ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE)
359359
}
360360

361361
fn size(&self) -> usize {
@@ -516,7 +516,7 @@ where
516516
}
517517

518518
fn evaluate(&self) -> Result<ScalarValue> {
519-
Ok(ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE))
519+
ScalarValue::new_primitive::<T>(self.value, &T::DATA_TYPE)
520520
}
521521

522522
fn size(&self) -> usize {
@@ -641,9 +641,9 @@ where
641641
.values
642642
.iter()
643643
.map(|x| ScalarValue::new_primitive::<T>(Some(*x), &T::DATA_TYPE))
644-
.collect();
644+
.collect::<Result<Vec<_>>>();
645645

646-
vec![ScalarValue::new_list(Some(values), T::DATA_TYPE)]
646+
vec![ScalarValue::new_list(Some(values?), T::DATA_TYPE)]
647647
};
648648
Ok(state_out)
649649
}
@@ -684,7 +684,7 @@ where
684684
acc = acc ^ *distinct_value;
685685
}
686686
let v = (!self.values.is_empty()).then_some(acc);
687-
Ok(ScalarValue::new_primitive::<T>(v, &T::DATA_TYPE))
687+
ScalarValue::new_primitive::<T>(v, &T::DATA_TYPE)
688688
}
689689

690690
fn size(&self) -> usize {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ impl<T: ArrowNumericType> Accumulator for MedianAccumulator<T> {
150150
.all_values
151151
.iter()
152152
.map(|x| ScalarValue::new_primitive::<T>(Some(*x), &self.data_type))
153-
.collect();
154-
let state = ScalarValue::new_list(Some(all_values), self.data_type.clone());
153+
.collect::<Result<Vec<_>>>();
154+
let state = ScalarValue::new_list(Some(all_values?), self.data_type.clone());
155155

156156
Ok(vec![state])
157157
}
@@ -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::new_list(
170-
Some(distinct_values),
171+
Some(distinct_values?),
171172
self.data_type.clone(),
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)