|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +use std::collections::HashSet; |
| 16 | +use std::sync::Arc; |
| 17 | + |
15 | 18 | use databend_common_ast::ast::MatchOperation; |
16 | 19 | use databend_common_ast::ast::MatchedClause; |
17 | 20 | use databend_common_ast::ast::MutationUpdateExpr; |
18 | 21 | use databend_common_ast::ast::TableReference; |
19 | 22 | use databend_common_ast::ast::UpdateStmt; |
20 | 23 | use databend_common_exception::Result; |
21 | 24 |
|
| 25 | +use crate::binder::aggregate::AggregateRewriter; |
22 | 26 | use crate::binder::bind_mutation::bind::Mutation; |
23 | 27 | use crate::binder::bind_mutation::bind::MutationStrategy; |
24 | 28 | use crate::binder::bind_mutation::mutation_expression::MutationExpression; |
25 | 29 | use crate::binder::util::TableIdentifier; |
26 | 30 | use crate::binder::Binder; |
| 31 | +use crate::optimizer::SExpr; |
| 32 | +use crate::plans::AggregateFunction; |
| 33 | +use crate::plans::BoundColumnRef; |
27 | 34 | use crate::plans::Plan; |
| 35 | +use crate::plans::RelOperator; |
| 36 | +use crate::plans::ScalarItem; |
| 37 | +use crate::plans::VisitorMut; |
28 | 38 | use crate::BindContext; |
| 39 | +use crate::ScalarExpr; |
29 | 40 |
|
30 | 41 | impl Binder { |
31 | 42 | #[async_backtrace::framed] |
@@ -93,6 +104,192 @@ impl Binder { |
93 | 104 | unmatched_clauses: vec![], |
94 | 105 | }; |
95 | 106 |
|
96 | | - self.bind_mutation(bind_context, mutation).await |
| 107 | + let plan = self.bind_mutation(bind_context, mutation).await?; |
| 108 | + |
| 109 | + let settings = self.ctx.get_settings(); |
| 110 | + if settings.get_error_on_nondeterministic_update()? { |
| 111 | + Ok(plan) |
| 112 | + } else { |
| 113 | + self.rewrite_nondeterministic_update(plan) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + fn rewrite_nondeterministic_update(&mut self, plan: Plan) -> Result<Plan> { |
| 118 | + let Plan::DataMutation { box s_expr, .. } = &plan else { |
| 119 | + return Ok(plan); |
| 120 | + }; |
| 121 | + let RelOperator::Mutation(mutation) = &*s_expr.plan else { |
| 122 | + return Ok(plan); |
| 123 | + }; |
| 124 | + let filter_expr = &s_expr.children[0]; |
| 125 | + let RelOperator::Filter(_) = &*filter_expr.plan else { |
| 126 | + return Ok(plan); |
| 127 | + }; |
| 128 | + let input = &filter_expr.children[0]; |
| 129 | + let RelOperator::Join(_) = &*input.plan else { |
| 130 | + return Ok(plan); |
| 131 | + }; |
| 132 | + |
| 133 | + let mut mutation = mutation.clone(); |
| 134 | + |
| 135 | + let row_id = mutation |
| 136 | + .bind_context |
| 137 | + .columns |
| 138 | + .iter() |
| 139 | + .find(|binding| binding.index == mutation.row_id_index) |
| 140 | + .unwrap() |
| 141 | + .clone(); |
| 142 | + |
| 143 | + let table_schema = mutation |
| 144 | + .metadata |
| 145 | + .read() |
| 146 | + .table(mutation.target_table_index) |
| 147 | + .table() |
| 148 | + .schema(); |
| 149 | + |
| 150 | + let fields_bindings = table_schema |
| 151 | + .fields |
| 152 | + .iter() |
| 153 | + .filter_map(|field| { |
| 154 | + if field.computed_expr.is_some() { |
| 155 | + return None; |
| 156 | + } |
| 157 | + mutation |
| 158 | + .bind_context |
| 159 | + .columns |
| 160 | + .iter() |
| 161 | + .find(|binding| { |
| 162 | + binding.table_index == Some(mutation.target_table_index) |
| 163 | + && binding.column_name == field.name |
| 164 | + }) |
| 165 | + .cloned() |
| 166 | + }) |
| 167 | + .collect::<Vec<_>>(); |
| 168 | + |
| 169 | + let used_columns = mutation |
| 170 | + .matched_evaluators |
| 171 | + .iter() |
| 172 | + .flat_map(|eval| { |
| 173 | + eval.update.iter().flat_map(|update| { |
| 174 | + update |
| 175 | + .values() |
| 176 | + .flat_map(|expr| expr.used_columns().into_iter()) |
| 177 | + }) |
| 178 | + }) |
| 179 | + .chain(mutation.required_columns.iter().copied()) |
| 180 | + .collect::<HashSet<_>>(); |
| 181 | + |
| 182 | + let used_columns = used_columns |
| 183 | + .difference(&fields_bindings.iter().map(|column| column.index).collect()) |
| 184 | + .copied() |
| 185 | + .collect::<HashSet<_>>(); |
| 186 | + |
| 187 | + let aggr_columns = used_columns |
| 188 | + .iter() |
| 189 | + .copied() |
| 190 | + .filter_map(|i| { |
| 191 | + if i == mutation.row_id_index { |
| 192 | + return None; |
| 193 | + } |
| 194 | + |
| 195 | + let binding = mutation |
| 196 | + .bind_context |
| 197 | + .columns |
| 198 | + .iter() |
| 199 | + .find(|binding| binding.index == i) |
| 200 | + .cloned()?; |
| 201 | + |
| 202 | + let display_name = format!("any({})", binding.index); |
| 203 | + let old = binding.index; |
| 204 | + let mut aggr_func = ScalarExpr::AggregateFunction(AggregateFunction { |
| 205 | + span: None, |
| 206 | + func_name: "any".to_string(), |
| 207 | + distinct: false, |
| 208 | + params: vec![], |
| 209 | + args: vec![ScalarExpr::BoundColumnRef(BoundColumnRef { |
| 210 | + span: None, |
| 211 | + column: binding.clone(), |
| 212 | + })], |
| 213 | + return_type: binding.data_type.clone(), |
| 214 | + sort_descs: vec![], |
| 215 | + display_name: display_name.clone(), |
| 216 | + }); |
| 217 | + |
| 218 | + let mut rewriter = |
| 219 | + AggregateRewriter::new(&mut mutation.bind_context, self.metadata.clone()); |
| 220 | + rewriter.visit(&mut aggr_func).unwrap(); |
| 221 | + |
| 222 | + let new = mutation |
| 223 | + .bind_context |
| 224 | + .aggregate_info |
| 225 | + .get_aggregate_function(&display_name) |
| 226 | + .unwrap() |
| 227 | + .index; |
| 228 | + |
| 229 | + Some((aggr_func, old, new)) |
| 230 | + }) |
| 231 | + .collect::<Vec<_>>(); |
| 232 | + |
| 233 | + mutation.bind_context.aggregate_info.group_items = fields_bindings |
| 234 | + .into_iter() |
| 235 | + .chain(std::iter::once(row_id)) |
| 236 | + .map(|column| ScalarItem { |
| 237 | + index: column.index, |
| 238 | + scalar: ScalarExpr::BoundColumnRef(BoundColumnRef { span: None, column }), |
| 239 | + }) |
| 240 | + .collect(); |
| 241 | + |
| 242 | + for eval in &mut mutation.matched_evaluators { |
| 243 | + if let Some(expr) = &mut eval.condition { |
| 244 | + for (_, old, new) in &aggr_columns { |
| 245 | + expr.replace_column(*old, *new)? |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + if let Some(update) = &mut eval.update { |
| 250 | + for (_, expr) in update.iter_mut() { |
| 251 | + for (_, old, new) in &aggr_columns { |
| 252 | + expr.replace_column(*old, *new)? |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + for (_, column) in mutation.field_index_map.iter_mut() { |
| 259 | + if let Some((_, _, index)) = aggr_columns |
| 260 | + .iter() |
| 261 | + .find(|(_, i, _)| i.to_string() == *column) |
| 262 | + { |
| 263 | + *column = index.to_string() |
| 264 | + }; |
| 265 | + } |
| 266 | + |
| 267 | + mutation.required_columns = Box::new( |
| 268 | + std::iter::once(mutation.row_id_index) |
| 269 | + .chain(aggr_columns.into_iter().map(|(_, _, i)| i)) |
| 270 | + .collect(), |
| 271 | + ); |
| 272 | + |
| 273 | + let aggr_expr = self.bind_aggregate(&mut mutation.bind_context, (**filter_expr).clone())?; |
| 274 | + |
| 275 | + let s_expr = SExpr::create_unary( |
| 276 | + Arc::new(RelOperator::Mutation(mutation)), |
| 277 | + Arc::new(aggr_expr), |
| 278 | + ); |
| 279 | + |
| 280 | + let Plan::DataMutation { |
| 281 | + schema, metadata, .. |
| 282 | + } = plan |
| 283 | + else { |
| 284 | + unreachable!() |
| 285 | + }; |
| 286 | + |
| 287 | + let plan = Plan::DataMutation { |
| 288 | + s_expr: Box::new(s_expr), |
| 289 | + schema, |
| 290 | + metadata, |
| 291 | + }; |
| 292 | + |
| 293 | + Ok(plan) |
97 | 294 | } |
98 | 295 | } |
0 commit comments