Skip to content

Commit

Permalink
Fix handling of JSON literal values julien-duponchelle#226 julien-dup…
Browse files Browse the repository at this point in the history
…onchelle#298

In case of large JSON documents leteral values should be retrieved with
`read_uint32` instead of `read_uint32` to avoid further AssertionError
  • Loading branch information
Alexander Ignatov committed Dec 13, 2019
1 parent fa5892d commit f56b942
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pymysqlreplication/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def read_offset_or_inline(packet, large):

if t in (JSONB_TYPE_LITERAL,
JSONB_TYPE_INT16, JSONB_TYPE_UINT16):
return (t, None, packet.read_binary_json_type_inlined(t))
return (t, None, packet.read_binary_json_type_inlined(t, large))
if large and t in (JSONB_TYPE_INT32, JSONB_TYPE_UINT32):
return (t, None, packet.read_binary_json_type_inlined(t))
return (t, None, packet.read_binary_json_type_inlined(t, large))

if large:
return (t, packet.read_uint32(), None)
Expand Down Expand Up @@ -255,7 +255,7 @@ def read_length_coded_pascal_string(self, size):
def read_variable_length_string(self):
"""Read a variable length string where the first 1-5 bytes stores the
length of the string.
For each byte, the first bit being high indicates another byte must be
read.
"""
Expand Down Expand Up @@ -384,9 +384,9 @@ def read_binary_json_type(self, t, length):

raise ValueError('Json type %d is not handled' % t)

def read_binary_json_type_inlined(self, t):
def read_binary_json_type_inlined(self, t, large):
if t == JSONB_TYPE_LITERAL:
value = self.read_uint16()
value = self.read_uint32() if large else self.read_uint16()
if value == JSONB_LITERAL_NULL:
return None
elif value == JSONB_LITERAL_TRUE:
Expand Down
10 changes: 10 additions & 0 deletions pymysqlreplication/tests/test_data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,16 @@ def test_json_large(self):

self.assertEqual(event.rows[0]["values"]["value"], to_binary_dict(data))

def test_json_large_with_literal(self):
if not self.isMySQL57():
self.skipTest("Json is only supported in mysql 5.7")
data = dict([('foooo%i'%i, 'baaaaar%i'%i) for i in range(2560)], literal=True) # Make it large with literal
create_query = "CREATE TABLE test (id int, value json);"
insert_query = """INSERT INTO test (id, value) VALUES (1, '%s');""" % json.dumps(data)
event = self.create_and_insert_value(create_query, insert_query)

self.assertEqual(event.rows[0]["values"]["value"], to_binary_dict(data))

def test_json_types(self):
if not self.isMySQL57():
self.skipTest("Json is only supported in mysql 5.7")
Expand Down

0 comments on commit f56b942

Please sign in to comment.