Skip to content

fix: dbCreateTable() gives character columns of zero-row data frames a type that holds any string - #588

Merged
krlmlr merged 2 commits into
claude/arrowfrom
claude/arrow-char-type
Nov 29, 2026
Merged

krlmlr merged 2 commits into
claude/arrowfrom
claude/arrow-char-type

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member

Closes #585. Stacked on #584.

Before this change, get_char_type() sized character and factor columns as VARCHAR(max(nchar(x), 1)). A column with no non-NA value therefore got VARCHAR(1), and appending any longer string failed with "Data too long for column". DBI's default dbCreateTableArrow() and dbWriteTableArrow() create tables from zero-row prototypes, so every Arrow write with strings hit this.

Change

  • get_char_type() returns TEXT when all(is.na(x)), which covers zero-length and all-NA input. Values wider than 255 characters already get TEXT. Input that has values gets the same type as before.
  • Factors are sized from their levels, as before. A zero-row factor with levels keeps VARCHAR(n); one without levels gets TEXT.
  • Behavior change: a non-empty all-NA character column used to become VARCHAR(1) and now becomes TEXT.
  • tests/testthat/test-dbCreateTable.R (new) covers zero-row and all-NA character and factor columns, each followed by appending and reading back a 600-character string.
  • The 18 DBItest Arrow specs skipped for dbCreateTable() with a zero-row data frame makes character columns VARCHAR(1) #585 are enabled again.

Tests

Run locally against MariaDB 10.11 with NOT_CRAN=true. DBItest had 8870 expectations, with 0 failures. The whole suite has one error, in test-timezone.R ("US/Alaska" unknown to CCTZ in this container), which also happens on main.

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


Generated by Claude Code

…s a type that holds any string

Without a non-NA value there is no width to size a character or factor column from,
so zero-row and all-NA columns now become `TEXT` instead of `VARCHAR(1)`,
the type that values wider than 255 characters already get.
This unskips the Arrow DBItest specs that create tables from zero-row prototypes.

Closes #585.

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

Question: TEXT for non-empty all-NA character columns

R/table.R now also gives TEXT to a non-empty all-NA column. Before, it got VARCHAR(1). TEXT can't be indexed without a prefix length: on MySQL 8.x, a later CREATE INDEX or PRIMARY KEY on such a column fails with error 1170. MariaDB 10.11 accepted it in a local check. VARCHAR(1) held almost nothing, so for most users this is a fix.

  • Keep TEXT for zero-length and all-NA input, and say so in the squash commit message, which fledge turns into the NEWS entry; field.types remains the way to get an indexable VARCHAR(n) (recommended)
  • Use VARCHAR(255) instead; indexable everywhere, but strings longer than 255 characters fail on append again
  • Use TEXT only for zero-length input, and keep VARCHAR(1) for all-NA input; the old behavior stays, but so does the "Data too long" failure for all-NA columns

If nobody decides, it stays TEXT.


Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Accepted limitations


Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 11:46
@krlmlr
krlmlr merged commit 5f299a6 into main Nov 29, 2026
9 checks passed
@krlmlr
krlmlr deleted the claude/arrow-char-type branch November 29, 2026 14:07
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.

dbCreateTable() with a zero-row data frame makes character columns VARCHAR(1)

2 participants