Skip to content

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

@hugomf

Description

@hugomf

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions