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