dolt icon indicating copy to clipboard operation
dolt copied to clipboard

ruby mysql2 gem reads field types incorrectly

Open carlivar opened this issue 3 years ago • 10 comments

The ruby mysql2 client (gem) is not interpreting column types correctly when connecting to the dolt sql-server.

I'm using the latest mysql2 gem (0.5.5) and I have tried many different ruby versions. I've also tried both MacOS and Linux.

My testing is with the "stocks" database: https://www.dolthub.com/repositories/post-no-preference/stocks

The table schema is:

mysql> describe ohlcv;
+------------+----------------+------+-----+---------+-------+
| Field      | Type           | Null | Key | Default | Extra |
+------------+----------------+------+-----+---------+-------+
| date       | date           | NO   | PRI | NULL    |       |
| act_symbol | varchar(16383) | NO   | PRI | NULL    |       |
| open       | decimal(7,2)   | YES  |     | NULL    |       |
| high       | decimal(7,2)   | YES  |     | NULL    |       |
| low        | decimal(7,2)   | YES  |     | NULL    |       |
| close      | decimal(7,2)   | YES  |     | NULL    |       |
| volume     | bigint         | YES  |     | NULL    |       |
+------------+----------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

An example query and values from dolt sql-client:

mysql> SELECT  `ohlcv`.* FROM `ohlcv` WHERE `ohlcv`.`date` = '2023-04-19' AND `ohlcv`.`act_symbol` = 'TTD' LIMIT 1
    -> ;
+------------+------------+-------+-------+-------+-------+---------+
| date       | act_symbol | open  | high  | low   | close | volume  |
+------------+------------+-------+-------+-------+-------+---------+
| 2023-04-19 | TTD        | 60.93 | 62.15 | 60.85 | 61.61 | 1510574 |
+------------+------------+-------+-------+-------+-------+---------+
1 row in set (0.00 sec)

Ruby test script:

#!/usr/bin/env ruby

require 'mysql2'
require 'pp'

client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "stocks")
r = client.query("SELECT * FROM ohlcv WHERE date='2023-04-19' AND act_symbol='TTD' LIMIT 1")
pp r.field_types
r.each do |row|
  puts row["close"].to_f
  puts row["close"].class
end

Output of above script is below. As you can see, almost everything is wrong. The decimal scale is the biggest problem and is causing the values to be Integer rather than what should normally (I think) be BigDecimal. But the size of the varchar and bigint types are also not matching.

% ./test.rb 
["date", "varchar(21844)", "decimal(8,0)", "decimal(8,0)", "decimal(8,0)", "decimal(8,0)", "bigint(20)"]
61.0
Integer

Interestingly, ActiveRecord from Ruby on Rails seems to correctly detect the Decimal type and precision/scale but is still giving wrong values, I assume because the underlying mysql2 connector that ActiveRecord has to use for Dolt has the issue (I originally discovered this problem in ActiveRecord but used the above test script to try to isolate things).

carlivar avatar Apr 28 '23 05:04 carlivar

SELECT COMPANY.COMPANY_CODE,COMPANY.FOUNDER,COUNT(DISTINCT (LEAD_MANAGER_CODE)),COUNT(DISTINCT (SENIOR_MANAGER_CODE)),COUNT(DISTINCT(MANAGER_CODE)),COUNT(DISTINCT(EMPLOYEE_CODE)) FROM COMPANY JOIN LEAD_MANAGER ON COMPANY.COMPANY_CODE = LEAD_MANAGER.COMPANY_CODE, JOIN SENIOR_MANAGER ON COMPANY.COMPANY_CODE = SENIOR_MANAGER.COMPANY_CODE, JOIN MANAGER ON COMPANY.COMPANY_CODE = MANAGER.COMPANY_CODE, JOIN EMPLOYEE ON COMPANY.COMPANY_CODE = EMPLOYEE.EMPLOYEE_CODE GROUP BY COMPANY.COMPANY_CODE,COMPANY.FOUNDER

Do you find any problem in this code ?

Raj4478 avatar Apr 28 '23 05:04 Raj4478

