Skip to content

fix: Correct values and parameter types for placeholder columns with MySQL servers - #641

Closed
krlmlr wants to merge 18 commits into
mainfrom
claude/mysql-placeholders
Closed

krlmlr wants to merge 18 commits into
mainfrom
claude/mysql-placeholders

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Fixes #638.

On a MySQL 8.4 server, SELECT ? AS x with 1.5 returned "". Integer and Date parameters came back as character, and 23 tests failed in CI's "MySQL server" job.

  • Wrong value (src/MariaRow.cpp): string and blob columns were bound with an empty buffer, then fetched again at the length the client reported. When libmysqlclient converts a DOUBLE to a string, it writes no more characters than the buffer holds, so it reported length 0 and the value came back empty. String and blob columns are now bound to a 256-byte buffer. Only longer values are fetched again in full with mysql_stmt_fetch_column(), into a separate buffer, so the bound one never moves. This applies to every column and also saves the second fetch MariaDB used to do for every non-empty string. The new test-fetch-length.R covers lengths 0 to 5000 for strings and blobs.
  • Placeholder types (src/MariaResultPrep.cpp): MySQL reports a ? column as VARCHAR when the statement is prepared, but gives the real type after execution. On a MySQL server (detected with mysql_get_server_info()), non-binary string columns now take their type from the metadata after execution, so 1.5 comes back as double and a date as Date. A real VARCHAR column stays a string. MariaDB servers are unchanged (fix: Read result types after dbBind(), so that placeholder columns keep their values #618's handling).
  • Leading NA on MySQL: a column whose first parameter set is all NULL is a string there, and MySQL keeps that type. Such a column stays character, so c(NA, 0.1, 2.5) returns c(NA, "0.1", "2.5"), see the question. Every other type change, including one to a binary type, still fails with the type-mismatch error.
  • Tests:
    • A new is_mariadb_server() helper.
    • test-dbBind-types.R expects MySQL's behavior where it really differs: integer64 for integer parameters, and the server's silent conversion in IF(?, ?, ?), commented as a server limitation.
    • test-dbBind.R and test-dbBind-completed.R use CAST(? AS CHAR), because what they test isn't placeholder typing.
    • test-fetch-error.R uses a regular table, because MySQL can't reference a TEMPORARY table twice in one query.

Blob parameters on MySQL come back as strings, without a UTF-8 check, as on main; that is #642.

Tests: the local database server is down, so this was tested in CI only, with dispatched runs of the full matrix. Job status is not reliable here (a job can pass with a failing test), so each server job's log was checked for [ FAIL 0. At 337db3d, all 26 jobs passed and the MySQL, "MariaDB server, MySQL client" and "MariaDB server + client" jobs had FAIL 0: run 46281404509. cbcc9aa only removes a test, and its run is pending.

Accepted limitations

https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ


Generated by Claude Code

The output buffer of a string or blob column was bound empty,
and each value was fetched again with the reported length.
MySQL's libmysqlclient converts a floating-point value to a string
no longer than the buffer, so a DOUBLE value of a column typed as a string,
such as a placeholder with a MySQL server, was read as an empty string.
The buffer now holds 256 bytes,
and only a longer value is fetched again in full.

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

A MySQL server reports a placeholder result column as a non-binary string
at prepare time, and its actual type after the execution.
Such columns now follow the type reported after the execution,
as placeholder columns of type NULL do with MariaDB (#604).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
Tests whose purpose is not placeholder typing cast their placeholders,
`SELECT ? + 1` expects integer64 with MySQL,
and the fetch error test uses a regular table,
because MySQL cannot refer to a temporary table twice in one query.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
With a MySQL server, an integer parameter gives a BIGINT column,
and a NULL parameter keeps the type of the previous execution,
which is a string for the first parameter set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
A query on the same connection cancels a pending result.
The comment on placeholder columns documents MySQL's typing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
…ULL (#638)

With MySQL, a NULL parameter in the first set gives a string column.
A later set of another type no longer errors for such a column,
its values are fetched as strings.
A column with a non-NULL value in the first set still errors on a type change.
The tests now call MySQL's silent conversion of 'a' a known server limitation
and use ASCII bytes for the blob parameter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
…lumn that was NULL first (#638)

The override that keeps such a column a string no longer applies
when a later set reports a binary type.
Its bytes would otherwise be read as a string.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
…olders (#638)

With MariaDB, the blob test uses the bytes 00 and ff again.
With MySQL, a blob parameter gives a string column,
the test uses valid UTF-8 control bytes and checks a set without NULL too.
A new MySQL-only test checks that a NUL byte after a NULL first set
fails the fetch instead of returning a corrupt string.
The leading-NA test adds 0.1, which is not exact in binary.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
The removed test expected R's "embedded nul" error from inside the fetch,
which locks in the behavior that #642 tracks.

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: A leading NA in a placeholder parameter on MySQL

On a MySQL server, a ? column whose first parameter set is all NULL gets a string type, and MySQL then keeps that type for the next execution. dbGetQuery(con, "SELECT ? AS x", params = list(c(NA, 0.1, 2.5))) now returns c(NA, "0.1", "2.5") (src/MariaResultPrep.cpp). Without this rule it would fail with "has type double for this set of parameters, but string". c(0.1, NA, 2.5) returns doubles either way, and MariaDB servers return doubles in both cases. A switch to a binary type still fails, and so does any type change after a first set with a real non-NULL type.

  • Return strings, as now: no data loss, but the column type depends on where the first non-NA value is (recommended)
  • Fail with the type-mismatch error, as for other type changes: strict, but a common data frame with a leading NA can't be used as parameters on MySQL
  • Re-prepare the statement when a later set has a different type: correct types, at the cost of a prepare per type change and more code

Kirill decides; if nobody does, the PR keeps returning strings.

Superseded on 2026-11-29: this PR was closed in favor of #656, which re-prepares a statement when a parameter's type changes. It is asked again, with the measured MySQL behavior, at #656 (comment).


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

Three review rounds. The first found two Mediums: a leading NA failed on MySQL, and a test asserted MySQL's silent 'a' → 0 as correct. Both are fixed in a055252. The second found a Medium, the string fallback accepting a binary type, which is fixed in 7f8d12b. The third found two Highs and a Medium in behavior that main already has: MySQL returns blob parameters as strings, invalid UTF-8 and NUL bytes go unchecked, and R's "embedded nul" error jumps out of the fetch. They are filed as #642. cbcc9aa removes a test that had locked in that error. The memory handling of the new 256-byte buffer and the refetch checked out in every round, and MariaDB fetches no longer read every non-empty string twice.

  • src/MariaResultPrep.cpp: on MySQL, a leading NA makes a placeholder column character; see the question. The same fallback applies when the first set returns no rows, and on a reused result the outcome depends on the previous binding. Medium relevance, high confidence.
  • tests/testthat/test-dbBind-types.R: on MySQL, an integer parameter comes back as integer64, because the server types it BIGINT. Low relevance, high confidence.
  • tests/testthat/test-dbBind-types.R: MySQL keeps the type of the first execution, so in IF(?, ?, ?) a later 'a' silently becomes 0. This is a server limitation the client can't see; the test says so in a comment (recorded). Low relevance, high confidence.
  • src/MariaResultPrep.cpp: on MySQL, every non-binary string column of a parameterized query reads the metadata again after each execution, one allocation per parameter set. Low relevance, high confidence.
  • tests/testthat/helper-server.R: the C++ code detects the server from the handshake, the tests from SELECT VERSION(). Behind a proxy that reports another version, the server-aware test expectations can be wrong, while the C++ behavior stays correct. Low relevance, medium confidence.
  • Blob parameters come back as invalid UTF-8 strings from MySQL servers, and a NUL byte aborts the fetch #642: blob parameters on MySQL, unchecked UTF-8 and NUL bytes in strings (behavior on main, not introduced here).

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 16:24
…ion columns (#638)

A server counts as MariaDB when its version is 10.0 or later
or its version string mentions MariaDB, so that a proxy or an
overridden version string does not misroute it.
Lowercasing goes through unsigned char.
On MySQL, only string columns without an origin table,
expressions and placeholders, have their metadata re-read per
parameter set; table columns keep their type.

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

MariaDB servers again test a bare integer placeholder with integer
results, MySQL servers keep the CAST().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
dbGetQuery(con, "SELECT ? AS x", params = list(NA_character_))$x,
NA_character_
)
expect_identical(

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.

Just skip this test on MySQL.

Comment thread tests/testthat/test-dbBind-types.R Outdated
dbGetQuery(con, "SELECT ? AS x", params = list(c(1.5, NA, 2.5)))$x,
c(1.5, NA, 2.5)
)
if (is_mariadb_server(con)) {

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.

Same here: document MySQL as odd-behaving and just skip the test, assuming we can't do anything about it.

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.

Doing this for every test in test-dbBind-types.R that differs between the servers: they become MariaDB-only, and a new dbBind() section documents how MySQL types placeholder columns. One small test still runs on both servers, so that the empty-string bug for doubles and dates (#638) stays covered on MySQL.

We could do something about the leading NA: re-preparing the statement when a later parameter set has another type would give correct types, at the cost of a prepare per type change and more code. That is the third option in the question. Until it is decided, this PR returns strings there and documents it.


Generated by Claude Code

Comment thread tests/testthat/test-dbBind-types.R Outdated
out <- dbGetQuery(con, "SELECT ? AS x", params = params)
expect_identical(out$x, blob::blob(NULL, as.raw(c(0, 255))))
} else {
# MySQL types a blob parameter as a string in the connection's charset,

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.

Skip.

Comment thread tests/testthat/test-dbBind-types.R Outdated
con <- mariadbDefault()
on.exit(dbDisconnect(con))

# At prepare time, MariaDB reports DOUBLE for `? + 1`, MySQL reports BIGINT

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.

This test may be ill-formed. Can we cast instead of just assuming, or create a temporary table?

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.

Or perhaps use dbConnect(bigint = "integer") for this test?

Comment thread tests/testthat/test-dbBind-types.R Outdated
)
expect_s3_class(out$b, "blob")
expect_identical(out$y, 1L)
if (is_mariadb_server(con)) {

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.

And the tests below?

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.

The tests below are about placeholder-only columns (? AS y next to NULL or BINARY(0), and IF(?, ?, ?)). Placeholder typing is exactly what MySQL does differently, so a cast would remove what they test. Since 518656a they are MariaDB-only, with the MySQL behavior documented in ?dbBind (5809634). The known-type test above is the one that only assumed server typing, and cf4ca65 rewrites it with a typed temporary table that runs on both servers. CI for the new head is running.


Generated by Claude Code

Comment thread tests/testthat/test-dbBind.R Outdated
sql <- "SELECT ? AS a, ? AS b"
expected <- data.frame(a = 1L, b = 2L)
} else {
sql <- "SELECT CAST(? AS CHAR) AS a, CAST(? AS CHAR) AS b"

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.

For this test, the second form might be portable.

Keep this branch's MySQL handling of the tests and drop main's
skip_if_mysql_server(), which no test uses here any more.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

I don't understand this PR. New PR: Reduce the problem to a reproducer in plain C against libmariadb/libmysql and MariaDB or MySQL. Attempt to find solutions in plain C first. Prepare a special GitHub workflow for testing this. Record as an experiment.

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Understood. Work on this PR is paused. A new PR from main (branch claude/experiment-638) will add a plain-C reproducer, recorded as an experiment under experiments/, with its own workflow. The workflow runs the reproducer against MariaDB and MySQL 8.4 servers, each with libmariadb and with libmysqlclient. The C program shows the reported length and value for the current approach, which binds string columns with an empty buffer. It also compares the prepare-time and post-execute metadata types. Then it tries candidate fixes in plain C, such as a non-empty buffer, binding with the post-execute type, and STMT_ATTR_UPDATE_MAX_LENGTH. Each one gets a verdict per server and client combination. I'll link it here once it's up.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

The red "macos-latest (release) mysql_config" job on 702f863 (job) is not this PR's code. The MariaDB server on the macOS runner never accepted connections, so every test that connects failed with "Can't connect to MySQL server on '127.0.0.1:3306' (61)" ([ FAIL 8 | SKIP 36 | PASS 178 ]). The setup steps also logged Homebrew errors ("No such keg: /opt/homebrew/Cellar/mariadb-connector-c"). This PR touches no workflow or setup code, and the same job passed on 337db3d. I'll re-run the job once when the run finishes. Work on this PR otherwise stays paused for the plain-C experiment.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

The plain-C experiment is up as #646. In short: the empty string buffer truncates converted doubles on both clients and both servers (1.5 → ""), and MySQL triggers it by typing ? as VAR_STRING at prepare time. MySQL also keeps a reused statement's result type, so with this PR's 256-byte buffer a later 'abc' after 1.5 still comes back as 0. Only rebinding with the post-execute type plus re-preparing on a parameter type change got every case right. The fix can be rebuilt from those findings once you've looked at them.


Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Correction to my earlier comment: the red macOS job is not this PR's, but it isn't a Homebrew setup problem either. The server stops accepting connections during the #621 interrupt test in test-dbDisconnect.R. That test came in through the merge of main, and main fails the same way intermittently (889bb6c, f40616c). This is now #651. Nothing in this PR's diff runs on that path, since the #621 query has no parameters.


Generated by Claude Code

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Rebuild fix from findings.

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Rebuilding from the findings of #646 on a new branch from main, claude/mysql-placeholders-v2. Resetting this branch was refused as a destructive action, so I kept it as it is. The new fix follows the only variant that was right in every case. After each execution, the result is bound with the post-execute metadata. When a non-NULL parameter changes its type between dbBind() calls, the statement is prepared again. Tests run on both servers where the fix makes MySQL correct, and skip on MySQL where it still differs. I'll open it as a PR that replaces this one and link it here. Closing this PR then is your call.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

The rebuilt fix is up as draft #656, from main, following variant c5 of #646. It is not yet reviewed and not yet verified on MySQL. This PR stays as it is. Closing it in favor of #656 is your call. The leading-NA question carries over to #656. This session is being archived, so nobody is watching this PR any more.


Generated by Claude Code

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Abandoning in favor of #656.

@krlmlr krlmlr closed this Nov 29, 2026
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.

On MySQL 8.4, SELECT ? returns the parameter as character or empty string, and dbBind tests fail

2 participants