ClickHouse-Encoder

 view release on metacpan or  search on metacpan

t/regressions.t  view on Meta::CPAN

{
    my $enc = ClickHouse::Encoder->new(columns => [['a', 'UInt32'], ['b', 'String']]);
    eval { $enc->encode([[1, 'ok'], [2]]) };
    like($@, qr/Row .* columns/, 'short row croaks');

    eval { $enc->encode([[1, 'ok', 'extra']]) };
    like($@, qr/Row .* columns/, 'long row croaks');
}

# Bug: Enum value out-of-range was silently truncated.
{
    eval { ClickHouse::Encoder->new(columns => [['v', "Enum8('big' = 300)"]]) };
    like($@, qr/out of range/, 'Enum8 value 300 rejected at parse time');

    eval { ClickHouse::Encoder->new(columns => [['v', "Enum16('huge' = 100000)"]]) };
    like($@, qr/out of range/, 'Enum16 value 100000 rejected at parse time');
}

# Enum value out-of-range at row-encode time (integer input).
{
    my $enc = ClickHouse::Encoder->new(
        columns => [['v', "Enum8('a' = 1, 'b' = 2)"]],
    );
    eval { $enc->encode([[200]]) };
    like($@, qr/out of range/, 'Enum8 integer value 200 rejected at encode time');
}

# Bug: empty enum name silently accepted.
{
    eval { ClickHouse::Encoder->new(columns => [['v', "Enum8('' = 1)"]]) };
    like($@, qr/empty name/, 'empty enum name rejected');
}
{
    # Enum names with backslash-escaped quotes (as CH emits in describe table)
    # must be unescaped in storage so user lookup matches.
    my $enc = ClickHouse::Encoder->new(
        columns => [['v', q{Enum8('it\'s' = 1, 'plain' = 2)}]],
    );
    my $bin = $enc->encode([["it's"], ['plain']]);
    is(ord(substr($bin, length($bin)-2, 1)), 1,
       q{Enum8 with \' in name: lookup matches the apostrophe form});
    is(ord(substr($bin, length($bin)-1, 1)), 2,
       'Enum8 plain entry alongside escaped one');
}

# Bug: Nullable(Nullable(T)) silently accepted (CH would reject).
{
    eval { ClickHouse::Encoder->new(columns => [['v', 'Nullable(Nullable(Int32))']]) };
    like($@, qr/Nullable\(Nullable/, 'Nullable(Nullable()) rejected');
}

# Bug: FixedString(0) silently accepted.
{
    eval { ClickHouse::Encoder->new(columns => [['v', 'FixedString(0)']]) };
    like($@, qr/FixedString needs positive length|positive/, 'FixedString(0) rejected');
}

# IEEE float special values must be preserved (LE byte order verification).
{
    my $enc = ClickHouse::Encoder->new(columns => [['v', 'Float32']]);
    my $bin = $enc->encode([['Inf'+0], [-'Inf'+0], ['NaN'+0]]);
    my $off = length($bin) - 12;
    my @bytes = map { unpack('H8', substr($bin, $off + $_*4, 4)) } 0..2;
    is($bytes[0], '0000807f', '+Inf as Float32 LE bytes');
    is($bytes[1], '000080ff', '-Inf as Float32 LE bytes');
    # IEEE 754 doesn't pin down NaN's sign bit; macOS / glibc / different
    # libc strtod implementations give either sign. Accept both.
    like($bytes[2], qr/^[0-9a-f]{4}c[0-9a-f][7f]f$/, 'NaN as Float32 LE bytes (quiet bit set, either sign)');
}

# UTF-8 byte boundary for FixedString — not codepoint truncation.
{
    use utf8;
    my $enc = ClickHouse::Encoder->new(columns => [['v', 'FixedString(6)']]);
    my $bin = $enc->encode([['Привет']]);  # 12 UTF-8 bytes
    my $tail = substr($bin, length($bin) - 6);
    is(length($tail), 6, 'FixedString truncates by bytes, not codepoints');
    # The first 6 bytes of "Привет" are bytes for "Прив"-prefix (UTF-8)
    is($tail, "\xd0\x9f\xd1\x80\xd0\xb8", 'UTF-8 bytes preserved');
}

# Bug: Variant sub-columns and discriminators must be emitted in
# alphabetical order of variant type names, not declaration order.
# Real ClickHouse rejects declaration-ordered buffers when the
# declaration is not alphabetical (TOO_LARGE_ARRAY_SIZE / "is empty,
# but expected to be read N values" depending on the inner types).
{
    # Declaration is not alphabetical: UInt32 before String. Wire order
    # is alphabetical (String=0, UInt32=1). The user's declaration idx
    # 0 (UInt32) must be emitted as wire byte 1, and sub-columns must
    # come out String first then UInt32.
    my $enc = ClickHouse::Encoder->new(
        columns => [['v', 'Variant(UInt32, String)']],
    );
    my $bin = $enc->encode([[[0, 42]], [[1, 'hi']]]);
    my $col_off = 2 + 1 + 1 + 1 + length('Variant(UInt32, String)');
    my $disc_off = $col_off + 8;
    my @disc = (ord(substr($bin, $disc_off, 1)),
                ord(substr($bin, $disc_off + 1, 1)));
    is($disc[0], 1, 'wire discriminator for declared UInt32 is alphabetical 1');
    is($disc[1], 0, 'wire discriminator for declared String is alphabetical 0');
    my $sc_off = $disc_off + 2;
    is(ord(substr($bin, $sc_off, 1)), 2, 'String sub-col first (varint length 2)');
    is(substr($bin, $sc_off + 1, 2), 'hi', 'String sub-col content');
    is(unpack('V', substr($bin, $sc_off + 3, 4)), 42,
       'UInt32 sub-col second, value 42');
}

{
    # LC(String) (non-Nullable) with undef previously emitted
    # "Use of uninitialized value" via SvPV. The encoder now coerces
    # undef to "" silently, matching the plain-String path.
    use warnings FATAL => 'uninitialized';
    my $enc = ClickHouse::Encoder->new(columns => [['v', 'LowCardinality(String)']]);
    my $bin = eval { $enc->encode([[undef], [""], ["a"]]) };
    is($@, '', 'LC(String) undef does not emit uninitialized warning');
    ok(defined $bin && length $bin > 0, 'LC(String) undef produces bytes');
}

done_testing();



( run in 1.103 second using v1.01-cache-2.11-cpan-60884a8df76 )