EV-ClickHouse

 view release on metacpan or  search on metacpan

t/26_json.t  view on Meta::CPAN


# 5-6: blessed Bool + Float64 + sparse paths.
{
    require JSON::PP;
    my ($ch, $rows, $err);
    my $table = '_ev_ch_json3_' . $$;
    $ch = EV::ClickHouse->new(
        host => $host, port => $nat_port, protocol => 'native',
        settings => { allow_experimental_json_type => 1 },
        on_connect => sub {
            $ch->query("create temporary table $table (j JSON) ENGINE = Memory", sub {
                $ch->insert($table, [
                    [{ active => JSON::PP::true(),  ratio => 1.5  }],
                    [{ active => JSON::PP::false(), ratio => 0.25, label => 'x' }],
                ], sub {
                    (undef, $err) = @_;
                    return EV::break if $err;
                    $ch->query("select count() from $table", sub {
                        ($rows, $err) = @_; EV::break;
                    });
                });
            });
        },
    );
    run_with_timeout(15);
    ok(!$err, "JSON Bool + Float64 + sparse paths: no error") or diag "err=$err";
    is($rows && @$rows ? $rows->[0][0] : undef, 2,
       "JSON sparse rows: count() returns 2");
    $ch->finish if $ch->is_connected;
}

# 7-9: typed-path JSON column round-trip — JSON(name String, age UInt32)
# pins schema; encoder writes typed paths as regular columns, decoder reads
# them back into the per-row hash under the declared names.
{
    my ($ch, $rows, $err);
    my $table = '_ev_ch_jsont_' . $$;
    $ch = EV::ClickHouse->new(
        host => $host, port => $nat_port, protocol => 'native',
        settings => { allow_experimental_json_type => 1 },
        on_connect => sub {
            $ch->query("create temporary table $table "
                     . "(j JSON(name String, age UInt32)) "
                     . "ENGINE = Memory", sub {
                $ch->insert($table, [
                    [{ name => 'alice', age => 30, extra => 'dyn1' }],
                    [{ name => 'bob',   age => 25 }],
                ], sub {
                    (undef, $err) = @_;
                    return EV::break if $err;
                    $ch->query("select j.name, j.age, j FROM $table order by j.name", sub {
                        ($rows, $err) = @_; EV::break;
                    });
                });
            });
        },
    );
    run_with_timeout(15);
    ok(!$err, "typed-path JSON: no error") or diag "err=$err";
    is($rows && @$rows ? $rows->[0][0] : undef, 'alice',
       "typed path j.name decoded as String column");
    is($rows && @$rows ? $rows->[0][1] : undef, 30,
       "typed path j.age decoded as UInt32 column");
    $ch->finish if $ch->is_connected;
}



( run in 0.303 second using v1.01-cache-2.11-cpan-f4a522933cf )