| 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 |
|
| #include "scalar_kind.h" |
| 12 |
|
| |
| 13 |
9588 |
| static void configure_type(pTHX_ Encoder *enc, TypeInfo *t, |
| 14 |
|
| CHTimezone *default_tz, int empty_as_null) { |
| 15 |
|
| int i; |
| 16 |
9588 |
▶ | 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) { |
9406 |
182 |
9588 |
| if (t->code == T_DATETIME || t->code == T_DATETIME64) { |
34 |
9372 |
9406 |
|
| 17 |
216 |
▶ | if (t->timezone) t->zones = &enc->zones; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->timezone) t->zones = &enc->zones; |
56 |
160 |
216 |
|
| 18 |
160 |
| else t->tz = default_tz; |
| 19 |
|
| } |
| 20 |
9588 |
▶ | if (t->code == T_NULLABLE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->code == T_NULLABLE) |
1125 |
8463 |
9588 |
|
| 21 |
1160 |
▶ | 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 |
28 |
7 |
35 |
|
| 22 |
1160 |
▶ | && t->inner->code != T_FIXEDSTRING; |
Branch: 2 branches
| Branch | True | False | Total |
| && t->inner->code != T_FIXEDSTRING; |
35 |
1090 |
1125 |
| && t->inner->code != T_FIXEDSTRING; |
26 |
2 |
28 |
|
| 23 |
9588 |
▶ | 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); |
2459 |
7129 |
9588 |
|
| 24 |
11823 |
▶ | for (i = 0; i < t->tuple_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) |
2235 |
9588 |
11823 |
|
| 25 |
2235 |
| configure_type(aTHX_ enc, t->tuple[i], default_tz, empty_as_null); |
| 26 |
9588 |
| } |
| 27 |
|
| |
| 28 |
1869 |
| void configure_encoder(pTHX_ Encoder *enc, const char *timezone, int empty_as_null) { |
| 29 |
1869 |
| CHTimezone *default_tz = timezone_load(aTHX_ &enc->zones, timezone); |
| 30 |
|
| int i; |
| 31 |
1865 |
| enc->timezone = savepv(timezone); |
| 32 |
1865 |
| enc->empty_as_null = empty_as_null; |
| 33 |
6759 |
▶ | for (i = 0; i < enc->num_columns; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < enc->num_columns; i++) |
4894 |
1865 |
6759 |
|
| 34 |
4894 |
| configure_type(aTHX_ enc, enc->columns[i].type, default_tz, empty_as_null); |
| 35 |
1865 |
| } |
| 36 |
|
| |
| 37 |
|
| #define CHE_GATHER_COLUMNS 16 |
| 38 |
|
| |
| 39 |
35 |
| static SV *type_default(pTHX_ TypeInfo *t) { |
| 40 |
|
| int i; |
| 41 |
|
| AV *av; |
| 42 |
35 |
| switch (t->code) { |
| 43 |
4 |
| case T_NULLABLE: case T_VARIANT: case T_DYNAMIC: |
| 44 |
|
| case T_ENUM8: case T_ENUM16: |
| 45 |
4 |
| return sv_2mortal(newSV(0)); |
| 46 |
2 |
| case T_LOWCARDINALITY: |
| 47 |
2 |
| return type_default(aTHX_ t->inner); |
| 48 |
3 |
| case T_ARRAY: case T_MAP: |
| 49 |
3 |
| return sv_2mortal(newRV_noinc((SV *)newAV())); |
| 50 |
2 |
| case T_TUPLE: |
| 51 |
2 |
| av = (AV *)sv_2mortal((SV *)newAV()); |
| 52 |
8 |
▶ | for (i = 0; i < t->tuple_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) |
6 |
2 |
8 |
|
| 53 |
6 |
| av_push(av, SvREFCNT_inc(type_default(aTHX_ t->tuple[i]))); |
| 54 |
2 |
| return sv_2mortal(newRV_inc((SV *)av)); |
| 55 |
2 |
| case T_JSON: |
| 56 |
2 |
| return sv_2mortal(newRV_noinc((SV *)newHV())); |
| 57 |
6 |
| case T_STRING: case T_FIXEDSTRING: |
| 58 |
6 |
| return sv_2mortal(newSVpvs("")); |
| 59 |
2 |
| case T_UUID: case T_IPV6: |
| 60 |
2 |
| return sv_2mortal(newSVpvn("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16)); |
| 61 |
14 |
| default: |
| 62 |
14 |
| return sv_2mortal(newSViv(0)); |
| 63 |
|
| } |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
1865 |
| void configure_defaults(pTHX_ Encoder *enc, int empty_as_default, HV *defaults, HV *expressions) { |
| 67 |
|
| int c; |
| 68 |
|
| HE *entry; |
| 69 |
1865 |
▶ | if (defaults) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (defaults) { |
37 |
1828 |
1865 |
|
| 70 |
37 |
| HV *snapshot = (HV *)sv_2mortal((SV *)newHV()); |
| 71 |
37 |
| hv_iterinit(defaults); |
| 72 |
120 |
▶ | while ((entry = hv_iternext(defaults))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((entry = hv_iternext(defaults))) { |
85 |
35 |
120 |
|
| 73 |
85 |
| SV *key = hv_iterkeysv(entry); |
| 74 |
|
| SV *value; |
| 75 |
|
| STRLEN len; |
| 76 |
85 |
| const char *name = SvPV(key, len); |
| 77 |
782 |
▶ | for (c = 0; c < enc->num_columns; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) |
780 |
2 |
782 |
|
| 78 |
780 |
▶ | if (enc->columns[c].name_len == len && !memcmp(enc->columns[c].name, name, len)) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (enc->columns[c].name_len == len && !memcmp(enc->columns[c].name, name, len)) break; |
527 |
253 |
780 |
| if (enc->columns[c].name_len == len && !memcmp(enc->columns[c].name, name, len)) break; |
83 |
444 |
527 |
|
| 79 |
85 |
▶ | if (c == enc->num_columns) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == enc->num_columns) |
2 |
83 |
85 |
|
| 80 |
2 |
| croak("defaults key '%.*s' matches no column", (int)len, name); |
| 81 |
83 |
| value = hv_iterval(defaults, entry); |
| 82 |
83 |
| hv_store_ent(snapshot, key, newSVsv(value), 0); |
| 83 |
|
| } |
| 84 |
35 |
| defaults = snapshot; |
| 85 |
|
| } |
| 86 |
6753 |
▶ | for (c = 0; c < enc->num_columns; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) { |
4890 |
1863 |
6753 |
|
| 87 |
4890 |
| Column *col = &enc->columns[c]; |
| 88 |
4890 |
| TypeInfo *t = col->type; |
| 89 |
4890 |
▶ | SV **value = defaults ? fetch_named_value(aTHX_ defaults, col->name, col->name_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **value = defaults ? fetch_named_value(aTHX_ defaults, col->name, col->name_len) : NULL; |
140 |
4750 |
4890 |
|
| 90 |
4932 |
▶ | while (t->code == T_LOWCARDINALITY) t = t->inner; |
Branch: 1 branch
| Branch | True | False | Total |
| while (t->code == T_LOWCARDINALITY) t = t->inner; |
42 |
4890 |
4932 |
|
| 91 |
4890 |
▶ | if (t->code == T_NULLABLE) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->code == T_NULLABLE) continue; |
98 |
4792 |
4890 |
|
| 92 |
4792 |
▶ | col->default_flags = t->code != T_STRING && t->code != T_FIXEDSTRING ? DEFAULT_EMPTY_STRING : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| col->default_flags = t->code != T_STRING && t->code != T_FIXEDSTRING ? DEFAULT_EMPTY_STRING : 0; |
4536 |
256 |
4792 |
| col->default_flags = t->code != T_STRING && t->code != T_FIXEDSTRING ? DEFAULT_EMPTY_STRING : 0; |
4516 |
20 |
4536 |
|
| 93 |
4792 |
▶ | if (value) col->default_value = newSVsv(*value); |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) col->default_value = newSVsv(*value); |
79 |
4713 |
4792 |
|
| 94 |
4713 |
▶ | else if (empty_as_default) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (empty_as_default) { |
70 |
4643 |
4713 |
|
| 95 |
70 |
▶ | SV **expr = expressions ? fetch_named_value(aTHX_ expressions, col->name, col->name_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **expr = expressions ? fetch_named_value(aTHX_ expressions, col->name, col->name_len) : NULL; |
43 |
27 |
70 |
|
| 96 |
70 |
▶ | if (expr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (expr) { |
43 |
27 |
70 |
|
| 97 |
43 |
| col->default_flags |= DEFAULT_EXPRESSION; |
| 98 |
43 |
| col->default_value = newSVsv(*expr); |
| 99 |
27 |
| } else col->default_value = newSVsv(type_default(aTHX_ col->type)); |
| 100 |
|
| } |
| 101 |
|
| } |
| 102 |
1863 |
| } |
| 103 |
|
| |
| 104 |
|
| typedef struct { |
| 105 |
|
| Buffer data, nulls; |
| 106 |
|
| TypeInfo *scalar; |
| 107 |
|
| int nullable, complex_slot; |
| 108 |
|
| } RowColumn; |
| 109 |
|
| |
| 110 |
751403 |
| static SV *row_value(pTHX_ AV *row, SSize_t pos, AV *owned) { |
| 111 |
751403 |
| int magical = SvMAGICAL(row); |
| 112 |
283 |
▶ | SV **slot = magical ? pos <= av_len(row) ? av_fetch(row, pos, 0) : NULL |
Branch: 1 branch
| Branch | True | False | Total |
| SV **slot = magical ? pos <= av_len(row) ? av_fetch(row, pos, 0) : NULL |
281 |
2 |
283 |
|
| 113 |
751686 |
▶ | : pos <= AvFILLp(row) ? AvARRAY(row) + pos : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| : pos <= AvFILLp(row) ? AvARRAY(row) + pos : NULL; |
283 |
751120 |
751403 |
| : pos <= AvFILLp(row) ? AvARRAY(row) + pos : NULL; |
751113 |
7 |
751120 |
|
| 114 |
751403 |
▶ | SV *value = slot && *slot ? *slot : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *value = slot && *slot ? *slot : &PL_sv_undef; |
751394 |
9 |
751403 |
| SV *value = slot && *slot ? *slot : &PL_sv_undef; |
751393 |
1 |
751394 |
|
| 115 |
751403 |
▶ | if (magical || SvGMAGICAL(value)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (magical || SvGMAGICAL(value)) { |
751120 |
283 |
751403 |
| if (magical || SvGMAGICAL(value)) { |
99 |
751021 |
751120 |
|
| 116 |
382 |
| SvGETMAGIC(value); |
| 117 |
382 |
| value = newSVsv(value); |
| 118 |
382 |
▶ | if (owned) av_push(owned, value); |
Branch: 1 branch
| Branch | True | False | Total |
| if (owned) av_push(owned, value); |
152 |
230 |
382 |
|
| 119 |
230 |
| else sv_2mortal(value); |
| 120 |
751021 |
▶ | } 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)); |
164969 |
586052 |
751021 |
|
| 121 |
751403 |
| return value; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
2785 |
| void do_encode(pTHX_ Encoder *enc, AV *rows_av, Buffer *buf, const SSize_t *positions) { |
| 125 |
2785 |
| SSize_t num_rows = av_len(rows_av) + 1; |
| 126 |
2785 |
| SSize_t r, c, required = enc->num_columns; |
| 127 |
2785 |
| int complex_count = 0, group, *complex_columns = NULL; |
| 128 |
|
| SV **gathered; |
| 129 |
2785 |
| RowColumn *states = NULL; |
| 130 |
|
| AV *owned; |
| 131 |
|
| |
| 132 |
|
| /* Validate every row up front and cache the dereffed AV*. Caching |
| 133 |
|
| * avoids a second av_fetch per row in the column loop and removes |
| 134 |
|
| * any concern about tied / magic arrays returning different values |
| 135 |
|
| * across passes. */ |
| 136 |
2785 |
| AV **row_avs = NULL; |
| 137 |
2785 |
▶ | if (num_rows > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_rows > 0) { |
2753 |
32 |
2785 |
|
| 138 |
2753 |
▶ | Newx(row_avs, num_rows, AV*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(row_avs, num_rows, AV*); |
0 |
2753 |
2753 |
|
| 139 |
2753 |
| SAVEFREEPV(row_avs); |
| 140 |
|
| } |
| 141 |
484056 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
481277 |
2779 |
484056 |
|
| 142 |
481277 |
| SV **row_sv = av_fetch(rows_av, r, 0); |
| 143 |
|
| AV *row_av; |
| 144 |
|
| SSize_t row_cols; |
| 145 |
481277 |
▶ | if (!row_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!row_sv) |
0 |
481277 |
481277 |
|
| 146 |
0 |
| croak("Row %" IVdf " must be arrayref", (IV)r); |
| 147 |
|
| /* Materialize any magic on the fetched SV - for tied AVs the |
| 148 |
|
| * FETCH result is delivered through magic and SvROK / SvTYPE |
| 149 |
|
| * see the unrealized magical placeholder until SvGETMAGIC has |
| 150 |
|
| * been called. Common in DBI-driven ingest (fetchall_arrayref |
| 151 |
|
| * may return magical AVs at either level). */ |
| 152 |
481277 |
| SvGETMAGIC(*row_sv); |
| 153 |
481277 |
▶ | 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) |
481274 |
3 |
481277 |
| if (!SvROK(*row_sv) || SvTYPE(SvRV(*row_sv)) != SVt_PVAV) |
0 |
481274 |
481274 |
|
| 154 |
3 |
| croak("Row %" IVdf " must be arrayref", (IV)r); |
| 155 |
481274 |
| row_av = (AV*)SvRV(*row_sv); |
| 156 |
481274 |
| row_cols = av_len(row_av) + 1; |
| 157 |
481274 |
▶ | if (!positions && row_cols != required) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!positions && row_cols != required) |
481248 |
26 |
481274 |
| if (!positions && row_cols != required) |
3 |
481245 |
481248 |
|
| 158 |
3 |
| croak("Row %" IVdf " has %" IVdf " columns, expected %" IVdf, |
| 159 |
|
| (IV)r, (IV)row_cols, (IV)required); |
| 160 |
481271 |
| row_avs[r] = row_av; |
| 161 |
481271 |
▶ | if (SvMAGICAL(rows_av) || SvGMAGICAL(*row_sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvMAGICAL(rows_av) || SvGMAGICAL(*row_sv)) |
481260 |
11 |
481271 |
| if (SvMAGICAL(rows_av) || SvGMAGICAL(*row_sv)) |
0 |
481260 |
481260 |
|
| 162 |
11 |
| sv_2mortal(SvREFCNT_inc((SV *)row_av)); |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
2779 |
| buf_varint(aTHX_ buf, enc->num_columns); |
| 166 |
2779 |
| buf_varint(aTHX_ buf, num_rows); |
| 167 |
|
| |
| 168 |
2779 |
▶ | if (enc->num_columns) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->num_columns) { |
2778 |
1 |
2779 |
|
| 169 |
2778 |
| Newxz(states, enc->num_columns, RowColumn); |
| 170 |
2778 |
| SAVEFREEPV(states); |
| 171 |
2778 |
| Newx(complex_columns, enc->num_columns, int); |
| 172 |
2778 |
| SAVEFREEPV(complex_columns); |
| 173 |
|
| } |
| 174 |
9871 |
▶ | for (c = 0; c < enc->num_columns; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) { |
7092 |
2779 |
9871 |
|
| 175 |
7092 |
| TypeInfo *type = enc->columns[c].type; |
| 176 |
7092 |
| RowColumn *state = &states[c]; |
| 177 |
7092 |
| state->nullable = type->code == T_NULLABLE; |
| 178 |
7092 |
▶ | state->scalar = state->nullable ? type->inner : type; |
Branch: 1 branch
| Branch | True | False | Total |
| state->scalar = state->nullable ? type->inner : type; |
311 |
6781 |
7092 |
|
| 179 |
7092 |
▶ | if (!is_simple_type(state->scalar)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_simple_type(state->scalar)) { |
2037 |
5055 |
7092 |
|
| 180 |
2037 |
| state->scalar = NULL; |
| 181 |
2037 |
| state->complex_slot = complex_count; |
| 182 |
2037 |
| complex_columns[complex_count++] = c; |
| 183 |
5055 |
▶ | } else if (num_rows) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (num_rows) { |
5008 |
47 |
5055 |
|
| 184 |
|
| STRLEN width; |
| 185 |
5008 |
| buf_init(aTHX_ &state->data); |
| 186 |
5008 |
| encode_null_scalar(aTHX_ &state->data, state->scalar); |
| 187 |
5008 |
| width = state->data.len; |
| 188 |
5008 |
| state->data.len = 0; |
| 189 |
5008 |
▶ | 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 |
5008 |
5008 |
|
| 190 |
5008 |
| buf_grow(aTHX_ &state->data, num_rows * width); |
| 191 |
5008 |
▶ | if (state->nullable) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->nullable) { |
294 |
4714 |
5008 |
|
| 192 |
294 |
| buf_init(aTHX_ &state->nulls); |
| 193 |
294 |
| buf_grow(aTHX_ &state->nulls, num_rows); |
| 194 |
|
| } |
| 195 |
|
| } |
| 196 |
|
| } |
| 197 |
2779 |
| group = complex_count < CHE_GATHER_COLUMNS ? complex_count : CHE_GATHER_COLUMNS; |
| 198 |
2779 |
▶ | 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 *)) |
1713 |
1066 |
2779 |
| if (num_rows > SSize_t_MAX / (group ? group : 1) / (SSize_t)sizeof(SV *)) |
0 |
2779 |
2779 |
|
| 199 |
0 |
| croak("Too many rows to gather"); |
| 200 |
2779 |
| gathered = alloc_sv_array(aTHX_ num_rows * group); |
| 201 |
2779 |
| ENTER; |
| 202 |
2779 |
| SAVETMPS; |
| 203 |
2779 |
| owned = (AV *)sv_2mortal((SV *)newAV()); |
| 204 |
483847 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
481269 |
2578 |
483847 |
|
| 205 |
481269 |
| AV *row = row_avs[r]; |
| 206 |
481269 |
| ENTER; |
| 207 |
481269 |
| SAVETMPS; |
| 208 |
1232471 |
▶ | for (c = 0; c < enc->num_columns; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) { |
751403 |
481068 |
1232471 |
|
| 209 |
751403 |
| RowColumn *state = &states[c]; |
| 210 |
751403 |
▶ | SSize_t pos = positions ? positions[c] : c; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t pos = positions ? positions[c] : c; |
323 |
751080 |
751403 |
|
| 211 |
|
| SV *value; |
| 212 |
751403 |
▶ | if (state->scalar) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->scalar) { |
586282 |
165121 |
751403 |
|
| 213 |
586282 |
| value = column_value(aTHX_ &enc->columns[c], row_value(aTHX_ row, pos, NULL)); |
| 214 |
586274 |
▶ | if (state->nullable) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->nullable) { |
41162 |
545112 |
586274 |
|
| 215 |
41162 |
| int is_null = nullable_is_null(value, enc->columns[c].type); |
| 216 |
41162 |
| buf_byte(aTHX_ &state->nulls, is_null); |
| 217 |
41162 |
▶ | if (is_null) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_null) { |
8278 |
32884 |
41162 |
|
| 218 |
8278 |
| encode_null_scalar(aTHX_ &state->data, state->scalar); |
| 219 |
8278 |
| continue; |
| 220 |
|
| } |
| 221 |
|
| } |
| 222 |
577996 |
| encode_scalar(aTHX_ &state->data, value, state->scalar); |
| 223 |
165121 |
▶ | } else if (state->complex_slot < group) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (state->complex_slot < group) { |
164867 |
254 |
165121 |
|
| 224 |
164867 |
| value = row_value(aTHX_ row, pos, owned); |
| 225 |
164867 |
| gathered[state->complex_slot * num_rows + r] = column_value(aTHX_ &enc->columns[c], value); |
| 226 |
|
| } |
| 227 |
|
| } |
| 228 |
481068 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
36 |
481032 |
481068 |
|
| 229 |
481068 |
| LEAVE; |
| 230 |
|
| } |
| 231 |
9325 |
▶ | for (c = 0; c < enc->num_columns; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < enc->num_columns; c++) { |
6780 |
2545 |
9325 |
|
| 232 |
6780 |
| Column *col = &enc->columns[c]; |
| 233 |
6780 |
| RowColumn *state = &states[c]; |
| 234 |
6780 |
| buf_string(aTHX_ buf, col->name, col->name_len); |
| 235 |
6780 |
| buf_string(aTHX_ buf, col->type_str, col->type_len); |
| 236 |
6780 |
▶ | if (state->scalar) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->scalar) { |
4805 |
1975 |
6780 |
|
| 237 |
4805 |
▶ | if (num_rows) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_rows) { |
4758 |
47 |
4805 |
|
| 238 |
4758 |
▶ | 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); |
289 |
4469 |
4758 |
|
| 239 |
4758 |
| buf_append(aTHX_ buf, state->data.ptr, state->data.len); |
| 240 |
|
| } |
| 241 |
|
| } else { |
| 242 |
1975 |
| int index = state->complex_slot; |
| 243 |
1975 |
▶ | if (index && index % CHE_GATHER_COLUMNS == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (index && index % CHE_GATHER_COLUMNS == 0) { |
277 |
1698 |
1975 |
| if (index && index % CHE_GATHER_COLUMNS == 0) { |
10 |
267 |
277 |
|
| 244 |
10 |
| int end = complex_count - index; |
| 245 |
|
| int j; |
| 246 |
10 |
▶ | 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; |
5 |
5 |
10 |
|
| 247 |
10 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
10 |
0 |
10 |
|
| 248 |
10 |
| LEAVE; |
| 249 |
10 |
| ENTER; |
| 250 |
10 |
| SAVETMPS; |
| 251 |
10 |
| owned = (AV *)sv_2mortal((SV *)newAV()); |
| 252 |
31 |
▶ | for (r = 0; r < num_rows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < num_rows; r++) { |
22 |
9 |
31 |
|
| 253 |
22 |
| AV *row = row_avs[r]; |
| 254 |
275 |
▶ | for (j = 0; j < end; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < end; j++) { |
254 |
21 |
275 |
|
| 255 |
254 |
| int column = complex_columns[index + j]; |
| 256 |
254 |
▶ | SSize_t pos = positions ? positions[column] : column; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t pos = positions ? positions[column] : column; |
72 |
182 |
254 |
|
| 257 |
254 |
| gathered[j * num_rows + r] = column_value(aTHX_ &enc->columns[column], row_value(aTHX_ row, pos, owned)); |
| 258 |
|
| } |
| 259 |
|
| } |
| 260 |
|
| } |
| 261 |
1974 |
| ENTER; |
| 262 |
1974 |
| SAVETMPS; |
| 263 |
1974 |
▶ | 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, |
1936 |
38 |
1974 |
|
| 264 |
|
| num_rows, col->type); |
| 265 |
1942 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1847 |
95 |
1942 |
|
| 266 |
1942 |
| LEAVE; |
| 267 |
|
| } |
| 268 |
|
| } |
| 269 |
2545 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
2545 |
0 |
2545 |
|
| 270 |
2545 |
| LEAVE; |
| 271 |
2545 |
| } |
| 272 |
|
| |
| 273 |
|
| /* Route `bytes` through ClickHouse::Encoder->compress_native_block |
| 274 |
|
| * and return a freshly-allocated SV with the compressed-block-framed |
| 275 |
|
| * result. Caller takes ownership (refcount 1) and is responsible for |
| 276 |
|
| * mortalizing or freeing. mode == NULL / "none" / "raw" -> just bumps |
| 277 |
|
| * the input's refcount and returns it. */ |
| 278 |
4 |
| static SV *maybe_compress(pTHX_ SV *bytes, |
| 279 |
|
| const char *mode, SV *hasher_sv) { |
| 280 |
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 |
|
| 281 |
0 |
| return SvREFCNT_inc(bytes); |
| 282 |
4 |
| dSP; |
| 283 |
4 |
| ENTER; |
| 284 |
4 |
| SAVETMPS; |
| 285 |
4 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
4 |
4 |
|
| 286 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpvs("ClickHouse::Encoder"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("ClickHouse::Encoder"))); |
0 |
4 |
4 |
|
| 287 |
4 |
▶ | XPUSHs(bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(bytes); |
0 |
4 |
4 |
|
| 288 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpvs("mode"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("mode"))); |
0 |
4 |
4 |
|
| 289 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpv(mode, 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(mode, 0))); |
0 |
4 |
4 |
|
| 290 |
4 |
▶ | if (hasher_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hasher_sv) { |
1 |
3 |
4 |
|
| 291 |
1 |
▶ | XPUSHs(sv_2mortal(newSVpvs("hasher"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("hasher"))); |
0 |
1 |
1 |
|
| 292 |
1 |
▶ | XPUSHs(hasher_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(hasher_sv); |
0 |
1 |
1 |
|
| 293 |
|
| } |
| 294 |
4 |
| PUTBACK; |
| 295 |
4 |
| int n = call_method("compress_native_block", G_SCALAR); |
| 296 |
4 |
| SPAGAIN; |
| 297 |
4 |
| SV *out = NULL; |
| 298 |
4 |
▶ | if (n == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 1) { |
4 |
0 |
4 |
|
| 299 |
|
| /* POPs gives a mortal SV; copy its body via newSVsv into a |
| 300 |
|
| * non-mortal SV that survives the FREETMPS below. */ |
| 301 |
4 |
| out = newSVsv(POPs); |
| 302 |
|
| } |
| 303 |
4 |
| PUTBACK; |
| 304 |
4 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
4 |
0 |
4 |
|
| 305 |
4 |
| LEAVE; |
| 306 |
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 |
|
| 307 |
4 |
| return out; |
| 308 |
|
| } |
| 309 |
|
| |
| 310 |
77 |
| void encode_and_emit(pTHX_ Encoder *enc, AV *batch, SV *writer) { |
| 311 |
77 |
| dSP; |
| 312 |
77 |
| ENTER; |
| 313 |
77 |
| SAVETMPS; |
| 314 |
|
| |
| 315 |
|
| Buffer buf; |
| 316 |
77 |
| buf_init(aTHX_ &buf); |
| 317 |
77 |
| do_encode(aTHX_ enc, batch, &buf, NULL); |
| 318 |
|
| |
| 319 |
77 |
| SV *bytes = sv_2mortal(newSVpvn(buf.ptr, buf.len)); |
| 320 |
|
| |
| 321 |
77 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
77 |
77 |
|
| 322 |
77 |
▶ | XPUSHs(bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(bytes); |
0 |
77 |
77 |
|
| 323 |
77 |
| PUTBACK; |
| 324 |
77 |
| call_sv(writer, G_VOID | G_DISCARD); |
| 325 |
|
| |
| 326 |
74 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
74 |
0 |
74 |
|
| 327 |
74 |
| LEAVE; |
| 328 |
74 |
| } |
| 329 |
|
| |
| 330 |
34 |
| void streamer_flush(pTHX_ Streamer *s) { |
| 331 |
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 |
|
| 332 |
|
| /* Swap in a fresh buffer BEFORE the (potentially croaking) emit so that |
| 333 |
|
| * if the writer dies and the user catches it via eval{}, the streamer |
| 334 |
|
| * is left in a clean state instead of replaying the failed batch's rows |
| 335 |
|
| * on the next push. The old buffer is mortalized so it's reclaimed |
| 336 |
|
| * either way. */ |
| 337 |
28 |
| AV *batch = s->buffer; |
| 338 |
28 |
| s->buffer = newAV(); |
| 339 |
28 |
| sv_2mortal((SV *)batch); |
| 340 |
|
| |
| 341 |
|
| /* If compression is enabled, build the bytes locally then route them |
| 342 |
|
| * through compress_native_block before invoking the user's writer. |
| 343 |
|
| * Otherwise fall through to encode_and_emit's direct call. */ |
| 344 |
28 |
▶ | if (s->compress_mode) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->compress_mode) { |
4 |
24 |
28 |
|
| 345 |
4 |
| dSP; |
| 346 |
4 |
| ENTER; |
| 347 |
4 |
| SAVETMPS; |
| 348 |
|
| Buffer buf; |
| 349 |
4 |
| buf_init(aTHX_ &buf); |
| 350 |
4 |
| do_encode(aTHX_ s->enc, batch, &buf, NULL); |
| 351 |
4 |
| SV *bytes = sv_2mortal(newSVpvn(buf.ptr, buf.len)); |
| 352 |
|
| /* maybe_compress returns an owned SV (refcount 1); mortalize it |
| 353 |
|
| * here so it's cleaned up after the writer call. */ |
| 354 |
4 |
| SV *out = sv_2mortal( |
| 355 |
|
| maybe_compress(aTHX_ bytes, s->compress_mode, s->hasher_sv)); |
| 356 |
4 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
4 |
4 |
|
| 357 |
4 |
▶ | XPUSHs(out); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(out); |
0 |
4 |
4 |
|
| 358 |
4 |
| PUTBACK; |
| 359 |
4 |
| call_sv(s->writer, G_VOID | G_DISCARD); |
| 360 |
4 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
4 |
0 |
4 |
|
| 361 |
4 |
| LEAVE; |
| 362 |
4 |
| return; |
| 363 |
|
| } |
| 364 |
24 |
| encode_and_emit(aTHX_ s->enc, batch, s->writer); |
| 365 |
|
| } |
| 366 |
|
| |
| 367 |
19 |
| void free_streamer(pTHX_ Streamer *s) { |
| 368 |
19 |
▶ | if (!s) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return; |
0 |
19 |
19 |
|
| 369 |
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 |
|
| 370 |
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 |
|
| 371 |
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 |
|
| 372 |
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 |
|
| 373 |
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 |
|
| 374 |
19 |
| Safefree(s); |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
19 |
| void cleanup_streamer_slot(pTHX_ void *p) { |
| 378 |
19 |
| Streamer **slot = (Streamer **)p; |
| 379 |
19 |
▶ | if (*slot) free_streamer(aTHX_ *slot); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*slot) free_streamer(aTHX_ *slot); |
0 |
19 |
19 |
|
| 380 |
19 |
| } |
| 381 |
|
| |
| 382 |
1900 |
| void free_encoder(pTHX_ Encoder *enc) { |
| 383 |
|
| int i; |
| 384 |
1900 |
▶ | if (!enc) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!enc) return; |
0 |
1900 |
1900 |
|
| 385 |
1900 |
▶ | if (enc->columns) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->columns) { |
1900 |
0 |
1900 |
|
| 386 |
6826 |
▶ | for (i = 0; i < enc->num_columns; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < enc->num_columns; i++) { |
4926 |
1900 |
6826 |
|
| 387 |
4926 |
▶ | 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); |
4924 |
2 |
4926 |
|
| 388 |
4926 |
▶ | 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); |
4924 |
2 |
4926 |
|
| 389 |
4926 |
▶ | 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); |
4895 |
31 |
4926 |
|
| 390 |
4926 |
| SvREFCNT_dec(enc->columns[i].default_value); |
| 391 |
|
| } |
| 392 |
1900 |
| Safefree(enc->columns); |
| 393 |
|
| } |
| 394 |
1900 |
| timezone_free(aTHX_ enc->zones); |
| 395 |
1900 |
| Safefree(enc->timezone); |
| 396 |
1900 |
| Safefree(enc); |
| 397 |
|
| } |
| 398 |
|
| |
| 399 |
1900 |
| void cleanup_encoder_slot(pTHX_ void *p) { |
| 400 |
1900 |
| Encoder **slot = (Encoder **)p; |
| 401 |
1900 |
▶ | if (*slot) free_encoder(aTHX_ *slot); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*slot) free_encoder(aTHX_ *slot); |
37 |
1863 |
1900 |
|
| 402 |
1900 |
| } |