Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,15 @@ private function getOffsetOrInLinedValue(int $bytes, int $intSize, int $valueEnt
$type = $this->binaryDataReader->readUInt8();

if (self::isInLinedType($type, $intSize)) {
$start = $this->binaryDataReader->getReadBytes();
$scalar = $this->parseScalar($type);

// In binlog format, JSON arrays are fixed width elements, even though type value can be smaller.
// In order to properly process this case, we need to move cursor to the next element, which is on position 1 + $valueEntrySize (1 is length of type)
if ($type === self::UINT16 || $type === self::INT16) {
$readNextBytes = $valueEntrySize - 2 - 1;
$this->binaryDataReader->read($readNextBytes);
// Value entries are fixed width (1 type byte + 2 bytes small / 4 bytes large) even when
// the inlined value is shorter, e.g. a literal or an int16 in a large (> 64 KiB)
// document. Skip the rest of the slot so the next entry is read from its own start.
$slack = $valueEntrySize - 1 - ($this->binaryDataReader->getReadBytes() - $start);
if ($slack > 0) {
$this->binaryDataReader->read($slack);
}

return $scalar;
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,25 @@ public function testShouldBeJson(): void
self::assertEquals('"4294967295"', $results[29]['j']);
}

public function testShouldDecodeLiteralsAndShortIntsInsideLargeJsonDocuments(): void
{
if ($this->checkForVersion(5.7) || $this->mySQLReplicationFactory?->getServerInfo()->isMariaDb()) {
self::markTestIncomplete('Only for mysql 5.7 or higher');
}

// documents above 64 KiB use the large format, whose value entries are 1 + 4 bytes wide
$padding = str_repeat('x', 70000);
$event = $this->createAndInsertValue(
'CREATE TABLE test (j JSON)',
"INSERT INTO test VALUES ('{\"a\":true,\"b\":null,\"c\":false,\"d\":7,\"e\":-3,\"f\":\"" . $padding . "\"}'), ('[true,7,null,false,-3,\"" . $padding . "\"]')"
);

self::assertSame('{"a":true,"b":null,"c":false,"d":7,"e":-3,"f":"' . $padding . '"}', $event->values[0]['j']);
// each row is larger than binlog_row_event_max_size, so the second one gets its own event
$second = count($event->values) > 1 ? $event : $this->getEvent();
self::assertSame('[true,7,null,false,-3,"' . $padding . '"]', $second->values[count($second->values) - 1]['j']);
}

public function testShouldKeepVirtualGeneratedColumnAligned(): void
{
$create_query = 'CREATE TABLE test (a INT, gen INT GENERATED ALWAYS AS (a + 1) VIRTUAL, b VARCHAR(20))';
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/JsonBinaryDecoder/JsonBinaryDecoderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ public function testShouldParseDouble(): void
self::assertStringContainsString('3.14', $result);
}

public function testShouldSkipTheWholeInlinedSlotInLargeArrays(): void
{
// [true, 7, null] in the large format: value entries are 1 type byte + 4 bytes, even
// when the inlined value is a 1-byte literal or a 2-byte int16
$binary = pack('C', JsonBinaryDecoderService::LARGE_ARRAY)
. pack('VV', 3, 8 + 3 * 5)
. pack('CV', JsonBinaryDecoderService::LITERAL, JsonBinaryDecoderService::LITERAL_TRUE)
. pack('CV', JsonBinaryDecoderService::INT16, 7)
. pack('CV', JsonBinaryDecoderService::LITERAL, JsonBinaryDecoderService::LITERAL_NULL);
$service = JsonBinaryDecoderService::makeJsonBinaryDecoder($binary);
self::assertSame('[true,7,null]', $service->parseToString());
}

public function testShouldSkipTheWholeInlinedSlotInLargeObjects(): void
{
// {"a":true,"b":7} in the large format: key entries (4-byte offset + 2-byte length),
// then value entries of 1 type byte + 4 bytes, then the key names at offsets 30 and 31
$binary = pack('C', JsonBinaryDecoderService::LARGE_OBJECT)
. pack('VV', 2, 32)
. pack('Vv', 30, 1) . pack('Vv', 31, 1)
. pack('CV', JsonBinaryDecoderService::LITERAL, JsonBinaryDecoderService::LITERAL_TRUE)
. pack('CV', JsonBinaryDecoderService::INT16, 7)
. 'ab';
$service = JsonBinaryDecoderService::makeJsonBinaryDecoder($binary);
self::assertSame('{"a":true,"b":7}', $service->parseToString());
}

public function testShouldThrowExceptionForUnknownType(): void
{
$this->expectException(JsonBinaryDecoderException::class);
Expand Down
Loading