Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(prost): optimize some Debug representation #18211

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/prost/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// If any configuration for `prost_build` is not exposed by `tonic_build`, specify it here.
let mut prost_config = prost_build::Config::new();
prost_config.skip_debug(["meta.SystemParams"]);

prost_config.skip_debug([
"meta.SystemParams",
"plan_common.ColumnDesc",
"data.DataType",
// TODO:
//"stream_plan.StreamNode"
]);
// Compile the proto files.
tonic_config
.out_dir(out_dir.as_path())
Expand Down
98 changes: 98 additions & 0 deletions src/prost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use std::str::FromStr;

use plan_common::AdditionalColumn;
pub use prost::Message;
use risingwave_error::tonic::ToTonicStatus;
use thiserror::Error;
Expand Down Expand Up @@ -329,6 +330,103 @@ impl std::fmt::Debug for meta::SystemParams {
}
}

// More compact formats for debugging

impl std::fmt::Debug for data::DataType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let data::DataType {
precision,
scale,
interval_type,
field_type,
field_names,
type_name,
// currently all data types are nullable
is_nullable: _,
} = self;

let type_name = data::data_type::TypeName::try_from(*type_name)
.map(|t| t.as_str_name())
.unwrap_or("Unknown");

let mut s = f.debug_struct(type_name);
if self.precision != 0 {
s.field("precision", precision);
}
if self.scale != 0 {
s.field("scale", scale);
}
if self.interval_type != 0 {
s.field("interval_type", interval_type);
}
if !self.field_type.is_empty() {
s.field("field_type", field_type);
}
if !self.field_names.is_empty() {
s.field("field_names", field_names);
}
s.finish()
}
}

impl std::fmt::Debug for plan_common::column_desc::GeneratedOrDefaultColumn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::GeneratedColumn(arg0) => f.debug_tuple("GeneratedColumn").field(arg0).finish(),
Self::DefaultColumn(arg0) => f.debug_tuple("DefaultColumn").field(arg0).finish(),
}
}
}

impl std::fmt::Debug for plan_common::ColumnDesc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// destruct here to avoid missing new fields in the future.
let plan_common::ColumnDesc {
column_type,
column_id,
name,
field_descs,
type_name,
description,
additional_column_type,
additional_column,
generated_or_default_column,
version,
} = self;

let mut s = f.debug_struct("ColumnDesc");
if let Some(column_type) = column_type {
s.field("column_type", column_type);
} else {
s.field("column_type", &"Unknown");
}
s.field("column_id", column_id).field("name", name);
if !self.field_descs.is_empty() {
s.field("field_descs", field_descs);
}
if !self.type_name.is_empty() {
s.field("type_name", type_name);
}
if let Some(description) = description {
s.field("description", description);
}
if self.additional_column_type != 0 {
s.field("additional_column_type", additional_column_type);
}
s.field("version", version);
if let Some(AdditionalColumn { column_type }) = additional_column {
// AdditionalColumn { None } means a normal column
if let Some(column_type) = column_type {
s.field("additional_column", &column_type);
}
}
if let Some(generated_or_default_column) = generated_or_default_column {
s.field("generated_or_default_column", &generated_or_default_column);
}
s.finish()
}
}

#[cfg(test)]
mod tests {
use crate::data::{data_type, DataType};
Expand Down
Loading