Apache DataFusion SQL Query Engine
Custom window frame logic (support `ROWS`, `RANGE`, `PRECEDING` and `FOLLOWING` for window functions) (#3570)
Metehan Yıldırım65a5c6bcdd27
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.ymlindex da25cdfc50..3ec1bd2caf 100644--- a/.github/workflows/rust.yml+++ b/.github/workflows/rust.yml@@ -119,6 +119,7 @@ jobs:env:POSTGRES_PASSWORD: postgresPOSTGRES_DB: db_test+ POSTGRES_INITDB_ARGS: --encoding=UTF-8 --lc-collate=C --lc-ctype=Cports:- 5432/tcpoptions: >-diff --git a/datafusion/common/src/bisect.rs b/datafusion/common/src/bisect.rsnew file mode 100644index 0000000000..67fb003d09--- /dev/null+++ b/datafusion/common/src/bisect.rs@@ -0,0 +1,243 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.++//! This module provides the bisect function, which implements binary search.++use crate::{DataFusionError, Result, ScalarValue};+use arrow::array::ArrayRef;+use arrow::compute::SortOptions;+use std::cmp::Ordering;++/// This function compares two tuples depending on the given sort options.+fn compare(+ x: &[ScalarValue],+ y: &[ScalarValue],+ sort_options: &[SortOptions],+) -> Result<Ordering> {+ let zip_it = x.iter().zip(y.iter()).zip(sort_options.iter());+ // Preserving lexical ordering.+ for ((lhs, rhs), sort_options) in zip_it {+ // Consider all combinations of NULLS FIRST/LAST and ASC/DESC configurations.+ let result = match (lhs.is_null(), rhs.is_null(), sort_options.nulls_first) {+ (true, false, false) | (false, true, true) => Ordering::Greater,+ (true, false, true) | (false, true, false) => Ordering::Less,+ (false, false, _) => if sort_options.descending {+ rhs.partial_cmp(lhs)+ } else {+ lhs.partial_cmp(rhs)+ }+ .ok_or_else(|| {+ DataFusionError::Internal("Column array shouldn't be empty".to_string())+ })?,+ (true, true, _) => continue,+ };+ if result != Ordering::Equal {+ return Ok(result);+ }+ }+ Ok(Ordering::Equal)+}++/// This function implements both bisect_left and bisect_right, having the same+/// semantics with the Python Standard Library. To use bisect_left, supply true+/// as the template argument. To use bisect_right, supply false as the template argument.+pub fn bisect<const SIDE: bool>(+ item_columns: &[ArrayRef],+ target: &[ScalarValue],+ sort_options: &[SortOptions],+) -> Result<usize> {+ let mut low: usize = 0;+ let mut high: usize = item_columns+ .get(0)+ .ok_or_else(|| {+ DataFusionError::Internal("Column array shouldn't be empty".to_string())+ })?+ .len();+ while low < high {+ let mid = ((high - low) / 2) + low;+ let val = item_columns+ .iter()+ .map(|arr| ScalarValue::try_from_array(arr, mid))+ .collect::<Result<Vec<ScalarValue>>>()?;+ let cmp = compare(&val, target, sort_options)?;+ let flag = if SIDE { cmp.is_lt() } else { cmp.is_le() };+ if flag {+ low = mid + 1;+ } else {+ high = mid;+ }+ }+ Ok(low)+}++#[cfg(test)]+mod tests {+ use arrow::array::Float64Array;+ use std::sync::Arc;++ use crate::from_slice::FromSlice;+ use crate::ScalarValue;+ use crate::ScalarValue::Null;++ use super::*;++ #[test]+ fn test_bisect_left_and_right() {+ let arrays: Vec<ArrayRef> = vec![+ Arc::new(Float64Array::from_slice(&[5.0, 7.0, 8.0, 9., 10.])),+ Arc::new(Float64Array::from_slice(&[2.0, 3.0, 3.0, 4.0, 5.0])),+ Arc::new(Float64Array::from_slice(&[5.0, 7.0, 8.0, 10., 11.0])),+ Arc::new(Float64Array::from_slice(&[15.0, 13.0, 8.0, 5., 0.0])),+ ];+ let search_tuple: Vec<ScalarValue> = vec![+ ScalarValue::Float64(Some(8.0)),+ ScalarValue::Float64(Some(3.0)),+ ScalarValue::Float64(Some(8.0)),+ ScalarValue::Float64(Some(8.0)),+ ];+ let ords = [+ SortOptions {+ descending: false,+ nulls_first: true,+ },+ SortOptions {+ descending: false,+ nulls_first: true,+ },+ SortOptions {+ descending: false,+ nulls_first: true,+ },+ SortOptions {+ descending: true,+ nulls_first: true,+ },+ ];+ let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 2);+ let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 3);+ }++ #[test]+ fn vector_ord() {+ assert!(vec![1, 0, 0, 0, 0, 0, 0, 1] < vec![1, 0, 0, 0, 0, 0, 0, 2]);+ assert!(vec![1, 0, 0, 0, 0, 0, 1, 1] > vec![1, 0, 0, 0, 0, 0, 0, 2]);+ assert!(+ vec![+ ScalarValue::Int32(Some(2)),+ Null,+ ScalarValue::Int32(Some(0)),+ ] < vec![+ ScalarValue::Int32(Some(2)),+ Null,+ ScalarValue::Int32(Some(1)),+ ]+ );+ assert!(+ vec![+ ScalarValue::Int32(Some(2)),+ ScalarValue::Int32(None),+ ScalarValue::Int32(Some(0)),+ ] < vec![+ ScalarValue::Int32(Some(2)),+ ScalarValue::Int32(None),+ ScalarValue::Int32(Some(1)),+ ]+ );+ }++ #[test]+ fn ord_same_type() {+ assert!((ScalarValue::Int32(Some(2)) < ScalarValue::Int32(Some(3))));+ }++ #[test]+ fn test_bisect_left_and_right_diff_sort() {+ // Descending, left+ let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[+ 4.0, 3.0, 2.0, 1.0, 0.0,+ ]))];+ let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(4.0))];+ let ords = [SortOptions {+ descending: true,+ nulls_first: true,+ }];+ let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 0);++ // Descending, right+ let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[+ 4.0, 3.0, 2.0, 1.0, 0.0,+ ]))];+ let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(4.0))];+ let ords = [SortOptions {+ descending: true,+ nulls_first: true,+ }];+ let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 1);++ // Ascending, left+ let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[+ 5.0, 7.0, 8.0, 9., 10.,+ ]))];+ let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(7.0))];+ let ords = [SortOptions {+ descending: false,+ nulls_first: true,+ }];+ let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 1);++ // Ascending, right+ let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[+ 5.0, 7.0, 8.0, 9., 10.,+ ]))];+ let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(7.0))];+ let ords = [SortOptions {+ descending: false,+ nulls_first: true,+ }];+ let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 2);++ let arrays: Vec<ArrayRef> = vec![+ Arc::new(Float64Array::from_slice(&[5.0, 7.0, 8.0, 8.0, 9., 10.])),+ Arc::new(Float64Array::from_slice(&[10.0, 9.0, 8.0, 7.5, 7., 6.])),+ ];+ let search_tuple: Vec<ScalarValue> = vec![+ ScalarValue::Float64(Some(8.0)),+ ScalarValue::Float64(Some(8.0)),+ ];+ let ords = [+ SortOptions {+ descending: false,+ nulls_first: true,+ },+ SortOptions {+ descending: true,+ nulls_first: true,+ },+ ];+ let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 3);++ let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();+ assert_eq!(res, 2);+ }+}diff --git a/datafusion/common/src/lib.rs b/datafusion/common/src/lib.rsindex 48fd792cf9..864172d96a 100644--- a/datafusion/common/src/lib.rs+++ b/datafusion/common/src/lib.rs@@ -15,6 +15,7 @@// specific language governing permissions and limitations// under the License.+pub mod bisect;mod column;mod dfschema;mod error;diff --git a/datafusion/common/src/scalar.rs b/datafusion/common/src/scalar.rsindex 6c60bc373f..2f8f125585 100644--- a/datafusion/common/src/scalar.rs+++ b/datafusion/common/src/scalar.rs@@ -17,7 +17,12 @@//! This module provides ScalarValue, an enum that can be used for storage of single elements-use crate::error::{DataFusionError, Result};+use std::borrow::Borrow;+use std::cmp::{max, Ordering};+use std::convert::{Infallible, TryInto};+use std::str::FromStr;+use std::{convert::TryFrom, fmt, iter::repeat, sync::Arc};+use arrow::{array::*,compute::kernels::cast::{cast, cast_with_options, CastOptions},@@ -32,13 +37,11 @@ use arrow::{util::decimal::Decimal128,};use ordered_float::OrderedFloat;-use std::cmp::Ordering;-use std::convert::{Infallible, TryInto};-use std::str::FromStr;-use std::{convert::TryFrom, fmt, iter::repeat, sync::Arc};++use crate::error::{DataFusionError, Result};/// Represents a dynamically typed, nullable single value.-/// This is the single-valued counter-part of arrow’s `Array`.+/// This is the single-valued counter-part of arrow's `Array`./// https://arrow.apache.org/docs/python/api/datatypes.html/// https://github.com/apache/arrow/blob/master/format/Schema.fbs#L354-L375#[derive(Clone)]@@ -306,6 +309,181 @@ impl PartialOrd for ScalarValue {impl Eq for ScalarValue {}+// TODO implement this in arrow-rs with simd+// https://github.com/apache/arrow-rs/issues/1010+macro_rules! decimal_op {+ ($LHS:expr, $RHS:expr, $PRECISION:expr, $LHS_SCALE:expr, $RHS_SCALE:expr, $OPERATION:tt ) => {{+ let (difference, side) = if $LHS_SCALE > $RHS_SCALE {+ ($LHS_SCALE - $RHS_SCALE, true)+ } else {+ ($RHS_SCALE - $LHS_SCALE, false)+ };+ let scale = max($LHS_SCALE, $RHS_SCALE);+ match ($LHS, $RHS, difference) {+ (None, None, _) => ScalarValue::Decimal128(None, $PRECISION, scale),+ (None, Some(rhs_value), 0) => ScalarValue::Decimal128(Some((0 as i128) $OPERATION rhs_value), $PRECISION, scale),+ (None, Some(rhs_value), _) => {+ let mut new_value = ((0 as i128) $OPERATION rhs_value);+ if side {+ new_value *= 10_i128.pow((difference) as u32)+ };+ ScalarValue::Decimal128(Some(new_value), $PRECISION, scale)+ }+ (Some(lhs_value), None, 0) => ScalarValue::Decimal128(Some(lhs_value $OPERATION (0 as i128)), $PRECISION, scale),+ (Some(lhs_value), None, _) => {+ let mut new_value = (lhs_value $OPERATION (0 as i128));+ if !!!side {+ new_value *= 10_i128.pow((difference) as u32)+ }+ ScalarValue::Decimal128(Some(new_value), $PRECISION, scale)+ }+ (Some(lhs_value), Some(rhs_value), 0) => {+ ScalarValue::Decimal128(Some(lhs_value $OPERATION rhs_value), $PRECISION, scale)+ }+ (Some(lhs_value), Some(rhs_value), _) => {+ let new_value = if side {+ rhs_value * 10_i128.pow((difference) as u32) $OPERATION lhs_value+ } else {+ lhs_value * 10_i128.pow((difference) as u32) $OPERATION rhs_value+ };+ ScalarValue::Decimal128(Some(new_value), $PRECISION, scale)+ }+ }}++ }+}++// Returns the result of applying operation to two scalar values, including coercion into $TYPE.+macro_rules! typed_op {+ ($LEFT:expr, $RIGHT:expr, $SCALAR:ident, $TYPE:ident, $OPERATION:tt) => {+ Some(ScalarValue::$SCALAR(match ($LEFT, $RIGHT) {+ (None, None) => None,+ (Some(a), None) => Some((*a as $TYPE) $OPERATION (0 as $TYPE)),+ (None, Some(b)) => Some((0 as $TYPE) $OPERATION (*b as $TYPE)),+ (Some(a), Some(b)) => Some((*a as $TYPE) $OPERATION (*b as $TYPE)),+ }))+ };+}++macro_rules! impl_common_symmetric_cases_op {+ ($LHS:expr, $RHS:expr, $OPERATION:tt, [$([$L_TYPE:ident, $R_TYPE:ident, $O_TYPE:ident, $O_PRIM:ident]),+]) => {+ match ($LHS, $RHS) {+ $(+ (ScalarValue::$L_TYPE(lhs), ScalarValue::$R_TYPE(rhs)) => {+ typed_op!(lhs, rhs, $O_TYPE, $O_PRIM, $OPERATION)+ }+ (ScalarValue::$R_TYPE(lhs), ScalarValue::$L_TYPE(rhs)) => {+ typed_op!(lhs, rhs, $O_TYPE, $O_PRIM, $OPERATION)+ }+ )++ _ => None+ }+ }+}++macro_rules! impl_common_cases_op {+ ($LHS:expr, $RHS:expr, $OPERATION:tt) => {+ match ($LHS, $RHS) {+ (+ ScalarValue::Decimal128(v1, p1, s1),+ ScalarValue::Decimal128(v2, p2, s2),+ ) => {+ let max_precision = *p1.max(p2);+ Some(decimal_op!(v1, v2, max_precision, *s1, *s2, $OPERATION))+ }+ (ScalarValue::Float64(lhs), ScalarValue::Float64(rhs)) => {+ typed_op!(lhs, rhs, Float64, f64, $OPERATION)+ }+ (ScalarValue::Float32(lhs), ScalarValue::Float32(rhs)) => {+ typed_op!(lhs, rhs, Float32, f32, $OPERATION)+ }+ (ScalarValue::UInt64(lhs), ScalarValue::UInt64(rhs)) => {+ typed_op!(lhs, rhs, UInt64, u64, $OPERATION)+ }+ (ScalarValue::Int64(lhs), ScalarValue::Int64(rhs)) => {+ typed_op!(lhs, rhs, Int64, i64, $OPERATION)+ }+ (ScalarValue::UInt32(lhs), ScalarValue::UInt32(rhs)) => {+ typed_op!(lhs, rhs, UInt32, u32, $OPERATION)+ }+ (ScalarValue::Int32(lhs), ScalarValue::Int32(rhs)) => {+ typed_op!(lhs, rhs, Int32, i32, $OPERATION)+ }+ (ScalarValue::UInt16(lhs), ScalarValue::UInt16(rhs)) => {+ typed_op!(lhs, rhs, UInt16, u16, $OPERATION)+ }+ (ScalarValue::Int16(lhs), ScalarValue::Int16(rhs)) => {+ typed_op!(lhs, rhs, Int16, i16, $OPERATION)+ }+ (ScalarValue::UInt8(lhs), ScalarValue::UInt8(rhs)) => {+ typed_op!(lhs, rhs, UInt8, u8, $OPERATION)+ }+ (ScalarValue::Int8(lhs), ScalarValue::Int8(rhs)) => {+ typed_op!(lhs, rhs, Int8, i8, $OPERATION)+ }+ _ => impl_common_symmetric_cases_op!(+ $LHS,+ $RHS,+ $OPERATION,+ [+ // Float64 coerces everything to f64:+ [Float64, Float32, Float64, f64],+ [Float64, Int64, Float64, f64],+ [Float64, Int32, Float64, f64],+ [Float64, Int16, Float64, f64],+ [Float64, Int8, Float64, f64],+ [Float64, UInt64, Float64, f64],+ [Float64, UInt32, Float64, f64],+ [Float64, UInt16, Float64, f64],+ [Float64, UInt8, Float64, f64],+ // UInt64 coerces all smaller unsigned types to u64:+ [UInt64, UInt32, UInt64, u64],+ [UInt64, UInt16, UInt64, u64],+ [UInt64, UInt8, UInt64, u64],+ // Int64 coerces all smaller integral types to i64:+ [Int64, Int32, Int64, i64],+ [Int64, Int16, Int64, i64],+ [Int64, Int8, Int64, i64],+ [Int64, UInt32, Int64, i64],+ [Int64, UInt16, Int64, i64],+ [Int64, UInt8, Int64, i64]+ ]+ ),+ }+ };+}++/// If we want a special implementation for an ooperation this is the place to implement it+/// For instance, in the future we may want to implement subtraction for dates but not summation+/// so we can implement special case in the corresponding place+macro_rules! impl_distinct_cases_op {+ ($LHS:expr, $RHS:expr, +) => {+ match ($LHS, $RHS) {+ e => Err(DataFusionError::Internal(format!(+ "Summation is not implemented for {:?}",+ e+ ))),+ }+ };+ ($LHS:expr, $RHS:expr, -) => {+ match ($LHS, $RHS) {+ e => Err(DataFusionError::Internal(format!(+ "Subtraction is not implemented for {:?}",+ e+ ))),+ }+ };+}++macro_rules! impl_op {+ ($LHS:expr, $RHS:expr, $OPERATION:tt) => {+ match impl_common_cases_op!($LHS, $RHS, $OPERATION) {+ Some(elem) => Ok(elem),+ None => impl_distinct_cases_op!($LHS, $RHS, $OPERATION),+ }+ };+}+// manual implementation of `Hash` that uses OrderedFloat to// get defined behavior for floating pointimpl std::hash::Hash for ScalarValue {@@ -744,6 +922,24 @@ impl ScalarValue {}}+ pub fn add<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue> {+ let rhs = other.borrow();+ impl_op!(self, rhs, +)+ }++ pub fn sub<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue> {+ let rhs = other.borrow();+ impl_op!(self, rhs, -)+ }++ pub fn is_unsigned(&self) -> bool {+ let value_type = self.get_datatype();+ value_type == DataType::UInt64+ || value_type == DataType::UInt32+ || value_type == DataType::UInt16+ || value_type == DataType::UInt8+ }+/// whether this value is null or not.pub fn is_null(&self) -> bool {match self {@@ -970,7 +1166,7 @@ impl ScalarValue {DataType::Decimal256(_, _) => {return Err(DataFusionError::Internal("Decimal256 is not supported for ScalarValue".to_string(),- ))+ ));}DataType::Null => ScalarValue::iter_to_null_array(scalars),DataType::Boolean => build_array_primitive!(BooleanArray, Boolean),@@ -1104,16 +1300,16 @@ impl ScalarValue {ScalarValue::Dictionary(inner_key_type, scalar) => {if &inner_key_type == key_type {Ok(*scalar)- } else{+ } else {panic!("Expected inner key type of {} but found: {}, value was ({:?})", key_type, inner_key_type, scalar);}- },+ }_ => {Err(DataFusionError::Internal(format!("Expected scalar of type {} but found: {} {:?}",value_type, scalar, scalar)))- },+ }}).collect::<Result<Vec<_>>>()?;@@ -1435,7 +1631,7 @@ impl ScalarValue {.iter().map(|field| {let none_field = Self::try_from(field.data_type())- .expect("Failed to construct null ScalarValue from Struct field type");+ .expect("Failed to construct null ScalarValue from Struct field type");(field.clone(), none_field.to_array_of_size(size))}).collect();@@ -1979,6 +2175,36 @@ impl TryFrom<&DataType> for ScalarValue {}}+pub trait TryFromValue<T> {+ fn try_from_value(datatype: &DataType, value: T) -> Result<ScalarValue>;+}++macro_rules! impl_try_from_value {+ ($NATIVE:ty, [$([$SCALAR:ident, $PRIMITIVE:tt]),+]) => {+ impl TryFromValue<$NATIVE> for ScalarValue {+ fn try_from_value(datatype: &DataType, value: $NATIVE) -> Result<ScalarValue> {+ Ok(match datatype {+ $(DataType::$SCALAR => ScalarValue::$SCALAR(Some(value as $PRIMITIVE)),)++ _ => {+ return Err(DataFusionError::NotImplemented(format!(+ "Can't create a scalar from data_type \"{:?}\"",+ datatype+ )));+ }+ })+ }+ }+ };+}++macro_rules! impl_try_from_value_all {+ ([$($NATIVE:ty),+]) => {+ $(impl_try_from_value!($NATIVE, [[Float64, f64], [Float32, f32], [UInt64, u64], [UInt32, u32], [UInt16, u16], [UInt8, u8], [Int64, i64], [Int32, i32], [Int16, i16], [Int8, i8]]);)++ }+}++impl_try_from_value_all!([f64, f32, u64, u32, u16, u8, i64, i32, i16, i8]);+macro_rules! format_option {($F:expr, $EXPR:expr) => {{match $EXPR {@@ -2176,13 +2402,58 @@ impl ScalarType<i64> for TimestampNanosecondType {#[cfg(test)]mod tests {- use super::*;- use crate::from_slice::FromSlice;- use arrow::compute::kernels;- use arrow::datatypes::ArrowPrimitiveType;use std::cmp::Ordering;use std::sync::Arc;+ use arrow::compute::kernels;+ use arrow::datatypes::ArrowPrimitiveType;++ use crate::from_slice::FromSlice;++ use super::*;++ #[test]+ fn scalar_add_trait_test() -> Result<()> {+ let float_value = ScalarValue::Float64(Some(123.));+ let float_value_2 = ScalarValue::Float64(Some(123.));+ assert_eq!(+ (float_value.add(&float_value_2))?,+ ScalarValue::Float64(Some(246.))+ );+ assert_eq!(+ (float_value.add(float_value_2))?,+ ScalarValue::Float64(Some(246.))+ );+ Ok(())+ }++ #[test]+ fn scalar_sub_trait_test() -> Result<()> {+ let float_value = ScalarValue::Float64(Some(123.));+ let float_value_2 = ScalarValue::Float64(Some(123.));+ assert_eq!(+ float_value.sub(&float_value_2)?,+ ScalarValue::Float64(Some(0.))+ );+ assert_eq!(+ float_value.sub(&float_value_2)?,+ ScalarValue::Float64(Some(0.))+ );+ assert_eq!(+ float_value.sub(&float_value_2)?,+ ScalarValue::Float64(Some(0.))+ );+ assert_eq!(+ float_value.sub(&float_value_2)?,+ ScalarValue::Float64(Some(0.))+ );+ assert_eq!(+ float_value.sub(float_value_2)?,+ ScalarValue::Float64(Some(0.))+ );+ Ok(())+ }+#[test]fn scalar_decimal_test() -> Result<()> {let decimal_value = ScalarValue::Decimal128(Some(123), 10, 1);@@ -3416,4 +3687,208 @@ mod tests {assert!(value.arithmetic_negate().is_err());Ok(())}++ #[test]+ fn test_subtraction() {+ let lhs = ScalarValue::Float64(Some(11.0));+ let rhs = ScalarValue::Float64(Some(12.0));+ assert_eq!(lhs.sub(rhs).unwrap(), ScalarValue::Float64(Some(-1.0)));+ }++ #[test]+ fn expect_subtraction_error() {+ let lhs = ScalarValue::UInt64(Some(12));+ let rhs = ScalarValue::Int32(Some(-3));+ let expected_error = "Subtraction is not implemented";+ match lhs.sub(&rhs) {+ Ok(_result) => {+ panic!(+ "Expected summation error between lhs: '{:?}', rhs: {:?}",+ lhs, rhs+ );+ }+ Err(e) => {+ let error_message = e.to_string();+ assert!(+ error_message.contains(expected_error),+ "Expected error '{}' not found in actual error '{}'",+ expected_error,+ error_message+ );+ }+ }+ }++ macro_rules! decimal_op_test_cases {+ ($OPERATION:ident, [$([$L_VALUE:expr, $L_PRECISION:expr, $L_SCALE:expr, $R_VALUE:expr, $R_PRECISION:expr, $R_SCALE:expr, $O_VALUE:expr, $O_PRECISION:expr, $O_SCALE:expr]),+]) => {+ $(++ let left = ScalarValue::Decimal128($L_VALUE, $L_PRECISION, $L_SCALE);+ let right = ScalarValue::Decimal128($R_VALUE, $R_PRECISION, $R_SCALE);+ let result = left.$OPERATION(&right).unwrap();+ assert_eq!(ScalarValue::Decimal128($O_VALUE, $O_PRECISION, $O_SCALE), result);++ )++ };+ }++ #[test]+ fn decimal_operations() {+ decimal_op_test_cases!(+ add,+ [+ [Some(123), 10, 2, Some(124), 10, 2, Some(123 + 124), 10, 2],+ // test sum decimal with diff scale+ [+ Some(123),+ 10,+ 3,+ Some(124),+ 10,+ 2,+ Some(123 + 124 * 10_i128.pow(1)),+ 10,+ 3+ ],+ // diff precision and scale for decimal data type+ [+ Some(123),+ 10,+ 2,+ Some(124),+ 11,+ 3,+ Some(123 * 10_i128.pow(3 - 2) + 124),+ 11,+ 3+ ]+ ]+ );+ }++ #[test]+ fn decimal_operations_with_nulls() {+ decimal_op_test_cases!(+ add,+ [+ // Case: (None, Some, 0)+ [None, 10, 2, Some(123), 10, 2, Some(123), 10, 2],+ // Case: (Some, None, 0)+ [Some(123), 10, 2, None, 10, 2, Some(123), 10, 2],+ // Case: (Some, None, _) + Side=False+ [Some(123), 8, 2, None, 10, 3, Some(1230), 10, 3],+ // Case: (None, Some, _) + Side=False+ [None, 8, 2, Some(123), 10, 3, Some(123), 10, 3],+ // Case: (Some, None, _) + Side=True+ [Some(123), 8, 4, None, 10, 3, Some(123), 10, 4],+ // Case: (None, Some, _) + Side=True+ [None, 10, 3, Some(123), 8, 4, Some(123), 10, 4]+ ]+ );+ }++ macro_rules! op_test_cases {+ ($LHS:expr, $RHS:expr, $OUT:expr, $OPERATION:ident, [$([$L_TYPE:ident, $L_PRIM:ident, $R_TYPE:ident, $R_PRIM:ident, $O_TYPE:ident, $O_PRIM:ident]),+]) => {+ $(+ // From left+ let lhs = ScalarValue::$L_TYPE(Some($LHS as $L_PRIM));+ let rhs = ScalarValue::$R_TYPE(Some($RHS as $R_PRIM));+ assert_eq!(lhs.$OPERATION(rhs).unwrap(), ScalarValue::$O_TYPE(Some($OUT as $O_PRIM)));+ // From right. The values ($RHS and $LHS) also crossed to produce same output for subtraction.+ let lhs = ScalarValue::$L_TYPE(Some($RHS as $L_PRIM));+ let rhs = ScalarValue::$R_TYPE(Some($LHS as $R_PRIM));+ assert_eq!(rhs.$OPERATION(lhs).unwrap(), ScalarValue::$O_TYPE(Some($OUT as $O_PRIM)));+ )++ };+ }++ #[test]+ fn test_sum_operation_different_types() {+ op_test_cases!(+ 11,+ 12,+ 23,+ add,+ [+ // FloatXY coerces everything to fXY:+ [Float64, f64, Float32, f32, Float64, f64],+ [Float64, f64, Int64, i64, Float64, f64],+ [Float64, f64, Int32, i32, Float64, f64],+ [Float64, f64, Int16, i16, Float64, f64],+ [Float64, f64, Int8, i8, Float64, f64],+ [Float64, f64, UInt64, u64, Float64, f64],+ [Float64, f64, UInt32, u32, Float64, f64],+ [Float64, f64, UInt16, u16, Float64, f64],+ [Float64, f64, UInt8, u8, Float64, f64],+ // UIntXY coerces all smaller unsigned types to uXY:+ [UInt64, u64, UInt32, u32, UInt64, u64],+ [UInt64, u64, UInt16, u16, UInt64, u64],+ [UInt64, u64, UInt8, u8, UInt64, u64],+ // IntXY types coerce smaller integral types to iXY:+ [Int64, i64, Int32, i32, Int64, i64],+ [Int64, i64, Int16, i16, Int64, i64],+ [Int64, i64, Int8, i8, Int64, i64],+ [Int64, i64, UInt32, u32, Int64, i64],+ [Int64, i64, UInt16, u16, Int64, i64],+ [Int64, i64, UInt8, u8, Int64, i64]+ ]+ );+ }++ #[test]+ fn test_sub_operation_different_types() {+ op_test_cases!(+ 20,+ 8,+ 12,+ sub,+ [+ // FloatXY coerces everything to fXY:+ [Float64, f64, Float32, f32, Float64, f64],+ [Float64, f64, Int64, i64, Float64, f64],+ [Float64, f64, Int32, i32, Float64, f64],+ [Float64, f64, Int16, i16, Float64, f64],+ [Float64, f64, Int8, i8, Float64, f64],+ [Float64, f64, UInt64, u64, Float64, f64],+ [Float64, f64, UInt32, u32, Float64, f64],+ [Float64, f64, UInt16, u16, Float64, f64],+ [Float64, f64, UInt8, u8, Float64, f64],+ // UIntXY coerces all smaller unsigned types to uXY:+ [UInt64, u64, UInt32, u32, UInt64, u64],+ [UInt64, u64, UInt16, u16, UInt64, u64],+ [UInt64, u64, UInt8, u8, UInt64, u64],+ // IntXY types coerce smaller integral types to iXY:+ [Int64, i64, Int32, i32, Int64, i64],+ [Int64, i64, Int16, i16, Int64, i64],+ [Int64, i64, Int8, i8, Int64, i64],+ [Int64, i64, UInt32, u32, Int64, i64],+ [Int64, i64, UInt16, u16, Int64, i64],+ [Int64, i64, UInt8, u8, Int64, i64]+ ]+ );+ }++ #[test]+ fn expect_summation_error() {+ let lhs = ScalarValue::UInt64(Some(12));+ let rhs = ScalarValue::Int32(Some(-3));+ let expected_error = "Summation is not implemented";+ match lhs.add(&rhs) {+ Ok(_result) => {+ panic!(+ "Expected summation error between lhs: '{:?}', rhs: {:?}",+ lhs, rhs+ );+ }+ Err(e) => {+ let error_message = e.to_string();+ assert!(+ error_message.contains(expected_error),+ "Expected error '{}' not found in actual error '{}'",+ expected_error,+ error_message+ );+ }+ }+ }}diff --git a/datafusion/core/src/physical_plan/planner.rs b/datafusion/core/src/physical_plan/planner.rsindex 8b247a4521..cb9d461c5d 100644--- a/datafusion/core/src/physical_plan/planner.rs+++ b/datafusion/core/src/physical_plan/planner.rs@@ -62,6 +62,7 @@ use async_trait::async_trait;use datafusion_common::ScalarValue;use datafusion_expr::expr::GroupingSet;use datafusion_expr::utils::{expand_wildcard, expr_to_columns};+use datafusion_expr::WindowFrameUnits;use datafusion_physical_expr::expressions::Literal;use datafusion_sql::utils::window_expr_common_partition_keys;use futures::future::BoxFuture;@@ -1434,10 +1435,12 @@ pub fn create_window_expr_with_name()),}).collect::<Result<Vec<_>>>()?;- if window_frame.is_some() {+ if window_frame.is_some()+ && window_frame.unwrap().units == WindowFrameUnits::Groups+ {return Err(DataFusionError::NotImplemented(- "window expression with window frame definition is not yet supported"- .to_owned(),+ "Window frame definitions involving GROUPS are not supported yet"+ .to_string(),));}windows::create_window_expr(diff --git a/datafusion/core/tests/null_cases.csv b/datafusion/core/tests/null_cases.csvnew file mode 100644index 0000000000..055844010c--- /dev/null+++ b/datafusion/core/tests/null_cases.csv@@ -0,0 +1,101 @@+c1,c2,c3+,1.2,0+72,1.1,1+24,1.9,2+19,,3+25,1.1,4+14,1.3,5+31,1.8,6+40,1.9,7+82,,8+,1.9,9+32,,10+73,2.0,11+76,1.7,12+55,1.4,13+10,1.5,14+,1.2,15+38,1.2,16+60,1.2,17+11,1.3,18+35,1.5,19+61,,20+7,1.2,21+69,,22+27,1.8,23+3,1.6,24+90,1.6,25+14,1.1,26+14,,27+53,1.9,28+63,1.3,29+36,,30+15,1.2,31+65,1.2,32+89,1.3,33+76,1.7,34+30,,35+91,1.6,36+21,1.8,37+24,1.9,38+2,1.2,39+58,1.1,40+,1.5,41+38,1.2,42+56,,43+,2.0,44+,1.2,45+,1.6,46+65,,47+88,1.7,48+42,1.9,49+86,1.8,50+14,1.6,51+2,,52+70,1.1,53+56,1.8,54+94,1.7,55+73,1.4,56+36,,57+68,1.8,58+1,,59+27,1.6,60+25,1.5,61+46,1.8,62+74,1.2,63+87,1.2,64+96,1.4,65+48,1.8,66+10,1.5,67+58,1.9,68+,2.0,69+70,1.5,70+94,1.6,71+44,1.9,72+73,1.2,73+63,,74+16,1.1,75+58,1.0,76+44,1.7,77+38,1.7,78+86,1.6,79+88,,80+61,,81+,1.9,82+20,1.9,83+25,1.3,84+97,1.5,85+84,1.5,86+52,,87+13,,88+89,1.6,89+91,,90+39,1.1,91+91,,92+25,1.2,93+73,1.2,94+78,1.5,95+25,1.7,96+80,1.0,97+95,1.2,98+50,1.8,99diff --git a/datafusion/core/tests/sql/mod.rs b/datafusion/core/tests/sql/mod.rsindex dc50a5abe6..8ee70cb898 100644--- a/datafusion/core/tests/sql/mod.rs+++ b/datafusion/core/tests/sql/mod.rs@@ -695,7 +695,7 @@ async fn register_boolean(ctx: &SessionContext) -> Result<()> {}async fn register_aggregate_simple_csv(ctx: &SessionContext) -> Result<()> {- // It's not possible to use aggregate_test_100, not enought similar values to test grouping on floats+ // It's not possible to use aggregate_test_100 as it doesn't have enough similar values to test grouping on floats.let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Float32, false),Field::new("c2", DataType::Float64, false),@@ -711,6 +711,23 @@ async fn register_aggregate_simple_csv(ctx: &SessionContext) -> Result<()> {Ok(())}+async fn register_aggregate_null_cases_csv(ctx: &SessionContext) -> Result<()> {+ // It's not possible to use aggregate_test_100, not enought similar values to test grouping on floats+ let schema = Arc::new(Schema::new(vec![+ Field::new("c1", DataType::Int64, true),+ Field::new("c2", DataType::Float64, true),+ Field::new("c3", DataType::Int64, false),+ ]));++ ctx.register_csv(+ "null_cases",+ "tests/null_cases.csv",+ CsvReadOptions::new().schema(&schema),+ )+ .await?;+ Ok(())+}+async fn register_aggregate_csv(ctx: &SessionContext) -> Result<()> {let testdata = datafusion::test_util::arrow_test_data();let schema = test_util::aggr_test_schema();diff --git a/datafusion/core/tests/sql/window.rs b/datafusion/core/tests/sql/window.rsindex 6a1f39a030..fe1b539c77 100644--- a/datafusion/core/tests/sql/window.rs+++ b/datafusion/core/tests/sql/window.rs@@ -454,10 +454,10 @@ async fn window_in_expression() -> Result<()> {#[tokio::test]async fn window_with_agg_in_expression() -> Result<()> {let ctx = SessionContext::new();- let sql = "select col1, idx, count(*), sum(amount), lag(sum(amount), 1) over (order by idx) as prev_amount,+ let sql = "select col1, idx, count(*), sum(amount), lag(sum(amount), 1) over (order by idx) as prev_amount,sum(amount) - lag(sum(amount), 1) over (order by idx) as difference from (select * from (values ('a', 1, 100), ('a', 2, 150)) as t (col1, idx, amount)- ) a+ ) agroup by col1, idx;";let actual = execute_to_batches(&ctx, sql).await;let expected = vec![@@ -471,3 +471,651 @@ async fn window_with_agg_in_expression() -> Result<()> {assert_batches_eq!(expected, &actual);Ok(())}++#[tokio::test]+async fn window_frame_empty() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c3) OVER(),\+ COUNT(*) OVER ()\+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c3) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| 781 | 100 |",+ "| 781 | 100 |",+ "| 781 | 100 |",+ "| 781 | 100 |",+ "| 781 | 100 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_rows_preceding() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c4) OVER(ORDER BY c4 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\+ COUNT(*) OVER(ORDER BY c4 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)\+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c4) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| -48302 | 3 |",+ "| 11243 | 3 |",+ "| -51311 | 3 |",+ "| -2391 | 3 |",+ "| 46756 | 3 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}+#[tokio::test]+async fn window_frame_rows_preceding_with_partition_unique_order_by() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c4) OVER(PARTITION BY c1 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\+ COUNT(*) OVER(PARTITION BY c2 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)\+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c4) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| -38611 | 2 |",+ "| 17547 | 2 |",+ "| -1301 | 2 |",+ "| 26638 | 3 |",+ "| 26861 | 3 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}+/// The partition by clause conducts sorting according to given partition column by default. If the+/// sorting columns have non unique values, the unstable sorting may produce indeterminate results.+/// Therefore, we are commenting out the following test for now.++// #[tokio::test]+// async fn window_frame_rows_preceding_with_non_unique_partition() -> Result<()> {+// let ctx = SessionContext::new();+// register_aggregate_csv(&ctx).await?;+// let sql = "SELECT \+// SUM(c4) OVER(PARTITION BY c1 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\+// COUNT(*) OVER(PARTITION BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)\+// FROM aggregate_test_100 \+// ORDER BY c9 \+// LIMIT 5";+// let actual = execute_to_batches(&ctx, sql).await;+// let expected = vec![+// "+----------------------------+-----------------+",+// "| SUM(aggregate_test_100.c4) | COUNT(UInt8(1)) |",+// "+----------------------------+-----------------+",+// "| -33822 | 3 |",+// "| 20808 | 3 |",+// "| -29881 | 3 |",+// "| -47613 | 3 |",+// "| -13474 | 3 |",+// "+----------------------------+-----------------+",+// ];+// assert_batches_eq!(expected, &actual);+// Ok(())+// }++#[tokio::test]+async fn window_frame_ranges_preceding_following_desc() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c4) OVER(ORDER BY c2 DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING),\+ SUM(c3) OVER(ORDER BY c2 DESC RANGE BETWEEN 10000 PRECEDING AND 10000 FOLLOWING),\+ COUNT(*) OVER(ORDER BY c2 DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c4) | SUM(aggregate_test_100.c3) | COUNT(UInt8(1)) |",+ "+----------------------------+----------------------------+-----------------+",+ "| 52276 | 781 | 56 |",+ "| 260620 | 781 | 63 |",+ "| -28623 | 781 | 37 |",+ "| 260620 | 781 | 63 |",+ "| 260620 | 781 | 63 |",+ "+----------------------------+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_asc_desc_large() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT+ SUM(c5) OVER (ORDER BY c2 ASC, c6 DESC)+ FROM aggregate_test_100+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+",+ "| SUM(aggregate_test_100.c5) |",+ "+----------------------------+",+ "| -1383162419 |",+ "| -3265456275 |",+ "| -3909681744 |",+ "| -5241214934 |",+ "| -4246910946 |",+ "+----------------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_desc_large() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT+ SUM(c5) OVER (ORDER BY c2 DESC, c6 ASC)+ FROM aggregate_test_100+ ORDER BY c9+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+",+ "| SUM(aggregate_test_100.c5) |",+ "+----------------------------+",+ "| 11212193439 |",+ "| 22799733943 |",+ "| 2935356871 |",+ "| 15810962683 |",+ "| 18035025006 |",+ "+----------------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_null_timestamp_order_by() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_null_cases_csv(&ctx).await?;+ let sql = "SELECT+ SUM(c1) OVER (ORDER BY c2 DESC)+ FROM null_cases+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+--------------------+",+ "| SUM(null_cases.c1) |",+ "+--------------------+",+ "| 962 |",+ "| 962 |",+ "| 962 |",+ "| 962 |",+ "| 962 |",+ "+--------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_null_desc() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_null_cases_csv(&ctx).await?;+ let sql = "SELECT+ COUNT(c2) OVER (ORDER BY c1 DESC RANGE BETWEEN 5 PRECEDING AND 3 FOLLOWING)+ FROM null_cases+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------+",+ "| COUNT(null_cases.c2) |",+ "+----------------------+",+ "| 9 |",+ "| 9 |",+ "| 9 |",+ "| 9 |",+ "| 9 |",+ "+----------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_null_asc() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_null_cases_csv(&ctx).await?;+ let sql = "SELECT+ COUNT(c2) OVER (ORDER BY c1 RANGE BETWEEN 5 PRECEDING AND 3 FOLLOWING)+ FROM null_cases+ ORDER BY c1+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------+",+ "| COUNT(null_cases.c2) |",+ "+----------------------+",+ "| 2 |",+ "| 2 |",+ "| 2 |",+ "| 2 |",+ "| 5 |",+ "+----------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_null_asc_null_first() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_null_cases_csv(&ctx).await?;+ let sql = "SELECT+ COUNT(c2) OVER (ORDER BY c1 NULLS FIRST RANGE BETWEEN 5 PRECEDING AND 3 FOLLOWING)+ FROM null_cases+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------+",+ "| COUNT(null_cases.c2) |",+ "+----------------------+",+ "| 9 |",+ "| 9 |",+ "| 9 |",+ "| 9 |",+ "| 9 |",+ "+----------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_null_desc_null_last() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_null_cases_csv(&ctx).await?;+ let sql = "SELECT+ COUNT(c2) OVER (ORDER BY c1 DESC NULLS LAST RANGE BETWEEN 5 PRECEDING AND 3 FOLLOWING)+ FROM null_cases+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------+",+ "| COUNT(null_cases.c2) |",+ "+----------------------+",+ "| 5 |",+ "| 5 |",+ "| 5 |",+ "| 6 |",+ "| 6 |",+ "+----------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_rows_order_by_null() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_null_cases_csv(&ctx).await?;+ let sql = "SELECT+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN 10 PRECEDING AND 11 FOLLOWING) as a,+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN 10 PRECEDING AND 11 FOLLOWING) as b,+ SUM(c1) OVER (ORDER BY c3 DESC RANGE BETWEEN 10 PRECEDING AND 11 FOLLOWING) as c,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN 10 PRECEDING AND 11 FOLLOWING) as d,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS last RANGE BETWEEN 10 PRECEDING AND 11 FOLLOWING) as e,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS first RANGE BETWEEN 10 PRECEDING AND 11 FOLLOWING) as f,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN 10 PRECEDING AND 11 FOLLOWING) as g,+ SUM(c1) OVER (ORDER BY c3) as h,+ SUM(c1) OVER (ORDER BY c3 DESC) as i,+ SUM(c1) OVER (ORDER BY c3 NULLS first) as j,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS first) as k,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS last) as l,+ SUM(c1) OVER (ORDER BY c3, c2) as m,+ SUM(c1) OVER (ORDER BY c3, c1 DESC) as n,+ SUM(c1) OVER (ORDER BY c3 DESC, c1) as o,+ SUM(c1) OVER (ORDER BY c3, c1 NULLs first) as p,+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as a1,+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as b1,+ SUM(c1) OVER (ORDER BY c3 DESC RANGE BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as c1,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as d1,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS last RANGE BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as e1,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS first RANGE BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as f1,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN UNBOUNDED PRECEDING AND 11 FOLLOWING) as g1,+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as h1,+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as j1,+ SUM(c1) OVER (ORDER BY c3 DESC RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as k1,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as l1,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS last RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as m1,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS first RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as n1,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as o1,+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN current row AND UNBOUNDED FOLLOWING) as h11,+ SUM(c1) OVER (ORDER BY c3 RANGE BETWEEN current row AND UNBOUNDED FOLLOWING) as j11,+ SUM(c1) OVER (ORDER BY c3 DESC RANGE BETWEEN current row AND UNBOUNDED FOLLOWING) as k11,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN current row AND UNBOUNDED FOLLOWING) as l11,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS last RANGE BETWEEN current row AND UNBOUNDED FOLLOWING) as m11,+ SUM(c1) OVER (ORDER BY c3 DESC NULLS first RANGE BETWEEN current row AND UNBOUNDED FOLLOWING) as n11,+ SUM(c1) OVER (ORDER BY c3 NULLS first RANGE BETWEEN current row AND UNBOUNDED FOLLOWING) as o11+ FROM null_cases+ ORDER BY c3+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+------+------+-----+-----+------+-----+-----+-----+------+-----+------+------+-----+-----+-----+------+-----+------+------+-----+------+------+-----+------+-----+-----+------+",+ "| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | a1 | b1 | c1 | d1 | e1 | f1 | g1 | h1 | j1 | k1 | l1 | m1 | n1 | o1 | h11 | j11 | k11 | l11 | m11 | n11 | o11 |",+ "+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+------+------+-----+-----+------+-----+-----+-----+------+-----+------+------+-----+-----+-----+------+-----+------+------+-----+------+------+-----+------+-----+-----+------+",+ "| 412 | 412 | 339 | 412 | 339 | 339 | 412 | | 4627 | | 4627 | 4627 | | | 4627 | | 412 | 412 | 4627 | 412 | 4627 | 4627 | 412 | | | 4627 | | 4627 | 4627 | | 4627 | 4627 | | 4627 | | | 4627 |",+ "| 488 | 488 | 412 | 488 | 412 | 412 | 488 | 72 | 4627 | 72 | 4627 | 4627 | 72 | 72 | 4627 | 72 | 488 | 488 | 4627 | 488 | 4627 | 4627 | 488 | 72 | 72 | 4627 | 72 | 4627 | 4627 | 72 | 4627 | 4627 | 72 | 4627 | 72 | 72 | 4627 |",+ "| 543 | 543 | 488 | 543 | 488 | 488 | 543 | 96 | 4555 | 96 | 4555 | 4555 | 96 | 96 | 4555 | 96 | 543 | 543 | 4627 | 543 | 4627 | 4627 | 543 | 96 | 96 | 4555 | 96 | 4555 | 4555 | 96 | 4555 | 4555 | 96 | 4555 | 96 | 96 | 4555 |",+ "| 553 | 553 | 543 | 553 | 543 | 543 | 553 | 115 | 4531 | 115 | 4531 | 4531 | 115 | 115 | 4531 | 115 | 553 | 553 | 4627 | 553 | 4627 | 4627 | 553 | 115 | 115 | 4531 | 115 | 4531 | 4531 | 115 | 4531 | 4531 | 115 | 4531 | 115 | 115 | 4531 |",+ "| 553 | 553 | 553 | 553 | 553 | 553 | 553 | 140 | 4512 | 140 | 4512 | 4512 | 140 | 140 | 4512 | 140 | 553 | 553 | 4627 | 553 | 4627 | 4627 | 553 | 140 | 140 | 4512 | 140 | 4512 | 4512 | 140 | 4512 | 4512 | 140 | 4512 | 140 | 140 | 4512 |",+ "+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+------+------+-----+-----+------+-----+-----+-----+------+-----+------+------+-----+-----+-----+------+-----+------+------+-----+------+------+-----+------+-----+-----+------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_rows_preceding_with_unique_partition() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c4) OVER(PARTITION BY c1 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\+ COUNT(*) OVER(PARTITION BY c1 ORDER BY c9 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)\+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c4) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| -38611 | 2 |",+ "| 17547 | 2 |",+ "| -1301 | 2 |",+ "| 26638 | 2 |",+ "| 26861 | 3 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_ranges_preceding_following() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c4) OVER(ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING),\+ SUM(c3) OVER(ORDER BY c2 RANGE BETWEEN 10000 PRECEDING AND 10000 FOLLOWING),\+ COUNT(*) OVER(ORDER BY c2 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c4) | SUM(aggregate_test_100.c3) | COUNT(UInt8(1)) |",+ "+----------------------------+----------------------------+-----------------+",+ "| 52276 | 781 | 56 |",+ "| 260620 | 781 | 63 |",+ "| -28623 | 781 | 37 |",+ "| 260620 | 781 | 63 |",+ "| 260620 | 781 | 63 |",+ "+----------------------------+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_ranges_string_check() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(LENGTH(c13)) OVER(ORDER BY c13), \+ SUM(LENGTH(c1)) OVER(ORDER BY c1) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------------------------+---------------------------------------------+",+ "| SUM(characterlength(aggregate_test_100.c13)) | SUM(characterlength(aggregate_test_100.c1)) |",+ "+----------------------------------------------+---------------------------------------------+",+ "| 2100 | 100 |",+ "| 510 | 79 |",+ "| 1440 | 21 |",+ "| 1830 | 61 |",+ "| 2010 | 21 |",+ "+----------------------------------------------+---------------------------------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_order_by_unique() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c5) OVER (ORDER BY c5), \+ COUNT(*) OVER (ORDER BY c9) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c5) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| -49877765574 | 1 |",+ "| -50025861694 | 2 |",+ "| -45402230071 | 3 |",+ "| -14557735645 | 4 |",+ "| -18365391649 | 5 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++/// If the sorting columns have non unique values, the unstable sorting may produce+/// indeterminate results. Therefore, we are commenting out the following test for now.+///+// #[tokio::test]+// async fn window_frame_order_by_non_unique() -> Result<()> {+// let ctx = SessionContext::new();+// register_aggregate_csv(&ctx).await?;+// let sql = "SELECT \+// c2, \+// c9, \+// SUM(c5) OVER (ORDER BY c2), \+// COUNT(*) OVER (ORDER BY c2) \+// FROM aggregate_test_100 \+// ORDER BY c2 \+// LIMIT 5";+// let actual = execute_to_batches(&ctx, sql).await;+// let expected = vec![+// "+----+------------+----------------------------+-----------------+",+// "| c2 | c9 | SUM(aggregate_test_100.c5) | COUNT(UInt8(1)) |",+// "+----+------------+----------------------------+-----------------+",+// "| 1 | 879082834 | -438598674 | 22 |",+// "| 1 | 3542840110 | -438598674 | 22 |",+// "| 1 | 3275293996 | -438598674 | 22 |",+// "| 1 | 774637006 | -438598674 | 22 |",+// "| 1 | 4015442341 | -438598674 | 22 |",+// "+----+------------+----------------------------+-----------------+",+// ];+// assert_batches_eq!(expected, &actual);+// Ok(())+// }++#[tokio::test]+async fn window_frame_ranges_unbounded_preceding_following() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING), \+ COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c2) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| 285 | 100 |",+ "| 123 | 63 |",+ "| 285 | 100 |",+ "| 123 | 63 |",+ "| 123 | 63 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_ranges_preceding_and_preceding() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING), \+ COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c2) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| 123 | 63 |",+ "| 22 | 22 |",+ "| 193 | 64 |",+ "| 22 | 22 |",+ "| 22 | 22 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_ranges_unbounded_preceding_following_diff_col() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT \+ SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING), \+ COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+-----------------+",+ "| SUM(aggregate_test_100.c2) | COUNT(UInt8(1)) |",+ "+----------------------------+-----------------+",+ "| 162 | 37 |",+ "| 101 | 41 |",+ "| 70 | 14 |",+ "| 101 | 41 |",+ "| 101 | 41 |",+ "+----------------------------+-----------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_partition_by_order_by_desc() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ let sql = "SELECT+ SUM(c4) OVER(PARTITION BY c1 ORDER BY c2 DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)+ FROM aggregate_test_100+ ORDER BY c9+ LIMIT 5";+ let actual = execute_to_batches(&ctx, sql).await;+ let expected = vec![+ "+----------------------------+",+ "| SUM(aggregate_test_100.c4) |",+ "+----------------------------+",+ "| -124618 |",+ "| 205080 |",+ "| -40819 |",+ "| -19517 |",+ "| 47246 |",+ "+----------------------------+",+ ];+ assert_batches_eq!(expected, &actual);+ Ok(())+}++#[tokio::test]+async fn window_frame_ranges_unbounded_preceding_err() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ // execute the query+ let df = ctx+ .sql(+ "SELECT \+ SUM(c2) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING), \+ COUNT(*) OVER (ORDER BY c2 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING) \+ FROM aggregate_test_100 \+ ORDER BY c9 \+ LIMIT 5",+ )+ .await;+ assert_eq!(+ df.err().unwrap().to_string(),+ "Execution error: Invalid window frame: end bound cannot be unbounded preceding"+ .to_owned()+ );+ Ok(())+}++#[tokio::test]+async fn window_frame_groups_query() -> Result<()> {+ let ctx = SessionContext::new();+ register_aggregate_csv(&ctx).await?;+ // execute the query+ let df = ctx+ .sql(+ "SELECT+ COUNT(c1) OVER (ORDER BY c2 GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)+ FROM aggregate_test_100;",+ )+ .await?;+ let results = df.collect().await;+ assert!(results+ .as_ref()+ .err()+ .unwrap()+ .to_string()+ .contains("Window frame definitions involving GROUPS are not supported yet"));+ Ok(())+}diff --git a/datafusion/expr/src/accumulator.rs b/datafusion/expr/src/accumulator.rsindex 6c146bc306..d0540f5019 100644--- a/datafusion/expr/src/accumulator.rs+++ b/datafusion/expr/src/accumulator.rs@@ -26,6 +26,7 @@ use std::fmt::Debug;////// An accumulator knows how to:/// * update its state from inputs via `update_batch`+/// * retract an update to its state from given inputs via `retract_batch`/// * convert its internal state to a vector of aggregate values/// * update its state from multiple accumulators' states via `merge_batch`/// * compute the final value from its internal state via `evaluate`@@ -35,9 +36,19 @@ pub trait Accumulator: Send + Sync + Debug {/// of two values, sum and n.fn state(&self) -> Result<Vec<AggregateState>>;- /// updates the accumulator's state from a vector of arrays.+ /// Updates the accumulator's state from a vector of arrays.fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()>;+ /// Retracts an update (caused by the given inputs) to accumulator's state.+ /// Inverse operation of the `update_batch` operation. This method must be+ /// for accumulators that should support bounded OVER aggregates.+ fn retract_batch(&mut self, _values: &[ArrayRef]) -> Result<()> {+ // TODO add retract for all accumulators+ Err(DataFusionError::Internal(+ "Retract should be implemented for aggregate functions when used with custom window frame queries".to_string()+ ))+ }+/// updates the accumulator's state from a vector of states.fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()>;diff --git a/datafusion/expr/src/window_frame.rs b/datafusion/expr/src/window_frame.rsindex a0d6ed0ebd..7f9afd0b51 100644--- a/datafusion/expr/src/window_frame.rs+++ b/datafusion/expr/src/window_frame.rs@@ -78,27 +78,11 @@ impl TryFrom<ast::WindowFrame> for WindowFrame {))} else if start_bound > end_bound {Err(DataFusionError::Execution(format!(- "Invalid window frame: start bound ({}) cannot be larger than end bound ({})",- start_bound, end_bound- )))+ "Invalid window frame: start bound ({}) cannot be larger than end bound ({})",+ start_bound, end_bound+ )))} else {let units = value.units.into();- if units == WindowFrameUnits::Range {- for bound in &[start_bound, end_bound] {- match bound {- WindowFrameBound::Preceding(Some(v))- | WindowFrameBound::Following(Some(v))- if *v > 0 =>- {- Err(DataFusionError::NotImplemented(format!(- "With WindowFrameUnits={}, the bound cannot be {} PRECEDING or FOLLOWING at the moment",- units, v- )))- }- _ => Ok(()),- }?;- }- }Ok(Self {units,start_bound,@@ -296,17 +280,6 @@ mod tests {"Execution error: Invalid window frame: start bound (1 PRECEDING) cannot be larger than end bound (2 PRECEDING)".to_owned());- let window_frame = ast::WindowFrame {- units: ast::WindowFrameUnits::Range,- start_bound: ast::WindowFrameBound::Preceding(Some(2)),- end_bound: Some(ast::WindowFrameBound::Preceding(Some(1))),- };- let result = WindowFrame::try_from(window_frame);- assert_eq!(- result.err().unwrap().to_string(),- "This feature is not implemented: With WindowFrameUnits=RANGE, the bound cannot be 2 PRECEDING or FOLLOWING at the moment".to_owned()- );-let window_frame = ast::WindowFrame {units: ast::WindowFrameUnits::Rows,start_bound: ast::WindowFrameBound::Preceding(Some(2)),diff --git a/datafusion/physical-expr/src/aggregate/average.rs b/datafusion/physical-expr/src/aggregate/average.rsindex 501e624564..723ae7e9ab 100644--- a/datafusion/physical-expr/src/aggregate/average.rs+++ b/datafusion/physical-expr/src/aggregate/average.rs@@ -161,10 +161,9 @@ impl Accumulator for AvgAccumulator {let values = &values[0];self.count += (values.len() - values.data().null_count()) as u64;- self.sum = sum::sum(- &self.sum,- &sum::sum_batch(values, &self.sum.get_datatype())?,- )?;+ self.sum = self+ .sum+ .add(&sum::sum_batch(values, &self.sum.get_datatype())?)?;Ok(())}@@ -174,10 +173,9 @@ impl Accumulator for AvgAccumulator {self.count += compute::sum(counts).unwrap_or(0);// sums are summed- self.sum = sum::sum(- &self.sum,- &sum::sum_batch(&states[1], &self.sum.get_datatype())?,- )?;+ self.sum = self+ .sum+ .add(&sum::sum_batch(&states[1], &self.sum.get_datatype())?)?;Ok(())}diff --git a/datafusion/physical-expr/src/aggregate/correlation.rs b/datafusion/physical-expr/src/aggregate/correlation.rsindex 3bbea5d9b3..f25cb5790d 100644--- a/datafusion/physical-expr/src/aggregate/correlation.rs+++ b/datafusion/physical-expr/src/aggregate/correlation.rs@@ -146,8 +146,15 @@ impl Accumulator for CorrelationAccumulator {fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {self.covar.update_batch(values)?;- self.stddev1.update_batch(&[values[0].clone()])?;- self.stddev2.update_batch(&[values[1].clone()])?;+ self.stddev1.update_batch(&values[0..1])?;+ self.stddev2.update_batch(&values[1..2])?;+ Ok(())+ }++ fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {+ self.covar.retract_batch(values)?;+ self.stddev1.retract_batch(&values[0..1])?;+ self.stddev2.retract_batch(&values[1..2])?;Ok(())}diff --git a/datafusion/physical-expr/src/aggregate/count.rs b/datafusion/physical-expr/src/aggregate/count.rsindex 96a8febd04..8cfb85fa91 100644--- a/datafusion/physical-expr/src/aggregate/count.rs+++ b/datafusion/physical-expr/src/aggregate/count.rs@@ -119,12 +119,24 @@ impl CountAccumulator {}impl Accumulator for CountAccumulator {+ fn state(&self) -> Result<Vec<AggregateState>> {+ Ok(vec![AggregateState::Scalar(ScalarValue::Int64(Some(+ self.count,+ )))])+ }+fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {let array = &values[0];self.count += (array.len() - array.data().null_count()) as i64;Ok(())}+ fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {+ let array = &values[0];+ self.count -= (array.len() - array.data().null_count()) as i64;+ Ok(())+ }+fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {let counts = downcast_value!(states[0], Int64Array);let delta = &compute::sum(counts);@@ -134,12 +146,6 @@ impl Accumulator for CountAccumulator {Ok(())}- fn state(&self) -> Result<Vec<AggregateState>> {- Ok(vec![AggregateState::Scalar(ScalarValue::Int64(Some(- self.count,- )))])- }-fn evaluate(&self) -> Result<ScalarValue> {Ok(ScalarValue::Int64(Some(self.count)))}diff --git a/datafusion/physical-expr/src/aggregate/covariance.rs b/datafusion/physical-expr/src/aggregate/covariance.rsindex c848816204..0911111a3f 100644--- a/datafusion/physical-expr/src/aggregate/covariance.rs+++ b/datafusion/physical-expr/src/aggregate/covariance.rs@@ -285,6 +285,40 @@ impl Accumulator for CovarianceAccumulator {Ok(())}+ fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {+ let values1 = &cast(&values[0], &DataType::Float64)?;+ let values2 = &cast(&values[1], &DataType::Float64)?;+ let mut arr1 = downcast_value!(values1, Float64Array).iter().flatten();+ let mut arr2 = downcast_value!(values2, Float64Array).iter().flatten();++ for _i in 0..values1.len() {+ let value1 = arr1.next();+ let value2 = arr2.next();++ if value1 == None || value2 == None {+ if value1 == None && value2 == None {+ continue;+ } else {+ return Err(DataFusionError::Internal(+ "The two columns are not aligned".to_string(),+ ));+ }+ }+ let new_count = self.count - 1;+ let delta1 = self.mean1 - value1.unwrap();+ let new_mean1 = delta1 / new_count as f64 + self.mean1;+ let delta2 = self.mean2 - value2.unwrap();+ let new_mean2 = delta2 / new_count as f64 + self.mean2;+ let new_c = self.algo_const - delta1 * (new_mean2 - value2.unwrap());++ self.count -= 1;+ self.mean1 = new_mean1;+ self.mean2 = new_mean2;+ self.algo_const = new_c;+ }+ Ok(())+ }+fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {let counts = downcast_value!(states[0], UInt64Array);let means1 = downcast_value!(states[1], Float64Array);diff --git a/datafusion/physical-expr/src/aggregate/sum.rs b/datafusion/physical-expr/src/aggregate/sum.rsindex d639a11708..892ef5964d 100644--- a/datafusion/physical-expr/src/aggregate/sum.rs+++ b/datafusion/physical-expr/src/aggregate/sum.rs@@ -85,11 +85,18 @@ impl AggregateExpr for Sum {}fn state_fields(&self) -> Result<Vec<Field>> {- Ok(vec![Field::new(- &format_state_name(&self.name, "sum"),- self.data_type.clone(),- self.nullable,- )])+ Ok(vec![+ Field::new(+ &format_state_name(&self.name, "sum"),+ self.data_type.clone(),+ self.nullable,+ ),+ Field::new(+ &format_state_name(&self.name, "count"),+ DataType::UInt64,+ self.nullable,+ ),+ ])}fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {@@ -130,6 +137,7 @@ impl AggregateExpr for Sum {#[derive(Debug)]struct SumAccumulator {sum: ScalarValue,+ count: u64,}impl SumAccumulator {@@ -137,6 +145,7 @@ impl SumAccumulator {pub fn try_new(data_type: &DataType) -> Result<Self> {Ok(Self {sum: ScalarValue::try_from(data_type)?,+ count: 0,})}}@@ -194,18 +203,6 @@ pub(crate) fn sum_batch(values: &ArrayRef, sum_type: &DataType) -> Result<Scalar})}-// returns the sum of two scalar values, including coercion into $TYPE.-macro_rules! typed_sum {- ($OLD_VALUE:expr, $DELTA:expr, $SCALAR:ident, $TYPE:ident) => {{- ScalarValue::$SCALAR(match ($OLD_VALUE, $DELTA) {- (None, None) => None,- (Some(a), None) => Some(a.clone()),- (None, Some(b)) => Some(b.clone() as $TYPE),- (Some(a), Some(b)) => Some(a + (*b as $TYPE)),- })- }};-}-macro_rules! sum_row {($INDEX:ident, $ACC:ident, $DELTA:expr, $TYPE:ident) => {{paste::item! {@@ -217,141 +214,6 @@ macro_rules! sum_row {}};}-// TODO implement this in arrow-rs with simd-// https://github.com/apache/arrow-rs/issues/1010-fn sum_decimal(- lhs: &Option<i128>,- rhs: &Option<i128>,- precision: u8,- scale: u8,-) -> ScalarValue {- match (lhs, rhs) {- (None, None) => ScalarValue::Decimal128(None, precision, scale),- (None, rhs) => ScalarValue::Decimal128(*rhs, precision, scale),- (lhs, None) => ScalarValue::Decimal128(*lhs, precision, scale),- (Some(lhs_value), Some(rhs_value)) => {- ScalarValue::Decimal128(Some(lhs_value + rhs_value), precision, scale)- }- }-}--fn sum_decimal_with_diff_scale(- lhs: &Option<i128>,- rhs: &Option<i128>,- precision: u8,- lhs_scale: u8,- rhs_scale: u8,-) -> ScalarValue {- // the lhs_scale must be greater or equal rhs_scale.- match (lhs, rhs) {- (None, None) => ScalarValue::Decimal128(None, precision, lhs_scale),- (None, Some(rhs_value)) => {- let new_value = rhs_value * 10_i128.pow((lhs_scale - rhs_scale) as u32);- ScalarValue::Decimal128(Some(new_value), precision, lhs_scale)- }- (lhs, None) => ScalarValue::Decimal128(*lhs, precision, lhs_scale),- (Some(lhs_value), Some(rhs_value)) => {- let new_value =- rhs_value * 10_i128.pow((lhs_scale - rhs_scale) as u32) + lhs_value;- ScalarValue::Decimal128(Some(new_value), precision, lhs_scale)- }- }-}--pub(crate) fn sum(lhs: &ScalarValue, rhs: &ScalarValue) -> Result<ScalarValue> {- Ok(match (lhs, rhs) {- (ScalarValue::Decimal128(v1, p1, s1), ScalarValue::Decimal128(v2, p2, s2)) => {- let max_precision = *p1.max(p2);- if s1.eq(s2) {- // s1 = s2- sum_decimal(v1, v2, max_precision, *s1)- } else if s1.gt(s2) {- // s1 > s2- sum_decimal_with_diff_scale(v1, v2, max_precision, *s1, *s2)- } else {- // s1 < s2- sum_decimal_with_diff_scale(v2, v1, max_precision, *s2, *s1)- }- }- // float64 coerces everything to f64- (ScalarValue::Float64(lhs), ScalarValue::Float64(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::Float32(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::Int64(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::Int32(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::Int16(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::Int8(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::UInt64(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::UInt32(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::UInt16(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- (ScalarValue::Float64(lhs), ScalarValue::UInt8(rhs)) => {- typed_sum!(lhs, rhs, Float64, f64)- }- // float32 has no cast- (ScalarValue::Float32(lhs), ScalarValue::Float32(rhs)) => {- typed_sum!(lhs, rhs, Float32, f32)- }- // u64 coerces u* to u64- (ScalarValue::UInt64(lhs), ScalarValue::UInt64(rhs)) => {- typed_sum!(lhs, rhs, UInt64, u64)- }- (ScalarValue::UInt64(lhs), ScalarValue::UInt32(rhs)) => {- typed_sum!(lhs, rhs, UInt64, u64)- }- (ScalarValue::UInt64(lhs), ScalarValue::UInt16(rhs)) => {- typed_sum!(lhs, rhs, UInt64, u64)- }- (ScalarValue::UInt64(lhs), ScalarValue::UInt8(rhs)) => {- typed_sum!(lhs, rhs, UInt64, u64)- }- // i64 coerces i* to i64- (ScalarValue::Int64(lhs), ScalarValue::Int64(rhs)) => {- typed_sum!(lhs, rhs, Int64, i64)- }- (ScalarValue::Int64(lhs), ScalarValue::Int32(rhs)) => {- typed_sum!(lhs, rhs, Int64, i64)- }- (ScalarValue::Int64(lhs), ScalarValue::Int16(rhs)) => {- typed_sum!(lhs, rhs, Int64, i64)- }- (ScalarValue::Int64(lhs), ScalarValue::Int8(rhs)) => {- typed_sum!(lhs, rhs, Int64, i64)- }- (ScalarValue::Int64(lhs), ScalarValue::UInt32(rhs)) => {- typed_sum!(lhs, rhs, Int64, i64)- }- (ScalarValue::Int64(lhs), ScalarValue::UInt16(rhs)) => {- typed_sum!(lhs, rhs, Int64, i64)- }- (ScalarValue::Int64(lhs), ScalarValue::UInt8(rhs)) => {- typed_sum!(lhs, rhs, Int64, i64)- }- e => {- return Err(DataFusionError::Internal(format!(- "Sum is not expected to receive a scalar {:?}",- e- )));- }- })-}-pub(crate) fn add_to_row(dt: &DataType,index: usize,@@ -432,12 +294,27 @@ pub(crate) fn add_to_row(impl Accumulator for SumAccumulator {fn state(&self) -> Result<Vec<AggregateState>> {- Ok(vec![AggregateState::Scalar(self.sum.clone())])+ Ok(vec![+ AggregateState::Scalar(self.sum.clone()),+ AggregateState::Scalar(ScalarValue::from(self.count)),+ ])}fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {let values = &values[0];- self.sum = sum(&self.sum, &sum_batch(values, &self.sum.get_datatype())?)?;+ self.count += (values.len() - values.data().null_count()) as u64;+ self.sum = self+ .sum+ .add(&sum_batch(values, &self.sum.get_datatype())?)?;+ Ok(())+ }++ fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {+ let values = &values[0];+ self.count -= (values.len() - values.data().null_count()) as u64;+ self.sum = self+ .sum+ .sub(&sum_batch(values, &self.sum.get_datatype())?)?;Ok(())}@@ -449,7 +326,11 @@ impl Accumulator for SumAccumulator {fn evaluate(&self) -> Result<ScalarValue> {// TODO: add the checker for overflow// For the decimal(precision,_) data type, the absolute of value must be less than 10^precision.- Ok(self.sum.clone())+ if self.count == 0 {+ ScalarValue::try_from(&self.sum.get_datatype())+ } else {+ Ok(self.sum.clone())+ }}}@@ -511,28 +392,6 @@ mod tests {#[test]fn sum_decimal() -> Result<()> {- // test sum- let left = ScalarValue::Decimal128(Some(123), 10, 2);- let right = ScalarValue::Decimal128(Some(124), 10, 2);- let result = sum(&left, &right)?;- assert_eq!(ScalarValue::Decimal128(Some(123 + 124), 10, 2), result);- // test sum decimal with diff scale- let left = ScalarValue::Decimal128(Some(123), 10, 3);- let right = ScalarValue::Decimal128(Some(124), 10, 2);- let result = sum(&left, &right)?;- assert_eq!(- ScalarValue::Decimal128(Some(123 + 124 * 10_i128.pow(1)), 10, 3),- result- );- // diff precision and scale for decimal data type- let left = ScalarValue::Decimal128(Some(123), 10, 2);- let right = ScalarValue::Decimal128(Some(124), 11, 3);- let result = sum(&left, &right);- assert_eq!(- ScalarValue::Decimal128(Some(123 * 10_i128.pow(3 - 2) + 124), 11, 3),- result.unwrap()- );-// test sum batchlet array: ArrayRef = Arc::new((1..6)@@ -562,12 +421,6 @@ mod tests {#[test]fn sum_decimal_with_nulls() -> Result<()> {- // test sum- let left = ScalarValue::Decimal128(None, 10, 2);- let right = ScalarValue::Decimal128(Some(123), 10, 2);- let result = sum(&left, &right)?;- assert_eq!(ScalarValue::Decimal128(Some(123), 10, 2), result);-// test with batchlet array: ArrayRef = Arc::new((1..6)@@ -596,12 +449,6 @@ mod tests {#[test]fn sum_decimal_all_nulls() -> Result<()> {- // test sum- let left = ScalarValue::Decimal128(None, 10, 2);- let right = ScalarValue::Decimal128(None, 10, 2);- let result = sum(&left, &right)?;- assert_eq!(ScalarValue::Decimal128(None, 10, 2), result);-// test with batchlet array: ArrayRef = Arc::new(std::iter::repeat::<Option<i128>>(None)diff --git a/datafusion/physical-expr/src/aggregate/sum_distinct.rs b/datafusion/physical-expr/src/aggregate/sum_distinct.rsindex 5f9738f680..c8abdcac0c 100644--- a/datafusion/physical-expr/src/aggregate/sum_distinct.rs+++ b/datafusion/physical-expr/src/aggregate/sum_distinct.rs@@ -15,7 +15,6 @@// specific language governing permissions and limitations// under the License.-use crate::aggregate::sum;use crate::expressions::format_state_name;use arrow::datatypes::{DataType, Field};use std::any::Any;@@ -172,7 +171,7 @@ impl Accumulator for DistinctSumAccumulator {fn evaluate(&self) -> Result<ScalarValue> {let mut sum_value = ScalarValue::try_from(&self.data_type)?;for distinct_value in self.hash_values.iter() {- sum_value = sum::sum(&sum_value, distinct_value)?;+ sum_value = sum_value.add(distinct_value)?;}Ok(sum_value)}diff --git a/datafusion/physical-expr/src/aggregate/variance.rs b/datafusion/physical-expr/src/aggregate/variance.rsindex 07f8a34ce4..7ccec55ac3 100644--- a/datafusion/physical-expr/src/aggregate/variance.rs+++ b/datafusion/physical-expr/src/aggregate/variance.rs@@ -238,6 +238,25 @@ impl Accumulator for VarianceAccumulator {Ok(())}+ fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {+ let values = &cast(&values[0], &DataType::Float64)?;+ let arr = downcast_value!(values, Float64Array).iter().flatten();++ for value in arr {+ let new_count = self.count - 1;+ let delta1 = self.mean - value;+ let new_mean = delta1 / new_count as f64 + self.mean;+ let delta2 = new_mean - value;+ let new_m2 = self.m2 - delta1 * delta2;++ self.count -= 1;+ self.mean = new_mean;+ self.m2 = new_m2;+ }++ Ok(())+ }+fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {let counts = downcast_value!(states[0], UInt64Array);let means = downcast_value!(states[1], Float64Array);diff --git a/datafusion/physical-expr/src/expressions/negative.rs b/datafusion/physical-expr/src/expressions/negative.rsindex 0307f71843..8619df1b39 100644--- a/datafusion/physical-expr/src/expressions/negative.rs+++ b/datafusion/physical-expr/src/expressions/negative.rs@@ -103,7 +103,7 @@ impl PhysicalExpr for NegativeExpr {result.map(|a| ColumnarValue::Array(a))}ColumnarValue::Scalar(scalar) => {- Ok(ColumnarValue::Scalar(scalar.arithmetic_negate()?))+ Ok(ColumnarValue::Scalar((scalar.arithmetic_negate())?))}}}diff --git a/datafusion/physical-expr/src/window/aggregate.rs b/datafusion/physical-expr/src/window/aggregate.rsindex 9caa847c02..b81c0eaf24 100644--- a/datafusion/physical-expr/src/window/aggregate.rs+++ b/datafusion/physical-expr/src/window/aggregate.rs@@ -17,21 +17,27 @@//! Physical exec for aggregate window function expressions.-use crate::window::partition_evaluator::find_ranges_in_range;-use crate::{expressions::PhysicalSortExpr, PhysicalExpr};-use crate::{window::WindowExpr, AggregateExpr};-use arrow::compute::concat;-use arrow::record_batch::RecordBatch;-use arrow::{array::ArrayRef, datatypes::Field};-use datafusion_common::DataFusionError;-use datafusion_common::Result;-use datafusion_expr::Accumulator;-use datafusion_expr::{WindowFrame, WindowFrameUnits};use std::any::Any;+use std::cmp::min;use std::iter::IntoIterator;use std::ops::Range;use std::sync::Arc;+use arrow::array::Array;+use arrow::compute::{concat, SortOptions};+use arrow::record_batch::RecordBatch;+use arrow::{array::ArrayRef, datatypes::Field};++use datafusion_common::bisect::bisect;+use datafusion_common::scalar::TryFromValue;+use datafusion_common::Result;+use datafusion_common::{DataFusionError, ScalarValue};+use datafusion_expr::{Accumulator, WindowFrameBound};+use datafusion_expr::{WindowFrame, WindowFrameUnits};++use crate::{expressions::PhysicalSortExpr, PhysicalExpr};+use crate::{window::WindowExpr, AggregateExpr};+/// A window expr that takes the form of an aggregate function#[derive(Debug)]pub struct AggregateWindowExpr {@@ -57,38 +63,65 @@ impl AggregateWindowExpr {}}- /// the aggregate window function operates based on window frame, and by default the mode is- /// "range".- fn evaluation_mode(&self) -> WindowFrameUnits {- self.window_frame.unwrap_or_default().units- }-/// create a new accumulator based on the underlying aggregation functionfn create_accumulator(&self) -> Result<AggregateWindowAccumulator> {let accumulator = self.aggregate.create_accumulator()?;- Ok(AggregateWindowAccumulator { accumulator })+ let window_frame = self.window_frame;+ let partition_by = self.partition_by().to_vec();+ let order_by = self.order_by.to_vec();+ let field = self.aggregate.field()?;++ Ok(AggregateWindowAccumulator {+ accumulator,+ window_frame,+ partition_by,+ order_by,+ field,+ })+ }+}++/// peer based evaluation based on the fact that batch is pre-sorted given the sort columns+/// and then per partition point we'll evaluate the peer group (e.g. SUM or MAX gives the same+/// results for peers) and concatenate the results.++impl WindowExpr for AggregateWindowExpr {+ /// Return a reference to Any that can be used for downcasting+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn field(&self) -> Result<Field> {+ self.aggregate.field()+ }++ fn name(&self) -> &str {+ self.aggregate.name()+ }++ fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {+ self.aggregate.expressions()}- /// peer based evaluation based on the fact that batch is pre-sorted given the sort columns- /// and then per partition point we'll evaluate the peer group (e.g. SUM or MAX gives the same- /// results for peers) and concatenate the results.- fn peer_based_evaluate(&self, batch: &RecordBatch) -> Result<ArrayRef> {+ fn evaluate(&self, batch: &RecordBatch) -> Result<ArrayRef> {let num_rows = batch.num_rows();let partition_points =self.evaluate_partition_points(num_rows, &self.partition_columns(batch)?)?;- let sort_partition_points =- self.evaluate_partition_points(num_rows, &self.sort_columns(batch)?)?;let values = self.evaluate_args(batch)?;++ let columns = self.sort_columns(batch)?;+ let array_refs: Vec<&ArrayRef> = columns.iter().map(|s| &s.values).collect();+ // Sort values, this will make the same partitions consecutive. Also, within the partition+ // range, values will be sorted.let results = partition_points.iter().map(|partition_range| {- let sort_partition_points =- find_ranges_in_range(partition_range, &sort_partition_points);let mut window_accumulators = self.create_accumulator()?;- sort_partition_points- .iter()- .map(|range| window_accumulators.scan_peers(&values, range))- .collect::<Result<Vec<_>>>()+ Ok(vec![window_accumulators.scan(+ &values,+ &array_refs,+ partition_range,+ )?])}).collect::<Result<Vec<Vec<ArrayRef>>>>()?.into_iter()@@ -98,39 +131,6 @@ impl AggregateWindowExpr {concat(&results).map_err(DataFusionError::ArrowError)}- fn group_based_evaluate(&self, _batch: &RecordBatch) -> Result<ArrayRef> {- Err(DataFusionError::NotImplemented(format!(- "Group based evaluation for {} is not yet implemented",- self.name()- )))- }-- fn row_based_evaluate(&self, _batch: &RecordBatch) -> Result<ArrayRef> {- Err(DataFusionError::NotImplemented(format!(- "Row based evaluation for {} is not yet implemented",- self.name()- )))- }-}--impl WindowExpr for AggregateWindowExpr {- /// Return a reference to Any that can be used for downcasting- fn as_any(&self) -> &dyn Any {- self- }-- fn name(&self) -> &str {- self.aggregate.name()- }-- fn field(&self) -> Result<Field> {- self.aggregate.field()- }-- fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {- self.aggregate.expressions()- }-fn partition_by(&self) -> &[Arc<dyn PhysicalExpr>] {&self.partition_by}@@ -138,30 +138,288 @@ impl WindowExpr for AggregateWindowExpr {fn order_by(&self) -> &[PhysicalSortExpr] {&self.order_by}+}- /// evaluate the window function values against the batch- fn evaluate(&self, batch: &RecordBatch) -> Result<ArrayRef> {- match self.evaluation_mode() {- WindowFrameUnits::Range => self.peer_based_evaluate(batch),- WindowFrameUnits::Rows => self.row_based_evaluate(batch),- WindowFrameUnits::Groups => self.group_based_evaluate(batch),+fn calculate_index_of_row<const BISECT_SIDE: bool, const SEARCH_SIDE: bool>(+ range_columns: &[ArrayRef],+ sort_options: &[SortOptions],+ idx: usize,+ delta: u64,+) -> Result<usize> {+ let current_row_values = range_columns+ .iter()+ .map(|col| ScalarValue::try_from_array(col, idx))+ .collect::<Result<Vec<ScalarValue>>>()?;+ let end_range = if delta == 0 {+ current_row_values+ } else {+ let is_descending: bool = sort_options+ .first()+ .ok_or_else(|| DataFusionError::Internal("Array is empty".to_string()))?+ .descending;++ current_row_values+ .iter()+ .map(|value| {+ if value.is_null() {+ return Ok(value.clone());+ };+ let offset = ScalarValue::try_from_value(&value.get_datatype(), delta)?;+ if SEARCH_SIDE == is_descending {+ // TODO: Handle positive overflows+ value.add(&offset)+ } else if value.is_unsigned() && value < &offset {+ ScalarValue::try_from_value(&value.get_datatype(), 0)+ } else {+ // TODO: Handle negative overflows+ value.sub(&offset)+ }+ })+ .collect::<Result<Vec<ScalarValue>>>()?+ };+ // `BISECT_SIDE` true means bisect_left, false means bisect_right+ bisect::<BISECT_SIDE>(range_columns, &end_range, sort_options)+}++/// We use start and end bounds to calculate current row's starting and ending range.+/// This function supports different modes, but we currently do not support window calculation for GROUPS inside window frames.+fn calculate_current_window(+ window_frame: &WindowFrame,+ range_columns: &[ArrayRef],+ sort_options: &[SortOptions],+ length: usize,+ idx: usize,+) -> Result<(usize, usize)> {+ match window_frame.units {+ WindowFrameUnits::Range => {+ let start = match window_frame.start_bound {+ // UNBOUNDED PRECEDING+ WindowFrameBound::Preceding(None) => Ok(0),+ WindowFrameBound::Preceding(Some(n)) => {+ calculate_index_of_row::<true, true>(+ range_columns,+ sort_options,+ idx,+ n,+ )+ }+ WindowFrameBound::CurrentRow => calculate_index_of_row::<true, true>(+ range_columns,+ sort_options,+ idx,+ 0,+ ),+ WindowFrameBound::Following(Some(n)) => {+ calculate_index_of_row::<true, false>(+ range_columns,+ sort_options,+ idx,+ n,+ )+ }+ // UNBOUNDED FOLLOWING+ WindowFrameBound::Following(None) => {+ Err(DataFusionError::Internal(format!(+ "Frame start cannot be UNBOUNDED FOLLOWING '{:?}'",+ window_frame+ )))+ }+ };+ let end = match window_frame.end_bound {+ // UNBOUNDED PRECEDING+ WindowFrameBound::Preceding(None) => {+ Err(DataFusionError::Internal(format!(+ "Frame end cannot be UNBOUNDED PRECEDING '{:?}'",+ window_frame+ )))+ }+ WindowFrameBound::Preceding(Some(n)) => {+ calculate_index_of_row::<false, true>(+ range_columns,+ sort_options,+ idx,+ n,+ )+ }+ WindowFrameBound::CurrentRow => calculate_index_of_row::<false, false>(+ range_columns,+ sort_options,+ idx,+ 0,+ ),+ WindowFrameBound::Following(Some(n)) => {+ calculate_index_of_row::<false, false>(+ range_columns,+ sort_options,+ idx,+ n,+ )+ }+ // UNBOUNDED FOLLOWING+ WindowFrameBound::Following(None) => Ok(length),+ };+ Ok((start?, end?))+ }+ WindowFrameUnits::Rows => {+ let start = match window_frame.start_bound {+ // UNBOUNDED PRECEDING+ WindowFrameBound::Preceding(None) => Ok(0),+ WindowFrameBound::Preceding(Some(n)) => {+ if idx >= n as usize {+ Ok(idx - n as usize)+ } else {+ Ok(0)+ }+ }+ WindowFrameBound::CurrentRow => Ok(idx),+ WindowFrameBound::Following(Some(n)) => Ok(min(idx + n as usize, length)),+ // UNBOUNDED FOLLOWING+ WindowFrameBound::Following(None) => {+ Err(DataFusionError::Internal(format!(+ "Frame start cannot be UNBOUNDED FOLLOWING '{:?}'",+ window_frame+ )))+ }+ };+ let end = match window_frame.end_bound {+ // UNBOUNDED PRECEDING+ WindowFrameBound::Preceding(None) => {+ Err(DataFusionError::Internal(format!(+ "Frame end cannot be UNBOUNDED PRECEDING '{:?}'",+ window_frame+ )))+ }+ WindowFrameBound::Preceding(Some(n)) => {+ if idx >= n as usize {+ Ok(idx - n as usize + 1)+ } else {+ Ok(0)+ }+ }+ WindowFrameBound::CurrentRow => Ok(idx + 1),+ WindowFrameBound::Following(Some(n)) => {+ Ok(min(idx + n as usize + 1, length))+ }+ // UNBOUNDED FOLLOWING+ WindowFrameBound::Following(None) => Ok(length),+ };+ Ok((start?, end?))}+ WindowFrameUnits::Groups => Err(DataFusionError::NotImplemented(+ "Window frame for groups is not implemented".to_string(),+ )),}}/// Aggregate window accumulator utilizes the accumulator from aggregation and do a accumulative sum-/// across evaluation arguments based on peer equivalences.+/// across evaluation arguments based on peer equivalences. It uses many information to calculate+/// correct running window.#[derive(Debug)]struct AggregateWindowAccumulator {accumulator: Box<dyn Accumulator>,+ window_frame: Option<WindowFrame>,+ partition_by: Vec<Arc<dyn PhysicalExpr>>,+ order_by: Vec<PhysicalSortExpr>,+ field: Field,}impl AggregateWindowAccumulator {- /// scan one peer group of values (as arguments to window function) given by the value_range- /// and return evaluation result that are of the same number of rows.- fn scan_peers(+ /// This function constructs a simple window frame with a single ORDER BY.+ fn implicit_order_by_window() -> WindowFrame {+ WindowFrame {+ units: WindowFrameUnits::Range,+ start_bound: WindowFrameBound::Preceding(None),+ end_bound: WindowFrameBound::Following(Some(0)),+ }+ }+ /// This function calculates the aggregation on all rows in `value_slice`.+ /// Returns an array of size `len`.+ fn calculate_whole_table(+ &mut self,+ value_slice: &[ArrayRef],+ length: usize,+ ) -> Result<ArrayRef> {+ self.accumulator.update_batch(value_slice)?;+ let value = self.accumulator.evaluate()?;+ Ok(value.to_array_of_size(length))+ }++ /// This function calculates the running window logic for the rows in `value_range` of `value_slice`.+ /// We maintain the accumulator state via `update_batch` and `retract_batch` functions.+ /// Note that not all aggregators implement `retract_batch` just yet.+ fn calculate_running_window(+ &mut self,+ value_slice: &[ArrayRef],+ order_bys: &[&ArrayRef],+ value_range: &Range<usize>,+ ) -> Result<ArrayRef> {+ // We iterate on each row to perform a running calculation.+ // First, cur_range is calculated, then it is compared with last_range.+ let length = value_range.end - value_range.start;+ let slice_order_columns = order_bys+ .iter()+ .map(|v| v.slice(value_range.start, length))+ .collect::<Vec<_>>();+ let sort_options: Vec<SortOptions> =+ self.order_by.iter().map(|o| o.options).collect();++ let updated_zero_offset_value_range = Range {+ start: 0,+ end: length,+ };+ let mut row_wise_results: Vec<ScalarValue> = vec![];+ let mut last_range: (usize, usize) = (+ updated_zero_offset_value_range.start,+ updated_zero_offset_value_range.start,+ );++ for i in 0..length {+ let window_frame = self.window_frame.as_ref().ok_or_else(|| {+ DataFusionError::Internal(+ "Window frame cannot be empty to calculate window ranges".to_string(),+ )+ })?;+ let cur_range = calculate_current_window(+ window_frame,+ &slice_order_columns,+ &sort_options,+ length,+ i,+ )?;++ if cur_range.0 == cur_range.1 {+ // We produce None if the window is empty.+ row_wise_results.push(ScalarValue::try_from(self.field.data_type())?)+ } else {+ // Accumulate any new rows that have entered the window:+ let update_bound = cur_range.1 - last_range.1;+ if update_bound > 0 {+ let update: Vec<ArrayRef> = value_slice+ .iter()+ .map(|v| v.slice(last_range.1, update_bound))+ .collect();+ self.accumulator.update_batch(&update)?+ }+ // Remove rows that have now left the window:+ let retract_bound = cur_range.0 - last_range.0;+ if retract_bound > 0 {+ let retract: Vec<ArrayRef> = value_slice+ .iter()+ .map(|v| v.slice(last_range.0, retract_bound))+ .collect();+ self.accumulator.retract_batch(&retract)?+ }+ row_wise_results.push(self.accumulator.evaluate()?);+ }+ last_range = cur_range;+ }+ ScalarValue::iter_to_array(row_wise_results.into_iter())+ }++ fn scan(&mut self,values: &[ArrayRef],+ order_bys: &[&ArrayRef],value_range: &Range<usize>,) -> Result<ArrayRef> {if value_range.is_empty() {@@ -169,13 +427,48 @@ impl AggregateWindowAccumulator {"Value range cannot be empty".to_owned(),));}- let len = value_range.end - value_range.start;- let values = values+ let length = value_range.end - value_range.start;+ let value_slice = values.iter()- .map(|v| v.slice(value_range.start, len))+ .map(|v| v.slice(value_range.start, length)).collect::<Vec<_>>();- self.accumulator.update_batch(&values)?;- let value = self.accumulator.evaluate()?;- Ok(value.to_array_of_size(len))+ let order_columns = &order_bys[self.partition_by.len()..order_bys.len()].to_vec();+ match (order_columns.len(), self.window_frame) {+ (0, None) => {+ // OVER () case+ self.calculate_whole_table(&value_slice, length)+ }+ (_n, None) => {+ // OVER (ORDER BY a) case+ // We create an implicit window for ORDER BY.+ self.window_frame =+ Some(AggregateWindowAccumulator::implicit_order_by_window());++ self.calculate_running_window(&value_slice, order_columns, value_range)+ }+ (0, Some(frame)) => {+ match frame.units {+ WindowFrameUnits::Range => {+ // OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) case+ self.calculate_whole_table(&value_slice, length)+ }+ WindowFrameUnits::Rows => {+ // OVER (ROWS BETWEEN X PRECEDING AND Y FOLLOWING) case+ self.calculate_running_window(+ &value_slice,+ order_bys,+ value_range,+ )+ }+ WindowFrameUnits::Groups => Err(DataFusionError::NotImplemented(+ "Window frame for groups is not implemented".to_string(),+ )),+ }+ }+ // OVER (ORDER BY a ROWS/RANGE BETWEEN X PRECEDING AND Y FOLLOWING) case+ (_n, _) => {+ self.calculate_running_window(&value_slice, order_columns, value_range)+ }+ }}}diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rsindex a740d1af8f..2026411752 100644--- a/datafusion/sql/src/planner.rs+++ b/datafusion/sql/src/planner.rs@@ -4402,16 +4402,6 @@ mod tests {quick_test(sql, expected);}- #[test]- fn over_order_by_with_window_frame_range_value_check() {- let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id RANGE 3 PRECEDING) from orders";- let err = logical_plan(sql).expect_err("query should have failed");- assert_eq!(- "NotImplemented(\"With WindowFrameUnits=RANGE, the bound cannot be 3 PRECEDING or FOLLOWING at the moment\")",- format!("{:?}", err)- );- }-#[test]fn over_order_by_with_window_frame_range_order_by_check() {let sql =diff --git a/integration-tests/sqls/simple_ordered_row.sql b/integration-tests/sqls/simple_ordered_row.sqlnew file mode 100644index 0000000000..d91dc428ea--- /dev/null+++ b/integration-tests/sqls/simple_ordered_row.sql@@ -0,0 +1,26 @@+-- Licensed to the Apache Software Foundation (ASF) under one+-- or more contributor license agreements. See the NOTICE file+-- distributed with this work for additional information+-- regarding copyright ownership. The ASF licenses this file+-- to you under the Apache License, Version 2.0 (the+-- "License"); you may not use this file except in compliance+-- with the License. You may obtain a copy of the License at++-- http://www.apache.org/licenses/LICENSE-2.0++-- Unless required by applicable law or agreed to in writing, software+-- distributed under the License is distributed on an "AS IS" BASIS,+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+-- See the License for the specific language governing permissions and+-- limitations under the License.++SELECT+SUM(c5) OVER(ORDER BY c13 ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) as summation2,+SUM(c2) OVER(PARTITION BY c5 ORDER BY c13 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation10,+SUM(c2) OVER(PARTITION BY c1 ORDER BY c13 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation14,+SUM(c5) OVER(ORDER BY c5 ROWS BETWEEN 3 PRECEDING AND UNBOUNDED FOLLOWING) as summation5,+SUM(c5) OVER(ORDER BY c5 ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING) as summation6,+SUM(c2) OVER(PARTITION BY c5, c7, c9 ORDER BY c13, c5 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation20,+SUM(c2) OVER(PARTITION BY c5 ORDER BY c13, c5 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation21+FROM test+ORDER BY c9;diff --git a/integration-tests/sqls/simple_row_past_future.sql b/integration-tests/sqls/simple_row_past_future.sqlnew file mode 100644index 0000000000..1a8580a714--- /dev/null+++ b/integration-tests/sqls/simple_row_past_future.sql@@ -0,0 +1,24 @@+-- Licensed to the Apache Software Foundation (ASF) under one+-- or more contributor license agreements. See the NOTICE file+-- distributed with this work for additional information+-- regarding copyright ownership. The ASF licenses this file+-- to you under the Apache License, Version 2.0 (the+-- "License"); you may not use this file except in compliance+-- with the License. You may obtain a copy of the License at++-- http://www.apache.org/licenses/LICENSE-2.0++-- Unless required by applicable law or agreed to in writing, software+-- distributed under the License is distributed on an "AS IS" BASIS,+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+-- See the License for the specific language governing permissions and+-- limitations under the License.++SELECT+ SUM(c2) OVER(ORDER BY c5 ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as summation1,+ SUM(c2) OVER(ORDER BY c5 ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING) as summation2,+ SUM(c2) OVER(ORDER BY c5 ROWS BETWEEN 2 FOLLOWING AND UNBOUNDED FOLLOWING) as summation3,+ SUM(c2) OVER(ORDER BY c5 RANGE BETWEEN 2 FOLLOWING AND UNBOUNDED FOLLOWING) as summation4,+ SUM(c2) OVER(ORDER BY c5 RANGE BETWEEN 1 PRECEDING AND 1 PRECEDING) as summation5+FROM test+ORDER BY c9;diff --git a/integration-tests/sqls/simple_window_range.sql b/integration-tests/sqls/simple_window_range.sqlnew file mode 100644index 0000000000..98b5b18a5c--- /dev/null+++ b/integration-tests/sqls/simple_window_range.sql@@ -0,0 +1,29 @@+-- Licensed to the Apache Software Foundation (ASF) under one+-- or more contributor license agreements. See the NOTICE file+-- distributed with this work for additional information+-- regarding copyright ownership. The ASF licenses this file+-- to you under the Apache License, Version 2.0 (the+-- "License"); you may not use this file except in compliance+-- with the License. You may obtain a copy of the License at++-- http://www.apache.org/licenses/LICENSE-2.0++-- Unless required by applicable law or agreed to in writing, software+-- distributed under the License is distributed on an "AS IS" BASIS,+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+-- See the License for the specific language governing permissions and+-- limitations under the License.++SELECT+ SUM(c5) OVER(ORDER BY c4 RANGE BETWEEN 3 PRECEDING AND 1 FOLLOWING) as summation2,+ SUM(c4) OVER(ORDER BY c3 RANGE 3 PRECEDING) as summation3,+ SUM(c4) OVER(ORDER BY c5 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation6,+ SUM(c4) OVER(ORDER BY c5 RANGE UNBOUNDED PRECEDING) as summation7,+ SUM(c2) OVER(PARTITION BY c5 ORDER BY c5 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation10,+ SUM(c4) OVER(PARTITION BY c1 ORDER BY c5 RANGE UNBOUNDED PRECEDING) as summation11,+ SUM(c2) OVER(PARTITION BY c1 ORDER BY c5 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation14,+ SUM(c4) OVER(PARTITION BY c5 ORDER BY c5 RANGE UNBOUNDED PRECEDING) as summation15,+ SUM(c2) OVER(PARTITION BY c5, c7, c9 ORDER BY c5 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation20,+ SUM(c2) OVER(PARTITION BY c5 ORDER BY c5 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as summation21+FROM test+ORDER BY c9;diff --git a/integration-tests/test_psql_parity.py b/integration-tests/test_psql_parity.pyindex 3629645a62..be197eb612 100644--- a/integration-tests/test_psql_parity.py+++ b/integration-tests/test_psql_parity.py@@ -82,10 +82,11 @@ test_files = set(root.glob("*.sql"))class TestPsqlParity:def test_tests_count(self):- assert len(test_files) == 22, "tests are missed"+ assert len(test_files) == 25, "tests are missed"- @pytest.mark.parametrize("fname", test_files)+ @pytest.mark.parametrize("fname", test_files, ids=str)def test_sql_file(self, fname):datafusion_output = pd.read_csv(io.BytesIO(generate_csv_from_datafusion(fname)))psql_output = pd.read_csv(io.BytesIO(generate_csv_from_psql(fname)))- np.testing.assert_allclose(datafusion_output, psql_output, equal_nan=True)+ np.testing.assert_allclose(datafusion_output, psql_output, equal_nan=True, verbose=True)+