Skip to content

fix: dbFetch() applies a UTC offset in TIMESTAMP and TIME columns as SQLite does - #885

Open
krlmlr wants to merge 4 commits into
mainfrom
claude/fix-877-negative-utc-offset
Open

krlmlr wants to merge 4 commits into
mainfrom
claude/fix-877-negative-utc-offset

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

With extended_types = TRUE, dbFetch() misread text in TIMESTAMP, DATETIME and TIME columns that ends in a negative UTC offset, and gave no warning. Boost's tokenizer splits on -, so it read the offset as a fraction of a second: '2024-01-02 12:00:00-05:00' came back as 12:00:00.05 UTC. Python's sqlite3 module writes aware timestamps in this form, so a timestamp written anywhere west of UTC came back hours off. SQLite's own datetime() reads the same text as 17:00:00.

The parser now splits a trailing offset off before boost reads the text, then applies the offset. It accepts the forms SQLite's date and time functions accept: Z, or [+-]HH:MM with hours up to 14, with optional whitespace around it. This covers negative and positive offsets, offsets after a fraction, and offsets after a time without seconds. Positive offsets and Z used to give NA with a warning, and now give the instant SQLite reads. A TIME with an offset becomes a time of day in UTC and wraps around midnight, as time() does: '23:00:00-02:00' reads as 01:00:00. The time before the offset must be one that time() accepts: HH:MM[:SS[.SSS]], with hours up to 24 (hour 24 only as 24:00:00). Durations such as '25:00:00-02:00' or '-01:00:00+00:00' therefore give NA with a warning, and '24:00:00+00:00' reads as 00:00:00, as in time(). Without an offset, a TIME is still a duration, so '24:00:00' stays 24 hours. The Extended Types section of ?SQLite now describes the offset. If anything else after the time starts with a sign or Z, such as -0500 or -15:00, the value is NA with the usual warning, which matches SQLite returning NULL for it. Text without an offset parses exactly as before. The offset is applied as SQLite applies it, but the time before it is still read by boost, which accepts some text that SQLite rejects, such as 12:00:60 (#887, already on main).

Out of scope: DATE columns, a T between date and time, and a trailing UTC. These still give NA with a warning, as before. #394 would replace the boost parser altogether.

Tests: four new test_that() blocks in tests/testthat/test-extendedTypes.R. They read offsets in DATETIME and TIME columns, compare the result with SQLite's datetime() and time() on the same rows, and check that the offsets and TIME values SQLite rejects give NA with a warning. All four fail on main and pass with this change. devtools::test() with NOT_CRAN=true gives no new failures. The test-httpvfs.R errors happen on main as well (#832, #837). R CMD check --no-manual reports only a NOTE about the worktree's .git file.

Where it sits: this is based on main, outside the Arrow stack. The Arrow arrays of #861 share this parser and pin today's misreadings in their tests. Once this is merged up, #861 needs its expectations updated, and the parsing helpers it adds need the same offset handling.

Review and limitations: review, Accepted limitations.

Closes #877.

Coordinating session: https://claude.ai/code/session_01Hr1rd7mthffYErGmWTGDfQ


Generated by Claude Code

claude and others added 3 commits November 13, 2026 18:16
…s SQLite does

With `extended_types = TRUE`,
text ending in a negative UTC offset such as `-05:00` was misread without a warning,
because boost took the offset for a fraction of a second.
The parser now splits off a trailing offset in the forms SQLite's date functions accept,
`Z` or `[+-]HH:MM` with hours up to 14,
and applies it, so the result matches `datetime()` and `time()`.
A time of day with an offset wraps around midnight, as in `time()`.
Other text after the time that starts with a sign or `Z`,
such as `-0500`, gives `NA` with a warning, as SQLite gives `NULL`.

Closes #877.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7mthffYErGmWTGDfQ
`split_utc_offset()` applied the offset to any duration boost reads,
so `'25:00:00-02:00'` became 03:00:00
and `'-01:00:00+00:00'` became 23:00:00,
where SQLite's `time()` gives `NULL`.
A TIME with an offset now has to be `HH:MM[:SS[.SSS]]`
with hours up to 24, minutes and seconds up to 59,
and hour 24 only as 24:00:00,
otherwise it gives `NA` with a warning.
`'24:00:00+00:00'` reads as 00:00:00, as in `time()`;
without an offset, `'24:00:00'` stays a duration of 24 hours, as before.

Document in the Extended Types section that a trailing offset is applied
and that a TIME with an offset becomes a time of day in UTC.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7mthffYErGmWTGDfQ

