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

Simplify making schema definitions nullable #1110

Merged
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
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Pipeline/HashJoinPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function process(FlowContext $context) : \Generator

if ($this->join === Join::left) {
foreach ($rightSchema->definitions() as $rightEntryDefinition) {
$rightEntries[] = $context->entryFactory()->create($rightEntryDefinition->entry()->name(), null, $rightEntryDefinition->nullable());
$rightEntries[] = $context->entryFactory()->create($rightEntryDefinition->entry()->name(), null, $rightEntryDefinition->makeNullable());
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public function process(FlowContext $context) : \Generator

if ($this->join === Join::right) {
foreach ($leftSchema->definitions() as $leftEntryDefinition) {
$leftEntries[] = $context->entryFactory()->create($leftEntryDefinition->entry()->name(), null, $leftEntryDefinition->nullable());
$leftEntries[] = $context->entryFactory()->create($leftEntryDefinition->entry()->name(), null, $leftEntryDefinition->makeNullable());
}

foreach ($hashTable->unmatchedRows() as $unmatchedRow) {
Expand Down
43 changes: 27 additions & 16 deletions src/core/etl/src/Flow/ETL/Row/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ public function keep(string|Reference ...$entries) : self
return $this;
}

/**
* Makes all schema definitions nullable.
*/
public function makeNullable() : self
{
$definitions = [];

foreach ($this->definitions as $definition) {
if (!$definition->isNullable()) {
$definitions[] = $definition->makeNullable();
} else {
$definitions[] = $definition;
}
}

$this->setDefinitions(...$definitions);

return $this;
}

public function matches(self $schema, SchemaMatcher $matcher = new StrictSchemaMatcher()) : bool
{
return $matcher->match($this, $schema);
Expand All @@ -155,21 +175,21 @@ public function merge(self $schema) : self

foreach ($schema->definitions as $entry => $definition) {
if (!\array_key_exists($definition->entry()->name(), $newDefinitions)) {
$newDefinitions[$entry] = $definition->nullable();
$newDefinitions[$entry] = $definition->makeNullable();
} elseif (!$newDefinitions[$entry]->isEqual($definition)) {
$newDefinitions[$entry] = $newDefinitions[$entry]->merge($definition);
}
}

foreach ($schema->definitions as $entry => $definition) {
if (!\array_key_exists($definition->entry()->name(), $newDefinitions)) {
$newDefinitions[$entry] = $definition->nullable();
$newDefinitions[$entry] = $definition->makeNullable();
}
}

foreach ($newDefinitions as $entry => $definition) {
if (!\array_key_exists($definition->entry()->name(), $schema->definitions)) {
$newDefinitions[$entry] = $definition->nullable();
$newDefinitions[$entry] = $definition->makeNullable();
}
}

Expand All @@ -189,21 +209,12 @@ public function normalize() : array
return $definitions;
}

/**
* @deprecated use makeNullable instead
*/
public function nullable() : self
{
$definitions = [];

foreach ($this->definitions as $definition) {
if (!$definition->isNullable()) {
$definitions[] = $definition->nullable();
} else {
$definitions[] = $definition;
}
}

$this->setDefinitions(...$definitions);

return $this;
return $this->makeNullable();
}

public function remove(string|Reference ...$entries) : self
Expand Down
10 changes: 9 additions & 1 deletion src/core/etl/src/Flow/ETL/Row/Schema/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public function isNullable() : bool
return $this->type->nullable();
}

public function makeNullable(bool $nullable = true) : self
{
return new self($this->ref, $this->entryClass, $this->type->makeNullable($nullable), $this->metadata);
}

public function matches(Entry $entry) : bool
{
if ($this->isNullable() && $entry->is($this->ref)) {
Expand Down Expand Up @@ -317,9 +322,12 @@ public function normalize() : array
];
}

/**
* @deprecated Use makeNullable() instead
*/
public function nullable() : self
{
return new self($this->ref, $this->entryClass, $this->type->makeNullable(true), $this->metadata);
return $this->makeNullable();
}

public function rename(string $newName) : self
Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Rows.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public function joinLeft(self $right, Expression $expression) : self
$entries = [];

foreach ($rightSchema->definitions() as $definition) {
$entries[] = $entryFactory->create($definition->entry()->name(), null, $definition->nullable());
$entries[] = $entryFactory->create($definition->entry()->name(), null, $definition->makeNullable());
}

$joinedRow = $leftRow->merge(row(...$entries), $expression->prefix());
Expand Down Expand Up @@ -457,7 +457,7 @@ public function joinRight(self $right, Expression $expression) : self
$entries = [];

foreach ($leftSchema->definitions() as $definition) {
$entries[] = $entryFactory->create($definition->entry()->name(), null, $definition->nullable());
$entries[] = $entryFactory->create($definition->entry()->name(), null, $definition->makeNullable());
}

$joined[] = row(...$entries)->merge($rightRow, $expression->prefix());
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function test_making_whole_schema_nullable() : void
Schema\Definition::integer('id', $nullable = true),
Schema\Definition::string('name', $nullable = true)
),
$schema->nullable()
$schema->makeNullable()
);
}

Expand Down
Loading