|
15 | 15 | use itertools::Itertools;
|
16 | 16 |
|
17 | 17 | use crate::array::column::Column;
|
| 18 | +use crate::array::ArrayImpl; |
| 19 | +use crate::for_all_type_pairs; |
18 | 20 | use crate::types::DataType;
|
19 | 21 |
|
20 | 22 | /// Check if the schema of `columns` matches the expected `data_types`. Used for debugging.
|
21 | 23 | pub fn schema_check<'a, T, C>(data_types: T, columns: C) -> Result<(), String>
|
22 | 24 | where
|
23 |
| - T: IntoIterator<Item = &'a DataType> + Clone, |
24 |
| - C: IntoIterator<Item = &'a Column> + Clone, |
| 25 | + T: IntoIterator<Item = &'a DataType>, |
| 26 | + C: IntoIterator<Item = &'a Column>, |
25 | 27 | {
|
26 |
| - tracing::event!( |
27 |
| - tracing::Level::TRACE, |
28 |
| - "input schema = \n{:#?}\nexpected schema = \n{:#?}", |
29 |
| - columns |
30 |
| - .clone() |
31 |
| - .into_iter() |
32 |
| - .map(|col| col.array_ref().get_ident()) |
33 |
| - .collect_vec(), |
34 |
| - data_types.clone().into_iter().collect_vec(), |
35 |
| - ); |
36 |
| - |
37 |
| - for (i, pair) in columns.into_iter().zip_longest(data_types).enumerate() { |
38 |
| - let array = pair.as_ref().left().map(|c| c.array_ref()); |
39 |
| - let builder = pair.as_ref().right().map(|d| d.create_array_builder(0)); // TODO: check `data_type` directly |
40 |
| - |
41 |
| - macro_rules! check_schema { |
42 |
| - ($( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { |
43 |
| - use crate::array::ArrayBuilderImpl; |
44 |
| - use crate::array::ArrayImpl; |
45 |
| - |
46 |
| - match (array, &builder) { |
47 |
| - $( (Some(ArrayImpl::$variant_name(_)), Some(ArrayBuilderImpl::$variant_name(_))) => {} ),* |
48 |
| - _ => return Err(format!("column {} should be {:?}, while stream chunk gives {:?}", |
49 |
| - i, builder.map(|b| b.get_ident()), array.map(|a| a.get_ident()))), |
| 28 | + for (i, pair) in data_types |
| 29 | + .into_iter() |
| 30 | + .zip_longest(columns.into_iter().map(|c| c.array_ref())) |
| 31 | + .enumerate() |
| 32 | + { |
| 33 | + macro_rules! matches { |
| 34 | + ($( { $DataType:ident, $PhysicalType:ident }),*) => { |
| 35 | + match (pair.as_ref().left(), pair.as_ref().right()) { |
| 36 | + $( (Some(DataType::$DataType { .. }), Some(ArrayImpl::$PhysicalType(_))) => continue, )* |
| 37 | + (data_type, array) => { |
| 38 | + let array_ident = array.map(|a| a.get_ident()); |
| 39 | + return Err(format!( |
| 40 | + "column type mismatched at position {i}: expected {data_type:?}, found {array_ident:?}" |
| 41 | + )); |
| 42 | + } |
50 | 43 | }
|
51 |
| - }; |
| 44 | + } |
52 | 45 | }
|
53 | 46 |
|
54 |
| - for_all_variants! { check_schema }; |
| 47 | + for_all_type_pairs! { matches } |
55 | 48 | }
|
56 | 49 |
|
57 | 50 | Ok(())
|
|
0 commit comments