| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
14 |
14 |
|
| 12 |
14 |
▶ | if (rc < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) return -1; |
0 |
14 |
14 |
|
| 13 |
14 |
▶ | if (field_num == 0) return 1; /* end marker */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (field_num == 0) return 1; /* end marker */ |
14 |
0 |
14 |
|
| 14 |
0 |
▶ | if (field_num == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc <= 0) return rc; |
0 |
0 |
0 |
|
| 19 |
0 |
▶ | } else if (field_num == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| } 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 69 |
0 |
▶ | if (rc < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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 |
|
| if (self->compress) { |
| 77 |
|
| int need_more = 0; |
| 78 |
|
| const char *lz4_err = NULL; |
| 79 |
|
| decompressed = ch_lz4_decompress_chain(buf, len, &pos, &blen, |
| 80 |
|
| &need_more, &lz4_err); |
| 81 |
|
| if (!decompressed) { |
| 82 |
|
| if (need_more) return 0; |
| 83 |
|
| if (!lz4_optional) { |
| 84 |
|
| snprintf(errbuf, sizeof(errbuf), "%s: LZ4 decompression failed", kind); |
| 85 |
|
| *errmsg = safe_strdup(lz4_err ? lz4_err : errbuf); |
| 86 |
|
| return -1; |
| 87 |
|
| } |
| 88 |
|
| /* fall through to uncompressed parsing */ |
| 89 |
|
| } |
| 90 |
|
| } |
| 91 |
|
| if (decompressed) { |
| 92 |
|
| bbuf = decompressed; |
| 93 |
|
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if ((uint8_t)bbuf[bpos]) { |
0 |
0 |
0 |
|
| 129 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (!vals) { |
0 |
0 |
0 |
|
| 141 |
0 |
| free_col_type(ct); |
| 142 |
0 |
▶ | if (col_err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (col_err) { |
0 |
0 |
0 |
|
| 143 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (!decompressed) pos = bpos; |
0 |
0 |
0 |
|
| 164 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 183 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed log block"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed log block"); return -1; } |
0 |
0 |
0 |
|
| 184 |
|
| |
| 185 |
|
| #ifdef HAVE_LZ4 |
| 186 |
|
| if (self->compress) { |
| 187 |
|
| int need_more = 0; |
| 188 |
|
| const char *lz4_err = NULL; |
| 189 |
|
| decompressed = ch_lz4_decompress_chain(buf, len, &pos, &blen, |
| 190 |
|
| &need_more, &lz4_err); |
| 191 |
|
| if (!decompressed) { |
| 192 |
|
| if (need_more) return 0; |
| 193 |
|
| /* server log frames are not always compressed even with |
| 194 |
|
| * compress=1 negotiated — fall through to raw parsing. */ |
| 195 |
|
| } |
| 196 |
|
| } |
| 197 |
|
| if (decompressed) { bbuf = decompressed; bpos = 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| Newxz(names, nc, char *); |
0 |
0 |
0 |
|
| 227 |
0 |
▶ | Newxz(data, nc, SV **); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (bpos >= blen) { err_seen = 0; break; } |
0 |
0 |
0 |
|
| 246 |
0 |
▶ | if ((uint8_t)bbuf[bpos]) { err_seen = -1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t)bbuf[bpos]) { err_seen = -1; break; } |
0 |
0 |
0 |
|
| 247 |
0 |
| bpos++; |
| 248 |
0 |
▶ | if (nr > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
0 |
0 |
|
| 274 |
0 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| for (uint64_t c = 0; c < nc; c++) { |
0 |
0 |
0 |
|
| 294 |
0 |
▶ | if (data && data[c]) { |
Branch: 2 branches
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (data) Safefree(data); |
0 |
0 |
0 |
|
| 301 |
0 |
▶ | if (names) Safefree(names); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (check_destroyed(self)) { |
0 |
0 |
0 |
|
| 311 |
0 |
| ret = -2; |
| 312 |
0 |
▶ | } else if (err_seen < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } 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
| Branch | True | False | Total |
| } 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 339 |
0 |
▶ | EXTEND(SP, 5); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 5); |
0 |
0 |
0 |
|
| 340 |
0 |
▶ | for (i = 0; i < 5; i++) PUSHs(sv_2mortal(newSVuv(pp[i]))); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 345 |
0 |
| self->callback_depth--; |
| 346 |
0 |
▶ | return check_destroyed(self) ? -2 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
50 |
50 |
|
| 358 |
50 |
▶ | if (rc < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
21 |
21 |
|
| 371 |
21 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed server name"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
20 |
20 |
|
| 408 |
20 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed timezone"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed timezone"); return -1; } |
0 |
20 |
20 |
|
| 409 |
20 |
▶ | CLEAR_STR(self->server_timezone); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
20 |
20 |
|
| 417 |
20 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed display name"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
20 |
20 |
|
| 426 |
20 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed version patch"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
14 |
14 |
|
| 446 |
14 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed table name"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed table name"); return -1; } |
0 |
14 |
14 |
|
| 447 |
|
| |
| 448 |
|
| #ifdef HAVE_LZ4 |
| 449 |
|
| if (self->compress) { |
| 450 |
|
| /* Decompress the block body — may span multiple LZ4 sub-blocks. */ |
| 451 |
|
| int need_more = 0; |
| 452 |
|
| const char *lz4_err = NULL; |
| 453 |
|
| decompressed = ch_lz4_decompress_chain(buf, len, &pos, &dlen, |
| 454 |
|
| &need_more, &lz4_err); |
| 455 |
|
| if (!decompressed) { |
| 456 |
|
| if (need_more) return 0; |
| 457 |
|
| *errmsg = safe_strdup(lz4_err ? lz4_err : "LZ4 decompression failed"); |
| 458 |
|
| return -1; |
| 459 |
|
| } |
| 460 |
|
| dbuf = decompressed; |
| 461 |
|
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) { |
0 |
14 |
14 |
|
| 474 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) { |
0 |
14 |
14 |
|
| 482 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (rc == 0) { |
0 |
14 |
14 |
|
| 489 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| && (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
| Branch | True | False | Total |
| if (num_cols > (uint64_t)(dlen - dpos)) { |
0 |
14 |
14 |
|
| 515 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 516 |
0 |
| return 0; |
| 517 |
|
| } |
| 518 |
14 |
▶ | Newxz(cnames, num_cols, const char *); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(cnames, num_cols, const char *); |
0 |
14 |
14 |
|
| 519 |
14 |
▶ | Newxz(cname_lens, num_cols, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(cname_lens, num_cols, size_t); |
0 |
14 |
14 |
|
| 520 |
14 |
▶ | Newxz(ctypes_str, num_cols, const char *); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(ctypes_str, num_cols, const char *); |
0 |
14 |
14 |
|
| 521 |
14 |
▶ | Newxz(ctype_lens, num_cols, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(ctype_lens, num_cols, size_t); |
0 |
14 |
14 |
|
| 522 |
14 |
▶ | Newxz(ctypes, num_cols, col_type_t *); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(ctypes, num_cols, col_type_t *); |
0 |
14 |
14 |
|
| 523 |
|
| |
| 524 |
28 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
|
| |
| 543 |
|
| /* custom serialization flag (revision >= 54446) */ |
| 544 |
14 |
▶ | if (dpos >= dlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dpos >= dlen) { |
0 |
14 |
14 |
|
| 545 |
0 |
| meta_hard = 0; |
| 546 |
0 |
| meta_err = "truncated custom_ser"; |
| 547 |
0 |
| goto meta_cleanup; |
| 548 |
|
| } |
| 549 |
14 |
▶ | if ((uint8_t)dbuf[dpos]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t)dbuf[dpos]) { |
0 |
14 |
14 |
|
| 550 |
0 |
| meta_hard = 1; |
| 551 |
0 |
| meta_err = "custom serialization not supported"; |
| 552 |
0 |
| goto meta_cleanup; |
| 553 |
|
| } |
| 554 |
14 |
| dpos++; |
| 555 |
|
| } |
| 556 |
14 |
| goto meta_ok; |
| 557 |
|
| |
| 558 |
0 |
| meta_cleanup: |
| 559 |
0 |
▶ | for (c = 0; c < num_cols; c++) if (ctypes[c]) free_col_type(ctypes[c]); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 560 |
0 |
| Safefree(cnames); Safefree(cname_lens); |
| 561 |
0 |
| Safefree(ctypes_str); Safefree(ctype_lens); |
| 562 |
0 |
| Safefree(ctypes); |
| 563 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 564 |
0 |
▶ | if (meta_hard) { *errmsg = safe_strdup(meta_err); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (meta_hard) { *errmsg = safe_strdup(meta_err); return -1; } |
0 |
0 |
0 |
|
| 565 |
0 |
| return 0; |
| 566 |
|
| |
| 567 |
14 |
| meta_ok: ; |
| 568 |
|
| |
| 569 |
14 |
▶ | if (self->cancel_after_meta) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->cancel_after_meta) { |
2 |
12 |
14 |
|
| 570 |
4 |
▶ | for (c = 0; c < num_cols; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) |
2 |
2 |
4 |
|
| 571 |
2 |
| free_col_type(ctypes[c]); |
| 572 |
2 |
| Safefree(cnames); Safefree(cname_lens); |
| 573 |
2 |
| Safefree(ctypes_str); Safefree(ctype_lens); |
| 574 |
2 |
| Safefree(ctypes); |
| 575 |
2 |
| free_insert_default_state(self); |
| 576 |
2 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
2 |
2 |
|
| 577 |
2 |
| else pos = dpos; |
| 578 |
2 |
| recv_consume(self, pos); |
| 579 |
2 |
| self->cancel_after_meta = 0; |
| 580 |
2 |
| self->native_state = NATIVE_WAIT_RESULT; |
| 581 |
2 |
| Newx(data_pkt, 1, char); |
| 582 |
2 |
| data_pkt[0] = (char)CLIENT_CANCEL; |
| 583 |
2 |
| send_replace(self, data_pkt, 1); |
| 584 |
2 |
▶ | if (try_write(self)) return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (try_write(self)) return -2; |
0 |
2 |
2 |
|
| 585 |
2 |
| return 1; |
| 586 |
|
| } |
| 587 |
|
| |
| 588 |
|
| /* Build binary data block from stored data */ |
| 589 |
|
| char ibuf[640]; |
| 590 |
12 |
| col_default_t *cdefs = NULL; |
| 591 |
|
| /* Resolving defaults and the AV build stringify user SVs, |
| 592 |
|
| * so overload/tie runs arbitrary Perl that may destroy/ |
| 593 |
|
| * reset/finish this very connection. Pin the rows |
| 594 |
|
| * (teardown would free them) and hold depth (DESTROY then |
| 595 |
|
| * defers Safefree); re-validate below before touching |
| 596 |
|
| * self. */ |
| 597 |
12 |
| SV *av_pin = self->insert_av; |
| 598 |
12 |
| int gen0 = self->connect_gen; |
| 599 |
12 |
| ibuf[0] = '\0'; |
| 600 |
12 |
▶ | if (av_pin) SvREFCNT_inc(av_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_pin) SvREFCNT_inc(av_pin); |
12 |
0 |
12 |
|
| 601 |
12 |
| self->callback_depth++; |
| 602 |
12 |
▶ | if (self->insert_empty_as_default || self->insert_defaults) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->insert_empty_as_default || self->insert_defaults) { |
12 |
0 |
12 |
| if (self->insert_empty_as_default || self->insert_defaults) { |
0 |
12 |
12 |
|
| 603 |
0 |
| cdefs = resolve_insert_defaults_guarded(aTHX_ self, |
| 604 |
|
| cnames, cname_lens, ctypes, |
| 605 |
|
| ctypes_str, ctype_lens, |
| 606 |
|
| (int)num_cols, ibuf, sizeof(ibuf)); |
| 607 |
0 |
▶ | if (!cdefs && !ibuf[0]) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cdefs && !ibuf[0]) |
0 |
0 |
0 |
| if (!cdefs && !ibuf[0]) |
0 |
0 |
0 |
|
| 608 |
0 |
| snprintf(ibuf, sizeof(ibuf), |
| 609 |
|
| "failed to resolve column defaults"); |
| 610 |
|
| } |
| 611 |
12 |
▶ | if (self->magic != EV_CH_MAGIC || self->connect_gen != gen0 |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 612 |
12 |
▶ | || !self->connected) { |
Branch: 1 branch
| Branch | True | False | Total |
| || !self->connected) { |
0 |
12 |
12 |
|
| 613 |
0 |
| data_pkt = NULL; |
| 614 |
0 |
| data_pkt_len = 0; |
| 615 |
12 |
▶ | } else if (!cdefs && (self->insert_empty_as_default |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (!cdefs && (self->insert_empty_as_default |
12 |
0 |
12 |
| } else if (!cdefs && (self->insert_empty_as_default |
12 |
0 |
12 |
|
| 616 |
12 |
▶ | || self->insert_defaults)) { |
Branch: 1 branch
| Branch | True | False | Total |
| || self->insert_defaults)) { |
0 |
12 |
12 |
|
| 617 |
0 |
| data_pkt = NULL; |
| 618 |
0 |
| data_pkt_len = (size_t)-1; |
| 619 |
12 |
▶ | } else if (self->insert_av) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->insert_av) { |
12 |
0 |
12 |
|
| 620 |
12 |
| data_pkt = build_native_insert_data_from_av_guarded(aTHX_ self, |
| 621 |
|
| self->insert_av, |
| 622 |
|
| cnames, cname_lens, ctypes_str, ctype_lens, |
| 623 |
|
| ctypes, (int)num_cols, cdefs, &data_pkt_len, |
| 624 |
|
| ibuf, sizeof(ibuf)); |
| 625 |
|
| } else { |
| 626 |
0 |
| data_pkt = build_native_insert_data(self, |
| 627 |
0 |
| self->insert_data, self->insert_data_len, |
| 628 |
|
| cnames, cname_lens, ctypes_str, ctype_lens, |
| 629 |
|
| ctypes, (int)num_cols, cdefs, &data_pkt_len, |
| 630 |
|
| ibuf, sizeof(ibuf)); |
| 631 |
|
| } |
| 632 |
|
| /* Defaults may hold user SVs: drop them and the pin BEFORE |
| 633 |
|
| * depth--, so a value holding the last $ch ref DESTROYs |
| 634 |
|
| * into depth>0 (cf. on_data). */ |
| 635 |
12 |
| free_col_defaults(cdefs, (int)num_cols); |
| 636 |
12 |
| cdefs = NULL; |
| 637 |
12 |
▶ | if (av_pin) SvREFCNT_dec(av_pin); |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_pin) SvREFCNT_dec(av_pin); |
12 |
0 |
12 |
|
| 638 |
12 |
| self->callback_depth--; |
| 639 |
|
| { |
| 640 |
12 |
▶ | if (self->magic != EV_CH_MAGIC) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->magic != EV_CH_MAGIC) { |
0 |
12 |
12 |
|
| 641 |
0 |
▶ | if (data_pkt) Safefree(data_pkt); |
Branch: 1 branch
| Branch | True | False | Total |
| if (data_pkt) Safefree(data_pkt); |
0 |
0 |
0 |
|
| 642 |
0 |
▶ | for (c = 0; c < num_cols; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) |
0 |
0 |
0 |
|
| 643 |
0 |
| free_col_type(ctypes[c]); |
| 644 |
0 |
| Safefree(cnames); Safefree(cname_lens); |
| 645 |
0 |
| Safefree(ctypes_str); Safefree(ctype_lens); |
| 646 |
0 |
| Safefree(ctypes); |
| 647 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 648 |
0 |
| check_destroyed(self); |
| 649 |
0 |
| return -2; |
| 650 |
|
| } |
| 651 |
12 |
▶ | if (self->connect_gen != gen0 || !self->connected) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connect_gen != gen0 || !self->connected) { |
12 |
0 |
12 |
| if (self->connect_gen != gen0 || !self->connected) { |
0 |
12 |
12 |
|
| 652 |
|
| /* reset()/finish() from magic: the INSERT was |
| 653 |
|
| * already failed by the teardown; drop the |
| 654 |
|
| * built block instead of sending it into the |
| 655 |
|
| * new/closed connection. */ |
| 656 |
0 |
▶ | if (data_pkt) Safefree(data_pkt); |
Branch: 1 branch
| Branch | True | False | Total |
| if (data_pkt) Safefree(data_pkt); |
0 |
0 |
0 |
|
| 657 |
0 |
| free_col_defaults(cdefs, (int)num_cols); |
| 658 |
0 |
| free_insert_default_state(self); |
| 659 |
0 |
▶ | for (c = 0; c < num_cols; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) |
0 |
0 |
0 |
|
| 660 |
0 |
| free_col_type(ctypes[c]); |
| 661 |
0 |
| Safefree(cnames); Safefree(cname_lens); |
| 662 |
0 |
| Safefree(ctypes_str); Safefree(ctype_lens); |
| 663 |
0 |
| Safefree(ctypes); |
| 664 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 665 |
|
| /* recv is already empty (teardown zeroes it). */ |
| 666 |
0 |
| return 0; |
| 667 |
|
| } |
| 668 |
|
| } |
| 669 |
|
| |
| 670 |
24 |
▶ | for (c = 0; c < num_cols; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) |
12 |
12 |
24 |
|
| 671 |
12 |
| free_col_type(ctypes[c]); |
| 672 |
12 |
| Safefree(cnames); Safefree(cname_lens); |
| 673 |
12 |
| Safefree(ctypes_str); Safefree(ctype_lens); |
| 674 |
12 |
| Safefree(ctypes); |
| 675 |
12 |
| free_col_defaults(cdefs, (int)num_cols); |
| 676 |
12 |
| free_insert_default_state(self); |
| 677 |
|
| |
| 678 |
|
| { |
| 679 |
|
| /* Check encode-failure sentinel before freeing insert data */ |
| 680 |
12 |
▶ | int encode_failed = (!data_pkt && data_pkt_len == (size_t)-1); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 681 |
|
| |
| 682 |
|
| /* Free stored INSERT data */ |
| 683 |
12 |
▶ | CLEAR_INSERT(self); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(self); |
0 |
12 |
12 |
| CLEAR_INSERT(self); |
12 |
0 |
12 |
|
| 684 |
|
| |
| 685 |
12 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
12 |
12 |
|
| 686 |
12 |
| else pos = dpos; |
| 687 |
12 |
| recv_consume(self, pos); |
| 688 |
|
| |
| 689 |
12 |
▶ | if (!data_pkt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!data_pkt) { |
8 |
4 |
12 |
|
| 690 |
|
| /* Send empty Data block to complete the INSERT protocol */ |
| 691 |
|
| native_buf_t fallback; |
| 692 |
8 |
| nbuf_init(&fallback); |
| 693 |
8 |
| nbuf_empty_data_block(&fallback, self->compress); |
| 694 |
8 |
| data_pkt = fallback.data; |
| 695 |
8 |
| data_pkt_len = fallback.len; |
| 696 |
8 |
▶ | if (encode_failed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (encode_failed) { |
8 |
0 |
8 |
|
| 697 |
|
| char fbuf[700]; |
| 698 |
8 |
▶ | if (ibuf[0]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ibuf[0]) |
8 |
0 |
8 |
|
| 699 |
8 |
| snprintf(fbuf, sizeof(fbuf), "native INSERT encoding " |
| 700 |
|
| "failed (%s)", ibuf); |
| 701 |
|
| else |
| 702 |
0 |
| snprintf(fbuf, sizeof(fbuf), "native INSERT encoding " |
| 703 |
|
| "failed (unsupported type or invalid value)"); |
| 704 |
8 |
| self->insert_err = safe_strdup(fbuf); |
| 705 |
|
| } |
| 706 |
|
| } |
| 707 |
|
| } |
| 708 |
|
| |
| 709 |
|
| /* Cancellation can also run while encoding user values. */ |
| 710 |
12 |
| self->native_state = NATIVE_WAIT_RESULT; |
| 711 |
12 |
▶ | if (self->cancel_after_meta) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->cancel_after_meta) { |
0 |
12 |
12 |
|
| 712 |
0 |
| self->cancel_after_meta = 0; |
| 713 |
0 |
| Safefree(data_pkt); |
| 714 |
0 |
| Newx(data_pkt, 1, char); |
| 715 |
0 |
| data_pkt[0] = (char)CLIENT_CANCEL; |
| 716 |
0 |
| data_pkt_len = 1; |
| 717 |
0 |
▶ | CLEAR_STR(self->insert_err); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->insert_err); |
0 |
0 |
0 |
|
| 718 |
|
| } |
| 719 |
12 |
| send_replace(self, data_pkt, data_pkt_len); |
| 720 |
12 |
▶ | if (try_write(self)) return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (try_write(self)) return -2; |
0 |
12 |
12 |
|
| 721 |
12 |
| return 1; |
| 722 |
|
| } |
| 723 |
|
| |
| 724 |
|
| /* INSERT two-phase with 0-column sample block: free data, |
| 725 |
|
| * send empty Data block, transition to WAIT_RESULT */ |
| 726 |
0 |
▶ | if (self->native_state == NATIVE_WAIT_INSERT_META |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->native_state == NATIVE_WAIT_INSERT_META |
0 |
0 |
0 |
|
| 727 |
0 |
▶ | && (self->insert_data || self->insert_av) && num_cols == 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| && (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 |
|
| 728 |
|
| native_buf_t fallback; |
| 729 |
|
| |
| 730 |
0 |
▶ | CLEAR_INSERT(self); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(self); |
0 |
0 |
0 |
| CLEAR_INSERT(self); |
0 |
0 |
0 |
|
| 731 |
0 |
| free_insert_default_state(self); |
| 732 |
|
| |
| 733 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 734 |
0 |
| else pos = dpos; |
| 735 |
0 |
| recv_consume(self, pos); |
| 736 |
|
| |
| 737 |
0 |
| nbuf_init(&fallback); |
| 738 |
0 |
| nbuf_empty_data_block(&fallback, self->compress); |
| 739 |
0 |
| self->native_state = NATIVE_WAIT_RESULT; |
| 740 |
0 |
▶ | if (self->cancel_after_meta) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->cancel_after_meta) { |
0 |
0 |
0 |
|
| 741 |
0 |
| self->cancel_after_meta = 0; |
| 742 |
0 |
| fallback.len = 0; |
| 743 |
0 |
| nbuf_u8(&fallback, (uint8_t)CLIENT_CANCEL); |
| 744 |
|
| } else { |
| 745 |
0 |
| self->insert_err = safe_strdup( |
| 746 |
|
| "INSERT failed: server sent 0-column sample block"); |
| 747 |
|
| } |
| 748 |
0 |
| send_replace(self, fallback.data, fallback.len); |
| 749 |
0 |
▶ | if (try_write(self)) return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (try_write(self)) return -2; |
0 |
0 |
0 |
|
| 750 |
0 |
| return 1; |
| 751 |
|
| } |
| 752 |
|
| |
| 753 |
|
| /* Normal empty block — capture column names/types so callers can |
| 754 |
|
| * inspect schema after a zero-row SELECT. Reset only when we have |
| 755 |
|
| * columns to record so the terminating empty block (num_cols=0) |
| 756 |
|
| * doesn't wipe the schema we already captured; this also drops |
| 757 |
|
| * any partial state from a need-more retry. */ |
| 758 |
|
| { |
| 759 |
|
| uint64_t c; |
| 760 |
0 |
▶ | if (num_cols > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_cols > 0) { |
0 |
0 |
0 |
|
| 761 |
0 |
▶ | CLEAR_SV(self->native_col_names); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_names); |
0 |
0 |
0 |
|
| 762 |
0 |
▶ | CLEAR_SV(self->native_col_types); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_types); |
0 |
0 |
0 |
|
| 763 |
0 |
| self->native_col_names = newAV(); |
| 764 |
0 |
| self->native_col_types = newAV(); |
| 765 |
|
| } |
| 766 |
0 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) { |
0 |
0 |
0 |
|
| 767 |
|
| const char *cname, *ctype; |
| 768 |
|
| size_t cname_len, ctype_len; |
| 769 |
|
| |
| 770 |
0 |
| rc = read_native_string_ref(dbuf, dlen, &dpos, &cname, &cname_len); |
| 771 |
0 |
▶ | if (rc <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) { |
0 |
0 |
0 |
|
| 772 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 773 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed cname"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed cname"); return -1; } |
0 |
0 |
0 |
|
| 774 |
0 |
| return 0; |
| 775 |
|
| } |
| 776 |
0 |
| av_push(self->native_col_names, newSVpvn(cname, cname_len)); |
| 777 |
|
| |
| 778 |
0 |
| rc = read_native_string_ref(dbuf, dlen, &dpos, &ctype, &ctype_len); |
| 779 |
0 |
▶ | if (rc <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) { |
0 |
0 |
0 |
|
| 780 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 781 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); return -1; } |
0 |
0 |
0 |
|
| 782 |
0 |
| return 0; |
| 783 |
|
| } |
| 784 |
0 |
| av_push(self->native_col_types, newSVpvn(ctype, ctype_len)); |
| 785 |
|
| |
| 786 |
|
| /* custom serialization flag (revision >= 54446) */ |
| 787 |
0 |
▶ | if (dpos >= dlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dpos >= dlen) { |
0 |
0 |
0 |
|
| 788 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 789 |
0 |
| return 0; |
| 790 |
|
| } |
| 791 |
0 |
▶ | if ((uint8_t)dbuf[dpos]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t)dbuf[dpos]) { |
0 |
0 |
0 |
|
| 792 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 793 |
0 |
| *errmsg = safe_strdup("custom serialization not supported"); |
| 794 |
0 |
| return -1; |
| 795 |
|
| } |
| 796 |
0 |
| dpos++; |
| 797 |
|
| } |
| 798 |
|
| } |
| 799 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 800 |
0 |
| else pos = dpos; /* uncompressed: advance pos to match dpos */ |
| 801 |
0 |
| recv_consume(self, pos); |
| 802 |
0 |
| return 1; |
| 803 |
|
| } |
| 804 |
|
| |
| 805 |
|
| /* Decode columns and convert to rows */ |
| 806 |
|
| { |
| 807 |
0 |
| SV ***columns = NULL; |
| 808 |
0 |
| col_type_t **col_types = NULL; |
| 809 |
0 |
| const char **cnames = NULL; |
| 810 |
0 |
| size_t *cname_lens = NULL; |
| 811 |
|
| uint64_t c, r; |
| 812 |
0 |
| int named = (self->decode_flags & DECODE_NAMED_ROWS) ? 1 : 0; |
| 813 |
|
| |
| 814 |
|
| /* More columns than remaining bytes proves nothing yet: on |
| 815 |
|
| * a live stream the rest may simply not have arrived (short |
| 816 |
|
| * first read, split LZ4 chain). Wait for more; the column |
| 817 |
|
| * loop below rules on genuinely malformed data. */ |
| 818 |
0 |
▶ | if (num_cols > (uint64_t)(dlen - dpos)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_cols > (uint64_t)(dlen - dpos)) { |
0 |
0 |
0 |
|
| 819 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 820 |
0 |
| return 0; |
| 821 |
|
| } |
| 822 |
0 |
▶ | Newxz(columns, num_cols, SV**); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(columns, num_cols, SV**); |
0 |
0 |
0 |
|
| 823 |
0 |
▶ | Newxz(col_types, num_cols, col_type_t*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(col_types, num_cols, col_type_t*); |
0 |
0 |
0 |
|
| 824 |
0 |
▶ | if (named) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (named) { |
0 |
0 |
0 |
|
| 825 |
0 |
▶ | Newxz(cnames, num_cols, const char *); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(cnames, num_cols, const char *); |
0 |
0 |
0 |
|
| 826 |
0 |
▶ | Newx(cname_lens, num_cols, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(cname_lens, num_cols, size_t); |
0 |
0 |
0 |
|
| 827 |
|
| } |
| 828 |
|
| |
| 829 |
0 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) { |
0 |
0 |
0 |
|
| 830 |
|
| const char *cname, *ctype; |
| 831 |
|
| size_t cname_len, ctype_len; |
| 832 |
|
| |
| 833 |
0 |
| rc = read_native_string_ref(dbuf, dlen, &dpos, &cname, &cname_len); |
| 834 |
0 |
▶ | if (rc == 0) goto data_need_more; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) goto data_need_more; |
0 |
0 |
0 |
|
| 835 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed cname"); goto data_error; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed cname"); goto data_error; } |
0 |
0 |
0 |
|
| 836 |
|
| |
| 837 |
0 |
▶ | if (named) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (named) { |
0 |
0 |
0 |
|
| 838 |
0 |
| cnames[c] = cname; |
| 839 |
0 |
| cname_lens[c] = cname_len; |
| 840 |
|
| } |
| 841 |
|
| |
| 842 |
|
| /* Allocate AVs on first column of first data block. Reset |
| 843 |
|
| * any partial state from a need-more retry: pipeline_advance |
| 844 |
|
| * cleared at dispatch, but if we returned 0 mid-loop the AVs |
| 845 |
|
| * may now hold a partial column list. */ |
| 846 |
0 |
▶ | if (c == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == 0) { |
0 |
0 |
0 |
|
| 847 |
0 |
▶ | CLEAR_SV(self->native_col_names); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_names); |
0 |
0 |
0 |
|
| 848 |
0 |
▶ | CLEAR_SV(self->native_col_types); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_types); |
0 |
0 |
0 |
|
| 849 |
0 |
| self->native_col_names = newAV(); |
| 850 |
0 |
| self->native_col_types = newAV(); |
| 851 |
|
| } |
| 852 |
0 |
| av_push(self->native_col_names, newSVpvn(cname, cname_len)); |
| 853 |
|
| |
| 854 |
0 |
| rc = read_native_string_ref(dbuf, dlen, &dpos, &ctype, &ctype_len); |
| 855 |
0 |
▶ | if (rc == 0) goto data_need_more; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) goto data_need_more; |
0 |
0 |
0 |
|
| 856 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); goto data_error; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed ctype"); goto data_error; } |
0 |
0 |
0 |
|
| 857 |
|
| |
| 858 |
0 |
| col_types[c] = parse_col_type(ctype, ctype_len); |
| 859 |
0 |
| col_type_default_tz(col_types[c], |
| 860 |
0 |
| effective_tz(self, self->query_session_tz)); |
| 861 |
0 |
| av_push(self->native_col_types, newSVpvn(ctype, ctype_len)); |
| 862 |
|
| |
| 863 |
|
| /* custom serialization flag (revision >= 54446) */ |
| 864 |
0 |
▶ | if (dpos >= dlen) goto data_need_more; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dpos >= dlen) goto data_need_more; |
0 |
0 |
0 |
|
| 865 |
0 |
▶ | if ((uint8_t)dbuf[dpos]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t)dbuf[dpos]) { |
0 |
0 |
0 |
|
| 866 |
0 |
| *errmsg = safe_strdup("custom serialization not supported"); |
| 867 |
0 |
| goto data_error; |
| 868 |
|
| } |
| 869 |
0 |
| dpos++; |
| 870 |
|
| |
| 871 |
|
| /* Allocate LC dict state on first column of first block */ |
| 872 |
0 |
▶ | if (c == 0 && !self->lc_dicts) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == 0 && !self->lc_dicts) { |
0 |
0 |
0 |
| if (c == 0 && !self->lc_dicts) { |
0 |
0 |
0 |
|
| 873 |
0 |
▶ | Newxz(self->lc_dicts, num_cols, SV**); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(self->lc_dicts, num_cols, SV**); |
0 |
0 |
0 |
|
| 874 |
0 |
▶ | Newxz(self->lc_dict_sizes, num_cols, uint64_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(self->lc_dict_sizes, num_cols, uint64_t); |
0 |
0 |
0 |
|
| 875 |
0 |
| self->lc_num_cols = (int)num_cols; |
| 876 |
|
| } |
| 877 |
|
| |
| 878 |
|
| { |
| 879 |
0 |
| int col_err = 0; |
| 880 |
0 |
| columns[c] = decode_column_ex(dbuf, dlen, &dpos, num_rows, col_types[c], &col_err, self->decode_flags, self, (int)c); |
| 881 |
0 |
▶ | if (!columns[c]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!columns[c]) { |
0 |
0 |
0 |
|
| 882 |
0 |
▶ | if (col_err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (col_err) { |
0 |
0 |
0 |
|
| 883 |
0 |
| *errmsg = safe_strdup("decode_column failed"); |
| 884 |
0 |
| goto data_error; |
| 885 |
|
| } |
| 886 |
0 |
| goto data_need_more; |
| 887 |
|
| } |
| 888 |
|
| } |
| 889 |
|
| } |
| 890 |
|
| |
| 891 |
|
| /* Convert column-oriented to row-oriented */ |
| 892 |
|
| { |
| 893 |
|
| AV **target; |
| 894 |
0 |
▶ | if (ptype == SERVER_TOTALS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptype == SERVER_TOTALS) { |
0 |
0 |
0 |
|
| 895 |
0 |
▶ | if (!self->native_totals) self->native_totals = newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->native_totals) self->native_totals = newAV(); |
0 |
0 |
0 |
|
| 896 |
0 |
| target = &self->native_totals; |
| 897 |
0 |
▶ | } else if (ptype == SERVER_EXTREMES) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ptype == SERVER_EXTREMES) { |
0 |
0 |
0 |
|
| 898 |
0 |
▶ | if (!self->native_extremes) self->native_extremes = newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->native_extremes) self->native_extremes = newAV(); |
0 |
0 |
0 |
|
| 899 |
0 |
| target = &self->native_extremes; |
| 900 |
|
| } else { |
| 901 |
0 |
▶ | if (!self->native_rows) self->native_rows = newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->native_rows) self->native_rows = newAV(); |
0 |
0 |
0 |
|
| 902 |
0 |
| target = &self->native_rows; |
| 903 |
|
| } |
| 904 |
|
| |
| 905 |
0 |
▶ | if (named) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (named) { |
0 |
0 |
0 |
|
| 906 |
0 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
0 |
0 |
0 |
|
| 907 |
0 |
| HV *hv = newHV(); |
| 908 |
0 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) { |
0 |
0 |
0 |
|
| 909 |
0 |
▶ | if (!hv_store(hv, cnames[c], cname_lens[c], columns[c][r], 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_store(hv, cnames[c], cname_lens[c], columns[c][r], 0)) |
0 |
0 |
0 |
|
| 910 |
0 |
| SvREFCNT_dec(columns[c][r]); |
| 911 |
|
| } |
| 912 |
0 |
| av_push(*target, newRV_noinc((SV*)hv)); |
| 913 |
|
| } |
| 914 |
|
| } else { |
| 915 |
0 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
0 |
0 |
0 |
|
| 916 |
0 |
| AV *row = newAV(); |
| 917 |
0 |
▶ | if (num_cols > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_cols > 0) |
0 |
0 |
0 |
|
| 918 |
0 |
| av_extend(row, num_cols - 1); |
| 919 |
0 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) { |
0 |
0 |
0 |
|
| 920 |
0 |
| av_push(row, columns[c][r]); |
| 921 |
|
| } |
| 922 |
0 |
| av_push(*target, newRV_noinc((SV*)row)); |
| 923 |
|
| } |
| 924 |
|
| } |
| 925 |
|
| } |
| 926 |
|
| |
| 927 |
|
| /* Fire on_data streaming callback if set (only for DATA, not TOTALS/EXTREMES) */ |
| 928 |
|
| { |
| 929 |
0 |
▶ | SV *on_data = (ptype == SERVER_DATA) ? peek_cb_on_data(self) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *on_data = (ptype == SERVER_DATA) ? peek_cb_on_data(self) : NULL; |
0 |
0 |
0 |
|
| 930 |
0 |
▶ | if (on_data && self->native_rows) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (on_data && self->native_rows) { |
0 |
0 |
0 |
| if (on_data && self->native_rows) { |
0 |
0 |
0 |
|
| 931 |
|
| /* Hold a reference across call_sv: a reentrant |
| 932 |
|
| * skip_pending() / cancel() in the handler would |
| 933 |
|
| * otherwise pop the cb_queue entry and free this |
| 934 |
|
| * callback while we're still invoking it. */ |
| 935 |
0 |
| SvREFCNT_inc(on_data); |
| 936 |
0 |
| self->callback_depth++; |
| 937 |
|
| { |
| 938 |
0 |
| dSP; |
| 939 |
0 |
| ENTER; SAVETMPS; |
| 940 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 941 |
0 |
| PUSHs(sv_2mortal(newRV_inc((SV*)self->native_rows))); |
| 942 |
0 |
| PUTBACK; |
| 943 |
0 |
| call_sv(on_data, G_DISCARD | G_EVAL); |
| 944 |
0 |
▶ | WARN_AND_CLEAR_ERRSV("on_data handler"); |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 945 |
0 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 946 |
|
| } |
| 947 |
|
| /* Decrement on_data BEFORE callback_depth-- so a DESTROY |
| 948 |
|
| * triggered by the dec (closure holding last $ch ref) |
| 949 |
|
| * still sees callback_depth > 0 and defers Safefree(self). */ |
| 950 |
0 |
| SvREFCNT_dec(on_data); |
| 951 |
0 |
| self->callback_depth--; |
| 952 |
|
| /* Clear accumulated rows for next block */ |
| 953 |
0 |
▶ | CLEAR_SV(self->native_rows); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_rows); |
0 |
0 |
0 |
|
| 954 |
0 |
▶ | if (check_destroyed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_destroyed(self)) { |
0 |
0 |
0 |
|
| 955 |
0 |
▶ | if (cnames) Safefree(cnames); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cnames) Safefree(cnames); |
0 |
0 |
0 |
|
| 956 |
0 |
▶ | if (cname_lens) Safefree(cname_lens); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cname_lens) Safefree(cname_lens); |
0 |
0 |
0 |
|
| 957 |
0 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) { |
0 |
0 |
0 |
|
| 958 |
0 |
| Safefree(columns[c]); |
| 959 |
0 |
| free_col_type(col_types[c]); |
| 960 |
|
| } |
| 961 |
0 |
| Safefree(columns); Safefree(col_types); |
| 962 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 963 |
0 |
| return -2; |
| 964 |
|
| } |
| 965 |
|
| } |
| 966 |
|
| } |
| 967 |
|
| |
| 968 |
|
| /* Cleanup column arrays (SVs moved to rows, don't dec refcnt) */ |
| 969 |
0 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) { |
0 |
0 |
0 |
|
| 970 |
0 |
| Safefree(columns[c]); |
| 971 |
0 |
| free_col_type(col_types[c]); |
| 972 |
|
| } |
| 973 |
0 |
| Safefree(columns); |
| 974 |
0 |
| Safefree(col_types); |
| 975 |
0 |
▶ | if (cnames) Safefree(cnames); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cnames) Safefree(cnames); |
0 |
0 |
0 |
|
| 976 |
0 |
▶ | if (cname_lens) Safefree(cname_lens); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cname_lens) Safefree(cname_lens); |
0 |
0 |
0 |
|
| 977 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 978 |
0 |
| else pos = dpos; /* uncompressed: advance pos to match dpos */ |
| 979 |
|
| |
| 980 |
|
| /* Consume from recv_buf */ |
| 981 |
0 |
| recv_consume(self, pos); |
| 982 |
0 |
| return 1; |
| 983 |
|
| |
| 984 |
0 |
| data_error: |
| 985 |
0 |
| data_need_more: |
| 986 |
|
| /* Cleanup partial decode */ |
| 987 |
0 |
▶ | for (c = 0; c < num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < num_cols; c++) { |
0 |
0 |
0 |
|
| 988 |
0 |
▶ | if (columns[c]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (columns[c]) { |
0 |
0 |
0 |
|
| 989 |
|
| uint64_t j; |
| 990 |
0 |
▶ | for (j = 0; j < num_rows; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < num_rows; j++) { |
0 |
0 |
0 |
|
| 991 |
0 |
▶ | if (columns[c][j]) SvREFCNT_dec(columns[c][j]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (columns[c][j]) SvREFCNT_dec(columns[c][j]); |
0 |
0 |
0 |
|
| 992 |
|
| } |
| 993 |
0 |
| Safefree(columns[c]); |
| 994 |
|
| } |
| 995 |
0 |
▶ | if (col_types[c]) free_col_type(col_types[c]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (col_types[c]) free_col_type(col_types[c]); |
0 |
0 |
0 |
|
| 996 |
|
| } |
| 997 |
0 |
| Safefree(columns); |
| 998 |
0 |
| Safefree(col_types); |
| 999 |
0 |
▶ | if (cnames) Safefree(cnames); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cnames) Safefree(cnames); |
0 |
0 |
0 |
|
| 1000 |
0 |
▶ | if (cname_lens) Safefree(cname_lens); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cname_lens) Safefree(cname_lens); |
0 |
0 |
0 |
|
| 1001 |
0 |
▶ | if (decompressed) Safefree(decompressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompressed) Safefree(decompressed); |
0 |
0 |
0 |
|
| 1002 |
0 |
▶ | if (*errmsg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*errmsg) { |
0 |
0 |
0 |
|
| 1003 |
|
| /* data_error: flush recv_buf — data is malformed, cannot resume */ |
| 1004 |
0 |
| self->recv_len = 0; |
| 1005 |
0 |
| return -1; |
| 1006 |
|
| } |
| 1007 |
0 |
| return 0; |
| 1008 |
|
| } |
| 1009 |
|
| } |
| 1010 |
|
| |
| 1011 |
2 |
| case SERVER_EXCEPTION: { |
| 1012 |
|
| /* code: Int32, name: String, message: String, |
| 1013 |
|
| * stack_trace: String, has_nested: UInt8 */ |
| 1014 |
|
| int32_t code; |
| 1015 |
|
| const char *name, *msg, *stack; |
| 1016 |
|
| size_t name_len, msg_len, stack_len; |
| 1017 |
|
| uint8_t has_nested; |
| 1018 |
|
| char *err; |
| 1019 |
|
| |
| 1020 |
|
| /* We just read the top-level exception */ |
| 1021 |
2 |
| rc = read_i32(buf, len, &pos, &code); |
| 1022 |
2 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
2 |
2 |
|
| 1023 |
2 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed exception code"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed exception code"); return -1; } |
0 |
2 |
2 |
|
| 1024 |
|
| |
| 1025 |
2 |
| rc = read_native_string_ref(buf, len, &pos, &name, &name_len); |
| 1026 |
2 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
2 |
2 |
|
| 1027 |
2 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed exception name"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed exception name"); return -1; } |
0 |
2 |
2 |
|
| 1028 |
|
| |
| 1029 |
2 |
| rc = read_native_string_ref(buf, len, &pos, &msg, &msg_len); |
| 1030 |
2 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
2 |
2 |
|
| 1031 |
2 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed exception message"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed exception message"); return -1; } |
0 |
2 |
2 |
|
| 1032 |
|
| |
| 1033 |
2 |
| rc = read_native_string_ref(buf, len, &pos, &stack, &stack_len); |
| 1034 |
2 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
2 |
2 |
|
| 1035 |
2 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed exception stack"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed exception stack"); return -1; } |
0 |
2 |
2 |
|
| 1036 |
|
| |
| 1037 |
2 |
| rc = read_u8(buf, len, &pos, &has_nested); |
| 1038 |
2 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
2 |
2 |
|
| 1039 |
2 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed exception has_nested"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed exception has_nested"); return -1; } |
0 |
2 |
2 |
|
| 1040 |
|
| |
| 1041 |
|
| /* Skip nested exceptions — keep the top-level code, not the innermost. */ |
| 1042 |
2 |
▶ | while (has_nested) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (has_nested) { |
0 |
2 |
2 |
|
| 1043 |
|
| int32_t nested_code; |
| 1044 |
|
| int i; |
| 1045 |
0 |
| rc = read_i32(buf, len, &pos, &nested_code); |
| 1046 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1047 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } |
0 |
0 |
0 |
|
| 1048 |
|
| /* name, message, stack_trace */ |
| 1049 |
0 |
▶ | for (i = 0; i < 3; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 3; i++) { |
0 |
0 |
0 |
|
| 1050 |
0 |
| rc = skip_native_string(buf, len, &pos); |
| 1051 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1052 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } |
0 |
0 |
0 |
|
| 1053 |
|
| } |
| 1054 |
0 |
| rc = read_u8(buf, len, &pos, &has_nested); |
| 1055 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1056 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed nested exception"); return -1; } |
0 |
0 |
0 |
|
| 1057 |
|
| } |
| 1058 |
|
| |
| 1059 |
2 |
| self->last_error_code = code; |
| 1060 |
|
| |
| 1061 |
2 |
| Newx(err, msg_len + name_len + 64, char); |
| 1062 |
2 |
| snprintf(err, msg_len + name_len + 64, "Code: %d. %.*s: %.*s", |
| 1063 |
|
| (int)code, (int)name_len, name, (int)msg_len, msg); |
| 1064 |
|
| |
| 1065 |
2 |
| recv_consume(self, pos); |
| 1066 |
|
| |
| 1067 |
2 |
| *errmsg = err; |
| 1068 |
2 |
| return 5; |
| 1069 |
|
| } |
| 1070 |
|
| |
| 1071 |
0 |
| case SERVER_PROGRESS: { |
| 1072 |
|
| /* rows, bytes, total_rows, written_rows (>=54420), written_bytes (>=54420) */ |
| 1073 |
0 |
| uint64_t pp[5] = {0}; |
| 1074 |
0 |
▶ | int n = (self->server_revision >= DBMS_MIN_REVISION_WITH_PROGRESS_WRITES) ? 5 : 3; |
Branch: 1 branch
| Branch | True | False | Total |
| int n = (self->server_revision >= DBMS_MIN_REVISION_WITH_PROGRESS_WRITES) ? 5 : 3; |
0 |
0 |
0 |
|
| 1075 |
|
| int i; |
| 1076 |
0 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
0 |
0 |
0 |
|
| 1077 |
0 |
| rc = read_varuint(buf, len, &pos, &pp[i]); |
| 1078 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1079 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed progress packet"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed progress packet"); return -1; } |
0 |
0 |
0 |
|
| 1080 |
|
| } |
| 1081 |
|
| |
| 1082 |
0 |
| recv_consume(self, pos); |
| 1083 |
|
| |
| 1084 |
0 |
▶ | if (self->on_progress) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->on_progress) { |
0 |
0 |
0 |
|
| 1085 |
|
| /* Coalesce when progress_period is set: the protocol sends |
| 1086 |
|
| * incremental deltas, so accumulate and fire at the cadence. */ |
| 1087 |
0 |
▶ | if (self->progress_period > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->progress_period > 0) { |
0 |
0 |
0 |
|
| 1088 |
0 |
▶ | for (i = 0; i < 5; i++) self->progress_acc[i] += pp[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 5; i++) self->progress_acc[i] += pp[i]; |
0 |
0 |
0 |
|
| 1089 |
0 |
| double now = ev_now(self->loop); |
| 1090 |
0 |
▶ | if (now - self->progress_last < self->progress_period) |
Branch: 1 branch
| Branch | True | False | Total |
| if (now - self->progress_last < self->progress_period) |
0 |
0 |
0 |
|
| 1091 |
0 |
| return 1; |
| 1092 |
0 |
| self->progress_last = now; |
| 1093 |
0 |
▶ | for (i = 0; i < 5; i++) { pp[i] = self->progress_acc[i]; self->progress_acc[i] = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 5; i++) { pp[i] = self->progress_acc[i]; self->progress_acc[i] = 0; } |
0 |
0 |
0 |
|
| 1094 |
|
| } |
| 1095 |
|
| |
| 1096 |
0 |
▶ | if (fire_progress_cb(self, pp) < 0) return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fire_progress_cb(self, pp) < 0) return -2; |
0 |
0 |
0 |
|
| 1097 |
|
| } |
| 1098 |
|
| |
| 1099 |
0 |
| return 1; |
| 1100 |
|
| } |
| 1101 |
|
| |
| 1102 |
0 |
| case SERVER_PROFILE_INFO: { |
| 1103 |
|
| /* rows, blocks, bytes, applied_limit, rows_before_limit, calc_rows_before_limit */ |
| 1104 |
|
| uint64_t pi[6]; |
| 1105 |
|
| int i; |
| 1106 |
0 |
▶ | for (i = 0; i < 6; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 6; i++) { |
0 |
0 |
0 |
|
| 1107 |
0 |
| rc = read_varuint(buf, len, &pos, &pi[i]); |
| 1108 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1109 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed profile_info packet"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed profile_info packet"); return -1; } |
0 |
0 |
0 |
|
| 1110 |
|
| } |
| 1111 |
0 |
| self->profile_rows = pi[0]; |
| 1112 |
0 |
| self->profile_bytes = pi[2]; |
| 1113 |
0 |
| self->profile_rows_before_limit = pi[4]; |
| 1114 |
0 |
| recv_consume(self, pos); |
| 1115 |
0 |
| return 1; |
| 1116 |
|
| } |
| 1117 |
|
| |
| 1118 |
0 |
| case SERVER_TABLE_COLUMNS: { |
| 1119 |
|
| /* Format: string(table_name) + string(column_description). |
| 1120 |
|
| * The description carries each column's DEFAULT/MATERIALIZED/ |
| 1121 |
|
| * ALIAS kind and expression; keep it only when the pending |
| 1122 |
|
| * INSERT asked for empty_as_default or defaults. */ |
| 1123 |
|
| int i; |
| 1124 |
0 |
▶ | if ((self->insert_data || self->insert_av) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((self->insert_data || self->insert_av) |
0 |
0 |
0 |
| if ((self->insert_data || self->insert_av) |
0 |
0 |
0 |
|
| 1125 |
0 |
▶ | && (self->insert_empty_as_default || self->insert_defaults)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (self->insert_empty_as_default || self->insert_defaults)) { |
0 |
0 |
0 |
| && (self->insert_empty_as_default || self->insert_defaults)) { |
0 |
0 |
0 |
|
| 1126 |
0 |
| char *desc = NULL; |
| 1127 |
0 |
| size_t p0 = pos; |
| 1128 |
0 |
| rc = skip_native_string(buf, len, &p0); |
| 1129 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1130 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; } |
0 |
0 |
0 |
|
| 1131 |
0 |
| rc = read_native_string_alloc(buf, len, &p0, &desc, NULL); |
| 1132 |
0 |
▶ | if (rc == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) { |
0 |
0 |
0 |
|
| 1133 |
0 |
| Safefree(desc); |
| 1134 |
0 |
| return 0; |
| 1135 |
|
| } |
| 1136 |
0 |
▶ | if (rc < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { |
0 |
0 |
0 |
|
| 1137 |
0 |
| Safefree(desc); |
| 1138 |
0 |
| *errmsg = safe_strdup("malformed table_columns packet"); |
| 1139 |
0 |
| return -1; |
| 1140 |
|
| } |
| 1141 |
0 |
| free_insert_tc(self); |
| 1142 |
0 |
▶ | if (desc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (desc) { |
0 |
0 |
0 |
|
| 1143 |
0 |
| self->insert_tc = parse_table_columns_desc(desc, strlen(desc)); |
| 1144 |
0 |
| Safefree(desc); |
| 1145 |
|
| } |
| 1146 |
0 |
| pos = p0; |
| 1147 |
0 |
| recv_consume(self, pos); |
| 1148 |
0 |
| return 1; |
| 1149 |
|
| } |
| 1150 |
0 |
▶ | for (i = 0; i < 2; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 2; i++) { |
0 |
0 |
0 |
|
| 1151 |
0 |
| rc = skip_native_string(buf, len, &pos); |
| 1152 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1153 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed table_columns packet"); return -1; } |
0 |
0 |
0 |
|
| 1154 |
|
| } |
| 1155 |
0 |
| recv_consume(self, pos); |
| 1156 |
0 |
| return 1; |
| 1157 |
|
| } |
| 1158 |
|
| |
| 1159 |
0 |
| case SERVER_LOG: { |
| 1160 |
0 |
▶ | if (self->on_log) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->on_log) { |
0 |
0 |
0 |
|
| 1161 |
0 |
| rc = parse_and_emit_log_block(self, buf, len, &pos, self->on_log, errmsg); |
| 1162 |
|
| } else { |
| 1163 |
0 |
| rc = parse_and_discard_block(self, buf, len, &pos, "server log", 1, errmsg); |
| 1164 |
|
| } |
| 1165 |
0 |
▶ | if (rc <= 0) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) return rc; |
0 |
0 |
0 |
|
| 1166 |
0 |
| recv_consume(self, pos); |
| 1167 |
0 |
| return 1; |
| 1168 |
|
| } |
| 1169 |
|
| |
| 1170 |
0 |
| case SERVER_PROFILE_EVENTS: { |
| 1171 |
0 |
| rc = parse_and_discard_block(self, buf, len, &pos, "profile_events", 1, errmsg); |
| 1172 |
0 |
▶ | if (rc <= 0) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) return rc; |
0 |
0 |
0 |
|
| 1173 |
0 |
| recv_consume(self, pos); |
| 1174 |
0 |
| return 1; |
| 1175 |
|
| } |
| 1176 |
|
| |
| 1177 |
0 |
| case SERVER_TIMEZONE_UPDATE: { |
| 1178 |
|
| /* Server-side session timezone changed mid-query. Format: string(tz). */ |
| 1179 |
0 |
| char *tz = NULL; |
| 1180 |
0 |
| rc = read_native_string_alloc(buf, len, &pos, &tz, NULL); |
| 1181 |
0 |
▶ | if (rc == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; |
0 |
0 |
0 |
|
| 1182 |
0 |
▶ | if (rc < 0) { *errmsg = safe_strdup("malformed timezone_update packet"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { *errmsg = safe_strdup("malformed timezone_update packet"); return -1; } |
0 |
0 |
0 |
|
| 1183 |
0 |
▶ | CLEAR_STR(self->server_timezone); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->server_timezone); |
0 |
0 |
0 |
|
| 1184 |
0 |
| self->server_timezone = tz; |
| 1185 |
0 |
| recv_consume(self, pos); |
| 1186 |
0 |
| return 1; |
| 1187 |
|
| } |
| 1188 |
|
| |
| 1189 |
0 |
| case SERVER_PONG: |
| 1190 |
0 |
| recv_consume(self, pos); |
| 1191 |
0 |
| return 3; |
| 1192 |
|
| |
| 1193 |
12 |
| case SERVER_END_OF_STREAM: |
| 1194 |
12 |
| recv_consume(self, pos); |
| 1195 |
12 |
| return 2; |
| 1196 |
|
| |
| 1197 |
1 |
| default: { |
| 1198 |
|
| /* Unknown packet type */ |
| 1199 |
|
| char err[64]; |
| 1200 |
1 |
| snprintf(err, sizeof(err), "unknown server packet type: %llu", |
| 1201 |
|
| (unsigned long long)ptype); |
| 1202 |
1 |
| *errmsg = safe_strdup(err); |
| 1203 |
1 |
| self->recv_len = 0; |
| 1204 |
1 |
| return -1; |
| 1205 |
|
| } |
| 1206 |
|
| } |
| 1207 |
|
| } |
| 1208 |
|
| |
| 1209 |
|
| /* |
| 1210 |
|
| * Process native protocol responses from recv_buf. |
| 1211 |
|
| * Called from on_readable when protocol == PROTO_NATIVE. |
| 1212 |
|
| */ |
| 1213 |
50 |
| static void process_native_response(ev_clickhouse_t *self) { |
| 1214 |
64 |
▶ | while (self->recv_len > 0 && self->magic == EV_CH_MAGIC) { |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 1215 |
50 |
| char *errmsg = NULL; |
| 1216 |
|
| int rc; |
| 1217 |
50 |
| rc = parse_native_packet(self, &errmsg); |
| 1218 |
|
| |
| 1219 |
50 |
▶ | if (rc == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) { |
0 |
50 |
50 |
|
| 1220 |
|
| /* need more data */ |
| 1221 |
36 |
| return; |
| 1222 |
|
| } |
| 1223 |
|
| |
| 1224 |
50 |
▶ | if (rc == -2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == -2) { |
0 |
50 |
50 |
|
| 1225 |
|
| /* object destroyed inside callback */ |
| 1226 |
0 |
| return; |
| 1227 |
|
| } |
| 1228 |
|
| |
| 1229 |
50 |
▶ | if (rc == 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 4) { |
20 |
30 |
50 |
|
| 1230 |
|
| /* ServerHello received — send addendum (revision >= 54458) */ |
| 1231 |
20 |
▶ | if (self->native_state == NATIVE_WAIT_HELLO) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->native_state == NATIVE_WAIT_HELLO) { |
20 |
0 |
20 |
|
| 1232 |
|
| /* Addendum: quota_key (only if server supports it) */ |
| 1233 |
20 |
▶ | if (self->server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_ADDENDUM) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_ADDENDUM) { |
20 |
0 |
20 |
|
| 1234 |
|
| native_buf_t ab; |
| 1235 |
20 |
| nbuf_init(&ab); |
| 1236 |
20 |
| nbuf_cstring(&ab, ""); /* quota_key */ |
| 1237 |
20 |
| send_replace(self, ab.data, ab.len); |
| 1238 |
20 |
▶ | if (try_write(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (try_write(self)) return; |
0 |
20 |
20 |
|
| 1239 |
20 |
▶ | if (self->send_pos < self->send_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->send_pos < self->send_len) { |
0 |
20 |
20 |
|
| 1240 |
|
| /* Addendum partially written (EAGAIN); io_cb finishes |
| 1241 |
|
| * connect once the buffer drains, otherwise pipeline_advance |
| 1242 |
|
| * could overwrite the unsent tail with a queued query. */ |
| 1243 |
0 |
| self->pending_addendum_finish = 1; |
| 1244 |
0 |
| return; |
| 1245 |
|
| } |
| 1246 |
|
| } |
| 1247 |
20 |
| self->native_state = NATIVE_IDLE; |
| 1248 |
20 |
▶ | if (finish_connect(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (finish_connect(self)) return; |
0 |
20 |
20 |
|
| 1249 |
|
| } |
| 1250 |
|
| /* pipeline_advance -> try_write may free self; no data |
| 1251 |
|
| * in recv_buf for the just-dispatched request yet */ |
| 1252 |
20 |
| return; |
| 1253 |
|
| } |
| 1254 |
|
| |
| 1255 |
30 |
▶ | if (rc == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == -1) { |
2 |
28 |
30 |
|
| 1256 |
|
| /* Stream corruption: native packets aren't length-delimited, |
| 1257 |
|
| * so resync is impossible — the server's remaining output |
| 1258 |
|
| * for this query would be misattributed to the next one. |
| 1259 |
|
| * Tear down; auto_reconnect re-establishes an idle link. */ |
| 1260 |
2 |
▶ | if (self->native_state == NATIVE_WAIT_HELLO) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->native_state == NATIVE_WAIT_HELLO) { |
1 |
1 |
2 |
|
| 1261 |
|
| /* Skip auto_reconnect on a malformed ServerHello: the peer |
| 1262 |
|
| * isn't a ClickHouse server, so retrying just spins. |
| 1263 |
|
| * (connect_timeout still uses fail_connection, which retries.) */ |
| 1264 |
1 |
| teardown_io_error(self, errmsg, "connection failed"); |
| 1265 |
1 |
| Safefree(errmsg); |
| 1266 |
1 |
| return; |
| 1267 |
|
| } |
| 1268 |
1 |
| fail_connection(self, errmsg); |
| 1269 |
1 |
| Safefree(errmsg); |
| 1270 |
1 |
| return; |
| 1271 |
|
| } |
| 1272 |
|
| |
| 1273 |
28 |
▶ | if (rc == 5) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 5) { |
2 |
26 |
28 |
|
| 1274 |
|
| /* Server exception — a normal query error with its bytes |
| 1275 |
|
| * consumed, so the stream stays in sync: deliver to the |
| 1276 |
|
| * query callback and pipeline the next query. */ |
| 1277 |
2 |
▶ | if (self->native_state == NATIVE_WAIT_HELLO) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->native_state == NATIVE_WAIT_HELLO) { |
0 |
2 |
2 |
|
| 1278 |
|
| /* The server rejected the handshake itself (e.g. wrong |
| 1279 |
|
| * credentials) — fail fast with the real text, to |
| 1280 |
|
| * on_error and to any pre-connect queued queries. No |
| 1281 |
|
| * reconnect: retrying a rejected handshake just spins. */ |
| 1282 |
0 |
| teardown_io_error(self, errmsg, errmsg); |
| 1283 |
0 |
| Safefree(errmsg); |
| 1284 |
0 |
| return; |
| 1285 |
|
| } |
| 1286 |
2 |
| stop_timing(self); |
| 1287 |
|
| |
| 1288 |
2 |
▶ | CLEAR_SV(self->native_rows); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_rows); |
0 |
2 |
2 |
|
| 1289 |
2 |
▶ | CLEAR_INSERT(self); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(self); |
0 |
2 |
2 |
| CLEAR_INSERT(self); |
2 |
0 |
2 |
|
| 1290 |
2 |
| free_insert_default_state(self); |
| 1291 |
2 |
▶ | CLEAR_STR(self->insert_err); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->insert_err); |
0 |
2 |
2 |
|
| 1292 |
2 |
| self->native_state = NATIVE_IDLE; |
| 1293 |
2 |
| self->cancel_after_meta = 0; |
| 1294 |
2 |
| self->cancelled = 0; |
| 1295 |
2 |
| self->recv_len = 0; /* drop this dead query's tail, if coalesced */ |
| 1296 |
2 |
▶ | if (self->send_count > 0) self->send_count--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->send_count > 0) self->send_count--; |
2 |
0 |
2 |
|
| 1297 |
2 |
| lc_free_dicts(self); |
| 1298 |
2 |
| int destroyed = deliver_error(self, errmsg); |
| 1299 |
2 |
| Safefree(errmsg); |
| 1300 |
2 |
▶ | if (destroyed) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (destroyed) return; |
0 |
2 |
2 |
|
| 1301 |
|
| |
| 1302 |
|
| /* advance pipeline — may free self via try_write error */ |
| 1303 |
2 |
| pipeline_advance(self); |
| 1304 |
2 |
| return; |
| 1305 |
|
| } |
| 1306 |
|
| |
| 1307 |
26 |
▶ | if (rc == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 2) { |
12 |
14 |
26 |
|
| 1308 |
|
| /* EndOfStream — deliver accumulated rows or deferred error */ |
| 1309 |
12 |
| stop_timing(self); |
| 1310 |
12 |
| self->native_state = NATIVE_IDLE; |
| 1311 |
12 |
| self->cancel_after_meta = 0; |
| 1312 |
12 |
▶ | CLEAR_INSERT(self); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(self); |
0 |
12 |
12 |
| CLEAR_INSERT(self); |
2 |
10 |
12 |
|
| 1313 |
12 |
| free_insert_default_state(self); |
| 1314 |
12 |
▶ | if (self->send_count > 0) self->send_count--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->send_count > 0) self->send_count--; |
12 |
0 |
12 |
|
| 1315 |
12 |
| lc_free_dicts(self); |
| 1316 |
|
| |
| 1317 |
|
| /* Flush any uncoalesced progress accumulated since the last |
| 1318 |
|
| * fire so users instrumenting via on_progress see the full |
| 1319 |
|
| * total when the query completes within one progress_period |
| 1320 |
|
| * of the last fire. */ |
| 1321 |
12 |
▶ | if (self->on_progress && self->progress_period > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->on_progress && self->progress_period > 0) { |
0 |
12 |
12 |
| if (self->on_progress && self->progress_period > 0) { |
0 |
0 |
0 |
|
| 1322 |
0 |
| int any = 0, i; |
| 1323 |
0 |
▶ | for (i = 0; i < 5; i++) if (self->progress_acc[i]) { any = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 1324 |
0 |
▶ | if (any) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (any) { |
0 |
0 |
0 |
|
| 1325 |
|
| uint64_t pp[5]; |
| 1326 |
0 |
| memcpy(pp, self->progress_acc, sizeof(pp)); |
| 1327 |
0 |
| memset(self->progress_acc, 0, sizeof(self->progress_acc)); |
| 1328 |
0 |
▶ | if (fire_progress_cb(self, pp) < 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fire_progress_cb(self, pp) < 0) return; |
0 |
0 |
0 |
|
| 1329 |
|
| } |
| 1330 |
|
| } |
| 1331 |
|
| |
| 1332 |
12 |
▶ | if (self->insert_err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->insert_err) { |
8 |
4 |
12 |
|
| 1333 |
8 |
| char *err = self->insert_err; |
| 1334 |
8 |
| self->insert_err = NULL; |
| 1335 |
8 |
▶ | CLEAR_SV(self->native_rows); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_rows); |
0 |
8 |
8 |
|
| 1336 |
8 |
| int destroyed = deliver_error(self, err); |
| 1337 |
8 |
| Safefree(err); |
| 1338 |
8 |
▶ | if (destroyed) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (destroyed) return; |
0 |
8 |
8 |
|
| 1339 |
4 |
▶ | } else if (self->cancelled) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->cancelled) { |
2 |
2 |
4 |
|
| 1340 |
2 |
| self->cancelled = 0; |
| 1341 |
2 |
▶ | CLEAR_SV(self->native_rows); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_rows); |
0 |
2 |
2 |
|
| 1342 |
2 |
▶ | if (deliver_error(self, "query cancelled")) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (deliver_error(self, "query cancelled")) return; |
0 |
2 |
2 |
|
| 1343 |
|
| } else { |
| 1344 |
2 |
| AV *rows = self->native_rows; |
| 1345 |
2 |
| self->native_rows = NULL; |
| 1346 |
2 |
▶ | if (deliver_rows(self, rows)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (deliver_rows(self, rows)) return; |
0 |
2 |
2 |
|
| 1347 |
|
| } |
| 1348 |
|
| |
| 1349 |
|
| /* advance pipeline — may free self via try_write error */ |
| 1350 |
12 |
| pipeline_advance(self); |
| 1351 |
12 |
| return; |
| 1352 |
|
| } |
| 1353 |
|
| |
| 1354 |
14 |
▶ | if (rc == 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 3) { |
0 |
14 |
14 |
|
| 1355 |
|
| /* Pong — deliver to the queued cb (keepalive no-op or user ping) */ |
| 1356 |
0 |
| self->native_state = NATIVE_IDLE; |
| 1357 |
0 |
| self->cancel_after_meta = 0; |
| 1358 |
0 |
| stop_timing(self); |
| 1359 |
0 |
▶ | if (self->send_count > 0) self->send_count--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->send_count > 0) self->send_count--; |
0 |
0 |
0 |
|
| 1360 |
0 |
▶ | if (self->cancelled) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->cancelled) { |
0 |
0 |
0 |
|
| 1361 |
0 |
| self->cancelled = 0; |
| 1362 |
0 |
▶ | if (deliver_error(self, "query cancelled")) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (deliver_error(self, "query cancelled")) return; |
0 |
0 |
0 |
|
| 1363 |
|
| } else { |
| 1364 |
0 |
| AV *rows = newAV(); |
| 1365 |
0 |
▶ | if (deliver_rows(self, rows)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (deliver_rows(self, rows)) return; |
0 |
0 |
0 |
|
| 1366 |
|
| } |
| 1367 |
0 |
| pipeline_advance(self); |
| 1368 |
0 |
| return; |
| 1369 |
|
| } |
| 1370 |
|
| |
| 1371 |
|
| /* rc == 1: Data/Progress/ProfileInfo — continue reading */ |
| 1372 |
|
| } |
| 1373 |
|
| } |
| 1374 |
|
| |