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
1 change: 1 addition & 0 deletions documentation/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ $df->filter(ref('a')->notSame(lit(5)));
| `$df->mode(execution_lenient())` | removed - `optional($function)` per function |
| `$df->mode(execution_strict())` | removed - strict is the only behaviour |
| `coalesce($a, $b)` skipped a branch that threw | rethrows - `coalesce(optional($a), $b)` |
| `cast($v, type_integer())` over `null` returned `null` | throws - `cast($v, type_optional(type_integer()))` |
| `ref('l')->onEach($fn)` set an element whose `$fn` threw to `null` | throws - `ref('l')->onEach(optional($fn))` |
| `ref('s')->indexOf('a')` / `->indexOfLast('a')` over `null` - `false` | throws - `optional(ref('s')->indexOf('a'))` |
| `DataFrame::mode()` | removed |
Expand Down
3 changes: 2 additions & 1 deletion src/core/etl/src/Flow/ETL/Function/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use function Flow\ETL\DSL\definition_from_type;
use function Flow\ETL\DSL\lit;
use function Flow\Types\DSL\type_is_nullable;
use function sprintf;

final class Cast implements ScalarFunction
Expand Down Expand Up @@ -82,7 +83,7 @@ public function eval(Row $row, FlowContext $context): mixed
{
$value = (new Parameter($this->value))->eval($row, $context);

if (null === $value) {
if (null === $value && !type_is_nullable($this->type)) {
throw new InvalidArgumentException('Cast function requires non-null value');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,44 @@
use Flow\ETL\Tests\FlowTestCase;
use Flow\Types\Value\Json;

use function Flow\ETL\DSL\array_get;
use function Flow\ETL\DSL\cast;
use function Flow\ETL\DSL\df;
use function Flow\ETL\DSL\from_array;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\schema;
use function Flow\ETL\DSL\structure_schema;
use function Flow\ETL\DSL\to_memory;
use function Flow\Types\DSL\structure_element;
use function Flow\Types\DSL\type_datetime;
use function Flow\Types\DSL\type_integer;
use function Flow\Types\DSL\type_list;
use function Flow\Types\DSL\type_optional;
use function Flow\Types\DSL\type_string;
use function Flow\Types\DSL\type_structure;

final class CastTest extends FlowTestCase
{
public function test_cast_of_an_absent_nullsafe_path_to_an_optional_target_is_null(): void
{
$rows = df()
->read(from_array([
['record' => ['deleteTime' => '2026-09-01T00:00:00Z']],
['record' => []],
], schema(structure_schema('record', type_structure([
'deleteTime' => structure_element('deleteTime', type_string(), optional: true),
])))))
->withEntry('delete_time', cast(array_get(ref('record'), '?deleteTime'), type_optional(type_datetime())))
->drop('record')
->fetch();

static::assertEquals(
[['delete_time' => new DateTimeImmutable('2026-09-01T00:00:00Z')], ['delete_time' => null]],
$rows->toArray(),
);
static::assertTrue($rows->schema()->get('delete_time')->isNullable());
}

public function test_cast(): void
{
df()
Expand Down
18 changes: 18 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Function/CastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use function Flow\ETL\DSL\flow_context;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\row;
use function Flow\Types\DSL\type_datetime;
use function Flow\Types\DSL\type_integer;
use function Flow\Types\DSL\type_optional;
use function is_object;

final class CastTest extends FlowTestCase
Expand All @@ -29,6 +32,21 @@ public function test_cast_of_a_null_value_throws(): void
cast(ref('value'), 'int')->eval(row(['value' => null]), flow_context());
}

public function test_cast_of_a_null_value_to_an_optional_target_is_null(): void
{
static::assertNull(cast(ref('value'), type_optional(type_integer()))->eval(row([
'value' => null,
]), flow_context()));
}

public function test_casting_an_unconvertible_value_to_an_optional_target_throws(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cast function failed: Can\'t cast "string" into "datetime" type');

cast(ref('value'), type_optional(type_datetime()))->eval(row(['value' => 'abc']), flow_context());
}

public function test_constructor_rejects_a_non_representable_target(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/types/src/Flow/Types/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use function Flow\Types\DSL\type_null;
use function Flow\Types\DSL\type_numeric_string;
use function Flow\Types\DSL\type_object;
use function Flow\Types\DSL\type_optional;
use function Flow\Types\DSL\type_positive_integer;
use function Flow\Types\DSL\type_resource;
use function Flow\Types\DSL\type_scalar;
Expand All @@ -44,6 +45,8 @@
use function Flow\Types\DSL\type_xml;
use function Flow\Types\DSL\type_xml_element;
use function mb_strtolower;
use function str_starts_with;
use function substr;

final class TypeFactory
{
Expand Down Expand Up @@ -109,6 +112,10 @@ public static function fromArray(array $data): Type
*/
public static function fromString(string $name): Type
{
if (str_starts_with($name, '?')) {
return type_optional(self::fromString(substr($name, 1)));
}

return match (mb_strtolower($name)) {
'int', 'integer' => self::fromArray(['type' => 'integer', 'scalar_type' => 'integer']),
'float', 'double', 'real' => self::fromArray(['type' => 'float', 'scalar_type' => 'float']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use function Flow\Types\DSL\type_list;
use function Flow\Types\DSL\type_map;
use function Flow\Types\DSL\type_null;
use function Flow\Types\DSL\type_optional;
use function Flow\Types\DSL\type_resource;
use function Flow\Types\DSL\type_string;
use function Flow\Types\DSL\type_structure;
Expand Down Expand Up @@ -81,6 +82,15 @@ public function test_double_and_real_resolve_to_float(string $alias): void
static::assertEquals(type_float(), TypeFactory::fromString($alias));
}

public function test_a_question_mark_prefix_resolves_to_an_optional_type(): void
{
static::assertEquals(type_optional(type_integer()), TypeFactory::fromString('?int'));
static::assertEquals(
type_optional(type_datetime()),
TypeFactory::fromString(type_optional(type_datetime())->toString()),
);
}

public function test_normalizing_and_creating_enum_type(): void
{
$enum = type_enum(SomeEnum::class);
Expand Down