Reports the unique keys of the relation given the arities and the unique keys of the input relations. `input_arities` and `input_keys` are required to contain the corresponding info for the input relations of the current relation in the same order as they are visited by `try_visit_children` method, even though not all may be used for computing the schema of the current relation. For example, `Let
(
&self,
mut input_arities: I,
mut input_keys: J,
)
| 558 | /// It is meant to be used during post-order traversals to compute unique keys |
| 559 | /// incrementally. |
| 560 | pub fn keys_with_input_keys<'a, I, J>( |
| 561 | &self, |
| 562 | mut input_arities: I, |
| 563 | mut input_keys: J, |
| 564 | ) -> Vec<Vec<usize>> |
| 565 | where |
| 566 | I: Iterator<Item = usize>, |
| 567 | J: Iterator<Item = &'a Vec<Vec<usize>>>, |
| 568 | { |
| 569 | use MirRelationExpr::*; |
| 570 | |
| 571 | let mut keys = match self { |
| 572 | Constant { |
| 573 | rows: Ok(rows), |
| 574 | typ, |
| 575 | } => { |
| 576 | let n_cols = typ.arity(); |
| 577 | // If the `i`th entry is `Some`, then we have not yet observed non-uniqueness in the `i`th column. |
| 578 | let mut unique_values_per_col = vec![Some(BTreeSet::<Datum>::default()); n_cols]; |
| 579 | for (row, diff) in rows { |
| 580 | for (i, datum) in row.iter().enumerate() { |
| 581 | if datum != Datum::Dummy { |
| 582 | if let Some(unique_vals) = &mut unique_values_per_col[i] { |
| 583 | let is_dupe = *diff != Diff::ONE || !unique_vals.insert(datum); |
| 584 | if is_dupe { |
| 585 | unique_values_per_col[i] = None; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | if rows.len() == 0 || (rows.len() == 1 && rows[0].1 == Diff::ONE) { |
| 592 | vec![vec![]] |
| 593 | } else { |
| 594 | // XXX - Multi-column keys are not detected. |
| 595 | typ.keys |
| 596 | .iter() |
| 597 | .cloned() |
| 598 | .chain( |
| 599 | unique_values_per_col |
| 600 | .into_iter() |
| 601 | .enumerate() |
| 602 | .filter(|(_idx, unique_vals)| unique_vals.is_some()) |
| 603 | .map(|(idx, _)| vec![idx]), |
| 604 | ) |
| 605 | .collect() |
| 606 | } |
| 607 | } |
| 608 | Constant { rows: Err(_), typ } | Get { typ, .. } => typ.keys.clone(), |
| 609 | Threshold { .. } | ArrangeBy { .. } => input_keys.next().unwrap().clone(), |
| 610 | Let { .. } => { |
| 611 | // skip over the unique keys for value |
| 612 | input_keys.nth(1).unwrap().clone() |
| 613 | } |
| 614 | LetRec { values, .. } => { |
| 615 | // skip over the unique keys for value |
| 616 | input_keys.nth(values.len()).unwrap().clone() |
| 617 | } |
no test coverage detected