Encoder.xs

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