ClickHouse-Encoder
view release on metacpan or search on metacpan
epoch = zone_wall_time(aTHX_ s, len, pos, (int64_t)days * 86400 + seconds, t);
/* DateTime is UInt32 on the wire (1970-01-01 .. 2106-02-07). Reject
* out-of-range epochs explicitly rather than silently wrapping. */
if (epoch < 0 || epoch > 0xFFFFFFFFLL)
croak("DateTime out of UInt32 range "
"(1970-01-01 .. 2106-02-07 06:28:15): %.*s",
(int)(len > 30 ? 30 : len), s);
return (uint32_t)epoch;
}
/* Multiply a Perl double by 10^precision, croak on Inf/NaN/overflow,
* return the rounded int64. Shared by the SvNOK and string-number
* branches of the T_DATETIME64 encode path. */
int64_t dt64_double_to_int64(pTHX_ double v, int precision) {
double d = v * decimal_pow10(precision);
/* Same bound as the T_DECIMAL64 path: 2^63 in double form. Strict
* inequality so 2^63 itself (which would overflow int64_t after
* llround) is rejected. */
if (!(isfinite(d) && d > -9.223372036854776e18 && d < 9.223372036854776e18))
croak("DateTime64 overflow (or non-finite value)");
return (int64_t)llround(d);
if (SvIsBOOL(val)) return JV_BOOL;
#endif
if (SvIOK(val) && !SvNOK(val))
return SvIsUV(val) && SvUV(val) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64;
if (SvNOK(val)) {
NV n = SvNV(val);
/* NV holding an exact integer value (e.g. `1.0`) collapses to
* Int64 - matches CH's JSONEachRow type-inference. Users who
* need `1.0` to survive as Float64 on the wire should put the
* value through a float-typed Variant or an explicit Float64
* column. NaN/Inf fall through to Float64 because the integer
* round-trip equality check fails for them. */
if (n >= (NV)INT64_MIN && n < (NV)9223372036854775808.0L
&& n == (NV)(int64_t)n)
return JV_INT64;
return JV_FLOAT64;
}
return JV_STRING;
}
/* Classify a (defined) JSON/Dynamic value SV. Dispatches arrayrefs to
lib/ClickHouse/Encoder.pm view on Meta::CPAN
=over 4
=item *
Integers: C<Int8>, C<Int16>, C<Int32>, C<Int64>, C<UInt8>, C<UInt16>,
C<UInt32>, C<UInt64>.
=item *
Floats: C<Float32>, C<Float64>, C<BFloat16> (CH 24.x; 2-byte truncated
Float32). C<Inf>, C<-Inf>, and C<NaN> are preserved.
=item *
Strings: C<String> (length-prefixed bytes), C<FixedString(N)> (N bytes,
null-padded). Both pass the SV's bytes through unchanged: a UTF-8 string
encodes its UTF-8 bytes, a binary blob encodes its bytes, and truncation
is by byte not codepoint.
=item *
lib/ClickHouse/Encoder.pm view on Meta::CPAN
Numeric SV with the integer flag set but not the float flag
(C<SvIOK && !SvNOK>) -> C<Int64>.
=item *
Numeric SV with the float flag set (C<SvNOK>): non-integer value ->
C<Float64>; a NV that happens to equal an integer (e.g. C<1.0>)
collapses to C<Int64> on the wire, mirroring CH's own C<JSONEachRow>
inference. Round-trip then decodes back as an integer; to keep an
integer-valued float as C<Float64> use a dedicated C<Float64> column
or a C<<< Variant(Float64, ...) >>>. C<NaN> and C<Inf> stay C<Float64>.
=item *
Blessed scalarref into C<JSON::PP::Boolean>, C<JSON::XS::Boolean>,
C<Types::Serialiser::Boolean>, C<Cpanel::JSON::XS::Boolean>, or
C<boolean> -> C<Bool>. Perl 5.36+ native booleans (C<SvIsBOOL>) also work.
=item *
Anything else stringy -> C<String>.
t/error_messages.t view on Meta::CPAN
my $enc = ClickHouse::Encoder->new(columns => [['v', 'DateTime']]);
croak_like(sub { $enc->encode([['2024-06-15 AB:CD:EF']]) },
qr/Invalid datetime string/, 'DateTime: non-digit time fields');
croak_like(sub { $enc->encode([['2024-06-15 12:34:5x']]) },
qr/Invalid datetime string/, 'DateTime: non-digit second');
my $enc64 = ClickHouse::Encoder->new(columns => [['v', 'DateTime64(3)']]);
croak_like(sub { $enc64->encode([['2024-06-15 9X:34:56.000']]) },
qr/Invalid datetime string/, 'DateTime64: non-digit hour');
# Inf / NaN passed as numeric float must croak, not silently produce
# a garbage int64 via implementation-defined llround() out-of-range
# behavior.
croak_like(sub { $enc64->encode([['Inf' + 0]]) },
qr/DateTime64 overflow/, 'DateTime64: Inf croaks');
croak_like(sub { $enc64->encode([['NaN' + 0]]) },
qr/DateTime64 overflow/, 'DateTime64: NaN croaks');
}
# Decimal256 float-path overflow at the 2^255 boundary: values in
# [2^255, 2^256) used to silently produce a buffer with the sign bit
# set, which CH would interpret as a large negative Int256.
{
my $enc = ClickHouse::Encoder->new(columns => [['v', 'Decimal256(0)']]);
croak_like(sub { $enc->encode([[1e77]]) },
qr/Decimal256 overflow/, 'Decimal256 (float path) > 2^255 croaks');
croak_like(sub { $enc->encode([[1e80]]) },
t/extended_types.t view on Meta::CPAN
is($bytes, 0, 'reset() discards buffered rows; finish flushes nothing');
$st->push_row([$_]) for 1..3;
$st->finish;
cmp_ok($bytes, '>', 0, 'streamer is reusable after reset()');
}
# BFloat16 (CH 24.x) -------------------------------------------------------
{
my $enc = ClickHouse::Encoder->new(columns => [['v', 'BFloat16']]);
my $bin = $enc->encode([[1.0], [0.5], [-1.0], ['NaN' + 0]]);
my $tail = substr($bin, length($bin) - 8);
is(unpack('H4', substr($tail, 0, 2)), '803f', 'BFloat16: 1.0 = 803f');
is(unpack('H4', substr($tail, 2, 2)), '003f', 'BFloat16: 0.5 = 003f');
is(unpack('H4', substr($tail, 4, 2)), '80bf', 'BFloat16: -1.0 = 80bf');
like(unpack('H4', substr($tail, 6, 2)), qr/^c0[7f]f$/i,
'BFloat16: NaN has quiet bit (sign-agnostic)');
my $enc2 = ClickHouse::Encoder->new(columns => [['v', 'Nullable(BFloat16)']]);
my $bin2 = $enc2->encode([[1.0], [undef]]);
is(substr($bin2, length($bin2) - 2), "\x00\x00",
'Nullable(BFloat16): null encodes as two zero bytes');
}
# validate_rows ------------------------------------------------------------
{
my $enc = ClickHouse::Encoder->new(columns => [['v', 'UInt32']]);
t/regressions.t view on Meta::CPAN
# 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)
( run in 1.536 second using v1.01-cache-2.11-cpan-60884a8df76 )