Skip to content

Commit 93bb81c

Browse files
authored
Don't coalesce unless there are multiple component IDs (#9)
2 parents 1ed2a5b + e2b104a commit 93bb81c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/graph/formulas/generators/generic/coalesce.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ impl CoalesceFormulaBuilder {
3535
/// Generates a formula that uses the `COALESCE` function to return the first
3636
/// non-null value from the provided component IDs.
3737
pub fn build(self) -> Result<String, Error> {
38+
if self.component_ids.len() == 1 {
39+
if let Some(component_id) = self.component_ids.into_iter().next() {
40+
return Ok(Expr::Component { component_id }.to_string());
41+
} else {
42+
return Err(Error::internal(
43+
"Failed to create expression for single component ID.",
44+
));
45+
}
46+
}
3847
let expr = Expr::coalesce(
3948
self.component_ids
4049
.into_iter()
@@ -65,7 +74,7 @@ mod tests {
6574
let formula = graph.coalesce(BTreeSet::from([1, 2]))?;
6675
assert_eq!(formula, "COALESCE(#1, #2)");
6776
let formula = graph.coalesce(BTreeSet::from([1]))?;
68-
assert_eq!(formula, "COALESCE(#1)");
77+
assert_eq!(formula, "#1");
6978
let formula = graph.coalesce(BTreeSet::from([]));
7079
assert_eq!(
7180
formula,

0 commit comments

Comments
 (0)