datafusion_physical_expr/expressions/
negative.rs1use std::any::Any;
21use std::hash::Hash;
22use std::sync::Arc;
23
24use crate::PhysicalExpr;
25
26use arrow::{
27 compute::kernels::numeric::neg_wrapping,
28 datatypes::{DataType, Schema},
29 record_batch::RecordBatch,
30};
31use datafusion_common::{internal_err, plan_err, Result};
32use datafusion_expr::interval_arithmetic::Interval;
33use datafusion_expr::sort_properties::ExprProperties;
34use datafusion_expr::statistics::Distribution::{
35 self, Bernoulli, Exponential, Gaussian, Generic, Uniform,
36};
37use datafusion_expr::{
38 type_coercion::{is_interval, is_null, is_signed_numeric, is_timestamp},
39 ColumnarValue,
40};
41
42#[derive(Debug, Eq)]
44pub struct NegativeExpr {
45 arg: Arc<dyn PhysicalExpr>,
47}
48
49impl PartialEq for NegativeExpr {
51 fn eq(&self, other: &Self) -> bool {
52 self.arg.eq(&other.arg)
53 }
54}
55
56impl Hash for NegativeExpr {
57 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
58 self.arg.hash(state);
59 }
60}
61
62impl NegativeExpr {
63 pub fn new(arg: Arc<dyn PhysicalExpr>) -> Self {
65 Self { arg }
66 }
67
68 pub fn arg(&self) -> &Arc<dyn PhysicalExpr> {
70 &self.arg
71 }
72}
73
74impl std::fmt::Display for NegativeExpr {
75 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
76 write!(f, "(- {})", self.arg)
77 }
78}
79
80impl PhysicalExpr for NegativeExpr {
81 fn as_any(&self) -> &dyn Any {
83 self
84 }
85
86 fn data_type(&self, input_schema: &Schema) -> Result<DataType> {
87 self.arg.data_type(input_schema)
88 }
89
90 fn nullable(&self, input_schema: &Schema) -> Result<bool> {
91 self.arg.nullable(input_schema)
92 }
93
94 fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
95 match self.arg.evaluate(batch)? {
96 ColumnarValue::Array(array) => {
97 let result = neg_wrapping(array.as_ref())?;
98 Ok(ColumnarValue::Array(result))
99 }
100 ColumnarValue::Scalar(scalar) => {
101 Ok(ColumnarValue::Scalar(scalar.arithmetic_negate()?))
102 }
103 }
104 }
105
106 fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
107 vec![&self.arg]
108 }
109
110 fn with_new_children(
111 self: Arc<Self>,
112 children: Vec<Arc<dyn PhysicalExpr>>,
113 ) -> Result<Arc<dyn PhysicalExpr>> {
114 Ok(Arc::new(NegativeExpr::new(Arc::clone(&children[0]))))
115 }
116
117 fn evaluate_bounds(&self, children: &[&Interval]) -> Result<Interval> {
121 children[0].arithmetic_negate()
122 }
123
124 fn propagate_constraints(
127 &self,
128 interval: &Interval,
129 children: &[&Interval],
130 ) -> Result<Option<Vec<Interval>>> {
131 let negated_interval = interval.arithmetic_negate()?;
132
133 Ok(children[0]
134 .intersect(negated_interval)?
135 .map(|result| vec![result]))
136 }
137
138 fn evaluate_statistics(&self, children: &[&Distribution]) -> Result<Distribution> {
139 match children[0] {
140 Uniform(u) => Distribution::new_uniform(u.range().arithmetic_negate()?),
141 Exponential(e) => Distribution::new_exponential(
142 e.rate().clone(),
143 e.offset().arithmetic_negate()?,
144 !e.positive_tail(),
145 ),
146 Gaussian(g) => Distribution::new_gaussian(
147 g.mean().arithmetic_negate()?,
148 g.variance().clone(),
149 ),
150 Bernoulli(_) => {
151 internal_err!("NegativeExpr cannot operate on Boolean datatypes")
152 }
153 Generic(u) => Distribution::new_generic(
154 u.mean().arithmetic_negate()?,
155 u.median().arithmetic_negate()?,
156 u.variance().clone(),
157 u.range().arithmetic_negate()?,
158 ),
159 }
160 }
161
162 fn get_properties(&self, children: &[ExprProperties]) -> Result<ExprProperties> {
164 Ok(ExprProperties {
165 sort_properties: -children[0].sort_properties,
166 range: children[0].range.clone().arithmetic_negate()?,
167 preserves_lex_ordering: false,
168 })
169 }
170}
171
172pub fn negative(
178 arg: Arc<dyn PhysicalExpr>,
179 input_schema: &Schema,
180) -> Result<Arc<dyn PhysicalExpr>> {
181 let data_type = arg.data_type(input_schema)?;
182 if is_null(&data_type) {
183 Ok(arg)
184 } else if !is_signed_numeric(&data_type)
185 && !is_interval(&data_type)
186 && !is_timestamp(&data_type)
187 {
188 plan_err!("Negation only supports numeric, interval and timestamp types")
189 } else {
190 Ok(Arc::new(NegativeExpr::new(arg)))
191 }
192}
193
194#[cfg(test)]
195mod tests {
196 use super::*;
197 use crate::expressions::{col, Column};
198
199 use arrow::array::*;
200 use arrow::datatypes::DataType::{Float32, Float64, Int16, Int32, Int64, Int8};
201 use arrow::datatypes::*;
202 use datafusion_common::cast::as_primitive_array;
203 use datafusion_common::{DataFusionError, ScalarValue};
204
205 use paste::paste;
206
207 macro_rules! test_array_negative_op {
208 ($DATA_TY:tt, $($VALUE:expr),* ) => {
209 let schema = Schema::new(vec![Field::new("a", DataType::$DATA_TY, true)]);
210 let expr = negative(col("a", &schema)?, &schema)?;
211 assert_eq!(expr.data_type(&schema)?, DataType::$DATA_TY);
212 assert!(expr.nullable(&schema)?);
213 let mut arr = Vec::new();
214 let mut arr_expected = Vec::new();
215 $(
216 arr.push(Some($VALUE));
217 arr_expected.push(Some(-$VALUE));
218 )+
219 arr.push(None);
220 arr_expected.push(None);
221 let input = paste!{[<$DATA_TY Array>]::from(arr)};
222 let expected = &paste!{[<$DATA_TY Array>]::from(arr_expected)};
223 let batch =
224 RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(input)])?;
225 let result = expr.evaluate(&batch)?.into_array(batch.num_rows()).expect("Failed to convert to array");
226 let result =
227 as_primitive_array(&result).expect(format!("failed to downcast to {:?}Array", $DATA_TY).as_str());
228 assert_eq!(result, expected);
229 };
230 }
231
232 #[test]
233 fn array_negative_op() -> Result<()> {
234 test_array_negative_op!(Int8, 2i8, 1i8);
235 test_array_negative_op!(Int16, 234i16, 123i16);
236 test_array_negative_op!(Int32, 2345i32, 1234i32);
237 test_array_negative_op!(Int64, 23456i64, 12345i64);
238 test_array_negative_op!(Float32, 2345.0f32, 1234.0f32);
239 test_array_negative_op!(Float64, 23456.0f64, 12345.0f64);
240 Ok(())
241 }
242
243 #[test]
244 fn test_evaluate_bounds() -> Result<()> {
245 let negative_expr = NegativeExpr::new(Arc::new(Column::new("a", 0)));
246 let child_interval = Interval::make(Some(-2), Some(1))?;
247 let negative_expr_interval = Interval::make(Some(-1), Some(2))?;
248 assert_eq!(
249 negative_expr.evaluate_bounds(&[&child_interval])?,
250 negative_expr_interval
251 );
252 Ok(())
253 }
254
255 #[test]
256 fn test_evaluate_statistics() -> Result<()> {
257 let negative_expr = NegativeExpr::new(Arc::new(Column::new("a", 0)));
258
259 assert_eq!(
261 negative_expr.evaluate_statistics(&[&Distribution::new_uniform(
262 Interval::make(Some(-2.), Some(3.))?
263 )?])?,
264 Distribution::new_uniform(Interval::make(Some(-3.), Some(2.))?)?
265 );
266
267 assert!(negative_expr
269 .evaluate_statistics(&[&Distribution::new_bernoulli(ScalarValue::from(
270 0.75
271 ))?])
272 .is_err());
273
274 assert_eq!(
276 negative_expr.evaluate_statistics(&[&Distribution::new_exponential(
277 ScalarValue::from(1.),
278 ScalarValue::from(1.),
279 true
280 )?])?,
281 Distribution::new_exponential(
282 ScalarValue::from(1.),
283 ScalarValue::from(-1.),
284 false
285 )?
286 );
287
288 assert_eq!(
290 negative_expr.evaluate_statistics(&[&Distribution::new_gaussian(
291 ScalarValue::from(15),
292 ScalarValue::from(225),
293 )?])?,
294 Distribution::new_gaussian(ScalarValue::from(-15), ScalarValue::from(225),)?
295 );
296
297 assert_eq!(
299 negative_expr.evaluate_statistics(&[&Distribution::new_generic(
300 ScalarValue::from(15),
301 ScalarValue::from(15),
302 ScalarValue::from(10),
303 Interval::make(Some(10), Some(20))?
304 )?])?,
305 Distribution::new_generic(
306 ScalarValue::from(-15),
307 ScalarValue::from(-15),
308 ScalarValue::from(10),
309 Interval::make(Some(-20), Some(-10))?
310 )?
311 );
312
313 Ok(())
314 }
315
316 #[test]
317 fn test_propagate_constraints() -> Result<()> {
318 let negative_expr = NegativeExpr::new(Arc::new(Column::new("a", 0)));
319 let original_child_interval = Interval::make(Some(-2), Some(3))?;
320 let negative_expr_interval = Interval::make(Some(0), Some(4))?;
321 let after_propagation = Some(vec![Interval::make(Some(-2), Some(0))?]);
322 assert_eq!(
323 negative_expr.propagate_constraints(
324 &negative_expr_interval,
325 &[&original_child_interval]
326 )?,
327 after_propagation
328 );
329 Ok(())
330 }
331
332 #[test]
333 fn test_propagate_statistics_range_holders() -> Result<()> {
334 let negative_expr = NegativeExpr::new(Arc::new(Column::new("a", 0)));
335 let original_child_interval = Interval::make(Some(-2), Some(3))?;
336 let after_propagation = Interval::make(Some(-2), Some(0))?;
337
338 let parent = Distribution::new_uniform(Interval::make(Some(0), Some(4))?)?;
339 let children: Vec<Vec<Distribution>> = vec![
340 vec![Distribution::new_uniform(original_child_interval.clone())?],
341 vec![Distribution::new_generic(
342 ScalarValue::from(0),
343 ScalarValue::from(0),
344 ScalarValue::Int32(None),
345 original_child_interval.clone(),
346 )?],
347 ];
348
349 for child_view in children {
350 let child_refs: Vec<_> = child_view.iter().collect();
351 let actual = negative_expr.propagate_statistics(&parent, &child_refs)?;
352 let expected = Some(vec![Distribution::new_from_interval(
353 after_propagation.clone(),
354 )?]);
355 assert_eq!(actual, expected);
356 }
357
358 Ok(())
359 }
360
361 #[test]
362 fn test_negation_valid_types() -> Result<()> {
363 let negatable_types = [
364 Int8,
365 DataType::Timestamp(TimeUnit::Second, None),
366 DataType::Interval(IntervalUnit::YearMonth),
367 ];
368 for negatable_type in negatable_types {
369 let schema = Schema::new(vec![Field::new("a", negatable_type, true)]);
370 let _expr = negative(col("a", &schema)?, &schema)?;
371 }
372 Ok(())
373 }
374
375 #[test]
376 fn test_negation_invalid_types() -> Result<()> {
377 let schema = Schema::new(vec![Field::new("a", DataType::Utf8, true)]);
378 let expr = negative(col("a", &schema)?, &schema).unwrap_err();
379 matches!(expr, DataFusionError::Plan(_));
380 Ok(())
381 }
382}