timsaucer opened a new issue, #11981:
URL: https://github.com/apache/datafusion/issues/11981

   ### Describe the bug
   
   When using an aggregate function as a window function, the `order_by` is not 
respected. I've tried a few permutations of setting this value on the 
`WindowFunction` and setting it via the expression function builder.
   
   ### To Reproduce
   
   Note: In the below snippet I tried with and without calling `.order_by()` 
and with and without setting `order_by` in the `WindowFunction` struct.
   
   When trying to set it on the expression function builder you get an error
   `Error: SchemaError(FieldNotFound { field: Column { relation: None, name: 
"a" }, valid_fields: [] }, Some(""))`
   
   
   ```
       let schema = Schema::new(vec![
           Field::new("a", DataType::Int32, true),
           Field::new("b", DataType::Int32, true),
       ]);
   
       let batch = RecordBatch::try_new(
           Arc::new(schema.clone()),
           vec![
               Arc::new(Int32Array::from(vec![1, 5, 3, 2, 4])),
               Arc::new(Int32Array::from(vec![Some(1), None, Some(3), None, 
Some(4)])),
           ],
       )?;
   
       let ctx = SessionContext::new();
   
       let provider = MemTable::try_new(Arc::new(schema), vec![vec![batch]])?;
       ctx.register_table("t", Arc::new(provider))?;
   
       let df = ctx.table("t").await?;
   
       df.clone().show().await?;
   
       let mut w = WindowFunction::new(
           WindowFunctionDefinition::AggregateUDF(last_value_udaf()),
           vec![col("b")],
       );
       w.order_by = vec![col("a").sort(true, true)];
   
       let func = Expr::WindowFunction(w)
           .null_treatment(NullTreatment::IgnoreNulls)
           // .order_by(vec![col("a").sort(true, true)])
           .window_frame(WindowFrame::new_bounds(WindowFrameUnits::Rows, 
WindowFrameBound::Preceding(ScalarValue::UInt32(None)), 
WindowFrameBound::CurrentRow))
           .build()?
           .alias("last_val");
   
   
       df.select(vec![col("a"), col("b"), func])?.show().await?;
   
       // Expectation
       // +---+---+----------+
       // | a | b | last_val |
       // +---+---+----------+
       // | 1 | 1 | 1        |
       // | 2 |   | 1        |
       // | 3 | 3 | 3        |
       // | 4 | 4 | 4        |
       // | 5 |   | 4        |
       // +---+---+----------+
   
       // Actual
       // +---+---+----------+
       // | a | b | last_val |
       // +---+---+----------+
       // | 1 | 1 | 1        |
       // | 5 |   | 1        |
       // | 3 | 3 | 3        |
       // | 2 |   | 3        |
       // | 4 | 4 | 4        |
       // +---+---+----------+
   ```
   
   ### Expected behavior
   
   In the above example, we should be able to set a sort order to get the 
`last_value` as a window function.
   
   ### Additional context
   
   _No response_


-- 
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]

Reply via email to