1
- use crate :: cir:: {
2
- goal:: { Cost , Goal } ,
3
- group:: GroupId ,
4
- operators:: { Child , Operator , OperatorData } ,
5
- plans:: { LogicalPlan , PartialLogicalPlan , PartialPhysicalPlan } ,
6
- properties:: { LogicalProperties , PhysicalProperties , PropertiesData } ,
7
- } ;
1
+ //! Converts HIR [`Value`]s into optd's type representations (CIR).
2
+
3
+ use crate :: cir:: * ;
8
4
use optd_dsl:: analyzer:: hir:: { self , CoreData , Literal , Materializable , Value } ;
9
5
use std:: sync:: Arc ;
10
6
use Child :: * ;
11
7
use CoreData :: * ;
12
8
use Literal :: * ;
13
9
use Materializable :: * ;
14
10
15
- //=============================================================================
16
- // Main conversion functions
17
- //=============================================================================
18
-
19
- /// Converts a HIR Value into a PartialLogicalPlan representation.
11
+ /// Converts a [`Value`] into a [`PartialLogicalPlan`].
20
12
///
21
- /// Transforms the DSL's HIR into the optimizer's IR for logical operators.
13
+ /// # Panics
14
+ ///
15
+ /// Panics if the [`Value`] is not a [`Logical`] variant.
22
16
pub ( crate ) fn value_to_partial_logical ( value : & Value ) -> PartialLogicalPlan {
23
17
match & value. 0 {
24
18
Logical ( logical_op) => match & logical_op. 0 {
@@ -35,9 +29,11 @@ pub(crate) fn value_to_partial_logical(value: &Value) -> PartialLogicalPlan {
35
29
}
36
30
}
37
31
38
- /// Converts a HIR Value into a PartialPhysicalPlan representation.
32
+ /// Converts a [`Value`] into a [`PartialPhysicalPlan`].
33
+ ///
34
+ /// # Panics
39
35
///
40
- /// Transforms the DSL's HIR into the CIR for physical operators .
36
+ /// Panics if the [`Value`] is not a [`Physical`] variant .
41
37
pub ( crate ) fn value_to_partial_physical ( value : & Value ) -> PartialPhysicalPlan {
42
38
match & value. 0 {
43
39
Physical ( physical_op) => match & physical_op. 0 {
@@ -54,47 +50,57 @@ pub(crate) fn value_to_partial_physical(value: &Value) -> PartialPhysicalPlan {
54
50
}
55
51
}
56
52
57
- /// Converts a HIR Value into a CIR cost.
53
+ /// Converts a [`Value`] into a CIR [`Cost`].
54
+ ///
55
+ /// # Panics
56
+ ///
57
+ /// Panics if the [`Value`] is not a [`Literal`] variant with a [`Float64`] value.
58
58
pub ( crate ) fn value_to_cost ( value : & Value ) -> Cost {
59
59
match & value. 0 {
60
60
Literal ( Float64 ( f) ) => Cost ( * f) ,
61
61
_ => panic ! ( "Expected Float64 literal, found: {:?}" , value. 0 ) ,
62
62
}
63
63
}
64
64
65
- /// Convert HIR properties value to CIR LogicalProperties
65
+ /// Converts an HIR properties [`Value`] into a CIR [` LogicalProperties`].
66
66
pub ( crate ) fn value_to_logical_properties ( properties_value : & Value ) -> LogicalProperties {
67
67
match & properties_value. 0 {
68
68
Null => LogicalProperties ( None ) ,
69
69
_ => LogicalProperties ( Some ( value_to_properties_data ( properties_value) ) ) ,
70
70
}
71
71
}
72
72
73
- /// Converts a HIR GroupId to a CIR GroupId.
73
+ /// Convert an HIR properties [`Value`] into a CIR [`PhysicalProperties`].
74
+ fn value_to_physical_properties ( properties_value : & Value ) -> PhysicalProperties {
75
+ match & properties_value. 0 {
76
+ Null => PhysicalProperties ( None ) ,
77
+ _ => PhysicalProperties ( Some ( value_to_properties_data ( properties_value) ) ) ,
78
+ }
79
+ }
80
+
81
+ /// Converts an HIR [`GroupId`](hir::GroupId) to a CIR [`GroupId`].
74
82
///
75
- /// This function provides a consistent way to convert group identifiers
76
- /// from the HIR representation to the optimizer's internal representation.
83
+ /// This function provides a consistent way to convert group identifiers from the HIR into the
84
+ /// optimizer's internal representation (CIR) .
77
85
pub ( crate ) fn hir_group_id_to_cir ( hir_group_id : & hir:: GroupId ) -> GroupId {
78
86
GroupId ( hir_group_id. 0 )
79
87
}
80
88
81
- /// Converts a HIR Goal to a CIR Goal.
82
- ///
83
- /// Transforms the DSL's HIR Goal into the optimizer's IR Goal representation,
84
- /// handling both the group ID and physical properties components.
89
+ /// Converts an HIR [`Goal`](hir::Goal) to a CIR [`Goal`].
85
90
pub ( crate ) fn hir_goal_to_cir ( hir_goal : & hir:: Goal ) -> Goal {
86
91
let group_id = hir_group_id_to_cir ( & hir_goal. group_id ) ;
87
92
let properties = value_to_physical_properties ( & hir_goal. properties ) ;
88
93
Goal ( group_id, properties)
89
94
}
90
95
91
- //=============================================================================
92
- // HIR to CIR conversion helpers
93
- //=============================================================================
94
-
95
- /// Converts a HIR Value into a complete LogicalPlan (not a partial plan).
96
+ /// Converts a [`Value`] into a fully materialized [`LogicalPlan`].
96
97
///
97
- /// Used when fully materializing a logical expression for use in properties.
98
+ /// We use this function when materializing a logical expression for use in properties.
99
+ ///
100
+ /// # Panics
101
+ ///
102
+ /// Panics if the [`Value`] is not a [`Logical`] variant or if the [`Logical`] variant is not a
103
+ /// [`Materialized`] variant.
98
104
fn value_to_logical ( value : & Value ) -> LogicalPlan {
99
105
match & value. 0 {
100
106
Logical ( logical_op) => match & logical_op. 0 {
@@ -111,15 +117,8 @@ fn value_to_logical(value: &Value) -> LogicalPlan {
111
117
}
112
118
}
113
119
114
- /// Convert HIR properties value to CIR PhysicalProperties
115
- fn value_to_physical_properties ( properties_value : & Value ) -> PhysicalProperties {
116
- match & properties_value. 0 {
117
- Null => PhysicalProperties ( None ) ,
118
- _ => PhysicalProperties ( Some ( value_to_properties_data ( properties_value) ) ) ,
119
- }
120
- }
121
-
122
- /// Converts a Vec of Values to Vec of Children for any target type.
120
+ /// A generic function to convert a slice of [`Value`]s into a vector of mapped results via the
121
+ /// input `converter` function.
123
122
fn convert_values_to_children < T , F > ( values : & [ Value ] , converter : F ) -> Vec < Child < Arc < T > > >
124
123
where
125
124
F : Fn ( & Value ) -> T ,
@@ -128,25 +127,32 @@ where
128
127
values
129
128
. iter ( )
130
129
. map ( |value| match & value. 0 {
131
- Array ( elements) => {
132
- VarLength ( elements. iter ( ) . map ( |el| Arc :: new ( converter ( el) ) ) . collect ( ) )
133
- }
130
+ Array ( elements) => VarLength (
131
+ elements
132
+ . iter ( )
133
+ . map ( |elem| Arc :: new ( converter ( elem) ) )
134
+ . collect ( ) ,
135
+ ) ,
134
136
_ => Singleton ( Arc :: new ( converter ( value) ) ) ,
135
137
} )
136
138
. collect ( )
137
139
}
138
140
139
- /// Converts a slice of HIR Values to a Vec of OperatorData.
141
+ /// Converts a slice of [`Value`]s into a vector of [` OperatorData`] .
140
142
fn convert_values_to_operator_data ( values : & [ Value ] ) -> Vec < OperatorData > {
141
143
values. iter ( ) . map ( value_to_operator_data) . collect ( )
142
144
}
143
145
144
- /// Converts a slice of HIR Values to a Vec of PropertiesData.
146
+ /// Converts a slice of [`Value`]s into a vector of [` PropertiesData`] .
145
147
fn convert_values_to_properties_data ( values : & [ Value ] ) -> Vec < PropertiesData > {
146
148
values. iter ( ) . map ( value_to_properties_data) . collect ( )
147
149
}
148
150
149
- /// Converts a HIR Value to an OperatorData representation.
151
+ /// Converts a [`Value`] into an [`OperatorData`] representation.
152
+ ///
153
+ /// # Panics
154
+ ///
155
+ /// Panics if the [`Value`] cannot be converted to [`OperatorData`], such as a [`Unit`] literal.
150
156
fn value_to_operator_data ( value : & Value ) -> OperatorData {
151
157
match & value. 0 {
152
158
Literal ( constant) => match constant {
@@ -164,7 +170,11 @@ fn value_to_operator_data(value: &Value) -> OperatorData {
164
170
}
165
171
}
166
172
167
- /// Converts a HIR Value to a PropertiesData representation.
173
+ /// Converts a [`Value`] into a [`PropertiesData`] representation.
174
+ ///
175
+ /// # Panics
176
+ ///
177
+ /// Panics if the [`Value`] cannot be converted to [`PropertiesData`], such as a [`Unit`] literal.
168
178
fn value_to_properties_data ( value : & Value ) -> PropertiesData {
169
179
match & value. 0 {
170
180
Literal ( constant) => match constant {
0 commit comments