Skip to content

feat: Capture DBI-level arguments to dbConnect() in the .dbi slot - #800

Open
krlmlr wants to merge 4 commits into
claude/dbconnect-args-slotfrom
claude/dbi-connect-args
Open

krlmlr wants to merge 4 commits into
claude/dbconnect-args-slotfrom
claude/dbi-connect-args

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Stacked on #794, as Kirill asked there: "Move dbi_connect_args() and all logic related to it to a stacked PR." This PR restores that logic on top of #794.

Changes

  • Depends: R (>= 4.1.0), for ...names().
  • The dbConnect() generic captures DBI-level arguments with ...names() and ...elt() after the method has returned: only names on DBI's internal list dbi_connect_arg_names(), empty for now, are evaluated and merged into the connection's .dbi value, next to the driver. Other arguments are never forced. The names drv and conn are never captured, so they cannot collide with the stored driver or connection.
  • In nested calls, such as through a DBIConnector, the arguments are merged and the outermost call wins on a name passed at both levels. The connection keeps the connector's inner driver, as in feat: Keep the driver in each connection and the connection in each result #794, and only listed names of the connector's stored arguments are copied.
  • A DBI-level argument that the method did not force is evaluated after the method returns; if that fails, it is dropped, so dbConnect() never fails because of DBI's bookkeeping. ?dbConnect documents both, and that a method forcing all its arguments, such as the connector's, lets such an error propagate.
  • Tests mock dbi_connect_arg_names(), since no DBI-level argument exists yet.

Status

https://claude.ai/code/session_01Hr1rd6RgjpQxckjWTEReje

🤖 Generated with Claude Code

