xs/proto_native_parse.c

filter: Statementstmt 33.3% 267 / 801 Branchbran 18.5% 145 / 780 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 26.0% 412 / 1581 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/tzif.c ↑ Summary xs/codecs.c »
1 /* --- Native protocol response parser --- */
2  
3 /*
4 * Skip block info fields (revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO).
5 * Returns 1 on success, 0 if need more data.
6 */
7 14 static int skip_block_info(const char *buf, size_t len, size_t *pos) {
8 0 for (;;) {
9 uint64_t field_num;
10 14 int rc = read_varuint(buf, len, pos, &field_num);
11 28 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 14 14
12 14 ▶ if (rc < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) return -1; 0 14 14
13 14 ▶ if (field_num == 0) return 1; /* end marker */
Branch: 1 branch
BranchTrueFalseTotal
if (field_num == 0) return 1; /* end marker */ 14 0 14
14 0 ▶ if (field_num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (field_num == 1) { 0 0 0
15 /* is_overflows: UInt8 */
16 uint8_t dummy;
17 0 rc = read_u8(buf, len, pos, &dummy);
18 0 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 0 0 0
19 0 ▶ } else if (field_num == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (field_num == 2) { 0 0 0
20 /* bucket_num: Int32 */
21 int32_t dummy;
22 0 rc = read_i32(buf, len, pos, &dummy);
23 0 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 0 0 0
24 } else {
25 0 return -1; /* protocol error */
26 }
27 }
28 }
29  
30 /*
31 * Try to parse one server packet from recv_buf.
32 * Returns:
33 * 1 = packet consumed, continue reading
34 * 0 = need more data
35 * -1 = stream corruption (message in *errmsg, caller must Safefree);
36 * bytes NOT consumed, resync impossible
37 * 2 = EndOfStream
38 * 3 = Pong
39 * 4 = Hello parsed (self->server_* fields populated)
40 * 5 = Server exception: a normal query error, bytes consumed,
41 * stream stays in sync (message in *errmsg)
42 */
43  
44 /* Parse a Data block (table_name + optional-LZ4-chain + block_info + columns)
45 * and decode-and-discard each column. Used by SERVER_LOG and SERVER_PROFILE_EVENTS,
46 * which carry a block in the same wire format as SERVER_DATA but whose contents
47 * the client does not surface to Perl.
48 *
49 * `lz4_optional` (PROFILE_EVENTS, server log): if set, fall back to parsing
50 * the body uncompressed when LZ4 hard-fails — servers don't always compress
51 * these auxiliary blocks even on a compressed connection.
52 *
53 * Returns 1 on success (advances *outer_pos past the consumed bytes),
54 * 0 on need-more-data,
55 * -1 on hard error (sets *errmsg). */
56 0 static int parse_and_discard_block(ev_clickhouse_t *self,
57 const char *buf, size_t len, size_t *outer_pos,
58 const char *kind, int lz4_optional,
59 char **errmsg) {
60 0 size_t pos = *outer_pos;
61 int rc;
62 0 char *decompressed = NULL;
63 const char *bbuf;
64 size_t blen, bpos;
65 char errbuf[64];
66  
67 0 rc = skip_native_string(buf, len, &pos);
68 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
69 0 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 0 0 0
70 0 snprintf(errbuf, sizeof(errbuf), "malformed %s block", kind);
71 0 *errmsg = safe_strdup(errbuf);
72 0 return -1;
73 }
74  
75 0 ▶ if (self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress) { 0 0 0
76 0 int need_more = 0;
77 0 const char *lz4_err = NULL;
78 0 decompressed = ch_lz4_decompress_chain(buf, len, &pos, &blen,
79 &need_more, &lz4_err);
80 0 ▶ if (!decompressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!decompressed) { 0 0 0
81 0 ▶ if (need_more) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (need_more) return 0; 0 0 0
82 0 ▶ if (!lz4_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if (!lz4_optional) { 0 0 0
83 0 snprintf(errbuf, sizeof(errbuf), "%s: LZ4 decompression failed", kind);
84 0 ▶ *errmsg = safe_strdup(lz4_err ? lz4_err : errbuf);
Branch: 1 branch
BranchTrueFalseTotal
*errmsg = safe_strdup(lz4_err ? lz4_err : errbuf); 0 0 0
85 0 return -1;
86 }
87 /* fall through to uncompressed parsing */
88 }
89 }
90 0 ▶ if (decompressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) { 0 0 0
91 0 bbuf = decompressed;
92 0 bpos = 0;
93 } else {
94 0 bbuf = buf;
95 0 blen = len;
96 0 bpos = pos;
97 }
98  
99 #define _BAIL(rc_val) do { \
100 if (decompressed) Safefree(decompressed); \
101 if (rc_val < 0) { snprintf(errbuf, sizeof(errbuf), "malformed %s block", kind); *errmsg = safe_strdup(errbuf); } \
102 return rc_val; \
103 } while (0)
104  
105 0 ▶ if (self->server_revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO) { 0 0 0
106 0 rc = skip_block_info(bbuf, blen, &bpos);
107 0 ▶ if (rc <= 0) _BAIL(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
108 }
109 {
110 uint64_t nc, nr, c;
111 0 rc = read_varuint(bbuf, blen, &bpos, &nc);
112 0 ▶ if (rc <= 0) _BAIL(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
113 0 rc = read_varuint(bbuf, blen, &bpos, &nr);
114 0 ▶ if (rc <= 0) _BAIL(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
115  
116 0 ▶ for (c = 0; c < nc; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < nc; c++) { 0 0 0
117 const char *ctype;
118 size_t ctype_len;
119 0 rc = skip_native_string(bbuf, blen, &bpos);
120 0 ▶ if (rc <= 0) _BAIL(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
121 0 rc = read_native_string_ref(bbuf, blen, &bpos, &ctype, &ctype_len);
122 0 ▶ if (rc <= 0) _BAIL(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
if (rc <= 0) _BAIL(rc); 0 0 0
123 /* custom serialization flag (revision >= 54446) */
124 0 ▶ if (bpos >= blen) _BAIL(0);
Branch: 2 branches
BranchTrueFalseTotal
if (bpos >= blen) _BAIL(0); 0 0 0
if (bpos >= blen) _BAIL(0); 0 0 0
125 0 ▶ if ((uint8_t)bbuf[bpos]) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint8_t)bbuf[bpos]) { 0 0 0
126 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
127 0 *errmsg = safe_strdup("custom serialization not supported");
128 0 return -1;
129 }
130 0 bpos++;
131 0 ▶ if (nr > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nr > 0) { 0 0 0
132 0 col_type_t *ct = parse_col_type(ctype, ctype_len);
133 0 int col_err = 0;
134 /* lc_self is NULL: a LowCardinality column with an inherited
135 * dictionary hard-errors here. Real log schemas have none. */
136 0 SV **vals = decode_column(bbuf, blen, &bpos, nr, ct, &col_err, 0);
137 0 ▶ if (!vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (!vals) { 0 0 0
138 0 free_col_type(ct);
139 0 ▶ if (col_err) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_err) { 0 0 0
140 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
141 0 snprintf(errbuf, sizeof(errbuf), "malformed %s block", kind);
142 0 *errmsg = safe_strdup(errbuf);
143 0 return -1;
144 }
145 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
146 0 return 0;
147 }
148 {
149 uint64_t j;
150 0 ▶ for (j = 0; j < nr; j++) SvREFCNT_dec(vals[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nr; j++) SvREFCNT_dec(vals[j]); 0 0 0
151 }
152 0 Safefree(vals);
153 0 free_col_type(ct);
154 }
155 }
156 }
157  
158 #undef _BAIL
159  
160 0 ▶ if (!decompressed) pos = bpos;
Branch: 1 branch
BranchTrueFalseTotal
if (!decompressed) pos = bpos; 0 0 0
161 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
162 0 *outer_pos = pos;
163 0 return 1;
164 }
165  
166 /* Like parse_and_discard_block, but for each row of the block invokes
167 * `cb` with one hashref keyed by column name. Used by on_log.
168 * Caller has already verified cb is non-NULL. */
169 0 static int parse_and_emit_log_block(ev_clickhouse_t *self,
170 const char *buf, size_t len, size_t *outer_pos,
171 SV *cb, char **errmsg) {
172 0 size_t pos = *outer_pos;
173 int rc;
174 0 char *decompressed = NULL;
175 const char *bbuf;
176 size_t blen, bpos;
177  
178 0 rc = skip_native_string(buf, len, &pos);
179 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
180 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed log block"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed log block"); return -1; } 0 0 0
181  
182 0 ▶ if (self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress) { 0 0 0
183 0 int need_more = 0;
184 0 const char *lz4_err = NULL;
185 0 decompressed = ch_lz4_decompress_chain(buf, len, &pos, &blen,
186 &need_more, &lz4_err);
187 0 ▶ if (!decompressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!decompressed) { 0 0 0
188 0 ▶ if (need_more) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (need_more) return 0; 0 0 0
189 /* server log frames are not always compressed even with
190 * compress=1 negotiated — fall through to raw parsing. */
191 }
192 }
193 0 ▶ if (decompressed) { bbuf = decompressed; bpos = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) { bbuf = decompressed; bpos = 0; } 0 0 0
194 0 else { bbuf = buf; blen = len; bpos = pos; }
195  
196 #define _BAIL_LOG(rc_val) do { \
197 if (decompressed) Safefree(decompressed); \
198 if (rc_val < 0) { *errmsg = safe_strdup("malformed log block"); } \
199 return rc_val; \
200 } while (0)
201  
202 0 ▶ if (self->server_revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO) { 0 0 0
203 0 rc = skip_block_info(bbuf, blen, &bpos);
204 0 ▶ if (rc <= 0) _BAIL_LOG(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
205 }
206 uint64_t nc, nr;
207 0 rc = read_varuint(bbuf, blen, &bpos, &nc);
208 0 ▶ if (rc <= 0) _BAIL_LOG(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
209 0 rc = read_varuint(bbuf, blen, &bpos, &nr);
210 0 ▶ if (rc <= 0) _BAIL_LOG(rc);
Branch: 3 branches
BranchTrueFalseTotal
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
if (rc <= 0) _BAIL_LOG(rc); 0 0 0
211  
212 /* Collect column name + decoded values, then assemble per-row HVs. */
213 0 char **names = NULL;
214 0 SV ***data = NULL; /* data[col][row] */
215 0 ▶ if (nc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nc > 0) { 0 0 0
216 /* More columns than remaining bytes proves nothing yet: on a
217 * live stream the rest may simply not have arrived. Wait for
218 * more; the column loop below rules on malformed data. */
219 0 ▶ if (nc > (uint64_t)(blen - bpos)) _BAIL_LOG(0);
Branch: 2 branches
BranchTrueFalseTotal
if (nc > (uint64_t)(blen - bpos)) _BAIL_LOG(0); 0 0 0
if (nc > (uint64_t)(blen - bpos)) _BAIL_LOG(0); 0 0 0
220 0 ▶ Newxz(names, nc, char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(names, nc, char *); 0 0 0
221 0 ▶ Newxz(data, nc, SV **);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(data, nc, SV **); 0 0 0
222 }
223 /* err_seen: -1 = malformed, 0 = success / need-more, +1 = all columns
224 * parsed cleanly. We distinguish "loop completed all nc columns" from
225 * "loop broke early needing more data" via the explicit flag rather
226 * than inspecting bpos, since bpos always advances past the header. */
227 0 int err_seen = 1;
228 0 ▶ for (uint64_t c = 0; c < nc; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint64_t c = 0; c < nc; c++) { 0 0 0
229 const char *cname; size_t cname_len;
230 0 rc = read_native_string_ref(bbuf, blen, &bpos, &cname, &cname_len);
231 0 ▶ if (rc <= 0) { err_seen = rc < 0 ? -1 : 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc <= 0) { err_seen = rc < 0 ? -1 : 0; break; } 0 0 0
if (rc <= 0) { err_seen = rc < 0 ? -1 : 0; break; } 0 0 0
232 0 Newx(names[c], cname_len + 1, char);
233 0 Copy(cname, names[c], cname_len, char);
234 0 names[c][cname_len] = '\0';
235  
236 const char *ctype; size_t ctype_len;
237 0 rc = read_native_string_ref(bbuf, blen, &bpos, &ctype, &ctype_len);
238 0 ▶ if (rc <= 0) { err_seen = rc < 0 ? -1 : 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc <= 0) { err_seen = rc < 0 ? -1 : 0; break; } 0 0 0
if (rc <= 0) { err_seen = rc < 0 ? -1 : 0; break; } 0 0 0
239 0 ▶ if (bpos >= blen) { err_seen = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (bpos >= blen) { err_seen = 0; break; } 0 0 0
240 0 ▶ if ((uint8_t)bbuf[bpos]) { err_seen = -1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if ((uint8_t)bbuf[bpos]) { err_seen = -1; break; } 0 0 0
241 0 bpos++;
242 0 ▶ if (nr > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nr > 0) { 0 0 0
243 0 col_type_t *ct = parse_col_type(ctype, ctype_len);
244 0 int col_err = 0;
245 /* lc_self is NULL: a LowCardinality column with an inherited
246 * dictionary hard-errors here. Real log schemas have none. */
247 0 SV **vals = decode_column(bbuf, blen, &bpos, nr, ct, &col_err, 0);
248 0 free_col_type(ct);
249 0 ▶ if (!vals) { err_seen = col_err ? -1 : 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!vals) { err_seen = col_err ? -1 : 0; break; } 0 0 0
if (!vals) { err_seen = col_err ? -1 : 0; break; } 0 0 0
250 0 data[c] = vals;
251 }
252 }
253  
254 0 ▶ if (err_seen == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (err_seen == 1) { 0 0 0
255 /* Pin cb across the loop: an on_log handler that calls
256 * $ch->on_log(undef) would otherwise free the CV mid-iteration. */
257 0 SvREFCNT_inc(cb);
258 0 self->callback_depth++;
259 0 ▶ for (uint64_t r = 0; r < nr; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint64_t r = 0; r < nr; r++) { 0 0 0
260 0 HV *row = newHV();
261 0 ▶ for (uint64_t c = 0; c < nc; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint64_t c = 0; c < nc; c++) { 0 0 0
262 0 SV *v = data[c][r];
263 0 SvREFCNT_inc(v); /* hv_store consumes one ref */
264 0 (void)hv_store(row, names[c], strlen(names[c]), v, 0);
265 }
266 0 dSP;
267 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
268 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
269 0 PUSHs(sv_2mortal(newRV_noinc((SV *)row)));
270 0 PUTBACK;
271 0 call_sv(cb, G_EVAL | G_VOID | G_DISCARD);
272 0 ▶ WARN_AND_CLEAR_ERRSV("on_log");
Branch: 4 branches
BranchTrueFalseTotal
WARN_AND_CLEAR_ERRSV("on_log"); 0 0 0
WARN_AND_CLEAR_ERRSV("on_log"); 0 0 0
WARN_AND_CLEAR_ERRSV("on_log"); 0 0 0
WARN_AND_CLEAR_ERRSV("on_log"); 0 0 0
273 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
274 /* freed state is authoritatively handled by check_destroyed below */
275 0 ▶ if (self->magic == EV_CH_FREED) break;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_CH_FREED) break; 0 0 0
276 }
277 /* Drop the cb pin BEFORE callback_depth-- : dropping the last ref
278 * to the on_log CV (a handler may have reassigned on_log) can free
279 * a closure that captured $ch, triggering DESTROY. Doing it while
280 * callback_depth is still raised keeps Safefree(self) deferred so
281 * the check_destroyed below can detect and finalize it. */
282 0 SvREFCNT_dec(cb);
283 0 self->callback_depth--;
284 }
285  
286 /* Free locally-collected SVs + names regardless of self's state. */
287 0 ▶ for (uint64_t c = 0; c < nc; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint64_t c = 0; c < nc; c++) { 0 0 0
288 0 ▶ if (data && data[c]) {
Branch: 2 branches
BranchTrueFalseTotal
if (data && data[c]) { 0 0 0
if (data && data[c]) { 0 0 0
289 0 ▶ for (uint64_t r = 0; r < nr; r++) SvREFCNT_dec(data[c][r]);
Branch: 1 branch
BranchTrueFalseTotal
for (uint64_t r = 0; r < nr; r++) SvREFCNT_dec(data[c][r]); 0 0 0
290 0 Safefree(data[c]);
291 }
292 0 ▶ if (names && names[c]) Safefree(names[c]);
Branch: 2 branches
BranchTrueFalseTotal
if (names && names[c]) Safefree(names[c]); 0 0 0
if (names && names[c]) Safefree(names[c]); 0 0 0
293 }
294 0 ▶ if (data) Safefree(data);
Branch: 1 branch
BranchTrueFalseTotal
if (data) Safefree(data); 0 0 0
295 0 ▶ if (names) Safefree(names);
Branch: 1 branch
BranchTrueFalseTotal
if (names) Safefree(names); 0 0 0
296  
297 #undef _BAIL_LOG
298  
299 /* Single cleanup point: decompressed always needs freeing if allocated,
300 * regardless of which branch we return from. */
301 int ret;
302 /* on_log — or dropping the pinned cb above — may have freed self.
303 * check_destroyed finalizes any deferred Safefree and reports it. */
304 0 ▶ if (check_destroyed(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) { 0 0 0
305 0 ret = -2;
306 0 ▶ } else if (err_seen < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (err_seen < 0) { 0 0 0
307 0 *errmsg = safe_strdup("malformed log block");
308 0 ret = -1;
309 0 ▶ } else if (err_seen == 0) { /* need more data — do not advance outer_pos */
Branch: 1 branch
BranchTrueFalseTotal
} else if (err_seen == 0) { /* need more data — do not advance outer_pos */ 0 0 0
310 0 ret = 0;
311 } else {
312 /* For the uncompressed path bpos has been advanced inside the outer
313 * buf; copy it back to pos. For the LZ4 path pos was already advanced
314 * past the chain by ch_lz4_decompress_chain, and bpos is an offset
315 * into the freed decompressed buffer — don't touch pos. */
316 0 ▶ if (!decompressed) pos = bpos;
Branch: 1 branch
BranchTrueFalseTotal
if (!decompressed) pos = bpos; 0 0 0
317 0 *outer_pos = pos;
318 0 ret = 1;
319 }
320 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
321 0 return ret;
322 }
323  
324 /* Dispatch the on_progress callback with five UInt values. Returns -2 if
325 * the handler freed self, 0 otherwise. Shared between SERVER_PROGRESS
326 * (per-packet) and EndOfStream (flush of any uncoalesced accumulator). */
327 0 static int fire_progress_cb(ev_clickhouse_t *self, const uint64_t pp[5]) {
328 int i;
329 0 dSP;
330 0 self->callback_depth++;
331 0 ENTER; SAVETMPS;
332 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
333 0 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 0 0
334 0 ▶ for (i = 0; i < 5; i++) PUSHs(sv_2mortal(newSVuv(pp[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) PUSHs(sv_2mortal(newSVuv(pp[i]))); 0 0 0
335 0 PUTBACK;
336 0 PINNED_CALL_SV(self->on_progress, G_DISCARD | G_EVAL);
337 0 ▶ WARN_AND_CLEAR_ERRSV("progress handler");
Branch: 4 branches
BranchTrueFalseTotal
WARN_AND_CLEAR_ERRSV("progress handler"); 0 0 0
WARN_AND_CLEAR_ERRSV("progress handler"); 0 0 0
WARN_AND_CLEAR_ERRSV("progress handler"); 0 0 0
WARN_AND_CLEAR_ERRSV("progress handler"); 0 0 0
338 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
339 0 self->callback_depth--;
340 0 ▶ return check_destroyed(self) ? -2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return check_destroyed(self) ? -2 : 0; 0 0 0
341 }
342  
343 50 static int parse_native_packet(ev_clickhouse_t *self, char **errmsg) {
344 50 const char *buf = self->recv_buf;
345 50 size_t len = self->recv_len;
346 50 size_t pos = 0;
347 uint64_t ptype;
348 int rc;
349  
350 50 rc = read_varuint(buf, len, &pos, &ptype);
351 50 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 50 50
352 50 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 0 50 50
353 0 *errmsg = safe_strdup("malformed packet type");
354 0 return -1;
355 }
356  
357 50 switch ((int)ptype) {
358  
359 21 case SERVER_HELLO: {
360 21 char *sname = NULL;
361 uint64_t major, minor, revision;
362  
363 21 rc = read_native_string_alloc(buf, len, &pos, &sname, NULL);
364 21 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 21 21
365 21 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed server name"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed server name"); return -1; } 0 21 21
366  
367 21 rc = read_varuint(buf, len, &pos, &major);
368 21 ▶ if (rc == 0) { Safefree(sname); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { Safefree(sname); return 0; } 0 21 21
369 21 ▶ if (rc < 0) { Safefree(sname); *errmsg = safe_strdup("malformed server version major"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { Safefree(sname); *errmsg = safe_strdup("malformed server version major"); return -1; } 0 21 21
370  
371 21 rc = read_varuint(buf, len, &pos, &minor);
372 21 ▶ if (rc == 0) { Safefree(sname); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { Safefree(sname); return 0; } 0 21 21
373 21 ▶ if (rc < 0) { Safefree(sname); *errmsg = safe_strdup("malformed server version minor"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { Safefree(sname); *errmsg = safe_strdup("malformed server version minor"); return -1; } 0 21 21
374  
375 21 rc = read_varuint(buf, len, &pos, &revision);
376 21 ▶ if (rc == 0) { Safefree(sname); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { Safefree(sname); return 0; } 0 21 21
377 21 ▶ if (rc < 0) { Safefree(sname); *errmsg = safe_strdup("malformed server revision"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { Safefree(sname); *errmsg = safe_strdup("malformed server revision"); return -1; } 0 21 21
378  
379 21 ▶ CLEAR_STR(self->server_name);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_STR(self->server_name); 1 20 21
380 21 self->server_name = sname;
381 21 self->server_version_major = (unsigned int)major;
382 21 self->server_version_minor = (unsigned int)minor;
383 21 self->server_revision = (unsigned int)revision;
384  
385 21 ▶ if (self->server_revision < DBMS_MIN_SUPPORTED_SERVER_REVISION) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision < DBMS_MIN_SUPPORTED_SERVER_REVISION) { 1 20 21
386 char emsg[128];
387 1 snprintf(emsg, sizeof(emsg),
388 "ClickHouse server revision %u too old (minimum: %u)",
389 self->server_revision,
390 DBMS_MIN_SUPPORTED_SERVER_REVISION);
391 1 *errmsg = safe_strdup(emsg);
392 1 return -1;
393 }
394  
395 /* The server emits these fields per the negotiated revision =
396 * min(server_rev, our advertised CH_CLIENT_REVISION). Gate on
397 * server_revision so we don't read garbage from older servers. */
398 20 ▶ if (self->server_revision >= DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision >= DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE) { 20 0 20
399 20 char *tz = NULL;
400 20 rc = read_native_string_alloc(buf, len, &pos, &tz, NULL);
401 20 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 20 20
402 20 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed timezone"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed timezone"); return -1; } 0 20 20
403 20 ▶ CLEAR_STR(self->server_timezone);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_STR(self->server_timezone); 1 19 20
404 20 self->server_timezone = tz;
405 }
406  
407 20 ▶ if (self->server_revision >= DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision >= DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME) { 20 0 20
408 20 char *dn = NULL;
409 20 rc = read_native_string_alloc(buf, len, &pos, &dn, NULL);
410 20 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 20 20
411 20 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed display name"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed display name"); return -1; } 0 20 20
412 20 ▶ CLEAR_STR(self->server_display_name);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_STR(self->server_display_name); 1 19 20
413 20 self->server_display_name = dn;
414 }
415  
416 20 ▶ if (self->server_revision >= DBMS_MIN_REVISION_WITH_VERSION_PATCH) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision >= DBMS_MIN_REVISION_WITH_VERSION_PATCH) { 20 0 20
417 uint64_t patch;
418 20 rc = read_varuint(buf, len, &pos, &patch);
419 20 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 20 20
420 20 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed version patch"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed version patch"); return -1; } 0 20 20
421 20 self->server_version_patch = (unsigned int)patch;
422 }
423  
424 /* consume from recv_buf */
425 20 recv_consume(self, pos);
426 20 return 4;
427 }
428  
429 14 case SERVER_DATA:
430 case SERVER_TOTALS:
431 case SERVER_EXTREMES: {
432 uint64_t num_cols, num_rows;
433 const char *dbuf; /* data buffer (may point to decompressed data) */
434 size_t dlen, dpos;
435 14 char *decompressed = NULL;
436  
437 /* table name — outside compression */
438 14 rc = skip_native_string(buf, len, &pos);
439 14 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 14 14
440 14 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed table name"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed table name"); return -1; } 0 14 14
441  
442 14 ▶ if (self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress) { 0 14 14
443 /* Decompress the block body — may span multiple LZ4 sub-blocks. */
444 0 int need_more = 0;
445 0 const char *lz4_err = NULL;
446 0 decompressed = ch_lz4_decompress_chain(buf, len, &pos, &dlen,
447 &need_more, &lz4_err);
448 0 ▶ if (!decompressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!decompressed) { 0 0 0
449 0 ▶ if (need_more) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (need_more) return 0; 0 0 0
450 0 ▶ *errmsg = safe_strdup(lz4_err ? lz4_err : "LZ4 decompression failed");
Branch: 1 branch
BranchTrueFalseTotal
*errmsg = safe_strdup(lz4_err ? lz4_err : "LZ4 decompression failed"); 0 0 0
451 0 return -1;
452 }
453 0 dbuf = decompressed;
454 0 dpos = 0;
455 } else {
456 14 dbuf = buf;
457 14 dlen = len;
458 14 dpos = pos;
459 }
460  
461 /* block info */
462 14 ▶ if (self->server_revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO) { 14 0 14
463 14 rc = skip_block_info(dbuf, dlen, &dpos);
464 14 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 0 14 14
465 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
466 0 return 0;
467 }
468 14 ▶ if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed block info"); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed block info"); return -1; } 0 14 14
if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed block info"); return -1; } 0 0 0
469 }
470  
471 14 rc = read_varuint(dbuf, dlen, &dpos, &num_cols);
472 14 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 0 14 14
473 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
474 0 return 0;
475 }
476 14 ▶ if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed num_cols"); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed num_cols"); return -1; } 0 14 14
if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed num_cols"); return -1; } 0 0 0
477  
478 14 rc = read_varuint(dbuf, dlen, &dpos, &num_rows);
479 14 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 0 14 14
480 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
481 0 return 0;
482 }
483 14 ▶ if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed num_rows"); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed num_rows"); return -1; } 0 14 14
if (rc < 0) { if (decompressed) Safefree(decompressed); *errmsg = safe_strdup("malformed num_rows"); return -1; } 0 0 0
484  
485 /* Empty data block — skip or handle column names/types */
486 14 ▶ if (num_rows == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_rows == 0) { 14 0 14
487 /* INSERT two-phase: server sent sample block with column structure */
488 14 ▶ if (self->native_state == NATIVE_WAIT_INSERT_META
Branch: 1 branch
BranchTrueFalseTotal
if (self->native_state == NATIVE_WAIT_INSERT_META 14 0 14
489 14 ▶ && (self->insert_data || self->insert_av) && num_cols > 0) {
Branch: 3 branches
BranchTrueFalseTotal
&& (self->insert_data || self->insert_av) && num_cols > 0) { 14 0 14
&& (self->insert_data || self->insert_av) && num_cols > 0) { 14 0 14
&& (self->insert_data || self->insert_av) && num_cols > 0) { 14 0 14
490 const char **cnames;
491 size_t *cname_lens;
492 const char **ctypes_str;
493 size_t *ctype_lens;
494 col_type_t **ctypes;
495 char *data_pkt;
496 size_t data_pkt_len;
497 uint64_t c;
498 14 int meta_hard = 0; /* 1 = hard error, 0 = need more */
499 14 const char *meta_err = NULL; /* non-NULL → goto meta_cleanup */
500  
501 /* More columns than remaining bytes proves nothing yet: on
502 * a live stream the rest may simply not have arrived (short
503 * first read, split LZ4 chain). Wait for more; the column
504 * loop below rules on genuinely malformed data. */
505 14 ▶ if (num_cols > (uint64_t)(dlen - dpos)) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_cols > (uint64_t)(dlen - dpos)) { 0 14 14
506 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
507 0 return 0;
508 }
509 14 ▶ Newxz(cnames, num_cols, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(cnames, num_cols, const char *); 0 14 14
510 14 ▶ Newxz(cname_lens, num_cols, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(cname_lens, num_cols, size_t); 0 14 14
511 14 ▶ Newxz(ctypes_str, num_cols, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(ctypes_str, num_cols, const char *); 0 14 14
512 14 ▶ Newxz(ctype_lens, num_cols, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(ctype_lens, num_cols, size_t); 0 14 14
513 14 ▶ Newxz(ctypes, num_cols, col_type_t *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(ctypes, num_cols, col_type_t *); 0 14 14
514  
515 28 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 14 14 28
516 14 rc = read_native_string_ref(dbuf, dlen, &dpos,
517 14 &cnames[c], &cname_lens[c]);
518 14 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 0 14 14
519 0 meta_hard = (rc < 0);
520 0 meta_err = "malformed cname";
521 0 goto meta_cleanup;
522 }
523 14 rc = read_native_string_ref(dbuf, dlen, &dpos,
524 14 &ctypes_str[c], &ctype_lens[c]);
525 14 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 0 14 14
526 0 meta_hard = (rc < 0);
527 0 meta_err = "malformed ctype";
528 0 goto meta_cleanup;
529 }
530 14 ctypes[c] = parse_col_type(ctypes_str[c], ctype_lens[c]);
531 14 col_type_default_tz(ctypes[c],
532 14 effective_tz(self, self->query_session_tz),
533 14 &self->zones);
534  
535 /* custom serialization flag (revision >= 54446) */
536 14 ▶ if (dpos >= dlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (dpos >= dlen) { 0 14 14
537 0 meta_hard = 0;
538 0 meta_err = "truncated custom_ser";
539 0 goto meta_cleanup;
540 }
541 14 ▶ if ((uint8_t)dbuf[dpos]) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint8_t)dbuf[dpos]) { 0 14 14
542 0 meta_hard = 1;
543 0 meta_err = "custom serialization not supported";
544 0 goto meta_cleanup;
545 }
546 14 dpos++;
547 }
548 14 goto meta_ok;
549  
550 0 meta_cleanup:
551 0 ▶ for (c = 0; c < num_cols; c++) if (ctypes[c]) free_col_type(ctypes[c]);
Branch: 2 branches
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) if (ctypes[c]) free_col_type(ctypes[c]); 0 0 0
for (c = 0; c < num_cols; c++) if (ctypes[c]) free_col_type(ctypes[c]); 0 0 0
552 0 Safefree(cnames); Safefree(cname_lens);
553 0 Safefree(ctypes_str); Safefree(ctype_lens);
554 0 Safefree(ctypes);
555 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
556 0 ▶ if (meta_hard) { *errmsg = safe_strdup(meta_err); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (meta_hard) { *errmsg = safe_strdup(meta_err); return -1; } 0 0 0
557 0 return 0;
558  
559 14 meta_ok: ;
560  
561 14 ▶ if (self->cancel_after_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cancel_after_meta) { 2 12 14
562 4 ▶ for (c = 0; c < num_cols; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) 2 2 4
563 2 free_col_type(ctypes[c]);
564 2 Safefree(cnames); Safefree(cname_lens);
565 2 Safefree(ctypes_str); Safefree(ctype_lens);
566 2 Safefree(ctypes);
567 2 free_insert_default_state(self);
568 2 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 2 2
569 2 else pos = dpos;
570 2 recv_consume(self, pos);
571 2 self->cancel_after_meta = 0;
572 2 self->native_state = NATIVE_WAIT_RESULT;
573 2 Newx(data_pkt, 1, char);
574 2 data_pkt[0] = (char)CLIENT_CANCEL;
575 2 send_replace(self, data_pkt, 1);
576 2 ▶ if (try_write(self)) return -2;
Branch: 1 branch
BranchTrueFalseTotal
if (try_write(self)) return -2; 0 2 2
577 2 return 1;
578 }
579  
580 /* Build binary data block from stored data */
581 char ibuf[640];
582 12 col_default_t *cdefs = NULL;
583 /* Resolving defaults and the AV build stringify user SVs,
584 * so overload/tie runs arbitrary Perl that may destroy/
585 * reset/finish this very connection. Pin the rows
586 * (teardown would free them) and hold depth (DESTROY then
587 * defers Safefree); re-validate below before touching
588 * self. */
589 12 SV *av_pin = self->insert_av;
590 12 int gen0 = self->connect_gen;
591 12 ibuf[0] = '\0';
592 12 ▶ if (av_pin) SvREFCNT_inc(av_pin);
Branch: 1 branch
BranchTrueFalseTotal
if (av_pin) SvREFCNT_inc(av_pin); 12 0 12
593 12 self->callback_depth++;
594 12 ▶ if (self->insert_empty_as_default || self->insert_defaults) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->insert_empty_as_default || self->insert_defaults) { 12 0 12
if (self->insert_empty_as_default || self->insert_defaults) { 0 12 12
595 0 cdefs = resolve_insert_defaults_guarded(aTHX_ self,
596 cnames, cname_lens, ctypes,
597 ctypes_str, ctype_lens,
598 (int)num_cols, ibuf, sizeof(ibuf));
599 0 ▶ if (!cdefs && !ibuf[0])
Branch: 2 branches
BranchTrueFalseTotal
if (!cdefs && !ibuf[0]) 0 0 0
if (!cdefs && !ibuf[0]) 0 0 0
600 0 snprintf(ibuf, sizeof(ibuf),
601 "failed to resolve column defaults");
602 }
603 12 ▶ if (self->magic != EV_CH_MAGIC || self->connect_gen != gen0
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic != EV_CH_MAGIC || self->connect_gen != gen0 12 0 12
if (self->magic != EV_CH_MAGIC || self->connect_gen != gen0 12 0 12
604 12 ▶ || !self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
|| !self->connected) { 0 12 12
605 0 data_pkt = NULL;
606 0 data_pkt_len = 0;
607 12 ▶ } else if (!cdefs && (self->insert_empty_as_default
Branch: 2 branches
BranchTrueFalseTotal
} else if (!cdefs && (self->insert_empty_as_default 12 0 12
} else if (!cdefs && (self->insert_empty_as_default 12 0 12
608 12 ▶ || self->insert_defaults)) {
Branch: 1 branch
BranchTrueFalseTotal
|| self->insert_defaults)) { 0 12 12
609 0 data_pkt = NULL;
610 0 data_pkt_len = (size_t)-1;
611 12 ▶ } else if (self->insert_native_body) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (self->insert_native_body) { 0 12 12
612 0 data_pkt = build_native_body_packet(self, self->insert_data,
613 self->insert_data_len, cnames, cname_lens,
614 ctypes_str, ctype_lens, (int)num_cols, &data_pkt_len,
615 ibuf, sizeof(ibuf));
616 12 ▶ } else if (self->insert_av) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (self->insert_av) { 12 0 12
617 12 data_pkt = build_native_insert_data_from_av_guarded(aTHX_ self,
618 self->insert_av,
619 cnames, cname_lens, ctypes_str, ctype_lens,
620 ctypes, (int)num_cols, cdefs, &data_pkt_len,
621 ibuf, sizeof(ibuf));
622 } else {
623 0 data_pkt = build_native_insert_data(self,
624 0 self->insert_data, self->insert_data_len,
625 cnames, cname_lens, ctypes_str, ctype_lens,
626 ctypes, (int)num_cols, cdefs, &data_pkt_len,
627 ibuf, sizeof(ibuf));
628 }
629 /* Defaults may hold user SVs: drop them and the pin BEFORE
630 * depth--, so a value holding the last $ch ref DESTROYs
631 * into depth>0 (cf. on_data). */
632 12 free_col_defaults(cdefs, (int)num_cols);
633 12 cdefs = NULL;
634 12 ▶ if (av_pin) SvREFCNT_dec(av_pin);
Branch: 1 branch
BranchTrueFalseTotal
if (av_pin) SvREFCNT_dec(av_pin); 12 0 12
635 12 self->callback_depth--;
636 {
637 12 ▶ if (self->magic != EV_CH_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_CH_MAGIC) { 0 12 12
638 0 ▶ if (data_pkt) Safefree(data_pkt);
Branch: 1 branch
BranchTrueFalseTotal
if (data_pkt) Safefree(data_pkt); 0 0 0
639 0 ▶ for (c = 0; c < num_cols; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) 0 0 0
640 0 free_col_type(ctypes[c]);
641 0 Safefree(cnames); Safefree(cname_lens);
642 0 Safefree(ctypes_str); Safefree(ctype_lens);
643 0 Safefree(ctypes);
644 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
645 0 check_destroyed(self);
646 0 return -2;
647 }
648 12 ▶ if (self->connect_gen != gen0 || !self->connected) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->connect_gen != gen0 || !self->connected) { 12 0 12
if (self->connect_gen != gen0 || !self->connected) { 0 12 12
649 /* reset()/finish() from magic: the INSERT was
650 * already failed by the teardown; drop the
651 * built block instead of sending it into the
652 * new/closed connection. */
653 0 ▶ if (data_pkt) Safefree(data_pkt);
Branch: 1 branch
BranchTrueFalseTotal
if (data_pkt) Safefree(data_pkt); 0 0 0
654 0 free_col_defaults(cdefs, (int)num_cols);
655 0 free_insert_default_state(self);
656 0 ▶ for (c = 0; c < num_cols; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) 0 0 0
657 0 free_col_type(ctypes[c]);
658 0 Safefree(cnames); Safefree(cname_lens);
659 0 Safefree(ctypes_str); Safefree(ctype_lens);
660 0 Safefree(ctypes);
661 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
662 /* recv is already empty (teardown zeroes it). */
663 0 return 0;
664 }
665 }
666  
667 24 ▶ for (c = 0; c < num_cols; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) 12 12 24
668 12 free_col_type(ctypes[c]);
669 12 Safefree(cnames); Safefree(cname_lens);
670 12 Safefree(ctypes_str); Safefree(ctype_lens);
671 12 Safefree(ctypes);
672 12 free_col_defaults(cdefs, (int)num_cols);
673 12 free_insert_default_state(self);
674  
675 {
676 /* Check encode-failure sentinel before freeing insert data */
677 12 ▶ int encode_failed = (!data_pkt && data_pkt_len == (size_t)-1);
Branch: 2 branches
BranchTrueFalseTotal
int encode_failed = (!data_pkt && data_pkt_len == (size_t)-1); 8 4 12
int encode_failed = (!data_pkt && data_pkt_len == (size_t)-1); 8 0 8
678  
679 /* Free stored INSERT data */
680 12 ▶ CLEAR_INSERT(self);
Branch: 2 branches
BranchTrueFalseTotal
CLEAR_INSERT(self); 0 12 12
CLEAR_INSERT(self); 12 0 12
681  
682 12 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 12 12
683 12 else pos = dpos;
684 12 recv_consume(self, pos);
685  
686 12 ▶ if (!data_pkt) {
Branch: 1 branch
BranchTrueFalseTotal
if (!data_pkt) { 8 4 12
687 /* Send empty Data block to complete the INSERT protocol */
688 native_buf_t fallback;
689 8 nbuf_init(&fallback);
690 8 nbuf_empty_data_block(&fallback, self);
691 8 data_pkt = fallback.data;
692 8 data_pkt_len = fallback.len;
693 8 ▶ if (encode_failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (encode_failed) { 8 0 8
694 char fbuf[700];
695 8 ▶ if (ibuf[0])
Branch: 1 branch
BranchTrueFalseTotal
if (ibuf[0]) 8 0 8
696 8 snprintf(fbuf, sizeof(fbuf), "native INSERT encoding "
697 "failed (%s)", ibuf);
698 else
699 0 snprintf(fbuf, sizeof(fbuf), "native INSERT encoding "
700 "failed (unsupported type or invalid value)");
701 8 self->insert_err = safe_strdup(fbuf);
702 }
703 }
704 }
705  
706 /* Cancellation can also run while encoding user values. */
707 12 self->native_state = NATIVE_WAIT_RESULT;
708 12 ▶ if (self->cancel_after_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cancel_after_meta) { 0 12 12
709 0 self->cancel_after_meta = 0;
710 0 Safefree(data_pkt);
711 0 Newx(data_pkt, 1, char);
712 0 data_pkt[0] = (char)CLIENT_CANCEL;
713 0 data_pkt_len = 1;
714 0 ▶ CLEAR_STR(self->insert_err);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_STR(self->insert_err); 0 0 0
715 }
716 12 send_replace(self, data_pkt, data_pkt_len);
717 12 ▶ if (try_write(self)) return -2;
Branch: 1 branch
BranchTrueFalseTotal
if (try_write(self)) return -2; 0 12 12
718 12 return 1;
719 }
720  
721 /* INSERT two-phase with 0-column sample block: free data,
722 * send empty Data block, transition to WAIT_RESULT */
723 0 ▶ if (self->native_state == NATIVE_WAIT_INSERT_META
Branch: 1 branch
BranchTrueFalseTotal
if (self->native_state == NATIVE_WAIT_INSERT_META 0 0 0
724 0 ▶ && (self->insert_data || self->insert_av) && num_cols == 0) {
Branch: 3 branches
BranchTrueFalseTotal
&& (self->insert_data || self->insert_av) && num_cols == 0) { 0 0 0
&& (self->insert_data || self->insert_av) && num_cols == 0) { 0 0 0
&& (self->insert_data || self->insert_av) && num_cols == 0) { 0 0 0
725 native_buf_t fallback;
726  
727 0 ▶ CLEAR_INSERT(self);
Branch: 2 branches
BranchTrueFalseTotal
CLEAR_INSERT(self); 0 0 0
CLEAR_INSERT(self); 0 0 0
728 0 free_insert_default_state(self);
729  
730 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
731 0 else pos = dpos;
732 0 recv_consume(self, pos);
733  
734 0 nbuf_init(&fallback);
735 0 nbuf_empty_data_block(&fallback, self);
736 0 self->native_state = NATIVE_WAIT_RESULT;
737 0 ▶ if (self->cancel_after_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cancel_after_meta) { 0 0 0
738 0 self->cancel_after_meta = 0;
739 0 fallback.len = 0;
740 0 nbuf_u8(&fallback, (uint8_t)CLIENT_CANCEL);
741 } else {
742 0 self->insert_err = safe_strdup(
743 "INSERT failed: server sent 0-column sample block");
744 }
745 0 send_replace(self, fallback.data, fallback.len);
746 0 ▶ if (try_write(self)) return -2;
Branch: 1 branch
BranchTrueFalseTotal
if (try_write(self)) return -2; 0 0 0
747 0 return 1;
748 }
749  
750 /* Normal empty block — capture column names/types so callers can
751 * inspect schema after a zero-row SELECT. Reset only when we have
752 * columns to record so the terminating empty block (num_cols=0)
753 * doesn't wipe the schema we already captured; this also drops
754 * any partial state from a need-more retry. */
755 {
756 uint64_t c;
757 0 ▶ if (num_cols > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_cols > 0) { 0 0 0
758 0 ▶ CLEAR_SV(self->native_col_names);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_col_names); 0 0 0
759 0 ▶ CLEAR_SV(self->native_col_types);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_col_types); 0 0 0
760 0 self->native_col_names = newAV();
761 0 self->native_col_types = newAV();
762 }
763 0 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 0 0 0
764 const char *cname, *ctype;
765 size_t cname_len, ctype_len;
766  
767 0 rc = read_native_string_ref(dbuf, dlen, &dpos, &cname, &cname_len);
768 0 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 0 0 0
769 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
770 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed cname"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed cname"); return -1; } 0 0 0
771 0 return 0;
772 }
773 0 av_push(self->native_col_names, newSVpvn(cname, cname_len));
774  
775 0 rc = read_native_string_ref(dbuf, dlen, &dpos, &ctype, &ctype_len);
776 0 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 0 0 0
777 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
778 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); return -1; } 0 0 0
779 0 return 0;
780 }
781 0 av_push(self->native_col_types, newSVpvn(ctype, ctype_len));
782  
783 /* custom serialization flag (revision >= 54446) */
784 0 ▶ if (dpos >= dlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (dpos >= dlen) { 0 0 0
785 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
786 0 return 0;
787 }
788 0 ▶ if ((uint8_t)dbuf[dpos]) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint8_t)dbuf[dpos]) { 0 0 0
789 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
790 0 *errmsg = safe_strdup("custom serialization not supported");
791 0 return -1;
792 }
793 0 dpos++;
794 }
795 }
796 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
797 0 else pos = dpos; /* uncompressed: advance pos to match dpos */
798 0 recv_consume(self, pos);
799 0 return 1;
800 }
801  
802 /* Decode columns and convert to rows */
803 {
804 0 SV ***columns = NULL;
805 0 col_type_t **col_types = NULL;
806 0 const char **cnames = NULL;
807 0 size_t *cname_lens = NULL;
808 uint64_t c, r;
809 0 int named = (self->decode_flags & DECODE_NAMED_ROWS) ? 1 : 0;
810  
811 /* More columns than remaining bytes proves nothing yet: on
812 * a live stream the rest may simply not have arrived (short
813 * first read, split LZ4 chain). Wait for more; the column
814 * loop below rules on genuinely malformed data. */
815 0 ▶ if (num_cols > (uint64_t)(dlen - dpos)) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_cols > (uint64_t)(dlen - dpos)) { 0 0 0
816 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
817 0 return 0;
818 }
819 0 ▶ Newxz(columns, num_cols, SV**);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(columns, num_cols, SV**); 0 0 0
820 0 ▶ Newxz(col_types, num_cols, col_type_t*);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(col_types, num_cols, col_type_t*); 0 0 0
821 0 ▶ if (named) {
Branch: 1 branch
BranchTrueFalseTotal
if (named) { 0 0 0
822 0 ▶ Newxz(cnames, num_cols, const char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(cnames, num_cols, const char *); 0 0 0
823 0 ▶ Newx(cname_lens, num_cols, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(cname_lens, num_cols, size_t); 0 0 0
824 }
825  
826 0 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 0 0 0
827 const char *cname, *ctype;
828 size_t cname_len, ctype_len;
829  
830 0 rc = read_native_string_ref(dbuf, dlen, &dpos, &cname, &cname_len);
831 0 ▶ if (rc == 0) goto data_need_more;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) goto data_need_more; 0 0 0
832 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed cname"); goto data_error; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed cname"); goto data_error; } 0 0 0
833  
834 0 ▶ if (named) {
Branch: 1 branch
BranchTrueFalseTotal
if (named) { 0 0 0
835 0 cnames[c] = cname;
836 0 cname_lens[c] = cname_len;
837 }
838  
839 /* Allocate AVs on first column of first data block. Reset
840 * any partial state from a need-more retry: pipeline_advance
841 * cleared at dispatch, but if we returned 0 mid-loop the AVs
842 * may now hold a partial column list. */
843 0 ▶ if (c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) { 0 0 0
844 0 ▶ CLEAR_SV(self->native_col_names);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_col_names); 0 0 0
845 0 ▶ CLEAR_SV(self->native_col_types);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_col_types); 0 0 0
846 0 self->native_col_names = newAV();
847 0 self->native_col_types = newAV();
848 }
849 0 av_push(self->native_col_names, newSVpvn(cname, cname_len));
850  
851 0 rc = read_native_string_ref(dbuf, dlen, &dpos, &ctype, &ctype_len);
852 0 ▶ if (rc == 0) goto data_need_more;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) goto data_need_more; 0 0 0
853 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); goto data_error; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); goto data_error; } 0 0 0
854  
855 0 col_types[c] = parse_col_type(ctype, ctype_len);
856 0 col_type_default_tz(col_types[c],
857 0 effective_tz(self, self->query_session_tz),
858 0 &self->zones);
859 0 av_push(self->native_col_types, newSVpvn(ctype, ctype_len));
860  
861 /* custom serialization flag (revision >= 54446) */
862 0 ▶ if (dpos >= dlen) goto data_need_more;
Branch: 1 branch
BranchTrueFalseTotal
if (dpos >= dlen) goto data_need_more; 0 0 0
863 0 ▶ if ((uint8_t)dbuf[dpos]) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint8_t)dbuf[dpos]) { 0 0 0
864 0 *errmsg = safe_strdup("custom serialization not supported");
865 0 goto data_error;
866 }
867 0 dpos++;
868  
869 /* Allocate LC dict state on first column of first block */
870 0 ▶ if (c == 0 && !self->lc_dicts) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0 && !self->lc_dicts) { 0 0 0
if (c == 0 && !self->lc_dicts) { 0 0 0
871 0 ▶ Newxz(self->lc_dicts, num_cols, SV**);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(self->lc_dicts, num_cols, SV**); 0 0 0
872 0 ▶ Newxz(self->lc_dict_sizes, num_cols, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(self->lc_dict_sizes, num_cols, uint64_t); 0 0 0
873 0 self->lc_num_cols = (int)num_cols;
874 }
875  
876 {
877 0 int col_err = 0;
878 0 columns[c] = decode_column_ex(dbuf, dlen, &dpos, num_rows, col_types[c], &col_err, self->decode_flags, self, (int)c);
879 0 ▶ if (!columns[c]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!columns[c]) { 0 0 0
880 0 ▶ if (col_err) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_err) { 0 0 0
881 0 const char *bad_tz = col_type_zone_error(col_types[c]);
882 0 ▶ if (bad_tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (bad_tz) { 0 0 0
883 char zbuf[160];
884 0 snprintf(zbuf, sizeof(zbuf), "unknown timezone "
885 "'%.60s' or missing timezone database",
886 bad_tz);
887 0 *errmsg = safe_strdup(zbuf);
888 } else {
889 0 *errmsg = safe_strdup("decode_column failed");
890 }
891 0 goto data_error;
892 }
893 0 goto data_need_more;
894 }
895 }
896 }
897  
898 /* Convert column-oriented to row-oriented */
899 {
900 AV **target;
901 0 ▶ if (ptype == SERVER_TOTALS) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptype == SERVER_TOTALS) { 0 0 0
902 0 ▶ if (!self->native_totals) self->native_totals = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!self->native_totals) self->native_totals = newAV(); 0 0 0
903 0 target = &self->native_totals;
904 0 ▶ } else if (ptype == SERVER_EXTREMES) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ptype == SERVER_EXTREMES) { 0 0 0
905 0 ▶ if (!self->native_extremes) self->native_extremes = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!self->native_extremes) self->native_extremes = newAV(); 0 0 0
906 0 target = &self->native_extremes;
907 } else {
908 0 ▶ if (!self->native_rows) self->native_rows = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!self->native_rows) self->native_rows = newAV(); 0 0 0
909 0 target = &self->native_rows;
910 }
911  
912 0 ▶ if (named) {
Branch: 1 branch
BranchTrueFalseTotal
if (named) { 0 0 0
913 0 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 0 0 0
914 0 HV *hv = newHV();
915 0 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 0 0 0
916 0 ▶ if (!hv_store(hv, cnames[c], cname_lens[c], columns[c][r], 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(hv, cnames[c], cname_lens[c], columns[c][r], 0)) 0 0 0
917 0 SvREFCNT_dec(columns[c][r]);
918 }
919 0 av_push(*target, newRV_noinc((SV*)hv));
920 }
921 } else {
922 0 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 0 0 0
923 0 AV *row = newAV();
924 0 ▶ if (num_cols > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (num_cols > 0) 0 0 0
925 0 av_extend(row, num_cols - 1);
926 0 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 0 0 0
927 0 av_push(row, columns[c][r]);
928 }
929 0 av_push(*target, newRV_noinc((SV*)row));
930 }
931 }
932 }
933  
934 /* Fire on_data streaming callback if set (only for DATA, not TOTALS/EXTREMES) */
935 {
936 0 ▶ SV *on_data = (ptype == SERVER_DATA) ? peek_cb_on_data(self) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *on_data = (ptype == SERVER_DATA) ? peek_cb_on_data(self) : NULL; 0 0 0
937 0 ▶ if (on_data && self->native_rows) {
Branch: 2 branches
BranchTrueFalseTotal
if (on_data && self->native_rows) { 0 0 0
if (on_data && self->native_rows) { 0 0 0
938 /* Hold a reference across call_sv: a reentrant
939 * skip_pending() / cancel() in the handler would
940 * otherwise pop the cb_queue entry and free this
941 * callback while we're still invoking it. */
942 0 SvREFCNT_inc(on_data);
943 0 self->callback_depth++;
944 {
945 0 dSP;
946 0 ENTER; SAVETMPS;
947 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
948 0 PUSHs(sv_2mortal(newRV_inc((SV*)self->native_rows)));
949 0 PUTBACK;
950 0 call_sv(on_data, G_DISCARD | G_EVAL);
951 0 ▶ WARN_AND_CLEAR_ERRSV("on_data handler");
Branch: 4 branches
BranchTrueFalseTotal
WARN_AND_CLEAR_ERRSV("on_data handler"); 0 0 0
WARN_AND_CLEAR_ERRSV("on_data handler"); 0 0 0
WARN_AND_CLEAR_ERRSV("on_data handler"); 0 0 0
WARN_AND_CLEAR_ERRSV("on_data handler"); 0 0 0
952 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
953 }
954 /* Decrement on_data BEFORE callback_depth-- so a DESTROY
955 * triggered by the dec (closure holding last $ch ref)
956 * still sees callback_depth > 0 and defers Safefree(self). */
957 0 SvREFCNT_dec(on_data);
958 0 self->callback_depth--;
959 /* Clear accumulated rows for next block */
960 0 ▶ CLEAR_SV(self->native_rows);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_rows); 0 0 0
961 0 ▶ if (check_destroyed(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) { 0 0 0
962 0 ▶ if (cnames) Safefree(cnames);
Branch: 1 branch
BranchTrueFalseTotal
if (cnames) Safefree(cnames); 0 0 0
963 0 ▶ if (cname_lens) Safefree(cname_lens);
Branch: 1 branch
BranchTrueFalseTotal
if (cname_lens) Safefree(cname_lens); 0 0 0
964 0 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 0 0 0
965 0 Safefree(columns[c]);
966 0 free_col_type(col_types[c]);
967 }
968 0 Safefree(columns); Safefree(col_types);
969 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
970 0 return -2;
971 }
972 }
973 }
974  
975 /* Cleanup column arrays (SVs moved to rows, don't dec refcnt) */
976 0 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 0 0 0
977 0 Safefree(columns[c]);
978 0 free_col_type(col_types[c]);
979 }
980 0 Safefree(columns);
981 0 Safefree(col_types);
982 0 ▶ if (cnames) Safefree(cnames);
Branch: 1 branch
BranchTrueFalseTotal
if (cnames) Safefree(cnames); 0 0 0
983 0 ▶ if (cname_lens) Safefree(cname_lens);
Branch: 1 branch
BranchTrueFalseTotal
if (cname_lens) Safefree(cname_lens); 0 0 0
984 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
985 0 else pos = dpos; /* uncompressed: advance pos to match dpos */
986  
987 /* Consume from recv_buf */
988 0 recv_consume(self, pos);
989 0 return 1;
990  
991 0 data_error:
992 0 data_need_more:
993 /* Cleanup partial decode */
994 0 ▶ for (c = 0; c < num_cols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < num_cols; c++) { 0 0 0
995 0 ▶ if (columns[c]) {
Branch: 1 branch
BranchTrueFalseTotal
if (columns[c]) { 0 0 0
996 uint64_t j;
997 0 ▶ for (j = 0; j < num_rows; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_rows; j++) { 0 0 0
998 0 ▶ if (columns[c][j]) SvREFCNT_dec(columns[c][j]);
Branch: 1 branch
BranchTrueFalseTotal
if (columns[c][j]) SvREFCNT_dec(columns[c][j]); 0 0 0
999 }
1000 0 Safefree(columns[c]);
1001 }
1002 0 ▶ if (col_types[c]) free_col_type(col_types[c]);
Branch: 1 branch
BranchTrueFalseTotal
if (col_types[c]) free_col_type(col_types[c]); 0 0 0
1003 }
1004 0 Safefree(columns);
1005 0 Safefree(col_types);
1006 0 ▶ if (cnames) Safefree(cnames);
Branch: 1 branch
BranchTrueFalseTotal
if (cnames) Safefree(cnames); 0 0 0
1007 0 ▶ if (cname_lens) Safefree(cname_lens);
Branch: 1 branch
BranchTrueFalseTotal
if (cname_lens) Safefree(cname_lens); 0 0 0
1008 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
1009 0 ▶ if (*errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if (*errmsg) { 0 0 0
1010 /* data_error: flush recv_buf — data is malformed, cannot resume */
1011 0 self->recv_len = 0;
1012 0 return -1;
1013 }
1014 0 return 0;
1015 }
1016 }
1017  
1018 2 case SERVER_EXCEPTION: {
1019 /* code: Int32, name: String, message: String,
1020 * stack_trace: String, has_nested: UInt8 */
1021 int32_t code;
1022 const char *name, *msg, *stack;
1023 size_t name_len, msg_len, stack_len;
1024 uint8_t has_nested;
1025 char *err;
1026  
1027 /* We just read the top-level exception */
1028 2 rc = read_i32(buf, len, &pos, &code);
1029 2 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 2 2
1030 2 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed exception code"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed exception code"); return -1; } 0 2 2
1031  
1032 2 rc = read_native_string_ref(buf, len, &pos, &name, &name_len);
1033 2 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 2 2
1034 2 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed exception name"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed exception name"); return -1; } 0 2 2
1035  
1036 2 rc = read_native_string_ref(buf, len, &pos, &msg, &msg_len);
1037 2 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 2 2
1038 2 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed exception message"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed exception message"); return -1; } 0 2 2
1039  
1040 2 rc = read_native_string_ref(buf, len, &pos, &stack, &stack_len);
1041 2 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 2 2
1042 2 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed exception stack"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed exception stack"); return -1; } 0 2 2
1043  
1044 2 rc = read_u8(buf, len, &pos, &has_nested);
1045 2 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 2 2
1046 2 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed exception has_nested"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed exception has_nested"); return -1; } 0 2 2
1047  
1048 /* Skip nested exceptions — keep the top-level code, not the innermost. */
1049 2 ▶ while (has_nested) {
Branch: 1 branch
BranchTrueFalseTotal
while (has_nested) { 0 2 2
1050 int32_t nested_code;
1051 int i;
1052 0 rc = read_i32(buf, len, &pos, &nested_code);
1053 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1054 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } 0 0 0
1055 /* name, message, stack_trace */
1056 0 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 0 0 0
1057 0 rc = skip_native_string(buf, len, &pos);
1058 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1059 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } 0 0 0
1060 }
1061 0 rc = read_u8(buf, len, &pos, &has_nested);
1062 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1063 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } 0 0 0
1064 }
1065  
1066 2 self->last_error_code = code;
1067  
1068 2 Newx(err, msg_len + name_len + 64, char);
1069 2 snprintf(err, msg_len + name_len + 64, "Code: %d. %.*s: %.*s",
1070 (int)code, (int)name_len, name, (int)msg_len, msg);
1071  
1072 2 recv_consume(self, pos);
1073  
1074 2 *errmsg = err;
1075 2 return 5;
1076 }
1077  
1078 0 case SERVER_PROGRESS: {
1079 /* rows, bytes, total_rows, written_rows (>=54420), written_bytes (>=54420) */
1080 0 uint64_t pp[5] = {0};
1081 0 ▶ int n = (self->server_revision >= DBMS_MIN_REVISION_WITH_PROGRESS_WRITES) ? 5 : 3;
Branch: 1 branch
BranchTrueFalseTotal
int n = (self->server_revision >= DBMS_MIN_REVISION_WITH_PROGRESS_WRITES) ? 5 : 3; 0 0 0
1082 int i;
1083 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
1084 0 rc = read_varuint(buf, len, &pos, &pp[i]);
1085 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1086 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed progress packet"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed progress packet"); return -1; } 0 0 0
1087 }
1088  
1089 0 recv_consume(self, pos);
1090  
1091 0 ▶ if (self->on_progress) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->on_progress) { 0 0 0
1092 /* Coalesce when progress_period is set: the protocol sends
1093 * incremental deltas, so accumulate and fire at the cadence. */
1094 0 ▶ if (self->progress_period > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->progress_period > 0) { 0 0 0
1095 0 ▶ for (i = 0; i < 5; i++) self->progress_acc[i] += pp[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) self->progress_acc[i] += pp[i]; 0 0 0
1096 0 double now = ev_now(self->loop);
1097 0 ▶ if (now - self->progress_last < self->progress_period)
Branch: 1 branch
BranchTrueFalseTotal
if (now - self->progress_last < self->progress_period) 0 0 0
1098 0 return 1;
1099 0 self->progress_last = now;
1100 0 ▶ for (i = 0; i < 5; i++) { pp[i] = self->progress_acc[i]; self->progress_acc[i] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) { pp[i] = self->progress_acc[i]; self->progress_acc[i] = 0; } 0 0 0
1101 }
1102  
1103 0 ▶ if (fire_progress_cb(self, pp) < 0) return -2;
Branch: 1 branch
BranchTrueFalseTotal
if (fire_progress_cb(self, pp) < 0) return -2; 0 0 0
1104 }
1105  
1106 0 return 1;
1107 }
1108  
1109 0 case SERVER_PROFILE_INFO: {
1110 /* rows, blocks, bytes, applied_limit, rows_before_limit, calc_rows_before_limit */
1111 uint64_t pi[6];
1112 int i;
1113 0 ▶ for (i = 0; i < 6; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 6; i++) { 0 0 0
1114 0 rc = read_varuint(buf, len, &pos, &pi[i]);
1115 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1116 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed profile_info packet"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed profile_info packet"); return -1; } 0 0 0
1117 }
1118 0 self->profile_rows = pi[0];
1119 0 self->profile_bytes = pi[2];
1120 0 self->profile_rows_before_limit = pi[4];
1121 0 recv_consume(self, pos);
1122 0 return 1;
1123 }
1124  
1125 0 case SERVER_TABLE_COLUMNS: {
1126 /* Format: string(table_name) + string(column_description).
1127 * The description carries each column's DEFAULT/MATERIALIZED/
1128 * ALIAS kind and expression; keep it only when the pending
1129 * INSERT asked for empty_as_default or defaults. */
1130 int i;
1131 0 ▶ if ((self->insert_data || self->insert_av)
Branch: 2 branches
BranchTrueFalseTotal
if ((self->insert_data || self->insert_av) 0 0 0
if ((self->insert_data || self->insert_av) 0 0 0
1132 0 ▶ && (self->insert_empty_as_default || self->insert_defaults)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (self->insert_empty_as_default || self->insert_defaults)) { 0 0 0
&& (self->insert_empty_as_default || self->insert_defaults)) { 0 0 0
1133 0 char *desc = NULL;
1134 0 size_t p0 = pos;
1135 0 rc = skip_native_string(buf, len, &p0);
1136 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1137 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; } 0 0 0
1138 0 rc = read_native_string_alloc(buf, len, &p0, &desc, NULL);
1139 0 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 0 0 0
1140 0 Safefree(desc);
1141 0 return 0;
1142 }
1143 0 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 0 0 0
1144 0 Safefree(desc);
1145 0 *errmsg = safe_strdup("malformed table_columns packet");
1146 0 return -1;
1147 }
1148 0 free_insert_tc(self);
1149 0 ▶ if (desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (desc) { 0 0 0
1150 0 self->insert_tc = parse_table_columns_desc(desc, strlen(desc));
1151 0 Safefree(desc);
1152 }
1153 0 pos = p0;
1154 0 recv_consume(self, pos);
1155 0 return 1;
1156 }
1157 0 ▶ for (i = 0; i < 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2; i++) { 0 0 0
1158 0 rc = skip_native_string(buf, len, &pos);
1159 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1160 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; } 0 0 0
1161 }
1162 0 recv_consume(self, pos);
1163 0 return 1;
1164 }
1165  
1166 0 case SERVER_LOG: {
1167 0 ▶ if (self->on_log) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->on_log) { 0 0 0
1168 0 rc = parse_and_emit_log_block(self, buf, len, &pos, self->on_log, errmsg);
1169 } else {
1170 0 rc = parse_and_discard_block(self, buf, len, &pos, "server log", 1, errmsg);
1171 }
1172 0 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 0 0 0
1173 0 recv_consume(self, pos);
1174 0 return 1;
1175 }
1176  
1177 0 case SERVER_PROFILE_EVENTS: {
1178 0 rc = parse_and_discard_block(self, buf, len, &pos, "profile_events", 1, errmsg);
1179 0 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 0 0 0
1180 0 recv_consume(self, pos);
1181 0 return 1;
1182 }
1183  
1184 0 case SERVER_TIMEZONE_UPDATE: {
1185 /* Server-side session timezone changed mid-query. Format: string(tz). */
1186 0 char *tz = NULL;
1187 0 rc = read_native_string_alloc(buf, len, &pos, &tz, NULL);
1188 0 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 0 0 0
1189 0 ▶ if (rc < 0) { *errmsg = safe_strdup("malformed timezone_update packet"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { *errmsg = safe_strdup("malformed timezone_update packet"); return -1; } 0 0 0
1190 0 ▶ CLEAR_STR(self->server_timezone);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_STR(self->server_timezone); 0 0 0
1191 0 self->server_timezone = tz;
1192 0 recv_consume(self, pos);
1193 0 return 1;
1194 }
1195  
1196 0 case SERVER_PONG:
1197 0 recv_consume(self, pos);
1198 0 return 3;
1199  
1200 12 case SERVER_END_OF_STREAM:
1201 12 recv_consume(self, pos);
1202 12 return 2;
1203  
1204 1 default: {
1205 /* Unknown packet type */
1206 char err[64];
1207 1 snprintf(err, sizeof(err), "unknown server packet type: %llu",
1208 (unsigned long long)ptype);
1209 1 *errmsg = safe_strdup(err);
1210 1 self->recv_len = 0;
1211 1 return -1;
1212 }
1213 }
1214 }
1215  
1216 /*
1217 * Process native protocol responses from recv_buf.
1218 * Called from on_readable when protocol == PROTO_NATIVE.
1219 */
1220 50 static void process_native_response(ev_clickhouse_t *self) {
1221 64 ▶ while (self->recv_len > 0 && self->magic == EV_CH_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
while (self->recv_len > 0 && self->magic == EV_CH_MAGIC) { 50 14 64
while (self->recv_len > 0 && self->magic == EV_CH_MAGIC) { 50 0 50
1222 50 char *errmsg = NULL;
1223 int rc;
1224 50 rc = parse_native_packet(self, &errmsg);
1225  
1226 50 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 0 50 50
1227 /* need more data */
1228 36 return;
1229 }
1230  
1231 50 ▶ if (rc == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == -2) { 0 50 50
1232 /* object destroyed inside callback */
1233 0 return;
1234 }
1235  
1236 50 ▶ if (rc == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 4) { 20 30 50
1237 /* ServerHello received — send addendum (revision >= 54458) */
1238 20 ▶ if (self->native_state == NATIVE_WAIT_HELLO) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->native_state == NATIVE_WAIT_HELLO) { 20 0 20
1239 /* Addendum: quota_key (only if server supports it) */
1240 20 ▶ if (self->server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_ADDENDUM) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_ADDENDUM) { 20 0 20
1241 native_buf_t ab;
1242 20 nbuf_init(&ab);
1243 20 nbuf_cstring(&ab, ""); /* quota_key */
1244 20 send_replace(self, ab.data, ab.len);
1245 20 ▶ if (try_write(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (try_write(self)) return; 0 20 20
1246 20 ▶ if (self->send_pos < self->send_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->send_pos < self->send_len) { 0 20 20
1247 /* Addendum partially written (EAGAIN); io_cb finishes
1248 * connect once the buffer drains, otherwise pipeline_advance
1249 * could overwrite the unsent tail with a queued query. */
1250 0 self->pending_addendum_finish = 1;
1251 0 return;
1252 }
1253 }
1254 20 self->native_state = NATIVE_IDLE;
1255 20 ▶ if (finish_connect(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (finish_connect(self)) return; 0 20 20
1256 }
1257 /* pipeline_advance -> try_write may free self; no data
1258 * in recv_buf for the just-dispatched request yet */
1259 20 return;
1260 }
1261  
1262 30 ▶ if (rc == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == -1) { 2 28 30
1263 /* Stream corruption: native packets aren't length-delimited,
1264 * so resync is impossible — the server's remaining output
1265 * for this query would be misattributed to the next one.
1266 * Tear down; auto_reconnect re-establishes an idle link. */
1267 2 ▶ if (self->native_state == NATIVE_WAIT_HELLO) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->native_state == NATIVE_WAIT_HELLO) { 1 1 2
1268 /* Skip auto_reconnect on a malformed ServerHello: the peer
1269 * isn't a ClickHouse server, so retrying just spins.
1270 * (connect_timeout still uses fail_connection, which retries.) */
1271 1 teardown_io_error(self, errmsg, "connection failed");
1272 1 Safefree(errmsg);
1273 1 return;
1274 }
1275 1 fail_connection(self, errmsg);
1276 1 Safefree(errmsg);
1277 1 return;
1278 }
1279  
1280 28 ▶ if (rc == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 5) { 2 26 28
1281 /* Server exception — a normal query error with its bytes
1282 * consumed, so the stream stays in sync: deliver to the
1283 * query callback and pipeline the next query. */
1284 2 ▶ if (self->native_state == NATIVE_WAIT_HELLO) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->native_state == NATIVE_WAIT_HELLO) { 0 2 2
1285 /* The server rejected the handshake itself (e.g. wrong
1286 * credentials) — fail fast with the real text, to
1287 * on_error and to any pre-connect queued queries. No
1288 * reconnect: retrying a rejected handshake just spins. */
1289 0 teardown_io_error(self, errmsg, errmsg);
1290 0 Safefree(errmsg);
1291 0 return;
1292 }
1293 2 stop_timing(self);
1294  
1295 2 ▶ CLEAR_SV(self->native_rows);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_rows); 0 2 2
1296 2 ▶ CLEAR_INSERT(self);
Branch: 2 branches
BranchTrueFalseTotal
CLEAR_INSERT(self); 0 2 2
CLEAR_INSERT(self); 2 0 2
1297 2 free_insert_default_state(self);
1298 2 ▶ CLEAR_STR(self->insert_err);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_STR(self->insert_err); 0 2 2
1299 2 self->native_state = NATIVE_IDLE;
1300 2 self->cancel_after_meta = 0;
1301 2 self->cancelled = 0;
1302 2 self->recv_len = 0; /* drop this dead query's tail, if coalesced */
1303 2 ▶ if (self->send_count > 0) self->send_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (self->send_count > 0) self->send_count--; 2 0 2
1304 2 lc_free_dicts(self);
1305 2 int destroyed = deliver_error(self, errmsg);
1306 2 Safefree(errmsg);
1307 2 ▶ if (destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (destroyed) return; 0 2 2
1308  
1309 /* advance pipeline — may free self via try_write error */
1310 2 pipeline_advance(self);
1311 2 return;
1312 }
1313  
1314 26 ▶ if (rc == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 2) { 12 14 26
1315 /* EndOfStream — deliver accumulated rows or deferred error */
1316 12 stop_timing(self);
1317 12 self->native_state = NATIVE_IDLE;
1318 12 self->cancel_after_meta = 0;
1319 12 ▶ CLEAR_INSERT(self);
Branch: 2 branches
BranchTrueFalseTotal
CLEAR_INSERT(self); 0 12 12
CLEAR_INSERT(self); 2 10 12
1320 12 free_insert_default_state(self);
1321 12 ▶ if (self->send_count > 0) self->send_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (self->send_count > 0) self->send_count--; 12 0 12
1322 12 lc_free_dicts(self);
1323  
1324 /* Flush any uncoalesced progress accumulated since the last
1325 * fire so users instrumenting via on_progress see the full
1326 * total when the query completes within one progress_period
1327 * of the last fire. */
1328 12 ▶ if (self->on_progress && self->progress_period > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->on_progress && self->progress_period > 0) { 0 12 12
if (self->on_progress && self->progress_period > 0) { 0 0 0
1329 0 int any = 0, i;
1330 0 ▶ for (i = 0; i < 5; i++) if (self->progress_acc[i]) { any = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < 5; i++) if (self->progress_acc[i]) { any = 1; break; } 0 0 0
for (i = 0; i < 5; i++) if (self->progress_acc[i]) { any = 1; break; } 0 0 0
1331 0 ▶ if (any) {
Branch: 1 branch
BranchTrueFalseTotal
if (any) { 0 0 0
1332 uint64_t pp[5];
1333 0 memcpy(pp, self->progress_acc, sizeof(pp));
1334 0 memset(self->progress_acc, 0, sizeof(self->progress_acc));
1335 0 ▶ if (fire_progress_cb(self, pp) < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (fire_progress_cb(self, pp) < 0) return; 0 0 0
1336 }
1337 }
1338  
1339 12 ▶ if (self->insert_err) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->insert_err) { 8 4 12
1340 8 char *err = self->insert_err;
1341 8 self->insert_err = NULL;
1342 8 ▶ CLEAR_SV(self->native_rows);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_rows); 0 8 8
1343 8 int destroyed = deliver_error(self, err);
1344 8 Safefree(err);
1345 8 ▶ if (destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (destroyed) return; 0 8 8
1346 4 ▶ } else if (self->cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (self->cancelled) { 2 2 4
1347 2 self->cancelled = 0;
1348 2 ▶ CLEAR_SV(self->native_rows);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_SV(self->native_rows); 0 2 2
1349 2 ▶ if (deliver_error(self, "query cancelled")) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, "query cancelled")) return; 0 2 2
1350 } else {
1351 2 AV *rows = self->native_rows;
1352 2 self->native_rows = NULL;
1353 2 ▶ if (deliver_rows(self, rows)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_rows(self, rows)) return; 0 2 2
1354 }
1355  
1356 /* advance pipeline — may free self via try_write error */
1357 12 pipeline_advance(self);
1358 12 return;
1359 }
1360  
1361 14 ▶ if (rc == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 3) { 0 14 14
1362 /* Pong — deliver to the queued cb (keepalive no-op or user ping) */
1363 0 self->native_state = NATIVE_IDLE;
1364 0 self->cancel_after_meta = 0;
1365 0 stop_timing(self);
1366 0 ▶ if (self->send_count > 0) self->send_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (self->send_count > 0) self->send_count--; 0 0 0
1367 0 ▶ if (self->cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cancelled) { 0 0 0
1368 0 self->cancelled = 0;
1369 0 ▶ if (deliver_error(self, "query cancelled")) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, "query cancelled")) return; 0 0 0
1370 } else {
1371 0 AV *rows = newAV();
1372 0 ▶ if (deliver_rows(self, rows)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_rows(self, rows)) return; 0 0 0
1373 }
1374 0 pipeline_advance(self);
1375 0 return;
1376 }
1377  
1378 /* rc == 1: Data/Progress/ProfileInfo — continue reading */
1379 }
1380 }
1381  
« xs/tzif.c ↑ Summary xs/codecs.c »