From e36a630dfe0ee60ac9b4293c76f87fc2f035a59b Mon Sep 17 00:00:00 2001 From: Eric Fu Date: Mon, 19 Aug 2024 11:49:48 +0800 Subject: [PATCH] chore: fix RustRover macro expansion (#18077) --- src/common/src/array/iterator.rs | 6 +-- src/common/src/array/mod.rs | 14 +++--- src/common/src/test_utils/rand_array.rs | 6 +-- src/common/src/types/macros.rs | 57 ------------------------- src/common/src/types/mod.rs | 19 ++++----- src/common/src/util/schema_check.rs | 8 ++-- src/expr/core/src/expr/value.rs | 6 +-- 7 files changed, 29 insertions(+), 87 deletions(-) diff --git a/src/common/src/array/iterator.rs b/src/common/src/array/iterator.rs index 78c89f422aa02..31518150e6f8d 100644 --- a/src/common/src/array/iterator.rs +++ b/src/common/src/array/iterator.rs @@ -56,10 +56,10 @@ mod tests { use super::*; use crate::array::{ArrayBuilder, ArrayImpl}; - use crate::for_all_array_variants; + use crate::for_all_variants; macro_rules! test_trusted_len { - ($( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { $( paste! { #[test] @@ -91,5 +91,5 @@ mod tests { }; } - for_all_array_variants! { test_trusted_len } + for_all_variants! { test_trusted_len } } diff --git a/src/common/src/array/mod.rs b/src/common/src/array/mod.rs index ae6f7d0fa144b..b34e5f9b9c470 100644 --- a/src/common/src/array/mod.rs +++ b/src/common/src/array/mod.rs @@ -68,7 +68,7 @@ pub use self::error::ArrayError; pub use crate::array::num256_array::{Int256Array, Int256ArrayBuilder}; use crate::bitmap::Bitmap; use crate::types::*; -use crate::{dispatch_array_builder_variants, dispatch_array_variants, for_all_array_variants}; +use crate::{dispatch_array_builder_variants, dispatch_array_variants, for_all_variants}; pub type ArrayResult = Result; pub type I64Array = PrimitiveArray; @@ -325,7 +325,7 @@ impl CompactableArray for A { /// Define `ArrayImpl` with macro. macro_rules! array_impl_enum { - ( $( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { + ( $( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { /// `ArrayImpl` embeds all possible array in `array` module. #[derive(Debug, Clone, EstimateSize)] pub enum ArrayImpl { @@ -334,7 +334,7 @@ macro_rules! array_impl_enum { }; } -for_all_array_variants! { array_impl_enum } +for_all_variants! { array_impl_enum } // We cannot put the From implementations in impl_convert, // because then we can't prove for all `T: PrimitiveArrayItemType`, @@ -401,7 +401,7 @@ impl From for ArrayImpl { /// * `ArrayImpl -> Array` with `From` trait. /// * `ArrayBuilder -> ArrayBuilderImpl` with `From` trait. macro_rules! impl_convert { - ($( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { $( paste! { impl ArrayImpl { @@ -455,11 +455,11 @@ macro_rules! impl_convert { }; } -for_all_array_variants! { impl_convert } +for_all_variants! { impl_convert } /// Define `ArrayImplBuilder` with macro. macro_rules! array_builder_impl_enum { - ($( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { /// `ArrayBuilderImpl` embeds all possible array in `array` module. #[derive(Debug, Clone, EstimateSize)] pub enum ArrayBuilderImpl { @@ -468,7 +468,7 @@ macro_rules! array_builder_impl_enum { }; } -for_all_array_variants! { array_builder_impl_enum } +for_all_variants! { array_builder_impl_enum } /// Implements all `ArrayBuilder` functions with `for_all_variant`. impl ArrayBuilderImpl { diff --git a/src/common/src/test_utils/rand_array.rs b/src/common/src/test_utils/rand_array.rs index a7c13e3178f26..f201b4de33843 100644 --- a/src/common/src/test_utils/rand_array.rs +++ b/src/common/src/test_utils/rand_array.rs @@ -201,12 +201,12 @@ where #[cfg(test)] mod tests { use super::*; - use crate::for_all_array_variants; + use crate::for_all_variants; #[test] fn test_create_array() { macro_rules! gen_rand_array { - ($( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { $( { let array = seed_rand_array::<$array>(10, 1024, 0.5); @@ -216,6 +216,6 @@ mod tests { }; } - for_all_array_variants! { gen_rand_array } + for_all_variants! { gen_rand_array } } } diff --git a/src/common/src/types/macros.rs b/src/common/src/types/macros.rs index 1dd29156dd651..9fa9c7ffe5ecd 100644 --- a/src/common/src/types/macros.rs +++ b/src/common/src/types/macros.rs @@ -64,63 +64,6 @@ macro_rules! for_all_variants { }; } -/// The projected version of `for_all_variants` for handling scalar variants. -/// -/// Arguments are `$variant_name`, `$suffix_name`, `$scalar`, `$scalar_ref`. -#[macro_export(local_inner_macros)] -macro_rules! for_all_scalar_variants { - ($macro:ident $(, $x:tt)*) => { - for_all_variants! { project_scalar_variants, $macro, [ $($x, )* ] } - }; -} -#[macro_export] -macro_rules! project_scalar_variants { - ($macro:ident, [ $($x:tt, )* ], $( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { - $macro! { - $($x, )* - $( { $variant_name, $suffix_name, $scalar, $scalar_ref } ),* - } - }; -} - -/// The projected version of `for_all_variants` for handling array variants. -/// -/// Arguments are `$variant_name`, `$suffix_name`, `$array`, `$builder`. -#[macro_export(local_inner_macros)] -macro_rules! for_all_array_variants { - ($macro:ident $(, $x:tt)*) => { - for_all_variants! { project_array_variants, $macro, [ $($x, )* ] } - }; -} -#[macro_export] -macro_rules! project_array_variants { - ($macro:ident, [ $($x:tt, )* ], $( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { - $macro! { - $($x, )* - $( { $variant_name, $suffix_name, $array, $builder } ),* - } - }; -} - -/// The projected version of `for_all_variants` for handling mapping of data types and array types. -/// -/// Arguments are `$data_type`, `$variant_name`. -#[macro_export(local_inner_macros)] -macro_rules! for_all_type_pairs { - ($macro:ident $(, $x:tt)*) => { - for_all_variants! { project_type_pairs, $macro, [ $($x, )* ] } - }; -} -#[macro_export] -macro_rules! project_type_pairs { - ($macro:ident, [ $($x:tt, )* ], $( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { - $macro! { - $($x, )* - $( { $data_type, $variant_name } ),* - } - }; -} - /// Helper macro for expanding type aliases and constants. Internally used by `dispatch_` macros. #[macro_export] macro_rules! do_expand_alias { diff --git a/src/common/src/types/mod.rs b/src/common/src/types/mod.rs index e76bfaba384f8..b86e70b85d8bf 100644 --- a/src/common/src/types/mod.rs +++ b/src/common/src/types/mod.rs @@ -42,8 +42,7 @@ pub use crate::array::{ListRef, ListValue, MapRef, MapValue, StructRef, StructVa use crate::cast::{str_to_bool, str_to_bytea}; use crate::error::BoxedError; use crate::{ - dispatch_data_types, dispatch_scalar_ref_variants, dispatch_scalar_variants, - for_all_scalar_variants, for_all_type_pairs, + dispatch_data_types, dispatch_scalar_ref_variants, dispatch_scalar_variants, for_all_variants, }; mod cow; @@ -552,7 +551,7 @@ pub trait ScalarRef<'a>: private::ScalarBounds> + 'a + Copy { /// Define `ScalarImpl` and `ScalarRefImpl` with macro. macro_rules! scalar_impl_enum { - ($( { $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty } ),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { /// `ScalarImpl` embeds all possible scalars in the evaluation framework. /// /// Note: `ScalarImpl` doesn't contain all information of its `DataType`, @@ -580,7 +579,7 @@ macro_rules! scalar_impl_enum { }; } -for_all_scalar_variants! { scalar_impl_enum } +for_all_variants! { scalar_impl_enum } // We MUST NOT implement `Ord` for `ScalarImpl` because that will make `Datum` derive an incorrect // default `Ord`. To get a default-ordered `ScalarImpl`/`ScalarRefImpl`/`Datum`/`DatumRef`, you can @@ -686,7 +685,7 @@ macro_rules! for_all_native_types { /// * `&ScalarImpl -> &Scalar` with `impl.as_int16()`. /// * `ScalarImpl -> Scalar` with `impl.into_int16()`. macro_rules! impl_convert { - ($( { $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty } ),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { $( impl From<$scalar> for ScalarImpl { fn from(val: $scalar) -> Self { @@ -758,7 +757,7 @@ macro_rules! impl_convert { }; } -for_all_scalar_variants! { impl_convert } +for_all_variants! { impl_convert } // Implement `From` for `ScalarImpl::Float` as a sugar. impl From for ScalarImpl { @@ -1092,16 +1091,16 @@ pub fn literal_type_match(data_type: &DataType, literal: Option<&ScalarImpl>) -> match literal { Some(scalar) => { macro_rules! matches { - ($( { $DataType:ident, $PhysicalType:ident }),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty }),*) => { match (data_type, scalar) { $( - (DataType::$DataType { .. }, ScalarImpl::$PhysicalType(_)) => true, - (DataType::$DataType { .. }, _) => false, // so that we won't forget to match a new logical type + (DataType::$data_type { .. }, ScalarImpl::$variant_name(_)) => true, + (DataType::$data_type { .. }, _) => false, // so that we won't forget to match a new logical type )* } } } - for_all_type_pairs! { matches } + for_all_variants! { matches } } None => true, } diff --git a/src/common/src/util/schema_check.rs b/src/common/src/util/schema_check.rs index 850329766577c..f035ed7137934 100644 --- a/src/common/src/util/schema_check.rs +++ b/src/common/src/util/schema_check.rs @@ -15,7 +15,7 @@ use itertools::Itertools; use crate::array::{ArrayImpl, ArrayRef}; -use crate::for_all_type_pairs; +use crate::for_all_variants; use crate::types::DataType; /// Check if the schema of `columns` matches the expected `data_types`. Used for debugging. @@ -30,9 +30,9 @@ where .enumerate() { macro_rules! matches { - ($( { $DataType:ident, $PhysicalType:ident }),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty }),*) => { match (pair.as_ref().left(), pair.as_ref().right()) { - $( (Some(DataType::$DataType { .. }), Some(ArrayImpl::$PhysicalType(_))) => continue, )* + $( (Some(DataType::$data_type { .. }), Some(ArrayImpl::$variant_name(_))) => continue, )* (data_type, array) => { let array_ident = array.map(|a| a.get_ident()); return Err(format!( @@ -43,7 +43,7 @@ where } } - for_all_type_pairs! { matches } + for_all_variants! { matches } } Ok(()) diff --git a/src/expr/core/src/expr/value.rs b/src/expr/core/src/expr/value.rs index c4ec77268488a..00aa2bf71ce1f 100644 --- a/src/expr/core/src/expr/value.rs +++ b/src/expr/core/src/expr/value.rs @@ -14,7 +14,7 @@ use either::Either; use risingwave_common::array::*; -use risingwave_common::for_all_array_variants; +use risingwave_common::for_all_variants; use risingwave_common::types::{Datum, DatumRef, Scalar, ToDatumRef}; /// The type-erased return value of an expression. @@ -79,7 +79,7 @@ impl<'a, A: Array> ValueRef<'a, A> { } macro_rules! impl_convert { - ($( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { + ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => { $( paste::paste! { /// Converts a type-erased value to a reference of a specific array type. @@ -102,4 +102,4 @@ macro_rules! impl_convert { }; } -for_all_array_variants! { impl_convert } +for_all_variants! { impl_convert }