Skip to content

Zero-pad fixed-length binary fields #401

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

Merged
merged 1 commit into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pymysqlreplication/binlogstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,8 @@ def __get_table_information(self, schema, table):
cur.execute("""
SELECT
COLUMN_NAME, COLLATION_NAME, CHARACTER_SET_NAME,
COLUMN_COMMENT, COLUMN_TYPE, COLUMN_KEY, ORDINAL_POSITION
COLUMN_COMMENT, COLUMN_TYPE, COLUMN_KEY, ORDINAL_POSITION,
DATA_TYPE, CHARACTER_OCTET_LENGTH
FROM
information_schema.columns
WHERE
Expand Down
6 changes: 6 additions & 0 deletions pymysqlreplication/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def __parse_column_definition(self, column_type, column_schema, packet):
self.type_is_bool = False
self.is_primary = column_schema["COLUMN_KEY"] == "PRI"

# Check for fixed-length binary type. When that's the case then we need
# to zero-pad the values to full length at read time.
self.fixed_binary_length = None
if column_schema["DATA_TYPE"] == "binary":
self.fixed_binary_length = column_schema["CHARACTER_OCTET_LENGTH"]

if self.type == FIELD_TYPE.VARCHAR:
self.max_length = struct.unpack('<H', packet.read(2))[0]
elif self.type == FIELD_TYPE.DOUBLE:
Expand Down
11 changes: 11 additions & 0 deletions pymysqlreplication/row_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def _read_column_data(self, cols_bitmap):
name = self.table_map[self.table_id].columns[i].name
unsigned = self.table_map[self.table_id].columns[i].unsigned
zerofill = self.table_map[self.table_id].columns[i].zerofill
fixed_binary_length = self.table_map[self.table_id].columns[i].fixed_binary_length

if BitGet(cols_bitmap, i) == 0:
values[name] = None
Expand Down Expand Up @@ -154,6 +155,14 @@ def _read_column_data(self, cols_bitmap):
values[name] = self.__read_string(2, column)
else:
values[name] = self.__read_string(1, column)

if fixed_binary_length and len(values[name]) < fixed_binary_length:
# Fixed-length binary fields are stored in the binlog
# without trailing zeros and must be padded with zeros up
# to the specified length at read time.
nr_pad = fixed_binary_length - len(values[name])
values[name] += b'\x00' * nr_pad

elif column.type == FIELD_TYPE.NEWDECIMAL:
values[name] = self.__read_new_decimal(column)
elif column.type == FIELD_TYPE.BLOB:
Expand Down Expand Up @@ -640,6 +649,8 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
'COLUMN_NAME': '__dropped_col_{i}__'.format(i=i),
'COLLATION_NAME': None,
'CHARACTER_SET_NAME': None,
'CHARACTER_OCTET_LENGTH': None,
'DATA_TYPE': 'BLOB',
'COLUMN_COMMENT': None,
'COLUMN_TYPE': 'BLOB', # we don't know what it is, so let's not do anything with it.
'COLUMN_KEY': '',
Expand Down
6 changes: 6 additions & 0 deletions pymysqlreplication/tests/test_data_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def test_column_is_primary(self):
{"COLUMN_NAME": "test",
"COLLATION_NAME": "utf8_general_ci",
"CHARACTER_SET_NAME": "UTF8",
"CHARACTER_OCTET_LENGTH": None,
"DATA_TYPE": "tinyint",
"COLUMN_COMMENT": "",
"COLUMN_TYPE": "tinyint(2)",
"COLUMN_KEY": "PRI"},
Expand All @@ -33,6 +35,8 @@ def test_column_not_primary(self):
{"COLUMN_NAME": "test",
"COLLATION_NAME": "utf8_general_ci",
"CHARACTER_SET_NAME": "UTF8",
"CHARACTER_OCTET_LENGTH": None,
"DATA_TYPE": "tinyint",
"COLUMN_COMMENT": "",
"COLUMN_TYPE": "tinyint(2)",
"COLUMN_KEY": ""},
Expand All @@ -44,6 +48,8 @@ def test_column_serializable(self):
{"COLUMN_NAME": "test",
"COLLATION_NAME": "utf8_general_ci",
"CHARACTER_SET_NAME": "UTF8",
"CHARACTER_OCTET_LENGTH": None,
"DATA_TYPE": "tinyint",
"COLUMN_COMMENT": "",
"COLUMN_TYPE": "tinyint(2)",
"COLUMN_KEY": "PRI"},
Expand Down
12 changes: 12 additions & 0 deletions pymysqlreplication/tests/test_data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def create_and_get_tablemap_event(self, bit):

return event

def test_varbinary(self):
create_query = "CREATE TABLE test(b VARBINARY(4))"
insert_query = "INSERT INTO test VALUES(UNHEX('ff010000'))"
event = self.create_and_insert_value(create_query, insert_query)
self.assertEqual(event.rows[0]["values"]["b"], b'\xff\x01\x00\x00')

def test_fixed_length_binary(self):
create_query = "CREATE TABLE test(b BINARY(4))"
insert_query = "INSERT INTO test VALUES(UNHEX('ff010000'))"
event = self.create_and_insert_value(create_query, insert_query)
self.assertEqual(event.rows[0]["values"]["b"], b'\xff\x01\x00\x00')

def test_decimal(self):
create_query = "CREATE TABLE test (test DECIMAL(2,1))"
insert_query = "INSERT INTO test VALUES(4.2)"
Expand Down