singlestore-labs / singlestoredb-python

Python SDK for the SingleStore database and workspace management APIs
Apache License 2.0
22 stars 17 forks source link

fix corrupted integer value followed by long string #7

Closed morokosi closed 1 year ago

morokosi commented 1 year ago

Recently I switched from pymysql to singlestoredb and found that integer column values returned from singlestoredb is corrupted when followed by a string column of a certain length.

I added a minimal reproductive as a test case. Without this change, the test case fails as follows:

    def test_long_string(self):
        string = 'a' * 48
        self.cur.execute(f"SELECT 1 AS column_1, '{string}' AS column_2")
>       self.assertEqual((1, string), self.cur.fetchone())
E       AssertionError: Tuples differ: (1, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') != (10, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
E
E       First differing element 0:
E       1
E       10
E
E       - (1, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
E       + (10, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
E       ?   +

The root cause is a misuse of stortoll/stotoull, which reads a consecutive numeric characters, so if a length-encoded string follows and the encoded length can be interpreted as numeric characters, they are read as part of integer value incorrectly. (in the test case, 48 == '0 'in ASCII)

By creating a substring with a provided length, the integer value can be read correctly.

kesmit13 commented 1 year ago

@morokosi Thanks again for the bug report and fix. This was a great catch!