| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #include "types.h" |
| 7 |
|
| #include "buffer.h" |
| 8 |
|
| #include "encode.h" |
| 9 |
|
| #include "runtime.h" |
| 10 |
|
| #include "datetime.h" |
| 11 |
|
| |
| 12 |
9112 |
| static void configure_type(pTHX_ Encoder *enc, TypeInfo *t, |
| 13 |
|
| CHTimezone *default_tz, int empty_as_null) { |
| 14 |
|
| int i; |
| 15 |
9112 |
▶ | if (t->code == T_DATETIME || t->code == T_DATETIME64) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->code == T_DATETIME || t->code == T_DATETIME64) { |
8937 |
175 |
9112 |
| if (t->code == T_DATETIME || t->code == T_DATETIME64) { |
32 |
8905 |
8937 |
|
| 16 |
207 |
▶ | if (t->timezone) t->zones = &enc->zones; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->timezone) t->zones = &enc->zones; |
56 |
151 |
207 |
|
| 17 |
151 |
| else t->tz = default_tz; |
| 18 |
|
| } |
| 19 |
9112 |
▶ | if (t->code == T_NULLABLE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->code == T_NULLABLE) |
1108 |
8004 |
9112 |
|
| 20 |
1133 |
▶ | t->empty_as_null = empty_as_null && t->inner->code != T_STRING |
Branch: 1 branch
| Branch | True | False | Total |
| t->empty_as_null = empty_as_null && t->inner->code != T_STRING |
20 |
5 |
25 |
|
| 21 |
1133 |
▶ | && t->inner->code != T_FIXEDSTRING; |
Branch: 2 branches
| Branch | True | False | Total |
| && t->inner->code != T_FIXEDSTRING; |
25 |
1083 |
1108 |
| && t->inner->code != T_FIXEDSTRING; |
18 |
2 |
20 |
|
| 22 |
9112 |
▶ | if (t->inner) configure_type(aTHX_ enc, t->inner, default_tz, empty_as_null); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->inner) configure_type(aTHX_ enc, t->inner, default_tz, empty_as_null); |
2328 |
6784 |
9112 |
|
| 23 |
11279 |
▶ | for (i = 0; i < t->tuple_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) |
2167 |
9112 |
11279 |
|
| 24 |
2167 |
| configure_type(aTHX_ enc, t->tuple[i], default_tz, empty_as_null); |
| 25 |
9112 |
| } |
| 26 |
|
| |
| 27 |
1780 |
| void configure_encoder(pTHX_ Encoder *enc, const char *timezone, int empty_as_null) { |
| 28 |
1780 |
| CHTimezone *default_tz = timezone_load(aTHX_ &enc->zones, timezone); |
| 29 |
|
| int i; |
| 30 |
1776 |
| enc->timezone = savepv(timezone); |
| 31 |
1776 |
| enc->empty_as_null = empty_as_null; |
| 32 |
6393 |
▶ | for (i = 0; i < enc->num_columns; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < enc->num_columns; i++) |
4617 |
1776 |
6393 |
|
| 33 |
4617 |
| configure_type(aTHX_ enc, enc->columns[i].type, default_tz, empty_as_null); |
| 34 |
1776 |
| } |
| 35 |
|
| |
| 36 |
|
| #define CHE_GATHER_COLUMNS 16 |
| 37 |
|
| |
| 38 |
|
| typedef struct { |
| 39 |
|
| Buffer data, nulls; |
| 40 |
|
| TypeInfo *scalar; |
| 41 |
|
| int nullable, complex_slot; |
| 42 |
|
| } RowColumn; |
| 43 |
|
| |
| 44 |
750927 |
| static SV *row_value(pTHX_ AV *row, SSize_t pos, AV *owned) { |
| 45 |
750927 |
| int magical = SvMAGICAL(row); |
| 46 |
281 |
| SV **slot = magical ? av_fetch(row, pos, 0) |
| 47 |
750927 |
▶ | : pos <= AvFILLp(row) ? AvARRAY(row) + pos : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| : pos <= AvFILLp(row) ? AvARRAY(row) + pos : NULL; |
281 |
750646 |
750927 |
| : pos <= AvFILLp(row) ? AvARRAY(row) + pos : NULL; |
750646 |
0 |
750646 |
|
| 48 |
750927 |
▶ | SV *value = slot && *slot ? *slot : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *value = slot && *slot ? *slot : &PL_sv_undef; |
750927 |
0 |
750927 |
| SV *value = slot && *slot ? *slot : &PL_sv_undef; |
750926 |
1 |
750927 |
|
| 49 |
750927 |
▶ | if (magical || SvGMAGICAL(value)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (magical || SvGMAGICAL(value)) { |
750646 |
281 |
750927 |
| if (magical || SvGMAGICAL(value)) { |
99 |
750547 |
750646 |
|
| 50 |
380 |
| SvGETMAGIC(value); |
| 51 |
380 |
| value = newSVsv(value); |
| 52 |
380 |
▶ | if (owned) av_push(owned, value); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owned) av_push(owned, value); |
152 |
228 |
380 |
|
| 53 |
228 |
| else sv_2mortal(value); |
| 54 |
750547 |
▶ | } else if (owned) av_push(owned, SvREFCNT_inc_simple_NN(value)); |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (owned) av_push(owned, SvREFCNT_inc_simple_NN(value)); |
164764 |
585783 |
750547 |
|
| 55 |
750927 |
| return value; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
2642 |
| void do_encode(pTHX_ Encoder *enc, AV *rows_av, Buffer *buf, const SSize_t *positions) { |
| 59 |
2642 |
| SSize_t num_rows = av_len(rows_av) + 1; |
| 60 |
2642 |
| SSize_t r, c, required = enc->num_columns; |
| 61 |
2642 |
| int complex_count = 0, group, *complex_columns = NULL; |
| 62 |
|
| SV **gathered; |
| 63 |
2642 |
| RowColumn *states = NULL; |
| 64 |
|
| AV *owned; |
| 65 |
2642 |
▶ | if (positions) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (positions) { |
9 |
2633 |
2642 |
|
| 66 |
9 |
| required = 0; |
| 67 |
139 |
▶ | for (c = 0; c < enc->num_columns; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) |
130 |
9 |
139 |
|
| 68 |
130 |
▶ | if (positions[c] >= required) required = positions[c] + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (positions[c] >= required) required = positions[c] + 1; |
15 |
115 |
130 |
|
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| /* Validate every row up front and cache the dereffed AV*. Caching |
| 72 |
|
| * avoids a second av_fetch per row in the column loop and removes |
| 73 |
|
| * any concern about tied / magic arrays returning different values |
| 74 |
|
| * across passes. */ |
| 75 |
2642 |
| AV **row_avs = NULL; |
| 76 |
2642 |
▶ | if (num_rows > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_rows > 0) { |
2612 |
30 |
2642 |
|
| 77 |
2612 |
▶ | Newx(row_avs, num_rows, AV*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(row_avs, num_rows, AV*); |
0 |
2612 |
2612 |
|
| 78 |
2612 |
| SAVEFREEPV(row_avs); |
| 79 |
|
| } |
| 80 |
483755 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
481120 |
2635 |
483755 |
|
| 81 |
481120 |
| SV **row_sv = av_fetch(rows_av, r, 0); |
| 82 |
|
| AV *row_av; |
| 83 |
|
| SSize_t row_cols; |
| 84 |
481120 |
▶ | if (!row_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!row_sv) |
0 |
481120 |
481120 |
|
| 85 |
0 |
| croak("Row %" IVdf " must be arrayref", (IV)r); |
| 86 |
|
| /* Materialize any magic on the fetched SV - for tied AVs the |
| 87 |
|
| * FETCH result is delivered through magic and SvROK / SvTYPE |
| 88 |
|
| * see the unrealized magical placeholder until SvGETMAGIC has |
| 89 |
|
| * been called. Common in DBI-driven ingest (fetchall_arrayref |
| 90 |
|
| * may return magical AVs at either level). */ |
| 91 |
481120 |
| SvGETMAGIC(*row_sv); |
| 92 |
481120 |
▶ | if (!SvROK(*row_sv) || SvTYPE(SvRV(*row_sv)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(*row_sv) || SvTYPE(SvRV(*row_sv)) != SVt_PVAV) |
481117 |
3 |
481120 |
| if (!SvROK(*row_sv) || SvTYPE(SvRV(*row_sv)) != SVt_PVAV) |
0 |
481117 |
481117 |
|
| 93 |
3 |
| croak("Row %" IVdf " must be arrayref", (IV)r); |
| 94 |
481117 |
| row_av = (AV*)SvRV(*row_sv); |
| 95 |
481117 |
| row_cols = av_len(row_av) + 1; |
| 96 |
481117 |
▶ | if (positions ? row_cols < required : row_cols != required) |
Branch: 2 branches
| Branch | True | False | Total |
| if (positions ? row_cols < required : row_cols != required) |
22 |
481095 |
481117 |
| if (positions ? row_cols < required : row_cols != required) |
4 |
481113 |
481117 |
|
| 97 |
4 |
▶ | croak("Row %" IVdf " has %" IVdf " columns, expected %" IVdf "%s", |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Row %" IVdf " has %" IVdf " columns, expected %" IVdf "%s", |
1 |
3 |
4 |
|
| 98 |
|
| (IV)r, (IV)row_cols, (IV)required, positions ? " or more" : ""); |
| 99 |
481113 |
| row_avs[r] = row_av; |
| 100 |
481113 |
▶ | if (SvMAGICAL(rows_av) || SvGMAGICAL(*row_sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvMAGICAL(rows_av) || SvGMAGICAL(*row_sv)) |
481102 |
11 |
481113 |
| if (SvMAGICAL(rows_av) || SvGMAGICAL(*row_sv)) |
0 |
481102 |
481102 |
|
| 101 |
11 |
| sv_2mortal(SvREFCNT_inc((SV *)row_av)); |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
2635 |
| buf_varint(aTHX_ buf, enc->num_columns); |
| 105 |
2635 |
| buf_varint(aTHX_ buf, num_rows); |
| 106 |
|
| |
| 107 |
2635 |
▶ | if (enc->num_columns) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->num_columns) { |
2634 |
1 |
2635 |
|
| 108 |
2634 |
| Newxz(states, enc->num_columns, RowColumn); |
| 109 |
2634 |
| SAVEFREEPV(states); |
| 110 |
2634 |
| Newx(complex_columns, enc->num_columns, int); |
| 111 |
2634 |
| SAVEFREEPV(complex_columns); |
| 112 |
|
| } |
| 113 |
9266 |
▶ | for (c = 0; c < enc->num_columns; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) { |
6631 |
2635 |
9266 |
|
| 114 |
6631 |
| TypeInfo *type = enc->columns[c].type; |
| 115 |
6631 |
| RowColumn *state = &states[c]; |
| 116 |
6631 |
| state->nullable = type->code == T_NULLABLE; |
| 117 |
6631 |
▶ | state->scalar = state->nullable ? type->inner : type; |
Branch: 1 branch
| Branch | True | False | Total |
| state->scalar = state->nullable ? type->inner : type; |
295 |
6336 |
6631 |
|
| 118 |
6631 |
▶ | if (!is_simple_type(state->scalar)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_simple_type(state->scalar)) { |
1818 |
4813 |
6631 |
|
| 119 |
1818 |
| state->scalar = NULL; |
| 120 |
1818 |
| state->complex_slot = complex_count; |
| 121 |
1818 |
| complex_columns[complex_count++] = c; |
| 122 |
4813 |
▶ | } else if (num_rows) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (num_rows) { |
4774 |
39 |
4813 |
|
| 123 |
|
| STRLEN width; |
| 124 |
4774 |
| buf_init(aTHX_ &state->data); |
| 125 |
4774 |
| encode_null_scalar(aTHX_ &state->data, state->scalar); |
| 126 |
4774 |
| width = state->data.len; |
| 127 |
4774 |
| state->data.len = 0; |
| 128 |
4774 |
▶ | if ((UV)num_rows > (UV)SSize_t_MAX / width) croak("Too many rows to encode"); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)num_rows > (UV)SSize_t_MAX / width) croak("Too many rows to encode"); |
0 |
4774 |
4774 |
|
| 129 |
4774 |
| buf_grow(aTHX_ &state->data, num_rows * width); |
| 130 |
4774 |
▶ | if (state->nullable) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->nullable) { |
278 |
4496 |
4774 |
|
| 131 |
278 |
| buf_init(aTHX_ &state->nulls); |
| 132 |
278 |
| buf_grow(aTHX_ &state->nulls, num_rows); |
| 133 |
|
| } |
| 134 |
|
| } |
| 135 |
|
| } |
| 136 |
2635 |
| group = complex_count < CHE_GATHER_COLUMNS ? complex_count : CHE_GATHER_COLUMNS; |
| 137 |
2635 |
▶ | if (num_rows > SSize_t_MAX / (group ? group : 1) / (SSize_t)sizeof(SV *)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (num_rows > SSize_t_MAX / (group ? group : 1) / (SSize_t)sizeof(SV *)) |
1651 |
984 |
2635 |
| if (num_rows > SSize_t_MAX / (group ? group : 1) / (SSize_t)sizeof(SV *)) |
0 |
2635 |
2635 |
|
| 138 |
0 |
| croak("Too many rows to gather"); |
| 139 |
2635 |
| gathered = alloc_sv_array(aTHX_ num_rows * group); |
| 140 |
2635 |
| ENTER; |
| 141 |
2635 |
| SAVETMPS; |
| 142 |
2635 |
| owned = (AV *)sv_2mortal((SV *)newAV()); |
| 143 |
483572 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
481111 |
2461 |
483572 |
|
| 144 |
481111 |
| AV *row = row_avs[r]; |
| 145 |
481111 |
| ENTER; |
| 146 |
481111 |
| SAVETMPS; |
| 147 |
1231864 |
▶ | for (c = 0; c < enc->num_columns; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) { |
750927 |
480937 |
1231864 |
|
| 148 |
750927 |
| RowColumn *state = &states[c]; |
| 149 |
750927 |
▶ | SSize_t pos = positions ? positions[c] : c; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t pos = positions ? positions[c] : c; |
314 |
750613 |
750927 |
|
| 150 |
|
| SV *value; |
| 151 |
750927 |
▶ | if (state->scalar) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->scalar) { |
586011 |
164916 |
750927 |
|
| 152 |
586011 |
| value = row_value(aTHX_ row, pos, NULL); |
| 153 |
586011 |
▶ | if (state->nullable) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->nullable) { |
41140 |
544871 |
586011 |
|
| 154 |
41140 |
| int is_null = nullable_is_null(value, enc->columns[c].type); |
| 155 |
41140 |
| buf_byte(aTHX_ &state->nulls, is_null); |
| 156 |
41140 |
▶ | if (is_null) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_null) { |
8264 |
32876 |
41140 |
|
| 157 |
8264 |
| encode_null_scalar(aTHX_ &state->data, state->scalar); |
| 158 |
8264 |
| continue; |
| 159 |
|
| } |
| 160 |
|
| } |
| 161 |
577747 |
| encode_scalar(aTHX_ &state->data, value, state->scalar); |
| 162 |
164916 |
▶ | } else if (state->complex_slot < group) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (state->complex_slot < group) { |
164700 |
216 |
164916 |
|
| 163 |
164700 |
| value = row_value(aTHX_ row, pos, owned); |
| 164 |
164700 |
| gathered[state->complex_slot * num_rows + r] = value; |
| 165 |
|
| } |
| 166 |
|
| } |
| 167 |
480937 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
32 |
480905 |
480937 |
|
| 168 |
480937 |
| LEAVE; |
| 169 |
|
| } |
| 170 |
8891 |
▶ | for (c = 0; c < enc->num_columns; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) { |
6453 |
2438 |
8891 |
|
| 171 |
6453 |
| Column *col = &enc->columns[c]; |
| 172 |
6453 |
| RowColumn *state = &states[c]; |
| 173 |
6453 |
| buf_string(aTHX_ buf, col->name, col->name_len); |
| 174 |
6453 |
| buf_string(aTHX_ buf, col->type_str, col->type_len); |
| 175 |
6453 |
▶ | if (state->scalar) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->scalar) { |
4635 |
1818 |
6453 |
|
| 176 |
4635 |
▶ | if (num_rows) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_rows) { |
4596 |
39 |
4635 |
|
| 177 |
4596 |
▶ | if (state->nullable) buf_append(aTHX_ buf, state->nulls.ptr, state->nulls.len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->nullable) buf_append(aTHX_ buf, state->nulls.ptr, state->nulls.len); |
277 |
4319 |
4596 |
|
| 178 |
4596 |
| buf_append(aTHX_ buf, state->data.ptr, state->data.len); |
| 179 |
|
| } |
| 180 |
|
| } else { |
| 181 |
1818 |
| int index = state->complex_slot; |
| 182 |
1818 |
▶ | if (index && index % CHE_GATHER_COLUMNS == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (index && index % CHE_GATHER_COLUMNS == 0) { |
167 |
1651 |
1818 |
| if (index && index % CHE_GATHER_COLUMNS == 0) { |
6 |
161 |
167 |
|
| 183 |
6 |
| int end = complex_count - index; |
| 184 |
|
| int j; |
| 185 |
6 |
▶ | if (end > CHE_GATHER_COLUMNS) end = CHE_GATHER_COLUMNS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (end > CHE_GATHER_COLUMNS) end = CHE_GATHER_COLUMNS; |
3 |
3 |
6 |
|
| 186 |
6 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
6 |
0 |
6 |
|
| 187 |
6 |
| LEAVE; |
| 188 |
6 |
| ENTER; |
| 189 |
6 |
| SAVETMPS; |
| 190 |
6 |
| owned = (AV *)sv_2mortal((SV *)newAV()); |
| 191 |
24 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
18 |
6 |
24 |
|
| 192 |
18 |
| AV *row = row_avs[r]; |
| 193 |
234 |
▶ | for (j = 0; j < end; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < end; j++) { |
216 |
18 |
234 |
|
| 194 |
216 |
| int column = complex_columns[index + j]; |
| 195 |
216 |
▶ | SSize_t pos = positions ? positions[column] : column; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t pos = positions ? positions[column] : column; |
72 |
144 |
216 |
|
| 196 |
216 |
| gathered[j * num_rows + r] = row_value(aTHX_ row, pos, owned); |
| 197 |
|
| } |
| 198 |
|
| } |
| 199 |
|
| } |
| 200 |
1818 |
| ENTER; |
| 201 |
1818 |
| SAVETMPS; |
| 202 |
1818 |
▶ | encode_column(aTHX_ buf, num_rows ? gathered + (index % CHE_GATHER_COLUMNS) * num_rows : NULL, |
Branch: 1 branch
| Branch | True | False | Total |
| encode_column(aTHX_ buf, num_rows ? gathered + (index % CHE_GATHER_COLUMNS) * num_rows : NULL, |
1788 |
30 |
1818 |
|
| 203 |
|
| num_rows, col->type); |
| 204 |
1795 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1754 |
41 |
1795 |
|
| 205 |
1795 |
| LEAVE; |
| 206 |
|
| } |
| 207 |
|
| } |
| 208 |
2438 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
2438 |
0 |
2438 |
|
| 209 |
2438 |
| LEAVE; |
| 210 |
2438 |
| } |
| 211 |
|
| |
| 212 |
|
| /* Route `bytes` through ClickHouse::Encoder->compress_native_block |
| 213 |
|
| * and return a freshly-allocated SV with the compressed-block-framed |
| 214 |
|
| * result. Caller takes ownership (refcount 1) and is responsible for |
| 215 |
|
| * mortalizing or freeing. mode == NULL / "none" / "raw" -> just bumps |
| 216 |
|
| * the input's refcount and returns it. */ |
| 217 |
4 |
| static SV *maybe_compress(pTHX_ SV *bytes, |
| 218 |
|
| const char *mode, SV *hasher_sv) { |
| 219 |
4 |
▶ | if (!mode || strcmp(mode, "none") == 0 || strcmp(mode, "raw") == 0) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!mode || strcmp(mode, "none") == 0 || strcmp(mode, "raw") == 0) |
4 |
0 |
4 |
| if (!mode || strcmp(mode, "none") == 0 || strcmp(mode, "raw") == 0) |
4 |
0 |
4 |
| if (!mode || strcmp(mode, "none") == 0 || strcmp(mode, "raw") == 0) |
0 |
4 |
4 |
|
| 220 |
0 |
| return SvREFCNT_inc(bytes); |
| 221 |
4 |
| dSP; |
| 222 |
4 |
| ENTER; |
| 223 |
4 |
| SAVETMPS; |
| 224 |
4 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
4 |
4 |
|
| 225 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpvs("ClickHouse::Encoder"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("ClickHouse::Encoder"))); |
0 |
4 |
4 |
|
| 226 |
4 |
▶ | XPUSHs(bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(bytes); |
0 |
4 |
4 |
|
| 227 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpvs("mode"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("mode"))); |
0 |
4 |
4 |
|
| 228 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpv(mode, 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(mode, 0))); |
0 |
4 |
4 |
|
| 229 |
4 |
▶ | if (hasher_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hasher_sv) { |
1 |
3 |
4 |
|
| 230 |
1 |
▶ | XPUSHs(sv_2mortal(newSVpvs("hasher"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("hasher"))); |
0 |
1 |
1 |
|
| 231 |
1 |
▶ | XPUSHs(hasher_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(hasher_sv); |
0 |
1 |
1 |
|
| 232 |
|
| } |
| 233 |
4 |
| PUTBACK; |
| 234 |
4 |
| int n = call_method("compress_native_block", G_SCALAR); |
| 235 |
4 |
| SPAGAIN; |
| 236 |
4 |
| SV *out = NULL; |
| 237 |
4 |
▶ | if (n == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 1) { |
4 |
0 |
4 |
|
| 238 |
|
| /* POPs gives a mortal SV; copy its body via newSVsv into a |
| 239 |
|
| * non-mortal SV that survives the FREETMPS below. */ |
| 240 |
4 |
| out = newSVsv(POPs); |
| 241 |
|
| } |
| 242 |
4 |
| PUTBACK; |
| 243 |
4 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
4 |
0 |
4 |
|
| 244 |
4 |
| LEAVE; |
| 245 |
4 |
▶ | if (!out) croak("streamer: compress_native_block returned nothing"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) croak("streamer: compress_native_block returned nothing"); |
0 |
4 |
4 |
|
| 246 |
4 |
| return out; |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
77 |
| void encode_and_emit(pTHX_ Encoder *enc, AV *batch, SV *writer) { |
| 250 |
77 |
| dSP; |
| 251 |
77 |
| ENTER; |
| 252 |
77 |
| SAVETMPS; |
| 253 |
|
| |
| 254 |
|
| Buffer buf; |
| 255 |
77 |
| buf_init(aTHX_ &buf); |
| 256 |
77 |
| do_encode(aTHX_ enc, batch, &buf, NULL); |
| 257 |
|
| |
| 258 |
77 |
| SV *bytes = sv_2mortal(newSVpvn(buf.ptr, buf.len)); |
| 259 |
|
| |
| 260 |
77 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
77 |
77 |
|
| 261 |
77 |
▶ | XPUSHs(bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(bytes); |
0 |
77 |
77 |
|
| 262 |
77 |
| PUTBACK; |
| 263 |
77 |
| call_sv(writer, G_VOID | G_DISCARD); |
| 264 |
|
| |
| 265 |
74 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
74 |
0 |
74 |
|
| 266 |
74 |
| LEAVE; |
| 267 |
74 |
| } |
| 268 |
|
| |
| 269 |
34 |
| void streamer_flush(pTHX_ Streamer *s) { |
| 270 |
34 |
▶ | if (av_len(s->buffer) < 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(s->buffer) < 0) return; |
6 |
28 |
34 |
|
| 271 |
|
| /* Swap in a fresh buffer BEFORE the (potentially croaking) emit so that |
| 272 |
|
| * if the writer dies and the user catches it via eval{}, the streamer |
| 273 |
|
| * is left in a clean state instead of replaying the failed batch's rows |
| 274 |
|
| * on the next push. The old buffer is mortalized so it's reclaimed |
| 275 |
|
| * either way. */ |
| 276 |
28 |
| AV *batch = s->buffer; |
| 277 |
28 |
| s->buffer = newAV(); |
| 278 |
28 |
| sv_2mortal((SV *)batch); |
| 279 |
|
| |
| 280 |
|
| /* If compression is enabled, build the bytes locally then route them |
| 281 |
|
| * through compress_native_block before invoking the user's writer. |
| 282 |
|
| * Otherwise fall through to encode_and_emit's direct call. */ |
| 283 |
28 |
▶ | if (s->compress_mode) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->compress_mode) { |
4 |
24 |
28 |
|
| 284 |
4 |
| dSP; |
| 285 |
4 |
| ENTER; |
| 286 |
4 |
| SAVETMPS; |
| 287 |
|
| Buffer buf; |
| 288 |
4 |
| buf_init(aTHX_ &buf); |
| 289 |
4 |
| do_encode(aTHX_ s->enc, batch, &buf, NULL); |
| 290 |
4 |
| SV *bytes = sv_2mortal(newSVpvn(buf.ptr, buf.len)); |
| 291 |
|
| /* maybe_compress returns an owned SV (refcount 1); mortalize it |
| 292 |
|
| * here so it's cleaned up after the writer call. */ |
| 293 |
4 |
| SV *out = sv_2mortal( |
| 294 |
|
| maybe_compress(aTHX_ bytes, s->compress_mode, s->hasher_sv)); |
| 295 |
4 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
4 |
4 |
|
| 296 |
4 |
▶ | XPUSHs(out); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(out); |
0 |
4 |
4 |
|
| 297 |
4 |
| PUTBACK; |
| 298 |
4 |
| call_sv(s->writer, G_VOID | G_DISCARD); |
| 299 |
4 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
4 |
0 |
4 |
|
| 300 |
4 |
| LEAVE; |
| 301 |
4 |
| return; |
| 302 |
|
| } |
| 303 |
24 |
| encode_and_emit(aTHX_ s->enc, batch, s->writer); |
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
19 |
| void free_streamer(pTHX_ Streamer *s) { |
| 307 |
19 |
▶ | if (!s) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return; |
0 |
19 |
19 |
|
| 308 |
19 |
▶ | if (s->enc_sv) SvREFCNT_dec(s->enc_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->enc_sv) SvREFCNT_dec(s->enc_sv); |
19 |
0 |
19 |
|
| 309 |
19 |
▶ | if (s->writer) SvREFCNT_dec(s->writer); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->writer) SvREFCNT_dec(s->writer); |
19 |
0 |
19 |
|
| 310 |
19 |
▶ | if (s->buffer) SvREFCNT_dec((SV *)s->buffer); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->buffer) SvREFCNT_dec((SV *)s->buffer); |
19 |
0 |
19 |
|
| 311 |
19 |
▶ | if (s->compress_mode) Safefree(s->compress_mode); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->compress_mode) Safefree(s->compress_mode); |
2 |
17 |
19 |
|
| 312 |
19 |
▶ | if (s->hasher_sv) SvREFCNT_dec(s->hasher_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->hasher_sv) SvREFCNT_dec(s->hasher_sv); |
1 |
18 |
19 |
|
| 313 |
19 |
| Safefree(s); |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
19 |
| void cleanup_streamer_slot(pTHX_ void *p) { |
| 317 |
19 |
| Streamer **slot = (Streamer **)p; |
| 318 |
19 |
▶ | if (*slot) free_streamer(aTHX_ *slot); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*slot) free_streamer(aTHX_ *slot); |
0 |
19 |
19 |
|
| 319 |
19 |
| } |
| 320 |
|
| |
| 321 |
1811 |
| void free_encoder(pTHX_ Encoder *enc) { |
| 322 |
|
| int i; |
| 323 |
1811 |
▶ | if (!enc) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!enc) return; |
0 |
1811 |
1811 |
|
| 324 |
1811 |
▶ | if (enc->columns) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->columns) { |
1811 |
0 |
1811 |
|
| 325 |
6460 |
▶ | for (i = 0; i < enc->num_columns; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < enc->num_columns; i++) { |
4649 |
1811 |
6460 |
|
| 326 |
4649 |
▶ | if (enc->columns[i].name) Safefree(enc->columns[i].name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->columns[i].name) Safefree(enc->columns[i].name); |
4647 |
2 |
4649 |
|
| 327 |
4649 |
▶ | if (enc->columns[i].type_str) Safefree(enc->columns[i].type_str); |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->columns[i].type_str) Safefree(enc->columns[i].type_str); |
4647 |
2 |
4649 |
|
| 328 |
4649 |
▶ | if (enc->columns[i].type) free_typeinfo(aTHX_ enc->columns[i].type); |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->columns[i].type) free_typeinfo(aTHX_ enc->columns[i].type); |
4618 |
31 |
4649 |
|
| 329 |
|
| } |
| 330 |
1811 |
| Safefree(enc->columns); |
| 331 |
|
| } |
| 332 |
1811 |
| timezone_free(aTHX_ enc->zones); |
| 333 |
1811 |
| Safefree(enc->timezone); |
| 334 |
1811 |
| Safefree(enc); |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
1811 |
| void cleanup_encoder_slot(pTHX_ void *p) { |
| 338 |
1811 |
| Encoder **slot = (Encoder **)p; |
| 339 |
1811 |
▶ | if (*slot) free_encoder(aTHX_ *slot); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*slot) free_encoder(aTHX_ *slot); |
35 |
1776 |
1811 |
|
| 340 |
1811 |
| } |