Skip to content

fix: Fail on infinite and out-of-range time values instead of storing garbage - #623

Merged
krlmlr merged 12 commits into
mainfrom
claude/bind-nonfinite-times
Nov 29, 2026
Merged

krlmlr merged 12 commits into
mainfrom
claude/bind-nonfinite-times

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #613, closes #627.

Before this change, Inf, -Inf and huge time values were bound without a check, and the server stored garbage (0000-00-00 00:00:00, or TIME values that depend on the client library). The conversion in C++ was undefined behavior for these values. Date values were affected too: Inf was stored as 0000-00-00.

Change

  • sql_data() calls a new time_to_finite() in R/coerce.R, after posixlt_to_posixct(). csv_quote() in R/table.R calls it too, so the load_data_local_infile = TRUE path fails on infinite and out-of-range values, as binding does. That path also converts POSIXlt columns to POSIXct first: before, dbAppendTable() treated them as list columns and wrote NULL for every value (POSIXlt columns are written as NULL with load_data_local_infile = TRUE #627). There is no C++ change.
  • Inf and -Inf in POSIXct, difftime/hms and Date fail with "Can't represent N infinite time value(s) in column(s) ts: use NA for missing values.", as decided in the question below. NA and NaN are written as NULL. feat!: Fail on infinite numbers instead of writing NULL #636, stacked on this PR, does the same for numeric columns, which is a breaking change.
  • Finite POSIXct, hms/difftime and Date values beyond 9.2e12 seconds can't be represented in the 64-bit microsecond count. As you asked, they now fail with an error on every write path: dbBind(), sqlData() and load_data_local_infile = TRUE. The error reads "Can't represent 1 time value(s) in column(s) ts: the absolute value must not exceed 9.2e+12 seconds." Unnamed parameters are labeled by position. The limit is checked in seconds, whatever the unit of a difftime.
  • dbWriteTable() computes the data once, so with overwrite = TRUE a value that can't be represented fails after the old table is dropped, like other write errors.

Accepted limitations

Tests

tests/testthat/test-bind-nonfinite.R (new) covers Inf, -Inf, 1e13, -1e13 and 0 in DATETIME(6), TIME(6) and DATE, and checks that NA and NaN are still written as NULL. It checks the error for out-of-range values on every write path, including sqlData(), a difftime in minutes, and dbWriteTable() and dbAppendTable() with load_data_local_infile = TRUE, where no rows are written. It also covers a POSIXlt column on that path. Local run against MariaDB 10.11 with NOT_CRAN=true, before Inf was made to fail (the local test server is down since; CI runs the tests of the final version): the whole suite had 0 failures and the one test-timezone.R error ("US/Alaska" unknown to CCTZ in this container), which also happens on main. MySQL servers were not tested: there is no server in this container, and CI's MySQL entries currently run MariaDB (#592).

Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ


Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Superseded: Accepted limitations

Three review rounds. The first found a Medium: the load_data_local_infile = TRUE path skipped the new check. That is fixed in 5ae2913. The second found that path silent in dbWriteTable(), which is fixed in 5eeaa55 together with #627. The third found nothing above Low.

  • R/table.R: the load_data_local_infile = TRUE path always warns about dropped time values, whatever warn_factor says. The prepared path follows warn. Callers that silence factor warnings now still see this one, which is intended, since data is dropped. Relevance: low. Reviewer confidence: high.
  • R/coerce.R: the warning counts values across all columns, without naming them. Relevance: low. Reviewer confidence: high.
  • Dates after year 9999 but within the 64-bit limit still reach the server, which rejects them or stores zero dates, as before. Relevance: low. Reviewer confidence: high.
  • tests/testthat/test-bind-nonfinite.R: the tests that write through load_data_local_infile use UTC only. The POSIXlt test is titled "(Non-finite and out-of-range POSIXct and hms values are bound as garbage #613)" but covers POSIXlt columns are written as NULL with load_data_local_infile = TRUE #627, and doesn't check that Inf in a POSIXlt goes without a warning. A reviewer checked non-UTC connections by hand with identical results. Relevance: low. Reviewer confidence: high.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 12:31
Base automatically changed from claude/bind-time-hours to main November 29, 2026 13:56
@krlmlr
krlmlr force-pushed the claude/bind-nonfinite-times branch from 5eeaa55 to 4f1933b Compare November 29, 2026 13:56
@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Resolve conflicts. Fail instead of warning if encountering values that can't be represented.

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Resolved: Inf and -Inf in time columns

Following the request above, finite time values that can't be represented (beyond about 9.2e12 seconds, the range of the 64-bit microsecond count) will now fail with an error on every write path, instead of becoming NULL with a warning. That is in progress, together with resolving the conflicts. Inf and -Inf can't be represented either. For numbers, numeric_to_finite() binds them as NULL without a warning, and this PR does the same for time values.

  • Keep Inf/-Inf → NULL silently, consistent with numbers (recommended)
  • Fail on Inf/-Inf in time columns too; this is stricter, but inconsistent with numeric columns unless numeric_to_finite() changes as well
  • Fail on Inf/-Inf in both time and numeric columns; consistent and strict, but a behavior change for numbers that is outside this PR

If nobody decides, Inf/-Inf stay NULL.


Generated by Claude Code

…g `NULL`

Finite `POSIXct`, `hms`, `difftime`, and `Date` values
beyond the 64-bit microsecond range now raise an error
that names the affected columns,
in `dbBind()`, `sqlData()`, and writes with `load_data_local_infile = TRUE`.
Infinite values are still written as `NULL` without a warning.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Done in 1db2f5d, ac7369f and 1695af2. The conflicts with main are resolved, so the diff shows only this PR's files. Finite time values out of range now fail on every write path, with "Can't represent N time value(s) in column(s) x: the absolute value must not exceed 9.2e+12 seconds.". With load_data_local_infile = TRUE, no rows are written in that case. Inf stays NULL, pending the question above. A clean review of the change is running.


Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Superseded: Accepted limitations

Three review rounds before the requested change. The first found a Medium: the load_data_local_infile = TRUE path skipped the check. The second found a Medium: dbWriteTable() was silent on that path. Both are fixed. The change to an error (ac7369f, 1695af2) had a clean review of its own, which found nothing above Low. No partial writes happen: with prepared statements the whole column is checked before the first row is bound, and with load_data_local_infile = TRUE the check runs before the file is written.

  • R/coerce.R: via dbWriteTable() and dbAppendTable() with prepared statements, the error labels columns by position (#2), because DBI's dbAppendTable() binds an unnamed list. sqlData() and the load_data_local_infile path use names. Relevance: low. Reviewer confidence: high.
  • A non-temporary dbWriteTable() leaves an empty table behind when the error comes after CREATE TABLE, as any other bind error does, because DDL commits implicitly. Relevance: low. Reviewer confidence: high.
  • Values between the server's date range and the 9.2e12 s limit still reach the server, which stores them as NULL, as before. Relevance: low. Reviewer confidence: high.
  • The warning about dropped time values is gone, since such values now fail. The tests write through load_data_local_infile in UTC only. Relevance: low. Reviewer confidence: high.

Generated by Claude Code

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Implement strict variant here, stacked PR for breaking changes in existing behavior.

Infinite `POSIXct`, `hms`, `difftime`, and `Date` values now raise an error
that names the affected columns,
in `dbBind()`, `sqlData()`, and writes with `load_data_local_infile = TRUE`,
instead of being written as `NULL`.
Missing values are still written as `NULL`.

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

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Superseded: Accepted limitations

Three review rounds before the requested changes, with two Mediums fixed. The change to an error for values out of range had a clean review of its own, which found nothing above Low. So did the change to fail on Inf (8ff6134). One Low is fixed anyway, because it could lose data. dbWriteTable(overwrite = TRUE) dropped and recreated the old table before the check failed, so dbWriteTable() now checks the data first (e375e23). The temporary file of the load_data_local_infile path is also removed on error.

  • R/coerce.R: via dbAppendTable() with prepared statements, and for unnamed parameters, the error labels columns by position (#2). Relevance: low. Reviewer confidence: high.
  • A non-temporary dbWriteTable() without overwrite still leaves an empty table behind if another error, such as one from the server, comes after CREATE TABLE, because DDL commits implicitly. Relevance: low. Reviewer confidence: high.
  • Values between the server's date range and the 9.2e12 s limit still reach the server, which stores them as NULL, as before. Relevance: low. Reviewer confidence: high.
  • The local test server is down, so the database tests for Inf and for the old table surviving have not run here; CI runs them. Relevance: medium until CI is green. Reviewer confidence: high.

Generated by Claude Code

stopc("Cannot specify `field.types` with `append = TRUE`")
}

# Fail on values that can't be written before the old table is dropped:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accept the dropped old table, don't compute twice.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1c704b8: dbWriteTable() computes the data only once again, and the test that kept the old table is gone. A time value that can't be represented now fails after overwrite = TRUE has dropped the old table, like any other write error. #636 has the same change through the merge in 76d0d12. Its own numeric check also runs only in that single pass.


Generated by Claude Code

An out-of-range time value now fails after an `overwrite = TRUE` has
dropped the old table, as other write errors already do.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

Three review rounds before the requested changes, with two Mediums fixed. The change to an error for values out of range had a clean review of its own, which found nothing above Low. So did the change to fail on Inf (8ff6134). The temporary file of the load_data_local_infile path is removed on error. Following the review comment, 1c704b8 drops the early data check, so dbWriteTable() computes the data only once.

  • R/dbWriteTable_MariaDBConnection_character_data.frame.R: a time value that can't be represented fails after dbWriteTable(overwrite = TRUE) has dropped the old table, like other write errors, because DDL commits implicitly. Accepted in the review comment. Relevance: medium. Reviewer confidence: high.
  • R/coerce.R: via dbAppendTable() with prepared statements, and for unnamed parameters, the error labels columns by position (#2). Relevance: low. Reviewer confidence: high.
  • A non-temporary dbWriteTable() without overwrite still leaves an empty table behind if another error, such as one from the server, comes after CREATE TABLE, because DDL commits implicitly. Relevance: low. Reviewer confidence: high.
  • Values between the server's date range and the 9.2e12 s limit still reach the server, which stores them as NULL, as before. Relevance: low. Reviewer confidence: high.
  • The local test server is down, so the database tests for Inf have not run here; CI runs them. Relevance: medium until CI is green. Reviewer confidence: high.

Generated by Claude Code

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Adjust PR titles in the stack.

@krlmlr krlmlr changed the title fix: Bind non-finite and out-of-range POSIXct and hms values as NULL fix: Fail on infinite and out-of-range time values instead of storing garbage Nov 29, 2026

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Done. This PR is now "fix: Fail on infinite and out-of-range time values instead of storing garbage". The old title still said the values were bound as NULL. #636 keeps "feat!: Fail on infinite numbers instead of writing NULL", which already matches what it does.


Generated by Claude Code

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.

POSIXlt columns are written as NULL with load_data_local_infile = TRUE Non-finite and out-of-range POSIXct and hms values are bound as garbage

2 participants