mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-23 00:10:38 +00:00
feat: the schema of RegionMetadata is not output during debug (#2498)
* feat: the schema of RegionMetadata is not output during debug because column_metadatas contains duplicate information Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * feat: the id_to_index of RegionMetadata is not output during debug Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * feat: add debug trait Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * feat: use default debug in ConcreteDataType Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * chore: add std::fmt Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * test: add debug trait test Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * chore: typo Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * chore: resolve conversation Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * chore: format Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> * fix: test bug Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com> --------- Signed-off-by: ZhuZiyi <zyzhu2001@gmail.com>
This commit is contained in:
@@ -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<ColumnSchema>,
|
||||
name_to_index: HashMap<String, usize>,
|
||||
@@ -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;
|
||||
|
||||
@@ -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<T: Into<String>>(
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> = std::result::Result<T, MetadataError>;
|
||||
|
||||
/// 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<Self> {
|
||||
@@ -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<RegionMetadata>;
|
||||
|
||||
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 }");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user