Flatten an inline-or-named `WindowSpec`, merging in any referenced window per the SQL inheritance rules (see module docs).
(
spec: &'a ast::WindowSpec,
named: &HashMap<String, &'a ast::NamedWindowExpr>,
seen: &mut Vec<String>,
)
| 70 | /// Flatten an inline-or-named `WindowSpec`, merging in any referenced window |
| 71 | /// per the SQL inheritance rules (see module docs). |
| 72 | pub(super) fn flatten_window_spec<'a>( |
| 73 | spec: &'a ast::WindowSpec, |
| 74 | named: &HashMap<String, &'a ast::NamedWindowExpr>, |
| 75 | seen: &mut Vec<String>, |
| 76 | ) -> Result<FlatWindow> { |
| 77 | let Some(ref_ident) = &spec.window_name else { |
| 78 | return Ok(FlatWindow { |
| 79 | partition_by: spec.partition_by.clone(), |
| 80 | order_by: spec.order_by.clone(), |
| 81 | frame: spec.window_frame.clone(), |
| 82 | }); |
| 83 | }; |
| 84 | let ref_name = normalize_ident(ref_ident); |
| 85 | if seen.contains(&ref_name) { |
| 86 | return Err(SqlError::Unsupported { |
| 87 | detail: format!("circular WINDOW definition involving '{ref_name}'"), |
| 88 | }); |
| 89 | } |
| 90 | if !spec.partition_by.is_empty() { |
| 91 | return Err(SqlError::Unsupported { |
| 92 | detail: format!( |
| 93 | "window referencing '{ref_name}' cannot also declare its own PARTITION BY" |
| 94 | ), |
| 95 | }); |
| 96 | } |
| 97 | seen.push(ref_name.clone()); |
| 98 | let base_spec = resolve_named_def(&ref_name, named, seen)?; |
| 99 | let base = flatten_window_spec(base_spec, named, seen)?; |
| 100 | if base.frame.is_some() { |
| 101 | return Err(SqlError::Unsupported { |
| 102 | detail: format!( |
| 103 | "window '{ref_name}' declares a frame clause and cannot be referenced by another window" |
| 104 | ), |
| 105 | }); |
| 106 | } |
| 107 | let order_by = if spec.order_by.is_empty() { |
| 108 | base.order_by |
| 109 | } else if base.order_by.is_empty() { |
| 110 | spec.order_by.clone() |
| 111 | } else { |
| 112 | return Err(SqlError::Unsupported { |
| 113 | detail: format!( |
| 114 | "window referencing '{ref_name}' cannot add ORDER BY because the referenced window already has one" |
| 115 | ), |
| 116 | }); |
| 117 | }; |
| 118 | Ok(FlatWindow { |
| 119 | partition_by: base.partition_by, |
| 120 | order_by, |
| 121 | frame: spec.window_frame.clone(), |
| 122 | }) |
| 123 | } |
no test coverage detected