The `dbConnect()` generic now also keeps the arguments in `...`
that DBI itself needs, under their own names,
in the `.dbi` slot of the returned connection.
The list of these names is empty for now;
future DBI-level settings, such as lazy Arrow fetching (#783), are added to it.
Only these arguments are evaluated, all others are left alone,
and all arguments are still passed on to the method.
Capturing never fails: an argument that fails to evaluate is skipped.

For a connection created from a `DBIConnector`,
the arguments stored in the connector are captured as well,
alongside the driver that the connector wraps,
and arguments passed to `dbConnect()` win over them.

`...names()` and `...elt()` require R 4.1.0,
so the package now depends on R (>= 4.1.0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6RgjpQxckjWTEReje
@krlmlr
krlmlr added this pull request to stack #801 November 29, 2026 16:04

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: How do DBI-level arguments get past backends, and how are they named?

Moved from #794. The generic passes every argument on to the backend's method, and only afterwards captures the arguments named in dbi_connect_arg_names(), which is empty for now. Once a DBI-level argument such as lazy Arrow fetching (#783) is added, a backend whose method rejects unknown arguments errors on it, and a backend with an argument of the same name would receive DBI's value.

  • Reserve a prefix (e.g. dbi_lazy_arrow or dbi.lazy_arrow) and have the generic strip arguments with it before dispatch (recommended): works with every backend unchanged, no clash with backend argument names, and the list of names stays DBI's own.
  • Keep passing everything through and use plain names: simplest, but fails on strict backends and can clash with a backend's own argument.
  • Use a prefix, but keep passing the arguments through: no clash, but strict backends still fail.

Kirill decides. If nobody decides, arguments are passed through and no name is reserved; this only matters once the first DBI-level argument is added.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: In nested dbConnect() calls, which call's DBI-level arguments are kept?

Moved from #794. A dbConnect() method can call dbConnect() again, as dbConnect(<DBIConnector>) does. The inner call stores its DBI-level arguments first; the outer call then merges its own over them, so on a name passed at both levels the outermost call wins, and names passed at only one level are all kept. A test covers the connector path; the connection keeps the connector's inner driver, as in #794.

  • Merge, outermost wins on conflicts, as now (recommended): what the user typed takes precedence, and settings from a connector or wrapper are not lost.
  • Innermost call wins on conflicts: what the inner call received takes precedence.
  • Outermost call replaces everything: simplest to explain, but drops settings the inner call added.

Kirill decides. If nobody decides, the merge with outermost wins stays.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Warn when a DBI-level argument fails to evaluate?

Moved from #794. In dbi_connect_args(), a DBI-level argument whose evaluation errors is dropped without a message, so a typo such as dbConnect(drv, dbi_x = typo_var) silently falls back to the default. This keeps dbConnect() from ever failing because of DBI's own bookkeeping. No DBI-level argument exists yet, so this matters only once the first one is added.

  • Emit a warning naming the argument and the error, and keep connecting (recommended): the typo becomes visible, and dbConnect() still succeeds.
  • Stay silent, and document it: no noise, but mistakes go unnoticed.
  • Let the error propagate: strictest, but dbConnect() then fails after the backend has already connected, leaving a connection nobody can close.

Kirill decides. If nobody decides, the argument is dropped silently.


Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Clean review, round 1 (abf8b2f)

Scope: the capture of DBI-level arguments, restored on top of #794 (diff against a9659a1). No High findings.

Medium

  1. A clone made with dbConnect(con) keeps the original's driver but not its DBI-level arguments, so a future setting would be dropped silently on cloning. → question
  2. A DBI-level argument the method does not force is evaluated after the connection is open, and dropped without a message if that fails (dbi_a = stop("boom"), dbi_a = undefined_var via do.call() both give no setting). Intended, but the docs say neither. → Being documented; the warning is the open question.

Low

  1. Both tryCatch() blocks catch only error, so a DBI-level argument signaling a non-error condition with stop() makes dbConnect() fail. Unlikely; catching every condition would also swallow warnings. → To be accepted.
  2. Duplicated names (dbi_a = 1, dbi_a = 2) keep the last value, while list(...)$dbi_a in a method sees the first. → To be accepted, as on feat: Keep the driver in each connection and the connection in each result #794 before the move.
  3. A future name drv or conn in dbi_connect_arg_names() would collide with the stored driver or connection. → Being fixed with a guard.
  4. With R (>= 4.1.0), the getRversion() checks for 3.5 and 4.0 in the vignettes, tests/testthat/test-DBItest.R and a CI action are dead code. Harmless. → To be accepted.

Checked and correct: other arguments are never forced; an argument the method forced is not evaluated again; missing, empty and NULL arguments; exact name matching; evaluation in the caller's frame through wrappers, ..1 and do.call(); recursion with the same ...; connectors: their stored arguments are captured by the inner call and call arguments win, and only listed names are copied, so connector credentials stay out; return value unchanged; docs match. devtools::test(): 0 failures, 60 skips, 9973 passed; each of the 18 test_that() blocks in test-dbConnect-dbi.R passes alone after load_all().

Not checked: R 4.1 (not installed; ...names() returned NA or NULL for unnamed arguments before R 4.1.3, which %in% handles by reading); backend clone methods such as RSQLite's; reverse dependencies affected by the Depends bump.


Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Question: Should a clone made with dbConnect(con) inherit the original's DBI-level arguments?

Found by the clean review (Medium 1). A clone keeps the original connection's driver, but its DBI-level arguments come only from the clone call itself: dbi_connect_args() reads the ... of that call, and the generic does not look at the original's .dbi value. Once a setting such as lazy Arrow fetching (#783) exists, dbConnect(con) would silently drop it. Nothing breaks today, since no DBI-level argument exists yet.

  • Inherit: a clone starts from the original's DBI-level arguments, and arguments passed to the clone call win (recommended): a clone behaves like the original, consistent with how a connector's stored arguments are merged.
  • Do not inherit, and document it: a clone only has what its own call passes, which is simpler but surprising.

Kirill decides. If nobody decides, clones do not inherit, and the docs say so.


Generated by Claude Code

…nd `conn`

Document that a DBI-level argument the method has not forced
is evaluated by DBI after the method returns,
and that a failing evaluation drops the argument silently.
Never keep a DBI-level argument named `drv` or `conn`,
so that it cannot overwrite the driver kept by DBI.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Clean review, round 2 (abf8b2f..69bc030)

Scope: the fixes of round 1 (69bc030) and the merge of #794's e8769ad (27dc214). No High or Medium findings, so the review stops here.

Low

  1. The merge kept the older DBIConnection wording "keeps its drv argument there as drv" instead of e8769ad's, which is wrong for a connector or a clone. The only part of e8769ad that was dropped. → Being fixed.
  2. ?dbConnect says an unforced DBI-level argument that fails is dropped, right after the connector sentence, but the connector method forces all its arguments, so there the error propagates. Technically correct ("not forced by the method"), easy to misread. → Being clarified.
  3. Excluding drv from DBI-level names only matters for direct calls of dbi_connect_args(), since drv always matches the generic's formal; only conn is a live collision. Harmless. → Accepted.

Checked and correct: git diff abf8b2f 27dc214 is exactly e8769ad's change apart from item 1, with both new tests; the exclusion works end to end, also for a connector's stored arguments, and the new test fails without it; the setdiff() sits inside the guard; a value the method forced is not evaluated again; each of the 21 test_that() blocks in test-dbConnect-dbi.R passes alone after load_all(); the full suite passes (0 failures, 60 skips).

Not checked: R CMD check, other R versions, warnings raised while evaluating a DBI-level argument (they propagate; undocumented).


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Also catch non-error conditions signaled while evaluating a DBI-level argument?

Found by the clean review (Low 3). Both guards in dbi_connect_args() catch only error. A DBI-level argument that calls stop() with a condition object not of class error gets past both, and dbConnect() fails after the backend has connected. Warnings propagate, as they should.

  • Accept: such conditions are very unusual, and catching every condition would also swallow warnings and messages (recommended).
  • Add a handler for conditions that are neither warnings nor messages: closes the gap, at the cost of a less obvious handler.

Kirill decides. If nobody decides, this goes into the Accepted limitations.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Remove the getRversion() checks that R (>= 4.1.0) makes dead?

Found by the clean review (Low 6). With the bump to R (>= 4.1.0), the checks for R < 3.5 in the vignettes (DBI.Rmd, DBI-advanced.Rmd, DBI-arrow.Rmd), for R < 4.0 in test-DBItest.R, and in a CI action can never be true. They are harmless.

  • Leave them: unrelated to this PR's purpose, and a later cleanup can remove them together with other version checks (recommended).
  • Remove them in this PR: the code matches the declared R version, at the cost of touching vignettes and CI in a PR about dbConnect().

Kirill decides. If nobody decides, they stay and this goes into the Accepted limitations.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

  • R/dbi-state.R#L51-L72: a DBI-level argument that the method does not force and that fails to evaluate is dropped silently, now documented in ?dbConnect (question). Relevance low until the first DBI-level argument exists; reviewer confidence high.
  • R/dbi-state.R#L51-L72: a DBI-level argument that signals a non-error condition with stop() makes dbConnect() fail (question). Relevance very low; reviewer confidence high.
  • R/dbi-state.R#L51-L72: duplicated DBI-level argument names keep the last value, while a method reading list(...) sees the first. Relevance very low (a caller error); reviewer confidence high.
  • DESCRIPTION: with R (>= 4.1.0), some getRversion() checks in vignettes, tests and CI are dead code (question). Relevance very low; reviewer confidence high.
  • R/dbi-state.R#L51-L72: excluding the name drv from DBI-level arguments only matters for direct calls, since drv always matches the generic's formal. Harmless; reviewer confidence high.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 16:24
@krlmlr krlmlr mentioned this pull request Nov 29, 2026
15 of 22 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.

2 participants