realno commented on a change in pull request #1525:
URL: https://github.com/apache/arrow-datafusion/pull/1525#discussion_r780630182
##########
File path: datafusion/src/scalar.rs
##########
@@ -526,6 +526,282 @@ macro_rules! eq_array_primitive {
}
impl ScalarValue {
+ /// Return true if the value is numeric
+ pub fn is_numeric(&self) -> bool {
+ matches!(self,
+ ScalarValue::Float32(_)
+ | ScalarValue::Float64(_)
+ | ScalarValue::Decimal128(_, _, _)
+ | ScalarValue::Int8(_)
+ | ScalarValue::Int16(_)
+ | ScalarValue::Int32(_)
+ | ScalarValue::Int64(_)
+ | ScalarValue::UInt8(_)
+ | ScalarValue::UInt16(_)
+ | ScalarValue::UInt32(_)
+ | ScalarValue::UInt64(_)
+ )
+ }
+
+ /// Add two numeric ScalarValues
+ pub fn add(lhs: &ScalarValue, rhs: &ScalarValue) -> Result<ScalarValue> {
+ if !lhs.is_numeric() || !rhs.is_numeric() {
+ return Err(DataFusionError::Internal(format!(
+ "Addition only supports numeric types, \
+ here has {:?} and {:?}",
+ lhs.get_datatype(),
+ rhs.get_datatype()
+ )));
+ }
+
+ // TODO: Finding a good way to support operation between different
types without
+ // writing a hige match block.
+ // TODO: Add support for decimal types
+ match (lhs, rhs) {
+ (ScalarValue::Decimal128(_, _, _), _) |
+ (_, ScalarValue::Decimal128(_, _, _)) => {
+ Err(DataFusionError::Internal(
+ "Addition with Decimals are not supported for
now".to_string()
+ ))
+ },
+ // f64 / _
+ (ScalarValue::Float64(f1), ScalarValue::Float64(f2)) => {
+ Ok(ScalarValue::Float64(Some(f1.unwrap() + f2.unwrap())))
Review comment:
Added checkes for `None` values.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]