Skip to content

Commit

Permalink
feat: the schema of RegionMetadata is not output during debug (#2498)
Browse files Browse the repository at this point in the history
* feat: the schema of RegionMetadata is not output during debug because column_metadatas contains duplicate information

Signed-off-by: ZhuZiyi <[email protected]>

* feat: the id_to_index of RegionMetadata is not output during debug

Signed-off-by: ZhuZiyi <[email protected]>

* feat: add debug trait

Signed-off-by: ZhuZiyi <[email protected]>

* feat: use default debug in ConcreteDataType

Signed-off-by: ZhuZiyi <[email protected]>

* chore: add std::fmt

Signed-off-by: ZhuZiyi <[email protected]>

* test: add debug trait test

Signed-off-by: ZhuZiyi <[email protected]>

* chore: typo

Signed-off-by: ZhuZiyi <[email protected]>

* chore: resolve conversation

Signed-off-by: ZhuZiyi <[email protected]>

* chore: format

Signed-off-by: ZhuZiyi <[email protected]>

* fix: test bug

Signed-off-by: ZhuZiyi <[email protected]>

---------

Signed-off-by: ZhuZiyi <[email protected]>
  • Loading branch information
Nateiru authored Oct 7, 2023
1 parent f50f2a8 commit b44e39f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/datatypes/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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>,
Expand All @@ -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;
Expand Down
41 changes: 40 additions & 1 deletion src/datatypes/src/schema/column_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::collections::HashMap;
use std::fmt;

use arrow::datatypes::Field;
use serde::{Deserialize, Serialize};
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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");
}
}
34 changes: 32 additions & 2 deletions src/store-api/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::Arc;

use api::helper::ColumnDataTypeWrapper;
Expand All @@ -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,
Expand All @@ -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> {
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }");
}
}

0 comments on commit b44e39f

Please sign in to comment.