Skip to content

Commit

Permalink
adding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiayu Liu committed Jun 4, 2021
1 parent d26595f commit 1e09359
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions datafusion/src/physical_plan/window_frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,75 @@ fn get_bound_rank(bound: &WindowFrameBound) -> (u8, u64) {
pub(crate) fn validate_window_frame(
window_frame: &Option<WindowFrame>,
) -> Result<&WindowFrame> {
let window_frame = window_frame
.as_ref()
.unwrap_or_else(|| &DEFAULT_WINDOW_FRAME);
let window_frame: &WindowFrame =
window_frame.as_ref().unwrap_or(&DEFAULT_WINDOW_FRAME);
let start_bound = &window_frame.start_bound;
let end_bound = window_frame
.end_bound
.as_ref()
.unwrap_or_else(|| &WindowFrameBound::CurrentRow);
match (&window_frame.start_bound, end_bound) {
(WindowFrameBound::Following(None),_)=>{
Err(DataFusionError::Execution(
"Invalid window frame: start bound cannot be unbounded following".to_owned()
.unwrap_or(&WindowFrameBound::CurrentRow);

if let WindowFrameBound::Following(None) = *start_bound {
Err(DataFusionError::Execution(
"Invalid window frame: start bound cannot be unbounded following".to_owned(),
))
},
(_,WindowFrameBound::Preceding(None))=>{Err(DataFusionError::Execution(
"Invalid window frame: end bound cannot be unbounded preceding".to_owned()
))},
(start, end) if get_bound_rank(start) > get_bound_rank(end) => {
} else if let WindowFrameBound::Preceding(None) = *end_bound {
Err(DataFusionError::Execution(
format!("Invalid window frame: start bound ({}) cannot be larger than end bound ({})", start, end)
"Invalid window frame: end bound cannot be unbounded preceding".to_owned(),
))
},
_ => Ok(window_frame),
}
} else if get_bound_rank(start_bound) > get_bound_rank(end_bound) {
Err(DataFusionError::Execution(format!(
"Invalid window frame: start bound ({}) cannot be larger than end bound ({})",
start_bound, end_bound
)))
} else {
Ok(window_frame)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_validate_window_frame() -> Result<()> {
let default_value = validate_window_frame(&None)?;
assert_eq!(default_value, &DEFAULT_WINDOW_FRAME);

let window_frame = Some(WindowFrame {
units: WindowFrameUnits::Range,
start_bound: WindowFrameBound::Following(None),
end_bound: None,
});
let result = validate_window_frame(&window_frame);
assert_eq!(
result.err().unwrap().to_string(),
"Execution error: Invalid window frame: start bound cannot be unbounded following".to_owned()
);

let window_frame = Some(WindowFrame {
units: WindowFrameUnits::Range,
start_bound: WindowFrameBound::Preceding(None),
end_bound: Some(WindowFrameBound::Preceding(None)),
});
let result = validate_window_frame(&window_frame);
assert_eq!(
result.err().unwrap().to_string(),
"Execution error: Invalid window frame: end bound cannot be unbounded preceding".to_owned()
);
let window_frame = Some(WindowFrame {
units: WindowFrameUnits::Range,
start_bound: WindowFrameBound::Preceding(Some(1)),
end_bound: Some(WindowFrameBound::Preceding(Some(2))),
});
let result = validate_window_frame(&window_frame);
assert_eq!(
result.err().unwrap().to_string(),
"Execution error: Invalid window frame: start bound (1 PRECEDING) cannot be larger than end bound (2 PRECEDING)".to_owned()
);
Ok(())
}

#[test]
fn test_get_bound_rank_eq() {
assert_eq!(
Expand Down

0 comments on commit 1e09359

Please sign in to comment.