krlmlr commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Review of this pull request at ec9d698, by a Claude Code reviewer with clean context that read issue #877, the description of #885, its diff against main (53f2693), the parser code around it in src/SqliteColumnDataSource.cpp and the new tests; built the pull request and its base into separate libraries; ran the changed tests against both, each new block alone after pkgload::load_all(), the full suite on both, a 63-row edge-case table against SQLite's own functions, and the new code under valgrind. I posted nothing myself.

What was run

  • R CMD INSTALL of the pull request and of main into separate scratch libraries.
  • tests/testthat/test-extendedTypes.R against both: all four new blocks fail on main (7 expectations) and pass on the pull request.
  • Each of the four new test_that() blocks alone in a fresh R session after pkgload::load_all(): all pass.
  • devtools::test() with NOT_CRAN=true on both: 818 passed on the pull request against 809 on main, 0 failures on either. Both show the three encoding warnings (zh_TW locale), the three test-httpvfs.R problems (test: Run the HTTP VFS tests for real, and make their errors fail the check #832, testthat hides a test's error when the test records another result after it #837), and one error in "interrupt in busy handler", which is the callr child not finding RSQLite in the default library; with R_LIBS set to the pull request's library, test-dbConnect.R passes 33/33.
  • A table of 41 DATETIME and 22 TIME strings, each read through dbGetQuery() and compared in the same database with unixepoch(x, 'subsec') and time(x, 'subsec'), on both builds.
  • valgrind (R -d valgrind) on the new tests, the edge table and malformed inputs (12:00:00+, -0, -05:, -05:0, a lone Z, x:, ::-, 5000 colons, 5000 spaces before Z, empty string): no errors.

What holds

  • On the table, the pull request agrees with SQLite on every input that carries an offset SQLite accepts: negative and positive offsets, +14:00/-14:00, +00:00/-00:00, Z and z, whitespace before and after the offset including tab and newline, fractions before the offset, HH:MM without seconds, and day, month, year, leap-day (2024-02-28 to 02-29, 2023-02-28 to 03-01) and pre-1970 crossings. On main 32 of 41 DATETIME rows disagreed; on the pull request 6 do, none of them involving an accepted offset (see Low 2 and 3; the other two are date-only text and a T separator, out of scope).
  • Offsets SQLite rejects (-15:00, -0500, -05, -05:60, +5:00, Zx, -01:00Z, trailing junk) give NA with the usual warning, as SQLite gives NULL.
  • Text with no +, -, Z or z after its first colon takes exactly the old path: split_utc_offset() returns the string unchanged and boost receives the same characters. The only inputs that change without a real offset are offset-like strings such as -05, which used to be read as a fraction and now give NA, as the description says.
  • The positive-offset and Z widening is consistent across DATETIME and TIME and matches SQLite, and the description states it.
  • Bounds in split_utc_offset() are checked before indexing (text.size() < pos + 6 before reading pos + 5); valgrind confirms.
  • The tests use nothing newer than R 4.2 (.POSIXct(), capture_warnings(), no native pipe or lambda); DESCRIPTION still says R >= 3.1.0. Their 'subsec' modifier and unixepoch() need SQLite >= 3.42, which the bundled 3.54.1 satisfies.
  • The scope matches the title; no src/*.dd file changes; the description matches the code.

High

None.

Medium

None.

Low

  1. A TIME with an offset wraps durations SQLite rejects into plausible times of day, without a warning. src/SqliteColumnDataSource.cpp#L263-L272. bp::duration_from_string() accepts any duration, and the fmod() then folds it into one day: '25:00:00-02:00' reads as 03:00:00, '100:00:00-01:00' as 05:00:00, '12:60:00-01:00' as 14:00:00 and '-01:00:00+00:00' as 23:00:00, where time() gives NULL for all four. It is also inconsistent with the same text without an offset: '24:00:00' stays 24:00:00 but '24:00:00+00:00' becomes 00:00:00 (Confirmed). Introduced by the pull request: on main the first three gave other wrong values (25:00:00.02 and so on) and '-01:00:00+00:00' gave NA with a warning, so that one goes from a warning to a silent value. Suggested fix: when has_offset is true, require 0 <= secs < 24 h (or <= 24 h, as SQLite allows hour 24) before applying the offset and throw otherwise, so these give NA with the warning, as the description promises for text SQLite rejects; add one such row to the TIME test.

  2. The Extended Types documentation does not mention offsets, Z, or that a TIME with an offset becomes a UTC time of day. R/dbConnect_SQLiteDriver.R#L45-L55 (by reading). The table still says "HMS formatted string (e.g. 12:34:56)" and "DATE and TIME as above separated by a space". Positive offsets and Z, which used to give NA, now parse, and TIME now changes meaning when an offset is present (a duration without one, a UTC time of day with one); a user would learn this only from NEWS. Introduced by the pull request, in the sense that it widens behaviour the page does not cover. Suggested fix: add a sentence below the table, e.g. "Text may end in a UTC offset (Z or [+-]HH:MM), which is applied as SQLite's datetime() and time() do; a TIME with an offset becomes a time of day in UTC.", and re-render man/SQLite.Rd.

  3. Boost's leniency still accepts some times SQLite rejects, now with the offset applied. src/SqliteColumnDataSource.cpp#L224-L231. '2024-01-02 12:00:60-02:00' reads as 14:01:00, '2024-01-02 1:00:00-01:00' as 02:00:00 and '2024-01-02 12:00:00.-01:00' as 13:00:00, where datetime() gives NULL (Confirmed). Already present on main: without an offset, main reads '12:00:60' as 12:01:00, '1:00:00' as 01:00:00 and '12:00:00.' as 12:00:00 while datetime() gives NULL. So "agrees with SQLite" holds for the offset, not for the time before it. Suggested fix: none needed here; leave it to Get rid of boost::date_time #394, and perhaps word the description as "applies the offset as SQLite does" rather than claiming the whole parse matches.


Generated by Claude Code

krlmlr commented Nov 13, 2026 •

Copy link
Copy Markdown
Member Author

Superseded: Accepted limitations

Superseded by #885 (comment).

One review round, found nothing above Low. 9343c20 fixes both Low findings the pull request introduced. A TIME with an offset must now be a time of day that SQLite's time() accepts, so '25:00:00-02:00' and '-01:00:00+00:00' give NA with a warning. The Extended Types documentation now describes the offset. Nothing introduced by this pull request remains open.


Generated by Claude Code

@krlmlr

krlmlr commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Is there a Boost update that we could vendor that would fix the problem too?

krlmlr commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

No, I don't think so. The Boost headers in src/vendor/boost are Boost 1.81, copied with bcp from BH 1.81.0-1 in 12bad0d (#450). The parser calls boost::date_time::parse_delimited_time() for TIMESTAMP and boost::posix_time::duration_from_string() for TIME. Both go through str_from_delimited_time_duration(), which treats -, :, , and . as delimiters and reads at most four numbers. It has no concept of a time zone offset: -05:00 becomes a fraction of a second, and Z or +05:00 make lexical_cast throw. That code has not changed since then. git diff boost-1.81.0 boost-1.92.0 -- include in boostorg/date_time touches only period.hpp, and the develop branch is the same. Boost.DateTime has one other parser, time_input_facet, and even there the ISO offset flags %q/%Q/%z are commented out for input. Only %ZP remains, which reads a POSIX TZ string into a local_date_time and not an ISO offset.

I also checked this by compiling the same calls against the vendored 1.81 headers and against BH 1.90.0-1 from CRAN. The output was identical for every input. 12:00:00-05:00 gives 12:00:00.05, and Z and +05:00 throw (#877). 12:00:60 rolls over to 12:01:00, and 1:00:00 and a trailing . are accepted (#887). 12:00:00.123-05:00 silently drops the offset. A Boost update would therefore fix neither #877 nor #887, and it would still cost something. The vendored tree is about 20 MB (1554 files) and is regenerated with bcp, then the local sprintf patch from a8d0f58 has to be applied again. Each such update is a large diff to review for CRAN warnings on every compiler. The alternative, going back to LinkingTo: BH, would undo #362 and tie RSQLite to whatever BH version CRAN has. If we want the parser to agree with SQLite, the way forward is #885 for the offset now, then #394: a small parser of our own for SQLite's time value formats. That would also fix #887 and remove the last real use of boost::date_time.


Generated by Claude Code

krlmlr commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

One review round, found nothing above Low. 9343c20 fixes both Low findings the pull request introduced. A TIME with an offset must now be a time of day that SQLite's time() accepts, so '25:00:00-02:00' and '-01:00:00+00:00' give NA with a warning. The Extended Types documentation now describes the offset. Nothing introduced by this pull request remains open.


Generated by Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dbFetch() misreads a negative UTC offset in a TIMESTAMP or TIME column, without a warning

2 participants