Skip to content

fix: Read result types after dbBind(), so that placeholder columns keep their values - #618

Merged
krlmlr merged 6 commits into
claude/bind-has-completedfrom
claude/bind-result-types
Nov 29, 2026
Merged

krlmlr merged 6 commits into
claude/bind-has-completedfrom
claude/bind-result-types

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #604. Stacked on #608, which makes bind() execute the first set of parameters.

Before this change, result types were fixed when the statement was prepared. A column that is just a placeholder, as in SELECT ? AS s, has type NULL at prepare time and was read as an integer, so after dbBind() a string came back as 0, 1.5 as 1, and a date as a number.

Change

  • send_query() records which result columns have type NULL at prepare time, which is the case for a column that is just a placeholder. After each set of parameters runs, update_metadata() reads the metadata again and updates the types of those columns only, and sets up the output bindings again if they changed. All other columns keep their prepare-time types, so for example SELECT ? + 1 still returns a double for c(NA, 1L) and for 1:2, and a real BINARY(0) column stays a blob.
  • The new types are built in a local vector and assigned only on success, so an error leaves the result consistent.
  • dbColumnInfo() before dbBind() still reports integer for a placeholder column, because the type isn't known yet. After dbBind(), it reports the parameter's type, also after a rebind with another type.
  • With several sets of parameters, dbFetch() appends every set to one data frame, so the first set's types win. If a later set gives a placeholder column another type, as SELECT IF(?, ?, ?) can, dbFetch() raises an error that names the column and suggests CAST(). The result is then marked complete and the remaining sets are skipped, so that a later dbFetch() returns no rows instead of misreading values, and a new dbBind() works.
  • If a placeholder column is still NULL, or a zero-length binary string, after a first set with a NULL parameter, it is read as character. MariaDB 10.11 reports the bound type in that case with both client libraries, so this is a fallback for other servers. See the question below.

Tests

tests/testthat/test-dbBind-types.R (new) covers string, double, date and NULL parameters in SELECT ? AS x, a rebind with another type, and a multi-row bind. It also covers SELECT ? + 1, BINARY(0) columns, a literal NULL column next to a placeholder, and the type-mismatch error. Its 25 expectations pass with libmysqlclient 8.0.46 and MariaDB Connector/C 3.3.17. Local runs against MariaDB 10.11 with NOT_CRAN=true, with each client library: 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
krlmlr added this pull request to stack #619 November 29, 2026 12:13

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Question: Placeholder expressions that are all NULL in the first set of parameters

After three review rounds, the rule is narrower than when this question was first posted. Only columns whose type is unknown at prepare time, such as ? or IF(?, ?, ?), get their type from the server after execution. A plain SELECT ? AS x works for every type, also with NA first, because the server reports the bound type.

What remains is an expression that is all NULL in the first set, such as IF(?, ?, ?) with NA branches. MariaDB 10.11 reports it as a zero-length binary string, which looks the same as an NA string parameter. This PR then reads the column as character if the first set has an NA string or blob parameter, and as integer otherwise. A later set with another consistent type, such as c(NA, 1.5), fails with a clear error that suggests CAST(). Before this PR, such values were silently misread: 1.5 became 1, and a date became 2024.

  • Keep the rule and the error, which can be avoided with CAST() (recommended)
  • When all NULL parameters of the first set share one type, use that type for these columns; this covers more cases, but it is a heuristic that guesses which parameter feeds which column
  • Read such columns as character always; then the later sets never error, but numbers come back as strings

If nobody decides, the rule stays as it is.


Generated by Claude Code

Only columns that the server reports as NULL at prepare time,
such as `SELECT ? AS x`, take their type from the metadata after execution.
All other columns keep their prepare-time type,
so that `SELECT ? + 1` returns doubles as before,
and a real zero-length binary column stays a blob.
A column that is still NULL after execution is read as character
only if the first parameter set has a NULL parameter,
so that `SELECT NULL AS n, ? AS y` keeps `n` integer.

A type change in a later parameter set is still an error.
The error leaves the column types unchanged and skips the remaining parameter sets,
so that the result stays usable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
MariaRow::value_raw() took `&buffers_[j][0]` of the column buffer,
which is empty for a zero-length BLOB value,
so the access was out of bounds and aborted with _GLIBCXX_ASSERTIONS
(as in the covr job) for `SELECT CAST('' AS BINARY(0)) AS b, ? AS y`.
Use `buffers_[j].data()`, which is valid for an empty vector.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
A result column that the server reports as NULL after the first
execution was read as character whenever any parameter of the first set
was NULL. With an NA integer parameter, this made `SELECT NULL AS n, ?`
return a character `n`, and `SELECT IF(?, ?, ?)` fail with a type
mismatch once a later set reported an integer.
Fall back to character only for a NULL character or blob parameter,
and read the column as integer otherwise, as for a plain `SELECT NULL`.

Also free the result metadata in update_metadata() before computing the
types, so that a warning turned into an error does not leak it, and
restrict the comment on the mismatch error to placeholder columns.

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. The first found a High: SELECT ? + 1 with c(NA, 1L) errored, and integer expressions became integer64. It also found two Mediums: BINARY(0) columns became character, and the result was broken after the new error. All are fixed in ceb1338. The second round found a Medium: integer-only parameters could raise the new error. That is fixed in cb06f32, together with a crash on empty blobs under libstdc++ assertions that CI found (9a27961). The third round found nothing above Low. It ran valgrind and a build with -D_GLIBCXX_ASSERTIONS, with both client libraries.

  • src/MariaResultPrep.cpp: an expression that is all NULL in the first set of parameters and has another type in a later set fails with a clear error; see the question. Before this PR, such values were silently misread. Relevance: low. Reviewer confidence: high.
  • When a full dbFetch() hits that error, the rows already fetched from the first set are discarded with it, while dbGetRowCount() counts them. Relevance: low. Reviewer confidence: high.
  • tests/testthat/_problems/ (testthat's output for failed tests) is in neither .gitignore nor .Rbuildignore. One such file was committed by accident and removed in cb06f32. Relevance: low. Reviewer confidence: high.
  • Columns with a type known at prepare time still convert values of another type silently, for example IF(?, i, ?) with a string. This already happens on main.
  • MySQL servers were not tested: there is no server in this container, and CI's MySQL entries currently run MariaDB (CI: MySQL server and MySQL client matrix entries silently test MariaDB #592). A MySQL 8 server may report NULL parameters differently.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 12:49
@krlmlr
krlmlr merged commit 1e19c23 into main Nov 29, 2026
9 checks passed
@krlmlr
krlmlr deleted the claude/bind-result-types branch November 29, 2026 13:54
krlmlr pushed a commit that referenced this pull request Nov 29, 2026
update_metadata() from #618 is now the single path for the metadata after an execution.
It replaces setup_output():
its first call caches names, types and the column info and sets up the output binding,
also when the metadata appears only after mysql_stmt_execute().
For the first parameter set it also updates the column info of the placeholder columns,
so that the chunk takes its types from the latest execution,
and later sets keep the types of the first set or raise #618's error.

MariaRow.cpp keeps the chunk version of the row code;
the .data() fix of #618 for empty buffers is already part of it.
bind() keeps #608 and #620 as on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7atAZVq1J42WzXJpS
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.

Columns whose type depends on a bound parameter are read with the type from prepare time

2 participants