xs/proto_native_build.c

filter: Statementstmt 85.8% 79 / 92 Branchbran 38.1% 16 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.9% 95 / 134 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, const ev_clickhouse_t *self) {
41 native_buf_t body;
42 31 nbuf_varuint(b, CLIENT_DATA);
43 31 nbuf_cstring(b, ""); /* table name — outside compression */
44 31 nbuf_init(&body);
45 31 nbuf_block_info(&body);
46 31 nbuf_varuint(&body, 0); /* num_columns = 0 */
47 31 nbuf_varuint(&body, 0); /* num_rows = 0 */
48 31 nbuf_put_block(b, &body, self);
49 31 }
50  
51 19 static char* build_native_query(ev_clickhouse_t *self, const char *sql,
52 size_t sql_len, HV *defaults, HV *overrides,
53 const char *ext_data, size_t ext_len,
54 size_t *out_len) {
55 native_buf_t b;
56 19 const char *query_id = NULL;
57 19 STRLEN query_id_len = 0;
58 19 nbuf_init(&b);
59  
60 /* Pre-scan settings for query_id (needed before settings block).
61 * Undef values mean absent (matches the settings serializers). */
62 {
63 SV **svp;
64 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
65 2 ▶ if (SvGMAGICAL(*svp)) mg_get(*svp);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(*svp)) mg_get(*svp); 0 2 2
66 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
67 }
68 19 ▶ if (!query_id && defaults &&
Branch: 1 branch
BranchTrueFalseTotal
if (!query_id && defaults && 17 2 19
69 0 (svp = hv_fetch(defaults, "query_id", 8, 0))) {
70 0 ▶ if (SvGMAGICAL(*svp)) mg_get(*svp);
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(*svp)) mg_get(*svp); 0 0 0
71 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
72 }
73 }
74  
75 /* Query packet */
76 19 nbuf_varuint(&b, CLIENT_QUERY);
77 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
78  
79 /* Client info — field order must match ClientInfo::read() */
80 19 nbuf_u8(&b, QUERY_INITIAL);
81 19 nbuf_cstring(&b, ""); /* initial_user */
82 19 nbuf_cstring(&b, ""); /* initial_query_id */
83 19 nbuf_cstring(&b, "[::ffff:127.0.0.1]:0"); /* initial_address */
84  
85 /* initial_query_start_time_microseconds (revision >= 54449) */
86 {
87 19 uint64_t zero64 = 0;
88 19 nbuf_append(&b, (const char *)&zero64, 8);
89 }
90  
91 /* iface_type: 1=TCP, os_user, client_hostname, client_name */
92 19 nbuf_u8(&b, 1);
93 19 nbuf_cstring(&b, ""); /* os_user */
94 19 nbuf_cstring(&b, ""); /* client_hostname */
95 19 nbuf_cstring(&b, CH_CLIENT_NAME);
96 19 nbuf_varuint(&b, CH_CLIENT_VERSION_MAJOR);
97 19 nbuf_varuint(&b, CH_CLIENT_VERSION_MINOR);
98 19 nbuf_varuint(&b, CH_CLIENT_REVISION);
99  
100 /* quota_key_in_client_info (always present, revision >= ~54060) */
101 19 nbuf_cstring(&b, "");
102  
103 /* distributed_depth (revision >= 54448) */
104 19 nbuf_varuint(&b, 0);
105  
106 /* version_patch (revision >= 54401) */
107 19 nbuf_varuint(&b, 0);
108  
109 /* OpenTelemetry trace context (revision >= 54442): no trace */
110 19 nbuf_u8(&b, 0);
111  
112 /* parallel_replicas (revision >= 54453) */
113 19 nbuf_varuint(&b, 0); /* collaborate_with_initiator */
114 19 nbuf_varuint(&b, 0); /* count_participating_replicas */
115 19 nbuf_varuint(&b, 0); /* number_of_current_replica */
116  
117 /* Settings (serialized as strings: revision >= 54429)
118 * Format: repeated (String name, UInt8 is_important, String value),
119 * terminated by empty name. */
120 19 write_native_settings(&b, defaults, overrides);
121 /* We decode LZ4 only: pin the method so a server whose default is
122 * ZSTD (or anything else) still answers in a form we can read.
123 * An explicit user setting always wins. */
124 19 ▶ if (self->compress
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress 0 19 19
125 0 ▶ && !(overrides && hv_exists(overrides, "network_compression_method", 26))
Branch: 2 branches
BranchTrueFalseTotal
&& !(overrides && hv_exists(overrides, "network_compression_method", 26)) 0 0 0
&& !(overrides && hv_exists(overrides, "network_compression_method", 26)) 0 0 0
126 0 ▶ && !(defaults && hv_exists(defaults, "network_compression_method", 26))) {
Branch: 2 branches
BranchTrueFalseTotal
&& !(defaults && hv_exists(defaults, "network_compression_method", 26))) { 0 0 0
&& !(defaults && hv_exists(defaults, "network_compression_method", 26))) { 0 0 0
127 0 nbuf_varuint(&b, 26);
128 0 nbuf_append(&b, "network_compression_method", 26);
129 0 nbuf_u8(&b, 0); /* is_important = 0 */
130 0 nbuf_varuint(&b, 3);
131 0 nbuf_append(&b, "LZ4", 3);
132 }
133 19 nbuf_cstring(&b, ""); /* empty name = end of settings */
134  
135 /* interserver_secret: empty string (revision >= 54441) */
136 19 nbuf_cstring(&b, "");
137  
138 /* state (stage), compression, query */
139 19 nbuf_varuint(&b, STAGE_COMPLETE);
140 19 nbuf_varuint(&b, self->compress ? 1 : 0);
141 19 nbuf_string(&b, sql, sql_len);
142  
143 /* Parameters block: param_* keys, terminated by empty name.
144 * Servers below 54459 (22.1-22.7) read the terminator as the next
145 * packet and stall, so omit the whole block there — and fail
146 * callers that actually pass parameters rather than drop them. */
147 19 ▶ if (self->server_revision == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (self->server_revision == 0 || 19 0 19
148 19 ▶ self->server_revision >= DBMS_MIN_REVISION_WITH_PARAMETERS) {
Branch: 1 branch
BranchTrueFalseTotal
self->server_revision >= DBMS_MIN_REVISION_WITH_PARAMETERS) { 19 0 19
149 19 write_native_params(&b, defaults, overrides);
150 19 nbuf_cstring(&b, ""); /* end of parameters */
151 }
152 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
153 0 Safefree(b.data);
154 0 return NULL;
155 }
156  
157 /* External tables: named Data packets the query can reference as
158 * tables, sent before the terminating empty block. */
159 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
160  
161 19 nbuf_empty_data_block(&b, self);
162  
163 19 *out_len = b.len;
164 19 return b.data;
165 }
166  
« xs/queues.c ↑ Summary