Skip to content
Search
Examples

Transformations

Description

when() replaces nulls with 0. Without else, a row that does not match gets null, so else: ref('value') keeps the value it had.

Documentation

Transformations are expressions over a row, evaluated column by column.

Code

<?php

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, int_schema, lit, ref, schema, to_output, when};

require __DIR__ . '/vendor/autoload.php';

data_frame()
    ->read(from_array(
        [
            ['id' => 1, 'value' => 1],
            ['id' => 2, 'value' => 1],
            ['id' => 3, 'value' => null],
            ['id' => 4, 'value' => 1],
            ['id' => 5, 'value' => null],
        ],
        schema(int_schema('id'), int_schema('value', nullable: true)),
    ))
    ->withColumn(
        'value',
        when(ref('value')->isNull(), then: lit(0), else: ref('value'))
    )
    ->write(to_output(truncate: false))
    ->run();
Contributors

Built in the open.

Join us on GitHub
scroll back to top