comphead commented on code in PR #22134:
URL: https://github.com/apache/datafusion/pull/22134#discussion_r3229583778
##########
datafusion/functions-window/src/lead_lag.rs:
##########
@@ -691,12 +950,392 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_lag_with_column_default() -> Result<()> {
+ let salary: ArrayRef =
+ Arc::new(Int32Array::from(vec![1, -2, 3, -4, 5, -6, 7, 8]));
+ let bonus: ArrayRef = Arc::new(Int32Array::from(vec![
+ 100, 200, 300, 400, 500, 600, 700, 800,
+ ]));
+
+ let batch =
+ make_batch(vec![("salary", salary.clone()), ("bonus",
bonus.clone())]);
+
+ let salary_expr = Arc::new(Column::new("salary", 0)) as Arc<dyn
PhysicalExpr>;
+ let offset_expr =
+ Arc::new(Literal::new(ScalarValue::Int64(Some(1)))) as Arc<dyn
PhysicalExpr>;
+ let default_expr = Arc::new(Column::new("bonus", 1)) as Arc<dyn
PhysicalExpr>;
+
+ let input_exprs = [salary_expr, offset_expr, default_expr];
+ let input_fields: Vec<FieldRef> = vec![
+ Field::new("salary", DataType::Int32, true).into(),
+ Field::new("offset", DataType::Int64, true).into(),
+ Field::new("bonus", DataType::Int32, true).into(),
+ ];
+
+ let result = evaluate_window_on_batch(
+ &WindowShift::lag(),
+ &input_exprs,
+ &input_fields,
+ false,
+ false,
+ &batch,
+ )?;
+
+ let result = as_int32_array(&result)?;
+ let expected = [
+ Some(100),
+ Some(1),
+ Some(-2),
+ Some(3),
+ Some(-4),
+ Some(5),
+ Some(-6),
+ Some(7),
+ ]
+ .iter()
+ .collect::<Int32Array>();
+
+ assert_eq!(result, &expected);
+ Ok(())
+ }
+
+ #[test]
+ fn test_lead_with_column_default() -> Result<()> {
+ let salary: ArrayRef =
+ Arc::new(Int32Array::from(vec![1, -2, 3, -4, 5, -6, 7, 8]));
+ let bonus: ArrayRef = Arc::new(Int32Array::from(vec![
+ 100, 200, 300, 400, 500, 600, 700, 800,
+ ]));
+
+ let batch =
+ make_batch(vec![("salary", salary.clone()), ("bonus",
bonus.clone())]);
+
+ let salary_expr = Arc::new(Column::new("salary", 0)) as Arc<dyn
PhysicalExpr>;
+ let offset_expr =
+ Arc::new(Literal::new(ScalarValue::Int64(Some(1)))) as Arc<dyn
PhysicalExpr>;
+ let default_expr = Arc::new(Column::new("bonus", 1)) as Arc<dyn
PhysicalExpr>;
+
+ let input_exprs = [salary_expr, offset_expr, default_expr];
+ let input_fields: Vec<FieldRef> = vec![
+ Field::new("salary", DataType::Int32, true).into(),
+ Field::new("offset", DataType::Int64, true).into(),
+ Field::new("bonus", DataType::Int32, true).into(),
+ ];
+
+ let result = evaluate_window_on_batch(
+ &WindowShift::lead(),
+ &input_exprs,
+ &input_fields,
+ false,
+ false,
+ &batch,
+ )?;
+
+ let result = as_int32_array(&result)?;
+ let expected = [
+ Some(-2),
+ Some(3),
+ Some(-4),
+ Some(5),
+ Some(-6),
+ Some(7),
+ Some(8),
+ Some(800),
+ ]
+ .iter()
+ .collect::<Int32Array>();
+
+ assert_eq!(result, &expected);
+ Ok(())
+ }
+
+ #[test]
+ fn test_lag_with_expression_offset_2() -> Result<()> {
+ let salary: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5,
6, 7, 8]));
+ let bonus: ArrayRef =
+ Arc::new(Int32Array::from(vec![10, 20, 30, 40, 50, 60, 70, 80]));
+
+ let batch =
+ make_batch(vec![("salary", salary.clone()), ("bonus",
bonus.clone())]);
+
+ let salary_expr = Arc::new(Column::new("salary", 0)) as Arc<dyn
PhysicalExpr>;
+ let offset_expr =
+ Arc::new(Literal::new(ScalarValue::Int64(Some(2)))) as Arc<dyn
PhysicalExpr>;
+ let default_expr = Arc::new(Column::new("bonus", 1)) as Arc<dyn
PhysicalExpr>;
+
+ let input_exprs = [salary_expr, offset_expr, default_expr];
+ let input_fields: Vec<FieldRef> = vec![
+ Field::new("salary", DataType::Int32, true).into(),
+ Field::new("offset", DataType::Int64, true).into(),
+ Field::new("bonus", DataType::Int32, true).into(),
+ ];
+
+ let result = evaluate_window_on_batch(
+ &WindowShift::lag(),
+ &input_exprs,
+ &input_fields,
+ false,
+ false,
+ &batch,
+ )?;
+
+ let result = as_int32_array(&result)?;
+ let expected = [
+ Some(10),
+ Some(20),
+ Some(1),
+ Some(2),
+ Some(3),
+ Some(4),
+ Some(5),
+ Some(6),
+ ]
+ .iter()
+ .collect::<Int32Array>();
+
+ assert_eq!(result, &expected);
+ Ok(())
+ }
+
+ #[test]
+ fn test_lead_with_offset_3() -> Result<()> {
+ let salary: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5,
6, 7, 8]));
+ let bonus: ArrayRef =
+ Arc::new(Int32Array::from(vec![10, 20, 30, 40, 50, 60, 70, 80]));
+
+ let batch =
+ make_batch(vec![("salary", salary.clone()), ("bonus",
bonus.clone())]);
+
+ let salary_expr = Arc::new(Column::new("salary", 0)) as Arc<dyn
PhysicalExpr>;
+ let offset_expr =
+ Arc::new(Literal::new(ScalarValue::Int64(Some(3)))) as Arc<dyn
PhysicalExpr>;
+ let default_expr = Arc::new(Column::new("bonus", 1)) as Arc<dyn
PhysicalExpr>;
+
+ let input_exprs = [salary_expr, offset_expr, default_expr];
+ let input_fields: Vec<FieldRef> = vec![
+ Field::new("salary", DataType::Int32, true).into(),
+ Field::new("offset", DataType::Int64, true).into(),
+ Field::new("bonus", DataType::Int32, true).into(),
+ ];
+
+ let result = evaluate_window_on_batch(
+ &WindowShift::lead(),
+ &input_exprs,
+ &input_fields,
+ false,
+ false,
+ &batch,
+ )?;
+
+ let result = as_int32_array(&result)?;
+ let expected = [
+ Some(4),
+ Some(5),
+ Some(6),
+ Some(7),
+ Some(8),
+ Some(60),
+ Some(70),
+ Some(80),
+ ]
+ .iter()
+ .collect::<Int32Array>();
+
+ assert_eq!(result, &expected);
+ Ok(())
+ }
+
+ #[test]
+ fn test_lag_with_null_data_and_expression_default() -> Result<()> {
+ let salary: ArrayRef = Arc::new(Int32Array::from(vec![
+ Some(1),
+ None,
+ Some(3),
+ None,
+ Some(5),
+ Some(6),
+ None,
+ Some(8),
+ ]));
+ let bonus: ArrayRef = Arc::new(Int32Array::from(vec![
+ Some(10),
+ Some(20),
+ Some(30),
+ Some(40),
+ Some(50),
+ Some(60),
+ Some(70),
+ Some(80),
+ ]));
+
+ let batch =
+ make_batch(vec![("salary", salary.clone()), ("bonus",
bonus.clone())]);
+
+ let salary_expr = Arc::new(Column::new("salary", 0)) as Arc<dyn
PhysicalExpr>;
+ let offset_expr =
+ Arc::new(Literal::new(ScalarValue::Int64(Some(1)))) as Arc<dyn
PhysicalExpr>;
+ let default_expr = Arc::new(Column::new("bonus", 1)) as Arc<dyn
PhysicalExpr>;
+
+ let input_exprs = [salary_expr, offset_expr, default_expr];
+ let input_fields: Vec<FieldRef> = vec![
+ Field::new("salary", DataType::Int32, true).into(),
+ Field::new("offset", DataType::Int64, true).into(),
+ Field::new("bonus", DataType::Int32, true).into(),
+ ];
+
+ let result = evaluate_window_on_batch(
+ &WindowShift::lag(),
+ &input_exprs,
+ &input_fields,
+ false,
+ false,
+ &batch,
+ )?;
+
+ let result = as_int32_array(&result)?;
+ let expected = [
+ Some(10),
+ Some(1),
+ None,
+ Some(3),
+ None,
+ Some(5),
+ Some(6),
+ None,
+ ]
+ .iter()
+ .collect::<Int32Array>();
+
+ assert_eq!(result, &expected);
+ Ok(())
+ }
+
+ #[test]
+ fn test_lag_ignore_nulls_with_expression_default() -> Result<()> {
+ let salary: ArrayRef = Arc::new(Int32Array::from(vec![
+ Some(1),
+ None,
+ Some(3),
+ None,
+ Some(5),
+ Some(6),
+ None,
+ Some(8),
+ ]));
+ let bonus: ArrayRef = Arc::new(Int32Array::from(vec![
+ Some(10),
+ Some(20),
+ Some(30),
+ Some(40),
+ Some(50),
+ Some(60),
+ Some(70),
+ Some(80),
+ ]));
+
+ let batch =
+ make_batch(vec![("salary", salary.clone()), ("bonus",
bonus.clone())]);
+
+ let salary_expr = Arc::new(Column::new("salary", 0)) as Arc<dyn
PhysicalExpr>;
+ let offset_expr =
+ Arc::new(Literal::new(ScalarValue::Int64(Some(1)))) as Arc<dyn
PhysicalExpr>;
+ let default_expr = Arc::new(Column::new("bonus", 1)) as Arc<dyn
PhysicalExpr>;
+
+ let input_exprs = [salary_expr, offset_expr, default_expr];
+ let input_fields: Vec<FieldRef> = vec![
+ Field::new("salary", DataType::Int32, true).into(),
+ Field::new("offset", DataType::Int64, true).into(),
+ Field::new("bonus", DataType::Int32, true).into(),
+ ];
+
+ let result = evaluate_window_on_batch(
+ &WindowShift::lag(),
+ &input_exprs,
+ &input_fields,
+ false,
+ true,
+ &batch,
+ )?;
+
+ let result = as_int32_array(&result)?;
+ let expected = [
+ Some(10),
+ Some(1),
+ Some(1),
+ Some(3),
+ Some(3),
+ Some(5),
+ Some(6),
+ Some(6),
+ ]
+ .iter()
+ .collect::<Int32Array>();
+
+ assert_eq!(result, &expected);
+ Ok(())
+ }
Review Comment:
please have tests in `.slt`
--
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]