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

[Python] Fix lossy datetime.timedelta to INTERVAL conversion #9688

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix the lossy nature of the timedelta -> interval conversion
  • Loading branch information
Tishj committed Nov 7, 2023
commit 6c7aaf7c4514e20ecc4d3ee71567b0a54aa42155
15 changes: 1 addition & 14 deletions src/core_functions/scalar/date/to_interval.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "duckdb/core_functions/scalar/date_functions.hpp"
#include "duckdb/common/types/interval.hpp"
#include "duckdb/common/operator/multiply.hpp"
#include "duckdb/core_functions/to_interval.hpp"

namespace duckdb {

Expand Down Expand Up @@ -68,20 +69,6 @@ struct ToMinutesOperator {
}
};

struct ToSecondsOperator {
template <class TA, class TR>
static inline TR Operation(TA input) {
interval_t result;
result.months = 0;
result.days = 0;
if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(input, Interval::MICROS_PER_SEC,
result.micros)) {
throw OutOfRangeException("Interval value %d seconds out of range", input);
}
return result;
}
};

struct ToMilliSecondsOperator {
template <class TA, class TR>
static inline TR Operation(TA input) {
Expand Down
30 changes: 30 additions & 0 deletions src/include/duckdb/core_functions/to_interval.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// duckdb/core_functions/to_interval.hpp
//
//
//===----------------------------------------------------------------------===//

#pragma once

#include "duckdb/function/function_set.hpp"
#include "duckdb/common/operator/multiply.hpp"

namespace duckdb {

struct ToSecondsOperator {
template <class TA, class TR>
static inline TR Operation(TA input) {
interval_t result;
result.months = 0;
result.days = 0;
if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(input, Interval::MICROS_PER_SEC,
result.micros)) {
throw OutOfRangeException("Interval value %d seconds out of range", input);
}
return result;
}
};

} // namespace duckdb
4 changes: 2 additions & 2 deletions tools/pythonpkg/src/include/duckdb_python/python_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ struct PyDecimal {
struct PyTimeDelta {
public:
PyTimeDelta(py::handle &obj);
int64_t days;
int64_t seconds;
int32_t days;
int32_t seconds;
int64_t microseconds;

public:
Expand Down
22 changes: 11 additions & 11 deletions tools/pythonpkg/src/native/python_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "duckdb/common/types/cast_helpers.hpp"
#include "duckdb/common/operator/cast_operators.hpp"
#include "duckdb_python/pyconnection/pyconnection.hpp"
#include "duckdb/common/operator/add.hpp"
#include "duckdb/core_functions/to_interval.hpp"

#include "datetime.h" // Python datetime initialize #1

Expand All @@ -26,19 +28,17 @@ PyTimeDelta::PyTimeDelta(py::handle &obj) {
}

interval_t PyTimeDelta::ToInterval() {
interval_t interval;
interval_t result;

// Timedelta stores any amount of seconds lower than a day only
D_ASSERT(seconds < Interval::SECS_PER_DAY);
auto micros_interval = Interval::FromMicro(microseconds);
auto days_interval = interval_t {/*months = */ 0,
/*days = */ days,
/*micros = */ 0};
auto seconds_interval = ToSecondsOperator::Operation<int64_t, interval_t>(days);

// Convert overflow of days to months
interval.months = days / Interval::DAYS_PER_MONTH;
days -= interval.months * Interval::DAYS_PER_MONTH;

microseconds += seconds * Interval::MICROS_PER_SEC;
interval.days = days;
interval.micros = microseconds;
return interval;
result = AddOperator::Operation<interval_t, interval_t, interval_t>(micros_interval, days_interval);
result = AddOperator::Operation<interval_t, interval_t, interval_t>(result, seconds_interval);
return result;
}

int64_t PyTimeDelta::GetDays(py::handle &obj) {
Expand Down