xs/types.c

filter: Statementstmt 9.2% 414 / 4481 Branchbran 4.7% 223 / 4688 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 6.9% 637 / 9169 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/lz4.c ↑ Summary xs/tzif.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 ch_tz_t **zones; /* zone cache to load tz from (NULL: process cache) */
348 const ch_tz_t *zone; /* tz once loaded */
349 int zone_state; /* 0 not loaded yet, 1 loaded, -1 failed */
350 };
351  
352 static ch_tz_t *ch_tz_process_zones;
353  
354 /* The column's zone, loaded on first use; NULL for UTC and on failure
355 * (zone_state -1, see col_type_zone_error). */
356 2 static const ch_tz_t *col_zone(col_type_t *t) {
357 2 ▶ if (!t->tz) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t->tz) return NULL; 0 2 2
358 2 ▶ if (!t->zone_state) {
Branch: 1 branch
BranchTrueFalseTotal
if (!t->zone_state) { 2 0 2
359 char err[8];
360 2 t->zone = ch_tz_load(t->zones ? t->zones : &ch_tz_process_zones,
361 2 ▶ t->tz, err, sizeof(err));
Branch: 1 branch
BranchTrueFalseTotal
t->tz, err, sizeof(err)); 2 0 2
362 2 ▶ t->zone_state = t->zone ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
t->zone_state = t->zone ? 1 : -1; 2 0 2
363 }
364 2 return t->zone;
365 }
366  
367 /* The tz of the first node whose zone failed to load, else NULL. */
368 16 static const char *col_type_zone_error(const col_type_t *t) {
369 int i;
370 const char *e;
371 16 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 8 8 16
372 8 ▶ if (t->zone_state < 0) return t->tz;
Branch: 1 branch
BranchTrueFalseTotal
if (t->zone_state < 0) return t->tz; 0 8 8
373 8 ▶ if ((e = col_type_zone_error(t->inner))) return e;
Branch: 1 branch
BranchTrueFalseTotal
if ((e = col_type_zone_error(t->inner))) return e; 0 8 8
374 8 ▶ for (i = 0; i < t->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->num_inners; i++) 0 8 8
375 0 ▶ if ((e = col_type_zone_error(t->inners[i]))) return e;
Branch: 1 branch
BranchTrueFalseTotal
if ((e = col_type_zone_error(t->inners[i]))) return e; 0 0 0
376 8 return NULL;
377 }
378  
379 14 static void free_col_type(col_type_t *t) {
380 int i;
381 14 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 14 14
382 14 ▶ if (t->inner) free_col_type(t->inner);
Branch: 1 branch
BranchTrueFalseTotal
if (t->inner) free_col_type(t->inner); 0 14 14
383 14 ▶ if (t->inners) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->inners) { 0 14 14
384 0 ▶ for (i = 0; i < t->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->num_inners; i++) 0 0 0
385 0 free_col_type(t->inners[i]);
386 0 Safefree(t->inners);
387 }
388 14 ▶ if (t->inner_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->inner_names) { 0 14 14
389 0 ▶ for (i = 0; i < t->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->num_inners; i++) 0 0 0
390 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
391 0 Safefree(t->inner_names);
392 }
393 14 ▶ if (t->type_str) Safefree(t->type_str);
Branch: 1 branch
BranchTrueFalseTotal
if (t->type_str) Safefree(t->type_str); 0 14 14
394 14 ▶ if (t->tz) Safefree(t->tz);
Branch: 1 branch
BranchTrueFalseTotal
if (t->tz) Safefree(t->tz); 10 4 14
395 14 Safefree(t);
396 }
397  
398 /* Max nesting depth for parse_col_type recursion (hostile type strings
399 * like "Array(Array(Array(..." are bounded only by the block size). */
400 #define PARSE_COL_TYPE_MAX_DEPTH 100
401  
402 static col_type_t* parse_col_type_depth(const char *type, size_t len, int depth);
403 static col_type_t* parse_col_type(const char *type, size_t len);
404  
405 /*
406 * Parse comma-separated type list inside Tuple(...) or Map(...).
407 * Handles nested parentheses correctly.
408 * Sets t->inners and t->num_inners.
409 */
410 /* Index of the quote closing the '...' literal opened at s[i] (an Enum
411 * label may hold parens and commas), or len if unterminated. */
412 0 static size_t skip_quoted(const char *s, size_t len, size_t i) {
413 0 ▶ for (i++; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i++; i < len; i++) { 0 0 0
414 0 ▶ if (s[i] == '\\') i++;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '\\') i++; 0 0 0
415 0 ▶ else if (s[i] == '\'') return i;
Branch: 1 branch
BranchTrueFalseTotal
else if (s[i] == '\'') return i; 0 0 0
416 }
417 0 return len;
418 }
419  
420 0 static void parse_type_list(col_type_t *t, const char *inner, size_t inner_len, int rec_depth) {
421 0 int depth = 0, count = 0;
422 0 size_t i, start = 0;
423 int in_bt;
424  
425 0 ▶ if (inner_len == 0) return; /* Tuple() — zero inners */
Branch: 1 branch
BranchTrueFalseTotal
if (inner_len == 0) return; /* Tuple() — zero inners */ 0 0 0
426  
427 /* Count elements; `quoted names` and 'labels' shield parens/commas */
428 0 in_bt = 0;
429 0 ▶ for (i = 0; i <= inner_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= inner_len; i++) { 0 0 0
430 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
431 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
432 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
433 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
434 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
435 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
436 0 count++;
437 }
438  
439 0 Newxz(t->inners, count, col_type_t*);
440 0 t->num_inners = count;
441  
442 /* Parse each element */
443 0 count = 0;
444 0 depth = 0;
445 0 in_bt = 0;
446 0 start = 0;
447 0 ▶ for (i = 0; i <= inner_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= inner_len; i++) { 0 0 0
448 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
449 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
450 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
451 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
452 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
453 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
454 0 size_t s = start, e = i;
455 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
456 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
457 /* Strip named tuple field prefix: "name Type" -> "Type".
458 * The split is the first space outside `quotes`/parens. */
459 {
460 size_t sp;
461 0 int q = 0;
462 0 ▶ for (sp = s; sp < e; sp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sp = s; sp < e; sp++) { 0 0 0
463 0 ▶ if (inner[sp] == '`') q = !q;
Branch: 1 branch
BranchTrueFalseTotal
if (inner[sp] == '`') q = !q; 0 0 0
464 0 ▶ else if (q) ;
Branch: 1 branch
BranchTrueFalseTotal
else if (q) ; 0 0 0
465 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
466 0 ▶ else if (inner[sp] == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
else if (inner[sp] == ' ') { 0 0 0
467 0 s = sp + 1;
468 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
469 0 break;
470 }
471 }
472 }
473 0 t->inners[count++] = parse_col_type_depth(inner + s, e - s, rec_depth + 1);
474 0 start = i + 1;
475 }
476 }
477 }
478  
479 14 static col_type_t* parse_col_type_depth(const char *type, size_t len, int depth) {
480 col_type_t *t;
481 14 ▶ if (depth > PARSE_COL_TYPE_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth > PARSE_COL_TYPE_MAX_DEPTH) { 0 14 14
482 /* Fail the way an unparsable type fails: CT_UNKNOWN reads as String */
483 0 Newxz(t, 1, col_type_t);
484 0 t->code = CT_UNKNOWN;
485 0 return t;
486 }
487 14 Newxz(t, 1, col_type_t);
488  
489 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
490 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
491 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
492 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
493 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
494 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
495 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
496 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
497 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
498 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
499 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
500 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
501 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
502 0 t->code = CT_FIXEDSTRING;
503 0 t->param = atoi(type + 12);
504 /* Clamp hostile negatives: param feeds (size_t) casts */
505 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
506 }
507 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
508 0 t->code = CT_ARRAY;
509 0 t->inner = parse_col_type_depth(type + 6, len - 7, depth + 1);
510 }
511 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
512 0 t->code = CT_NULLABLE;
513 0 t->inner = parse_col_type_depth(type + 9, len - 10, depth + 1);
514 }
515 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
516 0 t->code = CT_LOWCARDINALITY;
517 0 t->inner = parse_col_type_depth(type + 15, len - 16, depth + 1);
518 }
519 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
520 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
521 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
522 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
523 0 t->code = CT_DATETIME;
524 /* DateTime('timezone') — extract timezone */
525 0 {
526 0 const char *q = memchr(type + 9, '\'', len - 9);
527 0 ▶ if (q) {
Branch: 1 branch
BranchTrueFalseTotal
if (q) { 0 0 0
528 0 const char *qe = memchr(q + 1, '\'', type + len - q - 1);
529 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
530 0 size_t tzlen = qe - q - 1;
531 0 Newx(t->tz, tzlen + 1, char);
532 0 Copy(q + 1, t->tz, tzlen, char);
533 0 t->tz[tzlen] = '\0';
534 }
535 }
536 }
537 }
538 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
539 10 t->code = CT_DATETIME64;
540 10 t->param = atoi(type + 11);
541 /* Clamp hostile precision (drives a per-row scale loop) */
542 10 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 10 10
543 10 ▶ if (t->param > 9) t->param = 9;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 9) t->param = 9; 0 10 10
544 /* DateTime64(N, 'timezone') — extract timezone */
545 10 {
546 10 const char *comma = memchr(type + 11, ',', len - 11);
547 10 ▶ if (comma) {
Branch: 1 branch
BranchTrueFalseTotal
if (comma) { 0 10 10
548 0 const char *q = memchr(comma, '\'', type + len - comma);
549 0 ▶ if (q) {
Branch: 1 branch
BranchTrueFalseTotal
if (q) { 0 0 0
550 0 const char *qe = memchr(q + 1, '\'', type + len - q - 1);
551 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
552 0 size_t tzlen = qe - q - 1;
553 0 Newx(t->tz, tzlen + 1, char);
554 0 Copy(q + 1, t->tz, tzlen, char);
555 0 t->tz[tzlen] = '\0';
556 }
557 }
558 }
559 }
560 }
561 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
562 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
563 0 t->code = CT_TIME64;
564 0 t->param = atoi(type + 7);
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 > 9) t->param = 9;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 9) t->param = 9; 0 0 0
567 }
568 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
569 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
570 0 t->code = CT_ENUM8;
571 0 Newx(t->type_str, len + 1, char);
572 0 Copy(type, t->type_str, len, char);
573 0 t->type_str[len] = '\0';
574 0 t->type_str_len = len;
575 }
576 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
577 0 t->code = CT_ENUM16;
578 0 Newx(t->type_str, len + 1, char);
579 0 Copy(type, t->type_str, len, char);
580 0 t->type_str[len] = '\0';
581 0 t->type_str_len = len;
582 }
583 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
584 0 t->code = CT_DECIMAL32;
585 0 t->param = atoi(type + 10);
586 0 t->precision = 9;
587 /* Clamp hostile scale (drives per-row pow10 loops) */
588 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
589 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
590 }
591 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
592 0 t->code = CT_DECIMAL64;
593 0 t->param = atoi(type + 10);
594 0 t->precision = 18;
595 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
596 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
597 }
598 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
599 0 t->code = CT_DECIMAL128;
600 0 t->param = atoi(type + 11);
601 0 t->precision = 38;
602 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
603 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
604 }
605 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
606 0 t->code = CT_DECIMAL256;
607 0 t->param = atoi(type + 11);
608 0 t->precision = 76;
609 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
610 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
611 }
612 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
613 0 int precision = atoi(type + 8);
614 0 const char *comma = memchr(type + 8, ',', len - 8);
615 0 ▶ t->param = comma ? atoi(comma + 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
t->param = comma ? atoi(comma + 1) : 0; 0 0 0
616 0 ▶ if (t->param < 0) t->param = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param < 0) t->param = 0; 0 0 0
617 0 ▶ if (t->param > 76) t->param = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 76) t->param = 76; 0 0 0
618 0 ▶ if (precision < 1) precision = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (precision < 1) precision = 1; 0 0 0
619 0 ▶ if (precision > 76) precision = 76;
Branch: 1 branch
BranchTrueFalseTotal
if (precision > 76) precision = 76; 0 0 0
620 0 t->precision = precision;
621 0 ▶ if (precision <= 9) t->code = CT_DECIMAL32;
Branch: 1 branch
BranchTrueFalseTotal
if (precision <= 9) t->code = CT_DECIMAL32; 0 0 0
622 0 ▶ else if (precision <= 18) t->code = CT_DECIMAL64;
Branch: 1 branch
BranchTrueFalseTotal
else if (precision <= 18) t->code = CT_DECIMAL64; 0 0 0
623 0 ▶ else if (precision <= 38) t->code = CT_DECIMAL128;
Branch: 1 branch
BranchTrueFalseTotal
else if (precision <= 38) t->code = CT_DECIMAL128; 0 0 0
624 0 else t->code = CT_DECIMAL256;
625 }
626 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
627 /* Variant(...) and Dynamic: recognised here so the schema parser
628 * doesn't choke. Wire-format decoding is NOT generic — selecting
629 * one produces a clean decode error from decode_column so the
630 * caller's response parsing doesn't desync. Use
631 * `SELECT toString(col) FROM …` server-side to read the value as
632 * its JSON representation, or `CAST(col AS String)`. */
633 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
634 0 t->code = CT_VARIANT;
635 }
636 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
637 0 t->code = CT_DYNAMIC;
638 }
639 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
640 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
641 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
642 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
643 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
644 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
645 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
646 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
647 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
648 0 t->code = CT_TUPLE;
649 0 parse_type_list(t, type + 6, len - 7, depth);
650 }
651 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
652 0 t->code = CT_MAP;
653 0 parse_type_list(t, type + 4, len - 5, depth);
654 }
655 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
656 /* Nested(name1 Type1, name2 Type2) = Array(Tuple(Type1, Type2)) */
657 col_type_t *tuple;
658 0 Newxz(tuple, 1, col_type_t);
659 0 tuple->code = CT_TUPLE;
660 0 parse_type_list(tuple, type + 7, len - 8, depth);
661 0 t->code = CT_ARRAY;
662 0 t->inner = tuple;
663 }
664 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
665 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
666 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
667 /* ClickHouse stable JSON type (24.8+); Object('json') is the
668 * legacy spelling. JSON(name Type, ...) pins specific paths to
669 * concrete inner types ("typed paths"); those skip the
670 * Dynamic+Variant wrap and write as regular columns inline. */
671 0 t->code = CT_JSON;
672 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
673 /* parse "name Type, name Type, ..." */
674 0 const char *body = type + 5;
675 0 size_t blen = len - 6;
676 0 int idx = 0, pdepth = 0, in_bt = 0;
677 0 size_t start = 0, i;
678 typedef struct { size_t name_start, name_len, type_start, type_len; }
679 jp_bound_t;
680 jp_bound_t bounds[64];
681 0 ▶ for (i = 0; i <= blen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= blen; i++) { 0 0 0
682 0 ▶ char c = (i < blen) ? body[i] : ',';
Branch: 1 branch
BranchTrueFalseTotal
char c = (i < blen) ? body[i] : ','; 0 0 0
683 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
684 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
685 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
686 0 ▶ else if (c == '(') pdepth++;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '(') pdepth++; 0 0 0
687 0 ▶ else if (c == ')') pdepth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ')') pdepth--; 0 0 0
688 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
689 0 size_t ts = start, te = i, id;
690 0 int q = 0;
691 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
692 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
693 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
694 /* split on first blank outside `quotes` */
695 0 ▶ for (id = ts; id < te; id++) {
Branch: 1 branch
BranchTrueFalseTotal
for (id = ts; id < te; id++) { 0 0 0
696 0 ▶ if (body[id] == '`') q = !q;
Branch: 1 branch
BranchTrueFalseTotal
if (body[id] == '`') q = !q; 0 0 0
697 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
698 }
699 {
700 0 size_t ws = id;
701 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
702 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
703 /* strip `quoting` for name compare/store */
704 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
705 0 ts++;
706 0 id--;
707 }
708 0 bounds[idx].name_start = ts;
709 0 bounds[idx].name_len = id - ts;
710 0 bounds[idx].type_start = ws;
711 0 bounds[idx].type_len = te - ws;
712 0 idx++;
713 }
714 }
715 }
716 0 start = i + 1;
717 }
718 }
719 0 ▶ if (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx > 0) { 0 0 0
720 /* Sort by name (lex) — wire-order requirement. */
721 int j;
722 0 ▶ for (j = 1; j < idx; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < idx; j++) { 0 0 0
723 0 int z = j;
724 0 ▶ while (z > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (z > 0) { 0 0 0
725 0 size_t la = bounds[z-1].name_len, lb = bounds[z].name_len;
726 0 size_t m = la < lb ? la : lb;
727 0 int cmp = memcmp(body + bounds[z-1].name_start,
728 0 body + bounds[z].name_start, m);
729 0 ▶ if (cmp == 0) cmp = (int)la - (int)lb;
Branch: 1 branch
BranchTrueFalseTotal
if (cmp == 0) cmp = (int)la - (int)lb; 0 0 0
730 0 ▶ if (cmp <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (cmp <= 0) break; 0 0 0
731 0 jp_bound_t tmp = bounds[z-1];
732 0 bounds[z-1] = bounds[z];
733 0 bounds[z] = tmp;
734 0 z--;
735 }
736 }
737 0 t->num_inners = idx;
738 0 Newxz(t->inners, idx, col_type_t *);
739 0 Newxz(t->inner_names, idx, char *);
740 0 ▶ for (j = 0; j < idx; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < idx; j++) { 0 0 0
741 0 Newx(t->inner_names[j], bounds[j].name_len + 1, char);
742 0 memcpy(t->inner_names[j],
743 0 body + bounds[j].name_start, bounds[j].name_len);
744 0 t->inner_names[j][bounds[j].name_len] = '\0';
745 0 t->inners[j] = parse_col_type_depth(
746 0 body + bounds[j].type_start, bounds[j].type_len, depth + 1);
747 }
748 }
749 }
750 }
751 /* Geo type aliases (per ClickHouse docs / Encoder layout) */
752 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
753 /* Point = Tuple(Float64, Float64) */
754 0 t->code = CT_TUPLE;
755 0 parse_type_list(t, "Float64,Float64", 15, depth);
756 }
757 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
758 /* Ring = Array(Point) */
759 0 t->code = CT_ARRAY;
760 0 t->inner = parse_col_type_depth("Point", 5, depth + 1);
761 }
762 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
763 /* LineString = Array(Point) */
764 0 t->code = CT_ARRAY;
765 0 t->inner = parse_col_type_depth("Point", 5, depth + 1);
766 }
767 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
768 /* MultiLineString = Array(Array(Point)) */
769 0 t->code = CT_ARRAY;
770 0 t->inner = parse_col_type_depth("Array(Point)", 12, depth + 1);
771 }
772 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
773 /* Polygon = Array(Ring) */
774 0 t->code = CT_ARRAY;
775 0 t->inner = parse_col_type_depth("Ring", 4, depth + 1);
776 }
777 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
778 /* MultiPolygon = Array(Polygon) */
779 0 t->code = CT_ARRAY;
780 0 t->inner = parse_col_type_depth("Polygon", 7, depth + 1);
781 }
782 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
783 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
784 /* (Simple)AggregateFunction(func, Type...) — skip func, parse inner.
785 * For SAF this is exact. For full AggregateFunction the raw inner
786 * type matches simple aggregates (sum/min/max etc) but is wrong
787 * for complex states (quantile, uniqExact, ...) — for those, run
788 * finalizeAggregation(col) server-side and read the result. */
789 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
790 0 const char *inner = type + off;
791 0 size_t inner_len = len - off - 1;
792 size_t ci;
793 0 int pdepth = 0;
794 0 ▶ for (ci = 0; ci < inner_len; ci++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ci = 0; ci < inner_len; ci++) { 0 0 0
795 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
796 0 ▶ else if (inner[ci] == '(') pdepth++;
Branch: 1 branch
BranchTrueFalseTotal
else if (inner[ci] == '(') pdepth++; 0 0 0
797 0 ▶ else if (inner[ci] == ')') pdepth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (inner[ci] == ')') pdepth--; 0 0 0
798 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
799 }
800 0 ▶ if (ci < inner_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (ci < inner_len) { 0 0 0
801 0 ci++;
802 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
803 0 Safefree(t);
804 0 t = parse_col_type_depth(inner + ci, inner_len - ci, depth + 1);
805 } else {
806 0 t->code = CT_UNKNOWN;
807 }
808 }
809 else {
810 /* Unknown type — treat as String (read raw bytes) */
811 0 t->code = CT_UNKNOWN;
812 }
813  
814 14 return t;
815 }
816  
817 14 static col_type_t* parse_col_type(const char *type, size_t len) {
818 14 return parse_col_type_depth(type, len, 0);
819 }
820  
821 /* Structurally equal types; a timezone missing on either side matches
822 * any (it means the session's). */
823 0 static int col_type_same(const col_type_t *a, const col_type_t *b) {
824 int i;
825 0 ▶ if (!a || !b) return a == b;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) return a == b; 0 0 0
if (!a || !b) return a == b; 0 0 0
826 0 ▶ if (a->code != b->code || a->code == CT_UNKNOWN || a->param != b->param
Branch: 3 branches
BranchTrueFalseTotal
if (a->code != b->code || a->code == CT_UNKNOWN || a->param != b->param 0 0 0
if (a->code != b->code || a->code == CT_UNKNOWN || a->param != b->param 0 0 0
if (a->code != b->code || a->code == CT_UNKNOWN || a->param != b->param 0 0 0
827 0 ▶ || a->precision != b->precision || a->num_inners != b->num_inners)
Branch: 2 branches
BranchTrueFalseTotal
|| a->precision != b->precision || a->num_inners != b->num_inners) 0 0 0
|| a->precision != b->precision || a->num_inners != b->num_inners) 0 0 0
828 0 return 0;
829 0 ▶ if (a->tz && b->tz && strcmp(a->tz, b->tz)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (a->tz && b->tz && strcmp(a->tz, b->tz)) return 0; 0 0 0
if (a->tz && b->tz && strcmp(a->tz, b->tz)) return 0; 0 0 0
if (a->tz && b->tz && strcmp(a->tz, b->tz)) return 0; 0 0 0
830 0 ▶ if ((a->code == CT_ENUM8 || a->code == CT_ENUM16)
Branch: 2 branches
BranchTrueFalseTotal
if ((a->code == CT_ENUM8 || a->code == CT_ENUM16) 0 0 0
if ((a->code == CT_ENUM8 || a->code == CT_ENUM16) 0 0 0
831 0 ▶ && (a->type_str_len != b->type_str_len
Branch: 1 branch
BranchTrueFalseTotal
&& (a->type_str_len != b->type_str_len 0 0 0
832 0 ▶ || memcmp(a->type_str, b->type_str, a->type_str_len)))
Branch: 1 branch
BranchTrueFalseTotal
|| memcmp(a->type_str, b->type_str, a->type_str_len))) 0 0 0
833 0 return 0;
834 0 ▶ if (!col_type_same(a->inner, b->inner)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!col_type_same(a->inner, b->inner)) return 0; 0 0 0
835 0 ▶ for (i = 0; i < a->num_inners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->num_inners; i++) { 0 0 0
836 0 ▶ if (!col_type_same(a->inners[i], b->inners[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!col_type_same(a->inners[i], b->inners[i])) return 0; 0 0 0
837 0 ▶ if (a->inner_names && b->inner_names
Branch: 2 branches
BranchTrueFalseTotal
if (a->inner_names && b->inner_names 0 0 0
if (a->inner_names && b->inner_names 0 0 0
838 0 ▶ && strcmp(a->inner_names[i] ? a->inner_names[i] : "",
Branch: 2 branches
BranchTrueFalseTotal
&& strcmp(a->inner_names[i] ? a->inner_names[i] : "", 0 0 0
&& strcmp(a->inner_names[i] ? a->inner_names[i] : "", 0 0 0
839 0 ▶ b->inner_names[i] ? b->inner_names[i] : ""))
Branch: 1 branch
BranchTrueFalseTotal
b->inner_names[i] ? b->inner_names[i] : "")) 0 0 0
840 0 return 0;
841 }
842 0 return 1;
843 }
844  
845 /* Give tz-less DateTime/DateTime64 nodes the session's timezone (the
846 * server reads and renders their wall times in it), and load zones from
847 * the connection's cache. */
848 28 static void col_type_default_tz(col_type_t *t, const char *tz, ch_tz_t **zones) {
849 int i;
850 28 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 14 14 28
851 14 t->zones = zones;
852 14 ▶ if (tz && tz[0] && (t->code == CT_DATETIME || t->code == CT_DATETIME64)
Branch: 4 branches
BranchTrueFalseTotal
if (tz && tz[0] && (t->code == CT_DATETIME || t->code == CT_DATETIME64) 14 0 14
if (tz && tz[0] && (t->code == CT_DATETIME || t->code == CT_DATETIME64) 14 0 14
if (tz && tz[0] && (t->code == CT_DATETIME || t->code == CT_DATETIME64) 14 0 14
if (tz && tz[0] && (t->code == CT_DATETIME || t->code == CT_DATETIME64) 10 4 14
853 10 ▶ && !t->tz) {
Branch: 1 branch
BranchTrueFalseTotal
&& !t->tz) { 10 0 10
854 10 size_t n = strlen(tz);
855 10 Newx(t->tz, n + 1, char);
856 10 Copy(tz, t->tz, n + 1, char);
857 }
858 14 col_type_default_tz(t->inner, tz, zones);
859 14 ▶ for (i = 0; i < t->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->num_inners; i++) 0 14 14
860 0 col_type_default_tz(t->inners[i], tz, zones);
861 }
862  
863 98 static const char *hv_session_tz(pTHX_ HV *hv) {
864 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
865 98 ▶ if (!svp) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) return NULL; 98 0 98
866 0 SvGETMAGIC(*svp);
867 0 ▶ return SvOK(*svp) ? SvPV_nomg_nolen(*svp) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(*svp) ? SvPV_nomg_nolen(*svp) : NULL; 0 0 0
868 }
869  
870 /* The session_timezone a query is sent with: its own setting, else the
871 * connection's. Read once at enqueue, so later decoding never runs user
872 * code or sees the settings changed in flight. */
873 49 static char *query_session_tz(pTHX_ ev_clickhouse_t *self, HV *settings) {
874 49 const char *tz = hv_session_tz(aTHX_ settings);
875 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
876 49 ▶ return tz ? safe_strdup(tz) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return tz ? safe_strdup(tz) : NULL; 0 49 49
877 }
878  
879 /* "" restores the server default, as on the server. */
880 14 static const char *effective_tz(ev_clickhouse_t *self, const char *query_tz) {
881 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
882 }
883  
884 /* Size in bytes for fixed-width types. Returns 0 for variable-width. */
885 12 static size_t col_type_fixed_size(col_type_t *t) {
886 12 switch (t->code) {
887 0 case CT_INT8: case CT_UINT8: case CT_ENUM8: case CT_BOOL: return 1;
888 0 case CT_INT16: case CT_UINT16: case CT_ENUM16:
889 0 case CT_DATE: case CT_BFLOAT16: return 2;
890 2 case CT_INT32: case CT_UINT32: case CT_FLOAT32:
891 case CT_DECIMAL32: case CT_DATE32: case CT_DATETIME:
892 2 case CT_IPV4: case CT_TIME: return 4;
893 10 case CT_INT64: case CT_UINT64: case CT_FLOAT64:
894 10 case CT_DECIMAL64: case CT_DATETIME64: case CT_TIME64: return 8;
895 0 case CT_UUID: case CT_DECIMAL128:
896 0 case CT_INT128: case CT_UINT128: case CT_IPV6: return 16;
897 0 case CT_INT256: case CT_UINT256: case CT_DECIMAL256: return 32;
898 0 case CT_FIXEDSTRING: return (size_t)t->param;
899 0 default: return 0;
900 }
901 }
902  
903 /* --- Decode helper functions for opt-in type formatting --- */
904  
905 /* Proleptic Gregorian date of a day count since 1970-01-01, in int64
906 * (no time_t: 32-bit platforms would wrap past 2038). */
907 0 static void civil_from_days(int64_t days, int64_t *y, int *m, int *d) {
908 int64_t era, yy;
909 unsigned doe, yoe, doy, mp;
910 0 days += 719468;
911 0 ▶ era = (days >= 0 ? days : days - 146096) / 146097;
Branch: 1 branch
BranchTrueFalseTotal
era = (days >= 0 ? days : days - 146096) / 146097; 0 0 0
912 0 doe = (unsigned)(days - era * 146097);
913 0 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
914 0 yy = (int64_t)yoe + era * 400;
915 0 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
916 0 mp = (5 * doy + 2) / 153;
917 0 *d = (int)(doy - (153 * mp + 2) / 5 + 1);
918 0 ▶ *m = (int)(mp < 10 ? mp + 3 : mp - 9);
Branch: 1 branch
BranchTrueFalseTotal
*m = (int)(mp < 10 ? mp + 3 : mp - 9); 0 0 0
919 0 *y = yy + (*m <= 2);
920 0 }
921  
922 /* "YYYY-MM-DD HH:MM:SS" of seconds since the epoch, into buf; length. */
923 0 static int format_civil_secs(int64_t secs, char *buf, size_t sz) {
924 0 int64_t days = secs / 86400, rem = secs % 86400, y;
925 int m, d;
926 0 ▶ if (rem < 0) { rem += 86400; days--; }
Branch: 1 branch
BranchTrueFalseTotal
if (rem < 0) { rem += 86400; days--; } 0 0 0
927 0 civil_from_days(days, &y, &m, &d);
928 0 return snprintf(buf, sz, "%04lld-%02d-%02d %02d:%02d:%02d", (long long)y,
929 0 m, d, (int)(rem / 3600), (int)(rem / 60 % 60), (int)(rem % 60));
930 }
931  
932 /* Convert days since Unix epoch to "YYYY-MM-DD". */
933 0 static SV* days_to_date_sv(int32_t days) {
934 char buf[32];
935 int64_t y;
936 int m, d, n;
937 0 civil_from_days(days, &y, &m, &d);
938 0 n = snprintf(buf, sizeof(buf), "%04lld-%02d-%02d", (long long)y, m, d);
939 0 return newSVpvn(buf, n);
940 }
941  
942 /* Format a UNIX epoch as "YYYY-MM-DD HH:MM:SS" in zone (NULL = UTC). */
943 0 static SV* epoch_to_datetime_sv_ex(uint32_t epoch, const ch_tz_t *zone) {
944 char buf[48];
945 0 int n = format_civil_secs((int64_t)epoch + ch_tz_offset(zone, epoch),
946 buf, sizeof(buf));
947 0 return newSVpvn(buf, n);
948 }
949  
950 /* Convert DateTime64 to "YYYY-MM-DD HH:MM:SS.fff..." in zone (NULL = UTC) */
951 0 static SV* dt64_to_datetime_sv_ex(int64_t val, int precision, const ch_tz_t *zone) {
952 0 int64_t scale = 1;
953 int p;
954 int64_t epoch, frac;
955 char buf[64];
956 int n;
957  
958 0 ▶ for (p = 0; p < precision; p++) scale *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < precision; p++) scale *= 10; 0 0 0
959 0 epoch = val / scale;
960 0 frac = val % scale;
961 0 ▶ if (frac < 0) { epoch--; frac += scale; }
Branch: 1 branch
BranchTrueFalseTotal
if (frac < 0) { epoch--; frac += scale; } 0 0 0
962  
963 /* zone rules work in int years: beyond ~34000 years show UTC */
964 0 ▶ if (epoch > -((int64_t)1 << 40) && epoch < ((int64_t)1 << 40))
Branch: 2 branches
BranchTrueFalseTotal
if (epoch > -((int64_t)1 << 40) && epoch < ((int64_t)1 << 40)) 0 0 0
if (epoch > -((int64_t)1 << 40) && epoch < ((int64_t)1 << 40)) 0 0 0
965 0 epoch += ch_tz_offset(zone, epoch);
966 0 n = format_civil_secs(epoch, buf, 40);
967 0 ▶ if (precision > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (precision > 0) { 0 0 0
968 char fracbuf[24];
969 int fi;
970 0 snprintf(fracbuf, sizeof(fracbuf), "%0*lld", precision, (long long)frac);
971 0 buf[n++] = '.';
972 0 ▶ for (fi = 0; fi < precision && n < 63; fi++)
Branch: 2 branches
BranchTrueFalseTotal
for (fi = 0; fi < precision && n < 63; fi++) 0 0 0
for (fi = 0; fi < precision && n < 63; fi++) 0 0 0
973 0 buf[n++] = fracbuf[fi];
974 }
975 0 return newSVpvn(buf, n);
976 }
977  
978 /* Wall time given as seconds-as-if-UTC, read in the column's zone; *ok
979 * is 0 when that zone cannot be loaded. */
980 2 static int64_t wall_in_tz_to_epoch(int64_t utc_wall, col_type_t *ct, int *ok) {
981 2 const ch_tz_t *zone = col_zone(ct);
982 2 ▶ *ok = !(ct->tz && !zone);
Branch: 2 branches
BranchTrueFalseTotal
*ok = !(ct->tz && !zone); 2 0 2
*ok = !(ct->tz && !zone); 2 0 2
983 2 return ch_tz_wall_to_epoch(zone, utc_wall);
984 }
985  
986 /* Compute 10^n as double */
987 0 static double pow10_int(int n) {
988 0 double r = 1.0;
989 int i;
990 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
991 0 return r;
992 }
993  
994 /* Parse enum label for a given code from type string like "Enum8('a'=1,'b'=2)" */
995 0 static SV* enum_label_for_code(const char *type_str, size_t type_str_len, int code) {
996 /* Find the opening '(' */
997 0 const char *p = memchr(type_str, '(', type_str_len);
998 const char *end;
999 0 ▶ if (!p) return newSViv(code);
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return newSViv(code); 0 0 0
1000 0 p++;
1001 0 end = type_str + type_str_len - 1; /* skip closing ')' */
1002  
1003 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1004 /* Skip whitespace */
1005 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
1006 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
1007 0 p++; /* skip opening quote */
1008  
1009 /* Read label (handle escaped quotes) */
1010 {
1011 0 const char *label_start = p;
1012 size_t label_len;
1013 int val;
1014  
1015 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1016 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
1017 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
1018 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
1019 0 break;
1020 }
1021 0 p++;
1022 }
1023 0 label_len = p - label_start;
1024 0 ▶ if (p < end) p++; /* skip closing quote */
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; /* skip closing quote */ 0 0 0
1025  
1026 /* Skip ' = ' */
1027 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
1028  
1029 /* Read integer value */
1030 0 val = (int)strtol(p, NULL, 10);
1031  
1032 0 ▶ if (val == code) {
Branch: 1 branch
BranchTrueFalseTotal
if (val == code) { 0 0 0
1033 /* Labels carry C-style escapes on the wire (\' \\ \t
1034 * ...) and possibly '' doubling: restore both. */
1035 0 const char *r = label_start, *span_end, *q, *span;
1036 char *out, *w;
1037 0 span = label_start + label_len;
1038 0 ▶ for (q = label_start; q < span; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = label_start; q < span; q++) { 0 0 0
1039 0 ▶ if (*q == '\\') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\\') break; 0 0 0
1040 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
1041 }
1042 0 ▶ if (q >= span)
Branch: 1 branch
BranchTrueFalseTotal
if (q >= span) 0 0 0
1043 0 return newSVpvn(label_start, label_len);
1044 0 Newx(out, label_len + 1, char);
1045 0 w = out;
1046 0 span_end = span;
1047 0 ▶ while (r < span_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < span_end) { 0 0 0
1048 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
1049 0 char e = *++r;
1050 0 switch (e) {
1051 0 case 't': *w++ = '\t'; break;
1052 0 case 'n': *w++ = '\n'; break;
1053 0 case 'r': *w++ = '\r'; break;
1054 0 case 'b': *w++ = '\b'; break;
1055 0 case 'f': *w++ = '\f'; break;
1056 0 case 'v': *w++ = '\v'; break;
1057 0 case '0': *w++ = '\0'; break;
1058 0 default: *w++ = e; break;
1059 }
1060 0 r++;
1061 0 continue;
1062 }
1063 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
1064 0 *w++ = *r++;
1065 }
1066 {
1067 0 SV *sv = newSVpvn(out, w - out);
1068 0 Safefree(out);
1069 0 return sv;
1070 }
1071 }
1072  
1073 /* Skip to next entry */
1074 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
1075 0 ▶ if (p < end) p++; /* skip comma */
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; /* skip comma */ 0 0 0
1076 }
1077 }
1078 /* Not found — return numeric code */
1079 0 return newSViv(code);
1080 }
1081  
1082 /* Decoded Enum entry for label lookup on insert. */
1083 typedef struct {
1084 char *label;
1085 size_t len;
1086 int code;
1087 } enum_entry_t;
1088  
1089 /* Parse "Enum8('a' = 1, ...)" once per column into decoded entries.
1090 * Mirrors enum_label_for_code's label scanning (C escapes, '' doubling).
1091 * Returns the entry count; free with enum_free_entries. */
1092 0 static size_t enum_parse_entries(const char *type_str, size_t type_str_len,
1093 enum_entry_t **out) {
1094 const char *p, *end;
1095 0 size_t n = 0, cap = 0;
1096 0 enum_entry_t *entries = NULL;
1097 0 *out = NULL;
1098 0 ▶ if (!type_str) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!type_str) return 0; 0 0 0
1099 0 p = memchr(type_str, '(', type_str_len);
1100 0 ▶ if (!p) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return 0; 0 0 0
1101 0 p++;
1102 0 end = type_str + type_str_len - 1;
1103 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1104 const char *label_start, *span_end, *r;
1105 0 size_t raw_len, dlen = 0;
1106 0 char *dec = NULL;
1107 int code;
1108 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
1109 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
1110 0 p++;
1111 0 label_start = p;
1112 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1113 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
1114 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
1115 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
1116 0 break;
1117 }
1118 0 p++;
1119 }
1120 0 raw_len = p - label_start;
1121 0 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 0 0 0
1122 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
1123 0 code = (int)strtol(p, NULL, 10);
1124 0 ▶ if (raw_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (raw_len > 0) { 0 0 0
1125 0 Newx(dec, raw_len, char);
1126 0 span_end = label_start + raw_len;
1127 0 r = label_start;
1128 0 ▶ while (r < span_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < span_end) { 0 0 0
1129 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
1130 0 char e = *++r;
1131 0 switch (e) {
1132 0 case 't': dec[dlen++] = '\t'; break;
1133 0 case 'n': dec[dlen++] = '\n'; break;
1134 0 case 'r': dec[dlen++] = '\r'; break;
1135 0 case 'b': dec[dlen++] = '\b'; break;
1136 0 case 'f': dec[dlen++] = '\f'; break;
1137 0 case 'v': dec[dlen++] = '\v'; break;
1138 0 case '0': dec[dlen++] = '\0'; break;
1139 0 default: dec[dlen++] = e; break;
1140 }
1141 0 r++;
1142 0 continue;
1143 }
1144 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
1145 0 dec[dlen++] = *r++;
1146 }
1147 }
1148 0 ▶ if (n == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == cap) { 0 0 0
1149 0 ▶ cap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 8; 0 0 0
1150 0 ▶ Renew(entries, cap, enum_entry_t);
Branch: 1 branch
BranchTrueFalseTotal
Renew(entries, cap, enum_entry_t); 0 0 0
1151 }
1152 0 entries[n].label = dec;
1153 0 entries[n].len = dlen;
1154 0 entries[n].code = code;
1155 0 n++;
1156 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
1157 0 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 0 0 0
1158 }
1159 0 *out = entries;
1160 0 return n;
1161 }
1162  
1163 0 static void enum_free_entries(enum_entry_t *entries, size_t n) {
1164 size_t i;
1165 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
1166 0 Safefree(entries[i].label);
1167 0 Safefree(entries);
1168 0 }
1169  
1170 0 static int enum_code_for_label(const enum_entry_t *entries, size_t n,
1171 const char *s, size_t len, int *code_out) {
1172 size_t i;
1173 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
1174 0 ▶ if (entries[i].len == len
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i].len == len 0 0 0
1175 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
1176 0 *code_out = entries[i].code;
1177 0 return 1;
1178 }
1179 }
1180 0 return 0;
1181 }
1182  
1183 /*
1184 * Decode a column of `nrows` values from the native binary format.
1185 * Returns an array of SVs (one per row). Returns NULL on failure.
1186 * Sets *decode_err=1 on definitive errors (vs needing more data).
1187 * Advances *pos past the consumed bytes.
1188 */
1189  
1190 #ifdef __SIZEOF_INT128__
1191 0 static SV* int128_to_sv(const char *p, int is_signed) {
1192 unsigned __int128 uv;
1193 char dbuf[42];
1194 0 int dlen = 0, neg = 0, k;
1195 0 ▶ if (is_signed) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_signed) { 0 0 0
1196 __int128 sv;
1197 0 memcpy(&sv, p, 16);
1198 0 neg = sv < 0;
1199 0 ▶ uv = neg ? -(unsigned __int128)sv : (unsigned __int128)sv;
Branch: 1 branch
BranchTrueFalseTotal
uv = neg ? -(unsigned __int128)sv : (unsigned __int128)sv; 0 0 0
1200 } else {
1201 0 memcpy(&uv, p, 16);
1202 }
1203 do {
1204 0 dbuf[dlen++] = '0' + (int)(uv % 10);
1205 0 uv /= 10;
1206 0 ▶ } while (uv);
Branch: 1 branch
BranchTrueFalseTotal
} while (uv); 0 0 0
1207 0 ▶ if (neg) dbuf[dlen++] = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) dbuf[dlen++] = '-'; 0 0 0
1208 0 ▶ for (k = 0; k < dlen/2; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < dlen/2; k++) { 0 0 0
1209 0 char tmp = dbuf[k]; dbuf[k] = dbuf[dlen-1-k]; dbuf[dlen-1-k] = tmp;
1210 }
1211 0 return newSVpvn(dbuf, dlen);
1212 }
1213 #endif
1214  
1215 /* Convert a 256-bit LE unsigned integer (as 4 x uint64_t) to decimal string.
1216 * Works on all platforms (no __int128 required). */
1217 0 static SV* uint256_to_sv(const char *p) {
1218 /* Copy into 4 x uint64_t LE limbs: v[0] = lowest */
1219 uint64_t v[4];
1220 char dbuf[80];
1221 0 int dlen = 0, k;
1222  
1223 0 memcpy(v, p, 32);
1224  
1225 /* Handle zero */
1226 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
1227 0 return newSVpvn("0", 1);
1228  
1229 /* Repeatedly divide by 10, collecting remainders */
1230 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
1231 0 uint64_t rem = 0;
1232 int i;
1233 0 ▶ for (i = 3; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i >= 0; i--) { 0 0 0
1234 #ifdef __SIZEOF_INT128__
1235 0 unsigned __int128 cur = ((unsigned __int128)rem << 64) | v[i];
1236 0 v[i] = (uint64_t)(cur / 10);
1237 0 rem = (uint64_t)(cur % 10);
1238 #else
1239 /* Without 128-bit: split each 64-bit limb into hi32:lo32 */
1240 uint64_t hi = (rem << 32) | (v[i] >> 32);
1241 uint64_t q_hi = hi / 10;
1242 uint64_t r_hi = hi % 10;
1243 uint64_t lo = (r_hi << 32) | (v[i] & 0xFFFFFFFFULL);
1244 uint64_t q_lo = lo / 10;
1245 rem = lo % 10;
1246 v[i] = (q_hi << 32) | q_lo;
1247 #endif
1248 }
1249 0 dbuf[dlen++] = '0' + (int)rem;
1250 }
1251 0 ▶ for (k = 0; k < dlen/2; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < dlen/2; k++) { 0 0 0
1252 0 char tmp = dbuf[k]; dbuf[k] = dbuf[dlen-1-k]; dbuf[dlen-1-k] = tmp;
1253 }
1254 0 return newSVpvn(dbuf, dlen);
1255 }
1256  
1257 0 static SV* int256_to_sv(const char *p, int is_signed) {
1258 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
1259 /* Negative: two's complement negate, format, prepend '-' */
1260 unsigned char neg[32];
1261 0 int i, carry = 1;
1262 SV *sv;
1263 STRLEN svlen;
1264 char *s;
1265 0 ▶ for (i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) { 0 0 0
1266 0 int b = (unsigned char)(~((unsigned char)p[i])) + carry;
1267 0 neg[i] = (unsigned char)(b & 0xFF);
1268 0 carry = b >> 8;
1269 }
1270 0 sv = uint256_to_sv((const char *)neg);
1271 /* Prepend '-' */
1272 0 s = SvPV(sv, svlen);
1273 {
1274 0 SV *result = newSV(svlen + 1);
1275 0 SvPOK_on(result);
1276 0 SvCUR_set(result, svlen + 1);
1277 0 *SvPVX(result) = '-';
1278 0 Copy(s, SvPVX(result) + 1, svlen, char);
1279 0 SvPVX(result)[svlen + 1] = '\0';
1280 0 SvREFCNT_dec(sv);
1281 0 return result;
1282 }
1283 }
1284 0 return uint256_to_sv(p);
1285 }
1286  
1287 /* Native sends every serialization state prefix of a column's type tree,
1288 * depth first, before any of its data; the only one we meet outside JSON
1289 * is the LowCardinality keys version. 1 = ok, 0 = need more, -1 = bad. */
1290 0 static int read_col_prefix(const char *buf, size_t len, size_t *pos,
1291 col_type_t *ct) {
1292 int i, rc;
1293 0 switch (ct->code) {
1294 0 case CT_LOWCARDINALITY: {
1295 uint64_t version;
1296 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
1297 0 memcpy(&version, buf + *pos, 8);
1298 0 ▶ if (version != 1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (version != 1) return -1; 0 0 0
1299 0 *pos += 8;
1300 0 return 1;
1301 }
1302 0 case CT_ARRAY:
1303 case CT_NULLABLE:
1304 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
1305 0 case CT_TUPLE:
1306 case CT_MAP:
1307 0 ▶ for (i = 0; i < ct->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ct->num_inners; i++) 0 0 0
1308 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
1309 0 return rc;
1310 0 return 1;
1311 0 default:
1312 0 return 1;
1313 }
1314 }
1315  
1316 static SV** decode_column_data(const char *buf, size_t len, size_t *pos,
1317 uint64_t nrows, col_type_t *ct, int *decode_err,
1318 uint32_t decode_flags, ev_clickhouse_t *lc_self,
1319 int lc_col_idx);
1320  
1321 0 static SV** decode_data(const char *buf, size_t len, size_t *pos,
1322 uint64_t nrows, col_type_t *ct, int *decode_err,
1323 uint32_t decode_flags) {
1324 0 return decode_column_data(buf, len, pos, nrows, ct, decode_err, decode_flags, NULL, -1);
1325 }
1326  
1327 /* One whole column of a block: prefixes, then data. */
1328 0 static SV** decode_column_ex(const char *buf, size_t len, size_t *pos,
1329 uint64_t nrows, col_type_t *ct, int *decode_err,
1330 uint32_t decode_flags, ev_clickhouse_t *lc_self,
1331 int lc_col_idx) {
1332 0 ▶ if (nrows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows) { 0 0 0
1333 0 int rc = read_col_prefix(buf, len, pos, ct);
1334 0 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 0 0 0
1335 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
1336 0 return NULL;
1337 }
1338 }
1339 0 return decode_column_data(buf, len, pos, nrows, ct, decode_err, decode_flags,
1340 lc_self, lc_col_idx);
1341 }
1342  
1343 0 static SV** decode_column(const char *buf, size_t len, size_t *pos,
1344 uint64_t nrows, col_type_t *ct, int *decode_err,
1345 uint32_t decode_flags) {
1346 0 return decode_column_ex(buf, len, pos, nrows, ct, decode_err, decode_flags, NULL, -1);
1347 }
1348  
1349 0 static SV** decode_column_data(const char *buf, size_t len, size_t *pos,
1350 uint64_t nrows, col_type_t *ct, int *decode_err,
1351 uint32_t decode_flags, ev_clickhouse_t *lc_self,
1352 int lc_col_idx) {
1353 SV **out;
1354 uint64_t i;
1355 size_t fsz;
1356  
1357 /* Bound nrows against the remaining buffer before allocating out[].
1358 * Every wire column consumes at least one byte per row (a value, an
1359 * offset, a null-map byte or an index), so a row count larger than the
1360 * bytes left cannot be satisfied yet. Return NULL WITHOUT setting
1361 * decode_err -- exactly the per-type need-more convention -- so the caller
1362 * treats it as "need more data" on the incremental (uncompressed) path and
1363 * as a hard error on the compressed path (where the whole block is already
1364 * present). This caps the out[] allocation to the available data and
1365 * stops a hostile server forcing a huge alloc from a tiny message, while
1366 * also covering recursive Array/Map/Tuple/LowCardinality element counts.
1367 * No exception for FixedString(0): real servers reject it outright,
1368 * so it can only arrive from a hostile one. (An empty Tuple() takes
1369 * a 1-byte-per-row placeholder on the wire, so the bound holds.) */
1370 0 ▶ if (*pos > len) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos > len) return NULL; 0 0 0
1371 0 ▶ if (nrows > (uint64_t)(len - *pos)) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > (uint64_t)(len - *pos)) { 0 0 0
1372 0 return NULL;
1373 }
1374  
1375 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
1376  
1377 0 ▶ if (ct->code == CT_NOTHING) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_NOTHING) { 0 0 0
1378 /* Nothing type: 1 placeholder byte ('0') per row */
1379 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
1380 0 *pos += nrows;
1381 0 ▶ for (i = 0; i < nrows; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) 0 0 0
1382 0 out[i] = newSV(0);
1383 0 return out;
1384 }
1385  
1386 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
1387 /* The wire format is per-version and includes shared substream
1388 * dispatch we don't replicate here. Set decode_err so the calling
1389 * parser surfaces a clean error and tears the connection down,
1390 * instead of trying to read garbage strings (which would desync
1391 * every subsequent column). Recommend a server-side workaround. */
1392 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1393 0 goto fail;
1394 }
1395  
1396 0 ▶ if (ct->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_NULLABLE) { 0 0 0
1397 /* null bitmap: nrows bytes of UInt8 */
1398 uint8_t *nulls;
1399 SV **inner;
1400 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
1401 0 Newx(nulls, nrows, uint8_t);
1402 0 Copy(buf + *pos, nulls, nrows, uint8_t);
1403 0 *pos += nrows;
1404  
1405 /* decode inner column */
1406 0 inner = decode_data(buf, len, pos, nrows, ct->inner, decode_err, decode_flags);
1407 0 ▶ if (!inner) { Safefree(nulls); goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!inner) { Safefree(nulls); goto fail; } 0 0 0
1408  
1409 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1410 0 ▶ if (nulls[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (nulls[i]) { 0 0 0
1411 0 SvREFCNT_dec(inner[i]);
1412 0 out[i] = newSV(0); /* undef */
1413 } else {
1414 0 out[i] = inner[i];
1415 }
1416 }
1417 0 Safefree(nulls);
1418 0 Safefree(inner);
1419 0 return out;
1420 }
1421  
1422 0 ▶ if (ct->code == CT_LOWCARDINALITY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_LOWCARDINALITY) { 0 0 0
1423 /*
1424 * LowCardinality wire format (all multi-byte integers are UInt64 LE):
1425 * PREFIX: UInt64 key_version, read by read_col_prefix()
1426 * DATA: UInt64 serialization_type (bits 0-7: index type,
1427 * bit 8: NeedGlobalDictionary, bit 9: HasAdditionalKeys,
1428 * bit 10: NeedUpdateDictionary)
1429 * if NeedUpdateDictionary: UInt64 num_keys + dictionary data
1430 * UInt64 num_indices + index data
1431 * Zero rows (an empty nested array) carry no DATA at all.
1432 */
1433 0 uint64_t ser_type, num_keys = 0, num_indices;
1434 0 size_t saved = *pos;
1435 int key_type;
1436 size_t idx_size;
1437 0 SV **dict = NULL;
1438 0 int dict_borrowed = 0; /* 1 if dict points to lc_self storage */
1439 /* LC(Nullable(T)) dictionaries travel as plain T with slot 0
1440 * reserved for NULL (the server synthesizes the null map). */
1441 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
1442 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
1443  
1444 0 ▶ if (nrows == 0) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return out; 0 0 0
1445  
1446 /* serialization_type: UInt64 */
1447 0 ▶ if (*pos + 8 > len) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos + 8 > len) goto lc_fail; 0 0 0
1448 0 memcpy(&ser_type, buf + *pos, 8); *pos += 8;
1449  
1450 0 key_type = (int)(ser_type & 0xFF);
1451 /* key_type: 0=UInt8, 1=UInt16, 2=UInt32, 3=UInt64 */
1452 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
1453 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1454 0 goto lc_fail;
1455 }
1456  
1457 /* Read dictionary if NeedUpdateDictionary (bit 10) */
1458 0 ▶ if (ser_type & (1ULL << 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ser_type & (1ULL << 10)) { 0 0 0
1459 0 ▶ if (*pos + 8 > len) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos + 8 > len) goto lc_fail; 0 0 0
1460 0 memcpy(&num_keys, buf + *pos, 8); *pos += 8;
1461  
1462 0 dict = decode_data(buf, len, pos, num_keys, dict_ct, decode_err, decode_flags);
1463 0 ▶ if (!dict) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!dict) goto lc_fail; 0 0 0
1464 } else {
1465 /* NeedUpdateDictionary=0: reuse dictionary from prior block */
1466 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
1467 0 ▶ && lc_self->lc_dicts[lc_col_idx]) {
Branch: 1 branch
BranchTrueFalseTotal
&& lc_self->lc_dicts[lc_col_idx]) { 0 0 0
1468 0 dict = lc_self->lc_dicts[lc_col_idx];
1469 0 num_keys = lc_self->lc_dict_sizes[lc_col_idx];
1470 0 dict_borrowed = 1;
1471 } else {
1472 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1473 0 goto lc_fail;
1474 }
1475 }
1476  
1477 /* Read indices: UInt64 num_indices + index data */
1478 0 ▶ if (*pos + 8 > len) goto lc_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos + 8 > len) goto lc_fail; 0 0 0
1479 0 memcpy(&num_indices, buf + *pos, 8); *pos += 8;
1480  
1481 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
1482 (key_type == 2) ? 4 : 8;
1483 0 ▶ if (num_indices != nrows) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_indices != nrows) { 0 0 0
1484 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1485 0 goto lc_fail;
1486 }
1487 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
1488 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1489 0 uint64_t idx = 0;
1490 0 memcpy(&idx, buf + *pos + i * idx_size, idx_size);
1491 0 ▶ if (idx >= num_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= num_keys) { 0 0 0
1492 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1493 0 goto lc_fail;
1494 }
1495 }
1496  
1497 /* Store new dictionary for cross-block reuse (after validation) */
1498 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
1499 0 ▶ if (lc_self->lc_dicts[lc_col_idx]) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc_self->lc_dicts[lc_col_idx]) { 0 0 0
1500 uint64_t di;
1501 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
1502 0 SvREFCNT_dec(lc_self->lc_dicts[lc_col_idx][di]);
1503 0 Safefree(lc_self->lc_dicts[lc_col_idx]);
1504 }
1505 SV **dcopy;
1506 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
1507 0 ▶ for (i = 0; i < num_keys; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_keys; i++) 0 0 0
1508 0 dcopy[i] = SvREFCNT_inc(dict[i]);
1509 0 lc_self->lc_dicts[lc_col_idx] = dcopy;
1510 0 lc_self->lc_dict_sizes[lc_col_idx] = num_keys;
1511 }
1512  
1513 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1514 0 uint64_t idx = 0;
1515 0 memcpy(&idx, buf + *pos + i * idx_size, idx_size);
1516 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
1517 0 out[i] = newSV(0);
1518 else
1519 0 out[i] = SvREFCNT_inc(dict[idx]);
1520 }
1521 0 *pos += num_indices * idx_size;
1522  
1523 0 ▶ if (dict && !dict_borrowed) {
Branch: 2 branches
BranchTrueFalseTotal
if (dict && !dict_borrowed) { 0 0 0
if (dict && !dict_borrowed) { 0 0 0
1524 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
1525 0 Safefree(dict);
1526 }
1527 0 return out;
1528  
1529 0 lc_fail:
1530 0 ▶ if (dict && !dict_borrowed) {
Branch: 2 branches
BranchTrueFalseTotal
if (dict && !dict_borrowed) { 0 0 0
if (dict && !dict_borrowed) { 0 0 0
1531 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
1532 0 Safefree(dict);
1533 }
1534 0 *pos = saved;
1535 0 goto fail;
1536 }
1537  
1538 0 ▶ if (ct->code == CT_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_STRING) { 0 0 0
1539 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1540 const char *s;
1541 size_t slen;
1542 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
1543 /* clean up already-created SVs */
1544 uint64_t j;
1545 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
1546 0 goto fail;
1547 }
1548 0 out[i] = newSVpvn(s, slen);
1549 }
1550 0 return out;
1551 }
1552  
1553 0 ▶ if (ct->code == CT_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_ARRAY) { 0 0 0
1554 /* offsets: nrows x UInt64 */
1555 uint64_t *offsets;
1556 SV **elems;
1557 uint64_t total, prev;
1558  
1559 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
1560 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
1561 0 ▶ Copy(buf + *pos, offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Copy(buf + *pos, offsets, nrows, uint64_t); 0 0 0
1562 0 *pos += nrows * 8;
1563  
1564 /* validate offset monotonicity */
1565 0 prev = 0;
1566 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1567 0 ▶ if (offsets[i] < prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (offsets[i] < prev) { 0 0 0
1568 0 Safefree(offsets);
1569 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1570 0 goto fail;
1571 }
1572 0 prev = offsets[i];
1573 }
1574  
1575 0 ▶ total = nrows > 0 ? offsets[nrows - 1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
total = nrows > 0 ? offsets[nrows - 1] : 0; 0 0 0
1576  
1577 /* decode all inner elements */
1578 0 elems = decode_data(buf, len, pos, total, ct->inner, decode_err, decode_flags);
1579 0 ▶ if (!elems) { Safefree(offsets); goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!elems) { Safefree(offsets); goto fail; } 0 0 0
1580  
1581 /* build AV for each row */
1582 0 prev = 0;
1583 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1584 0 uint64_t count = offsets[i] - prev;
1585 0 AV *av = newAV();
1586 uint64_t j;
1587 0 ▶ if (count > 0) av_extend(av, count - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) av_extend(av, count - 1); 0 0 0
1588 0 ▶ for (j = 0; j < count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) { 0 0 0
1589 0 av_push(av, elems[prev + j]);
1590 }
1591 0 out[i] = newRV_noinc((SV*)av);
1592 0 prev = offsets[i];
1593 }
1594  
1595 0 Safefree(offsets);
1596 0 Safefree(elems);
1597 0 return out;
1598 }
1599  
1600 0 ▶ if (ct->code == CT_TUPLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_TUPLE) { 0 0 0
1601 /* Tuple: each element is a separate column, transpose to row arrays */
1602 0 SV ***cols = NULL;
1603 int j;
1604  
1605 0 ▶ if (ct->num_inners == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners == 0) { 0 0 0
1606 /* Empty Tuple(): 1 placeholder byte per row on the wire */
1607 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
1608 0 *pos += nrows;
1609 }
1610 else {
1611 0 Newxz(cols, ct->num_inners, SV**);
1612 0 ▶ for (j = 0; j < ct->num_inners; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) { 0 0 0
1613 0 cols[j] = decode_data(buf, len, pos, nrows, ct->inners[j], decode_err, decode_flags);
1614 0 ▶ if (!cols[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cols[j]) { 0 0 0
1615 int k;
1616 0 ▶ for (k = 0; k < j; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < j; k++) { 0 0 0
1617 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
1618 0 Safefree(cols[k]);
1619 }
1620 0 Safefree(cols);
1621 0 goto fail;
1622 }
1623 }
1624 }
1625  
1626 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1627 0 AV *av = newAV();
1628 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
1629 0 ▶ for (j = 0; j < ct->num_inners; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) 0 0 0
1630 0 av_push(av, cols[j][i]);
1631 0 out[i] = newRV_noinc((SV*)av);
1632 }
1633  
1634 0 ▶ if (cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (cols) { 0 0 0
1635 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
1636 0 Safefree(cols);
1637 }
1638 0 return out;
1639 }
1640  
1641 0 ▶ if (ct->code == CT_JSON) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_JSON) { 0 0 0
1642 /* Wire layout (V1/V2/V3 supported); see JSON helpers + Encoder docs. */
1643 0 size_t saved = *pos;
1644 uint64_t obj_ver;
1645 uint64_t num_paths64;
1646 0 json_path_entry_t *jpe = NULL;
1647 0 int *path_kinds_buf = NULL; /* concatenated [path][kind_idx] */
1648 0 int *path_kind_count = NULL;
1649 0 int wire_slots_cleanup = 0; /* slot-count for var_avs cleanup */
1650 0 AV **var_avs = NULL;
1651 0 uint64_t *offs = NULL;
1652 0 AV *pending_inner = NULL;
1653 int p;
1654  
1655 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
1656 0 memcpy(&obj_ver, buf + *pos, 8); *pos += 8;
1657 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
1658 0 ▶ if (obj_ver == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_ver == 0) { 0 0 0
1659 uint64_t dummy;
1660 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
1661 }
1662 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
1663 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
1664 /* each path serializes to >=1 wire byte, so a count larger than the
1665 * bytes left is malformed -- bound before the (jpe/path_kind) allocs */
1666 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
1667 0 int num_paths = (int)num_paths64;
1668  
1669 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
1670 0 Newx(jpe, num_paths, json_path_entry_t);
1671 0 Newxz(path_kind_count, num_paths, int);
1672 0 Newxz(path_kinds_buf, num_paths * JSON_LEX_SLOTS, int);
1673 }
1674 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
1675 const char *ps; size_t pl;
1676 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
1677 0 jpe[p].path = (char*)ps;
1678 0 jpe[p].len = pl;
1679 }
1680 0 ▶ if (obj_ver == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_ver == 4) { 0 0 0
1681 uint64_t shared_ver, dummy;
1682 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
1683 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
1684 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
1685 }
1686 0 ▶ for (p = 0; p < ct->num_inners; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < ct->num_inners; p++) { 0 0 0
1687 0 int prc = read_col_prefix(buf, len, pos, ct->inners[p]);
1688 0 ▶ if (prc == 0) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (prc == 0) goto json_fail; 0 0 0
1689 0 ▶ if (prc < 0) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (prc < 0) goto json_hard; 0 0 0
1690 }
1691  
1692 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
1693 uint64_t dyn_ver, var_mode, ntypes;
1694 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
1695 0 memcpy(&dyn_ver, buf + *pos, 8); *pos += 8;
1696 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
1697 0 ▶ if (dyn_ver == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (dyn_ver == 1) { 0 0 0
1698 uint64_t dummy;
1699 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
1700 }
1701 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
1702 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
1703 0 int *kinds = path_kinds_buf + p * JSON_LEX_SLOTS;
1704 uint64_t ti;
1705 0 ▶ for (ti = 0; ti < ntypes; ti++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ti = 0; ti < ntypes; ti++) { 0 0 0
1706 const char *ts; size_t tl;
1707 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
1708 0 int k = json_kind_from_type_name(ts, tl);
1709 0 ▶ if (k < 0) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) goto json_hard; 0 0 0
1710 0 kinds[ti] = k;
1711 }
1712 0 path_kind_count[p] = (int)ntypes;
1713 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
1714 0 memcpy(&var_mode, buf + *pos, 8); *pos += 8;
1715 0 ▶ if (var_mode != 0) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (var_mode != 0) goto json_hard; 0 0 0
1716 }
1717  
1718 /* Allocate per-row hashref. */
1719 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
1720  
1721 /* Typed-path data first (in declaration order). For each typed path,
1722 * decode a regular column via inner type, then store per-row into
1723 * out[i]'s hashref under the typed-path name. */
1724 0 ▶ if (ct->num_inners > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners > 0) { 0 0 0
1725 int tp;
1726 0 ▶ for (tp = 0; tp < ct->num_inners; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < ct->num_inners; tp++) { 0 0 0
1727 0 SV **tpcol = decode_data(buf, len, pos, (uint64_t)nrows,
1728 0 ct->inners[tp], decode_err, decode_flags);
1729 0 ▶ if (!tpcol) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!tpcol) goto json_fail; 0 0 0
1730 0 size_t nlen = strlen(ct->inner_names[tp]);
1731 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1732 0 HV *row_hv = (HV*)SvRV(out[i]);
1733 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
1734 tpcol[i], 0))
1735 0 SvREFCNT_dec(tpcol[i]);
1736 }
1737 0 Safefree(tpcol);
1738 }
1739 }
1740  
1741 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
1742 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
1743 0 const unsigned char *discs = (const unsigned char *)(buf + *pos);
1744 0 *pos += (size_t)nrows;
1745  
1746 0 int nv = path_kind_count[p];
1747 int slot_to_kind[JSON_LEX_SLOTS];
1748 0 unsigned mask = 0;
1749 int s, kk;
1750 0 int *kinds = path_kinds_buf + p * JSON_LEX_SLOTS;
1751 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
1752 0 int wire_slots = json_build_lex_table(mask, slot_to_kind);
1753  
1754 0 uint64_t var_counts[JSON_LEX_SLOTS] = {0};
1755 uint64_t r2;
1756 0 ▶ for (r2 = 0; r2 < (uint64_t)nrows; r2++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r2 = 0; r2 < (uint64_t)nrows; r2++) { 0 0 0
1757 0 unsigned char d = discs[r2];
1758 0 ▶ if (d == 0xff) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d == 0xff) continue; 0 0 0
1759 0 ▶ if (d >= wire_slots) goto json_hard;
Branch: 1 branch
BranchTrueFalseTotal
if (d >= wire_slots) goto json_hard; 0 0 0
1760 0 var_counts[d]++;
1761 }
1762  
1763 0 Newxz(var_avs, wire_slots, AV*);
1764 0 wire_slots_cleanup = wire_slots;
1765  
1766 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
1767 0 int kind = slot_to_kind[s];
1768 0 uint64_t nv_rows = var_counts[s];
1769 0 AV *sub = newAV();
1770 0 var_avs[s] = sub;
1771 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
1772 /* SharedVariant (kind<0) takes the same String wire shape;
1773 * the encoder never routes rows there but the format still
1774 * allocates the slot. */
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 const char *vs; size_t vl;
1778 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
1779 0 goto json_fail;
1780 0 av_push(sub, newSVpvn(vs, vl));
1781 }
1782 0 ▶ } else if (kind == JV_INT64) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_INT64) { 0 0 0
1783 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
1784 uint64_t k;
1785 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1786 0 int64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1787 0 av_push(sub, newSViv((IV)v));
1788 }
1789 0 ▶ } else if (kind == JV_UINT64) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_UINT64) { 0 0 0
1790 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
1791 uint64_t k;
1792 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1793 0 uint64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1794 0 av_push(sub, newSVuv((UV)v));
1795 }
1796 0 ▶ } else if (kind == JV_FLOAT64) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_FLOAT64) { 0 0 0
1797 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
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 double v; memcpy(&v, buf + *pos, 8); *pos += 8;
1801 0 av_push(sub, newSVnv(v));
1802 }
1803 0 ▶ } else if (kind == JV_BOOL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (kind == JV_BOOL) { 0 0 0
1804 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
1805 uint64_t k;
1806 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1807 0 unsigned char b8 = (unsigned char)buf[(*pos)++];
1808 0 av_push(sub, newSViv(b8 ? 1 : 0));
1809 }
1810 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
1811 0 ▶ if (nv_rows == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (nv_rows == 0) continue; 0 0 0
1812 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
1813 0 ▶ Newx(offs, nv_rows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offs, nv_rows, uint64_t); 0 0 0
1814 uint64_t k;
1815 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1816 0 memcpy(&offs[k], buf + *pos, 8); *pos += 8;
1817 }
1818 0 uint64_t prev = 0;
1819 0 ▶ for (k = 0; k < nv_rows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nv_rows; k++) { 0 0 0
1820 uint64_t cnt;
1821 /* validate offset monotonicity (cf. Array/Map);
1822 * a decrease would wrap cnt below */
1823 0 ▶ if (offs[k] < prev) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (offs[k] < prev) goto json_fail; 0 0 0
1824 0 cnt = offs[k] - prev;
1825 0 pending_inner = newAV();
1826 /* every element costs >= 1 byte, so only pre-extend
1827 * what is actually present; a wilder cnt trips the
1828 * per-element bounds checks instead of OOMing here */
1829 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
1830 0 av_extend(pending_inner, (SSize_t)cnt - 1);
1831 uint64_t j;
1832 0 ▶ for (j = 0; j < cnt; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < cnt; j++) { 0 0 0
1833 0 switch (kind) {
1834 0 case JV_ARRAY_BOOL:
1835 0 ▶ if (*pos >= len) goto json_fail;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos >= len) goto json_fail; 0 0 0
1836 0 av_push(pending_inner,
1837 newSViv((unsigned char)buf[(*pos)++] ? 1 : 0));
1838 0 break;
1839 0 case JV_ARRAY_INT64: {
1840 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
1841 0 int64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1842 0 av_push(pending_inner, newSViv((IV)v));
1843 0 break;
1844 }
1845 0 case JV_ARRAY_UINT64: {
1846 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
1847 0 uint64_t v; memcpy(&v, buf + *pos, 8); *pos += 8;
1848 0 av_push(pending_inner, newSVuv((UV)v));
1849 0 break;
1850 }
1851 0 case JV_ARRAY_FLOAT64: {
1852 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
1853 0 double v; memcpy(&v, buf + *pos, 8); *pos += 8;
1854 0 av_push(pending_inner, newSVnv(v));
1855 0 break;
1856 }
1857 0 case JV_ARRAY_STRING: {
1858 const char *vs; size_t vl;
1859 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
1860 0 goto json_fail;
1861 0 av_push(pending_inner, newSVpvn(vs, vl));
1862 0 break;
1863 }
1864 }
1865 }
1866 0 av_push(sub, newRV_noinc((SV*)pending_inner));
1867 0 pending_inner = NULL; /* now owned by sub */
1868 0 prev = offs[k];
1869 }
1870 0 Safefree(offs); offs = NULL;
1871 } else {
1872 0 goto json_hard;
1873 }
1874 }
1875  
1876 /* Distribute values into per-row hashes (dotted keys, unflattened later). */
1877 0 SSize_t cursors[JSON_LEX_SLOTS] = {0};
1878 uint64_t r3;
1879 0 ▶ for (r3 = 0; r3 < (uint64_t)nrows; r3++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r3 = 0; r3 < (uint64_t)nrows; r3++) { 0 0 0
1880 0 unsigned char d = discs[r3];
1881 0 ▶ if (d == 0xff) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d == 0xff) continue; 0 0 0
1882 0 SV **e = av_fetch(var_avs[d], cursors[d]++, 0);
1883 0 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 0 0
1884 0 HV *row_hv = (HV*)SvRV(out[r3]);
1885 0 SV *vsv = SvREFCNT_inc(*e);
1886 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
1887 0 SvREFCNT_dec(vsv);
1888 }
1889 0 ▶ for (s = 0; s < wire_slots; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) 0 0 0
1890 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
1891 0 Safefree(var_avs); var_avs = NULL;
1892 0 wire_slots_cleanup = 0;
1893 }
1894  
1895 /* Trailing shared data: N UInt64 offsets, then if last>0 strings. */
1896 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
1897 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
1898 uint64_t last_offset;
1899 0 memcpy(&last_offset, buf + *pos + 8 * (nrows - 1), 8);
1900 0 *pos += 8 * (size_t)nrows;
1901 0 ▶ if (last_offset > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_offset > 0) { 0 0 0
1902 uint64_t k;
1903 0 ▶ for (k = 0; k < 2 * last_offset; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 2 * last_offset; k++) { 0 0 0
1904 const char *s; size_t l;
1905 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
1906 }
1907 }
1908 }
1909  
1910 /* Unflatten dotted keys back into nested hashes. */
1911 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
1912 0 HV *row_hv = (HV*)SvRV(out[i]);
1913 0 AV *keys = (AV*)sv_2mortal((SV*)newAV());
1914 0 hv_iterinit(row_hv);
1915 HE *he;
1916 0 ▶ while ((he = hv_iternext(row_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(row_hv))) { 0 0 0
1917 I32 klen;
1918 0 char *kstr = hv_iterkey(he, &klen);
1919 0 ▶ if (memchr(kstr, '.', klen))
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(kstr, '.', klen)) 0 0 0
1920 0 av_push(keys, newSVpvn(kstr, klen));
1921 }
1922 0 SSize_t nk = av_len(keys) + 1, ki;
1923 0 ▶ for (ki = 0; ki < nk; ki++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ki = 0; ki < nk; ki++) { 0 0 0
1924 0 SV *ksv = *av_fetch(keys, ki, 0);
1925 STRLEN klen;
1926 0 const char *kstr = SvPV(ksv, klen);
1927 0 HV *cur = row_hv;
1928 0 STRLEN seg_start = 0, off;
1929 0 int conflict = 0;
1930 0 ▶ for (off = 0; off <= klen; off++) {
Branch: 1 branch
BranchTrueFalseTotal
for (off = 0; off <= klen; off++) { 0 0 0
1931 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
1932 0 const char *seg = kstr + seg_start;
1933 0 STRLEN slen = off - seg_start;
1934 0 ▶ if (off == klen) {
Branch: 1 branch
BranchTrueFalseTotal
if (off == klen) { 0 0 0
1935 0 SV **leaf = hv_fetch(row_hv, kstr, (I32)klen, 0);
1936 0 ▶ if (!leaf) { conflict = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!leaf) { conflict = 1; break; } 0 0 0
1937 0 SV *val = SvREFCNT_inc(*leaf);
1938 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
1939 0 SvREFCNT_dec(val);
1940 0 conflict = 1;
1941 }
1942 } else {
1943 0 SV **next = hv_fetch(cur, seg, (I32)slen, 0);
1944 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
1945 0 cur = (HV*)SvRV(*next);
1946 0 ▶ } else if (next) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (next) { 0 0 0
1947 0 conflict = 1; break;
1948 } else {
1949 0 HV *new_hv = newHV();
1950 0 hv_store(cur, seg, (I32)slen,
1951 newRV_noinc((SV*)new_hv), 0);
1952 0 cur = new_hv;
1953 }
1954 }
1955 0 seg_start = off + 1;
1956 }
1957 }
1958 0 ▶ if (!conflict)
Branch: 1 branch
BranchTrueFalseTotal
if (!conflict) 0 0 0
1959 0 (void)hv_delete(row_hv, kstr, (I32)klen, G_DISCARD);
1960 }
1961 }
1962  
1963 0 ▶ if (jpe) Safefree(jpe);
Branch: 1 branch
BranchTrueFalseTotal
if (jpe) Safefree(jpe); 0 0 0
1964 0 ▶ if (path_kinds_buf) Safefree(path_kinds_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kinds_buf) Safefree(path_kinds_buf); 0 0 0
1965 0 ▶ if (path_kind_count) Safefree(path_kind_count);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kind_count) Safefree(path_kind_count); 0 0 0
1966 0 return out;
1967  
1968 0 json_hard:
1969 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
1970 /* fall through to shared cleanup */
1971 0 json_fail:
1972 0 ▶ if (pending_inner) SvREFCNT_dec((SV*)pending_inner);
Branch: 1 branch
BranchTrueFalseTotal
if (pending_inner) SvREFCNT_dec((SV*)pending_inner); 0 0 0
1973 0 ▶ if (offs) Safefree(offs);
Branch: 1 branch
BranchTrueFalseTotal
if (offs) Safefree(offs); 0 0 0
1974 0 ▶ if (var_avs) {
Branch: 1 branch
BranchTrueFalseTotal
if (var_avs) { 0 0 0
1975 int sx;
1976 0 ▶ for (sx = 0; sx < wire_slots_cleanup; sx++)
Branch: 1 branch
BranchTrueFalseTotal
for (sx = 0; sx < wire_slots_cleanup; sx++) 0 0 0
1977 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
1978 0 Safefree(var_avs);
1979 }
1980 0 ▶ if (jpe) Safefree(jpe);
Branch: 1 branch
BranchTrueFalseTotal
if (jpe) Safefree(jpe); 0 0 0
1981 0 ▶ if (path_kinds_buf) Safefree(path_kinds_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kinds_buf) Safefree(path_kinds_buf); 0 0 0
1982 0 ▶ if (path_kind_count) Safefree(path_kind_count);
Branch: 1 branch
BranchTrueFalseTotal
if (path_kind_count) Safefree(path_kind_count); 0 0 0
1983 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
1984 0 *pos = saved;
1985 /* Short read: leave decode_err clear so the incremental parser
1986 * waits for more data. Definitive corruption jumps to json_hard. */
1987 0 goto fail;
1988 }
1989  
1990 0 ▶ if (ct->code == CT_MAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_MAP) { 0 0 0
1991 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
1992 /* Map(K,V): wire format same as Array — offsets + keys column + values column */
1993 uint64_t *offsets, total, prev;
1994 SV **keys_col, **vals_col;
1995  
1996 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
1997 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
1998 0 ▶ Copy(buf + *pos, offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Copy(buf + *pos, offsets, nrows, uint64_t); 0 0 0
1999 0 *pos += nrows * 8;
2000  
2001 /* validate offset monotonicity */
2002 0 prev = 0;
2003 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2004 0 ▶ if (offsets[i] < prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (offsets[i] < prev) { 0 0 0
2005 0 Safefree(offsets);
2006 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
2007 0 goto fail;
2008 }
2009 0 prev = offsets[i];
2010 }
2011  
2012 0 ▶ total = nrows > 0 ? offsets[nrows - 1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
total = nrows > 0 ? offsets[nrows - 1] : 0; 0 0 0
2013  
2014 0 keys_col = decode_data(buf, len, pos, total, ct->inners[0], decode_err, decode_flags);
2015 0 ▶ if (!keys_col) { Safefree(offsets); goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!keys_col) { Safefree(offsets); goto fail; } 0 0 0
2016  
2017 0 vals_col = decode_data(buf, len, pos, total, ct->inners[1], decode_err, decode_flags);
2018 0 ▶ if (!vals_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (!vals_col) { 0 0 0
2019 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
2020 0 Safefree(keys_col);
2021 0 Safefree(offsets);
2022 0 goto fail;
2023 }
2024  
2025 0 prev = 0;
2026 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2027 0 uint64_t count = offsets[i] - prev;
2028 0 HV *hv = newHV();
2029 uint64_t j;
2030 0 ▶ for (j = 0; j < count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) { 0 0 0
2031 STRLEN klen;
2032 0 const char *kstr = SvPV(keys_col[prev + j], klen);
2033 {
2034 0 SV *val_sv = SvREFCNT_inc(vals_col[prev + j]);
2035 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
2036 0 SvREFCNT_dec(val_sv);
2037 }
2038 }
2039 0 out[i] = newRV_noinc((SV*)hv);
2040 0 prev = offsets[i];
2041 }
2042  
2043 0 ▶ for (i = 0; i < total; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < total; i++) { 0 0 0
2044 0 SvREFCNT_dec(keys_col[i]);
2045 0 SvREFCNT_dec(vals_col[i]);
2046 }
2047 0 Safefree(keys_col);
2048 0 Safefree(vals_col);
2049 0 Safefree(offsets);
2050 0 return out;
2051 }
2052  
2053 /* Fixed-width types */
2054 0 fsz = col_type_fixed_size(ct);
2055 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
2056 /* FixedString(0): 0 bytes per row, produce empty strings */
2057 0 ▶ for (i = 0; i < nrows; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) 0 0 0
2058 0 out[i] = newSVpvn("", 0);
2059 0 return out;
2060 }
2061 0 ▶ if (fsz > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fsz > 0) { 0 0 0
2062 0 const ch_tz_t *zone = NULL;
2063  
2064 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
2065  
2066 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
2067 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
2068 0 zone = col_zone(ct);
2069 0 ▶ if (!zone) {
Branch: 1 branch
BranchTrueFalseTotal
if (!zone) { 0 0 0
2070 0 ▶ if (decode_err) *decode_err = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (decode_err) *decode_err = 1; 0 0 0
2071 0 goto fail;
2072 }
2073 }
2074  
2075 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2076 0 const char *p = buf + *pos + i * fsz;
2077 0 switch (ct->code) {
2078 0 case CT_INT8: out[i] = newSViv(*(int8_t*)p); break;
2079 0 case CT_INT16: { int16_t v; memcpy(&v, p, 2); out[i] = newSViv(v); break; }
2080 0 case CT_INT32: { int32_t v; memcpy(&v, p, 4); out[i] = newSViv(v); break; }
2081 0 case CT_INT64: { int64_t v; memcpy(&v, p, 8); out[i] = newSViv((IV)v); break; }
2082 0 case CT_UINT8: case CT_BOOL:
2083 0 out[i] = newSVuv(*(uint8_t*)p); break;
2084 0 case CT_UINT16: { uint16_t v; memcpy(&v, p, 2); out[i] = newSVuv(v); break; }
2085 0 case CT_UINT32: { uint32_t v; memcpy(&v, p, 4); out[i] = newSVuv(v); break; }
2086 0 case CT_UINT64: { uint64_t v; memcpy(&v, p, 8); out[i] = newSVuv((UV)v); break; }
2087 0 case CT_FLOAT32: { float v; memcpy(&v, p, 4); out[i] = newSVnv(v); break; }
2088 0 case CT_FLOAT64: { double v; memcpy(&v, p, 8); out[i] = newSVnv(v); break; }
2089 0 case CT_ENUM8:
2090 0 ▶ if (decode_flags & DECODE_ENUM_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_ENUM_STR) 0 0 0
2091 0 out[i] = enum_label_for_code(ct->type_str, ct->type_str_len, *(int8_t*)p);
2092 else
2093 0 out[i] = newSViv(*(int8_t*)p);
2094 0 break;
2095 0 case CT_ENUM16: {
2096 0 int16_t v; memcpy(&v, p, 2);
2097 0 ▶ if (decode_flags & DECODE_ENUM_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_ENUM_STR) 0 0 0
2098 0 out[i] = enum_label_for_code(ct->type_str, ct->type_str_len, v);
2099 else
2100 0 out[i] = newSViv(v);
2101 0 break;
2102 }
2103 0 case CT_DATE: {
2104 0 uint16_t v; memcpy(&v, p, 2);
2105 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2106 0 out[i] = days_to_date_sv((int32_t)v);
2107 else
2108 0 out[i] = newSVuv(v);
2109 0 break;
2110 }
2111 0 case CT_DATE32: {
2112 0 int32_t v; memcpy(&v, p, 4);
2113 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2114 0 out[i] = days_to_date_sv(v);
2115 else
2116 0 out[i] = newSViv(v);
2117 0 break;
2118 }
2119 0 case CT_DATETIME: {
2120 0 uint32_t v; memcpy(&v, p, 4);
2121 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2122 0 out[i] = epoch_to_datetime_sv_ex(v, zone);
2123 else
2124 0 out[i] = newSVuv(v);
2125 0 break;
2126 }
2127 0 case CT_DATETIME64: {
2128 0 int64_t v; memcpy(&v, p, 8);
2129 0 ▶ if (decode_flags & DECODE_DT_STR)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DT_STR) 0 0 0
2130 0 out[i] = dt64_to_datetime_sv_ex(v, ct->param, zone);
2131 else
2132 0 out[i] = newSViv((IV)v);
2133 0 break;
2134 }
2135 0 case CT_TIME: {
2136 0 int32_t v; memcpy(&v, p, 4);
2137 0 out[i] = newSViv(v);
2138 0 break;
2139 }
2140 0 case CT_TIME64: {
2141 0 int64_t v; memcpy(&v, p, 8);
2142 0 out[i] = newSViv((IV)v);
2143 0 break;
2144 }
2145 0 case CT_DECIMAL32: {
2146 0 int32_t v; memcpy(&v, p, 4);
2147 0 ▶ if (decode_flags & DECODE_DEC_SCALE)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DEC_SCALE) 0 0 0
2148 0 out[i] = newSVnv((double)v / pow10_int(ct->param));
2149 else
2150 0 out[i] = newSViv(v);
2151 0 break;
2152 }
2153 0 case CT_DECIMAL64: {
2154 0 int64_t v; memcpy(&v, p, 8);
2155 0 ▶ if (decode_flags & DECODE_DEC_SCALE)
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DEC_SCALE) 0 0 0
2156 0 out[i] = newSVnv((double)v / pow10_int(ct->param));
2157 else
2158 0 out[i] = newSViv((IV)v);
2159 0 break;
2160 }
2161 0 case CT_DECIMAL128: {
2162 #ifdef __SIZEOF_INT128__
2163 0 ▶ if (decode_flags & DECODE_DEC_SCALE) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_flags & DECODE_DEC_SCALE) { 0 0 0
2164 __int128 sv128;
2165 0 memcpy(&sv128, p, 16);
2166 /* Use long double for Decimal128 to preserve more precision */
2167 0 out[i] = newSVnv((NV)((long double)sv128 / (long double)pow10_int(ct->param)));
2168 } else {
2169 0 out[i] = int128_to_sv(p, 1);
2170 }
2171 #else
2172 out[i] = newSVpvn(p, 16);
2173 #endif
2174 0 break;
2175 }
2176 0 case CT_DECIMAL256: {
2177 /* No native int256; deliver raw 32 bytes (LE, signed) so
2178 * users can pass them to e.g. Math::BigInt. */
2179 0 out[i] = newSVpvn(p, 32);
2180 0 break;
2181 }
2182 0 case CT_BFLOAT16: {
2183 /* Top 16 bits of a Float32 (BE assembled into LE Float32). */
2184 0 uint8_t buf[4] = {0, 0, ((const uint8_t*)p)[0], ((const uint8_t*)p)[1]};
2185 float v;
2186 0 memcpy(&v, buf, 4);
2187 0 out[i] = newSVnv(v);
2188 0 break;
2189 }
2190 0 case CT_UUID: {
2191 /* UUID: two LE UInt64 halves, each reversed for display.
2192 * Nibble table instead of snprintf: same bytes, ~10x
2193 * less formatting work per row. */
2194 static const char hexd[] = "0123456789abcdef";
2195 static const uint8_t uord[16] =
2196 {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
2197 char ustr[36];
2198 0 char *w = ustr;
2199 0 const unsigned char *u = (const unsigned char *)p;
2200 int k;
2201 0 ▶ for (k = 0; k < 16; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 16; k++) { 0 0 0
2202 unsigned char b;
2203 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
2204 0 *w++ = '-';
2205 0 b = u[uord[k]];
2206 0 *w++ = hexd[b >> 4];
2207 0 *w++ = hexd[b & 15];
2208 }
2209 0 out[i] = newSVpvn(ustr, 36);
2210 0 break;
2211 }
2212 0 case CT_IPV4: {
2213 /* UInt32 LE, MSB is first octet */
2214 uint32_t v;
2215 struct in_addr addr;
2216 char abuf[INET_ADDRSTRLEN];
2217 0 memcpy(&v, p, 4);
2218 0 addr.s_addr = htonl(v);
2219 0 inet_ntop(AF_INET, &addr, abuf, sizeof(abuf));
2220 0 out[i] = newSVpv(abuf, 0);
2221 0 break;
2222 }
2223 0 case CT_IPV6: {
2224 /* 16 bytes in network byte order */
2225 char abuf[INET6_ADDRSTRLEN];
2226 0 inet_ntop(AF_INET6, p, abuf, sizeof(abuf));
2227 0 out[i] = newSVpv(abuf, 0);
2228 0 break;
2229 }
2230 0 case CT_INT128: {
2231 #ifdef __SIZEOF_INT128__
2232 0 out[i] = int128_to_sv(p, 1);
2233 #else
2234 out[i] = newSVpvn(p, 16);
2235 #endif
2236 0 break;
2237 }
2238 0 case CT_UINT128: {
2239 #ifdef __SIZEOF_INT128__
2240 0 out[i] = int128_to_sv(p, 0);
2241 #else
2242 out[i] = newSVpvn(p, 16);
2243 #endif
2244 0 break;
2245 }
2246 0 case CT_INT256:
2247 0 out[i] = int256_to_sv(p, 1); break;
2248 0 case CT_UINT256:
2249 0 out[i] = int256_to_sv(p, 0); break;
2250 0 case CT_FIXEDSTRING: default:
2251 0 out[i] = newSVpvn(p, fsz); break;
2252 }
2253 }
2254 0 *pos += nrows * fsz;
2255 0 return out;
2256 }
2257  
2258 /* CT_UNKNOWN: try reading as String */
2259 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2260 const char *s;
2261 size_t slen;
2262 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
2263 uint64_t j;
2264 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
2265 0 goto fail;
2266 }
2267 0 out[i] = newSVpvn(s, slen);
2268 }
2269 0 return out;
2270  
2271 0 fail:
2272 0 Safefree(out);
2273 0 return NULL;
2274 }
2275  
2276 /* --- Native protocol column encoder (for INSERT) --- */
2277  
2278 /* inet_pton's strictness is not portable: glibc rejects a leading zero in a
2279 * dotted quad, macOS and the BSDs accept it. ClickHouse rejects it, so the same
2280 * INSERT would be refused on Linux and silently stored on macOS. */
2281 0 static int ipv4_text_is_strict(const char *s, size_t slen) {
2282 0 size_t i = 0;
2283 int group;
2284 0 ▶ for (group = 0; group < 4; group++) {
Branch: 1 branch
BranchTrueFalseTotal
for (group = 0; group < 4; group++) { 0 0 0
2285 0 size_t start = i;
2286 0 unsigned v = 0;
2287 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
2288 0 v = v * 10u + (unsigned)(s[i] - '0');
2289 0 i++;
2290 }
2291 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
2292 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
2293 0 ▶ if (group < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (group < 3) { 0 0 0
2294 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
2295 0 i++;
2296 }
2297 }
2298 0 return i == slen;
2299 }
2300  
2301 /* Parse an IPv4 address text into a host-order UInt32. Sets *ok. */
2302 0 static uint32_t parse_ipv4_to_u32(const char *s, size_t slen, int *ok) {
2303 struct in_addr addr;
2304 char tmp[64];
2305 0 *ok = 0;
2306 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
2307 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
2308 0 memcpy(tmp, s, slen);
2309 0 tmp[slen] = '\0';
2310 0 ▶ if (inet_pton(AF_INET, tmp, &addr) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, tmp, &addr) == 1) { 0 0 0
2311 0 *ok = 1;
2312 0 return ntohl(addr.s_addr);
2313 }
2314 0 return 0;
2315 }
2316  
2317 /* Parse an IPv6 address text into 16 network-order bytes. Returns 1 on
2318 * success, 0 on failure (zero-fills the output either way). A dotted
2319 * quad maps to ::ffff:0:0/96, like the server's text parser. */
2320 0 static int parse_ipv6_to_bytes(const char *s, size_t slen, unsigned char addr[16]) {
2321 char tmp[64];
2322 struct in_addr a4;
2323 0 memset(addr, 0, 16);
2324 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
2325 0 memcpy(tmp, s, slen);
2326 0 tmp[slen] = '\0';
2327 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
2328 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
2329 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
2330 0 addr[10] = 0xff;
2331 0 addr[11] = 0xff;
2332 0 memcpy(addr + 12, &a4.s_addr, 4);
2333 0 return 1;
2334 }
2335  
2336 /* Mirror of read_col_prefix(): every LowCardinality keys version of the
2337 * type tree, depth first, ahead of the column data. */
2338 4 static void write_col_prefix(native_buf_t *b, col_type_t *ct) {
2339 int i;
2340 4 switch (ct->code) {
2341 0 case CT_LOWCARDINALITY:
2342 0 nbuf_le64(b, 1);
2343 0 break;
2344 0 case CT_ARRAY:
2345 case CT_NULLABLE:
2346 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
2347 0 break;
2348 0 case CT_TUPLE:
2349 case CT_MAP:
2350 0 ▶ for (i = 0; i < ct->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ct->num_inners; i++) 0 0 0
2351 0 write_col_prefix(b, ct->inners[i]);
2352 0 break;
2353 4 default:
2354 4 break;
2355 }
2356 4 }
2357  
2358 /* Write the LowCardinality serialization-type + num_keys header for a
2359 * trivial 1:1 dictionary encoding (every value becomes its own dict
2360 * entry). Returns idx_size via *out_idx_size for the caller's index-write loop. */
2361 0 static void lc_write_keys_header(native_buf_t *b, uint64_t nrows, size_t *out_idx_size) {
2362 int key_type;
2363 size_t idx_size;
2364 0 uint64_t ser_type, nk = nrows;
2365 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
2366 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
2367 0 else { key_type = 2; idx_size = 4; }
2368 /* HasAdditionalKeys | NeedUpdateDictionary */
2369 0 ser_type = (uint64_t)key_type | (1ULL << 9) | (1ULL << 10);
2370 0 nbuf_append(b, (const char *)&ser_type, 8);
2371 0 nbuf_append(b, (const char *)&nk, 8);
2372 0 *out_idx_size = idx_size;
2373 0 }
2374  
2375 /* An Enum type's default value, as on the server: its smallest code. */
2376 0 static int enum_default_code(col_type_t *t) {
2377 0 enum_entry_t *entries = NULL;
2378 0 size_t i, n = enum_parse_entries(t->type_str, t->type_str_len, &entries);
2379 0 ▶ int code = n ? entries[0].code : 0;
Branch: 1 branch
BranchTrueFalseTotal
int code = n ? entries[0].code : 0; 0 0 0
2380 0 ▶ for (i = 1; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) 0 0 0
2381 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
2382 0 enum_free_entries(entries, n);
2383 0 return code;
2384 }
2385  
2386 /* Placeholder for a NULL of the given inner type. Must itself parse
2387 * under the strict encoders, or one NULL fails the batch. */
2388 0 static void nullable_inner_zero(col_type_t *t, const char **p, size_t *len) {
2389 static const char zero_str[] = "0";
2390 static const char empty_str[] = "";
2391 static const char zero_uuid[] = "00000000-0000-0000-0000-000000000000";
2392 static const char zero_ip4[] = "0.0.0.0";
2393 static const char zero_ip6[] = "::";
2394 static const char zero_time[] = "00:00:00";
2395 static char enum_buf[8];
2396 0 switch (t->code) {
2397 0 case CT_ENUM8:
2398 case CT_ENUM16:
2399 0 *len = (size_t)snprintf(enum_buf, sizeof(enum_buf), "%d", enum_default_code(t));
2400 0 *p = enum_buf;
2401 0 break;
2402 0 case CT_STRING:
2403 case CT_FIXEDSTRING:
2404 0 *p = empty_str; *len = 0; break;
2405 0 case CT_UUID:
2406 0 *p = zero_uuid; *len = sizeof(zero_uuid) - 1; break;
2407 0 case CT_IPV4:
2408 0 *p = zero_ip4; *len = sizeof(zero_ip4) - 1; break;
2409 0 case CT_IPV6:
2410 0 *p = zero_ip6; *len = sizeof(zero_ip6) - 1; break;
2411 0 case CT_TIME:
2412 case CT_TIME64:
2413 0 *p = zero_time; *len = sizeof(zero_time) - 1; break;
2414 0 default:
2415 0 *p = zero_str; *len = 1; break;
2416 }
2417 0 }
2418  
2419 0 static SV *nullable_inner_zero_sv(pTHX_ col_type_t *t) {
2420 const char *p;
2421 size_t len;
2422 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
2423 0 return newSViv(enum_default_code(t));
2424 0 nullable_inner_zero(t, &p, &len);
2425 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
2426 0 return newSVpvn("", 0);
2427 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
2428 0 return newSViv(0);
2429 0 return newSVpvn(p, len);
2430 }
2431  
2432 /* Write num_indices + identity-mapped indices [0..nrows-1] for a trivial
2433 * LowCardinality 1:1 dictionary. */
2434 0 static void lc_write_indices(native_buf_t *b, uint64_t nrows, size_t idx_size) {
2435 0 uint64_t i, ni = nrows;
2436 0 nbuf_append(b, (const char *)&ni, 8);
2437 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2438 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
2439 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
2440 0 else { uint32_t idx = (uint32_t)i; nbuf_append(b, (const char *)&idx, 4); }
2441 }
2442 0 }
2443  
2444 /* Write num_indices + per-row indices for an LC(Nullable) dictionary
2445 * whose slot 0 is NULL: null rows map to 0, row i maps to i + 1. */
2446 0 static void lc_write_nullable_indices(native_buf_t *b, uint64_t nrows,
2447 size_t idx_size, uint8_t *nulls) {
2448 0 uint64_t i, ni = nrows;
2449 0 nbuf_append(b, (const char *)&ni, 8);
2450 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
2451 0 ▶ uint64_t v = nulls[i] ? 0 : i + 1;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t v = nulls[i] ? 0 : i + 1; 0 0 0
2452 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
2453 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
2454 0 else { uint32_t idx = (uint32_t)v; nbuf_append(b, (const char *)&idx, 4); }
2455 }
2456 0 }
2457  
2458 /* Parse "YYYY-MM-DD HH:MM:SS[.fff...]" into a DateTime64 scaled int64
2459 * (total_seconds * 10^precision + fractional). Sets *ok; on invalid input
2460 * returns 0 with *ok = 0. Pre-1970 dates work: the day count is a floor,
2461 * so the fraction always adds. */
2462 18 static int64_t parse_datetime64_to_scaled(const char *s, size_t len, int precision,
2463 col_type_t *ct, int *ok) {
2464 int32_t days;
2465 18 int64_t total, v, scale = 1;
2466 int sc;
2467 18 *ok = 0;
2468 18 ▶ if (!valid_date_prefix(s, len)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, len)) return 0; 0 18 18
2469 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
2470 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
2471 2 days = date_string_to_days(s, 10);
2472 2 total = (int64_t)days * 86400 + parse_hms(s, len);
2473 2 ▶ if (ct->tz && ct->tz[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct->tz && ct->tz[0]) { 2 0 2
if (ct->tz && ct->tz[0]) { 2 0 2
2474 2 int tok = 0;
2475 2 total = wall_in_tz_to_epoch(total, ct, &tok);
2476 2 ▶ if (!tok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) return 0; 0 2 2
2477 }
2478 8 ▶ for (sc = 0; sc < precision; sc++) scale *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (sc = 0; sc < precision; sc++) scale *= 10; 6 2 8
2479 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
2480 2 v = total * scale;
2481 /* Full consumption: a suffix past the seconds must be a fraction.
2482 * The server interprets designators like 'Z'/'+03:00' with TZ
2483 * conversion the client cannot replicate, so anything but digits
2484 * fails rather than silently writing a divergent instant. Extra
2485 * fraction digits truncate, matching the server. */
2486 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
2487 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
2488 0 const char *fp = s + 20;
2489 0 const char *fe = s + len;
2490 0 int64_t frac = 0;
2491 0 int digits = 0;
2492 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
2493 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
2494 0 frac = frac * 10 + (*fp - '0');
2495 0 fp++;
2496 0 digits++;
2497 }
2498 0 ▶ while (fp < fe) {
Branch: 1 branch
BranchTrueFalseTotal
while (fp < fe) { 0 0 0
2499 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
2500 0 fp++;
2501 }
2502 0 ▶ while (digits < precision) { frac *= 10; digits++; }
Branch: 1 branch
BranchTrueFalseTotal
while (digits < precision) { frac *= 10; digits++; } 0 0 0
2503 /* total*scale was bounded; the fraction can still push v over */
2504 0 ▶ if (v > INT64_MAX - frac) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > INT64_MAX - frac) return 0; 0 0 0
2505 0 v += frac;
2506 }
2507 2 *ok = 1;
2508 2 return v;
2509 }
2510  
2511 /* Expand an exponent-form decimal ("[+-]mantissa[eE][+-]digits") into
2512 * plain text for the existing decimal parsers. Returns 1 with *out_len
2513 * set, 0 when there is no exponent marker (caller uses the input as is),
2514 * -1 when invalid. The point shifts textually (exact, no float maths);
2515 * int_digits past precision-scale fail, huge positive exponents fail
2516 * fast and huge negative ones collapse to zero, without big buffers. */
2517 0 static int decimal_expand_exp(const char *p, size_t len, int scale,
2518 int precision, char *out, size_t out_sz,
2519 size_t *out_len) {
2520 size_t k, mant_len, exp_len, i, j;
2521 const char *mant, *exp_p, *ip, *fp;
2522 size_t ilen, flen;
2523 0 int neg = 0, exp_neg = 0;
2524 0 long exp = 0, int_after, limit;
2525 const char *sig;
2526 size_t sig_len, use, k3, need;
2527 char *w;
2528 0 ▶ for (k = 0; k < len; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < len; k++) 0 0 0
2529 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
2530 0 ▶ if (k == len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (k == len) return 0; 0 0 0
2531 0 mant = p;
2532 0 mant_len = k;
2533 0 exp_p = p + k + 1;
2534 0 exp_len = len - mant_len - 1;
2535 0 ▶ if (mant_len == 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (mant_len == 0) return -1; 0 0 0
2536 0 i = 0;
2537 0 ▶ if (mant[0] == '-') { neg = 1; i = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (mant[0] == '-') { neg = 1; i = 1; } 0 0 0
2538 0 ▶ else if (mant[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (mant[0] == '+') i = 1; 0 0 0
2539 0 ▶ if (i >= mant_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= mant_len) return -1; 0 0 0
2540 0 j = i;
2541 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
2542 0 ip = mant + i;
2543 0 ilen = j - i;
2544 0 fp = mant + j;
2545 0 flen = 0;
2546 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
2547 size_t fstart;
2548 0 j++;
2549 0 fstart = j;
2550 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
2551 0 fp = mant + fstart;
2552 0 flen = j - fstart;
2553 }
2554 0 ▶ if (j != mant_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (j != mant_len) return -1; 0 0 0
2555 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
2556 0 ▶ if (exp_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (exp_len == 0) { 0 0 0
2557 0 ▶ if (mant_len >= out_sz) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (mant_len >= out_sz) return -1; 0 0 0
2558 0 memcpy(out, mant, mant_len);
2559 0 *out_len = mant_len;
2560 0 return 1;
2561 }
2562 0 i = 0;
2563 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
2564 0 ▶ else if (exp_p[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (exp_p[0] == '+') i = 1; 0 0 0
2565 0 ▶ if (i >= exp_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= exp_len) return -1; 0 0 0
2566 0 ▶ for (; i < exp_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < exp_len; i++) { 0 0 0
2567 int d;
2568 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
2569 0 d = exp_p[i] - '0';
2570 0 ▶ if (exp < 2000000L)
Branch: 1 branch
BranchTrueFalseTotal
if (exp < 2000000L) 0 0 0
2571 0 exp = exp * 10 + d;
2572 }
2573 0 ▶ if (exp_neg) exp = -exp;
Branch: 1 branch
BranchTrueFalseTotal
if (exp_neg) exp = -exp; 0 0 0
2574 /* Significant digits live in two spans (int, frac) around the point;
2575 * gather the needed prefix into one buffer. int_after <= limit and
2576 * scale bound every copy below, so no big buffers. */
2577 {
2578 char dig[256];
2579 0 size_t first, total = ilen + flen, want = 0, got = 0, k4;
2580 0 size_t z = 0;
2581 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
2582 0 ▶ if (z == ilen) {
Branch: 1 branch
BranchTrueFalseTotal
if (z == ilen) { 0 0 0
2583 0 size_t z2 = 0;
2584 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
2585 0 first = ilen + z2;
2586 } else {
2587 0 first = z;
2588 }
2589 0 ▶ sig_len = (first >= total) ? 0 : total - first;
Branch: 1 branch
BranchTrueFalseTotal
sig_len = (first >= total) ? 0 : total - first; 0 0 0
2590 0 sig = dig;
2591 0 limit = (long)precision - (long)scale;
2592 0 int_after = (long)sig_len + exp - (long)flen;
2593 0 ▶ if (int_after > limit) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (int_after > limit) return -1; 0 0 0
2594 0 ▶ if (sig_len == 0) goto zero;
Branch: 1 branch
BranchTrueFalseTotal
if (sig_len == 0) goto zero; 0 0 0
2595 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
2596 0 ▶ if (int_after <= 0) want = (size_t)scale;
Branch: 1 branch
BranchTrueFalseTotal
if (int_after <= 0) want = (size_t)scale; 0 0 0
2597 0 else want = (size_t)int_after + (size_t)scale;
2598 0 ▶ if (want > sizeof(dig)) want = sizeof(dig);
Branch: 1 branch
BranchTrueFalseTotal
if (want > sizeof(dig)) want = sizeof(dig); 0 0 0
2599 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
2600 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
2601 0 sig_len = got;
2602 0 w = out;
2603 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2604 0 ▶ if (out_sz < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (out_sz < 2) return -1; 0 0 0
2605 0 *w++ = '-';
2606 }
2607 0 ▶ if (int_after <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (int_after <= 0) { 0 0 0
2608 0 long lead = -int_after;
2609 0 use = sig_len;
2610 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
2611 0 need = 2 + (size_t)lead + use;
2612 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
2613 0 *w++ = '0';
2614 0 *w++ = '.';
2615 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
2616 0 memcpy(w, sig, use);
2617 0 w += use;
2618 0 ▶ } else if (sig_len >= (size_t)int_after) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sig_len >= (size_t)int_after) { 0 0 0
2619 0 use = sig_len - (size_t)int_after;
2620 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
2621 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
2622 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
2623 0 memcpy(w, sig, (size_t)int_after);
2624 0 w += int_after;
2625 0 ▶ if (use) {
Branch: 1 branch
BranchTrueFalseTotal
if (use) { 0 0 0
2626 0 *w++ = '.';
2627 0 memcpy(w, sig + int_after, use);
2628 0 w += use;
2629 }
2630 } else {
2631 0 size_t pad = (size_t)int_after - sig_len;
2632 0 need = sig_len + pad;
2633 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
2634 0 memcpy(w, sig, sig_len);
2635 0 w += sig_len;
2636 0 ▶ for (k3 = 0; k3 < pad; k3++) *w++ = '0';
Branch: 1 branch
BranchTrueFalseTotal
for (k3 = 0; k3 < pad; k3++) *w++ = '0'; 0 0 0
2637 }
2638 0 *out_len = (size_t)(w - out);
2639 0 return 1;
2640 0 zero:
2641 0 w = out;
2642 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2643 0 ▶ if (out_sz < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (out_sz < 2) return -1; 0 0 0
2644 0 *w++ = '-';
2645 }
2646 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
2647 0 *w++ = '0';
2648 0 *out_len = (size_t)(w - out);
2649 0 return 1;
2650 }
2651 }
2652  
2653 /* Parse "[+-]?digits[.digits]" into a scaled int64 raw value. `scale`
2654 * is the target Decimal's fractional digits; `precision` its total
2655 * digits. Mirrors the server's TSV verdicts (probed): full consumption
2656 * (leading '+' ok, no whitespace, trailing garbage fails), extra
2657 * fraction digits truncated, significant integer digits beyond
2658 * precision-scale fail. Sets *ok; the digit bound keeps every
2659 * intermediate below 10^18, so no accumulation can overflow. */
2660 0 static int64_t parse_decimal_to_raw(const char *p, size_t len, int scale,
2661 int precision, int *ok) {
2662 const char *e, *dstart, *sig;
2663 0 int neg = 0, int_digits = 0, frac_digits = 0, seen = 0, s;
2664 0 uint64_t integer_part = 0, frac_part = 0, raw;
2665 char exp_buf[256];
2666 0 size_t exp_len = 0, ek;
2667 0 int has_e = 0;
2668 0 *ok = 0;
2669 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
2670 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2671 0 ▶ for (ek = 0; ek < len; ek++)
Branch: 1 branch
BranchTrueFalseTotal
for (ek = 0; ek < len; ek++) 0 0 0
2672 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
2673 0 ▶ if (has_e) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_e) { 0 0 0
2674 0 ▶ if (decimal_expand_exp(p, len, scale, precision,
Branch: 1 branch
BranchTrueFalseTotal
if (decimal_expand_exp(p, len, scale, precision, 0 0 0
2675 exp_buf, sizeof(exp_buf), &exp_len) != 1)
2676 0 return 0;
2677 0 p = exp_buf;
2678 0 len = exp_len;
2679 }
2680 0 e = p + len;
2681 0 ▶ if (*p == '-') { neg = 1; p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { neg = 1; p++; } 0 0 0
2682 0 ▶ else if (*p == '+') p++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') p++; 0 0 0
2683 0 dstart = p;
2684 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
2685 0 ▶ if (p > dstart) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p > dstart) seen = 1; 0 0 0
2686 0 sig = dstart;
2687 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
2688 0 int_digits = (int)(p - sig);
2689 0 ▶ if (p < e && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < e && *p == '.') { 0 0 0
if (p < e && *p == '.') { 0 0 0
2690 0 p++;
2691 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
2692 0 frac_part = frac_part * 10 + (uint64_t)(*p - '0');
2693 0 p++;
2694 0 frac_digits++;
2695 }
2696 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
2697 0 ▶ if (frac_digits > 0) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frac_digits > 0) seen = 1; 0 0 0
2698 }
2699 0 ▶ if (p != e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p != e) return 0; 0 0 0
2700 0 ▶ if (!seen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) return 0; 0 0 0
2701 0 ▶ if (int_digits > precision - scale) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 0; 0 0 0
2702 /* Significant span only: leading zeros never reach the accumulator,
2703 * and int_digits <= precision - scale keeps the scaled value < 10^18. */
2704 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
2705 0 integer_part = integer_part * 10 + (uint64_t)(*p - '0');
2706 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
2707 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
2708 0 raw = integer_part + frac_part;
2709 0 *ok = 1;
2710 0 ▶ return neg ? -(int64_t)raw : (int64_t)raw;
Branch: 1 branch
BranchTrueFalseTotal
return neg ? -(int64_t)raw : (int64_t)raw; 0 0 0
2711 }
2712  
2713 /* limbs = limbs * 10 + digit over nlimbs LE u64 limbs. Returns 0 on
2714 * overflow. Portable 32-bit-half arithmetic (no __int128 needed). */
2715 0 static int limbs_mul10add(uint64_t *limbs, int nlimbs, unsigned digit) {
2716 0 uint64_t carry = digit;
2717 int i;
2718 0 ▶ for (i = 0; i < nlimbs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlimbs; i++) { 0 0 0
2719 0 uint64_t t = limbs[i];
2720 0 uint64_t p1 = (t & 0xffffffffULL) * 10 + carry;
2721 0 uint64_t p2 = (t >> 32) * 10 + (p1 >> 32);
2722 0 limbs[i] = ((p2 & 0xffffffffULL) << 32) | (p1 & 0xffffffffULL);
2723 0 carry = p2 >> 32;
2724 }
2725 0 return carry == 0;
2726 }
2727  
2728 /* Two's-complement negate over nlimbs LE u64 limbs. */
2729 0 static void limbs_negate(uint64_t *limbs, int nlimbs) {
2730 0 uint64_t carry = 1;
2731 int i;
2732 0 ▶ for (i = 0; i < nlimbs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlimbs; i++) { 0 0 0
2733 0 uint64_t v = ~limbs[i] + carry;
2734 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
2735 0 limbs[i] = v;
2736 }
2737 0 }
2738  
2739 /* Parse decimal text into a scaled raw value over nlimbs LE u64 limbs.
2740 * Same shape as parse_decimal_to_raw: full consumption, truncation of
2741 * extra fraction digits, int_digits <= precision - scale. Values above
2742 * the signed max fail (the server errors on those too). */
2743 0 static int parse_decimal_limbs(const char *p, size_t len, int scale,
2744 int precision, int max_scale, int max_prec,
2745 uint64_t *limbs, int nlimbs) {
2746 const char *e, *dstart, *sig;
2747 0 int neg = 0, int_digits = 0, frac_digits = 0, seen = 0, s, i;
2748 char exp_buf[256];
2749 0 size_t exp_len = 0, ek;
2750 0 int has_e = 0;
2751 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
2752 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
2753 0 return 0;
2754 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2755 0 ▶ for (ek = 0; ek < len; ek++)
Branch: 1 branch
BranchTrueFalseTotal
for (ek = 0; ek < len; ek++) 0 0 0
2756 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
2757 0 ▶ if (has_e) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_e) { 0 0 0
2758 0 ▶ if (decimal_expand_exp(p, len, scale, precision,
Branch: 1 branch
BranchTrueFalseTotal
if (decimal_expand_exp(p, len, scale, precision, 0 0 0
2759 exp_buf, sizeof(exp_buf), &exp_len) != 1)
2760 0 return 0;
2761 0 p = exp_buf;
2762 0 len = exp_len;
2763 }
2764 0 e = p + len;
2765 0 ▶ if (*p == '-') { neg = 1; p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { neg = 1; p++; } 0 0 0
2766 0 ▶ else if (*p == '+') p++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') p++; 0 0 0
2767 0 dstart = p;
2768 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
2769 0 ▶ if (p > dstart) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p > dstart) seen = 1; 0 0 0
2770 0 sig = dstart;
2771 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
2772 0 int_digits = (int)(p - sig);
2773 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
2774 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
2775 0 ▶ if (p < e && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < e && *p == '.') { 0 0 0
if (p < e && *p == '.') { 0 0 0
2776 0 p++;
2777 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
2778 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
2779 0 p++;
2780 0 frac_digits++;
2781 }
2782 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
2783 0 ▶ if (frac_digits > 0) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frac_digits > 0) seen = 1; 0 0 0
2784 }
2785 0 ▶ if (p != e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p != e) return 0; 0 0 0
2786 0 ▶ if (!seen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) return 0; 0 0 0
2787 0 ▶ if (int_digits > precision - scale) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 0; 0 0 0
2788 0 ▶ for (s = frac_digits; s < scale; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = frac_digits; s < scale; s++) 0 0 0
2789 0 ▶ if (!limbs_mul10add(limbs, nlimbs, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!limbs_mul10add(limbs, nlimbs, 0)) return 0; 0 0 0
2790 0 ▶ if (limbs[nlimbs - 1] & 0x8000000000000000ULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] & 0x8000000000000000ULL) { 0 0 0
2791 /* magnitude >= 2^(64n-1): valid only as exactly -MIN */
2792 0 int k, is_min = neg;
2793 0 ▶ for (k = 0; k < nlimbs - 1; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nlimbs - 1; k++) 0 0 0
2794 0 ▶ if (limbs[k]) { is_min = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[k]) { is_min = 0; break; } 0 0 0
2795 0 ▶ if (limbs[nlimbs - 1] != 0x8000000000000000ULL) is_min = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] != 0x8000000000000000ULL) is_min = 0; 0 0 0
2796 0 ▶ if (!is_min) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_min) return 0; 0 0 0
2797 }
2798 0 ▶ if (neg) limbs_negate(limbs, nlimbs);
Branch: 1 branch
BranchTrueFalseTotal
if (neg) limbs_negate(limbs, nlimbs); 0 0 0
2799 0 return 1;
2800 }
2801  
2802 0 static int parse_decimal256_to_raw(const char *p, size_t len, int scale,
2803 int precision, unsigned char out[32]) {
2804 uint64_t limbs[4];
2805 0 memset(out, 0, 32);
2806 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
2807 0 return 0;
2808 0 memcpy(out, limbs, 32);
2809 0 return 1;
2810 }
2811  
2812 0 static int parse_decimal128_to_raw(const char *p, size_t len, int scale,
2813 int precision, unsigned char out[16]) {
2814 uint64_t limbs[2];
2815 0 memset(out, 0, 16);
2816 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
2817 0 return 0;
2818 0 memcpy(out, limbs, 16);
2819 0 return 1;
2820 }
2821  
2822 /* Parse decimal integer text into nlimbs LE u64 limbs. signed=0 rejects
2823 * '-'; either way the magnitude must fit (signed: sign bit clear for
2824 * positive, representable INT_MIN for negative). */
2825 0 static int parse_int_limbs(const char *p, size_t len, uint64_t *limbs,
2826 int nlimbs, int is_signed) {
2827 size_t i;
2828 0 int neg = 0, k;
2829 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2830 0 ▶ if (*p == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { 0 0 0
2831 0 ▶ if (!is_signed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_signed) return 0; 0 0 0
2832 0 neg = 1; p++; len--;
2833 }
2834 0 ▶ else if (*p == '+') { p++; len--; }
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') { p++; len--; } 0 0 0
2835 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2836 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
2837 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
2838 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
2839 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
2840 }
2841 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2842 0 ▶ for (k = 0; k < nlimbs; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nlimbs; k++) 0 0 0
2843 0 ▶ if (limbs[k]) break;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[k]) break; 0 0 0
2844 0 ▶ if (k == nlimbs) return 1; /* -0 */
Branch: 1 branch
BranchTrueFalseTotal
if (k == nlimbs) return 1; /* -0 */ 0 0 0
2845 0 limbs_negate(limbs, nlimbs);
2846 0 ▶ if (!(limbs[nlimbs - 1] & 0x8000000000000000ULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(limbs[nlimbs - 1] & 0x8000000000000000ULL)) return 0; 0 0 0
2847 0 ▶ } else if (is_signed) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_signed) { 0 0 0
2848 0 ▶ if (limbs[nlimbs - 1] & 0x8000000000000000ULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] & 0x8000000000000000ULL) return 0; 0 0 0
2849 }
2850 0 return 1;
2851 }
2852  
2853 /* Parse a UUID string into 16 LE bytes (two LE UInt64 halves of the
2854 * 128-bit value). Returns 1 on success. Accepts exactly what the server
2855 * accepts: 32 hex digits, or 36 chars with dashes at 8/13/18/23. */
2856 0 static int parse_uuid_to_le_bytes(const char *s, size_t slen, unsigned char ubytes[16]) {
2857 0 unsigned char raw[16] = {0};
2858 0 int k = 0, j;
2859 0 memset(ubytes, 0, 16);
2860 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
2861 0 ▶ for (j = 0; j < (int)slen; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < (int)slen; j++) { 0 0 0
2862 0 char c = s[j];
2863 unsigned char nibble;
2864 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
2865 0 ▶ if (c != '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c != '-') return 0; 0 0 0
2866 0 continue;
2867 }
2868 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
2869 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
2870 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
2871 0 else return 0;
2872 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
2873 0 else raw[k/2] |= nibble;
2874 0 k++;
2875 }
2876 0 ▶ if (k != 32) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (k != 32) return 0; 0 0 0
2877 /* Reverse each 8-byte half for LE storage */
2878 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
2879 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
2880 0 return 1;
2881 }
2882  
2883 /* Parse '[−]H+:MM:SS[.frac]' or '[−]MM:SS[.frac]' to seconds (Time)
2884 * or scaled units (Time64). Mirrors the server's TSV grammar (lax
2885 * ranges, fraction truncated to scale, rejected when scale is 0).
2886 * Returns the value with *ok = 1, or 0 with *ok = 0. */
2887 0 static int64_t parse_time_text(const char *s, size_t len, int scale, int *ok) {
2888 0 uint64_t parts[3] = {0, 0, 0};
2889 0 int nparts = 0, neg = 0, frac_digits = 0, sc;
2890 uint64_t mag, tail;
2891 0 size_t i = 0, j;
2892 0 *ok = 0;
2893 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2894 0 ▶ if (s[0] == '-') { neg = 1; i = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == '-') { neg = 1; i = 1; } 0 0 0
2895 0 ▶ else if (s[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (s[0] == '+') i = 1; 0 0 0
2896 0 for (;;) {
2897 0 uint64_t v = 0;
2898 0 size_t start = i, ndig;
2899 0 ▶ if (nparts == 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nparts == 3) return 0; 0 0 0
2900 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
2901 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
2902 0 v = v * 10 + (uint64_t)(s[i] - '0');
2903 0 i++;
2904 }
2905 0 ndig = i - start;
2906 0 ▶ if (ndig == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ndig == 0) return 0; 0 0 0
2907 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
2908 0 parts[nparts++] = v;
2909 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
2910 0 break;
2911 }
2912 0 ▶ if (nparts < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nparts < 2) return 0; 0 0 0
2913 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
2914 0 ▶ if (parts[0] > (UINT64_MAX / 3600)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (parts[0] > (UINT64_MAX / 3600)) return 0; 0 0 0
2915 0 mag = parts[0] * 3600;
2916 0 tail = parts[1] * 60 + parts[2];
2917 0 ▶ if (mag > UINT64_MAX - tail) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > UINT64_MAX - tail) return 0; 0 0 0
2918 0 mag += tail;
2919 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
2920 0 ▶ if (scale <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (scale <= 0) return 0; 0 0 0
2921 0 i++;
2922 0 j = i;
2923 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
2924 0 ▶ if (j == i) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j == i) return 0; 0 0 0
2925 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
2926 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
2927 0 mag = mag * 10 + (uint64_t)(s[i] - '0');
2928 }
2929 0 i = j;
2930 }
2931 0 ▶ if (i != len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i != len) return 0; 0 0 0
2932 0 ▶ for (sc = frac_digits; sc < scale; sc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sc = frac_digits; sc < scale; sc++) { 0 0 0
2933 0 ▶ if (mag > UINT64_MAX / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > UINT64_MAX / 10) return 0; 0 0 0
2934 0 mag *= 10;
2935 }
2936 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2937 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
2938 0 *ok = 1;
2939 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
2940 }
2941 0 ▶ if (mag > (uint64_t)INT64_MAX) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > (uint64_t)INT64_MAX) return 0; 0 0 0
2942 0 *ok = 1;
2943 0 return (int64_t)mag;
2944 }
2945  
2946 /* TSV unescape: \\ → \, \n → newline, \t → tab, \0 → null byte */
2947 0 static size_t tsv_unescape(const char *src, size_t src_len, char *dst) {
2948 0 size_t i, j = 0;
2949 0 ▶ for (i = 0; i < src_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < src_len; i++) { 0 0 0
2950 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
2951 0 switch (src[i+1]) {
2952 0 case '\\': dst[j++] = '\\'; i++; break;
2953 0 case 'n': dst[j++] = '\n'; i++; break;
2954 0 case 't': dst[j++] = '\t'; i++; break;
2955 0 case '0': dst[j++] = '\0'; i++; break;
2956 0 case '\'': dst[j++] = '\''; i++; break;
2957 0 case 'b': dst[j++] = '\b'; i++; break;
2958 0 case 'r': dst[j++] = '\r'; i++; break;
2959 0 case 'a': dst[j++] = '\a'; i++; break;
2960 0 case 'f': dst[j++] = '\f'; i++; break;
2961 0 default: dst[j++] = src[i]; break;
2962 }
2963 } else {
2964 0 dst[j++] = src[i];
2965 }
2966 }
2967 0 return j;
2968 }
2969  
2970 0 static int is_tsv_null(const char *s, size_t len) {
2971 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
2972 }
2973  
2974 /* TSV escape: inverse of tsv_unescape — appends escaped bytes to buffer */
2975 0 static void tsv_escape(native_buf_t *b, const char *s, size_t len) {
2976 0 size_t i, start = 0;
2977 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
2978 0 char esc = 0;
2979 0 switch (s[i]) {
2980 0 case '\\': esc = '\\'; break;
2981 0 case '\t': esc = 't'; break;
2982 0 case '\n': esc = 'n'; break;
2983 0 case '\0': esc = '0'; break;
2984 0 case '\b': esc = 'b'; break;
2985 0 case '\r': esc = 'r'; break;
2986 0 case '\a': esc = 'a'; break;
2987 0 case '\f': esc = 'f'; break;
2988 }
2989 0 ▶ if (esc) {
Branch: 1 branch
BranchTrueFalseTotal
if (esc) { 0 0 0
2990 0 ▶ if (i > start)
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) 0 0 0
2991 0 nbuf_append(b, s + start, i - start);
2992 0 nbuf_grow(b, 2);
2993 0 b->data[b->len++] = '\\';
2994 0 b->data[b->len++] = esc;
2995 0 start = i + 1;
2996 }
2997 }
2998 0 ▶ if (start < len)
Branch: 1 branch
BranchTrueFalseTotal
if (start < len) 0 0 0
2999 0 nbuf_append(b, s + start, len - start);
3000 0 }
3001  
3002 /* Snapshot of arrayref INSERT data. Native INSERT encodes rows later (at
3003 * sample-block time), so holding the caller's AV would let a post-call
3004 * clear/push silently change the batch: keep the cells, shared by
3005 * reference, row-major in one array, projected through columns_at. */
3006 typedef struct {
3007 SSize_t nrows;
3008 SSize_t bad_row; /* first row that is not an arrayref, -1 if none */
3009 SSize_t width; /* columns_at entries, -1 without projection */
3010 SSize_t ncells, cap;
3011 SSize_t *off; /* row r owns cells[off[r] .. off[r + 1]) */
3012 SV **cells; /* NULL = missing cell */
3013 } insert_snap_t;
3014  
3015 16 static int insert_snap_free(pTHX_ SV *sv, MAGIC *mg) {
3016 16 insert_snap_t *sn = (insert_snap_t *)mg->mg_ptr;
3017 SSize_t i;
3018 PERL_UNUSED_ARG(sv);
3019 32 ▶ for (i = 0; i < sn->ncells; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sn->ncells; i++) 16 16 32
3020 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
3021 16 Safefree(sn->cells);
3022 16 Safefree(sn->off);
3023 16 Safefree(sn);
3024 16 return 0;
3025 }
3026  
3027 static MGVTBL insert_snap_vtbl = {
3028 NULL, NULL, NULL, NULL, insert_snap_free, NULL, NULL, NULL
3029 };
3030  
3031 12 static insert_snap_t *insert_snap_get(pTHX_ SV *sv) {
3032 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
3033 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
3034 }
3035  
3036 16 static void insert_snap_push(insert_snap_t *sn, SV *cell) {
3037 16 ▶ if (sn->ncells == sn->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (sn->ncells == sn->cap) { 16 0 16
3038 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
3039 0 croak("insert data too large");
3040 16 ▶ sn->cap = sn->cap ? sn->cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
sn->cap = sn->cap ? sn->cap * 2 : 64; 0 16 16
3041 16 ▶ Renew(sn->cells, sn->cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(sn->cells, sn->cap, SV *); 0 16 16
3042 }
3043 16 sn->cells[sn->ncells++] = cell;
3044 16 }
3045  
3046 /* The columns_at insert option as positions in a mortal buffer, NULL
3047 * when absent; croaks unless it is a non-empty array of non-negative
3048 * integers. */
3049 18 static SSize_t *insert_columns_at(pTHX_ HV *settings, SSize_t *count) {
3050 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
3051 AV *av;
3052 SSize_t n, j, *pos;
3053 18 *count = 0;
3054 18 ▶ if (!svp) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) return NULL; 18 0 18
3055 0 SvGETMAGIC(*svp);
3056 0 ▶ if (!SvOK(*svp)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(*svp)) return NULL; 0 0 0
3057 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
3058 0 ▶ || (n = av_len((AV *)SvRV(*svp)) + 1) < 1)
Branch: 1 branch
BranchTrueFalseTotal
|| (n = av_len((AV *)SvRV(*svp)) + 1) < 1) 0 0 0
3059 0 croak("columns_at must be a non-empty array of non-negative integers");
3060 0 av = (AV *)SvRV(*svp);
3061 0 pos = (SSize_t *)SvPVX(sv_2mortal(newSV(n * sizeof(SSize_t))));
3062 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
3063 0 SV **e = av_fetch(av, j, 0);
3064 0 ▶ SV *v = e ? *e : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = e ? *e : &PL_sv_undef; 0 0 0
3065 NV nv;
3066 0 SvGETMAGIC(v);
3067 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
3068 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
3069 0 ▶ || nv != (NV)(SSize_t)nv)
Branch: 1 branch
BranchTrueFalseTotal
|| nv != (NV)(SSize_t)nv) 0 0 0
3070 0 croak("columns_at must be a non-empty array of non-negative integers");
3071 0 pos[j] = (SSize_t)nv;
3072 }
3073 0 *count = n;
3074 0 return pos;
3075 }
3076  
3077 /* A plain row's cell is shared by reference; a get-magical one (a tied
3078 * scalar) is FETCHed once now and copied, as in a tied row. */
3079 16 static SV *snap_cell(pTHX_ SV *cell) {
3080 16 ▶ if (!cell) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cell) return NULL; 0 16 16
3081 16 ▶ return SvGMAGICAL(cell) ? newSVsv(cell) : SvREFCNT_inc(cell);
Branch: 1 branch
BranchTrueFalseTotal
return SvGMAGICAL(cell) ? newSVsv(cell) : SvREFCNT_inc(cell); 0 16 16
3082 }
3083  
3084 16 static SV *snapshot_insert_av(pTHX_ AV *src, const SSize_t *proj, SSize_t nproj) {
3085 16 SSize_t n = av_len(src) + 1;
3086 SSize_t i;
3087 insert_snap_t *sn;
3088 /* av_fetch on a tied AV returns a lazy mortal FETCH proxy, not the
3089 * value. Materialize tied rows/cells now (src is alive) and copy
3090 * the values so the snapshot never dangles if the caller frees
3091 * the tied container before the deferred encode. */
3092 16 int src_magical = SvRMAGICAL((SV *)src);
3093 /* Mortal during the build: a dying FETCH longjmps out of insert(),
3094 * and the partial copy must die with the scope instead of leaking. */
3095 16 SV *holder = sv_2mortal(newSV(0));
3096  
3097 16 Newxz(sn, 1, insert_snap_t);
3098 16 sn->bad_row = -1;
3099 16 ▶ sn->width = proj ? nproj : -1;
Branch: 1 branch
BranchTrueFalseTotal
sn->width = proj ? nproj : -1; 0 16 16
3100 16 sv_magicext(holder, NULL, PERL_MAGIC_ext, &insert_snap_vtbl, (char *)sn, 0);
3101 16 ▶ Newx(sn->off, n + 1, SSize_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(sn->off, n + 1, SSize_t); 0 16 16
3102 16 sn->off[0] = 0;
3103 16 ▶ if (proj && n > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (proj && n > 0) { 0 16 16
if (proj && n > 0) { 0 0 0
3104 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
3105 0 croak("insert data too large");
3106 0 sn->cap = n * nproj;
3107 0 ▶ Newx(sn->cells, sn->cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(sn->cells, sn->cap, SV *); 0 0 0
3108 }
3109 32 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 16 16 32
3110 16 SV **e = av_fetch(src, i, 0);
3111 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
3112 16 AV *rsrc = NULL;
3113 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
3114 0 ▶ if (SvGMAGICAL(row)) mg_get(row);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(row)) mg_get(row); 0 0 0
3115 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
3116 0 rsrc = (AV *)SvRV(row);
3117 /* Own the row AV through the copy loop below; the
3118 * FETCH proxy borrows it. Mortal: scope frees it. */
3119 0 (void)sv_2mortal(newRV_inc((SV *)rsrc));
3120 }
3121 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
3122 16 rsrc = (AV *)SvRV(row);
3123 }
3124 16 ▶ if (!rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rsrc) { 0 16 16
3125 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
3126 16 ▶ } else if (SvRMAGICAL((SV *)rsrc)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvRMAGICAL((SV *)rsrc)) { 0 16 16
3127 0 SSize_t len = av_len(rsrc) + 1;
3128 0 ▶ SSize_t j, rn = proj ? nproj : len;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, rn = proj ? nproj : len; 0 0 0
3129 0 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 0 0 0
3130 0 ▶ SSize_t at = proj ? proj[j] : j;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t at = proj ? proj[j] : j; 0 0 0
3131 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
3132 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
3133 }
3134 } else {
3135 16 SSize_t len = AvFILLp(rsrc) + 1;
3136 16 SV **arr = AvARRAY(rsrc);
3137 SSize_t j;
3138 16 ▶ if (proj) {
Branch: 1 branch
BranchTrueFalseTotal
if (proj) { 0 16 16
3139 0 ▶ for (j = 0; j < nproj; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nproj; j++) 0 0 0
3140 0 insert_snap_push(sn, snap_cell(aTHX_
3141 0 ▶ proj[j] < len ? arr[proj[j]] : NULL));
Branch: 1 branch
BranchTrueFalseTotal
proj[j] < len ? arr[proj[j]] : NULL)); 0 0 0
3142 } else {
3143 32 ▶ for (j = 0; j < len; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < len; j++) 16 16 32
3144 16 insert_snap_push(sn, snap_cell(aTHX_ arr[j]));
3145 }
3146 }
3147 16 sn->off[i + 1] = sn->ncells;
3148 }
3149 16 sn->nrows = n;
3150 /* Un-mortalize: the caller owns the holder from here on. */
3151 16 return SvREFCNT_inc(holder);
3152 }
3153  
3154 /* Serialize an AV of AVs to TabSeparated format for HTTP INSERT, each
3155 * row projected through proj when given. Returns malloc'd buffer;
3156 * caller must Safefree(). */
3157  
3158 0 static char* serialize_av_to_tsv(pTHX_ AV *rows, const SSize_t *proj,
3159 SSize_t nproj, size_t *out_len) {
3160 native_buf_t b;
3161 0 SSize_t nrows = av_len(rows) + 1;
3162 SSize_t r;
3163  
3164 0 nbuf_init(&b);
3165  
3166 0 ▶ for (r = 0; r < nrows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < nrows; r++) { 0 0 0
3167 0 SV **row_svp = av_fetch(rows, r, 0);
3168 AV *row;
3169 SSize_t ncols, c, last;
3170  
3171 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
3172 0 mg_get(*row_svp);
3173 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
3174 0 ▶ SvTYPE(SvRV(*row_svp)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(*row_svp)) != SVt_PVAV) { 0 0 0
3175 0 Safefree(b.data);
3176 0 croak("insert data: row %" IVdf " is not an ARRAY ref", (IV)r);
3177 }
3178 0 row = (AV *)SvRV(*row_svp);
3179 0 last = av_len(row);
3180 0 ▶ ncols = proj ? nproj : last + 1;
Branch: 1 branch
BranchTrueFalseTotal
ncols = proj ? nproj : last + 1; 0 0 0
3181  
3182 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
3183 0 SV **val_svp = !proj ? av_fetch(row, c, 0)
3184 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
3185 0 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 0 0 0
3186 0 ▶ if (c > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c > 0) 0 0 0
3187 0 nbuf_u8(&b, '\t');
3188  
3189 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
3190 0 mg_get(*val_svp);
3191 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
3192 0 nbuf_append(&b, "\\N", 2);
3193 0 ▶ } else if (SvROK(*val_svp)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvROK(*val_svp)) { 0 0 0
3194 /* Nested AV/HV refs (Array/Tuple/Map columns) cannot be
3195 * round-tripped through TSV without column-type info from
3196 * the server, which the HTTP path doesn't have. Fail loudly
3197 * rather than silently sending ARRAY(0x...) garbage. */
3198 0 Safefree(b.data);
3199 0 croak("insert data: row %" IVdf " column %" IVdf " is a "
3200 "reference; nested Array/Tuple/Map columns require "
3201 "the native protocol", (IV)r, (IV)c);
3202 } else {
3203 STRLEN vlen;
3204 /* nomg: already FETCHed above; a second FETCH would
3205 * double user-visible side effects. */
3206 0 const char *v = SvPV_nomg(*val_svp, vlen);
3207 0 tsv_escape(&b, v, vlen);
3208 }
3209 }
3210 0 nbuf_u8(&b, '\n');
3211 }
3212  
3213 0 *out_len = b.len;
3214 0 return b.data;
3215 }
3216  
3217 /* Strict "[+-]?digits" parses with full consumption and overflow
3218 * detection (server TSV parity: no whitespace, no trailing garbage).
3219 * Length-delimited: TSV fields are not NUL-terminated. */
3220 0 static int64_t parse_int64_strict(const char *s, size_t len, int *ok) {
3221 0 const char *e = s + len;
3222 0 int neg = 0;
3223 0 uint64_t cutoff, cutlim, v = 0;
3224 0 *ok = 0;
3225 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3226 0 ▶ if (*s == '-') { neg = 1; s++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { neg = 1; s++; } 0 0 0
3227 0 ▶ else if (*s == '+') s++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '+') s++; 0 0 0
3228 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
3229 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
3230 0 cutlim = cutoff % 10;
3231 0 cutoff /= 10;
3232 0 ▶ for (; s < e; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; s < e; s++) { 0 0 0
3233 unsigned d;
3234 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
3235 0 d = (unsigned)(*s - '0');
3236 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
3237 0 v = v * 10 + d;
3238 }
3239 0 *ok = 1;
3240 0 ▶ if (!neg) return (int64_t)v;
Branch: 1 branch
BranchTrueFalseTotal
if (!neg) return (int64_t)v; 0 0 0
3241 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
3242 }
3243  
3244 0 static uint64_t parse_uint64_strict(const char *s, size_t len, int *ok) {
3245 0 const char *e = s + len;
3246 0 uint64_t v = 0;
3247 0 *ok = 0;
3248 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3249 0 ▶ if (*s == '+') s++;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '+') s++; 0 0 0
3250 0 ▶ else if (*s == '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '-') return 0; 0 0 0
3251 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
3252 0 ▶ for (; s < e; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; s < e; s++) { 0 0 0
3253 unsigned d;
3254 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
3255 0 d = (unsigned)(*s - '0');
3256 0 ▶ if (v > (UINT64_MAX - d) / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (UINT64_MAX - d) / 10) return 0; 0 0 0
3257 0 v = v * 10 + d;
3258 }
3259 0 *ok = 1;
3260 0 return v;
3261 }
3262  
3263 /* SV to int64/uint64 for integer columns. IOK passes through; NOK goes
3264 * through SvNV with a range check so 1e15 encodes exactly instead of
3265 * stringifying to "1e+15" and truncating to 1; anything else parses the
3266 * string form strictly. !SvOK fails: a null into a non-nullable column
3267 * must error, never land as 0. */
3268 0 static int64_t sv_to_int64_strict(pTHX_ SV *sv, int *ok) {
3269 STRLEN vlen;
3270 const char *s;
3271 0 *ok = 0;
3272 0 ▶ if (!SvOK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return 0; 0 0 0
3273 0 ▶ if (SvIOK(sv)) { *ok = 1; return SvIV(sv); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { *ok = 1; return SvIV(sv); } 0 0 0
3274 0 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 0 0 0
3275 0 double d = SvNV(sv);
3276 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
3277 0 *ok = 1;
3278 0 return (int64_t)d;
3279 }
3280 0 s = SvPV(sv, vlen);
3281 0 return parse_int64_strict(s, vlen, ok);
3282 }
3283  
3284 2 static uint64_t sv_to_uint64_strict(pTHX_ SV *sv, int *ok) {
3285 STRLEN vlen;
3286 const char *s;
3287 2 *ok = 0;
3288 2 ▶ if (!SvOK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return 0; 0 2 2
3289 2 ▶ if (SvIsUV(sv)) { *ok = 1; return SvUV(sv); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) { *ok = 1; return SvUV(sv); } 0 2 2
3290 2 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 2 0 2
3291 2 IV iv = SvIV(sv);
3292 2 ▶ if (iv < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (iv < 0) return 0; 0 2 2
3293 2 *ok = 1;
3294 2 return (uint64_t)iv;
3295 }
3296 0 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 0 0 0
3297 0 double d = SvNV(sv);
3298 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
3299 0 *ok = 1;
3300 0 return (uint64_t)d;
3301 }
3302 0 s = SvPV(sv, vlen);
3303 0 return parse_uint64_strict(s, vlen, ok);
3304 }
3305  
3306 /* TSV boolean: exactly "0"/"1" or true/false in any case (probed server
3307 * verdicts: "2", "00", "01" all fail with Code 467). */
3308 0 static int bool_text_to_u8(const char *s, size_t len, uint8_t *out) {
3309 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
3310 0 *out = (uint8_t)(*s - '0');
3311 0 return 1;
3312 }
3313 0 ▶ if (len == 4 &&
Branch: 1 branch
BranchTrueFalseTotal
if (len == 4 && 0 0 0
3314 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
3315 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
3316 0 *out = 1;
3317 0 return 1;
3318 }
3319 0 ▶ if (len == 5 &&
Branch: 1 branch
BranchTrueFalseTotal
if (len == 5 && 0 0 0
3320 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
3321 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
3322 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
3323 0 *out = 0;
3324 0 return 1;
3325 }
3326 0 return 0;
3327 }
3328  
3329 /* Exact label match for a TSV field. Backslash escapes decode before
3330 * comparing, mirroring the server's TSV parsing. */
3331 0 static int enum_match_tsv(const enum_entry_t *entries, size_t n,
3332 const char *s, size_t len, int *code_out) {
3333 char *tmp;
3334 size_t ulen;
3335 int found;
3336 0 ▶ if (!memchr(s, '\\', len))
Branch: 1 branch
BranchTrueFalseTotal
if (!memchr(s, '\\', len)) 0 0 0
3337 0 return enum_code_for_label(entries, n, s, len, code_out);
3338 0 Newx(tmp, len, char);
3339 0 ulen = tsv_unescape(s, len, tmp);
3340 0 found = enum_code_for_label(entries, n, tmp, ulen, code_out);
3341 0 Safefree(tmp);
3342 0 return found;
3343 }
3344  
3345 /* A code the type does not declare would make every later read of the
3346 * column fail ("Unexpected value in enum"); the server's text parser
3347 * rejects it too. */
3348 0 static int enum_has_code(const enum_entry_t *entries, size_t n, int64_t code) {
3349 size_t i;
3350 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
3351 0 ▶ if (entries[i].code == code) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i].code == code) return 1; 0 0 0
3352 0 return 0;
3353 }
3354  
3355 /* Labels win over codes, as in the server's text parsers; a number
3356 * needs the label lookup only when some label looks like one. */
3357 0 static int enum_has_numeric_label(const enum_entry_t *entries, size_t n) {
3358 size_t i, j;
3359 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
3360 0 const char *l = entries[i].label;
3361 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
3362 0 ▶ if (j >= entries[i].len) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (j >= entries[i].len) continue; 0 0 0
3363 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
3364 0 ▶ if (j == entries[i].len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == entries[i].len) return 1; 0 0 0
3365 }
3366 0 return 0;
3367 }
3368  
3369 0 static void nbuf_enum(native_buf_t *b, int type_code, int code) {
3370 0 ▶ if (type_code == CT_ENUM8) {
Branch: 1 branch
BranchTrueFalseTotal
if (type_code == CT_ENUM8) { 0 0 0
3371 0 int8_t v = (int8_t)code;
3372 0 nbuf_append(b, (const char *)&v, 1);
3373 } else {
3374 0 int16_t v = (int16_t)code;
3375 0 nbuf_append(b, (const char *)&v, 2);
3376 }
3377 0 }
3378  
3379 /* The whole of s[0..len) as a number. Text values need not be
3380 * NUL-terminated (substituted defaults are not), so strtod gets a copy. */
3381 0 static int text_to_double(const char *s, size_t len, int as_float, double *out) {
3382 0 char buf[64], *p = buf, *endp;
3383 int ok;
3384 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3385 0 ▶ if (len >= sizeof(buf)) Newx(p, len + 1, char);
Branch: 1 branch
BranchTrueFalseTotal
if (len >= sizeof(buf)) Newx(p, len + 1, char); 0 0 0
3386 0 memcpy(p, s, len);
3387 0 p[len] = '\0';
3388 0 ▶ *out = as_float ? (double)strtof(p, &endp) : strtod(p, &endp);
Branch: 1 branch
BranchTrueFalseTotal
*out = as_float ? (double)strtof(p, &endp) : strtod(p, &endp); 0 0 0
3389 0 ok = endp == p + len;
3390 0 ▶ if (p != buf) Safefree(p);
Branch: 1 branch
BranchTrueFalseTotal
if (p != buf) Safefree(p); 0 0 0
3391 0 return ok;
3392 }
3393  
3394 /*
3395 * Encode a column of text values into native binary format.
3396 * Returns 1 on success, 0 if type is unsupported (caller falls back to inline SQL).
3397 */
3398 0 static int encode_column_text(native_buf_t *b,
3399 const char **values, size_t *value_lens,
3400 uint64_t nrows, col_type_t *ct) {
3401 uint64_t i;
3402  
3403 0 switch (ct->code) {
3404 0 case CT_INT8: {
3405 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3406 0 int ok = 0;
3407 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3408 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
3409 {
3410 0 int8_t v = (int8_t)w;
3411 0 nbuf_append(b, (const char *)&v, 1);
3412 }
3413 }
3414 0 return 1;
3415 }
3416 0 case CT_ENUM8: case CT_ENUM16: {
3417 0 enum_entry_t *entries = NULL;
3418 0 size_t nentries = enum_parse_entries(ct->type_str, ct->type_str_len, &entries);
3419 0 int rc = 1;
3420 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3421 0 int code, ok = 0;
3422 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
3423 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3424 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
3425 0 code = (int)w;
3426 }
3427 0 nbuf_enum(b, ct->code, code);
3428 }
3429 0 enum_free_entries(entries, nentries);
3430 0 return rc;
3431 }
3432 0 case CT_INT16: {
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 w = parse_int64_strict(values[i], value_lens[i], &ok);
3436 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
3437 {
3438 0 int16_t v = (int16_t)w;
3439 0 nbuf_append(b, (const char *)&v, 2);
3440 }
3441 }
3442 0 return 1;
3443 }
3444 0 case CT_INT32: case CT_DATE32: {
3445 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3446 int32_t v;
3447 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
3448 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3449 0 ▶ if (value_lens[i] != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] != 10) return 0; 0 0 0
3450 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
3451 0 v = date_string_to_days(values[i], value_lens[i]);
3452 } else {
3453 0 int ok = 0;
3454 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3455 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
3456 0 return 0;
3457 0 v = (int32_t)w;
3458 }
3459 0 nbuf_append(b, (const char *)&v, 4);
3460 }
3461 0 return 1;
3462 }
3463 0 case CT_INT64: {
3464 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3465 0 int ok = 0;
3466 0 int64_t v = parse_int64_strict(values[i], value_lens[i], &ok);
3467 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3468 0 nbuf_append(b, (const char *)&v, 8);
3469 }
3470 0 return 1;
3471 }
3472 0 case CT_UINT8: {
3473 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3474 0 int ok = 0;
3475 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i], &ok);
3476 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
3477 {
3478 0 uint8_t v = (uint8_t)w;
3479 0 nbuf_append(b, (const char *)&v, 1);
3480 }
3481 }
3482 0 return 1;
3483 }
3484 0 case CT_BOOL: {
3485 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3486 uint8_t v;
3487 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
3488 0 nbuf_append(b, (const char *)&v, 1);
3489 }
3490 0 return 1;
3491 }
3492 0 case CT_UINT16: case CT_DATE: {
3493 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3494 uint16_t v;
3495 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
3496 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3497 int32_t days;
3498 0 ▶ if (value_lens[i] != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] != 10) return 0; 0 0 0
3499 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
3500 0 days = date_string_to_days(values[i], value_lens[i]);
3501 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
3502 0 v = (uint16_t)days;
3503 } else {
3504 0 int ok = 0;
3505 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i],
3506 &ok);
3507 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
3508 0 v = (uint16_t)w;
3509 }
3510 0 nbuf_append(b, (const char *)&v, 2);
3511 }
3512 0 return 1;
3513 }
3514 0 case CT_UINT32: case CT_DATETIME: {
3515 0 int rc = 1;
3516 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3517 uint32_t v;
3518 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
3519 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3520 int32_t days;
3521 int64_t total;
3522 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
3523 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
3524 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
3525 0 days = date_string_to_days(values[i], 10);
3526 0 total = (int64_t)days * 86400
3527 0 + parse_hms(values[i], value_lens[i]);
3528 0 ▶ if (ct->tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->tz) { 0 0 0
3529 0 int tok = 0;
3530 0 total = wall_in_tz_to_epoch(total, ct, &tok);
3531 0 ▶ if (!tok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) { rc = 0; break; } 0 0 0
3532 }
3533 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
3534 0 v = (uint32_t)total;
3535 } else {
3536 0 int ok = 0;
3537 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i],
3538 &ok);
3539 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
3540 0 v = (uint32_t)w;
3541 }
3542 0 nbuf_append(b, (const char *)&v, 4);
3543 }
3544 0 return rc;
3545 }
3546 0 case CT_UINT64: {
3547 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3548 0 int ok = 0;
3549 0 uint64_t v = parse_uint64_strict(values[i], value_lens[i], &ok);
3550 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3551 0 nbuf_append(b, (const char *)&v, 8);
3552 }
3553 0 return 1;
3554 }
3555 0 case CT_FLOAT32: {
3556 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3557 double d;
3558 float v;
3559 0 ▶ if (!text_to_double(values[i], value_lens[i], 1, &d)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!text_to_double(values[i], value_lens[i], 1, &d)) return 0; 0 0 0
3560 0 v = (float)d;
3561 0 nbuf_append(b, (const char *)&v, 4);
3562 }
3563 0 return 1;
3564 }
3565 0 case CT_FLOAT64: {
3566 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3567 double v;
3568 0 ▶ if (!text_to_double(values[i], value_lens[i], 0, &v)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!text_to_double(values[i], value_lens[i], 0, &v)) return 0; 0 0 0
3569 0 nbuf_append(b, (const char *)&v, 8);
3570 }
3571 0 return 1;
3572 }
3573 0 case CT_DATETIME64: {
3574 0 int rc = 1;
3575 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3576 int64_t v;
3577 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
3578 0 int ok = 0;
3579 0 v = parse_datetime64_to_scaled(values[i], value_lens[i],
3580 ct->param, ct, &ok);
3581 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
3582 } else {
3583 0 int ok = 0;
3584 0 v = parse_int64_strict(values[i], value_lens[i], &ok);
3585 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
3586 }
3587 0 nbuf_append(b, (const char *)&v, 8);
3588 }
3589 0 return rc;
3590 }
3591 0 case CT_TIME: {
3592 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3593 0 int ok = 0;
3594 0 int64_t v = parse_time_text(values[i], value_lens[i], 0, &ok);
3595 int32_t w;
3596 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
3597 0 w = (int32_t)v;
3598 0 nbuf_append(b, (const char *)&w, 4);
3599 }
3600 0 return 1;
3601 }
3602 0 case CT_TIME64: {
3603 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3604 0 int ok = 0;
3605 0 int64_t v = parse_time_text(values[i], value_lens[i],
3606 ct->param, &ok);
3607 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3608 0 nbuf_append(b, (const char *)&v, 8);
3609 }
3610 0 return 1;
3611 }
3612 0 case CT_DECIMAL32: {
3613 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3614 0 int ok = 0;
3615 0 int32_t v = (int32_t)parse_decimal_to_raw(values[i], value_lens[i],
3616 ct->param, ct->precision,
3617 &ok);
3618 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3619 0 nbuf_append(b, (const char *)&v, 4);
3620 }
3621 0 return 1;
3622 }
3623 0 case CT_DECIMAL64: {
3624 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3625 0 int ok = 0;
3626 0 int64_t v = parse_decimal_to_raw(values[i], value_lens[i],
3627 ct->param, ct->precision, &ok);
3628 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3629 0 nbuf_append(b, (const char *)&v, 8);
3630 }
3631 0 return 1;
3632 }
3633 0 case CT_DECIMAL128: {
3634 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3635 unsigned char raw[16];
3636 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
3637 ct->param, ct->precision, raw))
3638 0 return 0;
3639 0 nbuf_append(b, (const char *)raw, 16);
3640 }
3641 0 return 1;
3642 }
3643 0 case CT_DECIMAL256: {
3644 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3645 unsigned char raw[32];
3646 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
3647 ct->param, ct->precision, raw))
3648 0 return 0;
3649 0 nbuf_append(b, (const char *)raw, 32);
3650 }
3651 0 return 1;
3652 }
3653 0 case CT_BFLOAT16: {
3654 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3655 double d;
3656 float fv;
3657 0 ▶ if (!text_to_double(values[i], value_lens[i], 0, &d)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!text_to_double(values[i], value_lens[i], 0, &d)) return 0; 0 0 0
3658 0 fv = (float)d;
3659 0 uint8_t b32[4]; memcpy(b32, &fv, 4);
3660 0 nbuf_append(b, (const char *)&b32[2], 2);
3661 }
3662 0 return 1;
3663 }
3664 0 case CT_STRING: {
3665 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3666 0 ▶ if (memchr(values[i], '\\', value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(values[i], '\\', value_lens[i])) { 0 0 0
3667 char *tmp;
3668 size_t ulen;
3669 0 Newx(tmp, value_lens[i], char);
3670 0 ulen = tsv_unescape(values[i], value_lens[i], tmp);
3671 0 nbuf_string(b, tmp, ulen);
3672 0 Safefree(tmp);
3673 } else {
3674 0 nbuf_string(b, values[i], value_lens[i]);
3675 }
3676 }
3677 0 return 1;
3678 }
3679 0 case CT_FIXEDSTRING: {
3680 0 size_t fsz = (size_t)ct->param;
3681 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3682 0 ▶ if (memchr(values[i], '\\', value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(values[i], '\\', value_lens[i])) { 0 0 0
3683 char *tmp;
3684 size_t ulen;
3685 0 size_t tmp_sz = value_lens[i] > fsz ? value_lens[i] : fsz;
3686 0 Newxz(tmp, tmp_sz, char);
3687 0 ulen = tsv_unescape(values[i], value_lens[i], tmp);
3688 0 ▶ if (ulen > fsz) { Safefree(tmp); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ulen > fsz) { Safefree(tmp); return 0; } 0 0 0
3689 0 nbuf_append(b, tmp, fsz);
3690 0 Safefree(tmp);
3691 } else {
3692 size_t cplen;
3693 0 ▶ if (value_lens[i] > fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] > fsz) return 0; 0 0 0
3694 0 cplen = value_lens[i];
3695 0 nbuf_grow(b, fsz);
3696 0 memcpy(b->data + b->len, values[i], cplen);
3697 0 ▶ if (cplen < fsz)
Branch: 1 branch
BranchTrueFalseTotal
if (cplen < fsz) 0 0 0
3698 0 memset(b->data + b->len + cplen, 0, fsz - cplen);
3699 0 b->len += fsz;
3700 }
3701 }
3702 0 return 1;
3703 }
3704 0 case CT_UUID: {
3705 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3706 unsigned char ubytes[16];
3707 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
3708 0 return 0;
3709 0 nbuf_append(b, (const char *)ubytes, 16);
3710 }
3711 0 return 1;
3712 }
3713 0 case CT_IPV4: {
3714 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3715 0 int ok = 0;
3716 0 uint32_t v = parse_ipv4_to_u32(values[i], value_lens[i], &ok);
3717 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3718 0 nbuf_append(b, (const char *)&v, 4);
3719 }
3720 0 return 1;
3721 }
3722 0 case CT_IPV6: {
3723 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3724 unsigned char addr[16];
3725 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
3726 0 return 0;
3727 0 nbuf_append(b, (const char *)addr, 16);
3728 }
3729 0 return 1;
3730 }
3731 0 case CT_NULLABLE: {
3732 /* null bitmap + inner column */
3733 uint8_t *nulls;
3734 const char **inner_vals;
3735 size_t *inner_lens;
3736  
3737 0 Newx(nulls, nrows, uint8_t);
3738 0 ▶ Newxz(inner_vals, nrows, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(inner_vals, nrows, const char *); 0 0 0
3739 0 ▶ Newx(inner_lens, nrows, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(inner_lens, nrows, size_t); 0 0 0
3740  
3741 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3742 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
3743 0 nulls[i] = 1;
3744 0 nullable_inner_zero(ct->inner,
3745 0 &inner_vals[i], &inner_lens[i]);
3746 } else {
3747 0 nulls[i] = 0;
3748 0 inner_vals[i] = values[i];
3749 0 inner_lens[i] = value_lens[i];
3750 }
3751 }
3752  
3753 0 nbuf_append(b, (const char *)nulls, nrows);
3754 {
3755 0 int rc = encode_column_text(b, inner_vals, inner_lens, nrows, ct->inner);
3756 0 Safefree(nulls);
3757 0 Safefree(inner_vals);
3758 0 Safefree(inner_lens);
3759 0 return rc;
3760 }
3761 }
3762 0 case CT_LOWCARDINALITY: {
3763 /* Trivial 1:1 dictionary: each value is its own dict entry. */
3764 size_t idx_size;
3765 native_buf_t dict_buf;
3766 int rc;
3767  
3768 0 ▶ if (nrows == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return 1; 0 0 0
3769 0 ▶ if (ct->inner->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->inner->code == CT_NULLABLE) { 0 0 0
3770 /* Dict travels as plain T with slot 0 reserved for NULL:
3771 * [T default] + rows (nulls substituted), null->0 else i+1. */
3772 const char **dvals;
3773 size_t *dlens;
3774 uint8_t *nulls;
3775 const char *zp;
3776 size_t zlen;
3777 0 nullable_inner_zero(ct->inner->inner, &zp, &zlen);
3778 0 ▶ Newx(dvals, nrows + 1, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dvals, nrows + 1, const char *); 0 0 0
3779 0 ▶ Newx(dlens, nrows + 1, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dlens, nrows + 1, size_t); 0 0 0
3780 0 ▶ Newx(nulls, nrows ? nrows : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(nulls, nrows ? nrows : 1, uint8_t); 0 0 0
3781 0 dvals[0] = zp;
3782 0 dlens[0] = zlen;
3783 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3784 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
3785 0 nulls[i] = 1;
3786 0 dvals[i + 1] = zp;
3787 0 dlens[i + 1] = zlen;
3788 } else {
3789 0 nulls[i] = 0;
3790 0 dvals[i + 1] = values[i];
3791 0 dlens[i + 1] = value_lens[i];
3792 }
3793 }
3794 0 lc_write_keys_header(b, nrows + 1, &idx_size);
3795 0 nbuf_init(&dict_buf);
3796 0 rc = encode_column_text(&dict_buf, dvals, dlens, nrows + 1,
3797 0 ct->inner->inner);
3798 0 Safefree(dvals);
3799 0 Safefree(dlens);
3800 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
3801 0 nbuf_append(b, dict_buf.data, dict_buf.len);
3802 0 Safefree(dict_buf.data);
3803 0 lc_write_nullable_indices(b, nrows, idx_size, nulls);
3804 0 Safefree(nulls);
3805 0 return 1;
3806 }
3807  
3808 0 lc_write_keys_header(b, nrows, &idx_size);
3809 0 nbuf_init(&dict_buf);
3810 0 rc = encode_column_text(&dict_buf, values, value_lens, nrows, ct->inner);
3811 0 ▶ if (!rc) { Safefree(dict_buf.data); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { Safefree(dict_buf.data); return 0; } 0 0 0
3812 0 nbuf_append(b, dict_buf.data, dict_buf.len);
3813 0 Safefree(dict_buf.data);
3814 0 lc_write_indices(b, nrows, idx_size);
3815 0 return 1;
3816 }
3817 0 default:
3818 0 return 0; /* unsupported type — fall back to inline SQL */
3819 }
3820 }
3821  
3822 /* A plain number never reads as date text; stringifying it would only
3823 * allocate a PV in the caller's scalar. */
3824 #define SV_PLAIN_NUM(sv) (!SvGMAGICAL(sv) && !SvPOK(sv) && !SvROK(sv) \
3825 && (SvIOK(sv) || SvNOK(sv)))
3826  
3827 /* The text SvPV would give a plain number (same rules as sv_2pv: IV
3828 * first, 0 for zeroes, Inf/NaN, %g at NV_DIG), written to buf instead of
3829 * cached in the caller's scalar. buf holds SV_NUM_TEXT_MAX bytes. */
3830 #define SV_NUM_TEXT_MAX 64
3831  
3832 #if defined(__SIZEOF_INT128__) && NVSIZE == 8 && NV_DIG == 15
3833 #define HAVE_FAST_G15 1
3834 /* The 15 significant digits %.15g prints for 1e-13 <= |v| < 1e21: the
3835 * exact binary value rounded half-even, as glibc does. v equals
3836 * q * 10^(e10 - 14) after rounding, q in [10^14, 10^15). 0 = out of range. */
3837 0 static int g15_digits(double v, uint64_t *qp, int *e10p, int *negp) {
3838 static const uint64_t p5[28] = {
3839 1ULL, 5ULL, 25ULL, 125ULL, 625ULL, 3125ULL, 15625ULL, 78125ULL,
3840 390625ULL, 1953125ULL, 9765625ULL, 48828125ULL, 244140625ULL,
3841 1220703125ULL, 6103515625ULL, 30517578125ULL, 152587890625ULL,
3842 762939453125ULL, 3814697265625ULL, 19073486328125ULL,
3843 95367431640625ULL, 476837158203125ULL, 2384185791015625ULL,
3844 11920928955078125ULL, 59604644775390625ULL, 298023223876953125ULL,
3845 1490116119384765625ULL, 7450580596923828125ULL
3846 };
3847 0 const uint64_t lo15 = 100000000000000ULL, hi15 = 1000000000000000ULL;
3848 uint64_t bits, mant, q;
3849 int exp2, neg, e10, tries;
3850 double a;
3851 0 memcpy(&bits, &v, 8);
3852 0 neg = (int)(bits >> 63);
3853 0 exp2 = (int)((bits >> 52) & 0x7ff);
3854 0 ▶ if (exp2 == 0 || exp2 == 0x7ff) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (exp2 == 0 || exp2 == 0x7ff) return 0; 0 0 0
if (exp2 == 0 || exp2 == 0x7ff) return 0; 0 0 0
3855 0 ▶ a = neg ? -v : v;
Branch: 1 branch
BranchTrueFalseTotal
a = neg ? -v : v; 0 0 0
3856 0 ▶ if (!(a >= 1e-13 && a < 1e21)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(a >= 1e-13 && a < 1e21)) return 0; 0 0 0
if (!(a >= 1e-13 && a < 1e21)) return 0; 0 0 0
3857 0 mant = (bits & ((1ULL << 52) - 1)) | (1ULL << 52);
3858 0 exp2 -= 1075;
3859 /* floor(log10(a)) to within one, corrected below */
3860 0 e10 = ((exp2 + 52) * 78913) >> 18;
3861 /* Double arithmetic first: a * 10^k is off by at most 0.12 from the
3862 * exact product, so it decides the rounding unless it lands near .5
3863 * or a digit-count boundary. */
3864 0 ▶ for (tries = 0; tries < 3; tries++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tries = 0; tries < 3; tries++) { 0 0 0
3865 static const double p10d[23] = {
3866 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
3867 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
3868 };
3869 0 int k = 14 - e10;
3870 double y, f, r;
3871 0 ▶ if (k < -22 || k > 22) break;
Branch: 2 branches
BranchTrueFalseTotal
if (k < -22 || k > 22) break; 0 0 0
if (k < -22 || k > 22) break; 0 0 0
3872 0 ▶ y = k >= 0 ? a * p10d[k] : a / p10d[-k];
Branch: 1 branch
BranchTrueFalseTotal
y = k >= 0 ? a * p10d[k] : a / p10d[-k]; 0 0 0
3873 0 ▶ if (y < 1e14) { e10--; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (y < 1e14) { e10--; continue; } 0 0 0
3874 0 ▶ if (y >= 1e15) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (y >= 1e15) { e10++; continue; } 0 0 0
3875 0 ▶ if (y < 1e14 + 1 || y > 1e15 - 2) break;
Branch: 2 branches
BranchTrueFalseTotal
if (y < 1e14 + 1 || y > 1e15 - 2) break; 0 0 0
if (y < 1e14 + 1 || y > 1e15 - 2) break; 0 0 0
3876 0 f = floor(y);
3877 0 r = y - f;
3878 0 ▶ if (r > 0.3 && r < 0.7) break;
Branch: 2 branches
BranchTrueFalseTotal
if (r > 0.3 && r < 0.7) break; 0 0 0
if (r > 0.3 && r < 0.7) break; 0 0 0
3879 0 *qp = (uint64_t)f + (r >= 0.7);
3880 0 *e10p = e10;
3881 0 *negp = neg;
3882 0 return 1;
3883 }
3884 0 for (tries = 0; ; tries++) {
3885 0 int k = 14 - e10, s, up;
3886 unsigned __int128 n, r;
3887 0 ▶ if (tries > 3 || k < -7 || k > 27) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (tries > 3 || k < -7 || k > 27) return 0; 0 0 0
if (tries > 3 || k < -7 || k > 27) return 0; 0 0 0
if (tries > 3 || k < -7 || k > 27) return 0; 0 0 0
3888 0 ▶ if (k >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (k >= 0) { 0 0 0
3889 0 n = (unsigned __int128)mant * p5[k];
3890 0 s = k + exp2;
3891 0 ▶ if (s >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (s >= 0) { 0 0 0
3892 0 ▶ if (s > 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s > 10) return 0; 0 0 0
3893 0 q = (uint64_t)(n << s);
3894 0 up = 0;
3895 0 ▶ if ((n << s) >> 64) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((n << s) >> 64) return 0; 0 0 0
3896 } else {
3897 unsigned __int128 half;
3898 0 ▶ if (-s > 120) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (-s > 120) return 0; 0 0 0
3899 0 ▶ if ((n >> -s) >> 64) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if ((n >> -s) >> 64) { e10++; continue; } 0 0 0
3900 0 q = (uint64_t)(n >> -s);
3901 0 r = n & ((((unsigned __int128)1) << -s) - 1);
3902 0 half = ((unsigned __int128)1) << (-s - 1);
3903 0 ▶ up = r > half || (r == half && (q & 1));
Branch: 3 branches
BranchTrueFalseTotal
up = r > half || (r == half && (q & 1)); 0 0 0
up = r > half || (r == half && (q & 1)); 0 0 0
up = r > half || (r == half && (q & 1)); 0 0 0
3904 }
3905 } else {
3906 0 uint64_t d = p5[-k];
3907 0 s = exp2 + k;
3908 0 ▶ if (s < 0 || s > 60) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s < 0 || s > 60) return 0; 0 0 0
if (s < 0 || s > 60) return 0; 0 0 0
3909 0 n = (unsigned __int128)mant << s;
3910 0 ▶ if ((n / d) >> 64) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if ((n / d) >> 64) { e10++; continue; } 0 0 0
3911 0 q = (uint64_t)(n / d);
3912 0 r = n % d;
3913 0 ▶ up = 2 * r > d || (2 * r == d && (q & 1));
Branch: 3 branches
BranchTrueFalseTotal
up = 2 * r > d || (2 * r == d && (q & 1)); 0 0 0
up = 2 * r > d || (2 * r == d && (q & 1)); 0 0 0
up = 2 * r > d || (2 * r == d && (q & 1)); 0 0 0
3914 }
3915 0 ▶ if (q >= hi15) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (q >= hi15) { e10++; continue; } 0 0 0
3916 0 ▶ if (q < lo15) { e10--; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (q < lo15) { e10--; continue; } 0 0 0
3917 0 ▶ if (up) q++;
Branch: 1 branch
BranchTrueFalseTotal
if (up) q++; 0 0 0
3918 0 ▶ if (q == hi15) { q = lo15; e10++; }
Branch: 1 branch
BranchTrueFalseTotal
if (q == hi15) { q = lo15; e10++; } 0 0 0
3919 0 break;
3920 }
3921 0 *qp = q;
3922 0 *e10p = e10;
3923 0 *negp = neg;
3924 0 return 1;
3925 }
3926  
3927 /* snprintf(buf, "%.15g", v) where g15_digits applies, else 0. */
3928 0 static int fast_g15(double v, char *buf) {
3929 uint64_t q;
3930 int e10, neg;
3931 0 char dig[16], *p = buf;
3932 0 ▶ if (!g15_digits(v, &q, &e10, &neg)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!g15_digits(v, &q, &e10, &neg)) return 0; 0 0 0
3933 {
3934 0 int i, nd = 15;
3935 0 uint32_t hi = (uint32_t)(q / 100000000), lo = (uint32_t)(q % 100000000);
3936 0 ▶ for (i = 14; i >= 7; i--) { dig[i] = (char)('0' + lo % 10); lo /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 14; i >= 7; i--) { dig[i] = (char)('0' + lo % 10); lo /= 10; } 0 0 0
3937 0 ▶ for (i = 6; i >= 0; i--) { dig[i] = (char)('0' + hi % 10); hi /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 6; i >= 0; i--) { dig[i] = (char)('0' + hi % 10); hi /= 10; } 0 0 0
3938 0 ▶ while (nd > 1 && dig[nd - 1] == '0') nd--;
Branch: 2 branches
BranchTrueFalseTotal
while (nd > 1 && dig[nd - 1] == '0') nd--; 0 0 0
while (nd > 1 && dig[nd - 1] == '0') nd--; 0 0 0
3939 0 ▶ if (neg) *p++ = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) *p++ = '-'; 0 0 0
3940 0 ▶ if (e10 < -4 || e10 >= 15) {
Branch: 2 branches
BranchTrueFalseTotal
if (e10 < -4 || e10 >= 15) { 0 0 0
if (e10 < -4 || e10 >= 15) { 0 0 0
3941 0 int x = e10 < 0 ? -e10 : e10;
3942 0 *p++ = dig[0];
3943 0 ▶ if (nd > 1) { *p++ = '.'; for (i = 1; i < nd; i++) *p++ = dig[i]; }
Branch: 2 branches
BranchTrueFalseTotal
if (nd > 1) { *p++ = '.'; for (i = 1; i < nd; i++) *p++ = dig[i]; } 0 0 0
if (nd > 1) { *p++ = '.'; for (i = 1; i < nd; i++) *p++ = dig[i]; } 0 0 0
3944 0 *p++ = 'e';
3945 0 ▶ *p++ = e10 < 0 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
*p++ = e10 < 0 ? '-' : '+'; 0 0 0
3946 0 ▶ if (x >= 100) *p++ = (char)('0' + x / 100);
Branch: 1 branch
BranchTrueFalseTotal
if (x >= 100) *p++ = (char)('0' + x / 100); 0 0 0
3947 0 *p++ = (char)('0' + x / 10 % 10);
3948 0 *p++ = (char)('0' + x % 10);
3949 0 ▶ } else if (e10 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (e10 >= 0) { 0 0 0
3950 0 ▶ for (i = 0; i <= e10; i++) *p++ = i < 15 ? dig[i] : '0';
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i <= e10; i++) *p++ = i < 15 ? dig[i] : '0'; 0 0 0
for (i = 0; i <= e10; i++) *p++ = i < 15 ? dig[i] : '0'; 0 0 0
3951 0 ▶ if (nd > e10 + 1) { *p++ = '.'; for (i = e10 + 1; i < nd; i++) *p++ = dig[i]; }
Branch: 2 branches
BranchTrueFalseTotal
if (nd > e10 + 1) { *p++ = '.'; for (i = e10 + 1; i < nd; i++) *p++ = dig[i]; } 0 0 0
if (nd > e10 + 1) { *p++ = '.'; for (i = e10 + 1; i < nd; i++) *p++ = dig[i]; } 0 0 0
3952 } else {
3953 0 *p++ = '0';
3954 0 *p++ = '.';
3955 0 ▶ for (i = 0; i < -e10 - 1; i++) *p++ = '0';
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < -e10 - 1; i++) *p++ = '0'; 0 0 0
3956 0 ▶ for (i = 0; i < nd; i++) *p++ = dig[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) *p++ = dig[i]; 0 0 0
3957 }
3958 }
3959 0 *p = '\0';
3960 0 return (int)(p - buf);
3961 }
3962 #endif
3963  
3964 0 static const char *sv_num_text(pTHX_ SV *sv, char *buf, STRLEN *len) {
3965 NV nv;
3966 int n;
3967 0 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 0 0 0
3968 0 n = SvIsUV(sv) ? snprintf(buf, SV_NUM_TEXT_MAX, "%" UVuf, SvUVX(sv))
3969 0 ▶ : snprintf(buf, SV_NUM_TEXT_MAX, "%" IVdf, SvIVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
: snprintf(buf, SV_NUM_TEXT_MAX, "%" IVdf, SvIVX(sv)); 0 0 0
3970 } else {
3971 0 nv = SvNVX(sv);
3972 0 ▶ if (nv == 0.0 && !Perl_isnan(nv))
Branch: 2 branches
BranchTrueFalseTotal
if (nv == 0.0 && !Perl_isnan(nv)) 0 0 0
if (nv == 0.0 && !Perl_isnan(nv)) 0 0 0
3973 0 n = snprintf(buf, SV_NUM_TEXT_MAX, "0");
3974 0 ▶ else if (Perl_isnan(nv))
Branch: 1 branch
BranchTrueFalseTotal
else if (Perl_isnan(nv)) 0 0 0
3975 0 n = snprintf(buf, SV_NUM_TEXT_MAX, "NaN");
3976 0 ▶ else if (Perl_isinf(nv))
Branch: 1 branch
BranchTrueFalseTotal
else if (Perl_isinf(nv)) 0 0 0
3977 0 ▶ n = snprintf(buf, SV_NUM_TEXT_MAX, nv > 0 ? "Inf" : "-Inf");
Branch: 1 branch
BranchTrueFalseTotal
n = snprintf(buf, SV_NUM_TEXT_MAX, nv > 0 ? "Inf" : "-Inf"); 0 0 0
3978 else {
3979 #ifdef HAVE_FAST_G15
3980 0 n = fast_g15((double)nv, buf);
3981 0 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 0 0 0
3982 #endif
3983 0 n = snprintf(buf, SV_NUM_TEXT_MAX, "%.*" NVgf, NV_DIG, nv);
3984 }
3985 }
3986 0 *len = (STRLEN)n;
3987 0 return buf;
3988 }
3989  
3990 /* SvPV(sv, len) without caching a string in a plain number. */
3991 #define SV_TEXT(sv, buf, len) \
3992 (SV_PLAIN_NUM(sv) ? sv_num_text(aTHX_ (sv), (buf), &(len)) : SvPV((sv), (len)))
3993  
3994 /* parse_decimal_to_raw() of the text SvPV gives a plain number, computed
3995 * from the number: 1 when done (*ok the verdict), 0 to parse the text. */
3996 0 static int num_decimal_raw(pTHX_ SV *sv, int scale, int precision,
3997 int64_t *raw, int *ok) {
3998 static const uint64_t p10[19] = {
3999 1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL,
4000 10000000ULL, 100000000ULL, 1000000000ULL, 10000000000ULL,
4001 100000000000ULL, 1000000000000ULL, 10000000000000ULL,
4002 100000000000000ULL, 1000000000000000ULL, 10000000000000000ULL,
4003 100000000000000000ULL, 1000000000000000000ULL
4004 };
4005 uint64_t mag, t;
4006 0 int neg = 0, int_digits = 0;
4007 0 *ok = 0;
4008 0 ▶ if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18
Branch: 7 branches
BranchTrueFalseTotal
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18 0 0 0
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18 0 0 0
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18 0 0 0
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18 0 0 0
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18 0 0 0
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18 0 0 0
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18 0 0 0
4009 0 ▶ || precision < 1 || precision > 18)
Branch: 2 branches
BranchTrueFalseTotal
|| precision < 1 || precision > 18) 0 0 0
|| precision < 1 || precision > 18) 0 0 0
4010 0 return 0;
4011 0 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 0 0 0
4012 0 ▶ if (SvIsUV(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) { 0 0 0
4013 0 mag = SvUVX(sv);
4014 } else {
4015 0 IV iv = SvIVX(sv);
4016 0 neg = iv < 0;
4017 0 ▶ mag = neg ? (uint64_t)0 - (uint64_t)iv : (uint64_t)iv;
Branch: 1 branch
BranchTrueFalseTotal
mag = neg ? (uint64_t)0 - (uint64_t)iv : (uint64_t)iv; 0 0 0
4018 }
4019 0 ▶ for (t = mag; t; t /= 10) int_digits++;
Branch: 1 branch
BranchTrueFalseTotal
for (t = mag; t; t /= 10) int_digits++; 0 0 0
4020 0 ▶ if (int_digits > precision - scale) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 1; 0 0 0
4021 0 mag *= p10[scale];
4022 } else {
4023 #ifdef HAVE_FAST_G15
4024 0 NV nv = SvNVX(sv);
4025 uint64_t q;
4026 int e10, sh;
4027 0 ▶ if (nv == 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nv == 0.0) { 0 0 0
4028 0 *raw = 0;
4029 0 *ok = 1;
4030 0 return 1;
4031 }
4032 0 ▶ if (!g15_digits((double)nv, &q, &e10, &neg)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!g15_digits((double)nv, &q, &e10, &neg)) return 0; 0 0 0
4033 0 int_digits = e10 >= 0 ? e10 + 1 : 0;
4034 0 ▶ if (int_digits > precision - scale) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 1; 0 0 0
4035 0 sh = e10 - 14 + scale;
4036 0 ▶ mag = sh >= 0 ? q * p10[sh] : -sh > 18 ? 0 : q / p10[-sh];
Branch: 2 branches
BranchTrueFalseTotal
mag = sh >= 0 ? q * p10[sh] : -sh > 18 ? 0 : q / p10[-sh]; 0 0 0
mag = sh >= 0 ? q * p10[sh] : -sh > 18 ? 0 : q / p10[-sh]; 0 0 0
4037 #else
4038 return 0;
4039 #endif
4040 }
4041 0 ▶ *raw = neg ? -(int64_t)mag : (int64_t)mag;
Branch: 1 branch
BranchTrueFalseTotal
*raw = neg ? -(int64_t)mag : (int64_t)mag; 0 0 0
4042 0 *ok = 1;
4043 0 return 1;
4044 }
4045  
4046 /*
4047 * Encode a column of Perl SV values into native binary format.
4048 * Like encode_column_text() but takes SVs directly — no TSV parsing/unescaping.
4049 * Returns 1 on success, 0 if type is unsupported.
4050 */
4051 28 static int encode_column_sv(pTHX_ native_buf_t *b,
4052 SV **values, uint64_t nrows,
4053 col_type_t *ct) {
4054 uint64_t i;
4055 char nbuf_num[SV_NUM_TEXT_MAX];
4056  
4057 28 switch (ct->code) {
4058 0 case CT_INT8: {
4059 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4060 0 int ok = 0;
4061 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
4062 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
4063 {
4064 0 int8_t v = (int8_t)w;
4065 0 nbuf_append(b, (const char *)&v, 1);
4066 }
4067 }
4068 0 return 1;
4069 }
4070 0 case CT_ENUM8: case CT_ENUM16: {
4071 0 enum_entry_t *entries = NULL;
4072 0 size_t nentries = enum_parse_entries(ct->type_str, ct->type_str_len, &entries);
4073 0 int numeric_labels = enum_has_numeric_label(entries, nentries);
4074 0 int rc = 1;
4075 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4076 0 SV *v = values[i];
4077 0 int code, ok = 0;
4078 0 ▶ if (!SvOK(v)) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { rc = 0; break; } 0 0 0
4079 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
4080 STRLEN vlen;
4081 0 const char *s = SvPV(v, vlen);
4082 0 ok = enum_code_for_label(entries, nentries, s, vlen, &code);
4083 }
4084 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4085 0 int64_t w = sv_to_int64_strict(aTHX_ v, &ok);
4086 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
4087 0 code = (int)w;
4088 }
4089 0 nbuf_enum(b, ct->code, code);
4090 }
4091 0 enum_free_entries(entries, nentries);
4092 0 return rc;
4093 }
4094 0 case CT_INT16: {
4095 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4096 0 int ok = 0;
4097 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
4098 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
4099 {
4100 0 int16_t v = (int16_t)w;
4101 0 nbuf_append(b, (const char *)&v, 2);
4102 }
4103 }
4104 0 return 1;
4105 }
4106 0 case CT_INT32: case CT_DATE32: {
4107 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4108 int32_t v;
4109 STRLEN vlen;
4110 const char *s;
4111 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4112 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
4113 0 else s = SvPV(values[i], vlen);
4114 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
4115 0 ▶ if (vlen != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen != 10) return 0; 0 0 0
4116 0 ▶ if (!valid_date_prefix(s, vlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, vlen)) return 0; 0 0 0
4117 0 v = date_string_to_days(s, vlen);
4118 } else {
4119 0 int ok = 0;
4120 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
4121 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
4122 0 return 0;
4123 0 v = (int32_t)w;
4124 }
4125 0 nbuf_append(b, (const char *)&v, 4);
4126 }
4127 0 return 1;
4128 }
4129 0 case CT_INT64: {
4130 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4131 0 int ok = 0;
4132 0 int64_t v = sv_to_int64_strict(aTHX_ values[i], &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_UINT8: {
4139 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4140 0 int ok = 0;
4141 0 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
4142 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
4143 {
4144 0 uint8_t v = (uint8_t)w;
4145 0 nbuf_append(b, (const char *)&v, 1);
4146 }
4147 }
4148 0 return 1;
4149 }
4150 0 case CT_BOOL: {
4151 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4152 uint8_t v;
4153 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4154 0 v = SvTRUE(values[i]) ? 1 : 0;
4155 0 nbuf_append(b, (const char *)&v, 1);
4156 }
4157 0 return 1;
4158 }
4159 0 case CT_UINT16: case CT_DATE: {
4160 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4161 uint16_t v;
4162 STRLEN vlen;
4163 const char *s;
4164 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4165 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
4166 0 else s = SvPV(values[i], vlen);
4167 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
4168 int32_t days;
4169 0 ▶ if (vlen != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen != 10) return 0; 0 0 0
4170 0 ▶ if (!valid_date_prefix(s, vlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, vlen)) return 0; 0 0 0
4171 0 days = date_string_to_days(s, vlen);
4172 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
4173 0 v = (uint16_t)days;
4174 } else {
4175 0 int ok = 0;
4176 0 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
4177 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
4178 0 v = (uint16_t)w;
4179 }
4180 0 nbuf_append(b, (const char *)&v, 2);
4181 }
4182 0 return 1;
4183 }
4184 2 case CT_UINT32: case CT_DATETIME: {
4185 2 int rc = 1;
4186 4 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 2 2 4
4187 uint32_t v;
4188 STRLEN vlen;
4189 const char *s;
4190 2 ▶ if (!SvOK(values[i])) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { rc = 0; break; } 0 2 2
4191 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
4192 0 else s = SvPV(values[i], vlen);
4193 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
4194 int32_t days;
4195 int64_t total;
4196 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
4197 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
4198 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
4199 0 days = date_string_to_days(s, 10);
4200 0 total = (int64_t)days * 86400 + parse_hms(s, vlen);
4201 0 ▶ if (ct->tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->tz) { 0 0 0
4202 0 int tok = 0;
4203 0 total = wall_in_tz_to_epoch(total, ct, &tok);
4204 0 ▶ if (!tok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) { rc = 0; break; } 0 0 0
4205 }
4206 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
4207 0 v = (uint32_t)total;
4208 } else {
4209 2 int ok = 0;
4210 2 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
4211 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
4212 2 v = (uint32_t)w;
4213 }
4214 2 nbuf_append(b, (const char *)&v, 4);
4215 }
4216 2 return rc;
4217 }
4218 0 case CT_UINT64: {
4219 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4220 0 int ok = 0;
4221 0 uint64_t v = sv_to_uint64_strict(aTHX_ values[i], &ok);
4222 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4223 0 nbuf_append(b, (const char *)&v, 8);
4224 }
4225 0 return 1;
4226 }
4227 0 case CT_FLOAT32: {
4228 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4229 float v;
4230 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4231 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4232 0 v = (float)SvNV(values[i]);
4233 } else {
4234 STRLEN vlen;
4235 0 ▶ const char *s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4236 char *endp;
4237 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4238 0 v = strtof(s, &endp);
4239 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4240 }
4241 0 nbuf_append(b, (const char *)&v, 4);
4242 }
4243 0 return 1;
4244 }
4245 0 case CT_FLOAT64: {
4246 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4247 double v;
4248 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4249 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4250 0 v = SvNV(values[i]);
4251 } else {
4252 STRLEN vlen;
4253 0 ▶ const char *s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4254 char *endp;
4255 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4256 0 v = strtod(s, &endp);
4257 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4258 }
4259 0 nbuf_append(b, (const char *)&v, 8);
4260 }
4261 0 return 1;
4262 }
4263 26 case CT_DATETIME64: {
4264 26 int rc = 1;
4265 28 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 18 10 28
4266 int64_t v;
4267 34 ▶ if (!SvOK(values[i])) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { rc = 0; break; } 0 18 18
4268 18 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 18 18
4269 0 v = (int64_t)SvIV(values[i]);
4270 } else {
4271 STRLEN vlen;
4272 36 ▶ const char *s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 18 0 18
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 18 18
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4273 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
4274 18 int ok = 0;
4275 18 v = parse_datetime64_to_scaled(s, vlen, ct->param, ct, &ok);
4276 18 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 16 2 18
4277 } else {
4278 0 int ok = 0;
4279 0 v = parse_int64_strict(s, vlen, &ok);
4280 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
4281 }
4282 }
4283 2 nbuf_append(b, (const char *)&v, 8);
4284 }
4285 26 return rc;
4286 }
4287 0 case CT_TIME: {
4288 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4289 int64_t v;
4290 int32_t w;
4291 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4292 0 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 0 0
4293 0 v = (int64_t)SvIV(values[i]);
4294 } else {
4295 STRLEN vlen;
4296 0 ▶ const char *s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4297 0 int ok = 0;
4298 0 v = parse_time_text(s, vlen, 0, &ok);
4299 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4300 0 v = parse_int64_strict(s, vlen, &ok);
4301 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4302 }
4303 }
4304 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
4305 0 w = (int32_t)v;
4306 0 nbuf_append(b, (const char *)&w, 4);
4307 }
4308 0 return 1;
4309 }
4310 0 case CT_TIME64: {
4311 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4312 int64_t v;
4313 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4314 0 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 0 0
4315 0 v = (int64_t)SvIV(values[i]);
4316 } else {
4317 STRLEN vlen;
4318 0 ▶ const char *s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4319 0 int ok = 0;
4320 0 v = parse_time_text(s, vlen, ct->param, &ok);
4321 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4322 0 v = parse_int64_strict(s, vlen, &ok);
4323 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4324 }
4325 }
4326 0 nbuf_append(b, (const char *)&v, 8);
4327 }
4328 0 return 1;
4329 }
4330 0 case CT_DECIMAL32: {
4331 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4332 STRLEN vlen;
4333 const char *s;
4334 0 int ok = 0;
4335 int32_t v;
4336 int64_t raw;
4337 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4338 0 ▶ if (!num_decimal_raw(aTHX_ values[i], ct->param, ct->precision,
Branch: 1 branch
BranchTrueFalseTotal
if (!num_decimal_raw(aTHX_ values[i], ct->param, ct->precision, 0 0 0
4339 &raw, &ok)) {
4340 0 ▶ s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4341 0 raw = parse_decimal_to_raw(s, vlen, ct->param,
4342 ct->precision, &ok);
4343 }
4344 0 v = (int32_t)raw;
4345 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4346 0 nbuf_append(b, (const char *)&v, 4);
4347 }
4348 0 return 1;
4349 }
4350 0 case CT_DECIMAL64: {
4351 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4352 STRLEN vlen;
4353 const char *s;
4354 0 int ok = 0;
4355 int64_t v;
4356 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4357 0 ▶ if (!num_decimal_raw(aTHX_ values[i], ct->param, ct->precision,
Branch: 1 branch
BranchTrueFalseTotal
if (!num_decimal_raw(aTHX_ values[i], ct->param, ct->precision, 0 0 0
4358 &v, &ok)) {
4359 0 ▶ s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4360 0 v = parse_decimal_to_raw(s, vlen, ct->param, ct->precision, &ok);
4361 }
4362 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4363 0 nbuf_append(b, (const char *)&v, 8);
4364 }
4365 0 return 1;
4366 }
4367 0 case CT_DECIMAL128: {
4368 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4369 STRLEN vlen;
4370 const char *s;
4371 unsigned char raw[16];
4372 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4373 0 ▶ s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4374 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
4375 raw))
4376 0 return 0;
4377 0 nbuf_append(b, (const char *)raw, 16);
4378 }
4379 0 return 1;
4380 }
4381 0 case CT_DECIMAL256: {
4382 /* Decimal text, or raw 32 bytes (LE, signed) — the shape the
4383 * decoder delivers. Useful with Math::BigInt::to_bytes(LE).
4384 * The raw fallback needs a non-text byte, so a 32-char typo
4385 * fails loudly instead of storing garbage. */
4386 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4387 STRLEN slen;
4388 const char *s;
4389 unsigned char raw[32];
4390 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4391 0 ▶ s = SV_TEXT(values[i], nbuf_num, slen);
Branch: 5 branches
BranchTrueFalseTotal
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
4392 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
4393 0 nbuf_append(b, (const char *)raw, 32);
4394 0 ▶ } else if (slen == 32) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (slen == 32) { 0 0 0
4395 STRLEN k;
4396 0 int binary = 0;
4397 0 ▶ for (k = 0; k < slen; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < slen; k++) { 0 0 0
4398 0 unsigned char c = (unsigned char)s[k];
4399 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
4400 }
4401 0 ▶ if (!binary) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!binary) return 0; 0 0 0
4402 0 nbuf_append(b, s, 32);
4403 } else {
4404 0 return 0;
4405 }
4406 }
4407 0 return 1;
4408 }
4409 0 case CT_BFLOAT16: {
4410 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4411 float fv;
4412 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4413 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4414 0 fv = (float)SvNV(values[i]);
4415 } else {
4416 STRLEN vlen;
4417 0 ▶ const char *s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
const char *s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4418 char *endp;
4419 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4420 0 fv = (float)strtod(s, &endp);
4421 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4422 }
4423 0 uint8_t b32[4]; memcpy(b32, &fv, 4);
4424 /* Truncate to top 16 bits — bytes [2,3] of a LE Float32. */
4425 0 nbuf_append(b, (const char *)&b32[2], 2);
4426 }
4427 0 return 1;
4428 }
4429 0 case CT_STRING: {
4430 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4431 STRLEN vlen;
4432 const char *v;
4433 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4434 0 ▶ v = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4435 0 nbuf_string(b, v, vlen);
4436 }
4437 0 return 1;
4438 }
4439 0 case CT_FIXEDSTRING: {
4440 0 size_t fsz = (size_t)ct->param;
4441 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4442 STRLEN vlen;
4443 const char *v;
4444 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4445 0 ▶ v = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
v = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4446 0 ▶ if ((size_t)vlen > fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)vlen > fsz) return 0; 0 0 0
4447 0 nbuf_grow(b, fsz);
4448 0 memcpy(b->data + b->len, v, vlen);
4449 0 ▶ if ((size_t)vlen < fsz)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)vlen < fsz) 0 0 0
4450 0 memset(b->data + b->len + vlen, 0, fsz - (size_t)vlen);
4451 0 b->len += fsz;
4452 }
4453 0 return 1;
4454 }
4455 0 case CT_UUID: {
4456 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4457 unsigned char ubytes[16];
4458 STRLEN slen;
4459 const char *s;
4460 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4461 0 ▶ s = SV_TEXT(values[i], nbuf_num, slen);
Branch: 5 branches
BranchTrueFalseTotal
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, slen); 0 0 0
4462 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
4463 0 nbuf_append(b, (const char *)ubytes, 16);
4464 }
4465 0 return 1;
4466 }
4467 0 case CT_IPV4: {
4468 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4469 STRLEN vlen;
4470 const char *s;
4471 uint32_t v;
4472 0 int ok = 0;
4473 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4474 0 ▶ s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4475 0 v = parse_ipv4_to_u32(s, (size_t)vlen, &ok);
4476 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4477 0 nbuf_append(b, (const char *)&v, 4);
4478 }
4479 0 return 1;
4480 }
4481 0 case CT_IPV6: {
4482 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4483 unsigned char addr[16];
4484 STRLEN vlen;
4485 const char *s;
4486 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4487 0 ▶ s = SV_TEXT(values[i], nbuf_num, vlen);
Branch: 5 branches
BranchTrueFalseTotal
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
s = SV_TEXT(values[i], nbuf_num, vlen); 0 0 0
4488 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
4489 0 nbuf_append(b, (const char *)addr, 16);
4490 }
4491 0 return 1;
4492 }
4493 0 case CT_NULLABLE: {
4494 uint8_t *nulls;
4495 SV **inner_vals;
4496 SV *zero_sv;
4497  
4498 0 Newx(nulls, nrows, uint8_t);
4499 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
4500 0 zero_sv = nullable_inner_zero_sv(aTHX_ ct->inner);
4501  
4502 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4503 0 ▶ if (!SvOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { 0 0 0
4504 0 nulls[i] = 1;
4505 0 inner_vals[i] = zero_sv;
4506 } else {
4507 0 nulls[i] = 0;
4508 0 inner_vals[i] = values[i];
4509 }
4510 }
4511  
4512 0 nbuf_append(b, (const char *)nulls, nrows);
4513 {
4514 0 int rc = encode_column_sv(aTHX_ b, inner_vals, nrows, ct->inner);
4515 0 Safefree(nulls);
4516 0 Safefree(inner_vals);
4517 0 SvREFCNT_dec(zero_sv);
4518 0 return rc;
4519 }
4520 }
4521 0 case CT_LOWCARDINALITY: {
4522 size_t idx_size;
4523 native_buf_t dict_buf;
4524 int rc;
4525  
4526 0 ▶ if (nrows == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return 1; 0 0 0
4527 0 ▶ if (ct->inner->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->inner->code == CT_NULLABLE) { 0 0 0
4528 /* Dict travels as plain T with slot 0 reserved for NULL:
4529 * [T default] + rows (nulls substituted), null->0 else i+1. */
4530 SV **dvals;
4531 uint8_t *nulls;
4532 0 SV *zero_sv = nullable_inner_zero_sv(aTHX_ ct->inner->inner);
4533 0 ▶ Newx(dvals, nrows + 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dvals, nrows + 1, SV *); 0 0 0
4534 0 ▶ Newx(nulls, nrows ? nrows : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(nulls, nrows ? nrows : 1, uint8_t); 0 0 0
4535 0 dvals[0] = zero_sv;
4536 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4537 0 ▶ if (!SvOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { 0 0 0
4538 0 nulls[i] = 1;
4539 0 dvals[i + 1] = zero_sv;
4540 } else {
4541 0 nulls[i] = 0;
4542 0 dvals[i + 1] = values[i];
4543 }
4544 }
4545 0 lc_write_keys_header(b, nrows + 1, &idx_size);
4546 0 nbuf_init(&dict_buf);
4547 0 rc = encode_column_sv(aTHX_ &dict_buf, dvals, nrows + 1,
4548 0 ct->inner->inner);
4549 0 Safefree(dvals);
4550 0 SvREFCNT_dec(zero_sv);
4551 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
4552 0 nbuf_append(b, dict_buf.data, dict_buf.len);
4553 0 Safefree(dict_buf.data);
4554 0 lc_write_nullable_indices(b, nrows, idx_size, nulls);
4555 0 Safefree(nulls);
4556 0 return 1;
4557 }
4558  
4559 0 lc_write_keys_header(b, nrows, &idx_size);
4560 0 nbuf_init(&dict_buf);
4561 0 rc = encode_column_sv(aTHX_ &dict_buf, values, nrows, ct->inner);
4562 0 ▶ if (!rc) { Safefree(dict_buf.data); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { Safefree(dict_buf.data); return 0; } 0 0 0
4563 0 nbuf_append(b, dict_buf.data, dict_buf.len);
4564 0 Safefree(dict_buf.data);
4565 0 lc_write_indices(b, nrows, idx_size);
4566 0 return 1;
4567 }
4568 0 case CT_ARRAY: {
4569 /* Each value must be an AV ref. Wire format: offsets + flat inner data */
4570 0 uint64_t total = 0;
4571 uint64_t *offsets;
4572 SV **all_elems;
4573 0 uint64_t pos = 0;
4574 int rc;
4575  
4576 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4577 AV *av;
4578 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
4579 0 return 0;
4580 0 av = (AV *)SvRV(values[i]);
4581 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
4582 }
4583  
4584 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
4585 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
4586  
4587 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4588 0 AV *av = (AV *)SvRV(values[i]);
4589 0 SSize_t n = av_len(av) + 1, j;
4590 /* av_fetch on a tie returns a lazy mortal proxy: copy the
4591 * fetched value (borrowing the proxy would re-FETCH on
4592 * every later read). Plain arrays borrow as before. */
4593 0 int av_magical = SvRMAGICAL((SV *)av);
4594 /* A tie whose FETCHSIZE grew since the sizing pass would
4595 * overflow all_elems: fail clean instead. */
4596 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
4597 0 goto arr_oom;
4598 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
4599 0 SV **ep = av_fetch(av, j, 0);
4600 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
4601 /* Fetch exactly once: a borrowed proxy re-FETCHes on
4602 * every later read, and a later SvPV would too. */
4603 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
4604 0 elem = sv_2mortal(newSVsv(elem));
4605 0 all_elems[pos++] = elem;
4606 }
4607 0 offsets[i] = pos;
4608 }
4609 /* Encode what was actually gathered: a tie that shrank since
4610 * the sizing pass must not leave total pointing past pos. */
4611 0 total = pos;
4612  
4613 /* write offsets as uint64 LE */
4614 0 nbuf_append(b, (const char *)offsets, nrows * 8);
4615 0 rc = encode_column_sv(aTHX_ b, all_elems, total, ct->inner);
4616 0 Safefree(offsets);
4617 0 Safefree(all_elems);
4618 0 return rc;
4619 0 arr_oom:
4620 0 Safefree(offsets);
4621 0 Safefree(all_elems);
4622 0 return 0;
4623 }
4624 0 case CT_TUPLE: {
4625 /* Each value must be an AV ref with num_inners elements */
4626 int j;
4627 0 ▶ if (ct->num_inners == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners == 0) { 0 0 0
4628 /* Empty Tuple(): 1 placeholder byte per row on the wire */
4629 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4630 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
4631 0 return 0;
4632 0 nbuf_append(b, "0", 1);
4633 }
4634 0 return 1;
4635 }
4636 0 ▶ for (j = 0; j < ct->num_inners; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) { 0 0 0
4637 SV **col_vals;
4638 int rc;
4639 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
4640 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4641 AV *av;
4642 SV **ep;
4643 SV *elem;
4644 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
4645 0 Safefree(col_vals);
4646 0 return 0;
4647 }
4648 0 av = (AV *)SvRV(values[i]);
4649 0 ep = av_fetch(av, j, 0);
4650 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
4651 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
4652 0 elem = sv_2mortal(newSVsv(elem));
4653 0 col_vals[i] = elem;
4654 }
4655 0 rc = encode_column_sv(aTHX_ b, col_vals, nrows, ct->inners[j]);
4656 0 Safefree(col_vals);
4657 0 ▶ if (!rc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return 0; 0 0 0
4658 }
4659 0 return 1;
4660 }
4661 0 case CT_MAP: {
4662 /* Each value must be a hashref. Wire format: offsets + key column + value column */
4663 0 uint64_t total = 0;
4664 0 uint64_t cap = 0;
4665 uint64_t *offsets;
4666 SV **all_keys, **all_vals;
4667 0 uint64_t pos = 0;
4668 int rc;
4669  
4670 0 ▶ if (ct->num_inners != 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners != 2) return 0; 0 0 0
4671  
4672 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4673 HV *hv;
4674 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
4675 0 return 0;
4676 0 hv = (HV *)SvRV(values[i]);
4677 /* sizing hint only: HvUSEDKEYS is meaningless for tied
4678 * hashes, so the fill loop grows below as needed. */
4679 0 ▶ total += HvUSEDKEYS(hv);
Branch: 1 branch
BranchTrueFalseTotal
total += HvUSEDKEYS(hv); 0 0 0
4680 }
4681  
4682 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
4683 0 ▶ cap = total ? total : 1;
Branch: 1 branch
BranchTrueFalseTotal
cap = total ? total : 1; 0 0 0
4684 0 ▶ Newx(all_keys, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(all_keys, cap, SV *); 0 0 0
4685 0 ▶ Newx(all_vals, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(all_vals, cap, SV *); 0 0 0
4686  
4687 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4688 0 HV *hv = (HV *)SvRV(values[i]);
4689 HE *he;
4690 /* hv_iterval on a tie returns a lazy mortal FETCH proxy:
4691 * copy the fetched value (borrowing the proxy would
4692 * re-FETCH on every later read, and its fake-HE key
4693 * is not safe to borrow either). */
4694 0 int hv_magical = SvRMAGICAL((SV *)hv);
4695 0 hv_iterinit(hv);
4696 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
4697 I32 klen;
4698 SV *vsv;
4699 0 char *key = hv_iterkey(he, &klen);
4700 0 ▶ if (pos >= cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= cap) { 0 0 0
4701 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
4702 0 goto map_oom;
4703 0 cap *= 2;
4704 0 ▶ Renew(all_keys, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(all_keys, cap, SV *); 0 0 0
4705 0 ▶ Renew(all_vals, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(all_vals, cap, SV *); 0 0 0
4706 }
4707 0 vsv = hv_iterval(hv, he);
4708 0 ▶ if (hv_magical) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_magical) { 0 0 0
4709 0 all_keys[pos] = sv_2mortal(newSVpvn(key, klen));
4710 0 all_vals[pos] = sv_2mortal(newSVsv(vsv));
4711 } else {
4712 0 all_keys[pos] = hv_iterkeysv(he);
4713 0 all_vals[pos] = SvGMAGICAL(vsv)
4714 0 ▶ ? sv_2mortal(newSVsv(vsv)) : vsv;
Branch: 1 branch
BranchTrueFalseTotal
? sv_2mortal(newSVsv(vsv)) : vsv; 0 0 0
4715 }
4716 0 pos++;
4717 }
4718 0 offsets[i] = pos;
4719 }
4720 0 total = pos;
4721  
4722 0 nbuf_append(b, (const char *)offsets, nrows * 8);
4723 0 rc = encode_column_sv(aTHX_ b, all_keys, total, ct->inners[0]);
4724 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
4725 0 Safefree(offsets);
4726 0 Safefree(all_keys);
4727 0 Safefree(all_vals);
4728 0 return rc;
4729 0 map_oom:
4730 0 Safefree(offsets);
4731 0 Safefree(all_keys);
4732 0 Safefree(all_vals);
4733 0 return 0;
4734 }
4735 0 case CT_JSON: {
4736 /* Wire layout (V1) per Clickhouse-Encoder/doc/json-research:
4737 * Object prefix: UInt64=0, varint K (twice), K * lenstr path
4738 * Per-path Dynamic prefix: UInt64=1, varint T (twice),
4739 * T * lenstr type-name, UInt64=0
4740 * Per-path data: N disc bytes, then per-variant data in lex
4741 * order including a SharedVariant slot
4742 * Shared data trailer: N * UInt64 LE zero (no shared paths)
4743 */
4744 0 ENTER; SAVETMPS;
4745 int p;
4746 0 HV **row_hvs = NULL;
4747 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
4748 0 ▶ Newxz(row_hvs, nrows, HV*);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(row_hvs, nrows, HV*); 0 0 0
4749 0 SAVEFREEPV(row_hvs);
4750 }
4751 0 HV *all_paths = (HV*)sv_2mortal((SV*)newHV());
4752  
4753 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4754 0 SV *val = values[i];
4755 0 ▶ if (!SvOK(val)) { row_hvs[i] = NULL; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { row_hvs[i] = NULL; continue; } 0 0 0
4756 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
4757 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4758 0 return 0;
4759 }
4760 0 HV *flat = (HV*)sv_2mortal((SV*)newHV());
4761 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
4762 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4763 0 return 0;
4764 }
4765 0 row_hvs[i] = flat;
4766 }
4767  
4768 /* Typed paths: extract per-row values from each flat HV (and
4769 * hv_delete to keep them out of the dynamic-paths discovery).
4770 * Missing keys substitute undef as a placeholder; the inner
4771 * encoder treats undef as the type's zero/default. */
4772 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
4773 0 SV ***typed_vals = NULL;
4774 0 ▶ if (n_typed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_typed > 0) { 0 0 0
4775 0 Newxz(typed_vals, n_typed, SV **);
4776 0 SAVEFREEPV(typed_vals);
4777 int tp;
4778 0 ▶ for (tp = 0; tp < n_typed; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) { 0 0 0
4779 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
4780 0 SAVEFREEPV(typed_vals[tp]);
4781 0 size_t nlen = strlen(ct->inner_names[tp]);
4782 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4783 0 ▶ if (!row_hvs[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) { 0 0 0
4784 0 typed_vals[tp][i] = &PL_sv_undef;
4785 0 continue;
4786 }
4787 0 SV **e = hv_fetch(row_hvs[i], ct->inner_names[tp],
4788 (I32)nlen, 0);
4789 0 ▶ if (e && SvOK(*e)) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) { 0 0 0
if (e && SvOK(*e)) { 0 0 0
4790 0 typed_vals[tp][i] = SvREFCNT_inc(*e);
4791 0 sv_2mortal(typed_vals[tp][i]);
4792 0 hv_delete(row_hvs[i], ct->inner_names[tp],
4793 (I32)nlen, G_DISCARD);
4794 } else {
4795 0 typed_vals[tp][i] = &PL_sv_undef;
4796 }
4797 }
4798 }
4799 }
4800  
4801 /* Now collect dynamic-path keys (typed paths already removed). */
4802 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4803 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4804 0 hv_iterinit(row_hvs[i]);
4805 HE *he;
4806 0 ▶ while ((he = hv_iternext(row_hvs[i]))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(row_hvs[i]))) { 0 0 0
4807 I32 klen;
4808 0 char *kstr = hv_iterkey(he, &klen);
4809 0 (void)hv_store(all_paths, kstr, klen, newSViv(1), 0);
4810 }
4811 }
4812  
4813 0 ▶ int num_paths = (int)HvUSEDKEYS(all_paths);
Branch: 1 branch
BranchTrueFalseTotal
int num_paths = (int)HvUSEDKEYS(all_paths); 0 0 0
4814 0 json_path_entry_t *pe = NULL;
4815 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
4816 0 Newx(pe, num_paths, json_path_entry_t);
4817 0 SAVEFREEPV(pe);
4818 0 p = 0;
4819 0 hv_iterinit(all_paths);
4820 HE *he;
4821 0 ▶ while ((he = hv_iternext(all_paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(all_paths))) { 0 0 0
4822 I32 klen;
4823 0 char *kstr = hv_iterkey(he, &klen);
4824 0 pe[p].path = kstr;
4825 0 pe[p].len = (STRLEN)klen;
4826 0 p++;
4827 }
4828 0 ▶ if (num_paths > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 1) 0 0 0
4829 0 qsort(pe, num_paths, sizeof(*pe), json_cmp_path_entry);
4830 }
4831  
4832 0 unsigned *kind_masks = NULL;
4833 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
4834 0 Newxz(kind_masks, num_paths, unsigned);
4835 0 SAVEFREEPV(kind_masks);
4836 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
4837 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4838 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4839 0 SV **e = hv_fetch(row_hvs[i], pe[p].path, (I32)pe[p].len, 0);
4840 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
4841 0 int k = json_classify_value(aTHX_ *e);
4842 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
4843 0 kind_masks[p] |= 1u << k;
4844 }
4845 }
4846 }
4847  
4848 /* Object structure prefix */
4849 0 nbuf_le64(b, 0); /* Object V1 */
4850 0 nbuf_varuint(b, (uint64_t)num_paths);
4851 0 nbuf_varuint(b, (uint64_t)num_paths);
4852 0 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 0 0 0
4853 0 nbuf_string(b, pe[p].path, pe[p].len);
4854 0 ▶ for (p = 0; p < n_typed; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < n_typed; p++) 0 0 0
4855 0 write_col_prefix(b, ct->inners[p]);
4856  
4857 0 int *path_slots = NULL;
4858 0 int *wire_slot_counts = NULL;
4859 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
4860 0 Newx(path_slots, num_paths * JSON_LEX_SLOTS, int);
4861 0 SAVEFREEPV(path_slots);
4862 0 Newx(wire_slot_counts, num_paths, int);
4863 0 SAVEFREEPV(wire_slot_counts);
4864 0 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 0 0 0
4865 0 wire_slot_counts[p] = json_build_lex_table(
4866 0 kind_masks[p], path_slots + p*JSON_LEX_SLOTS);
4867 }
4868  
4869 /* Per-path Dynamic V1 prefix + Variant mode */
4870 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
4871 0 int wire_slots = wire_slot_counts[p];
4872 0 int kc = wire_slots - 1;
4873 0 nbuf_le64(b, 1); /* Dynamic V1 */
4874 0 nbuf_varuint(b, (uint64_t)kc);
4875 0 nbuf_varuint(b, (uint64_t)kc);
4876 0 int *slots = path_slots + p*JSON_LEX_SLOTS;
4877 int s;
4878 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
4879 0 int k = slots[s];
4880 0 ▶ if (k < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) continue; 0 0 0
4881 0 const char *nm = json_kind_type_name[k];
4882 0 nbuf_string(b, nm, strlen(nm));
4883 }
4884 0 nbuf_le64(b, 0); /* Variant BASIC */
4885 }
4886  
4887 /* Typed-path column data — comes BEFORE dynamic-path Variant data
4888 * on the wire. Emitted in declaration (sorted) order via the
4889 * inner type's encoder. */
4890 0 ▶ if (n_typed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_typed > 0) { 0 0 0
4891 int tp;
4892 0 ▶ for (tp = 0; tp < n_typed; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) { 0 0 0
4893 0 int rc = encode_column_sv(aTHX_ b, typed_vals[tp],
4894 0 nrows, ct->inners[tp]);
4895 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
4896 }
4897 }
4898  
4899 /* Per-path Variant data */
4900 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
4901 0 int wire_slots = wire_slot_counts[p];
4902 0 int *slots = path_slots + p*JSON_LEX_SLOTS;
4903  
4904 /* Discriminator byte per row */
4905 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4906 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
4907 0 SV **e = hv_fetch(row_hvs[i], pe[p].path, (I32)pe[p].len, 0);
4908 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
4909 0 int k = json_classify_value(aTHX_ *e);
4910 0 nbuf_u8(b, (uint8_t)json_kind_disc_in(k, slots, wire_slots));
4911 }
4912  
4913 /* Per-variant data in lex order. SharedVariant has zero rows. */
4914 int s;
4915 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
4916 0 int k_match = slots[s];
4917 0 ▶ if (k_match < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k_match < 0) continue; 0 0 0
4918 0 int is_array = (k_match >= JV_ARRAY_BOOL
4919 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
4920 0 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 0 0 0
4921 0 uint64_t offset = 0;
4922 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4923 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4924 0 SV **e = hv_fetch(row_hvs[i], pe[p].path,
4925 (I32)pe[p].len, 0);
4926 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
4927 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
4928 0 AV *av = (AV*)SvRV(*e);
4929 0 SSize_t n = av_len(av) + 1;
4930 0 offset += (uint64_t)n;
4931 0 nbuf_le64(b, offset);
4932 }
4933 }
4934 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4935 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4936 0 SV **e = hv_fetch(row_hvs[i], pe[p].path,
4937 (I32)pe[p].len, 0);
4938 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
4939 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
4940 0 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 0 0 0
4941 0 AV *av = (AV*)SvRV(*e);
4942 0 SSize_t n = av_len(av) + 1, j;
4943 0 int av_magical = SvRMAGICAL((SV*)av);
4944 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
4945 0 SV **elem = av_fetch(av, j, 0);
4946 0 ▶ SV *ev = (elem && *elem)
Branch: 1 branch
BranchTrueFalseTotal
SV *ev = (elem && *elem) 0 0 0
4947 0 ▶ ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
? *elem : &PL_sv_undef; 0 0 0
4948 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
4949 0 ev = sv_2mortal(newSVsv(ev));
4950 0 ▶ if (!SvOK(ev)) ev = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ev)) ev = &PL_sv_undef; 0 0 0
4951 0 json_emit_array_elem(aTHX_ b, ev, k_match);
4952 }
4953 } else {
4954 0 json_emit_scalar(aTHX_ b, *e, k_match);
4955 }
4956 }
4957 }
4958 }
4959  
4960 /* Shared data trailer: N UInt64 LE zero offsets */
4961 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
4962 0 size_t nbytes = (size_t)nrows * 8;
4963 0 nbuf_grow(b, nbytes);
4964 0 memset(b->data + b->len, 0, nbytes);
4965 0 b->len += nbytes;
4966 }
4967 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4968 0 return 1;
4969 }
4970 0 default:
4971 0 return 0;
4972 }
4973 }
4974  
4975 /*
4976 * Wrap a filled Data block body into a CLIENT_DATA packet with optional LZ4 + empty trailing block.
4977 * Consumes body->data (frees it). Returns malloc'd packet, or NULL on failure.
4978 */
4979 0 static char* wrap_data_block(ev_clickhouse_t *self, native_buf_t *body, size_t *out_len) {
4980 native_buf_t pkt;
4981  
4982 0 nbuf_init(&pkt);
4983 0 nbuf_varuint(&pkt, CLIENT_DATA);
4984 0 nbuf_cstring(&pkt, ""); /* table name — outside compression */
4985  
4986 #ifdef HAVE_LZ4
4987 0 ▶ if (self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress) { 0 0 0
4988 char *compressed;
4989 size_t comp_len;
4990 0 compressed = ch_lz4_compress(body->data, body->len, &comp_len);
4991 0 Safefree(body->data);
4992 0 body->data = NULL;
4993 0 ▶ if (compressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (compressed) { 0 0 0
4994 0 nbuf_append(&pkt, compressed, comp_len);
4995 0 Safefree(compressed);
4996 } else {
4997 0 Safefree(pkt.data);
4998 0 *out_len = 0;
4999 0 return NULL;
5000 }
5001 } else
5002 #endif
5003 {
5004 0 nbuf_append(&pkt, body->data, body->len);
5005 0 Safefree(body->data);
5006 0 body->data = NULL;
5007 }
5008  
5009 0 nbuf_empty_data_block(&pkt, self->compress);
5010  
5011 0 *out_len = pkt.len;
5012 0 return pkt.data;
5013 }
5014  
5015 /* --- Client-side defaults for empty values (empty_as_default/defaults) ---
5016 *
5017 * The server sends a TableColumns packet before the INSERT sample block;
5018 * its description text carries each table column's DEFAULT/MATERIALIZED/
5019 * ALIAS/EPHEMERAL kind and expression. Resolution runs once per insert at
5020 * sample-block time and yields one col_default_t per column; the build
5021 * functions below then only substitute the cell before the existing
5022 * encoders run.
5023 */
5024  
5025 typedef struct {
5026 char *name; size_t name_len;
5027 char kind; /* 0 none, D/M/A/E */
5028 char *expr; size_t expr_len;
5029 } tc_col_t;
5030  
5031 struct tc_cols_s {
5032 tc_col_t *cols;
5033 int ncols;
5034 int bad; /* the description did not parse completely */
5035 };
5036  
5037 /* One column's resolved default for a pending insert. */
5038 typedef struct {
5039 int subst; /* substitute empty cells in this column */
5040 int nullable; /* column top is Nullable (or LC(Nullable)) */
5041 int to_null; /* substitution value is NULL */
5042 SV *subst_sv; /* arrayref-path value, owned */
5043 char *subst_text; /* string-path cell, owned */
5044 size_t subst_text_len;
5045 char *err; /* why an empty value here cannot be filled, owned */
5046 } col_default_t;
5047  
5048 /* ClickHouse string unescape shared by the wire and literal levels:
5049 * \n \t \r \b \f \v \a \0 \e \xHH, anything else \X to X. */
5050 0 static int hex_val(char c) {
5051 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
5052 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
5053 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
5054 0 return -1;
5055 }
5056  
5057 0 static size_t ch_unescape(const char *src, size_t len, char *dst) {
5058 0 size_t i = 0, j = 0;
5059 int hi, lo;
5060 0 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 0 0 0
5061 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
5062 0 char c = src[i + 1];
5063 0 size_t adv = 2;
5064 0 switch (c) {
5065 0 case 'n': c = '\n'; break;
5066 0 case 't': c = '\t'; break;
5067 0 case 'r': c = '\r'; break;
5068 0 case 'b': c = '\b'; break;
5069 0 case 'f': c = '\f'; break;
5070 0 case 'v': c = '\v'; break;
5071 0 case 'a': c = '\a'; break;
5072 0 case '0': c = '\0'; break;
5073 0 case 'e': c = '\x1b'; break;
5074 0 case 'x':
5075 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
5076 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
5077 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
5078 0 c = (char)((hi << 4) | lo);
5079 0 adv = 4;
5080 }
5081 0 break;
5082 0 default: break;
5083 }
5084 0 dst[j++] = c;
5085 0 i += adv;
5086 } else {
5087 0 dst[j++] = src[i++];
5088 }
5089 }
5090 0 return j;
5091 }
5092  
5093 179 static void free_insert_tc(ev_clickhouse_t *self) {
5094 179 ▶ if (self->insert_tc) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_tc) { 0 179 179
5095 int i;
5096 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
5097 0 Safefree(self->insert_tc->cols[i].name);
5098 0 Safefree(self->insert_tc->cols[i].expr);
5099 }
5100 0 Safefree(self->insert_tc->cols);
5101 0 Safefree(self->insert_tc);
5102 0 self->insert_tc = NULL;
5103 }
5104 179 }
5105  
5106 179 static void free_insert_default_state(ev_clickhouse_t *self) {
5107 179 self->insert_empty_as_default = 0;
5108 179 ▶ CLEAR_SV(self->insert_defaults);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->insert_defaults); 0 179 179
5109 179 free_insert_tc(self);
5110 179 }
5111  
5112 24 static void free_col_defaults(col_default_t *cdefs, int ncols) {
5113 int i;
5114 24 ▶ if (!cdefs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cdefs) return; 24 0 24
5115 0 ▶ for (i = 0; i < ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ncols; i++) { 0 0 0
5116 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
5117 0 Safefree(cdefs[i].subst_text);
5118 0 Safefree(cdefs[i].err);
5119 }
5120 0 Safefree(cdefs);
5121 }
5122  
5123 /* Split one description line into tab fields; returns field count. */
5124 0 static int split_tabs(const char *line, size_t len,
5125 const char **f, size_t *fl, int maxf) {
5126 0 int n = 0;
5127 0 size_t start = 0, i;
5128 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
5129 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
5130 0 f[n] = line + start;
5131 0 fl[n] = i - start;
5132 0 n++;
5133 0 start = i + 1;
5134 }
5135 }
5136 0 return n;
5137 }
5138  
5139 /* Parse the TableColumns description text: a header line, the count
5140 * line, then one line per column. Anything short of that marks it bad,
5141 * so no column silently falls back to its type's zero. */
5142 0 static tc_cols_t *parse_table_columns_desc(const char *s, size_t len) {
5143 tc_cols_t *tc;
5144 0 const char *p = s, *end = s + len, *nl, *q;
5145 const char *f[8];
5146 size_t fl[8];
5147 0 int nf, n = 0, i;
5148 0 Newxz(tc, 1, tc_cols_t);
5149 0 tc->bad = 1;
5150 0 nl = memchr(p, '\n', (size_t)(end - p));
5151 0 ▶ if (!nl) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return tc; 0 0 0
5152 0 p = nl + 1;
5153 0 nl = memchr(p, '\n', (size_t)(end - p));
5154 0 ▶ if (!nl) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return tc; 0 0 0
5155 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
5156 0 ▶ if (n > 100000) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 100000) return tc; 0 0 0
5157 0 n = n * 10 + (*q - '0');
5158 }
5159 /* "N columns:" */
5160 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
5161 0 p = nl + 1;
5162 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
5163 0 tc->ncols = n;
5164 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
5165 const char *le;
5166 size_t j, k;
5167 char *un;
5168 0 ▶ if (p >= end) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return tc; 0 0 0
5169 0 nl = memchr(p, '\n', (size_t)(end - p));
5170 0 ▶ le = nl ? nl : end;
Branch: 1 branch
BranchTrueFalseTotal
le = nl ? nl : end; 0 0 0
5171 0 nf = split_tabs(p, (size_t)(le - p), f, fl, 8);
5172 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
5173 0 j = 1;
5174 0 ▶ while (j < fl[0]) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < fl[0]) { 0 0 0
5175 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
5176 0 ▶ if (f[0][j] == '`') break;
Branch: 1 branch
BranchTrueFalseTotal
if (f[0][j] == '`') break; 0 0 0
5177 0 j++;
5178 }
5179 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
5180 0 Newx(un, j, char);
5181 0 k = ch_unescape(f[0] + 1, j - 1, un);
5182 0 tc->cols[i].name = un;
5183 0 tc->cols[i].name_len = k;
5184 0 ▶ if (nf > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nf > 1) { 0 0 0
5185 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
5186 0 tc->cols[i].kind = 'D';
5187 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
5188 0 tc->cols[i].kind = 'M';
5189 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
5190 0 tc->cols[i].kind = 'A';
5191 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
5192 0 tc->cols[i].kind = 'E';
5193 0 ▶ if (tc->cols[i].kind) {
Branch: 1 branch
BranchTrueFalseTotal
if (tc->cols[i].kind) { 0 0 0
5194 0 ▶ if (nf < 3) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (nf < 3) return tc; 0 0 0
5195 0 Newx(un, fl[2] + 1, char);
5196 0 k = ch_unescape(f[2], fl[2], un);
5197 0 tc->cols[i].expr = un;
5198 0 tc->cols[i].expr_len = k;
5199 }
5200 }
5201 0 p = le + 1;
5202 }
5203 0 tc->bad = 0;
5204 0 return tc;
5205 }
5206  
5207 0 static const tc_col_t *find_tc_col(const tc_cols_t *tc,
5208 const char *name, size_t len) {
5209 int i;
5210 0 ▶ if (!tc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!tc) return NULL; 0 0 0
5211 0 ▶ for (i = 0; i < tc->ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc->ncols; i++) { 0 0 0
5212 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
5213 0 ▶ && memcmp(tc->cols[i].name, name, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(tc->cols[i].name, name, len) == 0) 0 0 0
5214 0 return &tc->cols[i];
5215 }
5216 0 return NULL;
5217 }
5218  
5219 /* Constant DEFAULT literals: number, string, NULL, true/false,
5220 * array/tuple of literals. Anything else is not a literal. */
5221 enum { L_NUM, L_STR, L_NULL, L_BOOL, L_ARR, L_TUP };
5222  
5223 typedef struct lit_s lit_t;
5224 struct lit_s {
5225 int kind;
5226 const char *ptr; size_t len;
5227 char *str; size_t str_len;
5228 int boolean;
5229 lit_t **items; size_t nitems; size_t nicap;
5230 col_type_t *cast; /* CAST / :: target type, NULL when none */
5231 };
5232  
5233 0 static void free_lit(lit_t *l) {
5234 0 ▶ if (!l) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return; 0 0 0
5235 0 ▶ if (l->cast) free_col_type(l->cast);
Branch: 1 branch
BranchTrueFalseTotal
if (l->cast) free_col_type(l->cast); 0 0 0
5236 0 ▶ if (l->kind == L_STR) Safefree(l->str);
Branch: 1 branch
BranchTrueFalseTotal
if (l->kind == L_STR) Safefree(l->str); 0 0 0
5237 0 ▶ if (l->items) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->items) { 0 0 0
5238 size_t i;
5239 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
5240 0 Safefree(l->items);
5241 }
5242 0 Safefree(l);
5243 }
5244  
5245 0 static const char *skip_ws(const char *p, const char *end) {
5246 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
5247 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
5248 0 p++;
5249 0 return p;
5250 }
5251  
5252 0 static int is_wordchar(char c) {
5253 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
5254 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
5255 }
5256  
5257 static lit_t *parse_lit(const char **pp, const char *end, int depth);
5258  
5259 0 static int lit_push(lit_t *l, lit_t *item) {
5260 0 ▶ if (l->nitems == l->nicap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->nitems == l->nicap) { 0 0 0
5261 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
5262 0 ▶ if (ncap > 100000) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ncap > 100000) return 0; 0 0 0
5263 0 ▶ Renew(l->items, ncap, lit_t *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(l->items, ncap, lit_t *); 0 0 0
5264 0 l->nicap = ncap;
5265 }
5266 0 l->items[l->nitems++] = item;
5267 0 return 1;
5268 }
5269  
5270 /* Past a type name ("Decimal(9, 2)", "DateTime('UTC')"), else NULL. */
5271 0 static const char *skip_type_name(const char *p, const char *end) {
5272 0 int depth = 0;
5273 0 ▶ if (p >= end || !((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
if (p >= end || !((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') 0 0 0
if (p >= end || !((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') 0 0 0
if (p >= end || !((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') 0 0 0
if (p >= end || !((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') 0 0 0
if (p >= end || !((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') 0 0 0
5274 0 ▶ || *p == '_'))
Branch: 1 branch
BranchTrueFalseTotal
|| *p == '_')) 0 0 0
5275 0 return NULL;
5276 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
5277 0 p = skip_ws(p, end);
5278 0 ▶ if (p >= end || *p != '(') return p;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '(') return p; 0 0 0
if (p >= end || *p != '(') return p; 0 0 0
5279 0 ▶ for (; p < end; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; p < end; p++) { 0 0 0
5280 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5281 0 ▶ for (p++; p < end && *p != '\''; p++)
Branch: 2 branches
BranchTrueFalseTotal
for (p++; p < end && *p != '\''; p++) 0 0 0
for (p++; p < end && *p != '\''; p++) 0 0 0
5282 0 ▶ if (*p == '\\') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') p++; 0 0 0
5283 0 ▶ if (p >= end) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return NULL; 0 0 0
5284 0 ▶ } else if (*p == '(') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*p == '(') { 0 0 0
5285 0 depth++;
5286 0 ▶ } else if (*p == ')' && --depth == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (*p == ')' && --depth == 0) { 0 0 0
} else if (*p == ')' && --depth == 0) { 0 0 0
5287 0 return p + 1;
5288 }
5289 }
5290 0 return NULL;
5291 }
5292  
5293 static lit_t *parse_lit_atom(const char **pp, const char *end, int depth);
5294  
5295 /* Record a cast on l; 0 when l already carries a different cast type
5296 * (a cast of a cast is not a client-side literal). */
5297 0 static int lit_set_cast(lit_t *l, const char *type, size_t len) {
5298 0 col_type_t *t = parse_col_type(type, len);
5299 0 ▶ if (l->cast) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->cast) { 0 0 0
5300 0 int same = col_type_same(l->cast, t);
5301 0 free_col_type(t);
5302 0 return same;
5303 }
5304 0 l->cast = t;
5305 0 return 1;
5306 }
5307  
5308 /* A literal, also in the constant forms ALTER writes: CAST(lit, 'Type'),
5309 * CAST(lit AS Type), lit::Type. The cast type is kept: the literal only
5310 * stands for the default when it matches the column (lit_casts_ok). */
5311 0 static lit_t *parse_lit(const char **pp, const char *end, int depth) {
5312 0 const char *p = skip_ws(*pp, end);
5313 lit_t *l;
5314 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
5315 {
5316 0 ▶ size_t kw = end - p > 5 && strncmp(p, "_CAST", 5) == 0 ? 5
Branch: 1 branch
BranchTrueFalseTotal
size_t kw = end - p > 5 && strncmp(p, "_CAST", 5) == 0 ? 5 0 0 0
5317 0 ▶ : end - p > 4 && strncasecmp(p, "CAST", 4) == 0 ? 4 : 0;
Branch: 3 branches
BranchTrueFalseTotal
: end - p > 4 && strncasecmp(p, "CAST", 4) == 0 ? 4 : 0; 0 0 0
: end - p > 4 && strncasecmp(p, "CAST", 4) == 0 ? 4 : 0; 0 0 0
: end - p > 4 && strncasecmp(p, "CAST", 4) == 0 ? 4 : 0; 0 0 0
5318 0 ▶ const char *q = kw && !is_wordchar(p[kw]) ? skip_ws(p + kw, end) : end;
Branch: 2 branches
BranchTrueFalseTotal
const char *q = kw && !is_wordchar(p[kw]) ? skip_ws(p + kw, end) : end; 0 0 0
const char *q = kw && !is_wordchar(p[kw]) ? skip_ws(p + kw, end) : end; 0 0 0
5319 0 ▶ if (q < end && *q == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == '(') { 0 0 0
if (q < end && *q == '(') { 0 0 0
5320 0 q++;
5321 0 l = parse_lit(&q, end, depth + 1);
5322 0 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 0 0
5323 0 q = skip_ws(q, end);
5324 0 ▶ if (q < end && *q == ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == ',') { 0 0 0
if (q < end && *q == ',') { 0 0 0
5325 lit_t *t;
5326 int ok;
5327 0 q++;
5328 0 t = parse_lit(&q, end, depth + 1);
5329 0 ▶ if (!t || t->kind != L_STR || t->cast) { free_lit(t); free_lit(l); return NULL; }
Branch: 3 branches
BranchTrueFalseTotal
if (!t || t->kind != L_STR || t->cast) { free_lit(t); free_lit(l); return NULL; } 0 0 0
if (!t || t->kind != L_STR || t->cast) { free_lit(t); free_lit(l); return NULL; } 0 0 0
if (!t || t->kind != L_STR || t->cast) { free_lit(t); free_lit(l); return NULL; } 0 0 0
5330 0 ok = lit_set_cast(l, t->str, t->str_len);
5331 0 free_lit(t);
5332 0 ▶ if (!ok) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { free_lit(l); return NULL; } 0 0 0
5333 0 ▶ } else if (end - q > 2 && strncasecmp(q, "AS", 2) == 0
Branch: 2 branches
BranchTrueFalseTotal
} else if (end - q > 2 && strncasecmp(q, "AS", 2) == 0 0 0 0
} else if (end - q > 2 && strncasecmp(q, "AS", 2) == 0 0 0 0
5334 0 ▶ && !is_wordchar(q[2])) {
Branch: 1 branch
BranchTrueFalseTotal
&& !is_wordchar(q[2])) { 0 0 0
5335 0 const char *ts = skip_ws(q + 2, end);
5336 0 q = skip_type_name(ts, end);
5337 0 ▶ if (!q || !lit_set_cast(l, ts, (size_t)(q - ts))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!q || !lit_set_cast(l, ts, (size_t)(q - ts))) { 0 0 0
if (!q || !lit_set_cast(l, ts, (size_t)(q - ts))) { 0 0 0
5338 0 free_lit(l);
5339 0 return NULL;
5340 }
5341 } else {
5342 0 free_lit(l);
5343 0 return NULL;
5344 }
5345 0 q = skip_ws(q, end);
5346 0 ▶ if (q >= end || *q != ')') { free_lit(l); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (q >= end || *q != ')') { free_lit(l); return NULL; } 0 0 0
if (q >= end || *q != ')') { free_lit(l); return NULL; } 0 0 0
5347 0 p = q + 1;
5348 0 goto suffix;
5349 }
5350 }
5351 0 l = parse_lit_atom(&p, end, depth);
5352 0 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 0 0
5353 0 suffix:
5354 0 for (;;) {
5355 0 const char *ts, *q = skip_ws(p, end);
5356 0 ▶ if (end - q < 2 || q[0] != ':' || q[1] != ':') break;
Branch: 3 branches
BranchTrueFalseTotal
if (end - q < 2 || q[0] != ':' || q[1] != ':') break; 0 0 0
if (end - q < 2 || q[0] != ':' || q[1] != ':') break; 0 0 0
if (end - q < 2 || q[0] != ':' || q[1] != ':') break; 0 0 0
5357 0 ts = skip_ws(q + 2, end);
5358 0 q = skip_type_name(ts, end);
5359 0 ▶ if (!q || !lit_set_cast(l, ts, (size_t)(q - ts))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!q || !lit_set_cast(l, ts, (size_t)(q - ts))) { 0 0 0
if (!q || !lit_set_cast(l, ts, (size_t)(q - ts))) { 0 0 0
5360 0 free_lit(l);
5361 0 return NULL;
5362 }
5363 0 p = q;
5364 }
5365 0 *pp = p;
5366 0 return l;
5367 }
5368  
5369 0 static lit_t *parse_lit_atom(const char **pp, const char *end, int depth) {
5370 0 const char *p = skip_ws(*pp, end);
5371 lit_t *l;
5372 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
5373 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5374 0 size_t cap = (size_t)(end - p), k = 0;
5375 char *buf;
5376 0 ▶ Newx(buf, cap ? cap : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(buf, cap ? cap : 1, char); 0 0 0
5377 0 p++;
5378 for (;;) {
5379 0 ▶ if (p >= end) { Safefree(buf); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) { Safefree(buf); return NULL; } 0 0 0
5380 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5381 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
5382 0 buf[k++] = '\'';
5383 0 p += 2;
5384 0 continue;
5385 }
5386 0 p++;
5387 0 break;
5388 }
5389 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
5390 0 char c = p[1];
5391 0 size_t adv = 2;
5392 int hi, lo;
5393 0 switch (c) {
5394 0 case 'n': c = '\n'; break;
5395 0 case 't': c = '\t'; break;
5396 0 case 'r': c = '\r'; break;
5397 0 case 'b': c = '\b'; break;
5398 0 case 'f': c = '\f'; break;
5399 0 case 'v': c = '\v'; break;
5400 0 case 'a': c = '\a'; break;
5401 0 case '0': c = '\0'; break;
5402 0 case 'e': c = '\x1b'; break;
5403 0 case 'x':
5404 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
5405 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
5406 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
5407 0 c = (char)((hi << 4) | lo);
5408 0 adv = 4;
5409 }
5410 0 break;
5411 0 default: break;
5412 }
5413 0 buf[k++] = c;
5414 0 p += adv;
5415 0 continue;
5416 }
5417 0 buf[k++] = *p++;
5418 }
5419 0 Newxz(l, 1, lit_t);
5420 0 l->kind = L_STR;
5421 0 l->str = buf;
5422 0 l->str_len = k;
5423 0 *pp = p;
5424 0 return l;
5425 }
5426 0 ▶ if (*p == '[' || *p == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '[' || *p == '(') { 0 0 0
if (*p == '[' || *p == '(') { 0 0 0
5427 0 ▶ char open = *p, close = (open == '[') ? ']' : ')';
Branch: 1 branch
BranchTrueFalseTotal
char open = *p, close = (open == '[') ? ']' : ')'; 0 0 0
5428 0 int is_tup = (open == '(');
5429 0 lit_t *first = NULL;
5430 0 p++;
5431 0 Newxz(l, 1, lit_t);
5432 0 ▶ l->kind = is_tup ? L_TUP : L_ARR;
Branch: 1 branch
BranchTrueFalseTotal
l->kind = is_tup ? L_TUP : L_ARR; 0 0 0
5433 0 p = skip_ws(p, end);
5434 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
5435 0 p++;
5436 0 *pp = p;
5437 0 return l;
5438 }
5439 0 first = parse_lit(&p, end, depth + 1);
5440 0 ▶ if (!first) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!first) { free_lit(l); return NULL; } 0 0 0
5441 0 p = skip_ws(p, end);
5442 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
5443 0 p++;
5444 0 free_lit(l);
5445 0 *pp = p;
5446 0 return first;
5447 }
5448 0 ▶ if (p >= end || *p != ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != ',') { 0 0 0
if (p >= end || *p != ',') { 0 0 0
5449 0 free_lit(first);
5450 0 free_lit(l);
5451 0 return NULL;
5452 }
5453 0 ▶ if (!lit_push(l, first)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_push(l, first)) { 0 0 0
5454 0 free_lit(first);
5455 0 free_lit(l);
5456 0 return NULL;
5457 }
5458 0 for (;;) {
5459 lit_t *item;
5460 0 p++;
5461 0 item = parse_lit(&p, end, depth + 1);
5462 0 ▶ if (!item) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!item) { free_lit(l); return NULL; } 0 0 0
5463 0 ▶ if (!lit_push(l, item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_push(l, item)) { 0 0 0
5464 0 free_lit(item);
5465 0 free_lit(l);
5466 0 return NULL;
5467 }
5468 0 p = skip_ws(p, end);
5469 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
5470 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
5471 0 free_lit(l);
5472 0 return NULL;
5473 }
5474 0 *pp = p;
5475 0 return l;
5476 }
5477 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
5478 0 const char *start = p;
5479 0 int seendigit = 0;
5480 0 ▶ if (*p == '+' || *p == '-') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '+' || *p == '-') p++; 0 0 0
if (*p == '+' || *p == '-') p++; 0 0 0
5481 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
5482 0 p += 2;
5483 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
5484 0 ▶ if (!seendigit) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!seendigit) return NULL; 0 0 0
5485 } else {
5486 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
5487 0 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 0 0 0
if (p < end && *p == '.') { 0 0 0
5488 0 p++;
5489 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
5490 0 p++;
5491 0 seendigit = 1;
5492 }
5493 }
5494 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
5495 0 const char *e = p + 1;
5496 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
5497 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
5498 0 p = e + 1;
5499 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
5500 }
5501 }
5502 0 ▶ if (!seendigit) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!seendigit) return NULL; 0 0 0
5503 }
5504 0 Newxz(l, 1, lit_t);
5505 0 l->kind = L_NUM;
5506 0 l->ptr = start;
5507 0 l->len = (size_t)(p - start);
5508 0 *pp = p;
5509 0 return l;
5510 }
5511 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
5512 0 const char *w = p;
5513 size_t wl;
5514 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
5515 0 wl = (size_t)(p - w);
5516 0 Newxz(l, 1, lit_t);
5517 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
5518 0 l->kind = L_NULL;
5519 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
5520 0 l->kind = L_BOOL;
5521 0 l->boolean = 1;
5522 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
5523 0 l->kind = L_BOOL;
5524 0 l->boolean = 0;
5525 } else {
5526 0 Safefree(l);
5527 0 return NULL;
5528 }
5529 0 *pp = p;
5530 0 return l;
5531 }
5532 0 return NULL;
5533 }
5534  
5535 0 static const col_type_t *strip_lc(const col_type_t *t) {
5536 0 ▶ while (t && t->code == CT_LOWCARDINALITY) t = t->inner;
Branch: 2 branches
BranchTrueFalseTotal
while (t && t->code == CT_LOWCARDINALITY) t = t->inner; 0 0 0
while (t && t->code == CT_LOWCARDINALITY) t = t->inner; 0 0 0
5537 0 return t;
5538 }
5539  
5540 /* A CAST to the column's own type (LowCardinality aside, Nullable on the
5541 * column side allowed) leaves the value to the column's encoder; any other
5542 * target type converts on the server in ways the client does not mimic. */
5543 0 static int cast_matches(const col_type_t *cast, const col_type_t *col) {
5544 0 cast = strip_lc(cast);
5545 0 col = strip_lc(col);
5546 0 ▶ if (col_type_same(cast, col)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (col_type_same(cast, col)) return 1; 0 0 0
5547 0 ▶ return col && col->code == CT_NULLABLE
Branch: 1 branch
BranchTrueFalseTotal
return col && col->code == CT_NULLABLE 0 0 0
5548 0 ▶ && col_type_same(cast, strip_lc(col->inner));
Branch: 2 branches
BranchTrueFalseTotal
&& col_type_same(cast, strip_lc(col->inner)); 0 0 0
&& col_type_same(cast, strip_lc(col->inner)); 0 0 0
5549 }
5550  
5551 /* 1 when every cast in the literal targets the type at its position in
5552 * the column type. */
5553 0 static int lit_casts_ok(const lit_t *l, const col_type_t *ct) {
5554 0 const col_type_t *t = strip_lc(ct);
5555 size_t i;
5556 0 ▶ if (l->cast && !cast_matches(l->cast, ct)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (l->cast && !cast_matches(l->cast, ct)) return 0; 0 0 0
if (l->cast && !cast_matches(l->cast, ct)) return 0; 0 0 0
5557 0 ▶ if (t && t->code == CT_NULLABLE) t = strip_lc(t->inner);
Branch: 2 branches
BranchTrueFalseTotal
if (t && t->code == CT_NULLABLE) t = strip_lc(t->inner); 0 0 0
if (t && t->code == CT_NULLABLE) t = strip_lc(t->inner); 0 0 0
5558 0 ▶ for (i = 0; i < l->nitems; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->nitems; i++) { 0 0 0
5559 0 const col_type_t *et = NULL;
5560 0 ▶ if (l->kind == L_ARR && t && t->code == CT_ARRAY)
Branch: 3 branches
BranchTrueFalseTotal
if (l->kind == L_ARR && t && t->code == CT_ARRAY) 0 0 0
if (l->kind == L_ARR && t && t->code == CT_ARRAY) 0 0 0
if (l->kind == L_ARR && t && t->code == CT_ARRAY) 0 0 0
5561 0 et = t->inner;
5562 0 ▶ else if (l->kind == L_TUP && t && t->code == CT_TUPLE
Branch: 3 branches
BranchTrueFalseTotal
else if (l->kind == L_TUP && t && t->code == CT_TUPLE 0 0 0
else if (l->kind == L_TUP && t && t->code == CT_TUPLE 0 0 0
else if (l->kind == L_TUP && t && t->code == CT_TUPLE 0 0 0
5563 0 ▶ && i < (size_t)t->num_inners)
Branch: 1 branch
BranchTrueFalseTotal
&& i < (size_t)t->num_inners) 0 0 0
5564 0 et = t->inners[i];
5565 0 ▶ if (!lit_casts_ok(l->items[i], et)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_casts_ok(l->items[i], et)) return 0; 0 0 0
5566 }
5567 0 return 1;
5568 }
5569  
5570 /* A whole expression is a literal only if one value spans it. */
5571 0 static lit_t *parse_default_literal(const char *s, size_t len) {
5572 0 const char *p = s, *end = s + len;
5573 0 lit_t *l = parse_lit(&p, end, 0);
5574 0 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 0 0
5575 0 p = skip_ws(p, end);
5576 0 ▶ if (p != end) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) { free_lit(l); return NULL; } 0 0 0
5577 0 return l;
5578 }
5579  
5580 /* Verbatim number text, hex normalized to decimal; NULL when the hex
5581 * value does not fit 64 bits. Caller frees. */
5582 0 static char *num_normalize(const char *p, size_t len, size_t *out_len) {
5583 0 const char *q = p;
5584 0 size_t qlen = len;
5585 char *out;
5586 0 int neg = 0;
5587 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
5588 0 neg = (*q == '-');
5589 0 q++;
5590 0 qlen--;
5591 }
5592 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
5593 0 unsigned long long v = 0;
5594 size_t i;
5595 0 ▶ for (i = 2; i < qlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < qlen; i++) { 0 0 0
5596 0 int d = hex_val(q[i]);
5597 0 ▶ if (d < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (d < 0) break; 0 0 0
5598 0 ▶ if (v > (ULLONG_MAX >> 4)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (ULLONG_MAX >> 4)) return NULL; 0 0 0
5599 0 v = v * 16 + (unsigned)d;
5600 }
5601 0 Newx(out, 32, char);
5602 0 ▶ if (neg)
Branch: 1 branch
BranchTrueFalseTotal
if (neg) 0 0 0
5603 0 *out_len = (size_t)snprintf(out, 32, "-%llu", v);
5604 else
5605 0 *out_len = (size_t)snprintf(out, 32, "%llu", v);
5606 0 return out;
5607 }
5608 0 Newx(out, len + 1, char);
5609 0 Copy(p, out, len, char);
5610 0 out[len] = '\0';
5611 0 *out_len = len;
5612 0 return out;
5613 }
5614  
5615 0 static int nullable_top(col_type_t *ct) {
5616 0 ▶ if (!ct) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) return 0; 0 0 0
5617 0 ▶ if (ct->code == CT_NULLABLE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_NULLABLE) return 1; 0 0 0
5618 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
5619 0 ▶ && ct->inner->code == CT_NULLABLE)
Branch: 1 branch
BranchTrueFalseTotal
&& ct->inner->code == CT_NULLABLE) 0 0 0
5620 0 return 1;
5621 0 return 0;
5622 }
5623  
5624 /* Arrayref-path conversion of a literal. Returns an owned SV, or NULL
5625 * for exotic nests the client cannot express. */
5626 0 static SV *lit_to_sv(pTHX_ lit_t *l) {
5627 size_t i;
5628 AV *av;
5629 SV *sv;
5630 char *norm;
5631 size_t nlen;
5632 0 switch (l->kind) {
5633 0 case L_NUM:
5634 0 norm = num_normalize(l->ptr, l->len, &nlen);
5635 0 ▶ if (!norm) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!norm) return NULL; 0 0 0
5636 0 sv = newSVpvn(norm, nlen);
5637 0 Safefree(norm);
5638 0 return sv;
5639 0 case L_STR:
5640 0 return newSVpvn(l->str, l->str_len);
5641 0 case L_NULL:
5642 0 return newSV(0);
5643 0 case L_BOOL:
5644 0 return newSViv(l->boolean);
5645 0 case L_ARR:
5646 case L_TUP:
5647 0 av = newAV();
5648 0 ▶ for (i = 0; i < l->nitems; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->nitems; i++) { 0 0 0
5649 0 SV *e = lit_to_sv(aTHX_ l->items[i]);
5650 0 ▶ if (!e) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { 0 0 0
5651 0 SvREFCNT_dec((SV *)av);
5652 0 return NULL;
5653 }
5654 0 av_store(av, (SSize_t)i, e);
5655 }
5656 0 return newRV_noinc((SV *)av);
5657 }
5658 0 return NULL;
5659 }
5660  
5661 /* String-path conversion: appends the TSV cell. 0 when the value has
5662 * no text form (nested types need binary encoding). */
5663 0 static int lit_to_text(lit_t *l, native_buf_t *b) {
5664 char *norm;
5665 size_t nlen;
5666 0 switch (l->kind) {
5667 0 case L_NUM:
5668 0 norm = num_normalize(l->ptr, l->len, &nlen);
5669 0 ▶ if (!norm) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!norm) return 0; 0 0 0
5670 0 nbuf_append(b, norm, nlen);
5671 0 Safefree(norm);
5672 0 return 1;
5673 0 case L_STR:
5674 0 tsv_escape(b, l->str, l->str_len);
5675 0 return 1;
5676 0 case L_NULL:
5677 0 nbuf_append(b, "\\N", 2);
5678 0 return 1;
5679 0 case L_BOOL:
5680 0 ▶ nbuf_append(b, l->boolean ? "1" : "0", 1);
Branch: 1 branch
BranchTrueFalseTotal
nbuf_append(b, l->boolean ? "1" : "0", 1); 0 0 0
5681 0 return 1;
5682 0 default:
5683 0 return 0;
5684 }
5685 }
5686  
5687 /* Zero value for a column with no DEFAULT. Returns an owned SV, sets
5688 * *impossible when the type has no client-side zero. to_null handled
5689 * by the caller. */
5690 0 static SV *zero_sv_for(pTHX_ col_type_t *ct, int *impossible) {
5691 size_t i;
5692 AV *av;
5693 SV *sv;
5694 0 *impossible = 0;
5695 0 switch (ct->code) {
5696 0 case CT_ARRAY:
5697 0 return newRV_noinc((SV *)newAV());
5698 0 case CT_MAP:
5699 0 return newRV_noinc((SV *)newHV());
5700 0 case CT_TUPLE:
5701 0 av = newAV();
5702 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
5703 0 col_type_t *in = ct->inners[i];
5704 0 ▶ if (!in) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in) { 0 0 0
5705 0 SvREFCNT_dec((SV *)av);
5706 0 *impossible = 1;
5707 0 return NULL;
5708 }
5709 0 ▶ if (in->code == CT_NULLABLE
Branch: 1 branch
BranchTrueFalseTotal
if (in->code == CT_NULLABLE 0 0 0
5710 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
5711 0 ▶ && in->inner->code == CT_NULLABLE)) {
Branch: 1 branch
BranchTrueFalseTotal
&& in->inner->code == CT_NULLABLE)) { 0 0 0
5712 0 av_store(av, (SSize_t)i, newSV(0));
5713 0 continue;
5714 }
5715 0 sv = zero_sv_for(aTHX_ in, impossible);
5716 0 ▶ if (!sv || *impossible) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || *impossible) { 0 0 0
if (!sv || *impossible) { 0 0 0
5717 0 ▶ if (sv) SvREFCNT_dec(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (sv) SvREFCNT_dec(sv); 0 0 0
5718 0 SvREFCNT_dec((SV *)av);
5719 0 *impossible = 1;
5720 0 return NULL;
5721 }
5722 0 av_store(av, (SSize_t)i, sv);
5723 }
5724 0 return newRV_noinc((SV *)av);
5725 0 case CT_LOWCARDINALITY:
5726 0 ▶ if (!ct->inner) { *impossible = 1; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ct->inner) { *impossible = 1; return NULL; } 0 0 0
5727 0 return zero_sv_for(aTHX_ ct->inner, impossible);
5728 0 case CT_JSON:
5729 0 return newRV_noinc((SV *)newHV());
5730 0 default:
5731 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
5732 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
5733 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
5734 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
5735 0 *impossible = 1;
5736 0 return NULL;
5737 }
5738 0 return nullable_inner_zero_sv(aTHX_ ct);
5739 }
5740 }
5741  
5742 /* Zero value as a string-path cell. 1 with *out malloc'd, 0 when the
5743 * type has no text form. to_null handled by the caller. */
5744 0 static int zero_text_for(col_type_t *ct, char **out, size_t *out_len) {
5745 const char *p;
5746 size_t len;
5747 0 *out = NULL;
5748 0 *out_len = 0;
5749 0 switch (ct->code) {
5750 0 case CT_ARRAY:
5751 case CT_MAP:
5752 case CT_TUPLE:
5753 0 return 0;
5754 0 case CT_LOWCARDINALITY:
5755 0 ▶ if (!ct->inner) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct->inner) return 0; 0 0 0
5756 0 return zero_text_for(ct->inner, out, out_len);
5757 0 case CT_JSON:
5758 case CT_UNKNOWN:
5759 case CT_NOTHING:
5760 case CT_VARIANT:
5761 case CT_DYNAMIC:
5762 case CT_INT128:
5763 case CT_UINT128:
5764 case CT_INT256:
5765 case CT_UINT256:
5766 0 return 0;
5767 0 default:
5768 0 nullable_inner_zero(ct, &p, &len);
5769 0 Newx(*out, len + 1, char);
5770 0 Copy(p, *out, len, char);
5771 0 (*out)[len] = '\0';
5772 0 *out_len = len;
5773 0 return 1;
5774 }
5775 }
5776  
5777 /* Resolve one column's default from a caller value. */
5778 0 static void resolve_from_sv(col_default_t *cd, SV *v) {
5779 0 ▶ if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { 0 0 0
5780 0 cd->to_null = 1;
5781 0 return;
5782 }
5783 0 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 0 0 0
5784 0 cd->subst_sv = SvREFCNT_inc(v);
5785 0 return;
5786 }
5787 0 cd->subst_sv = SvREFCNT_inc(v);
5788 {
5789 STRLEN l;
5790 0 const char *s = SvPV(cd->subst_sv, l);
5791 native_buf_t b;
5792 0 nbuf_init(&b);
5793 0 tsv_escape(&b, s, l);
5794 0 cd->subst_text = b.data;
5795 0 cd->subst_text_len = b.len;
5796 }
5797 }
5798  
5799 /* Resolve one column from a DEFAULT literal. 1 ok, 0 failure. */
5800 0 static int resolve_from_lit(pTHX_ col_default_t *cd, lit_t *l) {
5801 native_buf_t b;
5802 0 ▶ if (l->kind == L_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->kind == L_NULL) { 0 0 0
5803 0 cd->to_null = 1;
5804 0 return 1;
5805 }
5806 0 cd->subst_sv = lit_to_sv(aTHX_ l);
5807 0 ▶ if (!cd->subst_sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!cd->subst_sv) return 0; 0 0 0
5808 0 nbuf_init(&b);
5809 0 ▶ if (lit_to_text(l, &b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lit_to_text(l, &b)) { 0 0 0
5810 0 cd->subst_text = b.data;
5811 0 cd->subst_text_len = b.len;
5812 } else {
5813 0 Safefree(b.data);
5814 }
5815 0 return 1;
5816 }
5817  
5818 /* Message helpers: short errors naming the column and the fix. */
5819 0 static void resolve_err_trunc(const char *src, size_t len,
5820 char *dst, size_t dstsz) {
5821 0 size_t n = len < dstsz - 1 ? len : dstsz - 1;
5822 size_t i;
5823 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
5824 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
5825 0 dst[n] = '\0';
5826 0 }
5827  
5828 /* Resolve every column's default once per insert. NULL on failure
5829 * with the reason in errbuf. */
5830 0 static col_default_t *resolve_insert_defaults(pTHX_ ev_clickhouse_t *self,
5831 const char **cnames, size_t *cname_lens, col_type_t **ctypes,
5832 const char **ctypes_str, size_t *ctype_lens, int num_cols,
5833 char *errbuf, size_t errbuf_sz) {
5834 col_default_t *cdefs;
5835 int i;
5836 0 Newxz(cdefs, num_cols, col_default_t);
5837 0 ▶ for (i = 0; i < num_cols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_cols; i++) { 0 0 0
5838 0 col_default_t *cd = &cdefs[i];
5839 0 SV **dp = NULL;
5840 const tc_col_t *tc;
5841 lit_t *lit;
5842 char expr_tr[72];
5843 0 ▶ if (self->insert_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_defaults) { 0 0 0
5844 0 dp = hv_fetch(self->insert_defaults, cnames[i],
5845 (I32)cname_lens[i], 0);
5846 }
5847 0 ▶ if (dp && *dp) {
Branch: 2 branches
BranchTrueFalseTotal
if (dp && *dp) { 0 0 0
if (dp && *dp) { 0 0 0
5848 0 cd->subst = 1;
5849 0 cd->nullable = nullable_top(ctypes[i]);
5850 0 resolve_from_sv(cd, *dp);
5851 0 continue;
5852 }
5853 0 ▶ if (!self->insert_empty_as_default) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->insert_empty_as_default) continue; 0 0 0
5854 0 cd->subst = 1;
5855 0 cd->nullable = nullable_top(ctypes[i]);
5856 0 ▶ if (!self->insert_tc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->insert_tc) { 0 0 0
5857 0 snprintf(errbuf, errbuf_sz, "server did not send column "
5858 "defaults for this insert: use data_format => "
5859 "'Native' for server-side defaults");
5860 0 goto col_fail;
5861 }
5862 0 ▶ if (self->insert_tc->bad) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_tc->bad) { 0 0 0
5863 0 snprintf(errbuf, errbuf_sz, "cannot parse the server's column "
5864 "description: use data_format => 'Native' for "
5865 "server-side defaults");
5866 0 goto col_fail;
5867 }
5868 0 tc = find_tc_col(self->insert_tc, cnames[i], cname_lens[i]);
5869 0 ▶ if (!tc || !tc->kind) {
Branch: 2 branches
BranchTrueFalseTotal
if (!tc || !tc->kind) { 0 0 0
if (!tc || !tc->kind) { 0 0 0
5870 0 int imp = 0;
5871 0 char *t = NULL;
5872 0 size_t tl = 0;
5873 0 ▶ if (cd->nullable) {
Branch: 1 branch
BranchTrueFalseTotal
if (cd->nullable) { 0 0 0
5874 0 cd->to_null = 1;
5875 0 continue;
5876 }
5877 0 cd->subst_sv = zero_sv_for(aTHX_ ctypes[i], &imp);
5878 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
5879 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
5880 0 cd->subst_sv = NULL;
5881 0 goto no_client_default;
5882 }
5883 0 ▶ if (zero_text_for(ctypes[i], &t, &tl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (zero_text_for(ctypes[i], &t, &tl)) { 0 0 0
5884 0 cd->subst_text = t;
5885 0 cd->subst_text_len = tl;
5886 }
5887 0 continue;
5888 }
5889 0 ▶ if (tc->kind != 'D') {
Branch: 1 branch
BranchTrueFalseTotal
if (tc->kind != 'D') { 0 0 0
5890 0 const char *kw = (tc->kind == 'M') ? "MATERIALIZED"
5891 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
5892 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
5893 0 resolve_err_trunc(tc->expr, tc->expr_len, expr_tr,
5894 sizeof(expr_tr));
5895 0 snprintf(errbuf, errbuf_sz, "%s '%s': use data_format => "
5896 "'Native' for server-side defaults", kw, expr_tr);
5897 } else {
5898 0 snprintf(errbuf, errbuf_sz, "%s column: use data_format => "
5899 "'Native' for server-side defaults", kw);
5900 }
5901 0 goto col_fail;
5902 }
5903 0 ▶ lit = parse_default_literal(tc->expr ? tc->expr : "",
Branch: 1 branch
BranchTrueFalseTotal
lit = parse_default_literal(tc->expr ? tc->expr : "", 0 0 0
5904 0 ▶ tc->expr ? tc->expr_len : 0);
Branch: 1 branch
BranchTrueFalseTotal
tc->expr ? tc->expr_len : 0); 0 0 0
5905 0 ▶ if (lit && !lit_casts_ok(lit, ctypes[i])) {
Branch: 2 branches
BranchTrueFalseTotal
if (lit && !lit_casts_ok(lit, ctypes[i])) { 0 0 0
if (lit && !lit_casts_ok(lit, ctypes[i])) { 0 0 0
5906 0 free_lit(lit);
5907 0 lit = NULL;
5908 }
5909 0 ▶ if (!lit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit) { 0 0 0
5910 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
5911 0 resolve_err_trunc(tc->expr, tc->expr_len, expr_tr,
5912 sizeof(expr_tr));
5913 else
5914 0 expr_tr[0] = '\0';
5915 0 snprintf(errbuf, errbuf_sz, "non-literal DEFAULT '%s': use "
5916 "data_format => 'Native' for server-side defaults",
5917 expr_tr);
5918 0 goto col_fail;
5919 }
5920 0 ▶ if (!resolve_from_lit(aTHX_ cd, lit)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!resolve_from_lit(aTHX_ cd, lit)) { 0 0 0
5921 0 free_lit(lit);
5922 0 goto no_client_default;
5923 }
5924 0 free_lit(lit);
5925 0 continue;
5926 0 no_client_default:
5927 0 snprintf(errbuf, errbuf_sz, "no client-side default for empty "
5928 "values: use data_format => 'Native'");
5929 0 col_fail:
5930 /* Fails the insert only when a value needs the substitution. */
5931 {
5932 0 size_t n = strlen(errbuf);
5933 0 Newx(cd->err, n + 1, char);
5934 0 Copy(errbuf, cd->err, n + 1, char);
5935 0 errbuf[0] = '\0';
5936 }
5937 }
5938 0 ▶ if (self->insert_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_defaults) { 0 0 0
5939 HE *he;
5940 0 hv_iterinit(self->insert_defaults);
5941 0 ▶ while ((he = hv_iternext(self->insert_defaults))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(self->insert_defaults))) { 0 0 0
5942 I32 klen;
5943 0 char *key = hv_iterkey(he, &klen);
5944 0 int j, found = 0;
5945 0 ▶ for (j = 0; j < num_cols; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_cols; j++) { 0 0 0
5946 0 ▶ if (cname_lens[j] == (size_t)klen
Branch: 1 branch
BranchTrueFalseTotal
if (cname_lens[j] == (size_t)klen 0 0 0
5947 0 ▶ && memcmp(cnames[j], key, (size_t)klen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(cnames[j], key, (size_t)klen) == 0) { 0 0 0
5948 0 found = 1;
5949 0 break;
5950 }
5951 }
5952 0 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 0 0 0
5953 char k_tr[64];
5954 0 resolve_err_trunc(key, (size_t)klen, k_tr,
5955 sizeof(k_tr));
5956 0 snprintf(errbuf, errbuf_sz, "defaults key '%s' matches "
5957 "no insert column", k_tr);
5958 0 goto fail;
5959 }
5960 }
5961 }
5962 0 return cdefs;
5963 0 fail:
5964 0 free_col_defaults(cdefs, num_cols);
5965 0 return NULL;
5966 }
5967  
5968 /* --- Insert encoding error context --- */
5969  
5970 /* The column and rows of the encode call in progress, so a failure or an
5971 * exception can be traced back to its row. */
5972 typedef struct {
5973 int col; /* -1 outside a column */
5974 SSize_t r0, n;
5975 } enc_progress_t;
5976  
5977 #define ERR_VALUE_MAX 40
5978 #define ERR_NAME_MAX 60
5979  
5980 8 static void err_quote(native_buf_t *b, const char *s, size_t len) {
5981 8 size_t i, n = len > ERR_VALUE_MAX ? ERR_VALUE_MAX : len;
5982 8 nbuf_u8(b, '\'');
5983 124 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 116 8 124
5984 116 unsigned char c = (unsigned char)s[i];
5985 char esc[8];
5986 116 switch (c) {
5987 0 case '\'': nbuf_append(b, "\\'", 2); break;
5988 0 case '\\': nbuf_append(b, "\\\\", 2); break;
5989 0 case '\n': nbuf_append(b, "\\n", 2); break;
5990 0 case '\t': nbuf_append(b, "\\t", 2); break;
5991 0 case '\r': nbuf_append(b, "\\r", 2); break;
5992 116 default:
5993 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
5994 0 snprintf(esc, sizeof(esc), "\\x%02x", c);
5995 0 nbuf_append(b, esc, 4);
5996 } else {
5997 116 nbuf_u8(b, c);
5998 }
5999 }
6000 }
6001 8 nbuf_u8(b, '\'');
6002 8 ▶ if (n < len) nbuf_append(b, "...", 3);
Branch: 1 branch
BranchTrueFalseTotal
if (n < len) nbuf_append(b, "...", 3); 0 8 8
6003 8 }
6004  
6005 40 static void err_append(native_buf_t *b, const char *s) {
6006 40 nbuf_append(b, s, strlen(s));
6007 40 }
6008  
6009 0 static void err_zone(native_buf_t *b, const char *tz) {
6010 0 err_append(b, ": unknown timezone '");
6011 0 ▶ nbuf_append(b, tz, strlen(tz) > 60 ? 60 : strlen(tz));
Branch: 1 branch
BranchTrueFalseTotal
nbuf_append(b, tz, strlen(tz) > 60 ? 60 : strlen(tz)); 0 0 0
6012 0 err_append(b, "' or missing timezone database");
6013 0 }
6014  
6015 /* A value as error text, without running overloads: undef, the type of a
6016 * reference, else the quoted (escaped, truncated) string. */
6017 8 static void err_value_sv(pTHX_ native_buf_t *b, SV *v) {
6018 8 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 0 8 8
6019 0 SV *rv = SvRV(v);
6020 0 err_append(b, sv_reftype(rv, SvOBJECT(rv)));
6021 0 ▶ err_append(b, SvOBJECT(rv) ? " object" : " reference");
Branch: 1 branch
BranchTrueFalseTotal
err_append(b, SvOBJECT(rv) ? " object" : " reference"); 0 0 0
6022 8 ▶ } else if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!SvOK(v)) { 0 8 8
6023 0 err_append(b, "undef");
6024 } else {
6025 STRLEN l;
6026 8 const char *s = SvPV_nomg(v, l);
6027 8 err_quote(b, s, l);
6028 }
6029 8 }
6030  
6031 16 static void err_name(native_buf_t *b, const char *s, size_t len) {
6032 16 nbuf_append(b, s, len > ERR_NAME_MAX ? ERR_NAME_MAX : len);
6033 16 ▶ if (len > ERR_NAME_MAX) err_append(b, "...");
Branch: 1 branch
BranchTrueFalseTotal
if (len > ERR_NAME_MAX) err_append(b, "..."); 0 16 16
6034 16 }
6035  
6036 /* "column 'name' (type)" */
6037 8 static void err_where(char *dst, size_t sz, const char *name, size_t name_len,
6038 const char *type, size_t type_len) {
6039 native_buf_t b;
6040 8 nbuf_init(&b);
6041 8 err_append(&b, "column '");
6042 8 err_name(&b, name, name_len);
6043 8 err_append(&b, "' (");
6044 8 err_name(&b, type, type_len);
6045 8 err_append(&b, ")");
6046 8 snprintf(dst, sz, "%.*s", (int)b.len, b.data);
6047 8 Safefree(b.data);
6048 8 }
6049  
6050 8 static void err_finish(char *errbuf, size_t sz, native_buf_t *msg) {
6051 8 snprintf(errbuf, sz, "%.*s", (int)msg->len, msg->data);
6052 8 Safefree(msg->data);
6053 8 }
6054  
6055 /* Exception text on one line, the die location kept. */
6056 0 static void err_exception(pTHX_ native_buf_t *b, SV *err) {
6057 STRLEN l, i;
6058 const char *s;
6059 0 ▶ if (!err) { err_append(b, "died"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!err) { err_append(b, "died"); return; } 0 0 0
6060 0 ▶ if (SvROK(err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(err)) { 0 0 0
6061 0 err_append(b, "died with ");
6062 0 err_value_sv(aTHX_ b, err);
6063 0 return;
6064 }
6065 0 s = SvPV_nomg(err, l);
6066 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
6067 0 ▶ if (l > 160) l = 160;
Branch: 1 branch
BranchTrueFalseTotal
if (l > 160) l = 160; 0 0 0
6068 0 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 0 0 0
6069 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
6070 }
6071  
6072 8 static int sv_encodes(pTHX_ SV *v, col_type_t *ct, native_buf_t *scratch) {
6073 8 scratch->len = 0;
6074 8 return encode_column_sv(aTHX_ scratch, &v, 1, ct);
6075 }
6076  
6077 /* Inside a top-level value that does not encode, the innermost element
6078 * that does not encode on its own (the value itself when none). */
6079 8 static SV *locate_bad_sv(pTHX_ SV *v, col_type_t *ct, native_buf_t *scratch) {
6080 SSize_t j, n;
6081 8 switch (ct->code) {
6082 0 case CT_NULLABLE:
6083 case CT_LOWCARDINALITY:
6084 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
6085 0 case CT_ARRAY:
6086 case CT_TUPLE:
6087 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
6088 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
6089 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
6090 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
6091 0 SV **e = av_fetch((AV *)SvRV(v), j, 0);
6092 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
6093 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
6094 0 return locate_bad_sv(aTHX_ ev, et, scratch);
6095 }
6096 0 return v;
6097 0 case CT_MAP: {
6098 HV *hv;
6099 HE *he;
6100 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
6101 0 return v;
6102 0 hv = (HV *)SvRV(v);
6103 0 hv_iterinit(hv);
6104 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
6105 0 SV *k = hv_iterkeysv(he);
6106 0 SV *val = hv_iterval(hv, he);
6107 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
6108 0 return k;
6109 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
6110 0 return locate_bad_sv(aTHX_ val, ct->inners[1], scratch);
6111 }
6112 0 return v;
6113 }
6114 8 default:
6115 8 return v;
6116 }
6117 }
6118  
6119 /* Why encoding vals[0..n) as ct failed: the type cannot be encoded at
6120 * all, else the first value that fails on its own (row base + index),
6121 * with the failing element of a nested value. Re-encodes only on this
6122 * error path. */
6123 8 static void describe_sv_failure(pTHX_ char *errbuf, size_t sz,
6124 const char *where, col_type_t *ct,
6125 SV **vals, SSize_t n, SSize_t base,
6126 const char *unit) {
6127 native_buf_t msg, scratch;
6128 SSize_t k;
6129 8 nbuf_init(&msg);
6130 8 nbuf_init(&scratch);
6131 8 err_append(&msg, where);
6132 8 ▶ if (col_type_zone_error(ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_type_zone_error(ct)) { 0 8 8
6133 0 err_zone(&msg, col_type_zone_error(ct));
6134 8 ▶ } else if (!encode_column_sv(aTHX_ &scratch, vals, 0, ct)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!encode_column_sv(aTHX_ &scratch, vals, 0, ct)) { 0 8 8
6135 0 err_append(&msg, ": unsupported type");
6136 } else {
6137 8 ▶ for (k = 0; k < n; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) 8 0 8
6138 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
6139 8 ▶ if (k < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (k < n) { 8 0 8
6140 char rb[64];
6141 8 SV *bad = locate_bad_sv(aTHX_ vals[k], ct, &scratch);
6142 8 snprintf(rb, sizeof(rb), ", %s %ld: invalid %s ", unit,
6143 8 ▶ (long)(base + k), bad == vals[k] ? "value" : "element");
Branch: 1 branch
BranchTrueFalseTotal
(long)(base + k), bad == vals[k] ? "value" : "element"); 8 0 8
6144 8 err_append(&msg, rb);
6145 8 err_value_sv(aTHX_ &msg, bad);
6146 } else {
6147 0 err_append(&msg, ": invalid value");
6148 }
6149 }
6150 8 Safefree(scratch.data);
6151 8 err_finish(errbuf, sz, &msg);
6152 8 }
6153  
6154 /* describe_sv_failure() for the TabSeparated path; rows are lines. */
6155 0 static void describe_text_failure(char *errbuf, size_t sz, const char *where,
6156 col_type_t *ct, const char **vals,
6157 size_t *lens, SSize_t n) {
6158 native_buf_t msg, scratch;
6159 SSize_t k;
6160 0 nbuf_init(&msg);
6161 0 nbuf_init(&scratch);
6162 0 err_append(&msg, where);
6163 0 ▶ if (col_type_zone_error(ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_type_zone_error(ct)) { 0 0 0
6164 0 err_zone(&msg, col_type_zone_error(ct));
6165 0 ▶ } else if (!encode_column_text(&scratch, vals, lens, 0, ct)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!encode_column_text(&scratch, vals, lens, 0, ct)) { 0 0 0
6166 0 err_append(&msg, ": unsupported type");
6167 } else {
6168 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6169 0 scratch.len = 0;
6170 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
6171 }
6172 0 ▶ if (k < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (k < n) { 0 0 0
6173 char rb[48];
6174 0 snprintf(rb, sizeof(rb), ", line %ld: invalid value ", (long)(k + 1));
6175 0 err_append(&msg, rb);
6176 0 err_quote(&msg, vals[k], lens[k]);
6177 } else {
6178 0 err_append(&msg, ": invalid value");
6179 }
6180 }
6181 0 Safefree(scratch.data);
6182 0 err_finish(errbuf, sz, &msg);
6183 0 }
6184  
6185 /* Exceptions from user code (overloads, tied values) during the deferred
6186 * native encode must not escape the I/O callback: run_guarded() calls a
6187 * C function inside an eval through a private XSUB. */
6188 typedef struct {
6189 void (*fn)(pTHX_ void *);
6190 void *arg;
6191 } ch_guarded_t;
6192  
6193 static CV *ch_guard_cv;
6194  
6195 12 XS_INTERNAL(ch_guard_xs) {
6196 12 dXSARGS;
6197 ch_guarded_t *g;
6198 12 ▶ if (items != 1) croak_xs_usage(cv, "guard");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak_xs_usage(cv, "guard"); 0 12 12
6199 12 g = INT2PTR(ch_guarded_t *, SvIV(ST(0)));
6200 12 g->fn(aTHX_ g->arg);
6201 12 XSRETURN_EMPTY;
6202 }
6203  
6204 /* 1 when fn returned, 0 when it died (exception copied to *err when err
6205 * is given); $@ is left as it was. */
6206 12 static int run_guarded(pTHX_ void (*fn)(pTHX_ void *), void *arg, SV **err) {
6207 12 dSP;
6208 ch_guarded_t g;
6209 int died;
6210 12 g.fn = fn;
6211 12 g.arg = arg;
6212 12 ▶ if (err) *err = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = NULL; 12 0 12
6213 12 ENTER; SAVETMPS;
6214 12 save_scalar(PL_errgv);
6215 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
6216 12 ▶ mXPUSHi(PTR2IV(&g));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(PTR2IV(&g)); 0 12 12
6217 12 PUTBACK;
6218 12 call_sv((SV *)ch_guard_cv, G_EVAL | G_DISCARD);
6219 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
6220 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
6221 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 12 0 12
6222 12 return !died;
6223 }
6224  
6225 typedef struct {
6226 ev_clickhouse_t *self;
6227 const char **names; size_t *name_lens;
6228 col_type_t **types;
6229 const char **types_str; size_t *type_lens;
6230 int ncols;
6231 char *errbuf; size_t errbuf_sz;
6232 col_default_t *cdefs;
6233 } resolve_args_t;
6234  
6235 0 static void resolve_fn(pTHX_ void *p) {
6236 0 resolve_args_t *a = (resolve_args_t *)p;
6237 0 a->cdefs = resolve_insert_defaults(aTHX_ a->self, a->names, a->name_lens,
6238 a->types, a->types_str, a->type_lens, a->ncols, a->errbuf,
6239 a->errbuf_sz);
6240 0 }
6241  
6242 /* resolve_insert_defaults(): a caller's default value may be an object
6243 * whose stringification dies. */
6244 0 static col_default_t *resolve_insert_defaults_guarded(pTHX_ ev_clickhouse_t *self,
6245 const char **cnames, size_t *cname_lens, col_type_t **ctypes,
6246 const char **ctypes_str, size_t *ctype_lens, int num_cols,
6247 char *errbuf, size_t errbuf_sz) {
6248 resolve_args_t a;
6249 0 SV *err = NULL;
6250 0 a.self = self; a.names = cnames; a.name_lens = cname_lens;
6251 0 a.types = ctypes; a.types_str = ctypes_str; a.type_lens = ctype_lens;
6252 0 a.ncols = num_cols; a.errbuf = errbuf; a.errbuf_sz = errbuf_sz;
6253 0 a.cdefs = NULL;
6254 0 ▶ if (!run_guarded(aTHX_ resolve_fn, &a, &err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!run_guarded(aTHX_ resolve_fn, &a, &err)) { 0 0 0
6255 native_buf_t msg;
6256 0 nbuf_init(&msg);
6257 0 err_append(&msg, "defaults: ");
6258 0 err_exception(aTHX_ &msg, err);
6259 0 ▶ if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) SvREFCNT_dec(err); 0 0 0
6260 0 err_finish(errbuf, errbuf_sz, &msg);
6261 0 return NULL;
6262 }
6263 0 return a.cdefs;
6264 }
6265  
6266 /* Skip one column's data (prefixes already consumed). 1 = ok, 0 = malformed,
6267 * -1 = a type this walker does not know (caller decodes instead). */
6268 0 static int skip_column_data(const char *buf, size_t len, size_t *pos,
6269 uint64_t nrows, col_type_t *ct) {
6270 size_t fsz;
6271 uint64_t i;
6272 int rc;
6273 0 ▶ if (!ct) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) return -1; 0 0 0
6274 0 ▶ if (*pos > len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos > len) return 0; 0 0 0
6275 0 fsz = col_type_fixed_size(ct);
6276 0 ▶ if (fsz) {
Branch: 1 branch
BranchTrueFalseTotal
if (fsz) { 0 0 0
6277 0 ▶ if (nrows > (len - *pos) / fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > (len - *pos) / fsz) return 0; 0 0 0
6278 0 *pos += nrows * fsz;
6279 0 return 1;
6280 }
6281 0 switch (ct->code) {
6282 0 case CT_STRING:
6283 0 ▶ for (i = 0; i < nrows; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) 0 0 0
6284 0 ▶ if (skip_native_string(buf, len, pos) <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (skip_native_string(buf, len, pos) <= 0) return 0; 0 0 0
6285 0 return 1;
6286 0 case CT_NOTHING:
6287 0 ▶ if (nrows > len - *pos) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > len - *pos) return 0; 0 0 0
6288 0 *pos += nrows;
6289 0 return 1;
6290 0 case CT_NULLABLE:
6291 0 ▶ if (nrows > len - *pos) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > len - *pos) return 0; 0 0 0
6292 0 *pos += nrows;
6293 0 return skip_column_data(buf, len, pos, nrows, ct->inner);
6294 0 case CT_ARRAY:
6295 case CT_MAP: {
6296 0 uint64_t total = 0;
6297 0 ▶ if (nrows > (len - *pos) / 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > (len - *pos) / 8) return 0; 0 0 0
6298 0 ▶ if (nrows) memcpy(&total, buf + *pos + (nrows - 1) * 8, 8);
Branch: 1 branch
BranchTrueFalseTotal
if (nrows) memcpy(&total, buf + *pos + (nrows - 1) * 8, 8); 0 0 0
6299 0 *pos += nrows * 8;
6300 0 ▶ if (ct->code == CT_ARRAY)
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_ARRAY) 0 0 0
6301 0 return skip_column_data(buf, len, pos, total, ct->inner);
6302 0 ▶ if (ct->num_inners != 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners != 2) return -1; 0 0 0
6303 0 ▶ if ((rc = skip_column_data(buf, len, pos, total, ct->inners[0])) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((rc = skip_column_data(buf, len, pos, total, ct->inners[0])) <= 0) 0 0 0
6304 0 return rc;
6305 0 return skip_column_data(buf, len, pos, total, ct->inners[1]);
6306 }
6307 0 case CT_TUPLE:
6308 0 ▶ for (i = 0; i < (uint64_t)ct->num_inners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (uint64_t)ct->num_inners; i++) 0 0 0
6309 0 ▶ if ((rc = skip_column_data(buf, len, pos, nrows, ct->inners[i])) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((rc = skip_column_data(buf, len, pos, nrows, ct->inners[i])) <= 0) 0 0 0
6310 0 return rc;
6311 0 return 1;
6312 0 case CT_LOWCARDINALITY: {
6313 uint64_t ser_type, nkeys, nidx;
6314 size_t idx_size;
6315 col_type_t *dict_ct;
6316 0 ▶ if (!nrows) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!nrows) return 1; 0 0 0
6317 0 ▶ if (!ct->inner || len - *pos < 8) return !ct->inner ? -1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!ct->inner || len - *pos < 8) return !ct->inner ? -1 : 0; 0 0 0
if (!ct->inner || len - *pos < 8) return !ct->inner ? -1 : 0; 0 0 0
if (!ct->inner || len - *pos < 8) return !ct->inner ? -1 : 0; 0 0 0
6318 0 memcpy(&ser_type, buf + *pos, 8);
6319 0 *pos += 8;
6320 /* One keys block follows HasAdditionalKeys and/or NeedUpdateDictionary. */
6321 0 ▶ if ((ser_type & 0xFF) > 3 || !(ser_type & ((1ULL << 9) | (1ULL << 10))))
Branch: 2 branches
BranchTrueFalseTotal
if ((ser_type & 0xFF) > 3 || !(ser_type & ((1ULL << 9) | (1ULL << 10)))) 0 0 0
if ((ser_type & 0xFF) > 3 || !(ser_type & ((1ULL << 9) | (1ULL << 10)))) 0 0 0
6322 0 return 0;
6323 0 idx_size = (size_t)1 << (ser_type & 0xFF);
6324 0 ▶ dict_ct = ct->inner->code == CT_NULLABLE ? ct->inner->inner : ct->inner;
Branch: 1 branch
BranchTrueFalseTotal
dict_ct = ct->inner->code == CT_NULLABLE ? ct->inner->inner : ct->inner; 0 0 0
6325 0 ▶ if (len - *pos < 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len - *pos < 8) return 0; 0 0 0
6326 0 memcpy(&nkeys, buf + *pos, 8);
6327 0 *pos += 8;
6328 0 ▶ if ((rc = skip_column_data(buf, len, pos, nkeys, dict_ct)) <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if ((rc = skip_column_data(buf, len, pos, nkeys, dict_ct)) <= 0) return rc; 0 0 0
6329 0 ▶ if (len - *pos < 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len - *pos < 8) return 0; 0 0 0
6330 0 memcpy(&nidx, buf + *pos, 8);
6331 0 *pos += 8;
6332 0 ▶ if (nidx != nrows || nidx > (len - *pos) / idx_size) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (nidx != nrows || nidx > (len - *pos) / idx_size) return 0; 0 0 0
if (nidx != nrows || nidx > (len - *pos) / idx_size) return 0; 0 0 0
6333 0 *pos += nidx * idx_size;
6334 0 return 1;
6335 }
6336 0 default:
6337 0 return -1;
6338 }
6339 }
6340  
6341 /*
6342 * Turn a Native-format block (as ClickHouse::Encoder or `format Native`
6343 * writes it) into a protocol Data packet: BlockInfo first, and the
6344 * custom-serialization byte after each column type. Returns NULL with
6345 * errbuf set on a malformed block.
6346 */
6347 /* The server's native reader converts String <-> LowCardinality(String)
6348 * but turns other mismatches (a NULL for a non-Nullable column) into the
6349 * type's zero rather than the column DEFAULT: insist on the table's types. */
6350 0 static int block_type_matches(const char *have, size_t have_len,
6351 const char *want, size_t want_len) {
6352 0 ▶ if (have_len == want_len && memcmp(have, want, have_len) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (have_len == want_len && memcmp(have, want, have_len) == 0) return 1; 0 0 0
if (have_len == want_len && memcmp(have, want, have_len) == 0) return 1; 0 0 0
6353 0 ▶ if (want_len > 16 && memcmp(want, "LowCardinality(", 15) == 0
Branch: 2 branches
BranchTrueFalseTotal
if (want_len > 16 && memcmp(want, "LowCardinality(", 15) == 0 0 0 0
if (want_len > 16 && memcmp(want, "LowCardinality(", 15) == 0 0 0 0
6354 0 ▶ && want_len - 16 == have_len && memcmp(want + 15, have, have_len) == 0)
Branch: 2 branches
BranchTrueFalseTotal
&& want_len - 16 == have_len && memcmp(want + 15, have, have_len) == 0) 0 0 0
&& want_len - 16 == have_len && memcmp(want + 15, have, have_len) == 0) 0 0 0
6355 0 return 1;
6356 0 return 0;
6357 }
6358  
6359 0 static char* build_native_body_packet(ev_clickhouse_t *self, const char *buf,
6360 size_t len, const char **want_names,
6361 size_t *want_name_lens,
6362 const char **want_types,
6363 size_t *want_type_lens, int want_cols,
6364 size_t *out_len, char *errbuf,
6365 size_t errbuf_sz) {
6366 native_buf_t body;
6367 uint64_t ncols, nrows, c;
6368 0 size_t pos = 0;
6369 0 nbuf_init(&body);
6370 0 nbuf_block_info(&body);
6371 0 ▶ if (read_varuint(buf, len, &pos, &ncols) <= 0
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, &pos, &ncols) <= 0 0 0 0
6372 0 ▶ || read_varuint(buf, len, &pos, &nrows) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
|| read_varuint(buf, len, &pos, &nrows) <= 0) 0 0 0
6373 0 goto bad;
6374 0 ▶ if (ncols != (uint64_t)want_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (ncols != (uint64_t)want_cols) { 0 0 0
6375 0 snprintf(errbuf, errbuf_sz, "Native block has %llu columns, the insert %d",
6376 (unsigned long long)ncols, want_cols);
6377 0 goto fail;
6378 }
6379 0 nbuf_varuint(&body, ncols);
6380 0 nbuf_varuint(&body, nrows);
6381 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
6382 const char *name, *type;
6383 size_t name_len, type_len, start;
6384 col_type_t *ct;
6385 0 int rc = 0;
6386 0 ▶ if (read_native_string_ref(buf, len, &pos, &name, &name_len) <= 0
Branch: 1 branch
BranchTrueFalseTotal
if (read_native_string_ref(buf, len, &pos, &name, &name_len) <= 0 0 0 0
6387 0 ▶ || read_native_string_ref(buf, len, &pos, &type, &type_len) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
|| read_native_string_ref(buf, len, &pos, &type, &type_len) <= 0) 0 0 0
6388 0 goto bad;
6389 0 ▶ if (name_len != want_name_lens[c] || memcmp(name, want_names[c], name_len)
Branch: 2 branches
BranchTrueFalseTotal
if (name_len != want_name_lens[c] || memcmp(name, want_names[c], name_len) 0 0 0
if (name_len != want_name_lens[c] || memcmp(name, want_names[c], name_len) 0 0 0
6390 0 ▶ || !block_type_matches(type, type_len, want_types[c], want_type_lens[c])) {
Branch: 1 branch
BranchTrueFalseTotal
|| !block_type_matches(type, type_len, want_types[c], want_type_lens[c])) { 0 0 0
6391 0 snprintf(errbuf, errbuf_sz, "Native block column %llu '%.*s' (%.*s) does "
6392 "not match the table's '%.*s' (%.*s); send it without data_block",
6393 (unsigned long long)c,
6394 0 ▶ (int)(name_len > 60 ? 60 : name_len), name,
Branch: 1 branch
BranchTrueFalseTotal
(int)(name_len > 60 ? 60 : name_len), name, 0 0 0
6395 0 ▶ (int)(type_len > 60 ? 60 : type_len), type,
Branch: 1 branch
BranchTrueFalseTotal
(int)(type_len > 60 ? 60 : type_len), type, 0 0 0
6396 0 ▶ (int)(want_name_lens[c] > 60 ? 60 : want_name_lens[c]), want_names[c],
Branch: 1 branch
BranchTrueFalseTotal
(int)(want_name_lens[c] > 60 ? 60 : want_name_lens[c]), want_names[c], 0 0 0
6397 0 ▶ (int)(want_type_lens[c] > 60 ? 60 : want_type_lens[c]), want_types[c]);
Branch: 1 branch
BranchTrueFalseTotal
(int)(want_type_lens[c] > 60 ? 60 : want_type_lens[c]), want_types[c]); 0 0 0
6398 0 goto fail;
6399 }
6400 0 nbuf_string(&body, name, name_len);
6401 0 nbuf_string(&body, type, type_len);
6402 0 nbuf_u8(&body, 0);
6403 0 start = pos;
6404 0 ct = parse_col_type(type, type_len);
6405 0 ▶ if (ct && (!nrows || read_col_prefix(buf, len, &pos, ct) > 0))
Branch: 3 branches
BranchTrueFalseTotal
if (ct && (!nrows || read_col_prefix(buf, len, &pos, ct) > 0)) 0 0 0
if (ct && (!nrows || read_col_prefix(buf, len, &pos, ct) > 0)) 0 0 0
if (ct && (!nrows || read_col_prefix(buf, len, &pos, ct) > 0)) 0 0 0
6406 0 ▶ rc = nrows ? skip_column_data(buf, len, &pos, nrows, ct) : 1;
Branch: 1 branch
BranchTrueFalseTotal
rc = nrows ? skip_column_data(buf, len, &pos, nrows, ct) : 1; 0 0 0
6407 0 ▶ if (ct && rc < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct && rc < 0) { 0 0 0
if (ct && rc < 0) { 0 0 0
6408 0 int derr = 0;
6409 SV **vals;
6410 0 pos = start;
6411 0 vals = decode_column(buf, len, &pos, nrows, ct, &derr, 0);
6412 0 ▶ if (vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (vals) { 0 0 0
6413 uint64_t i;
6414 0 ▶ for (i = 0; i < nrows; i++) SvREFCNT_dec(vals[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) SvREFCNT_dec(vals[i]); 0 0 0
6415 0 Safefree(vals);
6416 0 rc = 1;
6417 }
6418 }
6419 0 free_col_type(ct);
6420 0 ▶ if (rc != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 1) { 0 0 0
6421 0 snprintf(errbuf, errbuf_sz, "malformed Native data in column '%.*s' (%.*s)",
6422 0 ▶ (int)(name_len > 60 ? 60 : name_len), name,
Branch: 1 branch
BranchTrueFalseTotal
(int)(name_len > 60 ? 60 : name_len), name, 0 0 0
6423 0 ▶ (int)(type_len > 60 ? 60 : type_len), type);
Branch: 1 branch
BranchTrueFalseTotal
(int)(type_len > 60 ? 60 : type_len), type); 0 0 0
6424 0 Safefree(body.data);
6425 0 *out_len = (size_t)-1;
6426 0 return NULL;
6427 }
6428 0 nbuf_append(&body, buf + start, pos - start);
6429 }
6430 0 ▶ if (pos != len) goto bad;
Branch: 1 branch
BranchTrueFalseTotal
if (pos != len) goto bad; 0 0 0
6431 0 return wrap_data_block(self, &body, out_len);
6432 0 bad:
6433 0 snprintf(errbuf, errbuf_sz, "malformed Native data");
6434 0 fail:
6435 0 Safefree(body.data);
6436 0 *out_len = (size_t)-1;
6437 0 return NULL;
6438 }
6439  
6440 /*
6441 * Build a native protocol Data block from TabSeparated text data.
6442 * col_names/col_types_str are string references into the sample block buffer.
6443 * Returns malloc'd packet data (CLIENT_DATA + block), or NULL on failure.
6444 */
6445 0 static char* build_native_insert_data(ev_clickhouse_t *self,
6446 const char *tsv_data, size_t tsv_len,
6447 const char **col_names, size_t *col_name_lens,
6448 const char **col_types_str, size_t *col_type_lens,
6449 col_type_t **col_types,
6450 int num_cols,
6451 col_default_t *cdefs,
6452 size_t *out_len,
6453 char *errbuf, size_t errbuf_sz) {
6454 /* Parse TSV into rows and fields */
6455 0 int nrows = 0, max_rows = 64;
6456 0 const char **fields = NULL; /* flat array: fields[row * num_cols + col] */
6457 0 size_t *field_lens = NULL;
6458 0 const char *p = tsv_data;
6459 0 const char *end = tsv_data + tsv_len;
6460  
6461 0 Newxz(fields, max_rows * num_cols, const char *);
6462 0 Newx(field_lens, max_rows * num_cols, size_t);
6463  
6464 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
6465 0 const char *line_end = memchr(p, '\n', end - p);
6466 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
6467 int col;
6468  
6469 /* No blank-line skipping: the server keeps a blank line as an
6470 * (empty-field) row, or errors when columns are missing. */
6471 /* The server rejects DOS line endings (error 117); match it
6472 * rather than silently storing the \r. Mid-field \r stays. */
6473 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
6474 0 Safefree(fields);
6475 0 Safefree(field_lens);
6476 0 snprintf(errbuf, errbuf_sz, "line %d ends with \\r "
6477 "(CRLF line endings are illegal in TabSeparated data)",
6478 nrows + 1);
6479 0 *out_len = (size_t)-1;
6480 0 return NULL;
6481 }
6482  
6483 0 ▶ if (nrows >= max_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows >= max_rows) { 0 0 0
6484 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
6485 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
6486 0 Safefree(fields);
6487 0 Safefree(field_lens);
6488 0 *out_len = 0;
6489 0 return NULL;
6490 }
6491 0 max_rows *= 2;
6492 0 Renew(fields, max_rows * num_cols, const char *);
6493 0 Renew(field_lens, max_rows * num_cols, size_t);
6494 }
6495  
6496 /* split line by tabs */
6497 {
6498 0 const char *fp = p;
6499 0 ▶ for (col = 0; col < num_cols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < num_cols; col++) { 0 0 0
6500 const char *tab;
6501 0 ▶ if (fp > line_limit) fp = line_limit;
Branch: 1 branch
BranchTrueFalseTotal
if (fp > line_limit) fp = line_limit; 0 0 0
6502 0 ▶ if (col < num_cols - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (col < num_cols - 1) { 0 0 0
6503 0 tab = memchr(fp, '\t', line_limit - fp);
6504 0 ▶ if (!tab) tab = line_limit;
Branch: 1 branch
BranchTrueFalseTotal
if (!tab) tab = line_limit; 0 0 0
6505 } else {
6506 0 tab = line_limit;
6507 }
6508 0 fields[nrows * num_cols + col] = fp;
6509 0 field_lens[nrows * num_cols + col] = tab - fp;
6510 0 fp = tab + 1;
6511 }
6512 }
6513 0 nrows++;
6514 0 p = line_limit + 1;
6515 }
6516  
6517 0 ▶ if (nrows == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) { 0 0 0
6518 0 Safefree(fields);
6519 0 Safefree(field_lens);
6520 0 *out_len = 0;
6521 0 return NULL;
6522 }
6523  
6524 /* Build the Data block body: block info + num_cols + num_rows + columns */
6525 {
6526 native_buf_t body;
6527 int col;
6528  
6529 0 nbuf_init(&body);
6530 0 nbuf_block_info(&body);
6531 0 nbuf_varuint(&body, (uint64_t)num_cols);
6532 0 nbuf_varuint(&body, (uint64_t)nrows);
6533  
6534 /* encode each column */
6535 0 ▶ for (col = 0; col < num_cols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < num_cols; col++) { 0 0 0
6536 const char **col_vals;
6537 size_t *col_vlens;
6538 int row;
6539 0 ▶ int sub = cdefs ? cdefs[col].subst : 0;
Branch: 1 branch
BranchTrueFalseTotal
int sub = cdefs ? cdefs[col].subst : 0; 0 0 0
6540 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
6541  
6542 /* column name and type */
6543 0 nbuf_string(&body, col_names[col], col_name_lens[col]);
6544 0 nbuf_string(&body, col_types_str[col], col_type_lens[col]);
6545 0 nbuf_u8(&body, 0); /* has_custom_serialization = false */
6546  
6547 /* gather column values from row-major fields */
6548 0 Newxz(col_vals, nrows, const char *);
6549 0 Newx(col_vlens, nrows, size_t);
6550 0 ▶ for (row = 0; row < nrows; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < nrows; row++) { 0 0 0
6551 0 const char *fp = fields[row * num_cols + col];
6552 0 size_t fl = field_lens[row * num_cols + col];
6553 0 ▶ if (sub && (fl == 0
Branch: 2 branches
BranchTrueFalseTotal
if (sub && (fl == 0 0 0 0
if (sub && (fl == 0 0 0 0
6554 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
6555 0 ▶ if (cdefs[col].to_null) {
Branch: 1 branch
BranchTrueFalseTotal
if (cdefs[col].to_null) { 0 0 0
6556 0 fp = "\\N";
6557 0 fl = 2;
6558 0 ▶ } else if (cdefs[col].subst_text) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cdefs[col].subst_text) { 0 0 0
6559 0 fp = cdefs[col].subst_text;
6560 0 fl = cdefs[col].subst_text_len;
6561 } else {
6562 char where[160];
6563 0 err_where(where, sizeof(where), col_names[col],
6564 0 col_name_lens[col], col_types_str[col],
6565 0 col_type_lens[col]);
6566 0 snprintf(errbuf, errbuf_sz, "%s, line %d: %s", where,
6567 0 ▶ row + 1, cdefs[col].err ? cdefs[col].err
Branch: 1 branch
BranchTrueFalseTotal
row + 1, cdefs[col].err ? cdefs[col].err 0 0 0
6568 : "no client-side default for empty values: "
6569 "use data_format => 'Native'");
6570 0 Safefree(col_vals);
6571 0 Safefree(col_vlens);
6572 0 Safefree(body.data);
6573 0 Safefree(fields);
6574 0 Safefree(field_lens);
6575 0 *out_len = (size_t)-1;
6576 0 return NULL;
6577 }
6578 }
6579 0 col_vals[row] = fp;
6580 0 col_vlens[row] = fl;
6581 }
6582  
6583 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
6584 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
6585 0 (uint64_t)nrows, col_types[col])) {
6586 char where[160];
6587 0 err_where(where, sizeof(where), col_names[col],
6588 0 col_name_lens[col], col_types_str[col],
6589 0 col_type_lens[col]);
6590 0 describe_text_failure(errbuf, errbuf_sz, where, col_types[col],
6591 col_vals, col_vlens, nrows);
6592 0 Safefree(col_vals);
6593 0 Safefree(col_vlens);
6594 0 Safefree(body.data);
6595 0 Safefree(fields);
6596 0 Safefree(field_lens);
6597 0 *out_len = (size_t)-1; /* sentinel: encode failure */
6598 0 return NULL;
6599 }
6600 0 Safefree(col_vals);
6601 0 Safefree(col_vlens);
6602 }
6603  
6604 0 Safefree(fields);
6605 0 Safefree(field_lens);
6606  
6607 0 return wrap_data_block(self, &body, out_len);
6608 }
6609 }
6610  
6611 16 static void nbuf_reserve(native_buf_t *b, size_t cap) {
6612 16 ▶ if (cap > b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (cap > b->cap) { 0 16 16
6613 0 b->cap = cap;
6614 0 Renew(b->data, cap, char);
6615 }
6616 16 }
6617  
6618 /* Rows per pass of the insert build: a pass encodes every column of a
6619 * chunk of rows while their scalars are cache-hot, instead of walking all
6620 * rows' scalars once per column. */
6621 #define INSERT_CHUNK_ROWS 1024
6622  
6623 enum { ENC_WHOLE, ENC_PLAIN, ENC_NULLABLE, ENC_ARRAY, ENC_LC };
6624  
6625 /* Types whose column encoding is the concatenation of per-value bytes. */
6626 12 static int enc_plain_type(const col_type_t *ct) {
6627 12 ▶ switch (ct->code) {
Branch: 1 branch
BranchTrueFalseTotal
switch (ct->code) { 12 0 12
6628 12 case CT_INT8: case CT_INT16: case CT_INT32: case CT_INT64:
6629 case CT_UINT8: case CT_UINT16: case CT_UINT32: case CT_UINT64:
6630 case CT_BOOL: case CT_FLOAT32: case CT_FLOAT64: case CT_BFLOAT16:
6631 case CT_DATE: case CT_DATE32: case CT_DATETIME: case CT_DATETIME64:
6632 case CT_TIME: case CT_TIME64:
6633 case CT_DECIMAL32: case CT_DECIMAL64: case CT_DECIMAL128: case CT_DECIMAL256:
6634 case CT_STRING: case CT_FIXEDSTRING: case CT_UUID: case CT_IPV4: case CT_IPV6:
6635 case CT_ENUM8: case CT_ENUM16:
6636 12 return 1;
6637 0 default:
6638 0 return 0;
6639 }
6640 }
6641  
6642 /* Nullable/Array/LowCardinality of a plain type encode a chunk as parts
6643 * that concatenate across chunks; anything else is encoded whole. */
6644 12 static int enc_col_mode(const col_type_t *ct) {
6645 12 ▶ if (enc_plain_type(ct)) return ENC_PLAIN;
Branch: 1 branch
BranchTrueFalseTotal
if (enc_plain_type(ct)) return ENC_PLAIN; 12 0 12
6646 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
6647 0 switch (ct->code) {
6648 0 case CT_NULLABLE: return ENC_NULLABLE;
6649 0 case CT_ARRAY: return ENC_ARRAY;
6650 0 case CT_LOWCARDINALITY: return ENC_LC;
6651 0 default: return ENC_WHOLE;
6652 }
6653 }
6654  
6655 typedef struct {
6656 int mode;
6657 native_buf_t data; /* values, array elements or LC dictionary */
6658 native_buf_t aux; /* null map or array offsets */
6659 uint64_t base; /* array elements so far */
6660 } enc_col_t;
6661  
6662 typedef struct {
6663 enc_col_t *cols;
6664 int ncols;
6665 SV **vals;
6666 native_buf_t tmp;
6667 native_buf_t out;
6668 } enc_ctx_t;
6669  
6670 12 static void enc_ctx_free(pTHX_ void *p) {
6671 12 enc_ctx_t *x = (enc_ctx_t *)p;
6672 int c;
6673 24 ▶ for (c = 0; c < x->ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < x->ncols; c++) { 12 12 24
6674 12 Safefree(x->cols[c].data.data);
6675 12 Safefree(x->cols[c].aux.data);
6676 }
6677 12 Safefree(x->cols);
6678 12 Safefree(x->vals);
6679 12 Safefree(x->tmp.data);
6680 12 Safefree(x->out.data);
6681 12 Safefree(x);
6682 12 }
6683  
6684 /* Gather one column of n rows, with empty cells replaced by the column's
6685 * resolved default when cd asks for it: '' always, undef unless the
6686 * column is Nullable. */
6687 20 static SSize_t snap_gather(const insert_snap_t *sn, SSize_t r0, SSize_t n,
6688 int col, const col_default_t *cd, SV **vals) {
6689 SSize_t k;
6690 SV *to;
6691 40 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 20 20 40
6692 20 SSize_t o = sn->off[r0 + k];
6693 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
6694 20 ▶ vals[k] = v ? v : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
vals[k] = v ? v : &PL_sv_undef; 20 0 20
6695 }
6696 20 ▶ if (!cd || !cd->subst) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!cd || !cd->subst) return -1; 0 20 20
if (!cd || !cd->subst) return -1; 0 0 0
6697 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
6698 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6699 0 SV *v = vals[k];
6700 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
6701 0 ▶ if (!to) return cd->err ? k : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!to) return cd->err ? k : -1; 0 0 0
if (!to) return cd->err ? k : -1; 0 0 0
6702 0 vals[k] = to;
6703 }
6704 }
6705 0 return -1;
6706 }
6707  
6708 /* Move one chunk's encoding of n rows (tmp) into the column's parts. */
6709 0 static void enc_split_chunk(enc_col_t *ec, const native_buf_t *tmp, SSize_t n) {
6710 0 switch (ec->mode) {
6711 0 case ENC_NULLABLE:
6712 0 nbuf_append(&ec->aux, tmp->data, (size_t)n);
6713 0 nbuf_append(&ec->data, tmp->data + n, tmp->len - (size_t)n);
6714 0 break;
6715 0 case ENC_ARRAY: {
6716 SSize_t k;
6717 0 uint64_t off = 0, abs;
6718 0 nbuf_grow(&ec->aux, (size_t)n * 8);
6719 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6720 0 memcpy(&off, tmp->data + k * 8, 8);
6721 0 abs = ec->base + off;
6722 0 memcpy(ec->aux.data + ec->aux.len, &abs, 8);
6723 0 ec->aux.len += 8;
6724 }
6725 0 ec->base += off;
6726 0 nbuf_append(&ec->data, tmp->data + n * 8, tmp->len - (size_t)n * 8);
6727 0 break;
6728 }
6729 0 case ENC_LC: {
6730 /* ser_type, num_keys, dictionary, num_indices, indices */
6731 0 int key_type = (unsigned char)tmp->data[0];
6732 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
6733 0 nbuf_append(&ec->data, tmp->data + 16, tmp->len - 24 - (size_t)n * w);
6734 0 break;
6735 }
6736 0 default:
6737 0 break;
6738 }
6739 0 }
6740  
6741 /*
6742 * Build a native protocol Data block from an insert snapshot.
6743 * Like build_native_insert_data() but encodes SVs directly via encode_column_sv().
6744 */
6745 12 static char* build_native_insert_data_from_av(pTHX_ ev_clickhouse_t *self,
6746 SV *snap_sv,
6747 const char **col_names, size_t *col_name_lens,
6748 const char **col_types_str, size_t *col_type_lens,
6749 col_type_t **col_types,
6750 int num_cols,
6751 col_default_t *cdefs,
6752 enc_progress_t *prog,
6753 size_t *out_len,
6754 char *errbuf, size_t errbuf_sz) {
6755 12 insert_snap_t *sn = insert_snap_get(aTHX_ snap_sv);
6756 12 ▶ SSize_t nrows = sn ? sn->nrows : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t nrows = sn ? sn->nrows : 0; 12 0 12
6757 enc_ctx_t *x;
6758 12 SSize_t r0, fail_r0 = 0, fail_n = 0, fail_nodef = -1;
6759 12 int c, fail = num_cols;
6760 size_t need;
6761 char *pkt;
6762  
6763 12 ▶ if (nrows <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows <= 0) { 0 12 12
6764 0 *out_len = 0;
6765 0 return NULL;
6766 }
6767 12 ▶ if (sn->bad_row >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sn->bad_row >= 0) { 0 12 12
6768 0 snprintf(errbuf, errbuf_sz, "row %ld is not an arrayref",
6769 0 (long)sn->bad_row);
6770 0 *out_len = (size_t)-1; /* sentinel: encode failure */
6771 0 return NULL;
6772 }
6773 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
6774 0 snprintf(errbuf, errbuf_sz, "columns_at has %ld positions but the "
6775 0 "insert has %d columns", (long)sn->width, num_cols);
6776 0 *out_len = (size_t)-1;
6777 0 return NULL;
6778 }
6779  
6780 12 ENTER;
6781 12 Newxz(x, 1, enc_ctx_t);
6782 12 SAVEDESTRUCTOR_X(enc_ctx_free, x);
6783 12 Newxz(x->cols, num_cols, enc_col_t);
6784 12 x->ncols = num_cols;
6785 12 ▶ Newx(x->vals, nrows, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(x->vals, nrows, SV *); 0 12 12
6786 12 nbuf_init(&x->tmp);
6787 24 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 12 12 24
6788 12 enc_col_t *ec = &x->cols[c];
6789 12 size_t fsz = col_type_fixed_size(col_types[c]);
6790 12 ec->mode = enc_col_mode(col_types[c]);
6791 12 nbuf_init(&ec->data);
6792 12 nbuf_init(&ec->aux);
6793 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
6794 12 nbuf_reserve(&ec->data, fsz * (size_t)nrows);
6795 0 ▶ else if (ec->mode == ENC_NULLABLE)
Branch: 1 branch
BranchTrueFalseTotal
else if (ec->mode == ENC_NULLABLE) 0 0 0
6796 0 nbuf_reserve(&ec->aux, (size_t)nrows);
6797 0 ▶ else if (ec->mode == ENC_ARRAY)
Branch: 1 branch
BranchTrueFalseTotal
else if (ec->mode == ENC_ARRAY) 0 0 0
6798 0 nbuf_reserve(&ec->aux, 8 * (size_t)nrows);
6799 }
6800  
6801 /* A failing column stops the work on itself and every later column,
6802 * so the column reported is the first failing one, as column order
6803 * would find it. */
6804 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
6805 12 SSize_t n = nrows - r0 < INSERT_CHUNK_ROWS ? nrows - r0 : INSERT_CHUNK_ROWS;
6806 12 ENTER; SAVETMPS;
6807 24 ▶ for (c = 0; c < fail; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < fail; c++) { 12 12 24
6808 12 enc_col_t *ec = &x->cols[c];
6809 int ok;
6810 SSize_t nodef;
6811 12 ▶ if (ec->mode == ENC_WHOLE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_WHOLE) continue; 0 12 12
6812 12 ▶ nodef = snap_gather(sn, r0, n, c, cdefs ? &cdefs[c] : NULL, x->vals);
Branch: 1 branch
BranchTrueFalseTotal
nodef = snap_gather(sn, r0, n, c, cdefs ? &cdefs[c] : NULL, x->vals); 0 12 12
6813 12 ▶ if (nodef >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nodef >= 0) { 0 12 12
6814 0 fail = c; fail_r0 = r0; fail_n = n; fail_nodef = r0 + nodef;
6815 0 break;
6816 }
6817 12 prog->col = c; prog->r0 = r0; prog->n = n;
6818 12 ▶ if (ec->mode == ENC_PLAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_PLAIN) { 12 0 12
6819 12 ok = encode_column_sv(aTHX_ &ec->data, x->vals, (uint64_t)n,
6820 12 col_types[c]);
6821 } else {
6822 0 x->tmp.len = 0;
6823 0 ok = encode_column_sv(aTHX_ &x->tmp, x->vals, (uint64_t)n,
6824 0 col_types[c]);
6825 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
6826 }
6827 12 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 8 4 12
6828 8 fail = c; fail_r0 = r0; fail_n = n; fail_nodef = -1;
6829 }
6830 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
6831 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
6832 0 nbuf_reserve(&ec->data,
6833 0 (size_t)((double)ec->data.len / n * nrows * 1.1) + 64);
6834 }
6835 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 12 12
6836 }
6837 16 ▶ for (c = 0; c < fail; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < fail; c++) { 4 12 16
6838 4 ▶ if (x->cols[c].mode != ENC_WHOLE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (x->cols[c].mode != ENC_WHOLE) continue; 4 0 4
6839 SSize_t nodef;
6840 0 ENTER; SAVETMPS;
6841 0 ▶ nodef = snap_gather(sn, 0, nrows, c, cdefs ? &cdefs[c] : NULL, x->vals);
Branch: 1 branch
BranchTrueFalseTotal
nodef = snap_gather(sn, 0, nrows, c, cdefs ? &cdefs[c] : NULL, x->vals); 0 0 0
6842 0 prog->col = c; prog->r0 = 0; prog->n = nrows;
6843 0 ▶ if (nodef >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nodef >= 0) { 0 0 0
6844 0 fail = c; fail_r0 = 0; fail_n = nrows; fail_nodef = nodef;
6845 0 ▶ } else if (!encode_column_sv(aTHX_ &x->cols[c].data, x->vals,
Branch: 1 branch
BranchTrueFalseTotal
} else if (!encode_column_sv(aTHX_ &x->cols[c].data, x->vals, 0 0 0
6846 0 (uint64_t)nrows, col_types[c])) {
6847 0 fail = c; fail_r0 = 0; fail_n = nrows; fail_nodef = -1;
6848 }
6849 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
6850 }
6851 12 ▶ if (fail < num_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (fail < num_cols) { 8 4 12
6852 char where[160];
6853 8 prog->col = fail; prog->r0 = fail_r0; prog->n = fail_n;
6854 8 err_where(where, sizeof(where), col_names[fail], col_name_lens[fail],
6855 8 col_types_str[fail], col_type_lens[fail]);
6856 8 ▶ if (fail_nodef >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fail_nodef >= 0) { 0 8 8
6857 0 snprintf(errbuf, errbuf_sz, "%s, row %ld: %s", where,
6858 0 (long)fail_nodef, cdefs[fail].err);
6859 } else {
6860 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
6861 x->vals);
6862 8 describe_sv_failure(aTHX_ errbuf, errbuf_sz, where, col_types[fail],
6863 x->vals, fail_n, fail_r0, "row");
6864 }
6865 8 *out_len = (size_t)-1; /* sentinel: encode failure */
6866 8 LEAVE;
6867 8 return NULL;
6868 }
6869 4 prog->col = -1;
6870  
6871 4 need = 64;
6872 8 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 4 4 8
6873 4 need += col_name_lens[c] + col_type_lens[c] + 64
6874 4 + x->cols[c].data.len + x->cols[c].aux.len;
6875 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
6876 }
6877 4 nbuf_init(&x->out);
6878 4 nbuf_reserve(&x->out, need);
6879 4 ▶ if (!self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->compress) { 4 0 4
6880 4 nbuf_varuint(&x->out, CLIENT_DATA);
6881 4 nbuf_cstring(&x->out, ""); /* table name */
6882 }
6883 4 nbuf_block_info(&x->out);
6884 4 nbuf_varuint(&x->out, (uint64_t)num_cols);
6885 4 nbuf_varuint(&x->out, (uint64_t)nrows);
6886 8 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 4 4 8
6887 4 enc_col_t *ec = &x->cols[c];
6888 4 nbuf_string(&x->out, col_names[c], col_name_lens[c]);
6889 4 nbuf_string(&x->out, col_types_str[c], col_type_lens[c]);
6890 4 nbuf_u8(&x->out, 0); /* has_custom_serialization = false */
6891 4 write_col_prefix(&x->out, col_types[c]);
6892 4 ▶ if (ec->mode == ENC_LC) {
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_LC) { 0 4 4
6893 size_t idx_size;
6894 0 lc_write_keys_header(&x->out, (uint64_t)nrows, &idx_size);
6895 0 nbuf_append(&x->out, ec->data.data, ec->data.len);
6896 0 lc_write_indices(&x->out, (uint64_t)nrows, idx_size);
6897 } else {
6898 4 nbuf_append(&x->out, ec->aux.data, ec->aux.len);
6899 4 nbuf_append(&x->out, ec->data.data, ec->data.len);
6900 }
6901 4 Safefree(ec->data.data); ec->data.data = NULL;
6902 4 Safefree(ec->aux.data); ec->aux.data = NULL;
6903 }
6904  
6905 4 ▶ if (!self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->compress) { 4 0 4
6906 4 nbuf_empty_data_block(&x->out, 0);
6907 4 pkt = x->out.data;
6908 4 *out_len = x->out.len;
6909 } else {
6910 0 pkt = wrap_data_block(self, &x->out, out_len);
6911 }
6912 4 x->out.data = NULL;
6913 4 LEAVE;
6914 4 return pkt;
6915 }
6916  
6917 typedef struct {
6918 ev_clickhouse_t *self;
6919 SV *snap;
6920 const char **names; size_t *name_lens;
6921 const char **types_str; size_t *type_lens;
6922 col_type_t **types;
6923 int ncols;
6924 col_default_t *cdefs;
6925 enc_progress_t prog;
6926 size_t *out_len;
6927 char *errbuf; size_t errbuf_sz;
6928 char *pkt;
6929 } av_build_t;
6930  
6931 12 static void av_build_fn(pTHX_ void *p) {
6932 12 av_build_t *a = (av_build_t *)p;
6933 12 a->pkt = build_native_insert_data_from_av(aTHX_ a->self, a->snap,
6934 a->names, a->name_lens, a->types_str, a->type_lens, a->types,
6935 a->ncols, a->cdefs, &a->prog, a->out_len, a->errbuf, a->errbuf_sz);
6936 12 }
6937  
6938 typedef struct {
6939 SV *v;
6940 col_type_t *ct;
6941 native_buf_t *scratch;
6942 } encode_one_t;
6943  
6944 0 static void encode_one_fn(pTHX_ void *p) {
6945 0 encode_one_t *a = (encode_one_t *)p;
6946 0 (void)sv_encodes(aTHX_ a->v, a->ct, a->scratch);
6947 0 }
6948  
6949 /* build_native_insert_data_from_av() with exceptions from user code
6950 * turned into an encode failure naming the column and row that died. */
6951 12 static char* build_native_insert_data_from_av_guarded(pTHX_ ev_clickhouse_t *self,
6952 SV *snap_sv,
6953 const char **col_names, size_t *col_name_lens,
6954 const char **col_types_str, size_t *col_type_lens,
6955 col_type_t **col_types,
6956 int num_cols,
6957 col_default_t *cdefs,
6958 size_t *out_len,
6959 char *errbuf, size_t errbuf_sz) {
6960 av_build_t a;
6961 12 SV *err = NULL;
6962 native_buf_t msg;
6963 int c;
6964  
6965 12 a.self = self; a.snap = snap_sv;
6966 12 a.names = col_names; a.name_lens = col_name_lens;
6967 12 a.types_str = col_types_str; a.type_lens = col_type_lens;
6968 12 a.types = col_types; a.ncols = num_cols; a.cdefs = cdefs;
6969 12 a.prog.col = -1; a.prog.r0 = 0; a.prog.n = 0;
6970 12 a.out_len = out_len; a.errbuf = errbuf; a.errbuf_sz = errbuf_sz;
6971 12 a.pkt = NULL;
6972 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
6973 12 return a.pkt;
6974  
6975 0 nbuf_init(&msg);
6976 0 c = a.prog.col;
6977 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
6978 0 insert_snap_t *sn = insert_snap_get(aTHX_ snap_sv);
6979 char where[160];
6980 0 SSize_t k, row = -1;
6981 0 err_where(where, sizeof(where), col_names[c], col_name_lens[c],
6982 0 col_types_str[c], col_type_lens[c]);
6983 0 err_append(&msg, where);
6984 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
6985 SV **vals;
6986 native_buf_t scratch;
6987 encode_one_t one;
6988 0 ▶ Newx(vals, a.prog.n, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(vals, a.prog.n, SV *); 0 0 0
6989 0 nbuf_init(&scratch);
6990 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
6991 0 cdefs ? &cdefs[c] : NULL, vals);
6992 0 one.ct = col_types[c];
6993 0 one.scratch = &scratch;
6994 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
6995 0 one.v = vals[k];
6996 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
6997 0 row = a.prog.r0 + k;
6998 }
6999 0 Safefree(scratch.data);
7000 0 Safefree(vals);
7001 }
7002 0 ▶ if (row >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (row >= 0) { 0 0 0
7003 char rb[48];
7004 0 snprintf(rb, sizeof(rb), ", row %ld", (long)row);
7005 0 err_append(&msg, rb);
7006 }
7007 0 err_append(&msg, ": ");
7008 }
7009 0 err_exception(aTHX_ &msg, err);
7010 0 ▶ if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) SvREFCNT_dec(err); 0 0 0
7011 0 err_finish(errbuf, errbuf_sz, &msg);
7012 0 *out_len = (size_t)-1; /* sentinel: encode failure */
7013 0 return NULL;
7014 }
7015  
7016 /*
7017 * Build the external-table Data packets for a native query. `external`
7018 * maps table-name => { structure => [name => type, ...], data => [[..],..] }.
7019 * Each entry becomes one CLIENT_DATA packet (named block, optionally LZ4)
7020 * to be spliced into the query stream before the terminating empty block.
7021 *
7022 * Returns a malloc'd buffer of the concatenated packets with *out_len set
7023 * (an empty 0-length buffer when `external` has no tables). On error
7024 * returns NULL and writes a message into errbuf. The caller must
7025 * Safefree the returned buffer.
7026 */
7027 0 static char* build_external_tables(pTHX_ ev_clickhouse_t *self, HV *external,
7028 const char *tz, size_t *out_len,
7029 char *errbuf, size_t errbuf_sz) {
7030 native_buf_t out;
7031 HE *he;
7032  
7033 0 *out_len = 0;
7034 0 nbuf_init(&out);
7035 0 hv_iterinit(external);
7036  
7037 0 ▶ while ((he = hv_iternext(external))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(external))) { 0 0 0
7038 I32 tnlen;
7039 0 const char *tname = hv_iterkey(he, &tnlen);
7040 0 SV *spec_sv = hv_iterval(external, he);
7041 HV *spec;
7042 SV **st, **dt;
7043 AV *structure, *rows;
7044 SSize_t sn, nrows, row;
7045 int ncols, col;
7046 native_buf_t body;
7047 0 body.data = NULL;
7048  
7049 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
7050 0 snprintf(errbuf, errbuf_sz, "external table '%s': spec must be a "
7051 "hashref { structure => [...], data => [...] }", tname);
7052 0 goto fail;
7053 }
7054 0 spec = (HV *)SvRV(spec_sv);
7055 0 st = hv_fetchs(spec, "structure", 0);
7056 0 dt = hv_fetchs(spec, "data", 0);
7057 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
7058 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'structure' must "
7059 "be an arrayref of name => type pairs", tname);
7060 0 goto fail;
7061 }
7062 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
7063 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'data' must be "
7064 "an arrayref of rows", tname);
7065 0 goto fail;
7066 }
7067 0 structure = (AV *)SvRV(*st);
7068 0 rows = (AV *)SvRV(*dt);
7069 0 sn = av_len(structure) + 1;
7070 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
7071 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'structure' needs "
7072 "a non-empty, even list of name => type pairs", tname);
7073 0 goto fail;
7074 }
7075 0 ncols = (int)(sn / 2);
7076 0 nrows = av_len(rows) + 1;
7077 0 ▶ if (nrows < 0) nrows = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows < 0) nrows = 0; 0 0 0
7078  
7079 /* block body: block info + num_columns + num_rows + columns */
7080 0 nbuf_init(&body);
7081 0 nbuf_block_info(&body);
7082 0 nbuf_varuint(&body, (uint64_t)ncols);
7083 0 nbuf_varuint(&body, (uint64_t)nrows);
7084  
7085 0 ▶ for (col = 0; col < ncols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < ncols; col++) { 0 0 0
7086 0 SV **nm = av_fetch(structure, col * 2, 0);
7087 0 SV **ty = av_fetch(structure, col * 2 + 1, 0);
7088 STRLEN cnl, ctl;
7089 const char *cname, *ctype;
7090 col_type_t *ct;
7091 SV **col_vals;
7092 int enc;
7093  
7094 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
7095 0 snprintf(errbuf, errbuf_sz, "external table '%s': structure "
7096 "name/type entries must be defined", tname);
7097 0 Safefree(body.data);
7098 0 goto fail;
7099 }
7100 0 cname = SvPV(*nm, cnl);
7101 0 ▶ ctype = SvPV(*ty, ctl);
Branch: 1 branch
BranchTrueFalseTotal
ctype = SvPV(*ty, ctl); 0 0 0
7102 0 nbuf_string(&body, cname, cnl);
7103 0 nbuf_string(&body, ctype, ctl);
7104 0 nbuf_u8(&body, 0); /* has_custom_serialization = false */
7105  
7106 0 ct = parse_col_type(ctype, ctl);
7107 0 col_type_default_tz(ct, tz, &self->zones);
7108  
7109 /* gather this column's values from the row-major data */
7110 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
7111 0 ▶ for (row = 0; row < nrows; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < nrows; row++) { 0 0 0
7112 0 SV **rsv = av_fetch(rows, row, 0);
7113 AV *rav;
7114 SV **vsv;
7115 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
7116 0 snprintf(errbuf, errbuf_sz, "external table '%s': each data "
7117 "row must be an arrayref", tname);
7118 0 Safefree(col_vals);
7119 0 free_col_type(ct);
7120 0 Safefree(body.data);
7121 0 goto fail;
7122 }
7123 0 rav = (AV *)SvRV(*rsv);
7124 0 vsv = av_fetch(rav, col, 0);
7125 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
7126 }
7127  
7128 0 enc = 1;
7129 0 ▶ if (nrows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows) { 0 0 0
7130 0 write_col_prefix(&body, ct);
7131 0 enc = encode_column_sv(aTHX_ &body, col_vals,
7132 (uint64_t)nrows, ct);
7133 }
7134 0 ▶ if (!enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) { 0 0 0
7135 char where[160], what[512];
7136 0 err_where(where, sizeof(where), cname, cnl, ctype, ctl);
7137 0 describe_sv_failure(aTHX_ what, sizeof(what), where, ct,
7138 col_vals, nrows, 0, "row");
7139 0 snprintf(errbuf, errbuf_sz, "external table '%s': %s",
7140 tname, what);
7141 }
7142 0 Safefree(col_vals);
7143 0 free_col_type(ct);
7144 0 ▶ if (!enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) { 0 0 0
7145 0 Safefree(body.data);
7146 0 goto fail;
7147 }
7148 }
7149  
7150 /* [CLIENT_DATA] [table name] [block — optionally LZ4-compressed] */
7151 0 nbuf_varuint(&out, CLIENT_DATA);
7152 0 nbuf_string(&out, tname, (size_t)tnlen);
7153 #ifdef HAVE_LZ4
7154 0 ▶ if (self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress) { 0 0 0
7155 char *comp;
7156 size_t comp_len;
7157 0 comp = ch_lz4_compress(body.data, body.len, &comp_len);
7158 0 Safefree(body.data);
7159 0 body.data = NULL;
7160 0 ▶ if (!comp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!comp) { 0 0 0
7161 0 snprintf(errbuf, errbuf_sz, "external table '%s': LZ4 "
7162 "compression failed", tname);
7163 0 goto fail;
7164 }
7165 0 nbuf_append(&out, comp, comp_len);
7166 0 Safefree(comp);
7167 } else
7168 #endif
7169 {
7170 0 nbuf_append(&out, body.data, body.len);
7171 0 Safefree(body.data);
7172 0 body.data = NULL;
7173 }
7174 }
7175  
7176 0 *out_len = out.len;
7177 0 return out.data;
7178  
7179 0 fail:
7180 0 Safefree(out.data);
7181 0 *out_len = 0;
7182 0 return NULL;
7183 }
7184  
« xs/lz4.c ↑ Summary xs/tzif.c »