Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,26 @@ reviewed against. `read_rows` instead returns:
nonzero)
- `timestamptz` with the `+00:00` UTC offset (the wire value is UTC)
- `bytea` base64-encoded in full (not the 10 KB preview)
- `numeric`, `int8`, `float8`, `bool`, `text`, `uuid`, `json`/`jsonb` exactly
- `numeric`, `int8`, `float8`, `bool`, `text`, `uuid` exactly
- `json`/`jsonb` as serialized JSON text (so a scalar JSON string
`"\"hello\""` round-trips through `insert_records`, not the bare string
`"hello"` the display path returns)
- `float4`/`float8` `NaN` and `±Infinity` as text strings (`"NaN"`,
`"Infinity"`, `"-Infinity"` — not `null`, which the display path returns)
- `date`/`timestamp`/`timestamptz` `infinity`/`-infinity` as text strings (not
`null`)
- `date`/`timestamp`/`timestamptz` BC values in PostgreSQL's `"0043-03-15 BC"`
/ `"0043-03-15 12:30:45 BC"` format (not the `"-0042-03-15"` astronomical
form), so they round-trip through `insert_records`
- `time '24:00:00'` as the text string `"24:00:00"` (not `null`)
- `money` as a decimal text string (`"123.45"`, not the integer cents the
display path returns)
- `interval` without normalization (matching PostgreSQL's `interval_out`):
`48 hours` stays `"48:00:00"` (not `"2 days"`), `13 months` stays
`"13 mons"` (not `"1 year 1 mons"`)
- arrays decoded element-by-element through the same lossless decoders, so
`float8[]` with `NaN`, `timestamptz[]` with `infinity`, `numeric[]` with
wide values, `interval[]` etc. all survive the copy
- the PostgreSQL type name and OID per column, so the host can plan the
conversion

Expand Down Expand Up @@ -384,9 +396,10 @@ the same keys (a row missing a column is an error, not a silent NULL). The
`bind_pg_value`'s `decode_blob_wire_format`, so a PG→PG copy preserves every
value — `bytea`, full-precision timestamps, `numeric`, etc.

Arrays, ranges, and composites of temporals stay lossy in this release (they
delegate to the display path on the read side); the copy acceptance criteria are
scalar columns.
Arrays are decoded losslessly element-by-element (each element uses the same
full-precision decoder as a scalar column). Ranges and composites of
temporals still delegate to the display path on the read side (lossy); the
copy acceptance criteria are scalar and array columns.

## Building from Source

Expand Down
44 changes: 40 additions & 4 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ pub struct BindOptions<'a> {
/// OID and varies per installation. `None` when the column isn't
/// hstore, or resolution failed (surfaced as an error at bind time).
pub hstore_oid: Option<u32>,
/// `character_maximum_length` of the target column, used to construct the
/// correct `bit(n)` / `varbit(n)` CAST when the column type is `bit` or
/// `varbit` and the value needs an explicit cast (#222). `None` when the
/// column isn't a fixed-length bit type or the length wasn't discovered.
pub char_max_len: Option<i64>,
}

const USE_DEFAULT_SENTINEL: &str = "__USE_DEFAULT__";
Expand Down Expand Up @@ -197,10 +202,17 @@ pub fn bind_pg_value(
}),
Value::Array(arr) => {
let literal = json_array_to_pg_literal(&arr)?;
Ok(BoundValue {
sql: literal,
param: None,
})
let sql = if let Some(bt) = base_type.as_deref() {
if let Some(elem) = bt.strip_prefix('_') {
let cast_type = format!("{}[]", elem.to_lowercase());
format!("CAST({literal} AS {cast_type})")
} else {
literal
}
} else {
literal
};
Ok(BoundValue { sql, param: None })
}
Value::Object(_) => Err("Cannot bind a JSON object to a non-JSON column".to_string()),
}
Expand Down Expand Up @@ -325,6 +337,30 @@ fn bind_pg_string(
}
}

// 3.4. bit(n) / varbit(n) — the explicit CAST must include the length:
// `CAST($N AS bit)` defaults to `bit(1)`, truncating a wider value
// silently (#222). When `char_max_len` is known, emit
// `CAST($N AS bit(n))` / `CAST($N AS varbit(n))` instead. Falls through
// to the generic CAST (step 3.5) when the length is unknown.
if let Some(bt) = base_type {
let is_bit = bt == "BIT" || bt == "VARBIT" || bt == "BIT VARYING";
if is_bit {
if let Some(len) = options.char_max_len {
if len > 0 {
let cast_type = if bt == "BIT" {
format!("bit({len})")
} else {
format!("varbit({len})")
};
return Ok(BoundValue {
sql: format!("CAST(${placeholder_idx} AS {cast_type})"),
param: Some((Box::new(s.to_string()), Type::TEXT)),
});
}
}
}
}

// 3.5. Column types that need an explicit CAST (no text→type assignment
// cast). Must run before the BLOB decoder (step 3) and the TEXT fallback
// (step 12) — both would bind as TEXT, which PostgreSQL rejects for
Expand Down
Loading
Loading