Skip to content

Commit 997863b

Browse files
committed
refactor: replace serialize with pack on primivite values
1 parent b2bdbb1 commit 997863b

4 files changed

Lines changed: 13 additions & 30 deletions

File tree

src/lib/parquet/src/Flow/Parquet/ParquetFile/Data/Converter/Int64DateTimeConverter.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Flow\Parquet\ParquetFile\Data\Converter;
66

7-
use function Flow\Types\DSL\{type_instance_of, type_integer};
87
use Flow\Parquet\Options;
98
use Flow\Parquet\ParquetFile\Data\Converter;
109
use Flow\Parquet\ParquetFile\Schema\{FlatColumn, LogicalType, PhysicalType};
@@ -13,7 +12,8 @@ final class Int64DateTimeConverter implements Converter
1312
{
1413
public function fromParquetType(mixed $data) : \DateTimeImmutable
1514
{
16-
return $this->microsecondsToDateTimeImmutable(type_integer()->assert($data));
15+
/** @var int $data */
16+
return new \DateTimeImmutable('@' . \number_format($data / 1_000_000, 6, '.', ''));
1717
}
1818

1919
public function isFor(FlatColumn $column, Options $options) : bool
@@ -27,16 +27,7 @@ public function isFor(FlatColumn $column, Options $options) : bool
2727

2828
public function toParquetType(mixed $data) : int
2929
{
30-
return $this->dateTimeToMicroseconds(type_instance_of(\DateTimeInterface::class)->assert($data));
31-
}
32-
33-
private function dateTimeToMicroseconds(\DateTimeInterface $dateTime) : int
34-
{
35-
return (int) \bcadd(\bcmul($dateTime->format('U'), '1000000'), $dateTime->format('u'));
36-
}
37-
38-
private function microsecondsToDateTimeImmutable(int $microseconds) : \DateTimeImmutable
39-
{
40-
return new \DateTimeImmutable('@' . \number_format($microseconds / 1_000_000, 6, '.', ''));
30+
/** @var \DateTimeInterface $data */
31+
return $data->getTimestamp() * 1_000_000 + (int) $data->format('u');
4132
}
4233
}

src/lib/parquet/src/Flow/Parquet/Writer/PageBuilder/DictionaryBuilder/FloatDictionaryBuilder.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,17 @@ public function build(WriteFlatColumnValues $data) : Dictionary
2121
continue;
2222
}
2323

24-
$hash = \serialize($value);
24+
$hash = \pack('E', $value);
2525

2626
if (!isset($valueToIndex[$hash])) {
27-
$dictionary[] = $hash;
27+
$dictionary[] = $value;
2828
$valueToIndex[$hash] = $dictionarySize;
2929
$dictionarySize++;
3030
}
3131

3232
$indices[] = $valueToIndex[$hash];
3333
}
3434

35-
foreach ($dictionary as $index => $value) {
36-
$dictionary[$index] = @\unserialize($value, ['allowed_classes' => []]);
37-
}
38-
3935
return new Dictionary($dictionary, $indices);
4036
}
4137
}

src/lib/parquet/src/Flow/Parquet/Writer/PageBuilder/DictionaryBuilder/ObjectDictionaryBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ public function build(WriteFlatColumnValues $data) : Dictionary
2121
continue;
2222
}
2323

24-
$hash = \serialize($value);
24+
if ($value instanceof \DateTimeInterface) {
25+
$hash = $value->format('U u');
26+
} else {
27+
$hash = \serialize($value);
28+
}
2529

2630
if (!isset($valueToIndex[$hash])) {
27-
$dictionary[] = $hash;
31+
$dictionary[] = $value;
2832
$valueToIndex[$hash] = $dictionarySize;
2933
$dictionarySize++;
3034
}
3135

3236
$indices[] = $valueToIndex[$hash];
3337
}
3438

35-
foreach ($dictionary as $index => $value) {
36-
$dictionary[$index] = @\unserialize($value, ['allowed_classes' => [\DateTimeImmutable::class, \DateInterval::class]]);
37-
}
38-
3939
return new Dictionary($dictionary, $indices);
4040
}
4141
}

src/lib/parquet/src/Flow/Parquet/Writer/PageBuilder/DictionaryBuilder/ScalarDictionaryBuilder.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function build(WriteFlatColumnValues $data) : Dictionary
2222
}
2323

2424
if (\is_float($value)) {
25-
$key = \serialize($value);
25+
$key = \pack('E', $value);
2626
} else {
2727
$key = $value;
2828
}
@@ -36,10 +36,6 @@ public function build(WriteFlatColumnValues $data) : Dictionary
3636
$indices[] = $valueToIndex[$key];
3737
}
3838

39-
foreach ($dictionary as $index => $value) {
40-
$dictionary[$index] = $value;
41-
}
42-
4339
return new Dictionary($dictionary, $indices);
4440
}
4541
}

0 commit comments

Comments
 (0)