Conversation
Contributor
|
I'm seeing a similar issue, but with a regular As a workaround, I've copied/modified the legacy It's a little extra work, but it prevents a mandatory conversion to pub fn naive_dt_to_datetime1<'a>(dt: NaiveDateTime) -> ColumnData<'a> {
fn to_days(date: NaiveDate, start_year: i32) -> i64 {
(date - NaiveDate::from_ymd_opt(start_year, 1, 1).unwrap()).num_days()
}
fn to_sec_fragments(from: NaiveTime) -> i64 {
let nanos: i64 = (from - NaiveTime::from_hms_opt(0, 0, 0).unwrap())
.num_nanoseconds()
.unwrap();
nanos * 300 / (1e9 as i64)
}
let date = dt.date();
let time = dt.time();
let days = to_days(date, 1900) as i32;
let seconds_fragments = to_sec_fragments(time);
let dt = tiberius::time::DateTime::new(days, seconds_fragments as u32);
ColumnData::DateTime(Some(dt))
}For some example usage: /// Convert my domain object into a row usable by bulk insert
impl<'a> IntoRow<'a> for EventRow {
fn into_row(self) -> TokenRow<'a> {
let mut row = TokenRow::new();
row.push(naive_dt_to_datetime1(self.timestamp.naive_utc()));
row.push(self.event_id.into_sql());
row
}
}
/// Perform bulk insert
async fn do_inserts(db: &Client, events: &Vec<EventRow>) -> anyhow::Result<()>{
let mut bulk = db.bulk_insert("TABLE_NAME").await?;
for evt in events {
let row = evt.into_row();
bulk.send(row).await?;
}
_ = bulk.finalize().await?;
Ok(())
} |
joelparkerhenderson
added a commit
to mssql-rust/mssql-rust
that referenced
this pull request
Aug 29, 2026
Mirrors tiberius-rs/tiberius#298 (allow converting from DateTime2 to Datetimen in 'tds73'). With tds73 enabled, bulk_insert into a `datetime` column previously failed with a BulkInput "invalid data type" error for any value that reached ColumnData as DateTime2 (which chrono/time's NaiveDateTime/PrimitiveDateTime conversions always produce) -- unlike Client::execute, which already downgrades DateTime2 to the legacy DateTime type before encoding (see the sibling arm a few lines below). Disabling tds73 isn't a workaround either, since `date`/`time` columns in the same table need it. Reimplemented rather than cherry-picked: the original PR's patch added an unconditional `use chrono::{Duration, NaiveDate}` at the top of column_data.rs (a file compiled regardless of the `chrono` feature), which would have broken the default build (tds73 on, chrono off is exactly cargo's default feature set). This version needs neither chrono nor time -- date-days and seconds-fragment arithmetic reuses this crate's own tds::time::DateTime type directly, so the new encode arm is gated only by `tds73`, matching its sibling arms. Also generalized the seconds-fragments conversion to use DateTime2's actual `time().scale()` instead of hardcoding scale 7 (true for every chrono/time-crate conversion today, but not guaranteed for a value built directly via the public DateTime2::new/Time::new API), and fixed the day-range check: the original rejected anything before 1900-01-01, but `datetime`'s documented minimum -- and the actual boundary that matters for a `datetime` column -- is 1753-01-01. Verified: cargo check across all 6 CI feature combinations (including plain `cargo check`, i.e. tds73 on / chrono off, which is the case the original patch would have failed on), cargo clippy --all-targets, cargo fmt --check, and cargo test --lib (146 passing, including two new tests covering the DateTime2->Datetimen conversion and the 1753 boundary). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0156Di1tRRLsJK8ctU1AmAJr
MattJackson
referenced
this pull request
in MattJackson/tiberius-ng
Aug 29, 2026
…7, #373) Adapted from upstream #298 (author @Geo-W).
joelparkerhenderson
added a commit
to mssql-rust/mssql-rust
that referenced
this pull request
Aug 30, 2026
Mirrors tiberius-rs/tiberius#298 (allow converting from DateTime2 to Datetimen in 'tds73'). With tds73 enabled, bulk_insert into a `datetime` column previously failed with a BulkInput "invalid data type" error for any value that reached ColumnData as DateTime2 (which chrono/time's NaiveDateTime/PrimitiveDateTime conversions always produce) -- unlike Client::execute, which already downgrades DateTime2 to the legacy DateTime type before encoding (see the sibling arm a few lines below). Disabling tds73 isn't a workaround either, since `date`/`time` columns in the same table need it. Reimplemented rather than cherry-picked: the original PR's patch added an unconditional `use chrono::{Duration, NaiveDate}` at the top of column_data.rs (a file compiled regardless of the `chrono` feature), which would have broken the default build (tds73 on, chrono off is exactly cargo's default feature set). This version needs neither chrono nor time -- date-days and seconds-fragment arithmetic reuses this crate's own tds::time::DateTime type directly, so the new encode arm is gated only by `tds73`, matching its sibling arms. Also generalized the seconds-fragments conversion to use DateTime2's actual `time().scale()` instead of hardcoding scale 7 (true for every chrono/time-crate conversion today, but not guaranteed for a value built directly via the public DateTime2::new/Time::new API), and fixed the day-range check: the original rejected anything before 1900-01-01, but `datetime`'s documented minimum -- and the actual boundary that matters for a `datetime` column -- is 1753-01-01. Verified: cargo check across all 6 CI feature combinations (including plain `cargo check`, i.e. tds73 on / chrono off, which is the case the original patch would have failed on), cargo clippy --all-targets, cargo fmt --check, and cargo test --lib (146 passing, including two new tests covering the DateTime2->Datetimen conversion and the 1753 boundary). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0156Di1tRRLsJK8ctU1AmAJr
This was referenced Sep 1, 2026
MattJackson
added a commit
that referenced
this pull request
Sep 3, 2026
MattJackson
added a commit
that referenced
this pull request
Sep 4, 2026
MattJackson
added a commit
that referenced
this pull request
Sep 6, 2026
MattJackson
added a commit
that referenced
this pull request
Sep 16, 2026
MattJackson
added a commit
that referenced
this pull request
Sep 24, 2026
Contributor
|
Included in #442 — shipped in tiberius v0.13.0. Thanks for this! Closing. |
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.
This pr tries to address the following problem:
When inserting
NaiveDateTimeinto msdatetimecolumns withbulk_insertandtds73is turned on by default, it would raiseErr` value: BulkInput("invalid data type, expecting Some(VarLenSized(VarLenContext { type: Datetimen, len: 8, collation: None })) but found DateTime2(Some(DateTime2 { date: Date(693604), time: Time { increments: 330110000000, scale: 7 } }))cannot just disable
tds73feature asdate/timemight in the same table withdatetimesimultaneously andinto_sql/to_sqlis impl fordate/timein withouttds73only.normal inserting with
executedoes not get this issue as it callsDateTime2toNonewhen converting.https://github.com/prisma/tiberius/blob/98943b21b5f7a5135d3ae4bd51acbf268bb99189/src/tds/codec/column_data.rs#L615-L619
Don't know whether it is a good solution. Or maybe any other impl can solve this issue is appreciated.
Thank you so much~