mysql icon indicating copy to clipboard operation
mysql copied to clipboard

No way to access granular results from LOAD DATA

Open droberts-sea opened this issue 5 years ago • 1 comments

Issue description

This began life as a question on StackOverflow. Discussion is copied here for ease of access.

Question

I am writing a Go program that interacts with a MySQL database. In MySQL, when you do a LOAD DATA query, in addition to the regular X rows affected line you get a line with more granular information:

mysql> LOAD DATA LOCAL INFILE 'many-lines.tsv' REPLACE INTO TABLE test_table (id, timestamp);
Query OK, 6 rows affected (0.01 sec)
Records: 3  Deleted: 3  Skipped: 0  Warnings: 0

As documented here under the section "Statement Result Information".

I would love to be able to access this from my Go program, but I cannot figure out how, or whether it's even possible. sql.DB.Exec() returns a Result, but that only has a RowsAffected field. This contains a sum of rows written + rows deleted and ignores rows skipped, and is therefore ambiguous (write 3, delete 2 and skip 2 is the same as write 5, delete 0 and skip 0).

I looked through the documentation for the Go MySQL driver, but couldn't find anything there that does what I want.

Is there a way to get access to this information?

Answer

The information is actually a ER_LOAD_INFO "error" (notionally info) message of the server.

This gets communicated as an informational message in the OK response from the server.

Looking at the decoding of the OK packet in go, it isn't parsing out the info (human readable status information). When making the connection ensure that clientSessionTrack is part of the connection flags.

So a few small enhancements to the Go MySQL driver and you'll be able to access it.

Example code

N/A

Error log

N/A

Configuration

Driver version (or git SHA): v1.4.1

Go version:

$ go version
go version go1.15.2 darwin/amd64

Server version: Server OS:

mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| innodb_version          | 5.6.50                       |
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| version                 | 5.6.50                       |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | Linux                        |
+-------------------------+------------------------------+
7 rows in set (0.02 sec)

droberts-sea avatar Dec 04 '20 21:12 droberts-sea

just asking, is it anywhere on the roadmap?

yosefy avatar Feb 06 '23 08:02 yosefy