# Aims and scope The aim here is to amortize the overhead of the MIR window function pattern (see `window_func_applied_to`) by fusing groups of window function calls such that each group can be performed by one instance of the window function MIR pattern. For now, we fuse only value window function calls and window aggregations. (We probably won't need to fuse scalar window functions for a long
(
root: &mut HirRelationExpr,
_context: &crate::plan::lowering::Context,
)
| 405 | /// column references in the other direction, i.e., references in other expressions that refer to |
| 406 | /// columns in the group.) |
| 407 | pub fn fuse_window_functions( |
| 408 | root: &mut HirRelationExpr, |
| 409 | _context: &crate::plan::lowering::Context, |
| 410 | ) -> Result<(), RecursionLimitError> { |
| 411 | /// Those options of a window function call that are relevant for fusion. |
| 412 | #[derive(PartialEq, Eq)] |
| 413 | enum WindowFuncCallOptions { |
| 414 | Value(ValueWindowFuncCallOptions), |
| 415 | Agg(AggregateWindowFuncCallOptions), |
| 416 | } |
| 417 | #[derive(PartialEq, Eq)] |
| 418 | struct ValueWindowFuncCallOptions { |
| 419 | partition_by: Vec<HirScalarExpr>, |
| 420 | outer_order_by: Vec<HirScalarExpr>, |
| 421 | inner_order_by: Vec<ColumnOrder>, |
| 422 | window_frame: WindowFrame, |
| 423 | ignore_nulls: bool, |
| 424 | } |
| 425 | #[derive(PartialEq, Eq)] |
| 426 | struct AggregateWindowFuncCallOptions { |
| 427 | partition_by: Vec<HirScalarExpr>, |
| 428 | outer_order_by: Vec<HirScalarExpr>, |
| 429 | inner_order_by: Vec<ColumnOrder>, |
| 430 | window_frame: WindowFrame, |
| 431 | distinct: bool, |
| 432 | } |
| 433 | |
| 434 | /// Helper function to extract the above options. |
| 435 | fn extract_options(call: &HirScalarExpr) -> WindowFuncCallOptions { |
| 436 | match call { |
| 437 | HirScalarExpr::Windowing( |
| 438 | WindowExpr { |
| 439 | func: |
| 440 | WindowExprType::Value(ValueWindowExpr { |
| 441 | order_by: inner_order_by, |
| 442 | window_frame, |
| 443 | ignore_nulls, |
| 444 | func: _, |
| 445 | args: _, |
| 446 | }), |
| 447 | partition_by, |
| 448 | order_by: outer_order_by, |
| 449 | }, |
| 450 | _name, |
| 451 | ) => WindowFuncCallOptions::Value(ValueWindowFuncCallOptions { |
| 452 | partition_by: partition_by.clone(), |
| 453 | outer_order_by: outer_order_by.clone(), |
| 454 | inner_order_by: inner_order_by.clone(), |
| 455 | window_frame: window_frame.clone(), |
| 456 | ignore_nulls: ignore_nulls.clone(), |
| 457 | }), |
| 458 | HirScalarExpr::Windowing( |
| 459 | WindowExpr { |
| 460 | func: |
| 461 | WindowExprType::Aggregate(AggregateWindowExpr { |
| 462 | aggregate_expr: |
| 463 | AggregateExpr { |
| 464 | distinct, |
no test coverage detected