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 1243 static int parse_int_param(const char *s, STRLEN maxlen, int allow_comma) {
33 1243 STRLEN i = 0;
34 1243 long v = 0;
35 1250 ▶ while (i < maxlen && (s[i] == ' ' || s[i] == '\t'
Branch: 3 branches
BranchTrueFalseTotal
while (i < maxlen && (s[i] == ' ' || s[i] == '\t' 1250 0 1250
while (i < maxlen && (s[i] == ' ' || s[i] == '\t' 7 1243 1250
while (i < maxlen && (s[i] == ' ' || s[i] == '\t' 0 1243 1243
36 1250 ▶ || s[i] == '\n' || s[i] == '\r')) i++;
Branch: 2 branches
BranchTrueFalseTotal
|| s[i] == '\n' || s[i] == '\r')) i++; 0 1243 1243
|| s[i] == '\n' || s[i] == '\r')) i++; 0 1243 1243
37 1243 ▶ 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; 1243 0 1243
if (i >= maxlen || s[i] < '0' || s[i] > '9') return -1; 1243 0 1243
if (i >= maxlen || s[i] < '0' || s[i] > '9') return -1; 0 1243 1243
38 2509 ▶ for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) {
Branch: 3 branches
BranchTrueFalseTotal
for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) { 2509 0 2509
for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) { 1267 1242 2509
for (; i < maxlen && s[i] >= '0' && s[i] <= '9'; i++) { 1267 0 1267
39 1267 int digit = s[i] - '0';
40 1267 ▶ if (v > (INT_MAX - digit) / 10) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (v > (INT_MAX - digit) / 10) return -1; 1 1266 1267
41 1266 v = v * 10 + digit;
42 }
43 1242 ▶ while (i < maxlen && isSPACE(s[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < maxlen && isSPACE(s[i])) i++; 1242 0 1242
while (i < maxlen && isSPACE(s[i])) i++; 0 1242 1242
44 1242 ▶ 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; 1242 0 1242
if (i == maxlen || (s[i] != ')' && !(allow_comma && s[i] == ','))) return -1; 31 1211 1242
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 1242 ▶ if (s[i] == ')' && i + 1 != maxlen) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == ')' && i + 1 != maxlen) return -1; 1211 31 1242
if (s[i] == ')' && i + 1 != maxlen) return -1; 0 1211 1211
46 1242 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 66 static char *copy_type_name(pTHX_ const char *s, STRLEN len) {
63 char *out;
64 66 Newx(out, len + 1, char);
65 66 STRLEN i = 0, n = 0;
66 66 ▶ if (len && s[0] == '`') { i = 1; len--; }
Branch: 2 branches
BranchTrueFalseTotal
if (len && s[0] == '`') { i = 1; len--; } 66 0 66
if (len && s[0] == '`') { i = 1; len--; } 4 62 66
67 268 ▶ for (; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < len; i++) { 202 66 268
68 202 ▶ if (s[i] == '\\' && i + 1 < len) i++;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == '\\' && i + 1 < len) i++; 0 202 202
if (s[i] == '\\' && i + 1 < len) i++; 0 0 0
69 202 ▶ 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 202 202
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 202 out[n++] = s[i];
71 }
72 66 out[n] = 0;
73 66 return out;
74 }
75  
76 36402 void free_typeinfo(pTHX_ TypeInfo *t) {
77 36402 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 36402 36402
78 36402 ▶ if (t->inner) free_typeinfo(aTHX_ t->inner);
Branch: 1 branch
BranchTrueFalseTotal
if (t->inner) free_typeinfo(aTHX_ t->inner); 2446 33956 36402
79 36402 ▶ if (t->tuple) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple) { 1164 35238 36402
80 int i;
81 3466 ▶ 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]); 2302 1164 3466
82 1164 Safefree(t->tuple);
83 }
84 36402 ▶ if (t->enum_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_entries) { 1117 35285 36402
85 int i;
86 3468 ▶ 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); 2351 1117 3468
87 1117 Safefree(t->enum_entries);
88 }
89 36402 ▶ if (t->enum_lookup) SvREFCNT_dec((SV*)t->enum_lookup);
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_lookup) SvREFCNT_dec((SV*)t->enum_lookup); 1121 35281 36402
90 36402 ▶ if (t->enum_codes) Safefree(t->enum_codes);
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_codes) Safefree(t->enum_codes); 1121 35281 36402
91 36402 ▶ if (t->timezone) Safefree(t->timezone);
Branch: 1 branch
BranchTrueFalseTotal
if (t->timezone) Safefree(t->timezone); 97 36305 36402
92 36402 ▶ 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); 30 36372 36402
93 36402 ▶ 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); 30 36372 36402
94 36402 ▶ if (t->tuple_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_names) { 45 36357 36402
95 int i;
96 109 ▶ for (i = 0; i < t->tuple_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->tuple_len; i++) 64 45 109
97 64 ▶ if (t->tuple_names[i]) Safefree(t->tuple_names[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_names[i]) Safefree(t->tuple_names[i]); 64 0 64
98 45 Safefree(t->tuple_names);
99 }
100 36402 Safefree(t);
101 }
102  
103 1121 static void parse_enum_entries(pTHX_ TypeInfo *t, const char *s, STRLEN len, int code) {
104 1121 int cap = 0;
105 1121 ▶ long min_val = (code == T_ENUM8) ? -128 : -32768;
Branch: 1 branch
BranchTrueFalseTotal
long min_val = (code == T_ENUM8) ? -128 : -32768; 1066 55 1121
106 1121 ▶ long max_val = (code == T_ENUM8) ? 127 : 32767;
Branch: 1 branch
BranchTrueFalseTotal
long max_val = (code == T_ENUM8) ? 127 : 32767; 1066 55 1121
107 1121 t->enum_entries = NULL;
108 1121 t->enum_count = 0;
109 1121 t->enum_lookup = newHV();
110 1121 ▶ 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); 1066 55 1121
111  
112 1121 STRLEN i = 0;
113 3472 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 2355 1117 3472
114 4823 ▶ while (i < len && (s[i] == ' ' || s[i] == ',')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == ',')) i++; 4823 0 4823
while (i < len && (s[i] == ' ' || s[i] == ',')) i++; 1234 3589 4823
while (i < len && (s[i] == ' ' || s[i] == ',')) i++; 1234 2355 3589
115 2355 ▶ if (i >= len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) break; 0 2355 2355
116  
117 2355 ▶ if (s[i] != '\'')
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] != '\'') 0 2355 2355
118 0 croak("Invalid enum format: expected single quote at position %d", (int)i);
119 2355 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 2355 STRLEN name_start = i;
125 2355 STRLEN name_raw_len = 0; /* raw bytes consumed (incl. backslashes) */
126 5308 ▶ while (i < len && s[i] != '\'') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && s[i] != '\'') { 5308 0 5308
while (i < len && s[i] != '\'') { 2953 2355 5308
127 2953 ▶ 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 2952 2953
if (s[i] == '\\' && i + 1 < len) { i++; name_raw_len++; } 1 0 1
128 2953 i++; name_raw_len++;
129 }
130 2355 ▶ if (i >= len)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) 0 2355 2355
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 2355 Newx(name_buf, name_raw_len + 1, char);
137 2355 STRLEN name_len = 0;
138 2355 STRLEN j = name_start;
139 5308 ▶ while (j < i) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < i) { 2953 2355 5308
140 2953 ▶ if (s[j] == '\\' && j + 1 < i) j++;
Branch: 2 branches
BranchTrueFalseTotal
if (s[j] == '\\' && j + 1 < i) j++; 1 2952 2953
if (s[j] == '\\' && j + 1 < i) j++; 1 0 1
141 2953 name_buf[name_len++] = s[j++];
142 }
143 2355 name_buf[name_len] = 0;
144 2355 ▶ if (name_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_len == 0) { 1 2354 2355
145 1 Safefree(name_buf);
146 1 croak("Invalid enum format: empty name at position %d", (int)name_start);
147 }
148 2354 i++; /* closing quote */
149  
150 9416 ▶ while (i < len && (s[i] == ' ' || s[i] == '=')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '=')) i++; 9416 0 9416
while (i < len && (s[i] == ' ' || s[i] == '=')) i++; 4708 4708 9416
while (i < len && (s[i] == ' ' || s[i] == '=')) i++; 2354 2354 4708
151  
152 2354 int neg = 0;
153 2354 ▶ if (i < len && s[i] == '-') { neg = 1; i++; }
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == '-') { neg = 1; i++; } 2354 0 2354
if (i < len && s[i] == '-') { neg = 1; i++; } 48 2306 2354
154 2354 ▶ if (i >= len || s[i] < '0' || s[i] > '9') {
Branch: 3 branches
BranchTrueFalseTotal
if (i >= len || s[i] < '0' || s[i] > '9') { 2354 0 2354
if (i >= len || s[i] < '0' || s[i] > '9') { 2354 0 2354
if (i >= len || s[i] < '0' || s[i] > '9') { 0 2354 2354
155 0 Safefree(name_buf);
156 0 croak("Invalid enum format: expected digit at position %d", (int)i);
157 }
158 2354 long val = 0;
159 2354 int overflow = 0;
160 4759 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 3639 1120 4759
while (i < len && s[i] >= '0' && s[i] <= '9') { 2405 1234 3639
while (i < len && s[i] >= '0' && s[i] <= '9') { 2405 0 2405
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 2405 ▶ if (val > 100000) overflow = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (val > 100000) overflow = 1; 14 2391 2405
165 2391 else val = val * 10 + (s[i] - '0');
166 2405 i++;
167 }
168 2354 ▶ if (overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if (overflow) { 1 2353 2354
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 2353 ▶ if (neg) val = -val;
Branch: 1 branch
BranchTrueFalseTotal
if (neg) val = -val; 48 2305 2353
174 2353 ▶ if (val < min_val || val > max_val) {
Branch: 2 branches
BranchTrueFalseTotal
if (val < min_val || val > max_val) { 2353 0 2353
if (val < min_val || val > max_val) { 2 2351 2353
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 2351 ▶ if (t->enum_count >= cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->enum_count >= cap) { 1117 1234 2351
181 1117 ▶ cap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 8; 0 1117 1117
182 1117 Renew(t->enum_entries, cap, EnumEntry);
183 }
184 2351 t->enum_entries[t->enum_count].name = name_buf;
185 2351 t->enum_entries[t->enum_count].name_len = name_len;
186 2351 t->enum_entries[t->enum_count].value = (int16_t)val;
187 2351 t->enum_count++;
188  
189 2351 hv_store(t->enum_lookup, name_buf, name_len, newSViv(val), 0);
190 2351 ▶ 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 2237 2351
191 {
192 2351 unsigned index = (unsigned)(val - min_val);
193 2351 t->enum_codes[index >> 3] |= 1U << (index & 7);
194 }
195 }
196 1117 }
197  
198 /* Heap-allocated cleanup slot for a TypeInfo*. Disarmed by setting *slot=NULL. */
199 36403 static void cleanup_typeinfo_slot(pTHX_ void *p) {
200 36403 TypeInfo **slot = (TypeInfo **)p;
201 36403 ▶ if (*slot) free_typeinfo(aTHX_ *slot);
Branch: 1 branch
BranchTrueFalseTotal
if (*slot) free_typeinfo(aTHX_ *slot); 228 36175 36403
202 36403 Safefree(slot);
203 36403 }
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 26808 void cleanup_typeinfo_ptr(pTHX_ void *p) {
209 26808 TypeInfo **slot = (TypeInfo **)p;
210 26808 ▶ if (*slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot) { 26808 0 26808
211 26808 free_typeinfo(aTHX_ *slot);
212 26808 *slot = NULL;
213 }
214 26808 }
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 1167 static void cleanup_tuple_slot(pTHX_ void *p) {
225 1167 TupleSlot *s = (TupleSlot *)p;
226 1167 ▶ if (s->types) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->types) { 0 1167 1167
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 1167 Safefree(s);
233 1167 }
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 2348 static STRLEN type_list_capacity(const char *s, STRLEN len) {
252 2348 STRLEN n = 1;
253 2348 const char *end = s + len, *comma;
254 4662 ▶ while ((comma = memchr(s, ',', end - s))) { n++; s = comma + 1; }
Branch: 1 branch
BranchTrueFalseTotal
while ((comma = memchr(s, ',', end - s))) { n++; s = comma + 1; } 2314 2348 4662
255 2348 return n;
256 }
257  
258 1130 static int split_type_list(pTHX_ const char *s, STRLEN len, TypeBound *bounds) {
259 1130 int count = 0;
260 1130 int depth = 0;
261 1130 STRLEN start = 0, i;
262 #define IS_WS(c) ((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r')
263 27492 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 26362 1130 27492
264 26362 ▶ char c = (i < len) ? s[i] : ',';
Branch: 1 branch
BranchTrueFalseTotal
char c = (i < len) ? s[i] : ','; 25232 1130 26362
265 26362 ▶ 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; } 26350 12 26362
if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ s, len, i) - 1; continue; } 1 26349 26350
266 26349 ▶ if (c == '(') depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') depth++; 1042 25307 26349
267 25307 ▶ else if (c == ')') depth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ')') depth--; 1042 24265 25307
268 24265 ▶ else if ((c == ',' && depth == 0) || i == len) {
Branch: 3 branches
BranchTrueFalseTotal
else if ((c == ',' && depth == 0) || i == len) { 2265 22000 24265
else if ((c == ',' && depth == 0) || i == len) { 10 2255 2265
else if ((c == ',' && depth == 0) || i == len) { 0 22010 22010
269 2255 STRLEN tstart = start, tend = i;
270 2255 STRLEN nstart = 0, nlen = 0;
271 3380 ▶ while (tstart < tend && IS_WS(s[tstart])) tstart++;
Branch: 5 branches
BranchTrueFalseTotal
while (tstart < tend && IS_WS(s[tstart])) tstart++; 3376 4 3380
while (tstart < tend && IS_WS(s[tstart])) tstart++; 1125 2251 3376
while (tstart < tend && IS_WS(s[tstart])) tstart++; 0 2251 2251
while (tstart < tend && IS_WS(s[tstart])) tstart++; 0 2251 2251
while (tstart < tend && IS_WS(s[tstart])) tstart++; 0 2251 2251
272 2255 ▶ while (tend > tstart && IS_WS(s[tend-1])) tend--;
Branch: 5 branches
BranchTrueFalseTotal
while (tend > tstart && IS_WS(s[tend-1])) tend--; 2251 4 2255
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2251 2251
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2251 2251
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2251 2251
while (tend > tstart && IS_WS(s[tend-1])) tend--; 0 2251 2251
273 2255 ▶ if (tend > tstart) {
Branch: 1 branch
BranchTrueFalseTotal
if (tend > tstart) { 2251 4 2255
274 2251 STRLEN id_end = tstart;
275 2251 ▶ if (id_end < tend && s[id_end] == '`') {
Branch: 2 branches
BranchTrueFalseTotal
if (id_end < tend && s[id_end] == '`') { 2251 0 2251
if (id_end < tend && s[id_end] == '`') { 1 2250 2251
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 2250 ▶ } else if (id_end < tend
Branch: 1 branch
BranchTrueFalseTotal
} else if (id_end < tend 2250 0 2250
284 2250 ▶ && ((s[id_end] >= 'A' && s[id_end] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 2250 0 2250
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 12 2238 2250
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 2250 id_end++;
288 2250 while (id_end < tend
289 14523 ▶ && ((s[id_end] >= 'A' && s[id_end] <= 'Z')
Branch: 3 branches
BranchTrueFalseTotal
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 13324 1199 14523
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 10066 3258 13324
&& ((s[id_end] >= 'A' && s[id_end] <= 'Z') 82 9984 10066
290 13242 ▶ || (s[id_end] >= 'a' && s[id_end] <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
|| (s[id_end] >= 'a' && s[id_end] <= 'z') 9984 3258 13242
|| (s[id_end] >= 'a' && s[id_end] <= 'z') 9984 0 9984
291 3258 ▶ || (s[id_end] >= '0' && s[id_end] <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (s[id_end] >= '0' && s[id_end] <= '9') 2207 1051 3258
|| (s[id_end] >= '0' && s[id_end] <= '9') 2207 0 2207
292 1051 ▶ || s[id_end] == '_'))
Branch: 1 branch
BranchTrueFalseTotal
|| s[id_end] == '_')) 0 1051 1051
293 12273 id_end++;
294 2250 ▶ if (id_end < tend && IS_WS(s[id_end])) {
Branch: 5 branches
BranchTrueFalseTotal
if (id_end < tend && IS_WS(s[id_end])) { 1051 1199 2250
if (id_end < tend && IS_WS(s[id_end])) { 1039 12 1051
if (id_end < tend && IS_WS(s[id_end])) { 1039 0 1039
if (id_end < tend && IS_WS(s[id_end])) { 1039 0 1039
if (id_end < tend && IS_WS(s[id_end])) { 0 1039 1039
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 2251 bounds[count].start = tstart;
303 2251 bounds[count].len = tend - tstart;
304 2251 bounds[count].name_start = nstart;
305 2251 bounds[count].name_len = nlen;
306 2251 count++;
307 }
308 2255 start = i + 1;
309 }
310 }
311 #undef IS_WS
312 1130 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 1167 static TypeInfo** parse_tuple_types_with_bounds(pTHX_ const char *s,
319 TypeBound *bounds,
320 int n, int depth) {
321 TupleSlot *slot;
322 1167 Newxz(slot, 1, TupleSlot);
323 1167 SAVEDESTRUCTOR_X(cleanup_tuple_slot, slot);
324 1167 ▶ if (n > 0) Newxz(slot->types, n, TypeInfo*);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) Newxz(slot->types, n, TypeInfo*); 1164 3 1167
325  
326 int i;
327 3469 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2302 1167 3469
328 4604 slot->types[i] = parse_type_at(aTHX_ s + bounds[i].start,
329 2302 bounds[i].len, depth);
330 2302 slot->count = i + 1;
331 }
332 {
333 1167 TypeInfo **result = slot->types;
334 1167 slot->types = NULL; /* Disarm: caller now owns the array. */
335 1167 return result;
336 }
337 }
338  
339 54 static TypeInfo** parse_tuple_types(pTHX_ const char *s, STRLEN len, int *count,
340 int depth) {
341 TypeBound *bounds;
342 54 ▶ Newx(bounds, type_list_capacity(s, len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newx(bounds, type_list_capacity(s, len), TypeBound); 0 54 54
343 54 SAVEFREEPV(bounds);
344 54 *count = split_type_list(aTHX_ s, len, bounds);
345 54 return parse_tuple_types_with_bounds(aTHX_ s, bounds, *count, depth);
346 }
347  
348 64 static SV *canonical_variant_type(pTHX_ const char *s, STRLEN len, TypeInfo *t) {
349 64 ▶ while (len && isSPACE(*s)) { s++; len--; }
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(*s)) { s++; len--; } 64 0 64
while (len && isSPACE(*s)) { s++; len--; } 0 64 64
350 64 ▶ while (len && isSPACE(s[len-1])) len--;
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(s[len-1])) len--; 64 0 64
while (len && isSPACE(s[len-1])) len--; 0 64 64
351 64 const char *open = memchr(s, '(', len);
352 64 ▶ STRLEN base_len = open ? (STRLEN)(open - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN base_len = open ? (STRLEN)(open - s) : len; 8 56 64
353 64 ▶ if (base_len == 7 && memcmp(s, "Boolean", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (base_len == 7 && memcmp(s, "Boolean", 7) == 0) 0 64 64
if (base_len == 7 && memcmp(s, "Boolean", 7) == 0) 0 0 0
354 0 return sv_2mortal(newSVpvs("Bool"));
355 64 ▶ 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 58 64
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 58 SV *out = sv_2mortal(newSVpvn(s, base_len));
362 58 ▶ if (!open) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!open) return out; 56 2 58
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 69 static int type_can_be_typed_path(TypeInfo *t) {
409 69 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 6 case T_ARRAY:
416 case T_NULLABLE:
417 6 return type_can_be_typed_path(t->inner);
418 6 case T_TUPLE:
419 case T_MAP: {
420 int i;
421 18 ▶ for (i = 0; i < t->tuple_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->tuple_len; i++) 12 6 18
422 12 ▶ if (!type_can_be_typed_path(t->tuple[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!type_can_be_typed_path(t->tuple[i])) 0 12 12
423 0 return 0;
424 6 return 1;
425 }
426 51 default:
427 51 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 44 static void parse_json_typed_paths(pTHX_ TypeInfo *t,
436 const char *body, STRLEN body_len,
437 int depth) {
438 TypeBound *bounds;
439 44 ▶ Newxz(bounds, type_list_capacity(body, body_len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(bounds, type_list_capacity(body, body_len), TypeBound); 0 44 44
440 44 SAVEFREEPV(bounds);
441  
442 44 int idx = 0;
443 44 int paren_depth = 0; /* not `depth`, which is the type-nesting level */
444 44 STRLEN start = 0, i;
445 865 ▶ for (i = 0; i <= body_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= body_len; i++) { 824 41 865
446 824 ▶ char c = (i < body_len) ? body[i] : ',';
Branch: 1 branch
BranchTrueFalseTotal
char c = (i < body_len) ? body[i] : ','; 780 44 824
447 824 ▶ 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; } 824 0 824
if (c == '\'' || c == '`') { i = skip_type_quote(aTHX_ body, body_len, i) - 1; continue; } 3 821 824
448 821 ▶ if (c == '(') paren_depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') paren_depth++; 16 805 821
449 805 ▶ else if (c == ')') paren_depth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ')') paren_depth--; 16 789 805
450 789 ▶ else if ((c == ',' && paren_depth == 0) || i == body_len) {
Branch: 3 branches
BranchTrueFalseTotal
else if ((c == ',' && paren_depth == 0) || i == body_len) { 66 723 789
else if ((c == ',' && paren_depth == 0) || i == body_len) { 8 58 66
else if ((c == ',' && paren_depth == 0) || i == body_len) { 0 731 731
451 58 STRLEN ts = start, te = i;
452 #define J_WS(c2) ((c2)==' '||(c2)=='\t'||(c2)=='\n'||(c2)=='\r')
453 72 ▶ while (ts < te && J_WS(body[ts])) ts++;
Branch: 5 branches
BranchTrueFalseTotal
while (ts < te && J_WS(body[ts])) ts++; 70 2 72
while (ts < te && J_WS(body[ts])) ts++; 14 56 70
while (ts < te && J_WS(body[ts])) ts++; 0 56 56
while (ts < te && J_WS(body[ts])) ts++; 0 56 56
while (ts < te && J_WS(body[ts])) ts++; 0 56 56
454 58 ▶ while (te > ts && J_WS(body[te-1])) te--;
Branch: 5 branches
BranchTrueFalseTotal
while (te > ts && J_WS(body[te-1])) te--; 56 2 58
while (te > ts && J_WS(body[te-1])) te--; 0 56 56
while (te > ts && J_WS(body[te-1])) te--; 0 56 56
while (te > ts && J_WS(body[te-1])) te--; 0 56 56
while (te > ts && J_WS(body[te-1])) te--; 0 56 56
455 58 ▶ if (te > ts) {
Branch: 1 branch
BranchTrueFalseTotal
if (te > ts) { 56 2 58
456 56 STRLEN id = ts;
457 56 ▶ if (body[id] == '`') {
Branch: 1 branch
BranchTrueFalseTotal
if (body[id] == '`') { 3 53 56
458 3 id = skip_type_quote(aTHX_ body, te, id);
459 53 ▶ } else if (body[id] == '_'
Branch: 1 branch
BranchTrueFalseTotal
} else if (body[id] == '_' 53 0 53
460 53 ▶ || (body[id] >= 'A' && body[id] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'A' && body[id] <= 'Z') 53 0 53
|| (body[id] >= 'A' && body[id] <= 'Z') 53 0 53
461 53 ▶ || (body[id] >= 'a' && body[id] <= 'z')) {
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'a' && body[id] <= 'z')) { 53 0 53
|| (body[id] >= 'a' && body[id] <= 'z')) { 53 0 53
462 53 id++;
463 53 while (id < te
464 189 ▶ && (body[id] == '_' || body[id] == '.'
Branch: 3 branches
BranchTrueFalseTotal
&& (body[id] == '_' || body[id] == '.' 188 1 189
&& (body[id] == '_' || body[id] == '.' 0 188 188
&& (body[id] == '_' || body[id] == '.' 7 181 188
465 181 ▶ || (body[id] >= 'A' && body[id] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'A' && body[id] <= 'Z') 129 52 181
|| (body[id] >= 'A' && body[id] <= 'Z') 0 129 129
466 181 ▶ || (body[id] >= 'a' && body[id] <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= 'a' && body[id] <= 'z') 129 52 181
|| (body[id] >= 'a' && body[id] <= 'z') 129 0 129
467 52 ▶ || (body[id] >= '0' && body[id] <= '9')))
Branch: 2 branches
BranchTrueFalseTotal
|| (body[id] >= '0' && body[id] <= '9'))) 0 52 52
|| (body[id] >= '0' && body[id] <= '9'))) 0 0 0
468 136 id++;
469 }
470 56 ▶ if (id == ts)
Branch: 1 branch
BranchTrueFalseTotal
if (id == ts) 0 56 56
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 56 ▶ if (body[id - 1] == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (body[id - 1] == '.') 1 55 56
478 1 croak("JSON(...): path name must not end with '.' "
479 "in '%.*s'",
480 (int)(id - ts), body + ts);
481 STRLEN dk;
482 194 ▶ for (dk = ts + 1; dk < id; dk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (dk = ts + 1; dk < id; dk++) { 140 54 194
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 54 STRLEN ws = id;
489 107 ▶ while (ws < te && J_WS(body[ws])) ws++;
Branch: 5 branches
BranchTrueFalseTotal
while (ws < te && J_WS(body[ws])) ws++; 106 1 107
while (ws < te && J_WS(body[ws])) ws++; 53 53 106
while (ws < te && J_WS(body[ws])) ws++; 0 53 53
while (ws < te && J_WS(body[ws])) ws++; 0 53 53
while (ws < te && J_WS(body[ws])) ws++; 0 53 53
490 54 ▶ if (ws == id || ws == te)
Branch: 2 branches
BranchTrueFalseTotal
if (ws == id || ws == te) 53 1 54
if (ws == id || ws == te) 0 53 53
491 1 croak("JSON(...): expected 'name Type' but got '%.*s'",
492 (int)(te - ts), body + ts);
493 53 bounds[idx].name_start = ts;
494 53 bounds[idx].name_len = id - ts;
495 53 bounds[idx].start = ws;
496 53 bounds[idx].len = te - ws;
497 53 idx++;
498 }
499 #undef J_WS
500 55 start = i + 1;
501 }
502 }
503 41 ▶ if (idx == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (idx == 0) return; 2 39 41
504 39 int n = idx, ii, j;
505 92 ▶ for (ii = 0; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < n; ii++) { 53 39 92
506 106 bounds[ii].name = copy_type_name(aTHX_ body + bounds[ii].name_start,
507 53 bounds[ii].name_len);
508 53 SAVEFREEPV(bounds[ii].name);
509 }
510 53 ▶ for (ii = 1; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 1; ii < n; ii++) { 14 39 53
511 14 TypeBound key = bounds[ii];
512 14 j = ii - 1;
513 25 ▶ while (j >= 0 && strcmp(bounds[j].name, key.name) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (j >= 0 && strcmp(bounds[j].name, key.name) > 0) { 14 11 25
while (j >= 0 && strcmp(bounds[j].name, key.name) > 0) { 11 3 14
514 11 bounds[j+1] = bounds[j];
515 11 j--;
516 }
517 14 bounds[j+1] = key;
518 }
519 52 ▶ for (ii = 1; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 1; ii < n; ii++) { 14 38 52
520 14 ▶ 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 13 14
521 1 croak("JSON(...): duplicate typed path name '%s'", bounds[ii].name);
522 }
523 38 t->tuple_len = n;
524 38 Newxz(t->tuple_names, n, char*);
525 89 ▶ 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); 51 38 89
526 38 t->tuple = parse_tuple_types_with_bounds(aTHX_ body, bounds, n, depth);
527  
528 83 ▶ for (ii = 0; ii < n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < n; ii++) { 51 32 83
529 51 ▶ if (!type_can_be_typed_path(t->tuple[ii]))
Branch: 1 branch
BranchTrueFalseTotal
if (!type_can_be_typed_path(t->tuple[ii])) 6 45 51
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 31456 TypeInfo* parse_type(pTHX_ const char *type, STRLEN len) {
538 31456 return parse_type_at(aTHX_ type, len, 0);
539 }
540  
541 36405 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 36405 ▶ if (++depth > CHE_MAX_TYPE_DEPTH)
Branch: 1 branch
BranchTrueFalseTotal
if (++depth > CHE_MAX_TYPE_DEPTH) 2 36403 36405
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 36403 ▶ while (len && isSPACE(type[0])) { type++; len--; }
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(type[0])) { type++; len--; } 36403 0 36403
while (len && isSPACE(type[0])) { type++; len--; } 0 36403 36403
551 36403 ▶ while (len && isSPACE(type[len-1])) len--;
Branch: 2 branches
BranchTrueFalseTotal
while (len && isSPACE(type[len-1])) len--; 36403 0 36403
while (len && isSPACE(type[len-1])) len--; 0 36403 36403
552 36403 ▶ if (memchr(type, '(', len) && (!len || type[len-1] != ')'))
Branch: 3 branches
BranchTrueFalseTotal
if (memchr(type, '(', len) && (!len || type[len-1] != ')')) 6235 30168 36403
if (memchr(type, '(', len) && (!len || type[len-1] != ')')) 6235 0 6235
if (memchr(type, '(', len) && (!len || type[len-1] != ')')) 0 6235 6235
553 0 croak("Unbalanced type expression: %.*s", (int)len, type);
554 36403 Newx(slot, 1, TypeInfo*);
555 36403 *slot = NULL;
556 36403 SAVEDESTRUCTOR_X(cleanup_typeinfo_slot, slot);
557 36403 Newxz(t, 1, TypeInfo);
558 36403 *slot = t;
559  
560 36403 ▶ if (len == 4 && strncmp(type, "Int8", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 4 && strncmp(type, "Int8", 4) == 0) { 591 35812 36403
if (len == 4 && strncmp(type, "Int8", 4) == 0) { 20 571 591
561 20 t->code = T_INT8;
562 36383 ▶ } else if (len == 5 && strncmp(type, "Int16", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Int16", 5) == 0) { 1721 34662 36383
} else if (len == 5 && strncmp(type, "Int16", 5) == 0) { 15 1706 1721
563 15 t->code = T_INT16;
564 36368 ▶ } else if (len == 5 && strncmp(type, "Int32", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Int32", 5) == 0) { 1706 34662 36368
} else if (len == 5 && strncmp(type, "Int32", 5) == 0) { 1501 205 1706
565 1501 t->code = T_INT32;
566 34867 ▶ } else if (len == 5 && strncmp(type, "Int64", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Int64", 5) == 0) { 205 34662 34867
} else if (len == 5 && strncmp(type, "Int64", 5) == 0) { 25 180 205
567 25 t->code = T_INT64;
568 34842 ▶ } else if (len == 5 && strncmp(type, "UInt8", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "UInt8", 5) == 0) { 180 34662 34842
} else if (len == 5 && strncmp(type, "UInt8", 5) == 0) { 170 10 180
569 170 t->code = T_UINT8;
570 34672 ▶ } else if (len == 6 && strncmp(type, "UInt16", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "UInt16", 6) == 0) { 27090 7582 34672
} else if (len == 6 && strncmp(type, "UInt16", 6) == 0) { 9 27081 27090
571 9 t->code = T_UINT16;
572 34663 ▶ } else if (len == 6 && strncmp(type, "UInt32", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "UInt32", 6) == 0) { 27081 7582 34663
} else if (len == 6 && strncmp(type, "UInt32", 6) == 0) { 223 26858 27081
573 223 t->code = T_UINT32;
574 34440 ▶ } else if (len == 6 && strncmp(type, "UInt64", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "UInt64", 6) == 0) { 26858 7582 34440
} else if (len == 6 && strncmp(type, "UInt64", 6) == 0) { 49 26809 26858
575 49 t->code = T_UINT64;
576 34391 ▶ } else if (len == 7 && strncmp(type, "Float32", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Float32", 7) == 0) { 399 33992 34391
} else if (len == 7 && strncmp(type, "Float32", 7) == 0) { 12 387 399
577 12 t->code = T_FLOAT32;
578 34379 ▶ } else if (len == 7 && strncmp(type, "Float64", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Float64", 7) == 0) { 387 33992 34379
} else if (len == 7 && strncmp(type, "Float64", 7) == 0) { 355 32 387
579 355 t->code = T_FLOAT64;
580 34024 ▶ } else if (len == 8 && strncmp(type, "BFloat16", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 8 && strncmp(type, "BFloat16", 8) == 0) { 367 33657 34024
} else if (len == 8 && strncmp(type, "BFloat16", 8) == 0) { 10 357 367
581 10 t->code = T_BFLOAT16;
582 34014 ▶ } else if (len == 6 && strncmp(type, "String", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "String", 6) == 0) { 26809 7205 34014
} else if (len == 6 && strncmp(type, "String", 6) == 0) { 26798 11 26809
583 26798 t->code = T_STRING;
584 7216 ▶ } else if (len > 12 && strncmp(type, "FixedString(", 12) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 12 && strncmp(type, "FixedString(", 12) == 0) { 5093 2123 7216
} else if (len > 12 && strncmp(type, "FixedString(", 12) == 0) { 28 5065 5093
585 28 t->code = T_FIXEDSTRING;
586 28 t->param = parse_int_param(type + 12, len - 12, 0);
587 28 ▶ if (t->param <= 0) croak("FixedString needs positive length");
Branch: 1 branch
BranchTrueFalseTotal
if (t->param <= 0) croak("FixedString needs positive length"); 3 25 28
588 /* ClickHouse's MAX_FIXED_STRING_SIZE; beyond it the server itself
589 * refuses the type, so no real schema reaches here. */
590 25 ▶ if (t->param > 0xFFFFFF)
Branch: 1 branch
BranchTrueFalseTotal
if (t->param > 0xFFFFFF) 0 25 25
591 0 croak("FixedString length %d exceeds the maximum of %d",
592 t->param, 0xFFFFFF);
593 7188 ▶ } else if (len > 6 && strncmp(type, "Array(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 6 && strncmp(type, "Array(", 6) == 0) { 6596 592 7188
} else if (len > 6 && strncmp(type, "Array(", 6) == 0) { 1431 5165 6596
594 1431 t->code = T_ARRAY;
595 1431 t->inner = parse_type_at(aTHX_ type + 6, len - 7, depth);
596 6802 ▶ } else if (len > 6 && strncmp(type, "Tuple(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 6 && strncmp(type, "Tuple(", 6) == 0) { 5165 592 5757
} else if (len > 6 && strncmp(type, "Tuple(", 6) == 0) { 1045 4120 5165
597 1045 t->code = T_TUPLE;
598 1045 const char *body = type + 6;
599 1045 STRLEN body_len = len - 7;
600 TypeBound *bounds;
601 1045 ▶ Newx(bounds, type_list_capacity(body, body_len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newx(bounds, type_list_capacity(body, body_len), TypeBound); 0 1045 1045
602 1045 SAVEFREEPV(bounds);
603 1045 t->tuple_len = split_type_list(aTHX_ body, body_len, bounds);
604 1045 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 1045 int has_names = 0;
611 int j;
612 3113 ▶ for (j = 0; j < t->tuple_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len; j++) { 2075 1038 3113
613 2075 ▶ 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 2068 2075
614 }
615 1045 ▶ if (has_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_names) { 7 1038 1045
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 4712 ▶ } else if (len > 9 && strncmp(type, "Nullable(", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 9 && strncmp(type, "Nullable(", 9) == 0) { 3733 979 4712
} else if (len > 9 && strncmp(type, "Nullable(", 9) == 0) { 1141 2592 3733
626 1141 ▶ if (len > 18 && strncmp(type + 9, "Nullable(", 9) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (len > 18 && strncmp(type + 9, "Nullable(", 9) == 0) 41 1100 1141
if (len > 18 && strncmp(type + 9, "Nullable(", 9) == 0) 2 39 41
627 2 croak("Nullable(Nullable(...)) is not allowed");
628 1139 t->code = T_NULLABLE;
629 1139 t->inner = parse_type_at(aTHX_ type + 9, len - 10, depth);
630 3571 ▶ } else if (len > 6 && strncmp(type, "Enum8(", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 6 && strncmp(type, "Enum8(", 6) == 0) { 2979 592 3571
} else if (len > 6 && strncmp(type, "Enum8(", 6) == 0) { 1066 1913 2979
631 1066 t->code = T_ENUM8;
632 1066 parse_enum_entries(aTHX_ t, type + 6, len - 7, T_ENUM8);
633 2505 ▶ } else if (len > 7 && strncmp(type, "Enum16(", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 7 && strncmp(type, "Enum16(", 7) == 0) { 1884 621 2505
} else if (len > 7 && strncmp(type, "Enum16(", 7) == 0) { 55 1829 1884
634 55 t->code = T_ENUM16;
635 55 parse_enum_entries(aTHX_ t, type + 7, len - 8, T_ENUM16);
636 2450 ▶ } else if (len > 10 && strncmp(type, "Decimal32(", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 10 && strncmp(type, "Decimal32(", 10) == 0) { 1468 982 2450
} else if (len > 10 && strncmp(type, "Decimal32(", 10) == 0) { 15 1453 1468
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 2435 ▶ } else if (len > 10 && strncmp(type, "Decimal64(", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 10 && strncmp(type, "Decimal64(", 10) == 0) { 1453 982 2435
} else if (len > 10 && strncmp(type, "Decimal64(", 10) == 0) { 1020 433 1453
643 1020 t->code = T_DECIMAL64;
644 1020 t->precision = 18;
645 1020 t->param = parse_int_param(type + 10, len - 10, 0);
646 1020 ▶ if (t->param < 0 || t->param > 18)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 18) 1020 0 1020
if (t->param < 0 || t->param > 18) 0 1020 1020
647 0 croak("Decimal64 scale must be 0..18, got %d", t->param);
648 1415 ▶ } else if (len > 11 && strncmp(type, "Decimal128(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 11 && strncmp(type, "Decimal128(", 11) == 0) { 430 985 1415
} else if (len > 11 && strncmp(type, "Decimal128(", 11) == 0) { 46 384 430
649 46 t->code = T_DECIMAL128;
650 46 t->precision = 38;
651 46 t->param = parse_int_param(type + 11, len - 11, 0);
652 46 ▶ if (t->param < 0 || t->param > 38)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 38) 46 0 46
if (t->param < 0 || t->param > 38) 0 46 46
653 0 croak("Decimal128 scale must be 0..38, got %d", t->param);
654 1369 ▶ } else if (len > 11 && strncmp(type, "Decimal256(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 11 && strncmp(type, "Decimal256(", 11) == 0) { 384 985 1369
} else if (len > 11 && strncmp(type, "Decimal256(", 11) == 0) { 68 316 384
655 68 t->code = T_DECIMAL256;
656 68 t->precision = 76;
657 68 t->param = parse_int_param(type + 11, len - 11, 0);
658 68 ▶ if (t->param < 0 || t->param > 76)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 76) 68 0 68
if (t->param < 0 || t->param > 76) 0 68 68
659 0 croak("Decimal256 scale must be 0..76, got %d", t->param);
660 1307 ▶ } else if (len > 8 && strncmp(type, "Decimal(", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 8 && strncmp(type, "Decimal(", 8) == 0) { 323 978 1301
} else if (len > 8 && strncmp(type, "Decimal(", 8) == 0) { 7 316 323
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 1294 ▶ } else if (len == 4 && strncmp(type, "Date", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "Date", 4) == 0) { 571 723 1294
} else if (len == 4 && strncmp(type, "Date", 4) == 0) { 22 549 571
676 22 t->code = T_DATE;
677 1272 ▶ } else if (len == 6 && strncmp(type, "Date32", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 6 && strncmp(type, "Date32", 6) == 0) { 11 1261 1272
} else if (len == 6 && strncmp(type, "Date32", 6) == 0) { 9 2 11
678 9 t->code = T_DATE32;
679 1263 ▶ } else if (len == 8 && strncmp(type, "DateTime", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 8 && strncmp(type, "DateTime", 8) == 0) { 357 906 1263
} else if (len == 8 && strncmp(type, "DateTime", 8) == 0) { 357 0 357
680 357 t->code = T_DATETIME;
681 984 ▶ } else if (len > 9 && strncmp(type, "DateTime(", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 9 && strncmp(type, "DateTime(", 9) == 0) { 315 591 906
} else if (len > 9 && strncmp(type, "DateTime(", 9) == 0) { 78 237 315
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 828 ▶ } else if (len > 11 && strncmp(type, "DateTime64(", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 11 && strncmp(type, "DateTime64(", 11) == 0) { 233 595 828
} else if (len > 11 && strncmp(type, "DateTime64(", 11) == 0) { 47 186 233
691 47 t->code = T_DATETIME64;
692 47 t->param = parse_int_param(type + 11, len - 11, 1);
693 47 ▶ if (t->param < 0 || t->param > 9)
Branch: 2 branches
BranchTrueFalseTotal
if (t->param < 0 || t->param > 9) 47 0 47
if (t->param < 0 || t->param > 9) 1 46 47
694 1 croak("DateTime64 precision must be 0..9, got %d", t->param);
695 781 ▶ } else if (len == 4 && strncmp(type, "Bool", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "Bool", 4) == 0) { 549 232 781
} else if (len == 4 && strncmp(type, "Bool", 4) == 0) { 314 235 549
696 314 t->code = T_BOOL;
697 467 ▶ } else if (len == 7 && strncmp(type, "Boolean", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Boolean", 7) == 0) { 29 438 467
} else if (len == 7 && strncmp(type, "Boolean", 7) == 0) { 0 29 29
698 0 t->code = T_BOOL;
699 467 ▶ } else if (len == 4 && strncmp(type, "UUID", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "UUID", 4) == 0) { 235 232 467
} else if (len == 4 && strncmp(type, "UUID", 4) == 0) { 70 165 235
700 70 t->code = T_UUID;
701 397 ▶ } else if (len == 4 && strncmp(type, "IPv4", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "IPv4", 4) == 0) { 165 232 397
} else if (len == 4 && strncmp(type, "IPv4", 4) == 0) { 23 142 165
702 23 t->code = T_IPV4;
703 374 ▶ } else if (len == 4 && strncmp(type, "IPv6", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "IPv6", 4) == 0) { 142 232 374
} else if (len == 4 && strncmp(type, "IPv6", 4) == 0) { 37 105 142
704 37 t->code = T_IPV6;
705 338 ▶ } else if (len > 24 && strncmp(type, "SimpleAggregateFunction(", 24) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 24 && strncmp(type, "SimpleAggregateFunction(", 24) == 0) { 58 279 337
} else if (len > 24 && strncmp(type, "SimpleAggregateFunction(", 24) == 0) { 1 57 58
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 366 ▶ } else if (len > 8 && strncmp(type, "Variant(", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 8 && strncmp(type, "Variant(", 8) == 0) { 190 146 336
} else if (len > 8 && strncmp(type, "Variant(", 8) == 0) { 31 159 190
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 31 t->code = T_VARIANT;
730 31 const char *body = type + 8;
731 31 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 31 ▶ Newx(bounds, type_list_capacity(body, body_len), TypeBound);
Branch: 1 branch
BranchTrueFalseTotal
Newx(bounds, type_list_capacity(body, body_len), TypeBound); 0 31 31
738 31 SAVEFREEPV(bounds);
739 31 t->tuple_len = split_type_list(aTHX_ body, body_len, bounds);
740 31 ▶ if (t->tuple_len < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_len < 1) 1 30 31
741 1 croak("Variant requires at least one type argument");
742 30 ▶ if (t->tuple_len > 254)
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_len > 254) 0 30 30
743 0 croak("Variant supports at most 254 types (got %d)", t->tuple_len);
744 30 t->tuple = parse_tuple_types_with_bounds(aTHX_ body, bounds,
745 t->tuple_len, depth);
746  
747 SV **canonical;
748 30 Newx(canonical, t->tuple_len, SV *);
749 30 SAVEFREEPV(canonical);
750 int vi;
751 92 ▶ for (vi = 0; vi < t->tuple_len; vi++)
Branch: 1 branch
BranchTrueFalseTotal
for (vi = 0; vi < t->tuple_len; vi++) 62 30 92
752 62 canonical[vi] = canonical_variant_type(aTHX_ body + bounds[vi].start,
753 62 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 30 Newx(t->variant_wire_to_decl, t->tuple_len, int);
758 30 Newx(t->variant_decl_to_wire, t->tuple_len, int);
759 int j, k;
760 92 ▶ 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; 62 30 92
761 62 ▶ for (j = 0; j < t->tuple_len - 1; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len - 1; j++) { 32 30 62
762 32 int min_idx = j;
763 66 ▶ for (k = j + 1; k < t->tuple_len; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = j + 1; k < t->tuple_len; k++) { 34 32 66
764 34 int a = t->variant_wire_to_decl[min_idx];
765 34 int b = t->variant_wire_to_decl[k];
766 STRLEN la, lb;
767 68 ▶ 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); 34 0 34
768 34 STRLEN cmp_len = la < lb ? la : lb;
769 34 int cmp = memcmp(sa, sb, cmp_len);
770 34 ▶ if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k;
Branch: 3 branches
BranchTrueFalseTotal
if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k; 26 8 34
if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k; 0 26 26
if (cmp > 0 || (cmp == 0 && la > lb)) min_idx = k; 0 0 0
771 }
772 32 ▶ if (min_idx != j) {
Branch: 1 branch
BranchTrueFalseTotal
if (min_idx != j) { 8 24 32
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 92 ▶ for (j = 0; j < t->tuple_len; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < t->tuple_len; j++) 62 30 92
779 62 t->variant_decl_to_wire[t->variant_wire_to_decl[j]] = j;
780 305 ▶ } else if (len > 4 && strncmp(type, "Map(", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 4 && strncmp(type, "Map(", 4) == 0) { 200 105 305
} else if (len > 4 && strncmp(type, "Map(", 4) == 0) { 44 156 200
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 44 t->code = T_MAP;
784 44 t->tuple = parse_tuple_types(aTHX_ type + 4, len - 5, &t->tuple_len, depth);
785 44 ▶ if (t->tuple_len != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (t->tuple_len != 2) 0 44 44
786 0 croak("Map type requires exactly 2 type arguments, got %d", t->tuple_len);
787 261 ▶ } else if (len > 7 && strncmp(type, "Nested(", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 7 && strncmp(type, "Nested(", 7) == 0) { 115 146 261
} else if (len > 7 && strncmp(type, "Nested(", 7) == 0) { 2 113 115
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 259 ▶ } else if (len == 7 && strncmp(type, "Dynamic", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Dynamic", 7) == 0) { 29 230 259
} else if (len == 7 && strncmp(type, "Dynamic", 7) == 0) { 25 4 29
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 25 t->code = T_DYNAMIC;
802 234 ▶ } else if ((len == 4 && strncmp(type, "JSON", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
} else if ((len == 4 && strncmp(type, "JSON", 4) == 0) 105 129 234
} else if ((len == 4 && strncmp(type, "JSON", 4) == 0) 3 102 105
803 132 ▶ || (len > 5 && strncmp(type, "JSON(", 5) == 0
Branch: 2 branches
BranchTrueFalseTotal
|| (len > 5 && strncmp(type, "JSON(", 5) == 0 119 13 132
|| (len > 5 && strncmp(type, "JSON(", 5) == 0 44 75 119
804 44 ▶ && type[len-1] == ')')
Branch: 1 branch
BranchTrueFalseTotal
&& type[len-1] == ')') 0 44 44
805 88 ▶ || (len > 7 && strncmp(type, "Object(", 7) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (len > 7 && strncmp(type, "Object(", 7) == 0)) { 72 16 88
|| (len > 7 && strncmp(type, "Object(", 7) == 0)) { 0 72 72
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 146 t->code = T_JSON;
816 146 ▶ if (len > 5 && type[4] == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (len > 5 && type[4] == '(') { 44 102 146
if (len > 5 && type[4] == '(') { 44 0 44
817 44 parse_json_typed_paths(aTHX_ t, type + 5, len - 6, depth);
818 }
819 88 ▶ } else if (len == 5 && strncmp(type, "Point", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 5 && strncmp(type, "Point", 5) == 0) { 10 78 88
} 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 78 ▶ } else if (len == 4 && strncmp(type, "Ring", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 4 && strncmp(type, "Ring", 4) == 0) { 3 75 78
} 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 75 ▶ } else if (len == 10 && strncmp(type, "LineString", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 10 && strncmp(type, "LineString", 10) == 0) { 3 72 75
} 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 73 ▶ } else if (len == 15 && strncmp(type, "MultiLineString", 15) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 15 && strncmp(type, "MultiLineString", 15) == 0) { 0 73 73
} 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 73 ▶ } else if (len == 7 && strncmp(type, "Polygon", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 7 && strncmp(type, "Polygon", 7) == 0) { 3 70 73
} 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 70 ▶ } else if (len == 12 && strncmp(type, "MultiPolygon", 12) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 12 && strncmp(type, "MultiPolygon", 12) == 0) { 2 68 70
} 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 133 ▶ } else if (len > 15 && strncmp(type, "LowCardinality(", 15) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len > 15 && strncmp(type, "LowCardinality(", 15) == 0) { 66 2 68
} else if (len > 15 && strncmp(type, "LowCardinality(", 15) == 0) { 66 0 66
845 66 t->code = T_LOWCARDINALITY;
846 66 t->inner = parse_type_at(aTHX_ type + 15, len - 16, depth);
847 66 ▶ 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; 20 46 66
848 66 ▶ if (leaf->code > T_IPV6 || leaf->code == T_ARRAY
Branch: 2 branches
BranchTrueFalseTotal
if (leaf->code > T_IPV6 || leaf->code == T_ARRAY 66 0 66
if (leaf->code > T_IPV6 || leaf->code == T_ARRAY 65 1 66
849 65 ▶ || leaf->code == T_TUPLE || leaf->code == T_NULLABLE)
Branch: 2 branches
BranchTrueFalseTotal
|| leaf->code == T_TUPLE || leaf->code == T_NULLABLE) 65 0 65
|| leaf->code == T_TUPLE || leaf->code == T_NULLABLE) 0 65 65
850 1 croak("LowCardinality(T) supports scalar inner types");
851 } else {
852 2 croak("Unknown type: %.*s", (int)len, type);
853 }
854  
855 36175 ▶ 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) { 35743 432 36175
if ((t->code == T_DATETIME || t->code == T_DATETIME64) && !t->timezone) { 49 35694 35743
if ((t->code == T_DATETIME || t->code == T_DATETIME64) && !t->timezone) { 481 0 481
856 481 const char *quote = memchr(type, '\'', len);
857 481 ▶ if (quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (quote) { 97 384 481
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 36110 ▶ 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 36080 30 36110
871 72285 ▶ || t->code == T_JSON || t->code == T_DYNAMIC;
Branch: 3 branches
BranchTrueFalseTotal
|| t->code == T_JSON || t->code == T_DYNAMIC; 36110 65 36175
|| t->code == T_JSON || t->code == T_DYNAMIC; 35944 136 36080
|| t->code == T_JSON || t->code == T_DYNAMIC; 25 35919 35944
872 36175 ▶ 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; 2445 33730 36175
if (t->inner && t->inner->has_prefix) t->split_prefix = 1; 32 2413 2445
873 int pi;
874 38471 ▶ for (pi = 0; pi < t->tuple_len; pi++)
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < t->tuple_len; pi++) 2296 36175 38471
875 2296 ▶ 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 2281 2296
876 36175 t->has_prefix |= t->split_prefix;
877  
878 /* Disarm the slot: caller now owns t. */
879 36175 *slot = NULL;
880 36175 return t;
881 }
« scalar_kind.c ↑ Summary json_kind.c »