Encoder.xs

filter: Statementstmt 96.0% 391 / 407 Branchbran 69.5% 224 / 322 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.3% 615 / 729 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« json_kind.c ↑ Summary decode.c »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <string.h>
7 #include <stdint.h>
8  
9 #if IVSIZE < 8
10 #error "ClickHouse::Encoder requires a 64-bit Perl (IVSIZE >= 8)"
11 #endif
12  
13 #include "types.h"
14 #include "buffer.h"
15 #include "encode.h"
16 #include "decode.h"
17 #include "runtime.h"
18 #include "cityhash.h"
19 #include "scalar_kind.h"
20  
21 /* Read one varint from an SV's byte buffer starting at *off; advances
22 * *off past the consumed bytes. Croaks with TCP-style messages on
23 * truncation or overflow. Used by the ClickHouse::Encoder::TCP XSUBs
24 * which operate on offsets into a Perl scalar rather than the (p,end)
25 * cursor pair used by the internal native decoder. */
26 174 static UV tcp_read_varint(pTHX_ const unsigned char *p, UV buf_len,
27 UV *off) {
28 174 UV v = 0;
29 174 int shift = 0;
30 18 while (1) {
31 192 ▶ if (*off >= buf_len)
Branch: 1 branch
BranchTrueFalseTotal
if (*off >= buf_len) 14 178 192
32 14 croak("varint: truncated at offset %lu", (unsigned long)*off);
33 178 unsigned char b = p[(*off)++];
34 178 ▶ if (shift == 63 && (b & 0x7e)) croak("varint exceeds 64 bits");
Branch: 2 branches
BranchTrueFalseTotal
if (shift == 63 && (b & 0x7e)) croak("varint exceeds 64 bits"); 0 178 178
if (shift == 63 && (b & 0x7e)) croak("varint exceeds 64 bits"); 0 0 0
35 178 v |= ((UV)(b & 0x7f)) << shift;
36 178 ▶ if (!(b & 0x80)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!(b & 0x80)) break; 160 18 178
37 18 shift += 7;
38 18 ▶ if (shift >= 64) croak("varint exceeds 64 bits");
Branch: 1 branch
BranchTrueFalseTotal
if (shift >= 64) croak("varint exceeds 64 bits"); 0 18 18
39 }
40 160 return v;
41 }
42  
43 2569 static SSize_t *encode_positions(pTHX_ Encoder *enc, int count, SV **args) {
44 2569 SSize_t *positions = NULL;
45 int i;
46 2569 ▶ if (count % 2) croak("Expected key-value pairs after rows");
Branch: 1 branch
BranchTrueFalseTotal
if (count % 2) croak("Expected key-value pairs after rows"); 0 2569 2569
47 2578 ▶ for (i = 0; i < count; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i += 2) { 17 2561 2578
48 STRLEN len;
49 17 const char *key = SvPV(args[i], len);
50 SSize_t j;
51 AV *av;
52 17 ▶ if (len != 9 || memcmp(key, "positions", 9))
Branch: 2 branches
BranchTrueFalseTotal
if (len != 9 || memcmp(key, "positions", 9)) 17 0 17
if (len != 9 || memcmp(key, "positions", 9)) 0 17 17
53 0 croak("Unknown encode option: %.*s", (int)len, key);
54 17 SvGETMAGIC(args[i + 1]);
55 17 ▶ if (!SvROK(args[i + 1]) || SvTYPE(SvRV(args[i + 1])) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(args[i + 1]) || SvTYPE(SvRV(args[i + 1])) != SVt_PVAV) 16 1 17
if (!SvROK(args[i + 1]) || SvTYPE(SvRV(args[i + 1])) != SVt_PVAV) 1 15 16
56 2 croak("positions must be an arrayref");
57 15 av = (AV *)SvRV(args[i + 1]);
58 15 ▶ if (av_len(av) + 1 != enc->num_columns)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(av) + 1 != enc->num_columns) 1 14 15
59 1 croak("positions length must equal the number of columns (%d)", enc->num_columns);
60 14 ▶ if (!positions) {
Branch: 1 branch
BranchTrueFalseTotal
if (!positions) { 14 0 14
61 14 ▶ Newx(positions, enc->num_columns ? enc->num_columns : 1, SSize_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(positions, enc->num_columns ? enc->num_columns : 1, SSize_t); 14 0 14
62 14 SAVEFREEPV(positions);
63 }
64 154 ▶ for (j = 0; j < enc->num_columns; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < enc->num_columns; j++) { 145 9 154
65 145 SV **slot = av_fetch(av, j, 0);
66 STRLEN k, n;
67 const char *s;
68 145 UV value = 0;
69 145 ▶ if (!slot) croak("positions entries must be non-negative integers");
Branch: 1 branch
BranchTrueFalseTotal
if (!slot) croak("positions entries must be non-negative integers"); 0 145 145
70 145 SvGETMAGIC(*slot);
71 145 ▶ if (!SvOK(*slot) || SvROK(*slot)) croak("positions entries must be non-negative integers");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(*slot) || SvROK(*slot)) croak("positions entries must be non-negative integers"); 145 0 145
if (!SvOK(*slot) || SvROK(*slot)) croak("positions entries must be non-negative integers"); 0 145 145
72 145 s = SvPV(*slot, n);
73 145 ▶ if (!looks_like_int_str(s, n) || *s == '-')
Branch: 2 branches
BranchTrueFalseTotal
if (!looks_like_int_str(s, n) || *s == '-') 142 3 145
if (!looks_like_int_str(s, n) || *s == '-') 1 141 142
74 4 croak("positions entries must be non-negative integers");
75 389 ▶ for (k = *s == '+' ? 1 : 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = *s == '+' ? 1 : 0; k < n; k++) { 249 140 389
76 249 UV digit = s[k] - '0';
77 249 ▶ if (value > ((UV)SSize_t_MAX - 1 - digit) / 10)
Branch: 1 branch
BranchTrueFalseTotal
if (value > ((UV)SSize_t_MAX - 1 - digit) / 10) 1 248 249
78 1 croak("positions entry out of range");
79 248 value = value * 10 + digit;
80 }
81 140 positions[j] = (SSize_t)value;
82 }
83 }
84 2561 return positions;
85 }
86  
87 MODULE = ClickHouse::Encoder PACKAGE = ClickHouse::Encoder
88  
89 PROTOTYPES: DISABLE
90  
91 SV*
92 new(class, ...)
93 const char *class
94 CODE:
95 {
96 1814 Encoder *enc = NULL;
97 AV *cols_av;
98 SSize_t i, n;
99 1814 SV *cols_sv = NULL;
100 1814 const char *timezone = "UTC";
101 1814 int empty_as_null = 0;
102  
103 1814 ▶ if (items % 2 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (items % 2 == 0) 0 1814 1814
104 0 croak("Expected key-value pairs");
105  
106 3718 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 1905 1813 3718
107 STRLEN klen;
108 1905 const char *key = SvPV(ST(i), klen);
109 1905 ▶ if (klen == 7 && memcmp(key, "columns", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memcmp(key, "columns", 7) == 0) 1813 92 1905
if (klen == 7 && memcmp(key, "columns", 7) == 0) 1813 0 1813
110 1813 cols_sv = ST(i+1);
111 92 ▶ else if (klen == 8 && memcmp(key, "timezone", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 8 && memcmp(key, "timezone", 8) == 0) { 41 51 92
else if (klen == 8 && memcmp(key, "timezone", 8) == 0) { 41 0 41
112 STRLEN len;
113 const char *name;
114 41 SvGETMAGIC(ST(i+1));
115 41 ▶ if (!SvOK(ST(i+1))) croak("timezone must be a name");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ST(i+1))) croak("timezone must be a name"); 1 40 41
116 40 name = SvPV_nomg(ST(i+1), len);
117 40 ▶ if (!len || memchr(name, 0, len)) croak("Invalid timezone name");
Branch: 2 branches
BranchTrueFalseTotal
if (!len || memchr(name, 0, len)) croak("Invalid timezone name"); 40 0 40
if (!len || memchr(name, 0, len)) croak("Invalid timezone name"); 0 40 40
118 40 timezone = SvPVX(sv_2mortal(newSVpvn(name, len)));
119 51 ▶ } else if (klen == 13 && memcmp(key, "empty_as_null", 13) == 0)
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 13 && memcmp(key, "empty_as_null", 13) == 0) 51 0 51
} else if (klen == 13 && memcmp(key, "empty_as_null", 13) == 0) 51 0 51
120 51 empty_as_null = SvTRUE(ST(i+1));
121 }
122  
123 1813 ▶ if (!cols_sv || !SvROK(cols_sv) || SvTYPE(SvRV(cols_sv)) != SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (!cols_sv || !SvROK(cols_sv) || SvTYPE(SvRV(cols_sv)) != SVt_PVAV) 1812 1 1813
if (!cols_sv || !SvROK(cols_sv) || SvTYPE(SvRV(cols_sv)) != SVt_PVAV) 1811 1 1812
if (!cols_sv || !SvROK(cols_sv) || SvTYPE(SvRV(cols_sv)) != SVt_PVAV) 0 1811 1811
124 2 croak("columns required and must be arrayref");
125  
126 1811 cols_av = (AV*)SvRV(cols_sv);
127 1811 n = av_len(cols_av) + 1;
128  
129 /* Wrap allocation in our own ENTER/LEAVE so the cleanup destructor fires
130 * before this XSUB returns (XSUBs don't get implicit ENTER/LEAVE). */
131 1811 ENTER;
132 1811 Newxz(enc, 1, Encoder);
133 1811 SAVEDESTRUCTOR_X(cleanup_encoder_slot, &enc);
134  
135 1811 ▶ Newxz(enc->columns, n, Column);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(enc->columns, n, Column); 0 1811 1811
136 1811 enc->num_columns = n;
137  
138 6429 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4649 1780 6429
139 4649 SV **col_sv = av_fetch(cols_av, i, 0);
140 AV *col_av;
141 SV **name_sv, **type_sv;
142 STRLEN len;
143 const char *s;
144  
145 4649 ▶ if (!col_sv || !SvROK(*col_sv) || SvTYPE(SvRV(*col_sv)) != SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (!col_sv || !SvROK(*col_sv) || SvTYPE(SvRV(*col_sv)) != SVt_PVAV) 4649 0 4649
if (!col_sv || !SvROK(*col_sv) || SvTYPE(SvRV(*col_sv)) != SVt_PVAV) 4649 0 4649
if (!col_sv || !SvROK(*col_sv) || SvTYPE(SvRV(*col_sv)) != SVt_PVAV) 0 4649 4649
146 0 croak("Column must be [name, type]");
147  
148 4649 col_av = (AV*)SvRV(*col_sv);
149 4649 name_sv = av_fetch(col_av, 0, 0);
150 4649 type_sv = av_fetch(col_av, 1, 0);
151  
152 4649 ▶ if (!name_sv || !type_sv)
Branch: 2 branches
BranchTrueFalseTotal
if (!name_sv || !type_sv) 4649 0 4649
if (!name_sv || !type_sv) 2 4647 4649
153 2 croak("Column must be [name, type]");
154  
155 4647 s = SvPV(*name_sv, len);
156 4647 Newx(enc->columns[i].name, len + 1, char);
157 4647 memcpy(enc->columns[i].name, s, len);
158 4647 enc->columns[i].name[len] = 0;
159 4647 enc->columns[i].name_len = len;
160  
161 4647 ▶ s = SvPV(*type_sv, len);
Branch: 1 branch
BranchTrueFalseTotal
s = SvPV(*type_sv, len); 4647 0 4647
162 4647 Newx(enc->columns[i].type_str, len + 1, char);
163 4647 memcpy(enc->columns[i].type_str, s, len);
164 4647 enc->columns[i].type_str[len] = 0;
165 4647 enc->columns[i].type_len = len;
166  
167 4647 enc->columns[i].type = parse_type(aTHX_ enc->columns[i].type_str, len);
168 }
169  
170 1780 configure_encoder(aTHX_ enc, timezone, empty_as_null);
171 1776 RETVAL = newSV(0);
172 1776 sv_setref_pv(RETVAL, class, (void*)enc);
173 1776 enc = NULL; /* Disarm: the SV's DESTROY now owns the encoder. */
174 1776 LEAVE;
175 }
176 OUTPUT:
177 RETVAL
178  
179 SV*
180 encode(self, rows, ...)
181 SV *self
182 SV *rows
183 CODE:
184 {
185 Encoder *enc;
186 Buffer buf;
187  
188 2563 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 2563 2563
189 2563 ▶ if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) 2561 2 2563
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) 0 2561 2561
190 2 croak("rows must be arrayref");
191  
192 2561 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
193 2561 buf_init(aTHX_ &buf);
194 2553 do_encode(aTHX_ enc, (AV *)SvRV(rows), &buf,
195 2561 encode_positions(aTHX_ enc, items - 2, &ST(2)));
196 2349 RETVAL = newSVpvn(buf.ptr, buf.len);
197 }
198 OUTPUT:
199 RETVAL
200  
201 void
202 encode_into(self, target_ref, rows, ...)
203 SV *self
204 SV *target_ref
205 SV *rows
206 CODE:
207 {
208 Encoder *enc;
209 Buffer buf;
210 SV *target;
211  
212 5 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 5 5
213 5 ▶ if (!SvROK(target_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(target_ref)) 2 3 5
214 2 croak("encode_into: first argument must be a scalar reference");
215 3 ▶ if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) 3 0 3
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) 0 3 3
216 0 croak("rows must be arrayref");
217  
218 3 target = SvRV(target_ref);
219 3 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
220  
221 3 buf_init(aTHX_ &buf);
222 3 do_encode(aTHX_ enc, (AV *)SvRV(rows), &buf,
223 3 encode_positions(aTHX_ enc, items - 3, &ST(3)));
224  
225 3 ▶ if (!SvOK(target)) sv_setpvn(target, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(target)) sv_setpvn(target, "", 0); 0 3 3
226 3 sv_catpvn(target, buf.ptr, buf.len);
227 }
228  
229 void
230 encode_to_handle(self, fh, rows, ...)
231 SV *self
232 SV *fh
233 SV *rows
234 CODE:
235 {
236 Encoder *enc;
237 Buffer buf;
238 IO *io;
239 PerlIO *pio;
240  
241 6 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 6 6
242 6 ▶ if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) 6 0 6
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) 0 6 6
243 0 croak("rows must be arrayref");
244 6 io = sv_2io(fh);
245 6 ▶ if (!io) croak("encode_to_handle: not a filehandle");
Branch: 1 branch
BranchTrueFalseTotal
if (!io) croak("encode_to_handle: not a filehandle"); 0 6 6
246 6 pio = IoOFP(io);
247 6 ▶ if (!pio) croak("encode_to_handle: filehandle not open for writing");
Branch: 1 branch
BranchTrueFalseTotal
if (!pio) croak("encode_to_handle: filehandle not open for writing"); 1 5 6
248  
249 5 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
250 5 buf_init(aTHX_ &buf);
251 5 do_encode(aTHX_ enc, (AV *)SvRV(rows), &buf,
252 5 encode_positions(aTHX_ enc, items - 3, &ST(3)));
253  
254 5 ▶ if (PerlIO_write(pio, buf.ptr, buf.len) != (SSize_t)buf.len)
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_write(pio, buf.ptr, buf.len) != (SSize_t)buf.len) 1 4 5
255 1 croak("encode_to_handle: short write: %s", strerror(errno));
256 }
257  
258 SV*
259 encode_columns(self, cols_hv)
260 SV *self
261 SV *cols_hv
262 CODE:
263 {
264 /* Column-oriented input: cols_hv is a hashref { name => [v0, v1, ...] }
265 * with one array per column, all the same length. Skips the row->column
266 * permutation step that encode() does for arrayref-of-arrayref input. */
267 Encoder *enc;
268 HV *cols;
269 Buffer buf;
270 13 SSize_t num_rows = -1;
271 int c;
272  
273 13 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 13 13
274 13 ▶ if (!SvROK(cols_hv) || SvTYPE(SvRV(cols_hv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cols_hv) || SvTYPE(SvRV(cols_hv)) != SVt_PVHV) 12 1 13
if (!SvROK(cols_hv) || SvTYPE(SvRV(cols_hv)) != SVt_PVHV) 0 12 12
275 1 croak("encode_columns: arg must be hashref { name => [...] }");
276  
277 12 cols = (HV *)SvRV(cols_hv);
278 12 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
279  
280 /* Validate: every declared column must be present and same length. */
281 148 ▶ for (c = 0; c < enc->num_columns; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < enc->num_columns; c++) { 141 7 148
282 141 Column *col = &enc->columns[c];
283 141 SV **slot = fetch_named_value(aTHX_ cols, col->name, col->name_len);
284 141 ▶ if (!slot || !SvOK(*slot))
Branch: 2 branches
BranchTrueFalseTotal
if (!slot || !SvOK(*slot)) 139 2 141
if (!slot || !SvOK(*slot)) 0 139 139
285 2 croak("encode_columns: missing column '%.*s'",
286 (int)col->name_len, col->name);
287 139 ▶ if (!SvROK(*slot) || SvTYPE(SvRV(*slot)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*slot) || SvTYPE(SvRV(*slot)) != SVt_PVAV) 138 1 139
if (!SvROK(*slot) || SvTYPE(SvRV(*slot)) != SVt_PVAV) 0 138 138
288 1 croak("encode_columns: column '%.*s' must be an arrayref",
289 (int)col->name_len, col->name);
290 138 AV *av = (AV *)SvRV(*slot);
291 138 SSize_t n = av_len(av) + 1;
292 138 ▶ if (num_rows == -1) num_rows = n;
Branch: 1 branch
BranchTrueFalseTotal
if (num_rows == -1) num_rows = n; 11 127 138
293 127 ▶ else if (n != num_rows)
Branch: 1 branch
BranchTrueFalseTotal
else if (n != num_rows) 2 125 127
294 2 croak("encode_columns: column '%.*s' has %" IVdf " rows, "
295 "expected %" IVdf, (int)col->name_len, col->name,
296 (IV)n, (IV)num_rows);
297 }
298 7 ▶ if (num_rows == -1) num_rows = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (num_rows == -1) num_rows = 0; 0 7 7
299  
300 7 buf_init(aTHX_ &buf);
301 7 buf_varint(aTHX_ &buf, enc->num_columns);
302 7 buf_varint(aTHX_ &buf, num_rows);
303  
304 7 SV **col_values = alloc_sv_array(aTHX_ num_rows);
305 139 ▶ for (c = 0; c < enc->num_columns; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < enc->num_columns; c++) { 132 7 139
306 132 Column *col = &enc->columns[c];
307 132 SV **slot = fetch_named_value(aTHX_ cols, col->name, col->name_len);
308 132 AV *av = (AV *)SvRV(*slot);
309 SSize_t r;
310  
311 132 buf_string(aTHX_ &buf, col->name, col->name_len);
312 132 buf_string(aTHX_ &buf, col->type_str, col->type_len);
313  
314 406 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 274 132 406
315 274 SV **e = av_fetch(av, r, 0);
316 274 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 274 0 274
317 /* Same magic-realization step as do_encode's gather:
318 * tied per-column AVs return magical placeholders that
319 * downstream SvIV / SvPV won't see until SvGETMAGIC. */
320 274 SvGETMAGIC(*e);
321 274 col_values[r] = *e;
322 } else {
323 0 col_values[r] = &PL_sv_undef;
324 }
325 }
326 132 encode_column(aTHX_ &buf, col_values, num_rows, col->type);
327 }
328  
329 7 RETVAL = newSVpvn(buf.ptr, buf.len);
330 }
331 OUTPUT:
332 RETVAL
333  
334 void
335 stream(self, iter, writer, ...)
336 SV *self
337 SV *iter
338 SV *writer
339 CODE:
340 {
341 /* XS-side iterator loop: pull rows via call_sv, batch them in an AV,
342 * encode and call writer on threshold. Saves per-row Perl method
343 * dispatch compared to driving the loop from Perl. */
344 Encoder *enc;
345 2 int batch_size = 10000;
346 AV *batch;
347 int n;
348  
349 2 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 2 2
350 2 ▶ if (!SvROK(iter) || SvTYPE(SvRV(iter)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(iter) || SvTYPE(SvRV(iter)) != SVt_PVCV) 2 0 2
if (!SvROK(iter) || SvTYPE(SvRV(iter)) != SVt_PVCV) 0 2 2
351 0 croak("stream: iter must be a coderef");
352 2 ▶ if (!SvROK(writer) || SvTYPE(SvRV(writer)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(writer) || SvTYPE(SvRV(writer)) != SVt_PVCV) 2 0 2
if (!SvROK(writer) || SvTYPE(SvRV(writer)) != SVt_PVCV) 0 2 2
353 0 croak("stream: writer must be a coderef");
354  
355 /* Optional named arg: batch_size => N */
356 2 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 2 0 2
357 int i;
358 4 ▶ for (i = 3; i < items - 1; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i < items - 1; i += 2) { 2 2 4
359 STRLEN klen;
360 2 const char *key = SvPV(ST(i), klen);
361 2 ▶ if (klen == 10 && memcmp(key, "batch_size", 10) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 10 && memcmp(key, "batch_size", 10) == 0) 2 0 2
if (klen == 10 && memcmp(key, "batch_size", 10) == 0) 2 0 2
362 2 batch_size = (int)SvIV(ST(i+1));
363 }
364 2 ▶ if (batch_size < 1) batch_size = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_size < 1) batch_size = 1; 0 2 2
365 }
366  
367 2 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
368 2 batch = (AV *)sv_2mortal((SV *)newAV());
369  
370 50025 for (;;) {
371 50027 dSP;
372 50027 ENTER; SAVETMPS;
373 50027 ▶ PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); PUTBACK; 0 50027 50027
374 50027 n = call_sv(iter, G_SCALAR);
375 50027 SPAGAIN;
376 50027 ▶ SV *row = (n > 0) ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *row = (n > 0) ? POPs : &PL_sv_undef; 50027 0 50027
377 50027 int got = SvOK(row);
378 50027 ▶ if (got) SvREFCNT_inc(row);
Branch: 1 branch
BranchTrueFalseTotal
if (got) SvREFCNT_inc(row); 50025 2 50027
379 50027 PUTBACK;
380 50027 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 50027 0 50027
381  
382 50027 ▶ if (!got) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!got) break; 2 50025 50027
383 50025 av_push(batch, row);
384 50025 ▶ if (av_len(batch) + 1 >= batch_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(batch) + 1 >= batch_size) { 52 49973 50025
385 52 encode_and_emit(aTHX_ enc, batch, writer);
386 52 av_clear(batch);
387 }
388 }
389  
390 2 ▶ if (av_len(batch) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(batch) >= 0) 1 1 2
391 1 encode_and_emit(aTHX_ enc, batch, writer);
392 }
393  
394 SV*
395 streamer(self, writer, ...)
396 SV *self
397 SV *writer
398 CODE:
399 {
400 /* Constructor for ClickHouse::Encoder::Streamer. */
401 Streamer *s;
402 19 int batch_size = 10000;
403 19 const char *compress_mode = NULL;
404 19 STRLEN compress_mode_len = 0;
405 19 SV *hasher_sv = NULL;
406  
407 19 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 19 19
408 19 ▶ if (!SvROK(writer) || SvTYPE(SvRV(writer)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(writer) || SvTYPE(SvRV(writer)) != SVt_PVCV) 19 0 19
if (!SvROK(writer) || SvTYPE(SvRV(writer)) != SVt_PVCV) 0 19 19
409 0 croak("streamer: writer must be a coderef");
410  
411 19 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 19 0 19
412 int i;
413 42 ▶ for (i = 2; i < items - 1; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items - 1; i += 2) { 23 19 42
414 STRLEN klen;
415 23 const char *key = SvPV(ST(i), klen);
416 23 ▶ if (klen == 10 && memcmp(key, "batch_size", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 10 && memcmp(key, "batch_size", 10) == 0) { 19 4 23
if (klen == 10 && memcmp(key, "batch_size", 10) == 0) { 19 0 19
417 19 batch_size = (int)SvIV(ST(i+1));
418 }
419 4 ▶ else if (klen == 8 && memcmp(key, "compress", 8) == 0
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 8 && memcmp(key, "compress", 8) == 0 3 1 4
else if (klen == 8 && memcmp(key, "compress", 8) == 0 3 0 3
420 3 ▶ && SvOK(ST(i+1))) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvOK(ST(i+1))) { 3 0 3
421 6 compress_mode = SvPV(ST(i+1), compress_mode_len);
422 }
423 1 ▶ else if (klen == 6 && memcmp(key, "hasher", 6) == 0
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 6 && memcmp(key, "hasher", 6) == 0 1 0 1
else if (klen == 6 && memcmp(key, "hasher", 6) == 0 1 0 1
424 1 ▶ && SvROK(ST(i+1))
Branch: 1 branch
BranchTrueFalseTotal
&& SvROK(ST(i+1)) 1 0 1
425 1 ▶ && SvTYPE(SvRV(ST(i+1))) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(ST(i+1))) == SVt_PVCV) { 1 0 1
426 1 hasher_sv = ST(i+1);
427 }
428 }
429 19 ▶ if (batch_size < 1) batch_size = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_size < 1) batch_size = 1; 0 19 19
430 }
431  
432 /* Construct under our own ENTER/LEAVE so a croak partway through
433 * (e.g. OOM in newAV) frees the partially-built struct. */
434 19 ENTER;
435 19 Newxz(s, 1, Streamer);
436 19 SAVEDESTRUCTOR_X(cleanup_streamer_slot, &s);
437  
438 /* Hold an owned RV pointing at the same blessed inner SV, so the
439 * encoder survives even if the user's $enc goes out of scope. */
440 19 s->enc_sv = newRV_inc(SvRV(self));
441 19 s->enc = INT2PTR(Encoder*, SvIV(SvRV(s->enc_sv)));
442 19 s->writer = newSVsv(writer);
443 19 s->buffer = newAV();
444 19 s->batch_size = batch_size;
445  
446 /* Copy the compress mode into Streamer-owned memory. NULL / "none" /
447 * "raw" are all treated as "no compression" by streamer_flush. */
448 19 ▶ if (compress_mode
Branch: 1 branch
BranchTrueFalseTotal
if (compress_mode 3 16 19
449 3 ▶ && !(compress_mode_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& !(compress_mode_len == 0) 3 0 3
450 3 ▶ && !(compress_mode_len == 4 && memcmp(compress_mode, "none", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
&& !(compress_mode_len == 4 && memcmp(compress_mode, "none", 4) == 0) 1 2 3
&& !(compress_mode_len == 4 && memcmp(compress_mode, "none", 4) == 0) 0 1 1
451 2 ▶ && !(compress_mode_len == 3 && memcmp(compress_mode, "raw", 3) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
&& !(compress_mode_len == 3 && memcmp(compress_mode, "raw", 3) == 0)) { 2 0 2
&& !(compress_mode_len == 3 && memcmp(compress_mode, "raw", 3) == 0)) { 2 0 2
452 2 Newx(s->compress_mode, compress_mode_len + 1, char);
453 2 memcpy(s->compress_mode, compress_mode, compress_mode_len);
454 2 s->compress_mode[compress_mode_len] = '\0';
455 }
456 19 ▶ if (hasher_sv) s->hasher_sv = newSVsv(hasher_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (hasher_sv) s->hasher_sv = newSVsv(hasher_sv); 1 18 19
457  
458 19 RETVAL = newSV(0);
459 19 sv_setref_pv(RETVAL, "ClickHouse::Encoder::Streamer", (void *)s);
460 19 s = NULL; /* disarm: SV's DESTROY now owns the streamer */
461 19 LEAVE;
462 }
463 OUTPUT:
464 RETVAL
465  
466 SV*
467 _columns(self)
468 SV *self
469 CODE:
470 {
471 Encoder *enc;
472 AV *cols;
473 int i;
474  
475 66 ▶ if (!sv_isobject(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) 0 66 66
476 0 croak("Not an object");
477  
478 66 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
479 66 cols = newAV();
480  
481 177 ▶ for (i = 0; i < enc->num_columns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < enc->num_columns; i++) { 111 66 177
482 111 AV *col = newAV();
483 111 av_push(col, newSVpvn(enc->columns[i].name, enc->columns[i].name_len));
484 111 av_push(col, newSVpvn(enc->columns[i].type_str, enc->columns[i].type_len));
485 111 av_push(cols, newRV_noinc((SV*)col));
486 }
487  
488 66 RETVAL = newRV_noinc((SV*)cols);
489 }
490 OUTPUT:
491 RETVAL
492  
493 void
494 _options(self)
495 SV *self
496 PPCODE:
497 {
498 Encoder *enc;
499 26 ▶ if (!sv_isobject(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) 0 26 26
500 0 croak("Not an object");
501 26 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
502 26 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 26 26
503 26 mPUSHs(newSVpvs("timezone"));
504 26 mPUSHs(newSVpv(enc->timezone, 0));
505 26 mPUSHs(newSVpvs("empty_as_null"));
506 26 mPUSHs(newSViv(enc->empty_as_null));
507 }
508  
509 void
510 DESTROY(self)
511 SV *self
512 CODE:
513 {
514 Encoder *enc;
515 1776 ▶ if (!sv_isobject(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) return; 0 1776 1776
516 1776 enc = INT2PTR(Encoder*, SvIV(SvRV(self)));
517 1776 free_encoder(aTHX_ enc);
518 }
519  
520 SV *
521 decode_block(class, bytes, ...)
522 SV *class
523 SV *bytes
524 CODE:
525 {
526 PERL_UNUSED_VAR(class);
527 /* Optional 3rd arg: byte offset into the input. Lets callers walk
528 * a concatenated multi-block stream in O(N) instead of O(N*K) - the
529 * Perl-side decode_blocks wrapper relies on this to avoid substr
530 * copies on each iteration.
531 *
532 * Optional 4th arg: hashref of column names to KEEP. When provided,
533 * columns whose name isn't in the set get a placeholder values
534 * arrayref (one undef per row) and the wire bytes are still consumed
535 * for that column's data, but no SVs are allocated for the values.
536 * Memory win on wide select * when caller wants a few columns. */
537 25661 UV start_offset = 0;
538 25661 HV *keep_set = NULL;
539 25661 ▶ if (items >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 3) { 370 25291 25661
540 370 IV signed_off = SvIV(ST(2));
541 370 ▶ if (signed_off < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (signed_off < 0) 1 369 370
542 1 croak("decode_block: offset must be non-negative (got %"
543 IVdf ")", signed_off);
544 369 start_offset = (UV)signed_off;
545 }
546 25660 ▶ if (items >= 4 && SvOK(ST(3))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 4 && SvOK(ST(3))) { 354 25306 25660
if (items >= 4 && SvOK(ST(3))) { 19 335 354
547 19 ▶ if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVHV) 18 1 19
if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVHV) 1 17 18
548 2 ▶ croak("decode_block: columns filter must be a hashref "
Branch: 1 branch
BranchTrueFalseTotal
croak("decode_block: columns filter must be a hashref " 1 1 2
549 "(got %s)",
550 SvROK(ST(3)) ? sv_reftype(SvRV(ST(3)), 0)
551 : "non-reference");
552 17 keep_set = (HV *)SvRV(ST(3));
553 }
554 const unsigned char *start, *p, *end;
555 UV ncols, nrows;
556 25658 decode_block_prologue(aTHX_ bytes, start_offset, "decode_block",
557 &start, &p, &end, &ncols, &nrows);
558  
559 /* Mortalize cols so a mid-loop croak (from decode_column,
560 * dec_lenpfx_string, etc.) reclaims it instead of leaking. We
561 * SvREFCNT_inc when transferring ownership to the result HV. */
562 25651 AV *cols = (AV *)sv_2mortal((SV *)newAV());
563 25651 ▶ if (ncols > 0) av_extend(cols, ncols - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (ncols > 0) av_extend(cols, ncols - 1); 25650 1 25651
564 UV c;
565 27112 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 26756 356 27112
566 const char *name; STRLEN name_len;
567 const char *tstr; STRLEN tlen;
568 26756 dec_lenpfx_string(aTHX_ &p, end, &name, &name_len);
569 26756 dec_lenpfx_string(aTHX_ &p, end, &tstr, &tlen);
570 /* Own scope per column: parse_type disarms its own cleanup on
571 * return, so the type needs re-guarding here or a croak from the
572 * decode below leaks the whole tree. LEAVE frees it at the end of
573 * the body, keeping the eager free for wide blocks. */
574 26747 ENTER;
575 26747 TypeInfo *t = parse_type(aTHX_ tstr, tlen);
576 26746 SAVEDESTRUCTOR_X(cleanup_typeinfo_ptr, &t);
577  
578 26746 int keep = 1;
579 26746 ▶ if (keep_set && !fetch_named_value(aTHX_ keep_set, name, name_len))
Branch: 2 branches
BranchTrueFalseTotal
if (keep_set && !fetch_named_value(aTHX_ keep_set, name, name_len)) 547 26199 26746
if (keep_set && !fetch_named_value(aTHX_ keep_set, name, name_len)) 518 29 547
580 518 keep = 0;
581  
582 SV *values;
583 26746 ▶ if (keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (keep) { 26228 518 26746
584 26228 values = decode_column(aTHX_ &p, end, t, (SSize_t)nrows);
585 } else {
586 /* Decode then discard - we still must consume the wire
587 * bytes to keep the cursor aligned for the next column.
588 * The AV is freed immediately so peak memory is one
589 * column's values, not the full block's. */
590 518 SV *tmp = decode_column(aTHX_ &p, end, t, (SSize_t)nrows);
591 515 SvREFCNT_dec(tmp);
592 515 AV *placeholder = newAV();
593 515 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 515 0 515
594 515 av_extend(placeholder, nrows - 1);
595 SSize_t r;
596 50060 ▶ for (r = 0; r < (SSize_t)nrows; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < (SSize_t)nrows; r++) 49545 515 50060
597 49545 av_store(placeholder, r, newSV(0));
598 }
599 515 values = newRV_noinc((SV *)placeholder);
600 }
601  
602 1461 HV *col_hv = newHV();
603 1461 (void)hv_stores(col_hv, "name", newSVpvn(name, name_len));
604 1461 (void)hv_stores(col_hv, "type", newSVpvn(tstr, tlen));
605 1461 (void)hv_stores(col_hv, "values", values);
606 1461 ▶ if (!keep) (void)hv_stores(col_hv, "skipped", newSViv(1));
Branch: 1 branch
BranchTrueFalseTotal
if (!keep) (void)hv_stores(col_hv, "skipped", newSViv(1)); 515 946 1461
607 1461 av_store(cols, c, newRV_noinc((SV *)col_hv));
608 1461 LEAVE; /* frees this column's TypeInfo */
609 }
610  
611 356 HV *result = newHV();
612 356 (void)hv_stores(result, "ncols", newSVuv(ncols));
613 356 (void)hv_stores(result, "nrows", newSVuv(nrows));
614 /* Transfer ownership out of the mortal: bump refcount, then the
615 * mortal-stack cleanup at scope exit drops one back, leaving the
616 * net refcount at 1 (owned by `result`). */
617 356 (void)hv_stores(result, "columns",
618 newRV_inc((SV *)cols));
619 356 (void)hv_stores(result, "consumed", newSVuv((UV)(p - start)));
620 356 RETVAL = newRV_noinc((SV *)result);
621 }
622 OUTPUT: RETVAL
623  
624 # Row-oriented decoder: same wire walk as decode_block, but values are
625 # distributed into row-major arrayrefs as each column is decoded, then
626 # the per-column AV is freed. Peak memory holds one column's values
627 # plus all row AVs (vs decode_rows-via-Perl which holds both
628 # representations + does the transpose in Perl).
629 SV *
630 decode_block_rows(class, bytes, ...)
631 SV *class
632 SV *bytes
633 CODE:
634 {
635 PERL_UNUSED_VAR(class);
636 46 UV start_offset = 0;
637 46 ▶ if (items >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 3) { 45 1 46
638 45 IV signed_off = SvIV(ST(2));
639 45 ▶ if (signed_off < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (signed_off < 0) 0 45 45
640 0 croak("decode_block_rows: offset must be non-negative "
641 "(got %" IVdf ")", signed_off);
642 45 start_offset = (UV)signed_off;
643 }
644 const unsigned char *start, *p, *end;
645 UV ncols, nrows;
646 46 decode_block_prologue(aTHX_ bytes, start_offset, "decode_block_rows",
647 &start, &p, &end, &ncols, &nrows);
648  
649 /* Mortalize so a mid-loop croak unwinds and reclaims these
650 * (and, via the row RVs they hold, all populated row AVs).
651 * SvREFCNT_inc-via-newRV_inc transfers them to the result HV
652 * on the success path. */
653 45 AV *names = (AV *)sv_2mortal((SV *)newAV());
654 45 AV *types = (AV *)sv_2mortal((SV *)newAV());
655 45 AV *rows = (AV *)sv_2mortal((SV *)newAV());
656 45 ▶ if (ncols > 0) { av_extend(names, ncols - 1); av_extend(types, ncols - 1); }
Branch: 1 branch
BranchTrueFalseTotal
if (ncols > 0) { av_extend(names, ncols - 1); av_extend(types, ncols - 1); } 45 0 45
657 /* `rows` is deliberately NOT pre-extended from nrows: that count is
658 * unvalidated wire data, and sizing an AV from it hands a crafted
659 * header a per-byte allocation multiplier before any column has been
660 * read. The fill loop below extends it once column 0 has proved the
661 * row count real. */
662  
663 /* Row AVs are built after the first column decodes, not up front:
664 * nrows is wire data bounded only by the byte length, so one AV per
665 * row would allocate ~80 bytes per input byte before any column data
666 * was validated. Only the cheap pointer array is allocated here. */
667 45 AV **row_avs = NULL;
668 45 ▶ if (nrows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) { 45 0 45
669 45 ▶ Newxz(row_avs, nrows, AV *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(row_avs, nrows, AV *); 0 45 45
670 45 SAVEFREEPV(row_avs);
671 }
672  
673 UV c;
674 107 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 62 45 107
675 const char *name; STRLEN name_len;
676 const char *tstr; STRLEN tlen;
677 62 dec_lenpfx_string(aTHX_ &p, end, &name, &name_len);
678 62 dec_lenpfx_string(aTHX_ &p, end, &tstr, &tlen);
679 /* Per-column scope, as in decode_block. */
680 62 ENTER;
681 62 TypeInfo *t = parse_type(aTHX_ tstr, tlen);
682 62 SAVEDESTRUCTOR_X(cleanup_typeinfo_ptr, &t);
683 62 SV *col_rv = decode_column(aTHX_ &p, end, t, (SSize_t)nrows);
684  
685 62 AV *col_av = (AV *)SvRV(col_rv);
686 UV r;
687 62 ▶ if (c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) { 45 17 62
688 45 ▶ if (nrows > 0) av_extend(rows, nrows - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 0) av_extend(rows, nrows - 1); 45 0 45
689 118 ▶ for (r = 0; r < nrows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < nrows; r++) { 73 45 118
690 73 AV *row_av = newAV();
691 73 av_extend(row_av, ncols - 1);
692 73 row_avs[r] = row_av;
693 73 av_store(rows, r, newRV_noinc((SV *)row_av));
694 }
695 }
696 170 ▶ for (r = 0; r < nrows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < nrows; r++) { 108 62 170
697 108 SV **e = av_fetch(col_av, r, 0);
698 108 ▶ av_store(row_avs[r], c, e ? SvREFCNT_inc(*e) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_store(row_avs[r], c, e ? SvREFCNT_inc(*e) : newSV(0)); 108 0 108
699 }
700 /* Free the column AV eagerly; we no longer need it. */
701 62 SvREFCNT_dec(col_rv);
702  
703 62 av_store(names, c, newSVpvn(name, name_len));
704 62 av_store(types, c, newSVpvn(tstr, tlen));
705 62 LEAVE; /* frees this column's TypeInfo */
706 }
707  
708 45 HV *result = newHV();
709 45 (void)hv_stores(result, "ncols", newSVuv(ncols));
710 45 (void)hv_stores(result, "nrows", newSVuv(nrows));
711 /* Transfer ownership out of the mortal stack: newRV_inc bumps
712 * the AV refcount so when the mortal cleanup drops one, the net
713 * count stays at 1 (owned by `result`). */
714 45 (void)hv_stores(result, "names", newRV_inc((SV *)names));
715 45 (void)hv_stores(result, "types", newRV_inc((SV *)types));
716 45 (void)hv_stores(result, "rows", newRV_inc((SV *)rows));
717 45 (void)hv_stores(result, "consumed", newSVuv((UV)(p - start)));
718 45 RETVAL = newRV_noinc((SV *)result);
719 }
720 OUTPUT: RETVAL
721  
722 # CityHash128 in the "cityhash102" variant used by ClickHouse's
723 # compressed-block prefix. Returns a 16-byte string (8 bytes low,
724 # then 8 bytes high; matches the wire layout CH expects). Exposed
725 # so compress_native_block can default its `hasher` to this XSUB
726 # instead of forcing every caller to supply one.
727 SV *
728 _cityhash128(class_or_bytes, ...)
729 SV *class_or_bytes
730 CODE:
731 {
732 SV *input_sv;
733 /* Accept both class-method ($class->_cityhash128($bytes)) and
734 * function-style (\&_cityhash128 passed as a hasher coderef)
735 * call shapes. A class-method call has items >= 2 and the first
736 * arg is a non-reference string (the class name); a coderef call
737 * has items == 1 and the first arg IS the bytes. */
738 62 ▶ if (items >= 2 && SvPOK(class_or_bytes) && !SvROK(class_or_bytes)) {
Branch: 3 branches
BranchTrueFalseTotal
if (items >= 2 && SvPOK(class_or_bytes) && !SvROK(class_or_bytes)) { 0 62 62
if (items >= 2 && SvPOK(class_or_bytes) && !SvROK(class_or_bytes)) { 0 0 0
if (items >= 2 && SvPOK(class_or_bytes) && !SvROK(class_or_bytes)) { 0 0 0
739 0 input_sv = ST(1);
740 } else {
741 62 input_sv = class_or_bytes;
742 }
743 STRLEN len;
744 62 const char *s = SvPVbyte(input_sv, len);
745 unsigned char out[16];
746 62 cityhash128_v102(s, (size_t)len, out);
747 62 RETVAL = newSVpvn((const char *)out, 16);
748 }
749 OUTPUT: RETVAL
750  
751 MODULE = ClickHouse::Encoder PACKAGE = ClickHouse::Encoder::Streamer
752  
753 void
754 push_row(self, row)
755 SV *self
756 SV *row
757 CODE:
758 {
759 Streamer *s;
760 98 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 98 98
761 98 s = INT2PTR(Streamer *, SvIV(SvRV(self)));
762 98 av_push(s->buffer, SvREFCNT_inc(row));
763 98 ▶ if (av_len(s->buffer) + 1 >= s->batch_size)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(s->buffer) + 1 >= s->batch_size) 16 82 98
764 16 streamer_flush(aTHX_ s);
765 }
766  
767 void
768 finish(self)
769 SV *self
770 CODE:
771 {
772 Streamer *s;
773 18 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 18 18
774 18 s = INT2PTR(Streamer *, SvIV(SvRV(self)));
775 18 streamer_flush(aTHX_ s);
776 }
777  
778 void
779 reset(self)
780 SV *self
781 CODE:
782 {
783 /* Discard buffered rows without flushing -- useful for error recovery
784 * after an upstream failure when the in-flight batch should be dropped. */
785 Streamer *s;
786 3 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 3 3
787 3 s = INT2PTR(Streamer *, SvIV(SvRV(self)));
788 3 ▶ if (av_len(s->buffer) >= 0) av_clear(s->buffer);
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(s->buffer) >= 0) av_clear(s->buffer); 3 0 3
789 }
790  
791 UV
792 buffered_count(self)
793 SV *self
794 CODE:
795 {
796 Streamer *s;
797 10 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 10 10
798 10 s = INT2PTR(Streamer *, SvIV(SvRV(self)));
799 10 ▶ RETVAL = (UV)(av_len(s->buffer) + 1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)(av_len(s->buffer) + 1); 0 10 10
800 }
801 OUTPUT:
802 RETVAL
803  
804 bool
805 is_empty(self)
806 SV *self
807 CODE:
808 {
809 Streamer *s;
810 6 ▶ if (!sv_isobject(self)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) croak("Not an object"); 0 6 6
811 6 s = INT2PTR(Streamer *, SvIV(SvRV(self)));
812 6 ▶ RETVAL = (av_len(s->buffer) < 0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (av_len(s->buffer) < 0); 4 2 6
813 }
814 OUTPUT:
815 RETVAL
816  
817 void
818 DESTROY(self)
819 SV *self
820 CODE:
821 {
822 Streamer *s;
823 19 ▶ if (!sv_isobject(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) return; 0 19 19
824 19 s = INT2PTR(Streamer *, SvIV(SvRV(self)));
825 19 free_streamer(aTHX_ s);
826 }
827  
828 MODULE = ClickHouse::Encoder PACKAGE = ClickHouse::Encoder::TCP
829  
830 # Pack an unsigned varint (LEB128 / CH varuint form). Used by the
831 # protocol-packet builders in ClickHouse::Encoder::TCP. Equivalent
832 # to the buf_varint() internal helper but exposed as a Perl callable
833 # so the pure-Perl module can produce wire-format bytes via XS rather
834 # than a per-byte chr/shift loop.
835 SV *
836 pack_varint(v)
837 UV v
838 CODE:
839 {
840 char buf[10];
841 143 int i = 0;
842 179 ▶ while (v >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
while (v >= 0x80) { 36 143 179
843 36 buf[i++] = (char)((v & 0x7f) | 0x80);
844 36 v >>= 7;
845 }
846 143 buf[i++] = (char)v;
847 143 RETVAL = newSVpvn(buf, i);
848 }
849 OUTPUT:
850 RETVAL
851  
852 # Unpack one varint from a byte string starting at the given offset.
853 # Returns (value, new_offset). Croaks on truncated input or a varint
854 # wider than 64 bits.
855 void
856 unpack_varint(bytes, offset)
857 SV *bytes
858 UV offset
859 PPCODE:
860 {
861 STRLEN len;
862 140 const unsigned char *p = (const unsigned char *)SvPVbyte(bytes, len);
863 140 UV v = tcp_read_varint(aTHX_ p, (UV)len, &offset);
864 127 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 127 127
865 127 PUSHs(sv_2mortal(newSVuv(v)));
866 127 PUSHs(sv_2mortal(newSVuv(offset)));
867 }
868  
869 # Pack a length-prefixed string (varint length + UTF-8 bytes). The
870 # caller should pass Perl strings; we encode to UTF-8 explicitly so
871 # non-ASCII chars get the right byte length on the wire.
872 SV *
873 pack_string(s)
874 SV *s
875 CODE:
876 {
877 STRLEN len;
878 const char *p;
879 293 ▶ if (!SvOK(s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(s)) { 0 293 293
880 0 p = ""; len = 0;
881 } else {
882 293 p = SvPV(s, len);
883 }
884 /* Build prefix + bytes. */
885 char prefix[10];
886 293 int pi = 0;
887 293 UV vv = (UV)len;
888 293 ▶ while (vv >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
while (vv >= 0x80) { 0 293 293
889 0 prefix[pi++] = (char)((vv & 0x7f) | 0x80);
890 0 vv >>= 7;
891 }
892 293 prefix[pi++] = (char)vv;
893 293 RETVAL = newSVpvn(prefix, pi);
894 293 sv_catpvn(RETVAL, p, len);
895 }
896 OUTPUT:
897 RETVAL
898  
899 # Unpack a length-prefixed string at the given offset. Returns (string,
900 # new_offset). String bytes are returned without decoding (caller
901 # decides UTF-8 vs binary).
902 void
903 unpack_string(bytes, offset)
904 SV *bytes
905 UV offset
906 PPCODE:
907 {
908 STRLEN buf_len;
909 34 const unsigned char *p = (const unsigned char *)SvPVbyte(bytes, buf_len);
910 34 UV slen = tcp_read_varint(aTHX_ p, (UV)buf_len, &offset);
911 /* Subtraction form: addition (offset + slen) can wrap UV_MAX when
912 * slen is attacker-controlled via a crafted varint. tcp_read_varint
913 * guarantees offset <= buf_len on return, so buf_len - offset is
914 * safe. */
915 33 ▶ if (slen > (UV)buf_len - offset)
Branch: 1 branch
BranchTrueFalseTotal
if (slen > (UV)buf_len - offset) 3 30 33
916 3 croak("string: truncated at offset %lu (need %lu)",
917 (unsigned long)offset, (unsigned long)slen);
918 30 SV *out = newSVpvn((const char *)(p + offset), (STRLEN)slen);
919 30 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 30 30
920 30 PUSHs(sv_2mortal(out));
921 30 PUSHs(sv_2mortal(newSVuv(offset + slen)));
922 }
923  
« json_kind.c ↑ Summary decode.c »