Fix: bind parameters by position to fix BETWEEN and other duplicate-name binds - #20
Merged
Merged
Conversation
kou
reviewed
May 8, 2026
kou
reviewed
May 9, 2026
kou
approved these changes
Jun 10, 2026
suaron
force-pushed
the
fix/between-bind-overwrite
branch
from
June 10, 2026 12:32
e420f30 to
a9d76b8
Compare
Member
|
Could you update the PR description to reflect the latest change? We'll use the PR description as a commit message. |
Reusing one column across multiple `?` placeholders sent only one value to
the driver, so the prepared statement failed. Affects `where` with a range,
IN array, or raw SQL naming a column twice.
Cause: RecordBatch was keyed by `bind.name`, so duplicate names overwrote
each other. Now keyed by position (`"#{i}_#{name}"`); name is debug-only.
Also guards `.name` with `respond_to?` — raw SQL binds are plain values,
not QueryAttributes.
Tests: between, IN, raw fragment, range+raw, limit, offset.
suaron
force-pushed
the
fix/between-bind-overwrite
branch
from
June 16, 2026 10:26
a9d76b8 to
1c8506a
Compare
Contributor
Author
PR description updated to reflect the latest change and tidied up for use as the commit message. |
Member
|
Thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a query reuses one column across more than one
?placeholder, the adapter sent only one value to the driver, so the driver rejected the prepared statement (it expected N values and got 1).This affects any
wherewith a range (Arel::Between), anINarray, or a raw SQL fragment that names the same column twice.The fix stops duplicate column names from overwriting each other.
Reproduction
Root cause
ActiveRecordADBCAdapter::DatabaseStatements#perform_querykeyed the parameterRecordBatchbybind.name:where(sold_on: from..to)emits twoQueryAttributebinds, both named"sold_on", for:On the second iteration the hash assignment overwrites the first. The
RecordBatchends up with one column while the prepared statement still needs two values.The fix
Key columns by position and append the bind name only for readability:
The position prefix keeps keys unique (
0_sold_on,1_sold_on, ...) even when two binds share a name. The name suffix is only there to make the columns readable when debugging. Ruby preserves hash insertion order, so column order still matches the order of the?placeholders.Switching to
binds.zip(type_casted_binds)also fixes a separate crash: raw SQL fragments likewhere("col >= ?", 5)arrive inbindsas plain values, notQueryAttributeobjects, so.nameraisedNoMethodError. Therespond_to?(:name)guard falls back to"p", and the value comes fromtype_casted_binds, so the loop no longer cares about the bind's shape.Test plan
New cases in
test/test_model.rb:test_where_range_between—where(id: 1..2).counttest_where_range_between_exclusive_end—where(id: 1...2).counttest_where_raw_sql_fragment—where("id >= ?", 2)test_where_in_array—where(id: [1, 2, 3])test_where_mixed_range_and_raw—where(id: 1..2).where("id < ?", 2)test_limit—order(:id).limit(2)returns 2 rowstest_offset—order(:id).limit(2).offset(2)returns the trailing rowCI runs the suite three times via
ACTIVERECORD_ADBC_ADAPTER_BACKEND(sqlitedefault,postgresql,duckdb). All cases pass on each backend.