Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not panic if time is invalid #12466

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ fn date_range(s: &[Series], interval: Duration, closed: ClosedWindow) -> PolarsR
ensure_range_bounds_contain_exactly_one_value(start, end)?;

let dtype = DataType::Date;
let start = temporal_series_to_i64_scalar(start) * MILLISECONDS_IN_DAY;
let end = temporal_series_to_i64_scalar(end) * MILLISECONDS_IN_DAY;
let start = temporal_series_to_i64_scalar(start)
.ok_or_else(|| polars_err!(ComputeError: "start is an out-of-range time."))?
* MILLISECONDS_IN_DAY;
let end = temporal_series_to_i64_scalar(end)
.ok_or_else(|| polars_err!(ComputeError: "end is an out-of-range time."))?
* MILLISECONDS_IN_DAY;

let result = datetime_range_impl(
"date",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ pub(super) fn datetime_range(
_ => {},
};

let start = temporal_series_to_i64_scalar(&start);
let end = temporal_series_to_i64_scalar(&end);
let start = temporal_series_to_i64_scalar(&start)
.ok_or_else(|| polars_err!(ComputeError: "start is an out-of-range time."))?;
let end = temporal_series_to_i64_scalar(&end)
.ok_or_else(|| polars_err!(ComputeError: "end is an out-of-range time."))?;

let result = match dtype {
DataType::Datetime(tu, ref tz) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ pub(super) fn time_range(
ensure_range_bounds_contain_exactly_one_value(start, end)?;

let dtype = DataType::Time;
let start = temporal_series_to_i64_scalar(&start.cast(&dtype)?);
let end = temporal_series_to_i64_scalar(&end.cast(&dtype)?);
let start = temporal_series_to_i64_scalar(&start.cast(&dtype)?)
.ok_or_else(|| polars_err!(ComputeError: "start is an out-of-range time."))?;
let end = temporal_series_to_i64_scalar(&end.cast(&dtype)?)
.ok_or_else(|| polars_err!(ComputeError: "end is an out-of-range time."))?;

let out = time_range_impl("time", start, end, interval, closed)?;
Ok(out.cast(&dtype).unwrap().into_series())
Expand Down
8 changes: 2 additions & 6 deletions crates/polars-plan/src/dsl/function_expr/range/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use polars_core::prelude::{polars_bail, polars_ensure, PolarsResult};
use polars_core::series::Series;

pub(super) fn temporal_series_to_i64_scalar(s: &Series) -> i64 {
s.to_physical_repr()
.get(0)
.unwrap()
.extract::<i64>()
.unwrap()
pub(super) fn temporal_series_to_i64_scalar(s: &Series) -> Option<i64> {
s.to_physical_repr().get(0).unwrap().extract::<i64>()
}

pub(super) fn ensure_range_bounds_contain_exactly_one_value(
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/unit/functions/range/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def test_date_range_invalid_time_unit() -> None:
)


def test_date_range_invalid_time() -> None:
with pytest.raises(pl.ComputeError, match="end is an out-of-range time"):
pl.date_range(pl.date(2024, 1, 1), pl.date(2024, 2, 30), eager=True)


def test_date_range_lazy_with_literals() -> None:
df = pl.DataFrame({"misc": ["x"]}).with_columns(
pl.date_ranges(
Expand Down