Skip to content
Merged
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 @@ -127,6 +127,19 @@ public function test_an_empty_line_is_skipped(): void
static::assertSame([1, 4], $rows->reduceToArray('a'));
}

public function test_a_space_and_a_t_separated_naive_datetime_read_as_the_same_value(): void
{
$rows = ExtractedRows::of(from_csv(
'memory://source.csv',
filesystem: CSVFixtureContext::memory("at\n2023-11-14 22:13:20\n2023-11-14T22:13:20\n"),
)->withSchema(schema(datetime_schema('at'))));

static::assertEquals(
[new DateTimeImmutable('2023-11-14 22:13:20 UTC'), new DateTimeImmutable('2023-11-14 22:13:20 UTC')],
$rows->reduceToArray('at'),
);
}

public function test_a_value_that_does_not_cast_to_the_declared_type_is_a_corrupted_record(): void
{
try {
Expand Down
20 changes: 20 additions & 0 deletions src/core/etl/src/Flow/ETL/Functions/Join/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,29 @@ public function keys(): ?JoinKeys
: new JoinKeys(
array_map(static fn(array $pair): string => $pair[0], array_values($pairs)),
array_map(static fn(array $pair): string => $pair[1], array_values($pairs)),
$this->onlyEqual($this->comparison),
);
}

private function onlyEqual(Condition $comparison): bool
{
if ($comparison instanceof Equal) {
return true;
}

if (!$comparison instanceof All) {
return false;
}

foreach ($comparison->comparisons() as $child) {
if (!$this->onlyEqual($child)) {
return false;
}
}

return true;
}

/**
* @return list<array{Reference, Reference}>
*/
Expand Down
36 changes: 11 additions & 25 deletions src/core/etl/src/Flow/ETL/Kernels/Bucketing/HashBucketing.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
use Flow\ETL\Foundation\Exception\InvalidArgumentException;
use Flow\ETL\Foundation\RandomValueGenerator;
use Flow\ETL\Storage\RowsStorage;
use Flow\ETL\Values\Column\Backend;
use Flow\ETL\Values\Grouping\KeyEncoder;
use Flow\ETL\Values\Rows;
use Generator;

use function array_merge;
use function count;
use function hash;
use function hexdec;
use function sort;
use function sprintf;
use function substr;

final class HashBucketing
{
Expand All @@ -41,36 +36,29 @@ public function __construct(
}

/**
* Distinct keys only: KeyGrouping groups a batch's rows by key first, so xxh128 runs once per distinct key.
*
* @param Generator<Rows> $rows
* @param null|non-empty-string $runId null = random; given = the ids are "{namespace}-{runId}-{index}", the same
* in every process
*
* @return Generator<Bucket>
*/
public function bucketize(Generator $rows, RowsStorage $storage, ?string $runId = null): Generator
public function bucketize(Generator $rows, RowsStorage $storage, Backend $backend, ?string $runId = null): Generator
{
$runId ??= $this->random->string(16);

/** @var array<int, int> $totals */
$totals = [];

foreach ($rows as $batch) {
/** @var array<int, list<list<int>>> $groups */
$groups = [];

foreach (KeyGrouping::indices($this->keys->of($batch)) as $key => $rowsOfKey) {
$groups[$this->indexOf($key)][] = $rowsOfKey;
}

// a bucket is a subset of a batch that already passed the gate, under the same schema
foreach ($groups as $index => $parts) {
$indices = array_merge(...$parts);
// in row order, as the batch held them
sort($indices);
$storage->append(self::id($this->namespace, $runId, $index), $batch->gather($indices));
$totals[$index] = ($totals[$index] ?? 0) + count($indices);
foreach ($backend->partition(
$batch,
$this->keys->columns($batch, $backend),
$this->bucketsCount,
$this->hashOffset,
) as $index => $part) {
$storage->append(self::id($this->namespace, $runId, $index), $part);
$totals[$index] = ($totals[$index] ?? 0) + $part->count();
}
}

Expand All @@ -86,8 +74,6 @@ public static function id(string $namespace, string $runId, int $index): string

public function indexOf(string $key): int
{
return $this->bucketsCount === 1
? 0
: (int) hexdec(substr(hash('xxh128', $key), $this->hashOffset, 8)) % $this->bucketsCount;
return $this->bucketsCount === 1 ? 0 : KeyEncoder::bucketOf($key, $this->bucketsCount, $this->hashOffset);
}
}
12 changes: 6 additions & 6 deletions src/core/etl/src/Flow/ETL/Kernels/HashJoin/HashTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class HashTable
{
/**
* @var array<string, list<int>>
* @var array<int, list<int>>
*/
private array $buckets = [];

Expand All @@ -25,9 +25,9 @@ public function __construct(
private readonly bool $trackUnmatched = false,
) {}

public function add(string $key, int $index): void
public function add(int $group, int $index): void
{
$this->buckets[$key][] = $index;
$this->buckets[$group][] = $index;
$this->count++;

if ($this->trackUnmatched) {
Expand All @@ -36,11 +36,11 @@ public function add(string $key, int $index): void
}

/**
* @return list<int> the build-side rows under $key, in the order they were added
* @return list<int> the build-side rows of $group, in the order they were added
*/
public function candidatesFor(string $key): array
public function candidatesFor(int $group): array
{
return array_key_exists($key, $this->buckets) ? $this->buckets[$key] : [];
return array_key_exists($group, $this->buckets) ? $this->buckets[$group] : [];
}

public function count(): int
Expand Down
68 changes: 68 additions & 0 deletions src/core/etl/src/Flow/ETL/Kernels/HashJoin/JoinKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,69 @@

namespace Flow\ETL\Kernels\HashJoin;

use Flow\ETL\Values\Column\ComparableValues;
use Flow\ETL\Values\Column\ValueEquality;
use Flow\ETL\Values\Grouping\KeyEncoder;
use Flow\ETL\Values\Rows;
use Flow\ETL\Values\Schema;
use Flow\ETL\Values\Schema\Definitions;
use Flow\Types\Type;

use function array_key_exists;

final readonly class JoinKeys
{
/**
* @param list<string> $left
* @param list<string> $right column names, pairwise with $left
* @param bool $exact the condition is only Equal comparisons, so rows whose keys are equal and not null meet it
*/
public function __construct(
public array $left,
public array $right,
public bool $exact = false,
) {}

/**
* @param array<int, Type<mixed>> $types
*
* @return Rows the left key columns of $rows, each cast to its type in $types, named "0", "1", … by position
*/
public function leftKeys(Rows $rows, array $types): Rows
{
return $this->keysOf($this->left, $rows, $types);
}

/**
* @param array<int, Type<mixed>> $types
*
* @return Rows the right key columns of $rows, each cast to its type in $types, named "0", "1", … by position
*/
public function rightKeys(Rows $rows, array $types): Rows
{
return $this->keysOf($this->right, $rows, $types);
}

/**
* The keys alone decide the condition for $keys: exact, no null, every column type compared by physical.
*/
public function decides(Rows $keys): bool
{
if (!$this->exact) {
return false;
}

$comparable = new ComparableValues();

foreach ($keys->columns() as $column) {
if ($column->nullCount() !== 0 || !$comparable->equalByPhysical($column->type())) {
return false;
}
}

return true;
}

/**
* One key encoder per side, both keying in types().
*
Expand Down Expand Up @@ -53,4 +100,25 @@ public function types(?Schema $left, ?Schema $right): array

return $types;
}

/**
* @param list<string> $names
* @param array<int, Type<mixed>> $types
*/
private function keysOf(array $names, Rows $rows, array $types): Rows
{
$equality = new ValueEquality();
$definitions = [];
$columns = [];

foreach ($names as $position => $name) {
$column = array_key_exists($position, $types)
? $equality->cast($rows->column($name), $types[$position], $name)
: $rows->column($name);
$definitions[] = Definitions::of((string) $position, $column->type(), $column->nullCount() > 0);
$columns[$position] = $column;
}

return Rows::fromColumns(new Schema(...$definitions), $columns, $rows->count());
}
}
Loading
Loading