|
10 | 10 | use std::fmt::Write; |
11 | 11 | use std::str::FromStr; |
12 | 12 |
|
13 | | -use itertools::{EitherOrBoth, Itertools}; |
14 | 13 | use mysql_common::value::convert::from_value_opt; |
15 | 14 | use mysql_common::{Row as MySqlRow, Value}; |
16 | 15 |
|
@@ -43,50 +42,39 @@ pub fn pack_mysql_row( |
43 | 42 | .columns_ref() |
44 | 43 | .first() |
45 | 44 | .is_some_and(|col| col.name_ref().starts_with(b"@")); |
46 | | - // Wire indices of `row` to decode, in iteration order. Keeping indices |
47 | | - // (rather than moving values out via `row.unwrap()`) lets us describe the |
48 | | - // row's shape on a decode error without paying the allocation cost on the |
49 | | - // happy path. |
50 | | - let active_indices: Vec<usize> = if fallback_names { |
51 | | - (0..row.len()).collect() |
52 | | - } else { |
53 | | - row.columns_ref() |
54 | | - .iter() |
55 | | - .enumerate() |
56 | | - .filter(|(_, col)| { |
57 | | - table_desc |
58 | | - .columns |
59 | | - .iter() |
60 | | - .any(|c| c.name.as_str() == col.name_str()) |
61 | | - }) |
62 | | - .map(|(i, _)| i) |
63 | | - .collect() |
64 | | - }; |
65 | 45 |
|
66 | | - for pair in table_desc.columns.iter().zip_longest(&active_indices) { |
67 | | - let (col_desc, wire_idx) = match pair { |
68 | | - EitherOrBoth::Both(col_desc, &idx) => (col_desc, idx), |
69 | | - EitherOrBoth::Left(col_desc) => { |
| 46 | + // For each column in `table_desc` (in descriptor order), resolve its wire |
| 47 | + // index. Non-fallback rows are matched by name so a reordered upstream |
| 48 | + // still decodes correctly; fallback rows have no names and are matched |
| 49 | + // positionally. A `None` here means the upstream row is missing this |
| 50 | + // column and is only tolerated for ignored columns. |
| 51 | + for (i, col_desc) in table_desc.columns.iter().enumerate() { |
| 52 | + let wire_idx = if fallback_names { |
| 53 | + (i < row.len()).then_some(i) |
| 54 | + } else { |
| 55 | + row.columns_ref() |
| 56 | + .iter() |
| 57 | + .position(|wc| wc.name_str() == col_desc.name.as_str()) |
| 58 | + }; |
| 59 | + if col_desc.column_type.is_none() { |
| 60 | + // This column is ignored, so don't decode it. |
| 61 | + continue; |
| 62 | + } |
| 63 | + let wire_idx = match wire_idx { |
| 64 | + Some(idx) => idx, |
| 65 | + None => { |
70 | 66 | return Err(decode_error( |
71 | | - "extra column description", |
| 67 | + "upstream row is missing column", |
72 | 68 | col_desc, |
73 | 69 | table_desc, |
74 | 70 | gtid_set, |
75 | 71 | &row, |
76 | 72 | )); |
77 | 73 | } |
78 | | - EitherOrBoth::Right(_) => { |
79 | | - // If there are extra columns on the upstream table we can safely ignore them |
80 | | - break; |
81 | | - } |
82 | 74 | }; |
83 | | - if col_desc.column_type.is_none() { |
84 | | - // This column is ignored, so don't decode it. |
85 | | - continue; |
86 | | - } |
87 | 75 | let value = row |
88 | 76 | .as_ref(wire_idx) |
89 | | - .expect("active_indices is within row bounds") |
| 77 | + .expect("wire_idx resolved from row") |
90 | 78 | .clone(); |
91 | 79 | if let Err(err) = pack_val_as_datum(value, col_desc, &mut packer) { |
92 | 80 | return Err(decode_error( |
|
0 commit comments