1use std::fmt;
5use std::fmt::Display;
6use std::fmt::Formatter;
7
8use vortex_array::expr::display::DisplayTreeNode;
9use vortex_array::expr::display::ROOT_DISPLAY;
10use vortex_array::scalar_fn::ChildName;
11use vortex_array::scalar_fn::display::ExprDisplay;
12
13use crate::Expression;
14
15impl ExprDisplay for Expression {
16 fn display_child(&self, index: usize) -> &dyn ExprDisplay {
17 Expression::child(self, index)
18 }
19
20 fn display_children_count(&self) -> usize {
21 self.children().len()
22 }
23}
24
25impl DisplayTreeNode for Expression {
26 fn tree_children(&self) -> &[Self] {
27 Expression::children(self)
28 }
29
30 fn tree_child_name(&self, index: usize) -> ChildName {
31 match self {
32 Expression::Scalar { scalar_fn, .. } => scalar_fn.signature().child_name(index),
33 Expression::Root => unreachable!("the scope root has no children"),
34 Expression::Variable(_) => unreachable!("a variable has no children"),
35 }
36 }
37
38 fn fmt_tree_node(&self, f: &mut Formatter<'_>) -> fmt::Result {
39 match self {
40 Expression::Scalar { scalar_fn, .. } => Display::fmt(scalar_fn, f),
41 Expression::Root => write!(f, "{ROOT_DISPLAY}"),
42 Expression::Variable(name) => write!(f, "@{name}"),
43 }
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use vortex_array::dtype::DType;
50 use vortex_array::dtype::Nullability;
51 use vortex_array::dtype::PType;
52
53 use crate::and;
54 use crate::cast;
55 use crate::eq;
56 use crate::get_item;
57 use crate::gt;
58 use crate::lit;
59 use crate::not;
60 use crate::root;
61
62 #[test]
63 fn test_display_tree_root() {
64 use insta::assert_snapshot;
65 let root_expr = root();
66 assert_snapshot!(root_expr.display_tree().to_string(), @"vortex.root()");
67 }
68
69 #[test]
70 fn test_display_tree_literal() {
71 use insta::assert_snapshot;
72 let lit_expr = lit(42);
73 assert_snapshot!(lit_expr.display_tree().to_string(), @"vortex.literal(42i32)");
74 }
75
76 #[test]
77 fn test_display_tree_get_item() {
78 use insta::assert_snapshot;
79 let get_item_expr = get_item("my_field", root());
80 assert_snapshot!(get_item_expr.display_tree().to_string(), @r"
81 vortex.get_item(my_field)
82 └── input: vortex.root()
83 ");
84 }
85
86 #[test]
87 fn test_display_tree_binary() {
88 use insta::assert_snapshot;
89 let binary_expr = gt(get_item("x", root()), lit(10));
90 assert_snapshot!(binary_expr.display_tree().to_string(), @r"
91 vortex.compare(>)
92 ├── lhs: vortex.get_item(x)
93 │ └── input: vortex.root()
94 └── rhs: vortex.literal(10i32)
95 ");
96 }
97
98 #[test]
99 fn test_display_tree_complex_binary() {
100 use insta::assert_snapshot;
101 let complex_binary = and(
102 eq(get_item("name", root()), lit("alice")),
103 gt(get_item("age", root()), lit(18)),
104 );
105 assert_snapshot!(complex_binary.display_tree().to_string(), @r#"
106 vortex.boolean(and)
107 ├── lhs: vortex.compare(=)
108 │ ├── lhs: vortex.get_item(name)
109 │ │ └── input: vortex.root()
110 │ └── rhs: vortex.literal("alice")
111 └── rhs: vortex.compare(>)
112 ├── lhs: vortex.get_item(age)
113 │ └── input: vortex.root()
114 └── rhs: vortex.literal(18i32)
115 "#);
116 }
117
118 #[test]
119 fn test_display_tree_cast() {
120 use insta::assert_snapshot;
121 let cast_expr = cast(
122 get_item("value", root()),
123 DType::Primitive(PType::I64, Nullability::NonNullable),
124 );
125 assert_snapshot!(cast_expr.display_tree().to_string(), @r"
126 vortex.cast(i64)
127 └── input: vortex.get_item(value)
128 └── input: vortex.root()
129 ");
130 }
131
132 #[test]
133 fn test_display_tree_not() {
134 use insta::assert_snapshot;
135 let not_expr = not(eq(get_item("active", root()), lit(true)));
136 assert_snapshot!(not_expr.display_tree().to_string(), @r"
137 vortex.not()
138 └── input: vortex.compare(=)
139 ├── lhs: vortex.get_item(active)
140 │ └── input: vortex.root()
141 └── rhs: vortex.literal(true)
142 ");
143 }
144}