Skip to content
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

AVRO-4115: [PHP] Fix long encoding #3303

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
63 changes: 63 additions & 0 deletions lang/php/lib/AvroVarint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apache\Avro;

/**
* Varint encoding/decoding for 64bit php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more information would be nice!
With a link to the specification and/or other resource explaining what this is about.

*
* @package Avro
*/
class AvroVarint
{
public static function encodeLong(int $n): string
Copy link
Member

@martin-g martin-g Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it would be good to add some documentation for the new public methods in this class. The old methods (in AvroIOBinaryDecoder have at least @param, @returns, etc.
Also in the other SDKs the unit tests are associated with the class they test.
Here we don't have any tests. There are tests in DatumIOTest but it will be harder/confusing for the next contributor to make the connection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martin-g the PHP DocBlocks are a rather old-fashion thing in PHP, from the time it didn't support type-hinting. I'd suggest dropping them altogether, in favor of proper type-hinting, unless the documentation actually adds some information other than types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything is better than nothing.

{
if ($n >= 0 && $n < 0x80) {
return chr($n);
}

$buf = [];
if (($n & ~0x7F) != 0) {
$buf[] = ($n | 0x80) & 0xFF;
$n = ($n >> 7) ^ (($n >> 63) << 57); // unsigned shift right ($n >>> 7)

while ($n > 0x7F) {
$buf[] = ($n | 0x80) & 0xFF;
$n >>= 7; // $n is always positive here
}
}

$buf[] = $n;
return pack("C*", ...$buf);
}

public static function decodeLong(array $bytes): int
{
$b = array_shift($bytes);
$n = $b & 0x7f;
$shift = 7;
while (0 != ($b & 0x80)) {
$b = array_shift($bytes);
$n |= (($b & 0x7f) << $shift);
$shift += 7;
}
return ($n >> 7) ^ (($n >> 63) << 57) ^ -($n & 1);
}
}
11 changes: 2 additions & 9 deletions lang/php/lib/Datum/AvroIOBinaryDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Apache\Avro\AvroException;
use Apache\Avro\AvroGMP;
use Apache\Avro\AvroIO;
use Apache\Avro\AvroVarint;

/**
* Decodes and reads Avro data from an AvroIO object encoded using
Expand Down Expand Up @@ -116,15 +117,7 @@ public function readLong()
*/
public static function decodeLongFromArray($bytes)
{
$b = array_shift($bytes);
$n = $b & 0x7f;
$shift = 7;
while (0 != ($b & 0x80)) {
$b = array_shift($bytes);
$n |= (($b & 0x7f) << $shift);
$shift += 7;
}
return (($n >> 1) ^ -($n & 1));
return AvroVarint::decodeLong($bytes);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions lang/php/lib/Datum/AvroIOBinaryEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Apache\Avro\Avro;
use Apache\Avro\AvroGMP;
use Apache\Avro\AvroIO;
use Apache\Avro\AvroVarint;

/**
* Encodes and writes Avro data to an AvroIO object using
Expand Down Expand Up @@ -101,13 +102,7 @@ public static function encodeLong($n)
{
$n = (int) $n;
$n = ($n << 1) ^ ($n >> 63);
$str = '';
while (0 != ($n & ~0x7F)) {
$str .= chr(($n & 0x7F) | 0x80);
$n >>= 7;
}
$str .= chr($n);
return $str;
return AvroVarint::encodeLong($n);
}

/**
Expand Down
30 changes: 26 additions & 4 deletions lang/php/test/DatumIOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,33 @@ function data_provider()
array('"int"', 1, "\002"),
array('"int"', 2147483647, "\xFE\xFF\xFF\xFF\x0F"),

// array('"long"', (int) -9223372036854775808, "\001"),
array('"long"', (int) -9223372036854775808, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
array('"long"', -(1<<62), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F"),
array('"long"', -(1<<61), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x3F"),
array('"long"', -4294967295, "\xFD\xFF\xFF\xFF\x1F"),
array('"long"', -1<<24, "\xFF\xFF\xFF\x0F"),
array('"long"', -1<<16, "\xFF\xFF\x07"),
array('"long"', -255, "\xFD\x03"),
array('"long"', -128, "\xFF\x01"),
array('"long"', -127, "\xFD\x01"),
array('"long"', -10, "\x13"),
array('"long"', -3, "\005"),
array('"long"', -2, "\003"),
array('"long"', -1, "\001"),
array('"long"', 0, "\000"),
array('"long"', 1, "\002"),
// array('"long"', 9223372036854775807, "\002")
array('"long"', 0, "\000"),
array('"long"', 1, "\002"),
array('"long"', 2, "\004"),
array('"long"', 3, "\006"),
array('"long"', 10, "\x14"),
array('"long"', 127, "\xFE\x01"),
array('"long"', 128, "\x80\x02"),
array('"long"', 255, "\xFE\x03"),
array('"long"', 1<<16, "\x80\x80\x08"),
array('"long"', 1<<24, "\x80\x80\x80\x10"),
array('"long"', 4294967295, "\xFE\xFF\xFF\xFF\x1F"),
array('"long"', 1<<61, "\x80\x80\x80\x80\x80\x80\x80\x80\x40"),
array('"long"', 1<<62, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01"),
array('"long"', 9223372036854775807, "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),

array('"float"', (float) -10.0, "\000\000 \301"),
array('"float"', (float) -1.0, "\000\000\200\277"),
Expand Down
Loading