Skip to content

Commit 465f09f

Browse files
authored
Fixed date conversion to parquet (#1647)
1 parent 7729cfe commit 465f09f

File tree

4 files changed

+63
-19
lines changed

4 files changed

+63
-19
lines changed

.nix/shell/starship.sh

Lines changed: 0 additions & 14 deletions
This file was deleted.

shell.nix

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ pkgs.mkShell {
4949
;
5050

5151
shellHook = ''
52-
if [ -f "./.nix/shell/starship.toml" ]; then
53-
export STARSHIP_CONFIG="./.nix/shell/starship.toml"
52+
if [ -f "$PWD/.nix/shell/starship.toml" ]; then
53+
export STARSHIP_CONFIG="$PWD/.nix/shell/starship.toml"
5454
else
55-
export STARSHIP_CONFIG="./.nix/shell/starship.toml.dist"
55+
export STARSHIP_CONFIG="$PWD/.nix/shell/starship.toml.dist"
5656
fi
5757
5858
eval "$(${pkgs.starship}/bin/starship init bash)"
59+
5960
clear
6061
figlet "Flow PHP"
6162
'';

src/lib/parquet/src/Flow/Parquet/Data/Converter/Int32DateConverter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ private function dateTimeToNumberOfDays(\DateTime|\DateTimeImmutable $date) : in
3838
$epoch = new \DateTimeImmutable('1970-01-01 00:00:00 UTC');
3939
$interval = $epoch->diff($date->setTime(0, 0, 0, 0));
4040

41-
return (int) $interval->format('%a');
41+
return $interval->invert ? -(int) $interval->format('%a') : (int) $interval->format('%a');
4242
}
4343

4444
private function numberOfDaysToDateTime(int $data) : \DateTimeImmutable
4545
{
46-
return (new \DateTimeImmutable('1970-01-01 00:00:00 UTC'))->add(new \DateInterval('P' . $data . 'D'));
46+
$interval = new \DateInterval('P' . \abs($data) . 'D');
47+
48+
if ($data < 0) {
49+
$interval->invert = 1;
50+
}
51+
52+
return (new \DateTimeImmutable('1970-01-01 00:00:00 UTC'))->add($interval);
4753
}
4854
}

src/lib/parquet/tests/Flow/Parquet/Tests/Integration/IO/SimpleTypesWritingTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ public function test_writing_date_column() : void
9494
\unlink($path);
9595
}
9696

97+
public function test_writing_date_column_before_1970() : void
98+
{
99+
$path = __DIR__ . '/var/test-writer-parquet-test-' . generate_random_string() . '.parquet';
100+
101+
$writer = new Writer();
102+
$schema = Schema::with(FlatColumn::date('date'));
103+
104+
$faker = Factory::create();
105+
106+
$inputData = \array_merge(...\array_map(static fn (int $i) : array => [
107+
[
108+
'date' => \DateTimeImmutable::createFromMutable($faker->dateTimeBetween('1930-01-01', '1969-01-01'))->setTime(0, 0, 0, 0),
109+
],
110+
], \range(1, 100)));
111+
112+
$writer->write($path, $schema, $inputData);
113+
114+
self::assertEquals(
115+
$inputData,
116+
\iterator_to_array((new Reader())->read($path)->values())
117+
);
118+
119+
self::assertTrue(\file_exists($path));
120+
\unlink($path);
121+
}
122+
97123
public function test_writing_date_nullable_column() : void
98124
{
99125
$path = __DIR__ . '/var/test-writer-parquet-test-' . generate_random_string() . '.parquet';
@@ -573,6 +599,31 @@ public function test_writing_timestamp_column() : void
573599
\unlink($path);
574600
}
575601

602+
public function test_writing_timestamp_column_for_years_before_1970() : void
603+
{
604+
$path = __DIR__ . '/var/test-writer-parquet-test-' . generate_random_string() . '.parquet';
605+
606+
$writer = new Writer();
607+
$schema = Schema::with(FlatColumn::dateTime('dateTime'));
608+
609+
$faker = Factory::create();
610+
611+
$inputData = \array_merge(...\array_map(static fn (int $i) : array => [
612+
[
613+
'dateTime' => $faker->dateTimeBetween('1930-01-01', '1969-01-01'),
614+
],
615+
], \range(1, 100)));
616+
617+
$writer->write($path, $schema, $inputData);
618+
619+
self::assertEquals(
620+
$inputData,
621+
\iterator_to_array((new Reader())->read($path)->values())
622+
);
623+
self::assertTrue(\file_exists($path));
624+
\unlink($path);
625+
}
626+
576627
public function test_writing_timestamp_nullable_column() : void
577628
{
578629
$path = __DIR__ . '/var/test-writer-parquet-test-' . generate_random_string() . '.parquet';

0 commit comments

Comments
 (0)