1use datafusion_common::ScalarValue;
17use datafusion_proto_common::{FromProtoError, ToProtoError};
18use datafusion_proto_models::protobuf;
19
20use crate::dml::MergeIntoClauseKind;
21use crate::expr::NullTreatment;
22use crate::{WindowFrame, WindowFrameBound, WindowFrameUnits};
23
24impl From<protobuf::WindowFrameUnits> for WindowFrameUnits {
25 fn from(units: protobuf::WindowFrameUnits) -> Self {
26 match units {
27 protobuf::WindowFrameUnits::Rows => Self::Rows,
28 protobuf::WindowFrameUnits::Range => Self::Range,
29 protobuf::WindowFrameUnits::Groups => Self::Groups,
30 }
31 }
32}
33
34impl From<WindowFrameUnits> for protobuf::WindowFrameUnits {
35 fn from(units: WindowFrameUnits) -> Self {
36 match units {
37 WindowFrameUnits::Rows => Self::Rows,
38 WindowFrameUnits::Range => Self::Range,
39 WindowFrameUnits::Groups => Self::Groups,
40 }
41 }
42}
43
44impl TryFrom<protobuf::WindowFrameBound> for WindowFrameBound {
45 type Error = FromProtoError;
46
47 fn try_from(bound: protobuf::WindowFrameBound) -> Result<Self, Self::Error> {
48 let bound_type =
49 protobuf::WindowFrameBoundType::try_from(bound.window_frame_bound_type)
50 .map_err(|_| {
51 FromProtoError::unknown(
52 "WindowFrameBoundType",
53 bound.window_frame_bound_type,
54 )
55 })?;
56 match bound_type {
57 protobuf::WindowFrameBoundType::CurrentRow => Ok(Self::CurrentRow),
58 protobuf::WindowFrameBoundType::Preceding => match bound.bound_value {
59 Some(x) => Ok(Self::Preceding(ScalarValue::try_from(&x)?)),
60 None => Ok(Self::Preceding(ScalarValue::UInt64(None))),
61 },
62 protobuf::WindowFrameBoundType::Following => match bound.bound_value {
63 Some(x) => Ok(Self::Following(ScalarValue::try_from(&x)?)),
64 None => Ok(Self::Following(ScalarValue::UInt64(None))),
65 },
66 }
67 }
68}
69
70impl TryFrom<&WindowFrameBound> for protobuf::WindowFrameBound {
71 type Error = ToProtoError;
72
73 fn try_from(bound: &WindowFrameBound) -> Result<Self, Self::Error> {
74 Ok(match bound {
75 WindowFrameBound::CurrentRow => Self {
76 window_frame_bound_type: protobuf::WindowFrameBoundType::CurrentRow
77 .into(),
78 bound_value: None,
79 },
80 WindowFrameBound::Preceding(v) => Self {
81 window_frame_bound_type: protobuf::WindowFrameBoundType::Preceding.into(),
82 bound_value: Some(v.try_into()?),
83 },
84 WindowFrameBound::Following(v) => Self {
85 window_frame_bound_type: protobuf::WindowFrameBoundType::Following.into(),
86 bound_value: Some(v.try_into()?),
87 },
88 })
89 }
90}
91
92impl TryFrom<protobuf::WindowFrame> for WindowFrame {
93 type Error = FromProtoError;
94
95 fn try_from(window: protobuf::WindowFrame) -> Result<Self, Self::Error> {
96 let units = WindowFrameUnits::from(
97 protobuf::WindowFrameUnits::try_from(window.window_frame_units).map_err(
98 |_| {
99 FromProtoError::unknown("WindowFrameUnits", window.window_frame_units)
100 },
101 )?,
102 );
103 let start_bound = WindowFrameBound::try_from(
104 window
105 .start_bound
106 .ok_or_else(|| FromProtoError::required("start_bound"))?,
107 )?;
108 let end_bound = window
109 .end_bound
110 .map(|end_bound| match end_bound {
111 protobuf::window_frame::EndBound::Bound(end_bound) => {
112 WindowFrameBound::try_from(end_bound)
113 }
114 })
115 .transpose()?
116 .unwrap_or(WindowFrameBound::CurrentRow);
117 Ok(WindowFrame::new_bounds(units, start_bound, end_bound))
118 }
119}
120
121impl TryFrom<&WindowFrame> for protobuf::WindowFrame {
122 type Error = ToProtoError;
123
124 fn try_from(window: &WindowFrame) -> Result<Self, Self::Error> {
125 Ok(Self {
126 window_frame_units: protobuf::WindowFrameUnits::from(window.units).into(),
127 start_bound: Some((&window.start_bound).try_into()?),
128 end_bound: Some(protobuf::window_frame::EndBound::Bound(
129 (&window.end_bound).try_into()?,
130 )),
131 })
132 }
133}
134
135impl From<protobuf::merge_into_clause_node::Kind> for MergeIntoClauseKind {
136 fn from(kind: protobuf::merge_into_clause_node::Kind) -> Self {
137 match kind {
138 protobuf::merge_into_clause_node::Kind::Matched => Self::Matched,
139 protobuf::merge_into_clause_node::Kind::NotMatched => Self::NotMatched,
140 protobuf::merge_into_clause_node::Kind::NotMatchedByTarget => {
141 Self::NotMatchedByTarget
142 }
143 protobuf::merge_into_clause_node::Kind::NotMatchedBySource => {
144 Self::NotMatchedBySource
145 }
146 }
147 }
148}
149
150impl From<MergeIntoClauseKind> for protobuf::merge_into_clause_node::Kind {
151 fn from(kind: MergeIntoClauseKind) -> Self {
152 match kind {
153 MergeIntoClauseKind::Matched => Self::Matched,
154 MergeIntoClauseKind::NotMatched => Self::NotMatched,
155 MergeIntoClauseKind::NotMatchedByTarget => Self::NotMatchedByTarget,
156 MergeIntoClauseKind::NotMatchedBySource => Self::NotMatchedBySource,
157 }
158 }
159}
160
161impl From<protobuf::NullTreatment> for NullTreatment {
162 fn from(t: protobuf::NullTreatment) -> Self {
163 match t {
164 protobuf::NullTreatment::RespectNulls => Self::RespectNulls,
165 protobuf::NullTreatment::IgnoreNulls => Self::IgnoreNulls,
166 }
167 }
168}
169
170impl From<NullTreatment> for protobuf::NullTreatment {
171 fn from(t: NullTreatment) -> Self {
172 match t {
173 NullTreatment::RespectNulls => Self::RespectNulls,
174 NullTreatment::IgnoreNulls => Self::IgnoreNulls,
175 }
176 }
177}
178
179#[cfg(test)]
180mod tests {
181 use super::*;
182
183 #[test]
184 fn window_frame_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
185 let frame = WindowFrame::new_bounds(
186 WindowFrameUnits::Range,
187 WindowFrameBound::Preceding(ScalarValue::UInt64(Some(2))),
188 WindowFrameBound::Following(ScalarValue::UInt64(Some(3))),
189 );
190
191 let encoded = protobuf::WindowFrame::try_from(&frame)?;
192 let decoded = WindowFrame::try_from(encoded)?;
193
194 assert_eq!(decoded.units, frame.units);
195 assert_eq!(decoded.start_bound, frame.start_bound);
196 assert_eq!(decoded.end_bound, frame.end_bound);
197 Ok(())
198 }
199
200 #[test]
201 fn window_frame_from_proto_rejects_missing_start_bound() {
202 let proto = protobuf::WindowFrame {
203 window_frame_units: protobuf::WindowFrameUnits::Rows.into(),
204 start_bound: None,
205 end_bound: None,
206 };
207
208 let err = WindowFrame::try_from(proto).unwrap_err();
209 assert!(
210 err.to_string().contains("start_bound"),
211 "unexpected error: {err}"
212 );
213 }
214
215 #[test]
216 fn missing_end_bound_decodes_as_current_row() -> Result<(), Box<dyn std::error::Error>>
217 {
218 let proto = protobuf::WindowFrame {
219 window_frame_units: protobuf::WindowFrameUnits::Rows.into(),
220 start_bound: Some(protobuf::WindowFrameBound {
221 window_frame_bound_type: protobuf::WindowFrameBoundType::CurrentRow
222 .into(),
223 bound_value: None,
224 }),
225 end_bound: None,
226 };
227
228 let decoded = WindowFrame::try_from(proto)?;
229 assert_eq!(decoded.end_bound, WindowFrameBound::CurrentRow);
230 Ok(())
231 }
232}