xs/proto_native_build.c

filter: Statementstmt 92.6% 76 / 82 Branchbran 46.8% 15 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.8% 91 / 114 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/queues.c ↑ Summary
1 /* --- Native protocol packet builders --- */
2  
3 27 static char* build_native_hello(ev_clickhouse_t *self, size_t *out_len) {
4 native_buf_t b;
5 27 nbuf_init(&b);
6  
7 27 nbuf_varuint(&b, CLIENT_HELLO);
8 27 nbuf_cstring(&b, CH_CLIENT_NAME);
9 27 nbuf_varuint(&b, CH_CLIENT_VERSION_MAJOR);
10 27 nbuf_varuint(&b, CH_CLIENT_VERSION_MINOR);
11 27 nbuf_varuint(&b, CH_CLIENT_REVISION);
12 27 ▶ nbuf_cstring(&b, self->database ? self->database : "default");
Branch: 1 branch
BranchTrueFalseTotal
nbuf_cstring(&b, self->database ? self->database : "default"); 27 0 27
13 27 ▶ nbuf_cstring(&b, self->user ? self->user : "default");
Branch: 1 branch
BranchTrueFalseTotal
nbuf_cstring(&b, self->user ? self->user : "default"); 27 0 27
14 27 ▶ nbuf_cstring(&b, self->password ? self->password : "");
Branch: 1 branch
BranchTrueFalseTotal
nbuf_cstring(&b, self->password ? self->password : ""); 27 0 27
15  
16 27 *out_len = b.len;
17 27 return b.data;
18 }
19  
20 2 static char* build_native_ping(size_t *out_len) {
21 native_buf_t b;
22 2 nbuf_init(&b);
23 2 nbuf_varuint(&b, CLIENT_PING);
24 2 *out_len = b.len;
25 2 return b.data;
26 }
27  
28 /* Write a Data-block info header (field_num=1 is_overflows + field_num=2
29 * bucket_num=-1 + end marker). Required for revision >= DBMS_MIN_REVISION_WITH_BLOCK_INFO. */
30 35 static void nbuf_block_info(native_buf_t *b) {
31 35 int32_t bucket = -1;
32 35 nbuf_varuint(b, 1); /* field_num = 1 */
33 35 nbuf_u8(b, 0); /* is_overflows = false */
34 35 nbuf_varuint(b, 2); /* field_num = 2 */
35 35 nbuf_append(b, (const char *)&bucket, 4); /* bucket_num = -1 */
36 35 nbuf_varuint(b, 0); /* end of block info */
37 35 }
38  
39 /* Build an empty Data block (signals end of client data after Query) */
40 31 static void nbuf_empty_data_block(native_buf_t *b, int do_compress) {
41 31 nbuf_varuint(b, CLIENT_DATA);
42 31 nbuf_cstring(b, ""); /* table name — outside compression */
43  
44 /* block body: block info + num_cols + num_rows */
45 #ifdef HAVE_LZ4
46 if (do_compress) {
47 native_buf_t body;
48 char *compressed;
49 size_t comp_len;
50  
51 nbuf_init(&body);
52 nbuf_block_info(&body);
53 nbuf_varuint(&body, 0); /* num_columns = 0 */
54 nbuf_varuint(&body, 0); /* num_rows = 0 */
55  
56 compressed = ch_lz4_compress(body.data, body.len, &comp_len);
57 Safefree(body.data);
58 if (compressed) {
59 nbuf_append(b, compressed, comp_len);
60 Safefree(compressed);
61 return;
62 }
63 /* LZ4 failed (should never happen) — fall through to uncompressed */
64 }
65 #else
66 (void)do_compress;
67 #endif
68  
69 31 nbuf_block_info(b);
70 31 nbuf_varuint(b, 0); /* num_columns = 0 */
71 31 nbuf_varuint(b, 0); /* num_rows = 0 */
72 31 }
73  
74 19 static char* build_native_query(ev_clickhouse_t *self, const char *sql,
75 size_t sql_len, HV *defaults, HV *overrides,
76 const char *ext_data, size_t ext_len,
77 size_t *out_len) {
78 native_buf_t b;
79 19 const char *query_id = NULL;
80 19 STRLEN query_id_len = 0;
81 19 nbuf_init(&b);
82  
83 /* Pre-scan settings for query_id (needed before settings block).
84 * Undef values mean absent (matches the settings serializers). */
85 {
86 SV **svp;
87 19 ▶ if (overrides && (svp = hv_fetch(overrides, "query_id", 8, 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (overrides && (svp = hv_fetch(overrides, "query_id", 8, 0))) { 2 17 19
if (overrides && (svp = hv_fetch(overrides, "query_id", 8, 0))) { 2 0 2
88 2 ▶ if (SvGMAGICAL(*svp)) mg_get(*svp);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(*svp)) mg_get(*svp); 0 2 2
89 4 ▶ if (SvOK(*svp)) query_id = SvPV_nomg(*svp, query_id_len);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(*svp)) query_id = SvPV_nomg(*svp, query_id_len); 2 0 2
90 }
91 19 ▶ if (!query_id && defaults &&
Branch: 1 branch
BranchTrueFalseTotal
if (!query_id && defaults && 17 2 19
92 0 (svp = hv_fetch(defaults, "query_id", 8, 0))) {
93 0 ▶ if (SvGMAGICAL(*svp)) mg_get(*svp);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(*svp)) mg_get(*svp); 0 0 0
94 0 ▶ if (SvOK(*svp)) query_id = SvPV_nomg(*svp, query_id_len);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(*svp)) query_id = SvPV_nomg(*svp, query_id_len); 0 0 0
95 }
96 }
97  
98 /* Query packet */
99 19 nbuf_varuint(&b, CLIENT_QUERY);
100 19 ▶ nbuf_string(&b, query_id ? query_id : "", query_id_len);
Branch: 1 branch
BranchTrueFalseTotal
nbuf_string(&b, query_id ? query_id : "", query_id_len); 2 17 19
101  
102 /* Client info — field order must match ClientInfo::read() */
103 19 nbuf_u8(&b, QUERY_INITIAL);
104 19 nbuf_cstring(&b, ""); /* initial_user */
105 19 nbuf_cstring(&b, ""); /* initial_query_id */
106 19 nbuf_cstring(&b, "[::ffff:127.0.0.1]:0"); /* initial_address */
107  
108 /* initial_query_start_time_microseconds (revision >= 54449) */
109 {
110 19 uint64_t zero64 = 0;
111 19 nbuf_append(&b, (const char *)&zero64, 8);
112 }
113  
114 /* iface_type: 1=TCP, os_user, client_hostname, client_name */
115 19 nbuf_u8(&b, 1);
116 19 nbuf_cstring(&b, ""); /* os_user */
117 19 nbuf_cstring(&b, ""); /* client_hostname */
118 19 nbuf_cstring(&b, CH_CLIENT_NAME);
119 19 nbuf_varuint(&b, CH_CLIENT_VERSION_MAJOR);
120 19 nbuf_varuint(&b, CH_CLIENT_VERSION_MINOR);
121 19 nbuf_varuint(&b, CH_CLIENT_REVISION);
122  
123 /* quota_key_in_client_info (always present, revision >= ~54060) */
124 19 nbuf_cstring(&b, "");
125  
126 /* distributed_depth (revision >= 54448) */
127 19 nbuf_varuint(&b, 0);
128  
129 /* version_patch (revision >= 54401) */
130 19 nbuf_varuint(&b, 0);
131  
132 /* OpenTelemetry trace context (revision >= 54442): no trace */
133 19 nbuf_u8(&b, 0);
134  
135 /* parallel_replicas (revision >= 54453) */
136 19 nbuf_varuint(&b, 0); /* collaborate_with_initiator */
137 19 nbuf_varuint(&b, 0); /* count_participating_replicas */
138 19 nbuf_varuint(&b, 0); /* number_of_current_replica */
139  
140 /* Settings (serialized as strings: revision >= 54429)
141 * Format: repeated (String name, UInt8 is_important, String value),
142 * terminated by empty name. */
143 19 write_native_settings(&b, defaults, overrides);
144 #ifdef HAVE_LZ4
145 /* We decode LZ4 only: pin the method so a server whose default is
146 * ZSTD (or anything else) still answers in a form we can read.
147 * An explicit user setting always wins. */
148 if (self->compress
149 && !(overrides && hv_exists(overrides, "network_compression_method", 26))
150 && !(defaults && hv_exists(defaults, "network_compression_method", 26))) {
151 nbuf_varuint(&b, 26);
152 nbuf_append(&b, "network_compression_method", 26);
153 nbuf_u8(&b, 0); /* is_important = 0 */
154 nbuf_varuint(&b, 3);
155 nbuf_append(&b, "LZ4", 3);
156 }
157 #endif
158 19 nbuf_cstring(&b, ""); /* empty name = end of settings */
159  
160 /* interserver_secret: empty string (revision >= 54441) */
161 19 nbuf_cstring(&b, "");
162  
163 /* state (stage), compression, query */
164 19 nbuf_varuint(&b, STAGE_COMPLETE);
165 #ifdef HAVE_LZ4
166 nbuf_varuint(&b, self->compress ? 1 : 0);
167 #else
168 19 nbuf_varuint(&b, 0);
169 #endif
170 19 nbuf_string(&b, sql, sql_len);
171  
172 /* Parameters block: param_* keys, terminated by empty name.
173 * Servers below 54459 (22.1-22.7) read the terminator as the next
174 * packet and stall, so omit the whole block there — and fail
175 * callers that actually pass parameters rather than drop them. */
176 19 ▶ if (self->server_revision == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision == 0 || 19 0 19
177 19 ▶ self->server_revision >= DBMS_MIN_REVISION_WITH_PARAMETERS) {
Branch: 1 branch
BranchTrueFalseTotal
self->server_revision >= DBMS_MIN_REVISION_WITH_PARAMETERS) { 19 0 19
178 19 write_native_params(&b, defaults, overrides);
179 19 nbuf_cstring(&b, ""); /* end of parameters */
180 }
181 0 ▶ else if (write_native_params(&b, defaults, overrides) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (write_native_params(&b, defaults, overrides) > 0) { 0 0 0
182 0 Safefree(b.data);
183 0 return NULL;
184 }
185  
186 /* External tables: named Data packets the query can reference as
187 * tables, sent before the terminating empty block. */
188 19 ▶ if (ext_data && ext_len) nbuf_append(&b, ext_data, ext_len);
Branch: 2 branches
BranchTrueFalseTotal
if (ext_data && ext_len) nbuf_append(&b, ext_data, ext_len); 0 19 19
if (ext_data && ext_len) nbuf_append(&b, ext_data, ext_len); 0 0 0
189  
190 19 nbuf_empty_data_block(&b, self->compress);
191  
192 19 *out_len = b.len;
193 19 return b.data;
194 }
195  
« xs/queues.c ↑ Summary