diff --git a/src/datatypes/src/schema.rs b/src/datatypes/src/schema.rs index b506dd12ea00..972b02e822d6 100644 --- a/src/datatypes/src/schema.rs +++ b/src/datatypes/src/schema.rs @@ -17,6 +17,7 @@ mod constraint; mod raw; use std::collections::HashMap; +use std::fmt; use std::sync::Arc; use arrow::datatypes::{Field, Schema as ArrowSchema}; @@ -32,7 +33,7 @@ pub use crate::schema::raw::RawSchema; pub const VERSION_KEY: &str = "greptime:version"; /// A common schema, should be immutable. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] pub struct Schema { column_schemas: Vec, name_to_index: HashMap, @@ -48,6 +49,17 @@ pub struct Schema { version: u32, } +impl fmt::Debug for Schema { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Schema") + .field("column_schemas", &self.column_schemas) + .field("name_to_index", &self.name_to_index) + .field("timestamp_index", &self.timestamp_index) + .field("version", &self.version) + .finish() + } +} + impl Schema { /// Initial version of the schema. pub const INITIAL_VERSION: u32 = 0; diff --git a/src/datatypes/src/schema/column_schema.rs b/src/datatypes/src/schema/column_schema.rs index 39a5405898c7..865131811848 100644 --- a/src/datatypes/src/schema/column_schema.rs +++ b/src/datatypes/src/schema/column_schema.rs @@ -13,6 +13,7 @@ // limitations under the License. use std::collections::HashMap; +use std::fmt; use arrow::datatypes::Field; use serde::{Deserialize, Serialize}; @@ -33,7 +34,7 @@ pub const COMMENT_KEY: &str = "greptime:storage:comment"; const DEFAULT_CONSTRAINT_KEY: &str = "greptime:default_constraint"; /// Schema of a column, used as an immutable struct. -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ColumnSchema { pub name: String, pub data_type: ConcreteDataType, @@ -43,6 +44,30 @@ pub struct ColumnSchema { metadata: Metadata, } +impl fmt::Debug for ColumnSchema { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} {} {}", + self.name, + self.data_type, + if self.is_nullable { "null" } else { "not null" }, + )?; + + // Add default constraint if present + if let Some(default_constraint) = &self.default_constraint { + write!(f, " default={:?}", default_constraint)?; + } + + // Add metadata if present + if !self.metadata.is_empty() { + write!(f, " metadata={:?}", self.metadata)?; + } + + Ok(()) + } +} + impl ColumnSchema { pub fn new>( name: T, @@ -394,4 +419,18 @@ mod tests { let column_schema = ColumnSchema::new("test", ConcreteDataType::int32_datatype(), false); assert!(column_schema.create_default().unwrap().is_none()); } + + #[test] + fn test_debug_for_column_schema() { + let column_schema_int8 = + ColumnSchema::new("test_column_1", ConcreteDataType::int8_datatype(), true); + + let column_schema_int32 = + ColumnSchema::new("test_column_2", ConcreteDataType::int32_datatype(), false); + + let formatted_int8 = format!("{:?}", column_schema_int8); + let formatted_int32 = format!("{:?}", column_schema_int32); + assert_eq!(formatted_int8, "test_column_1 Int8 null"); + assert_eq!(formatted_int32, "test_column_2 Int32 not null"); + } } diff --git a/src/store-api/src/metadata.rs b/src/store-api/src/metadata.rs index f51284e6994d..6e1c236fac4a 100644 --- a/src/store-api/src/metadata.rs +++ b/src/store-api/src/metadata.rs @@ -18,6 +18,7 @@ use std::any::Any; use std::collections::{HashMap, HashSet}; +use std::fmt; use std::sync::Arc; use api::helper::ColumnDataTypeWrapper; @@ -39,7 +40,7 @@ use crate::storage::{ColumnId, RegionId}; pub type Result = std::result::Result; /// Metadata of a column. -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct ColumnMetadata { /// Schema of this column. Is the same as `column_schema` in [SchemaRef]. pub column_schema: ColumnSchema, @@ -49,6 +50,16 @@ pub struct ColumnMetadata { pub column_id: ColumnId, } +impl fmt::Debug for ColumnMetadata { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "[{:?} {:?} {:?}]", + self.column_schema, self.semantic_type, self.column_id, + ) + } +} + impl ColumnMetadata { /// Construct `Self` from protobuf struct [RegionColumnDef] pub fn try_from_column_def(column_def: RegionColumnDef) -> Result { @@ -106,7 +117,7 @@ impl ColumnMetadata { /// RegionMetadata o-- ColumnMetadata /// ColumnMetadata o-- SemanticType /// ``` -#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[derive(Clone, PartialEq, Eq, Serialize)] pub struct RegionMetadata { /// Latest schema constructed from [column_metadatas](RegionMetadata::column_metadatas). #[serde(skip)] @@ -135,6 +146,18 @@ pub struct RegionMetadata { pub schema_version: u64, } +impl fmt::Debug for RegionMetadata { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("RegionMetadata") + .field("column_metadatas", &self.column_metadatas) + .field("time_index", &self.time_index) + .field("primary_key", &self.primary_key) + .field("region_id", &self.region_id) + .field("schema_version", &self.schema_version) + .finish() + } +} + pub type RegionMetadataRef = Arc; impl<'de> Deserialize<'de> for RegionMetadata { @@ -1109,4 +1132,11 @@ mod test { "unexpected err: {err}", ); } + + #[test] + fn test_debug_for_column_metadata() { + let region_metadata = build_test_region_metadata(); + let formatted = format!("{:?}", region_metadata); + assert_eq!(formatted, "RegionMetadata { column_metadatas: [[a Int64 not null Tag 1], [b Float64 not null Field 2], [c Timestamp not null Timestamp 3]], time_index: 3, primary_key: [1], region_id: 5299989648942(1234, 5678), schema_version: 0 }"); + } }