types.c

filter: Statementstmt 92.7% 533 / 575 Branchbran 73.9% 633 / 856 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.4% 1166 / 1431 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« scalar_kind.c ↑ Summary json_kind.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 <stdlib.h>
8 #include <limits.h>
9 #include <stdint.h>
10  
11 #include "types.h"
12 #include "scalar_kind.h"
13  
14 /* Cap on type-expression nesting. parse_type recurses once per level, as
15 * do free_typeinfo / encode_column / decode_column over the tree it
16 * builds, so an unbounded depth overflows the C stack - and type strings
17 * are not always trusted: decode_block parses whatever a Native block
18 * carries. Real schemas nest a handful of levels (MultiPolygon, the
19 * deepest built-in alias, is 4). Capping the parse bounds every other
20 * recursive walk, since they all follow this tree. */
21 #define CHE_MAX_TYPE_DEPTH 100
22  
23 static TypeInfo* parse_type_at(pTHX_ const char *type, STRLEN len, int depth);
24  
25 /* Bounded stand-in for atoi() on a slice that is not NUL-terminated at
26 * `maxlen`, whose behaviour on an over-long digit run is undefined.
27 * Returns -1 when no digit is present or the value would not fit an int,
28 * leaving each caller's range check to report it. The ceiling is INT_MAX
29 * rather than anything tighter: FixedString(N) is legal up to CH's
30 * MAX_FIXED_STRING_SIZE and this parser also runs on wire type strings.
31 * Overflow is checked before the multiply, so `long`'s width is moot. */
32 1257 static int parse_int_param(const char *s, STRLEN maxlen, int allow_comma) {
33 1257 STRLEN i = 0;
34 1257 long v = 0;
35 1264 ▶ while (i < maxlen && (s[i] == ' ' || s[i] == '\t'
Branch: 3 branches
BranchTrueFalseTotal
while (i < maxlen && (s[i] == ' ' || s[i] == '\t' 1264 0 1264
while (i < maxlen && (s[i] == ' ' || s[i] == '\t' 7 1257 1264
while (i < maxlen && (s[i] == ' ' || s[i] == '\t' 0 1257 1257
36 1264 ▶ || s[i] == '\n' || s[i] == '\r')) i++;
Branch: 2 branches
BranchTrueFalseTotal
|| s[i] == '\n' || s[i] == '\r')) i++; 0 1257 1257
|| s[i] == '\n' || s[i] == '\r')) i++; 0 1257 1257
37 1257 ▶ if (i >= maxlen || s[i] < '0' || s[i] > '9') return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (i >= maxlen || s[i] < '0' || s[i] > '9') return -1; 1257 0 1257
if (i >= maxlen || s[i] < '0' || s[i] > '9') return -1; 1257 0 1257
if (i >= maxlen || s[i] < '0' || s[i] > '9') return -1; 0 1257 1257
38 2537 ▶ for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) {
Branch: 3 branches
BranchTrueFalseTotal
for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) { 2537 0 2537
for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) { 1281 1256 2537
for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) { 1281 0 1281
39 1281 int digit = s[i] - '0';
40 1281 ▶ if (v > (INT_MAX - digit) / 10) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (INT_MAX - digit) / 10) return -1; 1 1280 1281
41 1280 v = v * 10 + digit;
42 }
43 1256 ▶ while (i < maxlen && isSPACE(s[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < maxlen && isSPACE(s[i])) i++; 1256 0 1256
while (i < maxlen && isSPACE(s[i])) i++; 0 1256 1256
44 1256 ▶ if (i == maxlen || (s[i] != ')' && !(allow_comma && s[i] == ','))) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (i == maxlen || (s[i] != ')' && !(allow_comma && s[i] == ','))) return -1; 1256 0 1256
if (i == maxlen || (s[i] != ')' && !(allow_comma && s[i] == ','))) return -1; 31 1225 1256
if (i == maxlen || (s[i] != ')' && !(allow_comma && s[i] == ','))) return -1; 31 0 31
if (i == maxlen || (s[i] != ')' && !(allow_comma && s[i] == ','))) return -1; 0 31 31
45 1256 ▶ if (s[i] == ')' && i + 1 != maxlen) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == ')' && i + 1 != maxlen) return -1; 1225 31 1256
if (s[i] == ')' && i + 1 != maxlen) return -1; 0 1225 1225
46 1256 return (int)v;
47 }
48  
49 117 static STRLEN skip_type_quote(pTHX_ const char *s, STRLEN len, STRLEN pos) {
50 117 char quote = s[pos++];
51 780 ▶ while (pos < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < len) { 780 0 780
52 780 ▶ if (s[pos] == '\\') { pos += 2; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (s[pos] == '\\') { pos += 2; continue; } 0 780 780
53 780 ▶ if (s[pos++] == quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (s[pos++] == quote) { 117 663 780
54 117 ▶ if (pos < len && s[pos] == quote) { pos++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (pos < len && s[pos] == quote) { pos++; continue; } 117 0 117
if (pos < len && s[pos] == quote) { pos++; continue; } 0 117 117
55 117 return pos;
56 }
57 }
58 0 croak("Unterminated quote in type expression");
59 return len;
60 }
61  
62 70 static char *copy_type_name(pTHX_ const char *s, STRLEN len) {
63 char *out;
64 70 Newx(out, len + 1, char);
65 70 STRLEN i = 0, n = 0;
66 70 ▶ if (len && s[0] == '`') { i = 1; len--; }
Branch: 2 branches
BranchTrueFalseTotal
if (len && s[0] == '`') { i = 1; len--; } 70 0 70
if (len && s[0] == '`') { i = 1; len--; } 4 66 70
67 276 ▶ for (; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < len; i++) { 206 70 276
68 206 ▶ if (s[i] == '\\' && i + 1 < len) i++;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == '\\' && i + 1 < len) i++; 0 206 206
if (s[i] == '\\' && i + 1 < len) i++; 0 0 0
69 206 ▶ else if (s[i] == '`' && i + 1 < len && s[i+1] == '`') i++;
Branch: 3 branches
BranchTrueFalseTotal
else if (s[i] == '`' && i + 1 < len && s[i+1] == '`') i++; 0 206 206
else if (s[i] == '`' && i + 1 < len && s[i+1] == '`') i++; 0 0 0
else if (s[i] == '`' && i + 1 < len && s[i+1] == '`') i++; 0 0 0
70 206 out[n++] = s[i];
71 }
72 70 out[n] = 0;
73 70 return out;
74 }
75  
76 36948 void free_typeinfo(pTHX_ TypeInfo *t) {
77 36948 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 36948 36948
78 36948 ▶ if (t->inner) free_typeinfo(aTHX_ t->inner);
Branch: 1 branch
BranchTrueFalseTotal
if (t->inner) free_typeinfo(aTHX_ t->inner); 2612 34336 36948
79 36948 ▶ if (t->tuple) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple) { 1196 35752 36948
80 int i;
81 3566 ▶ for (i = 0; i < t->tuple_len; i++) free_typeinfo(aTHX_ t->tuple[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->tuple_len; i++) free_typeinfo(aTHX_ t->tuple[i]); 2370 1196 3566
82 1196 Safefree(t->tuple);
83 }
84 36948 ▶ if (t->enum_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_entries) { 1122 35826 36948
85 int i;
86 3482 ▶ for (i = 0; i < t->enum_count; i++) Safefree(t->enum_entries[i].name);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->enum_count; i++) Safefree(t->enum_entries[i].name); 2360 1122 3482
87 1122 Safefree(t->enum_entries);
88 }
89 36948 ▶ if (t->enum_lookup) SvREFCNT_dec((SV*)t->enum_lookup);
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_lookup) SvREFCNT_dec((SV*)t->enum_lookup); 1126 35822 36948
90 36948 ▶ if (t->enum_codes) Safefree(t->enum_codes);
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_codes) Safefree(t->enum_codes); 1126 35822 36948
91 36948 ▶ if (t->timezone) Safefree(t->timezone);
Branch: 1 branch
BranchTrueFalseTotal
if (t->timezone) Safefree(t->timezone); 97 36851 36948
92 36948 ▶ if (t->variant_decl_to_wire) Safefree(t->variant_decl_to_wire);
Branch: 1 branch
BranchTrueFalseTotal
if (t->variant_decl_to_wire) Safefree(t->variant_decl_to_wire); 32 36916 36948
93 36948 ▶ if (t->variant_wire_to_decl) Safefree(t->variant_wire_to_decl);
Branch: 1 branch
BranchTrueFalseTotal
if (t->variant_wire_to_decl) Safefree(t->variant_wire_to_decl); 32 36916 36948
94 36948 ▶ if (t->tuple_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_names) { 47 36901 36948
95 int i;
96 115 ▶ for (i = 0; i < t->tuple_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->tuple_len; i++) 68 47 115
97 68 ▶ if (t->tuple_names[i]) Safefree(t->tuple_names[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_names[i]) Safefree(t->tuple_names[i]); 68 0 68
98 47 Safefree(t->tuple_names);
99 }
100 36948 Safefree(t);
101 }
102  
103 1126 static void parse_enum_entries(pTHX_ TypeInfo *t, const char *s, STRLEN len, int code) {
104 1126 int cap = 0;
105 1126 ▶ long min_val = (code == T_ENUM8) ? -128 : -32768;
Branch: 1 branch
BranchTrueFalseTotal
long min_val = (code == T_ENUM8) ? -128 : -32768; 1069 57 1126
106 1126 ▶ long max_val = (code == T_ENUM8) ? 127 : 32767;
Branch: 1 branch
BranchTrueFalseTotal
long max_val = (code == T_ENUM8) ? 127 : 32767; 1069 57 1126
107 1126 t->enum_entries = NULL;
108 1126 t->enum_count = 0;
109 1126 t->enum_lookup = newHV();
110 1126 ▶ Newxz(t->enum_codes, code == T_ENUM8 ? 32 : 8192, unsigned char);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(t->enum_codes, code == T_ENUM8 ? 32 : 8192, unsigned char); 1069 57 1126
111  
112 1126 STRLEN i = 0;
113 3486 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 2364 1122 3486
114 4840 ▶ while (i < len && (s[i] == ' ' || s[i] == ',')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == ',')) i++; 4840 0 4840
while (i < len && (s[i] == ' ' || s[i] == ',')) i++; 1238 3602 4840
while (i < len && (s[i] == ' ' || s[i] == ',')) i++; 1238 2364 3602
115 2364 ▶ if (i >= len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) break; 0 2364 2364
116  
117 2364 ▶ if (s[i] != '\'')
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] != '\'') 0 2364 2364
118 0 croak("Invalid enum format: expected single quote at position %d", (int)i);
119 2364 i++;
120  
121 /* Scan the name and unescape backslash-escapes (\\, \') so the
122 * stored name matches the unescaped form ClickHouse emits in
123 * describe table output and that the user passes to encode(). */
124 2364 STRLEN name_start = i;
125 2364 STRLEN name_raw_len = 0; /* raw bytes consumed (incl. backslashes) */
126 5358 ▶ while (i < len && s[i] != '\'') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && s[i] != '\'') { 5358 0 5358
while (i < len && s[i] != '\'') { 2994 2364 5358
127 2994 ▶ if (s[i] == '\\' && i + 1 < len) { i++; name_raw_len++; }
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == '\\' && i + 1 < len) { i++; name_raw_len++; } 1 2993 2994
if (s[i] == '\\' && i + 1 < len) { i++; name_raw_len++; } 1 0 1
128 2994 i++; name_raw_len++;
129 }
130 2364 ▶ if (i >= len)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) 0 2364 2364
131 0 croak("Invalid enum format: unterminated quote");
132  
133 /* Build the unescaped name into a freshly-allocated buffer. The
134 * unescaped length is at most name_raw_len; allocate that bound. */
135 char *name_buf;
136 2364 Newx(name_buf, name_raw_len + 1, char);
137 2364 STRLEN name_len = 0;
138 2364 STRLEN j = name_start;
139 5358 ▶ while (j < i) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < i) { 2994 2364 5358
140 2994 ▶ if (s[j] == '\\' && j + 1 < i) j++;
Branch: 2 branches
BranchTrueFalseTotal
if (s[j] == '\\' && j + 1 < i) j++; 1 2993 2994
if (s[j] == '\\' && j + 1 < i) j++; 1 0 1
141 2994 name_buf[name_len++] = s[j++];
142 }
143 2364 name_buf[name_len] = 0;
144 2364 ▶ if (name_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_len == 0) { 1 2363 2364
145 1 Safefree(name_buf);
146 1 croak("Invalid enum format: empty name at position %d", (int)name_start);
147 }
148 2363 i++; /* closing quote */
149  
150 9452 ▶ while (i < len && (s[i] == ' ' || s[i] == '=')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '=')) i++; 9452 0 9452
while (i < len && (s[i] == ' ' || s[i] == '=')) i++; 4726 4726 9452
while (i < len && (s[i] == ' ' || s[i] == '=')) i++; 2363 2363 4726
151  
152 2363 int neg = 0;
153 2363 ▶ if (i < len && s[i] == '-') { neg = 1; i++; }
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '-') { neg = 1; i++; } 2363 0 2363
if (i < len && s[i] == '-') { neg = 1; i++; } 52 2311 2363
154 2363 ▶ if (i >= len || s[i] < '0' || s[i] > '9') {
Branch: 3 branches
BranchTrueFalseTotal
if (i >= len || s[i] < '0' || s[i] > '9') { 2363 0 2363
if (i >= len || s[i] < '0' || s[i] > '9') { 2363 0 2363
if (i >= len || s[i] < '0' || s[i] > '9') { 0 2363 2363
155 0 Safefree(name_buf);
156 0 croak("Invalid enum format: expected digit at position %d", (int)i);
157 }
158 2363 long val = 0;
159 2363 int overflow = 0;
160 4777 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 3652 1125 4777
while (i < len && s[i] >= '0' && s[i] <= '9') { 2414 1238 3652
while (i < len && s[i] >= '0' && s[i] <= '9') { 2414 0 2414
161 /* Stop well before `long` wraps: signed overflow is undefined
162 * and a wrapped value could land back inside the range check
163 * below. 100000 is past every Enum16 bound. */
164 2414 ▶ if (val > 100000) overflow = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (val > 100000) overflow = 1; 14 2400 2414
165 2400 else val = val * 10 + (s[i] - '0');
166 2414 i++;
167 }
168 2363 ▶ if (overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if (overflow) { 1 2362 2363
169 1 Safefree(name_buf);
170 1 ▶ croak("Enum value out of range for %s (must be %ld..%ld)",
Branch: 1 branch
BranchTrueFalseTotal
croak("Enum value out of range for %s (must be %ld..%ld)", 1 0 1
171 code == T_ENUM8 ? "Enum8" : "Enum16", min_val, max_val);
172 }
173 2362 ▶ if (neg) val = -val;
Branch: 1 branch
BranchTrueFalseTotal
if (neg) val = -val; 52 2310 2362
174 2362 ▶ if (val < min_val || val > max_val) {
Branch: 2 branches
BranchTrueFalseTotal
if (val < min_val || val > max_val) { 2362 0 2362
if (val < min_val || val > max_val) { 2 2360 2362
175 2 Safefree(name_buf);
176 2 ▶ croak("Enum value %ld out of range for %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Enum value %ld out of range for %s", 1 1 2
177 val, code == T_ENUM8 ? "Enum8" : "Enum16");
178 }
179  
180 2360 ▶ if (t->enum_count >= cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_count >= cap) { 1122 1238 2360
181 1122 ▶ cap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 8; 0 1122 1122
182 1122 Renew(t->enum_entries, cap, EnumEntry);
183 }
184 2360 t->enum_entries[t->enum_count].name = name_buf;
185 2360 t->enum_entries[t->enum_count].name_len = name_len;
186 2360 t->enum_entries[t->enum_count].value = (int16_t)val;
187 2360 t->enum_count++;
188  
189 2360 hv_store(t->enum_lookup, name_buf, name_len, newSViv(val), 0);
190 2360 ▶ if (looks_like_int_str(name_buf, name_len)) t->enum_has_integer_labels = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_int_str(name_buf, name_len)) t->enum_has_integer_labels = 1; 114 2246 2360
191 {
192 2360 unsigned index = (unsigned)(val - min_val);
193 2360 t->enum_codes[index >> 3] |= 1U << (index & 7);
194 }
195 }
196 1122 }
197  
198 /* Heap-allocated cleanup slot for a TypeInfo*. Disarmed by setting *slot=NULL. */
199 36949 static void cleanup_typeinfo_slot(pTHX_ void *p) {
200 36949 TypeInfo **slot = (TypeInfo **)p;
201 36949 ▶ if (*slot) free_typeinfo(aTHX_ *slot);
Branch: 1 branch
BranchTrueFalseTotal
if (*slot) free_typeinfo(aTHX_ *slot); 228 36721 36949
202 36949 Safefree(slot);
203 36949 }
204  
205 /* Cleanup for a caller-owned TypeInfo*. parse_type disarms its own slot on
206 * return, so a later croak would otherwise leak the tree. Frees the type,
207 * not the slot, so the slot can be a local. Disarm by setting it NULL. */
208 26843 void cleanup_typeinfo_ptr(pTHX_ void *p) {
209 26843 TypeInfo **slot = (TypeInfo **)p;
210 26843 ▶ if (*slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot) { 26843 0 26843
211 26843 free_typeinfo(aTHX_ *slot);
212 26843 *slot = NULL;
213 }
214 26843 }
215  
216 /* Cleanup for a partially-built Tuple types array. The struct owns the array
217 * directly so the destructor never dereferences stack memory after a longjmp
218 * out of parse_tuple_types. Disarm by setting slot->types = NULL. */
219 typedef struct {
220 TypeInfo **types;
221 int count;
222 } TupleSlot;
223  
224 1199 static void cleanup_tuple_slot(pTHX_ void *p) {
225 1199 TupleSlot *s = (TupleSlot *)p;
226 1199 ▶ if (s->types) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->types) { 0 1199 1199
227 int i;
228 0 ▶ for (i = 0; i < s->count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->count; i++) 0 0 0
229 0 ▶ if (s->types[i]) free_typeinfo(aTHX_ s->types[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (s->types[i]) free_typeinfo(aTHX_ s->types[i]); 0 0 0
230 0 Safefree(s->types);
231 }
232 1199 Safefree(s);
233 1199 }
234  
235 /* Bound of one entry in a comma-separated type list, after outer-WS strip
236 * and optional "field-name<ws>" prefix removal. Both Tuple/Map/Variant
237 * parsing and Variant alphabetical sorting need these post-strip bounds. */
238 typedef struct {
239 STRLEN start; /* offset of the trimmed type expression */
240 STRLEN len; /* length of the trimmed type expression */
241 STRLEN name_start; /* offset of the field-name prefix (or 0 if absent) */
242 STRLEN name_len; /* length of the field-name prefix (0 = no name) */
243 char *name; /* unquoted JSON path name */
244 } TypeBound;
245  
246 /* Split a comma-separated type list at depth-0 commas, trim outer
247 * whitespace, strip any leading "name<ws>" field-name prefix (Tuple
248 * named-element form). When a field-name is found, name_start/name_len
249 * record it so callers (e.g. T_TUPLE) can keep the names; when absent,
250 * name_len is 0. bounds holds type_list_capacity(s, len) slots. */
251 2412 static STRLEN type_list_capacity(const char *s, STRLEN len) {
252 2412 STRLEN n = 1;
253 2412 const char *end = s + len, *comma;
254 4806 ▶ while ((comma = memchr(s, ',', end - s))) { n++; s = comma + 1; }
Branch: 1 branch
BranchTrueFalseTotal
while ((comma = memchr(s, ',', end - s))) { n++; s = comma + 1; } 2394 2412 4806
255 2412 return n;
256 }
257  
258 1160 static int split_type_list(pTHX_ const char *s, STRLEN len, TypeBound *bounds) {
259 1160 int count = 0;
260 1160 int depth = 0;
261 1160 STRLEN start = 0, i;
262 #define IS_WS(c) ((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r')
263 28078 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 26918 1160 28078
264 26918 ▶ char c = (i < len) ? s[i] : ',';
Branch: 1 branch
BranchTrueFalseTotal
char c = (i < len) ? s[i] : ','; 25758 1160 26918
265 26918 ▶ if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ s, len, i) - 1; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ s, len, i) - 1; continue; } 26906 12 26918
if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ s, len, i) - 1; continue; } 1 26905 26906
266 26905 ▶ if (c == '(') depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') depth++; 1054 25851 26905
267 25851 ▶ else if (c == ')') depth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ')') depth--; 1054 24797 25851
268 24797 ▶ else if ((c == ',' && depth == 0) || i == len) {
Branch: 3 branches
BranchTrueFalseTotal
else if ((c == ',' && depth == 0) || i == len) { 2331 22466 24797
else if ((c == ',' && depth == 0) || i == len) { 12 2319 2331
else if ((c == ',' && depth == 0) || i == len) { 0 22478 22478
269 2319 STRLEN tstart = start, tend = i;
270 2319 STRLEN nstart = 0, nlen = 0;
271 3478 ▶ while (tstart < tend && IS_WS(s[tstart])) tstart++;
Branch: 5 branches
BranchTrueFalseTotal
while (tstart < tend && IS_WS(s[tstart])) tstart++; 3474 4 3478
while (tstart < tend && IS_WS(s[tstart])) tstart++; 1159 2315 3474
while (tstart < tend && IS_WS(s[tstart])) tstart++; 0 2315 2315
while (tstart < tend && IS_WS(s[tstart])) tstart++; 0 2315 2315
while (tstart < tend && IS_WS(s[tstart])) tstart++; 0 2315 2315
272 2319 ▶ while (tend > tstart && IS_WS(s[tend-1])) tend--;
Branch: 5 branches
BranchTrueFalseTotal
while (tend > tstart && IS_WS(s[tend-1])) tend--; 2315 4 2319
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2315 2315
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2315 2315
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2315 2315
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2315 2315
273 2319 ▶ if (tend > tstart) {
Branch: 1 branch
BranchTrueFalseTotal
if (tend > tstart) { 2315 4 2319
274 2315 STRLEN id_end = tstart;
275 2315 ▶ if (id_end < tend && s[id_end] == '`') {
Branch: 2 branches
BranchTrueFalseTotal
if (id_end < tend && s[id_end] == '`') { 2315 0 2315
if (id_end < tend && s[id_end] == '`') { 1 2314 2315
276 1 id_end = skip_type_quote(aTHX_ s, tend, id_end);
277 1 ▶ if (id_end < tend && IS_WS(s[id_end])) {
Branch: 5 branches
BranchTrueFalseTotal
if (id_end < tend && IS_WS(s[id_end])) { 1 0 1
if (id_end < tend && IS_WS(s[id_end])) { 0 1 1
if (id_end < tend && IS_WS(s[id_end])) { 0 0 0
if (id_end < tend && IS_WS(s[id_end])) { 0 0 0
if (id_end < tend && IS_WS(s[id_end])) { 0 0 0
278 1 nstart = tstart;
279 1 nlen = id_end - tstart;
280 2 ▶ while (id_end < tend && IS_WS(s[id_end])) id_end++;
Branch: 5 branches
BranchTrueFalseTotal
while (id_end < tend && IS_WS(s[id_end])) id_end++; 2 0 2
while (id_end < tend && IS_WS(s[id_end])) id_end++; 1 1 2
while (id_end < tend && IS_WS(s[id_end])) id_end++; 0 1 1
while (id_end < tend && IS_WS(s[id_end])) id_end++; 0 1 1
while (id_end < tend && IS_WS(s[id_end])) id_end++; 0 1 1
281 1 tstart = id_end;
282 }
283 2314 ▶ } else if (id_end < tend
Branch: 1 branch
BranchTrueFalseTotal
} else if (id_end < tend 2314 0 2314
284 2314 ▶ && ((s[id_end] >= 'A' && s[id_end] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 2314 0 2314
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 12 2302 2314
285 12 ▶ || (s[id_end] >= 'a' && s[id_end] <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
|| (s[id_end] >= 'a' && s[id_end] <= 'z') 12 0 12
|| (s[id_end] >= 'a' && s[id_end] <= 'z') 0 12 12
286 0 ▶ || s[id_end] == '_')) {
Branch: 1 branch
BranchTrueFalseTotal
|| s[id_end] == '_')) { 0 0 0
287 2314 id_end++;
288 2314 while (id_end < tend
289 14881 ▶ && ((s[id_end] >= 'A' && s[id_end] <= 'Z')
Branch: 3 branches
BranchTrueFalseTotal
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 13630 1251 14881
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 10328 3302 13630
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 104 10224 10328
290 13526 ▶ || (s[id_end] >= 'a' && s[id_end] <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
|| (s[id_end] >= 'a' && s[id_end] <= 'z') 10224 3302 13526
|| (s[id_end] >= 'a' && s[id_end] <= 'z') 10224 0 10224
291 3302 ▶ || (s[id_end] >= '0' && s[id_end] <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (s[id_end] >= '0' && s[id_end] <= '9') 2239 1063 3302
|| (s[id_end] >= '0' && s[id_end] <= '9') 2239 0 2239
292 1063 ▶ || s[id_end] == '_'))
Branch: 1 branch
BranchTrueFalseTotal
|| s[id_end] == '_')) 0 1063 1063
293 12567 id_end++;
294 2314 ▶ if (id_end < tend && IS_WS(s[id_end])) {
Branch: 5 branches
BranchTrueFalseTotal
if (id_end < tend && IS_WS(s[id_end])) { 1063 1251 2314
if (id_end < tend && IS_WS(s[id_end])) { 1051 12 1063
if (id_end < tend && IS_WS(s[id_end])) { 1051 0 1051
if (id_end < tend && IS_WS(s[id_end])) { 1051 0 1051
if (id_end < tend && IS_WS(s[id_end])) { 0 1051 1051
295 12 nstart = tstart;
296 12 nlen = id_end - tstart;
297 12 STRLEN ts = id_end;
298 24 ▶ while (ts < tend && IS_WS(s[ts])) ts++;
Branch: 5 branches
BranchTrueFalseTotal
while (ts < tend && IS_WS(s[ts])) ts++; 24 0 24
while (ts < tend && IS_WS(s[ts])) ts++; 12 12 24
while (ts < tend && IS_WS(s[ts])) ts++; 0 12 12
while (ts < tend && IS_WS(s[ts])) ts++; 0 12 12
while (ts < tend && IS_WS(s[ts])) ts++; 0 12 12
299 12 ▶ if (ts < tend) tstart = ts;
Branch: 1 branch
BranchTrueFalseTotal
if (ts < tend) tstart = ts; 12 0 12
300 }
301 }
302 2315 bounds[count].start = tstart;
303 2315 bounds[count].len = tend - tstart;
304 2315 bounds[count].name_start = nstart;
305 2315 bounds[count].name_len = nlen;
306 2315 count++;
307 }
308 2319 start = i + 1;
309 }
310 }
311 #undef IS_WS
312 1160 return count;
313 }
314  
315 /* parse_tuple_types_with_bounds: caller already split the list and wants
316 * to reuse the bounds (e.g. Variant alphabetical sort). For convenience,
317 * parse_tuple_types is a thin wrapper that splits internally. */
318 1199 static TypeInfo** parse_tuple_types_with_bounds(pTHX_ const char *s,
319 TypeBound *bounds,
320 int n, int depth) {
321 TupleSlot *slot;
322 1199 Newxz(slot, 1, TupleSlot);
323 1199 SAVEDESTRUCTOR_X(cleanup_tuple_slot, slot);
324 1199 ▶ if (n > 0) Newxz(slot->types, n, TypeInfo*);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) Newxz(slot->types, n, TypeInfo*); 1196 3 1199
325  
326 int i;
327 3569 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2370 1199 3569
328 4740 slot->types[i] = parse_type_at(aTHX_ s + bounds[i].start,
329 2370 bounds[i].len, depth);
330 2370 slot->count = i + 1;
331 }
332 {
333 1199 TypeInfo **result = slot->types;
334 1199 slot->types = NULL; /* Disarm: caller now owns the array. */
335 1199 return result;
336 }
337 }
338  
339 67 static TypeInfo** parse_tuple_types(pTHX_ const char *s, STRLEN len, int *count,
340 int depth) {
341 TypeBound *bounds;
342 67 ▶ Newx(bounds, type_list_capacity(s, len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newx(bounds, type_list_capacity(s, len), TypeBound); 0 67 67
343 67 SAVEFREEPV(bounds);
344 67 *count = split_type_list(aTHX_ s, len, bounds);
345 67 return parse_tuple_types_with_bounds(aTHX_ s, bounds, *count, depth);
346 }
347  
348 68 static SV *canonical_variant_type(pTHX_ const char *s, STRLEN len, TypeInfo *t) {
349 68 ▶ while (len && isSPACE(*s)) { s++; len--; }
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(*s)) { s++; len--; } 68 0 68
while (len && isSPACE(*s)) { s++; len--; } 0 68 68
350 68 ▶ while (len && isSPACE(s[len-1])) len--;
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(s[len-1])) len--; 68 0 68
while (len && isSPACE(s[len-1])) len--; 0 68 68
351 68 const char *open = memchr(s, '(', len);
352 68 ▶ STRLEN base_len = open ? (STRLEN)(open - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN base_len = open ? (STRLEN)(open - s) : len; 8 60 68
353 68 ▶ if (base_len == 7 && memcmp(s, "Boolean", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (base_len == 7 && memcmp(s, "Boolean", 7) == 0) 0 68 68
if (base_len == 7 && memcmp(s, "Boolean", 7) == 0) 0 0 0
354 0 return sv_2mortal(newSVpvs("Bool"));
355 68 ▶ if (base_len >= 7 && base_len <= 10 && memcmp(s, "Decimal", 7) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (base_len >= 7 && base_len <= 10 && memcmp(s, "Decimal", 7) == 0) { 6 62 68
if (base_len >= 7 && base_len <= 10 && memcmp(s, "Decimal", 7) == 0) { 6 0 6
if (base_len >= 7 && base_len <= 10 && memcmp(s, "Decimal", 7) == 0) { 6 0 6
356 9 ▶ int precision = t->code == T_DECIMAL32 ? 9 : t->code == T_DECIMAL64 ? 18
Branch: 1 branch
BranchTrueFalseTotal
int precision = t->code == T_DECIMAL32 ? 9 : t->code == T_DECIMAL64 ? 18 3 3 6
357 3 ▶ : t->code == T_DECIMAL128 ? 38 : 76;
Branch: 2 branches
BranchTrueFalseTotal
: t->code == T_DECIMAL128 ? 38 : 76; 0 3 3
: t->code == T_DECIMAL128 ? 38 : 76; 0 0 0
358 6 ▶ if (base_len == 7) precision = parse_int_param(open + 1, len - base_len - 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (base_len == 7) precision = parse_int_param(open + 1, len - base_len - 1, 1); 0 6 6
359 6 return sv_2mortal(newSVpvf("Decimal(%d, %d)", precision, t->param));
360 }
361 62 SV *out = sv_2mortal(newSVpvn(s, base_len));
362 62 ▶ if (!open) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!open) return out; 60 2 62
363 2 ▶ if (base_len == 8 && memcmp(s, "DateTime", 8) == 0 && t->code == T_DATETIME64)
Branch: 3 branches
BranchTrueFalseTotal
if (base_len == 8 && memcmp(s, "DateTime", 8) == 0 && t->code == T_DATETIME64) 0 2 2
if (base_len == 8 && memcmp(s, "DateTime", 8) == 0 && t->code == T_DATETIME64) 0 0 0
if (base_len == 8 && memcmp(s, "DateTime", 8) == 0 && t->code == T_DATETIME64) 0 0 0
364 0 sv_setpvs(out, "DateTime64");
365 2 sv_catpvs(out, "(");
366 2 const char *body = open + 1;
367 2 STRLEN body_len = len - base_len - 2;
368 2 ▶ if (t->inner && (t->code == T_ARRAY || t->code == T_NULLABLE || t->code == T_LOWCARDINALITY)) {
Branch: 4 branches
BranchTrueFalseTotal
if (t->inner && (t->code == T_ARRAY || t->code == T_NULLABLE || t->code == T_LOWCARDINALITY)) { 2 0 2
if (t->inner && (t->code == T_ARRAY || t->code == T_NULLABLE || t->code == T_LOWCARDINALITY)) { 0 2 2
if (t->inner && (t->code == T_ARRAY || t->code == T_NULLABLE || t->code == T_LOWCARDINALITY)) { 0 0 0
if (t->inner && (t->code == T_ARRAY || t->code == T_NULLABLE || t->code == T_LOWCARDINALITY)) { 0 0 0
369 2 sv_catsv(out, canonical_variant_type(aTHX_ body, body_len, t->inner));
370 0 ▶ } else if (t->code == T_TUPLE || t->code == T_MAP) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (t->code == T_TUPLE || t->code == T_MAP) { 0 0 0
} else if (t->code == T_TUPLE || t->code == T_MAP) { 0 0 0
371 /* Geo aliases have no '(' and retain their public name. */
372 TypeBound *bounds;
373 0 ▶ Newx(bounds, type_list_capacity(body, body_len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newx(bounds, type_list_capacity(body, body_len), TypeBound); 0 0 0
374 0 SAVEFREEPV(bounds);
375 0 int n = split_type_list(aTHX_ body, body_len, bounds), i;
376 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
377 0 ▶ if (i) sv_catpvs(out, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(out, ", "); 0 0 0
378 0 ▶ if (bounds[i].name_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (bounds[i].name_len) { 0 0 0
379 0 sv_catpvn(out, body + bounds[i].name_start, bounds[i].name_len);
380 0 sv_catpvs(out, " ");
381 }
382 0 sv_catsv(out, canonical_variant_type(aTHX_ body + bounds[i].start,
383 bounds[i].len, t->tuple[i]));
384 }
385 } else {
386 STRLEN i;
387 0 ▶ for (i = 0; i < body_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < body_len; i++) { 0 0 0
388 0 char c = body[i];
389 0 ▶ if (c == '\'' || c == '`') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\'' || c == '`') { 0 0 0
if (c == '\'' || c == '`') { 0 0 0
390 0 STRLEN end = skip_type_quote(aTHX_ body, body_len, i);
391 0 sv_catpvn(out, body + i, end - i); i = end - 1;
392 0 ▶ } else if (!isSPACE(c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!isSPACE(c)) { 0 0 0
393 0 sv_catpvn(out, &c, 1);
394 0 ▶ if (c == ',') sv_catpvs(out, " ");
Branch: 1 branch
BranchTrueFalseTotal
if (c == ',') sv_catpvs(out, " "); 0 0 0
395 }
396 }
397 }
398 2 sv_catpvs(out, ")");
399 2 return out;
400 }
401  
402 /* Return 1 if this type can be used as a JSON typed path. CH writes
403 * typed paths as a regular column; types whose serialization has a
404 * non-empty state-prefix stream (Variant: mode byte; LC: version +
405 * flags + dict; JSON/Dynamic: their own prefix) would interleave
406 * incorrectly with other paths' prefixes in the Object prefix
407 * section. Composites recursively check. */
408 79 static int type_can_be_typed_path(TypeInfo *t) {
409 79 switch (t->code) {
410 6 case T_VARIANT:
411 case T_LOWCARDINALITY:
412 case T_JSON:
413 case T_DYNAMIC:
414 6 return 0;
415 8 case T_ARRAY:
416 case T_NULLABLE:
417 8 return type_can_be_typed_path(t->inner);
418 8 case T_TUPLE:
419 case T_MAP: {
420 int i;
421 24 ▶ for (i = 0; i < t->tuple_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->tuple_len; i++) 16 8 24
422 16 ▶ if (!type_can_be_typed_path(t->tuple[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!type_can_be_typed_path(t->tuple[i])) 0 16 16
423 0 return 0;
424 8 return 1;
425 }
426 57 default:
427 57 return 1;
428 }
429 }
430  
431 /* Parse "name Type, name Type, ..." inside JSON(...). Names may include
432 * dots (CH typed paths are dotted, like JSON(user.id UInt64)); type is
433 * a full type expression. Stores parsed entries on t in name-sorted
434 * order via tuple_names + tuple. Empty body (JSON()) is a no-op. */
435 46 static void parse_json_typed_paths(pTHX_ TypeInfo *t,
436 const char *body, STRLEN body_len,
437 int depth) {
438 TypeBound *bounds;
439 46 ▶ Newxz(bounds, type_list_capacity(body, body_len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(bounds, type_list_capacity(body, body_len), TypeBound); 0 46 46
440 46 SAVEFREEPV(bounds);
441  
442 46 int idx = 0;
443 46 int paren_depth = 0; /* not `depth`, which is the type-nesting level */
444 46 STRLEN start = 0, i;
445 953 ▶ for (i = 0; i <= body_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= body_len; i++) { 910 43 953
446 910 ▶ char c = (i < body_len) ? body[i] : ',';
Branch: 1 branch
BranchTrueFalseTotal
char c = (i < body_len) ? body[i] : ','; 864 46 910
447 910 ▶ if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ body, body_len, i) - 1; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ body, body_len, i) - 1; continue; } 910 0 910
if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ body, body_len, i) - 1; continue; } 3 907 910
448 907 ▶ if (c == '(') paren_depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') paren_depth++; 20 887 907
449 887 ▶ else if (c == ')') paren_depth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ')') paren_depth--; 20 867 887
450 867 ▶ else if ((c == ',' && paren_depth == 0) || i == body_len) {
Branch: 3 branches
BranchTrueFalseTotal
else if ((c == ',' && paren_depth == 0) || i == body_len) { 72 795 867
else if ((c == ',' && paren_depth == 0) || i == body_len) { 10 62 72
else if ((c == ',' && paren_depth == 0) || i == body_len) { 0 805 805
451 62 STRLEN ts = start, te = i;
452 #define J_WS(c2) ((c2)==' '||(c2)=='\t'||(c2)=='\n'||(c2)=='\r')
453 78 ▶ while (ts < te && J_WS(body[ts])) ts++;
Branch: 5 branches
BranchTrueFalseTotal
while (ts < te && J_WS(body[ts])) ts++; 76 2 78
while (ts < te && J_WS(body[ts])) ts++; 16 60 76
while (ts < te && J_WS(body[ts])) ts++; 0 60 60
while (ts < te && J_WS(body[ts])) ts++; 0 60 60
while (ts < te && J_WS(body[ts])) ts++; 0 60 60
454 62 ▶ while (te > ts && J_WS(body[te-1])) te--;
Branch: 5 branches
BranchTrueFalseTotal
while (te > ts && J_WS(body[te-1])) te--; 60 2 62
while (te > ts && J_WS(body[te-1])) te--; 0 60 60
while (te > ts && J_WS(body[te-1])) te--; 0 60 60
while (te > ts && J_WS(body[te-1])) te--; 0 60 60
while (te > ts && J_WS(body[te-1])) te--; 0 60 60
455 62 ▶ if (te > ts) {
Branch: 1 branch
BranchTrueFalseTotal
if (te > ts) { 60 2 62
456 60 STRLEN id = ts;
457 60 ▶ if (body[id] == '`') {
Branch: 1 branch
BranchTrueFalseTotal
if (body[id] == '`') { 3 57 60
458 3 id = skip_type_quote(aTHX_ body, te, id);
459 57 ▶ } else if (body[id] == '_'
Branch: 1 branch
BranchTrueFalseTotal
} else if (body[id] == '_' 57 0 57
460 57 ▶ || (body[id] >= 'A' && body[id] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'A' && body[id] <= 'Z') 57 0 57
|| (body[id] >= 'A' && body[id] <= 'Z') 57 0 57
461 57 ▶ || (body[id] >= 'a' && body[id] <= 'z')) {
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'a' && body[id] <= 'z')) { 57 0 57
|| (body[id] >= 'a' && body[id] <= 'z')) { 57 0 57
462 57 id++;
463 57 while (id < te
464 193 ▶ && (body[id] == '_' || body[id] == '.'
Branch: 3 branches
BranchTrueFalseTotal
&& (body[id] == '_' || body[id] == '.' 192 1 193
&& (body[id] == '_' || body[id] == '.' 0 192 192
&& (body[id] == '_' || body[id] == '.' 7 185 192
465 185 ▶ || (body[id] >= 'A' && body[id] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'A' && body[id] <= 'Z') 129 56 185
|| (body[id] >= 'A' && body[id] <= 'Z') 0 129 129
466 185 ▶ || (body[id] >= 'a' && body[id] <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'a' && body[id] <= 'z') 129 56 185
|| (body[id] >= 'a' && body[id] <= 'z') 129 0 129
467 56 ▶ || (body[id] >= '0' && body[id] <= '9')))
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= '0' && body[id] <= '9'))) 0 56 56
|| (body[id] >= '0' && body[id] <= '9'))) 0 0 0
468 136 id++;
469 }
470 60 ▶ if (id == ts)
Branch: 1 branch
BranchTrueFalseTotal
if (id == ts) 0 60 60
471 0 croak("JSON(...): missing path name in '%.*s'",
472 (int)(te - ts), body + ts);
473 /* Reject trailing dot and consecutive dots in path names:
474 * "a.", "a..b", ".a" (the leading dot is already caught
475 * by the start-char rule). CH itself allows only well-
476 * formed dotted identifiers; mirror that. */
477 60 ▶ if (body[id - 1] == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (body[id - 1] == '.') 1 59 60
478 1 croak("JSON(...): path name must not end with '.' "
479 "in '%.*s'",
480 (int)(id - ts), body + ts);
481 STRLEN dk;
482 198 ▶ for (dk = ts + 1; dk < id; dk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (dk = ts + 1; dk < id; dk++) { 140 58 198
483 140 ▶ if (body[dk] == '.' && body[dk - 1] == '.')
Branch: 2 branches
BranchTrueFalseTotal
if (body[dk] == '.' && body[dk - 1] == '.') 6 134 140
if (body[dk] == '.' && body[dk - 1] == '.') 1 5 6
484 1 croak("JSON(...): path name must not contain "
485 "consecutive dots in '%.*s'",
486 (int)(id - ts), body + ts);
487 }
488 58 STRLEN ws = id;
489 115 ▶ while (ws < te && J_WS(body[ws])) ws++;
Branch: 5 branches
BranchTrueFalseTotal
while (ws < te && J_WS(body[ws])) ws++; 114 1 115
while (ws < te && J_WS(body[ws])) ws++; 57 57 114
while (ws < te && J_WS(body[ws])) ws++; 0 57 57
while (ws < te && J_WS(body[ws])) ws++; 0 57 57
while (ws < te && J_WS(body[ws])) ws++; 0 57 57
490 58 ▶ if (ws == id || ws == te)
Branch: 2 branches
BranchTrueFalseTotal
if (ws == id || ws == te) 57 1 58
if (ws == id || ws == te) 0 57 57
491 1 croak("JSON(...): expected 'name Type' but got '%.*s'",
492 (int)(te - ts), body + ts);
493 57 bounds[idx].name_start = ts;
494 57 bounds[idx].name_len = id - ts;
495 57 bounds[idx].start = ws;
496 57 bounds[idx].len = te - ws;
497 57 idx++;
498 }
499 #undef J_WS
500 59 start = i + 1;
501 }
502 }
503 43 ▶ if (idx == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (idx == 0) return; 2 41 43
504 41 int n = idx, ii, j;
505 98 ▶ for (ii = 0; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < n; ii++) { 57 41 98
506 114 bounds[ii].name = copy_type_name(aTHX_ body + bounds[ii].name_start,
507 57 bounds[ii].name_len);
508 57 SAVEFREEPV(bounds[ii].name);
509 }
510 57 ▶ for (ii = 1; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 1; ii < n; ii++) { 16 41 57
511 16 TypeBound key = bounds[ii];
512 16 j = ii - 1;
513 27 ▶ while (j >= 0 && strcmp(bounds[j].name, key.name) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (j >= 0 && strcmp(bounds[j].name, key.name) > 0) { 16 11 27
while (j >= 0 && strcmp(bounds[j].name, key.name) > 0) { 11 5 16
514 11 bounds[j+1] = bounds[j];
515 11 j--;
516 }
517 16 bounds[j+1] = key;
518 }
519 56 ▶ for (ii = 1; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 1; ii < n; ii++) { 16 40 56
520 16 ▶ if (strcmp(bounds[ii].name, bounds[ii-1].name) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(bounds[ii].name, bounds[ii-1].name) == 0) 1 15 16
521 1 croak("JSON(...): duplicate typed path name '%s'", bounds[ii].name);
522 }
523 40 t->tuple_len = n;
524 40 Newxz(t->tuple_names, n, char*);
525 95 ▶ for (ii = 0; ii < n; ii++) t->tuple_names[ii] = savepv(bounds[ii].name);
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < n; ii++) t->tuple_names[ii] = savepv(bounds[ii].name); 55 40 95
526 40 t->tuple = parse_tuple_types_with_bounds(aTHX_ body, bounds, n, depth);
527  
528 89 ▶ for (ii = 0; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < n; ii++) { 55 34 89
529 55 ▶ if (!type_can_be_typed_path(t->tuple[ii]))
Branch: 1 branch
BranchTrueFalseTotal
if (!type_can_be_typed_path(t->tuple[ii])) 6 49 55
530 6 croak("JSON(%s ...): typed path inner type cannot include "
531 "Variant, LowCardinality, JSON, or Dynamic (those have "
532 "wire prefixes that would interleave incorrectly)",
533 t->tuple_names[ii]);
534 }
535 }
536  
537 31768 TypeInfo* parse_type(pTHX_ const char *type, STRLEN len) {
538 31768 return parse_type_at(aTHX_ type, len, 0);
539 }
540  
541 36951 static TypeInfo* parse_type_at(pTHX_ const char *type, STRLEN len, int depth) {
542 TypeInfo *t;
543 /* Slot lives on the heap so its address is stable across the XSUB lifetime. */
544 TypeInfo **slot;
545  
546 36951 ▶ if (++depth > CHE_MAX_TYPE_DEPTH)
Branch: 1 branch
BranchTrueFalseTotal
if (++depth > CHE_MAX_TYPE_DEPTH) 2 36949 36951
547 2 ▶ croak("Type nesting too deep (limit %d): %.*s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Type nesting too deep (limit %d): %.*s", 0 2 2
548 CHE_MAX_TYPE_DEPTH, (int)(len > 60 ? 60 : len), type);
549  
550 36949 ▶ while (len && isSPACE(type[0])) { type++; len--; }
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(type[0])) { type++; len--; } 36949 0 36949
while (len && isSPACE(type[0])) { type++; len--; } 0 36949 36949
551 36949 ▶ while (len && isSPACE(type[len-1])) len--;
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(type[len-1])) len--; 36949 0 36949
while (len && isSPACE(type[len-1])) len--; 0 36949 36949
552 36949 ▶ if (memchr(type, '(', len) && (!len || type[len-1] != ')'))
Branch: 3 branches
BranchTrueFalseTotal
if (memchr(type, '(', len) && (!len || type[len-1] != ')')) 6452 30497 36949
if (memchr(type, '(', len) && (!len || type[len-1] != ')')) 6452 0 6452
if (memchr(type, '(', len) && (!len || type[len-1] != ')')) 0 6452 6452
553 0 croak("Unbalanced type expression: %.*s", (int)len, type);
554 36949 Newx(slot, 1, TypeInfo*);
555 36949 *slot = NULL;
556 36949 SAVEDESTRUCTOR_X(cleanup_typeinfo_slot, slot);
557 36949 Newxz(t, 1, TypeInfo);
558 36949 *slot = t;
559  
560 36949 ▶ if (len == 4 && strncmp(type, "Int8", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 4 && strncmp(type, "Int8", 4) == 0) { 603 36346 36949
if (len == 4 && strncmp(type, "Int8", 4) == 0) { 20 583 603
561 20 t->code = T_INT8;
562 36929 ▶ } else if (len == 5 && strncmp(type, "Int16", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Int16", 5) == 0) { 1944 34985 36929
} else if (len == 5 && strncmp(type, "Int16", 5) == 0) { 15 1929 1944
563 15 t->code = T_INT16;
564 36914 ▶ } else if (len == 5 && strncmp(type, "Int32", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Int32", 5) == 0) { 1929 34985 36914
} else if (len == 5 && strncmp(type, "Int32", 5) == 0) { 1509 420 1929
565 1509 t->code = T_INT32;
566 35405 ▶ } else if (len == 5 && strncmp(type, "Int64", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Int64", 5) == 0) { 420 34985 35405
} else if (len == 5 && strncmp(type, "Int64", 5) == 0) { 31 389 420
567 31 t->code = T_INT64;
568 35374 ▶ } else if (len == 5 && strncmp(type, "UInt8", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "UInt8", 5) == 0) { 389 34985 35374
} else if (len == 5 && strncmp(type, "UInt8", 5) == 0) { 379 10 389
569 379 t->code = T_UINT8;
570 34995 ▶ } else if (len == 6 && strncmp(type, "UInt16", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "UInt16", 6) == 0) { 27169 7826 34995
} else if (len == 6 && strncmp(type, "UInt16", 6) == 0) { 11 27158 27169
571 11 t->code = T_UINT16;
572 34984 ▶ } else if (len == 6 && strncmp(type, "UInt32", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "UInt32", 6) == 0) { 27158 7826 34984
} else if (len == 6 && strncmp(type, "UInt32", 6) == 0) { 234 26924 27158
573 234 t->code = T_UINT32;
574 34750 ▶ } else if (len == 6 && strncmp(type, "UInt64", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "UInt64", 6) == 0) { 26924 7826 34750
} else if (len == 6 && strncmp(type, "UInt64", 6) == 0) { 53 26871 26924
575 53 t->code = T_UINT64;
576 34697 ▶ } else if (len == 7 && strncmp(type, "Float32", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Float32", 7) == 0) { 405 34292 34697
} else if (len == 7 && strncmp(type, "Float32", 7) == 0) { 12 393 405
577 12 t->code = T_FLOAT32;
578 34685 ▶ } else if (len == 7 && strncmp(type, "Float64", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Float64", 7) == 0) { 393 34292 34685
} else if (len == 7 && strncmp(type, "Float64", 7) == 0) { 359 34 393
579 359 t->code = T_FLOAT64;
580 34326 ▶ } else if (len == 8 && strncmp(type, "BFloat16", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 8 && strncmp(type, "BFloat16", 8) == 0) { 376 33950 34326
} else if (len == 8 && strncmp(type, "BFloat16", 8) == 0) { 12 364 376
581 12 t->code = T_BFLOAT16;
582 34314 ▶ } else if (len == 6 && strncmp(type, "String", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "String", 6) == 0) { 26871 7443 34314
} else if (len == 6 && strncmp(type, "String", 6) == 0) { 26851 20 26871
583 26851 t->code = T_STRING;
584 7463 ▶ } else if (len > 12 && strncmp(type, "FixedString(", 12) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 12 && strncmp(type, "FixedString(", 12) == 0) { 5178 2285 7463
} else if (len > 12 && strncmp(type, "FixedString(", 12) == 0) { 34 5144 5178
585 34 t->code = T_FIXEDSTRING;
586 34 t->param = parse_int_param(type + 12, len - 12, 0);
587 34 ▶ if (t->param <= 0) croak("FixedString needs positive length");
Branch: 1 branch
BranchTrueFalseTotal
if (t->param <= 0) croak("FixedString needs positive length"); 3 31 34
588 /* ClickHouse's MAX_FIXED_STRING_SIZE; beyond it the server itself
589 * refuses the type, so no real schema reaches here. */
590 31 ▶ if (t->param > 0xFFFFFF)
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 0xFFFFFF) 0 31 31
591 0 croak("FixedString length %d exceeds the maximum of %d",
592 t->param, 0xFFFFFF);
593 7429 ▶ } else if (len > 6 && strncmp(type, "Array(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 6 && strncmp(type, "Array(", 6) == 0) { 6816 613 7429
} else if (len > 6 && strncmp(type, "Array(", 6) == 0) { 1566 5250 6816
594 1566 t->code = T_ARRAY;
595 1566 t->inner = parse_type_at(aTHX_ type + 6, len - 7, depth);
596 6923 ▶ } else if (len > 6 && strncmp(type, "Tuple(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 6 && strncmp(type, "Tuple(", 6) == 0) { 5250 613 5863
} else if (len > 6 && strncmp(type, "Tuple(", 6) == 0) { 1060 4190 5250
597 1060 t->code = T_TUPLE;
598 1060 const char *body = type + 6;
599 1060 STRLEN body_len = len - 7;
600 TypeBound *bounds;
601 1060 ▶ Newx(bounds, type_list_capacity(body, body_len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newx(bounds, type_list_capacity(body, body_len), TypeBound); 0 1060 1060
602 1060 SAVEFREEPV(bounds);
603 1060 t->tuple_len = split_type_list(aTHX_ body, body_len, bounds);
604 1060 t->tuple = parse_tuple_types_with_bounds(aTHX_ body, bounds,
605 t->tuple_len, depth);
606 /* If at least one element carries a field-name, capture all of
607 * them so encode_column can accept hashrefs for this tuple. A
608 * mix of named and unnamed elements isn't legal in ClickHouse;
609 * we accept any element having a name as "named tuple". */
610 1060 int has_names = 0;
611 int j;
612 3162 ▶ for (j = 0; j < t->tuple_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len; j++) { 2109 1053 3162
613 2109 ▶ if (bounds[j].name_len > 0) { has_names = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (bounds[j].name_len > 0) { has_names = 1; break; } 7 2102 2109
614 }
615 1060 ▶ if (has_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_names) { 7 1053 1060
616 7 Newxz(t->tuple_names, t->tuple_len, char *);
617 20 ▶ for (j = 0; j < t->tuple_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len; j++) { 13 7 20
618 13 ▶ if (bounds[j].name_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bounds[j].name_len > 0) { 13 0 13
619 13 t->tuple_names[j] = copy_type_name(aTHX_ body + bounds[j].name_start, bounds[j].name_len);
620 }
621 /* else: leave NULL -- mixed named/unnamed not really
622 * supported; encode will croak if hashref is used. */
623 }
624 }
625 4803 ▶ } else if (len > 9 && strncmp(type, "Nullable(", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 9 && strncmp(type, "Nullable(", 9) == 0) { 3794 1009 4803
} else if (len > 9 && strncmp(type, "Nullable(", 9) == 0) { 1158 2636 3794
626 1158 ▶ if (len > 18 && strncmp(type + 9, "Nullable(", 9) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (len > 18 && strncmp(type + 9, "Nullable(", 9) == 0) 42 1116 1158
if (len > 18 && strncmp(type + 9, "Nullable(", 9) == 0) 2 40 42
627 2 croak("Nullable(Nullable(...)) is not allowed");
628 1156 t->code = T_NULLABLE;
629 1156 t->inner = parse_type_at(aTHX_ type + 9, len - 10, depth);
630 3645 ▶ } else if (len > 6 && strncmp(type, "Enum8(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 6 && strncmp(type, "Enum8(", 6) == 0) { 3032 613 3645
} else if (len > 6 && strncmp(type, "Enum8(", 6) == 0) { 1069 1963 3032
631 1069 t->code = T_ENUM8;
632 1069 parse_enum_entries(aTHX_ t, type + 6, len - 7, T_ENUM8);
633 2576 ▶ } else if (len > 7 && strncmp(type, "Enum16(", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 7 && strncmp(type, "Enum16(", 7) == 0) { 1932 644 2576
} else if (len > 7 && strncmp(type, "Enum16(", 7) == 0) { 57 1875 1932
634 57 t->code = T_ENUM16;
635 57 parse_enum_entries(aTHX_ t, type + 7, len - 8, T_ENUM16);
636 2519 ▶ } else if (len > 10 && strncmp(type, "Decimal32(", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 10 && strncmp(type, "Decimal32(", 10) == 0) { 1507 1012 2519
} else if (len > 10 && strncmp(type, "Decimal32(", 10) == 0) { 15 1492 1507
637 15 t->code = T_DECIMAL32;
638 15 t->precision = 9;
639 15 t->param = parse_int_param(type + 10, len - 10, 0);
640 15 ▶ if (t->param < 0 || t->param > 9)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 9) 15 0 15
if (t->param < 0 || t->param > 9) 1 14 15
641 1 croak("Decimal32 scale must be 0..9, got %d", t->param);
642 2504 ▶ } else if (len > 10 && strncmp(type, "Decimal64(", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 10 && strncmp(type, "Decimal64(", 10) == 0) { 1492 1012 2504
} else if (len > 10 && strncmp(type, "Decimal64(", 10) == 0) { 1022 470 1492
643 1022 t->code = T_DECIMAL64;
644 1022 t->precision = 18;
645 1022 t->param = parse_int_param(type + 10, len - 10, 0);
646 1022 ▶ if (t->param < 0 || t->param > 18)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 18) 1022 0 1022
if (t->param < 0 || t->param > 18) 0 1022 1022
647 0 croak("Decimal64 scale must be 0..18, got %d", t->param);
648 1482 ▶ } else if (len > 11 && strncmp(type, "Decimal128(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 11 && strncmp(type, "Decimal128(", 11) == 0) { 467 1015 1482
} else if (len > 11 && strncmp(type, "Decimal128(", 11) == 0) { 48 419 467
649 48 t->code = T_DECIMAL128;
650 48 t->precision = 38;
651 48 t->param = parse_int_param(type + 11, len - 11, 0);
652 48 ▶ if (t->param < 0 || t->param > 38)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 38) 48 0 48
if (t->param < 0 || t->param > 38) 0 48 48
653 0 croak("Decimal128 scale must be 0..38, got %d", t->param);
654 1434 ▶ } else if (len > 11 && strncmp(type, "Decimal256(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 11 && strncmp(type, "Decimal256(", 11) == 0) { 419 1015 1434
} else if (len > 11 && strncmp(type, "Decimal256(", 11) == 0) { 70 349 419
655 70 t->code = T_DECIMAL256;
656 70 t->precision = 76;
657 70 t->param = parse_int_param(type + 11, len - 11, 0);
658 70 ▶ if (t->param < 0 || t->param > 76)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 76) 70 0 70
if (t->param < 0 || t->param > 76) 0 70 70
659 0 croak("Decimal256 scale must be 0..76, got %d", t->param);
660 1370 ▶ } else if (len > 8 && strncmp(type, "Decimal(", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 8 && strncmp(type, "Decimal(", 8) == 0) { 356 1008 1364
} else if (len > 8 && strncmp(type, "Decimal(", 8) == 0) { 7 349 356
661 7 int precision = parse_int_param(type + 8, len - 8, 1);
662 7 const char *comma = memchr(type + 8, ',', len - 8);
663 7 ▶ if (!comma) croak("Decimal(P, S) requires precision and scale");
Branch: 1 branch
BranchTrueFalseTotal
if (!comma) croak("Decimal(P, S) requires precision and scale"); 0 7 7
664 7 int scale = parse_int_param(comma + 1, len - (STRLEN)(comma + 1 - type), 0);
665 7 ▶ if (precision < 1 || precision > 76)
Branch: 2 branches
BranchTrueFalseTotal
if (precision < 1 || precision > 76) 6 1 7
if (precision < 1 || precision > 76) 0 6 6
666 1 croak("Decimal(P, S) precision must be 1..76, got %d", precision);
667 6 ▶ if (scale < 0 || scale > precision)
Branch: 2 branches
BranchTrueFalseTotal
if (scale < 0 || scale > precision) 6 0 6
if (scale < 0 || scale > precision) 0 6 6
668 0 croak("Decimal scale must be 0..precision, got %d", scale);
669 6 t->param = scale;
670 6 t->precision = precision;
671 6 ▶ if (precision <= 9) t->code = T_DECIMAL32;
Branch: 1 branch
BranchTrueFalseTotal
if (precision <= 9) t->code = T_DECIMAL32; 3 3 6
672 3 ▶ else if (precision <= 18) t->code = T_DECIMAL64;
Branch: 1 branch
BranchTrueFalseTotal
else if (precision <= 18) t->code = T_DECIMAL64; 1 2 3
673 2 ▶ else if (precision <= 38) t->code = T_DECIMAL128;
Branch: 1 branch
BranchTrueFalseTotal
else if (precision <= 38) t->code = T_DECIMAL128; 1 1 2
674 1 else t->code = T_DECIMAL256;
675 1357 ▶ } else if (len == 4 && strncmp(type, "Date", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "Date", 4) == 0) { 583 774 1357
} else if (len == 4 && strncmp(type, "Date", 4) == 0) { 24 559 583
676 24 t->code = T_DATE;
677 1333 ▶ } else if (len == 6 && strncmp(type, "Date32", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "Date32", 6) == 0) { 20 1313 1333
} else if (len == 6 && strncmp(type, "Date32", 6) == 0) { 18 2 20
678 18 t->code = T_DATE32;
679 1315 ▶ } else if (len == 8 && strncmp(type, "DateTime", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 8 && strncmp(type, "DateTime", 8) == 0) { 364 951 1315
} else if (len == 8 && strncmp(type, "DateTime", 8) == 0) { 364 0 364
680 364 t->code = T_DATETIME;
681 1029 ▶ } else if (len > 9 && strncmp(type, "DateTime(", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 9 && strncmp(type, "DateTime(", 9) == 0) { 348 603 951
} else if (len > 9 && strncmp(type, "DateTime(", 9) == 0) { 78 270 348
682 78 const char *arg = type + 9;
683 78 STRLEN arg_len = len - 9;
684 78 ▶ while (arg_len && isSPACE(*arg)) { arg++; arg_len--; }
Branch: 2 branches
BranchTrueFalseTotal
while (arg_len && isSPACE(*arg)) { arg++; arg_len--; } 78 0 78
while (arg_len && isSPACE(*arg)) { arg++; arg_len--; } 0 78 78
685 78 ▶ if (arg_len && *arg >= '0' && *arg <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
if (arg_len && *arg >= '0' && *arg <= '9') { 78 0 78
if (arg_len && *arg >= '0' && *arg <= '9') { 5 73 78
if (arg_len && *arg >= '0' && *arg <= '9') { 5 0 5
686 5 t->param = parse_int_param(arg, arg_len, 1);
687 5 ▶ if (t->param < 0 || t->param > 9) croak("DateTime precision must be 0..9");
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 9) croak("DateTime precision must be 0..9"); 5 0 5
if (t->param < 0 || t->param > 9) croak("DateTime precision must be 0..9"); 0 5 5
688 5 ▶ t->code = t->param ? T_DATETIME64 : T_DATETIME;
Branch: 1 branch
BranchTrueFalseTotal
t->code = t->param ? T_DATETIME64 : T_DATETIME; 3 2 5
689 73 } else t->code = T_DATETIME;
690 873 ▶ } else if (len > 11 && strncmp(type, "DateTime64(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 11 && strncmp(type, "DateTime64(", 11) == 0) { 266 607 873
} else if (len > 11 && strncmp(type, "DateTime64(", 11) == 0) { 49 217 266
691 49 t->code = T_DATETIME64;
692 49 t->param = parse_int_param(type + 11, len - 11, 1);
693 49 ▶ if (t->param < 0 || t->param > 9)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 9) 49 0 49
if (t->param < 0 || t->param > 9) 1 48 49
694 1 croak("DateTime64 precision must be 0..9, got %d", t->param);
695 824 ▶ } else if (len == 4 && strncmp(type, "Bool", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "Bool", 4) == 0) { 559 265 824
} else if (len == 4 && strncmp(type, "Bool", 4) == 0) { 316 243 559
696 316 t->code = T_BOOL;
697 508 ▶ } else if (len == 7 && strncmp(type, "Boolean", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Boolean", 7) == 0) { 31 477 508
} else if (len == 7 && strncmp(type, "Boolean", 7) == 0) { 0 31 31
698 0 t->code = T_BOOL;
699 508 ▶ } else if (len == 4 && strncmp(type, "UUID", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "UUID", 4) == 0) { 243 265 508
} else if (len == 4 && strncmp(type, "UUID", 4) == 0) { 72 171 243
700 72 t->code = T_UUID;
701 436 ▶ } else if (len == 4 && strncmp(type, "IPv4", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "IPv4", 4) == 0) { 171 265 436
} else if (len == 4 && strncmp(type, "IPv4", 4) == 0) { 25 146 171
702 25 t->code = T_IPV4;
703 411 ▶ } else if (len == 4 && strncmp(type, "IPv6", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "IPv6", 4) == 0) { 146 265 411
} else if (len == 4 && strncmp(type, "IPv6", 4) == 0) { 39 107 146
704 39 t->code = T_IPV6;
705 373 ▶ } else if (len > 24 && strncmp(type, "SimpleAggregateFunction(", 24) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 24 && strncmp(type, "SimpleAggregateFunction(", 24) == 0) { 66 306 372
} else if (len > 24 && strncmp(type, "SimpleAggregateFunction(", 24) == 0) { 1 65 66
706 /* SimpleAggregateFunction(func, T) is wire-equivalent to T -- the
707 * func name only affects how readers aggregate on read, not how
708 * values are stored. Strip it and parse the rest as the inner type. */
709 1 const char *body = type + 24;
710 1 STRLEN body_len = len - 25;
711 1 const char *comma = memchr(body, ',', body_len);
712 1 ▶ if (!comma) croak("SimpleAggregateFunction requires (func, T)");
Branch: 1 branch
BranchTrueFalseTotal
if (!comma) croak("SimpleAggregateFunction requires (func, T)"); 0 1 1
713 1 STRLEN inner_off = (comma - body) + 1;
714 2 ▶ while (inner_off < body_len && body[inner_off] == ' ') inner_off++;
Branch: 2 branches
BranchTrueFalseTotal
while (inner_off < body_len && body[inner_off] == ' ') inner_off++; 2 0 2
while (inner_off < body_len && body[inner_off] == ' ') inner_off++; 1 1 2
715 1 TypeInfo *inner = parse_type_at(aTHX_ body + inner_off,
716 body_len - inner_off, depth);
717 /* Steal inner's contents in one shot. The outer slot still owns t; the
718 * inner's slot was already disarmed before parse_type returned, so we
719 * can free the now-redundant inner struct directly. */
720 1 *t = *inner;
721 1 Safefree(inner);
722 403 ▶ } else if (len > 8 && strncmp(type, "Variant(", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 8 && strncmp(type, "Variant(", 8) == 0) { 221 150 371
} else if (len > 8 && strncmp(type, "Variant(", 8) == 0) { 33 188 221
723 /* Variant(T1, T2, ...) - tagged union. Each input row is either
724 * undef (NULL) or [$variant_idx, $value]. ClickHouse stores
725 * Variant sub-columns and per-row discriminators in alphabetical
726 * order of variant type names, not declaration order, so build
727 * a permutation that maps the user's declaration index to the
728 * wire (alphabetical) position. */
729 33 t->code = T_VARIANT;
730 33 const char *body = type + 8;
731 33 STRLEN body_len = len - 9;
732  
733 /* Split once, then share the bounds with parse_tuple_types_with_bounds
734 * (the alphabetical sort and the parsed TypeInfo entries reference
735 * the same ranges). */
736 TypeBound *bounds;
737 33 ▶ Newx(bounds, type_list_capacity(body, body_len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newx(bounds, type_list_capacity(body, body_len), TypeBound); 0 33 33
738 33 SAVEFREEPV(bounds);
739 33 t->tuple_len = split_type_list(aTHX_ body, body_len, bounds);
740 33 ▶ if (t->tuple_len < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_len < 1) 1 32 33
741 1 croak("Variant requires at least one type argument");
742 32 ▶ if (t->tuple_len > 254)
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_len > 254) 0 32 32
743 0 croak("Variant supports at most 254 types (got %d)", t->tuple_len);
744 32 t->tuple = parse_tuple_types_with_bounds(aTHX_ body, bounds,
745 t->tuple_len, depth);
746  
747 SV **canonical;
748 32 Newx(canonical, t->tuple_len, SV *);
749 32 SAVEFREEPV(canonical);
750 int vi;
751 98 ▶ for (vi = 0; vi < t->tuple_len; vi++)
Branch: 1 branch
BranchTrueFalseTotal
for (vi = 0; vi < t->tuple_len; vi++) 66 32 98
752 66 canonical[vi] = canonical_variant_type(aTHX_ body + bounds[vi].start,
753 66 bounds[vi].len, t->tuple[vi]);
754  
755 /* Sort declaration indices alphabetically by their type bytes.
756 * Selection sort -- nvar is at most 254. */
757 32 Newx(t->variant_wire_to_decl, t->tuple_len, int);
758 32 Newx(t->variant_decl_to_wire, t->tuple_len, int);
759 int j, k;
760 98 ▶ for (j = 0; j < t->tuple_len; j++) t->variant_wire_to_decl[j] = j;
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len; j++) t->variant_wire_to_decl[j] = j; 66 32 98
761 66 ▶ for (j = 0; j < t->tuple_len - 1; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len - 1; j++) { 34 32 66
762 34 int min_idx = j;
763 70 ▶ for (k = j + 1; k < t->tuple_len; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = j + 1; k < t->tuple_len; k++) { 36 34 70
764 36 int a = t->variant_wire_to_decl[min_idx];
765 36 int b = t->variant_wire_to_decl[k];
766 STRLEN la, lb;
767 72 ▶ const char *sa = SvPV(canonical[a], la), *sb = SvPV(canonical[b], lb);
Branch: 1 branch
BranchTrueFalseTotal
const char *sa = SvPV(canonical[a], la), *sb = SvPV(canonical[b], lb); 36 0 36
768 36 STRLEN cmp_len = la < lb ? la : lb;
769 36 int cmp = memcmp(sa, sb, cmp_len);
770 36 ▶ if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k;
Branch: 3 branches
BranchTrueFalseTotal
if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k; 28 8 36
if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k; 0 28 28
if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k; 0 0 0
771 }
772 34 ▶ if (min_idx != j) {
Branch: 1 branch
BranchTrueFalseTotal
if (min_idx != j) { 8 26 34
773 8 int tmp = t->variant_wire_to_decl[j];
774 8 t->variant_wire_to_decl[j] = t->variant_wire_to_decl[min_idx];
775 8 t->variant_wire_to_decl[min_idx] = tmp;
776 }
777 }
778 98 ▶ for (j = 0; j < t->tuple_len; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len; j++) 66 32 98
779 66 t->variant_decl_to_wire[t->variant_wire_to_decl[j]] = j;
780 338 ▶ } else if (len > 4 && strncmp(type, "Map(", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 4 && strncmp(type, "Map(", 4) == 0) { 231 107 338
} else if (len > 4 && strncmp(type, "Map(", 4) == 0) { 57 174 231
781 /* Map(K, V) is wire-equivalent to Array(Tuple(K, V)). Build the
782 * synthetic structure so encode_column can reuse Array+Tuple paths. */
783 57 t->code = T_MAP;
784 57 t->tuple = parse_tuple_types(aTHX_ type + 4, len - 5, &t->tuple_len, depth);
785 57 ▶ if (t->tuple_len != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_len != 2) 0 57 57
786 0 croak("Map type requires exactly 2 type arguments, got %d", t->tuple_len);
787 281 ▶ } else if (len > 7 && strncmp(type, "Nested(", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 7 && strncmp(type, "Nested(", 7) == 0) { 131 150 281
} else if (len > 7 && strncmp(type, "Nested(", 7) == 0) { 2 129 131
788 /* On the wire, ClickHouse splits a Nested(a T1, b T2) column into
789 * flat columns "<name>.a Array(T1)" and "<name>.b Array(T2)" -- this
790 * encoder does not perform that expansion. Use the flat form
791 * directly in your column spec. */
792 2 croak("Nested(...) is not supported directly; declare flat columns "
793 "like 'name.field' Array(T) instead (CH stores Nested that way "
794 "on the wire). describe table / for_table() returns the flat form.");
795 279 ▶ } else if (len == 7 && strncmp(type, "Dynamic", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Dynamic", 7) == 0) { 31 248 279
} else if (len == 7 && strncmp(type, "Dynamic", 7) == 0) { 27 4 31
796 /* Standalone Dynamic column: same wire machinery as a single
797 * JSON path's Dynamic sub-column. Each row is a scalar leaf
798 * (Bool/Float64/Int64/String), an Array(T) of those, or
799 * undef (NULL). Hashrefs aren't accepted here - use JSON for
800 * object-shaped values. */
801 27 t->code = T_DYNAMIC;
802 252 ▶ } else if ((len == 4 && strncmp(type, "JSON", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
} else if ((len == 4 && strncmp(type, "JSON", 4) == 0) 107 145 252
} else if ((len == 4 && strncmp(type, "JSON", 4) == 0) 3 104 107
803 148 ▶ || (len > 5 && strncmp(type, "JSON(", 5) == 0
Branch: 2 branches
BranchTrueFalseTotal
|| (len > 5 && strncmp(type, "JSON(", 5) == 0 135 13 148
|| (len > 5 && strncmp(type, "JSON(", 5) == 0 46 89 135
804 46 ▶ && type[len-1] == ')')
Branch: 1 branch
BranchTrueFalseTotal
&& type[len-1] == ')') 0 46 46
805 102 ▶ || (len > 7 && strncmp(type, "Object(", 7) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (len > 7 && strncmp(type, "Object(", 7) == 0)) { 86 16 102
|| (len > 7 && strncmp(type, "Object(", 7) == 0)) { 0 86 86
806 /* ClickHouse's stable JSON type (24.8+). Wire layout (V1 over
807 * Native, validated byte-for-byte against the server in
808 * doc/json-research/): Object structure prefix, then for each
809 * path a Dynamic prefix + Variant mode byte, then per-path
810 * Variant data, then a shared-data Array(Tuple(String,String))
811 * trailer. The per-row schema is determined at encode time by
812 * inspecting each value's Perl type. The JSON(name Type, ...)
813 * form pins specific paths to concrete inner types; those
814 * paths skip the Dynamic+Variant wrapping. */
815 150 t->code = T_JSON;
816 150 ▶ if (len > 5 && type[4] == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (len > 5 && type[4] == '(') { 46 104 150
if (len > 5 && type[4] == '(') { 46 0 46
817 46 parse_json_typed_paths(aTHX_ t, type + 5, len - 6, depth);
818 }
819 102 ▶ } else if (len == 5 && strncmp(type, "Point", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Point", 5) == 0) { 10 92 102
} else if (len == 5 && strncmp(type, "Point", 5) == 0) { 10 0 10
820 /* Point = Tuple(Float64, Float64) */
821 10 t->code = T_TUPLE;
822 10 t->tuple = parse_tuple_types(aTHX_ "Float64, Float64", 16,
823 &t->tuple_len, depth);
824 92 ▶ } else if (len == 4 && strncmp(type, "Ring", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "Ring", 4) == 0) { 3 89 92
} else if (len == 4 && strncmp(type, "Ring", 4) == 0) { 3 0 3
825 /* Ring = Array(Point) */
826 3 t->code = T_ARRAY;
827 3 t->inner = parse_type_at(aTHX_ "Point", 5, depth);
828 89 ▶ } else if (len == 10 && strncmp(type, "LineString", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 10 && strncmp(type, "LineString", 10) == 0) { 3 86 89
} else if (len == 10 && strncmp(type, "LineString", 10) == 0) { 2 1 3
829 /* LineString = Array(Point) */
830 2 t->code = T_ARRAY;
831 2 t->inner = parse_type_at(aTHX_ "Point", 5, depth);
832 87 ▶ } else if (len == 15 && strncmp(type, "MultiLineString", 15) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 15 && strncmp(type, "MultiLineString", 15) == 0) { 0 87 87
} else if (len == 15 && strncmp(type, "MultiLineString", 15) == 0) { 0 0 0
833 /* MultiLineString = Array(Array(Point)) */
834 0 t->code = T_ARRAY;
835 0 t->inner = parse_type_at(aTHX_ "Array(Point)", 12, depth);
836 87 ▶ } else if (len == 7 && strncmp(type, "Polygon", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Polygon", 7) == 0) { 3 84 87
} else if (len == 7 && strncmp(type, "Polygon", 7) == 0) { 3 0 3
837 /* Polygon = Array(Ring) */
838 3 t->code = T_ARRAY;
839 3 t->inner = parse_type_at(aTHX_ "Ring", 4, depth);
840 84 ▶ } else if (len == 12 && strncmp(type, "MultiPolygon", 12) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 12 && strncmp(type, "MultiPolygon", 12) == 0) { 2 82 84
} else if (len == 12 && strncmp(type, "MultiPolygon", 12) == 0) { 2 0 2
841 /* MultiPolygon = Array(Polygon) */
842 2 t->code = T_ARRAY;
843 2 t->inner = parse_type_at(aTHX_ "Polygon", 7, depth);
844 161 ▶ } else if (len > 15 && strncmp(type, "LowCardinality(", 15) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 15 && strncmp(type, "LowCardinality(", 15) == 0) { 80 2 82
} else if (len > 15 && strncmp(type, "LowCardinality(", 15) == 0) { 80 0 80
845 80 t->code = T_LOWCARDINALITY;
846 80 t->inner = parse_type_at(aTHX_ type + 15, len - 16, depth);
847 80 ▶ TypeInfo *leaf = t->inner->code == T_NULLABLE ? t->inner->inner : t->inner;
Branch: 1 branch
BranchTrueFalseTotal
TypeInfo *leaf = t->inner->code == T_NULLABLE ? t->inner->inner : t->inner; 24 56 80
848 80 ▶ if (leaf->code > T_IPV6 || leaf->code == T_ARRAY
Branch: 2 branches
BranchTrueFalseTotal
if (leaf->code > T_IPV6 || leaf->code == T_ARRAY 80 0 80
if (leaf->code > T_IPV6 || leaf->code == T_ARRAY 79 1 80
849 79 ▶ || leaf->code == T_TUPLE || leaf->code == T_NULLABLE)
Branch: 2 branches
BranchTrueFalseTotal
|| leaf->code == T_TUPLE || leaf->code == T_NULLABLE) 79 0 79
|| leaf->code == T_TUPLE || leaf->code == T_NULLABLE) 0 79 79
850 1 croak("LowCardinality(T) supports scalar inner types");
851 } else {
852 2 croak("Unknown type: %.*s", (int)len, type);
853 }
854  
855 36721 ▶ if ((t->code == T_DATETIME || t->code == T_DATETIME64) && !t->timezone) {
Branch: 3 branches
BranchTrueFalseTotal
if ((t->code == T_DATETIME || t->code == T_DATETIME64) && !t->timezone) { 36282 439 36721
if ((t->code == T_DATETIME || t->code == T_DATETIME64) && !t->timezone) { 51 36231 36282
if ((t->code == T_DATETIME || t->code == T_DATETIME64) && !t->timezone) { 490 0 490
856 490 const char *quote = memchr(type, '\'', len);
857 490 ▶ if (quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (quote) { 97 393 490
858 97 STRLEN pos = quote - type;
859 97 STRLEN end = skip_type_quote(aTHX_ type, len, pos);
860 97 STRLEN j, used = 0;
861 97 Newx(t->timezone, end - pos, char);
862 678 ▶ for (j = pos + 1; j < end - 1; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = pos + 1; j < end - 1; j++) { 581 97 678
863 581 ▶ if (type[j] == '\\' && j + 1 < end - 1) j++;
Branch: 2 branches
BranchTrueFalseTotal
if (type[j] == '\\' && j + 1 < end - 1) j++; 0 581 581
if (type[j] == '\\' && j + 1 < end - 1) j++; 0 0 0
864 581 t->timezone[used++] = type[j];
865 }
866 97 t->timezone[used] = 0;
867 97 ▶ if (!used || memchr(t->timezone, 0, used)) croak("Invalid timezone name");
Branch: 2 branches
BranchTrueFalseTotal
if (!used || memchr(t->timezone, 0, used)) croak("Invalid timezone name"); 97 0 97
if (!used || memchr(t->timezone, 0, used)) croak("Invalid timezone name"); 0 97 97
868 }
869 }
870 36642 ▶ t->has_prefix = t->code == T_LOWCARDINALITY || t->code == T_VARIANT
Branch: 1 branch
BranchTrueFalseTotal
t->has_prefix = t->code == T_LOWCARDINALITY || t->code == T_VARIANT 36610 32 36642
871 73363 ▶ || t->code == T_JSON || t->code == T_DYNAMIC;
Branch: 3 branches
BranchTrueFalseTotal
|| t->code == T_JSON || t->code == T_DYNAMIC; 36642 79 36721
|| t->code == T_JSON || t->code == T_DYNAMIC; 36470 140 36610
|| t->code == T_JSON || t->code == T_DYNAMIC; 27 36443 36470
872 36721 ▶ if (t->inner && t->inner->has_prefix) t->split_prefix = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (t->inner && t->inner->has_prefix) t->split_prefix = 1; 2611 34110 36721
if (t->inner && t->inner->has_prefix) t->split_prefix = 1; 34 2577 2611
873 int pi;
874 39085 ▶ for (pi = 0; pi < t->tuple_len; pi++)
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < t->tuple_len; pi++) 2364 36721 39085
875 2364 ▶ if (t->tuple[pi]->has_prefix) t->split_prefix = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple[pi]->has_prefix) t->split_prefix = 1; 15 2349 2364
876 36721 t->has_prefix |= t->split_prefix;
877  
878 /* Disarm the slot: caller now owns t. */
879 36721 *slot = NULL;
880 36721 return t;
881 }
« scalar_kind.c ↑ Summary json_kind.c »