ClickHouse-Encoder
view release on metacpan or search on metacpan
t/rowbinary.t view on Meta::CPAN
columns => [['m', 'Array(Array(Int16))']]);
my @rows = ([ [[1,2],[3],[]] ], [ [] ]);
my $back = $enc->decode_row_binary($enc->encode_row_binary(\@rows));
is_deeply($back, \@rows, 'nested Array(Array(Int16)) round-trips');
}
# --- Nullable null vs zero are distinct --------------------------------
{
my $enc = ClickHouse::Encoder->new(columns => [['n', 'Nullable(Int32)']]);
my $back = $enc->decode_row_binary(
$enc->encode_row_binary([[undef], [0], [-1]]));
is_deeply($back, [[undef], [0], [-1]],
'Nullable distinguishes undef from 0');
}
# --- unsupported types croak with a clear message ----------------------
for my $bad (
['Map(String, Int32)', { k => 1 }],
['Tuple(Int32, String)', [1, 'x']],
['JSON', { a => 1 }],
['Point', [1.0, 2.0]],
) {
my ($type, $val) = @$bad;
my $enc = ClickHouse::Encoder->new(columns => [['c', $type]]);
local $@;
eval { $enc->encode_row_binary([[ $val ]]) };
like($@, qr/not supported/,
"encode_row_binary croaks on $type");
}
# --- input-shape validation --------------------------------------------
{
my $enc = ClickHouse::Encoder->new(columns => [['a','Int32'],['b','Int32']]);
local $@;
eval { $enc->encode_row_binary([[1]]) };
like($@, qr/row 0 has 1 values, expected 2/,
'encode_row_binary checks row arity');
eval { $enc->encode_row_binary('not-an-arrayref') };
like($@, qr/rows must be an arrayref/,
'encode_row_binary checks top-level arrayref');
}
# --- empty input -------------------------------------------------------
{
my $enc = ClickHouse::Encoder->new(columns => [['v', 'Int32']]);
is($enc->encode_row_binary([]), '', 'zero rows -> empty string');
is_deeply($enc->decode_row_binary(''), [], 'empty string -> zero rows');
}
# decode_row_binary needs an encoder instance for its column types;
# calling it as a class method has nothing to decode against.
{
local $@;
eval { ClickHouse::Encoder->decode_row_binary("\x01\x02") };
like($@, qr/must be called on an encoder instance/,
'decode_row_binary as class method croaks');
}
# A zero-column encoder would loop forever on any non-empty buffer
# (no per-column work to advance the cursor); guard explicitly.
{
my $enc = ClickHouse::Encoder->new(columns => []);
is_deeply($enc->decode_row_binary(''), [],
'zero columns + empty buffer -> no rows');
local $@;
eval { $enc->decode_row_binary("\x01") };
like($@, qr/no columns but/, 'zero columns + non-empty buffer croaks');
}
done_testing();
( run in 0.692 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )