From 75fd1d0bb0acee0f22d1d6b35c0fd41cbc4b6589 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Thu, 4 Jul 2024 19:28:00 +0200 Subject: [PATCH] Simplify making schema definitions nullable --- .../Flow/ETL/Pipeline/HashJoinPipeline.php | 4 +- src/core/etl/src/Flow/ETL/Row/Schema.php | 43 ++++++++++++------- .../src/Flow/ETL/Row/Schema/Definition.php | 10 ++++- src/core/etl/src/Flow/ETL/Rows.php | 4 +- .../Flow/ETL/Tests/Unit/Row/SchemaTest.php | 2 +- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/core/etl/src/Flow/ETL/Pipeline/HashJoinPipeline.php b/src/core/etl/src/Flow/ETL/Pipeline/HashJoinPipeline.php index 061510b05..e6c2a9c6b 100644 --- a/src/core/etl/src/Flow/ETL/Pipeline/HashJoinPipeline.php +++ b/src/core/etl/src/Flow/ETL/Pipeline/HashJoinPipeline.php @@ -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()); } } @@ -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) { diff --git a/src/core/etl/src/Flow/ETL/Row/Schema.php b/src/core/etl/src/Flow/ETL/Row/Schema.php index 6854a05b3..f8a793970 100644 --- a/src/core/etl/src/Flow/ETL/Row/Schema.php +++ b/src/core/etl/src/Flow/ETL/Row/Schema.php @@ -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); @@ -155,7 +175,7 @@ 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); } @@ -163,13 +183,13 @@ 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(); } } foreach ($newDefinitions as $entry => $definition) { if (!\array_key_exists($definition->entry()->name(), $schema->definitions)) { - $newDefinitions[$entry] = $definition->nullable(); + $newDefinitions[$entry] = $definition->makeNullable(); } } @@ -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 diff --git a/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php b/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php index 5fdcee336..3462e8031 100644 --- a/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php +++ b/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php @@ -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)) { @@ -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 diff --git a/src/core/etl/src/Flow/ETL/Rows.php b/src/core/etl/src/Flow/ETL/Rows.php index 91422f321..5ff8fd892 100644 --- a/src/core/etl/src/Flow/ETL/Rows.php +++ b/src/core/etl/src/Flow/ETL/Rows.php @@ -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()); @@ -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()); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php index 4f684760b..2987ba17f 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php @@ -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() ); }