Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with Date datatype in bulk insert when trying to insert a record in a targe table with the same schema #373

Open
hugomf opened this issue Jan 23, 2025 · 2 comments

Comments

@hugomf
Copy link

hugomf commented Jan 23, 2025

I'm migrating a table from one database to another using Tiberius. The schemas of the source and target tables are almost identical, with the only difference being that the source table has an IDENTITY column (ID), while the target table has an INT column.

I have a date conversion function that converts a tiberius::time::time::Date to a tiberius::time::Date. The conversion seems to work fine, as I don't encounter any errors when pushing the data into the TokenRow. However, when I execute the following line:

let res = req.finalize().await?;
info!("########   TOTAL: {}", res.total());

I get the following error:

[2025-01-23T18:37:45Z ERROR tiberius::tds::stream::token] Invalid column type from bcp client for colid 18. code=4816

If I remove the Date column from the source table, the error does not occur.

Here is the relevant code for the date conversion:

...
tiberius::ColumnType::Daten => {
    let val = row.get::<tiberius::time::time::Date, _>(index);
    match val {
        Some(originaldate) => {
            info!("oooooooooooooo    date before: {}", originaldate);
            let newdate = convert_date(originaldate);
            info!("oooooooooooooo    date converted: {:?}", newdate);
            ColumnData::Date(Some(newdate))
        }
        None => ColumnData::Date(None), // Handle NULL values
    }
}
...

And the date conversion function:

fn convert_date(olddate: tiberius::time::time::Date) -> tiberius::time::Date {
    // Create a NaiveDate for January 1, year 1
    let epoch = NaiveDate::from_ymd_opt(1, 1, 1).expect("Invalid epoch date");
    let date = NaiveDate::from_ymd_opt(
        olddate.year(),
        olddate.month() as u32,
        olddate.day() as u32, 
    ).expect("Invalid date");
    let days_since_epoch = date.signed_duration_since(epoch).num_days() as u32;
    tiberius::time::Date::new(days_since_epoch)
}

Questions:

  • What could be causing the Invalid column type from bcp client for colid 18 error?
  • Is there an issue with the date conversion logic that might be causing this error?
  • Are there any known issues with Tiberius and BCP client compatibility for date columns?

Any help or insights would be greatly appreciated!

@setenum
Copy link

setenum commented Jan 23, 2025

@hugomf I think I have the same problem - #346

Maybe you can try with - https://github.com/setenum/tiberius/tree/fix/bulk-date-token-error

@hugomf
Copy link
Author

hugomf commented Jan 23, 2025

@hugomf I think I have the same problem - #346

Maybe you can try with - https://github.com/setenum/tiberius/tree/fix/bulk-date-token-error

wow!!! that was fast response, I will take a look thank you!

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

No branches or pull requests

2 participants