Hey @carlivar, thanks for reporting this. We have some tests that verify the ruby-mysql gem works with Dolt, but I don't think we have tests for the mysql2 gem. Thank you for including such a great repro case. I'm going to try to repro over on our end and see what we can figure out.

fulghum avatar Apr 28 '23 16:04 fulghum

Thanks. The ruby (and RoR) mysql ecosystem is a bit confusing. I didn't have the ruby-mysql gem on my radar, but I just installed that and added it to my test script. It works correctly, so I can confirm this seems to be particular to the mysql2 gem. I believe mysql2 wraps libmysqlclient, if that helps, whereas ruby-mysql is written in pure ruby.

This is helpful though because I think I can switch to ruby-mysql for now and get unblocked.

carlivar avatar Apr 28 '23 16:04 carlivar

Very cool! I'm not a Ruby expert, but always happy to dig in and learn something new. That's great news that you are potentially able to get unblocked through the ruby-mysql gem. I'm still going to dig in to the mysql2 lib and see if we can find any clues – we're striving for 100% MySQL compatibility, so it seems like there's some small difference there we can learn from.

fulghum avatar Apr 28 '23 17:04 fulghum

@carlivar We'd love to learn about your use case too. Feel free to come by Discord or email me if you want to share.

bpf120 avatar Apr 28 '23 17:04 bpf120

@fulghum I did some research into Ruby & mysql. Yeah, you are testing with a gem that is considered obsolete. mysql2 is mainstream. I'm not unblocked after all because Rails got rid of support for mysql (aka ruby-mysql) after 4.x (they are now on version 7.x).

carlivar avatar Apr 28 '23 18:04 carlivar

Thanks for the extra context @carlivar. It does look like the official MySQL docs point Ruby developers towards the mysql2 library.

I've been able to repro this behavior thanks to your easy instructions. I'm just starting to dig into some packet captures between MySQL and Dolt to see what's different in our response. Hopefully I'll have some more clues/ideas soon.

fulghum avatar Apr 28 '23 18:04 fulghum

@carlivar – Okay, made some progress here... looking at the wire captures, Dolt isn't filling in as much metadata as MySQL does for the Field metadata packets. In particular, the Decimal field should contain the scale of the type, i.e. how many digits are to the right of the decimal point. Dolt is currently always populating this with 0, so that explains the results we're seeing in your query.

I have a fix coded up to populate that Decimal field in the field metadata and it looks like it's working correctly now:

❯ ruby ./mysql2-repro.rb dolt 3306 dolt   
["int(11)", "decimal(8,2)"]
{"pk"=>1, "d1"=>0.12345678e6}

I've been focused just on decimal types so far, but next I wanna try this fix out with your exact repro (I've been using a slightly simplified version) and see if any other types are causing problems. Assuming that goes well, I'll figure out some good automated tests and can try to get this fix released by tonight. If you do want to try something out faster than that, I'm more than happy to provide instructions to build a dolt version from a dev branch.

This was a good find! Thank you for taking the time to report it to us so we can fix it! 🙏

fulghum avatar Apr 28 '23 19:04 fulghum

Great, thanks again @fulghum and glad to help. I'll pop into Discord at some point and describe my use case a bit more, though it's just a personal project. I can wait until the fix is released but I appreciate the dev branch offer!

carlivar avatar Apr 28 '23 20:04 carlivar

Happy to help! We just released Dolt version 0.75.15 and it includes the fix for this issue.

I've also updated our supported client library docs to mention the mysql2 gem.

And I put together an automated test that runs some sanity checks using the mysql2 gem to make sure it doesn't regress.

Thanks for bringing this to our attention and helping us improve our Ruby support! I'm going to go ahead and resolve this issue now, but please don't hesitate to reopen if you're still seeing this issue with the new version, or cut us a new issue if you hit any other snags. MySQL compatibility is really important to us, so we're happy to fix these.

If you do swing by DoltHub Discord, we'd love to hear more about what you're up to and we're happy to help if you have any questions about Dolt or feature requests/ideas. Personal projects are just as cool as any other project in our book 😎 it's always cool to hear what concrete ideas people have for versioned data.

fulghum avatar Apr 28 '23 23:04 fulghum