Skip to content

fix: Type placeholder result columns after each execution and re-prepare on a parameter type change - #656

Draft
krlmlr wants to merge 11 commits into
mainfrom
claude/mysql-placeholders-v2
Draft

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

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Fixes #638, rebuilt from the findings of the plain-C experiment #646, as asked on #641. It replaces #641, which is closed now.

Design, following variant c5 of #646, the only one that was right on all four server × client combinations:

  • After each execution, every result column takes its type from the post-execute metadata (mysql_stmt_result_metadata()). A DOUBLE is therefore never bound as a string, and the empty string buffer can't truncate it any more: 1.5 no longer comes back as "" on MySQL. fix: Read result types after dbBind(), so that placeholder columns keep their values #618's handling of columns that are NULL at prepare time stays, and a type change between parameter sets is still an error.
  • A NULL parameter can change the type of an expression over it: with a MariaDB server ? + 1 is BIGINT for a value but INT for NULL. So if the first set has a NULL parameter, a column with a type at prepare time keeps that type, and a later set may change it only to a type that the client library converts without loss (an integer to DOUBLE or BIGINT, a string to a blob). In a later set with a NULL parameter, a column of another type is fine as long as its values are NULL; a non-NULL value there is the usual type-mismatch error. Without this, SELECT ? + 1 with c(1L, NA), ABS(?) with c(-1L, NA) and UPPER(?) with c("ab", NA) failed on MariaDB.
  • dbBind() prepares the statement again when a parameter's type differs from its last non-NULL value. This avoids MySQL keeping a reused statement's type, where 'abc' after 1.5 came back as 0. The new prepare goes into a new statement handle, so a failure leaves the previous statement intact.
  • ?dbBind describes what still differs on MySQL servers.

A visible change on MariaDB: SELECT ? + 1 with an integer parameter now returns integer64 (BIGINT, what the server reports after execution, and what MySQL returns too) rather than double; with a leading NA it stays double. UPPER(?) with a leading NA still returns a blob on MariaDB, as on main.

Tests:

  • These run on both servers: 1.5, 0.1, dates, long strings (test-fetch-length.R), and the type change 'abc' → 2.5 → 'abc', also with unfetched rows. Also a typed TEMPORARY table for known column types, CAST() in the empty-names test plus an integer case for dbBind() rejects parameters whose names are all empty #599, doubles in the dbHasCompleted() is TRUE right after dbBind() of a query, and dbGetRowCount() is not reset on rebind #602 tests, and ? + 1, ABS(?) and UPPER(?) with an NA in the first or a later set, compared as numbers where the servers' types differ.
  • A leading NA in a placeholder column is now tested on both servers with server-specific expectations. With a MariaDB server, SELECT ? AS x with c(NA, 0.1, 2.5) returns c(NA, 0.1, 2.5) (double) and with c(NA, 1L, 2L) returns c(NA, 1L, 2L) (integer), also with the MySQL client library. With a MySQL server, both fail: "Column x has type double (resp. integer64) for this set of parameters, but string for the first one." ?dbBind now states exactly that. This behaviour is the open question that moved here from fix: Correct values and parameter types for placeholder columns with MySQL servers #641.
  • These still skip on MySQL, where it differs: integers come back as integer64 (BIGINT), blobs as character, and a NULL gives no type.

Status: draft. The full matrix (run 46314093558, dispatched by hand because same-repository pull requests run only the smoke test) is green on 42c2ff2, and every job that ran the tests has FAIL 0 in its log: "MySQL server" (MySQL 8.4) [ FAIL 0 | WARN 2 | SKIP 31 | PASS 9193 ], "MariaDB server, MySQL client" and "MariaDB server + client" [ FAIL 0 | WARN 2 | SKIP 15 | PASS 9231 ], and the macOS mysql_config and mariadb_config jobs PASS 9226. The warnings are the garbage-collected-connection warnings also seen on main. The "windows-latest (4.3)" and "(4.4)" jobs are green but never ran R CMD check: rcmdcheck stops at once with Error in `[.data.frame`(file.info(foo), , "uname"). This is a CI problem that also shows on earlier runs. Locally the suite passes against MariaDB 10.11 (FAIL 0 | PASS 9231) and MySQL 8.0 (FAIL 0 | SKIP 31 | PASS 9193), both with the MariaDB client. The new-handle prepare is not covered by a test, because a failing prepare can't be provoked without a metadata lock.

