xs/types.c

filter: Statementstmt 9.0% 415 / 4577 Branchbran 4.6% 224 / 4776 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 6.8% 639 / 9353 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 /* --- LowCardinality dictionaries ---
2359 * A column's distinct values travel once, in first-seen order, and each
2360 * row as its key. Values compare by their encoded bytes: the plain types
2361 * a dictionary holds have one encoding per value. */
2362  
2363 typedef struct {
2364 native_buf_t dict; /* the keys' encoded values, back to back */
2365 size_t *koff; /* key k is dict[koff[k] .. koff[k + 1]) */
2366 uint64_t *khash;
2367 uint64_t nkeys, kcap;
2368 uint64_t base; /* keys below it (the NULL slot) are never matched */
2369 uint32_t *slots; /* key + 1; 0 is free */
2370 size_t mask;
2371 uint32_t *idx; /* each row's key */
2372 uint64_t nidx, icap;
2373 int distinct; /* values seen so far were nearly all new: no more lookups */
2374 } lc_dict_t;
2375  
2376 /* Lookups stop, checked every LC_SAMPLE_ROWS rows, once the column has
2377 * LC_MIN_KEYS keys and more than LC_DISTINCT_TENTHS of its rows were new:
2378 * a dictionary below that size still pays for itself on the wire */
2379 #define LC_SAMPLE_ROWS 4096
2380 #define LC_MIN_KEYS 32768
2381 #define LC_DISTINCT_TENTHS 9
2382  
2383 static uint64_t lc_seed;
2384  
2385 0 static uint64_t lc_mix(uint64_t h) {
2386 0 h ^= h >> 33;
2387 0 h *= 0xff51afd7ed558ccdULL;
2388 0 h ^= h >> 33;
2389 0 h *= 0xc4ceb9fe1a85ec53ULL;
2390 0 h ^= h >> 33;
2391 0 return h;
2392 }
2393  
2394 0 static uint64_t lc_hash(const char *p, size_t n) {
2395 0 uint64_t h = lc_seed ^ ((uint64_t)n * 0x9e3779b97f4a7c15ULL), k;
2396 0 ▶ while (n >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
while (n >= 8) { 0 0 0
2397 0 memcpy(&k, p, 8);
2398 0 h = (h ^ k) * 0x9e3779b97f4a7c15ULL;
2399 0 h ^= h >> 29;
2400 0 p += 8;
2401 0 n -= 8;
2402 }
2403 0 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 0 0 0
2404 0 k = 0;
2405 0 memcpy(&k, p, n);
2406 0 h = (h ^ k) * 0x9e3779b97f4a7c15ULL;
2407 0 h ^= h >> 29;
2408 }
2409 0 return lc_mix(h);
2410 }
2411  
2412 0 static void lc_dict_init(lc_dict_t *d, uint64_t nrows) {
2413 0 ▶ if (!lc_seed)
Branch: 1 branch
BranchTrueFalseTotal
if (!lc_seed) 0 0 0
2414 0 lc_seed = lc_mix((uint64_t)time(NULL) ^ ((uint64_t)getpid() << 32)
2415 0 ^ (uint64_t)(uintptr_t)&lc_seed) | 1;
2416 0 nbuf_init(&d->dict);
2417 0 d->kcap = 16;
2418 0 ▶ Newx(d->koff, d->kcap, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(d->koff, d->kcap, size_t); 0 0 0
2419 0 ▶ Newx(d->khash, d->kcap, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(d->khash, d->kcap, uint64_t); 0 0 0
2420 0 d->koff[0] = 0;
2421 0 d->nkeys = d->base = 0;
2422 0 d->distinct = 0;
2423 0 Newxz(d->slots, 16, uint32_t);
2424 0 d->mask = 15;
2425 0 ▶ d->icap = nrows ? nrows : 1;
Branch: 1 branch
BranchTrueFalseTotal
d->icap = nrows ? nrows : 1; 0 0 0
2426 0 ▶ Newx(d->idx, d->icap, uint32_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(d->idx, d->icap, uint32_t); 0 0 0
2427 0 d->nidx = 0;
2428 0 }
2429  
2430 0 static void lc_dict_free(lc_dict_t *d) {
2431 0 Safefree(d->dict.data);
2432 0 Safefree(d->koff);
2433 0 Safefree(d->khash);
2434 0 Safefree(d->slots);
2435 0 Safefree(d->idx);
2436 0 }
2437  
2438 0 static void lc_dict_push_key(lc_dict_t *d, const char *p, size_t n, uint64_t h) {
2439 0 ▶ if (d->nkeys + 1 >= d->kcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (d->nkeys + 1 >= d->kcap) { 0 0 0
2440 0 d->kcap *= 2;
2441 0 ▶ Renew(d->koff, d->kcap, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Renew(d->koff, d->kcap, size_t); 0 0 0
2442 0 ▶ Renew(d->khash, d->kcap, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Renew(d->khash, d->kcap, uint64_t); 0 0 0
2443 }
2444 0 nbuf_append(&d->dict, p, n);
2445 0 d->khash[d->nkeys] = h;
2446 0 d->koff[++d->nkeys] = d->dict.len;
2447 0 }
2448  
2449 0 static void lc_dict_grow(lc_dict_t *d) {
2450 0 size_t n = (d->mask + 1) * 2;
2451 uint64_t k;
2452 0 Safefree(d->slots);
2453 0 ▶ Newxz(d->slots, n, uint32_t);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(d->slots, n, uint32_t); 0 0 0
2454 0 d->mask = n - 1;
2455 0 ▶ for (k = d->base; k < d->nkeys; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = d->base; k < d->nkeys; k++) { 0 0 0
2456 0 size_t i = (size_t)d->khash[k] & d->mask;
2457 0 ▶ while (d->slots[i]) i = (i + 1) & d->mask;
Branch: 1 branch
BranchTrueFalseTotal
while (d->slots[i]) i = (i + 1) & d->mask; 0 0 0
2458 0 d->slots[i] = (uint32_t)k + 1;
2459 }
2460 0 }
2461  
2462 /* The key of the value p[0..n), added if new; 0 once keys run out of uint32 */
2463 0 static int lc_dict_add(lc_dict_t *d, const char *p, size_t n, uint32_t *key) {
2464 0 uint64_t h = lc_hash(p, n);
2465 0 size_t i = (size_t)h & d->mask;
2466 uint32_t s;
2467 0 ▶ while ((s = d->slots[i])) {
Branch: 1 branch
BranchTrueFalseTotal
while ((s = d->slots[i])) { 0 0 0
2468 0 uint64_t k = s - 1;
2469 0 ▶ if (d->khash[k] == h && d->koff[k + 1] - d->koff[k] == n
Branch: 2 branches
BranchTrueFalseTotal
if (d->khash[k] == h && d->koff[k + 1] - d->koff[k] == n 0 0 0
if (d->khash[k] == h && d->koff[k + 1] - d->koff[k] == n 0 0 0
2470 0 ▶ && memcmp(d->dict.data + d->koff[k], p, n) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(d->dict.data + d->koff[k], p, n) == 0) { 0 0 0
2471 0 *key = (uint32_t)k;
2472 0 return 1;
2473 }
2474 0 i = (i + 1) & d->mask;
2475 }
2476 0 ▶ if (d->nkeys >= 0xFFFFFFFEULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (d->nkeys >= 0xFFFFFFFEULL) return 0; 0 0 0
2477 0 d->slots[i] = (uint32_t)d->nkeys + 1;
2478 0 *key = (uint32_t)d->nkeys;
2479 0 lc_dict_push_key(d, p, n, h);
2480 0 ▶ if (2 * (d->nkeys - d->base) > d->mask + 1) lc_dict_grow(d);
Branch: 1 branch
BranchTrueFalseTotal
if (2 * (d->nkeys - d->base) > d->mask + 1) lc_dict_grow(d); 0 0 0
2481 0 return 1;
2482 }
2483  
2484 0 static void lc_dict_row(lc_dict_t *d, uint32_t key) {
2485 0 ▶ if (d->nidx == d->icap) {
Branch: 1 branch
BranchTrueFalseTotal
if (d->nidx == d->icap) { 0 0 0
2486 0 d->icap *= 2;
2487 0 ▶ Renew(d->idx, d->icap, uint32_t);
Branch: 1 branch
BranchTrueFalseTotal
Renew(d->idx, d->icap, uint32_t); 0 0 0
2488 }
2489 0 d->idx[d->nidx++] = key;
2490 0 }
2491  
2492 /* Length of the encoded value at buf[pos] of a dictionary type: fixed
2493 * size or a length-prefixed String; 0 if it runs past len or the type is
2494 * neither. */
2495 0 static size_t lc_value_len(const char *buf, size_t len, size_t pos, col_type_t *ct) {
2496 0 size_t fsz = col_type_fixed_size(ct), q = pos;
2497 0 uint64_t n = 0;
2498 0 int shift = 0;
2499 0 ▶ if (fsz) return fsz <= len - pos ? fsz : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (fsz) return fsz <= len - pos ? fsz : 0; 0 0 0
if (fsz) return fsz <= len - pos ? fsz : 0; 0 0 0
2500 0 ▶ if (ct->code != CT_STRING) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code != CT_STRING) return 0; 0 0 0
2501 0 ▶ while (q < len && shift < 64) {
Branch: 2 branches
BranchTrueFalseTotal
while (q < len && shift < 64) { 0 0 0
while (q < len && shift < 64) { 0 0 0
2502 0 unsigned char c = (unsigned char)buf[q++];
2503 0 n |= (uint64_t)(c & 0x7F) << shift;
2504 0 ▶ if (!(c & 0x80))
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) 0 0 0
2505 0 ▶ return n <= len - q ? (q - pos) + (size_t)n : 0;
Branch: 1 branch
BranchTrueFalseTotal
return n <= len - q ? (q - pos) + (size_t)n : 0; 0 0 0
2506 0 shift += 7;
2507 }
2508 0 return 0;
2509 }
2510  
2511 /* Add the nvals values ct's encoder wrote back to back into buf[0..len),
2512 * one row each; a set nulls[i] makes row i the NULL key 0. 0 if a value
2513 * cannot be delimited. */
2514 0 static int lc_dict_feed(lc_dict_t *d, const char *buf, size_t len, uint64_t nvals,
2515 col_type_t *ct, const uint8_t *nulls) {
2516 0 size_t pos = 0;
2517 uint64_t i;
2518 0 ▶ for (i = 0; i < nvals; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nvals; i++) { 0 0 0
2519 0 size_t n = lc_value_len(buf, len, pos, ct);
2520 0 uint32_t key = 0;
2521 0 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 0 0 0
2522 0 ▶ if (nulls && nulls[i])
Branch: 2 branches
BranchTrueFalseTotal
if (nulls && nulls[i]) 0 0 0
if (nulls && nulls[i]) 0 0 0
2523 ;
2524 0 ▶ else if (d->distinct) {
Branch: 1 branch
BranchTrueFalseTotal
else if (d->distinct) { 0 0 0
2525 /* a key per value, as many as the server would hash anyway */
2526 0 ▶ if (d->nkeys >= 0xFFFFFFFEULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (d->nkeys >= 0xFFFFFFFEULL) return 0; 0 0 0
2527 0 key = (uint32_t)d->nkeys++;
2528 0 nbuf_append(&d->dict, buf + pos, n);
2529 }
2530 0 ▶ else if (!lc_dict_add(d, buf + pos, n, &key))
Branch: 1 branch
BranchTrueFalseTotal
else if (!lc_dict_add(d, buf + pos, n, &key)) 0 0 0
2531 0 return 0;
2532 0 lc_dict_row(d, key);
2533 0 pos += n;
2534 0 ▶ if (!d->distinct && d->nidx % LC_SAMPLE_ROWS == 0
Branch: 2 branches
BranchTrueFalseTotal
if (!d->distinct && d->nidx % LC_SAMPLE_ROWS == 0 0 0 0
if (!d->distinct && d->nidx % LC_SAMPLE_ROWS == 0 0 0 0
2535 0 ▶ && d->nkeys - d->base >= LC_MIN_KEYS
Branch: 1 branch
BranchTrueFalseTotal
&& d->nkeys - d->base >= LC_MIN_KEYS 0 0 0
2536 0 ▶ && (d->nkeys - d->base) * 10 > d->nidx * LC_DISTINCT_TENTHS)
Branch: 1 branch
BranchTrueFalseTotal
&& (d->nkeys - d->base) * 10 > d->nidx * LC_DISTINCT_TENTHS) 0 0 0
2537 0 d->distinct = 1;
2538 }
2539 0 return pos == len;
2540 }
2541  
2542 /* The index type of nk keys: UInt8, UInt16 or UInt32, as 0, 1 or 2 */
2543 0 static int lc_key_type(uint64_t nk) {
2544 0 ▶ return nk <= 0x100 ? 0 : nk <= 0x10000 ? 1 : 2;
Branch: 2 branches
BranchTrueFalseTotal
return nk <= 0x100 ? 0 : nk <= 0x10000 ? 1 : 2; 0 0 0
return nk <= 0x100 ? 0 : nk <= 0x10000 ? 1 : 2; 0 0 0
2545 }
2546  
2547 /* serialization type, keys, rows' keys at the narrowest width that holds them */
2548 0 static void lc_dict_write(native_buf_t *b, const lc_dict_t *d) {
2549 0 uint64_t ser_type, nk = d->nkeys, ni = d->nidx, i;
2550 0 int key_type = lc_key_type(nk);
2551 0 size_t w = (size_t)1 << key_type;
2552 char *o;
2553 /* HasAdditionalKeys | NeedUpdateDictionary */
2554 0 ser_type = (uint64_t)key_type | (1ULL << 9) | (1ULL << 10);
2555 0 nbuf_append(b, (const char *)&ser_type, 8);
2556 0 nbuf_append(b, (const char *)&nk, 8);
2557 0 nbuf_append(b, d->dict.data, d->dict.len);
2558 0 nbuf_append(b, (const char *)&ni, 8);
2559 0 nbuf_grow(b, (size_t)ni * w);
2560 0 o = b->data + b->len;
2561 0 ▶ if (w == 1) for (i = 0; i < ni; i++) o[i] = (char)(uint8_t)d->idx[i];
Branch: 2 branches
BranchTrueFalseTotal
if (w == 1) for (i = 0; i < ni; i++) o[i] = (char)(uint8_t)d->idx[i]; 0 0 0
if (w == 1) for (i = 0; i < ni; i++) o[i] = (char)(uint8_t)d->idx[i]; 0 0 0
2562 0 ▶ else if (w == 2) for (i = 0; i < ni; i++) { uint16_t v = (uint16_t)d->idx[i]; memcpy(o + 2 * i, &v, 2); }
Branch: 2 branches
BranchTrueFalseTotal
else if (w == 2) for (i = 0; i < ni; i++) { uint16_t v = (uint16_t)d->idx[i]; memcpy(o + 2 * i, &v, 2); } 0 0 0
else if (w == 2) for (i = 0; i < ni; i++) { uint16_t v = (uint16_t)d->idx[i]; memcpy(o + 2 * i, &v, 2); } 0 0 0
2563 0 else memcpy(o, d->idx, (size_t)ni * 4);
2564 0 b->len += (size_t)ni * w;
2565 0 }
2566  
2567 /* A LowCardinality column of nrows rows from raw, its values as the
2568 * dictionary type's encoder wrote them: the rows, or with nulls the NULL
2569 * placeholder and then the rows. 0 if the values cannot be delimited or
2570 * run past 2^32 keys. */
2571 0 static int lc_encode(native_buf_t *b, const native_buf_t *raw, uint64_t nrows,
2572 col_type_t *dict_ct, const uint8_t *nulls) {
2573 lc_dict_t d;
2574 0 size_t pos = 0;
2575 int ok;
2576 0 lc_dict_init(&d, nrows);
2577 0 ▶ if (nulls && (pos = lc_value_len(raw->data, raw->len, 0, dict_ct))) {
Branch: 2 branches
BranchTrueFalseTotal
if (nulls && (pos = lc_value_len(raw->data, raw->len, 0, dict_ct))) { 0 0 0
if (nulls && (pos = lc_value_len(raw->data, raw->len, 0, dict_ct))) { 0 0 0
2578 0 lc_dict_push_key(&d, raw->data, pos, 0);
2579 0 d.base = 1;
2580 }
2581 0 ▶ ok = (!nulls || pos)
Branch: 1 branch
BranchTrueFalseTotal
ok = (!nulls || pos) 0 0 0
2582 0 ▶ && lc_dict_feed(&d, raw->data + pos, raw->len - pos, nrows, dict_ct, nulls);
Branch: 2 branches
BranchTrueFalseTotal
&& lc_dict_feed(&d, raw->data + pos, raw->len - pos, nrows, dict_ct, nulls); 0 0 0
&& lc_dict_feed(&d, raw->data + pos, raw->len - pos, nrows, dict_ct, nulls); 0 0 0
2583 0 ▶ if (ok) lc_dict_write(b, &d);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) lc_dict_write(b, &d); 0 0 0
2584 0 lc_dict_free(&d);
2585 0 return ok;
2586 }
2587  
2588 /* An Enum type's default value, as on the server: its smallest code. */
2589 0 static int enum_default_code(col_type_t *t) {
2590 0 enum_entry_t *entries = NULL;
2591 0 size_t i, n = enum_parse_entries(t->type_str, t->type_str_len, &entries);
2592 0 ▶ int code = n ? entries[0].code : 0;
Branch: 1 branch
BranchTrueFalseTotal
int code = n ? entries[0].code : 0; 0 0 0
2593 0 ▶ for (i = 1; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) 0 0 0
2594 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
2595 0 enum_free_entries(entries, n);
2596 0 return code;
2597 }
2598  
2599 /* Placeholder for a NULL of the given inner type. Must itself parse
2600 * under the strict encoders, or one NULL fails the batch. */
2601 0 static void nullable_inner_zero(col_type_t *t, const char **p, size_t *len) {
2602 static const char zero_str[] = "0";
2603 static const char empty_str[] = "";
2604 static const char zero_uuid[] = "00000000-0000-0000-0000-000000000000";
2605 static const char zero_ip4[] = "0.0.0.0";
2606 static const char zero_ip6[] = "::";
2607 static const char zero_time[] = "00:00:00";
2608 static char enum_buf[8];
2609 0 switch (t->code) {
2610 0 case CT_ENUM8:
2611 case CT_ENUM16:
2612 0 *len = (size_t)snprintf(enum_buf, sizeof(enum_buf), "%d", enum_default_code(t));
2613 0 *p = enum_buf;
2614 0 break;
2615 0 case CT_STRING:
2616 case CT_FIXEDSTRING:
2617 0 *p = empty_str; *len = 0; break;
2618 0 case CT_UUID:
2619 0 *p = zero_uuid; *len = sizeof(zero_uuid) - 1; break;
2620 0 case CT_IPV4:
2621 0 *p = zero_ip4; *len = sizeof(zero_ip4) - 1; break;
2622 0 case CT_IPV6:
2623 0 *p = zero_ip6; *len = sizeof(zero_ip6) - 1; break;
2624 0 case CT_TIME:
2625 case CT_TIME64:
2626 0 *p = zero_time; *len = sizeof(zero_time) - 1; break;
2627 0 default:
2628 0 *p = zero_str; *len = 1; break;
2629 }
2630 0 }
2631  
2632 0 static SV *nullable_inner_zero_sv(pTHX_ col_type_t *t) {
2633 const char *p;
2634 size_t len;
2635 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
2636 0 return newSViv(enum_default_code(t));
2637 0 nullable_inner_zero(t, &p, &len);
2638 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
2639 0 return newSVpvn("", 0);
2640 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
2641 0 return newSViv(0);
2642 0 return newSVpvn(p, len);
2643 }
2644  
2645 /* Parse "YYYY-MM-DD HH:MM:SS[.fff...]" into a DateTime64 scaled int64
2646 * (total_seconds * 10^precision + fractional). Sets *ok; on invalid input
2647 * returns 0 with *ok = 0. Pre-1970 dates work: the day count is a floor,
2648 * so the fraction always adds. */
2649 18 static int64_t parse_datetime64_to_scaled(const char *s, size_t len, int precision,
2650 col_type_t *ct, int *ok) {
2651 int32_t days;
2652 18 int64_t total, v, scale = 1;
2653 int sc;
2654 18 *ok = 0;
2655 18 ▶ if (!valid_date_prefix(s, len)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, len)) return 0; 0 18 18
2656 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
2657 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
2658 2 days = date_string_to_days(s, 10);
2659 2 total = (int64_t)days * 86400 + parse_hms(s, len);
2660 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
2661 2 int tok = 0;
2662 2 total = wall_in_tz_to_epoch(total, ct, &tok);
2663 2 ▶ if (!tok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) return 0; 0 2 2
2664 }
2665 8 ▶ for (sc = 0; sc < precision; sc++) scale *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (sc = 0; sc < precision; sc++) scale *= 10; 6 2 8
2666 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
2667 2 v = total * scale;
2668 /* Full consumption: a suffix past the seconds must be a fraction.
2669 * The server interprets designators like 'Z'/'+03:00' with TZ
2670 * conversion the client cannot replicate, so anything but digits
2671 * fails rather than silently writing a divergent instant. Extra
2672 * fraction digits truncate, matching the server. */
2673 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
2674 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
2675 0 const char *fp = s + 20;
2676 0 const char *fe = s + len;
2677 0 int64_t frac = 0;
2678 0 int digits = 0;
2679 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
2680 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
2681 0 frac = frac * 10 + (*fp - '0');
2682 0 fp++;
2683 0 digits++;
2684 }
2685 0 ▶ while (fp < fe) {
Branch: 1 branch
BranchTrueFalseTotal
while (fp < fe) { 0 0 0
2686 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
2687 0 fp++;
2688 }
2689 0 ▶ while (digits < precision) { frac *= 10; digits++; }
Branch: 1 branch
BranchTrueFalseTotal
while (digits < precision) { frac *= 10; digits++; } 0 0 0
2690 /* total*scale was bounded; the fraction can still push v over */
2691 0 ▶ if (v > INT64_MAX - frac) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > INT64_MAX - frac) return 0; 0 0 0
2692 0 v += frac;
2693 }
2694 2 *ok = 1;
2695 2 return v;
2696 }
2697  
2698 /* Expand an exponent-form decimal ("[+-]mantissa[eE][+-]digits") into
2699 * plain text for the existing decimal parsers. Returns 1 with *out_len
2700 * set, 0 when there is no exponent marker (caller uses the input as is),
2701 * -1 when invalid. The point shifts textually (exact, no float maths);
2702 * int_digits past precision-scale fail, huge positive exponents fail
2703 * fast and huge negative ones collapse to zero, without big buffers. */
2704 0 static int decimal_expand_exp(const char *p, size_t len, int scale,
2705 int precision, char *out, size_t out_sz,
2706 size_t *out_len) {
2707 size_t k, mant_len, exp_len, i, j;
2708 const char *mant, *exp_p, *ip, *fp;
2709 size_t ilen, flen;
2710 0 int neg = 0, exp_neg = 0;
2711 0 long exp = 0, int_after, limit;
2712 const char *sig;
2713 size_t sig_len, use, k3, need;
2714 char *w;
2715 0 ▶ for (k = 0; k < len; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < len; k++) 0 0 0
2716 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
2717 0 ▶ if (k == len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (k == len) return 0; 0 0 0
2718 0 mant = p;
2719 0 mant_len = k;
2720 0 exp_p = p + k + 1;
2721 0 exp_len = len - mant_len - 1;
2722 0 ▶ if (mant_len == 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (mant_len == 0) return -1; 0 0 0
2723 0 i = 0;
2724 0 ▶ if (mant[0] == '-') { neg = 1; i = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (mant[0] == '-') { neg = 1; i = 1; } 0 0 0
2725 0 ▶ else if (mant[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (mant[0] == '+') i = 1; 0 0 0
2726 0 ▶ if (i >= mant_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= mant_len) return -1; 0 0 0
2727 0 j = i;
2728 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
2729 0 ip = mant + i;
2730 0 ilen = j - i;
2731 0 fp = mant + j;
2732 0 flen = 0;
2733 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
2734 size_t fstart;
2735 0 j++;
2736 0 fstart = j;
2737 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
2738 0 fp = mant + fstart;
2739 0 flen = j - fstart;
2740 }
2741 0 ▶ if (j != mant_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (j != mant_len) return -1; 0 0 0
2742 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
2743 0 ▶ if (exp_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (exp_len == 0) { 0 0 0
2744 0 ▶ if (mant_len >= out_sz) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (mant_len >= out_sz) return -1; 0 0 0
2745 0 memcpy(out, mant, mant_len);
2746 0 *out_len = mant_len;
2747 0 return 1;
2748 }
2749 0 i = 0;
2750 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
2751 0 ▶ else if (exp_p[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (exp_p[0] == '+') i = 1; 0 0 0
2752 0 ▶ if (i >= exp_len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= exp_len) return -1; 0 0 0
2753 0 ▶ for (; i < exp_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < exp_len; i++) { 0 0 0
2754 int d;
2755 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
2756 0 d = exp_p[i] - '0';
2757 0 ▶ if (exp < 2000000L)
Branch: 1 branch
BranchTrueFalseTotal
if (exp < 2000000L) 0 0 0
2758 0 exp = exp * 10 + d;
2759 }
2760 0 ▶ if (exp_neg) exp = -exp;
Branch: 1 branch
BranchTrueFalseTotal
if (exp_neg) exp = -exp; 0 0 0
2761 /* Significant digits live in two spans (int, frac) around the point;
2762 * gather the needed prefix into one buffer. int_after <= limit and
2763 * scale bound every copy below, so no big buffers. */
2764 {
2765 char dig[256];
2766 0 size_t first, total = ilen + flen, want = 0, got = 0, k4;
2767 0 size_t z = 0;
2768 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
2769 0 ▶ if (z == ilen) {
Branch: 1 branch
BranchTrueFalseTotal
if (z == ilen) { 0 0 0
2770 0 size_t z2 = 0;
2771 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
2772 0 first = ilen + z2;
2773 } else {
2774 0 first = z;
2775 }
2776 0 ▶ sig_len = (first >= total) ? 0 : total - first;
Branch: 1 branch
BranchTrueFalseTotal
sig_len = (first >= total) ? 0 : total - first; 0 0 0
2777 0 sig = dig;
2778 0 limit = (long)precision - (long)scale;
2779 0 int_after = (long)sig_len + exp - (long)flen;
2780 0 ▶ if (int_after > limit) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (int_after > limit) return -1; 0 0 0
2781 0 ▶ if (sig_len == 0) goto zero;
Branch: 1 branch
BranchTrueFalseTotal
if (sig_len == 0) goto zero; 0 0 0
2782 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
2783 0 ▶ if (int_after <= 0) want = (size_t)scale;
Branch: 1 branch
BranchTrueFalseTotal
if (int_after <= 0) want = (size_t)scale; 0 0 0
2784 0 else want = (size_t)int_after + (size_t)scale;
2785 0 ▶ if (want > sizeof(dig)) want = sizeof(dig);
Branch: 1 branch
BranchTrueFalseTotal
if (want > sizeof(dig)) want = sizeof(dig); 0 0 0
2786 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
2787 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
2788 0 sig_len = got;
2789 0 w = out;
2790 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2791 0 ▶ if (out_sz < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (out_sz < 2) return -1; 0 0 0
2792 0 *w++ = '-';
2793 }
2794 0 ▶ if (int_after <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (int_after <= 0) { 0 0 0
2795 0 long lead = -int_after;
2796 0 use = sig_len;
2797 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
2798 0 need = 2 + (size_t)lead + use;
2799 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
2800 0 *w++ = '0';
2801 0 *w++ = '.';
2802 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
2803 0 memcpy(w, sig, use);
2804 0 w += use;
2805 0 ▶ } else if (sig_len >= (size_t)int_after) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sig_len >= (size_t)int_after) { 0 0 0
2806 0 use = sig_len - (size_t)int_after;
2807 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
2808 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
2809 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
2810 0 memcpy(w, sig, (size_t)int_after);
2811 0 w += int_after;
2812 0 ▶ if (use) {
Branch: 1 branch
BranchTrueFalseTotal
if (use) { 0 0 0
2813 0 *w++ = '.';
2814 0 memcpy(w, sig + int_after, use);
2815 0 w += use;
2816 }
2817 } else {
2818 0 size_t pad = (size_t)int_after - sig_len;
2819 0 need = sig_len + pad;
2820 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
2821 0 memcpy(w, sig, sig_len);
2822 0 w += sig_len;
2823 0 ▶ for (k3 = 0; k3 < pad; k3++) *w++ = '0';
Branch: 1 branch
BranchTrueFalseTotal
for (k3 = 0; k3 < pad; k3++) *w++ = '0'; 0 0 0
2824 }
2825 0 *out_len = (size_t)(w - out);
2826 0 return 1;
2827 0 zero:
2828 0 w = out;
2829 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
2830 0 ▶ if (out_sz < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (out_sz < 2) return -1; 0 0 0
2831 0 *w++ = '-';
2832 }
2833 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
2834 0 *w++ = '0';
2835 0 *out_len = (size_t)(w - out);
2836 0 return 1;
2837 }
2838 }
2839  
2840 /* Parse "[+-]?digits[.digits]" into a scaled int64 raw value. `scale`
2841 * is the target Decimal's fractional digits; `precision` its total
2842 * digits. Mirrors the server's TSV verdicts (probed): full consumption
2843 * (leading '+' ok, no whitespace, trailing garbage fails), extra
2844 * fraction digits truncated, significant integer digits beyond
2845 * precision-scale fail. Sets *ok; the digit bound keeps every
2846 * intermediate below 10^18, so no accumulation can overflow. */
2847 0 static int64_t parse_decimal_to_raw(const char *p, size_t len, int scale,
2848 int precision, int *ok) {
2849 const char *e, *dstart, *sig;
2850 0 int neg = 0, int_digits = 0, frac_digits = 0, seen = 0, s;
2851 0 uint64_t integer_part = 0, frac_part = 0, raw;
2852 char exp_buf[256];
2853 0 size_t exp_len = 0, ek;
2854 0 int has_e = 0;
2855 0 *ok = 0;
2856 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
2857 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2858 0 ▶ for (ek = 0; ek < len; ek++)
Branch: 1 branch
BranchTrueFalseTotal
for (ek = 0; ek < len; ek++) 0 0 0
2859 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
2860 0 ▶ if (has_e) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_e) { 0 0 0
2861 0 ▶ if (decimal_expand_exp(p, len, scale, precision,
Branch: 1 branch
BranchTrueFalseTotal
if (decimal_expand_exp(p, len, scale, precision, 0 0 0
2862 exp_buf, sizeof(exp_buf), &exp_len) != 1)
2863 0 return 0;
2864 0 p = exp_buf;
2865 0 len = exp_len;
2866 }
2867 0 e = p + len;
2868 0 ▶ if (*p == '-') { neg = 1; p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { neg = 1; p++; } 0 0 0
2869 0 ▶ else if (*p == '+') p++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') p++; 0 0 0
2870 0 dstart = p;
2871 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
2872 0 ▶ if (p > dstart) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p > dstart) seen = 1; 0 0 0
2873 0 sig = dstart;
2874 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
2875 0 int_digits = (int)(p - sig);
2876 0 ▶ if (p < e && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < e && *p == '.') { 0 0 0
if (p < e && *p == '.') { 0 0 0
2877 0 p++;
2878 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
2879 0 frac_part = frac_part * 10 + (uint64_t)(*p - '0');
2880 0 p++;
2881 0 frac_digits++;
2882 }
2883 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
2884 0 ▶ if (frac_digits > 0) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frac_digits > 0) seen = 1; 0 0 0
2885 }
2886 0 ▶ if (p != e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p != e) return 0; 0 0 0
2887 0 ▶ if (!seen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) return 0; 0 0 0
2888 0 ▶ if (int_digits > precision - scale) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 0; 0 0 0
2889 /* Significant span only: leading zeros never reach the accumulator,
2890 * and int_digits <= precision - scale keeps the scaled value < 10^18. */
2891 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
2892 0 integer_part = integer_part * 10 + (uint64_t)(*p - '0');
2893 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
2894 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
2895 0 raw = integer_part + frac_part;
2896 0 *ok = 1;
2897 0 ▶ return neg ? -(int64_t)raw : (int64_t)raw;
Branch: 1 branch
BranchTrueFalseTotal
return neg ? -(int64_t)raw : (int64_t)raw; 0 0 0
2898 }
2899  
2900 /* limbs = limbs * 10 + digit over nlimbs LE u64 limbs. Returns 0 on
2901 * overflow. Portable 32-bit-half arithmetic (no __int128 needed). */
2902 0 static int limbs_mul10add(uint64_t *limbs, int nlimbs, unsigned digit) {
2903 0 uint64_t carry = digit;
2904 int i;
2905 0 ▶ for (i = 0; i < nlimbs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlimbs; i++) { 0 0 0
2906 0 uint64_t t = limbs[i];
2907 0 uint64_t p1 = (t & 0xffffffffULL) * 10 + carry;
2908 0 uint64_t p2 = (t >> 32) * 10 + (p1 >> 32);
2909 0 limbs[i] = ((p2 & 0xffffffffULL) << 32) | (p1 & 0xffffffffULL);
2910 0 carry = p2 >> 32;
2911 }
2912 0 return carry == 0;
2913 }
2914  
2915 /* Two's-complement negate over nlimbs LE u64 limbs. */
2916 0 static void limbs_negate(uint64_t *limbs, int nlimbs) {
2917 0 uint64_t carry = 1;
2918 int i;
2919 0 ▶ for (i = 0; i < nlimbs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlimbs; i++) { 0 0 0
2920 0 uint64_t v = ~limbs[i] + carry;
2921 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
2922 0 limbs[i] = v;
2923 }
2924 0 }
2925  
2926 /* Parse decimal text into a scaled raw value over nlimbs LE u64 limbs.
2927 * Same shape as parse_decimal_to_raw: full consumption, truncation of
2928 * extra fraction digits, int_digits <= precision - scale. Values above
2929 * the signed max fail (the server errors on those too). */
2930 0 static int parse_decimal_limbs(const char *p, size_t len, int scale,
2931 int precision, int max_scale, int max_prec,
2932 uint64_t *limbs, int nlimbs) {
2933 const char *e, *dstart, *sig;
2934 0 int neg = 0, int_digits = 0, frac_digits = 0, seen = 0, s, i;
2935 char exp_buf[256];
2936 0 size_t exp_len = 0, ek;
2937 0 int has_e = 0;
2938 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
2939 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
2940 0 return 0;
2941 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
2942 0 ▶ for (ek = 0; ek < len; ek++)
Branch: 1 branch
BranchTrueFalseTotal
for (ek = 0; ek < len; ek++) 0 0 0
2943 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
2944 0 ▶ if (has_e) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_e) { 0 0 0
2945 0 ▶ if (decimal_expand_exp(p, len, scale, precision,
Branch: 1 branch
BranchTrueFalseTotal
if (decimal_expand_exp(p, len, scale, precision, 0 0 0
2946 exp_buf, sizeof(exp_buf), &exp_len) != 1)
2947 0 return 0;
2948 0 p = exp_buf;
2949 0 len = exp_len;
2950 }
2951 0 e = p + len;
2952 0 ▶ if (*p == '-') { neg = 1; p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { neg = 1; p++; } 0 0 0
2953 0 ▶ else if (*p == '+') p++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') p++; 0 0 0
2954 0 dstart = p;
2955 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
2956 0 ▶ if (p > dstart) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p > dstart) seen = 1; 0 0 0
2957 0 sig = dstart;
2958 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
2959 0 int_digits = (int)(p - sig);
2960 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
2961 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
2962 0 ▶ if (p < e && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < e && *p == '.') { 0 0 0
if (p < e && *p == '.') { 0 0 0
2963 0 p++;
2964 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
2965 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
2966 0 p++;
2967 0 frac_digits++;
2968 }
2969 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
2970 0 ▶ if (frac_digits > 0) seen = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frac_digits > 0) seen = 1; 0 0 0
2971 }
2972 0 ▶ if (p != e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p != e) return 0; 0 0 0
2973 0 ▶ if (!seen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) return 0; 0 0 0
2974 0 ▶ if (int_digits > precision - scale) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 0; 0 0 0
2975 0 ▶ for (s = frac_digits; s < scale; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = frac_digits; s < scale; s++) 0 0 0
2976 0 ▶ if (!limbs_mul10add(limbs, nlimbs, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!limbs_mul10add(limbs, nlimbs, 0)) return 0; 0 0 0
2977 0 ▶ if (limbs[nlimbs - 1] & 0x8000000000000000ULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] & 0x8000000000000000ULL) { 0 0 0
2978 /* magnitude >= 2^(64n-1): valid only as exactly -MIN */
2979 0 int k, is_min = neg;
2980 0 ▶ for (k = 0; k < nlimbs - 1; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nlimbs - 1; k++) 0 0 0
2981 0 ▶ if (limbs[k]) { is_min = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[k]) { is_min = 0; break; } 0 0 0
2982 0 ▶ if (limbs[nlimbs - 1] != 0x8000000000000000ULL) is_min = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] != 0x8000000000000000ULL) is_min = 0; 0 0 0
2983 0 ▶ if (!is_min) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_min) return 0; 0 0 0
2984 }
2985 0 ▶ if (neg) limbs_negate(limbs, nlimbs);
Branch: 1 branch
BranchTrueFalseTotal
if (neg) limbs_negate(limbs, nlimbs); 0 0 0
2986 0 return 1;
2987 }
2988  
2989 0 static int parse_decimal256_to_raw(const char *p, size_t len, int scale,
2990 int precision, unsigned char out[32]) {
2991 uint64_t limbs[4];
2992 0 memset(out, 0, 32);
2993 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
2994 0 return 0;
2995 0 memcpy(out, limbs, 32);
2996 0 return 1;
2997 }
2998  
2999 0 static int parse_decimal128_to_raw(const char *p, size_t len, int scale,
3000 int precision, unsigned char out[16]) {
3001 uint64_t limbs[2];
3002 0 memset(out, 0, 16);
3003 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
3004 0 return 0;
3005 0 memcpy(out, limbs, 16);
3006 0 return 1;
3007 }
3008  
3009 /* Parse decimal integer text into nlimbs LE u64 limbs. signed=0 rejects
3010 * '-'; either way the magnitude must fit (signed: sign bit clear for
3011 * positive, representable INT_MIN for negative). */
3012 0 static int parse_int_limbs(const char *p, size_t len, uint64_t *limbs,
3013 int nlimbs, int is_signed) {
3014 size_t i;
3015 0 int neg = 0, k;
3016 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3017 0 ▶ if (*p == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') { 0 0 0
3018 0 ▶ if (!is_signed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_signed) return 0; 0 0 0
3019 0 neg = 1; p++; len--;
3020 }
3021 0 ▶ else if (*p == '+') { p++; len--; }
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '+') { p++; len--; } 0 0 0
3022 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3023 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
3024 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
3025 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
3026 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
3027 }
3028 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
3029 0 ▶ for (k = 0; k < nlimbs; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nlimbs; k++) 0 0 0
3030 0 ▶ if (limbs[k]) break;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[k]) break; 0 0 0
3031 0 ▶ if (k == nlimbs) return 1; /* -0 */
Branch: 1 branch
BranchTrueFalseTotal
if (k == nlimbs) return 1; /* -0 */ 0 0 0
3032 0 limbs_negate(limbs, nlimbs);
3033 0 ▶ if (!(limbs[nlimbs - 1] & 0x8000000000000000ULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(limbs[nlimbs - 1] & 0x8000000000000000ULL)) return 0; 0 0 0
3034 0 ▶ } else if (is_signed) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_signed) { 0 0 0
3035 0 ▶ if (limbs[nlimbs - 1] & 0x8000000000000000ULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[nlimbs - 1] & 0x8000000000000000ULL) return 0; 0 0 0
3036 }
3037 0 return 1;
3038 }
3039  
3040 /* Parse a UUID string into 16 LE bytes (two LE UInt64 halves of the
3041 * 128-bit value). Returns 1 on success. Accepts exactly what the server
3042 * accepts: 32 hex digits, or 36 chars with dashes at 8/13/18/23. */
3043 0 static int parse_uuid_to_le_bytes(const char *s, size_t slen, unsigned char ubytes[16]) {
3044 0 unsigned char raw[16] = {0};
3045 0 int k = 0, j;
3046 0 memset(ubytes, 0, 16);
3047 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
3048 0 ▶ for (j = 0; j < (int)slen; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < (int)slen; j++) { 0 0 0
3049 0 char c = s[j];
3050 unsigned char nibble;
3051 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
3052 0 ▶ if (c != '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c != '-') return 0; 0 0 0
3053 0 continue;
3054 }
3055 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
3056 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
3057 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
3058 0 else return 0;
3059 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
3060 0 else raw[k/2] |= nibble;
3061 0 k++;
3062 }
3063 0 ▶ if (k != 32) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (k != 32) return 0; 0 0 0
3064 /* Reverse each 8-byte half for LE storage */
3065 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
3066 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
3067 0 return 1;
3068 }
3069  
3070 /* Parse '[−]H+:MM:SS[.frac]' or '[−]MM:SS[.frac]' to seconds (Time)
3071 * or scaled units (Time64). Mirrors the server's TSV grammar (lax
3072 * ranges, fraction truncated to scale, rejected when scale is 0).
3073 * Returns the value with *ok = 1, or 0 with *ok = 0. */
3074 0 static int64_t parse_time_text(const char *s, size_t len, int scale, int *ok) {
3075 0 uint64_t parts[3] = {0, 0, 0};
3076 0 int nparts = 0, neg = 0, frac_digits = 0, sc;
3077 uint64_t mag, tail;
3078 0 size_t i = 0, j;
3079 0 *ok = 0;
3080 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3081 0 ▶ if (s[0] == '-') { neg = 1; i = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == '-') { neg = 1; i = 1; } 0 0 0
3082 0 ▶ else if (s[0] == '+') i = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (s[0] == '+') i = 1; 0 0 0
3083 0 for (;;) {
3084 0 uint64_t v = 0;
3085 0 size_t start = i, ndig;
3086 0 ▶ if (nparts == 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nparts == 3) return 0; 0 0 0
3087 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
3088 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
3089 0 v = v * 10 + (uint64_t)(s[i] - '0');
3090 0 i++;
3091 }
3092 0 ndig = i - start;
3093 0 ▶ if (ndig == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ndig == 0) return 0; 0 0 0
3094 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
3095 0 parts[nparts++] = v;
3096 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
3097 0 break;
3098 }
3099 0 ▶ if (nparts < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nparts < 2) return 0; 0 0 0
3100 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
3101 0 ▶ if (parts[0] > (UINT64_MAX / 3600)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (parts[0] > (UINT64_MAX / 3600)) return 0; 0 0 0
3102 0 mag = parts[0] * 3600;
3103 0 tail = parts[1] * 60 + parts[2];
3104 0 ▶ if (mag > UINT64_MAX - tail) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > UINT64_MAX - tail) return 0; 0 0 0
3105 0 mag += tail;
3106 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
3107 0 ▶ if (scale <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (scale <= 0) return 0; 0 0 0
3108 0 i++;
3109 0 j = i;
3110 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
3111 0 ▶ if (j == i) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j == i) return 0; 0 0 0
3112 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
3113 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
3114 0 mag = mag * 10 + (uint64_t)(s[i] - '0');
3115 }
3116 0 i = j;
3117 }
3118 0 ▶ if (i != len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i != len) return 0; 0 0 0
3119 0 ▶ for (sc = frac_digits; sc < scale; sc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sc = frac_digits; sc < scale; sc++) { 0 0 0
3120 0 ▶ if (mag > UINT64_MAX / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > UINT64_MAX / 10) return 0; 0 0 0
3121 0 mag *= 10;
3122 }
3123 0 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 0 0 0
3124 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
3125 0 *ok = 1;
3126 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
3127 }
3128 0 ▶ if (mag > (uint64_t)INT64_MAX) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > (uint64_t)INT64_MAX) return 0; 0 0 0
3129 0 *ok = 1;
3130 0 return (int64_t)mag;
3131 }
3132  
3133 /* TSV unescape: \\ → \, \n → newline, \t → tab, \0 → null byte */
3134 0 static size_t tsv_unescape(const char *src, size_t src_len, char *dst) {
3135 0 size_t i, j = 0;
3136 0 ▶ for (i = 0; i < src_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < src_len; i++) { 0 0 0
3137 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
3138 0 switch (src[i+1]) {
3139 0 case '\\': dst[j++] = '\\'; i++; break;
3140 0 case 'n': dst[j++] = '\n'; i++; break;
3141 0 case 't': dst[j++] = '\t'; i++; break;
3142 0 case '0': dst[j++] = '\0'; i++; break;
3143 0 case '\'': dst[j++] = '\''; i++; break;
3144 0 case 'b': dst[j++] = '\b'; i++; break;
3145 0 case 'r': dst[j++] = '\r'; i++; break;
3146 0 case 'a': dst[j++] = '\a'; i++; break;
3147 0 case 'f': dst[j++] = '\f'; i++; break;
3148 0 default: dst[j++] = src[i]; break;
3149 }
3150 } else {
3151 0 dst[j++] = src[i];
3152 }
3153 }
3154 0 return j;
3155 }
3156  
3157 0 static int is_tsv_null(const char *s, size_t len) {
3158 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
3159 }
3160  
3161 /* TSV escape: inverse of tsv_unescape — appends escaped bytes to buffer */
3162 0 static void tsv_escape(native_buf_t *b, const char *s, size_t len) {
3163 0 size_t i, start = 0;
3164 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
3165 0 char esc = 0;
3166 0 switch (s[i]) {
3167 0 case '\\': esc = '\\'; break;
3168 0 case '\t': esc = 't'; break;
3169 0 case '\n': esc = 'n'; break;
3170 0 case '\0': esc = '0'; break;
3171 0 case '\b': esc = 'b'; break;
3172 0 case '\r': esc = 'r'; break;
3173 0 case '\a': esc = 'a'; break;
3174 0 case '\f': esc = 'f'; break;
3175 }
3176 0 ▶ if (esc) {
Branch: 1 branch
BranchTrueFalseTotal
if (esc) { 0 0 0
3177 0 ▶ if (i > start)
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) 0 0 0
3178 0 nbuf_append(b, s + start, i - start);
3179 0 nbuf_grow(b, 2);
3180 0 b->data[b->len++] = '\\';
3181 0 b->data[b->len++] = esc;
3182 0 start = i + 1;
3183 }
3184 }
3185 0 ▶ if (start < len)
Branch: 1 branch
BranchTrueFalseTotal
if (start < len) 0 0 0
3186 0 nbuf_append(b, s + start, len - start);
3187 0 }
3188  
3189 /* Snapshot of arrayref INSERT data. Native INSERT encodes rows later (at
3190 * sample-block time), so holding the caller's AV would let a post-call
3191 * clear/push silently change the batch: keep the cells, shared by
3192 * reference, row-major in one array, projected through columns_at. */
3193 typedef struct {
3194 SSize_t nrows;
3195 SSize_t bad_row; /* first row that is not an arrayref, -1 if none */
3196 SSize_t width; /* columns_at entries, -1 without projection */
3197 SSize_t ncells, cap;
3198 SSize_t *off; /* row r owns cells[off[r] .. off[r + 1]) */
3199 SV **cells; /* NULL = missing cell */
3200 } insert_snap_t;
3201  
3202 16 static int insert_snap_free(pTHX_ SV *sv, MAGIC *mg) {
3203 16 insert_snap_t *sn = (insert_snap_t *)mg->mg_ptr;
3204 SSize_t i;
3205 PERL_UNUSED_ARG(sv);
3206 32 ▶ for (i = 0; i < sn->ncells; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sn->ncells; i++) 16 16 32
3207 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
3208 16 Safefree(sn->cells);
3209 16 Safefree(sn->off);
3210 16 Safefree(sn);
3211 16 return 0;
3212 }
3213  
3214 static MGVTBL insert_snap_vtbl = {
3215 NULL, NULL, NULL, NULL, insert_snap_free, NULL, NULL, NULL
3216 };
3217  
3218 12 static insert_snap_t *insert_snap_get(pTHX_ SV *sv) {
3219 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
3220 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
3221 }
3222  
3223 16 static void insert_snap_push(insert_snap_t *sn, SV *cell) {
3224 16 ▶ if (sn->ncells == sn->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (sn->ncells == sn->cap) { 16 0 16
3225 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
3226 0 croak("insert data too large");
3227 16 ▶ sn->cap = sn->cap ? sn->cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
sn->cap = sn->cap ? sn->cap * 2 : 64; 0 16 16
3228 16 ▶ Renew(sn->cells, sn->cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(sn->cells, sn->cap, SV *); 0 16 16
3229 }
3230 16 sn->cells[sn->ncells++] = cell;
3231 16 }
3232  
3233 /* The columns_at insert option as positions in a mortal buffer, NULL
3234 * when absent; croaks unless it is a non-empty array of non-negative
3235 * integers. */
3236 18 static SSize_t *insert_columns_at(pTHX_ HV *settings, SSize_t *count) {
3237 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
3238 AV *av;
3239 SSize_t n, j, *pos;
3240 18 *count = 0;
3241 18 ▶ if (!svp) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) return NULL; 18 0 18
3242 0 SvGETMAGIC(*svp);
3243 0 ▶ if (!SvOK(*svp)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(*svp)) return NULL; 0 0 0
3244 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
3245 0 ▶ || (n = av_len((AV *)SvRV(*svp)) + 1) < 1)
Branch: 1 branch
BranchTrueFalseTotal
|| (n = av_len((AV *)SvRV(*svp)) + 1) < 1) 0 0 0
3246 0 croak("columns_at must be a non-empty array of non-negative integers");
3247 0 av = (AV *)SvRV(*svp);
3248 0 pos = (SSize_t *)SvPVX(sv_2mortal(newSV(n * sizeof(SSize_t))));
3249 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
3250 0 SV **e = av_fetch(av, j, 0);
3251 0 ▶ SV *v = e ? *e : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = e ? *e : &PL_sv_undef; 0 0 0
3252 NV nv;
3253 0 SvGETMAGIC(v);
3254 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
3255 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
3256 0 ▶ || nv != (NV)(SSize_t)nv)
Branch: 1 branch
BranchTrueFalseTotal
|| nv != (NV)(SSize_t)nv) 0 0 0
3257 0 croak("columns_at must be a non-empty array of non-negative integers");
3258 0 pos[j] = (SSize_t)nv;
3259 }
3260 0 *count = n;
3261 0 return pos;
3262 }
3263  
3264 /* A plain row's cell is shared by reference; a get-magical one (a tied
3265 * scalar) is FETCHed once now and copied, as in a tied row. */
3266 16 static SV *snap_cell(pTHX_ SV *cell) {
3267 16 ▶ if (!cell) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cell) return NULL; 0 16 16
3268 16 ▶ return SvGMAGICAL(cell) ? newSVsv(cell) : SvREFCNT_inc(cell);
Branch: 1 branch
BranchTrueFalseTotal
return SvGMAGICAL(cell) ? newSVsv(cell) : SvREFCNT_inc(cell); 0 16 16
3269 }
3270  
3271 16 static SV *snapshot_insert_av(pTHX_ AV *src, const SSize_t *proj, SSize_t nproj) {
3272 16 SSize_t n = av_len(src) + 1;
3273 SSize_t i;
3274 insert_snap_t *sn;
3275 /* av_fetch on a tied AV returns a lazy mortal FETCH proxy, not the
3276 * value. Materialize tied rows/cells now (src is alive) and copy
3277 * the values so the snapshot never dangles if the caller frees
3278 * the tied container before the deferred encode. */
3279 16 int src_magical = SvRMAGICAL((SV *)src);
3280 /* Mortal during the build: a dying FETCH longjmps out of insert(),
3281 * and the partial copy must die with the scope instead of leaking. */
3282 16 SV *holder = sv_2mortal(newSV(0));
3283  
3284 16 Newxz(sn, 1, insert_snap_t);
3285 16 sn->bad_row = -1;
3286 16 ▶ sn->width = proj ? nproj : -1;
Branch: 1 branch
BranchTrueFalseTotal
sn->width = proj ? nproj : -1; 0 16 16
3287 16 sv_magicext(holder, NULL, PERL_MAGIC_ext, &insert_snap_vtbl, (char *)sn, 0);
3288 16 ▶ Newx(sn->off, n + 1, SSize_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(sn->off, n + 1, SSize_t); 0 16 16
3289 16 sn->off[0] = 0;
3290 16 ▶ if (proj && n > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (proj && n > 0) { 0 16 16
if (proj && n > 0) { 0 0 0
3291 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
3292 0 croak("insert data too large");
3293 0 sn->cap = n * nproj;
3294 0 ▶ Newx(sn->cells, sn->cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(sn->cells, sn->cap, SV *); 0 0 0
3295 }
3296 32 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 16 16 32
3297 16 SV **e = av_fetch(src, i, 0);
3298 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
3299 16 AV *rsrc = NULL;
3300 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
3301 0 ▶ if (SvGMAGICAL(row)) mg_get(row);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(row)) mg_get(row); 0 0 0
3302 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
3303 0 rsrc = (AV *)SvRV(row);
3304 /* Own the row AV through the copy loop below; the
3305 * FETCH proxy borrows it. Mortal: scope frees it. */
3306 0 (void)sv_2mortal(newRV_inc((SV *)rsrc));
3307 }
3308 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
3309 16 rsrc = (AV *)SvRV(row);
3310 }
3311 16 ▶ if (!rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rsrc) { 0 16 16
3312 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
3313 16 ▶ } else if (SvRMAGICAL((SV *)rsrc)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvRMAGICAL((SV *)rsrc)) { 0 16 16
3314 0 SSize_t len = av_len(rsrc) + 1;
3315 0 ▶ SSize_t j, rn = proj ? nproj : len;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, rn = proj ? nproj : len; 0 0 0
3316 0 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 0 0 0
3317 0 ▶ SSize_t at = proj ? proj[j] : j;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t at = proj ? proj[j] : j; 0 0 0
3318 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
3319 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
3320 }
3321 } else {
3322 16 SSize_t len = AvFILLp(rsrc) + 1;
3323 16 SV **arr = AvARRAY(rsrc);
3324 SSize_t j;
3325 16 ▶ if (proj) {
Branch: 1 branch
BranchTrueFalseTotal
if (proj) { 0 16 16
3326 0 ▶ for (j = 0; j < nproj; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nproj; j++) 0 0 0
3327 0 insert_snap_push(sn, snap_cell(aTHX_
3328 0 ▶ proj[j] < len ? arr[proj[j]] : NULL));
Branch: 1 branch
BranchTrueFalseTotal
proj[j] < len ? arr[proj[j]] : NULL)); 0 0 0
3329 } else {
3330 32 ▶ for (j = 0; j < len; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < len; j++) 16 16 32
3331 16 insert_snap_push(sn, snap_cell(aTHX_ arr[j]));
3332 }
3333 }
3334 16 sn->off[i + 1] = sn->ncells;
3335 }
3336 16 sn->nrows = n;
3337 /* Un-mortalize: the caller owns the holder from here on. */
3338 16 return SvREFCNT_inc(holder);
3339 }
3340  
3341 /* Serialize an AV of AVs to TabSeparated format for HTTP INSERT, each
3342 * row projected through proj when given. Returns malloc'd buffer;
3343 * caller must Safefree(). */
3344  
3345 0 static char* serialize_av_to_tsv(pTHX_ AV *rows, const SSize_t *proj,
3346 SSize_t nproj, size_t *out_len) {
3347 native_buf_t b;
3348 0 SSize_t nrows = av_len(rows) + 1;
3349 SSize_t r;
3350  
3351 0 nbuf_init(&b);
3352  
3353 0 ▶ for (r = 0; r < nrows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < nrows; r++) { 0 0 0
3354 0 SV **row_svp = av_fetch(rows, r, 0);
3355 AV *row;
3356 SSize_t ncols, c, last;
3357  
3358 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
3359 0 mg_get(*row_svp);
3360 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
3361 0 ▶ SvTYPE(SvRV(*row_svp)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(*row_svp)) != SVt_PVAV) { 0 0 0
3362 0 Safefree(b.data);
3363 0 croak("insert data: row %" IVdf " is not an ARRAY ref", (IV)r);
3364 }
3365 0 row = (AV *)SvRV(*row_svp);
3366 0 last = av_len(row);
3367 0 ▶ ncols = proj ? nproj : last + 1;
Branch: 1 branch
BranchTrueFalseTotal
ncols = proj ? nproj : last + 1; 0 0 0
3368  
3369 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
3370 0 SV **val_svp = !proj ? av_fetch(row, c, 0)
3371 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
3372 0 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 0 0 0
3373 0 ▶ if (c > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c > 0) 0 0 0
3374 0 nbuf_u8(&b, '\t');
3375  
3376 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
3377 0 mg_get(*val_svp);
3378 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
3379 0 nbuf_append(&b, "\\N", 2);
3380 0 ▶ } else if (SvROK(*val_svp)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvROK(*val_svp)) { 0 0 0
3381 /* Nested AV/HV refs (Array/Tuple/Map columns) cannot be
3382 * round-tripped through TSV without column-type info from
3383 * the server, which the HTTP path doesn't have. Fail loudly
3384 * rather than silently sending ARRAY(0x...) garbage. */
3385 0 Safefree(b.data);
3386 0 croak("insert data: row %" IVdf " column %" IVdf " is a "
3387 "reference; nested Array/Tuple/Map columns require "
3388 "the native protocol", (IV)r, (IV)c);
3389 } else {
3390 STRLEN vlen;
3391 /* nomg: already FETCHed above; a second FETCH would
3392 * double user-visible side effects. */
3393 0 const char *v = SvPV_nomg(*val_svp, vlen);
3394 0 tsv_escape(&b, v, vlen);
3395 }
3396 }
3397 0 nbuf_u8(&b, '\n');
3398 }
3399  
3400 0 *out_len = b.len;
3401 0 return b.data;
3402 }
3403  
3404 /* Strict "[+-]?digits" parses with full consumption and overflow
3405 * detection (server TSV parity: no whitespace, no trailing garbage).
3406 * Length-delimited: TSV fields are not NUL-terminated. */
3407 0 static int64_t parse_int64_strict(const char *s, size_t len, int *ok) {
3408 0 const char *e = s + len;
3409 0 int neg = 0;
3410 0 uint64_t cutoff, cutlim, v = 0;
3411 0 *ok = 0;
3412 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3413 0 ▶ if (*s == '-') { neg = 1; s++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { neg = 1; s++; } 0 0 0
3414 0 ▶ else if (*s == '+') s++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '+') s++; 0 0 0
3415 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
3416 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
3417 0 cutlim = cutoff % 10;
3418 0 cutoff /= 10;
3419 0 ▶ for (; s < e; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; s < e; s++) { 0 0 0
3420 unsigned d;
3421 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
3422 0 d = (unsigned)(*s - '0');
3423 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
3424 0 v = v * 10 + d;
3425 }
3426 0 *ok = 1;
3427 0 ▶ if (!neg) return (int64_t)v;
Branch: 1 branch
BranchTrueFalseTotal
if (!neg) return (int64_t)v; 0 0 0
3428 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
3429 }
3430  
3431 0 static uint64_t parse_uint64_strict(const char *s, size_t len, int *ok) {
3432 0 const char *e = s + len;
3433 0 uint64_t v = 0;
3434 0 *ok = 0;
3435 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3436 0 ▶ if (*s == '+') s++;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '+') s++; 0 0 0
3437 0 ▶ else if (*s == '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '-') return 0; 0 0 0
3438 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
3439 0 ▶ for (; s < e; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; s < e; s++) { 0 0 0
3440 unsigned d;
3441 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
3442 0 d = (unsigned)(*s - '0');
3443 0 ▶ if (v > (UINT64_MAX - d) / 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (UINT64_MAX - d) / 10) return 0; 0 0 0
3444 0 v = v * 10 + d;
3445 }
3446 0 *ok = 1;
3447 0 return v;
3448 }
3449  
3450 /* SV to int64/uint64 for integer columns. IOK passes through; NOK goes
3451 * through SvNV with a range check so 1e15 encodes exactly instead of
3452 * stringifying to "1e+15" and truncating to 1; anything else parses the
3453 * string form strictly. !SvOK fails: a null into a non-nullable column
3454 * must error, never land as 0. */
3455 0 static int64_t sv_to_int64_strict(pTHX_ SV *sv, int *ok) {
3456 STRLEN vlen;
3457 const char *s;
3458 0 *ok = 0;
3459 0 ▶ if (!SvOK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return 0; 0 0 0
3460 0 ▶ if (SvIOK(sv)) { *ok = 1; return SvIV(sv); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { *ok = 1; return SvIV(sv); } 0 0 0
3461 0 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 0 0 0
3462 0 double d = SvNV(sv);
3463 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
3464 0 *ok = 1;
3465 0 return (int64_t)d;
3466 }
3467 0 s = SvPV(sv, vlen);
3468 0 return parse_int64_strict(s, vlen, ok);
3469 }
3470  
3471 2 static uint64_t sv_to_uint64_strict(pTHX_ SV *sv, int *ok) {
3472 STRLEN vlen;
3473 const char *s;
3474 2 *ok = 0;
3475 2 ▶ if (!SvOK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return 0; 0 2 2
3476 2 ▶ if (SvIsUV(sv)) { *ok = 1; return SvUV(sv); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) { *ok = 1; return SvUV(sv); } 0 2 2
3477 2 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 2 0 2
3478 2 IV iv = SvIV(sv);
3479 2 ▶ if (iv < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (iv < 0) return 0; 0 2 2
3480 2 *ok = 1;
3481 2 return (uint64_t)iv;
3482 }
3483 0 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 0 0 0
3484 0 double d = SvNV(sv);
3485 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
3486 0 *ok = 1;
3487 0 return (uint64_t)d;
3488 }
3489 0 s = SvPV(sv, vlen);
3490 0 return parse_uint64_strict(s, vlen, ok);
3491 }
3492  
3493 /* TSV boolean: exactly "0"/"1" or true/false in any case (probed server
3494 * verdicts: "2", "00", "01" all fail with Code 467). */
3495 0 static int bool_text_to_u8(const char *s, size_t len, uint8_t *out) {
3496 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
3497 0 *out = (uint8_t)(*s - '0');
3498 0 return 1;
3499 }
3500 0 ▶ if (len == 4 &&
Branch: 1 branch
BranchTrueFalseTotal
if (len == 4 && 0 0 0
3501 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
3502 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
3503 0 *out = 1;
3504 0 return 1;
3505 }
3506 0 ▶ if (len == 5 &&
Branch: 1 branch
BranchTrueFalseTotal
if (len == 5 && 0 0 0
3507 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
3508 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
3509 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
3510 0 *out = 0;
3511 0 return 1;
3512 }
3513 0 return 0;
3514 }
3515  
3516 /* Exact label match for a TSV field. Backslash escapes decode before
3517 * comparing, mirroring the server's TSV parsing. */
3518 0 static int enum_match_tsv(const enum_entry_t *entries, size_t n,
3519 const char *s, size_t len, int *code_out) {
3520 char *tmp;
3521 size_t ulen;
3522 int found;
3523 0 ▶ if (!memchr(s, '\\', len))
Branch: 1 branch
BranchTrueFalseTotal
if (!memchr(s, '\\', len)) 0 0 0
3524 0 return enum_code_for_label(entries, n, s, len, code_out);
3525 0 Newx(tmp, len, char);
3526 0 ulen = tsv_unescape(s, len, tmp);
3527 0 found = enum_code_for_label(entries, n, tmp, ulen, code_out);
3528 0 Safefree(tmp);
3529 0 return found;
3530 }
3531  
3532 /* A code the type does not declare would make every later read of the
3533 * column fail ("Unexpected value in enum"); the server's text parser
3534 * rejects it too. */
3535 0 static int enum_has_code(const enum_entry_t *entries, size_t n, int64_t code) {
3536 size_t i;
3537 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
3538 0 ▶ if (entries[i].code == code) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i].code == code) return 1; 0 0 0
3539 0 return 0;
3540 }
3541  
3542 /* Labels win over codes, as in the server's text parsers; a number
3543 * needs the label lookup only when some label looks like one. */
3544 0 static int enum_has_numeric_label(const enum_entry_t *entries, size_t n) {
3545 size_t i, j;
3546 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
3547 0 const char *l = entries[i].label;
3548 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
3549 0 ▶ if (j >= entries[i].len) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (j >= entries[i].len) continue; 0 0 0
3550 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
3551 0 ▶ if (j == entries[i].len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == entries[i].len) return 1; 0 0 0
3552 }
3553 0 return 0;
3554 }
3555  
3556 0 static void nbuf_enum(native_buf_t *b, int type_code, int code) {
3557 0 ▶ if (type_code == CT_ENUM8) {
Branch: 1 branch
BranchTrueFalseTotal
if (type_code == CT_ENUM8) { 0 0 0
3558 0 int8_t v = (int8_t)code;
3559 0 nbuf_append(b, (const char *)&v, 1);
3560 } else {
3561 0 int16_t v = (int16_t)code;
3562 0 nbuf_append(b, (const char *)&v, 2);
3563 }
3564 0 }
3565  
3566 /* The whole of s[0..len) as a number. Text values need not be
3567 * NUL-terminated (substituted defaults are not), so strtod gets a copy. */
3568 0 static int text_to_double(const char *s, size_t len, int as_float, double *out) {
3569 0 char buf[64], *p = buf, *endp;
3570 int ok;
3571 0 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 0 0
3572 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
3573 0 memcpy(p, s, len);
3574 0 p[len] = '\0';
3575 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
3576 0 ok = endp == p + len;
3577 0 ▶ if (p != buf) Safefree(p);
Branch: 1 branch
BranchTrueFalseTotal
if (p != buf) Safefree(p); 0 0 0
3578 0 return ok;
3579 }
3580  
3581 /*
3582 * Encode a column of text values into native binary format.
3583 * Returns 1 on success, 0 if type is unsupported (caller falls back to inline SQL).
3584 */
3585 0 static int encode_column_text(native_buf_t *b,
3586 const char **values, size_t *value_lens,
3587 uint64_t nrows, col_type_t *ct) {
3588 uint64_t i;
3589  
3590 0 switch (ct->code) {
3591 0 case CT_INT8: {
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 w = parse_int64_strict(values[i], value_lens[i], &ok);
3595 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
3596 {
3597 0 int8_t v = (int8_t)w;
3598 0 nbuf_append(b, (const char *)&v, 1);
3599 }
3600 }
3601 0 return 1;
3602 }
3603 0 case CT_ENUM8: case CT_ENUM16: {
3604 0 enum_entry_t *entries = NULL;
3605 0 size_t nentries = enum_parse_entries(ct->type_str, ct->type_str_len, &entries);
3606 0 int rc = 1;
3607 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3608 0 int code, ok = 0;
3609 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
3610 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3611 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
3612 0 code = (int)w;
3613 }
3614 0 nbuf_enum(b, ct->code, code);
3615 }
3616 0 enum_free_entries(entries, nentries);
3617 0 return rc;
3618 }
3619 0 case CT_INT16: {
3620 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3621 0 int ok = 0;
3622 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3623 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
3624 {
3625 0 int16_t v = (int16_t)w;
3626 0 nbuf_append(b, (const char *)&v, 2);
3627 }
3628 }
3629 0 return 1;
3630 }
3631 0 case CT_INT32: case CT_DATE32: {
3632 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3633 int32_t v;
3634 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
3635 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3636 0 ▶ if (value_lens[i] != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] != 10) return 0; 0 0 0
3637 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
3638 0 v = date_string_to_days(values[i], value_lens[i]);
3639 } else {
3640 0 int ok = 0;
3641 0 int64_t w = parse_int64_strict(values[i], value_lens[i], &ok);
3642 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
3643 0 return 0;
3644 0 v = (int32_t)w;
3645 }
3646 0 nbuf_append(b, (const char *)&v, 4);
3647 }
3648 0 return 1;
3649 }
3650 0 case CT_INT64: {
3651 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3652 0 int ok = 0;
3653 0 int64_t v = parse_int64_strict(values[i], value_lens[i], &ok);
3654 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3655 0 nbuf_append(b, (const char *)&v, 8);
3656 }
3657 0 return 1;
3658 }
3659 0 case CT_UINT8: {
3660 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3661 0 int ok = 0;
3662 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i], &ok);
3663 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
3664 {
3665 0 uint8_t v = (uint8_t)w;
3666 0 nbuf_append(b, (const char *)&v, 1);
3667 }
3668 }
3669 0 return 1;
3670 }
3671 0 case CT_BOOL: {
3672 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3673 uint8_t v;
3674 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
3675 0 nbuf_append(b, (const char *)&v, 1);
3676 }
3677 0 return 1;
3678 }
3679 0 case CT_UINT16: case CT_DATE: {
3680 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3681 uint16_t v;
3682 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
3683 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3684 int32_t days;
3685 0 ▶ if (value_lens[i] != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] != 10) return 0; 0 0 0
3686 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
3687 0 days = date_string_to_days(values[i], value_lens[i]);
3688 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
3689 0 v = (uint16_t)days;
3690 } else {
3691 0 int ok = 0;
3692 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i],
3693 &ok);
3694 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
3695 0 v = (uint16_t)w;
3696 }
3697 0 nbuf_append(b, (const char *)&v, 2);
3698 }
3699 0 return 1;
3700 }
3701 0 case CT_UINT32: case CT_DATETIME: {
3702 0 int rc = 1;
3703 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3704 uint32_t v;
3705 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
3706 0 ▶ && values[i][4] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
&& values[i][4] == '-') { 0 0 0
3707 int32_t days;
3708 int64_t total;
3709 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
3710 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
3711 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
3712 0 days = date_string_to_days(values[i], 10);
3713 0 total = (int64_t)days * 86400
3714 0 + parse_hms(values[i], value_lens[i]);
3715 0 ▶ if (ct->tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->tz) { 0 0 0
3716 0 int tok = 0;
3717 0 total = wall_in_tz_to_epoch(total, ct, &tok);
3718 0 ▶ if (!tok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) { rc = 0; break; } 0 0 0
3719 }
3720 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
3721 0 v = (uint32_t)total;
3722 } else {
3723 0 int ok = 0;
3724 0 uint64_t w = parse_uint64_strict(values[i], value_lens[i],
3725 &ok);
3726 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
3727 0 v = (uint32_t)w;
3728 }
3729 0 nbuf_append(b, (const char *)&v, 4);
3730 }
3731 0 return rc;
3732 }
3733 0 case CT_UINT64: {
3734 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3735 0 int ok = 0;
3736 0 uint64_t v = parse_uint64_strict(values[i], value_lens[i], &ok);
3737 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3738 0 nbuf_append(b, (const char *)&v, 8);
3739 }
3740 0 return 1;
3741 }
3742 0 case CT_FLOAT32: {
3743 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3744 double d;
3745 float v;
3746 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
3747 0 v = (float)d;
3748 0 nbuf_append(b, (const char *)&v, 4);
3749 }
3750 0 return 1;
3751 }
3752 0 case CT_FLOAT64: {
3753 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3754 double v;
3755 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
3756 0 nbuf_append(b, (const char *)&v, 8);
3757 }
3758 0 return 1;
3759 }
3760 0 case CT_DATETIME64: {
3761 0 int rc = 1;
3762 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3763 int64_t v;
3764 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
3765 0 int ok = 0;
3766 0 v = parse_datetime64_to_scaled(values[i], value_lens[i],
3767 ct->param, ct, &ok);
3768 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
3769 } else {
3770 0 int ok = 0;
3771 0 v = parse_int64_strict(values[i], value_lens[i], &ok);
3772 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
3773 }
3774 0 nbuf_append(b, (const char *)&v, 8);
3775 }
3776 0 return rc;
3777 }
3778 0 case CT_TIME: {
3779 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3780 0 int ok = 0;
3781 0 int64_t v = parse_time_text(values[i], value_lens[i], 0, &ok);
3782 int32_t w;
3783 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
3784 0 w = (int32_t)v;
3785 0 nbuf_append(b, (const char *)&w, 4);
3786 }
3787 0 return 1;
3788 }
3789 0 case CT_TIME64: {
3790 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3791 0 int ok = 0;
3792 0 int64_t v = parse_time_text(values[i], value_lens[i],
3793 ct->param, &ok);
3794 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3795 0 nbuf_append(b, (const char *)&v, 8);
3796 }
3797 0 return 1;
3798 }
3799 0 case CT_DECIMAL32: {
3800 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3801 0 int ok = 0;
3802 0 int32_t v = (int32_t)parse_decimal_to_raw(values[i], value_lens[i],
3803 ct->param, ct->precision,
3804 &ok);
3805 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3806 0 nbuf_append(b, (const char *)&v, 4);
3807 }
3808 0 return 1;
3809 }
3810 0 case CT_DECIMAL64: {
3811 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3812 0 int ok = 0;
3813 0 int64_t v = parse_decimal_to_raw(values[i], value_lens[i],
3814 ct->param, ct->precision, &ok);
3815 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3816 0 nbuf_append(b, (const char *)&v, 8);
3817 }
3818 0 return 1;
3819 }
3820 0 case CT_DECIMAL128: {
3821 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3822 unsigned char raw[16];
3823 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
3824 ct->param, ct->precision, raw))
3825 0 return 0;
3826 0 nbuf_append(b, (const char *)raw, 16);
3827 }
3828 0 return 1;
3829 }
3830 0 case CT_DECIMAL256: {
3831 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3832 unsigned char raw[32];
3833 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
3834 ct->param, ct->precision, raw))
3835 0 return 0;
3836 0 nbuf_append(b, (const char *)raw, 32);
3837 }
3838 0 return 1;
3839 }
3840 0 case CT_BFLOAT16: {
3841 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3842 double d;
3843 float fv;
3844 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
3845 0 fv = (float)d;
3846 0 uint8_t b32[4]; memcpy(b32, &fv, 4);
3847 0 nbuf_append(b, (const char *)&b32[2], 2);
3848 }
3849 0 return 1;
3850 }
3851 0 case CT_STRING: {
3852 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3853 0 ▶ if (memchr(values[i], '\\', value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(values[i], '\\', value_lens[i])) { 0 0 0
3854 char *tmp;
3855 size_t ulen;
3856 0 Newx(tmp, value_lens[i], char);
3857 0 ulen = tsv_unescape(values[i], value_lens[i], tmp);
3858 0 nbuf_string(b, tmp, ulen);
3859 0 Safefree(tmp);
3860 } else {
3861 0 nbuf_string(b, values[i], value_lens[i]);
3862 }
3863 }
3864 0 return 1;
3865 }
3866 0 case CT_FIXEDSTRING: {
3867 0 size_t fsz = (size_t)ct->param;
3868 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3869 0 ▶ if (memchr(values[i], '\\', value_lens[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(values[i], '\\', value_lens[i])) { 0 0 0
3870 char *tmp;
3871 size_t ulen;
3872 0 size_t tmp_sz = value_lens[i] > fsz ? value_lens[i] : fsz;
3873 0 Newxz(tmp, tmp_sz, char);
3874 0 ulen = tsv_unescape(values[i], value_lens[i], tmp);
3875 0 ▶ if (ulen > fsz) { Safefree(tmp); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ulen > fsz) { Safefree(tmp); return 0; } 0 0 0
3876 0 nbuf_append(b, tmp, fsz);
3877 0 Safefree(tmp);
3878 } else {
3879 size_t cplen;
3880 0 ▶ if (value_lens[i] > fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value_lens[i] > fsz) return 0; 0 0 0
3881 0 cplen = value_lens[i];
3882 0 nbuf_grow(b, fsz);
3883 0 memcpy(b->data + b->len, values[i], cplen);
3884 0 ▶ if (cplen < fsz)
Branch: 1 branch
BranchTrueFalseTotal
if (cplen < fsz) 0 0 0
3885 0 memset(b->data + b->len + cplen, 0, fsz - cplen);
3886 0 b->len += fsz;
3887 }
3888 }
3889 0 return 1;
3890 }
3891 0 case CT_UUID: {
3892 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3893 unsigned char ubytes[16];
3894 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
3895 0 return 0;
3896 0 nbuf_append(b, (const char *)ubytes, 16);
3897 }
3898 0 return 1;
3899 }
3900 0 case CT_IPV4: {
3901 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3902 0 int ok = 0;
3903 0 uint32_t v = parse_ipv4_to_u32(values[i], value_lens[i], &ok);
3904 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
3905 0 nbuf_append(b, (const char *)&v, 4);
3906 }
3907 0 return 1;
3908 }
3909 0 case CT_IPV6: {
3910 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3911 unsigned char addr[16];
3912 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
3913 0 return 0;
3914 0 nbuf_append(b, (const char *)addr, 16);
3915 }
3916 0 return 1;
3917 }
3918 0 case CT_NULLABLE: {
3919 /* null bitmap + inner column */
3920 uint8_t *nulls;
3921 const char **inner_vals;
3922 size_t *inner_lens;
3923  
3924 0 Newx(nulls, nrows, uint8_t);
3925 0 ▶ Newxz(inner_vals, nrows, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(inner_vals, nrows, const char *); 0 0 0
3926 0 ▶ Newx(inner_lens, nrows, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(inner_lens, nrows, size_t); 0 0 0
3927  
3928 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3929 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
3930 0 nulls[i] = 1;
3931 0 nullable_inner_zero(ct->inner,
3932 0 &inner_vals[i], &inner_lens[i]);
3933 } else {
3934 0 nulls[i] = 0;
3935 0 inner_vals[i] = values[i];
3936 0 inner_lens[i] = value_lens[i];
3937 }
3938 }
3939  
3940 0 nbuf_append(b, (const char *)nulls, nrows);
3941 {
3942 0 int rc = encode_column_text(b, inner_vals, inner_lens, nrows, ct->inner);
3943 0 Safefree(nulls);
3944 0 Safefree(inner_vals);
3945 0 Safefree(inner_lens);
3946 0 return rc;
3947 }
3948 }
3949 0 case CT_LOWCARDINALITY: {
3950 native_buf_t dict_buf;
3951 int rc;
3952  
3953 0 ▶ if (nrows == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return 1; 0 0 0
3954 0 ▶ if (ct->inner->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->inner->code == CT_NULLABLE) { 0 0 0
3955 /* Dict travels as plain T with slot 0 reserved for NULL:
3956 * [T default] + rows (nulls substituted), a null row -> 0. */
3957 const char **dvals;
3958 size_t *dlens;
3959 uint8_t *nulls;
3960 const char *zp;
3961 size_t zlen;
3962 0 nullable_inner_zero(ct->inner->inner, &zp, &zlen);
3963 0 ▶ Newx(dvals, nrows + 1, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dvals, nrows + 1, const char *); 0 0 0
3964 0 ▶ Newx(dlens, nrows + 1, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dlens, nrows + 1, size_t); 0 0 0
3965 0 ▶ Newx(nulls, nrows ? nrows : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(nulls, nrows ? nrows : 1, uint8_t); 0 0 0
3966 0 dvals[0] = zp;
3967 0 dlens[0] = zlen;
3968 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
3969 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
3970 0 nulls[i] = 1;
3971 0 dvals[i + 1] = zp;
3972 0 dlens[i + 1] = zlen;
3973 } else {
3974 0 nulls[i] = 0;
3975 0 dvals[i + 1] = values[i];
3976 0 dlens[i + 1] = value_lens[i];
3977 }
3978 }
3979 0 nbuf_init(&dict_buf);
3980 0 rc = encode_column_text(&dict_buf, dvals, dlens, nrows + 1,
3981 0 ct->inner->inner);
3982 0 Safefree(dvals);
3983 0 Safefree(dlens);
3984 0 ▶ rc = rc && lc_encode(b, &dict_buf, nrows, ct->inner->inner, nulls);
Branch: 2 branches
BranchTrueFalseTotal
rc = rc && lc_encode(b, &dict_buf, nrows, ct->inner->inner, nulls); 0 0 0
rc = rc && lc_encode(b, &dict_buf, nrows, ct->inner->inner, nulls); 0 0 0
3985 0 Safefree(dict_buf.data);
3986 0 Safefree(nulls);
3987 0 return rc;
3988 }
3989  
3990 0 nbuf_init(&dict_buf);
3991 0 rc = encode_column_text(&dict_buf, values, value_lens, nrows, ct->inner)
3992 0 ▶ && lc_encode(b, &dict_buf, nrows, ct->inner, NULL);
Branch: 2 branches
BranchTrueFalseTotal
&& lc_encode(b, &dict_buf, nrows, ct->inner, NULL); 0 0 0
&& lc_encode(b, &dict_buf, nrows, ct->inner, NULL); 0 0 0
3993 0 Safefree(dict_buf.data);
3994 0 return rc;
3995 }
3996 0 default:
3997 0 return 0; /* unsupported type — fall back to inline SQL */
3998 }
3999 }
4000  
4001 /* A plain number never reads as date text; stringifying it would only
4002 * allocate a PV in the caller's scalar. */
4003 #define SV_PLAIN_NUM(sv) (!SvGMAGICAL(sv) && !SvPOK(sv) && !SvROK(sv) \
4004 && (SvIOK(sv) || SvNOK(sv)))
4005  
4006 /* The text SvPV would give a plain number (same rules as sv_2pv: IV
4007 * first, 0 for zeroes, Inf/NaN, %g at NV_DIG), written to buf instead of
4008 * cached in the caller's scalar. buf holds SV_NUM_TEXT_MAX bytes. */
4009 #define SV_NUM_TEXT_MAX 64
4010  
4011 #if defined(__SIZEOF_INT128__) && NVSIZE == 8 && NV_DIG == 15
4012 #define HAVE_FAST_G15 1
4013 /* The 15 significant digits %.15g prints for 1e-13 <= |v| < 1e21: the
4014 * exact binary value rounded half-even, as glibc does. v equals
4015 * q * 10^(e10 - 14) after rounding, q in [10^14, 10^15). 0 = out of range. */
4016 0 static int g15_digits(double v, uint64_t *qp, int *e10p, int *negp) {
4017 static const uint64_t p5[28] = {
4018 1ULL, 5ULL, 25ULL, 125ULL, 625ULL, 3125ULL, 15625ULL, 78125ULL,
4019 390625ULL, 1953125ULL, 9765625ULL, 48828125ULL, 244140625ULL,
4020 1220703125ULL, 6103515625ULL, 30517578125ULL, 152587890625ULL,
4021 762939453125ULL, 3814697265625ULL, 19073486328125ULL,
4022 95367431640625ULL, 476837158203125ULL, 2384185791015625ULL,
4023 11920928955078125ULL, 59604644775390625ULL, 298023223876953125ULL,
4024 1490116119384765625ULL, 7450580596923828125ULL
4025 };
4026 0 const uint64_t lo15 = 100000000000000ULL, hi15 = 1000000000000000ULL;
4027 uint64_t bits, mant, q;
4028 int exp2, neg, e10, tries;
4029 double a;
4030 0 memcpy(&bits, &v, 8);
4031 0 neg = (int)(bits >> 63);
4032 0 exp2 = (int)((bits >> 52) & 0x7ff);
4033 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
4034 0 ▶ a = neg ? -v : v;
Branch: 1 branch
BranchTrueFalseTotal
a = neg ? -v : v; 0 0 0
4035 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
4036 0 mant = (bits & ((1ULL << 52) - 1)) | (1ULL << 52);
4037 0 exp2 -= 1075;
4038 /* floor(log10(a)) to within one, corrected below */
4039 0 e10 = ((exp2 + 52) * 78913) >> 18;
4040 /* Double arithmetic first: a * 10^k is off by at most 0.12 from the
4041 * exact product, so it decides the rounding unless it lands near .5
4042 * or a digit-count boundary. */
4043 0 ▶ for (tries = 0; tries < 3; tries++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tries = 0; tries < 3; tries++) { 0 0 0
4044 static const double p10d[23] = {
4045 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
4046 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
4047 };
4048 0 int k = 14 - e10;
4049 double y, f, r;
4050 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
4051 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
4052 0 ▶ if (y < 1e14) { e10--; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (y < 1e14) { e10--; continue; } 0 0 0
4053 0 ▶ if (y >= 1e15) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (y >= 1e15) { e10++; continue; } 0 0 0
4054 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
4055 0 f = floor(y);
4056 0 r = y - f;
4057 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
4058 0 *qp = (uint64_t)f + (r >= 0.7);
4059 0 *e10p = e10;
4060 0 *negp = neg;
4061 0 return 1;
4062 }
4063 0 for (tries = 0; ; tries++) {
4064 0 int k = 14 - e10, s, up;
4065 unsigned __int128 n, r;
4066 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
4067 0 ▶ if (k >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (k >= 0) { 0 0 0
4068 0 n = (unsigned __int128)mant * p5[k];
4069 0 s = k + exp2;
4070 0 ▶ if (s >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (s >= 0) { 0 0 0
4071 0 ▶ if (s > 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s > 10) return 0; 0 0 0
4072 0 q = (uint64_t)(n << s);
4073 0 up = 0;
4074 0 ▶ if ((n << s) >> 64) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((n << s) >> 64) return 0; 0 0 0
4075 } else {
4076 unsigned __int128 half;
4077 0 ▶ if (-s > 120) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (-s > 120) return 0; 0 0 0
4078 0 ▶ if ((n >> -s) >> 64) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if ((n >> -s) >> 64) { e10++; continue; } 0 0 0
4079 0 q = (uint64_t)(n >> -s);
4080 0 r = n & ((((unsigned __int128)1) << -s) - 1);
4081 0 half = ((unsigned __int128)1) << (-s - 1);
4082 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
4083 }
4084 } else {
4085 0 uint64_t d = p5[-k];
4086 0 s = exp2 + k;
4087 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
4088 0 n = (unsigned __int128)mant << s;
4089 0 ▶ if ((n / d) >> 64) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if ((n / d) >> 64) { e10++; continue; } 0 0 0
4090 0 q = (uint64_t)(n / d);
4091 0 r = n % d;
4092 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
4093 }
4094 0 ▶ if (q >= hi15) { e10++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (q >= hi15) { e10++; continue; } 0 0 0
4095 0 ▶ if (q < lo15) { e10--; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (q < lo15) { e10--; continue; } 0 0 0
4096 0 ▶ if (up) q++;
Branch: 1 branch
BranchTrueFalseTotal
if (up) q++; 0 0 0
4097 0 ▶ if (q == hi15) { q = lo15; e10++; }
Branch: 1 branch
BranchTrueFalseTotal
if (q == hi15) { q = lo15; e10++; } 0 0 0
4098 0 break;
4099 }
4100 0 *qp = q;
4101 0 *e10p = e10;
4102 0 *negp = neg;
4103 0 return 1;
4104 }
4105  
4106 /* snprintf(buf, "%.15g", v) where g15_digits applies, else 0. */
4107 0 static int fast_g15(double v, char *buf) {
4108 uint64_t q;
4109 int e10, neg;
4110 0 char dig[16], *p = buf;
4111 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
4112 {
4113 0 int i, nd = 15;
4114 0 uint32_t hi = (uint32_t)(q / 100000000), lo = (uint32_t)(q % 100000000);
4115 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
4116 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
4117 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
4118 0 ▶ if (neg) *p++ = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) *p++ = '-'; 0 0 0
4119 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
4120 0 int x = e10 < 0 ? -e10 : e10;
4121 0 *p++ = dig[0];
4122 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
4123 0 *p++ = 'e';
4124 0 ▶ *p++ = e10 < 0 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
*p++ = e10 < 0 ? '-' : '+'; 0 0 0
4125 0 ▶ if (x >= 100) *p++ = (char)('0' + x / 100);
Branch: 1 branch
BranchTrueFalseTotal
if (x >= 100) *p++ = (char)('0' + x / 100); 0 0 0
4126 0 *p++ = (char)('0' + x / 10 % 10);
4127 0 *p++ = (char)('0' + x % 10);
4128 0 ▶ } else if (e10 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (e10 >= 0) { 0 0 0
4129 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
4130 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
4131 } else {
4132 0 *p++ = '0';
4133 0 *p++ = '.';
4134 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
4135 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
4136 }
4137 }
4138 0 *p = '\0';
4139 0 return (int)(p - buf);
4140 }
4141 #endif
4142  
4143 0 static const char *sv_num_text(pTHX_ SV *sv, char *buf, STRLEN *len) {
4144 NV nv;
4145 int n;
4146 0 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 0 0 0
4147 0 n = SvIsUV(sv) ? snprintf(buf, SV_NUM_TEXT_MAX, "%" UVuf, SvUVX(sv))
4148 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
4149 } else {
4150 0 nv = SvNVX(sv);
4151 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
4152 0 n = snprintf(buf, SV_NUM_TEXT_MAX, "0");
4153 0 ▶ else if (Perl_isnan(nv))
Branch: 1 branch
BranchTrueFalseTotal
else if (Perl_isnan(nv)) 0 0 0
4154 0 n = snprintf(buf, SV_NUM_TEXT_MAX, "NaN");
4155 0 ▶ else if (Perl_isinf(nv))
Branch: 1 branch
BranchTrueFalseTotal
else if (Perl_isinf(nv)) 0 0 0
4156 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
4157 else {
4158 #ifdef HAVE_FAST_G15
4159 0 n = fast_g15((double)nv, buf);
4160 0 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 0 0 0
4161 #endif
4162 0 n = snprintf(buf, SV_NUM_TEXT_MAX, "%.*" NVgf, NV_DIG, nv);
4163 }
4164 }
4165 0 *len = (STRLEN)n;
4166 0 return buf;
4167 }
4168  
4169 /* SvPV(sv, len) without caching a string in a plain number. */
4170 #define SV_TEXT(sv, buf, len) \
4171 (SV_PLAIN_NUM(sv) ? sv_num_text(aTHX_ (sv), (buf), &(len)) : SvPV((sv), (len)))
4172  
4173 /* parse_decimal_to_raw() of the text SvPV gives a plain number, computed
4174 * from the number: 1 when done (*ok the verdict), 0 to parse the text. */
4175 0 static int num_decimal_raw(pTHX_ SV *sv, int scale, int precision,
4176 int64_t *raw, int *ok) {
4177 static const uint64_t p10[19] = {
4178 1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL,
4179 10000000ULL, 100000000ULL, 1000000000ULL, 10000000000ULL,
4180 100000000000ULL, 1000000000000ULL, 10000000000000ULL,
4181 100000000000000ULL, 1000000000000000ULL, 10000000000000000ULL,
4182 100000000000000000ULL, 1000000000000000000ULL
4183 };
4184 uint64_t mag, t;
4185 0 int neg = 0, int_digits = 0;
4186 0 *ok = 0;
4187 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
4188 0 ▶ || precision < 1 || precision > 18)
Branch: 2 branches
BranchTrueFalseTotal
|| precision < 1 || precision > 18) 0 0 0
|| precision < 1 || precision > 18) 0 0 0
4189 0 return 0;
4190 0 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 0 0 0
4191 0 ▶ if (SvIsUV(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) { 0 0 0
4192 0 mag = SvUVX(sv);
4193 } else {
4194 0 IV iv = SvIVX(sv);
4195 0 neg = iv < 0;
4196 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
4197 }
4198 0 ▶ for (t = mag; t; t /= 10) int_digits++;
Branch: 1 branch
BranchTrueFalseTotal
for (t = mag; t; t /= 10) int_digits++; 0 0 0
4199 0 ▶ if (int_digits > precision - scale) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 1; 0 0 0
4200 0 mag *= p10[scale];
4201 } else {
4202 #ifdef HAVE_FAST_G15
4203 0 NV nv = SvNVX(sv);
4204 uint64_t q;
4205 int e10, sh;
4206 0 ▶ if (nv == 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nv == 0.0) { 0 0 0
4207 0 *raw = 0;
4208 0 *ok = 1;
4209 0 return 1;
4210 }
4211 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
4212 0 int_digits = e10 >= 0 ? e10 + 1 : 0;
4213 0 ▶ if (int_digits > precision - scale) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (int_digits > precision - scale) return 1; 0 0 0
4214 0 sh = e10 - 14 + scale;
4215 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
4216 #else
4217 return 0;
4218 #endif
4219 }
4220 0 ▶ *raw = neg ? -(int64_t)mag : (int64_t)mag;
Branch: 1 branch
BranchTrueFalseTotal
*raw = neg ? -(int64_t)mag : (int64_t)mag; 0 0 0
4221 0 *ok = 1;
4222 0 return 1;
4223 }
4224  
4225 /*
4226 * Encode a column of Perl SV values into native binary format.
4227 * Like encode_column_text() but takes SVs directly — no TSV parsing/unescaping.
4228 * Returns 1 on success, 0 if type is unsupported.
4229 */
4230 28 static int encode_column_sv(pTHX_ native_buf_t *b,
4231 SV **values, uint64_t nrows,
4232 col_type_t *ct) {
4233 uint64_t i;
4234 char nbuf_num[SV_NUM_TEXT_MAX];
4235  
4236 28 switch (ct->code) {
4237 0 case CT_INT8: {
4238 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4239 0 int ok = 0;
4240 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
4241 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
4242 {
4243 0 int8_t v = (int8_t)w;
4244 0 nbuf_append(b, (const char *)&v, 1);
4245 }
4246 }
4247 0 return 1;
4248 }
4249 0 case CT_ENUM8: case CT_ENUM16: {
4250 0 enum_entry_t *entries = NULL;
4251 0 size_t nentries = enum_parse_entries(ct->type_str, ct->type_str_len, &entries);
4252 0 int numeric_labels = enum_has_numeric_label(entries, nentries);
4253 0 int rc = 1;
4254 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4255 0 SV *v = values[i];
4256 0 int code, ok = 0;
4257 0 ▶ if (!SvOK(v)) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { rc = 0; break; } 0 0 0
4258 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
4259 STRLEN vlen;
4260 0 const char *s = SvPV(v, vlen);
4261 0 ok = enum_code_for_label(entries, nentries, s, vlen, &code);
4262 }
4263 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4264 0 int64_t w = sv_to_int64_strict(aTHX_ v, &ok);
4265 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
4266 0 code = (int)w;
4267 }
4268 0 nbuf_enum(b, ct->code, code);
4269 }
4270 0 enum_free_entries(entries, nentries);
4271 0 return rc;
4272 }
4273 0 case CT_INT16: {
4274 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4275 0 int ok = 0;
4276 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
4277 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
4278 {
4279 0 int16_t v = (int16_t)w;
4280 0 nbuf_append(b, (const char *)&v, 2);
4281 }
4282 }
4283 0 return 1;
4284 }
4285 0 case CT_INT32: case CT_DATE32: {
4286 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4287 int32_t v;
4288 STRLEN vlen;
4289 const char *s;
4290 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4291 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
4292 0 else s = SvPV(values[i], vlen);
4293 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
4294 0 ▶ if (vlen != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen != 10) return 0; 0 0 0
4295 0 ▶ if (!valid_date_prefix(s, vlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, vlen)) return 0; 0 0 0
4296 0 v = date_string_to_days(s, vlen);
4297 } else {
4298 0 int ok = 0;
4299 0 int64_t w = sv_to_int64_strict(aTHX_ values[i], &ok);
4300 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
4301 0 return 0;
4302 0 v = (int32_t)w;
4303 }
4304 0 nbuf_append(b, (const char *)&v, 4);
4305 }
4306 0 return 1;
4307 }
4308 0 case CT_INT64: {
4309 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4310 0 int ok = 0;
4311 0 int64_t v = sv_to_int64_strict(aTHX_ values[i], &ok);
4312 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4313 0 nbuf_append(b, (const char *)&v, 8);
4314 }
4315 0 return 1;
4316 }
4317 0 case CT_UINT8: {
4318 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4319 0 int ok = 0;
4320 0 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
4321 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
4322 {
4323 0 uint8_t v = (uint8_t)w;
4324 0 nbuf_append(b, (const char *)&v, 1);
4325 }
4326 }
4327 0 return 1;
4328 }
4329 0 case CT_BOOL: {
4330 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4331 uint8_t v;
4332 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4333 0 v = SvTRUE(values[i]) ? 1 : 0;
4334 0 nbuf_append(b, (const char *)&v, 1);
4335 }
4336 0 return 1;
4337 }
4338 0 case CT_UINT16: case CT_DATE: {
4339 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4340 uint16_t v;
4341 STRLEN vlen;
4342 const char *s;
4343 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4344 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
4345 0 else s = SvPV(values[i], vlen);
4346 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
4347 int32_t days;
4348 0 ▶ if (vlen != 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen != 10) return 0; 0 0 0
4349 0 ▶ if (!valid_date_prefix(s, vlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_date_prefix(s, vlen)) return 0; 0 0 0
4350 0 days = date_string_to_days(s, vlen);
4351 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
4352 0 v = (uint16_t)days;
4353 } else {
4354 0 int ok = 0;
4355 0 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
4356 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
4357 0 v = (uint16_t)w;
4358 }
4359 0 nbuf_append(b, (const char *)&v, 2);
4360 }
4361 0 return 1;
4362 }
4363 2 case CT_UINT32: case CT_DATETIME: {
4364 2 int rc = 1;
4365 4 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 2 2 4
4366 uint32_t v;
4367 STRLEN vlen;
4368 const char *s;
4369 2 ▶ if (!SvOK(values[i])) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { rc = 0; break; } 0 2 2
4370 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
4371 0 else s = SvPV(values[i], vlen);
4372 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
4373 int32_t days;
4374 int64_t total;
4375 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
4376 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
4377 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
4378 0 days = date_string_to_days(s, 10);
4379 0 total = (int64_t)days * 86400 + parse_hms(s, vlen);
4380 0 ▶ if (ct->tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->tz) { 0 0 0
4381 0 int tok = 0;
4382 0 total = wall_in_tz_to_epoch(total, ct, &tok);
4383 0 ▶ if (!tok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) { rc = 0; break; } 0 0 0
4384 }
4385 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
4386 0 v = (uint32_t)total;
4387 } else {
4388 2 int ok = 0;
4389 2 uint64_t w = sv_to_uint64_strict(aTHX_ values[i], &ok);
4390 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
4391 2 v = (uint32_t)w;
4392 }
4393 2 nbuf_append(b, (const char *)&v, 4);
4394 }
4395 2 return rc;
4396 }
4397 0 case CT_UINT64: {
4398 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4399 0 int ok = 0;
4400 0 uint64_t v = sv_to_uint64_strict(aTHX_ values[i], &ok);
4401 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4402 0 nbuf_append(b, (const char *)&v, 8);
4403 }
4404 0 return 1;
4405 }
4406 0 case CT_FLOAT32: {
4407 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4408 float v;
4409 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4410 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4411 0 v = (float)SvNV(values[i]);
4412 } else {
4413 STRLEN vlen;
4414 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
4415 char *endp;
4416 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4417 0 v = strtof(s, &endp);
4418 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4419 }
4420 0 nbuf_append(b, (const char *)&v, 4);
4421 }
4422 0 return 1;
4423 }
4424 0 case CT_FLOAT64: {
4425 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4426 double v;
4427 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4428 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4429 0 v = SvNV(values[i]);
4430 } else {
4431 STRLEN vlen;
4432 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
4433 char *endp;
4434 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4435 0 v = strtod(s, &endp);
4436 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4437 }
4438 0 nbuf_append(b, (const char *)&v, 8);
4439 }
4440 0 return 1;
4441 }
4442 26 case CT_DATETIME64: {
4443 26 int rc = 1;
4444 28 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 18 10 28
4445 int64_t v;
4446 34 ▶ if (!SvOK(values[i])) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { rc = 0; break; } 0 18 18
4447 18 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 18 18
4448 0 v = (int64_t)SvIV(values[i]);
4449 } else {
4450 STRLEN vlen;
4451 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
4452 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
4453 18 int ok = 0;
4454 18 v = parse_datetime64_to_scaled(s, vlen, ct->param, ct, &ok);
4455 18 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 16 2 18
4456 } else {
4457 0 int ok = 0;
4458 0 v = parse_int64_strict(s, vlen, &ok);
4459 0 ▶ if (!ok) { rc = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { rc = 0; break; } 0 0 0
4460 }
4461 }
4462 2 nbuf_append(b, (const char *)&v, 8);
4463 }
4464 26 return rc;
4465 }
4466 0 case CT_TIME: {
4467 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4468 int64_t v;
4469 int32_t w;
4470 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4471 0 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 0 0
4472 0 v = (int64_t)SvIV(values[i]);
4473 } else {
4474 STRLEN vlen;
4475 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
4476 0 int ok = 0;
4477 0 v = parse_time_text(s, vlen, 0, &ok);
4478 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4479 0 v = parse_int64_strict(s, vlen, &ok);
4480 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4481 }
4482 }
4483 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
4484 0 w = (int32_t)v;
4485 0 nbuf_append(b, (const char *)&w, 4);
4486 }
4487 0 return 1;
4488 }
4489 0 case CT_TIME64: {
4490 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4491 int64_t v;
4492 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4493 0 ▶ if (SvIOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(values[i])) { 0 0 0
4494 0 v = (int64_t)SvIV(values[i]);
4495 } else {
4496 STRLEN vlen;
4497 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
4498 0 int ok = 0;
4499 0 v = parse_time_text(s, vlen, ct->param, &ok);
4500 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
4501 0 v = parse_int64_strict(s, vlen, &ok);
4502 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4503 }
4504 }
4505 0 nbuf_append(b, (const char *)&v, 8);
4506 }
4507 0 return 1;
4508 }
4509 0 case CT_DECIMAL32: {
4510 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4511 STRLEN vlen;
4512 const char *s;
4513 0 int ok = 0;
4514 int32_t v;
4515 int64_t raw;
4516 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4517 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
4518 &raw, &ok)) {
4519 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
4520 0 raw = parse_decimal_to_raw(s, vlen, ct->param,
4521 ct->precision, &ok);
4522 }
4523 0 v = (int32_t)raw;
4524 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4525 0 nbuf_append(b, (const char *)&v, 4);
4526 }
4527 0 return 1;
4528 }
4529 0 case CT_DECIMAL64: {
4530 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4531 STRLEN vlen;
4532 const char *s;
4533 0 int ok = 0;
4534 int64_t v;
4535 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4536 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
4537 &v, &ok)) {
4538 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
4539 0 v = parse_decimal_to_raw(s, vlen, ct->param, ct->precision, &ok);
4540 }
4541 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4542 0 nbuf_append(b, (const char *)&v, 8);
4543 }
4544 0 return 1;
4545 }
4546 0 case CT_DECIMAL128: {
4547 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4548 STRLEN vlen;
4549 const char *s;
4550 unsigned char raw[16];
4551 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4552 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
4553 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
4554 raw))
4555 0 return 0;
4556 0 nbuf_append(b, (const char *)raw, 16);
4557 }
4558 0 return 1;
4559 }
4560 0 case CT_DECIMAL256: {
4561 /* Decimal text, or raw 32 bytes (LE, signed) — the shape the
4562 * decoder delivers. Useful with Math::BigInt::to_bytes(LE).
4563 * The raw fallback needs a non-text byte, so a 32-char typo
4564 * fails loudly instead of storing garbage. */
4565 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4566 STRLEN slen;
4567 const char *s;
4568 unsigned char raw[32];
4569 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4570 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
4571 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
4572 0 nbuf_append(b, (const char *)raw, 32);
4573 0 ▶ } else if (slen == 32) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (slen == 32) { 0 0 0
4574 STRLEN k;
4575 0 int binary = 0;
4576 0 ▶ for (k = 0; k < slen; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < slen; k++) { 0 0 0
4577 0 unsigned char c = (unsigned char)s[k];
4578 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
4579 }
4580 0 ▶ if (!binary) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!binary) return 0; 0 0 0
4581 0 nbuf_append(b, s, 32);
4582 } else {
4583 0 return 0;
4584 }
4585 }
4586 0 return 1;
4587 }
4588 0 case CT_BFLOAT16: {
4589 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4590 float fv;
4591 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4592 0 ▶ if (SvNOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(values[i])) { 0 0 0
4593 0 fv = (float)SvNV(values[i]);
4594 } else {
4595 STRLEN vlen;
4596 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
4597 char *endp;
4598 0 ▶ if (vlen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (vlen == 0) return 0; 0 0 0
4599 0 fv = (float)strtod(s, &endp);
4600 0 ▶ if (endp != s + vlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (endp != s + vlen) return 0; 0 0 0
4601 }
4602 0 uint8_t b32[4]; memcpy(b32, &fv, 4);
4603 /* Truncate to top 16 bits — bytes [2,3] of a LE Float32. */
4604 0 nbuf_append(b, (const char *)&b32[2], 2);
4605 }
4606 0 return 1;
4607 }
4608 0 case CT_STRING: {
4609 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4610 STRLEN vlen;
4611 const char *v;
4612 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4613 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
4614 0 nbuf_string(b, v, vlen);
4615 }
4616 0 return 1;
4617 }
4618 0 case CT_FIXEDSTRING: {
4619 0 size_t fsz = (size_t)ct->param;
4620 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4621 STRLEN vlen;
4622 const char *v;
4623 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4624 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
4625 0 ▶ if ((size_t)vlen > fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)vlen > fsz) return 0; 0 0 0
4626 0 nbuf_grow(b, fsz);
4627 0 memcpy(b->data + b->len, v, vlen);
4628 0 ▶ if ((size_t)vlen < fsz)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)vlen < fsz) 0 0 0
4629 0 memset(b->data + b->len + vlen, 0, fsz - (size_t)vlen);
4630 0 b->len += fsz;
4631 }
4632 0 return 1;
4633 }
4634 0 case CT_UUID: {
4635 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4636 unsigned char ubytes[16];
4637 STRLEN slen;
4638 const char *s;
4639 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4640 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
4641 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
4642 0 nbuf_append(b, (const char *)ubytes, 16);
4643 }
4644 0 return 1;
4645 }
4646 0 case CT_IPV4: {
4647 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4648 STRLEN vlen;
4649 const char *s;
4650 uint32_t v;
4651 0 int ok = 0;
4652 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4653 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
4654 0 v = parse_ipv4_to_u32(s, (size_t)vlen, &ok);
4655 0 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 0 0
4656 0 nbuf_append(b, (const char *)&v, 4);
4657 }
4658 0 return 1;
4659 }
4660 0 case CT_IPV6: {
4661 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4662 unsigned char addr[16];
4663 STRLEN vlen;
4664 const char *s;
4665 0 ▶ if (!SvOK(values[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) return 0; 0 0 0
4666 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
4667 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
4668 0 nbuf_append(b, (const char *)addr, 16);
4669 }
4670 0 return 1;
4671 }
4672 0 case CT_NULLABLE: {
4673 uint8_t *nulls;
4674 SV **inner_vals;
4675 SV *zero_sv;
4676  
4677 0 Newx(nulls, nrows, uint8_t);
4678 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
4679 0 zero_sv = nullable_inner_zero_sv(aTHX_ ct->inner);
4680  
4681 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4682 0 ▶ if (!SvOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { 0 0 0
4683 0 nulls[i] = 1;
4684 0 inner_vals[i] = zero_sv;
4685 } else {
4686 0 nulls[i] = 0;
4687 0 inner_vals[i] = values[i];
4688 }
4689 }
4690  
4691 0 nbuf_append(b, (const char *)nulls, nrows);
4692 {
4693 0 int rc = encode_column_sv(aTHX_ b, inner_vals, nrows, ct->inner);
4694 0 Safefree(nulls);
4695 0 Safefree(inner_vals);
4696 0 SvREFCNT_dec(zero_sv);
4697 0 return rc;
4698 }
4699 }
4700 0 case CT_LOWCARDINALITY: {
4701 native_buf_t dict_buf;
4702 int rc;
4703  
4704 0 ▶ if (nrows == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) return 1; 0 0 0
4705 0 ▶ if (ct->inner->code == CT_NULLABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->inner->code == CT_NULLABLE) { 0 0 0
4706 /* Dict travels as plain T with slot 0 reserved for NULL:
4707 * [T default] + rows (nulls substituted), a null row -> 0. */
4708 SV **dvals;
4709 uint8_t *nulls;
4710 0 SV *zero_sv = nullable_inner_zero_sv(aTHX_ ct->inner->inner);
4711 0 ▶ Newx(dvals, nrows + 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(dvals, nrows + 1, SV *); 0 0 0
4712 0 ▶ Newx(nulls, nrows ? nrows : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(nulls, nrows ? nrows : 1, uint8_t); 0 0 0
4713 0 dvals[0] = zero_sv;
4714 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4715 0 ▶ if (!SvOK(values[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(values[i])) { 0 0 0
4716 0 nulls[i] = 1;
4717 0 dvals[i + 1] = zero_sv;
4718 } else {
4719 0 nulls[i] = 0;
4720 0 dvals[i + 1] = values[i];
4721 }
4722 }
4723 0 nbuf_init(&dict_buf);
4724 0 rc = encode_column_sv(aTHX_ &dict_buf, dvals, nrows + 1,
4725 0 ct->inner->inner);
4726 0 Safefree(dvals);
4727 0 SvREFCNT_dec(zero_sv);
4728 0 ▶ rc = rc && lc_encode(b, &dict_buf, nrows, ct->inner->inner, nulls);
Branch: 2 branches
BranchTrueFalseTotal
rc = rc && lc_encode(b, &dict_buf, nrows, ct->inner->inner, nulls); 0 0 0
rc = rc && lc_encode(b, &dict_buf, nrows, ct->inner->inner, nulls); 0 0 0
4729 0 Safefree(dict_buf.data);
4730 0 Safefree(nulls);
4731 0 return rc;
4732 }
4733  
4734 0 nbuf_init(&dict_buf);
4735 0 rc = encode_column_sv(aTHX_ &dict_buf, values, nrows, ct->inner)
4736 0 ▶ && lc_encode(b, &dict_buf, nrows, ct->inner, NULL);
Branch: 2 branches
BranchTrueFalseTotal
&& lc_encode(b, &dict_buf, nrows, ct->inner, NULL); 0 0 0
&& lc_encode(b, &dict_buf, nrows, ct->inner, NULL); 0 0 0
4737 0 Safefree(dict_buf.data);
4738 0 return rc;
4739 }
4740 0 case CT_ARRAY: {
4741 /* Each value must be an AV ref. Wire format: offsets + flat inner data */
4742 0 uint64_t total = 0;
4743 uint64_t *offsets;
4744 SV **all_elems;
4745 0 uint64_t pos = 0;
4746 int rc;
4747  
4748 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4749 AV *av;
4750 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
4751 0 return 0;
4752 0 av = (AV *)SvRV(values[i]);
4753 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
4754 }
4755  
4756 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
4757 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
4758  
4759 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4760 0 AV *av = (AV *)SvRV(values[i]);
4761 0 SSize_t n = av_len(av) + 1, j;
4762 /* av_fetch on a tie returns a lazy mortal proxy: copy the
4763 * fetched value (borrowing the proxy would re-FETCH on
4764 * every later read). Plain arrays borrow as before. */
4765 0 int av_magical = SvRMAGICAL((SV *)av);
4766 /* A tie whose FETCHSIZE grew since the sizing pass would
4767 * overflow all_elems: fail clean instead. */
4768 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
4769 0 goto arr_oom;
4770 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
4771 0 SV **ep = av_fetch(av, j, 0);
4772 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
4773 /* Fetch exactly once: a borrowed proxy re-FETCHes on
4774 * every later read, and a later SvPV would too. */
4775 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
4776 0 elem = sv_2mortal(newSVsv(elem));
4777 0 all_elems[pos++] = elem;
4778 }
4779 0 offsets[i] = pos;
4780 }
4781 /* Encode what was actually gathered: a tie that shrank since
4782 * the sizing pass must not leave total pointing past pos. */
4783 0 total = pos;
4784  
4785 /* write offsets as uint64 LE */
4786 0 nbuf_append(b, (const char *)offsets, nrows * 8);
4787 0 rc = encode_column_sv(aTHX_ b, all_elems, total, ct->inner);
4788 0 Safefree(offsets);
4789 0 Safefree(all_elems);
4790 0 return rc;
4791 0 arr_oom:
4792 0 Safefree(offsets);
4793 0 Safefree(all_elems);
4794 0 return 0;
4795 }
4796 0 case CT_TUPLE: {
4797 /* Each value must be an AV ref with num_inners elements */
4798 int j;
4799 0 ▶ if (ct->num_inners == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners == 0) { 0 0 0
4800 /* Empty Tuple(): 1 placeholder byte per row on the wire */
4801 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4802 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
4803 0 return 0;
4804 0 nbuf_append(b, "0", 1);
4805 }
4806 0 return 1;
4807 }
4808 0 ▶ for (j = 0; j < ct->num_inners; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ct->num_inners; j++) { 0 0 0
4809 SV **col_vals;
4810 int rc;
4811 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
4812 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4813 AV *av;
4814 SV **ep;
4815 SV *elem;
4816 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
4817 0 Safefree(col_vals);
4818 0 return 0;
4819 }
4820 0 av = (AV *)SvRV(values[i]);
4821 0 ep = av_fetch(av, j, 0);
4822 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
4823 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
4824 0 elem = sv_2mortal(newSVsv(elem));
4825 0 col_vals[i] = elem;
4826 }
4827 0 rc = encode_column_sv(aTHX_ b, col_vals, nrows, ct->inners[j]);
4828 0 Safefree(col_vals);
4829 0 ▶ if (!rc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return 0; 0 0 0
4830 }
4831 0 return 1;
4832 }
4833 0 case CT_MAP: {
4834 /* Each value must be a hashref. Wire format: offsets + key column + value column */
4835 0 uint64_t total = 0;
4836 0 uint64_t cap = 0;
4837 uint64_t *offsets;
4838 SV **all_keys, **all_vals;
4839 0 uint64_t pos = 0;
4840 int rc;
4841  
4842 0 ▶ if (ct->num_inners != 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners != 2) return 0; 0 0 0
4843  
4844 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4845 HV *hv;
4846 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
4847 0 return 0;
4848 0 hv = (HV *)SvRV(values[i]);
4849 /* sizing hint only: HvUSEDKEYS is meaningless for tied
4850 * hashes, so the fill loop grows below as needed. */
4851 0 ▶ total += HvUSEDKEYS(hv);
Branch: 1 branch
BranchTrueFalseTotal
total += HvUSEDKEYS(hv); 0 0 0
4852 }
4853  
4854 0 ▶ Newx(offsets, nrows, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(offsets, nrows, uint64_t); 0 0 0
4855 0 ▶ cap = total ? total : 1;
Branch: 1 branch
BranchTrueFalseTotal
cap = total ? total : 1; 0 0 0
4856 0 ▶ Newx(all_keys, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(all_keys, cap, SV *); 0 0 0
4857 0 ▶ Newx(all_vals, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(all_vals, cap, SV *); 0 0 0
4858  
4859 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4860 0 HV *hv = (HV *)SvRV(values[i]);
4861 HE *he;
4862 /* hv_iterval on a tie returns a lazy mortal FETCH proxy:
4863 * copy the fetched value (borrowing the proxy would
4864 * re-FETCH on every later read, and its fake-HE key
4865 * is not safe to borrow either). */
4866 0 int hv_magical = SvRMAGICAL((SV *)hv);
4867 0 hv_iterinit(hv);
4868 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
4869 I32 klen;
4870 SV *vsv;
4871 0 char *key = hv_iterkey(he, &klen);
4872 0 ▶ if (pos >= cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= cap) { 0 0 0
4873 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
4874 0 goto map_oom;
4875 0 cap *= 2;
4876 0 ▶ Renew(all_keys, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(all_keys, cap, SV *); 0 0 0
4877 0 ▶ Renew(all_vals, cap, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(all_vals, cap, SV *); 0 0 0
4878 }
4879 0 vsv = hv_iterval(hv, he);
4880 0 ▶ if (hv_magical) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_magical) { 0 0 0
4881 0 all_keys[pos] = sv_2mortal(newSVpvn(key, klen));
4882 0 all_vals[pos] = sv_2mortal(newSVsv(vsv));
4883 } else {
4884 0 all_keys[pos] = hv_iterkeysv(he);
4885 0 all_vals[pos] = SvGMAGICAL(vsv)
4886 0 ▶ ? sv_2mortal(newSVsv(vsv)) : vsv;
Branch: 1 branch
BranchTrueFalseTotal
? sv_2mortal(newSVsv(vsv)) : vsv; 0 0 0
4887 }
4888 0 pos++;
4889 }
4890 0 offsets[i] = pos;
4891 }
4892 0 total = pos;
4893  
4894 0 nbuf_append(b, (const char *)offsets, nrows * 8);
4895 0 rc = encode_column_sv(aTHX_ b, all_keys, total, ct->inners[0]);
4896 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
4897 0 Safefree(offsets);
4898 0 Safefree(all_keys);
4899 0 Safefree(all_vals);
4900 0 return rc;
4901 0 map_oom:
4902 0 Safefree(offsets);
4903 0 Safefree(all_keys);
4904 0 Safefree(all_vals);
4905 0 return 0;
4906 }
4907 0 case CT_JSON: {
4908 /* Wire layout (V1) per Clickhouse-Encoder/doc/json-research:
4909 * Object prefix: UInt64=0, varint K (twice), K * lenstr path
4910 * Per-path Dynamic prefix: UInt64=1, varint T (twice),
4911 * T * lenstr type-name, UInt64=0
4912 * Per-path data: N disc bytes, then per-variant data in lex
4913 * order including a SharedVariant slot
4914 * Shared data trailer: N * UInt64 LE zero (no shared paths)
4915 */
4916 0 ENTER; SAVETMPS;
4917 int p;
4918 0 HV **row_hvs = NULL;
4919 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
4920 0 ▶ Newxz(row_hvs, nrows, HV*);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(row_hvs, nrows, HV*); 0 0 0
4921 0 SAVEFREEPV(row_hvs);
4922 }
4923 0 HV *all_paths = (HV*)sv_2mortal((SV*)newHV());
4924  
4925 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4926 0 SV *val = values[i];
4927 0 ▶ if (!SvOK(val)) { row_hvs[i] = NULL; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { row_hvs[i] = NULL; continue; } 0 0 0
4928 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
4929 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4930 0 return 0;
4931 }
4932 0 HV *flat = (HV*)sv_2mortal((SV*)newHV());
4933 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
4934 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
4935 0 return 0;
4936 }
4937 0 row_hvs[i] = flat;
4938 }
4939  
4940 /* Typed paths: extract per-row values from each flat HV (and
4941 * hv_delete to keep them out of the dynamic-paths discovery).
4942 * Missing keys substitute undef as a placeholder; the inner
4943 * encoder treats undef as the type's zero/default. */
4944 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
4945 0 SV ***typed_vals = NULL;
4946 0 ▶ if (n_typed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_typed > 0) { 0 0 0
4947 0 Newxz(typed_vals, n_typed, SV **);
4948 0 SAVEFREEPV(typed_vals);
4949 int tp;
4950 0 ▶ for (tp = 0; tp < n_typed; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) { 0 0 0
4951 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
4952 0 SAVEFREEPV(typed_vals[tp]);
4953 0 size_t nlen = strlen(ct->inner_names[tp]);
4954 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4955 0 ▶ if (!row_hvs[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) { 0 0 0
4956 0 typed_vals[tp][i] = &PL_sv_undef;
4957 0 continue;
4958 }
4959 0 SV **e = hv_fetch(row_hvs[i], ct->inner_names[tp],
4960 (I32)nlen, 0);
4961 0 ▶ if (e && SvOK(*e)) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) { 0 0 0
if (e && SvOK(*e)) { 0 0 0
4962 0 typed_vals[tp][i] = SvREFCNT_inc(*e);
4963 0 sv_2mortal(typed_vals[tp][i]);
4964 0 hv_delete(row_hvs[i], ct->inner_names[tp],
4965 (I32)nlen, G_DISCARD);
4966 } else {
4967 0 typed_vals[tp][i] = &PL_sv_undef;
4968 }
4969 }
4970 }
4971 }
4972  
4973 /* Now collect dynamic-path keys (typed paths already removed). */
4974 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
4975 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
4976 0 hv_iterinit(row_hvs[i]);
4977 HE *he;
4978 0 ▶ while ((he = hv_iternext(row_hvs[i]))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(row_hvs[i]))) { 0 0 0
4979 I32 klen;
4980 0 char *kstr = hv_iterkey(he, &klen);
4981 0 (void)hv_store(all_paths, kstr, klen, newSViv(1), 0);
4982 }
4983 }
4984  
4985 0 ▶ int num_paths = (int)HvUSEDKEYS(all_paths);
Branch: 1 branch
BranchTrueFalseTotal
int num_paths = (int)HvUSEDKEYS(all_paths); 0 0 0
4986 0 json_path_entry_t *pe = NULL;
4987 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
4988 0 Newx(pe, num_paths, json_path_entry_t);
4989 0 SAVEFREEPV(pe);
4990 0 p = 0;
4991 0 hv_iterinit(all_paths);
4992 HE *he;
4993 0 ▶ while ((he = hv_iternext(all_paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(all_paths))) { 0 0 0
4994 I32 klen;
4995 0 char *kstr = hv_iterkey(he, &klen);
4996 0 pe[p].path = kstr;
4997 0 pe[p].len = (STRLEN)klen;
4998 0 p++;
4999 }
5000 0 ▶ if (num_paths > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 1) 0 0 0
5001 0 qsort(pe, num_paths, sizeof(*pe), json_cmp_path_entry);
5002 }
5003  
5004 0 unsigned *kind_masks = NULL;
5005 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
5006 0 Newxz(kind_masks, num_paths, unsigned);
5007 0 SAVEFREEPV(kind_masks);
5008 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
5009 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
5010 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
5011 0 SV **e = hv_fetch(row_hvs[i], pe[p].path, (I32)pe[p].len, 0);
5012 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
5013 0 int k = json_classify_value(aTHX_ *e);
5014 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
5015 0 kind_masks[p] |= 1u << k;
5016 }
5017 }
5018 }
5019  
5020 /* Object structure prefix */
5021 0 nbuf_le64(b, 0); /* Object V1 */
5022 0 nbuf_varuint(b, (uint64_t)num_paths);
5023 0 nbuf_varuint(b, (uint64_t)num_paths);
5024 0 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 0 0 0
5025 0 nbuf_string(b, pe[p].path, pe[p].len);
5026 0 ▶ for (p = 0; p < n_typed; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < n_typed; p++) 0 0 0
5027 0 write_col_prefix(b, ct->inners[p]);
5028  
5029 0 int *path_slots = NULL;
5030 0 int *wire_slot_counts = NULL;
5031 0 ▶ if (num_paths > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_paths > 0) { 0 0 0
5032 0 Newx(path_slots, num_paths * JSON_LEX_SLOTS, int);
5033 0 SAVEFREEPV(path_slots);
5034 0 Newx(wire_slot_counts, num_paths, int);
5035 0 SAVEFREEPV(wire_slot_counts);
5036 0 ▶ for (p = 0; p < num_paths; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) 0 0 0
5037 0 wire_slot_counts[p] = json_build_lex_table(
5038 0 kind_masks[p], path_slots + p*JSON_LEX_SLOTS);
5039 }
5040  
5041 /* Per-path Dynamic V1 prefix + Variant mode */
5042 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
5043 0 int wire_slots = wire_slot_counts[p];
5044 0 int kc = wire_slots - 1;
5045 0 nbuf_le64(b, 1); /* Dynamic V1 */
5046 0 nbuf_varuint(b, (uint64_t)kc);
5047 0 nbuf_varuint(b, (uint64_t)kc);
5048 0 int *slots = path_slots + p*JSON_LEX_SLOTS;
5049 int s;
5050 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
5051 0 int k = slots[s];
5052 0 ▶ if (k < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) continue; 0 0 0
5053 0 const char *nm = json_kind_type_name[k];
5054 0 nbuf_string(b, nm, strlen(nm));
5055 }
5056 0 nbuf_le64(b, 0); /* Variant BASIC */
5057 }
5058  
5059 /* Typed-path column data — comes BEFORE dynamic-path Variant data
5060 * on the wire. Emitted in declaration (sorted) order via the
5061 * inner type's encoder. */
5062 0 ▶ if (n_typed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_typed > 0) { 0 0 0
5063 int tp;
5064 0 ▶ for (tp = 0; tp < n_typed; tp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp = 0; tp < n_typed; tp++) { 0 0 0
5065 0 int rc = encode_column_sv(aTHX_ b, typed_vals[tp],
5066 0 nrows, ct->inners[tp]);
5067 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
5068 }
5069 }
5070  
5071 /* Per-path Variant data */
5072 0 ▶ for (p = 0; p < num_paths; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < num_paths; p++) { 0 0 0
5073 0 int wire_slots = wire_slot_counts[p];
5074 0 int *slots = path_slots + p*JSON_LEX_SLOTS;
5075  
5076 /* Discriminator byte per row */
5077 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
5078 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
5079 0 SV **e = hv_fetch(row_hvs[i], pe[p].path, (I32)pe[p].len, 0);
5080 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
5081 0 int k = json_classify_value(aTHX_ *e);
5082 0 nbuf_u8(b, (uint8_t)json_kind_disc_in(k, slots, wire_slots));
5083 }
5084  
5085 /* Per-variant data in lex order. SharedVariant has zero rows. */
5086 int s;
5087 0 ▶ for (s = 0; s < wire_slots; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < wire_slots; s++) { 0 0 0
5088 0 int k_match = slots[s];
5089 0 ▶ if (k_match < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k_match < 0) continue; 0 0 0
5090 0 int is_array = (k_match >= JV_ARRAY_BOOL
5091 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
5092 0 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 0 0 0
5093 0 uint64_t offset = 0;
5094 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
5095 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
5096 0 SV **e = hv_fetch(row_hvs[i], pe[p].path,
5097 (I32)pe[p].len, 0);
5098 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
5099 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
5100 0 AV *av = (AV*)SvRV(*e);
5101 0 SSize_t n = av_len(av) + 1;
5102 0 offset += (uint64_t)n;
5103 0 nbuf_le64(b, offset);
5104 }
5105 }
5106 0 ▶ for (i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) { 0 0 0
5107 0 ▶ if (!row_hvs[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row_hvs[i]) continue; 0 0 0
5108 0 SV **e = hv_fetch(row_hvs[i], pe[p].path,
5109 (I32)pe[p].len, 0);
5110 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
5111 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
5112 0 ▶ if (is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array) { 0 0 0
5113 0 AV *av = (AV*)SvRV(*e);
5114 0 SSize_t n = av_len(av) + 1, j;
5115 0 int av_magical = SvRMAGICAL((SV*)av);
5116 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
5117 0 SV **elem = av_fetch(av, j, 0);
5118 0 ▶ SV *ev = (elem && *elem)
Branch: 1 branch
BranchTrueFalseTotal
SV *ev = (elem && *elem) 0 0 0
5119 0 ▶ ? *elem : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
? *elem : &PL_sv_undef; 0 0 0
5120 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
5121 0 ev = sv_2mortal(newSVsv(ev));
5122 0 ▶ if (!SvOK(ev)) ev = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ev)) ev = &PL_sv_undef; 0 0 0
5123 0 json_emit_array_elem(aTHX_ b, ev, k_match);
5124 }
5125 } else {
5126 0 json_emit_scalar(aTHX_ b, *e, k_match);
5127 }
5128 }
5129 }
5130 }
5131  
5132 /* Shared data trailer: N UInt64 LE zero offsets */
5133 0 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 0 0 0
5134 0 size_t nbytes = (size_t)nrows * 8;
5135 0 nbuf_grow(b, nbytes);
5136 0 memset(b->data + b->len, 0, nbytes);
5137 0 b->len += nbytes;
5138 }
5139 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
5140 0 return 1;
5141 }
5142 0 default:
5143 0 return 0;
5144 }
5145 }
5146  
5147 /* A filled Data block body as a CLIENT_DATA packet plus the empty trailing
5148 * block. Consumes body->data. Returns the malloc'd packet. */
5149 0 static char* wrap_data_block(ev_clickhouse_t *self, native_buf_t *body, size_t *out_len) {
5150 native_buf_t pkt;
5151  
5152 0 nbuf_init(&pkt);
5153 0 nbuf_varuint(&pkt, CLIENT_DATA);
5154 0 nbuf_cstring(&pkt, ""); /* table name — outside compression */
5155 0 nbuf_put_block(&pkt, body, self);
5156 0 nbuf_empty_data_block(&pkt, self);
5157  
5158 0 *out_len = pkt.len;
5159 0 return pkt.data;
5160 }
5161  
5162 /* --- Client-side defaults for empty values (empty_as_default/defaults) ---
5163 *
5164 * The server sends a TableColumns packet before the INSERT sample block;
5165 * its description text carries each table column's DEFAULT/MATERIALIZED/
5166 * ALIAS/EPHEMERAL kind and expression. Resolution runs once per insert at
5167 * sample-block time and yields one col_default_t per column; the build
5168 * functions below then only substitute the cell before the existing
5169 * encoders run.
5170 */
5171  
5172 typedef struct {
5173 char *name; size_t name_len;
5174 char kind; /* 0 none, D/M/A/E */
5175 char *expr; size_t expr_len;
5176 } tc_col_t;
5177  
5178 struct tc_cols_s {
5179 tc_col_t *cols;
5180 int ncols;
5181 int bad; /* the description did not parse completely */
5182 };
5183  
5184 /* One column's resolved default for a pending insert. */
5185 typedef struct {
5186 int subst; /* substitute empty cells in this column */
5187 int nullable; /* column top is Nullable (or LC(Nullable)) */
5188 int to_null; /* substitution value is NULL */
5189 SV *subst_sv; /* arrayref-path value, owned */
5190 char *subst_text; /* string-path cell, owned */
5191 size_t subst_text_len;
5192 char *err; /* why an empty value here cannot be filled, owned */
5193 } col_default_t;
5194  
5195 /* ClickHouse string unescape shared by the wire and literal levels:
5196 * \n \t \r \b \f \v \a \0 \e \xHH, anything else \X to X. */
5197 0 static int hex_val(char c) {
5198 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
5199 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
5200 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
5201 0 return -1;
5202 }
5203  
5204 0 static size_t ch_unescape(const char *src, size_t len, char *dst) {
5205 0 size_t i = 0, j = 0;
5206 int hi, lo;
5207 0 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 0 0 0
5208 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
5209 0 char c = src[i + 1];
5210 0 size_t adv = 2;
5211 0 switch (c) {
5212 0 case 'n': c = '\n'; break;
5213 0 case 't': c = '\t'; break;
5214 0 case 'r': c = '\r'; break;
5215 0 case 'b': c = '\b'; break;
5216 0 case 'f': c = '\f'; break;
5217 0 case 'v': c = '\v'; break;
5218 0 case 'a': c = '\a'; break;
5219 0 case '0': c = '\0'; break;
5220 0 case 'e': c = '\x1b'; break;
5221 0 case 'x':
5222 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
5223 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
5224 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
5225 0 c = (char)((hi << 4) | lo);
5226 0 adv = 4;
5227 }
5228 0 break;
5229 0 default: break;
5230 }
5231 0 dst[j++] = c;
5232 0 i += adv;
5233 } else {
5234 0 dst[j++] = src[i++];
5235 }
5236 }
5237 0 return j;
5238 }
5239  
5240 185 static void free_insert_tc(ev_clickhouse_t *self) {
5241 185 ▶ if (self->insert_tc) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_tc) { 0 185 185
5242 int i;
5243 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
5244 0 Safefree(self->insert_tc->cols[i].name);
5245 0 Safefree(self->insert_tc->cols[i].expr);
5246 }
5247 0 Safefree(self->insert_tc->cols);
5248 0 Safefree(self->insert_tc);
5249 0 self->insert_tc = NULL;
5250 }
5251 185 }
5252  
5253 185 static void free_insert_default_state(ev_clickhouse_t *self) {
5254 185 self->insert_empty_as_default = 0;
5255 185 ▶ CLEAR_SV(self->insert_defaults);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->insert_defaults); 0 185 185
5256 185 free_insert_tc(self);
5257 185 }
5258  
5259 24 static void free_col_defaults(col_default_t *cdefs, int ncols) {
5260 int i;
5261 24 ▶ if (!cdefs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cdefs) return; 24 0 24
5262 0 ▶ for (i = 0; i < ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ncols; i++) { 0 0 0
5263 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
5264 0 Safefree(cdefs[i].subst_text);
5265 0 Safefree(cdefs[i].err);
5266 }
5267 0 Safefree(cdefs);
5268 }
5269  
5270 /* Split one description line into tab fields; returns field count. */
5271 0 static int split_tabs(const char *line, size_t len,
5272 const char **f, size_t *fl, int maxf) {
5273 0 int n = 0;
5274 0 size_t start = 0, i;
5275 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
5276 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
5277 0 f[n] = line + start;
5278 0 fl[n] = i - start;
5279 0 n++;
5280 0 start = i + 1;
5281 }
5282 }
5283 0 return n;
5284 }
5285  
5286 /* Parse the TableColumns description text: a header line, the count
5287 * line, then one line per column. Anything short of that marks it bad,
5288 * so no column silently falls back to its type's zero. */
5289 0 static tc_cols_t *parse_table_columns_desc(const char *s, size_t len) {
5290 tc_cols_t *tc;
5291 0 const char *p = s, *end = s + len, *nl, *q;
5292 const char *f[8];
5293 size_t fl[8];
5294 0 int nf, n = 0, i;
5295 0 Newxz(tc, 1, tc_cols_t);
5296 0 tc->bad = 1;
5297 0 nl = memchr(p, '\n', (size_t)(end - p));
5298 0 ▶ if (!nl) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return tc; 0 0 0
5299 0 p = nl + 1;
5300 0 nl = memchr(p, '\n', (size_t)(end - p));
5301 0 ▶ if (!nl) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return tc; 0 0 0
5302 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
5303 0 ▶ if (n > 100000) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 100000) return tc; 0 0 0
5304 0 n = n * 10 + (*q - '0');
5305 }
5306 /* "N columns:" */
5307 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
5308 0 p = nl + 1;
5309 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
5310 0 tc->ncols = n;
5311 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
5312 const char *le;
5313 size_t j, k;
5314 char *un;
5315 0 ▶ if (p >= end) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return tc; 0 0 0
5316 0 nl = memchr(p, '\n', (size_t)(end - p));
5317 0 ▶ le = nl ? nl : end;
Branch: 1 branch
BranchTrueFalseTotal
le = nl ? nl : end; 0 0 0
5318 0 nf = split_tabs(p, (size_t)(le - p), f, fl, 8);
5319 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
5320 0 j = 1;
5321 0 ▶ while (j < fl[0]) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < fl[0]) { 0 0 0
5322 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
5323 0 ▶ if (f[0][j] == '`') break;
Branch: 1 branch
BranchTrueFalseTotal
if (f[0][j] == '`') break; 0 0 0
5324 0 j++;
5325 }
5326 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
5327 0 Newx(un, j, char);
5328 0 k = ch_unescape(f[0] + 1, j - 1, un);
5329 0 tc->cols[i].name = un;
5330 0 tc->cols[i].name_len = k;
5331 0 ▶ if (nf > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nf > 1) { 0 0 0
5332 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
5333 0 tc->cols[i].kind = 'D';
5334 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
5335 0 tc->cols[i].kind = 'M';
5336 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
5337 0 tc->cols[i].kind = 'A';
5338 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
5339 0 tc->cols[i].kind = 'E';
5340 0 ▶ if (tc->cols[i].kind) {
Branch: 1 branch
BranchTrueFalseTotal
if (tc->cols[i].kind) { 0 0 0
5341 0 ▶ if (nf < 3) return tc;
Branch: 1 branch
BranchTrueFalseTotal
if (nf < 3) return tc; 0 0 0
5342 0 Newx(un, fl[2] + 1, char);
5343 0 k = ch_unescape(f[2], fl[2], un);
5344 0 tc->cols[i].expr = un;
5345 0 tc->cols[i].expr_len = k;
5346 }
5347 }
5348 0 p = le + 1;
5349 }
5350 0 tc->bad = 0;
5351 0 return tc;
5352 }
5353  
5354 0 static const tc_col_t *find_tc_col(const tc_cols_t *tc,
5355 const char *name, size_t len) {
5356 int i;
5357 0 ▶ if (!tc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!tc) return NULL; 0 0 0
5358 0 ▶ for (i = 0; i < tc->ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc->ncols; i++) { 0 0 0
5359 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
5360 0 ▶ && memcmp(tc->cols[i].name, name, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(tc->cols[i].name, name, len) == 0) 0 0 0
5361 0 return &tc->cols[i];
5362 }
5363 0 return NULL;
5364 }
5365  
5366 /* Constant DEFAULT literals: number, string, NULL, true/false,
5367 * array/tuple of literals. Anything else is not a literal. */
5368 enum { L_NUM, L_STR, L_NULL, L_BOOL, L_ARR, L_TUP };
5369  
5370 typedef struct lit_s lit_t;
5371 struct lit_s {
5372 int kind;
5373 const char *ptr; size_t len;
5374 char *str; size_t str_len;
5375 int boolean;
5376 lit_t **items; size_t nitems; size_t nicap;
5377 col_type_t *cast; /* CAST / :: target type, NULL when none */
5378 };
5379  
5380 0 static void free_lit(lit_t *l) {
5381 0 ▶ if (!l) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return; 0 0 0
5382 0 ▶ if (l->cast) free_col_type(l->cast);
Branch: 1 branch
BranchTrueFalseTotal
if (l->cast) free_col_type(l->cast); 0 0 0
5383 0 ▶ if (l->kind == L_STR) Safefree(l->str);
Branch: 1 branch
BranchTrueFalseTotal
if (l->kind == L_STR) Safefree(l->str); 0 0 0
5384 0 ▶ if (l->items) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->items) { 0 0 0
5385 size_t i;
5386 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
5387 0 Safefree(l->items);
5388 }
5389 0 Safefree(l);
5390 }
5391  
5392 0 static const char *skip_ws(const char *p, const char *end) {
5393 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
5394 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
5395 0 p++;
5396 0 return p;
5397 }
5398  
5399 0 static int is_wordchar(char c) {
5400 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
5401 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
5402 }
5403  
5404 static lit_t *parse_lit(const char **pp, const char *end, int depth);
5405  
5406 0 static int lit_push(lit_t *l, lit_t *item) {
5407 0 ▶ if (l->nitems == l->nicap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->nitems == l->nicap) { 0 0 0
5408 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
5409 0 ▶ if (ncap > 100000) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ncap > 100000) return 0; 0 0 0
5410 0 ▶ Renew(l->items, ncap, lit_t *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(l->items, ncap, lit_t *); 0 0 0
5411 0 l->nicap = ncap;
5412 }
5413 0 l->items[l->nitems++] = item;
5414 0 return 1;
5415 }
5416  
5417 /* Past a type name ("Decimal(9, 2)", "DateTime('UTC')"), else NULL. */
5418 0 static const char *skip_type_name(const char *p, const char *end) {
5419 0 int depth = 0;
5420 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
5421 0 ▶ || *p == '_'))
Branch: 1 branch
BranchTrueFalseTotal
|| *p == '_')) 0 0 0
5422 0 return NULL;
5423 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
5424 0 p = skip_ws(p, end);
5425 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
5426 0 ▶ for (; p < end; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; p < end; p++) { 0 0 0
5427 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5428 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
5429 0 ▶ if (*p == '\\') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') p++; 0 0 0
5430 0 ▶ if (p >= end) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return NULL; 0 0 0
5431 0 ▶ } else if (*p == '(') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*p == '(') { 0 0 0
5432 0 depth++;
5433 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
5434 0 return p + 1;
5435 }
5436 }
5437 0 return NULL;
5438 }
5439  
5440 static lit_t *parse_lit_atom(const char **pp, const char *end, int depth);
5441  
5442 /* Record a cast on l; 0 when l already carries a different cast type
5443 * (a cast of a cast is not a client-side literal). */
5444 0 static int lit_set_cast(lit_t *l, const char *type, size_t len) {
5445 0 col_type_t *t = parse_col_type(type, len);
5446 0 ▶ if (l->cast) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->cast) { 0 0 0
5447 0 int same = col_type_same(l->cast, t);
5448 0 free_col_type(t);
5449 0 return same;
5450 }
5451 0 l->cast = t;
5452 0 return 1;
5453 }
5454  
5455 /* A literal, also in the constant forms ALTER writes: CAST(lit, 'Type'),
5456 * CAST(lit AS Type), lit::Type. The cast type is kept: the literal only
5457 * stands for the default when it matches the column (lit_casts_ok). */
5458 0 static lit_t *parse_lit(const char **pp, const char *end, int depth) {
5459 0 const char *p = skip_ws(*pp, end);
5460 lit_t *l;
5461 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
5462 {
5463 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
5464 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
5465 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
5466 0 ▶ if (q < end && *q == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == '(') { 0 0 0
if (q < end && *q == '(') { 0 0 0
5467 0 q++;
5468 0 l = parse_lit(&q, end, depth + 1);
5469 0 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 0 0
5470 0 q = skip_ws(q, end);
5471 0 ▶ if (q < end && *q == ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == ',') { 0 0 0
if (q < end && *q == ',') { 0 0 0
5472 lit_t *t;
5473 int ok;
5474 0 q++;
5475 0 t = parse_lit(&q, end, depth + 1);
5476 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
5477 0 ok = lit_set_cast(l, t->str, t->str_len);
5478 0 free_lit(t);
5479 0 ▶ if (!ok) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { free_lit(l); return NULL; } 0 0 0
5480 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
5481 0 ▶ && !is_wordchar(q[2])) {
Branch: 1 branch
BranchTrueFalseTotal
&& !is_wordchar(q[2])) { 0 0 0
5482 0 const char *ts = skip_ws(q + 2, end);
5483 0 q = skip_type_name(ts, end);
5484 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
5485 0 free_lit(l);
5486 0 return NULL;
5487 }
5488 } else {
5489 0 free_lit(l);
5490 0 return NULL;
5491 }
5492 0 q = skip_ws(q, end);
5493 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
5494 0 p = q + 1;
5495 0 goto suffix;
5496 }
5497 }
5498 0 l = parse_lit_atom(&p, end, depth);
5499 0 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 0 0
5500 0 suffix:
5501 0 for (;;) {
5502 0 const char *ts, *q = skip_ws(p, end);
5503 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
5504 0 ts = skip_ws(q + 2, end);
5505 0 q = skip_type_name(ts, end);
5506 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
5507 0 free_lit(l);
5508 0 return NULL;
5509 }
5510 0 p = q;
5511 }
5512 0 *pp = p;
5513 0 return l;
5514 }
5515  
5516 0 static lit_t *parse_lit_atom(const char **pp, const char *end, int depth) {
5517 0 const char *p = skip_ws(*pp, end);
5518 lit_t *l;
5519 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
5520 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5521 0 size_t cap = (size_t)(end - p), k = 0;
5522 char *buf;
5523 0 ▶ Newx(buf, cap ? cap : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(buf, cap ? cap : 1, char); 0 0 0
5524 0 p++;
5525 for (;;) {
5526 0 ▶ if (p >= end) { Safefree(buf); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) { Safefree(buf); return NULL; } 0 0 0
5527 0 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 0 0
5528 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
5529 0 buf[k++] = '\'';
5530 0 p += 2;
5531 0 continue;
5532 }
5533 0 p++;
5534 0 break;
5535 }
5536 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
5537 0 char c = p[1];
5538 0 size_t adv = 2;
5539 int hi, lo;
5540 0 switch (c) {
5541 0 case 'n': c = '\n'; break;
5542 0 case 't': c = '\t'; break;
5543 0 case 'r': c = '\r'; break;
5544 0 case 'b': c = '\b'; break;
5545 0 case 'f': c = '\f'; break;
5546 0 case 'v': c = '\v'; break;
5547 0 case 'a': c = '\a'; break;
5548 0 case '0': c = '\0'; break;
5549 0 case 'e': c = '\x1b'; break;
5550 0 case 'x':
5551 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
5552 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
5553 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
5554 0 c = (char)((hi << 4) | lo);
5555 0 adv = 4;
5556 }
5557 0 break;
5558 0 default: break;
5559 }
5560 0 buf[k++] = c;
5561 0 p += adv;
5562 0 continue;
5563 }
5564 0 buf[k++] = *p++;
5565 }
5566 0 Newxz(l, 1, lit_t);
5567 0 l->kind = L_STR;
5568 0 l->str = buf;
5569 0 l->str_len = k;
5570 0 *pp = p;
5571 0 return l;
5572 }
5573 0 ▶ if (*p == '[' || *p == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '[' || *p == '(') { 0 0 0
if (*p == '[' || *p == '(') { 0 0 0
5574 0 ▶ char open = *p, close = (open == '[') ? ']' : ')';
Branch: 1 branch
BranchTrueFalseTotal
char open = *p, close = (open == '[') ? ']' : ')'; 0 0 0
5575 0 int is_tup = (open == '(');
5576 0 lit_t *first = NULL;
5577 0 p++;
5578 0 Newxz(l, 1, lit_t);
5579 0 ▶ l->kind = is_tup ? L_TUP : L_ARR;
Branch: 1 branch
BranchTrueFalseTotal
l->kind = is_tup ? L_TUP : L_ARR; 0 0 0
5580 0 p = skip_ws(p, end);
5581 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
5582 0 p++;
5583 0 *pp = p;
5584 0 return l;
5585 }
5586 0 first = parse_lit(&p, end, depth + 1);
5587 0 ▶ if (!first) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!first) { free_lit(l); return NULL; } 0 0 0
5588 0 p = skip_ws(p, end);
5589 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
5590 0 p++;
5591 0 free_lit(l);
5592 0 *pp = p;
5593 0 return first;
5594 }
5595 0 ▶ if (p >= end || *p != ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != ',') { 0 0 0
if (p >= end || *p != ',') { 0 0 0
5596 0 free_lit(first);
5597 0 free_lit(l);
5598 0 return NULL;
5599 }
5600 0 ▶ if (!lit_push(l, first)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_push(l, first)) { 0 0 0
5601 0 free_lit(first);
5602 0 free_lit(l);
5603 0 return NULL;
5604 }
5605 0 for (;;) {
5606 lit_t *item;
5607 0 p++;
5608 0 item = parse_lit(&p, end, depth + 1);
5609 0 ▶ if (!item) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!item) { free_lit(l); return NULL; } 0 0 0
5610 0 ▶ if (!lit_push(l, item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_push(l, item)) { 0 0 0
5611 0 free_lit(item);
5612 0 free_lit(l);
5613 0 return NULL;
5614 }
5615 0 p = skip_ws(p, end);
5616 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
5617 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
5618 0 free_lit(l);
5619 0 return NULL;
5620 }
5621 0 *pp = p;
5622 0 return l;
5623 }
5624 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
5625 0 const char *start = p;
5626 0 int seendigit = 0;
5627 0 ▶ if (*p == '+' || *p == '-') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '+' || *p == '-') p++; 0 0 0
if (*p == '+' || *p == '-') p++; 0 0 0
5628 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
5629 0 p += 2;
5630 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
5631 0 ▶ if (!seendigit) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!seendigit) return NULL; 0 0 0
5632 } else {
5633 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
5634 0 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 0 0 0
if (p < end && *p == '.') { 0 0 0
5635 0 p++;
5636 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
5637 0 p++;
5638 0 seendigit = 1;
5639 }
5640 }
5641 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
5642 0 const char *e = p + 1;
5643 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
5644 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
5645 0 p = e + 1;
5646 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
5647 }
5648 }
5649 0 ▶ if (!seendigit) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!seendigit) return NULL; 0 0 0
5650 }
5651 0 Newxz(l, 1, lit_t);
5652 0 l->kind = L_NUM;
5653 0 l->ptr = start;
5654 0 l->len = (size_t)(p - start);
5655 0 *pp = p;
5656 0 return l;
5657 }
5658 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
5659 0 const char *w = p;
5660 size_t wl;
5661 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
5662 0 wl = (size_t)(p - w);
5663 0 Newxz(l, 1, lit_t);
5664 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
5665 0 l->kind = L_NULL;
5666 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
5667 0 l->kind = L_BOOL;
5668 0 l->boolean = 1;
5669 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
5670 0 l->kind = L_BOOL;
5671 0 l->boolean = 0;
5672 } else {
5673 0 Safefree(l);
5674 0 return NULL;
5675 }
5676 0 *pp = p;
5677 0 return l;
5678 }
5679 0 return NULL;
5680 }
5681  
5682 0 static const col_type_t *strip_lc(const col_type_t *t) {
5683 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
5684 0 return t;
5685 }
5686  
5687 /* A CAST to the column's own type (LowCardinality aside, Nullable on the
5688 * column side allowed) leaves the value to the column's encoder; any other
5689 * target type converts on the server in ways the client does not mimic. */
5690 0 static int cast_matches(const col_type_t *cast, const col_type_t *col) {
5691 0 cast = strip_lc(cast);
5692 0 col = strip_lc(col);
5693 0 ▶ if (col_type_same(cast, col)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (col_type_same(cast, col)) return 1; 0 0 0
5694 0 ▶ return col && col->code == CT_NULLABLE
Branch: 1 branch
BranchTrueFalseTotal
return col && col->code == CT_NULLABLE 0 0 0
5695 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
5696 }
5697  
5698 /* 1 when every cast in the literal targets the type at its position in
5699 * the column type. */
5700 0 static int lit_casts_ok(const lit_t *l, const col_type_t *ct) {
5701 0 const col_type_t *t = strip_lc(ct);
5702 size_t i;
5703 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
5704 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
5705 0 ▶ for (i = 0; i < l->nitems; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->nitems; i++) { 0 0 0
5706 0 const col_type_t *et = NULL;
5707 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
5708 0 et = t->inner;
5709 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
5710 0 ▶ && i < (size_t)t->num_inners)
Branch: 1 branch
BranchTrueFalseTotal
&& i < (size_t)t->num_inners) 0 0 0
5711 0 et = t->inners[i];
5712 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
5713 }
5714 0 return 1;
5715 }
5716  
5717 /* A whole expression is a literal only if one value spans it. */
5718 0 static lit_t *parse_default_literal(const char *s, size_t len) {
5719 0 const char *p = s, *end = s + len;
5720 0 lit_t *l = parse_lit(&p, end, 0);
5721 0 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 0 0 0
5722 0 p = skip_ws(p, end);
5723 0 ▶ if (p != end) { free_lit(l); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) { free_lit(l); return NULL; } 0 0 0
5724 0 return l;
5725 }
5726  
5727 /* Verbatim number text, hex normalized to decimal; NULL when the hex
5728 * value does not fit 64 bits. Caller frees. */
5729 0 static char *num_normalize(const char *p, size_t len, size_t *out_len) {
5730 0 const char *q = p;
5731 0 size_t qlen = len;
5732 char *out;
5733 0 int neg = 0;
5734 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
5735 0 neg = (*q == '-');
5736 0 q++;
5737 0 qlen--;
5738 }
5739 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
5740 0 unsigned long long v = 0;
5741 size_t i;
5742 0 ▶ for (i = 2; i < qlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < qlen; i++) { 0 0 0
5743 0 int d = hex_val(q[i]);
5744 0 ▶ if (d < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (d < 0) break; 0 0 0
5745 0 ▶ if (v > (ULLONG_MAX >> 4)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (ULLONG_MAX >> 4)) return NULL; 0 0 0
5746 0 v = v * 16 + (unsigned)d;
5747 }
5748 0 Newx(out, 32, char);
5749 0 ▶ if (neg)
Branch: 1 branch
BranchTrueFalseTotal
if (neg) 0 0 0
5750 0 *out_len = (size_t)snprintf(out, 32, "-%llu", v);
5751 else
5752 0 *out_len = (size_t)snprintf(out, 32, "%llu", v);
5753 0 return out;
5754 }
5755 0 Newx(out, len + 1, char);
5756 0 Copy(p, out, len, char);
5757 0 out[len] = '\0';
5758 0 *out_len = len;
5759 0 return out;
5760 }
5761  
5762 0 static int nullable_top(col_type_t *ct) {
5763 0 ▶ if (!ct) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) return 0; 0 0 0
5764 0 ▶ if (ct->code == CT_NULLABLE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_NULLABLE) return 1; 0 0 0
5765 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
5766 0 ▶ && ct->inner->code == CT_NULLABLE)
Branch: 1 branch
BranchTrueFalseTotal
&& ct->inner->code == CT_NULLABLE) 0 0 0
5767 0 return 1;
5768 0 return 0;
5769 }
5770  
5771 /* Arrayref-path conversion of a literal. Returns an owned SV, or NULL
5772 * for exotic nests the client cannot express. */
5773 0 static SV *lit_to_sv(pTHX_ lit_t *l) {
5774 size_t i;
5775 AV *av;
5776 SV *sv;
5777 char *norm;
5778 size_t nlen;
5779 0 switch (l->kind) {
5780 0 case L_NUM:
5781 0 norm = num_normalize(l->ptr, l->len, &nlen);
5782 0 ▶ if (!norm) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!norm) return NULL; 0 0 0
5783 0 sv = newSVpvn(norm, nlen);
5784 0 Safefree(norm);
5785 0 return sv;
5786 0 case L_STR:
5787 0 return newSVpvn(l->str, l->str_len);
5788 0 case L_NULL:
5789 0 return newSV(0);
5790 0 case L_BOOL:
5791 0 return newSViv(l->boolean);
5792 0 case L_ARR:
5793 case L_TUP:
5794 0 av = newAV();
5795 0 ▶ for (i = 0; i < l->nitems; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->nitems; i++) { 0 0 0
5796 0 SV *e = lit_to_sv(aTHX_ l->items[i]);
5797 0 ▶ if (!e) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { 0 0 0
5798 0 SvREFCNT_dec((SV *)av);
5799 0 return NULL;
5800 }
5801 0 av_store(av, (SSize_t)i, e);
5802 }
5803 0 return newRV_noinc((SV *)av);
5804 }
5805 0 return NULL;
5806 }
5807  
5808 /* String-path conversion: appends the TSV cell. 0 when the value has
5809 * no text form (nested types need binary encoding). */
5810 0 static int lit_to_text(lit_t *l, native_buf_t *b) {
5811 char *norm;
5812 size_t nlen;
5813 0 switch (l->kind) {
5814 0 case L_NUM:
5815 0 norm = num_normalize(l->ptr, l->len, &nlen);
5816 0 ▶ if (!norm) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!norm) return 0; 0 0 0
5817 0 nbuf_append(b, norm, nlen);
5818 0 Safefree(norm);
5819 0 return 1;
5820 0 case L_STR:
5821 0 tsv_escape(b, l->str, l->str_len);
5822 0 return 1;
5823 0 case L_NULL:
5824 0 nbuf_append(b, "\\N", 2);
5825 0 return 1;
5826 0 case L_BOOL:
5827 0 ▶ nbuf_append(b, l->boolean ? "1" : "0", 1);
Branch: 1 branch
BranchTrueFalseTotal
nbuf_append(b, l->boolean ? "1" : "0", 1); 0 0 0
5828 0 return 1;
5829 0 default:
5830 0 return 0;
5831 }
5832 }
5833  
5834 /* Zero value for a column with no DEFAULT. Returns an owned SV, sets
5835 * *impossible when the type has no client-side zero. to_null handled
5836 * by the caller. */
5837 0 static SV *zero_sv_for(pTHX_ col_type_t *ct, int *impossible) {
5838 size_t i;
5839 AV *av;
5840 SV *sv;
5841 0 *impossible = 0;
5842 0 switch (ct->code) {
5843 0 case CT_ARRAY:
5844 0 return newRV_noinc((SV *)newAV());
5845 0 case CT_MAP:
5846 0 return newRV_noinc((SV *)newHV());
5847 0 case CT_TUPLE:
5848 0 av = newAV();
5849 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
5850 0 col_type_t *in = ct->inners[i];
5851 0 ▶ if (!in) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in) { 0 0 0
5852 0 SvREFCNT_dec((SV *)av);
5853 0 *impossible = 1;
5854 0 return NULL;
5855 }
5856 0 ▶ if (in->code == CT_NULLABLE
Branch: 1 branch
BranchTrueFalseTotal
if (in->code == CT_NULLABLE 0 0 0
5857 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
5858 0 ▶ && in->inner->code == CT_NULLABLE)) {
Branch: 1 branch
BranchTrueFalseTotal
&& in->inner->code == CT_NULLABLE)) { 0 0 0
5859 0 av_store(av, (SSize_t)i, newSV(0));
5860 0 continue;
5861 }
5862 0 sv = zero_sv_for(aTHX_ in, impossible);
5863 0 ▶ if (!sv || *impossible) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || *impossible) { 0 0 0
if (!sv || *impossible) { 0 0 0
5864 0 ▶ if (sv) SvREFCNT_dec(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (sv) SvREFCNT_dec(sv); 0 0 0
5865 0 SvREFCNT_dec((SV *)av);
5866 0 *impossible = 1;
5867 0 return NULL;
5868 }
5869 0 av_store(av, (SSize_t)i, sv);
5870 }
5871 0 return newRV_noinc((SV *)av);
5872 0 case CT_LOWCARDINALITY:
5873 0 ▶ if (!ct->inner) { *impossible = 1; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ct->inner) { *impossible = 1; return NULL; } 0 0 0
5874 0 return zero_sv_for(aTHX_ ct->inner, impossible);
5875 0 case CT_JSON:
5876 0 return newRV_noinc((SV *)newHV());
5877 0 default:
5878 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
5879 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
5880 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
5881 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
5882 0 *impossible = 1;
5883 0 return NULL;
5884 }
5885 0 return nullable_inner_zero_sv(aTHX_ ct);
5886 }
5887 }
5888  
5889 /* Zero value as a string-path cell. 1 with *out malloc'd, 0 when the
5890 * type has no text form. to_null handled by the caller. */
5891 0 static int zero_text_for(col_type_t *ct, char **out, size_t *out_len) {
5892 const char *p;
5893 size_t len;
5894 0 *out = NULL;
5895 0 *out_len = 0;
5896 0 switch (ct->code) {
5897 0 case CT_ARRAY:
5898 case CT_MAP:
5899 case CT_TUPLE:
5900 0 return 0;
5901 0 case CT_LOWCARDINALITY:
5902 0 ▶ if (!ct->inner) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct->inner) return 0; 0 0 0
5903 0 return zero_text_for(ct->inner, out, out_len);
5904 0 case CT_JSON:
5905 case CT_UNKNOWN:
5906 case CT_NOTHING:
5907 case CT_VARIANT:
5908 case CT_DYNAMIC:
5909 case CT_INT128:
5910 case CT_UINT128:
5911 case CT_INT256:
5912 case CT_UINT256:
5913 0 return 0;
5914 0 default:
5915 0 nullable_inner_zero(ct, &p, &len);
5916 0 Newx(*out, len + 1, char);
5917 0 Copy(p, *out, len, char);
5918 0 (*out)[len] = '\0';
5919 0 *out_len = len;
5920 0 return 1;
5921 }
5922 }
5923  
5924 /* Resolve one column's default from a caller value. */
5925 0 static void resolve_from_sv(col_default_t *cd, SV *v) {
5926 0 ▶ if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { 0 0 0
5927 0 cd->to_null = 1;
5928 0 return;
5929 }
5930 0 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 0 0 0
5931 0 cd->subst_sv = SvREFCNT_inc(v);
5932 0 return;
5933 }
5934 0 cd->subst_sv = SvREFCNT_inc(v);
5935 {
5936 STRLEN l;
5937 0 const char *s = SvPV(cd->subst_sv, l);
5938 native_buf_t b;
5939 0 nbuf_init(&b);
5940 0 tsv_escape(&b, s, l);
5941 0 cd->subst_text = b.data;
5942 0 cd->subst_text_len = b.len;
5943 }
5944 }
5945  
5946 /* Resolve one column from a DEFAULT literal. 1 ok, 0 failure. */
5947 0 static int resolve_from_lit(pTHX_ col_default_t *cd, lit_t *l) {
5948 native_buf_t b;
5949 0 ▶ if (l->kind == L_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->kind == L_NULL) { 0 0 0
5950 0 cd->to_null = 1;
5951 0 return 1;
5952 }
5953 0 cd->subst_sv = lit_to_sv(aTHX_ l);
5954 0 ▶ if (!cd->subst_sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!cd->subst_sv) return 0; 0 0 0
5955 0 nbuf_init(&b);
5956 0 ▶ if (lit_to_text(l, &b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lit_to_text(l, &b)) { 0 0 0
5957 0 cd->subst_text = b.data;
5958 0 cd->subst_text_len = b.len;
5959 } else {
5960 0 Safefree(b.data);
5961 }
5962 0 return 1;
5963 }
5964  
5965 /* Message helpers: short errors naming the column and the fix. */
5966 0 static void resolve_err_trunc(const char *src, size_t len,
5967 char *dst, size_t dstsz) {
5968 0 size_t n = len < dstsz - 1 ? len : dstsz - 1;
5969 size_t i;
5970 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
5971 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
5972 0 dst[n] = '\0';
5973 0 }
5974  
5975 /* Resolve every column's default once per insert. NULL on failure
5976 * with the reason in errbuf. */
5977 0 static col_default_t *resolve_insert_defaults(pTHX_ ev_clickhouse_t *self,
5978 const char **cnames, size_t *cname_lens, col_type_t **ctypes,
5979 const char **ctypes_str, size_t *ctype_lens, int num_cols,
5980 char *errbuf, size_t errbuf_sz) {
5981 col_default_t *cdefs;
5982 int i;
5983 0 Newxz(cdefs, num_cols, col_default_t);
5984 0 ▶ for (i = 0; i < num_cols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_cols; i++) { 0 0 0
5985 0 col_default_t *cd = &cdefs[i];
5986 0 SV **dp = NULL;
5987 const tc_col_t *tc;
5988 lit_t *lit;
5989 char expr_tr[72];
5990 0 ▶ if (self->insert_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_defaults) { 0 0 0
5991 0 dp = hv_fetch(self->insert_defaults, cnames[i],
5992 (I32)cname_lens[i], 0);
5993 }
5994 0 ▶ if (dp && *dp) {
Branch: 2 branches
BranchTrueFalseTotal
if (dp && *dp) { 0 0 0
if (dp && *dp) { 0 0 0
5995 0 cd->subst = 1;
5996 0 cd->nullable = nullable_top(ctypes[i]);
5997 0 resolve_from_sv(cd, *dp);
5998 0 continue;
5999 }
6000 0 ▶ if (!self->insert_empty_as_default) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->insert_empty_as_default) continue; 0 0 0
6001 0 cd->subst = 1;
6002 0 cd->nullable = nullable_top(ctypes[i]);
6003 0 ▶ if (!self->insert_tc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->insert_tc) { 0 0 0
6004 0 snprintf(errbuf, errbuf_sz, "server did not send column "
6005 "defaults for this insert: use data_format => "
6006 "'Native' for server-side defaults");
6007 0 goto col_fail;
6008 }
6009 0 ▶ if (self->insert_tc->bad) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_tc->bad) { 0 0 0
6010 0 snprintf(errbuf, errbuf_sz, "cannot parse the server's column "
6011 "description: use data_format => 'Native' for "
6012 "server-side defaults");
6013 0 goto col_fail;
6014 }
6015 0 tc = find_tc_col(self->insert_tc, cnames[i], cname_lens[i]);
6016 0 ▶ if (!tc || !tc->kind) {
Branch: 2 branches
BranchTrueFalseTotal
if (!tc || !tc->kind) { 0 0 0
if (!tc || !tc->kind) { 0 0 0
6017 0 int imp = 0;
6018 0 char *t = NULL;
6019 0 size_t tl = 0;
6020 0 ▶ if (cd->nullable) {
Branch: 1 branch
BranchTrueFalseTotal
if (cd->nullable) { 0 0 0
6021 0 cd->to_null = 1;
6022 0 continue;
6023 }
6024 0 cd->subst_sv = zero_sv_for(aTHX_ ctypes[i], &imp);
6025 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
6026 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
6027 0 cd->subst_sv = NULL;
6028 0 goto no_client_default;
6029 }
6030 0 ▶ if (zero_text_for(ctypes[i], &t, &tl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (zero_text_for(ctypes[i], &t, &tl)) { 0 0 0
6031 0 cd->subst_text = t;
6032 0 cd->subst_text_len = tl;
6033 }
6034 0 continue;
6035 }
6036 0 ▶ if (tc->kind != 'D') {
Branch: 1 branch
BranchTrueFalseTotal
if (tc->kind != 'D') { 0 0 0
6037 0 const char *kw = (tc->kind == 'M') ? "MATERIALIZED"
6038 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
6039 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
6040 0 resolve_err_trunc(tc->expr, tc->expr_len, expr_tr,
6041 sizeof(expr_tr));
6042 0 snprintf(errbuf, errbuf_sz, "%s '%s': use data_format => "
6043 "'Native' for server-side defaults", kw, expr_tr);
6044 } else {
6045 0 snprintf(errbuf, errbuf_sz, "%s column: use data_format => "
6046 "'Native' for server-side defaults", kw);
6047 }
6048 0 goto col_fail;
6049 }
6050 0 ▶ lit = parse_default_literal(tc->expr ? tc->expr : "",
Branch: 1 branch
BranchTrueFalseTotal
lit = parse_default_literal(tc->expr ? tc->expr : "", 0 0 0
6051 0 ▶ tc->expr ? tc->expr_len : 0);
Branch: 1 branch
BranchTrueFalseTotal
tc->expr ? tc->expr_len : 0); 0 0 0
6052 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
6053 0 free_lit(lit);
6054 0 lit = NULL;
6055 }
6056 0 ▶ if (!lit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lit) { 0 0 0
6057 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
6058 0 resolve_err_trunc(tc->expr, tc->expr_len, expr_tr,
6059 sizeof(expr_tr));
6060 else
6061 0 expr_tr[0] = '\0';
6062 0 snprintf(errbuf, errbuf_sz, "non-literal DEFAULT '%s': use "
6063 "data_format => 'Native' for server-side defaults",
6064 expr_tr);
6065 0 goto col_fail;
6066 }
6067 0 ▶ if (!resolve_from_lit(aTHX_ cd, lit)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!resolve_from_lit(aTHX_ cd, lit)) { 0 0 0
6068 0 free_lit(lit);
6069 0 goto no_client_default;
6070 }
6071 0 free_lit(lit);
6072 0 continue;
6073 0 no_client_default:
6074 0 snprintf(errbuf, errbuf_sz, "no client-side default for empty "
6075 "values: use data_format => 'Native'");
6076 0 col_fail:
6077 /* Fails the insert only when a value needs the substitution. */
6078 {
6079 0 size_t n = strlen(errbuf);
6080 0 Newx(cd->err, n + 1, char);
6081 0 Copy(errbuf, cd->err, n + 1, char);
6082 0 errbuf[0] = '\0';
6083 }
6084 }
6085 0 ▶ if (self->insert_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_defaults) { 0 0 0
6086 HE *he;
6087 0 hv_iterinit(self->insert_defaults);
6088 0 ▶ while ((he = hv_iternext(self->insert_defaults))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(self->insert_defaults))) { 0 0 0
6089 I32 klen;
6090 0 char *key = hv_iterkey(he, &klen);
6091 0 int j, found = 0;
6092 0 ▶ for (j = 0; j < num_cols; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_cols; j++) { 0 0 0
6093 0 ▶ if (cname_lens[j] == (size_t)klen
Branch: 1 branch
BranchTrueFalseTotal
if (cname_lens[j] == (size_t)klen 0 0 0
6094 0 ▶ && memcmp(cnames[j], key, (size_t)klen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(cnames[j], key, (size_t)klen) == 0) { 0 0 0
6095 0 found = 1;
6096 0 break;
6097 }
6098 }
6099 0 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 0 0 0
6100 char k_tr[64];
6101 0 resolve_err_trunc(key, (size_t)klen, k_tr,
6102 sizeof(k_tr));
6103 0 snprintf(errbuf, errbuf_sz, "defaults key '%s' matches "
6104 "no insert column", k_tr);
6105 0 goto fail;
6106 }
6107 }
6108 }
6109 0 return cdefs;
6110 0 fail:
6111 0 free_col_defaults(cdefs, num_cols);
6112 0 return NULL;
6113 }
6114  
6115 /* --- Insert encoding error context --- */
6116  
6117 /* The column and rows of the encode call in progress, so a failure or an
6118 * exception can be traced back to its row. */
6119 typedef struct {
6120 int col; /* -1 outside a column */
6121 SSize_t r0, n;
6122 } enc_progress_t;
6123  
6124 #define ERR_VALUE_MAX 40
6125 #define ERR_NAME_MAX 60
6126  
6127 8 static void err_quote(native_buf_t *b, const char *s, size_t len) {
6128 8 size_t i, n = len > ERR_VALUE_MAX ? ERR_VALUE_MAX : len;
6129 8 nbuf_u8(b, '\'');
6130 124 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 116 8 124
6131 116 unsigned char c = (unsigned char)s[i];
6132 char esc[8];
6133 116 switch (c) {
6134 0 case '\'': nbuf_append(b, "\\'", 2); break;
6135 0 case '\\': nbuf_append(b, "\\\\", 2); break;
6136 0 case '\n': nbuf_append(b, "\\n", 2); break;
6137 0 case '\t': nbuf_append(b, "\\t", 2); break;
6138 0 case '\r': nbuf_append(b, "\\r", 2); break;
6139 116 default:
6140 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
6141 0 snprintf(esc, sizeof(esc), "\\x%02x", c);
6142 0 nbuf_append(b, esc, 4);
6143 } else {
6144 116 nbuf_u8(b, c);
6145 }
6146 }
6147 }
6148 8 nbuf_u8(b, '\'');
6149 8 ▶ if (n < len) nbuf_append(b, "...", 3);
Branch: 1 branch
BranchTrueFalseTotal
if (n < len) nbuf_append(b, "...", 3); 0 8 8
6150 8 }
6151  
6152 40 static void err_append(native_buf_t *b, const char *s) {
6153 40 nbuf_append(b, s, strlen(s));
6154 40 }
6155  
6156 0 static void err_zone(native_buf_t *b, const char *tz) {
6157 0 err_append(b, ": unknown timezone '");
6158 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
6159 0 err_append(b, "' or missing timezone database");
6160 0 }
6161  
6162 /* A value as error text, without running overloads: undef, the type of a
6163 * reference, else the quoted (escaped, truncated) string. */
6164 8 static void err_value_sv(pTHX_ native_buf_t *b, SV *v) {
6165 8 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 0 8 8
6166 0 SV *rv = SvRV(v);
6167 0 err_append(b, sv_reftype(rv, SvOBJECT(rv)));
6168 0 ▶ err_append(b, SvOBJECT(rv) ? " object" : " reference");
Branch: 1 branch
BranchTrueFalseTotal
err_append(b, SvOBJECT(rv) ? " object" : " reference"); 0 0 0
6169 8 ▶ } else if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!SvOK(v)) { 0 8 8
6170 0 err_append(b, "undef");
6171 } else {
6172 STRLEN l;
6173 8 const char *s = SvPV_nomg(v, l);
6174 8 err_quote(b, s, l);
6175 }
6176 8 }
6177  
6178 16 static void err_name(native_buf_t *b, const char *s, size_t len) {
6179 16 nbuf_append(b, s, len > ERR_NAME_MAX ? ERR_NAME_MAX : len);
6180 16 ▶ if (len > ERR_NAME_MAX) err_append(b, "...");
Branch: 1 branch
BranchTrueFalseTotal
if (len > ERR_NAME_MAX) err_append(b, "..."); 0 16 16
6181 16 }
6182  
6183 /* "column 'name' (type)" */
6184 8 static void err_where(char *dst, size_t sz, const char *name, size_t name_len,
6185 const char *type, size_t type_len) {
6186 native_buf_t b;
6187 8 nbuf_init(&b);
6188 8 err_append(&b, "column '");
6189 8 err_name(&b, name, name_len);
6190 8 err_append(&b, "' (");
6191 8 err_name(&b, type, type_len);
6192 8 err_append(&b, ")");
6193 8 snprintf(dst, sz, "%.*s", (int)b.len, b.data);
6194 8 Safefree(b.data);
6195 8 }
6196  
6197 8 static void err_finish(char *errbuf, size_t sz, native_buf_t *msg) {
6198 8 snprintf(errbuf, sz, "%.*s", (int)msg->len, msg->data);
6199 8 Safefree(msg->data);
6200 8 }
6201  
6202 /* Exception text on one line, the die location kept. */
6203 0 static void err_exception(pTHX_ native_buf_t *b, SV *err) {
6204 STRLEN l, i;
6205 const char *s;
6206 0 ▶ if (!err) { err_append(b, "died"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!err) { err_append(b, "died"); return; } 0 0 0
6207 0 ▶ if (SvROK(err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(err)) { 0 0 0
6208 0 err_append(b, "died with ");
6209 0 err_value_sv(aTHX_ b, err);
6210 0 return;
6211 }
6212 0 s = SvPV_nomg(err, l);
6213 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
6214 0 ▶ if (l > 160) l = 160;
Branch: 1 branch
BranchTrueFalseTotal
if (l > 160) l = 160; 0 0 0
6215 0 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 0 0 0
6216 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
6217 }
6218  
6219 8 static int sv_encodes(pTHX_ SV *v, col_type_t *ct, native_buf_t *scratch) {
6220 8 scratch->len = 0;
6221 8 return encode_column_sv(aTHX_ scratch, &v, 1, ct);
6222 }
6223  
6224 /* Inside a top-level value that does not encode, the innermost element
6225 * that does not encode on its own (the value itself when none). */
6226 8 static SV *locate_bad_sv(pTHX_ SV *v, col_type_t *ct, native_buf_t *scratch) {
6227 SSize_t j, n;
6228 8 switch (ct->code) {
6229 0 case CT_NULLABLE:
6230 case CT_LOWCARDINALITY:
6231 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
6232 0 case CT_ARRAY:
6233 case CT_TUPLE:
6234 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
6235 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
6236 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
6237 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
6238 0 SV **e = av_fetch((AV *)SvRV(v), j, 0);
6239 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
6240 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
6241 0 return locate_bad_sv(aTHX_ ev, et, scratch);
6242 }
6243 0 return v;
6244 0 case CT_MAP: {
6245 HV *hv;
6246 HE *he;
6247 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
6248 0 return v;
6249 0 hv = (HV *)SvRV(v);
6250 0 hv_iterinit(hv);
6251 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
6252 0 SV *k = hv_iterkeysv(he);
6253 0 SV *val = hv_iterval(hv, he);
6254 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
6255 0 return k;
6256 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
6257 0 return locate_bad_sv(aTHX_ val, ct->inners[1], scratch);
6258 }
6259 0 return v;
6260 }
6261 8 default:
6262 8 return v;
6263 }
6264 }
6265  
6266 /* Why encoding vals[0..n) as ct failed: the type cannot be encoded at
6267 * all, else the first value that fails on its own (row base + index),
6268 * with the failing element of a nested value. Re-encodes only on this
6269 * error path. */
6270 8 static void describe_sv_failure(pTHX_ char *errbuf, size_t sz,
6271 const char *where, col_type_t *ct,
6272 SV **vals, SSize_t n, SSize_t base,
6273 const char *unit) {
6274 native_buf_t msg, scratch;
6275 SSize_t k;
6276 8 nbuf_init(&msg);
6277 8 nbuf_init(&scratch);
6278 8 err_append(&msg, where);
6279 8 ▶ if (col_type_zone_error(ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_type_zone_error(ct)) { 0 8 8
6280 0 err_zone(&msg, col_type_zone_error(ct));
6281 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
6282 0 err_append(&msg, ": unsupported type");
6283 } else {
6284 8 ▶ for (k = 0; k < n; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) 8 0 8
6285 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
6286 8 ▶ if (k < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (k < n) { 8 0 8
6287 char rb[64];
6288 8 SV *bad = locate_bad_sv(aTHX_ vals[k], ct, &scratch);
6289 8 snprintf(rb, sizeof(rb), ", %s %ld: invalid %s ", unit,
6290 8 ▶ (long)(base + k), bad == vals[k] ? "value" : "element");
Branch: 1 branch
BranchTrueFalseTotal
(long)(base + k), bad == vals[k] ? "value" : "element"); 8 0 8
6291 8 err_append(&msg, rb);
6292 8 err_value_sv(aTHX_ &msg, bad);
6293 } else {
6294 0 err_append(&msg, ": invalid value");
6295 }
6296 }
6297 8 Safefree(scratch.data);
6298 8 err_finish(errbuf, sz, &msg);
6299 8 }
6300  
6301 /* describe_sv_failure() for the TabSeparated path; rows are lines. */
6302 0 static void describe_text_failure(char *errbuf, size_t sz, const char *where,
6303 col_type_t *ct, const char **vals,
6304 size_t *lens, SSize_t n) {
6305 native_buf_t msg, scratch;
6306 SSize_t k;
6307 0 nbuf_init(&msg);
6308 0 nbuf_init(&scratch);
6309 0 err_append(&msg, where);
6310 0 ▶ if (col_type_zone_error(ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_type_zone_error(ct)) { 0 0 0
6311 0 err_zone(&msg, col_type_zone_error(ct));
6312 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
6313 0 err_append(&msg, ": unsupported type");
6314 } else {
6315 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6316 0 scratch.len = 0;
6317 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
6318 }
6319 0 ▶ if (k < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (k < n) { 0 0 0
6320 char rb[48];
6321 0 snprintf(rb, sizeof(rb), ", line %ld: invalid value ", (long)(k + 1));
6322 0 err_append(&msg, rb);
6323 0 err_quote(&msg, vals[k], lens[k]);
6324 } else {
6325 0 err_append(&msg, ": invalid value");
6326 }
6327 }
6328 0 Safefree(scratch.data);
6329 0 err_finish(errbuf, sz, &msg);
6330 0 }
6331  
6332 /* Exceptions from user code (overloads, tied values) during the deferred
6333 * native encode must not escape the I/O callback: run_guarded() calls a
6334 * C function inside an eval through a private XSUB. */
6335 typedef struct {
6336 void (*fn)(pTHX_ void *);
6337 void *arg;
6338 } ch_guarded_t;
6339  
6340 static CV *ch_guard_cv;
6341  
6342 12 XS_INTERNAL(ch_guard_xs) {
6343 12 dXSARGS;
6344 ch_guarded_t *g;
6345 12 ▶ if (items != 1) croak_xs_usage(cv, "guard");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak_xs_usage(cv, "guard"); 0 12 12
6346 12 g = INT2PTR(ch_guarded_t *, SvIV(ST(0)));
6347 12 g->fn(aTHX_ g->arg);
6348 12 XSRETURN_EMPTY;
6349 }
6350  
6351 /* 1 when fn returned, 0 when it died (exception copied to *err when err
6352 * is given); $@ is left as it was. */
6353 12 static int run_guarded(pTHX_ void (*fn)(pTHX_ void *), void *arg, SV **err) {
6354 12 dSP;
6355 ch_guarded_t g;
6356 int died;
6357 12 g.fn = fn;
6358 12 g.arg = arg;
6359 12 ▶ if (err) *err = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = NULL; 12 0 12
6360 12 ENTER; SAVETMPS;
6361 12 save_scalar(PL_errgv);
6362 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
6363 12 ▶ mXPUSHi(PTR2IV(&g));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(PTR2IV(&g)); 0 12 12
6364 12 PUTBACK;
6365 12 call_sv((SV *)ch_guard_cv, G_EVAL | G_DISCARD);
6366 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
6367 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
6368 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 12 0 12
6369 12 return !died;
6370 }
6371  
6372 typedef struct {
6373 ev_clickhouse_t *self;
6374 const char **names; size_t *name_lens;
6375 col_type_t **types;
6376 const char **types_str; size_t *type_lens;
6377 int ncols;
6378 char *errbuf; size_t errbuf_sz;
6379 col_default_t *cdefs;
6380 } resolve_args_t;
6381  
6382 0 static void resolve_fn(pTHX_ void *p) {
6383 0 resolve_args_t *a = (resolve_args_t *)p;
6384 0 a->cdefs = resolve_insert_defaults(aTHX_ a->self, a->names, a->name_lens,
6385 a->types, a->types_str, a->type_lens, a->ncols, a->errbuf,
6386 a->errbuf_sz);
6387 0 }
6388  
6389 /* resolve_insert_defaults(): a caller's default value may be an object
6390 * whose stringification dies. */
6391 0 static col_default_t *resolve_insert_defaults_guarded(pTHX_ ev_clickhouse_t *self,
6392 const char **cnames, size_t *cname_lens, col_type_t **ctypes,
6393 const char **ctypes_str, size_t *ctype_lens, int num_cols,
6394 char *errbuf, size_t errbuf_sz) {
6395 resolve_args_t a;
6396 0 SV *err = NULL;
6397 0 a.self = self; a.names = cnames; a.name_lens = cname_lens;
6398 0 a.types = ctypes; a.types_str = ctypes_str; a.type_lens = ctype_lens;
6399 0 a.ncols = num_cols; a.errbuf = errbuf; a.errbuf_sz = errbuf_sz;
6400 0 a.cdefs = NULL;
6401 0 ▶ if (!run_guarded(aTHX_ resolve_fn, &a, &err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!run_guarded(aTHX_ resolve_fn, &a, &err)) { 0 0 0
6402 native_buf_t msg;
6403 0 nbuf_init(&msg);
6404 0 err_append(&msg, "defaults: ");
6405 0 err_exception(aTHX_ &msg, err);
6406 0 ▶ if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) SvREFCNT_dec(err); 0 0 0
6407 0 err_finish(errbuf, errbuf_sz, &msg);
6408 0 return NULL;
6409 }
6410 0 return a.cdefs;
6411 }
6412  
6413 /* Skip one column's data (prefixes already consumed). 1 = ok, 0 = malformed,
6414 * -1 = a type this walker does not know (caller decodes instead). */
6415 0 static int skip_column_data(const char *buf, size_t len, size_t *pos,
6416 uint64_t nrows, col_type_t *ct) {
6417 size_t fsz;
6418 uint64_t i;
6419 int rc;
6420 0 ▶ if (!ct) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) return -1; 0 0 0
6421 0 ▶ if (*pos > len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*pos > len) return 0; 0 0 0
6422 0 fsz = col_type_fixed_size(ct);
6423 0 ▶ if (fsz) {
Branch: 1 branch
BranchTrueFalseTotal
if (fsz) { 0 0 0
6424 0 ▶ if (nrows > (len - *pos) / fsz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > (len - *pos) / fsz) return 0; 0 0 0
6425 0 *pos += nrows * fsz;
6426 0 return 1;
6427 }
6428 0 switch (ct->code) {
6429 0 case CT_STRING:
6430 0 ▶ for (i = 0; i < nrows; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nrows; i++) 0 0 0
6431 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
6432 0 return 1;
6433 0 case CT_NOTHING:
6434 0 ▶ if (nrows > len - *pos) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > len - *pos) return 0; 0 0 0
6435 0 *pos += nrows;
6436 0 return 1;
6437 0 case CT_NULLABLE:
6438 0 ▶ if (nrows > len - *pos) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > len - *pos) return 0; 0 0 0
6439 0 *pos += nrows;
6440 0 return skip_column_data(buf, len, pos, nrows, ct->inner);
6441 0 case CT_ARRAY:
6442 case CT_MAP: {
6443 0 uint64_t total = 0;
6444 0 ▶ if (nrows > (len - *pos) / 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > (len - *pos) / 8) return 0; 0 0 0
6445 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
6446 0 *pos += nrows * 8;
6447 0 ▶ if (ct->code == CT_ARRAY)
Branch: 1 branch
BranchTrueFalseTotal
if (ct->code == CT_ARRAY) 0 0 0
6448 0 return skip_column_data(buf, len, pos, total, ct->inner);
6449 0 ▶ if (ct->num_inners != 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ct->num_inners != 2) return -1; 0 0 0
6450 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
6451 0 return rc;
6452 0 return skip_column_data(buf, len, pos, total, ct->inners[1]);
6453 }
6454 0 case CT_TUPLE:
6455 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
6456 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
6457 0 return rc;
6458 0 return 1;
6459 0 case CT_LOWCARDINALITY: {
6460 uint64_t ser_type, nkeys, nidx;
6461 size_t idx_size;
6462 col_type_t *dict_ct;
6463 0 ▶ if (!nrows) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!nrows) return 1; 0 0 0
6464 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
6465 0 memcpy(&ser_type, buf + *pos, 8);
6466 0 *pos += 8;
6467 /* One keys block follows HasAdditionalKeys and/or NeedUpdateDictionary. */
6468 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
6469 0 return 0;
6470 0 idx_size = (size_t)1 << (ser_type & 0xFF);
6471 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
6472 0 ▶ if (len - *pos < 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len - *pos < 8) return 0; 0 0 0
6473 0 memcpy(&nkeys, buf + *pos, 8);
6474 0 *pos += 8;
6475 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
6476 0 ▶ if (len - *pos < 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len - *pos < 8) return 0; 0 0 0
6477 0 memcpy(&nidx, buf + *pos, 8);
6478 0 *pos += 8;
6479 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
6480 0 *pos += nidx * idx_size;
6481 0 return 1;
6482 }
6483 0 default:
6484 0 return -1;
6485 }
6486 }
6487  
6488 /*
6489 * Turn a Native-format block (as ClickHouse::Encoder or `format Native`
6490 * writes it) into a protocol Data packet: BlockInfo first, and the
6491 * custom-serialization byte after each column type. Returns NULL with
6492 * errbuf set on a malformed block.
6493 */
6494 /* The server's native reader converts String <-> LowCardinality(String)
6495 * but turns other mismatches (a NULL for a non-Nullable column) into the
6496 * type's zero rather than the column DEFAULT: insist on the table's types. */
6497 0 static int block_type_matches(const char *have, size_t have_len,
6498 const char *want, size_t want_len) {
6499 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
6500 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
6501 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
6502 0 return 1;
6503 0 return 0;
6504 }
6505  
6506 0 static char* build_native_body_packet(ev_clickhouse_t *self, const char *buf,
6507 size_t len, const char **want_names,
6508 size_t *want_name_lens,
6509 const char **want_types,
6510 size_t *want_type_lens, int want_cols,
6511 size_t *out_len, char *errbuf,
6512 size_t errbuf_sz) {
6513 native_buf_t body;
6514 uint64_t ncols, nrows, c;
6515 0 size_t pos = 0;
6516 0 nbuf_init(&body);
6517 0 nbuf_block_info(&body);
6518 0 ▶ if (read_varuint(buf, len, &pos, &ncols) <= 0
Branch: 1 branch
BranchTrueFalseTotal
if (read_varuint(buf, len, &pos, &ncols) <= 0 0 0 0
6519 0 ▶ || read_varuint(buf, len, &pos, &nrows) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
|| read_varuint(buf, len, &pos, &nrows) <= 0) 0 0 0
6520 0 goto bad;
6521 0 ▶ if (ncols != (uint64_t)want_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (ncols != (uint64_t)want_cols) { 0 0 0
6522 0 snprintf(errbuf, errbuf_sz, "Native block has %llu columns, the insert %d",
6523 (unsigned long long)ncols, want_cols);
6524 0 goto fail;
6525 }
6526 0 nbuf_varuint(&body, ncols);
6527 0 nbuf_varuint(&body, nrows);
6528 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
6529 const char *name, *type;
6530 size_t name_len, type_len, start;
6531 col_type_t *ct;
6532 0 int rc = 0;
6533 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
6534 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
6535 0 goto bad;
6536 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
6537 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
6538 0 snprintf(errbuf, errbuf_sz, "Native block column %llu '%.*s' (%.*s) does "
6539 "not match the table's '%.*s' (%.*s); send it without data_block",
6540 (unsigned long long)c,
6541 0 ▶ (int)(name_len > 60 ? 60 : name_len), name,
Branch: 1 branch
BranchTrueFalseTotal
(int)(name_len > 60 ? 60 : name_len), name, 0 0 0
6542 0 ▶ (int)(type_len > 60 ? 60 : type_len), type,
Branch: 1 branch
BranchTrueFalseTotal
(int)(type_len > 60 ? 60 : type_len), type, 0 0 0
6543 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
6544 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
6545 0 goto fail;
6546 }
6547 0 nbuf_string(&body, name, name_len);
6548 0 nbuf_string(&body, type, type_len);
6549 0 nbuf_u8(&body, 0);
6550 0 start = pos;
6551 0 ct = parse_col_type(type, type_len);
6552 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
6553 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
6554 0 ▶ if (ct && rc < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct && rc < 0) { 0 0 0
if (ct && rc < 0) { 0 0 0
6555 0 int derr = 0;
6556 SV **vals;
6557 0 pos = start;
6558 0 vals = decode_column(buf, len, &pos, nrows, ct, &derr, 0);
6559 0 ▶ if (vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (vals) { 0 0 0
6560 uint64_t i;
6561 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
6562 0 Safefree(vals);
6563 0 rc = 1;
6564 }
6565 }
6566 0 free_col_type(ct);
6567 0 ▶ if (rc != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 1) { 0 0 0
6568 0 snprintf(errbuf, errbuf_sz, "malformed Native data in column '%.*s' (%.*s)",
6569 0 ▶ (int)(name_len > 60 ? 60 : name_len), name,
Branch: 1 branch
BranchTrueFalseTotal
(int)(name_len > 60 ? 60 : name_len), name, 0 0 0
6570 0 ▶ (int)(type_len > 60 ? 60 : type_len), type);
Branch: 1 branch
BranchTrueFalseTotal
(int)(type_len > 60 ? 60 : type_len), type); 0 0 0
6571 0 Safefree(body.data);
6572 0 *out_len = (size_t)-1;
6573 0 return NULL;
6574 }
6575 0 nbuf_append(&body, buf + start, pos - start);
6576 }
6577 0 ▶ if (pos != len) goto bad;
Branch: 1 branch
BranchTrueFalseTotal
if (pos != len) goto bad; 0 0 0
6578 0 return wrap_data_block(self, &body, out_len);
6579 0 bad:
6580 0 snprintf(errbuf, errbuf_sz, "malformed Native data");
6581 0 fail:
6582 0 Safefree(body.data);
6583 0 *out_len = (size_t)-1;
6584 0 return NULL;
6585 }
6586  
6587 /*
6588 * Build a native protocol Data block from TabSeparated text data.
6589 * col_names/col_types_str are string references into the sample block buffer.
6590 * Returns malloc'd packet data (CLIENT_DATA + block), or NULL on failure.
6591 */
6592 0 static char* build_native_insert_data(ev_clickhouse_t *self,
6593 const char *tsv_data, size_t tsv_len,
6594 const char **col_names, size_t *col_name_lens,
6595 const char **col_types_str, size_t *col_type_lens,
6596 col_type_t **col_types,
6597 int num_cols,
6598 col_default_t *cdefs,
6599 size_t *out_len,
6600 char *errbuf, size_t errbuf_sz) {
6601 /* Parse TSV into rows and fields */
6602 0 int nrows = 0, max_rows = 64;
6603 0 const char **fields = NULL; /* flat array: fields[row * num_cols + col] */
6604 0 size_t *field_lens = NULL;
6605 0 const char *p = tsv_data;
6606 0 const char *end = tsv_data + tsv_len;
6607  
6608 0 Newxz(fields, max_rows * num_cols, const char *);
6609 0 Newx(field_lens, max_rows * num_cols, size_t);
6610  
6611 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
6612 0 const char *line_end = memchr(p, '\n', end - p);
6613 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
6614 int col;
6615  
6616 /* No blank-line skipping: the server keeps a blank line as an
6617 * (empty-field) row, or errors when columns are missing. */
6618 /* The server rejects DOS line endings (error 117); match it
6619 * rather than silently storing the \r. Mid-field \r stays. */
6620 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
6621 0 Safefree(fields);
6622 0 Safefree(field_lens);
6623 0 snprintf(errbuf, errbuf_sz, "line %d ends with \\r "
6624 "(CRLF line endings are illegal in TabSeparated data)",
6625 nrows + 1);
6626 0 *out_len = (size_t)-1;
6627 0 return NULL;
6628 }
6629  
6630 0 ▶ if (nrows >= max_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows >= max_rows) { 0 0 0
6631 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
6632 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
6633 0 Safefree(fields);
6634 0 Safefree(field_lens);
6635 0 *out_len = 0;
6636 0 return NULL;
6637 }
6638 0 max_rows *= 2;
6639 0 Renew(fields, max_rows * num_cols, const char *);
6640 0 Renew(field_lens, max_rows * num_cols, size_t);
6641 }
6642  
6643 /* split line by tabs */
6644 {
6645 0 const char *fp = p;
6646 0 ▶ for (col = 0; col < num_cols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < num_cols; col++) { 0 0 0
6647 const char *tab;
6648 0 ▶ if (fp > line_limit) fp = line_limit;
Branch: 1 branch
BranchTrueFalseTotal
if (fp > line_limit) fp = line_limit; 0 0 0
6649 0 ▶ if (col < num_cols - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (col < num_cols - 1) { 0 0 0
6650 0 tab = memchr(fp, '\t', line_limit - fp);
6651 0 ▶ if (!tab) tab = line_limit;
Branch: 1 branch
BranchTrueFalseTotal
if (!tab) tab = line_limit; 0 0 0
6652 } else {
6653 0 tab = line_limit;
6654 }
6655 0 fields[nrows * num_cols + col] = fp;
6656 0 field_lens[nrows * num_cols + col] = tab - fp;
6657 0 fp = tab + 1;
6658 }
6659 }
6660 0 nrows++;
6661 0 p = line_limit + 1;
6662 }
6663  
6664 0 ▶ if (nrows == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows == 0) { 0 0 0
6665 0 Safefree(fields);
6666 0 Safefree(field_lens);
6667 0 *out_len = 0;
6668 0 return NULL;
6669 }
6670  
6671 /* Build the Data block body: block info + num_cols + num_rows + columns */
6672 {
6673 native_buf_t body;
6674 int col;
6675  
6676 0 nbuf_init(&body);
6677 0 nbuf_block_info(&body);
6678 0 nbuf_varuint(&body, (uint64_t)num_cols);
6679 0 nbuf_varuint(&body, (uint64_t)nrows);
6680  
6681 /* encode each column */
6682 0 ▶ for (col = 0; col < num_cols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < num_cols; col++) { 0 0 0
6683 const char **col_vals;
6684 size_t *col_vlens;
6685 int row;
6686 0 ▶ int sub = cdefs ? cdefs[col].subst : 0;
Branch: 1 branch
BranchTrueFalseTotal
int sub = cdefs ? cdefs[col].subst : 0; 0 0 0
6687 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
6688  
6689 /* column name and type */
6690 0 nbuf_string(&body, col_names[col], col_name_lens[col]);
6691 0 nbuf_string(&body, col_types_str[col], col_type_lens[col]);
6692 0 nbuf_u8(&body, 0); /* has_custom_serialization = false */
6693  
6694 /* gather column values from row-major fields */
6695 0 Newxz(col_vals, nrows, const char *);
6696 0 Newx(col_vlens, nrows, size_t);
6697 0 ▶ for (row = 0; row < nrows; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < nrows; row++) { 0 0 0
6698 0 const char *fp = fields[row * num_cols + col];
6699 0 size_t fl = field_lens[row * num_cols + col];
6700 0 ▶ if (sub && (fl == 0
Branch: 2 branches
BranchTrueFalseTotal
if (sub && (fl == 0 0 0 0
if (sub && (fl == 0 0 0 0
6701 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
6702 0 ▶ if (cdefs[col].to_null) {
Branch: 1 branch
BranchTrueFalseTotal
if (cdefs[col].to_null) { 0 0 0
6703 0 fp = "\\N";
6704 0 fl = 2;
6705 0 ▶ } else if (cdefs[col].subst_text) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cdefs[col].subst_text) { 0 0 0
6706 0 fp = cdefs[col].subst_text;
6707 0 fl = cdefs[col].subst_text_len;
6708 } else {
6709 char where[160];
6710 0 err_where(where, sizeof(where), col_names[col],
6711 0 col_name_lens[col], col_types_str[col],
6712 0 col_type_lens[col]);
6713 0 snprintf(errbuf, errbuf_sz, "%s, line %d: %s", where,
6714 0 ▶ row + 1, cdefs[col].err ? cdefs[col].err
Branch: 1 branch
BranchTrueFalseTotal
row + 1, cdefs[col].err ? cdefs[col].err 0 0 0
6715 : "no client-side default for empty values: "
6716 "use data_format => 'Native'");
6717 0 Safefree(col_vals);
6718 0 Safefree(col_vlens);
6719 0 Safefree(body.data);
6720 0 Safefree(fields);
6721 0 Safefree(field_lens);
6722 0 *out_len = (size_t)-1;
6723 0 return NULL;
6724 }
6725 }
6726 0 col_vals[row] = fp;
6727 0 col_vlens[row] = fl;
6728 }
6729  
6730 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
6731 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
6732 0 (uint64_t)nrows, col_types[col])) {
6733 char where[160];
6734 0 err_where(where, sizeof(where), col_names[col],
6735 0 col_name_lens[col], col_types_str[col],
6736 0 col_type_lens[col]);
6737 0 describe_text_failure(errbuf, errbuf_sz, where, col_types[col],
6738 col_vals, col_vlens, nrows);
6739 0 Safefree(col_vals);
6740 0 Safefree(col_vlens);
6741 0 Safefree(body.data);
6742 0 Safefree(fields);
6743 0 Safefree(field_lens);
6744 0 *out_len = (size_t)-1; /* sentinel: encode failure */
6745 0 return NULL;
6746 }
6747 0 Safefree(col_vals);
6748 0 Safefree(col_vlens);
6749 }
6750  
6751 0 Safefree(fields);
6752 0 Safefree(field_lens);
6753  
6754 0 return wrap_data_block(self, &body, out_len);
6755 }
6756 }
6757  
6758 16 static void nbuf_reserve(native_buf_t *b, size_t cap) {
6759 16 ▶ if (cap > b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (cap > b->cap) { 0 16 16
6760 0 b->cap = cap;
6761 0 Renew(b->data, cap, char);
6762 }
6763 16 }
6764  
6765 /* Rows per pass of the insert build: a pass encodes every column of a
6766 * chunk of rows while their scalars are cache-hot, instead of walking all
6767 * rows' scalars once per column. */
6768 #define INSERT_CHUNK_ROWS 1024
6769  
6770 enum { ENC_WHOLE, ENC_PLAIN, ENC_NULLABLE, ENC_ARRAY, ENC_LC };
6771  
6772 /* Types whose column encoding is the concatenation of per-value bytes. */
6773 12 static int enc_plain_type(const col_type_t *ct) {
6774 12 ▶ switch (ct->code) {
Branch: 1 branch
BranchTrueFalseTotal
switch (ct->code) { 12 0 12
6775 12 case CT_INT8: case CT_INT16: case CT_INT32: case CT_INT64:
6776 case CT_UINT8: case CT_UINT16: case CT_UINT32: case CT_UINT64:
6777 case CT_BOOL: case CT_FLOAT32: case CT_FLOAT64: case CT_BFLOAT16:
6778 case CT_DATE: case CT_DATE32: case CT_DATETIME: case CT_DATETIME64:
6779 case CT_TIME: case CT_TIME64:
6780 case CT_DECIMAL32: case CT_DECIMAL64: case CT_DECIMAL128: case CT_DECIMAL256:
6781 case CT_STRING: case CT_FIXEDSTRING: case CT_UUID: case CT_IPV4: case CT_IPV6:
6782 case CT_ENUM8: case CT_ENUM16:
6783 12 return 1;
6784 0 default:
6785 0 return 0;
6786 }
6787 }
6788  
6789 /* Nullable/Array/LowCardinality of a plain type encode a chunk as parts
6790 * that concatenate across chunks; anything else is encoded whole. */
6791 12 static int enc_col_mode(const col_type_t *ct) {
6792 12 ▶ if (enc_plain_type(ct)) return ENC_PLAIN;
Branch: 1 branch
BranchTrueFalseTotal
if (enc_plain_type(ct)) return ENC_PLAIN; 12 0 12
6793 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
6794 0 switch (ct->code) {
6795 0 case CT_NULLABLE: return ENC_NULLABLE;
6796 0 case CT_ARRAY: return ENC_ARRAY;
6797 0 case CT_LOWCARDINALITY: return ENC_LC;
6798 0 default: return ENC_WHOLE;
6799 }
6800 }
6801  
6802 typedef struct {
6803 int mode;
6804 native_buf_t data; /* values or array elements */
6805 native_buf_t aux; /* null map or array offsets */
6806 uint64_t base; /* array elements so far */
6807 lc_dict_t *lc; /* ENC_LC */
6808 } enc_col_t;
6809  
6810 typedef struct {
6811 enc_col_t *cols;
6812 int ncols;
6813 SV **vals;
6814 native_buf_t tmp;
6815 native_buf_t out;
6816 } enc_ctx_t;
6817  
6818 12 static void enc_ctx_free(pTHX_ void *p) {
6819 12 enc_ctx_t *x = (enc_ctx_t *)p;
6820 int c;
6821 24 ▶ for (c = 0; c < x->ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < x->ncols; c++) { 12 12 24
6822 12 Safefree(x->cols[c].data.data);
6823 12 Safefree(x->cols[c].aux.data);
6824 12 ▶ if (x->cols[c].lc) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->cols[c].lc) { 0 12 12
6825 0 lc_dict_free(x->cols[c].lc);
6826 0 Safefree(x->cols[c].lc);
6827 }
6828 }
6829 12 Safefree(x->cols);
6830 12 Safefree(x->vals);
6831 12 Safefree(x->tmp.data);
6832 12 Safefree(x->out.data);
6833 12 Safefree(x);
6834 12 }
6835  
6836 /* Gather one column of n rows, with empty cells replaced by the column's
6837 * resolved default when cd asks for it: '' always, undef unless the
6838 * column is Nullable. */
6839 20 static SSize_t snap_gather(const insert_snap_t *sn, SSize_t r0, SSize_t n,
6840 int col, const col_default_t *cd, SV **vals) {
6841 SSize_t k;
6842 SV *to;
6843 40 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 20 20 40
6844 20 SSize_t o = sn->off[r0 + k];
6845 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
6846 20 ▶ vals[k] = v ? v : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
vals[k] = v ? v : &PL_sv_undef; 20 0 20
6847 }
6848 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
6849 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
6850 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6851 0 SV *v = vals[k];
6852 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
6853 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
6854 0 vals[k] = to;
6855 }
6856 }
6857 0 return -1;
6858 }
6859  
6860 /* Move one chunk's encoding of n rows (tmp) into the column's parts. */
6861 0 static void enc_split_chunk(enc_col_t *ec, const native_buf_t *tmp, SSize_t n) {
6862 0 switch (ec->mode) {
6863 0 case ENC_NULLABLE:
6864 0 nbuf_append(&ec->aux, tmp->data, (size_t)n);
6865 0 nbuf_append(&ec->data, tmp->data + n, tmp->len - (size_t)n);
6866 0 break;
6867 0 case ENC_ARRAY: {
6868 SSize_t k;
6869 0 uint64_t off = 0, abs;
6870 0 nbuf_grow(&ec->aux, (size_t)n * 8);
6871 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
6872 0 memcpy(&off, tmp->data + k * 8, 8);
6873 0 abs = ec->base + off;
6874 0 memcpy(ec->aux.data + ec->aux.len, &abs, 8);
6875 0 ec->aux.len += 8;
6876 }
6877 0 ec->base += off;
6878 0 nbuf_append(&ec->data, tmp->data + n * 8, tmp->len - (size_t)n * 8);
6879 0 break;
6880 }
6881 0 default:
6882 0 break;
6883 }
6884 0 }
6885  
6886 /*
6887 * Build a native protocol Data block from an insert snapshot.
6888 * Like build_native_insert_data() but encodes SVs directly via encode_column_sv().
6889 */
6890 12 static char* build_native_insert_data_from_av(pTHX_ ev_clickhouse_t *self,
6891 SV *snap_sv,
6892 const char **col_names, size_t *col_name_lens,
6893 const char **col_types_str, size_t *col_type_lens,
6894 col_type_t **col_types,
6895 int num_cols,
6896 col_default_t *cdefs,
6897 enc_progress_t *prog,
6898 size_t *out_len,
6899 char *errbuf, size_t errbuf_sz) {
6900 12 insert_snap_t *sn = insert_snap_get(aTHX_ snap_sv);
6901 12 ▶ SSize_t nrows = sn ? sn->nrows : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t nrows = sn ? sn->nrows : 0; 12 0 12
6902 enc_ctx_t *x;
6903 12 SSize_t r0, fail_r0 = 0, fail_n = 0, fail_nodef = -1;
6904 12 int c, fail = num_cols;
6905 size_t need;
6906 char *pkt;
6907  
6908 12 ▶ if (nrows <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows <= 0) { 0 12 12
6909 0 *out_len = 0;
6910 0 return NULL;
6911 }
6912 12 ▶ if (sn->bad_row >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sn->bad_row >= 0) { 0 12 12
6913 0 snprintf(errbuf, errbuf_sz, "row %ld is not an arrayref",
6914 0 (long)sn->bad_row);
6915 0 *out_len = (size_t)-1; /* sentinel: encode failure */
6916 0 return NULL;
6917 }
6918 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
6919 0 snprintf(errbuf, errbuf_sz, "columns_at has %ld positions but the "
6920 0 "insert has %d columns", (long)sn->width, num_cols);
6921 0 *out_len = (size_t)-1;
6922 0 return NULL;
6923 }
6924  
6925 12 ENTER;
6926 12 Newxz(x, 1, enc_ctx_t);
6927 12 SAVEDESTRUCTOR_X(enc_ctx_free, x);
6928 12 Newxz(x->cols, num_cols, enc_col_t);
6929 12 x->ncols = num_cols;
6930 12 ▶ Newx(x->vals, nrows, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(x->vals, nrows, SV *); 0 12 12
6931 12 nbuf_init(&x->tmp);
6932 24 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 12 12 24
6933 12 enc_col_t *ec = &x->cols[c];
6934 12 size_t fsz = col_type_fixed_size(col_types[c]);
6935 12 ec->mode = enc_col_mode(col_types[c]);
6936 12 nbuf_init(&ec->data);
6937 12 nbuf_init(&ec->aux);
6938 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
6939 12 nbuf_reserve(&ec->data, fsz * (size_t)nrows);
6940 0 ▶ else if (ec->mode == ENC_NULLABLE)
Branch: 1 branch
BranchTrueFalseTotal
else if (ec->mode == ENC_NULLABLE) 0 0 0
6941 0 nbuf_reserve(&ec->aux, (size_t)nrows);
6942 0 ▶ else if (ec->mode == ENC_ARRAY)
Branch: 1 branch
BranchTrueFalseTotal
else if (ec->mode == ENC_ARRAY) 0 0 0
6943 0 nbuf_reserve(&ec->aux, 8 * (size_t)nrows);
6944 0 ▶ else if (ec->mode == ENC_LC) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ec->mode == ENC_LC) { 0 0 0
6945 0 Newx(ec->lc, 1, lc_dict_t);
6946 0 lc_dict_init(ec->lc, (uint64_t)nrows);
6947 }
6948 }
6949  
6950 /* A failing column stops the work on itself and every later column,
6951 * so the column reported is the first failing one, as column order
6952 * would find it. */
6953 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
6954 12 SSize_t n = nrows - r0 < INSERT_CHUNK_ROWS ? nrows - r0 : INSERT_CHUNK_ROWS;
6955 12 ENTER; SAVETMPS;
6956 24 ▶ for (c = 0; c < fail; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < fail; c++) { 12 12 24
6957 12 enc_col_t *ec = &x->cols[c];
6958 int ok;
6959 SSize_t nodef;
6960 12 ▶ if (ec->mode == ENC_WHOLE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_WHOLE) continue; 0 12 12
6961 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
6962 12 ▶ if (nodef >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nodef >= 0) { 0 12 12
6963 0 fail = c; fail_r0 = r0; fail_n = n; fail_nodef = r0 + nodef;
6964 0 break;
6965 }
6966 12 prog->col = c; prog->r0 = r0; prog->n = n;
6967 12 ▶ if (ec->mode == ENC_PLAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_PLAIN) { 12 0 12
6968 12 ok = encode_column_sv(aTHX_ &ec->data, x->vals, (uint64_t)n,
6969 12 col_types[c]);
6970 0 ▶ } else if (ec->mode == ENC_LC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ec->mode == ENC_LC) { 0 0 0
6971 0 x->tmp.len = 0;
6972 0 ok = encode_column_sv(aTHX_ &x->tmp, x->vals, (uint64_t)n,
6973 0 col_types[c]->inner)
6974 0 && lc_dict_feed(ec->lc, x->tmp.data, x->tmp.len, (uint64_t)n,
6975 0 col_types[c]->inner, NULL);
6976 } else {
6977 0 x->tmp.len = 0;
6978 0 ok = encode_column_sv(aTHX_ &x->tmp, x->vals, (uint64_t)n,
6979 0 col_types[c]);
6980 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
6981 }
6982 12 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 8 4 12
6983 8 fail = c; fail_r0 = r0; fail_n = n; fail_nodef = -1;
6984 }
6985 4 ▶ else if (r0 == 0 && n < nrows && ec->mode != ENC_LC
Branch: 3 branches
BranchTrueFalseTotal
else if (r0 == 0 && n < nrows && ec->mode != ENC_LC 4 0 4
else if (r0 == 0 && n < nrows && ec->mode != ENC_LC 0 4 4
else if (r0 == 0 && n < nrows && ec->mode != ENC_LC 0 0 0
6986 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
6987 0 nbuf_reserve(&ec->data,
6988 0 (size_t)((double)ec->data.len / n * nrows * 1.1) + 64);
6989 }
6990 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 12 12
6991 }
6992 16 ▶ for (c = 0; c < fail; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < fail; c++) { 4 12 16
6993 4 ▶ if (x->cols[c].mode != ENC_WHOLE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (x->cols[c].mode != ENC_WHOLE) continue; 4 0 4
6994 SSize_t nodef;
6995 0 ENTER; SAVETMPS;
6996 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
6997 0 prog->col = c; prog->r0 = 0; prog->n = nrows;
6998 0 ▶ if (nodef >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nodef >= 0) { 0 0 0
6999 0 fail = c; fail_r0 = 0; fail_n = nrows; fail_nodef = nodef;
7000 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
7001 0 (uint64_t)nrows, col_types[c])) {
7002 0 fail = c; fail_r0 = 0; fail_n = nrows; fail_nodef = -1;
7003 }
7004 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
7005 }
7006 12 ▶ if (fail < num_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (fail < num_cols) { 8 4 12
7007 char where[160];
7008 8 prog->col = fail; prog->r0 = fail_r0; prog->n = fail_n;
7009 8 err_where(where, sizeof(where), col_names[fail], col_name_lens[fail],
7010 8 col_types_str[fail], col_type_lens[fail]);
7011 8 ▶ if (fail_nodef >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fail_nodef >= 0) { 0 8 8
7012 0 snprintf(errbuf, errbuf_sz, "%s, row %ld: %s", where,
7013 0 (long)fail_nodef, cdefs[fail].err);
7014 } else {
7015 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
7016 x->vals);
7017 8 describe_sv_failure(aTHX_ errbuf, errbuf_sz, where, col_types[fail],
7018 x->vals, fail_n, fail_r0, "row");
7019 }
7020 8 *out_len = (size_t)-1; /* sentinel: encode failure */
7021 8 LEAVE;
7022 8 return NULL;
7023 }
7024 4 prog->col = -1;
7025  
7026 4 need = 64;
7027 8 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 4 4 8
7028 4 need += col_name_lens[c] + col_type_lens[c] + 64
7029 4 + x->cols[c].data.len + x->cols[c].aux.len;
7030 4 ▶ if (x->cols[c].mode == ENC_LC)
Branch: 1 branch
BranchTrueFalseTotal
if (x->cols[c].mode == ENC_LC) 0 4 4
7031 0 need += 24 + x->cols[c].lc->dict.len
7032 0 + ((size_t)nrows << lc_key_type(x->cols[c].lc->nkeys));
7033 }
7034 4 nbuf_init(&x->out);
7035 4 nbuf_reserve(&x->out, need);
7036 4 ▶ if (!self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->compress) { 4 0 4
7037 4 nbuf_varuint(&x->out, CLIENT_DATA);
7038 4 nbuf_cstring(&x->out, ""); /* table name */
7039 }
7040 4 nbuf_block_info(&x->out);
7041 4 nbuf_varuint(&x->out, (uint64_t)num_cols);
7042 4 nbuf_varuint(&x->out, (uint64_t)nrows);
7043 8 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 4 4 8
7044 4 enc_col_t *ec = &x->cols[c];
7045 4 nbuf_string(&x->out, col_names[c], col_name_lens[c]);
7046 4 nbuf_string(&x->out, col_types_str[c], col_type_lens[c]);
7047 4 nbuf_u8(&x->out, 0); /* has_custom_serialization = false */
7048 4 write_col_prefix(&x->out, col_types[c]);
7049 4 ▶ if (ec->mode == ENC_LC) {
Branch: 1 branch
BranchTrueFalseTotal
if (ec->mode == ENC_LC) { 0 4 4
7050 0 lc_dict_write(&x->out, ec->lc);
7051 0 lc_dict_free(ec->lc);
7052 0 Safefree(ec->lc);
7053 0 ec->lc = NULL;
7054 } else {
7055 4 nbuf_append(&x->out, ec->aux.data, ec->aux.len);
7056 4 nbuf_append(&x->out, ec->data.data, ec->data.len);
7057 }
7058 4 Safefree(ec->data.data); ec->data.data = NULL;
7059 4 Safefree(ec->aux.data); ec->aux.data = NULL;
7060 }
7061  
7062 4 ▶ if (!self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->compress) { 4 0 4
7063 4 nbuf_empty_data_block(&x->out, self);
7064 4 pkt = x->out.data;
7065 4 *out_len = x->out.len;
7066 } else {
7067 0 pkt = wrap_data_block(self, &x->out, out_len);
7068 }
7069 4 x->out.data = NULL;
7070 4 LEAVE;
7071 4 return pkt;
7072 }
7073  
7074 typedef struct {
7075 ev_clickhouse_t *self;
7076 SV *snap;
7077 const char **names; size_t *name_lens;
7078 const char **types_str; size_t *type_lens;
7079 col_type_t **types;
7080 int ncols;
7081 col_default_t *cdefs;
7082 enc_progress_t prog;
7083 size_t *out_len;
7084 char *errbuf; size_t errbuf_sz;
7085 char *pkt;
7086 } av_build_t;
7087  
7088 12 static void av_build_fn(pTHX_ void *p) {
7089 12 av_build_t *a = (av_build_t *)p;
7090 12 a->pkt = build_native_insert_data_from_av(aTHX_ a->self, a->snap,
7091 a->names, a->name_lens, a->types_str, a->type_lens, a->types,
7092 a->ncols, a->cdefs, &a->prog, a->out_len, a->errbuf, a->errbuf_sz);
7093 12 }
7094  
7095 typedef struct {
7096 SV *v;
7097 col_type_t *ct;
7098 native_buf_t *scratch;
7099 } encode_one_t;
7100  
7101 0 static void encode_one_fn(pTHX_ void *p) {
7102 0 encode_one_t *a = (encode_one_t *)p;
7103 0 (void)sv_encodes(aTHX_ a->v, a->ct, a->scratch);
7104 0 }
7105  
7106 /* build_native_insert_data_from_av() with exceptions from user code
7107 * turned into an encode failure naming the column and row that died. */
7108 12 static char* build_native_insert_data_from_av_guarded(pTHX_ ev_clickhouse_t *self,
7109 SV *snap_sv,
7110 const char **col_names, size_t *col_name_lens,
7111 const char **col_types_str, size_t *col_type_lens,
7112 col_type_t **col_types,
7113 int num_cols,
7114 col_default_t *cdefs,
7115 size_t *out_len,
7116 char *errbuf, size_t errbuf_sz) {
7117 av_build_t a;
7118 12 SV *err = NULL;
7119 native_buf_t msg;
7120 int c;
7121  
7122 12 a.self = self; a.snap = snap_sv;
7123 12 a.names = col_names; a.name_lens = col_name_lens;
7124 12 a.types_str = col_types_str; a.type_lens = col_type_lens;
7125 12 a.types = col_types; a.ncols = num_cols; a.cdefs = cdefs;
7126 12 a.prog.col = -1; a.prog.r0 = 0; a.prog.n = 0;
7127 12 a.out_len = out_len; a.errbuf = errbuf; a.errbuf_sz = errbuf_sz;
7128 12 a.pkt = NULL;
7129 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
7130 12 return a.pkt;
7131  
7132 0 nbuf_init(&msg);
7133 0 c = a.prog.col;
7134 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
7135 0 insert_snap_t *sn = insert_snap_get(aTHX_ snap_sv);
7136 char where[160];
7137 0 SSize_t k, row = -1;
7138 0 err_where(where, sizeof(where), col_names[c], col_name_lens[c],
7139 0 col_types_str[c], col_type_lens[c]);
7140 0 err_append(&msg, where);
7141 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
7142 SV **vals;
7143 native_buf_t scratch;
7144 encode_one_t one;
7145 0 ▶ Newx(vals, a.prog.n, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(vals, a.prog.n, SV *); 0 0 0
7146 0 nbuf_init(&scratch);
7147 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
7148 0 cdefs ? &cdefs[c] : NULL, vals);
7149 0 one.ct = col_types[c];
7150 0 one.scratch = &scratch;
7151 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
7152 0 one.v = vals[k];
7153 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
7154 0 row = a.prog.r0 + k;
7155 }
7156 0 Safefree(scratch.data);
7157 0 Safefree(vals);
7158 }
7159 0 ▶ if (row >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (row >= 0) { 0 0 0
7160 char rb[48];
7161 0 snprintf(rb, sizeof(rb), ", row %ld", (long)row);
7162 0 err_append(&msg, rb);
7163 }
7164 0 err_append(&msg, ": ");
7165 }
7166 0 err_exception(aTHX_ &msg, err);
7167 0 ▶ if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) SvREFCNT_dec(err); 0 0 0
7168 0 err_finish(errbuf, errbuf_sz, &msg);
7169 0 *out_len = (size_t)-1; /* sentinel: encode failure */
7170 0 return NULL;
7171 }
7172  
7173 /*
7174 * Build the external-table Data packets for a native query. `external`
7175 * maps table-name => { structure => [name => type, ...], data => [[..],..] }.
7176 * Each entry becomes one CLIENT_DATA packet (named block, optionally LZ4)
7177 * to be spliced into the query stream before the terminating empty block.
7178 *
7179 * Returns a malloc'd buffer of the concatenated packets with *out_len set
7180 * (an empty 0-length buffer when `external` has no tables). On error
7181 * returns NULL and writes a message into errbuf. The caller must
7182 * Safefree the returned buffer.
7183 */
7184 0 static char* build_external_tables(pTHX_ ev_clickhouse_t *self, HV *external,
7185 const char *tz, size_t *out_len,
7186 char *errbuf, size_t errbuf_sz) {
7187 native_buf_t out;
7188 HE *he;
7189  
7190 0 *out_len = 0;
7191 0 nbuf_init(&out);
7192 0 hv_iterinit(external);
7193  
7194 0 ▶ while ((he = hv_iternext(external))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(external))) { 0 0 0
7195 I32 tnlen;
7196 0 const char *tname = hv_iterkey(he, &tnlen);
7197 0 SV *spec_sv = hv_iterval(external, he);
7198 HV *spec;
7199 SV **st, **dt;
7200 AV *structure, *rows;
7201 SSize_t sn, nrows, row;
7202 int ncols, col;
7203 native_buf_t body;
7204 0 body.data = NULL;
7205  
7206 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
7207 0 snprintf(errbuf, errbuf_sz, "external table '%s': spec must be a "
7208 "hashref { structure => [...], data => [...] }", tname);
7209 0 goto fail;
7210 }
7211 0 spec = (HV *)SvRV(spec_sv);
7212 0 st = hv_fetchs(spec, "structure", 0);
7213 0 dt = hv_fetchs(spec, "data", 0);
7214 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
7215 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'structure' must "
7216 "be an arrayref of name => type pairs", tname);
7217 0 goto fail;
7218 }
7219 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
7220 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'data' must be "
7221 "an arrayref of rows", tname);
7222 0 goto fail;
7223 }
7224 0 structure = (AV *)SvRV(*st);
7225 0 rows = (AV *)SvRV(*dt);
7226 0 sn = av_len(structure) + 1;
7227 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
7228 0 snprintf(errbuf, errbuf_sz, "external table '%s': 'structure' needs "
7229 "a non-empty, even list of name => type pairs", tname);
7230 0 goto fail;
7231 }
7232 0 ncols = (int)(sn / 2);
7233 0 nrows = av_len(rows) + 1;
7234 0 ▶ if (nrows < 0) nrows = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nrows < 0) nrows = 0; 0 0 0
7235  
7236 /* block body: block info + num_columns + num_rows + columns */
7237 0 nbuf_init(&body);
7238 0 nbuf_block_info(&body);
7239 0 nbuf_varuint(&body, (uint64_t)ncols);
7240 0 nbuf_varuint(&body, (uint64_t)nrows);
7241  
7242 0 ▶ for (col = 0; col < ncols; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < ncols; col++) { 0 0 0
7243 0 SV **nm = av_fetch(structure, col * 2, 0);
7244 0 SV **ty = av_fetch(structure, col * 2 + 1, 0);
7245 STRLEN cnl, ctl;
7246 const char *cname, *ctype;
7247 col_type_t *ct;
7248 SV **col_vals;
7249 int enc;
7250  
7251 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
7252 0 snprintf(errbuf, errbuf_sz, "external table '%s': structure "
7253 "name/type entries must be defined", tname);
7254 0 Safefree(body.data);
7255 0 goto fail;
7256 }
7257 0 cname = SvPV(*nm, cnl);
7258 0 ▶ ctype = SvPV(*ty, ctl);
Branch: 1 branch
BranchTrueFalseTotal
ctype = SvPV(*ty, ctl); 0 0 0
7259 0 nbuf_string(&body, cname, cnl);
7260 0 nbuf_string(&body, ctype, ctl);
7261 0 nbuf_u8(&body, 0); /* has_custom_serialization = false */
7262  
7263 0 ct = parse_col_type(ctype, ctl);
7264 0 col_type_default_tz(ct, tz, &self->zones);
7265  
7266 /* gather this column's values from the row-major data */
7267 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
7268 0 ▶ for (row = 0; row < nrows; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < nrows; row++) { 0 0 0
7269 0 SV **rsv = av_fetch(rows, row, 0);
7270 AV *rav;
7271 SV **vsv;
7272 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
7273 0 snprintf(errbuf, errbuf_sz, "external table '%s': each data "
7274 "row must be an arrayref", tname);
7275 0 Safefree(col_vals);
7276 0 free_col_type(ct);
7277 0 Safefree(body.data);
7278 0 goto fail;
7279 }
7280 0 rav = (AV *)SvRV(*rsv);
7281 0 vsv = av_fetch(rav, col, 0);
7282 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
7283 }
7284  
7285 0 enc = 1;
7286 0 ▶ if (nrows) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows) { 0 0 0
7287 0 write_col_prefix(&body, ct);
7288 0 enc = encode_column_sv(aTHX_ &body, col_vals,
7289 (uint64_t)nrows, ct);
7290 }
7291 0 ▶ if (!enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) { 0 0 0
7292 char where[160], what[512];
7293 0 err_where(where, sizeof(where), cname, cnl, ctype, ctl);
7294 0 describe_sv_failure(aTHX_ what, sizeof(what), where, ct,
7295 col_vals, nrows, 0, "row");
7296 0 snprintf(errbuf, errbuf_sz, "external table '%s': %s",
7297 tname, what);
7298 }
7299 0 Safefree(col_vals);
7300 0 free_col_type(ct);
7301 0 ▶ if (!enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) { 0 0 0
7302 0 Safefree(body.data);
7303 0 goto fail;
7304 }
7305 }
7306  
7307 /* [CLIENT_DATA] [table name] [block — optionally compressed] */
7308 0 nbuf_varuint(&out, CLIENT_DATA);
7309 0 nbuf_string(&out, tname, (size_t)tnlen);
7310 0 nbuf_put_block(&out, &body, self);
7311 }
7312  
7313 0 *out_len = out.len;
7314 0 return out.data;
7315  
7316 0 fail:
7317 0 Safefree(out.data);
7318 0 *out_len = 0;
7319 0 return NULL;
7320 }
7321  
« xs/lz4.c ↑ Summary xs/tzif.c »