blib/lib/ClickHouse/Encoder/TCP.pm

filter: Statementstmt 99.6% 271 / 272 Branchbran 81.3% 96 / 118 Conditioncond 70.6% 77 / 109 MC/DCmcdc 44.6% 25 / 56 Subroutinesub 100% 40 / 40 Podpod 100% 10 / 10 totaltotal 85.7% 519 / 605 CC 108 SCAR 27.1
CC
108
Cov
89%
unpack_packet
29.3
_pack_client_info
22.6
read_packet
21.4
CRAP 15.0
« blib/lib/ClickHouse/Encoder.pm ↑ Summary datetime.c »
1 package ClickHouse::Encoder::TCP; ## no critic (Capitalization)
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION = '0.04';
6  
7 # Encoders/decoders for a useful subset of ClickHouse's native TCP
8 # protocol packets. Built for insert pipelines: pack a Hello + Query,
9 # then wrap encoded Native blocks in Data packets, then signal
10 # end-of-insert. Transport is the caller's job (IO::Socket,
11 # AnyEvent::Handle, IO::Async::Stream, etc.).
12 #
13 # Varint and length-prefixed string codecs are XS (shared with the
14 # main encoder's buffer helpers); packet-level layout stays in Perl
15 # for readability since it's not a hot path.
16 #
17 # Targets negotiated revision 54429; newer fields require a full client.
18  
19 # XS loader handled by the parent module; ensure it's loaded so the
20 # XSUBs under PACKAGE = ClickHouse::Encoder::TCP are available.
21 4 ▶use ClickHouse::Encoder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 ## no critic (ProhibitConstantPragma)
25 # Readability beats Readonly here - these are protocol-defined
26 # numeric tags, used as bare identifiers throughout the module.
27 4 ▶use constant DEFAULT_REVISION => 54429;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Client packet types
30 4 ▶use constant CLIENT_HELLO => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use constant CLIENT_QUERY => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use constant CLIENT_DATA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use constant CLIENT_CANCEL => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use constant CLIENT_PING => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # Server packet types
37 4 ▶use constant SERVER_HELLO => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use constant SERVER_DATA => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use constant SERVER_EXCEPTION => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use constant SERVER_PROGRESS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use constant SERVER_PONG => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use constant SERVER_END_OF_STREAM => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use constant SERVER_PROFILE_INFO => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use constant SERVER_TOTALS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶use constant SERVER_EXTREMES => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use constant SERVER_LOG => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use constant SERVER_TABLE_COLUMNS => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use constant SERVER_PROFILE_EVENTS => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # Query processing stages
51 4 ▶use constant STAGE_FETCH_COLUMNS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 4 ▶use constant STAGE_WITH_MERGEABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 ▶use constant STAGE_COMPLETE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # Compression flags in Query packet
56 4 ▶use constant COMPRESSION_DISABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use constant COMPRESSION_ENABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # Varint and length-prefixed string codecs are XS-backed; the bound
60 # subs (pack_varint, unpack_varint, pack_string, unpack_string)
61 # live in PACKAGE = ClickHouse::Encoder::TCP inside Encoder.xs.
62  
63 # ----- client packets ------------------------------------------------
64  
65 # Hello packet: announces our protocol revision and credentials.
66 #
67 # pack_hello(
68 # client_name => 'ClickHouse::Encoder', # default
69 # major => 1, # default
70 # minor => 0, # default
71 # revision => 54429, # default
72 # database => 'default',
73 # user => 'default',
74 # password => '',
75 # );
76 1 ▶sub pack_hello {
Subroutine pack_hello: called CC=8 SCAR=25.9
Pod documented
77 1 my (undef, %o) = @_;
78 return pack_varint(CLIENT_HELLO)
79 . pack_string($o{client_name} // 'ClickHouse::Encoder')
80 . pack_varint($o{major} // 1)
81 . pack_varint($o{minor} // 0)
82 . pack_varint($o{revision} // DEFAULT_REVISION)
83 . pack_string($o{database} // 'default')
84 . pack_string($o{user} // 'default')
85 1 ▶ . pack_string($o{password} // '');
Condition50%7/14
$o{"client_name"} // "ClickHouse::Encoder"
l!l
10
$o{"major"} // 1
l!l
01
$o{"minor"} // 0
l!l
01
$o{"revision"} // 54429
l!l
01
$o{"database"} // "default"
l!l
10
$o{"user"} // "default"
l!l
10
$o{"password"} // ""
l!l
10
MC/DC0%0/1
$o{"client_name"} // "ClickHouse::Encoder"
$o{"client_name"}
MC/DC0%0/1
$o{"major"} // 1
$o{"major"}
MC/DC0%0/1
$o{"minor"} // 0
$o{"minor"}
MC/DC0%0/1
$o{"revision"} // 54429
$o{"revision"}
MC/DC0%0/1
$o{"database"} // "default"
$o{"database"}
MC/DC0%0/1
$o{"user"} // "default"
$o{"user"}
MC/DC0%0/1
$o{"password"} // ""
$o{"password"}
Truth table50%1/2
A
A= $o{"client_name"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"major"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"minor"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"revision"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"database"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"password"}
Aresult
11
00
86 }
87  
88 # Query packet. At revision 54429 the body is:
89 # varint CLIENT_QUERY (=1)
90 # string query_id (often empty so the server generates one)
91 # <ClientInfo> (block - see _pack_client_info)
92 # <Settings> (see _pack_settings)
93 # varint query_processing_stage (default Complete)
94 # varint compression flag (default Disable)
95 # string query SQL text
96 13 ▶sub pack_query {
Subroutine pack_query: called CC=9 SCAR=23.5
Pod documented
97 13 my (undef, %o) = @_;
98 13 ▶ my $rev = $o{revision} // DEFAULT_REVISION;
Condition100%2/2
$o{"revision"} // 54429
l!l
310
MC/DC100%1/1
$o{"revision"} // 54429
$o{"revision"}
Truth table100%2/2
A
A= $o{"revision"}
Aresult
11
00
99 return pack_varint(CLIENT_QUERY)
100 . pack_string($o{query_id} // '')
101 . ($rev >= 54032 ? _pack_client_info(\%o) : '')
102 . _pack_settings($o{settings})
103 . pack_varint($o{stage} // STAGE_COMPLETE)
104 . pack_varint($o{compression} // COMPRESSION_DISABLE)
105 . pack_string($o{query} // die "pack_query: 'query' is required\n")
106 . __PACKAGE__->pack_data_end(revision => $rev,
107 13 ▶ compress => $o{compression} ? ($o{compress} // 'lz4') : 'raw');
Branch: 2 branches
BranchTrueFalseTotal
$rev >= 54032 ? : 13 0 13
$o{'compression'} ? : 0 12 12
Condition70%7/10
$o{"query_id"} // ""
l!l
76
$o{"stage"} // 2
l!l
310
$o{"compression"} // 0
l!l
013
$o{"query"} // die("pack_query: 'query' is required\n")
l!l
121
$o{"compress"} // "lz4"
l!l
00
MC/DC100%1/1
$o{"query_id"} // ""
$o{"query_id"}
MC/DC100%1/1
$o{"stage"} // 2
$o{"stage"}
MC/DC0%0/1
$o{"compression"} // 0
$o{"compression"}
MC/DC100%1/1
$o{"query"} // die("pack_query: 'query' is required\n")
$o{"query"}
MC/DC0%0/1
$o{"compress"} // "lz4"
$o{"compress"}
Truth table100%2/2
A
A= $o{"query_id"}
Aresult
11
00
Truth table100%2/2
A
A= $o{"stage"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"compression"}
Aresult
11
00
Truth table100%2/2
A
A= $o{"query"}
Aresult
11
00
Truth table0%0/2
A
A= $o{"compress"}
Aresult
11
00
108 }
109  
110 # ClientInfo subblock. At revision 54429 the fields after the
111 # fixed prefix are: quota_key (since 54058), version_patch (since
112 # 54401). We use those defaults; later-revision fields (interserver
113 # secret, OpenTelemetry trace context, parallel-replica metadata)
114 # would need a higher client revision to be negotiated.
115 13 ▶sub _pack_client_info {
Subroutine _pack_client_info: called CC=17 SCAR=31.2
116 13 my $o = shift;
117 13 ▶ my $rev = $o->{revision} // DEFAULT_REVISION;
Condition100%2/2
$o->{"revision"} // 54429
l!l
310
MC/DC100%1/1
$o->{"revision"} // 54429
$o->{"revision"}
Truth table100%2/2
A
A= $o->{"revision"}
Aresult
11
00
118 13 my $out = '';
119 13 ▶ my $kind = $o->{query_kind} // 1;
Condition50%1/2
$o->{"query_kind"} // 1
l!l
013
MC/DC0%0/1
$o->{"query_kind"} // 1
$o->{"query_kind"}
Truth table50%1/2
A
A= $o->{"query_kind"}
Aresult
11
00
120 13 ▶ return chr(0) if !$kind;
Branch: 1 branch
BranchTrueFalseTotal
unless $kind 0 13 13
121 13 $out .= chr($kind); # 1 = initial query
122  
123 13 ▶ $out .= pack_string($o->{initial_user} // '');
Condition50%1/2
$o->{"initial_user"} // ""
l!l
013
MC/DC0%0/1
$o->{"initial_user"} // ""
$o->{"initial_user"}
Truth table50%1/2
A
A= $o->{"initial_user"}
Aresult
11
00
124 13 ▶ $out .= pack_string($o->{initial_query_id} // '');
Condition50%1/2
$o->{"initial_query_id"} // ""
l!l
013
MC/DC0%0/1
$o->{"initial_query_id"} // ""
$o->{"initial_query_id"}
Truth table50%1/2
A
A= $o->{"initial_query_id"}
Aresult
11
00
125 13 ▶ $out .= pack_string($o->{initial_address} // '0.0.0.0:0');
Condition50%1/2
$o->{"initial_address"} // "0.0.0.0:0"
l!l
013
MC/DC0%0/1
$o->{"initial_address"} // "0.0.0.0:0"
$o->{"initial_address"}
Truth table50%1/2
A
A= $o->{"initial_address"}
Aresult
11
00
126 # interface: 1 = TCP
127 13 $out .= chr(1);
128 # os_user, client_hostname, client_name
129 13 ▶ $out .= pack_string($o->{os_user} // '');
Condition50%1/2
$o->{"os_user"} // ""
l!l
013
MC/DC0%0/1
$o->{"os_user"} // ""
$o->{"os_user"}
Truth table50%1/2
A
A= $o->{"os_user"}
Aresult
11
00
130 13 ▶ $out .= pack_string($o->{client_hostname} // 'localhost');
Condition50%1/2
$o->{"client_hostname"} // "localhost"
l!l
013
MC/DC0%0/1
$o->{"client_hostname"} // "localhost"
$o->{"client_hostname"}
Truth table50%1/2
A
A= $o->{"client_hostname"}
Aresult
11
00
131 13 ▶ $out .= pack_string($o->{client_name} // 'ClickHouse::Encoder');
Condition50%1/2
$o->{"client_name"} // "ClickHouse::Encoder"
l!l
013
MC/DC0%0/1
$o->{"client_name"} // "ClickHouse::Encoder"
$o->{"client_name"}
Truth table50%1/2
A
A= $o->{"client_name"}
Aresult
11
00
132 # client_version_{major,minor}, client_revision
133 13 ▶ $out .= pack_varint($o->{client_version_major} // 1);
Condition50%1/2
$o->{"client_version_major"} // 1
l!l
013
MC/DC0%0/1
$o->{"client_version_major"} // 1
$o->{"client_version_major"}
Truth table50%1/2
A
A= $o->{"client_version_major"}
Aresult
11
00
134 13 ▶ $out .= pack_varint($o->{client_version_minor} // 0);
Condition50%1/2
$o->{"client_version_minor"} // 0
l!l
013
MC/DC0%0/1
$o->{"client_version_minor"} // 0
$o->{"client_version_minor"}
Truth table50%1/2
A
A= $o->{"client_version_minor"}
Aresult
11
00
135 13 ▶ $out .= pack_varint($o->{client_revision} // $rev);
Condition33%1/3
$o->{"client_revision"} // $rev
l!l&&r!l&&!r
0130
MC/DC0%0/2
$o->{"client_revision"} // $rev
$o->{"client_revision"} $rev
Truth table33%1/3
A // B
A= $o->{"client_revision"}B= $rev
ABresult
1X1
011
000
136 # quota_key (rev >= 54058)
137 13 ▶ $out .= pack_string($o->{quota_key} // '') if $rev >= 54058;
Branch: 1 branch
BranchTrueFalseTotal
if $rev >= 54058 12 1 13
Condition50%1/2
$o->{"quota_key"} // ""
l!l
012
MC/DC0%0/1
$o->{"quota_key"} // ""
$o->{"quota_key"}
Truth table50%1/2
A
A= $o->{"quota_key"}
Aresult
11
00
138 # client_version_patch (rev >= 54401)
139 13 ▶ $out .= pack_varint($o->{client_version_patch} // 0)
Branch: 1 branch
BranchTrueFalseTotal
if $rev >= 54401 11 2 13
Condition50%1/2
$o->{"client_version_patch"} // 0
l!l
011
MC/DC0%0/1
$o->{"client_version_patch"} // 0
$o->{"client_version_patch"}
Truth table50%1/2
A
A= $o->{"client_version_patch"}
Aresult
11
00
140 if $rev >= 54401;
141 13 return $out;
142 }
143  
144 # Settings block. At revision >= 54429 each entry is:
145 # string name + varint flags + string value
146 # terminated by an empty-key string. flags is a bitmask where 0 means
147 # "ordinary setting"; bit 0x01 = IMPORTANT, bit 0x02 = CUSTOM. We
148 # always emit flags=0 - the server still accepts any setting at flag 0.
149 14 ▶sub _pack_settings {
Subroutine _pack_settings: called CC=4 SCAR=13.9
150 14 my $s = shift;
151 14 ▶ return pack_string('') unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 10 4 14
152 4 ▶ if (ref $s eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s eq "HASH") 3 1 4
153 3 my $body = '';
154 3 for my $k (sort keys %$s) {
155 4 $body .= pack_string($k);
156 4 $body .= pack_varint(0); # flags: ordinary
157 4 $body .= pack_string($s->{$k});
158 }
159 3 $body .= pack_string(''); # end marker
160 3 return $body;
161 }
162 # Allow caller to pass raw bytes (already in the right shape).
163 1 return $s;
164 }
165  
166 # Data packet wrapping a Native block. table_name is usually empty
167 # for inserts; the server already knows the target from the Query.
168 # With compress => 'lz4' (or 'zstd') the block is wrapped in CH's
169 # compressed-block framing (16-byte CityHash128 + 9-byte header +
170 # compressed payload) before being placed on the wire; the server
171 # must have been told to expect compressed data via the Query packet's
172 # compression flag (see pack_query's `compression =E<gt>
173 # COMPRESSION_ENABLE`).
174 21 ▶sub pack_block_info { return "\x01\x00\x02\xff\xff\xff\xff\x00" }
Subroutine pack_block_info: called CC=1 SCAR=0.0
Pod documented
175  
176 13 ▶sub unpack_block_info {
Subroutine unpack_block_info: called CC=8 SCAR=20.9
Pod documented
177 13 my ($bytes, $offset) = @_;
178 13 my %info;
179 13 while (1) {
180 39 (my $field, $offset) = unpack_varint($bytes, $offset);
181 39 ▶ last if !$field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 13 26 39
182 26 ▶ my $width = $field == 1 ? 1 : $field == 2 ? 4 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$field == 1 ? : 13 13 26
$field == 2 ? : 13 0 13
183 26 ▶ die "BlockInfo: unknown field $field\n" unless $width;
Branch: 1 branch
BranchTrueFalseTotal
unless $width 0 26 26
184 26 ▶ die "BlockInfo: truncated at offset $offset\n" if $offset + $width > length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $offset + $width > length $bytes 0 26 26
185 26 ▶ $info{$field == 1 ? 'is_overflows' : 'bucket_num'} = $field == 1
Branch: 2 branches
BranchTrueFalseTotal
$field == 1 ? : 13 13 26
$field == 1 ? : 13 13 26
186 ? ord substr($bytes, $offset, 1)
187 : unpack('l<', substr($bytes, $offset, 4));
188 26 $offset += $width;
189 }
190 13 return (\%info, $offset);
191 }
192  
193 19 ▶sub pack_data {
Subroutine pack_data: called CC=9 SCAR=22.3
Pod documented
194 19 my (undef, $block_bytes, %o) = @_;
195 19 ▶ die "pack_data: block bytes required\n" unless defined $block_bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block_bytes 1 18 19
196 18 ▶ my $rev = $o{revision} // DEFAULT_REVISION;
Condition100%2/2
$o{"revision"} // 54429
l!l
126
MC/DC100%1/1
$o{"revision"} // 54429
$o{"revision"}
Truth table100%2/2
A
A= $o{"revision"}
Aresult
11
00
197 18 ▶ $block_bytes = pack_block_info() . $block_bytes if $rev >= 51903;
Branch: 1 branch
BranchTrueFalseTotal
if $rev >= 51903 18 0 18
198 18 ▶ if (defined $o{compress} && $o{compress} ne 'none' && $o{compress} ne 'raw') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $o{"compress"} and $o{"compress"} ne "none" and $o{"compress"} ne "raw") 4 14 18
Condition83%5/6
defined $o{"compress"} and $o{"compress"} ne "none" and $o{"compress"} ne "raw"
!ll&&!rl&&r
2124
defined $o{"compress"} and $o{"compress"} ne "none"
!ll&&!rl&&r
2016
MC/DC66%2/3
defined $o{"compress"} and $o{"compress"} ne "none" and $o{"compress"} ne "raw"
defined $o{"compress"} $o{"compress"} ne "none" $o{"compress"} ne "raw"
Truth table75%3/4
A and B and C
A= defined $o{"compress"}B= $o{"compress"} ne "none"C= $o{"compress"} ne "raw"
ABCresult
0XX0
10X0
1100
1111
199 4 $block_bytes = ClickHouse::Encoder->compress_native_block($block_bytes, mode => $o{compress});
200 }
201 return pack_varint(CLIENT_DATA)
202 18 ▶ . ($rev >= 50264 ? pack_string($o{table_name} // '') : '')
Branch: 1 branch
BranchTrueFalseTotal
$rev >= 50264 ? : 18 0 18
Condition50%1/2
$o{"table_name"} // ""
l!l
018
MC/DC0%0/1
$o{"table_name"} // ""
$o{"table_name"}
Truth table50%1/2
A
A= $o{"table_name"}
Aresult
11
00
203 . $block_bytes;
204 }
205  
206 15 ▶sub pack_data_end {
Subroutine pack_data_end: called CC=1 SCAR=0.0
Pod documented
207 15 my ($class, %o) = @_;
208 15 return $class->pack_data("\x00\x00", %o);
209 }
210  
211 1 ▶sub pack_ping {
Subroutine pack_ping: called CC=1 SCAR=0.0
Pod documented
212 1 return pack_varint(CLIENT_PING);
213 }
214  
215 1 ▶sub pack_cancel {
Subroutine pack_cancel: called CC=1 SCAR=0.0
Pod documented
216 1 return pack_varint(CLIENT_CANCEL);
217 }
218  
219 # ----- server packet decoder -----------------------------------------
220  
221 # Parse one server packet from $bytes starting at $offset. Returns
222 # (\%packet, $new_offset). %packet always has 'type' (numeric);
223 # payload fields depend on type. Croaks on truncation; caller can
224 # pre-screen by ensuring at least 1 byte is available.
225 44 ▶sub unpack_packet {
Subroutine unpack_packet: called CC=29 SCAR=33.8
Pod documented
226 44 my (undef, $bytes, $offset, %opts) = @_;
227 44 ▶ $offset //= 0;
Condition100%2/2
$offset //= 0
l!l
431
MC/DC100%1/1
$offset //= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
228 44 (my $type, $offset) = unpack_varint($bytes, $offset);
229 32 my %pkt = (type => $type);
230 32 ▶ my $rev = $opts{revision} // DEFAULT_REVISION;
Condition50%1/2
$opts{"revision"} // 54429
l!l
032
MC/DC0%0/1
$opts{"revision"} // 54429
$opts{"revision"}
Truth table50%1/2
A
A= $opts{"revision"}
Aresult
11
00
231 32 ▶ if ($type == SERVER_HELLO) {
Branch: 7 branches
BranchTrueFalseTotal
if ($type == 0) { } 7 25 32
elsif ($type == 2) { } 3 22 25
elsif ($type == 3) { } 2 20 22
elsif ($type == 4 or $type == 5) { } 9 11 20
elsif ($type == 6) { } 1 10 11
elsif ($type == 1 or $type == 7 or $type == 8 or $type == 10 or $type == 14) { } 8 2 10
elsif ($type == 11) { } 1 1 2
232 7 ($pkt{name}, $offset) = unpack_string($bytes, $offset);
233 5 ($pkt{major}, $offset) = unpack_varint($bytes, $offset);
234 5 ($pkt{minor}, $offset) = unpack_varint($bytes, $offset);
235 5 ($pkt{revision}, $offset) = unpack_varint($bytes, $offset);
236 # Newer revisions add timezone / display_name / version_patch.
237 # Gate on the revision the server just reported, not on "are
238 # there bytes left": a buffer that ends mid-Hello must croak
239 # 'truncated' so the reader fetches more, not silently drop a
240 # field whose bytes simply have not arrived yet. Thresholds
241 # match ClickHouse's DBMS_MIN_REVISION_WITH_* constants.
242 5 ▶ my $hello_rev = $pkt{revision} < $rev ? $pkt{revision} : $rev;
Branch: 1 branch
BranchTrueFalseTotal
$pkt{'revision'} < $rev ? : 1 4 5
243 5 ▶ if ($hello_rev >= 54058) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hello_rev >= 54058) 4 1 5
244 4 ($pkt{timezone}, $offset)
245 = unpack_string($bytes, $offset);
246 }
247 4 ▶ if ($hello_rev >= 54372) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hello_rev >= 54372) 3 1 4
248 3 ($pkt{display_name}, $offset)
249 = unpack_string($bytes, $offset);
250 }
251 4 ▶ if ($hello_rev >= 54401) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hello_rev >= 54401) 3 1 4
252 3 ($pkt{version_patch}, $offset)
253 = unpack_varint($bytes, $offset);
254 }
255 }
256 elsif ($type == SERVER_EXCEPTION) {
257 # ExceptionPacket: code(Int32 LE), name, message, stack_trace, has_nested(byte)
258 3 ▶ die "unpack_packet: truncated at offset $offset"
Branch: 1 branch
BranchTrueFalseTotal
if $offset + 4 > length $bytes 0 3 3
259 if $offset + 4 > length $bytes;
260 3 $pkt{code} = unpack 'l<', substr($bytes, $offset, 4);
261 3 $offset += 4;
262 3 ($pkt{name}, $offset) = unpack_string($bytes, $offset);
263 3 ($pkt{message}, $offset) = unpack_string($bytes, $offset);
264 3 ($pkt{stack_trace}, $offset) = unpack_string($bytes, $offset);
265 3 ▶ die "unpack_packet: truncated at offset $offset"
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= length $bytes 0 3 3
266 if $offset >= length $bytes;
267 3 $pkt{has_nested} = ord substr($bytes, $offset++, 1);
268 3 ▶ if ($pkt{has_nested}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkt{"has_nested"}) 1 2 3
269 1 my ($nested, $next) = __PACKAGE__->unpack_packet(
270 pack_varint(SERVER_EXCEPTION) . substr($bytes, $offset));
271 1 $pkt{nested} = $nested;
272 1 $offset += $next - 1;
273 }
274 }
275 elsif ($type == SERVER_PROGRESS) {
276 # At the targeted protocol revision (54429) a Progress packet
277 # is exactly these five varints. Later revisions append fields
278 # (elapsed_ns, ...), but the Progress packet carries no
279 # revision of its own, so the only safe stopping point without
280 # threading the negotiated revision through is the end of the
281 # 54429 layout. Reading a trailing field "if bytes remain"
282 # would, on a buffer that already holds the next packet,
283 # consume that packet's bytes instead.
284 2 ($pkt{rows}, $offset) = unpack_varint($bytes, $offset);
285 2 ($pkt{bytes}, $offset) = unpack_varint($bytes, $offset);
286 2 ($pkt{total_rows}, $offset) = unpack_varint($bytes, $offset);
287 2 ($pkt{written_rows}, $offset) = unpack_varint($bytes, $offset);
288 2 ($pkt{written_bytes}, $offset) = unpack_varint($bytes, $offset);
289 }
290 ▶ elsif ($type == SERVER_PONG || $type == SERVER_END_OF_STREAM) {
Condition100%3/3
$type == 4 or $type == 5
l!l&&r!l&&!r
6311
MC/DC100%2/2
$type == 4 or $type == 5
$type == 4 $type == 5
Truth table100%3/3
A or B
A= $type == 4B= $type == 5
ABresult
1X1
011
000
291 # No payload.
292 }
293 elsif ($type == SERVER_PROFILE_INFO) {
294 1 ($pkt{rows}, $offset) = unpack_varint($bytes, $offset);
295 1 ($pkt{blocks}, $offset) = unpack_varint($bytes, $offset);
296 1 ($pkt{rows_bytes}, $offset) = unpack_varint($bytes, $offset);
297 1 ▶ die "unpack_packet: truncated at offset $offset"
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= length $bytes 0 1 1
298 if $offset >= length $bytes;
299 1 $pkt{applied_limit} = ord substr($bytes, $offset++, 1);
300 1 ($pkt{rows_before_limit}, $offset)
301 = unpack_varint($bytes, $offset);
302 1 ▶ die "unpack_packet: truncated at offset $offset"
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= length $bytes 0 1 1
303 if $offset >= length $bytes;
304 $pkt{calculated_rows_before_limit}
305 1 = ord substr($bytes, $offset++, 1);
306 }
307 ▶ elsif ($type == SERVER_DATA || $type == SERVER_TOTALS || $type == SERVER_EXTREMES
Condition100%12/12
$type == 1 or $type == 7 or $type == 8 or $type == 10 or $type == 14
l!l&&r!l&&!r
712
$type == 1 or $type == 7 or $type == 8 or $type == 10
l!l&&r!l&&!r
613
$type == 1 or $type == 7 or $type == 8
l!l&&r!l&&!r
514
$type == 1 or $type == 7
l!l&&r!l&&!r
415
MC/DC100%5/5
$type == 1 or $type == 7 or $type == 8 or $type == 10 or $type == 14
$type == 1 $type == 7 $type == 8 $type == 10 $type == 14
Truth table100%6/6
A or B or C or D or E
A= $type == 1B= $type == 7C= $type == 8D= $type == 10E= $type == 14
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
308 || $type == SERVER_LOG || $type == SERVER_PROFILE_EVENTS) {
309 8 ▶ ($pkt{table_name}, $offset) = unpack_string($bytes, $offset) if $rev >= 50264;
Branch: 1 branch
BranchTrueFalseTotal
if $rev >= 50264 8 0 8
310 8 ▶ my $compressed = $opts{compressed} && $type != SERVER_LOG && $type != SERVER_PROFILE_EVENTS;
Condition83%5/6
$opts{'compressed'} && $type != 10 && $type != 14
!ll&&!rl&&r
602
$opts{"compressed"} and $type != 10
!ll&&!rl&&r
512
MC/DC66%2/3
$opts{'compressed'} && $type != 10 && $type != 14
$opts{"compressed"} $type != 10 $type != 14
Truth table75%3/4
A and B && C
A= $opts{"compressed"}B= $type != 10C= $type != 14
ABCresult
0XX0
10X0
1100
1111
311 8 ▶ if (!$compressed && $rev >= 51903) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $compressed and $rev >= 51903) 6 2 8
Condition66%2/3
not $compressed and $rev >= 51903
!ll&&!rl&&r
206
MC/DC50%1/2
not $compressed and $rev >= 51903
not $compressed $rev >= 51903
Truth table66%2/3
not(A) and B
A= not $compressedB= $rev >= 51903
ABresult
0X0
100
111
312 6 ($pkt{block_info}, $offset) = unpack_block_info($bytes, $offset);
313 }
314 8 $pkt{block_offset} = $offset;
315 }
316 elsif ($type == SERVER_TABLE_COLUMNS) {
317 1 ($pkt{table_name}, $offset) = unpack_string($bytes, $offset);
318 1 ($pkt{column_descriptor}, $offset) = unpack_string($bytes, $offset);
319 }
320 else {
321 1 die "unpack_packet: unknown server packet type $type\n";
322 }
323 28 return (\%pkt, $offset);
324 }
325  
326 # Blocking read helper for IO::Socket / IO::Handle. Pulls bytes until
327 # one full packet parses, returns the packet hashref. For
328 # Data/Totals/Extremes/ProfileEvents packets the inner Native block
329 # bytes are read into $pkt->{block_bytes} (the raw slice ready for
330 # ClickHouse::Encoder->decode_block); $pkt->{block} carries the
331 # pre-decoded block hashref.
332 #
333 # Reads whatever sysread returns (up to 4 KiB at a time): the
334 # protocol packets are size-self-describing, so we let unpack_packet
335 # tell us when it has enough bytes. Avoids the trap of trying to
336 # block-read N bytes when the server has only sent K < N.
337 17 ▶sub read_packet {
Subroutine read_packet: called CC=21 SCAR=30.6
Pod documented
338 17 my (undef, $fh, %opts) = @_;
339 17 ▶ my $bufref = $opts{buffer} // \my $local_buf;
Condition100%2/2
$opts{"buffer"} // \my $local_buf
l!l
107
MC/DC100%1/1
$opts{"buffer"} // \my $local_buf
$opts{"buffer"}
Truth table100%2/2
A
A= $opts{"buffer"}
Aresult
11
00
340 17 ▶ $$bufref //= '';
Condition100%2/2
$$bufref //= ""
l!l
107
MC/DC100%1/1
$$bufref //= ""
$$bufref
Truth table100%2/2
A
A= $$bufref
Aresult
11
00
341 17 my $timeout = $opts{timeout};
342 17 my ($deadline, $fd);
343 17 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 4 13 17
344 4 require Time::HiRes;
345 4 $deadline = Time::HiRes::time() + $timeout;
346 4 $fd = fileno $fh;
347 }
348 18 ▶ my $read_some = sub {
Subroutine __ANON__: called CC=11 SCAR=24.2
349 18 while (1) {
350 22 ▶ if (defined $fd && $fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fd and $fd >= 0) 8 14 22
Condition66%2/3
defined $fd and $fd >= 0
!ll&&!rl&&r
1408
MC/DC50%1/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table66%2/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
351 8 my $left = $deadline - Time::HiRes::time();
352 8 my $expired = "read_packet: timed out after ${timeout}s waiting for the server\n";
353 8 ▶ die $expired if $left <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $left <= 0 1 7 8
354 7 my $ready = '';
355 7 vec($ready, $fd, 1) = 1;
356 7 my $n = select($ready, undef, undef, $left);
357 7 ▶ die $expired if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 2 5 7
358 5 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 3 2 5
359 3 ▶ next if $! == Errno::EINTR();
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 3 0 3
360 0 die "read_packet: select failed: $!\n";
361 }
362 }
363 16 my $got = sysread $fh, my $chunk, 4096;
364 16 ▶ next if !defined $got && $! == Errno::EINTR();
Branch: 1 branch
BranchTrueFalseTotal
if not defined $got and $! == 4 1 15 16
Condition66%2/3
not defined $got and $! == 4
!ll&&!rl&&r
1501
MC/DC50%1/2
not defined $got and $! == 4
not defined $got $! == 4
Truth table66%2/3
not(A) and B
A= not defined $gotB= $! == 4
ABresult
0X0
100
111
365 15 ▶ die "read_packet: read error: $!\n" unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 15 15
366 15 ▶ die "read_packet: connection closed mid-packet\n" if !$got;
Branch: 1 branch
BranchTrueFalseTotal
unless $got 1 14 15
367 14 $$bufref .= $chunk;
368 14 return;
369 }
370 17 };
371 17 my ($pkt, $end);
372 17 while (1) {
373 27 my $ok = eval { ($pkt, $end) = __PACKAGE__->unpack_packet($$bufref, 0, %opts); 1 };
374 27 ▶ last if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 13 14 27
375 14 ▶ die $@ unless $@ =~ /truncated/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /truncated/ 0 14 14
376 14 $read_some->();
377 }
378 13 ▶ if (exists $pkt->{block_offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pkt->{"block_offset"}) 5 8 13
379 my $compressed = $opts{compressed}
380 5 ▶ && $pkt->{type} != SERVER_LOG && $pkt->{type} != SERVER_PROFILE_EVENTS;
Condition83%5/6
$opts{'compressed'} && $pkt->{'type'} != 10 && $pkt->{'type'} != 14
!ll&&!rl&&r
302
$opts{"compressed"} and $pkt->{"type"} != 10
!ll&&!rl&&r
212
MC/DC66%2/3
$opts{'compressed'} && $pkt->{'type'} != 10 && $pkt->{'type'} != 14
$opts{"compressed"} $pkt->{"type"} != 10 $pkt->{'type'} != 14
Truth table75%3/4
A and B && C
A= $opts{"compressed"}B= $pkt->{"type"} != 10C= $pkt->{'type'} != 14
ABCresult
0XX0
10X0
1100
1111
381 5 ▶ if ($compressed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compressed) { } 2 3 5
382 2 my $plain = '';
383 2 my $off = $pkt->{block_offset};
384 2 while (1) {
385 6 my ($frame, $consumed);
386 6 while (1) {
387 7 my $ok = eval {
388 7 ($frame, $consumed) = ClickHouse::Encoder->decompress_native_block(
389 6 $$bufref, offset => $off); 1;
390 };
391 7 ▶ last if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 6 1 7
392 1 ▶ die $@ unless $@ =~ /truncated header|extends past buffer/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /truncated header|extends past buffer/ 0 1 1
393 1 $read_some->();
394 }
395 6 $off += $consumed;
396 6 $plain .= $frame;
397 6 my $ok = eval {
398 6 my $start = 0;
399 6 ▶ if (($opts{revision} // DEFAULT_REVISION) >= 51903) {
Branch: 1 branch
BranchTrueFalseTotal
if (($opts{"revision"} // 54429) >= 51903) 6 0 6
Condition50%1/2
$opts{"revision"} // 54429
l!l
06
MC/DC0%0/1
$opts{"revision"} // 54429
$opts{"revision"}
Truth table50%1/2
A
A= $opts{"revision"}
Aresult
11
00
400 6 ($pkt->{block_info}, $start) = unpack_block_info($plain, 0);
401 }
402 6 my $b = ClickHouse::Encoder->decode_block($plain, $start);
403 die "read_packet: trailing bytes in compressed Data packet\n"
404 2 ▶ unless $start + $b->{consumed} == length $plain;
Branch: 1 branch
BranchTrueFalseTotal
unless $start + $b->{"consumed"} == length $plain 0 2 2
405 2 $pkt->{block} = $b;
406 2 $pkt->{block_bytes} = substr($plain, $start);
407 2 $pkt->{compressed_consumed} = $off - $pkt->{block_offset};
408 2 1;
409 };
410 6 ▶ if ($ok) { $end = $off; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 2 4 6
411 4 ▶ die $@ unless $@ =~ /truncated|exceeds remaining buffer/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /truncated|exceeds remaining buffer/ 0 4 4
412 }
413 } else {
414 3 my $need_at = 0;
415 3 while (1) {
416 6 ▶ $read_some->() while length($$bufref) < $need_at;
Branch: 1 branch
BranchTrueFalseTotal
length $$bufref < $need_at and do { &$read_some() } 3 6 9
417 6 my $b = eval { ClickHouse::Encoder->decode_block($$bufref, $pkt->{block_offset}) };
418 6 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 3 3 6
419 3 $pkt->{block} = $b;
420 3 $pkt->{block_bytes} = substr($$bufref, $pkt->{block_offset}, $b->{consumed});
421 3 $end = $pkt->{block_offset} + $b->{consumed};
422 3 last;
423 }
424 3 ▶ die $@ unless $@ =~ /truncated|exceeds remaining buffer/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /truncated|exceeds remaining buffer/ 0 3 3
425 3 ▶ $need_at = length($$bufref) + ($@ =~ /need (\d+) more/ ? $1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /need (\d+) more/ ? : 0 3 3
426 }
427 }
428 }
429 13 substr($$bufref, 0, $end, '');
430 13 return $pkt;
431 }
432  
433 4 1;
434  
435 __END__
« blib/lib/ClickHouse/Encoder.pm ↑ Summary datetime.c »