Skip to content

Commit

Permalink
fix tests, timestmap precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxxen committed Nov 19, 2024
1 parent 3c94123 commit 5f9843f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
25 changes: 16 additions & 9 deletions src/excel/xlsx/read_xlsx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,23 +372,30 @@ static unique_ptr<GlobalTableFunctionState> InitGlobal(ClientContext &context, T

int64_t ExcelToEpochUS(const double serial) {
// Convert to microseconds since epoch
static constexpr auto SECONDS_PER_DAY = 86400;
static constexpr auto MICROSECONDS_PER_SECOND = 1000000;
static constexpr auto DAYS_BETWEEN_1900_AND_1970 = 25569;
static constexpr auto SECONDS_PER_DAY = 86400UL;
static constexpr auto MICROSECONDS_PER_SECOND = 1000000UL;
static constexpr auto DAYS_BETWEEN_1900_AND_1970 = 25569UL;

// Excel serial is days since 1900-01-01
const auto days = serial - DAYS_BETWEEN_1900_AND_1970;
const auto seconds = days * SECONDS_PER_DAY;
const auto micros = seconds * MICROSECONDS_PER_SECOND;
const auto serial_days = serial;
auto serial_secs = serial_days * SECONDS_PER_DAY;

if (std::fabs(serial_secs - std::round(serial_secs)) < 1e-3) {
serial_secs = std::round(serial_secs);
}

const auto epoch_secs = serial_secs - (DAYS_BETWEEN_1900_AND_1970 * SECONDS_PER_DAY);
const auto epoch_micros = epoch_secs * MICROSECONDS_PER_SECOND;

// Clamp to the range. Theres not much we can do if the value is out of range
if (micros <= static_cast<double>(NumericLimits<int64_t>::Minimum())) {
if (epoch_micros <= static_cast<double>(NumericLimits<int64_t>::Minimum())) {
return NumericLimits<int64_t>::Minimum();
}
if (micros >= static_cast<double>(NumericLimits<int64_t>::Maximum())) {
if (epoch_micros >= static_cast<double>(NumericLimits<int64_t>::Maximum())) {
return NumericLimits<int64_t>::Maximum();
}
return static_cast<int64_t>(micros);

return static_cast<int64_t>(epoch_micros);
}

static void TryCast(XLSXGlobalState &state, bool ignore_errors, const idx_t col_idx, ClientContext &context,
Expand Down
2 changes: 1 addition & 1 deletion test/sql/excel/xlsx/limits.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ TO '__TEST_DIR__/test_limit.xlsx' (FORMAT 'XLSX', sheet_name 'test');
----
Invalid Input Error: XLSX: Sheet row limit of '1048576' rows exceeded!
* XLSX files and compatible applications generally have a limit of '1048576' rows
* You can export larger sheets on your own risk by setting the 'sheet_row_limit' parameter to a higher value
* You can export larger sheets at your own risk by setting the 'sheet_row_limit' parameter to a higher value
12 changes: 6 additions & 6 deletions test/sql/excel/xlsx/read_sparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ query I
SELECT * FROM read_xlsx('test/data/xlsx/sparse.xlsx');
----

query I
DESCRIBE SELECT column_name, column_type FROM read_xlsx('test/data/xlsx/sparse.xlsx');
query II
SELECT column_name, column_type FROM (DESCRIBE FROM read_xlsx('test/data/xlsx/sparse.xlsx'));
----
duckdb DOUBLE
duck DOUBLE

# But if we disable the header, we can read the cell
query I
DESCRIBE SELECT column_name, column_type FROM read_xlsx('test/data/xlsx/sparse.xlsx', header = false);
query II
SELECT column_name, column_type FROM (DESCRIBE FROM read_xlsx('test/data/xlsx/sparse.xlsx', header = false));
----
R465
R465 VARCHAR

# Voila!
query I
Expand Down

0 comments on commit 5f9843f

Please sign in to comment.