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

MySqlBulkCopy always inserts TimeOnly type as '00:00:00' into TIME column #1146

Closed
Dargazo opened this issue Feb 24, 2022 · 1 comment
Closed
Assignees
Labels

Comments

@Dargazo
Copy link

Dargazo commented Feb 24, 2022

Software versions
MySqlConnector version: 2.1.7
Server type (MySQL, MariaDB, Aurora, etc.) and version: MariaDB 10.6.7 (also 10.6.3)
.NET version: 6

Describe the bug
I have a .NET DataTable that holds a TimeOnly Column. When I try to bulk load it into a MariaDB table, where the TimeOnly column maps to a MariaDB 'TIME' column, it is always inserted as '00:00:00'.

Exception
BulkCopyResult indicates a truncation warning, no exception is thrown

Code sample

       private static string GetConnStr() 
        {
            MySqlConnectionStringBuilder l_Builder = new MySqlConnectionStringBuilder();
            l_Builder.Database = "db_test";
            l_Builder.Server = "localhost";
            l_Builder.Port = 3306;
            l_Builder.UserID = "*******";
            l_Builder.Password = "************";
            l_Builder.AllowLoadLocalInfile = true;
            l_Builder.AllowUserVariables = true;
            return l_Builder.ToString();
        }

        static async Task Main(string[] args)
        {
            // Setup
            using (MySqlConnection l_Conn = new MySqlConnection(GetConnStr()))
            {
                string l_DropQ = "DROP TABLE IF EXISTS `tbl`";
                string l_CreateQ = @"CREATE TABLE `tbl` (
	                                `A` TIME NOT NULL
                                )
                                COLLATE='utf8_general_ci'
                                ENGINE=InnoDB";
                l_Conn.Open();

                MySqlCommand l_DropCmd = new MySqlCommand(l_DropQ, l_Conn);
                await l_DropCmd.ExecuteNonQueryAsync();

                MySqlCommand l_CreateCmd = new MySqlCommand(l_CreateQ, l_Conn);
                await l_CreateCmd.ExecuteNonQueryAsync();
            }

            DataTable l_Table = new DataTable();
            l_Table.Columns.Add("A", typeof(TimeOnly));

            TimeOnly l_TO = TimeOnly.Parse("11:05:01");
            l_Table.Rows.Add(new object[] { l_TO });

            using (MySqlConnection l_Conn = new MySqlConnection(GetConnStr()))
            {
                await l_Conn.OpenAsync();
                
                // bulk copy the data
                var bulkCopy = new MySqlBulkCopy(l_Conn);
                bulkCopy.DestinationTableName = "tbl";
                var result = await bulkCopy.WriteToServerAsync(l_Table);
            }
        }

The MySqlBulkCopyResult now holds the message: "Data truncated for column 'A' at row 1".

Expected behavior
When the code above is executed: 1 row in the 'tbl' table, containing '11:05:01'.

Additional context
Converting the TimeOnly to a TimeSpan seems to work. i.e.

l_Table.Columns.Add("A", typeof(TimeSpan));
 l_Table.Rows.Add(new object[] { l_TO.ToTimeSpan() });

Though the warning in MySqlBulkCopyResult still remains.

I have also tested this using the DateOnly struct. This always inserts '0000-00-00' in a MariaDB DATE column, regardless of the value I use in the DataTable object.
The workaround here is to convert the DateOnly struct to a DateTime struct, and it starts working.

@bgrainger bgrainger self-assigned this Feb 26, 2022
@bgrainger bgrainger added the bug label Feb 26, 2022
@bgrainger
Copy link
Member

Fixed in 2.1.8.

okramarenko added a commit to memsql/SingleStoreNETConnector that referenced this issue Mar 27, 2023
Summary:
This diff grabs changes from original connector that were made in 2.1.7 and 2.1.8 releases:
- Only insert a semicolon when concatenating SQL. Fixes mysql-net#1133
- Parse multiple TLS versions correctly. Fixes mysql-net#1138
- Document return value of ExecuteNonQuery. Fixes mysql-net#1136
- Enable all .NET analyzers
- Add support for new types to SingleStoreBulkCopy. Fixes mysql-net#1143
- Update SingleStoreParameter types documentation
- Update StyleCop to 1.2.0-beta.406
- Reset LastInsertedId to 0 between commands, not -1. Fixes mysql-net#1147
- Fix BulkCopy for DateOnly and TimeOnly. Fixes mysql-net#1146

Test Plan: https://app.circleci.com/pipelines/github/memsql/SingleStoreNETConnector/249/workflows/3100503c-8a0f-48e6-99a3-635efe59384f

Reviewers: pmishchenko-ua

Reviewed By: pmishchenko-ua

Subscribers: engineering-list

JIRA Issues: PLAT-6408

Differential Revision: https://grizzly.internal.memcompute.com/D61685
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants