Skip to content

Commit c10c580

Browse files
authored
[query-engine] Implement fmt_with_indent for summary expressions (open-telemetry#1338)
Relates to open-telemetry#1178 ## Details Examples: ``` Pipeline ├── Query: "source\n | extend t = gettype(EventName) | summarize Count = count(), max(SeverityNumber) by Timestamp, EventName | extend t = gettype(Count)" ├── Constants: │ └── 0 = Map: {"Array":"array","Boolean":"bool","DateTime":"datetime","Double":"real","Integer":"long","Map":"dictionary","Null":"null","Regex":"regex","String":"string","TimeSpan":"timespan"} ├── Initializations: [] └── Expressions: ├── Set │ ├── Source(Scalar): Constant(Reference) │ │ ├── ValueType: Map │ │ ├── Id: 0 │ │ └── Accessor: │ │ └── GetType(Scalar): Source │ │ └── Accessor: │ │ └── String: "EventName" │ └── Destination(Mutable): Source │ └── Accessor: │ ├── String: "Attributes" │ └── String: "t" └── Summary ├── GroupBys: │ ├── EventName = Source │ │ └── Accessor: │ │ └── String: "EventName" │ └── Timestamp = Source │ └── Accessor: │ └── String: "Timestamp" ├── Aggregations: │ ├── Count = Count │ └── max_SeverityNumber = Maximum(Scalar): Source │ └── Accessor: │ └── String: "SeverityNumber" └── PostExpressions: └── Set ├── Source(Scalar): Constant(Reference) │ ├── ValueType: Map │ ├── Id: 0 │ └── Accessor: │ └── GetType(Scalar): Source │ └── Accessor: │ └── String: "Count" └── Destination(Mutable): Source └── Accessor: └── String: "t" ```
1 parent 55cd0f3 commit c10c580

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

rust/experimental/query_engine/expressions/src/scalars/scalar_expressions.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,10 @@ impl Expression for ReferenceConstantScalarExpression {
557557
fn fmt_with_indent(&self, f: &mut std::fmt::Formatter<'_>, indent: &str) -> std::fmt::Result {
558558
writeln!(f, "Constant(Reference)")?;
559559
writeln!(f, "{indent}├── ValueType: {:?}", self.get_value_type())?;
560-
writeln!(f, "{indent}└── Id: {}", self.get_constant_id())?;
560+
writeln!(f, "{indent}├── Id: {}", self.get_constant_id())?;
561+
write!(f, "{indent}└── Accessor: ")?;
562+
self.accessor
563+
.fmt_with_indent(f, format!("{indent} ").as_str())?;
561564
Ok(())
562565
}
563566
}

rust/experimental/query_engine/expressions/src/summary/summary_data_expression.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,72 @@ impl Expression for SummaryDataExpression {
8282
fn get_name(&self) -> &'static str {
8383
"SummaryDataExpression"
8484
}
85+
86+
fn fmt_with_indent(&self, f: &mut std::fmt::Formatter<'_>, indent: &str) -> std::fmt::Result {
87+
writeln!(f, "Summary")?;
88+
89+
if self.group_by_expressions.is_empty() {
90+
writeln!(f, "{indent}├── GroupBys: None")?;
91+
} else {
92+
writeln!(f, "{indent}├── GroupBys: ")?;
93+
let last_idx = self.group_by_expressions.len() - 1;
94+
for (i, (name, g)) in self.group_by_expressions.iter().enumerate() {
95+
if i == last_idx {
96+
write!(f, "{indent}│ └── {name} = ")?;
97+
g.fmt_with_indent(
98+
f,
99+
format!("{indent}│ {} ", " ".repeat(name.len() + 3)).as_str(),
100+
)?;
101+
} else {
102+
write!(f, "{indent}│ ├── {name} = ")?;
103+
g.fmt_with_indent(
104+
f,
105+
format!("{indent}│ │{} ", " ".repeat(name.len() + 3)).as_str(),
106+
)?;
107+
}
108+
}
109+
}
110+
111+
if self.aggregation_expressions.is_empty() {
112+
writeln!(f, "{indent}├── Aggregations: None")?;
113+
} else {
114+
writeln!(f, "{indent}├── Aggregations: ")?;
115+
let last_idx = self.aggregation_expressions.len() - 1;
116+
for (i, (name, a)) in self.aggregation_expressions.iter().enumerate() {
117+
if i == last_idx {
118+
write!(f, "{indent}│ └── {name} = ")?;
119+
a.fmt_with_indent(
120+
f,
121+
format!("{indent}│ {} ", " ".repeat(name.len() + 3)).as_str(),
122+
)?;
123+
} else {
124+
write!(f, "{indent}│ ├── {name} = ")?;
125+
a.fmt_with_indent(
126+
f,
127+
format!("{indent}│ │{} ", " ".repeat(name.len() + 3)).as_str(),
128+
)?;
129+
}
130+
}
131+
}
132+
133+
if self.post_expressions.is_empty() {
134+
writeln!(f, "{indent}└── PostExpressions: None")?;
135+
} else {
136+
writeln!(f, "{indent}└── PostExpressions: ")?;
137+
let last_idx = self.post_expressions.len() - 1;
138+
for (i, e) in self.post_expressions.iter().enumerate() {
139+
if i == last_idx {
140+
write!(f, "{indent} └── ")?;
141+
e.fmt_with_indent(f, format!("{indent} ").as_str())?;
142+
} else {
143+
write!(f, "{indent} ├── ")?;
144+
e.fmt_with_indent(f, format!("{indent} │ ").as_str())?;
145+
}
146+
}
147+
}
148+
149+
Ok(())
150+
}
85151
}
86152

87153
#[derive(Debug, Clone, PartialEq)]
@@ -132,6 +198,17 @@ impl Expression for AggregationExpression {
132198
fn get_name(&self) -> &'static str {
133199
"AggregationExpression"
134200
}
201+
202+
fn fmt_with_indent(&self, f: &mut std::fmt::Formatter<'_>, indent: &str) -> std::fmt::Result {
203+
if let Some(value) = self.value_expression.as_ref() {
204+
let header = format!("{:?}(Scalar): ", self.aggregation_function);
205+
write!(f, "{header}")?;
206+
value.fmt_with_indent(f, format!("{indent}{}", " ".repeat(header.len())).as_str())?;
207+
} else {
208+
writeln!(f, "{:?}", self.aggregation_function)?;
209+
}
210+
Ok(())
211+
}
135212
}
136213

137214
#[derive(Debug, Clone, PartialEq)]

0 commit comments

Comments
 (0)