notfilippo commented on code in PR #13059:
URL: https://github.com/apache/datafusion/pull/13059#discussion_r1810628066
##########
datafusion/expr/src/window_frame.rs:
##########
@@ -479,8 +498,123 @@ mod tests {
ast::Expr::Value(ast::Value::Number("1".to_string(), false)),
)))),
};
- let result = WindowFrame::try_from(window_frame);
- assert!(result.is_ok());
+
+ let window_frame = WindowFrame::try_from(window_frame)?;
+ assert_eq!(window_frame.units, WindowFrameUnits::Rows);
+ assert_eq!(
+ window_frame.start_bound,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(2)))
+ );
+ assert_eq!(
+ window_frame.end_bound,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(1)))
+ );
+
+ Ok(())
+ }
+
+ macro_rules! test_bound {
+ ($unit:ident, $value:expr, $expected:expr) => {
+ let preceding = WindowFrameBound::try_parse(
+ ast::WindowFrameBound::Preceding($value),
+ &ast::WindowFrameUnits::$unit,
+ )?;
+ assert_eq!(preceding, WindowFrameBound::Preceding($expected));
+ let following = WindowFrameBound::try_parse(
+ ast::WindowFrameBound::Following($value),
+ &ast::WindowFrameUnits::$unit,
+ )?;
+ assert_eq!(following, WindowFrameBound::Following($expected));
+ };
+ }
+
+ macro_rules! test_bound_err {
+ ($unit:ident, $value:expr, $expected:expr) => {
+ let err = WindowFrameBound::try_parse(
+ ast::WindowFrameBound::Preceding($value),
+ &ast::WindowFrameUnits::$unit,
+ )
+ .unwrap_err();
+ assert_eq!(err.strip_backtrace(), $expected);
+ let err = WindowFrameBound::try_parse(
+ ast::WindowFrameBound::Following($value),
+ &ast::WindowFrameUnits::$unit,
+ )
+ .unwrap_err();
+ assert_eq!(err.strip_backtrace(), $expected);
+ };
+ }
+
+ #[test]
+ fn test_window_frame_bound_creation() -> Result<()> {
+ // Unbounded
+ test_bound!(Rows, None, ScalarValue::Null);
+ test_bound!(Groups, None, ScalarValue::Null);
+ test_bound!(Range, None, ScalarValue::Null);
+
+ // Numeric (Number)
+ let number = Some(Box::new(ast::Expr::Value(ast::Value::Number(
+ "42".to_string(),
+ false,
+ ))));
+ test_bound!(Rows, number.clone(), ScalarValue::UInt64(Some(42)));
+ test_bound!(Groups, number.clone(), ScalarValue::UInt64(Some(42)));
+ test_bound!(
+ Range,
+ number.clone(),
+ ScalarValue::Utf8(Some("42".to_string()))
+ );
+
+ // Numeric (SingleQuotedString)
+ let number =
Some(Box::new(ast::Expr::Value(ast::Value::SingleQuotedString(
+ "42".to_string(),
+ ))));
+ test_bound!(Rows, number.clone(), ScalarValue::UInt64(Some(42)));
+ test_bound!(Groups, number.clone(), ScalarValue::UInt64(Some(42)));
+ test_bound!(
+ Range,
+ number.clone(),
+ ScalarValue::Utf8(Some("42".to_string()))
+ );
+
+ // Non-numeric (SingleQuotedString)
+ let non_numeric = Some(Box::new(ast::Expr::Value(
+ ast::Value::SingleQuotedString("false".to_string()),
+ )));
+ test_bound_err!(
+ Rows,
+ non_numeric.clone(),
+ "Arrow error: Cast error: Cannot cast string 'false' to value of
UInt64 type"
+ );
+ test_bound_err!(
+ Groups,
+ non_numeric.clone(),
+ "Arrow error: Cast error: Cannot cast string 'false' to value of
UInt64 type"
+ );
+ test_bound!(
+ Range,
+ non_numeric.clone(),
+ ScalarValue::Utf8(Some("false".to_string()))
+ );
Review Comment:
Not really sure if this is expected but essentially I've tried to keep Range
with the original behaviour, only changing Rows and Groups since we know they
are certainly UInt64
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]