encode.c

filter: Statementstmt 95.5% 939 / 983 Branchbran 82.4% 719 / 872 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.3% 1658 / 1855 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« decimal.c ↑ Summary runtime.c »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <math.h>
10  
11 #ifdef _WIN32
12 # include <winsock2.h>
13 # include <ws2tcpip.h>
14 #else
15 # include <sys/socket.h>
16 # include <arpa/inet.h>
17 #endif
18  
19 #include "types.h"
20 #include "buffer.h"
21 #include "decimal.h"
22 #include "scalar_kind.h"
23 #include "datetime.h"
24 #include "json_kind.h"
25 #include "encode.h"
26  
27 /* Build a recursive NULL placeholder for `t`. Returns a mortal SV.
28 * Leaf types get a defined zero/empty SV (not &PL_sv_undef) so that the
29 * encoder doesn't warn about uninit values when filling padding bytes. */
30 68 static SV *make_null_placeholder(pTHX_ TypeInfo *t) {
31 68 ▶ if (t->code == T_ARRAY || t->code == T_MAP) {
Branch: 2 branches
BranchTrueFalseTotal
if (t->code == T_ARRAY || t->code == T_MAP) { 64 4 68
if (t->code == T_ARRAY || t->code == T_MAP) { 4 60 64
32 8 AV *empty = newAV();
33 8 return sv_2mortal(newRV_noinc((SV*)empty));
34 }
35 60 ▶ if (t->code == T_TUPLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->code == T_TUPLE) { 12 48 60
36 12 AV *tuple = newAV();
37 int i;
38 35 ▶ for (i = 0; i < t->tuple_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->tuple_len; i++) { 23 12 35
39 23 SV *child = make_null_placeholder(aTHX_ t->tuple[i]);
40 23 av_push(tuple, SvREFCNT_inc(child));
41 }
42 12 return sv_2mortal(newRV_noinc((SV*)tuple));
43 }
44 48 ▶ if (t->code == T_LOWCARDINALITY)
Branch: 1 branch
BranchTrueFalseTotal
if (t->code == T_LOWCARDINALITY) 0 48 48
45 0 return make_null_placeholder(aTHX_ t->inner);
46 48 ▶ if (t->code == T_VARIANT || t->code == T_JSON || t->code == T_DYNAMIC)
Branch: 3 branches
BranchTrueFalseTotal
if (t->code == T_VARIANT || t->code == T_JSON || t->code == T_DYNAMIC) 48 0 48
if (t->code == T_VARIANT || t->code == T_JSON || t->code == T_DYNAMIC) 48 0 48
if (t->code == T_VARIANT || t->code == T_JSON || t->code == T_DYNAMIC) 0 48 48
47 /* Variant's encoder treats undef as the NULL discriminator. */
48 0 return &PL_sv_undef;
49 48 ▶ if (t->code == T_ENUM8 || t->code == T_ENUM16) return &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
if (t->code == T_ENUM8 || t->code == T_ENUM16) return &PL_sv_undef; 45 3 48
if (t->code == T_ENUM8 || t->code == T_ENUM16) return &PL_sv_undef; 3 42 45
50 42 ▶ if (t->code == T_STRING || t->code == T_FIXEDSTRING)
Branch: 2 branches
BranchTrueFalseTotal
if (t->code == T_STRING || t->code == T_FIXEDSTRING) 28 14 42
if (t->code == T_STRING || t->code == T_FIXEDSTRING) 0 28 28
51 14 return sv_2mortal(newSVpvn("", 0));
52 28 ▶ if (t->code == T_UUID || t->code == T_IPV6)
Branch: 2 branches
BranchTrueFalseTotal
if (t->code == T_UUID || t->code == T_IPV6) 28 0 28
if (t->code == T_UUID || t->code == T_IPV6) 0 28 28
53 0 return sv_2mortal(newSVpvn("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16));
54 28 ▶ if (t->code == T_FLOAT32 || t->code == T_FLOAT64 || t->code == T_BFLOAT16)
Branch: 3 branches
BranchTrueFalseTotal
if (t->code == T_FLOAT32 || t->code == T_FLOAT64 || t->code == T_BFLOAT16) 28 0 28
if (t->code == T_FLOAT32 || t->code == T_FLOAT64 || t->code == T_BFLOAT16) 28 0 28
if (t->code == T_FLOAT32 || t->code == T_FLOAT64 || t->code == T_BFLOAT16) 0 28 28
55 0 return sv_2mortal(newSVnv(0.0));
56 /* numeric / date / decimal / bool / ipv4: a defined integer 0 is fine */
57 28 return sv_2mortal(newSViv(0));
58 }
59  
60 2088 static int enum_lookup_name(pTHX_ TypeInfo *t, const char *s, STRLEN slen, int16_t *out) {
61 2088 SV **psv = hv_fetch(t->enum_lookup, s, slen, 0);
62 2088 ▶ if (!psv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!psv) return 0; 34 2054 2088
63 2054 *out = (int16_t)SvIV(*psv);
64 2054 return 1;
65 }
66  
67 45739 int nullable_is_null(SV *value, TypeInfo *t) {
68 45739 ▶ return !SvOK(value) || (t->empty_as_null && SvPOK(value) && !SvCUR(value));
Branch: 4 branches
BranchTrueFalseTotal
return !SvOK(value) || (t->empty_as_null && SvPOK(value) && !SvCUR(value)); 35376 10363 45739
return !SvOK(value) || (t->empty_as_null && SvPOK(value) && !SvCUR(value)); 49 35327 35376
return !SvOK(value) || (t->empty_as_null && SvPOK(value) && !SvCUR(value)); 34 15 49
return !SvOK(value) || (t->empty_as_null && SvPOK(value) && !SvCUR(value)); 29 5 34
69 }
70  
71 2141 static int16_t enum_value(pTHX_ TypeInfo *t, SV *val) {
72 IV iv;
73 int16_t v;
74 2141 ▶ int bits = t->code == T_ENUM8 ? 8 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int bits = t->code == T_ENUM8 ? 8 : 16; 2084 57 2141
75 unsigned index;
76 2141 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 14 2127 2141
77 /* The type's default, as on the server: the smallest declared code. */
78 int k;
79 14 ▶ v = t->enum_count ? t->enum_entries[0].value : 0;
Branch: 1 branch
BranchTrueFalseTotal
v = t->enum_count ? t->enum_entries[0].value : 0; 14 0 14
80 32 ▶ for (k = 1; k < t->enum_count; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k < t->enum_count; k++) 18 14 32
81 18 ▶ if (t->enum_entries[k].value < v) v = t->enum_entries[k].value;
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_entries[k].value < v) v = t->enum_entries[k].value; 10 8 18
82 14 return v;
83 }
84 2127 ▶ if (SvPOK(val) || t->enum_has_integer_labels) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(val) || t->enum_has_integer_labels) { 55 2072 2127
if (SvPOK(val) || t->enum_has_integer_labels) { 16 39 55
85 STRLEN len;
86 2088 const char *s = SvPV(val, len);
87 2088 ▶ if (enum_lookup_name(aTHX_ t, s, len, &v)) return v;
Branch: 1 branch
BranchTrueFalseTotal
if (enum_lookup_name(aTHX_ t, s, len, &v)) return v; 2054 34 2088
88 34 ▶ if (looks_like_int_str(s, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_int_str(s, len)) { 30 4 34
89 30 ▶ STRLEN i = (s[0] == '+' || s[0] == '-') ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN i = (s[0] == '+' || s[0] == '-') ? 1 : 0; 26 4 30
STRLEN i = (s[0] == '+' || s[0] == '-') ? 1 : 0; 12 14 26
90 30 unsigned magnitude = 0, limit = 1U << (bits - 1);
91 30 int negative = s[0] == '-';
92 30 ▶ if (!negative) limit--;
Branch: 1 branch
BranchTrueFalseTotal
if (!negative) limit--; 18 12 30
93 77 ▶ for (; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < len; i++) { 55 22 77
94 55 unsigned digit = s[i] - '0';
95 55 ▶ if (magnitude > (limit - digit) / 10)
Branch: 1 branch
BranchTrueFalseTotal
if (magnitude > (limit - digit) / 10) 8 47 55
96 8 croak("Enum%d value %.*s out of range", bits, (int)len, s);
97 47 magnitude = magnitude * 10 + digit;
98 }
99 22 ▶ iv = negative ? -(IV)magnitude : (IV)magnitude;
Branch: 1 branch
BranchTrueFalseTotal
iv = negative ? -(IV)magnitude : (IV)magnitude; 10 12 22
100 22 goto validate;
101 }
102 4 ▶ if (!SvIOK(val) && !SvNOK(val))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvIOK(val) && !SvNOK(val)) 4 0 4
if (!SvIOK(val) && !SvNOK(val)) 4 0 4
103 4 croak("Unknown enum value: %.*s", (int)len, s);
104 }
105 39 ▶ if (SvIOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val)) { 39 0 39
106 39 ▶ if (SvIsUV(val) && SvUV(val) > (UV)((1 << (bits - 1)) - 1))
Branch: 2 branches
BranchTrueFalseTotal
if (SvIsUV(val) && SvUV(val) > (UV)((1 << (bits - 1)) - 1)) 0 39 39
if (SvIsUV(val) && SvUV(val) > (UV)((1 << (bits - 1)) - 1)) 0 0 0
107 0 croak("Enum%d value %" UVuf " out of range", bits, SvUV(val));
108 39 iv = SvIV(val);
109 } else {
110 0 NV nv = trunc(SvNV(val));
111 0 ▶ if (!isfinite(nv) || nv < -(1 << (bits - 1)) || nv > (1 << (bits - 1)) - 1)
Branch: 3 branches
BranchTrueFalseTotal
if (!isfinite(nv) || nv < -(1 << (bits - 1)) || nv > (1 << (bits - 1)) - 1) 0 0 0
if (!isfinite(nv) || nv < -(1 << (bits - 1)) || nv > (1 << (bits - 1)) - 1) 0 0 0
if (!isfinite(nv) || nv < -(1 << (bits - 1)) || nv > (1 << (bits - 1)) - 1) 0 0 0
112 0 croak("Enum%d value out of range", bits);
113 0 iv = (IV)nv;
114 }
115 39 ▶ if (iv < -(1 << (bits - 1)) || iv > (1 << (bits - 1)) - 1)
Branch: 2 branches
BranchTrueFalseTotal
if (iv < -(1 << (bits - 1)) || iv > (1 << (bits - 1)) - 1) 38 1 39
if (iv < -(1 << (bits - 1)) || iv > (1 << (bits - 1)) - 1) 3 35 38
116 4 croak("Enum%d value %" IVdf " out of range", bits, iv);
117 35 validate:
118 57 index = (unsigned)(iv + (1 << (bits - 1)));
119 57 ▶ if (!(t->enum_codes[index >> 3] & (1U << (index & 7))))
Branch: 1 branch
BranchTrueFalseTotal
if (!(t->enum_codes[index >> 3] & (1U << (index & 7)))) 16 41 57
120 16 croak("Enum%d undeclared code: %" IVdf, bits, iv);
121 41 return (int16_t)iv;
122 }
123  
124 310255 static uint64_t checked_integer(pTHX_ SV *val, const char *type,
125 unsigned bits, int is_unsigned) {
126 uint64_t magnitude;
127 310255 int negative = 0;
128 310255 SvGETMAGIC(val);
129 310255 ▶ if (!SvOK(val)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) return 0; 8 310247 310255
130 310247 ▶ if (SvIOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val)) { 310202 45 310247
131 310202 ▶ if (SvIsUV(val)) magnitude = (uint64_t)SvUVX(val);
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(val)) magnitude = (uint64_t)SvUVX(val); 5 310197 310202
132 else {
133 310197 IV iv = SvIVX(val);
134 310197 negative = iv < 0;
135 310197 ▶ magnitude = negative ? (uint64_t)0 - (uint64_t)iv : (uint64_t)iv;
Branch: 1 branch
BranchTrueFalseTotal
magnitude = negative ? (uint64_t)0 - (uint64_t)iv : (uint64_t)iv; 91 310106 310197
136 }
137 45 ▶ } else if (SvPOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvPOK(val)) { 6 39 45
138 UV uv;
139 39 int flags = grok_number(SvPVX_const(val), SvCUR(val), &uv);
140 39 ▶ if (flags & (IS_NUMBER_INFINITY | IS_NUMBER_NAN))
Branch: 1 branch
BranchTrueFalseTotal
if (flags & (IS_NUMBER_INFINITY | IS_NUMBER_NAN)) 0 39 39
141 0 croak("%s out of range", type);
142 39 ▶ if (flags & IS_NUMBER_NOT_INT) goto numeric;
Branch: 1 branch
BranchTrueFalseTotal
if (flags & IS_NUMBER_NOT_INT) goto numeric; 4 35 39
143 35 ▶ if (!(flags & IS_NUMBER_IN_UV)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & IS_NUMBER_IN_UV)) { 4 31 35
144 4 croak("%s out of range", type);
145 }
146 31 magnitude = (uint64_t)uv;
147 31 negative = (flags & IS_NUMBER_NEG) != 0;
148 } else {
149 6 numeric: ;
150 10 NV nv = SvNV_nomg(val);
151 10 NV upper = is_unsigned ? (bits == 64 ? (NV)18446744073709551616.0
152 4 ▶ : (NV)((uint64_t)1 << bits))
Branch: 1 branch
BranchTrueFalseTotal
: (NV)((uint64_t)1 << bits)) 3 1 4
153 14 ▶ : (NV)((uint64_t)1 << (bits - 1));
Branch: 1 branch
BranchTrueFalseTotal
: (NV)((uint64_t)1 << (bits - 1)); 4 6 10
154 10 ▶ NV lower = is_unsigned ? (NV)0 : -upper;
Branch: 1 branch
BranchTrueFalseTotal
NV lower = is_unsigned ? (NV)0 : -upper; 6 4 10
155 10 ▶ if (!isfinite(nv) || nv < lower || nv >= upper)
Branch: 3 branches
BranchTrueFalseTotal
if (!isfinite(nv) || nv < lower || nv >= upper) 10 0 10
if (!isfinite(nv) || nv < lower || nv >= upper) 7 3 10
if (!isfinite(nv) || nv < lower || nv >= upper) 4 3 7
156 7 croak("%s out of range", type);
157 3 ▶ return is_unsigned ? (uint64_t)nv : (uint64_t)(int64_t)nv;
Branch: 1 branch
BranchTrueFalseTotal
return is_unsigned ? (uint64_t)nv : (uint64_t)(int64_t)nv; 1 2 3
158 }
159 310233 ▶ if (is_unsigned) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_unsigned) { 297803 12430 310233
160 297803 ▶ uint64_t max = bits == 64 ? UINT64_MAX : ((uint64_t)1 << bits) - 1;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t max = bits == 64 ? UINT64_MAX : ((uint64_t)1 << bits) - 1; 256439 41364 297803
161 297803 ▶ if ((negative && magnitude) || magnitude > max)
Branch: 3 branches
BranchTrueFalseTotal
if ((negative && magnitude) || magnitude > max) 8 297795 297803
if ((negative && magnitude) || magnitude > max) 0 8 8
if ((negative && magnitude) || magnitude > max) 6 297789 297795
162 14 croak("%s out of range", type);
163 297789 return magnitude;
164 } else {
165 12430 uint64_t limit = (uint64_t)1 << (bits - 1);
166 12430 ▶ if (magnitude > limit || (!negative && magnitude == limit))
Branch: 3 branches
BranchTrueFalseTotal
if (magnitude > limit || (!negative && magnitude == limit)) 12425 5 12430
if (magnitude > limit || (!negative && magnitude == limit)) 12333 92 12425
if (magnitude > limit || (!negative && magnitude == limit)) 4 12329 12333
167 9 croak("%s out of range", type);
168 12421 ▶ return negative ? (uint64_t)0 - magnitude : magnitude;
Branch: 1 branch
BranchTrueFalseTotal
return negative ? (uint64_t)0 - magnitude : magnitude; 92 12329 12421
169 }
170 }
171  
172 2108 static void check_decimal_int64(pTHX_ TypeInfo *t, int64_t v, int bits) {
173 2108 ▶ uint64_t magnitude = v < 0 ? (uint64_t)0 - (uint64_t)v : (uint64_t)v;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t magnitude = v < 0 ? (uint64_t)0 - (uint64_t)v : (uint64_t)v; 20 2088 2108
174 2108 ▶ if (t->precision > 0 && t->precision < 19 && magnitude >= pow10_u64[t->precision])
Branch: 3 branches
BranchTrueFalseTotal
if (t->precision > 0 && t->precision < 19 && magnitude >= pow10_u64[t->precision]) 2108 0 2108
if (t->precision > 0 && t->precision < 19 && magnitude >= pow10_u64[t->precision]) 2108 0 2108
if (t->precision > 0 && t->precision < 19 && magnitude >= pow10_u64[t->precision]) 12 2096 2108
175 12 croak("Decimal%d overflow: value exceeds Decimal(%d, %d)", bits, t->precision, t->param);
176 2096 }
177  
178 927701 void encode_scalar(pTHX_ Buffer *b, SV *val, TypeInfo *t) {
179 927701 switch (t->code) {
180 30 case T_INT8: buf_byte(aTHX_ b, (uint8_t)checked_integer(aTHX_ val, "Int8", 8, 0)); break;
181 24 case T_INT16: buf_le16(aTHX_ b, (uint16_t)checked_integer(aTHX_ val, "Int16", 16, 0)); break;
182 12353 case T_INT32: buf_le32(aTHX_ b, (uint32_t)checked_integer(aTHX_ val, "Int32", 32, 0)); break;
183 34 case T_INT64: buf_le64(aTHX_ b, checked_integer(aTHX_ val, "Int64", 64, 0)); break;
184 4750 case T_UINT8: buf_byte(aTHX_ b, (uint8_t)checked_integer(aTHX_ val, "UInt8", 8, 1)); break;
185 20 case T_UINT16: buf_le16(aTHX_ b, (uint16_t)checked_integer(aTHX_ val, "UInt16", 16, 1)); break;
186 251669 case T_UINT32: buf_le32(aTHX_ b, (uint32_t)checked_integer(aTHX_ val, "UInt32", 32, 1)); break;
187 41366 case T_UINT64: buf_le64(aTHX_ b, checked_integer(aTHX_ val, "UInt64", 64, 1)); break;
188 27 case T_FLOAT32: buf_lefloat(aTHX_ b,(float)SvNV(val)); break;
189 42948 case T_FLOAT64: buf_ledouble(aTHX_ b,SvNV(val)); break;
190 24 case T_BFLOAT16: {
191 /* BFloat16 = top 16 bits of the Float32 binary representation
192 * (truncation, no rounding -- matches ClickHouse server). */
193 union { float f; uint32_t u; } u;
194 24 u.f = (float)SvNV(val);
195 24 buf_le16(aTHX_ b, (uint16_t)(u.u >> 16));
196 24 break;
197 }
198 503862 case T_STRING: {
199 503862 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 7 503855 503862
200 7 buf_varint(aTHX_ b,0);
201 } else {
202 STRLEN len;
203 503855 const char *s = SvPV(val, len);
204 503855 buf_string(aTHX_ b,s, len);
205 }
206 503862 break;
207 }
208 47 case T_FIXEDSTRING: {
209 47 buf_grow(aTHX_ b,t->param);
210 47 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 0 47 47
211 0 memset(b->ptr + b->len, 0, t->param);
212 0 b->len += t->param;
213 } else {
214 STRLEN len;
215 47 const char *s = SvPV(val, len);
216 47 ▶ if (len >= (STRLEN)t->param) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= (STRLEN)t->param) { 19 28 47
217 19 memcpy(b->ptr + b->len, s, t->param);
218 19 b->len += t->param;
219 } else {
220 28 memcpy(b->ptr + b->len, s, len);
221 28 memset(b->ptr + b->len + len, 0, t->param - len);
222 28 b->len += t->param;
223 }
224 }
225 47 break;
226 }
227 2084 case T_ENUM8: {
228 2084 int16_t v = enum_value(aTHX_ t, val);
229 2066 buf_byte(aTHX_ b,(uint8_t)(int8_t)v);
230 2066 break;
231 }
232 57 case T_ENUM16: {
233 57 int16_t v = enum_value(aTHX_ t, val);
234 43 buf_le16(aTHX_ b,(uint16_t)v);
235 43 break;
236 }
237 63 case T_DECIMAL32: {
238 int64_t v64;
239 63 ▶ if (looks_stringy(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_stringy(val)) { 53 10 63
240 STRLEN slen;
241 53 const char *s = SvPV(val, slen);
242 53 ▶ if (!parse_decimal_int64_str(s, slen, t->param, t->precision, &v64))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal_int64_str(s, slen, t->param, t->precision, &v64)) 8 45 53
243 8 croak("Invalid decimal string: %.*s", (int)slen, s);
244 } else {
245 10 double d = SvNV(val) * decimal_pow10(t->param);
246 10 ▶ if (!(d >= (double)INT32_MIN && d <= (double)INT32_MAX))
Branch: 2 branches
BranchTrueFalseTotal
if (!(d >= (double)INT32_MIN && d <= (double)INT32_MAX)) 10 0 10
if (!(d >= (double)INT32_MIN && d <= (double)INT32_MAX)) 0 10 10
247 0 croak("Decimal32 overflow");
248 10 v64 = (int64_t)llround(d);
249 }
250 55 check_decimal_int64(aTHX_ t, v64, 32);
251 45 ▶ if (v64 < INT32_MIN || v64 > INT32_MAX)
Branch: 2 branches
BranchTrueFalseTotal
if (v64 < INT32_MIN || v64 > INT32_MAX) 45 0 45
if (v64 < INT32_MIN || v64 > INT32_MAX) 0 45 45
252 0 croak("Decimal32 overflow");
253 45 buf_le32(aTHX_ b,(uint32_t)(int32_t)v64);
254 45 break;
255 }
256 2063 case T_DECIMAL64: {
257 int64_t v;
258 2063 ▶ if (looks_stringy(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_stringy(val)) { 2055 8 2063
259 STRLEN slen;
260 2055 const char *s = SvPV(val, slen);
261 2055 ▶ if (!parse_decimal_int64_str(s, slen, t->param, t->precision, &v))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal_int64_str(s, slen, t->param, t->precision, &v)) 7 2048 2055
262 7 croak("Invalid decimal string: %.*s", (int)slen, s);
263 } else {
264 8 double d = SvNV(val) * decimal_pow10(t->param);
265 /* INT64_MAX (2^63 - 1) isn't exactly representable in double;
266 * the next representable double above it is 2^63 itself, so use
267 * a strict less-than against 2^63 to bracket the valid range. */
268 8 ▶ if (!(d > -9.223372036854776e18 && d < 9.223372036854776e18))
Branch: 2 branches
BranchTrueFalseTotal
if (!(d > -9.223372036854776e18 && d < 9.223372036854776e18)) 8 0 8
if (!(d > -9.223372036854776e18 && d < 9.223372036854776e18)) 3 5 8
269 3 croak("Decimal64 overflow (or non-finite value)");
270 5 v = (int64_t)llround(d);
271 }
272 2053 check_decimal_int64(aTHX_ t, v, 64);
273 2051 buf_le64(aTHX_ b,(uint64_t)v);
274 2051 break;
275 }
276 859 case T_DECIMAL256: {
277 859 uint64_t limbs[4] = {0,0,0,0};
278 859 ▶ if (looks_stringy(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_stringy(val)) { 855 4 859
279 STRLEN slen;
280 855 const char *s = SvPV(val, slen);
281 855 ▶ if (!parse_decimal256_str(s, slen, t->param, t->precision, limbs))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal256_str(s, slen, t->param, t->precision, limbs)) 9 846 855
282 9 croak("Invalid decimal string: %.*s", (int)slen, s);
283 } else {
284 /* Float path through long double; lossy past ~18 digits. */
285 4 long double d = (long double)SvNV(val) * decimal_pow10l(t->param);
286 4 ▶ if (!isfinite(d))
Branch: 1 branch
BranchTrueFalseTotal
if (!isfinite(d)) 0 4 4
287 0 croak("Decimal256: cannot encode non-finite value");
288 4 int neg = d < 0;
289 4 ▶ if (neg) d = -d;
Branch: 1 branch
BranchTrueFalseTotal
if (neg) d = -d; 0 4 4
290 4 d = roundl(d);
291 4 long double two64 = (long double)18446744073709551616.0L;
292 4 long double two255 = two64 * two64 * two64
293 * (long double)9223372036854775808.0L;
294 /* Reject values that would overflow signed 256-bit (i.e. set
295 * the sign bit, which CH would interpret as negative).
296 * Mirrors the >= 2^127 check in the Decimal128 path. */
297 4 ▶ if (d >= two255)
Branch: 1 branch
BranchTrueFalseTotal
if (d >= two255) 3 1 4
298 3 croak("Decimal256 overflow");
299 int j;
300 5 ▶ for (j = 0; j < 4 && d > 0; j++) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < 4 && d > 0; j++) { 4 1 5
for (j = 0; j < 4 && d > 0; j++) { 4 0 4
301 4 limbs[j] = (uint64_t)fmodl(d, two64);
302 4 d = (d - limbs[j]) / two64;
303 }
304 1 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 1 1
305 0 ▶ for (j = 0; j < 4; j++) limbs[j] = ~limbs[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 4; j++) limbs[j] = ~limbs[j]; 0 0 0
306 0 add_digit_256(limbs, 1);
307 }
308 }
309 847 ▶ if (t->precision > 0 && !decimal256_within_precision(limbs, t->precision))
Branch: 2 branches
BranchTrueFalseTotal
if (t->precision > 0 && !decimal256_within_precision(limbs, t->precision)) 847 0 847
if (t->precision > 0 && !decimal256_within_precision(limbs, t->precision)) 5 842 847
310 5 croak("Decimal256 overflow: value exceeds Decimal(%d, %d)", t->precision, t->param);
311 842 buf_le64(aTHX_ b, limbs[0]);
312 842 buf_le64(aTHX_ b, limbs[1]);
313 842 buf_le64(aTHX_ b, limbs[2]);
314 842 buf_le64(aTHX_ b, limbs[3]);
315 842 break;
316 }
317 869 case T_DECIMAL128: {
318 uint64_t lo, hi;
319 869 ▶ if (looks_stringy(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_stringy(val)) { 865 4 869
320 STRLEN slen;
321 865 const char *s = SvPV(val, slen);
322 865 ▶ if (!parse_decimal128_str(s, slen, t->param, t->precision, &hi, &lo))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal128_str(s, slen, t->param, t->precision, &hi, &lo)) 11 854 865
323 11 croak("Invalid decimal string: %.*s", (int)slen, s);
324 } else {
325 4 long double d = (long double)SvNV(val) * decimal_pow10l(t->param);
326 4 ▶ if (!isfinite(d))
Branch: 1 branch
BranchTrueFalseTotal
if (!isfinite(d)) 0 4 4
327 0 croak("Decimal128: cannot encode non-finite value");
328 4 int neg = d < 0;
329 4 ▶ if (neg) d = -d;
Branch: 1 branch
BranchTrueFalseTotal
if (neg) d = -d; 1 3 4
330 4 d = roundl(d);
331 4 long double two64 = (long double)18446744073709551616.0L;
332 4 long double two127 = two64 * (long double)9223372036854775808.0L;
333 4 ▶ if (d >= two127)
Branch: 1 branch
BranchTrueFalseTotal
if (d >= two127) 2 2 4
334 2 croak("Decimal128 overflow");
335 2 hi = (uint64_t)(d / two64);
336 2 lo = (uint64_t)fmodl(d, two64);
337 2 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 1 1 2
338 1 lo = ~lo + 1;
339 1 ▶ hi = ~hi + (lo == 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
hi = ~hi + (lo == 0 ? 1 : 0); 0 1 1
340 }
341 }
342 856 ▶ if (t->precision > 0 && !decimal128_within_precision(hi, lo, t->precision))
Branch: 2 branches
BranchTrueFalseTotal
if (t->precision > 0 && !decimal128_within_precision(hi, lo, t->precision)) 856 0 856
if (t->precision > 0 && !decimal128_within_precision(hi, lo, t->precision)) 6 850 856
343 6 croak("Decimal128 overflow: value exceeds Decimal(%d, %d)", t->precision, t->param);
344 850 buf_le64(aTHX_ b,lo);
345 850 buf_le64(aTHX_ b,hi);
346 850 break;
347 }
348 47 case T_DATE: {
349 /* UInt16 days since the epoch: 1970-01-01 .. 2149-06-06.
350 * Out-of-range values would wrap to a different but
351 * valid-looking date, so reject them as T_DATETIME does. */
352 47 IV days = 0;
353 47 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 43 4 47
354 43 ▶ if (SvIOK(val) || SvNOK(val)) days = SvIV(val);
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(val) || SvNOK(val)) days = SvIV(val); 29 14 43
if (SvIOK(val) || SvNOK(val)) days = SvIV(val); 0 29 29
355 else {
356 STRLEN slen;
357 29 const char *s = SvPV(val, slen);
358 49 days = looks_like_int_str(s, slen)
359 1 ? SvIV(val)
360 29 ▶ : (IV)parse_date_string(aTHX_ s, slen);
Branch: 1 branch
BranchTrueFalseTotal
: (IV)parse_date_string(aTHX_ s, slen); 1 28 29
361 }
362 34 ▶ if (days < 0 || days > 65535) {
Branch: 2 branches
BranchTrueFalseTotal
if (days < 0 || days > 65535) { 32 2 34
if (days < 0 || days > 65535) { 2 30 32
363 STRLEN mlen;
364 4 const char *ms = SvPV(val, mlen);
365 4 ▶ croak("Date out of range "
Branch: 1 branch
BranchTrueFalseTotal
croak("Date out of range " 4 0 4
366 "(1970-01-01 .. 2149-06-06): %.*s",
367 (int)(mlen > 30 ? 30 : mlen), ms);
368 }
369 }
370 34 buf_le16(aTHX_ b, (uint16_t)days);
371 34 break;
372 }
373 22 case T_DATE32: {
374 /* Int32 days since the epoch. Date strings can't reach the
375 * limit (the year caps at 9999), but a raw number can. */
376 22 IV days = 0;
377 22 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 22 0 22
378 22 ▶ if (SvIOK(val) || SvNOK(val)) days = SvIV(val);
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(val) || SvNOK(val)) days = SvIV(val); 13 9 22
if (SvIOK(val) || SvNOK(val)) days = SvIV(val); 1 12 13
379 else {
380 STRLEN slen;
381 12 const char *s = SvPV(val, slen);
382 24 days = looks_like_int_str(s, slen)
383 0 ? SvIV(val)
384 12 ▶ : (IV)parse_date_string(aTHX_ s, slen);
Branch: 1 branch
BranchTrueFalseTotal
: (IV)parse_date_string(aTHX_ s, slen); 0 12 12
385 }
386 22 ▶ if (days < INT32_MIN || days > INT32_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
if (days < INT32_MIN || days > INT32_MAX) { 22 0 22
if (days < INT32_MIN || days > INT32_MAX) { 1 21 22
387 STRLEN mlen;
388 1 const char *ms = SvPV(val, mlen);
389 1 ▶ croak("Date32 out of range "
Branch: 1 branch
BranchTrueFalseTotal
croak("Date32 out of range " 1 0 1
390 "(CH accepts 1900-01-01 .. 2299-12-31): %.*s",
391 (int)(mlen > 30 ? 30 : mlen), ms);
392 }
393 }
394 21 buf_le32(aTHX_ b, (uint32_t)(int32_t)days);
395 21 break;
396 }
397 51134 case T_DATETIME: {
398 uint32_t v;
399 51134 ▶ if (!SvOK(val)) v = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) v = 0; 0 51134 51134
400 51134 ▶ else if (SvIOK(val) || SvNOK(val)) v = (uint32_t)SvUV(val);
Branch: 2 branches
BranchTrueFalseTotal
else if (SvIOK(val) || SvNOK(val)) v = (uint32_t)SvUV(val); 60 51074 51134
else if (SvIOK(val) || SvNOK(val)) v = (uint32_t)SvUV(val); 0 60 60
401 else {
402 STRLEN slen;
403 60 const char *s = SvPV(val, slen);
404 60 ▶ if (looks_like_int_str(s, slen)) v = (uint32_t)SvUV(val);
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_int_str(s, slen)) v = (uint32_t)SvUV(val); 2 58 60
405 58 else v = parse_datetime_string(aTHX_ s, slen, t);
406 }
407 51115 buf_le32(aTHX_ b,v);
408 51115 break;
409 }
410 41 case T_DATETIME64: {
411 int64_t v;
412 41 ▶ if (!SvOK(val)) v = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) v = 0; 0 41 41
413 41 ▶ else if (SvIOK(val)) v = (int64_t)SvIV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOK(val)) v = (int64_t)SvIV(val); 3 38 41
414 38 ▶ else if (SvNOK(val)) v = dt64_double_to_int64(aTHX_ SvNV(val), t->param);
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(val)) v = dt64_double_to_int64(aTHX_ SvNV(val), t->param); 4 34 38
415 else {
416 STRLEN slen;
417 34 const char *s = SvPV(val, slen);
418 34 ▶ if (looks_like_int_str(s, slen))
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_int_str(s, slen)) 2 32 34
419 2 v = (int64_t)SvIV(val);
420 32 ▶ else if (looks_like_number_str(s, slen))
Branch: 1 branch
BranchTrueFalseTotal
else if (looks_like_number_str(s, slen)) 3 29 32
421 3 v = dt64_double_to_int64(aTHX_ SvNV(val), t->param);
422 else
423 29 v = parse_datetime64_string(aTHX_ s, slen, t->param, t);
424 }
425 30 buf_le64(aTHX_ b,(uint64_t)v);
426 30 break;
427 }
428 10031 case T_BOOL: {
429 10031 ▶ uint8_t v = SvOK(val) && SvTRUE(val) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
uint8_t v = SvOK(val) && SvTRUE(val) ? 1 : 0; 10026 5 10031
uint8_t v = SvOK(val) && SvTRUE(val) ? 1 : 0; 5013 5013 10026
430 10031 buf_byte(aTHX_ b, v);
431 10031 break;
432 }
433 2424 case T_UUID: {
434 /* Wire format: two LE UInt64 halves with bytes reversed within
435 * each half. Standard input is "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
436 * also accept 16 raw bytes. */
437 uint8_t buf16[16];
438 2424 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 4 2420 2424
439 4 memset(buf16, 0, 16);
440 } else {
441 STRLEN slen;
442 2420 const char *s = SvPV(val, slen);
443 2420 ▶ if (slen == 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (slen == 16) { 5 2415 2420
444 5 memcpy(buf16, s, 16);
445 2415 ▶ } else if (slen == 36) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (slen == 36) { 2414 1 2415
446 /* parse hex with dashes at positions 8, 13, 18, 23 */
447 2414 int i, j = 0;
448 2414 ▶ if (s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-')
Branch: 4 branches
BranchTrueFalseTotal
if (s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') 2414 0 2414
if (s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') 2414 0 2414
if (s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') 2414 0 2414
if (s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') 0 2414 2414
449 0 croak("Invalid UUID format: %.*s", (int)slen, s);
450 89282 ▶ for (i = 0; i < 36; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 36; i++) { 86869 2413 89282
451 86869 ▶ if (i == 8 || i == 13 || i == 18 || i == 23) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (i == 8 || i == 13 || i == 18 || i == 23) continue; 84456 2413 86869
if (i == 8 || i == 13 || i == 18 || i == 23) continue; 82043 2413 84456
if (i == 8 || i == 13 || i == 18 || i == 23) continue; 79630 2413 82043
if (i == 8 || i == 13 || i == 18 || i == 23) continue; 2413 77217 79630
452 77217 char c = s[i];
453 77217 ▶ int nyb = (c >= '0' && c <= '9') ? c - '0'
Branch: 1 branch
BranchTrueFalseTotal
int nyb = (c >= '0' && c <= '9') ? c - '0' 65176 12041 77217
454 166474 ▶ : (c >= 'a' && c <= 'f') ? c - 'a' + 10
Branch: 2 branches
BranchTrueFalseTotal
: (c >= 'a' && c <= 'f') ? c - 'a' + 10 77217 0 77217
: (c >= 'a' && c <= 'f') ? c - 'a' + 10 12040 0 12040
455 24083 ▶ : (c >= 'A' && c <= 'F') ? c - 'A' + 10
Branch: 2 branches
BranchTrueFalseTotal
: (c >= 'A' && c <= 'F') ? c - 'A' + 10 12040 1 12041
: (c >= 'A' && c <= 'F') ? c - 'A' + 10 0 1 1
456 2 ▶ : -1;
Branch: 1 branch
BranchTrueFalseTotal
: -1; 1 0 1
457 77217 ▶ if (nyb < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (nyb < 0) 1 77216 77217
458 1 croak("Invalid UUID hex digit: %.*s", (int)slen, s);
459 77216 ▶ if ((j & 1) == 0) buf16[j >> 1] = nyb << 4;
Branch: 1 branch
BranchTrueFalseTotal
if ((j & 1) == 0) buf16[j >> 1] = nyb << 4; 38608 38608 77216
460 38608 else buf16[j >> 1] |= nyb;
461 77216 j++;
462 }
463 } else {
464 1 croak("UUID requires 36-char string or 16 raw bytes, got %d bytes",
465 (int)slen);
466 }
467 }
468 /* Reverse first half then second half. */
469 uint8_t out[16];
470 int i;
471 21798 ▶ for (i = 0; i < 8; i++) out[i] = buf16[7 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) out[i] = buf16[7 - i]; 19376 2422 21798
472 21798 ▶ for (i = 0; i < 8; i++) out[8+i] = buf16[15 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) out[8+i] = buf16[15 - i]; 19376 2422 21798
473 2422 buf_append(aTHX_ b, (const char *)out, 16);
474 2422 break;
475 }
476 32 case T_IPV4: {
477 uint32_t v;
478 32 ▶ if (!SvOK(val)) v = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) v = 0; 4 28 32
479 28 ▶ else if (SvIOK(val) || SvNOK(val))
Branch: 2 branches
BranchTrueFalseTotal
else if (SvIOK(val) || SvNOK(val)) 20 8 28
else if (SvIOK(val) || SvNOK(val)) 0 20 20
480 8 v = (uint32_t)checked_integer(aTHX_ val, "IPv4 integer", 32, 1);
481 else {
482 STRLEN slen;
483 20 const char *s = SvPV(val, slen);
484 20 ▶ if (looks_like_int_str(s, slen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_int_str(s, slen)) { 1 19 20
485 1 v = (uint32_t)checked_integer(aTHX_ val, "IPv4 integer", 32, 1);
486 } else {
487 char tmp[INET_ADDRSTRLEN];
488 19 ▶ if (slen >= sizeof(tmp))
Branch: 1 branch
BranchTrueFalseTotal
if (slen >= sizeof(tmp)) 0 19 19
489 0 croak("IPv4 string too long: %.*s", (int)slen, s);
490 19 memcpy(tmp, s, slen);
491 19 tmp[slen] = 0;
492 struct in_addr addr;
493 19 ▶ if (inet_pton(AF_INET, tmp, &addr) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, tmp, &addr) != 1) 2 17 19
494 2 croak("Invalid IPv4 address: %s", tmp);
495 /* inet_pton stores in network byte order; ntohl gives
496 * the canonical host-order integer (e.g. 0x01020304
497 * for "1.2.3.4") on any host. buf_le32 then writes
498 * the LE wire bytes [04][03][02][01]. */
499 17 v = ntohl((uint32_t)addr.s_addr);
500 }
501 }
502 27 buf_le32(aTHX_ b, v);
503 27 break;
504 }
505 821 case T_IPV6: {
506 uint8_t out[16];
507 821 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 4 817 821
508 4 memset(out, 0, 16);
509 } else {
510 STRLEN slen;
511 817 const char *s = SvPV(val, slen);
512 817 ▶ if (slen == 16 && !memchr(s, ':', slen)) {
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 16 && !memchr(s, ':', slen)) { 801 16 817
if (slen == 16 && !memchr(s, ':', slen)) { 401 400 801
513 401 memcpy(out, s, 16);
514 } else {
515 char tmp[INET6_ADDRSTRLEN];
516 416 ▶ if (slen >= sizeof(tmp))
Branch: 1 branch
BranchTrueFalseTotal
if (slen >= sizeof(tmp)) 0 416 416
517 0 croak("IPv6 string too long: %.*s", (int)slen, s);
518 416 memcpy(tmp, s, slen);
519 416 tmp[slen] = 0;
520 416 ▶ if (inet_pton(AF_INET6, tmp, out) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET6, tmp, out) != 1) { 6 410 416
521 struct in_addr v4;
522 6 ▶ if (inet_pton(AF_INET, tmp, &v4) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, tmp, &v4) == 1) { 5 1 6
523 5 memset(out, 0, 10);
524 5 out[10] = out[11] = 255;
525 5 memcpy(out + 12, &v4, 4);
526 1 ▶ } else if (slen == 16) memcpy(out, s, 16);
Branch: 1 branch
BranchTrueFalseTotal
} else if (slen == 16) memcpy(out, s, 16); 0 1 1
527 1 else croak("Invalid IPv6 address: %s", tmp);
528 }
529 }
530 }
531 820 buf_append(aTHX_ b, (const char *)out, 16);
532 820 break;
533 }
534 0 default:
535 0 croak("encode_scalar called on non-scalar type");
536 }
537 927520 }
538  
539 14076 void encode_null_scalar(pTHX_ Buffer *b, TypeInfo *t) {
540 14076 switch (t->code) {
541 1282 case T_INT8: case T_UINT8: case T_ENUM8: case T_BOOL:
542 1282 buf_byte(aTHX_ b,0); break;
543 105 case T_INT16: case T_UINT16: case T_ENUM16: case T_DATE: case T_BFLOAT16:
544 105 buf_le16(aTHX_ b,0); break;
545 1018 case T_INT32: case T_UINT32: case T_FLOAT32:
546 case T_DECIMAL32: case T_DATE32: case T_DATETIME: case T_IPV4:
547 1018 buf_le32(aTHX_ b,0); break;
548 9869 case T_INT64: case T_UINT64: case T_FLOAT64:
549 case T_DECIMAL64: case T_DATETIME64:
550 9869 buf_le64(aTHX_ b,0); break;
551 90 case T_DECIMAL128: case T_UUID: case T_IPV6:
552 90 buf_le64(aTHX_ b,0); buf_le64(aTHX_ b,0); break;
553 48 case T_DECIMAL256:
554 48 buf_le64(aTHX_ b,0); buf_le64(aTHX_ b,0);
555 48 buf_le64(aTHX_ b,0); buf_le64(aTHX_ b,0); break;
556 1634 case T_STRING:
557 1634 buf_varint(aTHX_ b,0); break;
558 30 case T_FIXEDSTRING: {
559 30 buf_grow(aTHX_ b,t->param);
560 30 memset(b->ptr + b->len, 0, t->param);
561 30 b->len += t->param;
562 30 break;
563 }
564 0 default:
565 0 croak("encode_null_scalar called on complex type");
566 }
567 14076 }
568  
569 7675 int is_simple_type(TypeInfo *t) {
570 6228 ▶ return t->code != T_ARRAY && t->code != T_TUPLE && t->code != T_NULLABLE
Branch: 2 branches
BranchTrueFalseTotal
return t->code != T_ARRAY && t->code != T_TUPLE && t->code != T_NULLABLE 6171 57 6228
return t->code != T_ARRAY && t->code != T_TUPLE && t->code != T_NULLABLE 6171 0 6171
571 6171 ▶ && t->code != T_MAP && t->code != T_LOWCARDINALITY
Branch: 2 branches
BranchTrueFalseTotal
&& t->code != T_MAP && t->code != T_LOWCARDINALITY 6154 17 6171
&& t->code != T_MAP && t->code != T_LOWCARDINALITY 6118 36 6154
572 6118 ▶ && t->code != T_VARIANT && t->code != T_JSON
Branch: 2 branches
BranchTrueFalseTotal
&& t->code != T_VARIANT && t->code != T_JSON 6096 22 6118
&& t->code != T_VARIANT && t->code != T_JSON 5854 242 6096
573 13903 ▶ && t->code != T_DYNAMIC;
Branch: 2 branches
BranchTrueFalseTotal
&& t->code != T_DYNAMIC; 6228 1447 7675
&& t->code != T_DYNAMIC; 5843 11 5854
574 }
575  
576 /* Allocate an SV** array backed by a mortal SV (auto-freed on scope exit). */
577 5388 SV **alloc_sv_array(pTHX_ SSize_t n) {
578 5388 ▶ if (n <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return NULL; 1011 4377 5388
579 4377 STRLEN bytes = n * sizeof(SV*);
580 4377 SV *holder = sv_2mortal(newSV(bytes));
581 4377 SvPOK_only(holder);
582 4377 SvCUR_set(holder, bytes); /* keep the SV's claimed length in sync */
583 4377 return (SV**)SvPVX(holder);
584 }
585  
586 static int json_is_bool_ref(pTHX_ SV *val);
587  
588 /* For an arrayref leaf, infer the element kind. All elements must
589 * resolve to the same scalar kind (Bool/Float64/Int64/String) or be
590 * undef. Returns the matching JV_ARRAY_* kind, or -1 if heterogeneous
591 * / unsupported. NULL elements are accepted and encoded as zero-valued
592 * placeholders; CH represents them via the element type's Nullable
593 * wrapper if the user declares one - we use the bare type here. */
594 3410 static int json_classify_array(pTHX_ AV *av) {
595 3410 SSize_t n = av_len(av) + 1;
596 3410 int seen_kind = -1;
597 SSize_t i;
598 11444 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8035 3409 11444
599 8035 SV **e = av_fetch(av, i, 0);
600 8035 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 8035 0 8035
if (!e || !SvOK(*e)) continue; 0 8035 8035
601 8035 ▶ if (SvROK(*e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(*e)) { 620 7415 8035
602 /* recognized booleans only; nested arrays/hashes in array
603 * leaves are not yet supported */
604 620 ▶ if (json_is_bool_ref(aTHX_ *e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (json_is_bool_ref(aTHX_ *e)) { 620 0 620
605 620 ▶ if (seen_kind == -1) seen_kind = JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_kind == -1) seen_kind = JV_BOOL; 234 386 620
606 386 ▶ else if (seen_kind != JV_BOOL) return -1;
Branch: 1 branch
BranchTrueFalseTotal
else if (seen_kind != JV_BOOL) return -1; 0 386 386
607 620 continue;
608 }
609 0 return -1;
610 }
611 int leaf;
612 #ifdef SvIsBOOL
613 7415 ▶ if (SvIsBOOL(*e)) leaf = JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsBOOL(*e)) leaf = JV_BOOL; 0 7415 7415
614 else
615 #endif
616 7415 ▶ if (SvIOK(*e) && !SvNOK(*e))
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(*e) && !SvNOK(*e)) 2316 5099 7415
if (SvIOK(*e) && !SvNOK(*e)) 2316 0 2316
617 2316 ▶ leaf = SvIsUV(*e) && SvUV(*e) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64;
Branch: 2 branches
BranchTrueFalseTotal
leaf = SvIsUV(*e) && SvUV(*e) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64; 0 2316 2316
leaf = SvIsUV(*e) && SvUV(*e) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64; 0 0 0
618 5099 ▶ else if (SvNOK(*e)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(*e)) { 1752 3347 5099
619 1752 NV nv = SvNV(*e);
620 3504 ▶ leaf = (nv >= (NV)INT64_MIN && nv < (NV)9223372036854775808.0L
Branch: 1 branch
BranchTrueFalseTotal
leaf = (nv >= (NV)INT64_MIN && nv < (NV)9223372036854775808.0L 1752 0 1752
621 1752 ▶ && nv == (NV)(int64_t)nv)
Branch: 1 branch
BranchTrueFalseTotal
&& nv == (NV)(int64_t)nv) 0 1752 1752
622 3504 ▶ ? JV_INT64 : JV_FLOAT64;
Branch: 1 branch
BranchTrueFalseTotal
? JV_INT64 : JV_FLOAT64; 1752 0 1752
623 } else {
624 3347 leaf = JV_STRING;
625 }
626 7415 ▶ if (seen_kind == -1) seen_kind = leaf;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_kind == -1) seen_kind = leaf; 2950 4465 7415
627 4465 ▶ else if (seen_kind != leaf) return -1;
Branch: 1 branch
BranchTrueFalseTotal
else if (seen_kind != leaf) return -1; 1 4464 4465
628 }
629 /* all-NULL or empty: default to Array(Int64). The empty array
630 * doesn't carry an element type, so we pick one arbitrarily. If
631 * another row in the same path has a non-empty Array(T), the kind
632 * mask will then carry both Array(Int64) and Array(T); for a clean
633 * single-variant column the user can declare the column explicitly
634 * via a Variant(...) type. */
635 3409 ▶ if (seen_kind == -1) return JV_ARRAY_INT64;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_kind == -1) return JV_ARRAY_INT64; 226 3183 3409
636 3183 switch (seen_kind) {
637 234 case JV_BOOL: return JV_ARRAY_BOOL;
638 806 case JV_FLOAT64: return JV_ARRAY_FLOAT64;
639 812 case JV_INT64: return JV_ARRAY_INT64;
640 1331 case JV_STRING: return JV_ARRAY_STRING;
641 0 default: return -1;
642 }
643 }
644  
645 2219 static int json_pkg_is_bool(const char *pkg) {
646 2234 ▶ return pkg && (strcmp(pkg, "JSON::PP::Boolean") == 0
Branch: 2 branches
BranchTrueFalseTotal
return pkg && (strcmp(pkg, "JSON::PP::Boolean") == 0 2219 0 2219
return pkg && (strcmp(pkg, "JSON::PP::Boolean") == 0 15 2204 2219
647 15 ▶ || strcmp(pkg, "Types::Serialiser::Boolean") == 0
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "Types::Serialiser::Boolean") == 0 15 0 15
648 15 ▶ || strcmp(pkg, "JSON::XS::Boolean") == 0
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "JSON::XS::Boolean") == 0 8 7 15
649 8 ▶ || strcmp(pkg, "Cpanel::JSON::XS::Boolean") == 0
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "Cpanel::JSON::XS::Boolean") == 0 1 7 8
650 1 ▶ || strcmp(pkg, "boolean") == 0);
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "boolean") == 0); 0 1 1
651 }
652  
653 /* True if val is a blessed ref into one of the recognized boolean packages. */
654 14043 static int json_is_bool_ref(pTHX_ SV *val) {
655 14043 ▶ if (!(SvROK(val) && sv_isobject(val))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(val) && sv_isobject(val))) return 0; 8365 5678 14043
if (!(SvROK(val) && sv_isobject(val))) return 0; 6146 2219 8365
656 2219 HV *stash = SvSTASH(SvRV(val));
657 2219 ▶ return stash && json_pkg_is_bool(HvNAME(stash));
Branch: 8 branches
BranchTrueFalseTotal
return stash && json_pkg_is_bool(HvNAME(stash)); 2219 0 2219
return stash && json_pkg_is_bool(HvNAME(stash)); 2219 0 2219
return stash && json_pkg_is_bool(HvNAME(stash)); 2219 0 2219
return stash && json_pkg_is_bool(HvNAME(stash)); 0 2219 2219
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 2219 0 2219
return stash && json_pkg_is_bool(HvNAME(stash)); 0 2219 2219
return stash && json_pkg_is_bool(HvNAME(stash)); 2218 1 2219
658 }
659  
660 /* Classify a non-undef leaf SV for JSON encoding. Hash/array refs (other
661 * than recognized booleans) must be rejected by the caller before this. */
662 6363 static JsonValueKind json_classify_leaf(pTHX_ SV *val) {
663 6363 ▶ if (json_is_bool_ref(aTHX_ val)) return JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (json_is_bool_ref(aTHX_ val)) return JV_BOOL; 685 5678 6363
664 #ifdef SvIsBOOL
665 5678 ▶ if (SvIsBOOL(val)) return JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsBOOL(val)) return JV_BOOL; 6 5672 5678
666 #endif
667 5672 ▶ if (SvIOK(val) && !SvNOK(val))
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(val) && !SvNOK(val)) 2504 3168 5672
if (SvIOK(val) && !SvNOK(val)) 2504 0 2504
668 2504 ▶ return SvIsUV(val) && SvUV(val) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64;
Branch: 2 branches
BranchTrueFalseTotal
return SvIsUV(val) && SvUV(val) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64; 0 2504 2504
return SvIsUV(val) && SvUV(val) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64; 0 0 0
669 3168 ▶ if (SvNOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(val)) { 908 2260 3168
670 908 NV n = SvNV(val);
671 /* NV holding an exact integer value (e.g. `1.0`) collapses to
672 * Int64 - matches CH's JSONEachRow type-inference. Users who
673 * need `1.0` to survive as Float64 on the wire should put the
674 * value through a float-typed Variant or an explicit Float64
675 * column. NaN/Inf fall through to Float64 because the integer
676 * round-trip equality check fails for them. */
677 908 ▶ if (n >= (NV)INT64_MIN && n < (NV)9223372036854775808.0L
Branch: 2 branches
BranchTrueFalseTotal
if (n >= (NV)INT64_MIN && n < (NV)9223372036854775808.0L 908 0 908
if (n >= (NV)INT64_MIN && n < (NV)9223372036854775808.0L 908 0 908
678 908 ▶ && n == (NV)(int64_t)n)
Branch: 1 branch
BranchTrueFalseTotal
&& n == (NV)(int64_t)n) 0 908 908
679 0 return JV_INT64;
680 908 return JV_FLOAT64;
681 }
682 2260 return JV_STRING;
683 }
684  
685 /* Classify a (defined) JSON/Dynamic value SV. Dispatches arrayrefs to
686 * json_classify_array and everything else to json_classify_leaf, so the
687 * caller doesn't have to repeat the SvROK / bool-ref check. Returns the
688 * JsonValueKind, or -1 if `val` is an arrayref with heterogeneous
689 * elements. Caller has already ruled out hashref-non-bool refs. */
690 9773 static int json_classify_value(pTHX_ SV *val) {
691 9773 ▶ if (SvROK(val) && !json_is_bool_ref(aTHX_ val))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && !json_is_bool_ref(aTHX_ val)) 4095 5678 9773
if (SvROK(val) && !json_is_bool_ref(aTHX_ val)) 3410 685 4095
692 3410 return json_classify_array(aTHX_ (AV*)SvRV(val));
693 6363 return json_classify_leaf(aTHX_ val);
694 }
695  
696 /* Emit one element of an Array(T) Dynamic variant. Used by both T_JSON
697 * (per-path) and T_DYNAMIC (top-level) encode paths; `ev` may be undef
698 * in which case a zero-valued placeholder of the element type is written
699 * (CH represents nulls via Nullable wrappers, which we don't introduce
700 * inside Dynamic variant arrays). */
701 2006 static void json_emit_array_elem(pTHX_ Buffer *b, SV *ev, int k_match) {
702 2006 switch (k_match) {
703 153 case JV_ARRAY_BOOL: {
704 153 ▶ if (!SvOK(ev)) { buf_byte(aTHX_ b, 0); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ev)) { buf_byte(aTHX_ b, 0); break; } 0 153 153
705 153 ▶ SV *bv = SvROK(ev) ? SvRV(ev) : ev;
Branch: 1 branch
BranchTrueFalseTotal
SV *bv = SvROK(ev) ? SvRV(ev) : ev; 153 0 153
706 153 buf_byte(aTHX_ b, SvTRUE(bv) ? 1 : 0);
707 153 break;
708 }
709 581 case JV_ARRAY_INT64:
710 581 ▶ buf_le64(aTHX_ b, SvOK(ev) ? (uint64_t)(int64_t)SvIV(ev) : 0);
Branch: 1 branch
BranchTrueFalseTotal
buf_le64(aTHX_ b, SvOK(ev) ? (uint64_t)(int64_t)SvIV(ev) : 0); 581 0 581
711 581 break;
712 436 case JV_ARRAY_FLOAT64:
713 436 ▶ buf_ledouble(aTHX_ b, SvOK(ev) ? SvNV(ev) : 0.0);
Branch: 1 branch
BranchTrueFalseTotal
buf_ledouble(aTHX_ b, SvOK(ev) ? SvNV(ev) : 0.0); 436 0 436
714 436 break;
715 836 case JV_ARRAY_STRING: {
716 836 ▶ if (!SvOK(ev)) { buf_varint(aTHX_ b, 0); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ev)) { buf_varint(aTHX_ b, 0); break; } 0 836 836
717 STRLEN sl;
718 836 const char *ss = SvPV(ev, sl);
719 836 buf_string(aTHX_ b, ss, sl);
720 836 break;
721 }
722 }
723 2006 }
724  
725 /* Emit one scalar value of a Dynamic variant. `val` is assumed defined
726 * (the disc loop has already routed undef rows to disc 0xff). */
727 1849 static void json_emit_scalar(pTHX_ Buffer *b, SV *val, int k_match) {
728 1849 switch (k_match) {
729 230 case JV_BOOL: {
730 230 ▶ SV *bv = SvROK(val) ? SvRV(val) : val;
Branch: 1 branch
BranchTrueFalseTotal
SV *bv = SvROK(val) ? SvRV(val) : val; 228 2 230
731 230 buf_byte(aTHX_ b, SvTRUE(bv) ? 1 : 0);
732 230 break;
733 }
734 746 case JV_INT64:
735 746 buf_le64(aTHX_ b, (uint64_t)(int64_t)SvIV(val));
736 746 break;
737 210 case JV_FLOAT64:
738 210 buf_ledouble(aTHX_ b, SvNV(val));
739 210 break;
740 663 case JV_STRING: {
741 STRLEN sl;
742 663 const char *s = SvPV(val, sl);
743 663 buf_string(aTHX_ b, s, sl);
744 663 break;
745 }
746 0 default: break;
747 }
748 1849 }
749  
750 /* Cap on JSON object nesting: flatten_json_hash recurses once per level,
751 * so an unbounded structure overflows the C stack. Wire data reaches it
752 * too - a path name with N dotted segments decodes to an N-deep hash, so
753 * re-encoding a hostile block recurses just as far. 512 matches the
754 * default max_depth of the common JSON parsers. */
755 #define CHE_MAX_JSON_DEPTH 512
756  
757 /* Recursively flatten a JSON value hash into a flat HV of dotted-path
758 * names. Allocated SVs are mortalized so the caller never has to free
759 * them; the only references stored in `out_flat` are to leaf SVs from
760 * the original hash (which the caller pins).
761 *
762 * If `stop_paths` is non-NULL, it's a HV of dotted-path names to treat
763 * as leaves: when we encounter a hash value at a path in the set, we
764 * store the hashref directly rather than recursing. Used by JSON typed
765 * paths whose declared inner type is a hash-shape (Map / Tuple). */
766 3489 static void flatten_json_hash(pTHX_ HV *src,
767 const char *prefix, STRLEN prefix_len,
768 HV *out_flat, HV *stop_paths, int depth) {
769 3489 ▶ if (++depth > CHE_MAX_JSON_DEPTH)
Branch: 1 branch
BranchTrueFalseTotal
if (++depth > CHE_MAX_JSON_DEPTH) 2 3487 3489
770 2 croak("JSON column: object nesting deeper than %d levels",
771 CHE_MAX_JSON_DEPTH);
772 3487 hv_iterinit(src);
773 HE *he;
774 7263 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 4802 2461 7263
775 STRLEN klen;
776 4802 const char *kstr = hash_key_bytes(aTHX_ he, &klen);
777 4802 SV *vsv = hv_iterval(src, he);
778  
779 4802 ▶ STRLEN new_len = prefix_len + (prefix ? 1 : 0) + klen;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN new_len = prefix_len + (prefix ? 1 : 0) + klen; 2228 2574 4802
780 4802 SV *path_sv = sv_2mortal(newSV(new_len + 1));
781 4802 SvPOK_only(path_sv);
782 4802 char *pbuf = SvPVX(path_sv);
783 4802 ▶ if (prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (prefix) { 2228 2574 4802
784 2228 memcpy(pbuf, prefix, prefix_len);
785 2228 pbuf[prefix_len] = '.';
786 2228 memcpy(pbuf + prefix_len + 1, kstr, klen);
787 } else {
788 2574 memcpy(pbuf, kstr, klen);
789 }
790 4802 SvCUR_set(path_sv, new_len);
791  
792 4802 int stop_here = stop_paths
793 4802 ▶ && hv_exists(stop_paths, SvPVX(path_sv), new_len);
Branch: 2 branches
BranchTrueFalseTotal
&& hv_exists(stop_paths, SvPVX(path_sv), new_len); 36 4766 4802
&& hv_exists(stop_paths, SvPVX(path_sv), new_len); 4 32 36
794  
795 4802 ▶ if (!stop_here
Branch: 1 branch
BranchTrueFalseTotal
if (!stop_here 4798 4 4802
796 4798 ▶ && SvROK(vsv) && SvTYPE(SvRV(vsv)) == SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
&& SvROK(vsv) && SvTYPE(SvRV(vsv)) == SVt_PVHV 2961 1837 4798
&& SvROK(vsv) && SvTYPE(SvRV(vsv)) == SVt_PVHV 1883 1078 2961
797 1883 ▶ && !json_is_bool_ref(aTHX_ vsv)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !json_is_bool_ref(aTHX_ vsv)) { 1883 0 1883
798 /* Reject blessed hashrefs that aren't recognized Booleans:
799 * a JSON value of `bless {}, "Custom"` shouldn't silently
800 * flatten as if it were a plain hash. */
801 1883 ▶ if (sv_isobject(vsv))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(vsv)) 1 1882 1883
802 1 ▶ croak("JSON column: opaque blessed hashref (package '%s') "
Branch: 6 branches
BranchTrueFalseTotal
croak("JSON column: opaque blessed hashref (package '%s') " 1 0 1
croak("JSON column: opaque blessed hashref (package '%s') " 1 0 1
croak("JSON column: opaque blessed hashref (package '%s') " 0 1 1
croak("JSON column: opaque blessed hashref (package '%s') " 0 0 0
croak("JSON column: opaque blessed hashref (package '%s') " 1 0 1
croak("JSON column: opaque blessed hashref (package '%s') " 0 1 1
803 "is not a JSON value; only known Boolean classes "
804 "are accepted as object leaves",
805 HvNAME(SvSTASH(SvRV(vsv))));
806 1882 flatten_json_hash(aTHX_ (HV*)SvRV(vsv),
807 1882 SvPVX(path_sv), new_len, out_flat,
808 stop_paths, depth);
809 } else {
810 2919 ▶ UV count = HvUSEDKEYS(out_flat);
Branch: 1 branch
BranchTrueFalseTotal
UV count = HvUSEDKEYS(out_flat); 0 2919 2919
811 2919 hv_store(out_flat, SvPVX(path_sv), new_len,
812 SvREFCNT_inc_simple_NN(vsv), 0);
813 2919 ▶ if (HvUSEDKEYS(out_flat) == count)
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(out_flat) == count) 0 2919 2919
if (HvUSEDKEYS(out_flat) == count) 1 2918 2919
814 1 croak("JSON column: duplicate path '%.*s' in one object", (int)new_len, SvPVX(path_sv));
815 }
816 }
817 2461 }
818  
819 /* (path, len) pair: lets the JSON encoder sort paths whose keys may
820 * contain embedded NUL bytes without falling back to strlen() after
821 * sort. Perl HV keys are NUL-terminated but the value bytes themselves
822 * are arbitrary, so we carry both together. */
823 typedef struct { char *path; STRLEN len; } PathEntry;
824  
825 5466 static int cmp_path_entry(const void *a, const void *b) {
826 5466 const PathEntry *pa = (const PathEntry *)a;
827 5466 const PathEntry *pb = (const PathEntry *)b;
828 5466 STRLEN n = pa->len < pb->len ? pa->len : pb->len;
829 5466 int r = memcmp(pa->path, pb->path, n);
830 5466 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 5405 61 5466
831 61 ▶ if (pa->len < pb->len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (pa->len < pb->len) return -1; 26 35 61
832 35 ▶ if (pa->len > pb->len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pa->len > pb->len) return 1; 35 0 35
833 0 return 0;
834 }
835  
836 static void encode_column_data(pTHX_ Buffer *b, Buffer *prefix,
837 SV **values, SSize_t num_rows, TypeInfo *t);
838  
839 1950 void encode_column(pTHX_ Buffer *b, SV **values, SSize_t num_rows, TypeInfo *t) {
840 1950 ▶ if (!num_rows) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!num_rows) return; 70 1880 1950
841 1880 ▶ if (t->split_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->split_prefix) { 17 1863 1880
842 Buffer data;
843 17 buf_init(aTHX_ &data);
844 17 encode_column_data(aTHX_ &data, b, values, num_rows, t);
845 17 buf_append(aTHX_ b, data.ptr, data.len);
846 } else {
847 1863 encode_column_data(aTHX_ b, b, values, num_rows, t);
848 }
849 }
850  
851 /* Native writes every state prefix before any container or value data. */
852 5627 static void encode_column_data(pTHX_ Buffer *b, Buffer *prefix,
853 SV **values, SSize_t num_rows, TypeInfo *t) {
854 SSize_t r;
855  
856 5627 switch (t->code) {
857 1686 case T_INT8: case T_INT16: case T_INT32: case T_INT64:
858 case T_UINT8: case T_UINT16: case T_UINT32: case T_UINT64:
859 case T_FLOAT32: case T_FLOAT64: case T_BFLOAT16:
860 case T_STRING: case T_FIXEDSTRING:
861 case T_ENUM8: case T_ENUM16:
862 case T_DECIMAL32: case T_DECIMAL64: case T_DECIMAL128: case T_DECIMAL256:
863 case T_DATE: case T_DATE32: case T_DATETIME: case T_DATETIME64:
864 case T_BOOL: case T_UUID: case T_IPV4: case T_IPV6:
865 350576 ▶ for (r = 0; r < num_rows; r++) encode_scalar(aTHX_ b,values[r], t);
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) encode_scalar(aTHX_ b,values[r], t); 348895 1681 350576
866 1681 break;
867  
868 1501 case T_ARRAY: {
869 1501 uint64_t offset = 0;
870 1501 SSize_t total_elems = 0;
871  
872 96444 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 94944 1500 96444
873 94944 SV *val = values[r];
874 94944 ▶ if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) 94943 1 94944
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) 0 94943 94943
875 1 croak("Expected arrayref for Array type");
876 94943 AV *av = (AV*)SvRV(val);
877 94943 SSize_t n = av_len(av) + 1;
878 94943 offset += (uint64_t)n;
879 94943 total_elems += n;
880 94943 buf_le64(aTHX_ b,offset);
881 }
882  
883 1500 ▶ if (total_elems > 0 || t->inner->has_prefix) {
Branch: 2 branches
BranchTrueFalseTotal
if (total_elems > 0 || t->inner->has_prefix) { 5 1495 1500
if (total_elems > 0 || t->inner->has_prefix) { 3 2 5
884 1498 SV **all_elems = alloc_sv_array(aTHX_ total_elems);
885 1498 SSize_t idx = 0;
886 96439 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 94941 1498 96439
887 94941 AV *av = (AV*)SvRV(values[r]);
888 94941 SSize_t n = av_len(av) + 1;
889 SSize_t i;
890 374418 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 279477 94941 374418
891 279477 SV **elem = av_fetch(av, i, 0);
892 279477 ▶ all_elems[idx++] = elem ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
all_elems[idx++] = elem ? *elem : &PL_sv_undef; 279477 0 279477
893 }
894 }
895 1498 encode_column_data(aTHX_ b, prefix, all_elems, total_elems, t->inner);
896 }
897 1498 break;
898 }
899  
900 1055 case T_TUPLE: {
901 1055 ▶ if (!t->tuple_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!t->tuple_len) { 1 1054 1055
902 1 buf_grow(aTHX_ b, num_rows);
903 1 memset(b->ptr + b->len, 0x30, num_rows);
904 1 b->len += num_rows;
905 1 break;
906 }
907 int i;
908 1054 SV **elem_values = alloc_sv_array(aTHX_ num_rows);
909  
910 3152 ▶ for (i = 0; i < t->tuple_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->tuple_len; i++) { 2102 1050 3152
911 6333 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 4233 2100 6333
912 4233 SV *val = values[r];
913 4233 ▶ if (!SvROK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(val)) 1 4232 4233
914 1 croak("Expected arrayref or hashref for Tuple type");
915 4232 SV *rv = SvRV(val);
916 4232 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 4224 8 4232
917 4224 SV **elem = av_fetch((AV*)rv, i, 0);
918 4224 ▶ elem_values[r] = elem ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
elem_values[r] = elem ? *elem : &PL_sv_undef; 4224 0 4224
919 8 ▶ } else if (SvTYPE(rv) == SVt_PVHV
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(rv) == SVt_PVHV 8 0 8
920 8 ▶ && t->tuple_names && t->tuple_names[i]) {
Branch: 2 branches
BranchTrueFalseTotal
&& t->tuple_names && t->tuple_names[i]) { 7 1 8
&& t->tuple_names && t->tuple_names[i]) { 7 0 7
921 /* Named-tuple input as hashref: look up by name. */
922 7 SV **elem = fetch_named_value(aTHX_ (HV*)rv, t->tuple_names[i],
923 7 strlen(t->tuple_names[i]));
924 7 ▶ elem_values[r] = elem ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
elem_values[r] = elem ? *elem : &PL_sv_undef; 6 1 7
925 1 ▶ } else if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(rv) == SVt_PVHV) { 1 0 1
926 1 croak("Tuple given as hashref but type is unnamed; "
927 "use arrayref or declare named Tuple(name Type, ...)");
928 } else {
929 0 croak("Expected arrayref or hashref for Tuple type");
930 }
931 }
932 2100 encode_column_data(aTHX_ b, prefix, elem_values, num_rows, t->tuple[i]);
933 }
934 1050 break;
935 }
936  
937 1044 case T_NULLABLE: {
938 3163 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 2119 1044 3163
939 2119 buf_byte(aTHX_ b, nullable_is_null(values[r], t) ? 1 : 0);
940 }
941  
942 1044 ▶ if (is_simple_type(t->inner)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_simple_type(t->inner)) { 1030 14 1044
943 3111 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 2081 1030 3111
944 2081 ▶ if (nullable_is_null(values[r], t)) encode_null_scalar(aTHX_ b, t->inner);
Branch: 1 branch
BranchTrueFalseTotal
if (nullable_is_null(values[r], t)) encode_null_scalar(aTHX_ b, t->inner); 1038 1043 2081
945 1043 else encode_scalar(aTHX_ b,values[r], t->inner);
946 }
947 } else {
948 14 SV **inner_values = alloc_sv_array(aTHX_ num_rows);
949 52 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 38 14 52
950 38 ▶ if (nullable_is_null(values[r], t))
Branch: 1 branch
BranchTrueFalseTotal
if (nullable_is_null(values[r], t)) 15 23 38
951 15 inner_values[r] = make_null_placeholder(aTHX_ t->inner);
952 else
953 23 inner_values[r] = values[r];
954 }
955 14 encode_column_data(aTHX_ b, prefix, inner_values, num_rows, t->inner);
956 }
957 1044 break;
958 }
959  
960 18 case T_MAP: {
961 /* Wire format is identical to Array(Tuple(K, V)). Each row's value
962 * is either a hashref or arrayref-of-pairs; normalize to a flat
963 * arrayref of [k, v] pairs and reuse the Array(Tuple) path. */
964 18 SV **norm = alloc_sv_array(aTHX_ num_rows);
965 850 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 833 17 850
966 833 SV *val = values[r];
967 833 ▶ if (!SvROK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(val)) 1 832 833
968 1 croak("Expected hashref or arrayref for Map type");
969 832 SV *rv = SvRV(val);
970 832 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 22 810 832
971 22 norm[r] = val; /* already arrayref-of-pairs */
972 810 ▶ } else if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(rv) == SVt_PVHV) { 810 0 810
973 810 HV *hv = (HV *)rv;
974 810 AV *pairs = newAV();
975 810 hv_iterinit(hv);
976 HE *he;
977 2024 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 1214 810 2024
978 1214 SV *vsv = hv_iterval(hv, he);
979 1214 AV *pair = newAV();
980 STRLEN klen;
981 1214 const char *kstr = hash_key_bytes(aTHX_ he, &klen);
982 1214 av_push(pair, newSVpvn(kstr, klen));
983 1214 av_push(pair, SvREFCNT_inc(vsv));
984 1214 av_push(pairs, newRV_noinc((SV *)pair));
985 }
986 810 norm[r] = sv_2mortal(newRV_noinc((SV *)pairs));
987 } else {
988 0 croak("Expected hashref or arrayref for Map type");
989 }
990 }
991  
992 /* Synthesize the Array(Tuple(K,V)) wire path. */
993 17 uint64_t offset = 0;
994 17 SSize_t total = 0;
995 849 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 832 17 849
996 832 AV *av = (AV *)SvRV(norm[r]);
997 832 SSize_t n = av_len(av) + 1;
998 832 offset += (uint64_t)n;
999 832 total += n;
1000 832 buf_le64(aTHX_ b, offset);
1001 }
1002 17 ▶ if (total > 0 || t->split_prefix) {
Branch: 2 branches
BranchTrueFalseTotal
if (total > 0 || t->split_prefix) { 0 17 17
if (total > 0 || t->split_prefix) { 0 0 0
1003 17 SV **all = alloc_sv_array(aTHX_ total);
1004 17 SSize_t idx = 0;
1005 849 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 832 17 849
1006 832 AV *av = (AV *)SvRV(norm[r]);
1007 832 SSize_t n = av_len(av) + 1;
1008 SSize_t i;
1009 2060 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1228 832 2060
1010 1228 SV **elem = av_fetch(av, i, 0);
1011 1228 ▶ all[idx++] = elem ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
all[idx++] = elem ? *elem : &PL_sv_undef; 1228 0 1228
1012 }
1013 }
1014 /* Encode as Tuple(K, V): first all keys, then all values. */
1015 int j;
1016 51 ▶ for (j = 0; j < 2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 2; j++) { 34 17 51
1017 34 SV **col = alloc_sv_array(aTHX_ total);
1018 SSize_t k;
1019 2490 ▶ for (k = 0; k < total; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < total; k++) { 2456 34 2490
1020 2456 ▶ if (!SvROK(all[k]) || SvTYPE(SvRV(all[k])) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(all[k]) || SvTYPE(SvRV(all[k])) != SVt_PVAV) 2456 0 2456
if (!SvROK(all[k]) || SvTYPE(SvRV(all[k])) != SVt_PVAV) 0 2456 2456
1021 0 croak("Map pair must be a 2-element arrayref");
1022 2456 AV *pair = (AV *)SvRV(all[k]);
1023 2456 SV **e = av_fetch(pair, j, 0);
1024 2456 ▶ col[k] = e ? *e : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
col[k] = e ? *e : &PL_sv_undef; 2456 0 2456
1025 }
1026 34 encode_column_data(aTHX_ b, prefix, col, total, t->tuple[j]);
1027 }
1028 }
1029 17 break;
1030 }
1031  
1032 23 case T_VARIANT: {
1033 /* Variant wire format (CH 24.1+):
1034 * UInt64 LE mode = 0 (non-shared serialization)
1035 * UInt8[N] wire discriminators (255 = NULL, else 0..n-1
1036 * referring to the alphabetical-order position)
1037 * <sub-cols> one per variant, in alphabetical order of type
1038 * names; each contains the values whose
1039 * discriminator equals that variant's wire index
1040 *
1041 * The user passes [decl_idx, value] using declaration order;
1042 * variant_decl_to_wire[d] / variant_wire_to_decl[w] handle
1043 * the alphabetical reordering ClickHouse requires. */
1044 23 int nvar = t->tuple_len;
1045 #define VARIANT_NULL 255
1046  
1047 23 buf_le64(aTHX_ prefix, 0);
1048  
1049 int *counts; /* indexed by declaration idx */
1050 23 Newxz(counts, nvar, int);
1051 23 SAVEFREEPV(counts);
1052 SV ***per_var; /* per_var[decl_idx] = values for that variant */
1053 23 Newxz(per_var, nvar, SV **);
1054 23 SAVEFREEPV(per_var);
1055 int v;
1056 71 ▶ for (v = 0; v < nvar; v++)
Branch: 1 branch
BranchTrueFalseTotal
for (v = 0; v < nvar; v++) 48 23 71
1057 48 per_var[v] = alloc_sv_array(aTHX_ num_rows);
1058  
1059 68 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 50 18 68
1060 50 SV *val = values[r];
1061 50 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 16 34 50
1062 16 buf_byte(aTHX_ b, VARIANT_NULL);
1063 16 continue;
1064 }
1065 34 ▶ if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) 32 2 34
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) 0 32 32
1066 2 croak("Variant value must be undef or [variant_idx, value]");
1067 32 AV *av = (AV *)SvRV(val);
1068 32 ▶ if (av_len(av) + 1 != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(av) + 1 != 2) 1 31 32
1069 1 croak("Variant value must be a 2-element [idx, value] pair");
1070 31 SV **idx_sv = av_fetch(av, 0, 0);
1071 31 SV **vsv = av_fetch(av, 1, 0);
1072 31 ▶ IV idx = idx_sv ? SvIV(*idx_sv) : -1;
Branch: 1 branch
BranchTrueFalseTotal
IV idx = idx_sv ? SvIV(*idx_sv) : -1; 31 0 31
1073 31 ▶ if (idx < 0 || idx >= nvar)
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= nvar) 31 0 31
if (idx < 0 || idx >= nvar) 2 29 31
1074 2 croak("Variant index %" IVdf " out of range (0..%d)",
1075 idx, nvar - 1);
1076 29 buf_byte(aTHX_ b, (uint8_t)t->variant_decl_to_wire[idx]);
1077 29 ▶ per_var[idx][counts[idx]++] = vsv ? *vsv : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
per_var[idx][counts[idx]++] = vsv ? *vsv : &PL_sv_undef; 29 0 29
1078 }
1079  
1080 /* Emit sub-columns in wire (alphabetical) order. */
1081 56 ▶ for (v = 0; v < nvar; v++) {
Branch: 1 branch
BranchTrueFalseTotal
for (v = 0; v < nvar; v++) { 38 18 56
1082 38 int d = t->variant_wire_to_decl[v];
1083 38 encode_column_data(aTHX_ b, prefix, per_var[d], counts[d], t->tuple[d]);
1084 }
1085 #undef VARIANT_NULL
1086 18 break;
1087 }
1088  
1089 46 case T_LOWCARDINALITY: {
1090 /* LowCardinality block format (per ClickHouse Native protocol):
1091 * UInt64 LE serialization version (= 1)
1092 * UInt64 LE flags: HasAdditionalKeys(=1<<9) | index-type-bits(0..3)
1093 * UInt64 LE dictionary keys count
1094 * <dict> keys serialized using the inner type's column format
1095 * UInt64 LE index count (= num_rows)
1096 * <indices> packed UInt8/16/32/64 according to flags low byte
1097 *
1098 * For Nullable(T) the underlying inner type for serialization is
1099 * the bare T; NULL is represented by reserving dictionary index 0
1100 * (which always carries the "default" value) and the wire format
1101 * for the actual non-NULL dict starts at index 1.
1102 */
1103 46 buf_le64(aTHX_ prefix, 1);
1104 46 ▶ if (!num_rows) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!num_rows) break; 1 45 46
1105 45 TypeInfo *inner = t->inner;
1106 45 int is_null = (inner->code == T_NULLABLE);
1107 45 ▶ TypeInfo *leaf = is_null ? inner->inner : inner;
Branch: 1 branch
BranchTrueFalseTotal
TypeInfo *leaf = is_null ? inner->inner : inner; 19 26 45
1108 45 ▶ int numeric = leaf->code != T_STRING && leaf->code != T_FIXEDSTRING;
Branch: 2 branches
BranchTrueFalseTotal
int numeric = leaf->code != T_STRING && leaf->code != T_FIXEDSTRING; 10 35 45
int numeric = leaf->code != T_STRING && leaf->code != T_FIXEDSTRING; 9 1 10
1109 Buffer key_buf;
1110 45 ▶ if (numeric) buf_init(aTHX_ &key_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (numeric) buf_init(aTHX_ &key_buf); 9 36 45
1111  
1112 45 HV *seen = (HV *)sv_2mortal((SV *)newHV());
1113 45 AV *dict_av = (AV *)sv_2mortal((SV *)newAV());
1114  
1115 /* For FixedString(N) inner, dedup by the canonical N-byte form
1116 * (truncate or zero-pad) so inputs of different length that
1117 * encode to the same wire bytes collapse into one dict slot. */
1118 45 ▶ int fixed_n = (leaf->code == T_FIXEDSTRING) ? leaf->param : 0;
Branch: 1 branch
BranchTrueFalseTotal
int fixed_n = (leaf->code == T_FIXEDSTRING) ? leaf->param : 0; 1 44 45
1119 45 SV *canon_holder = NULL;
1120 45 ▶ if (fixed_n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fixed_n > 0) { 1 44 45
1121 1 canon_holder = sv_2mortal(newSV(fixed_n));
1122 1 SvPOK_only(canon_holder);
1123 1 SvCUR_set(canon_holder, fixed_n);
1124 }
1125  
1126 /* Reserve dict slot 0. For Nullable inner this slot is the NULL
1127 * sentinel and must not be reused for the literal default value;
1128 * for non-Nullable it IS the default (empty / N-zero-bytes). */
1129 45 ▶ av_push(dict_av, numeric ? newSVsv(make_null_placeholder(aTHX_ leaf))
Branch: 1 branch
BranchTrueFalseTotal
av_push(dict_av, numeric ? newSVsv(make_null_placeholder(aTHX_ leaf)) 9 36 45
1130 : newSVpvn("", 0));
1131 45 ▶ if (!is_null) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_null) { 26 19 45
1132 26 ▶ if (numeric) {
Branch: 1 branch
BranchTrueFalseTotal
if (numeric) { 2 24 26
1133 2 encode_scalar(aTHX_ &key_buf, *av_fetch(dict_av, 0, 0), leaf);
1134 2 hv_store(seen, key_buf.ptr, key_buf.len, newSViv(0), 0);
1135 24 ▶ } else if (fixed_n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (fixed_n > 0) { 1 23 24
1136 1 char *cz = SvPVX(canon_holder);
1137 1 memset(cz, 0, fixed_n);
1138 1 hv_store(seen, cz, fixed_n, newSViv(0), 0);
1139 } else {
1140 23 hv_store(seen, "", 0, newSViv(0), 0);
1141 }
1142 }
1143  
1144 /* Pre-allocate index buffer. */
1145 45 STRLEN idx_buf_len = num_rows * sizeof(uint64_t);
1146 45 SV *idx_sv = sv_2mortal(newSV(idx_buf_len));
1147 45 SvPOK_only(idx_sv);
1148 45 SvCUR_set(idx_sv, idx_buf_len);
1149 45 uint64_t *indices = (uint64_t *)SvPVX(idx_sv);
1150  
1151 67509 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 67466 43 67509
1152 67466 SV *val = values[r];
1153 67466 ▶ if (is_null && nullable_is_null(val, inner)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_null && nullable_is_null(val, inner)) { 361 67105 67466
if (is_null && nullable_is_null(val, inner)) { 22 339 361
1154 22 indices[r] = 0;
1155 22 continue;
1156 }
1157 67444 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 3 67441 67444
1158 /* Nullable: slot 0 is the NULL sentinel. Non-nullable:
1159 * coerce undef to empty string (matches plain String
1160 * behaviour) without invoking SvPV on undef, which
1161 * would emit a "Use of uninitialized value" warning. */
1162 3 ▶ if (is_null) { indices[r] = 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_null) { indices[r] = 0; continue; } 0 3 3
1163 3 val = make_null_placeholder(aTHX_ leaf);
1164 }
1165 67444 STRLEN slen = 0;
1166 134874 ▶ const char *s = numeric ? NULL : SvPV(val, slen);
Branch: 1 branch
BranchTrueFalseTotal
const char *s = numeric ? NULL : SvPV(val, slen); 67430 14 67444
1167 const char *key;
1168 STRLEN klen;
1169 67444 ▶ if (numeric) {
Branch: 1 branch
BranchTrueFalseTotal
if (numeric) { 14 67430 67444
1170 14 key_buf.len = 0;
1171 14 encode_scalar(aTHX_ &key_buf, val, leaf);
1172 12 key = key_buf.ptr;
1173 12 klen = key_buf.len;
1174 67430 ▶ } else if (fixed_n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (fixed_n > 0) { 6 67424 67430
1175 /* Canonicalize to exactly N bytes: truncate or zero-pad. */
1176 6 char *cz = SvPVX(canon_holder);
1177 6 ▶ if (slen >= (STRLEN)fixed_n) {
Branch: 1 branch
BranchTrueFalseTotal
if (slen >= (STRLEN)fixed_n) { 5 1 6
1178 5 memcpy(cz, s, fixed_n);
1179 } else {
1180 1 memcpy(cz, s, slen);
1181 1 memset(cz + slen, 0, fixed_n - slen);
1182 }
1183 6 key = cz;
1184 6 klen = fixed_n;
1185 } else {
1186 67424 key = s;
1187 67424 klen = slen;
1188 }
1189 67442 SV **slot_sv = hv_fetch(seen, key, klen, 0);
1190 67442 ▶ if (slot_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_sv) { 727 66715 67442
1191 727 indices[r] = (uint64_t)SvUV(*slot_sv);
1192 } else {
1193 66715 UV pos = (UV)(av_len(dict_av) + 1);
1194 66715 ▶ av_push(dict_av, numeric ? newSVsv(val) : newSVpvn(key, klen));
Branch: 1 branch
BranchTrueFalseTotal
av_push(dict_av, numeric ? newSVsv(val) : newSVpvn(key, klen)); 12 66703 66715
1195 66715 hv_store(seen, key, klen, newSVuv(pos), 0);
1196 66715 indices[r] = (uint64_t)pos;
1197 }
1198 }
1199  
1200 43 UV dict_count = av_len(dict_av) + 1;
1201 int idx_bytes;
1202 int idx_type;
1203 43 ▶ if (dict_count <= 256) { idx_type = 0; idx_bytes = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (dict_count <= 256) { idx_type = 0; idx_bytes = 1; } 39 4 43
1204 4 ▶ else if (dict_count <= 65536) { idx_type = 1; idx_bytes = 2; }
Branch: 1 branch
BranchTrueFalseTotal
else if (dict_count <= 65536) { idx_type = 1; idx_bytes = 2; } 4 0 4
1205 0 ▶ else if (dict_count <= 0xFFFFFFFF) { idx_type = 2; idx_bytes = 4; }
Branch: 1 branch
BranchTrueFalseTotal
else if (dict_count <= 0xFFFFFFFF) { idx_type = 2; idx_bytes = 4; } 0 0 0
1206 0 else { idx_type = 3; idx_bytes = 8; }
1207  
1208 43 uint64_t flags = (uint64_t)idx_type | (1ULL << 9); /* HasAdditionalKeys */
1209  
1210 43 buf_le64(aTHX_ b, flags);
1211 43 buf_le64(aTHX_ b, (uint64_t)dict_count);
1212  
1213 /* Serialize the dict using the leaf type. */
1214 43 SV **dict_vals = alloc_sv_array(aTHX_ (SSize_t)dict_count);
1215 SSize_t di;
1216 66801 ▶ for (di = 0; di < (SSize_t)dict_count; di++) {
Branch: 1 branch
BranchTrueFalseTotal
for (di = 0; di < (SSize_t)dict_count; di++) { 66758 43 66801
1217 66758 SV **e = av_fetch(dict_av, di, 0);
1218 66758 ▶ dict_vals[di] = e ? *e : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
dict_vals[di] = e ? *e : &PL_sv_undef; 66758 0 66758
1219 }
1220 43 encode_column_data(aTHX_ b, prefix, dict_vals, (SSize_t)dict_count, leaf);
1221  
1222 43 buf_le64(aTHX_ b, (uint64_t)num_rows);
1223  
1224 67507 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 67464 43 67507
1225 67464 uint64_t v = indices[r];
1226 67464 switch (idx_bytes) {
1227 371 case 1: buf_byte(aTHX_ b, (uint8_t)v); break;
1228 67093 case 2: buf_le16(aTHX_ b, (uint16_t)v); break;
1229 0 case 4: buf_le32(aTHX_ b, (uint32_t)v); break;
1230 0 case 8: buf_le64(aTHX_ b, v); break;
1231 }
1232 }
1233 43 break;
1234 }
1235  
1236 241 case T_JSON: {
1237 /* Per doc/json-research/README.md (validated byte-for-byte
1238 * against ClickHouse 26.3). Wire layout (V1):
1239 * Object prefix: UInt64=0, varint K (twice for V1),
1240 * K * lenstr path -- K = dynamic paths
1241 * For each typed path (sorted): inner prefix (empty for simple types)
1242 * For each dynamic path: Dynamic prefix (UInt64=1, varint T x2,
1243 * T * lenstr type-name, UInt64=0)
1244 * SharedData prefix: empty for MAP version
1245 * For each typed path (sorted): inner column data
1246 * For each dynamic path: N disc bytes + per-variant data
1247 * Shared data: N * UInt64 LE zero
1248 *
1249 * t->tuple_len > 0 means JSON(name Type, ...) was declared:
1250 * those paths emit as regular columns, the rest as Dynamic.
1251 */
1252  
1253 /* Step 1: flatten each row's hashref to dotted-path leaves.
1254 * Typed paths whose declared inner type is itself a hash
1255 * shape (Map / Tuple) must not be recursed into during
1256 * flatten - the user's nested hash for those is the leaf
1257 * value, not a sub-object. Build a stop-set of those names
1258 * and pass it to flatten so it stops recursing at them. */
1259 241 HV *stop_paths = NULL;
1260 241 ▶ if (t->tuple_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_len > 0) { 15 226 241
1261 15 stop_paths = (HV*)sv_2mortal((SV*)newHV());
1262 int sk;
1263 35 ▶ for (sk = 0; sk < t->tuple_len; sk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sk = 0; sk < t->tuple_len; sk++) { 20 15 35
1264 /* Unwrap Nullable so JSON(x Nullable(Map(...))) also
1265 * stops at "x" rather than flattening the inner
1266 * hash and losing the user's data. */
1267 20 TypeInfo *inner = t->tuple[sk];
1268 20 ▶ if (inner->code == T_NULLABLE) inner = inner->inner;
Branch: 1 branch
BranchTrueFalseTotal
if (inner->code == T_NULLABLE) inner = inner->inner; 2 18 20
1269 20 ▶ if (inner->code == T_MAP || inner->code == T_TUPLE) {
Branch: 2 branches
BranchTrueFalseTotal
if (inner->code == T_MAP || inner->code == T_TUPLE) { 17 3 20
if (inner->code == T_MAP || inner->code == T_TUPLE) { 0 17 17
1270 3 STRLEN nlen = strlen(t->tuple_names[sk]);
1271 3 hv_store(stop_paths, t->tuple_names[sk],
1272 (I32)nlen, newSViv(1), 0);
1273 }
1274 }
1275 }
1276  
1277 241 HV **row_hvs = NULL;
1278 241 ▶ if (num_rows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_rows > 0) { 240 1 241
1279 240 ▶ Newxz(row_hvs, num_rows, HV*);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(row_hvs, num_rows, HV*); 0 240 240
1280 240 SAVEFREEPV(row_hvs);
1281 }
1282 241 HV *all_paths = (HV*)sv_2mortal((SV*)newHV());
1283  
1284 1985 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 1749 236 1985
1285 1749 SV *val = values[r];
1286 1749 ▶ if (!SvOK(val)) { row_hvs[r] = NULL; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { row_hvs[r] = NULL; continue; } 141 1608 1749
1287 1608 ▶ if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 1607 1 1608
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 0 1607 1607
1288 1 croak("JSON column row %" IVdf
1289 ": must be hashref or undef", (IV)r);
1290 1607 HV *flat = (HV*)sv_2mortal((SV*)newHV());
1291 1607 flatten_json_hash(aTHX_ (HV*)SvRV(val), NULL, 0, flat,
1292 stop_paths, 0);
1293 1603 row_hvs[r] = flat;
1294 }
1295  
1296 /* Step 1b: if typed paths are declared, extract their values
1297 * per row from each flat HV (and hv_delete to keep them out
1298 * of the dynamic-paths discovery below). For missing keys
1299 * we substitute a type-appropriate placeholder: undef for
1300 * Nullable/Variant, [] for Array/Map, "" for String, 0 for
1301 * numerics. The inner encoder then emits the default
1302 * bytes the same way it would for any other column. */
1303 236 SV ***typed_vals = NULL;
1304 236 int n_typed = t->tuple_len;
1305 236 ▶ if (n_typed > 0 && num_rows > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (n_typed > 0 && num_rows > 0) { 15 221 236
if (n_typed > 0 && num_rows > 0) { 15 0 15
1306 15 Newxz(typed_vals, n_typed, SV **);
1307 15 SAVEFREEPV(typed_vals);
1308 int tp;
1309 35 ▶ for (tp = 0; tp < n_typed; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) { 20 15 35
1310 20 typed_vals[tp] = alloc_sv_array(aTHX_ num_rows);
1311 20 STRLEN nlen = strlen(t->tuple_names[tp]);
1312 40 SV *missing = (t->tuple[tp]->code == T_NULLABLE)
1313 ? &PL_sv_undef
1314 20 ▶ : make_null_placeholder(aTHX_ t->tuple[tp]);
Branch: 1 branch
BranchTrueFalseTotal
: make_null_placeholder(aTHX_ t->tuple[tp]); 18 2 20
1315 58 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 38 20 58
1316 38 ▶ if (!row_hvs[r]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[r]) { 0 38 38
1317 0 typed_vals[tp][r] = missing;
1318 0 continue;
1319 }
1320 38 SV **e = hv_fetch(row_hvs[r], t->tuple_names[tp],
1321 (I32)nlen, 0);
1322 38 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 32 6 38
1323 32 ▶ typed_vals[tp][r] = SvOK(*e) ? *e : missing;
Branch: 1 branch
BranchTrueFalseTotal
typed_vals[tp][r] = SvOK(*e) ? *e : missing; 29 3 32
1324 /* hv_delete unconditionally on key presence
1325 * so an explicit `undef` value doesn't leak
1326 * into the dynamic-paths discovery below
1327 * (would double-emit the path). */
1328 32 hv_delete(row_hvs[r], t->tuple_names[tp],
1329 (I32)nlen, G_DISCARD);
1330 } else {
1331 6 typed_vals[tp][r] = missing;
1332 }
1333 }
1334 }
1335 }
1336  
1337 /* Step 1c: union remaining dynamic-path keys across rows. */
1338 1980 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 1744 236 1980
1339 1744 ▶ if (!row_hvs[r]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[r]) continue; 141 1603 1744
1340 1603 hv_iterinit(row_hvs[r]);
1341 HE *he;
1342 4488 ▶ while ((he = hv_iternext(row_hvs[r]))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(row_hvs[r]))) { 2885 1603 4488
1343 I32 klen;
1344 2885 char *kstr = hv_iterkey(he, &klen);
1345 2885 SV **e = hv_fetch(row_hvs[r], kstr, klen, 0);
1346 2885 ▶ if (e && SvOK(*e))
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) 2885 0 2885
if (e && SvOK(*e)) 2674 211 2885
1347 2674 hv_store(all_paths, kstr, klen, newSViv(1), 0);
1348 }
1349 }
1350  
1351 /* Step 2: collect & sort path names. Snapshot (ptr, len) into
1352 * a paired struct array so the sort preserves the original
1353 * length even if a key contains an embedded NUL byte (legal
1354 * in Perl HVs; strlen() after sort would silently truncate). */
1355 236 int num_paths = 0;
1356 236 char **paths = NULL;
1357 236 STRLEN *path_lens = NULL;
1358 {
1359 236 ▶ int total = (int)HvUSEDKEYS(all_paths);
Branch: 1 branch
BranchTrueFalseTotal
int total = (int)HvUSEDKEYS(all_paths); 0 236 236
1360 236 ▶ if (total > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (total > 0) { 221 15 236
1361 221 Newx(paths, total, char*);
1362 221 SAVEFREEPV(paths);
1363 221 Newx(path_lens, total, STRLEN);
1364 221 SAVEFREEPV(path_lens);
1365 PathEntry *pe;
1366 221 Newx(pe, total, PathEntry);
1367 221 SAVEFREEPV(pe);
1368 221 hv_iterinit(all_paths);
1369 HE *he;
1370 2312 ▶ while ((he = hv_iternext(all_paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(all_paths))) { 2091 221 2312
1371 I32 klen;
1372 2091 char *kstr = hv_iterkey(he, &klen);
1373 2091 pe[num_paths].path = kstr;
1374 2091 pe[num_paths].len = (STRLEN)klen;
1375 2091 num_paths++;
1376 }
1377 221 ▶ if (num_paths > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 1) 192 29 221
1378 192 qsort(pe, num_paths, sizeof(*pe), cmp_path_entry);
1379 int pi;
1380 2312 ▶ for (pi = 0; pi < num_paths; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < num_paths; pi++) { 2091 221 2312
1381 2091 paths[pi] = pe[pi].path;
1382 2091 path_lens[pi] = pe[pi].len;
1383 }
1384 }
1385 }
1386  
1387 /* Step 3: per-path kind mask. Walk each leaf; scalars and
1388 * recognized bool refs classify directly; arrayrefs classify
1389 * via json_classify_array (homogeneous element kind). Hash
1390 * refs or heterogeneous arrays fail loud with location. */
1391 236 unsigned *kind_masks = NULL;
1392 236 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 221 15 236
1393 221 Newxz(kind_masks, num_paths, unsigned);
1394 221 SAVEFREEPV(kind_masks);
1395 int p;
1396 2311 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 2091 220 2311
1397 23688 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 21598 2090 23688
1398 21598 ▶ if (!row_hvs[r]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[r]) continue; 1401 20197 21598
1399 20197 SV **e = hv_fetch(row_hvs[r], paths[p],
1400 (I32)path_lens[p], 0);
1401 20197 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 2678 17519 20197
if (!e || !SvOK(*e)) continue; 4 2674 2678
1402 2674 ▶ if (SvROK(*e) && !json_is_bool_ref(aTHX_ *e)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*e) && !json_is_bool_ref(aTHX_ *e) 1076 1598 2674
if (SvROK(*e) && !json_is_bool_ref(aTHX_ *e) 849 227 1076
1403 849 ▶ && SvTYPE(SvRV(*e)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*e)) != SVt_PVAV) 0 849 849
1404 0 croak("JSON column row %" IVdf
1405 " path '%s': "
1406 "hash refs as leaves are not "
1407 "supported (already flattened); "
1408 "got %s",
1409 (IV)r, paths[p],
1410 sv_reftype(SvRV(*e), 0));
1411 2674 int k = json_classify_value(aTHX_ *e);
1412 2674 ▶ if (k < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) 1 2673 2674
1413 1 croak("JSON column row %" IVdf " path '%s': "
1414 "heterogeneous or unsupported array "
1415 "(elements must all be int, float, "
1416 "bool, or string)",
1417 (IV)r, paths[p]);
1418 2673 kind_masks[p] |= 1u << k;
1419 }
1420 }
1421 }
1422  
1423 /* Step 4: emit Object structure prefix (V1). */
1424 235 buf_le64(aTHX_ prefix, 0); /* Object V1 */
1425 235 buf_varint(aTHX_ prefix, (UV)num_paths); /* max_dynamic_paths */
1426 235 buf_varint(aTHX_ prefix, (UV)num_paths); /* actual count */
1427 int p;
1428 2325 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 2090 235 2325
1429 2090 buf_string(aTHX_ prefix, paths[p], path_lens[p]);
1430  
1431 /* Per-path wire-slot tables. path_slots[p*5+s] is the lex-ordered
1432 * slot at index s (kind index or -1 for SharedVariant);
1433 * wire_slot_counts[p] is the slot count for path p. Computed
1434 * once, used by both prefix and data loops below. */
1435 235 int *path_slots = NULL;
1436 235 int *wire_slot_counts = NULL;
1437 235 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 220 15 235
1438 220 Newx(path_slots, num_paths * JSON_LEX_SLOTS, int);
1439 220 SAVEFREEPV(path_slots);
1440 220 Newx(wire_slot_counts, num_paths, int);
1441 220 SAVEFREEPV(wire_slot_counts);
1442 2310 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 2090 220 2310
1443 2090 wire_slot_counts[p] =
1444 2090 json_build_lex_table(kind_masks[p], path_slots + p*JSON_LEX_SLOTS);
1445 }
1446  
1447 /* Step 5: per-path Dynamic V1 prefix + Variant mode. */
1448 2325 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 2090 235 2325
1449 2090 int wire_slots = wire_slot_counts[p];
1450 2090 int kc = wire_slots - 1; /* minus SharedVariant */
1451  
1452 2090 buf_le64(aTHX_ prefix, 1); /* Dynamic V1 */
1453 2090 buf_varint(aTHX_ prefix, (UV)kc); /* max_dynamic_types */
1454 2090 buf_varint(aTHX_ prefix, (UV)kc); /* actual count */
1455  
1456 /* User variant type names in lex order (skip SharedVariant). */
1457 2090 int *slots = path_slots + p*JSON_LEX_SLOTS;
1458 int s;
1459 6562 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 4472 2090 6562
1460 4472 int k = slots[s];
1461 4472 ▶ if (k < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) continue; 2090 2382 4472
1462 2382 const char *nm = json_kind_type_name[k];
1463 2382 buf_string(aTHX_ prefix, nm, strlen(nm));
1464 }
1465 2090 buf_le64(aTHX_ prefix, 0); /* Variant BASIC */
1466 }
1467  
1468 /* Step 5b: emit typed-path column data (sorted by name).
1469 * Typed paths come before dynamic-path Variant data on the
1470 * wire; their inner types are simple-prefix so encode_column
1471 * emits only the column body. typed_vals is only allocated
1472 * when both n_typed and num_rows are positive; skip when
1473 * num_rows == 0 to avoid dereferencing a NULL outer array. */
1474 235 ▶ if (n_typed > 0 && num_rows > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (n_typed > 0 && num_rows > 0) { 15 220 235
if (n_typed > 0 && num_rows > 0) { 15 0 15
1475 int tp;
1476 34 ▶ for (tp = 0; tp < n_typed; tp++)
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) 20 14 34
1477 20 encode_column_data(aTHX_ b, prefix, typed_vals[tp], num_rows,
1478 20 t->tuple[tp]);
1479 }
1480  
1481 /* Step 6: per-path Variant data (discs + per-variant values).
1482 * For Array(T) variants the column's wire layout is:
1483 * N UInt64 LE offsets (cumulative element counts)
1484 * <inner-type values concatenated>
1485 * We emit offsets and inner data in a single pass per variant. */
1486 2324 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 2090 234 2324
1487 2090 int wire_slots = wire_slot_counts[p];
1488 2090 int *slots = path_slots + p*JSON_LEX_SLOTS;
1489  
1490 /* Discriminator byte per row. */
1491 23687 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 21597 2090 23687
1492 21597 ▶ if (!row_hvs[r]) { buf_byte(aTHX_ b, 0xff); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[r]) { buf_byte(aTHX_ b, 0xff); continue; } 1401 20196 21597
1493 20196 SV **e = hv_fetch(row_hvs[r], paths[p],
1494 (I32)path_lens[p], 0);
1495 20196 ▶ if (!e || !SvOK(*e)) { buf_byte(aTHX_ b, 0xff); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) { buf_byte(aTHX_ b, 0xff); continue; } 2677 17519 20196
if (!e || !SvOK(*e)) { buf_byte(aTHX_ b, 0xff); continue; } 4 2673 2677
1496 2673 int k = json_classify_value(aTHX_ *e);
1497 2673 buf_byte(aTHX_ b,
1498 2673 (uint8_t)json_kind_disc_in(k, slots, wire_slots));
1499 }
1500  
1501 /* Per-variant data in lex order. SharedVariant has zero
1502 * rows in our encoder's output. */
1503 int s;
1504 6562 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 4472 2090 6562
1505 4472 int k_match = slots[s];
1506 4472 ▶ if (k_match < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k_match < 0) continue; 2090 2382 4472
1507  
1508 /* Array(T) variants: first pass emits offsets (so the
1509 * downstream offset cursor is contiguous) and counts
1510 * total elements; second pass emits inner values. */
1511 2382 int is_array = (k_match >= JV_ARRAY_BOOL
1512 2382 ▶ && k_match <= JV_ARRAY_STRING);
Branch: 2 branches
BranchTrueFalseTotal
&& k_match <= JV_ARRAY_STRING); 2382 0 2382
&& k_match <= JV_ARRAY_STRING); 847 1535 2382
1513 2382 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 847 1535 2382
1514 847 uint64_t offset = 0;
1515 9980 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 9133 847 9980
1516 9133 ▶ if (!row_hvs[r]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[r]) continue; 560 8573 9133
1517 8573 SV **e = hv_fetch(row_hvs[r], paths[p],
1518 (I32)path_lens[p], 0);
1519 8573 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 857 7716 8573
if (!e || !SvOK(*e)) continue; 2 855 857
1520 855 ▶ if (json_classify_value(aTHX_ *e) != k_match)
Branch: 1 branch
BranchTrueFalseTotal
if (json_classify_value(aTHX_ *e) != k_match) 7 848 855
1521 7 continue;
1522 848 AV *av = (AV*)SvRV(*e);
1523 848 SSize_t n = av_len(av) + 1;
1524 848 offset += (uint64_t)n;
1525 848 buf_le64(aTHX_ b, offset);
1526 }
1527 }
1528  
1529 /* Element-value pass (Array(T)) or scalar pass (T). */
1530 26856 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 24474 2382 26856
1531 24474 ▶ if (!row_hvs[r]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[r]) continue; 1681 22793 24474
1532 22793 SV **e = hv_fetch(row_hvs[r], paths[p],
1533 (I32)path_lens[p], 0);
1534 22793 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 3548 19245 22793
if (!e || !SvOK(*e)) continue; 5 3543 3548
1535 3543 ▶ if (json_classify_value(aTHX_ *e) != k_match) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (json_classify_value(aTHX_ *e) != k_match) continue; 870 2673 3543
1536  
1537 2673 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 848 1825 2673
1538 848 AV *av = (AV*)SvRV(*e);
1539 848 SSize_t n = av_len(av) + 1, i;
1540 2845 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1997 848 2845
1541 1997 SV **elem = av_fetch(av, i, 0);
1542 1997 ▶ SV *ev = (elem && SvOK(*elem))
Branch: 1 branch
BranchTrueFalseTotal
SV *ev = (elem && SvOK(*elem)) 1997 0 1997
1543 3994 ▶ ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
? *elem : &PL_sv_undef; 1997 0 1997
1544 1997 json_emit_array_elem(aTHX_ b, ev, k_match);
1545 }
1546 } else {
1547 1825 json_emit_scalar(aTHX_ b, *e, k_match);
1548 }
1549 }
1550 }
1551 }
1552  
1553 /* Step 7: shared data trailer (Array(Tuple(String,String))
1554 * with all rows empty -> N UInt64 LE zero offsets). */
1555 234 ▶ if (num_rows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_rows > 0) { 233 1 234
1556 233 STRLEN nbytes = (STRLEN)num_rows * 8;
1557 233 buf_grow(aTHX_ b, nbytes);
1558 233 memset(b->ptr + b->len, 0, nbytes);
1559 233 b->len += nbytes;
1560 }
1561 234 break;
1562 }
1563  
1564 13 case T_DYNAMIC: {
1565 /* Standalone Dynamic column: same wire format as one JSON
1566 * path's Dynamic sub-column (Dynamic V1 prefix + Variant
1567 * mode + Variant data) with no Object wrapping or shared
1568 * data trailer. Each row is a scalar / array / undef. */
1569 13 unsigned mask = 0;
1570 13 int *row_kinds = NULL;
1571 13 ▶ if (num_rows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_rows > 0) { 13 0 13
1572 13 ▶ Newx(row_kinds, num_rows, int);
Branch: 1 branch
BranchTrueFalseTotal
Newx(row_kinds, num_rows, int); 0 13 13
1573 13 SAVEFREEPV(row_kinds);
1574 }
1575 51 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 39 12 51
1576 39 SV *val = values[r];
1577 39 ▶ if (!SvOK(val)) { row_kinds[r] = -1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { row_kinds[r] = -1; continue; } 10 29 39
1578 29 ▶ if (SvROK(val) && !json_is_bool_ref(aTHX_ val)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && !json_is_bool_ref(aTHX_ val) 6 23 29
if (SvROK(val) && !json_is_bool_ref(aTHX_ val) 5 1 6
1579 5 ▶ && SvTYPE(SvRV(val)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(val)) != SVt_PVAV) 1 4 5
1580 1 croak("Dynamic row %" IVdf ": hash refs are not "
1581 "supported; use JSON column instead "
1582 "(got %s)", (IV)r, sv_reftype(SvRV(val), 0));
1583 28 int k = json_classify_value(aTHX_ val);
1584 28 ▶ if (k < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) 0 28 28
1585 0 croak("Dynamic row %" IVdf ": heterogeneous or "
1586 "unsupported array", (IV)r);
1587 28 row_kinds[r] = k;
1588 28 mask |= 1u << k;
1589 }
1590  
1591 int slots[JSON_LEX_SLOTS];
1592 12 int wire_slots = json_build_lex_table(mask, slots);
1593 12 int kc = wire_slots - 1;
1594  
1595 12 buf_le64(aTHX_ prefix, 1); /* Dynamic V1 */
1596 12 buf_varint(aTHX_ prefix, (UV)kc); /* max_dynamic_types */
1597 12 buf_varint(aTHX_ prefix, (UV)kc); /* actual count */
1598 int s;
1599 52 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 40 12 52
1600 40 int k = slots[s];
1601 40 ▶ if (k < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) continue; 12 28 40
1602 28 const char *nm = json_kind_type_name[k];
1603 28 buf_string(aTHX_ prefix, nm, strlen(nm));
1604 }
1605 12 buf_le64(aTHX_ prefix, 0); /* Variant BASIC */
1606  
1607 /* Discriminator byte per row. */
1608 50 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 38 12 50
1609 38 ▶ if (row_kinds[r] < 0) { buf_byte(aTHX_ b, 0xff); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (row_kinds[r] < 0) { buf_byte(aTHX_ b, 0xff); continue; } 10 28 38
1610 28 buf_byte(aTHX_ b,
1611 28 (uint8_t)json_kind_disc_in(row_kinds[r],
1612 slots, wire_slots));
1613 }
1614  
1615 /* Per-variant data in lex order. */
1616 52 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 40 12 52
1617 40 int k_match = slots[s];
1618 40 ▶ if (k_match < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k_match < 0) continue; 12 28 40
1619 28 int is_array = (k_match >= JV_ARRAY_BOOL
1620 28 ▶ && k_match <= JV_ARRAY_STRING);
Branch: 2 branches
BranchTrueFalseTotal
&& k_match <= JV_ARRAY_STRING); 28 0 28
&& k_match <= JV_ARRAY_STRING); 4 24 28
1621  
1622 28 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 4 24 28
1623 4 uint64_t offset = 0;
1624 18 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 14 4 18
1625 14 ▶ if (row_kinds[r] != k_match) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (row_kinds[r] != k_match) continue; 10 4 14
1626 4 AV *av = (AV*)SvRV(values[r]);
1627 4 offset += (uint64_t)(av_len(av) + 1);
1628 4 buf_le64(aTHX_ b, offset);
1629 }
1630 18 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 14 4 18
1631 14 ▶ if (row_kinds[r] != k_match) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (row_kinds[r] != k_match) continue; 10 4 14
1632 4 AV *av = (AV*)SvRV(values[r]);
1633 4 SSize_t n = av_len(av) + 1, i;
1634 13 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 4 13
1635 9 SV **elem = av_fetch(av, i, 0);
1636 9 ▶ SV *ev = (elem && SvOK(*elem))
Branch: 1 branch
BranchTrueFalseTotal
SV *ev = (elem && SvOK(*elem)) 9 0 9
1637 18 ▶ ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
? *elem : &PL_sv_undef; 9 0 9
1638 9 json_emit_array_elem(aTHX_ b, ev, k_match);
1639 }
1640 }
1641 } else {
1642 106 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 82 24 106
1643 82 ▶ if (row_kinds[r] != k_match) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (row_kinds[r] != k_match) continue; 58 24 82
1644 24 json_emit_scalar(aTHX_ b, values[r], k_match);
1645 }
1646 }
1647 }
1648 12 break;
1649 }
1650  
1651 0 default:
1652 0 croak("encode_column: unhandled type code %d", t->code);
1653 }
1654 5599 }
« decimal.c ↑ Summary runtime.c »