Left to do:

  • Decide the leading-NA behaviour on MySQL servers (above).
  • A final review.

https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89 (now driving this PR)


Generated by Claude Code

…ameter type change (#638)

The result columns of a query with parameters now take their types
from the metadata the server reports after each execution.
MySQL reports `?` as a string at prepare time,
and a DOUBLE bound as a string came back as "" or "0".

A MySQL server keeps the result types of a statement from its first execution,
so `dbBind()` prepares the statement again
when a parameter has another type than its last non-NULL value.

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

Tests of 1.5, 0.1, dates, long strings and a type change across `dbBind()` calls
run on both servers.
Integer, blob and leading-NA placeholders, and `IF(?, ?, ?)`,
stay MariaDB-only.
Known column types come from a typed temporary table,
and the empty-names test uses `CAST()`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
claude and others added 3 commits November 29, 2026 21:37
A NULL parameter can change the type of an expression over it, such as
`? + 1`, which made several parameter sets with an NA fail with a type
mismatch. A first set with a NULL parameter keeps the prepare-time type of
columns known at prepare time, and later sets may change it to a type
that converts without loss. In a later set with a NULL parameter, a
column of another type is fine as long as its values are NULL.

A new prepare on a parameter type change now goes into a new statement
handle, so that a failure leaves the previous statement intact.

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

Restores the `SELECT ? + 1` tests with an NA, adds ABS() and UPPER(),
pins what a leading NA in a placeholder column gives on MariaDB and
MySQL servers and states it exactly in ?dbBind, and adds back an integer
case for #599.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: A leading NA in a placeholder parameter on MySQL servers

This moves the question from #641, now closed (old question), to this PR and updates it to its behavior at 42c2ff2.

On a MySQL server, a ? column whose first parameter set is all NULL gets a string type at prepare time. The PR re-prepares only when a parameter's type differs from its last non-NULL value, and a leading NA has none. The test that runs on every server records the current behavior:

  • MySQL server (job): dbGetQuery(con, "SELECT ? AS x", params = list(c(NA, 0.1, 2.5))) fails with "Column x has type double for this set of parameters, but string for the first one." c(NA, 1L, 2L) fails the same way, with integer64 in place of double.
  • MariaDB server, with either client (job): the same calls return double c(NA, 0.1, 2.5) and integer c(NA, 1L, 2L).

The options and their cost:

  • Keep the error on MySQL servers, as ?dbBind now documents. Strict and already tested, but a data frame with a leading NA can't be used as parameters on MySQL. A follow-up can add a re-prepare later. (recommended for this release)
  • Also re-prepare when a parameter's first non-NA value follows only NULLs, so MySQL gets the right type. Correct types, at the cost of one extra prepare after a leading NA, plus new code and tests before the release, which delays RMariaDB 1.4.0. Untested whether MySQL then reports the right type.
  • Return strings on MySQL, as fix: Correct values and parameter types for placeholder columns with MySQL servers #641 did: no error, but the column type depends on where the first non-NA value is.

Kirill decides. If nobody decides, the PR keeps the error and the release goes ahead with it.


Generated by Claude Code

…all others (#638)

Columns that are not just a placeholder keep their type from prepare
time again, as on main, so that `SELECT ? + 1`, `ABS(?)`, `UPPER(?)` or
`DATE_ADD(?, ...)` return the same types as before whatever the position
of an NA, and an NA of one parameter no longer changes the type of
another column. A placeholder column is one of type NULL at prepare
time, or with a MySQL server one of a non-binary string type.

A later parameter set that gives a placeholder column another type is
an error only when it returns a value that is not NULL in that column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89
Covers `? + 1`, `ABS()`, `CONCAT()`, `COALESCE()`, `UPPER()` and
`DATE_ADD()` with a leading and a trailing NA, and an NA of one parameter
next to another column, with main's types on MariaDB servers.

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

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.

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

2 participants