Skip to content
Merged
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
25 changes: 22 additions & 3 deletions lib/avro/datum.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public function read_data($writers_schema, $readers_schema, $decoder)
case AvroSchema::STRING_TYPE:
return $decoder->read_string();
case AvroSchema::BYTES_TYPE:
return $decoder->read_bytes();
return $decoder->read_bytes($writers_schema, $readers_schema, $decoder);
case AvroSchema::ARRAY_SCHEMA:
return $this->read_array($writers_schema, $readers_schema, $decoder);
case AvroSchema::MAP_SCHEMA:
Expand Down Expand Up @@ -918,6 +918,17 @@ static public function long_bits_to_double($bits)
return (double) $double[1];
}

/**
* @param $bytes
* @param $scale
* @return float|int
*/
static public function bytes_to_decimal($bytes, $scale = 0)
{
$int = hexdec(bin2hex($bytes));
return $scale > 0 ? ($int / (10 ** $scale)) : $int;
}

/**
* @var AvroIO
*/
Expand Down Expand Up @@ -996,12 +1007,20 @@ public function read_double()
* of UTF-8 encoded character data.
* @return string
*/
public function read_string() { return $this->read_bytes(); }
public function read_string() { return $this->read($this->read_long()); }

/**
* For Decimal logical type spec https://avro.apache.org/docs/1.10.2/spec.pdf Section 10.1
* @return string
*/
public function read_bytes() { return $this->read($this->read_long()); }
public function read_bytes($writers_schema, $readers_schema, $decoder)
{
$bytes = $this->read($this->read_long());
if ($writers_schema->logical_type() === 'decimal') {
return self::bytes_to_decimal($bytes, $writers_schema->extra_attributes()['scale'] ?? 0);
}
return $bytes;
}

/**
* @param int $len count of bytes to read
Expand Down