xs/types.c

filter: Statementstmt 10.5% 432 / 4100 Branchbran 5.4% 222 / 4062 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 8.0% 654 / 8162 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/ClickHouse.pm ↑ Summary xs/proto_native_parse.c »
1 /* --- JSON column helpers (ported from Clickhouse::Encoder) ---
2 * ClickHouse's stable JSON / Object('json') type (24.8+). Native wire
3 * layout (V1+V2+V3) is documented in Clickhouse-Encoder/doc/json-research.
4 * The encoder accepts a Perl hashref per row (auto-flattened to dotted
5 * paths) and the decoder returns the same shape on the way back.
6 * Supported leaf kinds: Int64, UInt64, Float64, Bool, String,
7 * Array(<those>).
8 */
9 typedef enum {
10 JV_ARRAY_BOOL = 0,
11 JV_ARRAY_FLOAT64,
12 JV_ARRAY_INT64,
13 JV_ARRAY_STRING,
14 JV_ARRAY_UINT64,
15 JV_BOOL,
16 JV_FLOAT64,
17 JV_INT64,
18 JV_STRING,
19 JV_UINT64,
20 JV_KIND_COUNT
21 } json_kind_t;
22  
23 /* Lex-sort position (0..N) for each kind in the variant list with
24 * "SharedVariant" inserted. Sorted order:
25 * "Array(Bool)" < "Array(Float64)" < "Array(Int64)" < "Array(String)"
26 * < "Array(UInt64)" < "Bool" < "Float64" < "Int64" < "SharedVariant"
27 * < "String" < "UInt64".
28 * SharedVariant takes lex pos 8. */
29 static const int json_kind_to_lex_pos[JV_KIND_COUNT] = {
30 /* JV_ARRAY_BOOL */ 0,
31 /* JV_ARRAY_FLOAT64 */ 1,
32 /* JV_ARRAY_INT64 */ 2,
33 /* JV_ARRAY_STRING */ 3,
34 /* JV_ARRAY_UINT64 */ 4,
35 /* JV_BOOL */ 5,
36 /* JV_FLOAT64 */ 6,
37 /* JV_INT64 */ 7,
38 /* JV_STRING */ 9,
39 /* JV_UINT64 */ 10
40 };
41  
42 static const char * const json_kind_type_name[JV_KIND_COUNT] = {
43 "Array(Bool)", "Array(Float64)", "Array(Int64)", "Array(String)",
44 "Array(UInt64)",
45 "Bool", "Float64", "Int64", "String", "UInt64"
46 };
47  
48 #define JSON_SHAREDVARIANT_LEX_POS 8
49 #define JSON_LEX_SLOTS 11
50  
51 /* Build the ordered wire-slot table: slots[disc] = kind (-1 for SharedVariant),
52 * skipping kinds absent from mask. Returns the total wire-slot count
53 * (present user kinds + 1 for the always-present SharedVariant slot). */
54 0 static int json_build_lex_table(unsigned mask, int slots[JSON_LEX_SLOTS]) {
55 0 int n = 0, lex;
56 0 ▶ for (lex = 0; lex < JSON_LEX_SLOTS; lex++) {
Branch: 1 branch
BranchTrueFalseTotal
for (lex = 0; lex < JSON_LEX_SLOTS; lex++) { 0 0 0
57 0 ▶ if (lex == JSON_SHAREDVARIANT_LEX_POS) { slots[n++] = -1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (lex == JSON_SHAREDVARIANT_LEX_POS) { slots[n++] = -1; continue; } 0 0 0
58 int k;
59 0 ▶ for (k = 0; k < JV_KIND_COUNT; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < JV_KIND_COUNT; k++) { 0 0 0
60 0 ▶ if (json_kind_to_lex_pos[k] == lex && (mask & (1u << k))) {
Branch: 2 branches
BranchTrueFalseTotal
if (json_kind_to_lex_pos[k] == lex && (mask & (1u << k))) { 0 0 0
if (json_kind_to_lex_pos[k] == lex && (mask & (1u << k))) { 0 0 0
61 0 slots[n++] = k;
62 0 break;
63 }
64 }
65 }
66 0 return n;
67 }
68  
69 0 static int json_kind_disc_in(int kind, const int slots[JSON_LEX_SLOTS], int n) {
70 int i;
71 0 ▶ for (i = 0; i < n; i++) if (slots[i] == kind) return i;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) if (slots[i] == kind) return i; 0 0 0
for (i = 0; i < n; i++) if (slots[i] == kind) return i; 0 0 0
72 0 return -1;
73 }
74  
75 0 static int json_pkg_is_bool(const char *pkg) {
76 0 ▶ return pkg && (strcmp(pkg, "JSON::PP::Boolean") == 0
Branch: 2 branches
BranchTrueFalseTotal
return pkg && (strcmp(pkg, "JSON::PP::Boolean") == 0 0 0 0
return pkg && (strcmp(pkg, "JSON::PP::Boolean") == 0 0 0 0
77 0 ▶ || strcmp(pkg, "Types::Serialiser::Boolean") == 0
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "Types::Serialiser::Boolean") == 0 0 0 0
78 0 ▶ || strcmp(pkg, "JSON::XS::Boolean") == 0
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "JSON::XS::Boolean") == 0 0 0 0
79 0 ▶ || strcmp(pkg, "Cpanel::JSON::XS::Boolean") == 0
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "Cpanel::JSON::XS::Boolean") == 0 0 0 0
80 0 ▶ || strcmp(pkg, "boolean") == 0);
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(pkg, "boolean") == 0); 0 0 0
81 }
82  
83 0 static int json_is_bool_ref(pTHX_ SV *val) {
84 0 ▶ if (!(SvROK(val) && sv_isobject(val))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(val) && sv_isobject(val))) return 0; 0 0 0
if (!(SvROK(val) && sv_isobject(val))) return 0; 0 0 0
85 0 HV *stash = SvSTASH(SvRV(val));
86 0 ▶ return stash && json_pkg_is_bool(HvNAME(stash));
Branch: 8 branches
BranchTrueFalseTotal
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
return stash && json_pkg_is_bool(HvNAME(stash)); 0 0 0
87 }
88  
89 /* For an arrayref leaf, infer the element kind. Returns the matching
90 * JV_ARRAY_* kind, or -1 if heterogeneous / unsupported. The two int
91 * flavors unify: all-non-negative with a big UV becomes UInt64, while
92 * a negative next to a big UV is unrepresentable and fails. */
93 0 static int json_classify_array(pTHX_ AV *av) {
94 0 SSize_t n = av_len(av) + 1, i;
95 0 int seen_kind = -1;
96 0 int seen_uint = 0, seen_neg = 0;
97 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
98 0 SV **e = av_fetch(av, i, 0);
99 0 ▶ if (e && *e && SvGMAGICAL(*e)) mg_get(*e);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvGMAGICAL(*e)) mg_get(*e); 0 0 0
if (e && *e && SvGMAGICAL(*e)) mg_get(*e); 0 0 0
if (e && *e && SvGMAGICAL(*e)) mg_get(*e); 0 0 0
100 0 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 0 0 0
if (!e || !SvOK(*e)) continue; 0 0 0
101 int leaf;
102 0 ▶ if (SvROK(*e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(*e)) { 0 0 0
103 0 ▶ if (json_is_bool_ref(aTHX_ *e)) leaf = JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (json_is_bool_ref(aTHX_ *e)) leaf = JV_BOOL; 0 0 0
104 0 else return -1; /* nested arrays/hashes not supported in array leaves */
105 }
106 #ifdef SvIsBOOL
107 0 ▶ else if (SvIsBOOL(*e)) leaf = JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIsBOOL(*e)) leaf = JV_BOOL; 0 0 0
108 #endif
109 0 ▶ else if (SvIOK(*e) && !SvNOK(*e)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvIOK(*e) && !SvNOK(*e)) { 0 0 0
else if (SvIOK(*e) && !SvNOK(*e)) { 0 0 0
110 0 ▶ if (SvIsUV(*e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(*e)) { 0 0 0
111 0 ▶ if (SvUVX(*e) > (UV)INT64_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUVX(*e) > (UV)INT64_MAX) { 0 0 0
112 0 leaf = JV_UINT64;
113 0 seen_uint = 1;
114 } else {
115 0 leaf = JV_INT64;
116 }
117 } else {
118 0 leaf = JV_INT64;
119 0 ▶ if (SvIVX(*e) < 0) seen_neg = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvIVX(*e) < 0) seen_neg = 1; 0 0 0
120 }
121 }
122 0 ▶ else if (SvNOK(*e)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(*e)) { 0 0 0
123 0 NV nv = SvNV(*e);
124 0 leaf = (nv == (NV)(int64_t)nv
125 0 ▶ && nv >= (NV)INT64_MIN && nv <= (NV)INT64_MAX)
Branch: 2 branches
BranchTrueFalseTotal
&& nv >= (NV)INT64_MIN && nv <= (NV)INT64_MAX) 0 0 0
&& nv >= (NV)INT64_MIN && nv <= (NV)INT64_MAX) 0 0 0
126 0 ▶ ? JV_INT64 : JV_FLOAT64;
Branch: 1 branch
BranchTrueFalseTotal
? JV_INT64 : JV_FLOAT64; 0 0 0
127 0 ▶ if (leaf == JV_INT64 && nv < 0) seen_neg = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (leaf == JV_INT64 && nv < 0) seen_neg = 1; 0 0 0
if (leaf == JV_INT64 && nv < 0) seen_neg = 1; 0 0 0
128 } else {
129 0 leaf = JV_STRING;
130 }
131 0 ▶ if (leaf == JV_INT64 || leaf == JV_UINT64) {
Branch: 2 branches
BranchTrueFalseTotal
if (leaf == JV_INT64 || leaf == JV_UINT64) { 0 0 0
if (leaf == JV_INT64 || leaf == JV_UINT64) { 0 0 0
132 0 ▶ if (seen_kind == -1) seen_kind = JV_INT64;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_kind == -1) seen_kind = JV_INT64; 0 0 0
133 0 ▶ else if (seen_kind != JV_INT64) return -1;
Branch: 1 branch
BranchTrueFalseTotal
else if (seen_kind != JV_INT64) return -1; 0 0 0
134 } else {
135 0 ▶ if (seen_kind == -1) seen_kind = leaf;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_kind == -1) seen_kind = leaf; 0 0 0
136 0 ▶ else if (seen_kind != leaf) return -1;
Branch: 1 branch
BranchTrueFalseTotal
else if (seen_kind != leaf) return -1; 0 0 0
137 }
138 }
139 0 ▶ if (seen_kind == -1) return JV_ARRAY_INT64; /* empty/all-null */
Branch: 1 branch
BranchTrueFalseTotal
if (seen_kind == -1) return JV_ARRAY_INT64; /* empty/all-null */ 0 0 0
140 0 ▶ if (seen_kind == JV_INT64) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_kind == JV_INT64) { 0 0 0
141 0 ▶ if (seen_neg && seen_uint) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (seen_neg && seen_uint) return -1; 0 0 0
if (seen_neg && seen_uint) return -1; 0 0 0
142 0 ▶ return seen_uint ? JV_ARRAY_UINT64 : JV_ARRAY_INT64;
Branch: 1 branch
BranchTrueFalseTotal
return seen_uint ? JV_ARRAY_UINT64 : JV_ARRAY_INT64; 0 0 0
143 }
144 0 switch (seen_kind) {
145 0 case JV_BOOL: return JV_ARRAY_BOOL;
146 0 case JV_FLOAT64: return JV_ARRAY_FLOAT64;
147 0 case JV_STRING: return JV_ARRAY_STRING;
148 0 default: return -1;
149 }
150 }
151  
152 0 static int json_classify_leaf(pTHX_ SV *val) {
153 0 ▶ if (json_is_bool_ref(aTHX_ val)) return JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (json_is_bool_ref(aTHX_ val)) return JV_BOOL; 0 0 0
154 #ifdef SvIsBOOL
155 0 ▶ if (SvIsBOOL(val)) return JV_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsBOOL(val)) return JV_BOOL; 0 0 0
156 #endif
157 0 ▶ if (SvIOK(val) && !SvNOK(val))
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(val) && !SvNOK(val)) 0 0 0
if (SvIOK(val) && !SvNOK(val)) 0 0 0
158 0 ▶ return (SvIsUV(val) && SvUVX(val) > (UV)INT64_MAX)
Branch: 1 branch
BranchTrueFalseTotal
return (SvIsUV(val) && SvUVX(val) > (UV)INT64_MAX) 0 0 0
159 0 ▶ ? JV_UINT64 : JV_INT64;
Branch: 1 branch
BranchTrueFalseTotal
? JV_UINT64 : JV_INT64; 0 0 0
160 0 ▶ if (SvNOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(val)) { 0 0 0
161 0 NV n = SvNV(val);
162 0 ▶ if (n == (NV)(int64_t)n && n >= (NV)INT64_MIN && n <= (NV)INT64_MAX)
Branch: 3 branches
BranchTrueFalseTotal
if (n == (NV)(int64_t)n && n >= (NV)INT64_MIN && n <= (NV)INT64_MAX) 0 0 0
if (n == (NV)(int64_t)n && n >= (NV)INT64_MIN && n <= (NV)INT64_MAX) 0 0 0
if (n == (NV)(int64_t)n && n >= (NV)INT64_MIN && n <= (NV)INT64_MAX) 0 0 0
163 0 return JV_INT64;
164 0 return JV_FLOAT64;
165 }
166 0 return JV_STRING;
167 }
168  
169 /* Classify any JSON value SV. Routes arrayrefs through json_classify_array
170 * and scalars/Booleans through json_classify_leaf. Returns -1 for an
171 * unsupported reference (hashref, blessed non-Boolean) or for a
172 * heterogeneous array. */
173 0 static int json_classify_value(pTHX_ SV *val) {
174 0 ▶ if (SvROK(val) && !json_is_bool_ref(aTHX_ val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && !json_is_bool_ref(aTHX_ val)) { 0 0 0
if (SvROK(val) && !json_is_bool_ref(aTHX_ val)) { 0 0 0
175 0 ▶ if (SvTYPE(SvRV(val)) != SVt_PVAV) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(val)) != SVt_PVAV) return -1; 0 0 0
176 0 return json_classify_array(aTHX_ (AV*)SvRV(val));
177 }
178 0 return json_classify_leaf(aTHX_ val);
179 }
180  
181 0 static int json_kind_from_type_name(const char *ts, size_t tl) {
182 0 ▶ if (tl == 4 && memcmp(ts, "Bool", 4) == 0) return JV_BOOL;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 4 && memcmp(ts, "Bool", 4) == 0) return JV_BOOL; 0 0 0
if (tl == 4 && memcmp(ts, "Bool", 4) == 0) return JV_BOOL; 0 0 0
183 0 ▶ if (tl == 7 && memcmp(ts, "Float64", 7) == 0) return JV_FLOAT64;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 7 && memcmp(ts, "Float64", 7) == 0) return JV_FLOAT64; 0 0 0
if (tl == 7 && memcmp(ts, "Float64", 7) == 0) return JV_FLOAT64; 0 0 0
184 0 ▶ if (tl == 5 && memcmp(ts, "Int64", 5) == 0) return JV_INT64;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 5 && memcmp(ts, "Int64", 5) == 0) return JV_INT64; 0 0 0
if (tl == 5 && memcmp(ts, "Int64", 5) == 0) return JV_INT64; 0 0 0
185 0 ▶ if (tl == 6 && memcmp(ts, "String", 6) == 0) return JV_STRING;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memcmp(ts, "String", 6) == 0) return JV_STRING; 0 0 0
if (tl == 6 && memcmp(ts, "String", 6) == 0) return JV_STRING; 0 0 0
186 0 ▶ if (tl == 11 && memcmp(ts, "Array(Bool)", 11) == 0) return JV_ARRAY_BOOL;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 11 && memcmp(ts, "Array(Bool)", 11) == 0) return JV_ARRAY_BOOL; 0 0 0
if (tl == 11 && memcmp(ts, "Array(Bool)", 11) == 0) return JV_ARRAY_BOOL; 0 0 0
187 0 ▶ if (tl == 14 && memcmp(ts, "Array(Float64)", 14) == 0) return JV_ARRAY_FLOAT64;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 14 && memcmp(ts, "Array(Float64)", 14) == 0) return JV_ARRAY_FLOAT64; 0 0 0
if (tl == 14 && memcmp(ts, "Array(Float64)", 14) == 0) return JV_ARRAY_FLOAT64; 0 0 0
188 0 ▶ if (tl == 12 && memcmp(ts, "Array(Int64)", 12) == 0) return JV_ARRAY_INT64;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 12 && memcmp(ts, "Array(Int64)", 12) == 0) return JV_ARRAY_INT64; 0 0 0
if (tl == 12 && memcmp(ts, "Array(Int64)", 12) == 0) return JV_ARRAY_INT64; 0 0 0
189 0 ▶ if (tl == 13 && memcmp(ts, "Array(String)", 13) == 0) return JV_ARRAY_STRING;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 13 && memcmp(ts, "Array(String)", 13) == 0) return JV_ARRAY_STRING; 0 0 0
if (tl == 13 && memcmp(ts, "Array(String)", 13) == 0) return JV_ARRAY_STRING; 0 0 0
190 0 ▶ if (tl == 13 && memcmp(ts, "Array(UInt64)", 13) == 0) return JV_ARRAY_UINT64;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 13 && memcmp(ts, "Array(UInt64)", 13) == 0) return JV_ARRAY_UINT64; 0 0 0
if (tl == 13 && memcmp(ts, "Array(UInt64)", 13) == 0) return JV_ARRAY_UINT64; 0 0 0
191 0 ▶ if (tl == 6 && memcmp(ts, "UInt64", 6) == 0) return JV_UINT64;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memcmp(ts, "UInt64", 6) == 0) return JV_UINT64; 0 0 0
if (tl == 6 && memcmp(ts, "UInt64", 6) == 0) return JV_UINT64; 0 0 0
192 0 return -1;
193 }
194  
195 /* Recursively flatten a JSON value hash into a flat HV of dotted-path names. */
196 /* Returns 1 on success, 0 when a value isn't encodable. Never croaks:
197 * the encoder also runs async (native INSERT sample-block time), where
198 * a croak would kill the process instead of erroring the query. */
199 0 static int flatten_json_hash(pTHX_ HV *src,
200 const char *prefix, STRLEN prefix_len,
201 HV *out_flat) {
202 0 int src_magical = SvRMAGICAL((SV *)src);
203 0 hv_iterinit(src);
204 HE *he;
205 0 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 0 0 0
206 I32 klen;
207 0 char *kstr = hv_iterkey(he, &klen);
208 0 SV *vsv = hv_iterval(src, he);
209 0 ▶ STRLEN new_len = prefix_len + (prefix_len ? 1 : 0) + klen;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN new_len = prefix_len + (prefix_len ? 1 : 0) + klen; 0 0 0
210 0 SV *path_sv = sv_2mortal(newSV(new_len));
211 0 SvPOK_only(path_sv);
212 0 char *pbuf = SvPVX(path_sv);
213 0 ▶ if (prefix_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (prefix_len) { 0 0 0
214 0 memcpy(pbuf, prefix, prefix_len);
215 0 pbuf[prefix_len] = '.';
216 0 memcpy(pbuf + prefix_len + 1, kstr, klen);
217 } else {
218 0 memcpy(pbuf, kstr, klen);
219 }
220 0 pbuf[new_len] = '\0'; /* keep the POK SV contract NUL-terminated */
221 0 SvCUR_set(path_sv, new_len);
222 /* A tied src yields lazy FETCH proxies: materialize for the
223 * checks below, and store a plain copy (borrowing the mortal
224 * proxy would re-FETCH on every later read). */
225 0 ▶ if (SvGMAGICAL(vsv)) mg_get(vsv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(vsv)) mg_get(vsv); 0 0 0
226 0 ▶ if (SvROK(vsv) && SvTYPE(SvRV(vsv)) == SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(vsv) && SvTYPE(SvRV(vsv)) == SVt_PVHV 0 0 0
if (SvROK(vsv) && SvTYPE(SvRV(vsv)) == SVt_PVHV 0 0 0
227 0 ▶ && !json_is_bool_ref(aTHX_ vsv)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !json_is_bool_ref(aTHX_ vsv)) { 0 0 0
228 0 ▶ if (sv_isobject(vsv))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(vsv)) 0 0 0
229 0 return 0;
230 0 ▶ if (!flatten_json_hash(aTHX_ (HV*)SvRV(vsv),
Branch: 1 branch
BranchTrueFalseTotal
if (!flatten_json_hash(aTHX_ (HV*)SvRV(vsv), 0 0 0
231 0 SvPVX(path_sv), new_len, out_flat))
232 0 return 0;
233 } else {
234 /* Copy whatever is magical (already FETCHed above): the
235 * flat hash outlives the borrow, and a later SvPV would
236 * re-FETCH. Plain values borrow as before. */
237 0 ▶ (void)hv_store(out_flat, SvPVX(path_sv), new_len,
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store(out_flat, SvPVX(path_sv), new_len, 0 0 0
(void)hv_store(out_flat, SvPVX(path_sv), new_len, 0 0 0
238 (src_magical || SvGMAGICAL(vsv))
239 ? newSVsv_nomg(vsv)
240 : SvREFCNT_inc_simple_NN(vsv), 0);
241 }
242 }
243 0 return 1;
244 }
245  
246 /* (path, len) pair for sorting paths whose keys may contain embedded NULs. */
247 typedef struct { char *path; STRLEN len; } json_path_entry_t;
248  
249 0 static int json_cmp_path_entry(const void *a, const void *b) {
250 0 const json_path_entry_t *pa = (const json_path_entry_t *)a;
251 0 const json_path_entry_t *pb = (const json_path_entry_t *)b;
252 0 STRLEN n = pa->len < pb->len ? pa->len : pb->len;
253 0 int r = memcmp(pa->path, pb->path, n);
254 0 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 0 0 0
255 0 ▶ if (pa->len < pb->len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (pa->len < pb->len) return -1; 0 0 0
256 0 ▶ if (pa->len > pb->len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pa->len > pb->len) return 1; 0 0 0
257 0 return 0;
258 }
259  
260 /* Emit one element of an Array(T) Dynamic variant. */
261 0 static void json_emit_array_elem(pTHX_ native_buf_t *b, SV *ev, int k_match) {
262 0 switch (k_match) {
263 0 case JV_ARRAY_BOOL:
264 0 ▶ if (!SvOK(ev)) { nbuf_u8(b, 0); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ev)) { nbuf_u8(b, 0); break; } 0 0 0
265 0 ▶ { SV *bv = SvROK(ev) ? SvRV(ev) : ev;
Branch: 1 branch
BranchTrueFalseTotal
{ SV *bv = SvROK(ev) ? SvRV(ev) : ev; 0 0 0
266 0 nbuf_u8(b, SvTRUE(bv) ? 1 : 0); break; }
267 0 case JV_ARRAY_INT64:
268 0 ▶ nbuf_le64(b, SvOK(ev) ? (uint64_t)(int64_t)SvIV(ev) : 0);
Branch: 1 branch
BranchTrueFalseTotal
nbuf_le64(b, SvOK(ev) ? (uint64_t)(int64_t)SvIV(ev) : 0); 0 0 0
269 0 break;
270 0 case JV_ARRAY_UINT64:
271 0 ▶ nbuf_le64(b, SvOK(ev) ? (uint64_t)SvUV(ev) : 0);
Branch: 1 branch
BranchTrueFalseTotal
nbuf_le64(b, SvOK(ev) ? (uint64_t)SvUV(ev) : 0); 0 0 0
272 0 break;
273 0 case JV_ARRAY_FLOAT64:
274 0 ▶ nbuf_ledouble(b, SvOK(ev) ? SvNV(ev) : 0.0);
Branch: 1 branch
BranchTrueFalseTotal
nbuf_ledouble(b, SvOK(ev) ? SvNV(ev) : 0.0); 0 0 0
275 0 break;
276 0 case JV_ARRAY_STRING: {
277 0 ▶ if (!SvOK(ev)) { nbuf_varuint(b, 0); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ev)) { nbuf_varuint(b, 0); break; } 0 0 0
278 STRLEN sl;
279 0 const char *ss = SvPV(ev, sl);
280 0 nbuf_string(b, ss, sl);
281 0 break;
282 }
283 }
284 0 }
285  
286 0 static void json_emit_scalar(pTHX_ native_buf_t *b, SV *val, int k_match) {
287 0 switch (k_match) {
288 0 case JV_BOOL: {
289 0 ▶ SV *bv = SvROK(val) ? SvRV(val) : val;
Branch: 1 branch
BranchTrueFalseTotal
SV *bv = SvROK(val) ? SvRV(val) : val; 0 0 0
290 0 nbuf_u8(b, SvTRUE(bv) ? 1 : 0);
291 0 break;
292 }
293 0 case JV_INT64:
294 0 nbuf_le64(b, (uint64_t)(int64_t)SvIV(val));
295 0 break;
296 0 case JV_UINT64:
297 0 nbuf_le64(b, (uint64_t)SvUV(val));
298 0 break;
299 0 case JV_FLOAT64:
300 0 nbuf_ledouble(b, SvNV(val));
301 0 break;
302 0 case JV_STRING: {
303 STRLEN sl;
304 0 const char *s = SvPV(val, sl);
305 0 nbuf_string(b, s, sl);
306 0 break;
307 }
308 0 default: break;
309 }
310 0 }
311  
312 /* --- Native protocol column decoder --- */
313  
314 /* Column type codes for decoding. */
315 enum {
316 CT_INT8, CT_INT16, CT_INT32, CT_INT64,
317 CT_UINT8, CT_UINT16, CT_UINT32, CT_UINT64,
318 CT_FLOAT32, CT_FLOAT64, CT_BFLOAT16,
319 CT_STRING, CT_FIXEDSTRING,
320 CT_ARRAY, CT_NULLABLE,
321 CT_DATE, CT_DATE32, CT_DATETIME, CT_DATETIME64,
322 CT_UUID, CT_ENUM8, CT_ENUM16,
323 CT_DECIMAL32, CT_DECIMAL64, CT_DECIMAL128, CT_DECIMAL256,
324 CT_LOWCARDINALITY, CT_NOTHING,
325 CT_BOOL, CT_IPV4, CT_IPV6,
326 CT_INT128, CT_UINT128,
327 CT_INT256, CT_UINT256,
328 CT_TUPLE, CT_MAP,
329 CT_JSON,
330 CT_VARIANT, CT_DYNAMIC,
331 CT_TIME, CT_TIME64,
332 CT_UNKNOWN
333 };
334  
335 typedef struct col_type_s col_type_t;
336 struct col_type_s {
337 int code;
338 int param; /* FixedString(N), DateTime64 precision, Decimal scale */
339 int precision; /* Decimal total digits (0 = width default) */
340 col_type_t *inner; /* Nullable, Array, LowCardinality */
341 col_type_t **inners; /* Tuple elements, Map key+value, JSON typed paths */
342 char **inner_names; /* JSON typed path names (NULL for other types) */
343 int num_inners;
344 char *type_str; /* full type string (for Enum label lookup) */
345 size_t type_str_len;
346 char *tz; /* timezone for DateTime/DateTime64 (NULL = UTC) */
347 };
348  
349 14 static void free_col_type(col_type_t *t) {
350 int i;
351 14 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 14 14
352 14 ▶ if (t->inner) free_col_type(t->inner);
Branch: 1 branch
BranchTrueFalseTotal
if (t->inner) free_col_type(t->inner); 0 14 14
353 14 ▶ if (t->inners) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->inners) { 0 14 14
354 0 ▶ for (i = 0; i < t->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->num_inners; i++) 0 0 0
355 0 free_col_type(t->inners[i]);
356 0 Safefree(t->inners);
357 }
358 14 ▶ if (t->inner_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->inner_names) { 0 14 14
359 0 ▶ for (i = 0; i < t->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->num_inners; i++) 0 0 0
360 0 ▶ if (t->inner_names[i]) Safefree(t->inner_names[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (t->inner_names[i]) Safefree(t->inner_names[i]); 0 0 0
361 0 Safefree(t->inner_names);
362 }
363 14 ▶ if (t->type_str) Safefree(t->type_str);
Branch: 1 branch
BranchTrueFalseTotal
if (t->type_str) Safefree(t->type_str); 0 14 14
364 14 ▶ if (t->tz) Safefree(t->tz);
Branch: 1 branch
BranchTrueFalseTotal
if (t->tz) Safefree(t->tz); 10 4 14
365 14 Safefree(t);
366 }
367  
368 /* Max nesting depth for parse_col_type recursion (hostile type strings
369 * like "Array(Array(Array(..." are bounded only by the block size). */
370 #define PARSE_COL_TYPE_MAX_DEPTH 100
371  
372 static col_type_t* parse_col_type_depth(const char *type, size_t len, int depth);
373 static col_type_t* parse_col_type(const char *type, size_t len);
374  
375 /*
376 * Parse comma-separated type list inside Tuple(...) or Map(...).
377 * Handles nested parentheses correctly.
378 * Sets t->inners and t->num_inners.
379 */
380 /* Index of the quote closing the '...' literal opened at s[i] (an Enum
381 * label may hold parens and commas), or len if unterminated. */
382 0 static size_t skip_quoted(const char *s, size_t len, size_t i) {
383 0 ▶ for (i++; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i++; i < len; i++) { 0 0 0
384 0 ▶ if (s[i] == '\\') i++;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '\\') i++; 0 0 0
385 0 ▶ else if (s[i] == '\'') return i;
Branch: 1 branch
BranchTrueFalseTotal
else if (s[i] == '\'') return i; 0 0 0
386 }
387 0 return len;
388 }
389  
390 0 static void parse_type_list(col_type_t *t, const char *inner, size_t inner_len, int rec_depth) {
391 0 int depth = 0, count = 0;
392 0 size_t i, start = 0;
393 int in_bt;
394  
395 0 ▶ if (inner_len == 0) return; /* Tuple() — zero inners */
Branch: 1 branch
BranchTrueFalseTotal
if (inner_len == 0) return; /* Tuple() — zero inners */ 0 0 0
396  
397 /* Count elements; `quoted names` and 'labels' shield parens/commas */
398 0 in_bt = 0;
399 0 ▶ for (i = 0; i <= inner_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= inner_len; i++) { 0 0 0
400 0 ▶ if (i < inner_len && inner[i] == '`') in_bt = !in_bt;
Branch: 2 branches
BranchTrueFalseTotal
if (i < inner_len && inner[i] == '`') in_bt = !in_bt; 0 0 0
if (i < inner_len && inner[i] == '`') in_bt = !in_bt; 0 0 0
401 0 ▶ else if (i < inner_len && in_bt) ;
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && in_bt) ; 0 0 0
else if (i < inner_len && in_bt) ; 0 0 0
402 0 ▶ else if (i < inner_len && inner[i] == '\'') i = skip_quoted(inner, inner_len, i);
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && inner[i] == '\'') i = skip_quoted(inner, inner_len, i); 0 0 0
else if (i < inner_len && inner[i] == '\'') i = skip_quoted(inner, inner_len, i); 0 0 0
403 0 ▶ else if (i < inner_len && inner[i] == '(') depth++;
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && inner[i] == '(') depth++; 0 0 0
else if (i < inner_len && inner[i] == '(') depth++; 0 0 0
404 0 ▶ else if (i < inner_len && inner[i] == ')') depth--;
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && inner[i] == ')') depth--; 0 0 0
else if (i < inner_len && inner[i] == ')') depth--; 0 0 0
405 0 ▶ else if (i == inner_len || (inner[i] == ',' && depth == 0))
Branch: 3 branches
BranchTrueFalseTotal
else if (i == inner_len || (inner[i] == ',' && depth == 0)) 0 0 0
else if (i == inner_len || (inner[i] == ',' && depth == 0)) 0 0 0
else if (i == inner_len || (inner[i] == ',' && depth == 0)) 0 0 0
406 0 count++;
407 }
408  
409 0 Newxz(t->inners, count, col_type_t*);
410 0 t->num_inners = count;
411  
412 /* Parse each element */
413 0 count = 0;
414 0 depth = 0;
415 0 in_bt = 0;
416 0 start = 0;
417 0 ▶ for (i = 0; i <= inner_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= inner_len; i++) { 0 0 0
418 0 ▶ if (i < inner_len && inner[i] == '`') in_bt = !in_bt;
Branch: 2 branches
BranchTrueFalseTotal
if (i < inner_len && inner[i] == '`') in_bt = !in_bt; 0 0 0
if (i < inner_len && inner[i] == '`') in_bt = !in_bt; 0 0 0
419 0 ▶ else if (i < inner_len && in_bt) ;
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && in_bt) ; 0 0 0
else if (i < inner_len && in_bt) ; 0 0 0
420 0 ▶ else if (i < inner_len && inner[i] == '\'') i = skip_quoted(inner, inner_len, i);
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && inner[i] == '\'') i = skip_quoted(inner, inner_len, i); 0 0 0
else if (i < inner_len && inner[i] == '\'') i = skip_quoted(inner, inner_len, i); 0 0 0
421 0 ▶ else if (i < inner_len && inner[i] == '(') depth++;
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && inner[i] == '(') depth++; 0 0 0
else if (i < inner_len && inner[i] == '(') depth++; 0 0 0
422 0 ▶ else if (i < inner_len && inner[i] == ')') depth--;
Branch: 2 branches
BranchTrueFalseTotal
else if (i < inner_len && inner[i] == ')') depth--; 0 0 0
else if (i < inner_len && inner[i] == ')') depth--; 0 0 0
423 0 ▶ else if (i == inner_len || (inner[i] == ',' && depth == 0)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (i == inner_len || (inner[i] == ',' && depth == 0)) { 0 0 0
else if (i == inner_len || (inner[i] == ',' && depth == 0)) { 0 0 0
else if (i == inner_len || (inner[i] == ',' && depth == 0)) { 0 0 0
424 0 size_t s = start, e = i;
425 0 ▶ while (s < e && inner[s] == ' ') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < e && inner[s] == ' ') s++; 0 0 0
while (s < e && inner[s] == ' ') s++; 0 0 0
426 0 ▶ while (e > s && inner[e-1] == ' ') e--;
Branch: 2 branches
BranchTrueFalseTotal
while (e > s && inner[e-1] == ' ') e--; 0 0 0
while (e > s && inner[e-1] == ' ') e--; 0 0 0
427 /* Strip named tuple field prefix: "name Type" -> "Type".
428 * The split is the first space outside `quotes`/parens. */
429 {
430 size_t sp;
431 0 int q = 0;
432 0 ▶ for (sp = s; sp < e; sp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sp = s; sp < e; sp++) { 0 0 0
433 0 ▶ if (inner[sp] == '`') q = !q;
Branch: 1 branch
BranchTrueFalseTotal
if (inner[sp] == '`') q = !q; 0 0 0
434 0 ▶ else if (q) ;
Branch: 1 branch
BranchTrueFalseTotal
else if (q) ; 0 0 0
435 0 ▶ else if (inner[sp] == '(') break; /* type w/ parens, stop */
Branch: 1 branch
BranchTrueFalseTotal
else if (inner[sp] == '(') break; /* type w/ parens, stop */ 0 0 0
436 0 ▶ else if (inner[sp] == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
else if (inner[sp] == ' ') { 0 0 0
437 0 s = sp + 1;
438 0 ▶ while (s < e && inner[s] == ' ') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < e && inner[s] == ' ') s++; 0 0 0
while (s < e && inner[s] == ' ') s++; 0 0 0
439 0 break;
440 }
441 }
442 }
443 0 t->inners[count++] = parse_col_type_depth(inner + s, e - s, rec_depth + 1);
444 0 start = i + 1;
445 }
446 }
447 }
448  
449 14 static col_type_t* parse_col_type_depth(const char *type, size_t len, int depth) {
450 col_type_t *t;
451 14 ▶ if (depth > PARSE_COL_TYPE_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth > PARSE_COL_TYPE_MAX_DEPTH) { 0 14 14
452 /* Fail the way an unparsable type fails: CT_UNKNOWN reads as String */
453 0 Newxz(t, 1, col_type_t);
454 0 t->code = CT_UNKNOWN;
455 0 return t;
456 }
457 14 Newxz(t, 1, col_type_t);
458  
459 14 ▶ if (len == 4 && memcmp(type, "Int8", 4) == 0) t->code = CT_INT8;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 4 && memcmp(type, "Int8", 4) == 0) t->code = CT_INT8; 0 14 14
if (len == 4 && memcmp(type, "Int8", 4) == 0) t->code = CT_INT8; 0 0 0
460 14 ▶ else if (len == 5 && memcmp(type, "Int16", 5) == 0) t->code = CT_INT16;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 5 && memcmp(type, "Int16", 5) == 0) t->code = CT_INT16; 2 12 14
else if (len == 5 && memcmp(type, "Int16", 5) == 0) t->code = CT_INT16; 0 2 2
461 14 ▶ else if (len == 5 && memcmp(type, "Int32", 5) == 0) t->code = CT_INT32;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 5 && memcmp(type, "Int32", 5) == 0) t->code = CT_INT32; 2 12 14
else if (len == 5 && memcmp(type, "Int32", 5) == 0) t->code = CT_INT32; 0 2 2
462 14 ▶ else if (len == 5 && memcmp(type, "Int64", 5) == 0) t->code = CT_INT64;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 5 && memcmp(type, "Int64", 5) == 0) t->code = CT_INT64; 2 12 14
else if (len == 5 && memcmp(type, "Int64", 5) == 0) t->code = CT_INT64; 0 2 2
463 14 ▶ else if (len > 8 && memcmp(type, "Interval", 8) == 0) t->code = CT_INT64;
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 8 && memcmp(type, "Interval", 8) == 0) t->code = CT_INT64; 10 4 14
else if (len > 8 && memcmp(type, "Interval", 8) == 0) t->code = CT_INT64; 0 10 10
464 14 ▶ else if (len == 5 && memcmp(type, "UInt8", 5) == 0) t->code = CT_UINT8;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 5 && memcmp(type, "UInt8", 5) == 0) t->code = CT_UINT8; 2 12 14
else if (len == 5 && memcmp(type, "UInt8", 5) == 0) t->code = CT_UINT8; 2 0 2
465 12 ▶ else if (len == 6 && memcmp(type, "UInt16", 6) == 0) t->code = CT_UINT16;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 6 && memcmp(type, "UInt16", 6) == 0) t->code = CT_UINT16; 2 10 12
else if (len == 6 && memcmp(type, "UInt16", 6) == 0) t->code = CT_UINT16; 0 2 2
466 12 ▶ else if (len == 6 && memcmp(type, "UInt32", 6) == 0) t->code = CT_UINT32;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 6 && memcmp(type, "UInt32", 6) == 0) t->code = CT_UINT32; 2 10 12
else if (len == 6 && memcmp(type, "UInt32", 6) == 0) t->code = CT_UINT32; 2 0 2
467 10 ▶ else if (len == 6 && memcmp(type, "UInt64", 6) == 0) t->code = CT_UINT64;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 6 && memcmp(type, "UInt64", 6) == 0) t->code = CT_UINT64; 0 10 10
else if (len == 6 && memcmp(type, "UInt64", 6) == 0) t->code = CT_UINT64; 0 0 0
468 10 ▶ else if (len == 7 && memcmp(type, "Float32", 7) == 0) t->code = CT_FLOAT32;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 7 && memcmp(type, "Float32", 7) == 0) t->code = CT_FLOAT32; 0 10 10
else if (len == 7 && memcmp(type, "Float32", 7) == 0) t->code = CT_FLOAT32; 0 0 0
469 10 ▶ else if (len == 7 && memcmp(type, "Float64", 7) == 0) t->code = CT_FLOAT64;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 7 && memcmp(type, "Float64", 7) == 0) t->code = CT_FLOAT64; 0 10 10
else if (len == 7 && memcmp(type, "Float64", 7) == 0) t->code = CT_FLOAT64; 0 0 0
470 10 ▶ else if (len == 6 && memcmp(type, "String", 6) == 0) t->code = CT_STRING;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 6 && memcmp(type, "String", 6) == 0) t->code = CT_STRING; 0 10 10
else if (len == 6 && memcmp(type, "String", 6) == 0) t->code = CT_STRING; 0 0 0
471 10 ▶ else if (len > 12 && memcmp(type, "FixedString(", 12) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 12 && memcmp(type, "FixedString(", 12) == 0) { 10 0 10
else if (len > 12 && memcmp(type, "FixedString(", 12) == 0) { 0 10 10
472 0 t->code = CT_FIXEDSTRING;
473 0 t->param = atoi(type + 12);
474 /* Clamp hostile negatives: param feeds (size_t) casts */
475 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
476 }
477 10 ▶ else if (len > 6 && memcmp(type, "Array(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 6 && memcmp(type, "Array(", 6) == 0) { 10 0 10
else if (len > 6 && memcmp(type, "Array(", 6) == 0) { 0 10 10
478 0 t->code = CT_ARRAY;
479 0 t->inner = parse_col_type_depth(type + 6, len - 7, depth + 1);
480 }
481 10 ▶ else if (len > 9 && memcmp(type, "Nullable(", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 9 && memcmp(type, "Nullable(", 9) == 0) { 10 0 10
else if (len > 9 && memcmp(type, "Nullable(", 9) == 0) { 0 10 10
482 0 t->code = CT_NULLABLE;
483 0 t->inner = parse_col_type_depth(type + 9, len - 10, depth + 1);
484 }
485 10 ▶ else if (len > 15 && memcmp(type, "LowCardinality(", 15) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 15 && memcmp(type, "LowCardinality(", 15) == 0) { 0 10 10
else if (len > 15 && memcmp(type, "LowCardinality(", 15) == 0) { 0 0 0
486 0 t->code = CT_LOWCARDINALITY;
487 0 t->inner = parse_col_type_depth(type + 15, len - 16, depth + 1);
488 }
489 10 ▶ else if (len == 4 && memcmp(type, "Date", 4) == 0) t->code = CT_DATE;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 4 && memcmp(type, "Date", 4) == 0) t->code = CT_DATE; 0 10 10
else if (len == 4 && memcmp(type, "Date", 4) == 0) t->code = CT_DATE; 0 0 0
490 10 ▶ else if (len == 6 && memcmp(type, "Date32", 6) == 0) t->code = CT_DATE32;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 6 && memcmp(type, "Date32", 6) == 0) t->code = CT_DATE32; 0 10 10
else if (len == 6 && memcmp(type, "Date32", 6) == 0) t->code = CT_DATE32; 0 0 0
491 10 ▶ else if (len == 8 && memcmp(type, "DateTime", 8) == 0) t->code = CT_DATETIME;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 8 && memcmp(type, "DateTime", 8) == 0) t->code = CT_DATETIME; 0 10 10
else if (len == 8 && memcmp(type, "DateTime", 8) == 0) t->code = CT_DATETIME; 0 0 0
492 10 ▶ else if (len > 9 && memcmp(type, "DateTime(", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 9 && memcmp(type, "DateTime(", 9) == 0) { 10 0 10
else if (len > 9 && memcmp(type, "DateTime(", 9) == 0) { 0 10 10
493 0 t->code = CT_DATETIME;
494 /* DateTime('timezone') — extract timezone */
495 0 {
496 0 const char *q = memchr(type + 9, '\'', len - 9);
497 0 ▶ if (q) {
Branch: 1 branch
BranchTrueFalseTotal
if (q) { 0 0 0
498 0 const char *qe = memchr(q + 1, '\'', type + len - q - 1);
499 0 ▶ if (qe && qe > q + 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (qe && qe > q + 1) { 0 0 0
if (qe && qe > q + 1) { 0 0 0
500 0 size_t tzlen = qe - q - 1;
501 0 Newx(t->tz, tzlen + 1, char);
502 0 Copy(q + 1, t->tz, tzlen, char);
503 0 t->tz[tzlen] = '\0';
504 }
505 }
506 }
507 }
508 10 ▶ else if (len > 11 && memcmp(type, "DateTime64(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 11 && memcmp(type, "DateTime64(", 11) == 0) { 10 0 10
else if (len > 11 && memcmp(type, "DateTime64(", 11) == 0) { 10 0 10
509 10 t->code = CT_DATETIME64;
510 10 t->param = atoi(type + 11);
511 /* Clamp hostile precision (drives a per-row scale loop) */
512 10 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 10 10
513 10 ▶ if (t->param > 9) t->param = 9;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 9) t->param = 9; 0 10 10
514 /* DateTime64(N, 'timezone') — extract timezone */
515 10 {
516 10 const char *comma = memchr(type + 11, ',', len - 11);
517 10 ▶ if (comma) {
Branch: 1 branch
BranchTrueFalseTotal
if (comma) { 0 10 10
518 0 const char *q = memchr(comma, '\'', type + len - comma);
519 0 ▶ if (q) {
Branch: 1 branch
BranchTrueFalseTotal
if (q) { 0 0 0
520 0 const char *qe = memchr(q + 1, '\'', type + len - q - 1);
521 0 ▶ if (qe && qe > q + 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (qe && qe > q + 1) { 0 0 0
if (qe && qe > q + 1) { 0 0 0
522 0 size_t tzlen = qe - q - 1;
523 0 Newx(t->tz, tzlen + 1, char);
524 0 Copy(q + 1, t->tz, tzlen, char);
525 0 t->tz[tzlen] = '\0';
526 }
527 }
528 }
529 }
530 }
531 0 ▶ else if (len == 4 && memcmp(type, "Time", 4) == 0) t->code = CT_TIME;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 4 && memcmp(type, "Time", 4) == 0) t->code = CT_TIME; 0 0 0
else if (len == 4 && memcmp(type, "Time", 4) == 0) t->code = CT_TIME; 0 0 0
532 0 ▶ else if (len > 7 && memcmp(type, "Time64(", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 7 && memcmp(type, "Time64(", 7) == 0) { 0 0 0
else if (len > 7 && memcmp(type, "Time64(", 7) == 0) { 0 0 0
533 0 t->code = CT_TIME64;
534 0 t->param = atoi(type + 7);
535 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
536 0 ▶ if (t->param > 9) t->param = 9;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 9) t->param = 9; 0 0 0
537 }
538 0 ▶ else if (len == 4 && memcmp(type, "UUID", 4) == 0) t->code = CT_UUID;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 4 && memcmp(type, "UUID", 4) == 0) t->code = CT_UUID; 0 0 0
else if (len == 4 && memcmp(type, "UUID", 4) == 0) t->code = CT_UUID; 0 0 0
539 0 ▶ else if (len > 6 && memcmp(type, "Enum8(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 6 && memcmp(type, "Enum8(", 6) == 0) { 0 0 0
else if (len > 6 && memcmp(type, "Enum8(", 6) == 0) { 0 0 0
540 0 t->code = CT_ENUM8;
541 0 Newx(t->type_str, len + 1, char);
542 0 Copy(type, t->type_str, len, char);
543 0 t->type_str[len] = '\0';
544 0 t->type_str_len = len;
545 }
546 0 ▶ else if (len > 7 && memcmp(type, "Enum16(", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 7 && memcmp(type, "Enum16(", 7) == 0) { 0 0 0
else if (len > 7 && memcmp(type, "Enum16(", 7) == 0) { 0 0 0
547 0 t->code = CT_ENUM16;
548 0 Newx(t->type_str, len + 1, char);
549 0 Copy(type, t->type_str, len, char);
550 0 t->type_str[len] = '\0';
551 0 t->type_str_len = len;
552 }
553 0 ▶ else if (len > 10 && memcmp(type, "Decimal32(", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 10 && memcmp(type, "Decimal32(", 10) == 0) { 0 0 0
else if (len > 10 && memcmp(type, "Decimal32(", 10) == 0) { 0 0 0
554 0 t->code = CT_DECIMAL32;
555 0 t->param = atoi(type + 10);
556 0 t->precision = 9;
557 /* Clamp hostile scale (drives per-row pow10 loops) */
558 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
559 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
560 }
561 0 ▶ else if (len > 10 && memcmp(type, "Decimal64(", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 10 && memcmp(type, "Decimal64(", 10) == 0) { 0 0 0
else if (len > 10 && memcmp(type, "Decimal64(", 10) == 0) { 0 0 0
562 0 t->code = CT_DECIMAL64;
563 0 t->param = atoi(type + 10);
564 0 t->precision = 18;
565 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
566 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
567 }
568 0 ▶ else if (len > 11 && memcmp(type, "Decimal128(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 11 && memcmp(type, "Decimal128(", 11) == 0) { 0 0 0
else if (len > 11 && memcmp(type, "Decimal128(", 11) == 0) { 0 0 0
569 0 t->code = CT_DECIMAL128;
570 0 t->param = atoi(type + 11);
571 0 t->precision = 38;
572 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
573 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
574 }
575 0 ▶ else if (len > 11 && memcmp(type, "Decimal256(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 11 && memcmp(type, "Decimal256(", 11) == 0) { 0 0 0
else if (len > 11 && memcmp(type, "Decimal256(", 11) == 0) { 0 0 0
576 0 t->code = CT_DECIMAL256;
577 0 t->param = atoi(type + 11);
578 0 t->precision = 76;
579 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
580 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
581 }
582 0 ▶ else if (len > 8 && memcmp(type, "Decimal(", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 8 && memcmp(type, "Decimal(", 8) == 0) { 0 0 0
else if (len > 8 && memcmp(type, "Decimal(", 8) == 0) { 0 0 0
583 0 int precision = atoi(type + 8);
584 0 const char *comma = memchr(type + 8, ',', len - 8);
585 0 ▶ t->param = comma ? atoi(comma + 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
t->param = comma ? atoi(comma + 1) : 0; 0 0 0
586 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
587 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
588 0 ▶ if (precision < 1) precision = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (precision < 1) precision = 1; 0 0 0
589 0 ▶ if (precision > 76) precision = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (precision > 76) precision = 76; 0 0 0
590 0 t->precision = precision;
591 0 ▶ if (precision <= 9) t->code = CT_DECIMAL32;
Branch: 1 branch
BranchTrueFalseTotal
if (precision <= 9) t->code = CT_DECIMAL32; 0 0 0
592 0 ▶ else if (precision <= 18) t->code = CT_DECIMAL64;
Branch: 1 branch
BranchTrueFalseTotal
else if (precision <= 18) t->code = CT_DECIMAL64; 0 0 0
593 0 ▶ else if (precision <= 38) t->code = CT_DECIMAL128;
Branch: 1 branch
BranchTrueFalseTotal
else if (precision <= 38) t->code = CT_DECIMAL128; 0 0 0
594 0 else t->code = CT_DECIMAL256;
595 }
596 0 ▶ else if (len == 8 && memcmp(type, "BFloat16", 8) == 0) t->code = CT_BFLOAT16;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 8 && memcmp(type, "BFloat16", 8) == 0) t->code = CT_BFLOAT16; 0 0 0
else if (len == 8 && memcmp(type, "BFloat16", 8) == 0) t->code = CT_BFLOAT16; 0 0 0
597 /* Variant(...) and Dynamic: recognised here so the schema parser
598 * doesn't choke. Wire-format decoding is NOT generic — selecting
599 * one produces a clean decode error from decode_column so the
600 * caller's response parsing doesn't desync. Use
601 * `SELECT toString(col) FROM …` server-side to read the value as
602 * its JSON representation, or `CAST(col AS String)`. */
603 0 ▶ else if (len > 8 && memcmp(type, "Variant(", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 8 && memcmp(type, "Variant(", 8) == 0) { 0 0 0
else if (len > 8 && memcmp(type, "Variant(", 8) == 0) { 0 0 0
604 0 t->code = CT_VARIANT;
605 }
606 0 ▶ else if (len == 7 && memcmp(type, "Dynamic", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 7 && memcmp(type, "Dynamic", 7) == 0) { 0 0 0
else if (len == 7 && memcmp(type, "Dynamic", 7) == 0) { 0 0 0
607 0 t->code = CT_DYNAMIC;
608 }
609 0 ▶ else if (len == 7 && memcmp(type, "Nothing", 7) == 0) t->code = CT_NOTHING;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 7 && memcmp(type, "Nothing", 7) == 0) t->code = CT_NOTHING; 0 0 0
else if (len == 7 && memcmp(type, "Nothing", 7) == 0) t->code = CT_NOTHING; 0 0 0
610 0 ▶ else if (len == 4 && memcmp(type, "Bool", 4) == 0) t->code = CT_BOOL;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 4 && memcmp(type, "Bool", 4) == 0) t->code = CT_BOOL; 0 0 0
else if (len == 4 && memcmp(type, "Bool", 4) == 0) t->code = CT_BOOL; 0 0 0
611 0 ▶ else if (len == 4 && memcmp(type, "IPv4", 4) == 0) t->code = CT_IPV4;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 4 && memcmp(type, "IPv4", 4) == 0) t->code = CT_IPV4; 0 0 0
else if (len == 4 && memcmp(type, "IPv4", 4) == 0) t->code = CT_IPV4; 0 0 0
612 0 ▶ else if (len == 4 && memcmp(type, "IPv6", 4) == 0) t->code = CT_IPV6;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 4 && memcmp(type, "IPv6", 4) == 0) t->code = CT_IPV6; 0 0 0
else if (len == 4 && memcmp(type, "IPv6", 4) == 0) t->code = CT_IPV6; 0 0 0
613 0 ▶ else if (len == 6 && memcmp(type, "Int128", 6) == 0) t->code = CT_INT128;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 6 && memcmp(type, "Int128", 6) == 0) t->code = CT_INT128; 0 0 0
else if (len == 6 && memcmp(type, "Int128", 6) == 0) t->code = CT_INT128; 0 0 0
614 0 ▶ else if (len == 7 && memcmp(type, "UInt128", 7) == 0) t->code = CT_UINT128;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 7 && memcmp(type, "UInt128", 7) == 0) t->code = CT_UINT128; 0 0 0
else if (len == 7 && memcmp(type, "UInt128", 7) == 0) t->code = CT_UINT128; 0 0 0
615 0 ▶ else if (len == 6 && memcmp(type, "Int256", 6) == 0) t->code = CT_INT256;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 6 && memcmp(type, "Int256", 6) == 0) t->code = CT_INT256; 0 0 0
else if (len == 6 && memcmp(type, "Int256", 6) == 0) t->code = CT_INT256; 0 0 0
616 0 ▶ else if (len == 7 && memcmp(type, "UInt256", 7) == 0) t->code = CT_UINT256;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 7 && memcmp(type, "UInt256", 7) == 0) t->code = CT_UINT256; 0 0 0
else if (len == 7 && memcmp(type, "UInt256", 7) == 0) t->code = CT_UINT256; 0 0 0
617 0 ▶ else if (len > 6 && memcmp(type, "Tuple(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 6 && memcmp(type, "Tuple(", 6) == 0) { 0 0 0
else if (len > 6 && memcmp(type, "Tuple(", 6) == 0) { 0 0 0
618 0 t->code = CT_TUPLE;
619 0 parse_type_list(t, type + 6, len - 7, depth);
620 }
621 0 ▶ else if (len > 4 && memcmp(type, "Map(", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 4 && memcmp(type, "Map(", 4) == 0) { 0 0 0
else if (len > 4 && memcmp(type, "Map(", 4) == 0) { 0 0 0
622 0 t->code = CT_MAP;
623 0 parse_type_list(t, type + 4, len - 5, depth);
624 }
625 0 ▶ else if (len > 7 && memcmp(type, "Nested(", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len > 7 && memcmp(type, "Nested(", 7) == 0) { 0 0 0
else if (len > 7 && memcmp(type, "Nested(", 7) == 0) { 0 0 0
626 /* Nested(name1 Type1, name2 Type2) = Array(Tuple(Type1, Type2)) */
627 col_type_t *tuple;
628 0 Newxz(tuple, 1, col_type_t);
629 0 tuple->code = CT_TUPLE;
630 0 parse_type_list(tuple, type + 7, len - 8, depth);
631 0 t->code = CT_ARRAY;
632 0 t->inner = tuple;
633 }
634 0 ▶ else if ((len == 4 && memcmp(type, "JSON", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if ((len == 4 && memcmp(type, "JSON", 4) == 0) 0 0 0
else if ((len == 4 && memcmp(type, "JSON", 4) == 0) 0 0 0
635 0 ▶ || (len > 5 && memcmp(type, "JSON(", 5) == 0 && type[len-1] == ')')
Branch: 3 branches
BranchTrueFalseTotal
|| (len > 5 && memcmp(type, "JSON(", 5) == 0 && type[len-1] == ')') 0 0 0
|| (len > 5 && memcmp(type, "JSON(", 5) == 0 && type[len-1] == ')') 0 0 0
|| (len > 5 && memcmp(type, "JSON(", 5) == 0 && type[len-1] == ')') 0 0 0
636 0 ▶ || (len > 7 && memcmp(type, "Object(", 7) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (len > 7 && memcmp(type, "Object(", 7) == 0)) { 0 0 0
|| (len > 7 && memcmp(type, "Object(", 7) == 0)) { 0 0 0
637 /* ClickHouse stable JSON type (24.8+); Object('json') is the
638 * legacy spelling. JSON(name Type, ...) pins specific paths to
639 * concrete inner types ("typed paths"); those skip the
640 * Dynamic+Variant wrap and write as regular columns inline. */
641 0 t->code = CT_JSON;
642 0 ▶ if (len > 5 && type[4] == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (len > 5 && type[4] == '(') { 0 0 0
if (len > 5 && type[4] == '(') { 0 0 0
643 /* parse "name Type, name Type, ..." */
644 0 const char *body = type + 5;
645 0 size_t blen = len - 6;
646 0 int idx = 0, pdepth = 0, in_bt = 0;
647 0 size_t start = 0, i;
648 typedef struct { size_t name_start, name_len, type_start, type_len; }
649 jp_bound_t;
650 jp_bound_t bounds[64];
651 0 ▶ for (i = 0; i <= blen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= blen; i++) { 0 0 0
652 0 ▶ char c = (i < blen) ? body[i] : ',';
Branch: 1 branch
BranchTrueFalseTotal
char c = (i < blen) ? body[i] : ','; 0 0 0
653 0 ▶ if (c == '`' && i < blen) in_bt = !in_bt;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '`' && i < blen) in_bt = !in_bt; 0 0 0
if (c == '`' && i < blen) in_bt = !in_bt; 0 0 0
654 0 ▶ else if (in_bt && i < blen) ;
Branch: 2 branches
BranchTrueFalseTotal
else if (in_bt && i < blen) ; 0 0 0
else if (in_bt && i < blen) ; 0 0 0
655 0 ▶ else if (c == '\'' && i < blen) i = skip_quoted(body, blen, i);
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '\'' && i < blen) i = skip_quoted(body, blen, i); 0 0 0
else if (c == '\'' && i < blen) i = skip_quoted(body, blen, i); 0 0 0
656 0 ▶ else if (c == '(') pdepth++;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '(') pdepth++; 0 0 0
657 0 ▶ else if (c == ')') pdepth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ')') pdepth--; 0 0 0
658 0 ▶ else if ((c == ',' && pdepth == 0) || i == blen) {
Branch: 3 branches
BranchTrueFalseTotal
else if ((c == ',' && pdepth == 0) || i == blen) { 0 0 0
else if ((c == ',' && pdepth == 0) || i == blen) { 0 0 0
else if ((c == ',' && pdepth == 0) || i == blen) { 0 0 0
659 0 size_t ts = start, te = i, id;
660 0 int q = 0;
661 0 ▶ while (ts < te && (body[ts]==' '||body[ts]=='\t')) ts++;
Branch: 3 branches
BranchTrueFalseTotal
while (ts < te && (body[ts]==' '||body[ts]=='\t')) ts++; 0 0 0
while (ts < te && (body[ts]==' '||body[ts]=='\t')) ts++; 0 0 0
while (ts < te && (body[ts]==' '||body[ts]=='\t')) ts++; 0 0 0
662 0 ▶ while (te > ts && (body[te-1]==' '||body[te-1]=='\t')) te--;
Branch: 3 branches
BranchTrueFalseTotal
while (te > ts && (body[te-1]==' '||body[te-1]=='\t')) te--; 0 0 0
while (te > ts && (body[te-1]==' '||body[te-1]=='\t')) te--; 0 0 0
while (te > ts && (body[te-1]==' '||body[te-1]=='\t')) te--; 0 0 0
663 0 ▶ if (te > ts && idx < 64) {
Branch: 2 branches
BranchTrueFalseTotal
if (te > ts && idx < 64) { 0 0 0
if (te > ts && idx < 64) { 0 0 0
664 /* split on first blank outside `quotes` */
665 0 ▶ for (id = ts; id < te; id++) {
Branch: 1 branch
BranchTrueFalseTotal
for (id = ts; id < te; id++) { 0 0 0
666 0 ▶ if (body[id] == '`') q = !q;
Branch: 1 branch
BranchTrueFalseTotal
if (body[id] == '`') q = !q; 0 0 0
667 0 ▶ else if (!q && (body[id] == ' ' || body[id] == '\t')) break;
Branch: 3 branches
BranchTrueFalseTotal
else if (!q && (body[id] == ' ' || body[id] == '\t')) break; 0 0 0
else if (!q && (body[id] == ' ' || body[id] == '\t')) break; 0 0 0
else if (!q && (body[id] == ' ' || body[id] == '\t')) break; 0 0 0
668 }
669 {
670 0 size_t ws = id;
671 0 ▶ while (ws < te && (body[ws]==' '||body[ws]=='\t')) ws++;
Branch: 3 branches
BranchTrueFalseTotal
while (ws < te && (body[ws]==' '||body[ws]=='\t')) ws++; 0 0 0
while (ws < te && (body[ws]==' '||body[ws]=='\t')) ws++; 0 0 0
while (ws < te && (body[ws]==' '||body[ws]=='\t')) ws++; 0 0 0
672 0 ▶ if (id > ts && ws < te) {
Branch: 2 branches
BranchTrueFalseTotal
if (id > ts && ws < te) { 0 0 0
if (id > ts && ws < te) { 0 0 0
673 /* strip `quoting` for name compare/store */
674 0 ▶ if (id - ts >= 2 && body[ts] == '`' && body[id-1] == '`') {
Branch: 3 branches
BranchTrueFalseTotal
if (id - ts >= 2 && body[ts] == '`' && body[id-1] == '`') { 0 0 0
if (id - ts >= 2 && body[ts] == '`' && body[id-1] == '`') { 0 0 0
if (id - ts >= 2 && body[ts] == '`' && body[id-1] == '`') { 0 0 0
675 0 ts++;
676 0 id--;
677 }
678 0 bounds[idx].name_start = ts;
679 0 bounds[idx].name_len = id - ts;
680 0 bounds[idx].type_start = ws;
681 0 bounds[idx].type_len = te - ws;
682 0 idx++;
683 }
684 }
685 }
686 0 start = i + 1;
687 }
688 }
689 0 ▶ if (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx > 0) { 0 0 0
690 /* Sort by name (lex) — wire-order requirement. */
691 int j;
692 0 ▶ for (j = 1; j < idx; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < idx; j++) { 0 0 0
693 0 int z = j;
694 0 ▶ while (z > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (z > 0) { 0 0 0
695 0 size_t la = bounds[z-1].name_len, lb = bounds[z].name_len;
696 0 size_t m = la < lb ? la : lb;
697 0 int cmp = memcmp(body + bounds[z-1].name_start,
698 0 body + bounds[z].name_start, m);
699 0 ▶ if (cmp == 0) cmp = (int)la - (int)lb;
Branch: 1 branch
BranchTrueFalseTotal
if (cmp == 0) cmp = (int)la - (int)lb; 0 0 0
700 0 ▶ if (cmp <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (cmp <= 0) break; 0 0 0
701 0 jp_bound_t tmp = bounds[z-1];
702 0 bounds[z-1] = bounds[z];
703 0 bounds[z] = tmp;
704 0 z--;
705 }
706 }
707 0 t->num_inners = idx;
708 0 Newxz(t->inners, idx, col_type_t *);
709 0 Newxz(t->inner_names, idx, char *);
710 0 ▶ for (j = 0; j < idx; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < idx; j++) { 0 0 0
711 0 Newx(t->inner_names[j], bounds[j].name_len + 1, char);
712 0 memcpy(t->inner_names[j],
713 0 body + bounds[j].name_start, bounds[j].name_len);
714 0 t->inner_names[j][bounds[j].name_len] = '\0';
715 0 t->inners[j] = parse_col_type_depth(
716 0 body + bounds[j].type_start, bounds[j].type_len, depth + 1);
717 }
718 }
719 }
720 }
721 /* Geo type aliases (per ClickHouse docs / Encoder layout) */
722 0 ▶ else if (len == 5 && memcmp(type, "Point", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 5 && memcmp(type, "Point", 5) == 0) { 0 0 0
else if (len == 5 && memcmp(type, "Point", 5) == 0) { 0 0 0
723 /* Point = Tuple(Float64, Float64) */
724 0 t->code = CT_TUPLE;
725 0 parse_type_list(t, "Float64,Float64", 15, depth);
726 }
727 0 ▶ else if (len == 4 && memcmp(type, "Ring", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 4 && memcmp(type, "Ring", 4) == 0) { 0 0 0
else if (len == 4 && memcmp(type, "Ring", 4) == 0) { 0 0 0
728 /* Ring = Array(Point) */
729 0 t->code = CT_ARRAY;
730 0 t->inner = parse_col_type_depth("Point", 5, depth + 1);
731 }
732 0 ▶ else if (len == 10 && memcmp(type, "LineString", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 10 && memcmp(type, "LineString", 10) == 0) { 0 0 0
else if (len == 10 && memcmp(type, "LineString", 10) == 0) { 0 0 0
733 /* LineString = Array(Point) */
734 0 t->code = CT_ARRAY;
735 0 t->inner = parse_col_type_depth("Point", 5, depth + 1);
736 }
737 0 ▶ else if (len == 15 && memcmp(type, "MultiLineString", 15) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 15 && memcmp(type, "MultiLineString", 15) == 0) { 0 0 0
else if (len == 15 && memcmp(type, "MultiLineString", 15) == 0) { 0 0 0
738 /* MultiLineString = Array(Array(Point)) */
739 0 t->code = CT_ARRAY;
740 0 t->inner = parse_col_type_depth("Array(Point)", 12, depth + 1);
741 }
742 0 ▶ else if (len == 7 && memcmp(type, "Polygon", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 7 && memcmp(type, "Polygon", 7) == 0) { 0 0 0
else if (len == 7 && memcmp(type, "Polygon", 7) == 0) { 0 0 0
743 /* Polygon = Array(Ring) */
744 0 t->code = CT_ARRAY;
745 0 t->inner = parse_col_type_depth("Ring", 4, depth + 1);
746 }
747 0 ▶ else if (len == 12 && memcmp(type, "MultiPolygon", 12) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 12 && memcmp(type, "MultiPolygon", 12) == 0) { 0 0 0
else if (len == 12 && memcmp(type, "MultiPolygon", 12) == 0) { 0 0 0
748 /* MultiPolygon = Array(Polygon) */
749 0 t->code = CT_ARRAY;
750 0 t->inner = parse_col_type_depth("Polygon", 7, depth + 1);
751 }
752 0 ▶ else if ((len > 25 && memcmp(type, "SimpleAggregateFunction(", 24) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if ((len > 25 && memcmp(type, "SimpleAggregateFunction(", 24) == 0) 0 0 0
else if ((len > 25 && memcmp(type, "SimpleAggregateFunction(", 24) == 0) 0 0 0
753 0 ▶ || (len > 19 && memcmp(type, "AggregateFunction(", 18) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (len > 19 && memcmp(type, "AggregateFunction(", 18) == 0)) { 0 0 0
|| (len > 19 && memcmp(type, "AggregateFunction(", 18) == 0)) { 0 0 0
754 /* (Simple)AggregateFunction(func, Type...) — skip func, parse inner.
755 * For SAF this is exact. For full AggregateFunction the raw inner
756 * type matches simple aggregates (sum/min/max etc) but is wrong
757 * for complex states (quantile, uniqExact, ...) — for those, run
758 * finalizeAggregation(col) server-side and read the result. */
759 0 ▶ size_t off = (memcmp(type, "Simple", 6) == 0) ? 24 : 18;
Branch: 1 branch
BranchTrueFalseTotal
size_t off = (memcmp(type, "Simple", 6) == 0) ? 24 : 18; 0 0 0
760 0 const char *inner = type + off;
761 0 size_t inner_len = len - off - 1;
762 size_t ci;
763 0 int pdepth = 0;
764 0 ▶ for (ci = 0; ci < inner_len; ci++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ci = 0; ci < inner_len; ci++) { 0 0 0
765 0 ▶ if (inner[ci] == '\'') ci = skip_quoted(inner, inner_len, ci);
Branch: 1 branch
BranchTrueFalseTotal
if (inner[ci] == '\'') ci = skip_quoted(inner, inner_len, ci); 0 0 0
766 0 ▶ else if (inner[ci] == '(') pdepth++;
Branch: 1 branch
BranchTrueFalseTotal
else if (inner[ci] == '(') pdepth++; 0 0 0
767 0 ▶ else if (inner[ci] == ')') pdepth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (inner[ci] == ')') pdepth--; 0 0 0
768 0 ▶ else if (inner[ci] == ',' && pdepth == 0) break;
Branch: 2 branches
BranchTrueFalseTotal
else if (inner[ci] == ',' && pdepth == 0) break; 0 0 0
else if (inner[ci] == ',' && pdepth == 0) break; 0 0 0
769 }
770 0 ▶ if (ci < inner_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (ci < inner_len) { 0 0 0
771 0 ci++;
772 0 ▶ while (ci < inner_len && inner[ci] == ' ') ci++;
Branch: 2 branches
BranchTrueFalseTotal
while (ci < inner_len && inner[ci] == ' ') ci++; 0 0 0
while (ci < inner_len && inner[ci] == ' ') ci++; 0 0 0
773 0 Safefree(t);
774 0 t = parse_col_type_depth(inner + ci, inner_len - ci, depth + 1);
775 } else {
776 0 t->code = CT_UNKNOWN;
777 }
778 }
779 else {
780 /* Unknown type — treat as String (read raw bytes) */
781 0 t->code = CT_UNKNOWN;
782 }
783  
784 14 return t;
785 }
786  
787 14 static col_type_t* parse_col_type(const char *type, size_t len) {
788 14 return parse_col_type_depth(type, len, 0);
789 }
790  
791 /* Give tz-less DateTime/DateTime64 nodes the session's timezone: the
792 * server reads and renders their wall times in it. */
793 28 static void col_type_default_tz(col_type_t *t, const char *tz) {
794 int i;
795 28 ▶ if (!t || !tz || !tz[0]) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!t || !tz || !tz[0]) return; 14 14 28
if (!t || !tz || !tz[0]) return; 14 0 14
if (!t || !tz || !tz[0]) return; 0 14 14
796 14 ▶ if ((t->code == CT_DATETIME || t->code == CT_DATETIME64) && !t->tz) {
Branch: 3 branches
BranchTrueFalseTotal
if ((t->code == CT_DATETIME || t->code == CT_DATETIME64) && !t->tz) { 14 0 14
if ((t->code == CT_DATETIME || t->code == CT_DATETIME64) && !t->tz) { 10 4 14
if ((t->code == CT_DATETIME || t->code == CT_DATETIME64) && !t->tz) { 10 0 10
797 10 size_t n = strlen(tz);
798 10 Newx(t->tz, n + 1, char);
799 10 Copy(tz, t->tz, n + 1, char);
800 }
801 14 col_type_default_tz(t->inner, tz);
802 14 ▶ for (i = 0; i < t->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->num_inners; i++) 0 14 14
803 0 col_type_default_tz(t->inners[i], tz);
804 }
805  
806 98 static const char *hv_session_tz(pTHX_ HV *hv) {
807 98 ▶ SV **svp = hv ? hv_fetchs(hv, "session_timezone", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **svp = hv ? hv_fetchs(hv, "session_timezone", 0) : NULL; 2 96 98
808 98 ▶ if (!svp) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) return NULL; 98 0 98
809 0 SvGETMAGIC(*svp);
810 0 ▶ return SvOK(*svp) ? SvPV_nomg_nolen(*svp) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(*svp) ? SvPV_nomg_nolen(*svp) : NULL; 0 0 0
811 }
812  
813 /* The session_timezone a query is sent with: its own setting, else the
814 * connection's. Read once at enqueue, so later decoding never runs user
815 * code or sees the settings changed in flight. */
816 49 static char *query_session_tz(pTHX_ ev_clickhouse_t *self, HV *settings) {
817 49 const char *tz = hv_session_tz(aTHX_ settings);
818 49 ▶ if (!tz) tz = hv_session_tz(aTHX_ self->default_settings);
Branch: 1 branch
BranchTrueFalseTotal
if (!tz) tz = hv_session_tz(aTHX_ self->default_settings); 49 0 49
819 49 ▶ return tz ? safe_strdup(tz) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return tz ? safe_strdup(tz) : NULL; 0 49 49
820 }
821  
822 /* "" restores the server default, as on the server. */
823 14 static const char *effective_tz(ev_clickhouse_t *self, const char *query_tz) {
824 14 ▶ return query_tz && query_tz[0] ? query_tz : self->server_timezone;
Branch: 2 branches
BranchTrueFalseTotal
return query_tz && query_tz[0] ? query_tz : self->server_timezone; 0 14 14
return query_tz && query_tz[0] ? query_tz : self->server_timezone; 0 0 0
825 }
826  
827 /* Size in bytes for fixed-width types. Returns 0 for variable-width. */
828 12 static size_t col_type_fixed_size(col_type_t *t) {
829 12 switch (t->code) {
830 0 case CT_INT8: case CT_UINT8: case CT_ENUM8: case CT_BOOL: return 1;
831 0 case CT_INT16: case CT_UINT16: case CT_ENUM16:
832 0 case CT_DATE: case CT_BFLOAT16: return 2;
833 2 case CT_INT32: case CT_UINT32: case CT_FLOAT32:
834 case CT_DECIMAL32: case CT_DATE32: case CT_DATETIME:
835 2 case CT_IPV4: case CT_TIME: return 4;
836 10 case CT_INT64: case CT_UINT64: case CT_FLOAT64:
837 10 case CT_DECIMAL64: case CT_DATETIME64: case CT_TIME64: return 8;
838 0 case CT_UUID: case CT_DECIMAL128:
839 0 case CT_INT128: case CT_UINT128: case CT_IPV6: return 16;
840 0 case CT_INT256: case CT_UINT256: case CT_DECIMAL256: return 32;
841 0 case CT_FIXEDSTRING: return (size_t)t->param;
842 0 default: return 0;
843 }
844 }
845  
846 /* --- Decode helper functions for opt-in type formatting --- */
847  
848 /* Convert days since Unix epoch to "YYYY-MM-DD".
849 * Cast to int64_t before multiply: 32-bit time_t platforms would otherwise
850 * overflow for any date past 2038. */
851 0 static SV* days_to_date_sv(int32_t days) {
852 0 time_t t = (time_t)((int64_t)days * 86400);
853 struct tm tm;
854 char buf[11];
855 0 ▶ if (!gmtime_r(&t, &tm)) return newSVpvn("0000-00-00", 10);
Branch: 1 branch
BranchTrueFalseTotal
if (!gmtime_r(&t, &tm)) return newSVpvn("0000-00-00", 10); 0 0 0
856 0 snprintf(buf, sizeof(buf), "%04d-%02d-%02d",
857 0 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
858 0 return newSVpvn(buf, 10);
859 }
860  
861 /* Convert epoch seconds to "YYYY-MM-DD HH:MM:SS" in UTC. */
862 /* Format a UNIX epoch as "YYYY-MM-DD HH:MM:SS". use_local=1 formats in the
863 * caller's currently-active TZ (set via set_tz); otherwise UTC. */
864 0 static SV* epoch_to_datetime_sv_ex(uint32_t epoch, int use_local) {
865 0 time_t t = (time_t)epoch;
866 struct tm tm;
867 char buf[20];
868 0 ▶ struct tm *r = use_local ? localtime_r(&t, &tm) : gmtime_r(&t, &tm);
Branch: 1 branch
BranchTrueFalseTotal
struct tm *r = use_local ? localtime_r(&t, &tm) : gmtime_r(&t, &tm); 0 0 0
869 0 ▶ if (!r) return newSVpvn("0000-00-00 00:00:00", 19);
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return newSVpvn("0000-00-00 00:00:00", 19); 0 0 0
870 0 snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
871 0 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
872 tm.tm_hour, tm.tm_min, tm.tm_sec);
873 0 return newSVpvn(buf, 19);
874 }
875  
876 /* Convert DateTime64 to "YYYY-MM-DD HH:MM:SS.fff...", use_local=1 for localtime */
877 0 static SV* dt64_to_datetime_sv_ex(int64_t val, int precision, int use_local) {
878 0 int64_t scale = 1;
879 int p;
880 int64_t epoch, frac;
881 time_t t;
882 struct tm tm;
883 char buf[32];
884 int n;
885  
886 0 ▶ for (p = 0; p < precision; p++) scale *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < precision; p++) scale *= 10; 0 0 0
887 0 epoch = val / scale;
888 0 frac = val % scale;
889 0 ▶ if (frac < 0) { epoch--; frac += scale; }
Branch: 1 branch
BranchTrueFalseTotal
if (frac < 0) { epoch--; frac += scale; } 0 0 0
890  
891 0 t = (time_t)epoch;
892 0 ▶ if (use_local) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_local) { 0 0 0
893 0 ▶ if (!localtime_r(&t, &tm)) return newSVpvn("0000-00-00 00:00:00", 19);
Branch: 1 branch
BranchTrueFalseTotal
if (!localtime_r(&t, &tm)) return newSVpvn("0000-00-00 00:00:00", 19); 0 0 0
894 } else {
895 0 ▶ if (!gmtime_r(&t, &tm)) return newSVpvn("0000-00-00 00:00:00", 19);
Branch: 1 branch
BranchTrueFalseTotal
if (!gmtime_r(&t, &tm)) return newSVpvn("0000-00-00 00:00:00", 19); 0 0 0
896 }
897 0 n = snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
898 0 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
899 tm.tm_hour, tm.tm_min, tm.tm_sec);
900 0 ▶ if (precision > 0 && n < 30) {
Branch: 2 branches
BranchTrueFalseTotal
if (precision > 0 && n < 30) { 0 0 0
if (precision > 0 && n < 30) { 0 0 0
901 char fracbuf[16];
902 int fi;
903 0 snprintf(fracbuf, sizeof(fracbuf), "%0*lld", precision, (long long)frac);
904 0 buf[n++] = '.';
905 0 ▶ for (fi = 0; fi < precision && n < 31; fi++)
Branch: 2 branches
BranchTrueFalseTotal
for (fi = 0; fi < precision && n < 31; fi++) 0 0 0
for (fi = 0; fi < precision && n < 31; fi++) 0 0 0
906 0 buf[n++] = fracbuf[fi];
907 }
908 0 return newSVpvn(buf, n);
909 }
910  
911 /* Set TZ env var and tzset(); returns saved old TZ (caller frees).
912 * Safe because EV is single-threaded and the set_tz / decode / restore_tz
913 * window contains no Perl callback dispatch. */
914 18 static char* set_tz(const char *tz) {
915 18 char *saved = safe_strdup(getenv("TZ"));
916 18 setenv("TZ", tz, 1);
917 18 tzset();
918 18 return saved;
919 }
920  
921 8 static int tz_offset_at(int64_t t, int64_t *off) {
922 struct tm tm;
923 8 time_t tt = (time_t)t;
924 8 ▶ if ((int64_t)tt != t || !localtime_r(&tt, &tm)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if ((int64_t)tt != t || !localtime_r(&tt, &tm)) return 0; 8 0 8
if ((int64_t)tt != t || !localtime_r(&tt, &tm)) return 0; 0 8 8
925 8 *off = (int64_t)tm.tm_gmtoff;
926 8 return 1;
927 }
928  
929 /* Wall time given as seconds-as-if-UTC, read in the column tz (caller
930 * holds set_tz across the column). Like the server: in a DST fold the
931 * earlier instant, in a gap the offset in force after the transition. */
932 2 static int64_t wall_in_tz_to_epoch(int64_t utc_wall, const char *tz, int *ok) {
933 int64_t off_before, off_after, chk, t1, t2;
934 int v1, v2;
935 2 *ok = 0;
936 2 ▶ if (!tz || !tz[0]) { *ok = 1; return utc_wall; }
Branch: 2 branches
BranchTrueFalseTotal
if (!tz || !tz[0]) { *ok = 1; return utc_wall; } 2 0 2
if (!tz || !tz[0]) { *ok = 1; return utc_wall; } 0 2 2
937 2 ▶ if (!tz_offset_at(utc_wall - 86400, &off_before)
Branch: 1 branch
BranchTrueFalseTotal
if (!tz_offset_at(utc_wall - 86400, &off_before) 2 0 2
938 2 ▶ || !tz_offset_at(utc_wall + 86400, &off_after))
Branch: 1 branch
BranchTrueFalseTotal
|| !tz_offset_at(utc_wall + 86400, &off_after)) 0 2 2
939 0 return 0;
940 2 t1 = utc_wall - off_before;
941 2 t2 = utc_wall - off_after;
942 2 ▶ v1 = tz_offset_at(t1, &chk) && t1 + chk == utc_wall;
Branch: 2 branches
BranchTrueFalseTotal
v1 = tz_offset_at(t1, &chk) && t1 + chk == utc_wall; 2 0 2
v1 = tz_offset_at(t1, &chk) && t1 + chk == utc_wall; 2 0 2
943 2 ▶ v2 = tz_offset_at(t2, &chk) && t2 + chk == utc_wall;
Branch: 2 branches
BranchTrueFalseTotal
v2 = tz_offset_at(t2, &chk) && t2 + chk == utc_wall; 2 0 2
v2 = tz_offset_at(t2, &chk) && t2 + chk == utc_wall; 2 0 2
944 2 *ok = 1;
945 2 ▶ if (v1 && v2) return t1 < t2 ? t1 : t2;
Branch: 2 branches
BranchTrueFalseTotal
if (v1 && v2) return t1 < t2 ? t1 : t2; 2 0 2
if (v1 && v2) return t1 < t2 ? t1 : t2; 2 0 2
946 0 ▶ return v1 ? t1 : t2;
Branch: 1 branch
BranchTrueFalseTotal
return v1 ? t1 : t2; 0 0 0
947 }
948  
949 /* Restore TZ from saved value (which may be NULL), then free saved */
950 18 static void restore_tz(char *saved) {
951 18 ▶ if (saved) {
Branch: 1 branch
BranchTrueFalseTotal
if (saved) { 0 18 18
952 0 setenv("TZ", saved, 1);
953 0 Safefree(saved);
954 } else {
955 18 unsetenv("TZ");
956 }
957 18 tzset();
958 18 }
959  
960 18 static void restore_tz_dtor(pTHX_ void *saved) {
961 18 restore_tz((char *)saved);
962 18 }
963  
964 /* set_tz() undone by the enclosing LEAVE, also when a croak in user code
965 * (an overloaded value) unwinds past it. */
966 18 static void set_tz_scoped(pTHX_ const char *tz) {
967 18 SAVEDESTRUCTOR_X(restore_tz_dtor, set_tz(tz));
968 18 }
969  
970 /* Compute 10^n as double */
971 0 static double pow10_int(int n) {
972 0 double r = 1.0;
973 int i;
974 0 ▶ for (i = 0; i < n; i++) r *= 10.0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) r *= 10.0; 0 0 0
975 0 return r;
976 }
977  
978 /* Parse enum label for a given code from type string like "Enum8('a'=1,'b'=2)" */
979 0 static SV* enum_label_for_code(const char *type_str, size_t type_str_len, int code) {
980 /* Find the opening '(' */
981 0 const char *p = memchr(type_str, '(', type_str_len);
982 const char *end;
983 0 ▶ if (!p) return newSViv(code);
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return newSViv(code); 0 0 0
984 0 p++;
985 0 end = type_str + type_str_len - 1; /* skip closing ')' */
986  
987 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
988 /* Skip whitespace */
989 0 ▶ while (p < end && *p == ' ') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == ' ') p++; 0 0 0
while (p < end && *p == ' ') p++; 0 0 0
990 0 ▶ if (p >= end || *p != '\'') break;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '\'') break; 0 0 0
if (p >= end || *p != '\'') break; 0 0 0
991 0 p++; /* skip opening quote */
992  
993 /* Read label (handle escaped quotes) */
994 {
995 0 const char *label_start = p;
996 size_t label_len;
997 int val;
998  
999 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1000 0 ▶ if (*p == '\\' && p + 1 < end) { p += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\' && p + 1 < end) { p += 2; continue; } 0 0 0
if (*p == '\\' && p + 1 < end) { p += 2; continue; } 0 0 0
1001 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
1002 0 ▶ if (p + 1 < end && *(p+1) == '\'') { p += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (p + 1 < end && *(p+1) == '\'') { p += 2; continue; } 0 0 0
if (p + 1 < end && *(p+1) == '\'') { p += 2; continue; } 0 0 0
1003 0 break;
1004 }
1005 0 p++;
1006 }
1007 0 label_len = p - label_start;
1008 0 ▶ if (p < end) p++; /* skip closing quote */
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; /* skip closing quote */ 0 0 0
1009  
1010 /* Skip ' = ' */
1011 0 ▶ while (p < end && (*p == ' ' || *p == '=')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '=')) p++; 0 0 0
while (p < end && (*p == ' ' || *p == '=')) p++; 0 0 0
while (p < end && (*p == ' ' || *p == '=')) p++; 0 0 0
1012  
1013 /* Read integer value */
1014 0 val = (int)strtol(p, NULL, 10);
1015  
1016 0 ▶ if (val == code) {
Branch: 1 branch
BranchTrueFalseTotal
if (val == code) { 0 0 0
1017 /* Labels carry C-style escapes on the wire (\' \\ \t
1018 * ...) and possibly '' doubling: restore both. */
1019 0 const char *r = label_start, *span_end, *q, *span;
1020 char *out, *w;
1021 0 span = label_start + label_len;
1022 0 ▶ for (q = label_start; q < span; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = label_start; q < span; q++) { 0 0 0
1023 0 ▶ if (*q == '\\') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\\') break; 0 0 0
1024 0 ▶ if (*q == '\'' && q + 1 < span && q[1] == '\'') break;
Branch: 3 branches
BranchTrueFalseTotal
if (*q == '\'' && q + 1 < span && q[1] == '\'') break; 0 0 0
if (*q == '\'' && q + 1 < span && q[1] == '\'') break; 0 0 0
if (*q == '\'' && q + 1 < span && q[1] == '\'') break; 0 0 0
1025 }
1026 0 ▶ if (q >= span)
Branch: 1 branch
BranchTrueFalseTotal
if (q >= span) 0 0 0
1027 0 return newSVpvn(label_start, label_len);
1028 0 Newx(out, label_len + 1, char);
1029 0 w = out;
1030 0 span_end = span;
1031 0 ▶ while (r < span_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < span_end) { 0 0 0
1032 0 ▶ if (*r == '\\' && r + 1 < span_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (*r == '\\' && r + 1 < span_end) { 0 0 0
if (*r == '\\' && r + 1 < span_end) { 0 0 0
1033 0 char e = *++r;
1034 0 switch (e) {
1035 0 case 't': *w++ = '\t'; break;
1036 0 case 'n': *w++ = '\n'; break;
1037 0 case 'r': *w++ = '\r'; break;
1038 0 case 'b': *w++ = '\b'; break;
1039 0 case 'f': *w++ = '\f'; break;
1040 0 case 'v': *w++ = '\v'; break;
1041 0 case '0': *w++ = '\0'; break;
1042 0 default: *w++ = e; break;
1043 }
1044 0 r++;
1045 0 continue;
1046 }
1047 0 ▶ if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++;
Branch: 3 branches
BranchTrueFalseTotal
if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++; 0 0 0
if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++; 0 0 0
if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++; 0 0 0
1048 0 *w++ = *r++;
1049 }
1050 {
1051 0 SV *sv = newSVpvn(out, w - out);
1052 0 Safefree(out);
1053 0 return sv;
1054 }
1055 }
1056  
1057 /* Skip to next entry */
1058 0 ▶ while (p < end && *p != ',') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != ',') p++; 0 0 0
while (p < end && *p != ',') p++; 0 0 0
1059 0 ▶ if (p < end) p++; /* skip comma */
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; /* skip comma */ 0 0 0
1060 }
1061 }
1062 /* Not found — return numeric code */
1063 0 return newSViv(code);
1064 }
1065  
1066 /* Decoded Enum entry for label lookup on insert. */
1067 typedef struct {
1068 char *label;
1069 size_t len;
1070 int code;
1071 } enum_entry_t;
1072  
1073 /* Parse "Enum8('a' = 1, ...)" once per column into decoded entries.
1074 * Mirrors enum_label_for_code's label scanning (C escapes, '' doubling).
1075 * Returns the entry count; free with enum_free_entries. */
1076 0 static size_t enum_parse_entries(const char *type_str, size_t type_str_len,
1077 enum_entry_t **out) {
1078 const char *p, *end;
1079 0 size_t n = 0, cap = 0;
1080 0 enum_entry_t *entries = NULL;
1081 0 *out = NULL;
1082 0 ▶ if (!type_str) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!type_str) return 0; 0 0 0
1083 0 p = memchr(type_str, '(', type_str_len);
1084 0 ▶ if (!p) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return 0; 0 0 0
1085 0 p++;
1086 0 end = type_str + type_str_len - 1;
1087 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1088 const char *label_start, *span_end, *r;
1089 0 size_t raw_len, dlen = 0;
1090 0 char *dec = NULL;
1091 int code;
1092 0 ▶ while (p < end && *p == ' ') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == ' ') p++; 0 0 0
while (p < end && *p == ' ') p++; 0 0 0
1093 0 ▶ if (p >= end || *p != '\'') break;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '\'') break; 0 0 0
if (p >= end || *p != '\'') break; 0 0 0
1094 0 p++;
1095 0 label_start = p;
1096 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1097 0 ▶ if (*p == '\\' && p + 1 < end) { p += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\' && p + 1 < end) { p += 2; continue; } 0 0 0
if (*p == '\\' && p + 1 < end) { p += 2; continue; } 0 0 0
1098 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
1099 0 ▶ if (p + 1 < end && *(p+1) == '\'') { p += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (p + 1 < end && *(p+1) == '\'') { p += 2; continue; } 0 0 0
if (p + 1 < end && *(p+1) == '\'') { p += 2; continue; } 0 0 0
1100 0 break;
1101 }
1102 0 p++;
1103 }
1104 0 raw_len = p - label_start;
1105 0 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 0 0 0
1106 0 ▶ while (p < end && (*p == ' ' || *p == '=')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '=')) p++; 0 0 0
while (p < end && (*p == ' ' || *p == '=')) p++; 0 0 0
while (p < end && (*p == ' ' || *p == '=')) p++; 0 0 0
1107 0 code = (int)strtol(p, NULL, 10);
1108 0 ▶ if (raw_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (raw_len > 0) { 0 0 0
1109 0 Newx(dec, raw_len, char);
1110 0 span_end = label_start + raw_len;
1111 0 r = label_start;
1112 0 ▶ while (r < span_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < span_end) { 0 0 0
1113 0 ▶ if (*r == '\\' && r + 1 < span_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (*r == '\\' && r + 1 < span_end) { 0 0 0
if (*r == '\\' && r + 1 < span_end) { 0 0 0
1114 0 char e = *++r;
1115 0 switch (e) {
1116 0 case 't': dec[dlen++] = '\t'; break;
1117 0 case 'n': dec[dlen++] = '\n'; break;
1118 0 case 'r': dec[dlen++] = '\r'; break;
1119 0 case 'b': dec[dlen++] = '\b'; break;
1120 0 case 'f': dec[dlen++] = '\f'; break;
1121 0 case 'v': dec[dlen++] = '\v'; break;
1122 0 case '0': dec[dlen++] = '\0'; break;
1123 0 default: dec[dlen++] = e; break;
1124 }
1125 0 r++;
1126 0 continue;
1127 }
1128 0 ▶ if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++;
Branch: 3 branches
BranchTrueFalseTotal
if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++; 0 0 0
if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++; 0 0 0
if (*r == '\'' && r + 1 < span_end && r[1] == '\'') r++; 0 0 0
1129 0 dec[dlen++] = *r++;
1130 }
1131 }
1132 0 ▶ if (n == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == cap) { 0 0 0
1133 0 ▶ cap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 8; 0 0 0
1134 0 ▶ Renew(entries, cap, enum_entry_t);
Branch: 1 branch
BranchTrueFalseTotal
Renew(entries, cap, enum_entry_t); 0 0 0
1135 }
1136 0 entries[n].label = dec;
1137 0 entries[n].len = dlen;
1138 0 entries[n].code = code;
1139 0 n++;
1140 0 ▶ while (p < end && *p != ',') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != ',') p++; 0 0 0
while (p < end && *p != ',') p++; 0 0 0
1141 0 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 0 0 0
1142 }
1143 0 *out = entries;
1144 0 return n;
1145 }
1146  
1147 0 static void enum_free_entries(enum_entry_t *entries, size_t n) {
1148 size_t i;
1149 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
1150 0 Safefree(entries[i].label);
1151 0 Safefree(entries);
1152 0 }
1153  
1154 0 static int enum_code_for_label(const enum_entry_t *entries, size_t n,
1155 const char *s, size_t len, int *code_out) {
1156 size_t i;
1157 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
1158 0 ▶ if (entries[i].len == len
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i].len == len 0 0 0
1159 0 ▶ && (len == 0 || memcmp(entries[i].label, s, len) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (len == 0 || memcmp(entries[i].label, s, len) == 0)) { 0 0 0
&& (len == 0 || memcmp(entries[i].label, s, len) == 0)) { 0 0 0
1160 0 *code_out = entries[i].code;
1161 0 return 1;
1162 }
1163 }
1164 0 return 0;
1165 }
1166  
1167 /*
1168 * Decode a column of `nrows` values from the native binary format.
1169 * Returns an array of SVs (one per row). Returns NULL on failure.
1170 * Sets *decode_err=1 on definitive errors (vs needing more data).
1171 * Advances *pos past the consumed bytes.
1172 */
1173  
1174 #ifdef __SIZEOF_INT128__
1175 0 static SV* int128_to_sv(const char *p, int is_signed) {
1176 unsigned __int128 uv;
1177 char dbuf[42];
1178 0 int dlen = 0, neg = 0, k;
1179 0 ▶ if (is_signed) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_signed) { 0 0 0
1180 __int128 sv;
1181 0 memcpy(&sv, p, 16);
1182 0 neg = sv < 0;
1183 0 ▶ uv = neg ? -(unsigned __int128)sv : (unsigned __int128)sv;
Branch: 1 branch
BranchTrueFalseTotal
uv = neg ? -(unsigned __int128)sv : (unsigned __int128)sv; 0 0 0
1184 } else {
1185 0 memcpy(&uv, p, 16);
1186 }
1187 do {
1188 0 dbuf[dlen++] = '0' + (int)(uv % 10);
1189 0 uv /= 10;
1190 0 ▶ } while (uv);
Branch: 1 branch
BranchTrueFalseTotal
} while (uv); 0 0 0
1191 0 ▶ if (neg) dbuf[dlen++] = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) dbuf[dlen++] = '-'; 0 0 0
1192 0 ▶ for (k = 0; k < dlen/2; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < dlen/2; k++) { 0 0 0
1193 0 char tmp = dbuf[k]; dbuf[k] = dbuf[dlen-1-k]; dbuf[dlen-1-k] = tmp;
1194 }
1195 0 return newSVpvn(dbuf, dlen);
1196 }
1197 #endif
1198  
1199 /* Convert a 256-bit LE unsigned integer (as 4 x uint64_t) to decimal string.
1200 * Works on all platforms (no __int128 required). */
1201 0 static SV* uint256_to_sv(const char *p) {
1202 /* Copy into 4 x uint64_t LE limbs: v[0] = lowest */
1203 uint64_t v[4];
1204 char dbuf[80];
1205 0 int dlen = 0, k;
1206  
1207 0 memcpy(v, p, 32);
1208  
1209 /* Handle zero */
1210 0 ▶ if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0)
Branch: 4 branches
BranchTrueFalseTotal
if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0) 0 0 0
if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0) 0 0 0
if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0) 0 0 0
if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0) 0 0 0
1211 0 return newSVpvn("0", 1);
1212  
1213 /* Repeatedly divide by 10, collecting remainders */
1214 0 ▶ while (v[0] || v[1] || v[2] || v[3]) {
Branch: 4 branches
BranchTrueFalseTotal
while (v[0] || v[1] || v[2] || v[3]) { 0 0 0
while (v[0] || v[1] || v[2] || v[3]) { 0 0 0
while (v[0] || v[1] || v[2] || v[3]) { 0 0 0
while (v[0] || v[1] || v[2] || v[3]) { 0 0 0
1215 0 uint64_t rem = 0;
1216 int i;
1217 0 ▶ for (i = 3; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i >= 0; i--) { 0 0 0
1218 #ifdef __SIZEOF_INT128__
1219 0 unsigned __int128 cur = ((unsigned __int128)rem << 64) | v[i];
1220 0 v[i] = (uint64_t)(cur / 10);
1221 0 rem = (uint64_t)(cur % 10);
1222 #else
1223 /* Without 128-bit: split each 64-bit limb into hi32:lo32 */
1224 uint64_t hi = (rem << 32) | (v[i] >> 32);
1225 uint64_t q_hi = hi / 10;
1226 uint64_t r_hi = hi % 10;
1227 uint64_t lo = (r_hi << 32) | (v[i] & 0xFFFFFFFFULL);
1228 uint64_t q_lo = lo / 10;
1229 rem = lo % 10;
1230 v[i] = (q_hi << 32) | q_lo;
1231 #endif
1232 }
1233 0 dbuf[dlen++] = '0' + (int)rem;
1234 }
1235 0 ▶ for (k = 0; k < dlen/2; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < dlen/2; k++) { 0 0 0
1236 0 char tmp = dbuf[k]; dbuf[k] = dbuf[dlen-1-k]; dbuf[dlen-1-k] = tmp;
1237 }
1238 0 return newSVpvn(dbuf, dlen);
1239 }
1240  
1241 0 static SV* int256_to_sv(const char *p, int is_signed) {
1242 0 ▶ if (is_signed && ((unsigned char)p[31] & 0x80)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_signed && ((unsigned char)p[31] & 0x80)) { 0 0 0
if (is_signed && ((unsigned char)p[31] & 0x80)) { 0 0 0
1243 /* Negative: two's complement negate, format, prepend '-' */
1244 unsigned char neg[32];
1245 0 int i, carry = 1;
1246 SV *sv;
1247 STRLEN svlen;
1248 char *s;
1249 0 ▶ for (i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) { 0 0 0
1250 0 int b = (unsigned char)(~((unsigned char)p[i])) + carry;
1251 0 neg[i] = (unsigned char)(b & 0xFF);
1252 0 carry = b >> 8;
1253 }
1254 0 sv = uint256_to_sv((const char *)neg);
1255 /* Prepend '-' */
1256 0 s = SvPV(sv, svlen);
1257 {
1258 0 SV *result = newSV(svlen + 1);
1259 0 SvPOK_on(result);
1260 0 SvCUR_set(result, svlen + 1);
1261 0 *SvPVX(result) = '-';
1262 0 Copy(s, SvPVX(result) + 1, svlen, char);
1263 0 SvPVX(result)[svlen + 1] = '\0';
1264 0 SvREFCNT_dec(sv);
1265 0 return result;
1266 }
1267 }
1268 0 return uint256_to_sv(p);
1269 }
1270  
1271 /* Native sends every serialization state prefix of a column's type tree,
1272 * depth first, before any of its data; the only one we meet outside JSON
1273 * is the LowCardinality keys version. 1 = ok, 0 = need more, -1 = bad. */
1274 0 static int read_col_prefix(const char *buf, size_t len, size_t *pos,
1275 col_type_t *ct) {
1276 int i, rc;
1277 0 switch (ct->code) {
1278 0 case CT_LOWCARDINALITY: {
1279 uint64_t version;
1280 0 ▶ if (*pos > len || len - *pos < 8) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8) return 0; 0 0 0
if (*pos > len || len - *pos < 8) return 0; 0 0 0
1281 0 memcpy(&version, buf + *pos, 8);
1282 0 ▶ if (version != 1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (version != 1) return -1; 0 0 0
1283 0 *pos += 8;
1284 0 return 1;
1285 }
1286 0 case CT_ARRAY:
1287 case CT_NULLABLE:
1288 0 ▶ return ct->inner ? read_col_prefix(buf, len, pos, ct->inner) : 1;
Branch: 1 branch
BranchTrueFalseTotal
return ct->inner ? read_col_prefix(buf, len, pos, ct->inner) : 1; 0 0 0
1289 0 case CT_TUPLE:
1290 case CT_MAP:
1291 0 ▶ for (i = 0; i < ct->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ct->num_inners; i++) 0 0 0
1292 0 ▶ if ((rc = read_col_prefix(buf, len, pos, ct->inners[i])) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((rc = read_col_prefix(buf, len, pos, ct->inners[i])) <= 0) 0 0 0
1293 0 return rc;
1294 0 return 1;
1295 0 default:
1296 0 return 1;
1297 }
1298 }
1299  
1300 static SV** decode_column_data(const char *buf, size_t len, size_t *pos,
1301 uint64_t nrows, col_type_t *ct, int *decode_err,
1302 uint32_t decode_flags, ev_clickhouse_t *lc_self,
1303 int lc_col_idx);
1304  
1305 0 static SV** decode_data(const char *buf, size_t len, size_t *pos,
1306 uint64_t nrows, col_type_t *ct, int *decode_err,
1307 uint32_t decode_flags) {
1308 0 return decode_column_data(buf, len, pos, nrows, ct, decode_err, decode_flags, NULL, -1);
1309 }
1310  
1311 /* One whole column of a block: prefixes, then data. */
1312 0 static SV** decode_column_ex(const char *buf, size_t len, size_t *pos,
1313 uint64_t nrows, col_type_t *ct, int *decode_err,
1314 uint32_t decode_flags, ev_clickhouse_t *lc_self,
1315 int lc_col_idx) {
1316 0 ▶ if (nrows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows) { 0 0 0
1317 0 int rc = read_col_prefix(buf, len, pos, ct);
1318 0 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 0 0 0
1319 0 ▶ if (rc < 0 && decode_err) *decode_err = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (rc < 0 && decode_err) *decode_err = 1; 0 0 0
if (rc < 0 && decode_err) *decode_err = 1; 0 0 0
1320 0 return NULL;
1321 }
1322 }
1323 0 return decode_column_data(buf, len, pos, nrows, ct, decode_err, decode_flags,
1324 lc_self, lc_col_idx);
1325 }
1326  
1327 0 static SV** decode_column(const char *buf, size_t len, size_t *pos,
1328 uint64_t nrows, col_type_t *ct, int *decode_err,
1329 uint32_t decode_flags) {
1330 0 return decode_column_ex(buf, len, pos, nrows, ct, decode_err, decode_flags, NULL, -1);
1331 }
1332  
1333 0 static SV** decode_column_data(const char *buf, size_t len, size_t *pos,
1334 uint64_t nrows, col_type_t *ct, int *decode_err,
1335 uint32_t decode_flags, ev_clickhouse_t *lc_self,
1336 int lc_col_idx) {
1337 SV **out;
1338 uint64_t i;
1339 size_t fsz;
1340  
1341 /* Bound nrows against the remaining buffer before allocating out[].
1342 * Every wire column consumes at least one byte per row (a value, an
1343 * offset, a null-map byte or an index), so a row count larger than the
1344 * bytes left cannot be satisfied yet. Return NULL WITHOUT setting
1345 * decode_err -- exactly the per-type need-more convention -- so the caller
1346 * treats it as "need more data" on the incremental (uncompressed) path and
1347 * as a hard error on the compressed path (where the whole block is already
1348 * present). This caps the out[] allocation to the available data and
1349 * stops a hostile server forcing a huge alloc from a tiny message, while
1350 * also covering recursive Array/Map/Tuple/LowCardinality element counts.
1351 * No exception for FixedString(0): real servers reject it outright,
1352 * so it can only arrive from a hostile one. (An empty Tuple() takes
1353 * a 1-byte-per-row placeholder on the wire, so the bound holds.) */
1354 0 ▶ if (*pos > len) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos > len) return NULL; 0 0 0
1355 0 ▶ if (nrows > (uint64_t)(len - *pos)) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > (uint64_t)(len - *pos)) { 0 0 0
1356 0 return NULL;
1357 }
1358  
1359 0 ▶ Newxz(out, nrows ? nrows : 1, SV*);
Branch: 3 branches
BranchTrueFalseTotal
Newxz(out, nrows ? nrows : 1, SV*); 0 0 0
Newxz(out, nrows ? nrows : 1, SV*); 0 0 0
Newxz(out, nrows ? nrows : 1, SV*); 0 0 0
1360  
1361 0 ▶ if (ct->code == CT_NOTHING) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_NOTHING) { 0 0 0
1362 /* Nothing type: 1 placeholder byte ('0') per row */
1363 0 ▶ if (*pos > len || nrows > len - *pos) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || nrows > len - *pos) goto fail; 0 0 0
if (*pos > len || nrows > len - *pos) goto fail; 0 0 0
1364 0 *pos += nrows;
1365 0 ▶ for (i = 0; i < nrows; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) 0 0 0
1366 0 out[i] = newSV(0);
1367 0 return out;
1368 }
1369  
1370 0 ▶ if (ct->code == CT_VARIANT || ct->code == CT_DYNAMIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_VARIANT || ct->code == CT_DYNAMIC) { 0 0 0
if (ct->code == CT_VARIANT || ct->code == CT_DYNAMIC) { 0 0 0
1371 /* The wire format is per-version and includes shared substream
1372 * dispatch we don't replicate here. Set decode_err so the calling
1373 * parser surfaces a clean error and tears the connection down,
1374 * instead of trying to read garbage strings (which would desync
1375 * every subsequent column). Recommend a server-side workaround. */
1376 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1377 0 goto fail;
1378 }
1379  
1380 0 ▶ if (ct->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_NULLABLE) { 0 0 0
1381 /* null bitmap: nrows bytes of UInt8 */
1382 uint8_t *nulls;
1383 SV **inner;
1384 0 ▶ if (*pos > len || nrows > len - *pos) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || nrows > len - *pos) goto fail; 0 0 0
if (*pos > len || nrows > len - *pos) goto fail; 0 0 0
1385 0 Newx(nulls, nrows, uint8_t);
1386 0 Copy(buf + *pos, nulls, nrows, uint8_t);
1387 0 *pos += nrows;
1388  
1389 /* decode inner column */
1390 0 inner = decode_data(buf, len, pos, nrows, ct->inner, decode_err, decode_flags);
1391 0 ▶ if (!inner) { Safefree(nulls); goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!inner) { Safefree(nulls); goto fail; } 0 0 0
1392  
1393 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1394 0 ▶ if (nulls[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (nulls[i]) { 0 0 0
1395 0 SvREFCNT_dec(inner[i]);
1396 0 out[i] = newSV(0); /* undef */
1397 } else {
1398 0 out[i] = inner[i];
1399 }
1400 }
1401 0 Safefree(nulls);
1402 0 Safefree(inner);
1403 0 return out;
1404 }
1405  
1406 0 ▶ if (ct->code == CT_LOWCARDINALITY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_LOWCARDINALITY) { 0 0 0
1407 /*
1408 * LowCardinality wire format (all multi-byte integers are UInt64 LE):
1409 * PREFIX: UInt64 key_version, read by read_col_prefix()
1410 * DATA: UInt64 serialization_type (bits 0-7: index type,
1411 * bit 8: NeedGlobalDictionary, bit 9: HasAdditionalKeys,
1412 * bit 10: NeedUpdateDictionary)
1413 * if NeedUpdateDictionary: UInt64 num_keys + dictionary data
1414 * UInt64 num_indices + index data
1415 * Zero rows (an empty nested array) carry no DATA at all.
1416 */
1417 0 uint64_t ser_type, num_keys = 0, num_indices;
1418 0 size_t saved = *pos;
1419 int key_type;
1420 size_t idx_size;
1421 0 SV **dict = NULL;
1422 0 int dict_borrowed = 0; /* 1 if dict points to lc_self storage */
1423 /* LC(Nullable(T)) dictionaries travel as plain T with slot 0
1424 * reserved for NULL (the server synthesizes the null map). */
1425 0 ▶ int dict_nullable = ct->inner && ct->inner->code == CT_NULLABLE;
Branch: 2 branches
BranchTrueFalseTotal
int dict_nullable = ct->inner && ct->inner->code == CT_NULLABLE; 0 0 0
int dict_nullable = ct->inner && ct->inner->code == CT_NULLABLE; 0 0 0
1426 0 ▶ col_type_t *dict_ct = dict_nullable ? ct->inner->inner : ct->inner;
Branch: 1 branch
BranchTrueFalseTotal
col_type_t *dict_ct = dict_nullable ? ct->inner->inner : ct->inner; 0 0 0
1427  
1428 0 ▶ if (nrows == 0) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return out; 0 0 0
1429  
1430 /* serialization_type: UInt64 */
1431 0 ▶ if (*pos + 8 > len) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos + 8 > len) goto lc_fail; 0 0 0
1432 0 memcpy(&ser_type, buf + *pos, 8); *pos += 8;
1433  
1434 0 key_type = (int)(ser_type & 0xFF);
1435 /* key_type: 0=UInt8, 1=UInt16, 2=UInt32, 3=UInt64 */
1436 0 ▶ if (key_type > 3 || (ser_type & ~(uint64_t)0x6FF)) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_type > 3 || (ser_type & ~(uint64_t)0x6FF)) { 0 0 0
if (key_type > 3 || (ser_type & ~(uint64_t)0x6FF)) { 0 0 0
1437 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1438 0 goto lc_fail;
1439 }
1440  
1441 /* Read dictionary if NeedUpdateDictionary (bit 10) */
1442 0 ▶ if (ser_type & (1ULL << 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ser_type & (1ULL << 10)) { 0 0 0
1443 0 ▶ if (*pos + 8 > len) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos + 8 > len) goto lc_fail; 0 0 0
1444 0 memcpy(&num_keys, buf + *pos, 8); *pos += 8;
1445  
1446 0 dict = decode_data(buf, len, pos, num_keys, dict_ct, decode_err, decode_flags);
1447 0 ▶ if (!dict) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!dict) goto lc_fail; 0 0 0
1448 } else {
1449 /* NeedUpdateDictionary=0: reuse dictionary from prior block */
1450 0 ▶ if (lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols
Branch: 3 branches
BranchTrueFalseTotal
if (lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols 0 0 0
if (lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols 0 0 0
if (lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols 0 0 0
1451 0 ▶ && lc_self->lc_dicts[lc_col_idx]) {
Branch: 1 branch
BranchTrueFalseTotal
&& lc_self->lc_dicts[lc_col_idx]) { 0 0 0
1452 0 dict = lc_self->lc_dicts[lc_col_idx];
1453 0 num_keys = lc_self->lc_dict_sizes[lc_col_idx];
1454 0 dict_borrowed = 1;
1455 } else {
1456 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1457 0 goto lc_fail;
1458 }
1459 }
1460  
1461 /* Read indices: UInt64 num_indices + index data */
1462 0 ▶ if (*pos + 8 > len) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos + 8 > len) goto lc_fail; 0 0 0
1463 0 memcpy(&num_indices, buf + *pos, 8); *pos += 8;
1464  
1465 0 ▶ idx_size = (key_type == 0) ? 1 : (key_type == 1) ? 2 :
Branch: 3 branches
BranchTrueFalseTotal
idx_size = (key_type == 0) ? 1 : (key_type == 1) ? 2 : 0 0 0
idx_size = (key_type == 0) ? 1 : (key_type == 1) ? 2 : 0 0 0
idx_size = (key_type == 0) ? 1 : (key_type == 1) ? 2 : 0 0 0
1466 (key_type == 2) ? 4 : 8;
1467 0 ▶ if (num_indices != nrows) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_indices != nrows) { 0 0 0
1468 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1469 0 goto lc_fail;
1470 }
1471 0 ▶ if (*pos > len || num_indices > (len - *pos) / idx_size) goto lc_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || num_indices > (len - *pos) / idx_size) goto lc_fail; 0 0 0
if (*pos > len || num_indices > (len - *pos) / idx_size) goto lc_fail; 0 0 0
1472 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1473 0 uint64_t idx = 0;
1474 0 memcpy(&idx, buf + *pos + i * idx_size, idx_size);
1475 0 ▶ if (idx >= num_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= num_keys) { 0 0 0
1476 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1477 0 goto lc_fail;
1478 }
1479 }
1480  
1481 /* Store new dictionary for cross-block reuse (after validation) */
1482 0 ▶ if (!dict_borrowed && lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols) {
Branch: 4 branches
BranchTrueFalseTotal
if (!dict_borrowed && lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols) { 0 0 0
if (!dict_borrowed && lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols) { 0 0 0
if (!dict_borrowed && lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols) { 0 0 0
if (!dict_borrowed && lc_self && lc_col_idx >= 0 && lc_col_idx < lc_self->lc_num_cols) { 0 0 0
1483 0 ▶ if (lc_self->lc_dicts[lc_col_idx]) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc_self->lc_dicts[lc_col_idx]) { 0 0 0
1484 uint64_t di;
1485 0 ▶ for (di = 0; di < lc_self->lc_dict_sizes[lc_col_idx]; di++)
Branch: 1 branch
BranchTrueFalseTotal
for (di = 0; di < lc_self->lc_dict_sizes[lc_col_idx]; di++) 0 0 0
1486 0 SvREFCNT_dec(lc_self->lc_dicts[lc_col_idx][di]);
1487 0 Safefree(lc_self->lc_dicts[lc_col_idx]);
1488 }
1489 SV **dcopy;
1490 0 ▶ Newx(dcopy, num_keys > 0 ? num_keys : 1, SV*);
Branch: 3 branches
BranchTrueFalseTotal
Newx(dcopy, num_keys > 0 ? num_keys : 1, SV*); 0 0 0
Newx(dcopy, num_keys > 0 ? num_keys : 1, SV*); 0 0 0
Newx(dcopy, num_keys > 0 ? num_keys : 1, SV*); 0 0 0
1491 0 ▶ for (i = 0; i < num_keys; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_keys; i++) 0 0 0
1492 0 dcopy[i] = SvREFCNT_inc(dict[i]);
1493 0 lc_self->lc_dicts[lc_col_idx] = dcopy;
1494 0 lc_self->lc_dict_sizes[lc_col_idx] = num_keys;
1495 }
1496  
1497 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1498 0 uint64_t idx = 0;
1499 0 memcpy(&idx, buf + *pos + i * idx_size, idx_size);
1500 0 ▶ if (dict_nullable && idx == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (dict_nullable && idx == 0) 0 0 0
if (dict_nullable && idx == 0) 0 0 0
1501 0 out[i] = newSV(0);
1502 else
1503 0 out[i] = SvREFCNT_inc(dict[idx]);
1504 }
1505 0 *pos += num_indices * idx_size;
1506  
1507 0 ▶ if (dict && !dict_borrowed) {
Branch: 2 branches
BranchTrueFalseTotal
if (dict && !dict_borrowed) { 0 0 0
if (dict && !dict_borrowed) { 0 0 0
1508 0 ▶ for (i = 0; i < num_keys; i++) SvREFCNT_dec(dict[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_keys; i++) SvREFCNT_dec(dict[i]); 0 0 0
1509 0 Safefree(dict);
1510 }
1511 0 return out;
1512  
1513 0 lc_fail:
1514 0 ▶ if (dict && !dict_borrowed) {
Branch: 2 branches
BranchTrueFalseTotal
if (dict && !dict_borrowed) { 0 0 0
if (dict && !dict_borrowed) { 0 0 0
1515 0 ▶ for (i = 0; i < num_keys; i++) SvREFCNT_dec(dict[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_keys; i++) SvREFCNT_dec(dict[i]); 0 0 0
1516 0 Safefree(dict);
1517 }
1518 0 *pos = saved;
1519 0 goto fail;
1520 }
1521  
1522 0 ▶ if (ct->code == CT_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_STRING) { 0 0 0
1523 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1524 const char *s;
1525 size_t slen;
1526 0 ▶ if (read_native_string_ref(buf, len, pos, &s, &slen) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, pos, &s, &slen) <= 0) { 0 0 0
1527 /* clean up already-created SVs */
1528 uint64_t j;
1529 0 ▶ for (j = 0; j < i; j++) SvREFCNT_dec(out[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) SvREFCNT_dec(out[j]); 0 0 0
1530 0 goto fail;
1531 }
1532 0 out[i] = newSVpvn(s, slen);
1533 }
1534 0 return out;
1535 }
1536  
1537 0 ▶ if (ct->code == CT_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_ARRAY) { 0 0 0
1538 /* offsets: nrows x UInt64 */
1539 uint64_t *offsets;
1540 SV **elems;
1541 uint64_t total, prev;
1542  
1543 0 ▶ if (*pos > len || nrows > (len - *pos) / 8) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || nrows > (len - *pos) / 8) goto fail; 0 0 0
if (*pos > len || nrows > (len - *pos) / 8) goto fail; 0 0 0
1544 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
1545 0 ▶ Copy(buf + *pos, offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Copy(buf + *pos, offsets, nrows, uint64_t); 0 0 0
1546 0 *pos += nrows * 8;
1547  
1548 /* validate offset monotonicity */
1549 0 prev = 0;
1550 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1551 0 ▶ if (offsets[i] < prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (offsets[i] < prev) { 0 0 0
1552 0 Safefree(offsets);
1553 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1554 0 goto fail;
1555 }
1556 0 prev = offsets[i];
1557 }
1558  
1559 0 ▶ total = nrows > 0 ? offsets[nrows - 1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
total = nrows > 0 ? offsets[nrows - 1] : 0; 0 0 0
1560  
1561 /* decode all inner elements */
1562 0 elems = decode_data(buf, len, pos, total, ct->inner, decode_err, decode_flags);
1563 0 ▶ if (!elems) { Safefree(offsets); goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!elems) { Safefree(offsets); goto fail; } 0 0 0
1564  
1565 /* build AV for each row */
1566 0 prev = 0;
1567 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1568 0 uint64_t count = offsets[i] - prev;
1569 0 AV *av = newAV();
1570 uint64_t j;
1571 0 ▶ if (count > 0) av_extend(av, count - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) av_extend(av, count - 1); 0 0 0
1572 0 ▶ for (j = 0; j < count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) { 0 0 0
1573 0 av_push(av, elems[prev + j]);
1574 }
1575 0 out[i] = newRV_noinc((SV*)av);
1576 0 prev = offsets[i];
1577 }
1578  
1579 0 Safefree(offsets);
1580 0 Safefree(elems);
1581 0 return out;
1582 }
1583  
1584 0 ▶ if (ct->code == CT_TUPLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_TUPLE) { 0 0 0
1585 /* Tuple: each element is a separate column, transpose to row arrays */
1586 0 SV ***cols = NULL;
1587 int j;
1588  
1589 0 ▶ if (ct->num_inners == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners == 0) { 0 0 0
1590 /* Empty Tuple(): 1 placeholder byte per row on the wire */
1591 0 ▶ if (*pos > len || nrows > len - *pos) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || nrows > len - *pos) goto fail; 0 0 0
if (*pos > len || nrows > len - *pos) goto fail; 0 0 0
1592 0 *pos += nrows;
1593 }
1594 else {
1595 0 Newxz(cols, ct->num_inners, SV**);
1596 0 ▶ for (j = 0; j < ct->num_inners; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) { 0 0 0
1597 0 cols[j] = decode_data(buf, len, pos, nrows, ct->inners[j], decode_err, decode_flags);
1598 0 ▶ if (!cols[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cols[j]) { 0 0 0
1599 int k;
1600 0 ▶ for (k = 0; k < j; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < j; k++) { 0 0 0
1601 0 ▶ for (i = 0; i < nrows; i++) SvREFCNT_dec(cols[k][i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) SvREFCNT_dec(cols[k][i]); 0 0 0
1602 0 Safefree(cols[k]);
1603 }
1604 0 Safefree(cols);
1605 0 goto fail;
1606 }
1607 }
1608 }
1609  
1610 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1611 0 AV *av = newAV();
1612 0 ▶ if (ct->num_inners > 0) av_extend(av, ct->num_inners - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners > 0) av_extend(av, ct->num_inners - 1); 0 0 0
1613 0 ▶ for (j = 0; j < ct->num_inners; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) 0 0 0
1614 0 av_push(av, cols[j][i]);
1615 0 out[i] = newRV_noinc((SV*)av);
1616 }
1617  
1618 0 ▶ if (cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (cols) { 0 0 0
1619 0 ▶ for (j = 0; j < ct->num_inners; j++) Safefree(cols[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) Safefree(cols[j]); 0 0 0
1620 0 Safefree(cols);
1621 }
1622 0 return out;
1623 }
1624  
1625 0 ▶ if (ct->code == CT_JSON) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_JSON) { 0 0 0
1626 /* Wire layout (V1/V2/V3 supported); see JSON helpers + Encoder docs. */
1627 0 size_t saved = *pos;
1628 uint64_t obj_ver;
1629 uint64_t num_paths64;
1630 0 json_path_entry_t *jpe = NULL;
1631 0 int *path_kinds_buf = NULL; /* concatenated [path][kind_idx] */
1632 0 int *path_kind_count = NULL;
1633 0 int wire_slots_cleanup = 0; /* slot-count for var_avs cleanup */
1634 0 AV **var_avs = NULL;
1635 0 uint64_t *offs = NULL;
1636 0 AV *pending_inner = NULL;
1637 int p;
1638  
1639 0 ▶ if (*pos > len || len - *pos < 8) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
1640 0 memcpy(&obj_ver, buf + *pos, 8); *pos += 8;
1641 0 ▶ if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) goto json_hard;
Branch: 3 branches
BranchTrueFalseTotal
if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) goto json_hard; 0 0 0
if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) goto json_hard; 0 0 0
if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) goto json_hard; 0 0 0
1642 0 ▶ if (obj_ver == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_ver == 0) { 0 0 0
1643 uint64_t dummy;
1644 0 ▶ if (read_varuint(buf, len, pos, &dummy) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, pos, &dummy) <= 0) goto json_fail; 0 0 0
1645 }
1646 0 ▶ if (read_varuint(buf, len, pos, &num_paths64) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, pos, &num_paths64) <= 0) goto json_fail; 0 0 0
1647 0 ▶ if (num_paths64 > (uint64_t)INT_MAX) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths64 > (uint64_t)INT_MAX) goto json_hard; 0 0 0
1648 /* each path serializes to >=1 wire byte, so a count larger than the
1649 * bytes left is malformed -- bound before the (jpe/path_kind) allocs */
1650 0 ▶ if (num_paths64 > (uint64_t)(len - *pos)) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths64 > (uint64_t)(len - *pos)) goto json_fail; 0 0 0
1651 0 int num_paths = (int)num_paths64;
1652  
1653 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
1654 0 Newx(jpe, num_paths, json_path_entry_t);
1655 0 Newxz(path_kind_count, num_paths, int);
1656 0 Newxz(path_kinds_buf, num_paths * JSON_LEX_SLOTS, int);
1657 }
1658 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
1659 const char *ps; size_t pl;
1660 0 ▶ if (read_native_string_ref(buf, len, pos, &ps, &pl) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, pos, &ps, &pl) <= 0) goto json_fail; 0 0 0
1661 0 jpe[p].path = (char*)ps;
1662 0 jpe[p].len = pl;
1663 }
1664 0 ▶ if (obj_ver == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_ver == 4) { 0 0 0
1665 uint64_t shared_ver, dummy;
1666 0 ▶ if (read_varuint(buf, len, pos, &shared_ver) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, pos, &shared_ver) <= 0) goto json_fail; 0 0 0
1667 0 ▶ if (shared_ver == 1 || shared_ver == 2)
Branch: 2 branches
BranchTrueFalseTotal
if (shared_ver == 1 || shared_ver == 2) 0 0 0
if (shared_ver == 1 || shared_ver == 2) 0 0 0
1668 0 ▶ if (read_varuint(buf, len, pos, &dummy) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, pos, &dummy) <= 0) goto json_fail; 0 0 0
1669 }
1670 0 ▶ for (p = 0; p < ct->num_inners; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < ct->num_inners; p++) { 0 0 0
1671 0 int prc = read_col_prefix(buf, len, pos, ct->inners[p]);
1672 0 ▶ if (prc == 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (prc == 0) goto json_fail; 0 0 0
1673 0 ▶ if (prc < 0) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (prc < 0) goto json_hard; 0 0 0
1674 }
1675  
1676 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
1677 uint64_t dyn_ver, var_mode, ntypes;
1678 0 ▶ if (*pos > len || len - *pos < 8) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
1679 0 memcpy(&dyn_ver, buf + *pos, 8); *pos += 8;
1680 0 ▶ if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) goto json_hard;
Branch: 3 branches
BranchTrueFalseTotal
if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) goto json_hard; 0 0 0
if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) goto json_hard; 0 0 0
if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) goto json_hard; 0 0 0
1681 0 ▶ if (dyn_ver == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (dyn_ver == 1) { 0 0 0
1682 uint64_t dummy;
1683 0 ▶ if (read_varuint(buf, len, pos, &dummy) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, pos, &dummy) <= 0) goto json_fail; 0 0 0
1684 }
1685 0 ▶ if (read_varuint(buf, len, pos, &ntypes) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, pos, &ntypes) <= 0) goto json_fail; 0 0 0
1686 0 ▶ if (ntypes >= (uint64_t)JSON_LEX_SLOTS) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (ntypes >= (uint64_t)JSON_LEX_SLOTS) goto json_hard; 0 0 0
1687 0 int *kinds = path_kinds_buf + p * JSON_LEX_SLOTS;
1688 uint64_t ti;
1689 0 ▶ for (ti = 0; ti < ntypes; ti++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ti = 0; ti < ntypes; ti++) { 0 0 0
1690 const char *ts; size_t tl;
1691 0 ▶ if (read_native_string_ref(buf, len, pos, &ts, &tl) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, pos, &ts, &tl) <= 0) goto json_fail; 0 0 0
1692 0 int k = json_kind_from_type_name(ts, tl);
1693 0 ▶ if (k < 0) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) goto json_hard; 0 0 0
1694 0 kinds[ti] = k;
1695 }
1696 0 path_kind_count[p] = (int)ntypes;
1697 0 ▶ if (*pos > len || len - *pos < 8) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
1698 0 memcpy(&var_mode, buf + *pos, 8); *pos += 8;
1699 0 ▶ if (var_mode != 0) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (var_mode != 0) goto json_hard; 0 0 0
1700 }
1701  
1702 /* Allocate per-row hashref. */
1703 0 ▶ for (i = 0; i < nrows; i++) out[i] = newRV_noinc((SV*)newHV());
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) out[i] = newRV_noinc((SV*)newHV()); 0 0 0
1704  
1705 /* Typed-path data first (in declaration order). For each typed path,
1706 * decode a regular column via inner type, then store per-row into
1707 * out[i]'s hashref under the typed-path name. */
1708 0 ▶ if (ct->num_inners > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners > 0) { 0 0 0
1709 int tp;
1710 0 ▶ for (tp = 0; tp < ct->num_inners; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < ct->num_inners; tp++) { 0 0 0
1711 0 SV **tpcol = decode_data(buf, len, pos, (uint64_t)nrows,
1712 0 ct->inners[tp], decode_err, decode_flags);
1713 0 ▶ if (!tpcol) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!tpcol) goto json_fail; 0 0 0
1714 0 size_t nlen = strlen(ct->inner_names[tp]);
1715 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1716 0 HV *row_hv = (HV*)SvRV(out[i]);
1717 0 ▶ if (!hv_store(row_hv, ct->inner_names[tp], (I32)nlen,
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(row_hv, ct->inner_names[tp], (I32)nlen, 0 0 0
1718 tpcol[i], 0))
1719 0 SvREFCNT_dec(tpcol[i]);
1720 }
1721 0 Safefree(tpcol);
1722 }
1723 }
1724  
1725 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
1726 0 ▶ if (*pos > len || len - *pos < (size_t)nrows) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < (size_t)nrows) goto json_fail; 0 0 0
if (*pos > len || len - *pos < (size_t)nrows) goto json_fail; 0 0 0
1727 0 const unsigned char *discs = (const unsigned char *)(buf + *pos);
1728 0 *pos += (size_t)nrows;
1729  
1730 0 int nv = path_kind_count[p];
1731 int slot_to_kind[JSON_LEX_SLOTS];
1732 0 unsigned mask = 0;
1733 int s, kk;
1734 0 int *kinds = path_kinds_buf + p * JSON_LEX_SLOTS;
1735 0 ▶ for (kk = 0; kk < nv; kk++) mask |= 1u << kinds[kk];
Branch: 1 branch
BranchTrueFalseTotal
for (kk = 0; kk < nv; kk++) mask |= 1u << kinds[kk]; 0 0 0
1736 0 int wire_slots = json_build_lex_table(mask, slot_to_kind);
1737  
1738 0 uint64_t var_counts[JSON_LEX_SLOTS] = {0};
1739 uint64_t r2;
1740 0 ▶ for (r2 = 0; r2 < (uint64_t)nrows; r2++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r2 = 0; r2 < (uint64_t)nrows; r2++) { 0 0 0
1741 0 unsigned char d = discs[r2];
1742 0 ▶ if (d == 0xff) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d == 0xff) continue; 0 0 0
1743 0 ▶ if (d >= wire_slots) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (d >= wire_slots) goto json_hard; 0 0 0
1744 0 var_counts[d]++;
1745 }
1746  
1747 0 Newxz(var_avs, wire_slots, AV*);
1748 0 wire_slots_cleanup = wire_slots;
1749  
1750 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
1751 0 int kind = slot_to_kind[s];
1752 0 uint64_t nv_rows = var_counts[s];
1753 0 AV *sub = newAV();
1754 0 var_avs[s] = sub;
1755 0 ▶ if (kind < 0 || kind == JV_STRING) {
Branch: 2 branches
BranchTrueFalseTotal
if (kind < 0 || kind == JV_STRING) { 0 0 0
if (kind < 0 || kind == JV_STRING) { 0 0 0
1756 /* SharedVariant (kind<0) takes the same String wire shape;
1757 * the encoder never routes rows there but the format still
1758 * allocates the slot. */
1759 uint64_t k;
1760 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1761 const char *vs; size_t vl;
1762 0 ▶ if (read_native_string_ref(buf, len, pos, &vs, &vl) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, pos, &vs, &vl) <= 0) 0 0 0
1763 0 goto json_fail;
1764 0 av_push(sub, newSVpvn(vs, vl));
1765 }
1766 0 ▶ } else if (kind == JV_INT64) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_INT64) { 0 0 0
1767 0 ▶ if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
1768 uint64_t k;
1769 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1770 0 int64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1771 0 av_push(sub, newSViv((IV)v));
1772 }
1773 0 ▶ } else if (kind == JV_UINT64) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_UINT64) { 0 0 0
1774 0 ▶ if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
1775 uint64_t k;
1776 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1777 0 uint64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1778 0 av_push(sub, newSVuv((UV)v));
1779 }
1780 0 ▶ } else if (kind == JV_FLOAT64) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_FLOAT64) { 0 0 0
1781 0 ▶ if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
1782 uint64_t k;
1783 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1784 0 double v; memcpy(&v, buf + *pos, 8); *pos += 8;
1785 0 av_push(sub, newSVnv(v));
1786 }
1787 0 ▶ } else if (kind == JV_BOOL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_BOOL) { 0 0 0
1788 0 ▶ if (*pos > len || len - *pos < nv_rows) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < nv_rows) goto json_fail; 0 0 0
if (*pos > len || len - *pos < nv_rows) goto json_fail; 0 0 0
1789 uint64_t k;
1790 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1791 0 unsigned char b8 = (unsigned char)buf[(*pos)++];
1792 0 av_push(sub, newSViv(b8 ? 1 : 0));
1793 }
1794 0 ▶ } else if (kind >= JV_ARRAY_BOOL && kind <= JV_ARRAY_UINT64) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (kind >= JV_ARRAY_BOOL && kind <= JV_ARRAY_UINT64) { 0 0 0
} else if (kind >= JV_ARRAY_BOOL && kind <= JV_ARRAY_UINT64) { 0 0 0
1795 0 ▶ if (nv_rows == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (nv_rows == 0) continue; 0 0 0
1796 0 ▶ if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8 * nv_rows) goto json_fail; 0 0 0
1797 0 ▶ Newx(offs, nv_rows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offs, nv_rows, uint64_t); 0 0 0
1798 uint64_t k;
1799 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1800 0 memcpy(&offs[k], buf + *pos, 8); *pos += 8;
1801 }
1802 0 uint64_t prev = 0;
1803 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1804 uint64_t cnt;
1805 /* validate offset monotonicity (cf. Array/Map);
1806 * a decrease would wrap cnt below */
1807 0 ▶ if (offs[k] < prev) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (offs[k] < prev) goto json_fail; 0 0 0
1808 0 cnt = offs[k] - prev;
1809 0 pending_inner = newAV();
1810 /* every element costs >= 1 byte, so only pre-extend
1811 * what is actually present; a wilder cnt trips the
1812 * per-element bounds checks instead of OOMing here */
1813 0 ▶ if (cnt > 0 && cnt <= (uint64_t)(len - *pos))
Branch: 2 branches
BranchTrueFalseTotal
if (cnt > 0 && cnt <= (uint64_t)(len - *pos)) 0 0 0
if (cnt > 0 && cnt <= (uint64_t)(len - *pos)) 0 0 0
1814 0 av_extend(pending_inner, (SSize_t)cnt - 1);
1815 uint64_t j;
1816 0 ▶ for (j = 0; j < cnt; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < cnt; j++) { 0 0 0
1817 0 switch (kind) {
1818 0 case JV_ARRAY_BOOL:
1819 0 ▶ if (*pos >= len) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos >= len) goto json_fail; 0 0 0
1820 0 av_push(pending_inner,
1821 newSViv((unsigned char)buf[(*pos)++] ? 1 : 0));
1822 0 break;
1823 0 case JV_ARRAY_INT64: {
1824 0 ▶ if (*pos > len || len - *pos < 8) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
1825 0 int64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1826 0 av_push(pending_inner, newSViv((IV)v));
1827 0 break;
1828 }
1829 0 case JV_ARRAY_UINT64: {
1830 0 ▶ if (*pos > len || len - *pos < 8) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
1831 0 uint64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1832 0 av_push(pending_inner, newSVuv((UV)v));
1833 0 break;
1834 }
1835 0 case JV_ARRAY_FLOAT64: {
1836 0 ▶ if (*pos > len || len - *pos < 8) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8) goto json_fail; 0 0 0
1837 0 double v; memcpy(&v, buf + *pos, 8); *pos += 8;
1838 0 av_push(pending_inner, newSVnv(v));
1839 0 break;
1840 }
1841 0 case JV_ARRAY_STRING: {
1842 const char *vs; size_t vl;
1843 0 ▶ if (read_native_string_ref(buf, len, pos, &vs, &vl) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, pos, &vs, &vl) <= 0) 0 0 0
1844 0 goto json_fail;
1845 0 av_push(pending_inner, newSVpvn(vs, vl));
1846 0 break;
1847 }
1848 }
1849 }
1850 0 av_push(sub, newRV_noinc((SV*)pending_inner));
1851 0 pending_inner = NULL; /* now owned by sub */
1852 0 prev = offs[k];
1853 }
1854 0 Safefree(offs); offs = NULL;
1855 } else {
1856 0 goto json_hard;
1857 }
1858 }
1859  
1860 /* Distribute values into per-row hashes (dotted keys, unflattened later). */
1861 0 SSize_t cursors[JSON_LEX_SLOTS] = {0};
1862 uint64_t r3;
1863 0 ▶ for (r3 = 0; r3 < (uint64_t)nrows; r3++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r3 = 0; r3 < (uint64_t)nrows; r3++) { 0 0 0
1864 0 unsigned char d = discs[r3];
1865 0 ▶ if (d == 0xff) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d == 0xff) continue; 0 0 0
1866 0 SV **e = av_fetch(var_avs[d], cursors[d]++, 0);
1867 0 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 0 0
1868 0 HV *row_hv = (HV*)SvRV(out[r3]);
1869 0 SV *vsv = SvREFCNT_inc(*e);
1870 0 ▶ if (!hv_store(row_hv, jpe[p].path, (I32)jpe[p].len, vsv, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(row_hv, jpe[p].path, (I32)jpe[p].len, vsv, 0)) 0 0 0
1871 0 SvREFCNT_dec(vsv);
1872 }
1873 0 ▶ for (s = 0; s < wire_slots; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) 0 0 0
1874 0 ▶ if (var_avs[s]) SvREFCNT_dec((SV*)var_avs[s]);
Branch: 1 branch
BranchTrueFalseTotal
if (var_avs[s]) SvREFCNT_dec((SV*)var_avs[s]); 0 0 0
1875 0 Safefree(var_avs); var_avs = NULL;
1876 0 wire_slots_cleanup = 0;
1877 }
1878  
1879 /* Trailing shared data: N UInt64 offsets, then if last>0 strings. */
1880 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
1881 0 ▶ if (*pos > len || len - *pos < 8 * (size_t)nrows) goto json_fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || len - *pos < 8 * (size_t)nrows) goto json_fail; 0 0 0
if (*pos > len || len - *pos < 8 * (size_t)nrows) goto json_fail; 0 0 0
1882 uint64_t last_offset;
1883 0 memcpy(&last_offset, buf + *pos + 8 * (nrows - 1), 8);
1884 0 *pos += 8 * (size_t)nrows;
1885 0 ▶ if (last_offset > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_offset > 0) { 0 0 0
1886 uint64_t k;
1887 0 ▶ for (k = 0; k < 2 * last_offset; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 2 * last_offset; k++) { 0 0 0
1888 const char *s; size_t l;
1889 0 ▶ if (read_native_string_ref(buf, len, pos, &s, &l) <= 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, pos, &s, &l) <= 0) goto json_fail; 0 0 0
1890 }
1891 }
1892 }
1893  
1894 /* Unflatten dotted keys back into nested hashes. */
1895 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1896 0 HV *row_hv = (HV*)SvRV(out[i]);
1897 0 AV *keys = (AV*)sv_2mortal((SV*)newAV());
1898 0 hv_iterinit(row_hv);
1899 HE *he;
1900 0 ▶ while ((he = hv_iternext(row_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(row_hv))) { 0 0 0
1901 I32 klen;
1902 0 char *kstr = hv_iterkey(he, &klen);
1903 0 ▶ if (memchr(kstr, '.', klen))
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(kstr, '.', klen)) 0 0 0
1904 0 av_push(keys, newSVpvn(kstr, klen));
1905 }
1906 0 SSize_t nk = av_len(keys) + 1, ki;
1907 0 ▶ for (ki = 0; ki < nk; ki++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ki = 0; ki < nk; ki++) { 0 0 0
1908 0 SV *ksv = *av_fetch(keys, ki, 0);
1909 STRLEN klen;
1910 0 const char *kstr = SvPV(ksv, klen);
1911 0 HV *cur = row_hv;
1912 0 STRLEN seg_start = 0, off;
1913 0 int conflict = 0;
1914 0 ▶ for (off = 0; off <= klen; off++) {
Branch: 1 branch
BranchTrueFalseTotal
for (off = 0; off <= klen; off++) { 0 0 0
1915 0 ▶ if (off == klen || kstr[off] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (off == klen || kstr[off] == '.') { 0 0 0
if (off == klen || kstr[off] == '.') { 0 0 0
1916 0 const char *seg = kstr + seg_start;
1917 0 STRLEN slen = off - seg_start;
1918 0 ▶ if (off == klen) {
Branch: 1 branch
BranchTrueFalseTotal
if (off == klen) { 0 0 0
1919 0 SV **leaf = hv_fetch(row_hv, kstr, (I32)klen, 0);
1920 0 ▶ if (!leaf) { conflict = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!leaf) { conflict = 1; break; } 0 0 0
1921 0 SV *val = SvREFCNT_inc(*leaf);
1922 0 ▶ if (!hv_store(cur, seg, (I32)slen, val, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(cur, seg, (I32)slen, val, 0)) { 0 0 0
1923 0 SvREFCNT_dec(val);
1924 0 conflict = 1;
1925 }
1926 } else {
1927 0 SV **next = hv_fetch(cur, seg, (I32)slen, 0);
1928 0 ▶ if (next && SvROK(*next) && SvTYPE(SvRV(*next)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (next && SvROK(*next) && SvTYPE(SvRV(*next)) == SVt_PVHV) { 0 0 0
if (next && SvROK(*next) && SvTYPE(SvRV(*next)) == SVt_PVHV) { 0 0 0
if (next && SvROK(*next) && SvTYPE(SvRV(*next)) == SVt_PVHV) { 0 0 0
1929 0 cur = (HV*)SvRV(*next);
1930 0 ▶ } else if (next) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (next) { 0 0 0
1931 0 conflict = 1; break;
1932 } else {
1933 0 HV *new_hv = newHV();
1934 0 hv_store(cur, seg, (I32)slen,
1935 newRV_noinc((SV*)new_hv), 0);
1936 0 cur = new_hv;
1937 }
1938 }
1939 0 seg_start = off + 1;
1940 }
1941 }
1942 0 ▶ if (!conflict)
Branch: 1 branch
BranchTrueFalseTotal
if (!conflict) 0 0 0
1943 0 (void)hv_delete(row_hv, kstr, (I32)klen, G_DISCARD);
1944 }
1945 }
1946  
1947 0 ▶ if (jpe) Safefree(jpe);
Branch: 1 branch
BranchTrueFalseTotal
if (jpe) Safefree(jpe); 0 0 0
1948 0 ▶ if (path_kinds_buf) Safefree(path_kinds_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kinds_buf) Safefree(path_kinds_buf); 0 0 0
1949 0 ▶ if (path_kind_count) Safefree(path_kind_count);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kind_count) Safefree(path_kind_count); 0 0 0
1950 0 return out;
1951  
1952 0 json_hard:
1953 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1954 /* fall through to shared cleanup */
1955 0 json_fail:
1956 0 ▶ if (pending_inner) SvREFCNT_dec((SV*)pending_inner);
Branch: 1 branch
BranchTrueFalseTotal
if (pending_inner) SvREFCNT_dec((SV*)pending_inner); 0 0 0
1957 0 ▶ if (offs) Safefree(offs);
Branch: 1 branch
BranchTrueFalseTotal
if (offs) Safefree(offs); 0 0 0
1958 0 ▶ if (var_avs) {
Branch: 1 branch
BranchTrueFalseTotal
if (var_avs) { 0 0 0
1959 int sx;
1960 0 ▶ for (sx = 0; sx < wire_slots_cleanup; sx++)
Branch: 1 branch
BranchTrueFalseTotal
for (sx = 0; sx < wire_slots_cleanup; sx++) 0 0 0
1961 0 ▶ if (var_avs[sx]) SvREFCNT_dec((SV*)var_avs[sx]);
Branch: 1 branch
BranchTrueFalseTotal
if (var_avs[sx]) SvREFCNT_dec((SV*)var_avs[sx]); 0 0 0
1962 0 Safefree(var_avs);
1963 }
1964 0 ▶ if (jpe) Safefree(jpe);
Branch: 1 branch
BranchTrueFalseTotal
if (jpe) Safefree(jpe); 0 0 0
1965 0 ▶ if (path_kinds_buf) Safefree(path_kinds_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kinds_buf) Safefree(path_kinds_buf); 0 0 0
1966 0 ▶ if (path_kind_count) Safefree(path_kind_count);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kind_count) Safefree(path_kind_count); 0 0 0
1967 0 ▶ for (i = 0; i < nrows; i++) if (out[i]) SvREFCNT_dec(out[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) if (out[i]) SvREFCNT_dec(out[i]); 0 0 0
for (i = 0; i < nrows; i++) if (out[i]) SvREFCNT_dec(out[i]); 0 0 0
1968 0 *pos = saved;
1969 /* Short read: leave decode_err clear so the incremental parser
1970 * waits for more data. Definitive corruption jumps to json_hard. */
1971 0 goto fail;
1972 }
1973  
1974 0 ▶ if (ct->code == CT_MAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_MAP) { 0 0 0
1975 0 ▶ if (ct->num_inners != 2) { if (decode_err) *decode_err = 1; goto fail; }
Branch: 2 branches
BranchTrueFalseTotal
if (ct->num_inners != 2) { if (decode_err) *decode_err = 1; goto fail; } 0 0 0
if (ct->num_inners != 2) { if (decode_err) *decode_err = 1; goto fail; } 0 0 0
1976 /* Map(K,V): wire format same as Array — offsets + keys column + values column */
1977 uint64_t *offsets, total, prev;
1978 SV **keys_col, **vals_col;
1979  
1980 0 ▶ if (*pos > len || nrows > (len - *pos) / 8) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || nrows > (len - *pos) / 8) goto fail; 0 0 0
if (*pos > len || nrows > (len - *pos) / 8) goto fail; 0 0 0
1981 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
1982 0 ▶ Copy(buf + *pos, offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Copy(buf + *pos, offsets, nrows, uint64_t); 0 0 0
1983 0 *pos += nrows * 8;
1984  
1985 /* validate offset monotonicity */
1986 0 prev = 0;
1987 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1988 0 ▶ if (offsets[i] < prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (offsets[i] < prev) { 0 0 0
1989 0 Safefree(offsets);
1990 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1991 0 goto fail;
1992 }
1993 0 prev = offsets[i];
1994 }
1995  
1996 0 ▶ total = nrows > 0 ? offsets[nrows - 1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
total = nrows > 0 ? offsets[nrows - 1] : 0; 0 0 0
1997  
1998 0 keys_col = decode_data(buf, len, pos, total, ct->inners[0], decode_err, decode_flags);
1999 0 ▶ if (!keys_col) { Safefree(offsets); goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!keys_col) { Safefree(offsets); goto fail; } 0 0 0
2000  
2001 0 vals_col = decode_data(buf, len, pos, total, ct->inners[1], decode_err, decode_flags);
2002 0 ▶ if (!vals_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (!vals_col) { 0 0 0
2003 0 ▶ for (i = 0; i < total; i++) SvREFCNT_dec(keys_col[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < total; i++) SvREFCNT_dec(keys_col[i]); 0 0 0
2004 0 Safefree(keys_col);
2005 0 Safefree(offsets);
2006 0 goto fail;
2007 }
2008  
2009 0 prev = 0;
2010 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2011 0 uint64_t count = offsets[i] - prev;
2012 0 HV *hv = newHV();
2013 uint64_t j;
2014 0 ▶ for (j = 0; j < count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) { 0 0 0
2015 STRLEN klen;
2016 0 const char *kstr = SvPV(keys_col[prev + j], klen);
2017 {
2018 0 SV *val_sv = SvREFCNT_inc(vals_col[prev + j]);
2019 0 ▶ if (!hv_store(hv, kstr, klen, val_sv, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(hv, kstr, klen, val_sv, 0)) 0 0 0
2020 0 SvREFCNT_dec(val_sv);
2021 }
2022 }
2023 0 out[i] = newRV_noinc((SV*)hv);
2024 0 prev = offsets[i];
2025 }
2026  
2027 0 ▶ for (i = 0; i < total; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < total; i++) { 0 0 0
2028 0 SvREFCNT_dec(keys_col[i]);
2029 0 SvREFCNT_dec(vals_col[i]);
2030 }
2031 0 Safefree(keys_col);
2032 0 Safefree(vals_col);
2033 0 Safefree(offsets);
2034 0 return out;
2035 }
2036  
2037 /* Fixed-width types */
2038 0 fsz = col_type_fixed_size(ct);
2039 0 ▶ if (ct->code == CT_FIXEDSTRING && fsz == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_FIXEDSTRING && fsz == 0) { 0 0 0
if (ct->code == CT_FIXEDSTRING && fsz == 0) { 0 0 0
2040 /* FixedString(0): 0 bytes per row, produce empty strings */
2041 0 ▶ for (i = 0; i < nrows; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) 0 0 0
2042 0 out[i] = newSVpvn("", 0);
2043 0 return out;
2044 }
2045 0 ▶ if (fsz > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fsz > 0) { 0 0 0
2046 0 char *saved_tz = NULL;
2047 0 int tz_set = 0;
2048  
2049 0 ▶ if (*pos > len || nrows > (len - *pos) / fsz) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (*pos > len || nrows > (len - *pos) / fsz) goto fail; 0 0 0
if (*pos > len || nrows > (len - *pos) / fsz) goto fail; 0 0 0
2050  
2051 /* Set timezone for DateTime/DateTime64 columns with explicit tz */
2052 0 ▶ if (ct->tz && (decode_flags & DECODE_DT_STR) &&
Branch: 2 branches
BranchTrueFalseTotal
if (ct->tz && (decode_flags & DECODE_DT_STR) && 0 0 0
if (ct->tz && (decode_flags & DECODE_DT_STR) && 0 0 0
2053 0 ▶ (ct->code == CT_DATETIME || ct->code == CT_DATETIME64)) {
Branch: 2 branches
BranchTrueFalseTotal
(ct->code == CT_DATETIME || ct->code == CT_DATETIME64)) { 0 0 0
(ct->code == CT_DATETIME || ct->code == CT_DATETIME64)) { 0 0 0
2054 0 saved_tz = set_tz(ct->tz);
2055 0 tz_set = 1;
2056 }
2057  
2058 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2059 0 const char *p = buf + *pos + i * fsz;
2060 0 switch (ct->code) {
2061 0 case CT_INT8: out[i] = newSViv(*(int8_t*)p); break;
2062 0 case CT_INT16: { int16_t v; memcpy(&v, p, 2); out[i] = newSViv(v); break; }
2063 0 case CT_INT32: { int32_t v; memcpy(&v, p, 4); out[i] = newSViv(v); break; }
2064 0 case CT_INT64: { int64_t v; memcpy(&v, p, 8); out[i] = newSViv((IV)v); break; }
2065 0 case CT_UINT8: case CT_BOOL:
2066 0 out[i] = newSVuv(*(uint8_t*)p); break;
2067 0 case CT_UINT16: { uint16_t v; memcpy(&v, p, 2); out[i] = newSVuv(v); break; }
2068 0 case CT_UINT32: { uint32_t v; memcpy(&v, p, 4); out[i] = newSVuv(v); break; }
2069 0 case CT_UINT64: { uint64_t v; memcpy(&v, p, 8); out[i] = newSVuv((UV)v); break; }
2070 0 case CT_FLOAT32: { float v; memcpy(&v, p, 4); out[i] = newSVnv(v); break; }
2071 0 case CT_FLOAT64: { double v; memcpy(&v, p, 8); out[i] = newSVnv(v); break; }
2072 0 case CT_ENUM8:
2073 0 ▶ if (decode_flags & DECODE_ENUM_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_ENUM_STR) 0 0 0
2074 0 out[i] = enum_label_for_code(ct->type_str, ct->type_str_len, *(int8_t*)p);
2075 else
2076 0 out[i] = newSViv(*(int8_t*)p);
2077 0 break;
2078 0 case CT_ENUM16: {
2079 0 int16_t v; memcpy(&v, p, 2);
2080 0 ▶ if (decode_flags & DECODE_ENUM_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_ENUM_STR) 0 0 0
2081 0 out[i] = enum_label_for_code(ct->type_str, ct->type_str_len, v);
2082 else
2083 0 out[i] = newSViv(v);
2084 0 break;
2085 }
2086 0 case CT_DATE: {
2087 0 uint16_t v; memcpy(&v, p, 2);
2088 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2089 0 out[i] = days_to_date_sv((int32_t)v);
2090 else
2091 0 out[i] = newSVuv(v);
2092 0 break;
2093 }
2094 0 case CT_DATE32: {
2095 0 int32_t v; memcpy(&v, p, 4);
2096 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2097 0 out[i] = days_to_date_sv(v);
2098 else
2099 0 out[i] = newSViv(v);
2100 0 break;
2101 }
2102 0 case CT_DATETIME: {
2103 0 uint32_t v; memcpy(&v, p, 4);
2104 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2105 0 out[i] = epoch_to_datetime_sv_ex(v, tz_set);
2106 else
2107 0 out[i] = newSVuv(v);
2108 0 break;
2109 }
2110 0 case CT_DATETIME64: {
2111 0 int64_t v; memcpy(&v, p, 8);
2112 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2113 0 out[i] = dt64_to_datetime_sv_ex(v, ct->param, tz_set);
2114 else
2115 0 out[i] = newSViv((IV)v);
2116 0 break;
2117 }
2118 0 case CT_TIME: {
2119 0 int32_t v; memcpy(&v, p, 4);
2120 0 out[i] = newSViv(v);
2121 0 break;
2122 }
2123 0 case CT_TIME64: {
2124 0 int64_t v; memcpy(&v, p, 8);
2125 0 out[i] = newSViv((IV)v);
2126 0 break;
2127 }
2128 0 case CT_DECIMAL32: {
2129 0 int32_t v; memcpy(&v, p, 4);
2130 0 ▶ if (decode_flags & DECODE_DEC_SCALE)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DEC_SCALE) 0 0 0
2131 0 out[i] = newSVnv((double)v / pow10_int(ct->param));
2132 else
2133 0 out[i] = newSViv(v);
2134 0 break;
2135 }
2136 0 case CT_DECIMAL64: {
2137 0 int64_t v; memcpy(&v, p, 8);
2138 0 ▶ if (decode_flags & DECODE_DEC_SCALE)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DEC_SCALE) 0 0 0
2139 0 out[i] = newSVnv((double)v / pow10_int(ct->param));
2140 else
2141 0 out[i] = newSViv((IV)v);
2142 0 break;
2143 }
2144 0 case CT_DECIMAL128: {
2145 #ifdef __SIZEOF_INT128__
2146 0 ▶ if (decode_flags & DECODE_DEC_SCALE) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DEC_SCALE) { 0 0 0
2147 __int128 sv128;
2148 0 memcpy(&sv128, p, 16);
2149 /* Use long double for Decimal128 to preserve more precision */
2150 0 out[i] = newSVnv((NV)((long double)sv128 / (long double)pow10_int(ct->param)));
2151 } else {
2152 0 out[i] = int128_to_sv(p, 1);
2153 }
2154 #else
2155 out[i] = newSVpvn(p, 16);
2156 #endif
2157 0 break;
2158 }
2159 0 case CT_DECIMAL256: {
2160 /* No native int256; deliver raw 32 bytes (LE, signed) so
2161 * users can pass them to e.g. Math::BigInt. */
2162 0 out[i] = newSVpvn(p, 32);
2163 0 break;
2164 }
2165 0 case CT_BFLOAT16: {
2166 /* Top 16 bits of a Float32 (BE assembled into LE Float32). */
2167 0 uint8_t buf[4] = {0, 0, ((const uint8_t*)p)[0], ((const uint8_t*)p)[1]};
2168 float v;
2169 0 memcpy(&v, buf, 4);
2170 0 out[i] = newSVnv(v);
2171 0 break;
2172 }
2173 0 case CT_UUID: {
2174 /* UUID: two LE UInt64 halves, each reversed for display.
2175 * Nibble table instead of snprintf: same bytes, ~10x
2176 * less formatting work per row. */
2177 static const char hexd[] = "0123456789abcdef";
2178 static const uint8_t uord[16] =
2179 {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
2180 char ustr[36];
2181 0 char *w = ustr;
2182 0 const unsigned char *u = (const unsigned char *)p;
2183 int k;
2184 0 ▶ for (k = 0; k < 16; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 16; k++) { 0 0 0
2185 unsigned char b;
2186 0 ▶ if (k == 4 || k == 6 || k == 8 || k == 10)
Branch: 4 branches
BranchTrueFalseTotal
if (k == 4 || k == 6 || k == 8 || k == 10) 0 0 0
if (k == 4 || k == 6 || k == 8 || k == 10) 0 0 0
if (k == 4 || k == 6 || k == 8 || k == 10) 0 0 0
if (k == 4 || k == 6 || k == 8 || k == 10) 0 0 0
2187 0 *w++ = '-';
2188 0 b = u[uord[k]];
2189 0 *w++ = hexd[b >> 4];
2190 0 *w++ = hexd[b & 15];
2191 }
2192 0 out[i] = newSVpvn(ustr, 36);
2193 0 break;
2194 }
2195 0 case CT_IPV4: {
2196 /* UInt32 LE, MSB is first octet */
2197 uint32_t v;
2198 struct in_addr addr;
2199 char abuf[INET_ADDRSTRLEN];
2200 0 memcpy(&v, p, 4);
2201 0 addr.s_addr = htonl(v);
2202 0 inet_ntop(AF_INET, &addr, abuf, sizeof(abuf));
2203 0 out[i] = newSVpv(abuf, 0);
2204 0 break;
2205 }
2206 0 case CT_IPV6: {
2207 /* 16 bytes in network byte order */
2208 char abuf[INET6_ADDRSTRLEN];
2209 0 inet_ntop(AF_INET6, p, abuf, sizeof(abuf));
2210 0 out[i] = newSVpv(abuf, 0);
2211 0 break;
2212 }
2213 0 case CT_INT128: {
2214 #ifdef __SIZEOF_INT128__
2215 0 out[i] = int128_to_sv(p, 1);
2216 #else
2217 out[i] = newSVpvn(p, 16);
2218 #endif
2219 0 break;
2220 }
2221 0 case CT_UINT128: {
2222 #ifdef __SIZEOF_INT128__
2223 0 out[i] = int128_to_sv(p, 0);
2224 #else
2225 out[i] = newSVpvn(p, 16);
2226 #endif
2227 0 break;
2228 }
2229 0 case CT_INT256:
2230 0 out[i] = int256_to_sv(p, 1); break;
2231 0 case CT_UINT256:
2232 0 out[i] = int256_to_sv(p, 0); break;
2233 0 case CT_FIXEDSTRING: default:
2234 0 out[i] = newSVpvn(p, fsz); break;
2235 }
2236 }
2237 0 ▶ if (tz_set) restore_tz(saved_tz);
Branch: 1 branch
BranchTrueFalseTotal
if (tz_set) restore_tz(saved_tz); 0 0 0
2238 0 *pos += nrows * fsz;
2239 0 return out;
2240 }
2241  
2242 /* CT_UNKNOWN: try reading as String */
2243 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2244 const char *s;
2245 size_t slen;
2246 0 ▶ if (read_native_string_ref(buf, len, pos, &s, &slen) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, pos, &s, &slen) <= 0) { 0 0 0
2247 uint64_t j;
2248 0 ▶ for (j = 0; j < i; j++) SvREFCNT_dec(out[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) SvREFCNT_dec(out[j]); 0 0 0
2249 0 goto fail;
2250 }
2251 0 out[i] = newSVpvn(s, slen);
2252 }
2253 0 return out;
2254  
2255 0 fail:
2256 0 Safefree(out);
2257 0 return NULL;
2258 }
2259  
2260 /* --- Native protocol column encoder (for INSERT) --- */
2261  
2262 /* inet_pton's strictness is not portable: glibc rejects a leading zero in a
2263 * dotted quad, macOS and the BSDs accept it. ClickHouse rejects it, so the same
2264 * INSERT would be refused on Linux and silently stored on macOS. */
2265 0 static int ipv4_text_is_strict(const char *s, size_t slen) {
2266 0 size_t i = 0;
2267 int group;
2268 0 ▶ for (group = 0; group < 4; group++) {
Branch: 1 branch
BranchTrueFalseTotal
for (group = 0; group < 4; group++) { 0 0 0
2269 0 size_t start = i;
2270 0 unsigned v = 0;
2271 0 ▶ while (i < slen && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < slen && s[i] >= '0' && s[i] <= '9') { 0 0 0
while (i < slen && s[i] >= '0' && s[i] <= '9') { 0 0 0
while (i < slen && s[i] >= '0' && s[i] <= '9') { 0 0 0
2272 0 v = v * 10u + (unsigned)(s[i] - '0');
2273 0 i++;
2274 }
2275 0 ▶ if (i == start || i - start > 3 || v > 255) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (i == start || i - start > 3 || v > 255) return 0; 0 0 0
if (i == start || i - start > 3 || v > 255) return 0; 0 0 0
if (i == start || i - start > 3 || v > 255) return 0; 0 0 0
2276 0 ▶ if (i - start > 1 && s[start] == '0') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i - start > 1 && s[start] == '0') return 0; 0 0 0
if (i - start > 1 && s[start] == '0') return 0; 0 0 0
2277 0 ▶ if (group < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (group < 3) { 0 0 0
2278 0 ▶ if (i >= slen || s[i] != '.') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= slen || s[i] != '.') return 0; 0 0 0
if (i >= slen || s[i] != '.') return 0; 0 0 0
2279 0 i++;
2280 }
2281 }
2282 0 return i == slen;
2283 }
2284  
2285 /* Parse an IPv4 address text into a host-order UInt32. Sets *ok. */
2286 0 static uint32_t parse_ipv4_to_u32(const char *s, size_t slen, int *ok) {
2287 struct in_addr addr;
2288 char tmp[64];
2289 0 *ok = 0;
2290 0 ▶ if (slen == 0 || slen > 63) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 0 || slen > 63) return 0; 0 0 0
if (slen == 0 || slen > 63) return 0; 0 0 0
2291 0 ▶ if (!ipv4_text_is_strict(s, slen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ipv4_text_is_strict(s, slen)) return 0; 0 0 0
2292 0 memcpy(tmp, s, slen);
2293 0 tmp[slen] = '\0';
2294 0 ▶ if (inet_pton(AF_INET, tmp, &addr) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, tmp, &addr) == 1) { 0 0 0
2295 0 *ok = 1;
2296 0 return ntohl(addr.s_addr);
2297 }
2298 0 return 0;
2299 }
2300  
2301 /* Parse an IPv6 address text into 16 network-order bytes. Returns 1 on
2302 * success, 0 on failure (zero-fills the output either way). A dotted
2303 * quad maps to ::ffff:0:0/96, like the server's text parser. */
2304 0 static int parse_ipv6_to_bytes(const char *s, size_t slen, unsigned char addr[16]) {
2305 char tmp[64];
2306 struct in_addr a4;
2307 0 memset(addr, 0, 16);
2308 0 ▶ if (slen == 0 || slen > 63) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 0 || slen > 63) return 0; 0 0 0
if (slen == 0 || slen > 63) return 0; 0 0 0
2309 0 memcpy(tmp, s, slen);
2310 0 tmp[slen] = '\0';
2311 0 ▶ if (inet_pton(AF_INET6, tmp, addr) == 1) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET6, tmp, addr) == 1) return 1; 0 0 0
2312 0 ▶ if (!ipv4_text_is_strict(s, slen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ipv4_text_is_strict(s, slen)) return 0; 0 0 0
2313 0 ▶ if (inet_pton(AF_INET, tmp, &a4) != 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, tmp, &a4) != 1) return 0; 0 0 0
2314 0 addr[10] = 0xff;
2315 0 addr[11] = 0xff;
2316 0 memcpy(addr + 12, &a4.s_addr, 4);
2317 0 return 1;
2318 }
2319  
2320 /* Mirror of read_col_prefix(): every LowCardinality keys version of the
2321 * type tree, depth first, ahead of the column data. */
2322 4 static void write_col_prefix(native_buf_t *b, col_type_t *ct) {
2323 int i;
2324 4 switch (ct->code) {
2325 0 case CT_LOWCARDINALITY:
2326 0 nbuf_le64(b, 1);
2327 0 break;
2328 0 case CT_ARRAY:
2329 case CT_NULLABLE:
2330 0 ▶ if (ct->inner) write_col_prefix(b, ct->inner);
Branch: 1 branch
BranchTrueFalseTotal
if (ct->inner) write_col_prefix(b, ct->inner); 0 0 0
2331 0 break;
2332 0 case CT_TUPLE:
2333 case CT_MAP:
2334 0 ▶ for (i = 0; i < ct->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ct->num_inners; i++) 0 0 0
2335 0 write_col_prefix(b, ct->inners[i]);
2336 0 break;
2337 4 default:
2338 4 break;
2339 }
2340 4 }
2341  
2342 /* Write the LowCardinality serialization-type + num_keys header for a
2343 * trivial 1:1 dictionary encoding (every value becomes its own dict
2344 * entry). Returns idx_size via *out_idx_size for the caller's index-write loop. */
2345 0 static void lc_write_keys_header(native_buf_t *b, uint64_t nrows, size_t *out_idx_size) {
2346 int key_type;
2347 size_t idx_size;
2348 0 uint64_t ser_type, nk = nrows;
2349 0 ▶ if (nrows <= 0xFF) { key_type = 0; idx_size = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (nrows <= 0xFF) { key_type = 0; idx_size = 1; } 0 0 0
2350 0 ▶ else if (nrows <= 0xFFFF) { key_type = 1; idx_size = 2; }
Branch: 1 branch
BranchTrueFalseTotal
else if (nrows <= 0xFFFF) { key_type = 1; idx_size = 2; } 0 0 0
2351 0 else { key_type = 2; idx_size = 4; }
2352 /* HasAdditionalKeys | NeedUpdateDictionary */
2353 0 ser_type = (uint64_t)key_type | (1ULL << 9) | (1ULL << 10);
2354 0 nbuf_append(b, (const char *)&ser_type, 8);
2355 0 nbuf_append(b, (const char *)&nk, 8);
2356 0 *out_idx_size = idx_size;
2357 0 }
2358  
2359 /* An Enum type's default value, as on the server: its smallest code. */
2360 0 static int enum_default_code(col_type_t *t) {
2361 0 enum_entry_t *entries = NULL;
2362 0 size_t i, n = enum_parse_entries(t->type_str, t->type_str_len, &entries);
2363 0 ▶ int code = n ? entries[0].code : 0;
Branch: 1 branch
BranchTrueFalseTotal
int code = n ? entries[0].code : 0; 0 0 0
2364 0 ▶ for (i = 1; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) 0 0 0
2365 0 ▶ if (entries[i].code < code) code = entries[i].code;
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i].code < code) code = entries[i].code; 0 0 0
2366 0 enum_free_entries(entries, n);
2367 0 return code;
2368 }
2369  
2370 /* Placeholder for a NULL of the given inner type. Must itself parse
2371 * under the strict encoders, or one NULL fails the batch. */
2372 0 static void nullable_inner_zero(col_type_t *t, const char **p, size_t *len) {
2373 static const char zero_str[] = "0";
2374 static const char empty_str[] = "";
2375 static const char zero_uuid[] = "00000000-0000-0000-0000-000000000000";
2376 static const char zero_ip4[] = "0.0.0.0";
2377 static const char zero_ip6[] = "::";
2378 static const char zero_time[] = "00:00:00";
2379 static char enum_buf[8];
2380 0 switch (t->code) {
2381 0 case CT_ENUM8:
2382 case CT_ENUM16:
2383 0 *len = (size_t)snprintf(enum_buf, sizeof(enum_buf), "%d", enum_default_code(t));
2384 0 *p = enum_buf;
2385 0 break;
2386 0 case CT_STRING:
2387 case CT_FIXEDSTRING:
2388 0 *p = empty_str; *len = 0; break;
2389 0 case CT_UUID:
2390 0 *p = zero_uuid; *len = sizeof(zero_uuid) - 1; break;
2391 0 case CT_IPV4:
2392 0 *p = zero_ip4; *len = sizeof(zero_ip4) - 1; break;
2393 0 case CT_IPV6:
2394 0 *p = zero_ip6; *len = sizeof(zero_ip6) - 1; break;
2395 0 case CT_TIME:
2396 case CT_TIME64:
2397 0 *p = zero_time; *len = sizeof(zero_time) - 1; break;
2398 0 default:
2399 0 *p = zero_str; *len = 1; break;
2400 }
2401 0 }
2402  
2403 0 static SV *nullable_inner_zero_sv(pTHX_ col_type_t *t) {
2404 const char *p;
2405 size_t len;
2406 0 ▶ if (t->code == CT_ENUM8 || t->code == CT_ENUM16)
Branch: 2 branches
BranchTrueFalseTotal
if (t->code == CT_ENUM8 || t->code == CT_ENUM16) 0 0 0
if (t->code == CT_ENUM8 || t->code == CT_ENUM16) 0 0 0
2407 0 return newSViv(enum_default_code(t));
2408 0 nullable_inner_zero(t, &p, &len);
2409 0 ▶ if (t->code == CT_STRING || t->code == CT_FIXEDSTRING)
Branch: 2 branches
BranchTrueFalseTotal
if (t->code == CT_STRING || t->code == CT_FIXEDSTRING) 0 0 0
if (t->code == CT_STRING || t->code == CT_FIXEDSTRING) 0 0 0
2410 0 return newSVpvn("", 0);
2411 0 ▶ if (len == 1 && p[0] == '0')
Branch: 2 branches
BranchTrueFalseTotal
if (len == 1 && p[0] == '0') 0 0 0
if (len == 1 && p[0] == '0') 0 0 0
2412 0 return newSViv(0);
2413 0 return newSVpvn(p, len);
2414 }
2415  
2416 /* Write num_indices + identity-mapped indices [0..nrows-1] for a trivial
2417 * LowCardinality 1:1 dictionary. */
2418 0 static void lc_write_indices(native_buf_t *b, uint64_t nrows, size_t idx_size) {
2419 0 uint64_t i, ni = nrows;
2420 0 nbuf_append(b, (const char *)&ni, 8);
2421 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2422 0 ▶ if (idx_size == 1) { uint8_t idx = (uint8_t)i; nbuf_append(b, (const char *)&idx, 1); }
Branch: 1 branch
BranchTrueFalseTotal
if (idx_size == 1) { uint8_t idx = (uint8_t)i; nbuf_append(b, (const char *)&idx, 1); } 0 0 0
2423 0 ▶ else if (idx_size == 2) { uint16_t idx = (uint16_t)i; nbuf_append(b, (const char *)&idx, 2); }
Branch: 1 branch
BranchTrueFalseTotal
else if (idx_size == 2) { uint16_t idx = (uint16_t)i; nbuf_append(b, (const char *)&idx, 2); } 0 0 0
2424 0 else { uint32_t idx = (uint32_t)i; nbuf_append(b, (const char *)&idx, 4); }
2425 }
2426 0 }
2427  
2428 /* Write num_indices + per-row indices for an LC(Nullable) dictionary
2429 * whose slot 0 is NULL: null rows map to 0, row i maps to i + 1. */
2430 0 static void lc_write_nullable_indices(native_buf_t *b, uint64_t nrows,
2431 size_t idx_size, uint8_t *nulls) {
2432 0 uint64_t i, ni = nrows;
2433 0 nbuf_append(b, (const char *)&ni, 8);
2434 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2435 0 ▶ uint64_t v = nulls[i] ? 0 : i + 1;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t v = nulls[i] ? 0 : i + 1; 0 0 0
2436 0 ▶ if (idx_size == 1) { uint8_t idx = (uint8_t)v; nbuf_append(b, (const char *)&idx, 1); }
Branch: 1 branch
BranchTrueFalseTotal
if (idx_size == 1) { uint8_t idx = (uint8_t)v; nbuf_append(b, (const char *)&idx, 1); } 0 0 0
2437 0 ▶ else if (idx_size == 2) { uint16_t idx = (uint16_t)v; nbuf_append(b, (const char *)&idx, 2); }
Branch: 1 branch
BranchTrueFalseTotal
else if (idx_size == 2) { uint16_t idx = (uint16_t)v; nbuf_append(b, (const char *)&idx, 2); } 0 0 0
2438 0 else { uint32_t idx = (uint32_t)v; nbuf_append(b, (const char *)&idx, 4); }
2439 }
2440 0 }
2441  
2442 /* Parse "YYYY-MM-DD HH:MM:SS[.fff...]" into a DateTime64 scaled int64
2443 * (total_seconds * 10^precision + fractional). Sets *ok; on invalid input
2444 * returns 0 with *ok = 0. Pre-1970 dates work: the day count is a floor,
2445 * so the fraction always adds. */
2446 18 static int64_t parse_datetime64_to_scaled(const char *s, size_t len, int precision,
2447 const char *tz, int *ok) {
2448 int32_t days;
2449 18 int64_t total, v, scale = 1;
2450 int sc;
2451 18 *ok = 0;
2452 18 ▶ if (!valid_date_prefix(s, len)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, len)) return 0; 0 18 18
2453 18 ▶ if (len != 10 && len < 19) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (len != 10 && len < 19) return 0; 17 1 18
if (len != 10 && len < 19) return 0; 16 1 17
2454 2 ▶ if (len >= 19 && !valid_time_part(s)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 19 && !valid_time_part(s)) return 0; 1 1 2
if (len >= 19 && !valid_time_part(s)) return 0; 0 1 1
2455 2 days = date_string_to_days(s, 10);
2456 2 total = (int64_t)days * 86400 + parse_hms(s, len);
2457 2 ▶ if (tz && tz[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (tz && tz[0]) { 2 0 2
if (tz && tz[0]) { 2 0 2
2458 2 int tok = 0;
2459 2 total = wall_in_tz_to_epoch(total, tz, &tok);
2460 2 ▶ if (!tok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) return 0; 0 2 2
2461 }
2462 8 ▶ for (sc = 0; sc < precision; sc++) scale *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (sc = 0; sc < precision; sc++) scale *= 10; 6 2 8
2463 2 ▶ if (total > INT64_MAX / scale || total < INT64_MIN / scale) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (total > INT64_MAX / scale || total < INT64_MIN / scale) return 0; 2 0 2
if (total > INT64_MAX / scale || total < INT64_MIN / scale) return 0; 0 2 2
2464 2 v = total * scale;
2465 /* Full consumption: a suffix past the seconds must be a fraction.
2466 * The server interprets designators like 'Z'/'+03:00' with TZ
2467 * conversion the client cannot replicate, so anything but digits
2468 * fails rather than silently writing a divergent instant. Extra
2469 * fraction digits truncate, matching the server. */
2470 2 ▶ if (len > 19 && (len < 20 || s[19] != '.')) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (len > 19 && (len < 20 || s[19] != '.')) return 0; 0 2 2
if (len > 19 && (len < 20 || s[19] != '.')) return 0; 0 0 0
if (len > 19 && (len < 20 || s[19] != '.')) return 0; 0 0 0
2471 2 ▶ if (len >= 20 && s[19] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 20 && s[19] == '.') { 0 2 2
if (len >= 20 && s[19] == '.') { 0 0 0
2472 0 const char *fp = s + 20;
2473 0 const char *fe = s + len;
2474 0 int64_t frac = 0;
2475 0 int digits = 0;
2476 0 ▶ while (fp < fe && digits < precision) {
Branch: 2 branches
BranchTrueFalseTotal
while (fp < fe && digits < precision) { 0 0 0
while (fp < fe && digits < precision) { 0 0 0
2477 0 ▶ if (*fp < '0' || *fp > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*fp < '0' || *fp > '9') return 0; 0 0 0
if (*fp < '0' || *fp > '9') return 0; 0 0 0
2478 0 frac = frac * 10 + (*fp - '0');
2479 0 fp++;
2480 0 digits++;
2481 }
2482 0 ▶ while (fp < fe) {
Branch: 1 branch
BranchTrueFalseTotal
while (fp < fe) { 0 0 0
2483 0 ▶ if (*fp < '0' || *fp > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*fp < '0' || *fp > '9') return 0; 0 0 0
if (*fp < '0' || *fp > '9') return 0; 0 0 0
2484 0 fp++;
2485 }
2486 0 ▶ while (digits < precision) { frac *= 10; digits++; }
Branch: 1 branch
BranchTrueFalseTotal
while (digits < precision) { frac *= 10; digits++; } 0 0 0
2487 /* total*scale was bounded; the fraction can still push v over */
2488 0 ▶ if (v > INT64_MAX - frac) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > INT64_MAX - frac) return 0; 0 0 0
2489 0 v += frac;
2490 }
2491 2 *ok = 1;
2492 2 return v;
2493 }
2494  
2495 /* Expand an exponent-form decimal ("[+-]mantissa[eE][+-]digits") into
2496 * plain text for the existing decimal parsers. Returns 1 with *out_len
2497 * set, 0 when there is no exponent marker (caller uses the input as is),
2498 * -1 when invalid. The point shifts textually (exact, no float maths);
2499 * int_digits past precision-scale fail, huge positive exponents fail
2500 * fast and huge negative ones collapse to zero, without big buffers. */
2501 0 static int decimal_expand_exp(const char *p, size_t len, int scale,
2502 int precision, char *out, size_t out_sz,
2503 size_t *out_len) {
2504 size_t k, mant_len, exp_len, i, j;
2505 const char *mant, *exp_p, *ip, *fp;
2506 size_t ilen, flen;
2507 0 int neg = 0, exp_neg = 0;
2508 0 long exp = 0, int_after, limit;
2509 const char *sig;
2510 size_t sig_len, use, k3, need;
2511 char *w;
2512 0 ▶ for (k = 0; k < len; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < len; k++) 0 0 0
2513 0 ▶ if (p[k] == 'e' || p[k] == 'E') break;
Branch: 2 branches
BranchTrueFalseTotal
if (p[k] == 'e' || p[k] == 'E') break; 0 0 0
if (p[k] == 'e' || p[k] == 'E') break; 0 0 0
2514 0 ▶ if (k == len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (k == len) return 0; 0 0 0
2515 0 mant = p;
2516 0 mant_len = k;
2517 0 exp_p = p + k + 1;
2518 0 exp_len = len - mant_len - 1;
2519 0 ▶ if (mant_len == 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (mant_len == 0) return -1; 0 0 0
2520 0 i = 0;
2521 0 ▶ if (mant[0] == '-') { neg = 1; i = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (mant[0] == '-') { neg = 1; i = 1; } 0 0 0
2522 0 ▶ else if (mant[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (mant[0] == '+') i = 1; 0 0 0
2523 0 ▶ if (i >= mant_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= mant_len) return -1; 0 0 0
2524 0 j = i;
2525 0 ▶ while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++; 0 0 0
while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++; 0 0 0
while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++; 0 0 0
2526 0 ip = mant + i;
2527 0 ilen = j - i;
2528 0 fp = mant + j;
2529 0 flen = 0;
2530 0 ▶ if (j < mant_len && mant[j] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (j < mant_len && mant[j] == '.') { 0 0 0
if (j < mant_len && mant[j] == '.') { 0 0 0
2531 size_t fstart;
2532 0 j++;
2533 0 fstart = j;
2534 0 ▶ while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++; 0 0 0
while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++; 0 0 0
while (j < mant_len && mant[j] >= '0' && mant[j] <= '9') j++; 0 0 0
2535 0 fp = mant + fstart;
2536 0 flen = j - fstart;
2537 }
2538 0 ▶ if (j != mant_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (j != mant_len) return -1; 0 0 0
2539 0 ▶ if (ilen == 0 && flen == 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (ilen == 0 && flen == 0) return -1; 0 0 0
if (ilen == 0 && flen == 0) return -1; 0 0 0
2540 0 ▶ if (exp_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (exp_len == 0) { 0 0 0
2541 0 ▶ if (mant_len >= out_sz) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (mant_len >= out_sz) return -1; 0 0 0
2542 0 memcpy(out, mant, mant_len);
2543 0 *out_len = mant_len;
2544 0 return 1;
2545 }
2546 0 i = 0;
2547 0 ▶ if (exp_p[0] == '-') { exp_neg = 1; i = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (exp_p[0] == '-') { exp_neg = 1; i = 1; } 0 0 0
2548 0 ▶ else if (exp_p[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (exp_p[0] == '+') i = 1; 0 0 0
2549 0 ▶ if (i >= exp_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= exp_len) return -1; 0 0 0
2550 0 ▶ for (; i < exp_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < exp_len; i++) { 0 0 0
2551 int d;
2552 0 ▶ if (exp_p[i] < '0' || exp_p[i] > '9') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (exp_p[i] < '0' || exp_p[i] > '9') return -1; 0 0 0
if (exp_p[i] < '0' || exp_p[i] > '9') return -1; 0 0 0
2553 0 d = exp_p[i] - '0';
2554 0 ▶ if (exp < 2000000L)
Branch: 1 branch
BranchTrueFalseTotal
if (exp < 2000000L) 0 0 0
2555 0 exp = exp * 10 + d;
2556 }
2557 0 ▶ if (exp_neg) exp = -exp;
Branch: 1 branch
BranchTrueFalseTotal
if (exp_neg) exp = -exp; 0 0 0
2558 /* Significant digits live in two spans (int, frac) around the point;
2559 * gather the needed prefix into one buffer. int_after <= limit and
2560 * scale bound every copy below, so no big buffers. */
2561 {
2562 char dig[256];
2563 0 size_t first, total = ilen + flen, want = 0, got = 0, k4;
2564 0 size_t z = 0;
2565 0 ▶ while (z < ilen && ip[z] == '0') z++;
Branch: 2 branches
BranchTrueFalseTotal
while (z < ilen && ip[z] == '0') z++; 0 0 0
while (z < ilen && ip[z] == '0') z++; 0 0 0
2566 0 ▶ if (z == ilen) {
Branch: 1 branch
BranchTrueFalseTotal
if (z == ilen) { 0 0 0
2567 0 size_t z2 = 0;
2568 0 ▶ while (z2 < flen && fp[z2] == '0') z2++;
Branch: 2 branches
BranchTrueFalseTotal
while (z2 < flen && fp[z2] == '0') z2++; 0 0 0
while (z2 < flen && fp[z2] == '0') z2++; 0 0 0
2569 0 first = ilen + z2;
2570 } else {
2571 0 first = z;
2572 }
2573 0 ▶ sig_len = (first >= total) ? 0 : total - first;
Branch: 1 branch
BranchTrueFalseTotal
sig_len = (first >= total) ? 0 : total - first; 0 0 0
2574 0 sig = dig;
2575 0 limit = (long)precision - (long)scale;
2576 0 int_after = (long)sig_len + exp - (long)flen;
2577 0 ▶ if (int_after > limit) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (int_after > limit) return -1; 0 0 0
2578 0 ▶ if (sig_len == 0) goto zero;
Branch: 1 branch
BranchTrueFalseTotal
if (sig_len == 0) goto zero; 0 0 0
2579 0 ▶ if (int_after <= 0 && -int_after >= scale) goto zero;
Branch: 2 branches
BranchTrueFalseTotal
if (int_after <= 0 && -int_after >= scale) goto zero; 0 0 0
if (int_after <= 0 && -int_after >= scale) goto zero; 0 0 0
2580 0 ▶ if (int_after <= 0) want = (size_t)scale;
Branch: 1 branch
BranchTrueFalseTotal
if (int_after <= 0) want = (size_t)scale; 0 0 0
2581 0 else want = (size_t)int_after + (size_t)scale;
2582 0 ▶ if (want > sizeof(dig)) want = sizeof(dig);
Branch: 1 branch
BranchTrueFalseTotal
if (want > sizeof(dig)) want = sizeof(dig); 0 0 0
2583 0 ▶ for (k4 = first; k4 < total && got < want; k4++)
Branch: 2 branches
BranchTrueFalseTotal
for (k4 = first; k4 < total && got < want; k4++) 0 0 0
for (k4 = first; k4 < total && got < want; k4++) 0 0 0
2584 0 ▶ dig[got++] = (k4 < ilen) ? ip[k4] : fp[k4 - ilen];
Branch: 1 branch
BranchTrueFalseTotal
dig[got++] = (k4 < ilen) ? ip[k4] : fp[k4 - ilen]; 0 0 0
2585 0 sig_len = got;
2586 0 w = out;
2587 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2588 0 ▶ if (out_sz < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (out_sz < 2) return -1; 0 0 0
2589 0 *w++ = '-';
2590 }
2591 0 ▶ if (int_after <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (int_after <= 0) { 0 0 0
2592 0 long lead = -int_after;
2593 0 use = sig_len;
2594 0 ▶ if (use > (size_t)(scale - lead)) use = (size_t)(scale - lead);
Branch: 1 branch
BranchTrueFalseTotal
if (use > (size_t)(scale - lead)) use = (size_t)(scale - lead); 0 0 0
2595 0 need = 2 + (size_t)lead + use;
2596 0 ▶ if ((size_t)(w - out) + need >= out_sz) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(w - out) + need >= out_sz) return -1; 0 0 0
2597 0 *w++ = '0';
2598 0 *w++ = '.';
2599 0 ▶ for (k3 = 0; k3 < (size_t)lead; k3++) *w++ = '0';
Branch: 1 branch
BranchTrueFalseTotal
for (k3 = 0; k3 < (size_t)lead; k3++) *w++ = '0'; 0 0 0
2600 0 memcpy(w, sig, use);
2601 0 w += use;
2602 0 ▶ } else if (sig_len >= (size_t)int_after) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sig_len >= (size_t)int_after) { 0 0 0
2603 0 use = sig_len - (size_t)int_after;
2604 0 ▶ if (use > (size_t)scale) use = (size_t)scale;
Branch: 1 branch
BranchTrueFalseTotal
if (use > (size_t)scale) use = (size_t)scale; 0 0 0
2605 0 ▶ need = (size_t)int_after + (use ? use + 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
need = (size_t)int_after + (use ? use + 1 : 0); 0 0 0
2606 0 ▶ if ((size_t)(w - out) + need >= out_sz) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(w - out) + need >= out_sz) return -1; 0 0 0
2607 0 memcpy(w, sig, (size_t)int_after);
2608 0 w += int_after;
2609 0 ▶ if (use) {
Branch: 1 branch
BranchTrueFalseTotal
if (use) { 0 0 0
2610 0 *w++ = '.';
2611 0 memcpy(w, sig + int_after, use);
2612 0 w += use;
2613 }
2614 } else {
2615 0 size_t pad = (size_t)int_after - sig_len;
2616 0 need = sig_len + pad;
2617 0 ▶ if ((size_t)(w - out) + need >= out_sz) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(w - out) + need >= out_sz) return -1; 0 0 0
2618 0 memcpy(w, sig, sig_len);
2619 0 w += sig_len;
2620 0 ▶ for (k3 = 0; k3 < pad; k3++) *w++ = '0';
Branch: 1 branch
BranchTrueFalseTotal
for (k3 = 0; k3 < pad; k3++) *w++ = '0'; 0 0 0
2621 }
2622 0 *out_len = (size_t)(w - out);
2623 0 return 1;
2624 0 zero:
2625 0 w = out;
2626 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2627 0 ▶ if (out_sz < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (out_sz < 2) return -1; 0 0 0
2628 0 *w++ = '-';
2629 }
2630 0 ▶ if ((size_t)(w - out) + 1 >= out_sz) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(w - out) + 1 >= out_sz) return -1; 0 0 0
2631 0 *w++ = '0';
2632 0 *out_len = (size_t)(w - out);
2633 0 return 1;
2634 }
2635 }
2636  
2637 /* Parse "[+-]?digits[.digits]" into a scaled int64 raw value. `scale`
2638 * is the target Decimal's fractional digits; `precision` its total
2639 * digits. Mirrors the server's TSV verdicts (probed): full consumption
2640 * (leading '+' ok, no whitespace, trailing garbage fails), extra
2641 * fraction digits truncated, significant integer digits beyond
2642 * precision-scale fail. Sets *ok; the digit bound keeps every
2643 * intermediate below 10^18, so no accumulation can overflow. */
2644 0 static int64_t parse_decimal_to_raw(const char *p, size_t len, int scale,
2645 int precision, int *ok) {
2646 const char *e, *dstart, *sig;
2647 0 int neg = 0, int_digits = 0, frac_digits = 0, seen = 0, s;
2648 0 uint64_t integer_part = 0, frac_part = 0, raw;
2649 char exp_buf[256];
2650 0 size_t exp_len = 0, ek;
2651 0 int has_e = 0;
2652 0 *ok = 0;
2653 0 ▶ if (scale < 0 || scale > 18 || precision < 1 || precision > 18) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (scale < 0 || scale > 18 || precision < 1 || precision > 18) return 0; 0 0 0
if (scale < 0 || scale > 18 || precision < 1 || precision > 18) return 0; 0 0 0
if (scale < 0 || scale > 18 || precision < 1 || precision > 18) return 0; 0 0 0
if (scale < 0 || scale > 18 || precision < 1 || precision > 18) return 0; 0 0 0
2654 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2655 0 ▶ for (ek = 0; ek < len; ek++)
Branch: 1 branch
BranchTrueFalseTotal
for (ek = 0; ek < len; ek++) 0 0 0
2656 0 ▶ if (p[ek] == 'e' || p[ek] == 'E') { has_e = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[ek] == 'e' || p[ek] == 'E') { has_e = 1; break; } 0 0 0
if (p[ek] == 'e' || p[ek] == 'E') { has_e = 1; break; } 0 0 0
2657 0 ▶ if (has_e) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_e) { 0 0 0
2658 0 ▶ if (decimal_expand_exp(p, len, scale, precision,
Branch: 1 branch
BranchTrueFalseTotal
if (decimal_expand_exp(p, len, scale, precision, 0 0 0
2659 exp_buf, sizeof(exp_buf), &exp_len) != 1)
2660 0 return 0;
2661 0 p = exp_buf;
2662 0 len = exp_len;
2663 }
2664 0 e = p + len;
2665 0 ▶ if (*p == '-') { neg = 1; p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { neg = 1; p++; } 0 0 0
2666 0 ▶ else if (*p == '+') p++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') p++; 0 0 0
2667 0 dstart = p;
2668 0 ▶ while (p < e && *p >= '0' && *p <= '9') p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < e && *p >= '0' && *p <= '9') p++; 0 0 0
while (p < e && *p >= '0' && *p <= '9') p++; 0 0 0
while (p < e && *p >= '0' && *p <= '9') p++; 0 0 0
2669 0 ▶ if (p > dstart) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p > dstart) seen = 1; 0 0 0
2670 0 sig = dstart;
2671 0 ▶ while (sig < p && *sig == '0') sig++;
Branch: 2 branches
BranchTrueFalseTotal
while (sig < p && *sig == '0') sig++; 0 0 0
while (sig < p && *sig == '0') sig++; 0 0 0
2672 0 int_digits = (int)(p - sig);
2673 0 ▶ if (p < e && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < e && *p == '.') { 0 0 0
if (p < e && *p == '.') { 0 0 0
2674 0 p++;
2675 0 ▶ while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) {
Branch: 4 branches
BranchTrueFalseTotal
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
2676 0 frac_part = frac_part * 10 + (uint64_t)(*p - '0');
2677 0 p++;
2678 0 frac_digits++;
2679 }
2680 0 ▶ while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; } 0 0 0
while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; } 0 0 0
while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; } 0 0 0
2681 0 ▶ if (frac_digits > 0) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frac_digits > 0) seen = 1; 0 0 0
2682 }
2683 0 ▶ if (p != e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p != e) return 0; 0 0 0
2684 0 ▶ if (!seen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) return 0; 0 0 0
2685 0 ▶ if (int_digits > precision - scale) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 0; 0 0 0
2686 /* Significant span only: leading zeros never reach the accumulator,
2687 * and int_digits <= precision - scale keeps the scaled value < 10^18. */
2688 0 ▶ for (p = sig; p < e && *p >= '0' && *p <= '9'; p++)
Branch: 3 branches
BranchTrueFalseTotal
for (p = sig; p < e && *p >= '0' && *p <= '9'; p++) 0 0 0
for (p = sig; p < e && *p >= '0' && *p <= '9'; p++) 0 0 0
for (p = sig; p < e && *p >= '0' && *p <= '9'; p++) 0 0 0
2689 0 integer_part = integer_part * 10 + (uint64_t)(*p - '0');
2690 0 ▶ for (s = frac_digits; s < scale; s++) frac_part *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (s = frac_digits; s < scale; s++) frac_part *= 10; 0 0 0
2691 0 ▶ for (s = 0; s < scale; s++) integer_part *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < scale; s++) integer_part *= 10; 0 0 0
2692 0 raw = integer_part + frac_part;
2693 0 *ok = 1;
2694 0 ▶ return neg ? -(int64_t)raw : (int64_t)raw;
Branch: 1 branch
BranchTrueFalseTotal
return neg ? -(int64_t)raw : (int64_t)raw; 0 0 0
2695 }
2696  
2697 /* limbs = limbs * 10 + digit over nlimbs LE u64 limbs. Returns 0 on
2698 * overflow. Portable 32-bit-half arithmetic (no __int128 needed). */
2699 0 static int limbs_mul10add(uint64_t *limbs, int nlimbs, unsigned digit) {
2700 0 uint64_t carry = digit;
2701 int i;
2702 0 ▶ for (i = 0; i < nlimbs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlimbs; i++) { 0 0 0
2703 0 uint64_t t = limbs[i];
2704 0 uint64_t p1 = (t & 0xffffffffULL) * 10 + carry;
2705 0 uint64_t p2 = (t >> 32) * 10 + (p1 >> 32);
2706 0 limbs[i] = ((p2 & 0xffffffffULL) << 32) | (p1 & 0xffffffffULL);
2707 0 carry = p2 >> 32;
2708 }
2709 0 return carry == 0;
2710 }
2711  
2712 /* Two's-complement negate over nlimbs LE u64 limbs. */
2713 0 static void limbs_negate(uint64_t *limbs, int nlimbs) {
2714 0 uint64_t carry = 1;
2715 int i;
2716 0 ▶ for (i = 0; i < nlimbs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlimbs; i++) { 0 0 0
2717 0 uint64_t v = ~limbs[i] + carry;
2718 0 ▶ carry = (v == 0 && carry == 1) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
carry = (v == 0 && carry == 1) ? 1 : 0; 0 0 0
carry = (v == 0 && carry == 1) ? 1 : 0; 0 0 0
2719 0 limbs[i] = v;
2720 }
2721 0 }
2722  
2723 /* Parse decimal text into a scaled raw value over nlimbs LE u64 limbs.
2724 * Same shape as parse_decimal_to_raw: full consumption, truncation of
2725 * extra fraction digits, int_digits <= precision - scale. Values above
2726 * the signed max fail (the server errors on those too). */
2727 0 static int parse_decimal_limbs(const char *p, size_t len, int scale,
2728 int precision, int max_scale, int max_prec,
2729 uint64_t *limbs, int nlimbs) {
2730 const char *e, *dstart, *sig;
2731 0 int neg = 0, int_digits = 0, frac_digits = 0, seen = 0, s, i;
2732 char exp_buf[256];
2733 0 size_t exp_len = 0, ek;
2734 0 int has_e = 0;
2735 0 ▶ for (i = 0; i < nlimbs; i++) limbs[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlimbs; i++) limbs[i] = 0; 0 0 0
2736 0 ▶ if (scale < 0 || scale > max_scale || precision < 1 || precision > max_prec)
Branch: 4 branches
BranchTrueFalseTotal
if (scale < 0 || scale > max_scale || precision < 1 || precision > max_prec) 0 0 0
if (scale < 0 || scale > max_scale || precision < 1 || precision > max_prec) 0 0 0
if (scale < 0 || scale > max_scale || precision < 1 || precision > max_prec) 0 0 0
if (scale < 0 || scale > max_scale || precision < 1 || precision > max_prec) 0 0 0
2737 0 return 0;
2738 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2739 0 ▶ for (ek = 0; ek < len; ek++)
Branch: 1 branch
BranchTrueFalseTotal
for (ek = 0; ek < len; ek++) 0 0 0
2740 0 ▶ if (p[ek] == 'e' || p[ek] == 'E') { has_e = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[ek] == 'e' || p[ek] == 'E') { has_e = 1; break; } 0 0 0
if (p[ek] == 'e' || p[ek] == 'E') { has_e = 1; break; } 0 0 0
2741 0 ▶ if (has_e) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_e) { 0 0 0
2742 0 ▶ if (decimal_expand_exp(p, len, scale, precision,
Branch: 1 branch
BranchTrueFalseTotal
if (decimal_expand_exp(p, len, scale, precision, 0 0 0
2743 exp_buf, sizeof(exp_buf), &exp_len) != 1)
2744 0 return 0;
2745 0 p = exp_buf;
2746 0 len = exp_len;
2747 }
2748 0 e = p + len;
2749 0 ▶ if (*p == '-') { neg = 1; p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { neg = 1; p++; } 0 0 0
2750 0 ▶ else if (*p == '+') p++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') p++; 0 0 0
2751 0 dstart = p;
2752 0 ▶ while (p < e && *p >= '0' && *p <= '9') p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < e && *p >= '0' && *p <= '9') p++; 0 0 0
while (p < e && *p >= '0' && *p <= '9') p++; 0 0 0
while (p < e && *p >= '0' && *p <= '9') p++; 0 0 0
2753 0 ▶ if (p > dstart) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p > dstart) seen = 1; 0 0 0
2754 0 sig = dstart;
2755 0 ▶ while (sig < p && *sig == '0') sig++;
Branch: 2 branches
BranchTrueFalseTotal
while (sig < p && *sig == '0') sig++; 0 0 0
while (sig < p && *sig == '0') sig++; 0 0 0
2756 0 int_digits = (int)(p - sig);
2757 0 ▶ for (p = sig; p < e && *p >= '0' && *p <= '9'; p++)
Branch: 3 branches
BranchTrueFalseTotal
for (p = sig; p < e && *p >= '0' && *p <= '9'; p++) 0 0 0
for (p = sig; p < e && *p >= '0' && *p <= '9'; p++) 0 0 0
for (p = sig; p < e && *p >= '0' && *p <= '9'; p++) 0 0 0
2758 0 ▶ if (!limbs_mul10add(limbs, nlimbs, (unsigned)(*p - '0'))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!limbs_mul10add(limbs, nlimbs, (unsigned)(*p - '0'))) return 0; 0 0 0
2759 0 ▶ if (p < e && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < e && *p == '.') { 0 0 0
if (p < e && *p == '.') { 0 0 0
2760 0 p++;
2761 0 ▶ while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) {
Branch: 4 branches
BranchTrueFalseTotal
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
while (p < e && *p >= '0' && *p <= '9' && frac_digits < scale) { 0 0 0
2762 0 ▶ if (!limbs_mul10add(limbs, nlimbs, (unsigned)(*p - '0'))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!limbs_mul10add(limbs, nlimbs, (unsigned)(*p - '0'))) return 0; 0 0 0
2763 0 p++;
2764 0 frac_digits++;
2765 }
2766 0 ▶ while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; } 0 0 0
while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; } 0 0 0
while (p < e && *p >= '0' && *p <= '9') { p++; seen = 1; } 0 0 0
2767 0 ▶ if (frac_digits > 0) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frac_digits > 0) seen = 1; 0 0 0
2768 }
2769 0 ▶ if (p != e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p != e) return 0; 0 0 0
2770 0 ▶ if (!seen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) return 0; 0 0 0
2771 0 ▶ if (int_digits > precision - scale) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 0; 0 0 0
2772 0 ▶ for (s = frac_digits; s < scale; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = frac_digits; s < scale; s++) 0 0 0
2773 0 ▶ if (!limbs_mul10add(limbs, nlimbs, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!limbs_mul10add(limbs, nlimbs, 0)) return 0; 0 0 0
2774 0 ▶ if (limbs[nlimbs - 1] & 0x8000000000000000ULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] & 0x8000000000000000ULL) { 0 0 0
2775 /* magnitude >= 2^(64n-1): valid only as exactly -MIN */
2776 0 int k, is_min = neg;
2777 0 ▶ for (k = 0; k < nlimbs - 1; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nlimbs - 1; k++) 0 0 0
2778 0 ▶ if (limbs[k]) { is_min = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[k]) { is_min = 0; break; } 0 0 0
2779 0 ▶ if (limbs[nlimbs - 1] != 0x8000000000000000ULL) is_min = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] != 0x8000000000000000ULL) is_min = 0; 0 0 0
2780 0 ▶ if (!is_min) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_min) return 0; 0 0 0
2781 }
2782 0 ▶ if (neg) limbs_negate(limbs, nlimbs);
Branch: 1 branch
BranchTrueFalseTotal
if (neg) limbs_negate(limbs, nlimbs); 0 0 0
2783 0 return 1;
2784 }
2785  
2786 0 static int parse_decimal256_to_raw(const char *p, size_t len, int scale,
2787 int precision, unsigned char out[32]) {
2788 uint64_t limbs[4];
2789 0 memset(out, 0, 32);
2790 0 ▶ if (!parse_decimal_limbs(p, len, scale, precision, 76, 76, limbs, 4))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal_limbs(p, len, scale, precision, 76, 76, limbs, 4)) 0 0 0
2791 0 return 0;
2792 0 memcpy(out, limbs, 32);
2793 0 return 1;
2794 }
2795  
2796 0 static int parse_decimal128_to_raw(const char *p, size_t len, int scale,
2797 int precision, unsigned char out[16]) {
2798 uint64_t limbs[2];
2799 0 memset(out, 0, 16);
2800 0 ▶ if (!parse_decimal_limbs(p, len, scale, precision, 38, 38, limbs, 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal_limbs(p, len, scale, precision, 38, 38, limbs, 2)) 0 0 0
2801 0 return 0;
2802 0 memcpy(out, limbs, 16);
2803 0 return 1;
2804 }
2805  
2806 /* Parse decimal integer text into nlimbs LE u64 limbs. signed=0 rejects
2807 * '-'; either way the magnitude must fit (signed: sign bit clear for
2808 * positive, representable INT_MIN for negative). */
2809 0 static int parse_int_limbs(const char *p, size_t len, uint64_t *limbs,
2810 int nlimbs, int is_signed) {
2811 size_t i;
2812 0 int neg = 0, k;
2813 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2814 0 ▶ if (*p == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { 0 0 0
2815 0 ▶ if (!is_signed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_signed) return 0; 0 0 0
2816 0 neg = 1; p++; len--;
2817 }
2818 0 ▶ else if (*p == '+') { p++; len--; }
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') { p++; len--; } 0 0 0
2819 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2820 0 ▶ for (k = 0; k < nlimbs; k++) limbs[k] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nlimbs; k++) limbs[k] = 0; 0 0 0
2821 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
2822 0 ▶ if (p[i] < '0' || p[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] < '0' || p[i] > '9') return 0; 0 0 0
if (p[i] < '0' || p[i] > '9') return 0; 0 0 0
2823 0 ▶ if (!limbs_mul10add(limbs, nlimbs, (unsigned)(p[i] - '0'))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!limbs_mul10add(limbs, nlimbs, (unsigned)(p[i] - '0'))) return 0; 0 0 0
2824 }
2825 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2826 0 ▶ for (k = 0; k < nlimbs; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nlimbs; k++) 0 0 0
2827 0 ▶ if (limbs[k]) break;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[k]) break; 0 0 0
2828 0 ▶ if (k == nlimbs) return 1; /* -0 */
Branch: 1 branch
BranchTrueFalseTotal
if (k == nlimbs) return 1; /* -0 */ 0 0 0
2829 0 limbs_negate(limbs, nlimbs);
2830 0 ▶ if (!(limbs[nlimbs - 1] & 0x8000000000000000ULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(limbs[nlimbs - 1] & 0x8000000000000000ULL)) return 0; 0 0 0
2831 0 ▶ } else if (is_signed) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_signed) { 0 0 0
2832 0 ▶ if (limbs[nlimbs - 1] & 0x8000000000000000ULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] & 0x8000000000000000ULL) return 0; 0 0 0
2833 }
2834 0 return 1;
2835 }
2836  
2837 /* Parse a UUID string into 16 LE bytes (two LE UInt64 halves of the
2838 * 128-bit value). Returns 1 on success. Accepts exactly what the server
2839 * accepts: 32 hex digits, or 36 chars with dashes at 8/13/18/23. */
2840 0 static int parse_uuid_to_le_bytes(const char *s, size_t slen, unsigned char ubytes[16]) {
2841 0 unsigned char raw[16] = {0};
2842 0 int k = 0, j;
2843 0 memset(ubytes, 0, 16);
2844 0 ▶ if (slen != 32 && slen != 36) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (slen != 32 && slen != 36) return 0; 0 0 0
if (slen != 32 && slen != 36) return 0; 0 0 0
2845 0 ▶ for (j = 0; j < (int)slen; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < (int)slen; j++) { 0 0 0
2846 0 char c = s[j];
2847 unsigned char nibble;
2848 0 ▶ if (slen == 36 && (j == 8 || j == 13 || j == 18 || j == 23)) {
Branch: 5 branches
BranchTrueFalseTotal
if (slen == 36 && (j == 8 || j == 13 || j == 18 || j == 23)) { 0 0 0
if (slen == 36 && (j == 8 || j == 13 || j == 18 || j == 23)) { 0 0 0
if (slen == 36 && (j == 8 || j == 13 || j == 18 || j == 23)) { 0 0 0
if (slen == 36 && (j == 8 || j == 13 || j == 18 || j == 23)) { 0 0 0
if (slen == 36 && (j == 8 || j == 13 || j == 18 || j == 23)) { 0 0 0
2849 0 ▶ if (c != '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c != '-') return 0; 0 0 0
2850 0 continue;
2851 }
2852 0 ▶ if (c >= '0' && c <= '9') nibble = (unsigned char)(c - '0');
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') nibble = (unsigned char)(c - '0'); 0 0 0
if (c >= '0' && c <= '9') nibble = (unsigned char)(c - '0'); 0 0 0
2853 0 ▶ else if (c >= 'a' && c <= 'f') nibble = (unsigned char)(10 + c - 'a');
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'f') nibble = (unsigned char)(10 + c - 'a'); 0 0 0
else if (c >= 'a' && c <= 'f') nibble = (unsigned char)(10 + c - 'a'); 0 0 0
2854 0 ▶ else if (c >= 'A' && c <= 'F') nibble = (unsigned char)(10 + c - 'A');
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'A' && c <= 'F') nibble = (unsigned char)(10 + c - 'A'); 0 0 0
else if (c >= 'A' && c <= 'F') nibble = (unsigned char)(10 + c - 'A'); 0 0 0
2855 0 else return 0;
2856 0 ▶ if (k % 2 == 0) raw[k/2] = (unsigned char)(nibble << 4);
Branch: 1 branch
BranchTrueFalseTotal
if (k % 2 == 0) raw[k/2] = (unsigned char)(nibble << 4); 0 0 0
2857 0 else raw[k/2] |= nibble;
2858 0 k++;
2859 }
2860 0 ▶ if (k != 32) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (k != 32) return 0; 0 0 0
2861 /* Reverse each 8-byte half for LE storage */
2862 0 ▶ for (k = 0; k < 8; k++) ubytes[k] = raw[7 - k];
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 8; k++) ubytes[k] = raw[7 - k]; 0 0 0
2863 0 ▶ for (k = 0; k < 8; k++) ubytes[8 + k] = raw[15 - k];
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 8; k++) ubytes[8 + k] = raw[15 - k]; 0 0 0
2864 0 return 1;
2865 }
2866  
2867 /* Parse '[−]H+:MM:SS[.frac]' or '[−]MM:SS[.frac]' to seconds (Time)
2868 * or scaled units (Time64). Mirrors the server's TSV grammar (lax
2869 * ranges, fraction truncated to scale, rejected when scale is 0).
2870 * Returns the value with *ok = 1, or 0 with *ok = 0. */
2871 0 static int64_t parse_time_text(const char *s, size_t len, int scale, int *ok) {
2872 0 uint64_t parts[3] = {0, 0, 0};
2873 0 int nparts = 0, neg = 0, frac_digits = 0, sc;
2874 uint64_t mag, tail;
2875 0 size_t i = 0, j;
2876 0 *ok = 0;
2877 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2878 0 ▶ if (s[0] == '-') { neg = 1; i = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == '-') { neg = 1; i = 1; } 0 0 0
2879 0 ▶ else if (s[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (s[0] == '+') i = 1; 0 0 0
2880 0 for (;;) {
2881 0 uint64_t v = 0;
2882 0 size_t start = i, ndig;
2883 0 ▶ if (nparts == 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nparts == 3) return 0; 0 0 0
2884 0 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 0 0 0
while (i < len && s[i] >= '0' && s[i] <= '9') { 0 0 0
while (i < len && s[i] >= '0' && s[i] <= '9') { 0 0 0
2885 0 ▶ if (v > (UINT64_MAX - (uint64_t)(s[i] - '0')) / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (UINT64_MAX - (uint64_t)(s[i] - '0')) / 10) return 0; 0 0 0
2886 0 v = v * 10 + (uint64_t)(s[i] - '0');
2887 0 i++;
2888 }
2889 0 ndig = i - start;
2890 0 ▶ if (ndig == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ndig == 0) return 0; 0 0 0
2891 0 ▶ if (nparts > 0 && ndig != 2) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (nparts > 0 && ndig != 2) return 0; 0 0 0
if (nparts > 0 && ndig != 2) return 0; 0 0 0
2892 0 parts[nparts++] = v;
2893 0 ▶ if (i < len && s[i] == ':') { i++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == ':') { i++; continue; } 0 0 0
if (i < len && s[i] == ':') { i++; continue; } 0 0 0
2894 0 break;
2895 }
2896 0 ▶ if (nparts < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nparts < 2) return 0; 0 0 0
2897 0 ▶ if (nparts == 2) { parts[2] = parts[1]; parts[1] = parts[0]; parts[0] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (nparts == 2) { parts[2] = parts[1]; parts[1] = parts[0]; parts[0] = 0; } 0 0 0
2898 0 ▶ if (parts[0] > (UINT64_MAX / 3600)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (parts[0] > (UINT64_MAX / 3600)) return 0; 0 0 0
2899 0 mag = parts[0] * 3600;
2900 0 tail = parts[1] * 60 + parts[2];
2901 0 ▶ if (mag > UINT64_MAX - tail) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > UINT64_MAX - tail) return 0; 0 0 0
2902 0 mag += tail;
2903 0 ▶ if (i < len && s[i] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '.') { 0 0 0
if (i < len && s[i] == '.') { 0 0 0
2904 0 ▶ if (scale <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (scale <= 0) return 0; 0 0 0
2905 0 i++;
2906 0 j = i;
2907 0 ▶ while (j < len && s[j] >= '0' && s[j] <= '9') j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j < len && s[j] >= '0' && s[j] <= '9') j++; 0 0 0
while (j < len && s[j] >= '0' && s[j] <= '9') j++; 0 0 0
while (j < len && s[j] >= '0' && s[j] <= '9') j++; 0 0 0
2908 0 ▶ if (j == i) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j == i) return 0; 0 0 0
2909 0 ▶ for (; i < j && frac_digits < scale; i++, frac_digits++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; i < j && frac_digits < scale; i++, frac_digits++) { 0 0 0
for (; i < j && frac_digits < scale; i++, frac_digits++) { 0 0 0
2910 0 ▶ if (mag > (UINT64_MAX - (uint64_t)(s[i] - '0')) / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > (UINT64_MAX - (uint64_t)(s[i] - '0')) / 10) return 0; 0 0 0
2911 0 mag = mag * 10 + (uint64_t)(s[i] - '0');
2912 }
2913 0 i = j;
2914 }
2915 0 ▶ if (i != len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i != len) return 0; 0 0 0
2916 0 ▶ for (sc = frac_digits; sc < scale; sc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sc = frac_digits; sc < scale; sc++) { 0 0 0
2917 0 ▶ if (mag > UINT64_MAX / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > UINT64_MAX / 10) return 0; 0 0 0
2918 0 mag *= 10;
2919 }
2920 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2921 0 ▶ if (mag > (uint64_t)INT64_MAX + 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > (uint64_t)INT64_MAX + 1) return 0; 0 0 0
2922 0 *ok = 1;
2923 0 ▶ return mag == (uint64_t)INT64_MAX + 1 ? INT64_MIN : -(int64_t)mag;
Branch: 1 branch
BranchTrueFalseTotal
return mag == (uint64_t)INT64_MAX + 1 ? INT64_MIN : -(int64_t)mag; 0 0 0
2924 }
2925 0 ▶ if (mag > (uint64_t)INT64_MAX) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > (uint64_t)INT64_MAX) return 0; 0 0 0
2926 0 *ok = 1;
2927 0 return (int64_t)mag;
2928 }
2929  
2930 /* TSV unescape: \\ → \, \n → newline, \t → tab, \0 → null byte */
2931 0 static size_t tsv_unescape(const char *src, size_t src_len, char *dst) {
2932 0 size_t i, j = 0;
2933 0 ▶ for (i = 0; i < src_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < src_len; i++) { 0 0 0
2934 0 ▶ if (src[i] == '\\' && i + 1 < src_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (src[i] == '\\' && i + 1 < src_len) { 0 0 0
if (src[i] == '\\' && i + 1 < src_len) { 0 0 0
2935 0 switch (src[i+1]) {
2936 0 case '\\': dst[j++] = '\\'; i++; break;
2937 0 case 'n': dst[j++] = '\n'; i++; break;
2938 0 case 't': dst[j++] = '\t'; i++; break;
2939 0 case '0': dst[j++] = '\0'; i++; break;
2940 0 case '\'': dst[j++] = '\''; i++; break;
2941 0 case 'b': dst[j++] = '\b'; i++; break;
2942 0 case 'r': dst[j++] = '\r'; i++; break;
2943 0 case 'a': dst[j++] = '\a'; i++; break;
2944 0 case 'f': dst[j++] = '\f'; i++; break;
2945 0 default: dst[j++] = src[i]; break;
2946 }
2947 } else {
2948 0 dst[j++] = src[i];
2949 }
2950 }
2951 0 return j;
2952 }
2953  
2954 0 static int is_tsv_null(const char *s, size_t len) {
2955 0 ▶ return len == 2 && s[0] == '\\' && s[1] == 'N';
Branch: 3 branches
BranchTrueFalseTotal
return len == 2 && s[0] == '\\' && s[1] == 'N'; 0 0 0
return len == 2 && s[0] == '\\' && s[1] == 'N'; 0 0 0
return len == 2 && s[0] == '\\' && s[1] == 'N'; 0 0 0
2956 }
2957  
2958 /* TSV escape: inverse of tsv_unescape — appends escaped bytes to buffer */
2959 0 static void tsv_escape(native_buf_t *b, const char *s, size_t len) {
2960 0 size_t i, start = 0;
2961 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
2962 0 char esc = 0;
2963 0 switch (s[i]) {
2964 0 case '\\': esc = '\\'; break;
2965 0 case '\t': esc = 't'; break;
2966 0 case '\n': esc = 'n'; break;
2967 0 case '\0': esc = '0'; break;
2968 0 case '\b': esc = 'b'; break;
2969 0 case '\r': esc = 'r'; break;
2970 0 case '\a': esc = 'a'; break;
2971 0 case '\f': esc = 'f'; break;
2972 }
2973 0 ▶ if (esc) {
Branch: 1 branch
BranchTrueFalseTotal
if (esc) { 0 0 0
2974 0 ▶ if (i > start)
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) 0 0 0
2975 0 nbuf_append(b, s + start, i - start);
2976 0 nbuf_grow(b, 2);
2977 0 b->data[b->len++] = '\\';
2978 0 b->data[b->len++] = esc;
2979 0 start = i + 1;
2980 }
2981 }
2982 0 ▶ if (start < len)
Branch: 1 branch
BranchTrueFalseTotal
if (start < len) 0 0 0
2983 0 nbuf_append(b, s + start, len - start);
2984 0 }
2985  
2986 /* Snapshot of arrayref INSERT data. Native INSERT encodes rows later (at
2987 * sample-block time), so holding the caller's AV would let a post-call
2988 * clear/push silently change the batch: keep the cells, shared by
2989 * reference, row-major in one array, projected through columns_at. */
2990 typedef struct {
2991 SSize_t nrows;
2992 SSize_t bad_row; /* first row that is not an arrayref, -1 if none */
2993 SSize_t width; /* columns_at entries, -1 without projection */
2994 SSize_t ncells, cap;
2995 SSize_t *off; /* row r owns cells[off[r] .. off[r + 1]) */
2996 SV **cells; /* NULL = missing cell */
2997 } insert_snap_t;
2998  
2999 16 static int insert_snap_free(pTHX_ SV *sv, MAGIC *mg) {
3000 16 insert_snap_t *sn = (insert_snap_t *)mg->mg_ptr;
3001 SSize_t i;
3002 PERL_UNUSED_ARG(sv);
3003 32 ▶ for (i = 0; i < sn->ncells; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sn->ncells; i++) 16 16 32
3004 16 ▶ if (sn->cells[i]) SvREFCNT_dec(sn->cells[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (sn->cells[i]) SvREFCNT_dec(sn->cells[i]); 16 0 16
3005 16 Safefree(sn->cells);
3006 16 Safefree(sn->off);
3007 16 Safefree(sn);
3008 16 return 0;
3009 }
3010  
3011 static MGVTBL insert_snap_vtbl = {
3012 NULL, NULL, NULL, NULL, insert_snap_free, NULL, NULL, NULL
3013 };
3014  
3015 12 static insert_snap_t *insert_snap_get(pTHX_ SV *sv) {
3016 12 ▶ MAGIC *mg = sv ? mg_findext(sv, PERL_MAGIC_ext, &insert_snap_vtbl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
MAGIC *mg = sv ? mg_findext(sv, PERL_MAGIC_ext, &insert_snap_vtbl) : NULL; 12 0 12
3017 12 ▶ return mg ? (insert_snap_t *)mg->mg_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return mg ? (insert_snap_t *)mg->mg_ptr : NULL; 12 0 12
3018 }
3019  
3020 16 static void insert_snap_push(insert_snap_t *sn, SV *cell) {
3021 16 ▶ if (sn->ncells == sn->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (sn->ncells == sn->cap) { 16 0 16
3022 16 ▶ if (sn->cap > (SSize_t)(SSize_t_MAX / 2 / sizeof(SV *)))
Branch: 1 branch
BranchTrueFalseTotal
if (sn->cap > (SSize_t)(SSize_t_MAX / 2 / sizeof(SV *))) 0 16 16
3023 0 croak("insert data too large");
3024 16 ▶ sn->cap = sn->cap ? sn->cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
sn->cap = sn->cap ? sn->cap * 2 : 64; 0 16 16
3025 16 ▶ Renew(sn->cells, sn->cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(sn->cells, sn->cap, SV *); 0 16 16
3026 }
3027 16 sn->cells[sn->ncells++] = cell;
3028 16 }
3029  
3030 /* The columns_at insert option as positions in a mortal buffer, NULL
3031 * when absent; croaks unless it is a non-empty array of non-negative
3032 * integers. */
3033 18 static SSize_t *insert_columns_at(pTHX_ HV *settings, SSize_t *count) {
3034 18 ▶ SV **svp = settings ? hv_fetchs(settings, "columns_at", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **svp = settings ? hv_fetchs(settings, "columns_at", 0) : NULL; 0 18 18
3035 AV *av;
3036 SSize_t n, j, *pos;
3037 18 *count = 0;
3038 18 ▶ if (!svp) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) return NULL; 18 0 18
3039 0 SvGETMAGIC(*svp);
3040 0 ▶ if (!SvOK(*svp)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(*svp)) return NULL; 0 0 0
3041 0 ▶ if (!SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV 0 0 0
if (!SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV 0 0 0
3042 0 ▶ || (n = av_len((AV *)SvRV(*svp)) + 1) < 1)
Branch: 1 branch
BranchTrueFalseTotal
|| (n = av_len((AV *)SvRV(*svp)) + 1) < 1) 0 0 0
3043 0 croak("columns_at must be a non-empty array of non-negative integers");
3044 0 av = (AV *)SvRV(*svp);
3045 0 pos = (SSize_t *)SvPVX(sv_2mortal(newSV(n * sizeof(SSize_t))));
3046 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
3047 0 SV **e = av_fetch(av, j, 0);
3048 0 ▶ SV *v = e ? *e : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = e ? *e : &PL_sv_undef; 0 0 0
3049 NV nv;
3050 0 SvGETMAGIC(v);
3051 0 ▶ if (!SvOK(v) || SvROK(v) || !looks_like_number(v)
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(v) || SvROK(v) || !looks_like_number(v) 0 0 0
if (!SvOK(v) || SvROK(v) || !looks_like_number(v) 0 0 0
if (!SvOK(v) || SvROK(v) || !looks_like_number(v) 0 0 0
3052 0 ▶ || !((nv = SvNV_nomg(v)) >= 0 && nv <= (NV)(SSize_t_MAX / 2))
Branch: 2 branches
BranchTrueFalseTotal
|| !((nv = SvNV_nomg(v)) >= 0 && nv <= (NV)(SSize_t_MAX / 2)) 0 0 0
|| !((nv = SvNV_nomg(v)) >= 0 && nv <= (NV)(SSize_t_MAX / 2)) 0 0 0
3053 0 ▶ || nv != (NV)(SSize_t)nv)
Branch: 1 branch
BranchTrueFalseTotal
|| nv != (NV)(SSize_t)nv) 0 0 0
3054 0 croak("columns_at must be a non-empty array of non-negative integers");
3055 0 pos[j] = (SSize_t)nv;
3056 }
3057 0 *count = n;
3058 0 return pos;
3059 }
3060  
3061 /* A plain row's cell is shared by reference; a get-magical one (a tied
3062 * scalar) is FETCHed once now and copied, as in a tied row. */
3063 16 static SV *snap_cell(pTHX_ SV *cell) {
3064 16 ▶ if (!cell) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cell) return NULL; 0 16 16
3065 16 ▶ return SvGMAGICAL(cell) ? newSVsv(cell) : SvREFCNT_inc(cell);
Branch: 1 branch
BranchTrueFalseTotal
return SvGMAGICAL(cell) ? newSVsv(cell) : SvREFCNT_inc(cell); 0 16 16
3066 }
3067  
3068 16 static SV *snapshot_insert_av(pTHX_ AV *src, const SSize_t *proj, SSize_t nproj) {
3069 16 SSize_t n = av_len(src) + 1;
3070 SSize_t i;
3071 insert_snap_t *sn;
3072 /* av_fetch on a tied AV returns a lazy mortal FETCH proxy, not the
3073 * value. Materialize tied rows/cells now (src is alive) and copy
3074 * the values so the snapshot never dangles if the caller frees
3075 * the tied container before the deferred encode. */
3076 16 int src_magical = SvRMAGICAL((SV *)src);
3077 /* Mortal during the build: a dying FETCH longjmps out of insert(),
3078 * and the partial copy must die with the scope instead of leaking. */
3079 16 SV *holder = sv_2mortal(newSV(0));
3080  
3081 16 Newxz(sn, 1, insert_snap_t);
3082 16 sn->bad_row = -1;
3083 16 ▶ sn->width = proj ? nproj : -1;
Branch: 1 branch
BranchTrueFalseTotal
sn->width = proj ? nproj : -1; 0 16 16
3084 16 sv_magicext(holder, NULL, PERL_MAGIC_ext, &insert_snap_vtbl, (char *)sn, 0);
3085 16 ▶ Newx(sn->off, n + 1, SSize_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(sn->off, n + 1, SSize_t); 0 16 16
3086 16 sn->off[0] = 0;
3087 16 ▶ if (proj && n > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (proj && n > 0) { 0 16 16
if (proj && n > 0) { 0 0 0
3088 0 ▶ if (nproj > SSize_t_MAX / (SSize_t)sizeof(SV *) / n)
Branch: 1 branch
BranchTrueFalseTotal
if (nproj > SSize_t_MAX / (SSize_t)sizeof(SV *) / n) 0 0 0
3089 0 croak("insert data too large");
3090 0 sn->cap = n * nproj;
3091 0 ▶ Newx(sn->cells, sn->cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(sn->cells, sn->cap, SV *); 0 0 0
3092 }
3093 32 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 16 16 32
3094 16 SV **e = av_fetch(src, i, 0);
3095 16 ▶ SV *row = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *row = (e && *e) ? *e : &PL_sv_undef; 16 0 16
SV *row = (e && *e) ? *e : &PL_sv_undef; 16 0 16
3096 16 AV *rsrc = NULL;
3097 16 ▶ if (src_magical || SvGMAGICAL(row)) {
Branch: 2 branches
BranchTrueFalseTotal
if (src_magical || SvGMAGICAL(row)) { 16 0 16
if (src_magical || SvGMAGICAL(row)) { 0 16 16
3098 0 ▶ if (SvGMAGICAL(row)) mg_get(row);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(row)) mg_get(row); 0 0 0
3099 0 ▶ if (SvROK(row) && SvTYPE(SvRV(row)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(row) && SvTYPE(SvRV(row)) == SVt_PVAV) { 0 0 0
if (SvROK(row) && SvTYPE(SvRV(row)) == SVt_PVAV) { 0 0 0
3100 0 rsrc = (AV *)SvRV(row);
3101 /* Own the row AV through the copy loop below; the
3102 * FETCH proxy borrows it. Mortal: scope frees it. */
3103 0 (void)sv_2mortal(newRV_inc((SV *)rsrc));
3104 }
3105 16 ▶ } else if (SvROK(row) && SvTYPE(SvRV(row)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(row) && SvTYPE(SvRV(row)) == SVt_PVAV) { 16 0 16
} else if (SvROK(row) && SvTYPE(SvRV(row)) == SVt_PVAV) { 16 0 16
3106 16 rsrc = (AV *)SvRV(row);
3107 }
3108 16 ▶ if (!rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rsrc) { 0 16 16
3109 0 ▶ if (sn->bad_row < 0) sn->bad_row = i;
Branch: 1 branch
BranchTrueFalseTotal
if (sn->bad_row < 0) sn->bad_row = i; 0 0 0
3110 16 ▶ } else if (SvRMAGICAL((SV *)rsrc)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvRMAGICAL((SV *)rsrc)) { 0 16 16
3111 0 SSize_t len = av_len(rsrc) + 1;
3112 0 ▶ SSize_t j, rn = proj ? nproj : len;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, rn = proj ? nproj : len; 0 0 0
3113 0 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 0 0 0
3114 0 ▶ SSize_t at = proj ? proj[j] : j;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t at = proj ? proj[j] : j; 0 0 0
3115 0 ▶ SV **c = at < len ? av_fetch(rsrc, at, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **c = at < len ? av_fetch(rsrc, at, 0) : NULL; 0 0 0
3116 0 ▶ insert_snap_push(sn, (c && *c) ? newSVsv(*c) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
insert_snap_push(sn, (c && *c) ? newSVsv(*c) : NULL); 0 0 0
insert_snap_push(sn, (c && *c) ? newSVsv(*c) : NULL); 0 0 0
3117 }
3118 } else {
3119 16 SSize_t len = AvFILLp(rsrc) + 1;
3120 16 SV **arr = AvARRAY(rsrc);
3121 SSize_t j;
3122 16 ▶ if (proj) {
Branch: 1 branch
BranchTrueFalseTotal
if (proj) { 0 16 16
3123 0 ▶ for (j = 0; j < nproj; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nproj; j++) 0 0 0
3124 0 insert_snap_push(sn, snap_cell(aTHX_
3125 0 ▶ proj[j] < len ? arr[proj[j]] : NULL));
Branch: 1 branch
BranchTrueFalseTotal
proj[j] < len ? arr[proj[j]] : NULL)); 0 0 0
3126 } else {
3127 32 ▶ for (j = 0; j < len; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < len; j++) 16 16 32
3128 16 insert_snap_push(sn, snap_cell(aTHX_ arr[j]));
3129 }
3130 }
3131 16 sn->off[i + 1] = sn->ncells;
3132 }
3133 16 sn->nrows = n;
3134 /* Un-mortalize: the caller owns the holder from here on. */
3135 16 return SvREFCNT_inc(holder);
3136 }
3137  
3138 /* Serialize an AV of AVs to TabSeparated format for HTTP INSERT, each
3139 * row projected through proj when given. Returns malloc'd buffer;
3140 * caller must Safefree(). */
3141  
3142 0 static char* serialize_av_to_tsv(pTHX_ AV *rows, const SSize_t *proj,
3143 SSize_t nproj, size_t *out_len) {
3144 native_buf_t b;
3145 0 SSize_t nrows = av_len(rows) + 1;
3146 SSize_t r;
3147  
3148 0 nbuf_init(&b);
3149  
3150 0 ▶ for (r = 0; r < nrows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < nrows; r++) { 0 0 0
3151 0 SV **row_svp = av_fetch(rows, r, 0);
3152 AV *row;
3153 SSize_t ncols, c, last;
3154  
3155 0 ▶ if (row_svp && *row_svp && SvGMAGICAL(*row_svp))
Branch: 3 branches
BranchTrueFalseTotal
if (row_svp && *row_svp && SvGMAGICAL(*row_svp)) 0 0 0
if (row_svp && *row_svp && SvGMAGICAL(*row_svp)) 0 0 0
if (row_svp && *row_svp && SvGMAGICAL(*row_svp)) 0 0 0
3156 0 mg_get(*row_svp);
3157 0 ▶ if (!row_svp || !SvROK(*row_svp) ||
Branch: 2 branches
BranchTrueFalseTotal
if (!row_svp || !SvROK(*row_svp) || 0 0 0
if (!row_svp || !SvROK(*row_svp) || 0 0 0
3158 0 ▶ SvTYPE(SvRV(*row_svp)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(*row_svp)) != SVt_PVAV) { 0 0 0
3159 0 Safefree(b.data);
3160 0 croak("insert data: row %" IVdf " is not an ARRAY ref", (IV)r);
3161 }
3162 0 row = (AV *)SvRV(*row_svp);
3163 0 last = av_len(row);
3164 0 ▶ ncols = proj ? nproj : last + 1;
Branch: 1 branch
BranchTrueFalseTotal
ncols = proj ? nproj : last + 1; 0 0 0
3165  
3166 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
3167 0 SV **val_svp = !proj ? av_fetch(row, c, 0)
3168 0 ▶ : proj[c] <= last ? av_fetch(row, proj[c], 0)
Branch: 1 branch
BranchTrueFalseTotal
: proj[c] <= last ? av_fetch(row, proj[c], 0) 0 0 0
3169 0 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 0 0 0
3170 0 ▶ if (c > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c > 0) 0 0 0
3171 0 nbuf_u8(&b, '\t');
3172  
3173 0 ▶ if (val_svp && *val_svp && SvGMAGICAL(*val_svp))
Branch: 3 branches
BranchTrueFalseTotal
if (val_svp && *val_svp && SvGMAGICAL(*val_svp)) 0 0 0
if (val_svp && *val_svp && SvGMAGICAL(*val_svp)) 0 0 0
if (val_svp && *val_svp && SvGMAGICAL(*val_svp)) 0 0 0
3174 0 mg_get(*val_svp);
3175 0 ▶ if (!val_svp || !SvOK(*val_svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!val_svp || !SvOK(*val_svp)) { 0 0 0
if (!val_svp || !SvOK(*val_svp)) { 0 0 0
3176 0 nbuf_append(&b, "\\N", 2);
3177 0 ▶ } else if (SvROK(*val_svp)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvROK(*val_svp)) { 0 0 0
3178 /* Nested AV/HV refs (Array/Tuple/Map columns) cannot be
3179 * round-tripped through TSV without column-type info from
3180 * the server, which the HTTP path doesn't have. Fail loudly
3181 * rather than silently sending ARRAY(0x...) garbage. */
3182 0 Safefree(b.data);
3183 0 croak("insert data: row %" IVdf " column %" IVdf " is a "
3184 "reference; nested Array/Tuple/Map columns require "
3185 "the native protocol", (IV)r, (IV)c);
3186 } else {
3187 STRLEN vlen;
3188 /* nomg: already FETCHed above; a second FETCH would
3189 * double user-visible side effects. */
3190 0 const char *v = SvPV_nomg(*val_svp, vlen);
3191 0 tsv_escape(&b, v, vlen);
3192 }
3193 }
3194 0 nbuf_u8(&b, '\n');
3195 }
3196  
3197 0 *out_len = b.len;
3198 0 return b.data;
3199 }
3200  
3201 /* Strict "[+-]?digits" parses with full consumption and overflow
3202 * detection (server TSV parity: no whitespace, no trailing garbage).
3203 * Length-delimited: TSV fields are not NUL-terminated. */
3204 0 static int64_t parse_int64_strict(const char *s, size_t len, int *ok) {
3205 0 const char *e = s + len;
3206 0 int neg = 0;
3207 0 uint64_t cutoff, cutlim, v = 0;
3208 0 *ok = 0;
3209 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3210 0 ▶ if (*s == '-') { neg = 1; s++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { neg = 1; s++; } 0 0 0
3211 0 ▶ else if (*s == '+') s++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '+') s++; 0 0 0
3212 0 ▶ if (s >= e || *s < '0' || *s > '9') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (s >= e || *s < '0' || *s > '9') return 0; 0 0 0
if (s >= e || *s < '0' || *s > '9') return 0; 0 0 0
if (s >= e || *s < '0' || *s > '9') return 0; 0 0 0
3213 0 ▶ cutoff = neg ? (uint64_t)INT64_MAX + 1 : (uint64_t)INT64_MAX;
Branch: 1 branch
BranchTrueFalseTotal
cutoff = neg ? (uint64_t)INT64_MAX + 1 : (uint64_t)INT64_MAX; 0 0 0
3214 0 cutlim = cutoff % 10;
3215 0 cutoff /= 10;
3216 0 ▶ for (; s < e; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; s < e; s++) { 0 0 0
3217 unsigned d;
3218 0 ▶ if (*s < '0' || *s > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*s < '0' || *s > '9') return 0; 0 0 0
if (*s < '0' || *s > '9') return 0; 0 0 0
3219 0 d = (unsigned)(*s - '0');
3220 0 ▶ if (v > cutoff || (v == cutoff && d > cutlim)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (v > cutoff || (v == cutoff && d > cutlim)) return 0; 0 0 0
if (v > cutoff || (v == cutoff && d > cutlim)) return 0; 0 0 0
if (v > cutoff || (v == cutoff && d > cutlim)) return 0; 0 0 0
3221 0 v = v * 10 + d;
3222 }
3223 0 *ok = 1;
3224 0 ▶ if (!neg) return (int64_t)v;
Branch: 1 branch
BranchTrueFalseTotal
if (!neg) return (int64_t)v; 0 0 0
3225 0 ▶ return v == (uint64_t)INT64_MAX + 1 ? INT64_MIN : -(int64_t)v;
Branch: 1 branch
BranchTrueFalseTotal
return v == (uint64_t)INT64_MAX + 1 ? INT64_MIN : -(int64_t)v; 0 0 0
3226 }
3227  
3228 0 static uint64_t parse_uint64_strict(const char *s, size_t len, int *ok) {
3229 0 const char *e = s + len;
3230 0 uint64_t v = 0;
3231 0 *ok = 0;
3232 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3233 0 ▶ if (*s == '+') s++;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '+') s++; 0 0 0
3234 0 ▶ else if (*s == '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '-') return 0; 0 0 0
3235 0 ▶ if (s >= e || *s < '0' || *s > '9') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (s >= e || *s < '0' || *s > '9') return 0; 0 0 0
if (s >= e || *s < '0' || *s > '9') return 0; 0 0 0
if (s >= e || *s < '0' || *s > '9') return 0; 0 0 0
3236 0 ▶ for (; s < e; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; s < e; s++) { 0 0 0
3237 unsigned d;
3238 0 ▶ if (*s < '0' || *s > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*s < '0' || *s > '9') return 0; 0 0 0
if (*s < '0' || *s > '9') return 0; 0 0 0
3239 0 d = (unsigned)(*s - '0');
3240 0 ▶ if (v > (UINT64_MAX - d) / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (UINT64_MAX - d) / 10) return 0; 0 0 0
3241 0 v = v * 10 + d;
3242 }
3243 0 *ok = 1;
3244 0 return v;
3245 }
3246  
3247 /* SV to int64/uint64 for integer columns. IOK passes through; NOK goes
3248 * through SvNV with a range check so 1e15 encodes exactly instead of
3249 * stringifying to "1e+15" and truncating to 1; anything else parses the
3250 * string form strictly. !SvOK fails: a null into a non-nullable column
3251 * must error, never land as 0. */
3252 0 static int64_t sv_to_int64_strict(pTHX_ SV *sv, int *ok) {
3253 STRLEN vlen;
3254 const char *s;
3255 0 *ok = 0;
3256 0 ▶ if (!SvOK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return 0; 0 0 0
3257 0 ▶ if (SvIOK(sv)) { *ok = 1; return SvIV(sv); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { *ok = 1; return SvIV(sv); } 0 0 0
3258 0 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 0 0 0
3259 0 double d = SvNV(sv);
3260 0 ▶ if (!(d > -9.223372036854776e18 && d < 9.223372036854776e18)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(d > -9.223372036854776e18 && d < 9.223372036854776e18)) return 0; 0 0 0
if (!(d > -9.223372036854776e18 && d < 9.223372036854776e18)) return 0; 0 0 0
3261 0 *ok = 1;
3262 0 return (int64_t)d;
3263 }
3264 0 s = SvPV(sv, vlen);
3265 0 return parse_int64_strict(s, vlen, ok);
3266 }
3267  
3268 2 static uint64_t sv_to_uint64_strict(pTHX_ SV *sv, int *ok) {
3269 STRLEN vlen;
3270 const char *s;
3271 2 *ok = 0;
3272 2 ▶ if (!SvOK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return 0; 0 2 2
3273 2 ▶ if (SvIsUV(sv)) { *ok = 1; return SvUV(sv); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) { *ok = 1; return SvUV(sv); } 0 2 2
3274 2 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 2 0 2
3275 2 IV iv = SvIV(sv);
3276 2 ▶ if (iv < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (iv < 0) return 0; 0 2 2
3277 2 *ok = 1;
3278 2 return (uint64_t)iv;
3279 }
3280 0 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 0 0 0
3281 0 double d = SvNV(sv);
3282 0 ▶ if (!(d >= 0.0 && d < 1.8446744073709552e19)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(d >= 0.0 && d < 1.8446744073709552e19)) return 0; 0 0 0
if (!(d >= 0.0 && d < 1.8446744073709552e19)) return 0; 0 0 0
3283 0 *ok = 1;
3284 0 return (uint64_t)d;
3285 }
3286 0 s = SvPV(sv, vlen);
3287 0 return parse_uint64_strict(s, vlen, ok);
3288 }
3289  
3290 /* TSV boolean: exactly "0"/"1" or true/false in any case (probed server
3291 * verdicts: "2", "00", "01" all fail with Code 467). */
3292 0 static int bool_text_to_u8(const char *s, size_t len, uint8_t *out) {
3293 0 ▶ if (len == 1 && (*s == '0' || *s == '1')) {
Branch: 3 branches
BranchTrueFalseTotal
if (len == 1 && (*s == '0' || *s == '1')) { 0 0 0
if (len == 1 && (*s == '0' || *s == '1')) { 0 0 0
if (len == 1 && (*s == '0' || *s == '1')) { 0 0 0
3294 0 *out = (uint8_t)(*s - '0');
3295 0 return 1;
3296 }
3297 0 ▶ if (len == 4 &&
Branch: 1 branch
BranchTrueFalseTotal
if (len == 4 && 0 0 0
3298 0 ▶ (*s == 't' || *s == 'T') && (s[1] == 'r' || s[1] == 'R') &&
Branch: 4 branches
BranchTrueFalseTotal
(*s == 't' || *s == 'T') && (s[1] == 'r' || s[1] == 'R') && 0 0 0
(*s == 't' || *s == 'T') && (s[1] == 'r' || s[1] == 'R') && 0 0 0
(*s == 't' || *s == 'T') && (s[1] == 'r' || s[1] == 'R') && 0 0 0
(*s == 't' || *s == 'T') && (s[1] == 'r' || s[1] == 'R') && 0 0 0
3299 0 ▶ (s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) {
Branch: 4 branches
BranchTrueFalseTotal
(s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) { 0 0 0
(s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) { 0 0 0
(s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) { 0 0 0
(s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) { 0 0 0
3300 0 *out = 1;
3301 0 return 1;
3302 }
3303 0 ▶ if (len == 5 &&
Branch: 1 branch
BranchTrueFalseTotal
if (len == 5 && 0 0 0
3304 0 ▶ (*s == 'f' || *s == 'F') && (s[1] == 'a' || s[1] == 'A') &&
Branch: 4 branches
BranchTrueFalseTotal
(*s == 'f' || *s == 'F') && (s[1] == 'a' || s[1] == 'A') && 0 0 0
(*s == 'f' || *s == 'F') && (s[1] == 'a' || s[1] == 'A') && 0 0 0
(*s == 'f' || *s == 'F') && (s[1] == 'a' || s[1] == 'A') && 0 0 0
(*s == 'f' || *s == 'F') && (s[1] == 'a' || s[1] == 'A') && 0 0 0
3305 0 ▶ (s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') &&
Branch: 4 branches
BranchTrueFalseTotal
(s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') && 0 0 0
(s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') && 0 0 0
(s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') && 0 0 0
(s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') && 0 0 0
3306 0 ▶ (s[4] == 'e' || s[4] == 'E')) {
Branch: 2 branches
BranchTrueFalseTotal
(s[4] == 'e' || s[4] == 'E')) { 0 0 0
(s[4] == 'e' || s[4] == 'E')) { 0 0 0
3307 0 *out = 0;
3308 0 return 1;
3309 }
3310 0 return 0;
3311 }
3312  
3313 /* Exact label match for a TSV field. Backslash escapes decode before
3314 * comparing, mirroring the server's TSV parsing. */
3315 0 static int enum_match_tsv(const enum_entry_t *entries, size_t n,
3316 const char *s, size_t len, int *code_out) {
3317 char *tmp;
3318 size_t ulen;
3319 int found;
3320 0 ▶ if (!memchr(s, '\\', len))
Branch: 1 branch
BranchTrueFalseTotal
if (!memchr(s, '\\', len)) 0 0 0
3321 0 return enum_code_for_label(entries, n, s, len, code_out);
3322 0 Newx(tmp, len, char);
3323 0 ulen = tsv_unescape(s, len, tmp);
3324 0 found = enum_code_for_label(entries, n, tmp, ulen, code_out);
3325 0 Safefree(tmp);
3326 0 return found;
3327 }
3328  
3329 /* A code the type does not declare would make every later read of the
3330 * column fail ("Unexpected value in enum"); the server's text parser
3331 * rejects it too. */
3332 0 static int enum_has_code(const enum_entry_t *entries, size_t n, int64_t code) {
3333 size_t i;
3334 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
3335 0 ▶ if (entries[i].code == code) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i].code == code) return 1; 0 0 0
3336 0 return 0;
3337 }
3338  
3339 /* Labels win over codes, as in the server's text parsers; a number
3340 * needs the label lookup only when some label looks like one. */
3341 0 static int enum_has_numeric_label(const enum_entry_t *entries, size_t n) {
3342 size_t i, j;
3343 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
3344 0 const char *l = entries[i].label;
3345 0 ▶ j = (entries[i].len > 1 && (l[0] == '-' || l[0] == '+')) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
j = (entries[i].len > 1 && (l[0] == '-' || l[0] == '+')) ? 1 : 0; 0 0 0
j = (entries[i].len > 1 && (l[0] == '-' || l[0] == '+')) ? 1 : 0; 0 0 0
j = (entries[i].len > 1 && (l[0] == '-' || l[0] == '+')) ? 1 : 0; 0 0 0
3346 0 ▶ if (j >= entries[i].len) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (j >= entries[i].len) continue; 0 0 0
3347 0 ▶ while (j < entries[i].len && l[j] >= '0' && l[j] <= '9') j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j < entries[i].len && l[j] >= '0' && l[j] <= '9') j++; 0 0 0
while (j < entries[i].len && l[j] >= '0' && l[j] <= '9') j++; 0 0 0
while (j < entries[i].len && l[j] >= '0' && l[j] <= '9') j++; 0 0 0
3348 0 ▶ if (j == entries[i].len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == entries[i].len) return 1; 0 0 0
3349 }
3350 0 return 0;
3351 }
3352  
3353 0 static void nbuf_enum(native_buf_t *b, int type_code, int code) {
3354 0 ▶ if (type_code == CT_ENUM8) {
Branch: 1 branch
BranchTrueFalseTotal
if (type_code == CT_ENUM8) { 0 0 0
3355 0 int8_t v = (int8_t)code;
3356 0 nbuf_append(b, (const char *)&v, 1);
3357 } else {
3358 0 int16_t v = (int16_t)code;
3359 0 nbuf_append(b, (const char *)&v, 2);
3360 }
3361 0 }
3362  
3363 /*
3364 * Encode a column of text values into native binary format.
3365 * Returns 1 on success, 0 if type is unsupported (caller falls back to inline SQL).
3366 */
3367 0 static int encode_column_text(native_buf_t *b,
3368 const char **values, size_t *value_lens,
3369 uint64_t nrows, col_type_t *ct) {
3370 uint64_t i;
3371  
3372 0 switch (ct->code) {
3373 0 case CT_INT8: {
3374 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3375 0 int ok = 0;
3376 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3377 0 ▶ if (!ok || w < -128 || w > 127) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || w < -128 || w > 127) return 0; 0 0 0
if (!ok || w < -128 || w > 127) return 0; 0 0 0
if (!ok || w < -128 || w > 127) return 0; 0 0 0
3378 {
3379 0 int8_t v = (int8_t)w;
3380 0 nbuf_append(b, (const char *)&v, 1);
3381 }
3382 }
3383 0 return 1;
3384 }
3385 0 case CT_ENUM8: case CT_ENUM16: {
3386 0 enum_entry_t *entries = NULL;
3387 0 size_t nentries = enum_parse_entries(ct->type_str, ct->type_str_len, &entries);
3388 0 int rc = 1;
3389 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3390 0 int code, ok = 0;
3391 0 ▶ if (!enum_match_tsv(entries, nentries, values[i], value_lens[i], &code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enum_match_tsv(entries, nentries, values[i], value_lens[i], &code)) { 0 0 0
3392 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3393 0 ▶ if (!ok || !enum_has_code(entries, nentries, w)) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || !enum_has_code(entries, nentries, w)) { rc = 0; break; } 0 0 0
if (!ok || !enum_has_code(entries, nentries, w)) { rc = 0; break; } 0 0 0
3394 0 code = (int)w;
3395 }
3396 0 nbuf_enum(b, ct->code, code);
3397 }
3398 0 enum_free_entries(entries, nentries);
3399 0 return rc;
3400 }
3401 0 case CT_INT16: {
3402 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3403 0 int ok = 0;
3404 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3405 0 ▶ if (!ok || w < -32768 || w > 32767) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || w < -32768 || w > 32767) return 0; 0 0 0
if (!ok || w < -32768 || w > 32767) return 0; 0 0 0
if (!ok || w < -32768 || w > 32767) return 0; 0 0 0
3406 {
3407 0 int16_t v = (int16_t)w;
3408 0 nbuf_append(b, (const char *)&v, 2);
3409 }
3410 }
3411 0 return 1;
3412 }
3413 0 case CT_INT32: case CT_DATE32: {
3414 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3415 int32_t v;
3416 0 ▶ if (ct->code == CT_DATE32 && value_lens[i] >= 10
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_DATE32 && value_lens[i] >= 10 0 0 0
if (ct->code == CT_DATE32 && value_lens[i] >= 10 0 0 0
3417 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3418 0 ▶ if (value_lens[i] != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] != 10) return 0; 0 0 0
3419 0 ▶ if (!valid_date_prefix(values[i], value_lens[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(values[i], value_lens[i])) return 0; 0 0 0
3420 0 v = date_string_to_days(values[i], value_lens[i]);
3421 } else {
3422 0 int ok = 0;
3423 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3424 0 ▶ if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX)
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX) 0 0 0
if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX) 0 0 0
if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX) 0 0 0
3425 0 return 0;
3426 0 v = (int32_t)w;
3427 }
3428 0 nbuf_append(b, (const char *)&v, 4);
3429 }
3430 0 return 1;
3431 }
3432 0 case CT_INT64: {
3433 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3434 0 int ok = 0;
3435 0 int64_t v = parse_int64_strict(values[i], value_lens[i], &ok);
3436 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3437 0 nbuf_append(b, (const char *)&v, 8);
3438 }
3439 0 return 1;
3440 }
3441 0 case CT_UINT8: {
3442 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3443 0 int ok = 0;
3444 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i], &ok);
3445 0 ▶ if (!ok || w > 255) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || w > 255) return 0; 0 0 0
if (!ok || w > 255) return 0; 0 0 0
3446 {
3447 0 uint8_t v = (uint8_t)w;
3448 0 nbuf_append(b, (const char *)&v, 1);
3449 }
3450 }
3451 0 return 1;
3452 }
3453 0 case CT_BOOL: {
3454 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3455 uint8_t v;
3456 0 ▶ if (!bool_text_to_u8(values[i], value_lens[i], &v)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!bool_text_to_u8(values[i], value_lens[i], &v)) return 0; 0 0 0
3457 0 nbuf_append(b, (const char *)&v, 1);
3458 }
3459 0 return 1;
3460 }
3461 0 case CT_UINT16: case CT_DATE: {
3462 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3463 uint16_t v;
3464 0 ▶ if (ct->code == CT_DATE && value_lens[i] >= 10
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_DATE && value_lens[i] >= 10 0 0 0
if (ct->code == CT_DATE && value_lens[i] >= 10 0 0 0
3465 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3466 int32_t days;
3467 0 ▶ if (value_lens[i] != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] != 10) return 0; 0 0 0
3468 0 ▶ if (!valid_date_prefix(values[i], value_lens[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(values[i], value_lens[i])) return 0; 0 0 0
3469 0 days = date_string_to_days(values[i], value_lens[i]);
3470 0 ▶ if (days < 0 || days > 65535) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (days < 0 || days > 65535) return 0; 0 0 0
if (days < 0 || days > 65535) return 0; 0 0 0
3471 0 v = (uint16_t)days;
3472 } else {
3473 0 int ok = 0;
3474 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i],
3475 &ok);
3476 0 ▶ if (!ok || w > 65535) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || w > 65535) return 0; 0 0 0
if (!ok || w > 65535) return 0; 0 0 0
3477 0 v = (uint16_t)w;
3478 }
3479 0 nbuf_append(b, (const char *)&v, 2);
3480 }
3481 0 return 1;
3482 }
3483 0 case CT_UINT32: case CT_DATETIME: {
3484 0 char *saved_tz = NULL;
3485 0 int tz_set = 0, rc = 1;
3486 0 ▶ if (ct->code == CT_DATETIME && ct->tz) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_DATETIME && ct->tz) { 0 0 0
if (ct->code == CT_DATETIME && ct->tz) { 0 0 0
3487 0 saved_tz = set_tz(ct->tz);
3488 0 tz_set = 1;
3489 }
3490 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3491 uint32_t v;
3492 0 ▶ if (ct->code == CT_DATETIME && value_lens[i] >= 10
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_DATETIME && value_lens[i] >= 10 0 0 0
if (ct->code == CT_DATETIME && value_lens[i] >= 10 0 0 0
3493 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3494 int32_t days;
3495 int64_t total;
3496 0 ▶ if (value_lens[i] != 10 && value_lens[i] != 19) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (value_lens[i] != 10 && value_lens[i] != 19) { rc = 0; break; } 0 0 0
if (value_lens[i] != 10 && value_lens[i] != 19) { rc = 0; break; } 0 0 0
3497 0 ▶ if (!valid_date_prefix(values[i], value_lens[i])) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(values[i], value_lens[i])) { rc = 0; break; } 0 0 0
3498 0 ▶ if (value_lens[i] >= 19 && !valid_time_part(values[i])) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (value_lens[i] >= 19 && !valid_time_part(values[i])) { rc = 0; break; } 0 0 0
if (value_lens[i] >= 19 && !valid_time_part(values[i])) { rc = 0; break; } 0 0 0
3499 0 days = date_string_to_days(values[i], 10);
3500 0 total = (int64_t)days * 86400
3501 0 + parse_hms(values[i], value_lens[i]);
3502 0 ▶ if (tz_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz_set) { 0 0 0
3503 0 int tok = 0;
3504 0 total = wall_in_tz_to_epoch(total, ct->tz, &tok);
3505 0 ▶ if (!tok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) { rc = 0; break; } 0 0 0
3506 }
3507 0 ▶ if (total < 0 || total > (int64_t)UINT32_MAX) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (total < 0 || total > (int64_t)UINT32_MAX) { rc = 0; break; } 0 0 0
if (total < 0 || total > (int64_t)UINT32_MAX) { rc = 0; break; } 0 0 0
3508 0 v = (uint32_t)total;
3509 } else {
3510 0 int ok = 0;
3511 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i],
3512 &ok);
3513 0 ▶ if (!ok || w > (uint64_t)UINT32_MAX) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || w > (uint64_t)UINT32_MAX) { rc = 0; break; } 0 0 0
if (!ok || w > (uint64_t)UINT32_MAX) { rc = 0; break; } 0 0 0
3514 0 v = (uint32_t)w;
3515 }
3516 0 nbuf_append(b, (const char *)&v, 4);
3517 }
3518 0 ▶ if (tz_set) restore_tz(saved_tz);
Branch: 1 branch
BranchTrueFalseTotal
if (tz_set) restore_tz(saved_tz); 0 0 0
3519 0 return rc;
3520 }
3521 0 case CT_UINT64: {
3522 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3523 0 int ok = 0;
3524 0 uint64_t v = parse_uint64_strict(values[i], value_lens[i], &ok);
3525 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3526 0 nbuf_append(b, (const char *)&v, 8);
3527 }
3528 0 return 1;
3529 }
3530 0 case CT_FLOAT32: {
3531 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3532 char *endp;
3533 float v;
3534 0 ▶ if (value_lens[i] == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] == 0) return 0; 0 0 0
3535 0 v = strtof(values[i], &endp);
3536 0 ▶ if (endp != values[i] + value_lens[i]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != values[i] + value_lens[i]) return 0; 0 0 0
3537 0 nbuf_append(b, (const char *)&v, 4);
3538 }
3539 0 return 1;
3540 }
3541 0 case CT_FLOAT64: {
3542 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3543 char *endp;
3544 double v;
3545 0 ▶ if (value_lens[i] == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] == 0) return 0; 0 0 0
3546 0 v = strtod(values[i], &endp);
3547 0 ▶ if (endp != values[i] + value_lens[i]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != values[i] + value_lens[i]) return 0; 0 0 0
3548 0 nbuf_append(b, (const char *)&v, 8);
3549 }
3550 0 return 1;
3551 }
3552 0 case CT_DATETIME64: {
3553 0 char *saved_tz = NULL;
3554 0 int tz_set = 0, rc = 1;
3555 0 ▶ if (ct->tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->tz) { 0 0 0
3556 0 saved_tz = set_tz(ct->tz);
3557 0 tz_set = 1;
3558 }
3559 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3560 int64_t v;
3561 0 ▶ if (value_lens[i] >= 10 && values[i][4] == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (value_lens[i] >= 10 && values[i][4] == '-') { 0 0 0
if (value_lens[i] >= 10 && values[i][4] == '-') { 0 0 0
3562 0 int ok = 0;
3563 0 v = parse_datetime64_to_scaled(values[i], value_lens[i],
3564 0 ct->param, ct->tz, &ok);
3565 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
3566 } else {
3567 0 int ok = 0;
3568 0 v = parse_int64_strict(values[i], value_lens[i], &ok);
3569 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
3570 }
3571 0 nbuf_append(b, (const char *)&v, 8);
3572 }
3573 0 ▶ if (tz_set) restore_tz(saved_tz);
Branch: 1 branch
BranchTrueFalseTotal
if (tz_set) restore_tz(saved_tz); 0 0 0
3574 0 return rc;
3575 }
3576 0 case CT_TIME: {
3577 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3578 0 int ok = 0;
3579 0 int64_t v = parse_time_text(values[i], value_lens[i], 0, &ok);
3580 int32_t w;
3581 0 ▶ if (!ok || v > INT32_MAX || v < INT32_MIN) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || v > INT32_MAX || v < INT32_MIN) return 0; 0 0 0
if (!ok || v > INT32_MAX || v < INT32_MIN) return 0; 0 0 0
if (!ok || v > INT32_MAX || v < INT32_MIN) return 0; 0 0 0
3582 0 w = (int32_t)v;
3583 0 nbuf_append(b, (const char *)&w, 4);
3584 }
3585 0 return 1;
3586 }
3587 0 case CT_TIME64: {
3588 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3589 0 int ok = 0;
3590 0 int64_t v = parse_time_text(values[i], value_lens[i],
3591 ct->param, &ok);
3592 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3593 0 nbuf_append(b, (const char *)&v, 8);
3594 }
3595 0 return 1;
3596 }
3597 0 case CT_DECIMAL32: {
3598 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3599 0 int ok = 0;
3600 0 int32_t v = (int32_t)parse_decimal_to_raw(values[i], value_lens[i],
3601 ct->param, ct->precision,
3602 &ok);
3603 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3604 0 nbuf_append(b, (const char *)&v, 4);
3605 }
3606 0 return 1;
3607 }
3608 0 case CT_DECIMAL64: {
3609 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3610 0 int ok = 0;
3611 0 int64_t v = parse_decimal_to_raw(values[i], value_lens[i],
3612 ct->param, ct->precision, &ok);
3613 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3614 0 nbuf_append(b, (const char *)&v, 8);
3615 }
3616 0 return 1;
3617 }
3618 0 case CT_DECIMAL128: {
3619 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3620 unsigned char raw[16];
3621 0 ▶ if (!parse_decimal128_to_raw(values[i], value_lens[i],
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal128_to_raw(values[i], value_lens[i], 0 0 0
3622 ct->param, ct->precision, raw))
3623 0 return 0;
3624 0 nbuf_append(b, (const char *)raw, 16);
3625 }
3626 0 return 1;
3627 }
3628 0 case CT_DECIMAL256: {
3629 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3630 unsigned char raw[32];
3631 0 ▶ if (!parse_decimal256_to_raw(values[i], value_lens[i],
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal256_to_raw(values[i], value_lens[i], 0 0 0
3632 ct->param, ct->precision, raw))
3633 0 return 0;
3634 0 nbuf_append(b, (const char *)raw, 32);
3635 }
3636 0 return 1;
3637 }
3638 0 case CT_BFLOAT16: {
3639 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3640 char *endp;
3641 float fv;
3642 0 ▶ if (value_lens[i] == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] == 0) return 0; 0 0 0
3643 0 fv = (float)strtod(values[i], &endp);
3644 0 ▶ if (endp != values[i] + value_lens[i]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != values[i] + value_lens[i]) return 0; 0 0 0
3645 0 uint8_t b32[4]; memcpy(b32, &fv, 4);
3646 0 nbuf_append(b, (const char *)&b32[2], 2);
3647 }
3648 0 return 1;
3649 }
3650 0 case CT_STRING: {
3651 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3652 0 ▶ if (memchr(values[i], '\\', value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(values[i], '\\', value_lens[i])) { 0 0 0
3653 char *tmp;
3654 size_t ulen;
3655 0 Newx(tmp, value_lens[i], char);
3656 0 ulen = tsv_unescape(values[i], value_lens[i], tmp);
3657 0 nbuf_string(b, tmp, ulen);
3658 0 Safefree(tmp);
3659 } else {
3660 0 nbuf_string(b, values[i], value_lens[i]);
3661 }
3662 }
3663 0 return 1;
3664 }
3665 0 case CT_FIXEDSTRING: {
3666 0 size_t fsz = (size_t)ct->param;
3667 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3668 0 ▶ if (memchr(values[i], '\\', value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(values[i], '\\', value_lens[i])) { 0 0 0
3669 char *tmp;
3670 size_t ulen;
3671 0 size_t tmp_sz = value_lens[i] > fsz ? value_lens[i] : fsz;
3672 0 Newxz(tmp, tmp_sz, char);
3673 0 ulen = tsv_unescape(values[i], value_lens[i], tmp);
3674 0 ▶ if (ulen > fsz) { Safefree(tmp); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ulen > fsz) { Safefree(tmp); return 0; } 0 0 0
3675 0 nbuf_append(b, tmp, fsz);
3676 0 Safefree(tmp);
3677 } else {
3678 size_t cplen;
3679 0 ▶ if (value_lens[i] > fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] > fsz) return 0; 0 0 0
3680 0 cplen = value_lens[i];
3681 0 nbuf_grow(b, fsz);
3682 0 memcpy(b->data + b->len, values[i], cplen);
3683 0 ▶ if (cplen < fsz)
Branch: 1 branch
BranchTrueFalseTotal
if (cplen < fsz) 0 0 0
3684 0 memset(b->data + b->len + cplen, 0, fsz - cplen);
3685 0 b->len += fsz;
3686 }
3687 }
3688 0 return 1;
3689 }
3690 0 case CT_UUID: {
3691 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3692 unsigned char ubytes[16];
3693 0 ▶ if (!parse_uuid_to_le_bytes(values[i], value_lens[i], ubytes))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_uuid_to_le_bytes(values[i], value_lens[i], ubytes)) 0 0 0
3694 0 return 0;
3695 0 nbuf_append(b, (const char *)ubytes, 16);
3696 }
3697 0 return 1;
3698 }
3699 0 case CT_IPV4: {
3700 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3701 0 int ok = 0;
3702 0 uint32_t v = parse_ipv4_to_u32(values[i], value_lens[i], &ok);
3703 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3704 0 nbuf_append(b, (const char *)&v, 4);
3705 }
3706 0 return 1;
3707 }
3708 0 case CT_IPV6: {
3709 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3710 unsigned char addr[16];
3711 0 ▶ if (!parse_ipv6_to_bytes(values[i], value_lens[i], addr))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_ipv6_to_bytes(values[i], value_lens[i], addr)) 0 0 0
3712 0 return 0;
3713 0 nbuf_append(b, (const char *)addr, 16);
3714 }
3715 0 return 1;
3716 }
3717 0 case CT_NULLABLE: {
3718 /* null bitmap + inner column */
3719 uint8_t *nulls;
3720 const char **inner_vals;
3721 size_t *inner_lens;
3722  
3723 0 Newx(nulls, nrows, uint8_t);
3724 0 ▶ Newxz(inner_vals, nrows, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(inner_vals, nrows, const char *); 0 0 0
3725 0 ▶ Newx(inner_lens, nrows, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(inner_lens, nrows, size_t); 0 0 0
3726  
3727 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3728 0 ▶ if (is_tsv_null(values[i], value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_tsv_null(values[i], value_lens[i])) { 0 0 0
3729 0 nulls[i] = 1;
3730 0 nullable_inner_zero(ct->inner,
3731 0 &inner_vals[i], &inner_lens[i]);
3732 } else {
3733 0 nulls[i] = 0;
3734 0 inner_vals[i] = values[i];
3735 0 inner_lens[i] = value_lens[i];
3736 }
3737 }
3738  
3739 0 nbuf_append(b, (const char *)nulls, nrows);
3740 {
3741 0 int rc = encode_column_text(b, inner_vals, inner_lens, nrows, ct->inner);
3742 0 Safefree(nulls);
3743 0 Safefree(inner_vals);
3744 0 Safefree(inner_lens);
3745 0 return rc;
3746 }
3747 }
3748 0 case CT_LOWCARDINALITY: {
3749 /* Trivial 1:1 dictionary: each value is its own dict entry. */
3750 size_t idx_size;
3751 native_buf_t dict_buf;
3752 int rc;
3753  
3754 0 ▶ if (nrows == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return 1; 0 0 0
3755 0 ▶ if (ct->inner->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->inner->code == CT_NULLABLE) { 0 0 0
3756 /* Dict travels as plain T with slot 0 reserved for NULL:
3757 * [T default] + rows (nulls substituted), null->0 else i+1. */
3758 const char **dvals;
3759 size_t *dlens;
3760 uint8_t *nulls;
3761 const char *zp;
3762 size_t zlen;
3763 0 nullable_inner_zero(ct->inner->inner, &zp, &zlen);
3764 0 ▶ Newx(dvals, nrows + 1, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dvals, nrows + 1, const char *); 0 0 0
3765 0 ▶ Newx(dlens, nrows + 1, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dlens, nrows + 1, size_t); 0 0 0
3766 0 ▶ Newx(nulls, nrows ? nrows : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(nulls, nrows ? nrows : 1, uint8_t); 0 0 0
3767 0 dvals[0] = zp;
3768 0 dlens[0] = zlen;
3769 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3770 0 ▶ if (is_tsv_null(values[i], value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_tsv_null(values[i], value_lens[i])) { 0 0 0
3771 0 nulls[i] = 1;
3772 0 dvals[i + 1] = zp;
3773 0 dlens[i + 1] = zlen;
3774 } else {
3775 0 nulls[i] = 0;
3776 0 dvals[i + 1] = values[i];
3777 0 dlens[i + 1] = value_lens[i];
3778 }
3779 }
3780 0 lc_write_keys_header(b, nrows + 1, &idx_size);
3781 0 nbuf_init(&dict_buf);
3782 0 rc = encode_column_text(&dict_buf, dvals, dlens, nrows + 1,
3783 0 ct->inner->inner);
3784 0 Safefree(dvals);
3785 0 Safefree(dlens);
3786 0 ▶ if (!rc) { Safefree(dict_buf.data); Safefree(nulls); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { Safefree(dict_buf.data); Safefree(nulls); return 0; } 0 0 0
3787 0 nbuf_append(b, dict_buf.data, dict_buf.len);
3788 0 Safefree(dict_buf.data);
3789 0 lc_write_nullable_indices(b, nrows, idx_size, nulls);
3790 0 Safefree(nulls);
3791 0 return 1;
3792 }
3793  
3794 0 lc_write_keys_header(b, nrows, &idx_size);
3795 0 nbuf_init(&dict_buf);
3796 0 rc = encode_column_text(&dict_buf, values, value_lens, nrows, ct->inner);
3797 0 ▶ if (!rc) { Safefree(dict_buf.data); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { Safefree(dict_buf.data); return 0; } 0 0 0
3798 0 nbuf_append(b, dict_buf.data, dict_buf.len);
3799 0 Safefree(dict_buf.data);
3800 0 lc_write_indices(b, nrows, idx_size);
3801 0 return 1;
3802 }
3803 0 default:
3804 0 return 0; /* unsupported type — fall back to inline SQL */
3805 }
3806 }
3807  
3808 /* A plain number never reads as date text; stringifying it would only
3809 * allocate a PV in the caller's scalar. */
3810 #define SV_PLAIN_NUM(sv) (!SvGMAGICAL(sv) && !SvPOK(sv) && !SvROK(sv) \
3811 && (SvIOK(sv) || SvNOK(sv)))
3812  
3813 /*
3814 * Encode a column of Perl SV values into native binary format.
3815 * Like encode_column_text() but takes SVs directly — no TSV parsing/unescaping.
3816 * Returns 1 on success, 0 if type is unsupported.
3817 */
3818 28 static int encode_column_sv(pTHX_ native_buf_t *b,
3819 SV **values, uint64_t nrows,
3820 col_type_t *ct) {
3821 uint64_t i;
3822  
3823 28 switch (ct->code) {
3824 0 case CT_INT8: {
3825 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3826 0 int ok = 0;
3827 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
3828 0 ▶ if (!ok || w < -128 || w > 127) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || w < -128 || w > 127) return 0; 0 0 0
if (!ok || w < -128 || w > 127) return 0; 0 0 0
if (!ok || w < -128 || w > 127) return 0; 0 0 0
3829 {
3830 0 int8_t v = (int8_t)w;
3831 0 nbuf_append(b, (const char *)&v, 1);
3832 }
3833 }
3834 0 return 1;
3835 }
3836 0 case CT_ENUM8: case CT_ENUM16: {
3837 0 enum_entry_t *entries = NULL;
3838 0 size_t nentries = enum_parse_entries(ct->type_str, ct->type_str_len, &entries);
3839 0 int numeric_labels = enum_has_numeric_label(entries, nentries);
3840 0 int rc = 1;
3841 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3842 0 SV *v = values[i];
3843 0 int code, ok = 0;
3844 0 ▶ if (!SvOK(v)) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { rc = 0; break; } 0 0 0
3845 0 ▶ if (SvPOK(v) || numeric_labels) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(v) || numeric_labels) { 0 0 0
if (SvPOK(v) || numeric_labels) { 0 0 0
3846 STRLEN vlen;
3847 0 const char *s = SvPV(v, vlen);
3848 0 ok = enum_code_for_label(entries, nentries, s, vlen, &code);
3849 }
3850 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
3851 0 int64_t w = sv_to_int64_strict(aTHX_ v, &ok);
3852 0 ▶ if (!ok || !enum_has_code(entries, nentries, w)) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || !enum_has_code(entries, nentries, w)) { rc = 0; break; } 0 0 0
if (!ok || !enum_has_code(entries, nentries, w)) { rc = 0; break; } 0 0 0
3853 0 code = (int)w;
3854 }
3855 0 nbuf_enum(b, ct->code, code);
3856 }
3857 0 enum_free_entries(entries, nentries);
3858 0 return rc;
3859 }
3860 0 case CT_INT16: {
3861 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3862 0 int ok = 0;
3863 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
3864 0 ▶ if (!ok || w < -32768 || w > 32767) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || w < -32768 || w > 32767) return 0; 0 0 0
if (!ok || w < -32768 || w > 32767) return 0; 0 0 0
if (!ok || w < -32768 || w > 32767) return 0; 0 0 0
3865 {
3866 0 int16_t v = (int16_t)w;
3867 0 nbuf_append(b, (const char *)&v, 2);
3868 }
3869 }
3870 0 return 1;
3871 }
3872 0 case CT_INT32: case CT_DATE32: {
3873 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3874 int32_t v;
3875 STRLEN vlen;
3876 const char *s;
3877 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
3878 0 ▶ if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; }
Branch: 5 branches
BranchTrueFalseTotal
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
3879 0 else s = SvPV(values[i], vlen);
3880 0 ▶ if (ct->code == CT_DATE32 && vlen >= 10 && s[4] == '-') {
Branch: 3 branches
BranchTrueFalseTotal
if (ct->code == CT_DATE32 && vlen >= 10 && s[4] == '-') { 0 0 0
if (ct->code == CT_DATE32 && vlen >= 10 && s[4] == '-') { 0 0 0
if (ct->code == CT_DATE32 && vlen >= 10 && s[4] == '-') { 0 0 0
3881 0 ▶ if (vlen != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen != 10) return 0; 0 0 0
3882 0 ▶ if (!valid_date_prefix(s, vlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, vlen)) return 0; 0 0 0
3883 0 v = date_string_to_days(s, vlen);
3884 } else {
3885 0 int ok = 0;
3886 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
3887 0 ▶ if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX)
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX) 0 0 0
if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX) 0 0 0
if (!ok || w < (int64_t)INT32_MIN || w > (int64_t)INT32_MAX) 0 0 0
3888 0 return 0;
3889 0 v = (int32_t)w;
3890 }
3891 0 nbuf_append(b, (const char *)&v, 4);
3892 }
3893 0 return 1;
3894 }
3895 0 case CT_INT64: {
3896 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3897 0 int ok = 0;
3898 0 int64_t v = sv_to_int64_strict(aTHX_ values[i], &ok);
3899 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3900 0 nbuf_append(b, (const char *)&v, 8);
3901 }
3902 0 return 1;
3903 }
3904 0 case CT_UINT8: {
3905 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3906 0 int ok = 0;
3907 0 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
3908 0 ▶ if (!ok || w > 255) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || w > 255) return 0; 0 0 0
if (!ok || w > 255) return 0; 0 0 0
3909 {
3910 0 uint8_t v = (uint8_t)w;
3911 0 nbuf_append(b, (const char *)&v, 1);
3912 }
3913 }
3914 0 return 1;
3915 }
3916 0 case CT_BOOL: {
3917 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3918 uint8_t v;
3919 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
3920 0 v = SvTRUE(values[i]) ? 1 : 0;
3921 0 nbuf_append(b, (const char *)&v, 1);
3922 }
3923 0 return 1;
3924 }
3925 0 case CT_UINT16: case CT_DATE: {
3926 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3927 uint16_t v;
3928 STRLEN vlen;
3929 const char *s;
3930 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
3931 0 ▶ if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; }
Branch: 5 branches
BranchTrueFalseTotal
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
3932 0 else s = SvPV(values[i], vlen);
3933 0 ▶ if (ct->code == CT_DATE && vlen >= 10 && s[4] == '-') {
Branch: 3 branches
BranchTrueFalseTotal
if (ct->code == CT_DATE && vlen >= 10 && s[4] == '-') { 0 0 0
if (ct->code == CT_DATE && vlen >= 10 && s[4] == '-') { 0 0 0
if (ct->code == CT_DATE && vlen >= 10 && s[4] == '-') { 0 0 0
3934 int32_t days;
3935 0 ▶ if (vlen != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen != 10) return 0; 0 0 0
3936 0 ▶ if (!valid_date_prefix(s, vlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, vlen)) return 0; 0 0 0
3937 0 days = date_string_to_days(s, vlen);
3938 0 ▶ if (days < 0 || days > 65535) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (days < 0 || days > 65535) return 0; 0 0 0
if (days < 0 || days > 65535) return 0; 0 0 0
3939 0 v = (uint16_t)days;
3940 } else {
3941 0 int ok = 0;
3942 0 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
3943 0 ▶ if (!ok || w > 65535) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || w > 65535) return 0; 0 0 0
if (!ok || w > 65535) return 0; 0 0 0
3944 0 v = (uint16_t)w;
3945 }
3946 0 nbuf_append(b, (const char *)&v, 2);
3947 }
3948 0 return 1;
3949 }
3950 2 case CT_UINT32: case CT_DATETIME: {
3951 2 int tz_set = 0, rc = 1;
3952 2 ENTER;
3953 4 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 2 2 4
3954 uint32_t v;
3955 STRLEN vlen;
3956 const char *s;
3957 2 ▶ if (!SvOK(values[i])) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { rc = 0; break; } 0 2 2
3958 2 ▶ if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; }
Branch: 5 branches
BranchTrueFalseTotal
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 2 0 2
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 2 0 2
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 2 0 2
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 2 2
if (SV_PLAIN_NUM(values[i])) { s = NULL; vlen = 0; } 0 0 0
3959 0 else s = SvPV(values[i], vlen);
3960 2 ▶ if (ct->code == CT_DATETIME && vlen >= 10 && s[4] == '-') {
Branch: 3 branches
BranchTrueFalseTotal
if (ct->code == CT_DATETIME && vlen >= 10 && s[4] == '-') { 0 2 2
if (ct->code == CT_DATETIME && vlen >= 10 && s[4] == '-') { 0 0 0
if (ct->code == CT_DATETIME && vlen >= 10 && s[4] == '-') { 0 0 0
3961 int32_t days;
3962 int64_t total;
3963 0 ▶ if (ct->tz && !tz_set) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct->tz && !tz_set) { 0 0 0
if (ct->tz && !tz_set) { 0 0 0
3964 0 set_tz_scoped(aTHX_ ct->tz);
3965 0 tz_set = 1;
3966 }
3967 0 ▶ if (vlen != 10 && vlen != 19) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (vlen != 10 && vlen != 19) { rc = 0; break; } 0 0 0
if (vlen != 10 && vlen != 19) { rc = 0; break; } 0 0 0
3968 0 ▶ if (!valid_date_prefix(s, vlen)) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, vlen)) { rc = 0; break; } 0 0 0
3969 0 ▶ if (vlen >= 19 && !valid_time_part(s)) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (vlen >= 19 && !valid_time_part(s)) { rc = 0; break; } 0 0 0
if (vlen >= 19 && !valid_time_part(s)) { rc = 0; break; } 0 0 0
3970 0 days = date_string_to_days(s, 10);
3971 0 total = (int64_t)days * 86400 + parse_hms(s, vlen);
3972 0 ▶ if (tz_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz_set) { 0 0 0
3973 0 int tok = 0;
3974 0 total = wall_in_tz_to_epoch(total, ct->tz, &tok);
3975 0 ▶ if (!tok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) { rc = 0; break; } 0 0 0
3976 }
3977 0 ▶ if (total < 0 || total > (int64_t)UINT32_MAX) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (total < 0 || total > (int64_t)UINT32_MAX) { rc = 0; break; } 0 0 0
if (total < 0 || total > (int64_t)UINT32_MAX) { rc = 0; break; } 0 0 0
3978 0 v = (uint32_t)total;
3979 } else {
3980 2 int ok = 0;
3981 2 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
3982 2 ▶ if (!ok || w > (uint64_t)UINT32_MAX) { rc = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || w > (uint64_t)UINT32_MAX) { rc = 0; break; } 2 0 2
if (!ok || w > (uint64_t)UINT32_MAX) { rc = 0; break; } 0 2 2
3983 2 v = (uint32_t)w;
3984 }
3985 2 nbuf_append(b, (const char *)&v, 4);
3986 }
3987 2 LEAVE;
3988 2 return rc;
3989 }
3990 0 case CT_UINT64: {
3991 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3992 0 int ok = 0;
3993 0 uint64_t v = sv_to_uint64_strict(aTHX_ values[i], &ok);
3994 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3995 0 nbuf_append(b, (const char *)&v, 8);
3996 }
3997 0 return 1;
3998 }
3999 0 case CT_FLOAT32: {
4000 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4001 float v;
4002 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4003 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4004 0 v = (float)SvNV(values[i]);
4005 } else {
4006 STRLEN vlen;
4007 0 const char *s = SvPV(values[i], vlen);
4008 char *endp;
4009 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4010 0 v = strtof(s, &endp);
4011 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4012 }
4013 0 nbuf_append(b, (const char *)&v, 4);
4014 }
4015 0 return 1;
4016 }
4017 0 case CT_FLOAT64: {
4018 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4019 double v;
4020 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4021 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4022 0 v = SvNV(values[i]);
4023 } else {
4024 STRLEN vlen;
4025 0 const char *s = SvPV(values[i], vlen);
4026 char *endp;
4027 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4028 0 v = strtod(s, &endp);
4029 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4030 }
4031 0 nbuf_append(b, (const char *)&v, 8);
4032 }
4033 0 return 1;
4034 }
4035 26 case CT_DATETIME64: {
4036 26 int tz_set = 0, rc = 1;
4037 26 ENTER;
4038 28 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 18 10 28
4039 int64_t v;
4040 34 ▶ if (!SvOK(values[i])) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { rc = 0; break; } 0 18 18
4041 18 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 18 18
4042 0 v = (int64_t)SvIV(values[i]);
4043 } else {
4044 STRLEN vlen;
4045 18 const char *s = SvPV(values[i], vlen);
4046 20 ▶ if (vlen >= 10 && s[4] == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (vlen >= 10 && s[4] == '-') { 18 0 18
if (vlen >= 10 && s[4] == '-') { 18 0 18
4047 18 int ok = 0;
4048 18 ▶ if (ct->tz && !tz_set) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct->tz && !tz_set) { 18 0 18
if (ct->tz && !tz_set) { 18 0 18
4049 18 set_tz_scoped(aTHX_ ct->tz);
4050 18 tz_set = 1;
4051 }
4052 36 v = parse_datetime64_to_scaled(s, vlen, ct->param,
4053 18 ct->tz, &ok);
4054 18 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 16 2 18
4055 } else {
4056 0 int ok = 0;
4057 0 v = parse_int64_strict(s, vlen, &ok);
4058 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
4059 }
4060 }
4061 2 nbuf_append(b, (const char *)&v, 8);
4062 }
4063 26 LEAVE;
4064 26 return rc;
4065 }
4066 0 case CT_TIME: {
4067 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4068 int64_t v;
4069 int32_t w;
4070 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4071 0 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 0 0
4072 0 v = (int64_t)SvIV(values[i]);
4073 } else {
4074 STRLEN vlen;
4075 0 const char *s = SvPV(values[i], vlen);
4076 0 int ok = 0;
4077 0 v = parse_time_text(s, vlen, 0, &ok);
4078 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4079 0 v = parse_int64_strict(s, vlen, &ok);
4080 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4081 }
4082 }
4083 0 ▶ if (v > INT32_MAX || v < INT32_MIN) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (v > INT32_MAX || v < INT32_MIN) return 0; 0 0 0
if (v > INT32_MAX || v < INT32_MIN) return 0; 0 0 0
4084 0 w = (int32_t)v;
4085 0 nbuf_append(b, (const char *)&w, 4);
4086 }
4087 0 return 1;
4088 }
4089 0 case CT_TIME64: {
4090 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4091 int64_t v;
4092 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4093 0 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 0 0
4094 0 v = (int64_t)SvIV(values[i]);
4095 } else {
4096 STRLEN vlen;
4097 0 const char *s = SvPV(values[i], vlen);
4098 0 int ok = 0;
4099 0 v = parse_time_text(s, vlen, ct->param, &ok);
4100 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4101 0 v = parse_int64_strict(s, vlen, &ok);
4102 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4103 }
4104 }
4105 0 nbuf_append(b, (const char *)&v, 8);
4106 }
4107 0 return 1;
4108 }
4109 0 case CT_DECIMAL32: {
4110 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4111 STRLEN vlen;
4112 const char *s;
4113 0 int ok = 0;
4114 int32_t v;
4115 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4116 0 s = SvPV(values[i], vlen);
4117 0 v = (int32_t)parse_decimal_to_raw(s, vlen, ct->param,
4118 ct->precision, &ok);
4119 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4120 0 nbuf_append(b, (const char *)&v, 4);
4121 }
4122 0 return 1;
4123 }
4124 0 case CT_DECIMAL64: {
4125 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4126 STRLEN vlen;
4127 const char *s;
4128 0 int ok = 0;
4129 int64_t v;
4130 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4131 0 s = SvPV(values[i], vlen);
4132 0 v = parse_decimal_to_raw(s, vlen, ct->param, ct->precision, &ok);
4133 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4134 0 nbuf_append(b, (const char *)&v, 8);
4135 }
4136 0 return 1;
4137 }
4138 0 case CT_DECIMAL128: {
4139 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4140 STRLEN vlen;
4141 const char *s;
4142 unsigned char raw[16];
4143 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4144 0 s = SvPV(values[i], vlen);
4145 0 ▶ if (!parse_decimal128_to_raw(s, vlen, ct->param, ct->precision,
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal128_to_raw(s, vlen, ct->param, ct->precision, 0 0 0
4146 raw))
4147 0 return 0;
4148 0 nbuf_append(b, (const char *)raw, 16);
4149 }
4150 0 return 1;
4151 }
4152 0 case CT_DECIMAL256: {
4153 /* Decimal text, or raw 32 bytes (LE, signed) — the shape the
4154 * decoder delivers. Useful with Math::BigInt::to_bytes(LE).
4155 * The raw fallback needs a non-text byte, so a 32-char typo
4156 * fails loudly instead of storing garbage. */
4157 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4158 STRLEN slen;
4159 const char *s;
4160 unsigned char raw[32];
4161 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4162 0 s = SvPV(values[i], slen);
4163 0 ▶ if (parse_decimal256_to_raw(s, slen, ct->param, ct->precision, raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_decimal256_to_raw(s, slen, ct->param, ct->precision, raw)) { 0 0 0
4164 0 nbuf_append(b, (const char *)raw, 32);
4165 0 ▶ } else if (slen == 32) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (slen == 32) { 0 0 0
4166 STRLEN k;
4167 0 int binary = 0;
4168 0 ▶ for (k = 0; k < slen; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < slen; k++) { 0 0 0
4169 0 unsigned char c = (unsigned char)s[k];
4170 0 ▶ if (c < 0x20 || c > 0x7E) { binary = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x20 || c > 0x7E) { binary = 1; break; } 0 0 0
if (c < 0x20 || c > 0x7E) { binary = 1; break; } 0 0 0
4171 }
4172 0 ▶ if (!binary) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!binary) return 0; 0 0 0
4173 0 nbuf_append(b, s, 32);
4174 } else {
4175 0 return 0;
4176 }
4177 }
4178 0 return 1;
4179 }
4180 0 case CT_BFLOAT16: {
4181 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4182 float fv;
4183 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4184 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4185 0 fv = (float)SvNV(values[i]);
4186 } else {
4187 STRLEN vlen;
4188 0 const char *s = SvPV(values[i], vlen);
4189 char *endp;
4190 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4191 0 fv = (float)strtod(s, &endp);
4192 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4193 }
4194 0 uint8_t b32[4]; memcpy(b32, &fv, 4);
4195 /* Truncate to top 16 bits — bytes [2,3] of a LE Float32. */
4196 0 nbuf_append(b, (const char *)&b32[2], 2);
4197 }
4198 0 return 1;
4199 }
4200 0 case CT_STRING: {
4201 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4202 STRLEN vlen;
4203 const char *v;
4204 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4205 0 v = SvPV(values[i], vlen);
4206 0 nbuf_string(b, v, vlen);
4207 }
4208 0 return 1;
4209 }
4210 0 case CT_FIXEDSTRING: {
4211 0 size_t fsz = (size_t)ct->param;
4212 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4213 STRLEN vlen;
4214 const char *v;
4215 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4216 0 v = SvPV(values[i], vlen);
4217 0 ▶ if ((size_t)vlen > fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)vlen > fsz) return 0; 0 0 0
4218 0 nbuf_grow(b, fsz);
4219 0 memcpy(b->data + b->len, v, vlen);
4220 0 ▶ if ((size_t)vlen < fsz)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)vlen < fsz) 0 0 0
4221 0 memset(b->data + b->len + vlen, 0, fsz - (size_t)vlen);
4222 0 b->len += fsz;
4223 }
4224 0 return 1;
4225 }
4226 0 case CT_UUID: {
4227 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4228 unsigned char ubytes[16];
4229 STRLEN slen;
4230 const char *s;
4231 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4232 0 s = SvPV(values[i], slen);
4233 0 ▶ if (!parse_uuid_to_le_bytes(s, slen, ubytes)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_uuid_to_le_bytes(s, slen, ubytes)) return 0; 0 0 0
4234 0 nbuf_append(b, (const char *)ubytes, 16);
4235 }
4236 0 return 1;
4237 }
4238 0 case CT_IPV4: {
4239 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4240 STRLEN vlen;
4241 const char *s;
4242 uint32_t v;
4243 0 int ok = 0;
4244 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4245 0 s = SvPV(values[i], vlen);
4246 0 v = parse_ipv4_to_u32(s, (size_t)vlen, &ok);
4247 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4248 0 nbuf_append(b, (const char *)&v, 4);
4249 }
4250 0 return 1;
4251 }
4252 0 case CT_IPV6: {
4253 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4254 unsigned char addr[16];
4255 STRLEN vlen;
4256 const char *s;
4257 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4258 0 s = SvPV(values[i], vlen);
4259 0 ▶ if (!parse_ipv6_to_bytes(s, (size_t)vlen, addr)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_ipv6_to_bytes(s, (size_t)vlen, addr)) return 0; 0 0 0
4260 0 nbuf_append(b, (const char *)addr, 16);
4261 }
4262 0 return 1;
4263 }
4264 0 case CT_NULLABLE: {
4265 uint8_t *nulls;
4266 SV **inner_vals;
4267 SV *zero_sv;
4268  
4269 0 Newx(nulls, nrows, uint8_t);
4270 0 ▶ Newx(inner_vals, nrows ? nrows : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newx(inner_vals, nrows ? nrows : 1, SV *); 0 0 0
Newx(inner_vals, nrows ? nrows : 1, SV *); 0 0 0
Newx(inner_vals, nrows ? nrows : 1, SV *); 0 0 0
4271 0 zero_sv = nullable_inner_zero_sv(aTHX_ ct->inner);
4272  
4273 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4274 0 ▶ if (!SvOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { 0 0 0
4275 0 nulls[i] = 1;
4276 0 inner_vals[i] = zero_sv;
4277 } else {
4278 0 nulls[i] = 0;
4279 0 inner_vals[i] = values[i];
4280 }
4281 }
4282  
4283 0 nbuf_append(b, (const char *)nulls, nrows);
4284 {
4285 0 int rc = encode_column_sv(aTHX_ b, inner_vals, nrows, ct->inner);
4286 0 Safefree(nulls);
4287 0 Safefree(inner_vals);
4288 0 SvREFCNT_dec(zero_sv);
4289 0 return rc;
4290 }
4291 }
4292 0 case CT_LOWCARDINALITY: {
4293 size_t idx_size;
4294 native_buf_t dict_buf;
4295 int rc;
4296  
4297 0 ▶ if (nrows == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return 1; 0 0 0
4298 0 ▶ if (ct->inner->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->inner->code == CT_NULLABLE) { 0 0 0
4299 /* Dict travels as plain T with slot 0 reserved for NULL:
4300 * [T default] + rows (nulls substituted), null->0 else i+1. */
4301 SV **dvals;
4302 uint8_t *nulls;
4303 0 SV *zero_sv = nullable_inner_zero_sv(aTHX_ ct->inner->inner);
4304 0 ▶ Newx(dvals, nrows + 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dvals, nrows + 1, SV *); 0 0 0
4305 0 ▶ Newx(nulls, nrows ? nrows : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(nulls, nrows ? nrows : 1, uint8_t); 0 0 0
4306 0 dvals[0] = zero_sv;
4307 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4308 0 ▶ if (!SvOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { 0 0 0
4309 0 nulls[i] = 1;
4310 0 dvals[i + 1] = zero_sv;
4311 } else {
4312 0 nulls[i] = 0;
4313 0 dvals[i + 1] = values[i];
4314 }
4315 }
4316 0 lc_write_keys_header(b, nrows + 1, &idx_size);
4317 0 nbuf_init(&dict_buf);
4318 0 rc = encode_column_sv(aTHX_ &dict_buf, dvals, nrows + 1,
4319 0 ct->inner->inner);
4320 0 Safefree(dvals);
4321 0 SvREFCNT_dec(zero_sv);
4322 0 ▶ if (!rc) { Safefree(dict_buf.data); Safefree(nulls); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { Safefree(dict_buf.data); Safefree(nulls); return 0; } 0 0 0
4323 0 nbuf_append(b, dict_buf.data, dict_buf.len);
4324 0 Safefree(dict_buf.data);
4325 0 lc_write_nullable_indices(b, nrows, idx_size, nulls);
4326 0 Safefree(nulls);
4327 0 return 1;
4328 }
4329  
4330 0 lc_write_keys_header(b, nrows, &idx_size);
4331 0 nbuf_init(&dict_buf);
4332 0 rc = encode_column_sv(aTHX_ &dict_buf, values, nrows, ct->inner);
4333 0 ▶ if (!rc) { Safefree(dict_buf.data); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { Safefree(dict_buf.data); return 0; } 0 0 0
4334 0 nbuf_append(b, dict_buf.data, dict_buf.len);
4335 0 Safefree(dict_buf.data);
4336 0 lc_write_indices(b, nrows, idx_size);
4337 0 return 1;
4338 }
4339 0 case CT_ARRAY: {
4340 /* Each value must be an AV ref. Wire format: offsets + flat inner data */
4341 0 uint64_t total = 0;
4342 uint64_t *offsets;
4343 SV **all_elems;
4344 0 uint64_t pos = 0;
4345 int rc;
4346  
4347 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4348 AV *av;
4349 0 ▶ if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV) 0 0 0
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV) 0 0 0
4350 0 return 0;
4351 0 av = (AV *)SvRV(values[i]);
4352 0 ▶ { SSize_t cnt = av_len(av) + 1; if (cnt > 0) total += (uint64_t)cnt; }
Branch: 1 branch
BranchTrueFalseTotal
{ SSize_t cnt = av_len(av) + 1; if (cnt > 0) total += (uint64_t)cnt; } 0 0 0
4353 }
4354  
4355 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
4356 0 ▶ Newx(all_elems, total ? total : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newx(all_elems, total ? total : 1, SV *); 0 0 0
Newx(all_elems, total ? total : 1, SV *); 0 0 0
Newx(all_elems, total ? total : 1, SV *); 0 0 0
4357  
4358 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4359 0 AV *av = (AV *)SvRV(values[i]);
4360 0 SSize_t n = av_len(av) + 1, j;
4361 /* av_fetch on a tie returns a lazy mortal proxy: copy the
4362 * fetched value (borrowing the proxy would re-FETCH on
4363 * every later read). Plain arrays borrow as before. */
4364 0 int av_magical = SvRMAGICAL((SV *)av);
4365 /* A tie whose FETCHSIZE grew since the sizing pass would
4366 * overflow all_elems: fail clean instead. */
4367 0 ▶ if (n > 0 && (uint64_t)n > total - pos)
Branch: 2 branches
BranchTrueFalseTotal
if (n > 0 && (uint64_t)n > total - pos) 0 0 0
if (n > 0 && (uint64_t)n > total - pos) 0 0 0
4368 0 goto arr_oom;
4369 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
4370 0 SV **ep = av_fetch(av, j, 0);
4371 0 ▶ SV *elem = (ep && *ep) ? *ep : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *elem = (ep && *ep) ? *ep : &PL_sv_undef; 0 0 0
SV *elem = (ep && *ep) ? *ep : &PL_sv_undef; 0 0 0
4372 /* Fetch exactly once: a borrowed proxy re-FETCHes on
4373 * every later read, and a later SvPV would too. */
4374 0 ▶ if (av_magical || SvGMAGICAL(elem))
Branch: 2 branches
BranchTrueFalseTotal
if (av_magical || SvGMAGICAL(elem)) 0 0 0
if (av_magical || SvGMAGICAL(elem)) 0 0 0
4375 0 elem = sv_2mortal(newSVsv(elem));
4376 0 all_elems[pos++] = elem;
4377 }
4378 0 offsets[i] = pos;
4379 }
4380 /* Encode what was actually gathered: a tie that shrank since
4381 * the sizing pass must not leave total pointing past pos. */
4382 0 total = pos;
4383  
4384 /* write offsets as uint64 LE */
4385 0 nbuf_append(b, (const char *)offsets, nrows * 8);
4386 0 rc = encode_column_sv(aTHX_ b, all_elems, total, ct->inner);
4387 0 Safefree(offsets);
4388 0 Safefree(all_elems);
4389 0 return rc;
4390 0 arr_oom:
4391 0 Safefree(offsets);
4392 0 Safefree(all_elems);
4393 0 return 0;
4394 }
4395 0 case CT_TUPLE: {
4396 /* Each value must be an AV ref with num_inners elements */
4397 int j;
4398 0 ▶ if (ct->num_inners == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners == 0) { 0 0 0
4399 /* Empty Tuple(): 1 placeholder byte per row on the wire */
4400 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4401 0 ▶ if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV) 0 0 0
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV) 0 0 0
4402 0 return 0;
4403 0 nbuf_append(b, "0", 1);
4404 }
4405 0 return 1;
4406 }
4407 0 ▶ for (j = 0; j < ct->num_inners; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) { 0 0 0
4408 SV **col_vals;
4409 int rc;
4410 0 ▶ Newx(col_vals, nrows ? nrows : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newx(col_vals, nrows ? nrows : 1, SV *); 0 0 0
Newx(col_vals, nrows ? nrows : 1, SV *); 0 0 0
Newx(col_vals, nrows ? nrows : 1, SV *); 0 0 0
4411 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4412 AV *av;
4413 SV **ep;
4414 SV *elem;
4415 0 ▶ if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV) { 0 0 0
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVAV) { 0 0 0
4416 0 Safefree(col_vals);
4417 0 return 0;
4418 }
4419 0 av = (AV *)SvRV(values[i]);
4420 0 ep = av_fetch(av, j, 0);
4421 0 ▶ elem = (ep && *ep) ? *ep : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
elem = (ep && *ep) ? *ep : &PL_sv_undef; 0 0 0
elem = (ep && *ep) ? *ep : &PL_sv_undef; 0 0 0
4422 0 ▶ if (SvRMAGICAL((SV *)av) || SvGMAGICAL(elem))
Branch: 2 branches
BranchTrueFalseTotal
if (SvRMAGICAL((SV *)av) || SvGMAGICAL(elem)) 0 0 0
if (SvRMAGICAL((SV *)av) || SvGMAGICAL(elem)) 0 0 0
4423 0 elem = sv_2mortal(newSVsv(elem));
4424 0 col_vals[i] = elem;
4425 }
4426 0 rc = encode_column_sv(aTHX_ b, col_vals, nrows, ct->inners[j]);
4427 0 Safefree(col_vals);
4428 0 ▶ if (!rc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return 0; 0 0 0
4429 }
4430 0 return 1;
4431 }
4432 0 case CT_MAP: {
4433 /* Each value must be a hashref. Wire format: offsets + key column + value column */
4434 0 uint64_t total = 0;
4435 0 uint64_t cap = 0;
4436 uint64_t *offsets;
4437 SV **all_keys, **all_vals;
4438 0 uint64_t pos = 0;
4439 int rc;
4440  
4441 0 ▶ if (ct->num_inners != 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners != 2) return 0; 0 0 0
4442  
4443 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4444 HV *hv;
4445 0 ▶ if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVHV) 0 0 0
if (!SvROK(values[i]) || SvTYPE(SvRV(values[i])) != SVt_PVHV) 0 0 0
4446 0 return 0;
4447 0 hv = (HV *)SvRV(values[i]);
4448 /* sizing hint only: HvUSEDKEYS is meaningless for tied
4449 * hashes, so the fill loop grows below as needed. */
4450 0 ▶ total += HvUSEDKEYS(hv);
Branch: 1 branch
BranchTrueFalseTotal
total += HvUSEDKEYS(hv); 0 0 0
4451 }
4452  
4453 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
4454 0 ▶ cap = total ? total : 1;
Branch: 1 branch
BranchTrueFalseTotal
cap = total ? total : 1; 0 0 0
4455 0 ▶ Newx(all_keys, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(all_keys, cap, SV *); 0 0 0
4456 0 ▶ Newx(all_vals, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(all_vals, cap, SV *); 0 0 0
4457  
4458 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4459 0 HV *hv = (HV *)SvRV(values[i]);
4460 HE *he;
4461 /* hv_iterval on a tie returns a lazy mortal FETCH proxy:
4462 * copy the fetched value (borrowing the proxy would
4463 * re-FETCH on every later read, and its fake-HE key
4464 * is not safe to borrow either). */
4465 0 int hv_magical = SvRMAGICAL((SV *)hv);
4466 0 hv_iterinit(hv);
4467 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
4468 I32 klen;
4469 SV *vsv;
4470 0 char *key = hv_iterkey(he, &klen);
4471 0 ▶ if (pos >= cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= cap) { 0 0 0
4472 0 ▶ if (cap > (uint64_t)SIZE_MAX / (2 * sizeof(SV *)))
Branch: 1 branch
BranchTrueFalseTotal
if (cap > (uint64_t)SIZE_MAX / (2 * sizeof(SV *))) 0 0 0
4473 0 goto map_oom;
4474 0 cap *= 2;
4475 0 ▶ Renew(all_keys, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(all_keys, cap, SV *); 0 0 0
4476 0 ▶ Renew(all_vals, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(all_vals, cap, SV *); 0 0 0
4477 }
4478 0 vsv = hv_iterval(hv, he);
4479 0 ▶ if (hv_magical) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_magical) { 0 0 0
4480 0 all_keys[pos] = sv_2mortal(newSVpvn(key, klen));
4481 0 all_vals[pos] = sv_2mortal(newSVsv(vsv));
4482 } else {
4483 0 all_keys[pos] = hv_iterkeysv(he);
4484 0 all_vals[pos] = SvGMAGICAL(vsv)
4485 0 ▶ ? sv_2mortal(newSVsv(vsv)) : vsv;
Branch: 1 branch
BranchTrueFalseTotal
? sv_2mortal(newSVsv(vsv)) : vsv; 0 0 0
4486 }
4487 0 pos++;
4488 }
4489 0 offsets[i] = pos;
4490 }
4491 0 total = pos;
4492  
4493 0 nbuf_append(b, (const char *)offsets, nrows * 8);
4494 0 rc = encode_column_sv(aTHX_ b, all_keys, total, ct->inners[0]);
4495 0 ▶ if (rc) rc = encode_column_sv(aTHX_ b, all_vals, total, ct->inners[1]);
Branch: 1 branch
BranchTrueFalseTotal
if (rc) rc = encode_column_sv(aTHX_ b, all_vals, total, ct->inners[1]); 0 0 0
4496 0 Safefree(offsets);
4497 0 Safefree(all_keys);
4498 0 Safefree(all_vals);
4499 0 return rc;
4500 0 map_oom:
4501 0 Safefree(offsets);
4502 0 Safefree(all_keys);
4503 0 Safefree(all_vals);
4504 0 return 0;
4505 }
4506 0 case CT_JSON: {
4507 /* Wire layout (V1) per Clickhouse-Encoder/doc/json-research:
4508 * Object prefix: UInt64=0, varint K (twice), K * lenstr path
4509 * Per-path Dynamic prefix: UInt64=1, varint T (twice),
4510 * T * lenstr type-name, UInt64=0
4511 * Per-path data: N disc bytes, then per-variant data in lex
4512 * order including a SharedVariant slot
4513 * Shared data trailer: N * UInt64 LE zero (no shared paths)
4514 */
4515 0 ENTER; SAVETMPS;
4516 int p;
4517 0 HV **row_hvs = NULL;
4518 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
4519 0 ▶ Newxz(row_hvs, nrows, HV*);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(row_hvs, nrows, HV*); 0 0 0
4520 0 SAVEFREEPV(row_hvs);
4521 }
4522 0 HV *all_paths = (HV*)sv_2mortal((SV*)newHV());
4523  
4524 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4525 0 SV *val = values[i];
4526 0 ▶ if (!SvOK(val)) { row_hvs[i] = NULL; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { row_hvs[i] = NULL; continue; } 0 0 0
4527 0 ▶ if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) { 0 0 0
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) { 0 0 0
4528 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4529 0 return 0;
4530 }
4531 0 HV *flat = (HV*)sv_2mortal((SV*)newHV());
4532 0 ▶ if (!flatten_json_hash(aTHX_ (HV*)SvRV(val), NULL, 0, flat)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!flatten_json_hash(aTHX_ (HV*)SvRV(val), NULL, 0, flat)) { 0 0 0
4533 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4534 0 return 0;
4535 }
4536 0 row_hvs[i] = flat;
4537 }
4538  
4539 /* Typed paths: extract per-row values from each flat HV (and
4540 * hv_delete to keep them out of the dynamic-paths discovery).
4541 * Missing keys substitute undef as a placeholder; the inner
4542 * encoder treats undef as the type's zero/default. */
4543 0 ▶ int n_typed = (ct->code == CT_JSON) ? ct->num_inners : 0;
Branch: 1 branch
BranchTrueFalseTotal
int n_typed = (ct->code == CT_JSON) ? ct->num_inners : 0; 0 0 0
4544 0 SV ***typed_vals = NULL;
4545 0 ▶ if (n_typed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_typed > 0) { 0 0 0
4546 0 Newxz(typed_vals, n_typed, SV **);
4547 0 SAVEFREEPV(typed_vals);
4548 int tp;
4549 0 ▶ for (tp = 0; tp < n_typed; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) { 0 0 0
4550 0 ▶ Newxz(typed_vals[tp], nrows > 0 ? nrows : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newxz(typed_vals[tp], nrows > 0 ? nrows : 1, SV *); 0 0 0
Newxz(typed_vals[tp], nrows > 0 ? nrows : 1, SV *); 0 0 0
Newxz(typed_vals[tp], nrows > 0 ? nrows : 1, SV *); 0 0 0
4551 0 SAVEFREEPV(typed_vals[tp]);
4552 0 size_t nlen = strlen(ct->inner_names[tp]);
4553 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4554 0 ▶ if (!row_hvs[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) { 0 0 0
4555 0 typed_vals[tp][i] = &PL_sv_undef;
4556 0 continue;
4557 }
4558 0 SV **e = hv_fetch(row_hvs[i], ct->inner_names[tp],
4559 (I32)nlen, 0);
4560 0 ▶ if (e && SvOK(*e)) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) { 0 0 0
if (e && SvOK(*e)) { 0 0 0
4561 0 typed_vals[tp][i] = SvREFCNT_inc(*e);
4562 0 sv_2mortal(typed_vals[tp][i]);
4563 0 hv_delete(row_hvs[i], ct->inner_names[tp],
4564 (I32)nlen, G_DISCARD);
4565 } else {
4566 0 typed_vals[tp][i] = &PL_sv_undef;
4567 }
4568 }
4569 }
4570 }
4571  
4572 /* Now collect dynamic-path keys (typed paths already removed). */
4573 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4574 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4575 0 hv_iterinit(row_hvs[i]);
4576 HE *he;
4577 0 ▶ while ((he = hv_iternext(row_hvs[i]))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(row_hvs[i]))) { 0 0 0
4578 I32 klen;
4579 0 char *kstr = hv_iterkey(he, &klen);
4580 0 (void)hv_store(all_paths, kstr, klen, newSViv(1), 0);
4581 }
4582 }
4583  
4584 0 ▶ int num_paths = (int)HvUSEDKEYS(all_paths);
Branch: 1 branch
BranchTrueFalseTotal
int num_paths = (int)HvUSEDKEYS(all_paths); 0 0 0
4585 0 json_path_entry_t *pe = NULL;
4586 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
4587 0 Newx(pe, num_paths, json_path_entry_t);
4588 0 SAVEFREEPV(pe);
4589 0 p = 0;
4590 0 hv_iterinit(all_paths);
4591 HE *he;
4592 0 ▶ while ((he = hv_iternext(all_paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(all_paths))) { 0 0 0
4593 I32 klen;
4594 0 char *kstr = hv_iterkey(he, &klen);
4595 0 pe[p].path = kstr;
4596 0 pe[p].len = (STRLEN)klen;
4597 0 p++;
4598 }
4599 0 ▶ if (num_paths > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 1) 0 0 0
4600 0 qsort(pe, num_paths, sizeof(*pe), json_cmp_path_entry);
4601 }
4602  
4603 0 unsigned *kind_masks = NULL;
4604 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
4605 0 Newxz(kind_masks, num_paths, unsigned);
4606 0 SAVEFREEPV(kind_masks);
4607 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
4608 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4609 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4610 0 SV **e = hv_fetch(row_hvs[i], pe[p].path, (I32)pe[p].len, 0);
4611 0 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 0 0 0
if (!e || !SvOK(*e)) continue; 0 0 0
4612 0 int k = json_classify_value(aTHX_ *e);
4613 0 ▶ if (k < 0) { FREETMPS; LEAVE; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (k < 0) { FREETMPS; LEAVE; return 0; } 0 0 0
if (k < 0) { FREETMPS; LEAVE; return 0; } 0 0 0
4614 0 kind_masks[p] |= 1u << k;
4615 }
4616 }
4617 }
4618  
4619 /* Object structure prefix */
4620 0 nbuf_le64(b, 0); /* Object V1 */
4621 0 nbuf_varuint(b, (uint64_t)num_paths);
4622 0 nbuf_varuint(b, (uint64_t)num_paths);
4623 0 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 0 0 0
4624 0 nbuf_string(b, pe[p].path, pe[p].len);
4625 0 ▶ for (p = 0; p < n_typed; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < n_typed; p++) 0 0 0
4626 0 write_col_prefix(b, ct->inners[p]);
4627  
4628 0 int *path_slots = NULL;
4629 0 int *wire_slot_counts = NULL;
4630 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
4631 0 Newx(path_slots, num_paths * JSON_LEX_SLOTS, int);
4632 0 SAVEFREEPV(path_slots);
4633 0 Newx(wire_slot_counts, num_paths, int);
4634 0 SAVEFREEPV(wire_slot_counts);
4635 0 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 0 0 0
4636 0 wire_slot_counts[p] = json_build_lex_table(
4637 0 kind_masks[p], path_slots + p*JSON_LEX_SLOTS);
4638 }
4639  
4640 /* Per-path Dynamic V1 prefix + Variant mode */
4641 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
4642 0 int wire_slots = wire_slot_counts[p];
4643 0 int kc = wire_slots - 1;
4644 0 nbuf_le64(b, 1); /* Dynamic V1 */
4645 0 nbuf_varuint(b, (uint64_t)kc);
4646 0 nbuf_varuint(b, (uint64_t)kc);
4647 0 int *slots = path_slots + p*JSON_LEX_SLOTS;
4648 int s;
4649 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
4650 0 int k = slots[s];
4651 0 ▶ if (k < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) continue; 0 0 0
4652 0 const char *nm = json_kind_type_name[k];
4653 0 nbuf_string(b, nm, strlen(nm));
4654 }
4655 0 nbuf_le64(b, 0); /* Variant BASIC */
4656 }
4657  
4658 /* Typed-path column data — comes BEFORE dynamic-path Variant data
4659 * on the wire. Emitted in declaration (sorted) order via the
4660 * inner type's encoder. */
4661 0 ▶ if (n_typed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_typed > 0) { 0 0 0
4662 int tp;
4663 0 ▶ for (tp = 0; tp < n_typed; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) { 0 0 0
4664 0 int rc = encode_column_sv(aTHX_ b, typed_vals[tp],
4665 0 nrows, ct->inners[tp]);
4666 0 ▶ if (rc != 1) { FREETMPS; LEAVE; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc != 1) { FREETMPS; LEAVE; return 0; } 0 0 0
if (rc != 1) { FREETMPS; LEAVE; return 0; } 0 0 0
4667 }
4668 }
4669  
4670 /* Per-path Variant data */
4671 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
4672 0 int wire_slots = wire_slot_counts[p];
4673 0 int *slots = path_slots + p*JSON_LEX_SLOTS;
4674  
4675 /* Discriminator byte per row */
4676 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4677 0 ▶ if (!row_hvs[i]) { nbuf_u8(b, 0xff); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) { nbuf_u8(b, 0xff); continue; } 0 0 0
4678 0 SV **e = hv_fetch(row_hvs[i], pe[p].path, (I32)pe[p].len, 0);
4679 0 ▶ if (!e || !SvOK(*e)) { nbuf_u8(b, 0xff); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) { nbuf_u8(b, 0xff); continue; } 0 0 0
if (!e || !SvOK(*e)) { nbuf_u8(b, 0xff); continue; } 0 0 0
4680 0 int k = json_classify_value(aTHX_ *e);
4681 0 nbuf_u8(b, (uint8_t)json_kind_disc_in(k, slots, wire_slots));
4682 }
4683  
4684 /* Per-variant data in lex order. SharedVariant has zero rows. */
4685 int s;
4686 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
4687 0 int k_match = slots[s];
4688 0 ▶ if (k_match < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k_match < 0) continue; 0 0 0
4689 0 int is_array = (k_match >= JV_ARRAY_BOOL
4690 0 ▶ && k_match <= JV_ARRAY_UINT64);
Branch: 2 branches
BranchTrueFalseTotal
&& k_match <= JV_ARRAY_UINT64); 0 0 0
&& k_match <= JV_ARRAY_UINT64); 0 0 0
4691 0 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 0 0 0
4692 0 uint64_t offset = 0;
4693 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4694 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4695 0 SV **e = hv_fetch(row_hvs[i], pe[p].path,
4696 (I32)pe[p].len, 0);
4697 0 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 0 0 0
if (!e || !SvOK(*e)) continue; 0 0 0
4698 0 ▶ if (json_classify_value(aTHX_ *e) != k_match) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (json_classify_value(aTHX_ *e) != k_match) continue; 0 0 0
4699 0 AV *av = (AV*)SvRV(*e);
4700 0 SSize_t n = av_len(av) + 1;
4701 0 offset += (uint64_t)n;
4702 0 nbuf_le64(b, offset);
4703 }
4704 }
4705 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4706 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4707 0 SV **e = hv_fetch(row_hvs[i], pe[p].path,
4708 (I32)pe[p].len, 0);
4709 0 ▶ if (!e || !SvOK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) continue; 0 0 0
if (!e || !SvOK(*e)) continue; 0 0 0
4710 0 ▶ if (json_classify_value(aTHX_ *e) != k_match) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (json_classify_value(aTHX_ *e) != k_match) continue; 0 0 0
4711 0 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 0 0 0
4712 0 AV *av = (AV*)SvRV(*e);
4713 0 SSize_t n = av_len(av) + 1, j;
4714 0 int av_magical = SvRMAGICAL((SV*)av);
4715 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
4716 0 SV **elem = av_fetch(av, j, 0);
4717 0 ▶ SV *ev = (elem && *elem)
Branch: 1 branch
BranchTrueFalseTotal
SV *ev = (elem && *elem) 0 0 0
4718 0 ▶ ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
? *elem : &PL_sv_undef; 0 0 0
4719 0 ▶ if (av_magical || SvGMAGICAL(ev))
Branch: 2 branches
BranchTrueFalseTotal
if (av_magical || SvGMAGICAL(ev)) 0 0 0
if (av_magical || SvGMAGICAL(ev)) 0 0 0
4720 0 ev = sv_2mortal(newSVsv(ev));
4721 0 ▶ if (!SvOK(ev)) ev = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ev)) ev = &PL_sv_undef; 0 0 0
4722 0 json_emit_array_elem(aTHX_ b, ev, k_match);
4723 }
4724 } else {
4725 0 json_emit_scalar(aTHX_ b, *e, k_match);
4726 }
4727 }
4728 }
4729 }
4730  
4731 /* Shared data trailer: N UInt64 LE zero offsets */
4732 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
4733 0 size_t nbytes = (size_t)nrows * 8;
4734 0 nbuf_grow(b, nbytes);
4735 0 memset(b->data + b->len, 0, nbytes);
4736 0 b->len += nbytes;
4737 }
4738 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4739 0 return 1;
4740 }
4741 0 default:
4742 0 return 0;
4743 }
4744 }
4745  
4746 /*
4747 * Wrap a filled Data block body into a CLIENT_DATA packet with optional LZ4 + empty trailing block.
4748 * Consumes body->data (frees it). Returns malloc'd packet, or NULL on failure.
4749 */
4750 0 static char* wrap_data_block(ev_clickhouse_t *self, native_buf_t *body, size_t *out_len) {
4751 native_buf_t pkt;
4752  
4753 0 nbuf_init(&pkt);
4754 0 nbuf_varuint(&pkt, CLIENT_DATA);
4755 0 nbuf_cstring(&pkt, ""); /* table name — outside compression */
4756  
4757 #ifdef HAVE_LZ4
4758 if (self->compress) {
4759 char *compressed;
4760 size_t comp_len;
4761 compressed = ch_lz4_compress(body->data, body->len, &comp_len);
4762 Safefree(body->data);
4763 body->data = NULL;
4764 if (compressed) {
4765 nbuf_append(&pkt, compressed, comp_len);
4766 Safefree(compressed);
4767 } else {
4768 Safefree(pkt.data);
4769 *out_len = 0;
4770 return NULL;
4771 }
4772 } else
4773 #endif
4774 {
4775 0 nbuf_append(&pkt, body->data, body->len);
4776 0 Safefree(body->data);
4777 0 body->data = NULL;
4778 }
4779  
4780 0 nbuf_empty_data_block(&pkt, self->compress);
4781  
4782 0 *out_len = pkt.len;
4783 0 return pkt.data;
4784 }
4785  
4786 /* --- Client-side defaults for empty values (empty_as_default/defaults) ---
4787 *
4788 * The server sends a TableColumns packet before the INSERT sample block;
4789 * its description text carries each table column's DEFAULT/MATERIALIZED/
4790 * ALIAS/EPHEMERAL kind and expression. Resolution runs once per insert at
4791 * sample-block time and yields one col_default_t per column; the build
4792 * functions below then only substitute the cell before the existing
4793 * encoders run.
4794 */
4795  
4796 typedef struct {
4797 char *name; size_t name_len;
4798 char kind; /* 0 none, D/M/A/E */
4799 char *expr; size_t expr_len;
4800 } tc_col_t;
4801  
4802 struct tc_cols_s {
4803 tc_col_t *cols;
4804 int ncols;
4805 int bad; /* the description did not parse completely */
4806 };
4807  
4808 /* One column's resolved default for a pending insert. */
4809 typedef struct {
4810 int subst; /* substitute empty cells in this column */
4811 int nullable; /* column top is Nullable (or LC(Nullable)) */
4812 int to_null; /* substitution value is NULL */
4813 SV *subst_sv; /* arrayref-path value, owned */
4814 char *subst_text; /* string-path cell, owned */
4815 size_t subst_text_len;
4816 } col_default_t;
4817  
4818 /* ClickHouse string unescape shared by the wire and literal levels:
4819 * \n \t \r \b \f \v \a \0 \e \xHH, anything else \X to X. */
4820 0 static int hex_val(char c) {
4821 0 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 0 0 0
if (c >= '0' && c <= '9') return c - '0'; 0 0 0
4822 0 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 0 0
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 0 0
4823 0 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 0 0 0
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 0 0 0
4824 0 return -1;
4825 }
4826  
4827 0 static size_t ch_unescape(const char *src, size_t len, char *dst) {
4828 0 size_t i = 0, j = 0;
4829 int hi, lo;
4830 0 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 0 0 0
4831 0 ▶ if (src[i] == '\\' && i + 1 < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (src[i] == '\\' && i + 1 < len) { 0 0 0
if (src[i] == '\\' && i + 1 < len) { 0 0 0
4832 0 char c = src[i + 1];
4833 0 size_t adv = 2;
4834 0 switch (c) {
4835 0 case 'n': c = '\n'; break;
4836 0 case 't': c = '\t'; break;
4837 0 case 'r': c = '\r'; break;
4838 0 case 'b': c = '\b'; break;
4839 0 case 'f': c = '\f'; break;
4840 0 case 'v': c = '\v'; break;
4841 0 case 'a': c = '\a'; break;
4842 0 case '0': c = '\0'; break;
4843 0 case 'e': c = '\x1b'; break;
4844 0 case 'x':
4845 0 ▶ hi = (i + 2 < len) ? hex_val(src[i + 2]) : -1;
Branch: 1 branch
BranchTrueFalseTotal
hi = (i + 2 < len) ? hex_val(src[i + 2]) : -1; 0 0 0
4846 0 ▶ lo = (i + 3 < len) ? hex_val(src[i + 3]) : -1;
Branch: 1 branch
BranchTrueFalseTotal
lo = (i + 3 < len) ? hex_val(src[i + 3]) : -1; 0 0 0
4847 0 ▶ if (hi >= 0 && lo >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (hi >= 0 && lo >= 0) { 0 0 0
if (hi >= 0 && lo >= 0) { 0 0 0
4848 0 c = (char)((hi << 4) | lo);
4849 0 adv = 4;
4850 }
4851 0 break;
4852 0 default: break;
4853 }
4854 0 dst[j++] = c;
4855 0 i += adv;
4856 } else {
4857 0 dst[j++] = src[i++];
4858 }
4859 }
4860 0 return j;
4861 }
4862  
4863 179 static void free_insert_tc(ev_clickhouse_t *self) {
4864 179 ▶ if (self->insert_tc) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_tc) { 0 179 179
4865 int i;
4866 0 ▶ for (i = 0; i < self->insert_tc->ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < self->insert_tc->ncols; i++) { 0 0 0
4867 0 Safefree(self->insert_tc->cols[i].name);
4868 0 Safefree(self->insert_tc->cols[i].expr);
4869 }
4870 0 Safefree(self->insert_tc->cols);
4871 0 Safefree(self->insert_tc);
4872 0 self->insert_tc = NULL;
4873 }
4874 179 }
4875  
4876 179 static void free_insert_default_state(ev_clickhouse_t *self) {
4877 179 self->insert_empty_as_default = 0;
4878 179 ▶ CLEAR_SV(self->insert_defaults);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->insert_defaults); 0 179 179
4879 179 free_insert_tc(self);
4880 179 }
4881  
4882 24 static void free_col_defaults(col_default_t *cdefs, int ncols) {
4883 int i;
4884 24 ▶ if (!cdefs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cdefs) return; 24 0 24
4885 0 ▶ for (i = 0; i < ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ncols; i++) { 0 0 0
4886 0 ▶ if (cdefs[i].subst_sv) SvREFCNT_dec(cdefs[i].subst_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (cdefs[i].subst_sv) SvREFCNT_dec(cdefs[i].subst_sv); 0 0 0
4887 0 Safefree(cdefs[i].subst_text);
4888 }
4889 0 Safefree(cdefs);
4890 }
4891  
4892 /* Split one description line into tab fields; returns field count. */
4893 0 static int split_tabs(const char *line, size_t len,
4894 const char **f, size_t *fl, int maxf) {
4895 0 int n = 0;
4896 0 size_t start = 0, i;
4897 0 ▶ for (i = 0; i <= len && n < maxf; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i <= len && n < maxf; i++) { 0 0 0
for (i = 0; i <= len && n < maxf; i++) { 0 0 0
4898 0 ▶ if (i == len || line[i] == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (i == len || line[i] == '\t') { 0 0 0
if (i == len || line[i] == '\t') { 0 0 0
4899 0 f[n] = line + start;
4900 0 fl[n] = i - start;
4901 0 n++;
4902 0 start = i + 1;
4903 }
4904 }
4905 0 return n;
4906 }
4907  
4908 /* Parse the TableColumns description text: a header line, the count
4909 * line, then one line per column. Anything short of that marks it bad,
4910 * so no column silently falls back to its type's zero. */
4911 0 static tc_cols_t *parse_table_columns_desc(const char *s, size_t len) {
4912 tc_cols_t *tc;
4913 0 const char *p = s, *end = s + len, *nl, *q;
4914 const char *f[8];
4915 size_t fl[8];
4916 0 int nf, n = 0, i;
4917 0 Newxz(tc, 1, tc_cols_t);
4918 0 tc->bad = 1;
4919 0 nl = memchr(p, '\n', (size_t)(end - p));
4920 0 ▶ if (!nl) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return tc; 0 0 0
4921 0 p = nl + 1;
4922 0 nl = memchr(p, '\n', (size_t)(end - p));
4923 0 ▶ if (!nl) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return tc; 0 0 0
4924 0 ▶ for (q = p; q < nl && *q >= '0' && *q <= '9'; q++) {
Branch: 3 branches
BranchTrueFalseTotal
for (q = p; q < nl && *q >= '0' && *q <= '9'; q++) { 0 0 0
for (q = p; q < nl && *q >= '0' && *q <= '9'; q++) { 0 0 0
for (q = p; q < nl && *q >= '0' && *q <= '9'; q++) { 0 0 0
4925 0 ▶ if (n > 100000) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 100000) return tc; 0 0 0
4926 0 n = n * 10 + (*q - '0');
4927 }
4928 /* "N columns:" */
4929 0 ▶ if (q == p || (q < nl && *q != ' ') || n > 100000) return tc;
Branch: 4 branches
BranchTrueFalseTotal
if (q == p || (q < nl && *q != ' ') || n > 100000) return tc; 0 0 0
if (q == p || (q < nl && *q != ' ') || n > 100000) return tc; 0 0 0
if (q == p || (q < nl && *q != ' ') || n > 100000) return tc; 0 0 0
if (q == p || (q < nl && *q != ' ') || n > 100000) return tc; 0 0 0
4930 0 p = nl + 1;
4931 0 ▶ if (n > 0) Newxz(tc->cols, n, tc_col_t);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) Newxz(tc->cols, n, tc_col_t); 0 0 0
4932 0 tc->ncols = n;
4933 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
4934 const char *le;
4935 size_t j, k;
4936 char *un;
4937 0 ▶ if (p >= end) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return tc; 0 0 0
4938 0 nl = memchr(p, '\n', (size_t)(end - p));
4939 0 ▶ le = nl ? nl : end;
Branch: 1 branch
BranchTrueFalseTotal
le = nl ? nl : end; 0 0 0
4940 0 nf = split_tabs(p, (size_t)(le - p), f, fl, 8);
4941 0 ▶ if (nf < 1 || fl[0] < 3 || f[0][0] != '`') return tc;
Branch: 3 branches
BranchTrueFalseTotal
if (nf < 1 || fl[0] < 3 || f[0][0] != '`') return tc; 0 0 0
if (nf < 1 || fl[0] < 3 || f[0][0] != '`') return tc; 0 0 0
if (nf < 1 || fl[0] < 3 || f[0][0] != '`') return tc; 0 0 0
4942 0 j = 1;
4943 0 ▶ while (j < fl[0]) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < fl[0]) { 0 0 0
4944 0 ▶ if (f[0][j] == '\\' && j + 1 < fl[0]) { j += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (f[0][j] == '\\' && j + 1 < fl[0]) { j += 2; continue; } 0 0 0
if (f[0][j] == '\\' && j + 1 < fl[0]) { j += 2; continue; } 0 0 0
4945 0 ▶ if (f[0][j] == '`') break;
Branch: 1 branch
BranchTrueFalseTotal
if (f[0][j] == '`') break; 0 0 0
4946 0 j++;
4947 }
4948 0 ▶ if (j >= fl[0] || j + 1 >= fl[0] || f[0][j + 1] != ' ') return tc;
Branch: 3 branches
BranchTrueFalseTotal
if (j >= fl[0] || j + 1 >= fl[0] || f[0][j + 1] != ' ') return tc; 0 0 0
if (j >= fl[0] || j + 1 >= fl[0] || f[0][j + 1] != ' ') return tc; 0 0 0
if (j >= fl[0] || j + 1 >= fl[0] || f[0][j + 1] != ' ') return tc; 0 0 0
4949 0 Newx(un, j, char);
4950 0 k = ch_unescape(f[0] + 1, j - 1, un);
4951 0 tc->cols[i].name = un;
4952 0 tc->cols[i].name_len = k;
4953 0 ▶ if (nf > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nf > 1) { 0 0 0
4954 0 ▶ if (fl[1] == 7 && memcmp(f[1], "DEFAULT", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (fl[1] == 7 && memcmp(f[1], "DEFAULT", 7) == 0) 0 0 0
if (fl[1] == 7 && memcmp(f[1], "DEFAULT", 7) == 0) 0 0 0
4955 0 tc->cols[i].kind = 'D';
4956 0 ▶ else if (fl[1] == 12 && memcmp(f[1], "MATERIALIZED", 12) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (fl[1] == 12 && memcmp(f[1], "MATERIALIZED", 12) == 0) 0 0 0
else if (fl[1] == 12 && memcmp(f[1], "MATERIALIZED", 12) == 0) 0 0 0
4957 0 tc->cols[i].kind = 'M';
4958 0 ▶ else if (fl[1] == 5 && memcmp(f[1], "ALIAS", 5) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (fl[1] == 5 && memcmp(f[1], "ALIAS", 5) == 0) 0 0 0
else if (fl[1] == 5 && memcmp(f[1], "ALIAS", 5) == 0) 0 0 0
4959 0 tc->cols[i].kind = 'A';
4960 0 ▶ else if (fl[1] == 9 && memcmp(f[1], "EPHEMERAL", 9) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (fl[1] == 9 && memcmp(f[1], "EPHEMERAL", 9) == 0) 0 0 0
else if (fl[1] == 9 && memcmp(f[1], "EPHEMERAL", 9) == 0) 0 0 0
4961 0 tc->cols[i].kind = 'E';
4962 0 ▶ if (tc->cols[i].kind) {
Branch: 1 branch
BranchTrueFalseTotal
if (tc->cols[i].kind) { 0 0 0
4963 0 ▶ if (nf < 3) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (nf < 3) return tc; 0 0 0
4964 0 Newx(un, fl[2] + 1, char);
4965 0 k = ch_unescape(f[2], fl[2], un);
4966 0 tc->cols[i].expr = un;
4967 0 tc->cols[i].expr_len = k;
4968 }
4969 }
4970 0 p = le + 1;
4971 }
4972 0 tc->bad = 0;
4973 0 return tc;
4974 }
4975  
4976 0 static const tc_col_t *find_tc_col(const tc_cols_t *tc,
4977 const char *name, size_t len) {
4978 int i;
4979 0 ▶ if (!tc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!tc) return NULL; 0 0 0
4980 0 ▶ for (i = 0; i < tc->ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc->ncols; i++) { 0 0 0
4981 0 ▶ if (tc->cols[i].name && tc->cols[i].name_len == len
Branch: 2 branches
BranchTrueFalseTotal
if (tc->cols[i].name && tc->cols[i].name_len == len 0 0 0
if (tc->cols[i].name && tc->cols[i].name_len == len 0 0 0
4982 0 ▶ && memcmp(tc->cols[i].name, name, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(tc->cols[i].name, name, len) == 0) 0 0 0
4983 0 return &tc->cols[i];
4984 }
4985 0 return NULL;
4986 }
4987  
4988 /* Constant DEFAULT literals: number, string, NULL, true/false,
4989 * array/tuple of literals. Anything else is not a literal. */
4990 enum { L_NUM, L_STR, L_NULL, L_BOOL, L_ARR, L_TUP };
4991  
4992 typedef struct lit_s lit_t;
4993 struct lit_s {
4994 int kind;
4995 const char *ptr; size_t len;
4996 char *str; size_t str_len;
4997 int boolean;
4998 lit_t **items; size_t nitems; size_t nicap;
4999 };
5000  
5001 0 static void free_lit(lit_t *l) {
5002 0 ▶ if (!l) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return; 0 0 0
5003 0 ▶ if (l->kind == L_STR) Safefree(l->str);
Branch: 1 branch
BranchTrueFalseTotal
if (l->kind == L_STR) Safefree(l->str); 0 0 0
5004 0 ▶ if (l->items) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->items) { 0 0 0
5005 size_t i;
5006 0 ▶ for (i = 0; i < l->nitems; i++) free_lit(l->items[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->nitems; i++) free_lit(l->items[i]); 0 0 0
5007 0 Safefree(l->items);
5008 }
5009 0 Safefree(l);
5010 }
5011  
5012 0 static const char *skip_ws(const char *p, const char *end) {
5013 0 ▶ while (p < end && (*p == ' ' || *p == '\t' || *p == '\n'
Branch: 4 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' 0 0 0
while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' 0 0 0
while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' 0 0 0
while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' 0 0 0
5014 0 ▶ || *p == '\r' || *p == '\f' || *p == '\v'))
Branch: 3 branches
BranchTrueFalseTotal
|| *p == '\r' || *p == '\f' || *p == '\v')) 0 0 0
|| *p == '\r' || *p == '\f' || *p == '\v')) 0 0 0
|| *p == '\r' || *p == '\f' || *p == '\v')) 0 0 0
5015 0 p++;
5016 0 return p;
5017 }
5018  
5019 0 static int is_wordchar(char c) {
5020 0 ▶ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 3 branches
BranchTrueFalseTotal
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 0 0
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 0 0
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 0 0
5021 0 ▶ || (c >= '0' && c <= '9') || c == '_';
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '_'; 0 0 0
|| (c >= '0' && c <= '9') || c == '_'; 0 0 0
|| (c >= '0' && c <= '9') || c == '_'; 0 0 0
|| (c >= '0' && c <= '9') || c == '_'; 0 0 0
5022 }
5023  
5024 static lit_t *parse_lit(const char **pp, const char *end, int depth);
5025  
5026 0 static int lit_push(lit_t *l, lit_t *item) {
5027 0 ▶ if (l->nitems == l->nicap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->nitems == l->nicap) { 0 0 0
5028 0 ▶ size_t ncap = l->nicap ? l->nicap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
size_t ncap = l->nicap ? l->nicap * 2 : 4; 0 0 0
5029 0 ▶ if (ncap > 100000) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ncap > 100000) return 0; 0 0 0
5030 0 ▶ Renew(l->items, ncap, lit_t *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(l->items, ncap, lit_t *); 0 0 0
5031 0 l->nicap = ncap;
5032 }
5033 0 l->items[l->nitems++] = item;
5034 0 return 1;
5035 }
5036  
5037 0 static lit_t *parse_lit(const char **pp, const char *end, int depth) {
5038 0 const char *p = skip_ws(*pp, end);
5039 lit_t *l;
5040 0 ▶ if (p >= end || depth > 64) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || depth > 64) return NULL; 0 0 0
if (p >= end || depth > 64) return NULL; 0 0 0
5041 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5042 0 size_t cap = (size_t)(end - p), k = 0;
5043 char *buf;
5044 0 ▶ Newx(buf, cap ? cap : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(buf, cap ? cap : 1, char); 0 0 0
5045 0 p++;
5046 for (;;) {
5047 0 ▶ if (p >= end) { Safefree(buf); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) { Safefree(buf); return NULL; } 0 0 0
5048 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5049 0 ▶ if (p + 1 < end && p[1] == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (p + 1 < end && p[1] == '\'') { 0 0 0
if (p + 1 < end && p[1] == '\'') { 0 0 0
5050 0 buf[k++] = '\'';
5051 0 p += 2;
5052 0 continue;
5053 }
5054 0 p++;
5055 0 break;
5056 }
5057 0 ▶ if (*p == '\\' && p + 1 < end) {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\' && p + 1 < end) { 0 0 0
if (*p == '\\' && p + 1 < end) { 0 0 0
5058 0 char c = p[1];
5059 0 size_t adv = 2;
5060 int hi, lo;
5061 0 switch (c) {
5062 0 case 'n': c = '\n'; break;
5063 0 case 't': c = '\t'; break;
5064 0 case 'r': c = '\r'; break;
5065 0 case 'b': c = '\b'; break;
5066 0 case 'f': c = '\f'; break;
5067 0 case 'v': c = '\v'; break;
5068 0 case 'a': c = '\a'; break;
5069 0 case '0': c = '\0'; break;
5070 0 case 'e': c = '\x1b'; break;
5071 0 case 'x':
5072 0 ▶ hi = (p + 2 < end) ? hex_val(p[2]) : -1;
Branch: 1 branch
BranchTrueFalseTotal
hi = (p + 2 < end) ? hex_val(p[2]) : -1; 0 0 0
5073 0 ▶ lo = (p + 3 < end) ? hex_val(p[3]) : -1;
Branch: 1 branch
BranchTrueFalseTotal
lo = (p + 3 < end) ? hex_val(p[3]) : -1; 0 0 0
5074 0 ▶ if (hi >= 0 && lo >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (hi >= 0 && lo >= 0) { 0 0 0
if (hi >= 0 && lo >= 0) { 0 0 0
5075 0 c = (char)((hi << 4) | lo);
5076 0 adv = 4;
5077 }
5078 0 break;
5079 0 default: break;
5080 }
5081 0 buf[k++] = c;
5082 0 p += adv;
5083 0 continue;
5084 }
5085 0 buf[k++] = *p++;
5086 }
5087 0 Newxz(l, 1, lit_t);
5088 0 l->kind = L_STR;
5089 0 l->str = buf;
5090 0 l->str_len = k;
5091 0 *pp = p;
5092 0 return l;
5093 }
5094 0 ▶ if (*p == '[' || *p == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '[' || *p == '(') { 0 0 0
if (*p == '[' || *p == '(') { 0 0 0
5095 0 ▶ char open = *p, close = (open == '[') ? ']' : ')';
Branch: 1 branch
BranchTrueFalseTotal
char open = *p, close = (open == '[') ? ']' : ')'; 0 0 0
5096 0 int is_tup = (open == '(');
5097 0 lit_t *first = NULL;
5098 0 p++;
5099 0 Newxz(l, 1, lit_t);
5100 0 ▶ l->kind = is_tup ? L_TUP : L_ARR;
Branch: 1 branch
BranchTrueFalseTotal
l->kind = is_tup ? L_TUP : L_ARR; 0 0 0
5101 0 p = skip_ws(p, end);
5102 0 ▶ if (p < end && *p == close) {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == close) { 0 0 0
if (p < end && *p == close) { 0 0 0
5103 0 p++;
5104 0 *pp = p;
5105 0 return l;
5106 }
5107 0 first = parse_lit(&p, end, depth + 1);
5108 0 ▶ if (!first) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!first) { free_lit(l); return NULL; } 0 0 0
5109 0 p = skip_ws(p, end);
5110 0 ▶ if (is_tup && p < end && *p == ')') {
Branch: 3 branches
BranchTrueFalseTotal
if (is_tup && p < end && *p == ')') { 0 0 0
if (is_tup && p < end && *p == ')') { 0 0 0
if (is_tup && p < end && *p == ')') { 0 0 0
5111 0 p++;
5112 0 free_lit(l);
5113 0 *pp = p;
5114 0 return first;
5115 }
5116 0 ▶ if (p >= end || *p != ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != ',') { 0 0 0
if (p >= end || *p != ',') { 0 0 0
5117 0 free_lit(first);
5118 0 free_lit(l);
5119 0 return NULL;
5120 }
5121 0 ▶ if (!lit_push(l, first)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_push(l, first)) { 0 0 0
5122 0 free_lit(first);
5123 0 free_lit(l);
5124 0 return NULL;
5125 }
5126 0 for (;;) {
5127 lit_t *item;
5128 0 p++;
5129 0 item = parse_lit(&p, end, depth + 1);
5130 0 ▶ if (!item) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!item) { free_lit(l); return NULL; } 0 0 0
5131 0 ▶ if (!lit_push(l, item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_push(l, item)) { 0 0 0
5132 0 free_lit(item);
5133 0 free_lit(l);
5134 0 return NULL;
5135 }
5136 0 p = skip_ws(p, end);
5137 0 ▶ if (p < end && *p == ',') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == ',') continue; 0 0 0
if (p < end && *p == ',') continue; 0 0 0
5138 0 ▶ if (p < end && *p == close) { p++; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == close) { p++; break; } 0 0 0
if (p < end && *p == close) { p++; break; } 0 0 0
5139 0 free_lit(l);
5140 0 return NULL;
5141 }
5142 0 *pp = p;
5143 0 return l;
5144 }
5145 0 ▶ if ((*p >= '0' && *p <= '9') || *p == '.' || *p == '+' || *p == '-') {
Branch: 5 branches
BranchTrueFalseTotal
if ((*p >= '0' && *p <= '9') || *p == '.' || *p == '+' || *p == '-') { 0 0 0
if ((*p >= '0' && *p <= '9') || *p == '.' || *p == '+' || *p == '-') { 0 0 0
if ((*p >= '0' && *p <= '9') || *p == '.' || *p == '+' || *p == '-') { 0 0 0
if ((*p >= '0' && *p <= '9') || *p == '.' || *p == '+' || *p == '-') { 0 0 0
if ((*p >= '0' && *p <= '9') || *p == '.' || *p == '+' || *p == '-') { 0 0 0
5146 0 const char *start = p;
5147 0 int seendigit = 0;
5148 0 ▶ if (*p == '+' || *p == '-') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '+' || *p == '-') p++; 0 0 0
if (*p == '+' || *p == '-') p++; 0 0 0
5149 0 ▶ if (p + 1 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
Branch: 4 branches
BranchTrueFalseTotal
if (p + 1 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 0 0 0
if (p + 1 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 0 0 0
if (p + 1 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 0 0 0
if (p + 1 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 0 0 0
5150 0 p += 2;
5151 0 ▶ while (p < end && hex_val(*p) >= 0) { p++; seendigit = 1; }
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && hex_val(*p) >= 0) { p++; seendigit = 1; } 0 0 0
while (p < end && hex_val(*p) >= 0) { p++; seendigit = 1; } 0 0 0
5152 0 ▶ if (!seendigit) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!seendigit) return NULL; 0 0 0
5153 } else {
5154 0 ▶ while (p < end && *p >= '0' && *p <= '9') { p++; seendigit = 1; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') { p++; seendigit = 1; } 0 0 0
while (p < end && *p >= '0' && *p <= '9') { p++; seendigit = 1; } 0 0 0
while (p < end && *p >= '0' && *p <= '9') { p++; seendigit = 1; } 0 0 0
5155 0 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 0 0 0
if (p < end && *p == '.') { 0 0 0
5156 0 p++;
5157 0 ▶ while (p < end && *p >= '0' && *p <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') { 0 0 0
while (p < end && *p >= '0' && *p <= '9') { 0 0 0
while (p < end && *p >= '0' && *p <= '9') { 0 0 0
5158 0 p++;
5159 0 seendigit = 1;
5160 }
5161 }
5162 0 ▶ if (p < end && (*p == 'e' || *p == 'E')) {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == 'e' || *p == 'E')) { 0 0 0
if (p < end && (*p == 'e' || *p == 'E')) { 0 0 0
if (p < end && (*p == 'e' || *p == 'E')) { 0 0 0
5163 0 const char *e = p + 1;
5164 0 ▶ if (e < end && (*e == '+' || *e == '-')) e++;
Branch: 3 branches
BranchTrueFalseTotal
if (e < end && (*e == '+' || *e == '-')) e++; 0 0 0
if (e < end && (*e == '+' || *e == '-')) e++; 0 0 0
if (e < end && (*e == '+' || *e == '-')) e++; 0 0 0
5165 0 ▶ if (e < end && *e >= '0' && *e <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
if (e < end && *e >= '0' && *e <= '9') { 0 0 0
if (e < end && *e >= '0' && *e <= '9') { 0 0 0
if (e < end && *e >= '0' && *e <= '9') { 0 0 0
5166 0 p = e + 1;
5167 0 ▶ while (p < end && *p >= '0' && *p <= '9') p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') p++; 0 0 0
while (p < end && *p >= '0' && *p <= '9') p++; 0 0 0
while (p < end && *p >= '0' && *p <= '9') p++; 0 0 0
5168 }
5169 }
5170 0 ▶ if (!seendigit) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!seendigit) return NULL; 0 0 0
5171 }
5172 0 Newxz(l, 1, lit_t);
5173 0 l->kind = L_NUM;
5174 0 l->ptr = start;
5175 0 l->len = (size_t)(p - start);
5176 0 *pp = p;
5177 0 return l;
5178 }
5179 0 ▶ if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '_') {
Branch: 5 branches
BranchTrueFalseTotal
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '_') { 0 0 0
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '_') { 0 0 0
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '_') { 0 0 0
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '_') { 0 0 0
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '_') { 0 0 0
5180 0 const char *w = p;
5181 size_t wl;
5182 0 ▶ while (p < end && is_wordchar(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && is_wordchar(*p)) p++; 0 0 0
while (p < end && is_wordchar(*p)) p++; 0 0 0
5183 0 wl = (size_t)(p - w);
5184 0 Newxz(l, 1, lit_t);
5185 0 ▶ if ((wl == 4 && strncasecmp(w, "NULL", 4) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((wl == 4 && strncasecmp(w, "NULL", 4) == 0)) { 0 0 0
if ((wl == 4 && strncasecmp(w, "NULL", 4) == 0)) { 0 0 0
5186 0 l->kind = L_NULL;
5187 0 ▶ } else if (wl == 4 && strncasecmp(w, "TRUE", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (wl == 4 && strncasecmp(w, "TRUE", 4) == 0) { 0 0 0
} else if (wl == 4 && strncasecmp(w, "TRUE", 4) == 0) { 0 0 0
5188 0 l->kind = L_BOOL;
5189 0 l->boolean = 1;
5190 0 ▶ } else if (wl == 5 && strncasecmp(w, "FALSE", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (wl == 5 && strncasecmp(w, "FALSE", 5) == 0) { 0 0 0
} else if (wl == 5 && strncasecmp(w, "FALSE", 5) == 0) { 0 0 0
5191 0 l->kind = L_BOOL;
5192 0 l->boolean = 0;
5193 } else {
5194 0 Safefree(l);
5195 0 return NULL;
5196 }
5197 0 *pp = p;
5198 0 return l;
5199 }
5200 0 return NULL;
5201 }
5202  
5203 /* A whole expression is a literal only if one value spans it. */
5204 0 static lit_t *parse_default_literal(const char *s, size_t len) {
5205 0 const char *p = s, *end = s + len;
5206 0 lit_t *l = parse_lit(&p, end, 0);
5207 0 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 0 0
5208 0 p = skip_ws(p, end);
5209 0 ▶ if (p != end) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) { free_lit(l); return NULL; } 0 0 0
5210 0 return l;
5211 }
5212  
5213 /* Verbatim number text, hex normalized to decimal; NULL when the hex
5214 * value does not fit 64 bits. Caller frees. */
5215 0 static char *num_normalize(const char *p, size_t len, size_t *out_len) {
5216 0 const char *q = p;
5217 0 size_t qlen = len;
5218 char *out;
5219 0 int neg = 0;
5220 0 ▶ if (qlen > 0 && (*q == '+' || *q == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (qlen > 0 && (*q == '+' || *q == '-')) { 0 0 0
if (qlen > 0 && (*q == '+' || *q == '-')) { 0 0 0
if (qlen > 0 && (*q == '+' || *q == '-')) { 0 0 0
5221 0 neg = (*q == '-');
5222 0 q++;
5223 0 qlen--;
5224 }
5225 0 ▶ if (qlen > 2 && q[0] == '0' && (q[1] == 'x' || q[1] == 'X')) {
Branch: 4 branches
BranchTrueFalseTotal
if (qlen > 2 && q[0] == '0' && (q[1] == 'x' || q[1] == 'X')) { 0 0 0
if (qlen > 2 && q[0] == '0' && (q[1] == 'x' || q[1] == 'X')) { 0 0 0
if (qlen > 2 && q[0] == '0' && (q[1] == 'x' || q[1] == 'X')) { 0 0 0
if (qlen > 2 && q[0] == '0' && (q[1] == 'x' || q[1] == 'X')) { 0 0 0
5226 0 unsigned long long v = 0;
5227 size_t i;
5228 0 ▶ for (i = 2; i < qlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < qlen; i++) { 0 0 0
5229 0 int d = hex_val(q[i]);
5230 0 ▶ if (d < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (d < 0) break; 0 0 0
5231 0 ▶ if (v > (ULLONG_MAX >> 4)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (ULLONG_MAX >> 4)) return NULL; 0 0 0
5232 0 v = v * 16 + (unsigned)d;
5233 }
5234 0 Newx(out, 32, char);
5235 0 ▶ if (neg)
Branch: 1 branch
BranchTrueFalseTotal
if (neg) 0 0 0
5236 0 *out_len = (size_t)snprintf(out, 32, "-%llu", v);
5237 else
5238 0 *out_len = (size_t)snprintf(out, 32, "%llu", v);
5239 0 return out;
5240 }
5241 0 Newx(out, len + 1, char);
5242 0 Copy(p, out, len, char);
5243 0 out[len] = '\0';
5244 0 *out_len = len;
5245 0 return out;
5246 }
5247  
5248 0 static int nullable_top(col_type_t *ct) {
5249 0 ▶ if (!ct) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) return 0; 0 0 0
5250 0 ▶ if (ct->code == CT_NULLABLE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_NULLABLE) return 1; 0 0 0
5251 0 ▶ if (ct->code == CT_LOWCARDINALITY && ct->inner
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_LOWCARDINALITY && ct->inner 0 0 0
if (ct->code == CT_LOWCARDINALITY && ct->inner 0 0 0
5252 0 ▶ && ct->inner->code == CT_NULLABLE)
Branch: 1 branch
BranchTrueFalseTotal
&& ct->inner->code == CT_NULLABLE) 0 0 0
5253 0 return 1;
5254 0 return 0;
5255 }
5256  
5257 /* Arrayref-path conversion of a literal. Returns an owned SV, or NULL
5258 * for exotic nests the client cannot express. */
5259 0 static SV *lit_to_sv(pTHX_ lit_t *l) {
5260 size_t i;
5261 AV *av;
5262 SV *sv;
5263 char *norm;
5264 size_t nlen;
5265 0 switch (l->kind) {
5266 0 case L_NUM:
5267 0 norm = num_normalize(l->ptr, l->len, &nlen);
5268 0 ▶ if (!norm) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!norm) return NULL; 0 0 0
5269 0 sv = newSVpvn(norm, nlen);
5270 0 Safefree(norm);
5271 0 return sv;
5272 0 case L_STR:
5273 0 return newSVpvn(l->str, l->str_len);
5274 0 case L_NULL:
5275 0 return newSV(0);
5276 0 case L_BOOL:
5277 0 return newSViv(l->boolean);
5278 0 case L_ARR:
5279 case L_TUP:
5280 0 av = newAV();
5281 0 ▶ for (i = 0; i < l->nitems; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->nitems; i++) { 0 0 0
5282 0 SV *e = lit_to_sv(aTHX_ l->items[i]);
5283 0 ▶ if (!e) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { 0 0 0
5284 0 SvREFCNT_dec((SV *)av);
5285 0 return NULL;
5286 }
5287 0 av_store(av, (SSize_t)i, e);
5288 }
5289 0 return newRV_noinc((SV *)av);
5290 }
5291 0 return NULL;
5292 }
5293  
5294 /* String-path conversion: appends the TSV cell. 0 when the value has
5295 * no text form (nested types need binary encoding). */
5296 0 static int lit_to_text(lit_t *l, native_buf_t *b) {
5297 char *norm;
5298 size_t nlen;
5299 0 switch (l->kind) {
5300 0 case L_NUM:
5301 0 norm = num_normalize(l->ptr, l->len, &nlen);
5302 0 ▶ if (!norm) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!norm) return 0; 0 0 0
5303 0 nbuf_append(b, norm, nlen);
5304 0 Safefree(norm);
5305 0 return 1;
5306 0 case L_STR:
5307 0 tsv_escape(b, l->str, l->str_len);
5308 0 return 1;
5309 0 case L_NULL:
5310 0 nbuf_append(b, "\\N", 2);
5311 0 return 1;
5312 0 case L_BOOL:
5313 0 ▶ nbuf_append(b, l->boolean ? "1" : "0", 1);
Branch: 1 branch
BranchTrueFalseTotal
nbuf_append(b, l->boolean ? "1" : "0", 1); 0 0 0
5314 0 return 1;
5315 0 default:
5316 0 return 0;
5317 }
5318 }
5319  
5320 /* Zero value for a column with no DEFAULT. Returns an owned SV, sets
5321 * *impossible when the type has no client-side zero. to_null handled
5322 * by the caller. */
5323 0 static SV *zero_sv_for(pTHX_ col_type_t *ct, int *impossible) {
5324 size_t i;
5325 AV *av;
5326 SV *sv;
5327 0 *impossible = 0;
5328 0 switch (ct->code) {
5329 0 case CT_ARRAY:
5330 0 return newRV_noinc((SV *)newAV());
5331 0 case CT_MAP:
5332 0 return newRV_noinc((SV *)newHV());
5333 0 case CT_TUPLE:
5334 0 av = newAV();
5335 0 ▶ for (i = 0; i < (size_t)ct->num_inners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (size_t)ct->num_inners; i++) { 0 0 0
5336 0 col_type_t *in = ct->inners[i];
5337 0 ▶ if (!in) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in) { 0 0 0
5338 0 SvREFCNT_dec((SV *)av);
5339 0 *impossible = 1;
5340 0 return NULL;
5341 }
5342 0 ▶ if (in->code == CT_NULLABLE
Branch: 1 branch
BranchTrueFalseTotal
if (in->code == CT_NULLABLE 0 0 0
5343 0 ▶ || (in->code == CT_LOWCARDINALITY && in->inner
Branch: 2 branches
BranchTrueFalseTotal
|| (in->code == CT_LOWCARDINALITY && in->inner 0 0 0
|| (in->code == CT_LOWCARDINALITY && in->inner 0 0 0
5344 0 ▶ && in->inner->code == CT_NULLABLE)) {
Branch: 1 branch
BranchTrueFalseTotal
&& in->inner->code == CT_NULLABLE)) { 0 0 0
5345 0 av_store(av, (SSize_t)i, newSV(0));
5346 0 continue;
5347 }
5348 0 sv = zero_sv_for(aTHX_ in, impossible);
5349 0 ▶ if (!sv || *impossible) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || *impossible) { 0 0 0
if (!sv || *impossible) { 0 0 0
5350 0 ▶ if (sv) SvREFCNT_dec(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (sv) SvREFCNT_dec(sv); 0 0 0
5351 0 SvREFCNT_dec((SV *)av);
5352 0 *impossible = 1;
5353 0 return NULL;
5354 }
5355 0 av_store(av, (SSize_t)i, sv);
5356 }
5357 0 return newRV_noinc((SV *)av);
5358 0 case CT_LOWCARDINALITY:
5359 0 ▶ if (!ct->inner) { *impossible = 1; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ct->inner) { *impossible = 1; return NULL; } 0 0 0
5360 0 return zero_sv_for(aTHX_ ct->inner, impossible);
5361 0 case CT_JSON:
5362 0 return newRV_noinc((SV *)newHV());
5363 0 default:
5364 0 ▶ if (ct->code == CT_UNKNOWN || ct->code == CT_NOTHING
Branch: 2 branches
BranchTrueFalseTotal
if (ct->code == CT_UNKNOWN || ct->code == CT_NOTHING 0 0 0
if (ct->code == CT_UNKNOWN || ct->code == CT_NOTHING 0 0 0
5365 0 ▶ || ct->code == CT_VARIANT || ct->code == CT_DYNAMIC
Branch: 2 branches
BranchTrueFalseTotal
|| ct->code == CT_VARIANT || ct->code == CT_DYNAMIC 0 0 0
|| ct->code == CT_VARIANT || ct->code == CT_DYNAMIC 0 0 0
5366 0 ▶ || ct->code == CT_INT128 || ct->code == CT_UINT128
Branch: 2 branches
BranchTrueFalseTotal
|| ct->code == CT_INT128 || ct->code == CT_UINT128 0 0 0
|| ct->code == CT_INT128 || ct->code == CT_UINT128 0 0 0
5367 0 ▶ || ct->code == CT_INT256 || ct->code == CT_UINT256) {
Branch: 2 branches
BranchTrueFalseTotal
|| ct->code == CT_INT256 || ct->code == CT_UINT256) { 0 0 0
|| ct->code == CT_INT256 || ct->code == CT_UINT256) { 0 0 0
5368 0 *impossible = 1;
5369 0 return NULL;
5370 }
5371 0 return nullable_inner_zero_sv(aTHX_ ct);
5372 }
5373 }
5374  
5375 /* Zero value as a string-path cell. 1 with *out malloc'd, 0 when the
5376 * type has no text form. to_null handled by the caller. */
5377 0 static int zero_text_for(col_type_t *ct, char **out, size_t *out_len) {
5378 const char *p;
5379 size_t len;
5380 0 *out = NULL;
5381 0 *out_len = 0;
5382 0 switch (ct->code) {
5383 0 case CT_ARRAY:
5384 case CT_MAP:
5385 case CT_TUPLE:
5386 0 return 0;
5387 0 case CT_LOWCARDINALITY:
5388 0 ▶ if (!ct->inner) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct->inner) return 0; 0 0 0
5389 0 return zero_text_for(ct->inner, out, out_len);
5390 0 case CT_JSON:
5391 case CT_UNKNOWN:
5392 case CT_NOTHING:
5393 case CT_VARIANT:
5394 case CT_DYNAMIC:
5395 case CT_INT128:
5396 case CT_UINT128:
5397 case CT_INT256:
5398 case CT_UINT256:
5399 0 return 0;
5400 0 default:
5401 0 nullable_inner_zero(ct, &p, &len);
5402 0 Newx(*out, len + 1, char);
5403 0 Copy(p, *out, len, char);
5404 0 (*out)[len] = '\0';
5405 0 *out_len = len;
5406 0 return 1;
5407 }
5408 }
5409  
5410 /* Resolve one column's default from a caller value. */
5411 0 static void resolve_from_sv(col_default_t *cd, SV *v) {
5412 0 ▶ if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { 0 0 0
5413 0 cd->to_null = 1;
5414 0 return;
5415 }
5416 0 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 0 0 0
5417 0 cd->subst_sv = SvREFCNT_inc(v);
5418 0 return;
5419 }
5420 0 cd->subst_sv = SvREFCNT_inc(v);
5421 {
5422 STRLEN l;
5423 0 const char *s = SvPV(cd->subst_sv, l);
5424 native_buf_t b;
5425 0 nbuf_init(&b);
5426 0 tsv_escape(&b, s, l);
5427 0 cd->subst_text = b.data;
5428 0 cd->subst_text_len = b.len;
5429 }
5430 }
5431  
5432 /* Resolve one column from a DEFAULT literal. 1 ok, 0 failure. */
5433 0 static int resolve_from_lit(pTHX_ col_default_t *cd, lit_t *l) {
5434 native_buf_t b;
5435 0 ▶ if (l->kind == L_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->kind == L_NULL) { 0 0 0
5436 0 cd->to_null = 1;
5437 0 return 1;
5438 }
5439 0 cd->subst_sv = lit_to_sv(aTHX_ l);
5440 0 ▶ if (!cd->subst_sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!cd->subst_sv) return 0; 0 0 0
5441 0 nbuf_init(&b);
5442 0 ▶ if (lit_to_text(l, &b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lit_to_text(l, &b)) { 0 0 0
5443 0 cd->subst_text = b.data;
5444 0 cd->subst_text_len = b.len;
5445 } else {
5446 0 Safefree(b.data);
5447 }
5448 0 return 1;
5449 }
5450  
5451 /* Message helpers: short errors naming the column and the fix. */
5452 0 static void resolve_err_trunc(const char *src, size_t len,
5453 char *dst, size_t dstsz) {
5454 0 size_t n = len < dstsz - 1 ? len : dstsz - 1;
5455 size_t i;
5456 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
5457 0 ▶ dst[i] = (src[i] == '\n' || src[i] == '\r') ? ' ' : src[i];
Branch: 2 branches
BranchTrueFalseTotal
dst[i] = (src[i] == '\n' || src[i] == '\r') ? ' ' : src[i]; 0 0 0
dst[i] = (src[i] == '\n' || src[i] == '\r') ? ' ' : src[i]; 0 0 0
5458 0 dst[n] = '\0';
5459 0 }
5460  
5461 /* Resolve every column's default once per insert. NULL on failure
5462 * with the reason in errbuf. */
5463 0 static col_default_t *resolve_insert_defaults(pTHX_ ev_clickhouse_t *self,
5464 const char **cnames, size_t *cname_lens, col_type_t **ctypes,
5465 const char **ctypes_str, size_t *ctype_lens, int num_cols,
5466 char *errbuf, size_t errbuf_sz) {
5467 col_default_t *cdefs;
5468 int i;
5469 0 Newxz(cdefs, num_cols, col_default_t);
5470 0 ▶ for (i = 0; i < num_cols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_cols; i++) { 0 0 0
5471 0 col_default_t *cd = &cdefs[i];
5472 0 SV **dp = NULL;
5473 const tc_col_t *tc;
5474 lit_t *lit;
5475 char expr_tr[72];
5476 0 ▶ if (self->insert_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_defaults) { 0 0 0
5477 0 dp = hv_fetch(self->insert_defaults, cnames[i],
5478 (I32)cname_lens[i], 0);
5479 }
5480 0 ▶ if (dp && *dp) {
Branch: 2 branches
BranchTrueFalseTotal
if (dp && *dp) { 0 0 0
if (dp && *dp) { 0 0 0
5481 0 cd->subst = 1;
5482 0 cd->nullable = nullable_top(ctypes[i]);
5483 0 resolve_from_sv(cd, *dp);
5484 0 continue;
5485 }
5486 0 ▶ if (!self->insert_empty_as_default) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->insert_empty_as_default) continue; 0 0 0
5487 0 cd->subst = 1;
5488 0 cd->nullable = nullable_top(ctypes[i]);
5489 0 ▶ if (!self->insert_tc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->insert_tc) { 0 0 0
5490 0 snprintf(errbuf, errbuf_sz, "server did not send column "
5491 "defaults for this insert: use data_format => "
5492 "'Native' for server-side defaults");
5493 0 goto fail;
5494 }
5495 0 ▶ if (self->insert_tc->bad) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_tc->bad) { 0 0 0
5496 0 snprintf(errbuf, errbuf_sz, "cannot parse the server's column "
5497 "description: use data_format => 'Native' for "
5498 "server-side defaults");
5499 0 goto fail;
5500 }
5501 0 tc = find_tc_col(self->insert_tc, cnames[i], cname_lens[i]);
5502 0 ▶ if (!tc || !tc->kind) {
Branch: 2 branches
BranchTrueFalseTotal
if (!tc || !tc->kind) { 0 0 0
if (!tc || !tc->kind) { 0 0 0
5503 0 int imp = 0;
5504 0 char *t = NULL;
5505 0 size_t tl = 0;
5506 0 ▶ if (cd->nullable) {
Branch: 1 branch
BranchTrueFalseTotal
if (cd->nullable) { 0 0 0
5507 0 cd->to_null = 1;
5508 0 continue;
5509 }
5510 0 cd->subst_sv = zero_sv_for(aTHX_ ctypes[i], &imp);
5511 0 ▶ if (!cd->subst_sv || imp) {
Branch: 2 branches
BranchTrueFalseTotal
if (!cd->subst_sv || imp) { 0 0 0
if (!cd->subst_sv || imp) { 0 0 0
5512 0 ▶ if (cd->subst_sv) SvREFCNT_dec(cd->subst_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (cd->subst_sv) SvREFCNT_dec(cd->subst_sv); 0 0 0
5513 0 cd->subst_sv = NULL;
5514 0 goto no_client_default;
5515 }
5516 0 ▶ if (zero_text_for(ctypes[i], &t, &tl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (zero_text_for(ctypes[i], &t, &tl)) { 0 0 0
5517 0 cd->subst_text = t;
5518 0 cd->subst_text_len = tl;
5519 }
5520 0 continue;
5521 0 no_client_default:
5522 {
5523 char cn_tr[40], ct_tr[32];
5524 0 resolve_err_trunc(cnames[i], cname_lens[i], cn_tr,
5525 sizeof(cn_tr));
5526 0 resolve_err_trunc(ctypes_str[i], ctype_lens[i], ct_tr,
5527 sizeof(ct_tr));
5528 0 snprintf(errbuf, errbuf_sz, "column '%s' (%s): no "
5529 "client-side default for empty values: use "
5530 "data_format => 'Native'", cn_tr, ct_tr);
5531 0 goto fail;
5532 }
5533 }
5534 0 ▶ if (tc->kind != 'D') {
Branch: 1 branch
BranchTrueFalseTotal
if (tc->kind != 'D') { 0 0 0
5535 char cn_tr[40], ct_tr[32];
5536 0 const char *kw = (tc->kind == 'M') ? "MATERIALIZED"
5537 0 ▶ : (tc->kind == 'A') ? "ALIAS" : "EPHEMERAL";
Branch: 2 branches
BranchTrueFalseTotal
: (tc->kind == 'A') ? "ALIAS" : "EPHEMERAL"; 0 0 0
: (tc->kind == 'A') ? "ALIAS" : "EPHEMERAL"; 0 0 0
5538 0 resolve_err_trunc(cnames[i], cname_lens[i], cn_tr,
5539 sizeof(cn_tr));
5540 0 resolve_err_trunc(ctypes_str[i], ctype_lens[i], ct_tr,
5541 sizeof(ct_tr));
5542 0 ▶ if (tc->expr && tc->expr_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (tc->expr && tc->expr_len) { 0 0 0
if (tc->expr && tc->expr_len) { 0 0 0
5543 0 resolve_err_trunc(tc->expr, tc->expr_len, expr_tr,
5544 sizeof(expr_tr));
5545 0 snprintf(errbuf, errbuf_sz, "column '%s' (%s): %s "
5546 "'%s': use data_format => 'Native' for "
5547 "server-side defaults", cn_tr, ct_tr,
5548 kw, expr_tr);
5549 } else {
5550 0 snprintf(errbuf, errbuf_sz, "column '%s' (%s): %s "
5551 "column: use data_format => 'Native' for "
5552 "server-side defaults", cn_tr, ct_tr, kw);
5553 }
5554 0 goto fail;
5555 }
5556 0 ▶ lit = parse_default_literal(tc->expr ? tc->expr : "",
Branch: 1 branch
BranchTrueFalseTotal
lit = parse_default_literal(tc->expr ? tc->expr : "", 0 0 0
5557 0 ▶ tc->expr ? tc->expr_len : 0);
Branch: 1 branch
BranchTrueFalseTotal
tc->expr ? tc->expr_len : 0); 0 0 0
5558 0 ▶ if (!lit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit) { 0 0 0
5559 char cn_tr[40], ct_tr[32];
5560 0 resolve_err_trunc(cnames[i], cname_lens[i], cn_tr,
5561 sizeof(cn_tr));
5562 0 resolve_err_trunc(ctypes_str[i], ctype_lens[i], ct_tr,
5563 sizeof(ct_tr));
5564 0 ▶ if (tc->expr && tc->expr_len)
Branch: 2 branches
BranchTrueFalseTotal
if (tc->expr && tc->expr_len) 0 0 0
if (tc->expr && tc->expr_len) 0 0 0
5565 0 resolve_err_trunc(tc->expr, tc->expr_len, expr_tr,
5566 sizeof(expr_tr));
5567 else
5568 0 expr_tr[0] = '\0';
5569 0 snprintf(errbuf, errbuf_sz, "column '%s' (%s): non-literal "
5570 "DEFAULT '%s': use data_format => 'Native' for "
5571 "server-side defaults", cn_tr, ct_tr, expr_tr);
5572 0 goto fail;
5573 }
5574 0 ▶ if (!resolve_from_lit(aTHX_ cd, lit)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!resolve_from_lit(aTHX_ cd, lit)) { 0 0 0
5575 char cn_tr[40], ct_tr[32];
5576 0 free_lit(lit);
5577 0 resolve_err_trunc(cnames[i], cname_lens[i], cn_tr,
5578 sizeof(cn_tr));
5579 0 resolve_err_trunc(ctypes_str[i], ctype_lens[i], ct_tr,
5580 sizeof(ct_tr));
5581 0 snprintf(errbuf, errbuf_sz, "column '%s' (%s): no "
5582 "client-side default for empty values: use "
5583 "data_format => 'Native'", cn_tr, ct_tr);
5584 0 goto fail;
5585 }
5586 0 free_lit(lit);
5587 }
5588 0 ▶ if (self->insert_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_defaults) { 0 0 0
5589 HE *he;
5590 0 hv_iterinit(self->insert_defaults);
5591 0 ▶ while ((he = hv_iternext(self->insert_defaults))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(self->insert_defaults))) { 0 0 0
5592 I32 klen;
5593 0 char *key = hv_iterkey(he, &klen);
5594 0 int j, found = 0;
5595 0 ▶ for (j = 0; j < num_cols; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_cols; j++) { 0 0 0
5596 0 ▶ if (cname_lens[j] == (size_t)klen
Branch: 1 branch
BranchTrueFalseTotal
if (cname_lens[j] == (size_t)klen 0 0 0
5597 0 ▶ && memcmp(cnames[j], key, (size_t)klen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(cnames[j], key, (size_t)klen) == 0) { 0 0 0
5598 0 found = 1;
5599 0 break;
5600 }
5601 }
5602 0 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 0 0 0
5603 char k_tr[64];
5604 0 resolve_err_trunc(key, (size_t)klen, k_tr,
5605 sizeof(k_tr));
5606 0 snprintf(errbuf, errbuf_sz, "defaults key '%s' matches "
5607 "no insert column", k_tr);
5608 0 goto fail;
5609 }
5610 }
5611 }
5612 0 return cdefs;
5613 0 fail:
5614 0 free_col_defaults(cdefs, num_cols);
5615 0 return NULL;
5616 }
5617  
5618 /* --- Insert encoding error context --- */
5619  
5620 /* The column and rows of the encode call in progress, so a failure or an
5621 * exception can be traced back to its row. */
5622 typedef struct {
5623 int col; /* -1 outside a column */
5624 SSize_t r0, n;
5625 } enc_progress_t;
5626  
5627 #define ERR_VALUE_MAX 40
5628 #define ERR_NAME_MAX 60
5629  
5630 8 static void err_quote(native_buf_t *b, const char *s, size_t len) {
5631 8 size_t i, n = len > ERR_VALUE_MAX ? ERR_VALUE_MAX : len;
5632 8 nbuf_u8(b, '\'');
5633 124 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 116 8 124
5634 116 unsigned char c = (unsigned char)s[i];
5635 char esc[8];
5636 116 switch (c) {
5637 0 case '\'': nbuf_append(b, "\\'", 2); break;
5638 0 case '\\': nbuf_append(b, "\\\\", 2); break;
5639 0 case '\n': nbuf_append(b, "\\n", 2); break;
5640 0 case '\t': nbuf_append(b, "\\t", 2); break;
5641 0 case '\r': nbuf_append(b, "\\r", 2); break;
5642 116 default:
5643 116 ▶ if (c < 0x20 || c > 0x7e) {
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x20 || c > 0x7e) { 116 0 116
if (c < 0x20 || c > 0x7e) { 0 116 116
5644 0 snprintf(esc, sizeof(esc), "\\x%02x", c);
5645 0 nbuf_append(b, esc, 4);
5646 } else {
5647 116 nbuf_u8(b, c);
5648 }
5649 }
5650 }
5651 8 nbuf_u8(b, '\'');
5652 8 ▶ if (n < len) nbuf_append(b, "...", 3);
Branch: 1 branch
BranchTrueFalseTotal
if (n < len) nbuf_append(b, "...", 3); 0 8 8
5653 8 }
5654  
5655 40 static void err_append(native_buf_t *b, const char *s) {
5656 40 nbuf_append(b, s, strlen(s));
5657 40 }
5658  
5659 /* A value as error text, without running overloads: undef, the type of a
5660 * reference, else the quoted (escaped, truncated) string. */
5661 8 static void err_value_sv(pTHX_ native_buf_t *b, SV *v) {
5662 8 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 0 8 8
5663 0 SV *rv = SvRV(v);
5664 0 err_append(b, sv_reftype(rv, SvOBJECT(rv)));
5665 0 ▶ err_append(b, SvOBJECT(rv) ? " object" : " reference");
Branch: 1 branch
BranchTrueFalseTotal
err_append(b, SvOBJECT(rv) ? " object" : " reference"); 0 0 0
5666 8 ▶ } else if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!SvOK(v)) { 0 8 8
5667 0 err_append(b, "undef");
5668 } else {
5669 STRLEN l;
5670 8 const char *s = SvPV_nomg(v, l);
5671 8 err_quote(b, s, l);
5672 }
5673 8 }
5674  
5675 16 static void err_name(native_buf_t *b, const char *s, size_t len) {
5676 16 nbuf_append(b, s, len > ERR_NAME_MAX ? ERR_NAME_MAX : len);
5677 16 ▶ if (len > ERR_NAME_MAX) err_append(b, "...");
Branch: 1 branch
BranchTrueFalseTotal
if (len > ERR_NAME_MAX) err_append(b, "..."); 0 16 16
5678 16 }
5679  
5680 /* "column 'name' (type)" */
5681 8 static void err_where(char *dst, size_t sz, const char *name, size_t name_len,
5682 const char *type, size_t type_len) {
5683 native_buf_t b;
5684 8 nbuf_init(&b);
5685 8 err_append(&b, "column '");
5686 8 err_name(&b, name, name_len);
5687 8 err_append(&b, "' (");
5688 8 err_name(&b, type, type_len);
5689 8 err_append(&b, ")");
5690 8 snprintf(dst, sz, "%.*s", (int)b.len, b.data);
5691 8 Safefree(b.data);
5692 8 }
5693  
5694 8 static void err_finish(char *errbuf, size_t sz, native_buf_t *msg) {
5695 8 snprintf(errbuf, sz, "%.*s", (int)msg->len, msg->data);
5696 8 Safefree(msg->data);
5697 8 }
5698  
5699 /* Exception text on one line, the die location kept. */
5700 0 static void err_exception(pTHX_ native_buf_t *b, SV *err) {
5701 STRLEN l, i;
5702 const char *s;
5703 0 ▶ if (!err) { err_append(b, "died"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!err) { err_append(b, "died"); return; } 0 0 0
5704 0 ▶ if (SvROK(err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(err)) { 0 0 0
5705 0 err_append(b, "died with ");
5706 0 err_value_sv(aTHX_ b, err);
5707 0 return;
5708 }
5709 0 s = SvPV_nomg(err, l);
5710 0 ▶ while (l && (s[l - 1] == '\n' || s[l - 1] == '\r')) l--;
Branch: 3 branches
BranchTrueFalseTotal
while (l && (s[l - 1] == '\n' || s[l - 1] == '\r')) l--; 0 0 0
while (l && (s[l - 1] == '\n' || s[l - 1] == '\r')) l--; 0 0 0
while (l && (s[l - 1] == '\n' || s[l - 1] == '\r')) l--; 0 0 0
5711 0 ▶ if (l > 160) l = 160;
Branch: 1 branch
BranchTrueFalseTotal
if (l > 160) l = 160; 0 0 0
5712 0 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 0 0 0
5713 0 ▶ nbuf_u8(b, (s[i] == '\n' || s[i] == '\r') ? ' ' : s[i]);
Branch: 2 branches
BranchTrueFalseTotal
nbuf_u8(b, (s[i] == '\n' || s[i] == '\r') ? ' ' : s[i]); 0 0 0
nbuf_u8(b, (s[i] == '\n' || s[i] == '\r') ? ' ' : s[i]); 0 0 0
5714 }
5715  
5716 8 static int sv_encodes(pTHX_ SV *v, col_type_t *ct, native_buf_t *scratch) {
5717 8 scratch->len = 0;
5718 8 return encode_column_sv(aTHX_ scratch, &v, 1, ct);
5719 }
5720  
5721 /* Inside a top-level value that does not encode, the innermost element
5722 * that does not encode on its own (the value itself when none). */
5723 8 static SV *locate_bad_sv(pTHX_ SV *v, col_type_t *ct, native_buf_t *scratch) {
5724 SSize_t j, n;
5725 8 switch (ct->code) {
5726 0 case CT_NULLABLE:
5727 case CT_LOWCARDINALITY:
5728 0 ▶ return SvOK(v) && ct->inner ? locate_bad_sv(aTHX_ v, ct->inner, scratch) : v;
Branch: 2 branches
BranchTrueFalseTotal
return SvOK(v) && ct->inner ? locate_bad_sv(aTHX_ v, ct->inner, scratch) : v; 0 0 0
return SvOK(v) && ct->inner ? locate_bad_sv(aTHX_ v, ct->inner, scratch) : v; 0 0 0
5729 0 case CT_ARRAY:
5730 case CT_TUPLE:
5731 0 ▶ if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) return v;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) return v; 0 0 0
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) return v; 0 0 0
5732 0 ▶ n = ct->code == CT_ARRAY ? av_len((AV *)SvRV(v)) + 1 : ct->num_inners;
Branch: 1 branch
BranchTrueFalseTotal
n = ct->code == CT_ARRAY ? av_len((AV *)SvRV(v)) + 1 : ct->num_inners; 0 0 0
5733 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
5734 0 ▶ col_type_t *et = ct->code == CT_ARRAY ? ct->inner : ct->inners[j];
Branch: 1 branch
BranchTrueFalseTotal
col_type_t *et = ct->code == CT_ARRAY ? ct->inner : ct->inners[j]; 0 0 0
5735 0 SV **e = av_fetch((AV *)SvRV(v), j, 0);
5736 0 ▶ SV *ev = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *ev = (e && *e) ? *e : &PL_sv_undef; 0 0 0
SV *ev = (e && *e) ? *e : &PL_sv_undef; 0 0 0
5737 0 ▶ if (et && !sv_encodes(aTHX_ ev, et, scratch))
Branch: 2 branches
BranchTrueFalseTotal
if (et && !sv_encodes(aTHX_ ev, et, scratch)) 0 0 0
if (et && !sv_encodes(aTHX_ ev, et, scratch)) 0 0 0
5738 0 return locate_bad_sv(aTHX_ ev, et, scratch);
5739 }
5740 0 return v;
5741 0 case CT_MAP: {
5742 HV *hv;
5743 HE *he;
5744 0 ▶ if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV || ct->num_inners != 2)
Branch: 3 branches
BranchTrueFalseTotal
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV || ct->num_inners != 2) 0 0 0
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV || ct->num_inners != 2) 0 0 0
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV || ct->num_inners != 2) 0 0 0
5745 0 return v;
5746 0 hv = (HV *)SvRV(v);
5747 0 hv_iterinit(hv);
5748 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
5749 0 SV *k = hv_iterkeysv(he);
5750 0 SV *val = hv_iterval(hv, he);
5751 0 ▶ if (!sv_encodes(aTHX_ k, ct->inners[0], scratch))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_encodes(aTHX_ k, ct->inners[0], scratch)) 0 0 0
5752 0 return k;
5753 0 ▶ if (!sv_encodes(aTHX_ val, ct->inners[1], scratch))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_encodes(aTHX_ val, ct->inners[1], scratch)) 0 0 0
5754 0 return locate_bad_sv(aTHX_ val, ct->inners[1], scratch);
5755 }
5756 0 return v;
5757 }
5758 8 default:
5759 8 return v;
5760 }
5761 }
5762  
5763 /* Why encoding vals[0..n) as ct failed: the type cannot be encoded at
5764 * all, else the first value that fails on its own (row base + index),
5765 * with the failing element of a nested value. Re-encodes only on this
5766 * error path. */
5767 8 static void describe_sv_failure(pTHX_ char *errbuf, size_t sz,
5768 const char *where, col_type_t *ct,
5769 SV **vals, SSize_t n, SSize_t base,
5770 const char *unit) {
5771 native_buf_t msg, scratch;
5772 SSize_t k;
5773 8 nbuf_init(&msg);
5774 8 nbuf_init(&scratch);
5775 8 err_append(&msg, where);
5776 8 ▶ if (!encode_column_sv(aTHX_ &scratch, vals, 0, ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_column_sv(aTHX_ &scratch, vals, 0, ct)) { 0 8 8
5777 0 err_append(&msg, ": unsupported type");
5778 } else {
5779 8 ▶ for (k = 0; k < n; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) 8 0 8
5780 8 ▶ if (!sv_encodes(aTHX_ vals[k], ct, &scratch)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_encodes(aTHX_ vals[k], ct, &scratch)) break; 8 0 8
5781 8 ▶ if (k < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (k < n) { 8 0 8
5782 char rb[64];
5783 8 SV *bad = locate_bad_sv(aTHX_ vals[k], ct, &scratch);
5784 8 snprintf(rb, sizeof(rb), ", %s %ld: invalid %s ", unit,
5785 8 ▶ (long)(base + k), bad == vals[k] ? "value" : "element");
Branch: 1 branch
BranchTrueFalseTotal
(long)(base + k), bad == vals[k] ? "value" : "element"); 8 0 8
5786 8 err_append(&msg, rb);
5787 8 err_value_sv(aTHX_ &msg, bad);
5788 } else {
5789 0 err_append(&msg, ": invalid value");
5790 }
5791 }
5792 8 Safefree(scratch.data);
5793 8 err_finish(errbuf, sz, &msg);
5794 8 }
5795  
5796 /* describe_sv_failure() for the TabSeparated path; rows are lines. */
5797 0 static void describe_text_failure(char *errbuf, size_t sz, const char *where,
5798 col_type_t *ct, const char **vals,
5799 size_t *lens, SSize_t n) {
5800 native_buf_t msg, scratch;
5801 SSize_t k;
5802 0 nbuf_init(&msg);
5803 0 nbuf_init(&scratch);
5804 0 err_append(&msg, where);
5805 0 ▶ if (!encode_column_text(&scratch, vals, lens, 0, ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_column_text(&scratch, vals, lens, 0, ct)) { 0 0 0
5806 0 err_append(&msg, ": unsupported type");
5807 } else {
5808 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
5809 0 scratch.len = 0;
5810 0 ▶ if (!encode_column_text(&scratch, &vals[k], &lens[k], 1, ct)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_column_text(&scratch, &vals[k], &lens[k], 1, ct)) break; 0 0 0
5811 }
5812 0 ▶ if (k < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (k < n) { 0 0 0
5813 char rb[48];
5814 0 snprintf(rb, sizeof(rb), ", line %ld: invalid value ", (long)(k + 1));
5815 0 err_append(&msg, rb);
5816 0 err_quote(&msg, vals[k], lens[k]);
5817 } else {
5818 0 err_append(&msg, ": invalid value");
5819 }
5820 }
5821 0 Safefree(scratch.data);
5822 0 err_finish(errbuf, sz, &msg);
5823 0 }
5824  
5825 /* Exceptions from user code (overloads, tied values) during the deferred
5826 * native encode must not escape the I/O callback: run_guarded() calls a
5827 * C function inside an eval through a private XSUB. */
5828 typedef struct {
5829 void (*fn)(pTHX_ void *);
5830 void *arg;
5831 } ch_guarded_t;
5832  
5833 static CV *ch_guard_cv;
5834  
5835 12 XS_INTERNAL(ch_guard_xs) {
5836 12 dXSARGS;
5837 ch_guarded_t *g;
5838 12 ▶ if (items != 1) croak_xs_usage(cv, "guard");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak_xs_usage(cv, "guard"); 0 12 12
5839 12 g = INT2PTR(ch_guarded_t *, SvIV(ST(0)));
5840 12 g->fn(aTHX_ g->arg);
5841 12 XSRETURN_EMPTY;
5842 }
5843  
5844 /* 1 when fn returned, 0 when it died (exception copied to *err when err
5845 * is given); $@ is left as it was. */
5846 12 static int run_guarded(pTHX_ void (*fn)(pTHX_ void *), void *arg, SV **err) {
5847 12 dSP;
5848 ch_guarded_t g;
5849 int died;
5850 12 g.fn = fn;
5851 12 g.arg = arg;
5852 12 ▶ if (err) *err = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = NULL; 12 0 12
5853 12 ENTER; SAVETMPS;
5854 12 save_scalar(PL_errgv);
5855 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
5856 12 ▶ mXPUSHi(PTR2IV(&g));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(PTR2IV(&g)); 0 12 12
5857 12 PUTBACK;
5858 12 call_sv((SV *)ch_guard_cv, G_EVAL | G_DISCARD);
5859 12 ▶ died = SvROK(ERRSV) || SvTRUE(ERRSV);
Branch: 4 branches
BranchTrueFalseTotal
died = SvROK(ERRSV) || SvTRUE(ERRSV); 12 0 12
died = SvROK(ERRSV) || SvTRUE(ERRSV); 12 0 12
died = SvROK(ERRSV) || SvTRUE(ERRSV); 12 0 12
died = SvROK(ERRSV) || SvTRUE(ERRSV); 0 12 12
5860 12 ▶ if (died && err) *err = newSVsv(ERRSV);
Branch: 3 branches
BranchTrueFalseTotal
if (died && err) *err = newSVsv(ERRSV); 0 12 12
if (died && err) *err = newSVsv(ERRSV); 0 0 0
if (died && err) *err = newSVsv(ERRSV); 0 0 0
5861 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 12 0 12
5862 12 return !died;
5863 }
5864  
5865 typedef struct {
5866 ev_clickhouse_t *self;
5867 const char **names; size_t *name_lens;
5868 col_type_t **types;
5869 const char **types_str; size_t *type_lens;
5870 int ncols;
5871 char *errbuf; size_t errbuf_sz;
5872 col_default_t *cdefs;
5873 } resolve_args_t;
5874  
5875 0 static void resolve_fn(pTHX_ void *p) {
5876 0 resolve_args_t *a = (resolve_args_t *)p;
5877 0 a->cdefs = resolve_insert_defaults(aTHX_ a->self, a->names, a->name_lens,
5878 a->types, a->types_str, a->type_lens, a->ncols, a->errbuf,
5879 a->errbuf_sz);
5880 0 }
5881  
5882 /* resolve_insert_defaults(): a caller's default value may be an object
5883 * whose stringification dies. */
5884 0 static col_default_t *resolve_insert_defaults_guarded(pTHX_ ev_clickhouse_t *self,
5885 const char **cnames, size_t *cname_lens, col_type_t **ctypes,
5886 const char **ctypes_str, size_t *ctype_lens, int num_cols,
5887 char *errbuf, size_t errbuf_sz) {
5888 resolve_args_t a;
5889 0 SV *err = NULL;
5890 0 a.self = self; a.names = cnames; a.name_lens = cname_lens;
5891 0 a.types = ctypes; a.types_str = ctypes_str; a.type_lens = ctype_lens;
5892 0 a.ncols = num_cols; a.errbuf = errbuf; a.errbuf_sz = errbuf_sz;
5893 0 a.cdefs = NULL;
5894 0 ▶ if (!run_guarded(aTHX_ resolve_fn, &a, &err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!run_guarded(aTHX_ resolve_fn, &a, &err)) { 0 0 0
5895 native_buf_t msg;
5896 0 nbuf_init(&msg);
5897 0 err_append(&msg, "defaults: ");
5898 0 err_exception(aTHX_ &msg, err);
5899 0 ▶ if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) SvREFCNT_dec(err); 0 0 0
5900 0 err_finish(errbuf, errbuf_sz, &msg);
5901 0 return NULL;
5902 }
5903 0 return a.cdefs;
5904 }
5905  
5906 /*
5907 * Build a native protocol Data block from TabSeparated text data.
5908 * col_names/col_types_str are string references into the sample block buffer.
5909 * Returns malloc'd packet data (CLIENT_DATA + block), or NULL on failure.
5910 */
5911 0 static char* build_native_insert_data(ev_clickhouse_t *self,
5912 const char *tsv_data, size_t tsv_len,
5913 const char **col_names, size_t *col_name_lens,
5914 const char **col_types_str, size_t *col_type_lens,
5915 col_type_t **col_types,
5916 int num_cols,
5917 col_default_t *cdefs,
5918 size_t *out_len,
5919 char *errbuf, size_t errbuf_sz) {
5920 /* Parse TSV into rows and fields */
5921 0 int nrows = 0, max_rows = 64;
5922 0 const char **fields = NULL; /* flat array: fields[row * num_cols + col] */
5923 0 size_t *field_lens = NULL;
5924 0 const char *p = tsv_data;
5925 0 const char *end = tsv_data + tsv_len;
5926  
5927 0 Newxz(fields, max_rows * num_cols, const char *);
5928 0 Newx(field_lens, max_rows * num_cols, size_t);
5929  
5930 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
5931 0 const char *line_end = memchr(p, '\n', end - p);
5932 0 ▶ const char *line_limit = line_end ? line_end : end;
Branch: 1 branch
BranchTrueFalseTotal
const char *line_limit = line_end ? line_end : end; 0 0 0
5933 int col;
5934  
5935 /* No blank-line skipping: the server keeps a blank line as an
5936 * (empty-field) row, or errors when columns are missing. */
5937 /* The server rejects DOS line endings (error 117); match it
5938 * rather than silently storing the \r. Mid-field \r stays. */
5939 0 ▶ if (line_limit > p && line_limit[-1] == '\r') {
Branch: 2 branches
BranchTrueFalseTotal
if (line_limit > p && line_limit[-1] == '\r') { 0 0 0
if (line_limit > p && line_limit[-1] == '\r') { 0 0 0
5940 0 Safefree(fields);
5941 0 Safefree(field_lens);
5942 0 snprintf(errbuf, errbuf_sz, "line %d ends with \\r "
5943 "(CRLF line endings are illegal in TabSeparated data)",
5944 nrows + 1);
5945 0 *out_len = (size_t)-1;
5946 0 return NULL;
5947 }
5948  
5949 0 ▶ if (nrows >= max_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows >= max_rows) { 0 0 0
5950 0 ▶ if (max_rows > INT_MAX / 2 ||
Branch: 2 branches
BranchTrueFalseTotal
if (max_rows > INT_MAX / 2 || 0 0 0
if (max_rows > INT_MAX / 2 || 0 0 0
5951 0 ▶ (num_cols > 0 && max_rows * 2 > INT_MAX / num_cols)) {
Branch: 1 branch
BranchTrueFalseTotal
(num_cols > 0 && max_rows * 2 > INT_MAX / num_cols)) { 0 0 0
5952 0 Safefree(fields);
5953 0 Safefree(field_lens);
5954 0 *out_len = 0;
5955 0 return NULL;
5956 }
5957 0 max_rows *= 2;
5958 0 Renew(fields, max_rows * num_cols, const char *);
5959 0 Renew(field_lens, max_rows * num_cols, size_t);
5960 }
5961  
5962 /* split line by tabs */
5963 {
5964 0 const char *fp = p;
5965 0 ▶ for (col = 0; col < num_cols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < num_cols; col++) { 0 0 0
5966 const char *tab;
5967 0 ▶ if (fp > line_limit) fp = line_limit;
Branch: 1 branch
BranchTrueFalseTotal
if (fp > line_limit) fp = line_limit; 0 0 0
5968 0 ▶ if (col < num_cols - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (col < num_cols - 1) { 0 0 0
5969 0 tab = memchr(fp, '\t', line_limit - fp);
5970 0 ▶ if (!tab) tab = line_limit;
Branch: 1 branch
BranchTrueFalseTotal
if (!tab) tab = line_limit; 0 0 0
5971 } else {
5972 0 tab = line_limit;
5973 }
5974 0 fields[nrows * num_cols + col] = fp;
5975 0 field_lens[nrows * num_cols + col] = tab - fp;
5976 0 fp = tab + 1;
5977 }
5978 }
5979 0 nrows++;
5980 0 p = line_limit + 1;
5981 }
5982  
5983 0 ▶ if (nrows == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) { 0 0 0
5984 0 Safefree(fields);
5985 0 Safefree(field_lens);
5986 0 *out_len = 0;
5987 0 return NULL;
5988 }
5989  
5990 /* Build the Data block body: block info + num_cols + num_rows + columns */
5991 {
5992 native_buf_t body;
5993 int col;
5994  
5995 0 nbuf_init(&body);
5996 0 nbuf_block_info(&body);
5997 0 nbuf_varuint(&body, (uint64_t)num_cols);
5998 0 nbuf_varuint(&body, (uint64_t)nrows);
5999  
6000 /* encode each column */
6001 0 ▶ for (col = 0; col < num_cols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < num_cols; col++) { 0 0 0
6002 const char **col_vals;
6003 size_t *col_vlens;
6004 int row;
6005 0 ▶ int sub = cdefs ? cdefs[col].subst : 0;
Branch: 1 branch
BranchTrueFalseTotal
int sub = cdefs ? cdefs[col].subst : 0; 0 0 0
6006 0 ▶ int nulcol = sub && cdefs[col].nullable;
Branch: 2 branches
BranchTrueFalseTotal
int nulcol = sub && cdefs[col].nullable; 0 0 0
int nulcol = sub && cdefs[col].nullable; 0 0 0
6007  
6008 /* column name and type */
6009 0 nbuf_string(&body, col_names[col], col_name_lens[col]);
6010 0 nbuf_string(&body, col_types_str[col], col_type_lens[col]);
6011 0 nbuf_u8(&body, 0); /* has_custom_serialization = false */
6012  
6013 /* gather column values from row-major fields */
6014 0 Newxz(col_vals, nrows, const char *);
6015 0 Newx(col_vlens, nrows, size_t);
6016 0 ▶ for (row = 0; row < nrows; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < nrows; row++) { 0 0 0
6017 0 const char *fp = fields[row * num_cols + col];
6018 0 size_t fl = field_lens[row * num_cols + col];
6019 0 ▶ if (sub && (fl == 0
Branch: 2 branches
BranchTrueFalseTotal
if (sub && (fl == 0 0 0 0
if (sub && (fl == 0 0 0 0
6020 0 ▶ || (!nulcol && is_tsv_null(fp, fl)))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (!nulcol && is_tsv_null(fp, fl)))) { 0 0 0
|| (!nulcol && is_tsv_null(fp, fl)))) { 0 0 0
6021 0 ▶ if (cdefs[col].to_null) {
Branch: 1 branch
BranchTrueFalseTotal
if (cdefs[col].to_null) { 0 0 0
6022 0 fp = "\\N";
6023 0 fl = 2;
6024 0 ▶ } else if (cdefs[col].subst_text) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cdefs[col].subst_text) { 0 0 0
6025 0 fp = cdefs[col].subst_text;
6026 0 fl = cdefs[col].subst_text_len;
6027 } else {
6028 0 Safefree(col_vals);
6029 0 Safefree(col_vlens);
6030 0 Safefree(body.data);
6031 0 Safefree(fields);
6032 0 Safefree(field_lens);
6033 0 snprintf(errbuf, errbuf_sz, "column '%.*s' "
6034 "(%.*s): no client-side default for "
6035 "empty values: use data_format => "
6036 "'Native'",
6037 0 (int)col_name_lens[col], col_names[col],
6038 0 (int)col_type_lens[col],
6039 0 col_types_str[col]);
6040 0 *out_len = (size_t)-1;
6041 0 return NULL;
6042 }
6043 }
6044 0 col_vals[row] = fp;
6045 0 col_vlens[row] = fl;
6046 }
6047  
6048 0 ▶ if (nrows) write_col_prefix(&body, col_types[col]);
Branch: 1 branch
BranchTrueFalseTotal
if (nrows) write_col_prefix(&body, col_types[col]); 0 0 0
6049 0 ▶ if (!encode_column_text(&body, col_vals, col_vlens,
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_column_text(&body, col_vals, col_vlens, 0 0 0
6050 0 (uint64_t)nrows, col_types[col])) {
6051 char where[160];
6052 0 err_where(where, sizeof(where), col_names[col],
6053 0 col_name_lens[col], col_types_str[col],
6054 0 col_type_lens[col]);
6055 0 describe_text_failure(errbuf, errbuf_sz, where, col_types[col],
6056 col_vals, col_vlens, nrows);
6057 0 Safefree(col_vals);
6058 0 Safefree(col_vlens);
6059 0 Safefree(body.data);
6060 0 Safefree(fields);
6061 0 Safefree(field_lens);
6062 0 *out_len = (size_t)-1; /* sentinel: encode failure */
6063 0 return NULL;
6064 }
6065 0 Safefree(col_vals);
6066 0 Safefree(col_vlens);
6067 }
6068  
6069 0 Safefree(fields);
6070 0 Safefree(field_lens);
6071  
6072 0 return wrap_data_block(self, &body, out_len);
6073 }
6074 }
6075  
6076 16 static void nbuf_reserve(native_buf_t *b, size_t cap) {
6077 16 ▶ if (cap > b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (cap > b->cap) { 0 16 16
6078 0 b->cap = cap;
6079 0 Renew(b->data, cap, char);
6080 }
6081 16 }
6082  
6083 /* Rows per pass of the insert build: a pass encodes every column of a
6084 * chunk of rows while their scalars are cache-hot, instead of walking all
6085 * rows' scalars once per column. */
6086 #define INSERT_CHUNK_ROWS 1024
6087  
6088 enum { ENC_WHOLE, ENC_PLAIN, ENC_NULLABLE, ENC_ARRAY, ENC_LC };
6089  
6090 /* Types whose column encoding is the concatenation of per-value bytes. */
6091 12 static int enc_plain_type(const col_type_t *ct) {
6092 12 ▶ switch (ct->code) {
Branch: 1 branch
BranchTrueFalseTotal
switch (ct->code) { 12 0 12
6093 12 case CT_INT8: case CT_INT16: case CT_INT32: case CT_INT64:
6094 case CT_UINT8: case CT_UINT16: case CT_UINT32: case CT_UINT64:
6095 case CT_BOOL: case CT_FLOAT32: case CT_FLOAT64: case CT_BFLOAT16:
6096 case CT_DATE: case CT_DATE32: case CT_DATETIME: case CT_DATETIME64:
6097 case CT_TIME: case CT_TIME64:
6098 case CT_DECIMAL32: case CT_DECIMAL64: case CT_DECIMAL128: case CT_DECIMAL256:
6099 case CT_STRING: case CT_FIXEDSTRING: case CT_UUID: case CT_IPV4: case CT_IPV6:
6100 case CT_ENUM8: case CT_ENUM16:
6101 12 return 1;
6102 0 default:
6103 0 return 0;
6104 }
6105 }
6106  
6107 /* Nullable/Array/LowCardinality of a plain type encode a chunk as parts
6108 * that concatenate across chunks; anything else is encoded whole. */
6109 12 static int enc_col_mode(const col_type_t *ct) {
6110 12 ▶ if (enc_plain_type(ct)) return ENC_PLAIN;
Branch: 1 branch
BranchTrueFalseTotal
if (enc_plain_type(ct)) return ENC_PLAIN; 12 0 12
6111 0 ▶ if (!ct->inner || !enc_plain_type(ct->inner)) return ENC_WHOLE;
Branch: 2 branches
BranchTrueFalseTotal
if (!ct->inner || !enc_plain_type(ct->inner)) return ENC_WHOLE; 0 0 0
if (!ct->inner || !enc_plain_type(ct->inner)) return ENC_WHOLE; 0 0 0
6112 0 switch (ct->code) {
6113 0 case CT_NULLABLE: return ENC_NULLABLE;
6114 0 case CT_ARRAY: return ENC_ARRAY;
6115 0 case CT_LOWCARDINALITY: return ENC_LC;
6116 0 default: return ENC_WHOLE;
6117 }
6118 }
6119  
6120 typedef struct {
6121 int mode;
6122 native_buf_t data; /* values, array elements or LC dictionary */
6123 native_buf_t aux; /* null map or array offsets */
6124 uint64_t base; /* array elements so far */
6125 } enc_col_t;
6126  
6127 typedef struct {
6128 enc_col_t *cols;
6129 int ncols;
6130 SV **vals;
6131 native_buf_t tmp;
6132 native_buf_t out;
6133 } enc_ctx_t;
6134  
6135 12 static void enc_ctx_free(pTHX_ void *p) {
6136 12 enc_ctx_t *x = (enc_ctx_t *)p;
6137 int c;
6138 24 ▶ for (c = 0; c < x->ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < x->ncols; c++) { 12 12 24
6139 12 Safefree(x->cols[c].data.data);
6140 12 Safefree(x->cols[c].aux.data);
6141 }
6142 12 Safefree(x->cols);
6143 12 Safefree(x->vals);
6144 12 Safefree(x->tmp.data);
6145 12 Safefree(x->out.data);
6146 12 Safefree(x);
6147 12 }
6148  
6149 /* Gather one column of n rows, with empty cells replaced by the column's
6150 * resolved default when cd asks for it: '' always, undef unless the
6151 * column is Nullable. */
6152 20 static void snap_gather(const insert_snap_t *sn, SSize_t r0, SSize_t n,
6153 int col, const col_default_t *cd, SV **vals) {
6154 SSize_t k;
6155 SV *to;
6156 40 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 20 20 40
6157 20 SSize_t o = sn->off[r0 + k];
6158 20 ▶ SV *v = col < sn->off[r0 + k + 1] - o ? sn->cells[o + col] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = col < sn->off[r0 + k + 1] - o ? sn->cells[o + col] : NULL; 20 0 20
6159 20 ▶ vals[k] = v ? v : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
vals[k] = v ? v : &PL_sv_undef; 20 0 20
6160 }
6161 20 ▶ if (!cd || !cd->subst) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!cd || !cd->subst) return; 0 20 20
if (!cd || !cd->subst) return; 0 0 0
6162 0 ▶ to = cd->to_null ? &PL_sv_undef : cd->subst_sv;
Branch: 1 branch
BranchTrueFalseTotal
to = cd->to_null ? &PL_sv_undef : cd->subst_sv; 0 0 0
6163 0 ▶ if (!to) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!to) return; 0 0 0
6164 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6165 0 SV *v = vals[k];
6166 0 ▶ if (SvOK(v) ? SvPOK(v) && SvCUR(v) == 0 : !cd->nullable)
Branch: 4 branches
BranchTrueFalseTotal
if (SvOK(v) ? SvPOK(v) && SvCUR(v) == 0 : !cd->nullable) 0 0 0
if (SvOK(v) ? SvPOK(v) && SvCUR(v) == 0 : !cd->nullable) 0 0 0
if (SvOK(v) ? SvPOK(v) && SvCUR(v) == 0 : !cd->nullable) 0 0 0
if (SvOK(v) ? SvPOK(v) && SvCUR(v) == 0 : !cd->nullable) 0 0 0
6167 0 vals[k] = to;
6168 }
6169 }
6170  
6171 /* Move one chunk's encoding of n rows (tmp) into the column's parts. */
6172 0 static void enc_split_chunk(enc_col_t *ec, const native_buf_t *tmp, SSize_t n) {
6173 0 switch (ec->mode) {
6174 0 case ENC_NULLABLE:
6175 0 nbuf_append(&ec->aux, tmp->data, (size_t)n);
6176 0 nbuf_append(&ec->data, tmp->data + n, tmp->len - (size_t)n);
6177 0 break;
6178 0 case ENC_ARRAY: {
6179 SSize_t k;
6180 0 uint64_t off = 0, abs;
6181 0 nbuf_grow(&ec->aux, (size_t)n * 8);
6182 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6183 0 memcpy(&off, tmp->data + k * 8, 8);
6184 0 abs = ec->base + off;
6185 0 memcpy(ec->aux.data + ec->aux.len, &abs, 8);
6186 0 ec->aux.len += 8;
6187 }
6188 0 ec->base += off;
6189 0 nbuf_append(&ec->data, tmp->data + n * 8, tmp->len - (size_t)n * 8);
6190 0 break;
6191 }
6192 0 case ENC_LC: {
6193 /* ser_type, num_keys, dictionary, num_indices, indices */
6194 0 int key_type = (unsigned char)tmp->data[0];
6195 0 ▶ size_t w = key_type == 0 ? 1 : key_type == 1 ? 2 : 4;
Branch: 2 branches
BranchTrueFalseTotal
size_t w = key_type == 0 ? 1 : key_type == 1 ? 2 : 4; 0 0 0
size_t w = key_type == 0 ? 1 : key_type == 1 ? 2 : 4; 0 0 0
6196 0 nbuf_append(&ec->data, tmp->data + 16, tmp->len - 24 - (size_t)n * w);
6197 0 break;
6198 }
6199 0 default:
6200 0 break;
6201 }
6202 0 }
6203  
6204 /*
6205 * Build a native protocol Data block from an insert snapshot.
6206 * Like build_native_insert_data() but encodes SVs directly via encode_column_sv().
6207 */
6208 12 static char* build_native_insert_data_from_av(pTHX_ ev_clickhouse_t *self,
6209 SV *snap_sv,
6210 const char **col_names, size_t *col_name_lens,
6211 const char **col_types_str, size_t *col_type_lens,
6212 col_type_t **col_types,
6213 int num_cols,
6214 col_default_t *cdefs,
6215 enc_progress_t *prog,
6216 size_t *out_len,
6217 char *errbuf, size_t errbuf_sz) {
6218 12 insert_snap_t *sn = insert_snap_get(aTHX_ snap_sv);
6219 12 ▶ SSize_t nrows = sn ? sn->nrows : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t nrows = sn ? sn->nrows : 0; 12 0 12
6220 enc_ctx_t *x;
6221 12 SSize_t r0, fail_r0 = 0, fail_n = 0;
6222 12 int c, fail = num_cols;
6223 size_t need;
6224 char *pkt;
6225  
6226 12 ▶ if (nrows <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows <= 0) { 0 12 12
6227 0 *out_len = 0;
6228 0 return NULL;
6229 }
6230 12 ▶ if (sn->bad_row >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sn->bad_row >= 0) { 0 12 12
6231 0 snprintf(errbuf, errbuf_sz, "row %ld is not an arrayref",
6232 0 (long)sn->bad_row);
6233 0 *out_len = (size_t)-1; /* sentinel: encode failure */
6234 0 return NULL;
6235 }
6236 12 ▶ if (sn->width >= 0 && sn->width != num_cols) {
Branch: 2 branches
BranchTrueFalseTotal
if (sn->width >= 0 && sn->width != num_cols) { 0 12 12
if (sn->width >= 0 && sn->width != num_cols) { 0 0 0
6237 0 snprintf(errbuf, errbuf_sz, "columns_at has %ld positions but the "
6238 0 "insert has %d columns", (long)sn->width, num_cols);
6239 0 *out_len = (size_t)-1;
6240 0 return NULL;
6241 }
6242  
6243 12 ENTER;
6244 12 Newxz(x, 1, enc_ctx_t);
6245 12 SAVEDESTRUCTOR_X(enc_ctx_free, x);
6246 12 Newxz(x->cols, num_cols, enc_col_t);
6247 12 x->ncols = num_cols;
6248 12 ▶ Newx(x->vals, nrows, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(x->vals, nrows, SV *); 0 12 12
6249 12 nbuf_init(&x->tmp);
6250 24 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 12 12 24
6251 12 enc_col_t *ec = &x->cols[c];
6252 12 size_t fsz = col_type_fixed_size(col_types[c]);
6253 12 ec->mode = enc_col_mode(col_types[c]);
6254 12 nbuf_init(&ec->data);
6255 12 nbuf_init(&ec->aux);
6256 12 ▶ if (ec->mode == ENC_PLAIN && fsz)
Branch: 2 branches
BranchTrueFalseTotal
if (ec->mode == ENC_PLAIN && fsz) 12 0 12
if (ec->mode == ENC_PLAIN && fsz) 12 0 12
6257 12 nbuf_reserve(&ec->data, fsz * (size_t)nrows);
6258 0 ▶ else if (ec->mode == ENC_NULLABLE)
Branch: 1 branch
BranchTrueFalseTotal
else if (ec->mode == ENC_NULLABLE) 0 0 0
6259 0 nbuf_reserve(&ec->aux, (size_t)nrows);
6260 0 ▶ else if (ec->mode == ENC_ARRAY)
Branch: 1 branch
BranchTrueFalseTotal
else if (ec->mode == ENC_ARRAY) 0 0 0
6261 0 nbuf_reserve(&ec->aux, 8 * (size_t)nrows);
6262 }
6263  
6264 /* A failing column stops the work on itself and every later column,
6265 * so the column reported is the first failing one, as column order
6266 * would find it. */
6267 24 ▶ for (r0 = 0; r0 < nrows && fail > 0; r0 += INSERT_CHUNK_ROWS) {
Branch: 2 branches
BranchTrueFalseTotal
for (r0 = 0; r0 < nrows && fail > 0; r0 += INSERT_CHUNK_ROWS) { 12 12 24
for (r0 = 0; r0 < nrows && fail > 0; r0 += INSERT_CHUNK_ROWS) { 12 0 12
6268 12 SSize_t n = nrows - r0 < INSERT_CHUNK_ROWS ? nrows - r0 : INSERT_CHUNK_ROWS;
6269 12 ENTER; SAVETMPS;
6270 24 ▶ for (c = 0; c < fail; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < fail; c++) { 12 12 24
6271 12 enc_col_t *ec = &x->cols[c];
6272 int ok;
6273 12 ▶ if (ec->mode == ENC_WHOLE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_WHOLE) continue; 0 12 12
6274 12 ▶ snap_gather(sn, r0, n, c, cdefs ? &cdefs[c] : NULL, x->vals);
Branch: 1 branch
BranchTrueFalseTotal
snap_gather(sn, r0, n, c, cdefs ? &cdefs[c] : NULL, x->vals); 0 12 12
6275 12 prog->col = c; prog->r0 = r0; prog->n = n;
6276 12 ▶ if (ec->mode == ENC_PLAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_PLAIN) { 12 0 12
6277 12 ok = encode_column_sv(aTHX_ &ec->data, x->vals, (uint64_t)n,
6278 12 col_types[c]);
6279 } else {
6280 0 x->tmp.len = 0;
6281 0 ok = encode_column_sv(aTHX_ &x->tmp, x->vals, (uint64_t)n,
6282 0 col_types[c]);
6283 0 ▶ if (ok) enc_split_chunk(ec, &x->tmp, n);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) enc_split_chunk(ec, &x->tmp, n); 0 0 0
6284 }
6285 12 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 8 4 12
6286 8 fail = c; fail_r0 = r0; fail_n = n;
6287 }
6288 4 ▶ else if (r0 == 0 && n < nrows
Branch: 2 branches
BranchTrueFalseTotal
else if (r0 == 0 && n < nrows 4 0 4
else if (r0 == 0 && n < nrows 0 4 4
6289 0 ▶ && !(ec->mode == ENC_PLAIN && col_type_fixed_size(col_types[c])))
Branch: 2 branches
BranchTrueFalseTotal
&& !(ec->mode == ENC_PLAIN && col_type_fixed_size(col_types[c]))) 0 0 0
&& !(ec->mode == ENC_PLAIN && col_type_fixed_size(col_types[c]))) 0 0 0
6290 0 nbuf_reserve(&ec->data,
6291 0 (size_t)((double)ec->data.len / n * nrows * 1.1) + 64);
6292 }
6293 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 12 12
6294 }
6295 16 ▶ for (c = 0; c < fail; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < fail; c++) { 4 12 16
6296 4 ▶ if (x->cols[c].mode != ENC_WHOLE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (x->cols[c].mode != ENC_WHOLE) continue; 4 0 4
6297 0 ENTER; SAVETMPS;
6298 0 ▶ snap_gather(sn, 0, nrows, c, cdefs ? &cdefs[c] : NULL, x->vals);
Branch: 1 branch
BranchTrueFalseTotal
snap_gather(sn, 0, nrows, c, cdefs ? &cdefs[c] : NULL, x->vals); 0 0 0
6299 0 prog->col = c; prog->r0 = 0; prog->n = nrows;
6300 0 ▶ if (!encode_column_sv(aTHX_ &x->cols[c].data, x->vals,
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_column_sv(aTHX_ &x->cols[c].data, x->vals, 0 0 0
6301 0 (uint64_t)nrows, col_types[c])) {
6302 0 fail = c; fail_r0 = 0; fail_n = nrows;
6303 }
6304 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
6305 }
6306 12 ▶ if (fail < num_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (fail < num_cols) { 8 4 12
6307 char where[160];
6308 8 prog->col = fail; prog->r0 = fail_r0; prog->n = fail_n;
6309 8 err_where(where, sizeof(where), col_names[fail], col_name_lens[fail],
6310 8 col_types_str[fail], col_type_lens[fail]);
6311 8 ▶ snap_gather(sn, fail_r0, fail_n, fail, cdefs ? &cdefs[fail] : NULL,
Branch: 1 branch
BranchTrueFalseTotal
snap_gather(sn, fail_r0, fail_n, fail, cdefs ? &cdefs[fail] : NULL, 0 8 8
6312 x->vals);
6313 8 describe_sv_failure(aTHX_ errbuf, errbuf_sz, where, col_types[fail],
6314 x->vals, fail_n, fail_r0, "row");
6315 8 *out_len = (size_t)-1; /* sentinel: encode failure */
6316 8 LEAVE;
6317 8 return NULL;
6318 }
6319 4 prog->col = -1;
6320  
6321 4 need = 64;
6322 8 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 4 4 8
6323 4 need += col_name_lens[c] + col_type_lens[c] + 64
6324 4 + x->cols[c].data.len + x->cols[c].aux.len;
6325 4 ▶ if (x->cols[c].mode == ENC_LC) need += 24 + 4 * (size_t)nrows;
Branch: 1 branch
BranchTrueFalseTotal
if (x->cols[c].mode == ENC_LC) need += 24 + 4 * (size_t)nrows; 0 4 4
6326 }
6327 4 nbuf_init(&x->out);
6328 4 nbuf_reserve(&x->out, need);
6329 4 ▶ if (!self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->compress) { 4 0 4
6330 4 nbuf_varuint(&x->out, CLIENT_DATA);
6331 4 nbuf_cstring(&x->out, ""); /* table name */
6332 }
6333 4 nbuf_block_info(&x->out);
6334 4 nbuf_varuint(&x->out, (uint64_t)num_cols);
6335 4 nbuf_varuint(&x->out, (uint64_t)nrows);
6336 8 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 4 4 8
6337 4 enc_col_t *ec = &x->cols[c];
6338 4 nbuf_string(&x->out, col_names[c], col_name_lens[c]);
6339 4 nbuf_string(&x->out, col_types_str[c], col_type_lens[c]);
6340 4 nbuf_u8(&x->out, 0); /* has_custom_serialization = false */
6341 4 write_col_prefix(&x->out, col_types[c]);
6342 4 ▶ if (ec->mode == ENC_LC) {
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_LC) { 0 4 4
6343 size_t idx_size;
6344 0 lc_write_keys_header(&x->out, (uint64_t)nrows, &idx_size);
6345 0 nbuf_append(&x->out, ec->data.data, ec->data.len);
6346 0 lc_write_indices(&x->out, (uint64_t)nrows, idx_size);
6347 } else {
6348 4 nbuf_append(&x->out, ec->aux.data, ec->aux.len);
6349 4 nbuf_append(&x->out, ec->data.data, ec->data.len);
6350 }
6351 4 Safefree(ec->data.data); ec->data.data = NULL;
6352 4 Safefree(ec->aux.data); ec->aux.data = NULL;
6353 }
6354  
6355 4 ▶ if (!self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->compress) { 4 0 4
6356 4 nbuf_empty_data_block(&x->out, 0);
6357 4 pkt = x->out.data;
6358 4 *out_len = x->out.len;
6359 } else {
6360 0 pkt = wrap_data_block(self, &x->out, out_len);
6361 }
6362 4 x->out.data = NULL;
6363 4 LEAVE;
6364 4 return pkt;
6365 }
6366  
6367 typedef struct {
6368 ev_clickhouse_t *self;
6369 SV *snap;
6370 const char **names; size_t *name_lens;
6371 const char **types_str; size_t *type_lens;
6372 col_type_t **types;
6373 int ncols;
6374 col_default_t *cdefs;
6375 enc_progress_t prog;
6376 size_t *out_len;
6377 char *errbuf; size_t errbuf_sz;
6378 char *pkt;
6379 } av_build_t;
6380  
6381 12 static void av_build_fn(pTHX_ void *p) {
6382 12 av_build_t *a = (av_build_t *)p;
6383 12 a->pkt = build_native_insert_data_from_av(aTHX_ a->self, a->snap,
6384 a->names, a->name_lens, a->types_str, a->type_lens, a->types,
6385 a->ncols, a->cdefs, &a->prog, a->out_len, a->errbuf, a->errbuf_sz);
6386 12 }
6387  
6388 typedef struct {
6389 SV *v;
6390 col_type_t *ct;
6391 native_buf_t *scratch;
6392 } encode_one_t;
6393  
6394 0 static void encode_one_fn(pTHX_ void *p) {
6395 0 encode_one_t *a = (encode_one_t *)p;
6396 0 (void)sv_encodes(aTHX_ a->v, a->ct, a->scratch);
6397 0 }
6398  
6399 /* build_native_insert_data_from_av() with exceptions from user code
6400 * turned into an encode failure naming the column and row that died. */
6401 12 static char* build_native_insert_data_from_av_guarded(pTHX_ ev_clickhouse_t *self,
6402 SV *snap_sv,
6403 const char **col_names, size_t *col_name_lens,
6404 const char **col_types_str, size_t *col_type_lens,
6405 col_type_t **col_types,
6406 int num_cols,
6407 col_default_t *cdefs,
6408 size_t *out_len,
6409 char *errbuf, size_t errbuf_sz) {
6410 av_build_t a;
6411 12 SV *err = NULL;
6412 native_buf_t msg;
6413 int c;
6414  
6415 12 a.self = self; a.snap = snap_sv;
6416 12 a.names = col_names; a.name_lens = col_name_lens;
6417 12 a.types_str = col_types_str; a.type_lens = col_type_lens;
6418 12 a.types = col_types; a.ncols = num_cols; a.cdefs = cdefs;
6419 12 a.prog.col = -1; a.prog.r0 = 0; a.prog.n = 0;
6420 12 a.out_len = out_len; a.errbuf = errbuf; a.errbuf_sz = errbuf_sz;
6421 12 a.pkt = NULL;
6422 12 ▶ if (run_guarded(aTHX_ av_build_fn, &a, &err))
Branch: 1 branch
BranchTrueFalseTotal
if (run_guarded(aTHX_ av_build_fn, &a, &err)) 12 0 12
6423 12 return a.pkt;
6424  
6425 0 nbuf_init(&msg);
6426 0 c = a.prog.col;
6427 0 ▶ if (c >= 0 && c < num_cols) {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0 && c < num_cols) { 0 0 0
if (c >= 0 && c < num_cols) { 0 0 0
6428 0 insert_snap_t *sn = insert_snap_get(aTHX_ snap_sv);
6429 char where[160];
6430 0 SSize_t k, row = -1;
6431 0 err_where(where, sizeof(where), col_names[c], col_name_lens[c],
6432 0 col_types_str[c], col_type_lens[c]);
6433 0 err_append(&msg, where);
6434 0 ▶ if (sn && a.prog.n > 0 && a.prog.r0 + a.prog.n <= sn->nrows) {
Branch: 3 branches
BranchTrueFalseTotal
if (sn && a.prog.n > 0 && a.prog.r0 + a.prog.n <= sn->nrows) { 0 0 0
if (sn && a.prog.n > 0 && a.prog.r0 + a.prog.n <= sn->nrows) { 0 0 0
if (sn && a.prog.n > 0 && a.prog.r0 + a.prog.n <= sn->nrows) { 0 0 0
6435 SV **vals;
6436 native_buf_t scratch;
6437 encode_one_t one;
6438 0 ▶ Newx(vals, a.prog.n, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(vals, a.prog.n, SV *); 0 0 0
6439 0 nbuf_init(&scratch);
6440 0 ▶ snap_gather(sn, a.prog.r0, a.prog.n, c,
Branch: 1 branch
BranchTrueFalseTotal
snap_gather(sn, a.prog.r0, a.prog.n, c, 0 0 0
6441 0 cdefs ? &cdefs[c] : NULL, vals);
6442 0 one.ct = col_types[c];
6443 0 one.scratch = &scratch;
6444 0 ▶ for (k = 0; k < a.prog.n && row < 0; k++) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < a.prog.n && row < 0; k++) { 0 0 0
for (k = 0; k < a.prog.n && row < 0; k++) { 0 0 0
6445 0 one.v = vals[k];
6446 0 ▶ if (!run_guarded(aTHX_ encode_one_fn, &one, NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (!run_guarded(aTHX_ encode_one_fn, &one, NULL)) 0 0 0
6447 0 row = a.prog.r0 + k;
6448 }
6449 0 Safefree(scratch.data);
6450 0 Safefree(vals);
6451 }
6452 0 ▶ if (row >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (row >= 0) { 0 0 0
6453 char rb[48];
6454 0 snprintf(rb, sizeof(rb), ", row %ld", (long)row);
6455 0 err_append(&msg, rb);
6456 }
6457 0 err_append(&msg, ": ");
6458 }
6459 0 err_exception(aTHX_ &msg, err);
6460 0 ▶ if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) SvREFCNT_dec(err); 0 0 0
6461 0 err_finish(errbuf, errbuf_sz, &msg);
6462 0 *out_len = (size_t)-1; /* sentinel: encode failure */
6463 0 return NULL;
6464 }
6465  
6466 /*
6467 * Build the external-table Data packets for a native query. `external`
6468 * maps table-name => { structure => [name => type, ...], data => [[..],..] }.
6469 * Each entry becomes one CLIENT_DATA packet (named block, optionally LZ4)
6470 * to be spliced into the query stream before the terminating empty block.
6471 *
6472 * Returns a malloc'd buffer of the concatenated packets with *out_len set
6473 * (an empty 0-length buffer when `external` has no tables). On error
6474 * returns NULL and writes a message into errbuf. The caller must
6475 * Safefree the returned buffer.
6476 */
6477 0 static char* build_external_tables(pTHX_ ev_clickhouse_t *self, HV *external,
6478 const char *tz, size_t *out_len,
6479 char *errbuf, size_t errbuf_sz) {
6480 native_buf_t out;
6481 HE *he;
6482  
6483 0 *out_len = 0;
6484 0 nbuf_init(&out);
6485 0 hv_iterinit(external);
6486  
6487 0 ▶ while ((he = hv_iternext(external))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(external))) { 0 0 0
6488 I32 tnlen;
6489 0 const char *tname = hv_iterkey(he, &tnlen);
6490 0 SV *spec_sv = hv_iterval(external, he);
6491 HV *spec;
6492 SV **st, **dt;
6493 AV *structure, *rows;
6494 SSize_t sn, nrows, row;
6495 int ncols, col;
6496 native_buf_t body;
6497 0 body.data = NULL;
6498  
6499 0 ▶ if (!SvROK(spec_sv) || SvTYPE(SvRV(spec_sv)) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(spec_sv) || SvTYPE(SvRV(spec_sv)) != SVt_PVHV) { 0 0 0
if (!SvROK(spec_sv) || SvTYPE(SvRV(spec_sv)) != SVt_PVHV) { 0 0 0
6500 0 snprintf(errbuf, errbuf_sz, "external table '%s': spec must be a "
6501 "hashref { structure => [...], data => [...] }", tname);
6502 0 goto fail;
6503 }
6504 0 spec = (HV *)SvRV(spec_sv);
6505 0 st = hv_fetchs(spec, "structure", 0);
6506 0 dt = hv_fetchs(spec, "data", 0);
6507 0 ▶ if (!st || !SvROK(*st) || SvTYPE(SvRV(*st)) != SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!st || !SvROK(*st) || SvTYPE(SvRV(*st)) != SVt_PVAV) { 0 0 0
if (!st || !SvROK(*st) || SvTYPE(SvRV(*st)) != SVt_PVAV) { 0 0 0
if (!st || !SvROK(*st) || SvTYPE(SvRV(*st)) != SVt_PVAV) { 0 0 0
6508 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'structure' must "
6509 "be an arrayref of name => type pairs", tname);
6510 0 goto fail;
6511 }
6512 0 ▶ if (!dt || !SvROK(*dt) || SvTYPE(SvRV(*dt)) != SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!dt || !SvROK(*dt) || SvTYPE(SvRV(*dt)) != SVt_PVAV) { 0 0 0
if (!dt || !SvROK(*dt) || SvTYPE(SvRV(*dt)) != SVt_PVAV) { 0 0 0
if (!dt || !SvROK(*dt) || SvTYPE(SvRV(*dt)) != SVt_PVAV) { 0 0 0
6513 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'data' must be "
6514 "an arrayref of rows", tname);
6515 0 goto fail;
6516 }
6517 0 structure = (AV *)SvRV(*st);
6518 0 rows = (AV *)SvRV(*dt);
6519 0 sn = av_len(structure) + 1;
6520 0 ▶ if (sn < 2 || (sn & 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sn < 2 || (sn & 1)) { 0 0 0
if (sn < 2 || (sn & 1)) { 0 0 0
6521 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'structure' needs "
6522 "a non-empty, even list of name => type pairs", tname);
6523 0 goto fail;
6524 }
6525 0 ncols = (int)(sn / 2);
6526 0 nrows = av_len(rows) + 1;
6527 0 ▶ if (nrows < 0) nrows = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows < 0) nrows = 0; 0 0 0
6528  
6529 /* block body: block info + num_columns + num_rows + columns */
6530 0 nbuf_init(&body);
6531 0 nbuf_block_info(&body);
6532 0 nbuf_varuint(&body, (uint64_t)ncols);
6533 0 nbuf_varuint(&body, (uint64_t)nrows);
6534  
6535 0 ▶ for (col = 0; col < ncols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < ncols; col++) { 0 0 0
6536 0 SV **nm = av_fetch(structure, col * 2, 0);
6537 0 SV **ty = av_fetch(structure, col * 2 + 1, 0);
6538 STRLEN cnl, ctl;
6539 const char *cname, *ctype;
6540 col_type_t *ct;
6541 SV **col_vals;
6542 int enc;
6543  
6544 0 ▶ if (!nm || !ty || !SvOK(*nm) || !SvOK(*ty)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!nm || !ty || !SvOK(*nm) || !SvOK(*ty)) { 0 0 0
if (!nm || !ty || !SvOK(*nm) || !SvOK(*ty)) { 0 0 0
if (!nm || !ty || !SvOK(*nm) || !SvOK(*ty)) { 0 0 0
if (!nm || !ty || !SvOK(*nm) || !SvOK(*ty)) { 0 0 0
6545 0 snprintf(errbuf, errbuf_sz, "external table '%s': structure "
6546 "name/type entries must be defined", tname);
6547 0 Safefree(body.data);
6548 0 goto fail;
6549 }
6550 0 cname = SvPV(*nm, cnl);
6551 0 ▶ ctype = SvPV(*ty, ctl);
Branch: 1 branch
BranchTrueFalseTotal
ctype = SvPV(*ty, ctl); 0 0 0
6552 0 nbuf_string(&body, cname, cnl);
6553 0 nbuf_string(&body, ctype, ctl);
6554 0 nbuf_u8(&body, 0); /* has_custom_serialization = false */
6555  
6556 0 ct = parse_col_type(ctype, ctl);
6557 0 col_type_default_tz(ct, tz);
6558  
6559 /* gather this column's values from the row-major data */
6560 0 ▶ Newx(col_vals, nrows > 0 ? nrows : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newx(col_vals, nrows > 0 ? nrows : 1, SV *); 0 0 0
Newx(col_vals, nrows > 0 ? nrows : 1, SV *); 0 0 0
Newx(col_vals, nrows > 0 ? nrows : 1, SV *); 0 0 0
6561 0 ▶ for (row = 0; row < nrows; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < nrows; row++) { 0 0 0
6562 0 SV **rsv = av_fetch(rows, row, 0);
6563 AV *rav;
6564 SV **vsv;
6565 0 ▶ if (!rsv || !SvROK(*rsv) || SvTYPE(SvRV(*rsv)) != SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!rsv || !SvROK(*rsv) || SvTYPE(SvRV(*rsv)) != SVt_PVAV) { 0 0 0
if (!rsv || !SvROK(*rsv) || SvTYPE(SvRV(*rsv)) != SVt_PVAV) { 0 0 0
if (!rsv || !SvROK(*rsv) || SvTYPE(SvRV(*rsv)) != SVt_PVAV) { 0 0 0
6566 0 snprintf(errbuf, errbuf_sz, "external table '%s': each data "
6567 "row must be an arrayref", tname);
6568 0 Safefree(col_vals);
6569 0 free_col_type(ct);
6570 0 Safefree(body.data);
6571 0 goto fail;
6572 }
6573 0 rav = (AV *)SvRV(*rsv);
6574 0 vsv = av_fetch(rav, col, 0);
6575 0 ▶ col_vals[row] = (vsv && *vsv) ? *vsv : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
col_vals[row] = (vsv && *vsv) ? *vsv : &PL_sv_undef; 0 0 0
col_vals[row] = (vsv && *vsv) ? *vsv : &PL_sv_undef; 0 0 0
6576 }
6577  
6578 0 enc = 1;
6579 0 ▶ if (nrows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows) { 0 0 0
6580 0 write_col_prefix(&body, ct);
6581 0 enc = encode_column_sv(aTHX_ &body, col_vals,
6582 (uint64_t)nrows, ct);
6583 }
6584 0 ▶ if (!enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) { 0 0 0
6585 char where[160], what[512];
6586 0 err_where(where, sizeof(where), cname, cnl, ctype, ctl);
6587 0 describe_sv_failure(aTHX_ what, sizeof(what), where, ct,
6588 col_vals, nrows, 0, "row");
6589 0 snprintf(errbuf, errbuf_sz, "external table '%s': %s",
6590 tname, what);
6591 }
6592 0 Safefree(col_vals);
6593 0 free_col_type(ct);
6594 0 ▶ if (!enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) { 0 0 0
6595 0 Safefree(body.data);
6596 0 goto fail;
6597 }
6598 }
6599  
6600 /* [CLIENT_DATA] [table name] [block — optionally LZ4-compressed] */
6601 0 nbuf_varuint(&out, CLIENT_DATA);
6602 0 nbuf_string(&out, tname, (size_t)tnlen);
6603 #ifdef HAVE_LZ4
6604 if (self->compress) {
6605 char *comp;
6606 size_t comp_len;
6607 comp = ch_lz4_compress(body.data, body.len, &comp_len);
6608 Safefree(body.data);
6609 body.data = NULL;
6610 if (!comp) {
6611 snprintf(errbuf, errbuf_sz, "external table '%s': LZ4 "
6612 "compression failed", tname);
6613 goto fail;
6614 }
6615 nbuf_append(&out, comp, comp_len);
6616 Safefree(comp);
6617 } else
6618 #endif
6619 {
6620 0 nbuf_append(&out, body.data, body.len);
6621 0 Safefree(body.data);
6622 0 body.data = NULL;
6623 }
6624 }
6625  
6626 0 *out_len = out.len;
6627 0 return out.data;
6628  
6629 0 fail:
6630 0 Safefree(out.data);
6631 0 *out_len = 0;
6632 0 return NULL;
6633 }
6634  
« blib/lib/EV/ClickHouse.pm ↑ Summary xs/proto_native_parse.c »