Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit c7f4610

Browse files
committed
feat(df-repr): implement display for properties (#223)
Signed-off-by: Alex Chi <[email protected]>
1 parent 3f8fa1b commit c7f4610

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

optd-core/src/property.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::nodes::{ArcPredNode, NodeType};
2-
use std::{any::Any, fmt::Debug};
2+
use std::{
3+
any::Any,
4+
fmt::{Debug, Display},
5+
};
36

47
pub trait PropertyBuilderAny<T: NodeType>: 'static + Send + Sync {
58
fn derive_any(
@@ -13,7 +16,7 @@ pub trait PropertyBuilderAny<T: NodeType>: 'static + Send + Sync {
1316
}
1417

1518
pub trait PropertyBuilder<T: NodeType>: 'static + Send + Sync + Sized {
16-
type Prop: 'static + Send + Sync + Sized + Clone + Debug;
19+
type Prop: 'static + Send + Sync + Sized + Clone + Debug + Display;
1720
fn derive(&self, typ: T, predicates: &[ArcPredNode<T>], children: &[&Self::Prop])
1821
-> Self::Prop;
1922
fn property_name(&self) -> &'static str;
@@ -41,7 +44,7 @@ impl<T: NodeType, P: PropertyBuilder<T>> PropertyBuilderAny<T> for P {
4144
let prop = prop
4245
.downcast_ref::<P::Prop>()
4346
.expect("Failed to downcast property");
44-
format!("{:?}", prop)
47+
prop.to_string()
4548
}
4649

4750
fn property_name(&self) -> &'static str {

optd-datafusion-repr/src/properties/column_ref.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,26 @@ impl ColumnRefPropertyBuilder {
367367
}
368368
}
369369

370+
impl std::fmt::Display for ColumnRef {
371+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
372+
match self {
373+
ColumnRef::BaseTableColumnRef(col) => write!(f, "{}.{}", col.table, col.col_idx),
374+
ColumnRef::ChildColumnRef { col_idx } => write!(f, "#{}", col_idx),
375+
ColumnRef::Derived => write!(f, "Derived"),
376+
}
377+
}
378+
}
379+
380+
impl std::fmt::Display for GroupColumnRefs {
381+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
382+
write!(
383+
f,
384+
"[{}]",
385+
self.column_refs.iter().map(|x| x.to_string()).join(", ")
386+
)
387+
}
388+
}
389+
370390
impl PropertyBuilder<DfNodeType> for ColumnRefPropertyBuilder {
371391
type Prop = GroupColumnRefs;
372392

optd-datafusion-repr/src/properties/schema.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ pub struct Field {
1717
pub nullable: bool,
1818
}
1919

20+
impl std::fmt::Display for Field {
21+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22+
if self.nullable {
23+
write!(f, "{}:{:?}", self.name, self.typ)
24+
} else {
25+
write!(f, "{}:{:?}(non-null)", self.name, self.typ)
26+
}
27+
}
28+
}
29+
2030
impl Field {
2131
/// Generate a field that is only a place holder whose members are never used.
2232
fn placeholder() -> Self {
@@ -33,6 +43,16 @@ pub struct Schema {
3343
pub fields: Vec<Field>,
3444
}
3545

46+
impl std::fmt::Display for Schema {
47+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48+
write!(
49+
f,
50+
"[{}]",
51+
self.fields.iter().map(|x| x.to_string()).join(", ")
52+
)
53+
}
54+
}
55+
3656
impl Schema {
3757
pub fn new(fields: Vec<Field>) -> Self {
3858
Self { fields }

0 commit comments

Comments
 (0)