Skip to content

fix: Reject list columns that are not lists of raw in dbWriteTable() - #657

Open
krlmlr wants to merge 2 commits into
mainfrom
claude/fix-list-columns
Open

krlmlr wants to merge 2 commits into
mainfrom
claude/fix-list-columns

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #648.

The COPY path in sql_data_copy() encoded every list element with format(), so a list column holding integer or character vectors was written as meaningless bytea with no error or warning. It now raises "Lists must contain raw vectors or NULL", the same error that dbQuoteLiteral() (used with copy = FALSE) and parameter binding already give. Because dbWriteTable() runs in a transaction, the table it created is rolled back, also inside a transaction the user opened, which stays usable. Lists of raw vectors, AsIs lists of raw and blob::blob columns are written as before.

dbDataType() still maps any list to BYTEA. DBI's dbDataType() specification only requires lists of raw vectors and blob objects to be accepted and leaves other types unspecified, and RSQLite 3.54.1 behaves the same way: dbDataType() returns BLOB for such a list, and dbWriteTable() fails with "Can only bind lists of raw vectors (or NULL)" without leaving a table behind. Mapping non-raw lists to PostgreSQL arrays instead is asked as a question in a comment below.

Tests: self-contained blocks in tests/testthat/test-dbWriteTable.R cover the error for dbWriteTable() with copy = TRUE and copy = FALSE, for dbAppendTable() and inside a user-opened dbBegin() transaction, as well as the round trip of lists of raw, AsIs lists of raw and blobs as bytea, and a zero-row data frame with a list column. devtools::test() passes locally against PostgreSQL 16.

Accepted limitations: see this comment.

The Arrow path (list<int32> via dbAppendTableArrow()) is coordinated in #631 and not touched here.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g

The COPY path encoded every list element with `format()`,
so a list of integer or character vectors was written as meaningless `bytea` without an error.
It now raises "Lists must contain raw vectors or NULL",
the error that `dbQuoteLiteral()` and parameter binding already give,
and `dbWriteTable()` rolls back the table it created.
Lists of raw and blob objects are written as before.

Closes #648.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: error or PostgreSQL arrays for list columns that are not lists of raw?

Right now dbWriteTable() and dbAppendTable() with a list column whose elements are not raw vectors, for example list(1:2, NULL, c("a", "b")), write meaningless bytea with no error (#648). This PR makes the COPY path raise "Lists must contain raw vectors or NULL", the error that dbQuoteLiteral() and parameter binding already give, and dbWriteTable() rolls back the table it created. dbDataType() still returns BYTEA for any list. The DBI dbDataType() specification only requires lists of raw and blob objects to be supported and leaves other types unspecified. RSQLite behaves the same way: BLOB from dbDataType(), and an error from dbWriteTable().

The alternative is to map homogeneous atomic list columns to PostgreSQL arrays (INTEGER[], TEXT[], and so on). That is a new feature: dbDataType() would have to inspect the elements, the COPY and SQL paths would need array literals with quoting, dbReadTable() would read the arrays back as character strings rather than lists, so there would be no round trip, and the Arrow work in #631 would have to make the same choice. An error can later be loosened into arrays without breaking anyone, but the reverse cannot. If nobody decides, the PR stays with the error.

  • Raise an error for lists that are not lists of raw, as this PR does (recommended)
  • Also raise the error earlier, from dbDataType(), so that dbCreateTable() refuses such columns too
  • Map homogeneous atomic list columns to PostgreSQL arrays instead (a follow-up feature PR)

Generated by Claude Code

…list columns error

The error for list columns that are not lists of raw is checked inside a transaction the user opened,
which stays usable and keeps its earlier work.
An AsIs list of raw still round-trips as `bytea`,
and a zero-row data frame with a list column still creates a `bytea` column.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Keep accepting character hex strings in list columns?

On main, the COPY path formatted every list element with format() and prefixed \x. A list element that is a character hex string, such as list("0102"), became \x0102 and was stored as valid bytea. Code that relies on this may exist. With this PR, such an element now raises "Lists must contain raw vectors or NULL". dbQuoteLiteral() (used with copy = FALSE) and parameter binding already reject it, so it only ever worked through COPY. If nobody decides, the PR keeps the error.

  • Keep the error, consistent with dbQuoteLiteral() and binding, which already reject it (recommended)
  • Accept character elements as hex-encoded bytea in all three paths (COPY, dbQuoteLiteral(), binding)
  • Accept them only in COPY, with a deprecation warning, and error in a later release

Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Name the offending column in the error for non-raw list columns?

The new error "Lists must contain raw vectors or NULL" does not say which column or row caused it. Packed data-frame columns are also caught, because is_blob is is.list(), which is true for a data frame, so a user with such a column gets a message about lists that does not obviously apply. The fix is small: loop over the list columns by name and include the column name and the first bad row in the message. The same message comes from dbQuoteLiteral() and binding, which would ideally change in step. If nobody decides, the message stays as it is.

  • Keep the message as it is, consistent with dbQuoteLiteral() and binding (recommended)
  • Name the column and the first offending row in the COPY path, in this PR
  • Name them in all three paths, in a follow-up PR

Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

  • R/tables.R#L57: the error for non-raw list columns names neither the column nor the row, and packed data-frame columns get the same message (question). Relevance low, confidence high.
  • R/tables.R#L76: character hex strings in list columns, such as list("0102"), were stored as valid bytea through COPY on main and now raise an error (question). Relevance medium, confidence high.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 12:15
@krlmlr krlmlr mentioned this pull request Nov 29, 2026
3 of 9 tasks

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.

dbWriteTable() writes list columns that are not lists of raw as garbage bytea

2 participants