blib/lib/ClickHouse/Encoder.pm

filter: Statementstmt 49.7% 944 / 1896 Branchbran 42.4% 531 / 1250 Conditioncond 38.2% 207 / 541 MC/DCmcdc 29.4% 90 / 306 Subroutinesub 54.1% 84 / 155 Podpod 100% 58 / 58 totaltotal 45.5% 1914 / 4206 CC 969 SCAR 41.7
CC
969
Cov
46%
_for_describe
380.0
_for_describe
380.0
_type_byte_estimate
208.6
CRAP 64.9
↑ Summary blib/lib/ClickHouse/Encoder/TCP.pm »
1 package ClickHouse::Encoder;
2 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 87 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 87 our $VERSION = '0.05';
6  
7 87 require XSLoader;
8 87 XSLoader::load('ClickHouse::Encoder', $VERSION);
9  
10 # An encoder is a blessed IV holding a raw Encoder* (likewise Streamer).
11 # ithreads clone SVs verbatim, so without this both interpreters own the
12 # same pointer and each frees it - a double free on any thread spawn.
13 # The child's copies become plain undef instead. See THREADS in the docs.
14 1 ▶sub CLONE_SKIP { return 1 }
Subroutine CLONE_SKIP: called CC=1 SCAR=0.0
15  
16 # Validate a [db.]table identifier as ASCII word characters only.
17 # Rejects anything that could inject SQL via the --query argument.
18 43 ▶sub _validate_table_name {
Subroutine _validate_table_name: called CC=2 SCAR=9.2
19 0 ▶ my $table = shift;
Subroutine _validate_table_name: not called CC=2 SCAR=9.2
20 0 ▶ $table =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?\z/
Branch: 2 branches
BranchTrueFalseTotal
$table =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?\z/ or die "Invalid table name '${table}': expected [db.]name with [A-Za-z0-9_]" 39 4 43
$table =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?\z/ or die "Invalid table name '${table}': expected [db.]name with [A-Za-z0-9_]" 0 0 0
21 or die "Invalid table name '$table': expected [db.]name with [A-Za-z0-9_]";
22 0 return;
23 }
24  
25 # Build (url, headers) for a ClickHouse HTTP request with the given SQL
26 # in the `query` parameter. Pulls connection params from %opts using
27 # the same defaults as for_table / insert_http. UTF-8 encodes before
28 # percent-escaping so non-ASCII (caf%C3%A9, emoji) round-trips correctly.
29 51 ▶sub _http_url_headers {
Subroutine _http_url_headers: called CC=9 SCAR=29.5
30 0 ▶ my ($sql, %opts) = @_;
Subroutine _http_url_headers: not called CC=9 SCAR=29.5
31 0 require Encode;
32 96 ▶ my $esc = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
33 0 ▶ my $s = utf8::is_utf8($_[0]) ? Encode::encode('UTF-8', $_[0], 0) : $_[0];
Subroutine __ANON__: not called CC=2 SCAR=9.2
Branch: 2 branches
BranchTrueFalseTotal
utf8::is_utf8($_[0]) ? : 1 95 96
utf8::is_utf8($_[0]) ? : 0 0 0
34 0 $s =~ s/([^A-Za-z0-9\-_.~])/sprintf('%%%02X', ord($1))/ge;
35 0 $s;
36 0 };
37 0 my ($scheme, $host, $port) = _check_endpoint(\%opts);
38 0 ▶ my $database = $opts{database} // 'default';
Condition50%2/4
$opts{"database"} // "default"
l!l
140
$opts{"database"} // "default"
l!l
00
MC/DC100%1/1
$opts{"database"} // "default"
$opts{"database"}
MC/DC0%0/1
$opts{"database"} // "default"
$opts{"database"}
Truth table100%2/2
A
A= $opts{"database"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"database"}
Aresult
11
00
39 0 ▶ my $user = $opts{user} // 'default';
Condition50%2/4
$opts{"user"} // "default"
l!l
140
$opts{"user"} // "default"
l!l
00
MC/DC100%1/1
$opts{"user"} // "default"
$opts{"user"}
MC/DC0%0/1
$opts{"user"} // "default"
$opts{"user"}
Truth table100%2/2
A
A= $opts{"user"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"user"}
Aresult
11
00
40 0 ▶ my $password = $opts{password} // '';
Condition50%2/4
$opts{"password"} // ""
l!l
239
$opts{"password"} // ""
l!l
00
MC/DC100%1/1
$opts{"password"} // ""
$opts{"password"}
MC/DC0%0/1
$opts{"password"} // ""
$opts{"password"}
Truth table100%2/2
A
A= $opts{"password"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"password"}
Aresult
11
00
41 0 my $url = "$scheme://$host:$port/?database=" . $esc->($database);
42 0 ▶ $url .= "&query=" . $esc->($sql) if length $sql;
Branch: 2 branches
BranchTrueFalseTotal
if length $sql 39 2 41
if length $sql 0 0 0
43 # Per-query settings: { max_memory_usage => '...', max_execution_time => 30 }
44 0 ▶ if (my $s = $opts{settings}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $s = $opts{"settings"}) 3 38 41
if (my $s = $opts{"settings"}) 0 0 0
45 0 for my $k (sort keys %$s) {
46 0 $url .= "&" . $esc->($k) . "=" . $esc->($s->{$k});
47 }
48 }
49 # Insert-side idempotency token: identical token + payload is rejected.
50 0 ▶ if (defined(my $tok = $opts{dedup_token})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $tok = $opts{"dedup_token"})) 8 33 41
if (defined(my $tok = $opts{"dedup_token"})) 0 0 0
51 0 $url .= "&insert_deduplication_token=" . $esc->($tok);
52 }
53 0 my %hdr = ('X-ClickHouse-User' => $user);
54 0 ▶ $hdr{'X-ClickHouse-Key'} = $password if $password ne '';
Branch: 2 branches
BranchTrueFalseTotal
if $password ne "" 2 39 41
if $password ne "" 0 0 0
55 0 return ($url, \%hdr);
56 }
57  
58 # Validate the host/port/scheme triple shared by every HTTP entry point.
59 # Rejects anything other than http/https, ensures the port is a positive
60 # integer, and refuses host strings that contain URL-structural characters
61 # (':/?#&@'). Centralised here so insert_http, bulk_inserter, ping, and
62 # select_blocks share a single allow-list and identical error messages.
63 # '@' counts: it opens the userinfo section, so 'evil.com@internal.db'
64 # would send the request to internal.db. IPv6 literals are the one host
65 # form that legitimately holds colons; accept them bracketed ('[::1]'),
66 # which is also the form the URL needs to keep the port unambiguous.
67 63 ▶sub _check_endpoint {
Subroutine _check_endpoint: called CC=12 SCAR=34.0
68 0 ▶ my ($opts) = @_;
Subroutine _check_endpoint: not called CC=12 SCAR=34.0
69 0 ▶ my $scheme = $opts->{scheme} // 'http';
Condition50%2/4
$opts->{"scheme"} // "http"
l!l
558
$opts->{"scheme"} // "http"
l!l
00
MC/DC100%1/1
$opts->{"scheme"} // "http"
$opts->{"scheme"}
MC/DC0%0/1
$opts->{"scheme"} // "http"
$opts->{"scheme"}
Truth table100%2/2
A
A= $opts->{"scheme"}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{"scheme"}
Aresult
11
00
70 0 ▶ my $host = $opts->{host} // 'localhost';
Condition50%2/4
$opts->{"host"} // "localhost"
l!l
2736
$opts->{"host"} // "localhost"
l!l
00
MC/DC100%1/1
$opts->{"host"} // "localhost"
$opts->{"host"}
MC/DC0%0/1
$opts->{"host"} // "localhost"
$opts->{"host"}
Truth table100%2/2
A
A= $opts->{"host"}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{"host"}
Aresult
11
00
71 0 ▶ my $port = $opts->{port} // 8123;
Condition50%2/4
$opts->{"port"} // 8123
l!l
954
$opts->{"port"} // 8123
l!l
00
MC/DC100%1/1
$opts->{"port"} // 8123
$opts->{"port"}
MC/DC0%0/1
$opts->{"port"} // 8123
$opts->{"port"}
Truth table100%2/2
A
A= $opts->{"port"}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{"port"}
Aresult
11
00
72 0 ▶ die "endpoint: scheme must be 'http' or 'https' (got '$scheme')\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $scheme eq "http" or $scheme eq "https" 2 61 63
unless $scheme eq "http" or $scheme eq "https" 0 0 0
Condition50%3/6
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
5832
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
000
MC/DC100%2/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
MC/DC0%0/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
Truth table100%3/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
73 unless $scheme eq 'http' || $scheme eq 'https';
74 0 my $is_ipv6 = $host =~ /\A\[[0-9A-Fa-f:.]+\]\z/;
75 0 ▶ die "endpoint: host must not contain URL-structural characters "
Branch: 2 branches
BranchTrueFalseTotal
if not length $host or $host =~ m[[/?#&\@\s]] or $host =~ /:/ and not $is_ipv6 9 52 61
if not length $host or $host =~ m[[/?#&\@\s]] or $host =~ /:/ and not $is_ipv6 0 0 0
Condition50%9/18
not length $host or $host =~ m[[/?#&\@\s]] or $host =~ /:/ and not $is_ipv6
l!l&&r!l&&!r
7252
not length $host or $host =~ m[[/?#&\@\s]]
l!l&&r!l&&!r
2554
$host =~ /:/ and not $is_ipv6
!ll&&!rl&&r
4932
not length $host or $host =~ m[[/?#&\@\s]] or $host =~ /:/ and not $is_ipv6
l!l&&r!l&&!r
000
not length $host or $host =~ m[[/?#&\@\s]]
l!l&&r!l&&!r
000
$host =~ /:/ and not $is_ipv6
!ll&&!rl&&r
000
MC/DC100%4/4
not length $host or $host =~ m[[/?#&\@\s]] or $host =~ /:/ and not $is_ipv6
not length $host $host =~ m[[/?#&\@\s]] $host =~ /:/ not $is_ipv6
MC/DC0%0/4
not length $host or $host =~ m[[/?#&\@\s]] or $host =~ /:/ and not $is_ipv6
not length $host $host =~ m[[/?#&\@\s]] $host =~ /:/ not $is_ipv6
Truth table100%5/5
not(A) or B or C and not(D)
A= not length $hostB= $host =~ m[[/?#&\@\s]]C= $host =~ /:/D= not $is_ipv6
ABCDresult
1XXX1
01XX1
00111
000X0
00100
Truth table0%0/5
not(A) or B or C and not(D)
A= not length $hostB= $host =~ m[[/?#&\@\s]]C= $host =~ /:/D= not $is_ipv6
ABCDresult
1XXX1
01XX1
00111
000X0
00100
76 . "(got '$host')\n"
77 if !length $host
78 || $host =~ m{[/?#&\@\s]}
79 || ($host =~ m{:} && !$is_ipv6);
80 0 ▶ die "endpoint: port must be a positive integer (got '$port')\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $port =~ /\A[1-9]\d{0,4}\z/ and $port < 65536 4 48 52
unless $port =~ /\A[1-9]\d{0,4}\z/ and $port < 65536 0 0 0
Condition50%3/6
$port =~ /\A[1-9]\d{0,4}\z/ and $port < 65536
!ll&&!rl&&r
3148
$port =~ /\A[1-9]\d{0,4}\z/ and $port < 65536
!ll&&!rl&&r
000
MC/DC100%2/2
$port =~ /\A[1-9]\d{0,4}\z/ and $port < 65536
$port =~ /\A[1-9]\d{0,4}\z/ $port < 65536
MC/DC0%0/2
$port =~ /\A[1-9]\d{0,4}\z/ and $port < 65536
$port =~ /\A[1-9]\d{0,4}\z/ $port < 65536
Truth table100%3/3
A and B
A= $port =~ /\A[1-9]\d{0,4}\z/B= $port < 65536
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $port =~ /\A[1-9]\d{0,4}\z/B= $port < 65536
ABresult
0X0
100
111
81 unless $port =~ /\A[1-9]\d{0,4}\z/ && $port < 65536;
82 0 return ($scheme, $host, $port);
83 }
84  
85 # Build an HTTP::Tiny instance honoring ssl_options (verify_SSL, SSL_ca_file,
86 # etc.) and keep_alive. Shared by insert_http, bulk_inserter, server_version,
87 # ping, select_blocks; callers pass %opts unchanged. Loading HTTP::Tiny here
88 # keeps the require local to HTTP code paths.
89 30 ▶sub _http_tiny {
Subroutine _http_tiny: called CC=5 SCAR=20.9
90 0 ▶ my (%opts) = @_;
Subroutine _http_tiny: not called CC=5 SCAR=20.9
91 0 require HTTP::Tiny;
92 my @args = (timeout => $opts{timeout} // 60, max_redirect => 0,
93 0 ▶ verify_SSL => $opts{verify_SSL} // 1);
Condition50%4/8
$opts{"timeout"} // 60
l!l
264
$opts{"verify_SSL"} // 1
l!l
228
$opts{"timeout"} // 60
l!l
00
$opts{"verify_SSL"} // 1
l!l
00
MC/DC100%1/1
$opts{"timeout"} // 60
$opts{"timeout"}
MC/DC100%1/1
$opts{"verify_SSL"} // 1
$opts{"verify_SSL"}
MC/DC0%0/1
$opts{"timeout"} // 60
$opts{"timeout"}
MC/DC0%0/1
$opts{"verify_SSL"} // 1
$opts{"verify_SSL"}
Truth table100%2/2
A
A= $opts{"timeout"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{"verify_SSL"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"timeout"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"verify_SSL"}
Aresult
11
00
94 0 ▶ push @args, keep_alive => 1 if $opts{keep_alive};
Branch: 2 branches
BranchTrueFalseTotal
if $opts{"keep_alive"} 18 12 30
if $opts{"keep_alive"} 0 0 0
95 0 ▶ push @args, SSL_options => $opts{ssl_options} if $opts{ssl_options};
Branch: 2 branches
BranchTrueFalseTotal
if $opts{"ssl_options"} 1 29 30
if $opts{"ssl_options"} 0 0 0
96 0 return HTTP::Tiny->new(@args);
97 }
98  
99 # Parse a flat CH JSON object string (X-ClickHouse-Summary /
100 # X-ClickHouse-Progress) without depending on JSON::PP. Both are small
101 # flat objects of stringified integers (read_rows, written_rows,
102 # total_rows_to_read, elapsed_ns, ...). Returns a hashref or undef.
103 19 ▶sub _parse_ch_kv {
Subroutine _parse_ch_kv: called CC=6 SCAR=25.8
104 0 ▶ my ($str) = @_;
Subroutine _parse_ch_kv: not called CC=6 SCAR=25.8
105 0 ▶ return unless defined $str && length $str;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $str and length $str 14 5 19
unless defined $str and length $str 0 0 0
Condition33%2/6
defined $str and length $str
!ll&&!rl&&r
1405
defined $str and length $str
!ll&&!rl&&r
000
MC/DC50%1/2
defined $str and length $str
defined $str length $str
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table66%2/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
106 0 my %h;
107 # NB: stash $1/$2 before the digit-test regex - that regex resets
108 # capture variables and would silently turn every key into ''.
109 0 ▶ while ($str =~ /"([^"\\]+)"\s*:\s*"([^"\\]*)"/g) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /"([^"\\]+)"\s*:\s*"([^"\\]*)"/g) 14 5 19
if ($str =~ /"([^"\\]+)"\s*:\s*"([^"\\]*)"/g) 0 0 0
110 0 my ($k, $v) = ($1, $2);
111 0 ▶ $h{$k} = ($v =~ /\A-?\d+\z/) ? $v + 0 : $v;
Branch: 2 branches
BranchTrueFalseTotal
$v =~ /\A-?\d+\z/ ? : 14 0 14
$v =~ /\A-?\d+\z/ ? : 0 0 0
112 }
113 0 ▶ return scalar(keys %h) ? \%h : undef;
Branch: 2 branches
BranchTrueFalseTotal
scalar keys %h ? : 5 0 5
scalar keys %h ? : 0 0 0
114 }
115  
116 # Lift a few ClickHouse response headers into a small hashref. Returns
117 # undef when none are present; otherwise carries query_id, server
118 # (revision), format, a parsed summary, and the final progress snapshot
119 # so callers don't reparse the same headers. X-ClickHouse-Progress is
120 # sent repeatedly while a query runs (with send_progress_in_http_headers
121 # =1); HTTP::Tiny collapses repeats into an arrayref, so we take the
122 # last - the most complete - snapshot.
123 22 ▶sub _decorate_response {
Subroutine _decorate_response: called CC=10 SCAR=32.2
124 0 ▶ my ($resp) = @_;
Subroutine _decorate_response: not called CC=10 SCAR=32.2
125 0 ▶ return $resp unless ref $resp eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $resp eq "HASH" 0 22 22
unless ref $resp eq "HASH" 0 0 0
126 0 ▶ my $h = $resp->{headers} or return $resp;
Branch: 2 branches
BranchTrueFalseTotal
my $h = $resp->{"headers"} or return $resp 17 5 22
my $h = $resp->{"headers"} or return $resp 0 0 0
127 0 my %ch;
128 0 for my $k (qw(query-id server format exception-code)) {
129 0 my $hv = $h->{"x-clickhouse-$k"};
130 0 ▶ $ch{$k} = $hv if defined $hv;
Branch: 2 branches
BranchTrueFalseTotal
if defined $hv 4 64 68
if defined $hv 0 0 0
131 }
132 0 ▶ if (my $sum = _parse_ch_kv($h->{'x-clickhouse-summary'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sum = _parse_ch_kv($h->{"x-clickhouse-summary"})) 3 14 17
if (my $sum = _parse_ch_kv($h->{"x-clickhouse-summary"})) 0 0 0
133 0 $ch{summary} = $sum;
134 }
135 0 ▶ if (defined(my $pv = $h->{'x-clickhouse-progress'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $pv = $h->{"x-clickhouse-progress"})) 2 15 17
if (defined(my $pv = $h->{"x-clickhouse-progress"})) 0 0 0
136 0 ▶ $pv = $pv->[-1] if ref $pv eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $pv eq "ARRAY" 1 1 2
if ref $pv eq "ARRAY" 0 0 0
137 0 ▶ if (my $prog = _parse_ch_kv($pv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $prog = _parse_ch_kv($pv)) 2 0 2
if (my $prog = _parse_ch_kv($pv)) 0 0 0
138 0 $ch{progress} = $prog;
139 }
140 }
141 0 ▶ $resp->{ch} = \%ch if %ch;
Branch: 2 branches
BranchTrueFalseTotal
if %ch 5 12 17
if %ch 0 0 0
142 0 return $resp;
143 }
144  
145 0 ▶sub for_table {
Subroutine for_table: not called CC=1 SCAR=6.9
Pod documented
146 0 ▶ my ($class, $table, %opts) = @_;
Subroutine for_table: not called CC=1 SCAR=6.9
Pod documented
147 0 _validate_table_name($table);
148 0 return $class->_for_describe("describe table $table", %opts);
149 }
150  
151 30 ▶sub from_columns {
Subroutine from_columns: called CC=17 SCAR=41.9
Pod documented
152 0 ▶ my ($class, $rows, %opts) = @_;
Subroutine from_columns: not called CC=17 SCAR=41.9
Pod documented
153 0 ▶ die "from_columns: rows must be an arrayref\n" unless ref($rows) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 2 28 30
unless ref $rows eq "ARRAY" 0 0 0
154 0 my (@columns, %defaults, %expressions);
155 0 for my $row (@$rows) {
156 0 my ($name, $type, $kind, $expr);
157 0 ▶ if (ref($row) eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $row eq 'ARRAY') { } 85 3 88
elsif (ref $row eq 'HASH') { } 2 1 3
if (ref $row eq 'ARRAY') { } 0 0 0
elsif (ref $row eq 'HASH') { } 0 0 0
158 0 ($name, $type, $kind, $expr) = @$row;
159 } elsif (ref($row) eq 'HASH') {
160 0 ($name, $type, $kind, $expr) = @$row{qw(name type default_kind default_expression)};
161 } else {
162 0 die "from_columns: each row must be an arrayref or hashref\n";
163 }
164 0 ▶ die "from_columns: each row needs name and type\n" unless defined $name && defined $type;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $name and defined $type 2 85 87
unless defined $name and defined $type 0 0 0
Condition50%3/6
defined $name and defined $type
!ll&&!rl&&r
1185
defined $name and defined $type
!ll&&!rl&&r
000
MC/DC100%2/2
defined $name and defined $type
defined $name defined $type
MC/DC0%0/2
defined $name and defined $type
defined $name defined $type
Truth table100%3/3
A and B
A= defined $nameB= defined $type
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $nameB= defined $type
ABresult
0X0
100
111
165 0 ▶ next if ($kind // '') =~ /\A(?:materialized|alias|ephemeral)\z/i;
Branch: 2 branches
BranchTrueFalseTotal
if ($kind // "") =~ /\A(?:materialized|alias|ephemeral)\z/i 3 82 85
if ($kind // "") =~ /\A(?:materialized|alias|ephemeral)\z/i 0 0 0
Condition25%1/4
$kind // ""
l!l
850
$kind // ""
l!l
00
MC/DC0%0/1
$kind // ""
$kind
MC/DC0%0/1
$kind // ""
$kind
Truth table50%1/2
A
A= $kind
Aresult
11
00
Truth table0%0/2
A
A= $kind
Aresult
11
00
166 0 push @columns, [$name, $type];
167 0 ▶ if (($kind // '') =~ /\Adefault\z/i && defined $expr) {
Branch: 2 branches
BranchTrueFalseTotal
if (($kind // "") =~ /\Adefault\z/i and defined $expr) 82 0 82
if (($kind // "") =~ /\Adefault\z/i and defined $expr) 0 0 0
Condition20%2/10
($kind // "") =~ /\Adefault\z/i and defined $expr
!ll&&!rl&&r
0082
$kind // ""
l!l
820
($kind // "") =~ /\Adefault\z/i and defined $expr
!ll&&!rl&&r
000
$kind // ""
l!l
00
MC/DC0%0/2
($kind // "") =~ /\Adefault\z/i and defined $expr
$kind defined $expr
MC/DC0%0/2
($kind // "") =~ /\Adefault\z/i and defined $expr
$kind defined $expr
Truth table33%1/3
A and B
A= $kindB= defined $expr
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $kindB= defined $expr
ABresult
0X0
100
111
168 0 my ($ok, $value) = _default_literal($expr);
169 0 ▶ if ($ok) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ok) { } 17 65 82
elsif ($opts{'empty_as_default'}) { } 56 9 65
if ($ok) { } 0 0 0
elsif ($opts{'empty_as_default'}) { } 0 0 0
170 0 $defaults{$name} = $value;
171 } elsif ($opts{empty_as_default}) {
172 0 $expressions{$name} = $expr;
173 }
174 }
175 }
176 0 ▶ if (exists $opts{defaults}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{"defaults"}) 4 21 25
if (exists $opts{"defaults"}) 0 0 0
177 0 ▶ die "defaults must be a hashref\n" unless ref($opts{defaults}) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $opts{"defaults"} eq "HASH" 0 4 4
unless ref $opts{"defaults"} eq "HASH" 0 0 0
178 0 @defaults{keys %{$opts{defaults}}} = values %{$opts{defaults}};
179 }
180 0 ▶ return $class->new(columns => \@columns, _encoder_options(%opts), defaults => \%defaults,
Branch: 2 branches
BranchTrueFalseTotal
%expressions ? : 7 18 25
%expressions ? : 0 0 0
181 %expressions ? (_default_expressions => \%expressions) : ());
182 }
183  
184 12 ▶sub _literal_string {
Subroutine _literal_string: called CC=5 SCAR=21.7
185 0 ▶ my ($text) = @_;
Subroutine _literal_string: not called CC=5 SCAR=21.7
186 0 ▶ return unless $$text =~ /\G'((?:[^'\\]|\\.|'')*)'/gcs;
Branch: 2 branches
BranchTrueFalseTotal
unless $$text =~ /\G'((?:[^'\\]|\\.|'')*)'/cgs 1 11 12
unless $$text =~ /\G'((?:[^'\\]|\\.|'')*)'/cgs 0 0 0
187 0 my $value = $1;
188 0 my %esc = (n => "\n", t => "\t", r => "\r", b => "\b", f => "\f",
189 v => "\x0b", a => "\a", '0' => "\0", e => "\x1b");
190 0 $value =~ s{\\x([0-9a-fA-F]{2})|\\(.)|('')}{
191 0 ▶ defined $1 ? chr(hex($1)) : defined $2 ? ($esc{$2} // $2) : "'"
Branch: 4 branches
BranchTrueFalseTotal
defined $1 ? : 1 5 6
defined $2 ? : 4 1 5
defined $1 ? : 0 0 0
defined $2 ? : 0 0 0
Condition33%2/6
$esc{$2} // $2
l!l&&r!l&&!r
310
$esc{$2} // $2
l!l&&r!l&&!r
000
MC/DC0%0/2
$esc{$2} // $2
$esc{$2} $2
MC/DC0%0/2
$esc{$2} // $2
$esc{$2} $2
Truth table66%2/3
A // B
A= $esc{$2}B= $2
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $esc{$2}B= $2
ABresult
1X1
011
000
192 }ges;
193 0 return (1, $value);
194 }
195  
196 4 ▶sub _literal_type {
Subroutine _literal_type: called CC=10 SCAR=36.1
197 0 ▶ my ($text) = @_;
Subroutine _literal_type: not called CC=10 SCAR=36.1
198 0 ▶ return unless $$text =~ /\G\s*[A-Za-z_][A-Za-z_0-9]*\s*/gc;
Branch: 2 branches
BranchTrueFalseTotal
unless $$text =~ /\G\s*[A-Za-z_][A-Za-z_0-9]*\s*/cg 0 4 4
unless $$text =~ /\G\s*[A-Za-z_][A-Za-z_0-9]*\s*/cg 0 0 0
199 0 ▶ if ($$text =~ /\G\(/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$text =~ /\G\(/cg) 1 3 4
if ($$text =~ /\G\(/cg) 0 0 0
200 0 my $depth = 1;
201 0 ▶ while ($depth) {
Branch: 2 branches
BranchTrueFalseTotal
if ($depth) 5 1 6
if ($depth) 0 0 0
202 0 ▶ if ($$text =~ /\G'/gc) {
Branch: 6 branches
BranchTrueFalseTotal
if ($$text =~ /\G'/cg) { } 0 5 5
elsif ($$text =~ /\G\(/cg) { } 1 4 5
elsif ($$text =~ /\G\)/cg) { } 2 2 4
if ($$text =~ /\G'/cg) { } 0 0 0
elsif ($$text =~ /\G\(/cg) { } 0 0 0
elsif ($$text =~ /\G\)/cg) { } 0 0 0
203 0 pos($$text)--;
204 0 my ($ok) = _literal_string($text);
205 0 ▶ return unless $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 0 0 0
unless $ok 0 0 0
206 } elsif ($$text =~ /\G\(/gc) {
207 0 ▶ return if ++$depth > 64;
Branch: 2 branches
BranchTrueFalseTotal
if ++$depth > 64 0 1 1
if ++$depth > 64 0 0 0
208 } elsif ($$text =~ /\G\)/gc) {
209 0 $depth--;
210 } else {
211 0 ▶ return unless $$text =~ /\G[^()']+/gc;
Branch: 2 branches
BranchTrueFalseTotal
unless $$text =~ /\G[^()']+/cg 0 2 2
unless $$text =~ /\G[^()']+/cg 0 0 0
212 }
213 }
214 }
215 0 return 1;
216 }
217  
218 107 ▶sub _literal_value {
Subroutine _literal_value: called CC=29 SCAR=51.3
219 0 ▶ my ($text, $depth) = @_;
Subroutine _literal_value: not called CC=29 SCAR=51.3
220 0 ▶ return if $depth > 64;
Branch: 2 branches
BranchTrueFalseTotal
if $depth > 64 0 107 107
if $depth > 64 0 0 0
221 0 $$text =~ /\G\s*/gc;
222 0 my ($ok, $value);
223 0 ▶ if ($$text =~ /\Gcast\s*\(/gci) {
Branch: 12 branches
BranchTrueFalseTotal
if ($$text =~ /\Gcast\s*\(/cig) { } 7 100 107
elsif ($$text =~ /\G'/cg) { } 7 93 100
elsif ($$text =~ /\G([\[(])/cg) { } 11 82 93
elsif ($$text =~ /\G([+-]?0[xX][0-9a-fA-F]+)/cg) { } 2 80 82
elsif ($$text =~ /\G([+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?)/cg) { } 16 64 80
elsif ($$text =~ /\G(null|true|false)\b/cig) { } 4 60 64
if ($$text =~ /\Gcast\s*\(/cig) { } 0 0 0
elsif ($$text =~ /\G'/cg) { } 0 0 0
elsif ($$text =~ /\G([\[(])/cg) { } 0 0 0
elsif ($$text =~ /\G([+-]?0[xX][0-9a-fA-F]+)/cg) { } 0 0 0
elsif ($$text =~ /\G([+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?)/cg) { } 0 0 0
elsif ($$text =~ /\G(null|true|false)\b/cig) { } 0 0 0
224 0 ($ok, $value) = _literal_value($text, $depth + 1);
225 0 ▶ return unless $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 1 6 7
unless $ok 0 0 0
226 0 ▶ if ($$text =~ /\G\s*,\s*/gc) {
Branch: 4 branches
BranchTrueFalseTotal
if ($$text =~ /\G\s*,\s*/cg) { } 5 1 6
elsif ($$text =~ /\G\s*as\b/cig) { } 1 0 1
if ($$text =~ /\G\s*,\s*/cg) { } 0 0 0
elsif ($$text =~ /\G\s*as\b/cig) { } 0 0 0
227 0 my ($type_ok) = _literal_string($text);
228 0 ▶ return unless $type_ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $type_ok 0 5 5
unless $type_ok 0 0 0
229 } elsif ($$text =~ /\G\s*as\b/gci) {
230 0 ▶ return unless _literal_type($text);
Branch: 2 branches
BranchTrueFalseTotal
unless _literal_type($text) 0 1 1
unless _literal_type($text) 0 0 0
231 } else {
232 0 return;
233 }
234 0 ▶ return unless $$text =~ /\G\s*\)/gc;
Branch: 2 branches
BranchTrueFalseTotal
unless $$text =~ /\G\s*\)/cg 0 6 6
unless $$text =~ /\G\s*\)/cg 0 0 0
235 } elsif ($$text =~ /\G'/gc) {
236 0 pos($$text)--;
237 0 ($ok, $value) = _literal_string($text);
238 0 ▶ return unless $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 1 6 7
unless $ok 0 0 0
239 } elsif ($$text =~ /\G([\[(])/gc) {
240 0 my $open = $1;
241 0 ▶ my $close = $open eq '[' ? ']' : ')';
Branch: 2 branches
BranchTrueFalseTotal
$open eq '[' ? : 6 5 11
$open eq '[' ? : 0 0 0
242 0 my @values;
243 0 $$text =~ /\G\s*/gc;
244 0 ▶ unless ($$text =~ /\G\Q$close\E/gc) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($$text =~ /\G\Q$close\E/cg) 10 1 11
unless ($$text =~ /\G\Q$close\E/cg) 0 0 0
245 0 while (1) {
246 0 my ($item_ok, $item) = _literal_value($text, $depth + 1);
247 0 ▶ return unless $item_ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $item_ok 2 16 18
unless $item_ok 0 0 0
248 0 push @values, $item;
249 0 ▶ return if @values > 100_000;
Branch: 2 branches
BranchTrueFalseTotal
if @values > 100000 0 16 16
if @values > 100000 0 0 0
250 0 $$text =~ /\G\s*/gc;
251 0 ▶ last if $$text =~ /\G\Q$close\E/gc;
Branch: 2 branches
BranchTrueFalseTotal
if $$text =~ /\G\Q$close\E/cg 8 8 16
if $$text =~ /\G\Q$close\E/cg 0 0 0
252 0 ▶ return unless $$text =~ /\G,/gc;
Branch: 2 branches
BranchTrueFalseTotal
unless $$text =~ /\G,/cg 0 8 8
unless $$text =~ /\G,/cg 0 0 0
253 }
254 }
255 0 ▶ $value = $open eq '(' && @values == 1 ? $values[0] : \@values;
Branch: 2 branches
BranchTrueFalseTotal
$open eq '(' && @values == 1 ? : 2 7 9
$open eq '(' && @values == 1 ? : 0 0 0
Condition50%3/6
$open eq "(" and @values == 1
!ll&&!rl&&r
432
$open eq "(" and @values == 1
!ll&&!rl&&r
000
MC/DC100%2/2
$open eq "(" and @values == 1
$open eq "(" @values == 1
MC/DC0%0/2
$open eq "(" and @values == 1
$open eq "(" @values == 1
Truth table100%3/3
A and B
A= $open eq "("B= @values == 1
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $open eq "("B= @values == 1
ABresult
0X0
100
111
256 } elsif ($$text =~ /\G([+-]?0[xX][0-9a-fA-F]+)/gc) {
257 0 my $num = $1;
258 0 my $negative = $num =~ s/^-//;
259 0 $num =~ s/^\+//;
260 0 require Math::BigInt;
261 0 my $big = Math::BigInt->from_hex($num);
262 0 ▶ return if $big > Math::BigInt->new('18446744073709551615');
Branch: 2 branches
BranchTrueFalseTotal
if $big > "Math::BigInt"->new("18446744073709551615") 0 2 2
if $big > "Math::BigInt"->new("18446744073709551615") 0 0 0
263 0 ▶ $value = ($negative ? '-' : '') . $big->bstr;
Branch: 2 branches
BranchTrueFalseTotal
$negative ? : 1 1 2
$negative ? : 0 0 0
264 } elsif ($$text =~ /\G([+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?)/gc) {
265 0 $value = $1;
266 } elsif ($$text =~ /\G(null|true|false)\b/gci) {
267 0 ▶ $value = lc($1) eq 'null' ? undef : lc($1) eq 'true' ? 1 : 0;
Branch: 4 branches
BranchTrueFalseTotal
lc $1 eq 'null' ? : 1 3 4
lc $1 eq 'true' ? : 2 1 3
lc $1 eq 'null' ? : 0 0 0
lc $1 eq 'true' ? : 0 0 0
268 } else {
269 0 return;
270 }
271 0 ▶ while ($$text =~ /\G\s*::/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$text =~ /\G\s*::/cg) 3 43 46
if ($$text =~ /\G\s*::/cg) 0 0 0
272 0 ▶ return unless _literal_type($text);
Branch: 2 branches
BranchTrueFalseTotal
unless _literal_type($text) 0 3 3
unless _literal_type($text) 0 0 0
273 }
274 0 return (1, $value);
275 }
276  
277 82 ▶sub _default_literal {
Subroutine _default_literal: called CC=3 SCAR=14.2
278 0 ▶ my ($text) = @_;
Subroutine _default_literal: not called CC=3 SCAR=14.2
279 0 pos($text) = 0;
280 0 my ($ok, $value) = _literal_value(\$text, 0);
281 0 ▶ return unless $ok && $text =~ /\G\s*\z/gc;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok and $text =~ /\G\s*\z/cg 65 17 82
unless $ok and $text =~ /\G\s*\z/cg 0 0 0
Condition50%3/6
$ok and $text =~ /\G\s*\z/cg
!ll&&!rl&&r
61417
$ok and $text =~ /\G\s*\z/cg
!ll&&!rl&&r
000
MC/DC100%2/2
$ok and $text =~ /\G\s*\z/cg
$ok $text =~ /\G\s*\z/cg
MC/DC0%0/2
$ok and $text =~ /\G\s*\z/cg
$ok $text =~ /\G\s*\z/cg
Truth table100%3/3
A and B
A= $okB= $text =~ /\G\s*\z/cg
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $okB= $text =~ /\G\s*\z/cg
ABresult
0X0
100
111
282 0 return (1, $value);
283 }
284  
285 0 ▶sub _for_describe {
Subroutine _for_describe: not called CC=19 SCAR=59.4
286 0 ▶ my ($class, $describe_sql, %opts) = @_;
Subroutine _for_describe: not called CC=19 SCAR=59.4
287  
288 0 ▶ my $via = $opts{via} // 'client';
Condition0%0/4
$opts{"via"} // "client"
l!l
00
$opts{"via"} // "client"
l!l
00
MC/DC0%0/1
$opts{"via"} // "client"
$opts{"via"}
MC/DC0%0/1
$opts{"via"} // "client"
$opts{"via"}
Truth table0%0/2
A
A= $opts{"via"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"via"}
Aresult
11
00
289 0 my @lines;
290  
291 0 ▶ if ($via eq 'http') {
Branch: 4 branches
BranchTrueFalseTotal
if ($via eq 'http') { } 0 0 0
elsif ($via eq 'client') { } 0 0 0
if ($via eq 'http') { } 0 0 0
elsif ($via eq 'client') { } 0 0 0
292 0 my ($url, $hdr) = _http_url_headers(
293 "$describe_sql format tabseparated", %opts);
294 0 ▶ my $resp = _http_tiny(%opts, timeout => $opts{timeout} // 10)
Condition0%0/4
$opts{"timeout"} // 10
l!l
00
$opts{"timeout"} // 10
l!l
00
MC/DC0%0/1
$opts{"timeout"} // 10
$opts{"timeout"}
MC/DC0%0/1
$opts{"timeout"} // 10
$opts{"timeout"}
Truth table0%0/2
A
A= $opts{"timeout"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"timeout"}
Aresult
11
00
295 ->get($url, { headers => $hdr });
296 die "HTTP describe table failed (status $resp->{status}): $resp->{content}\n"
297 0 ▶ unless $resp->{success};
Branch: 2 branches
BranchTrueFalseTotal
unless $resp->{"success"} 0 0 0
unless $resp->{"success"} 0 0 0
298 0 @lines = split /\n/, $resp->{content};
299 }
300 elsif ($via eq 'client') {
301 0 ▶ my $host = $opts{host} // 'localhost';
Condition0%0/4
$opts{"host"} // "localhost"
l!l
00
$opts{"host"} // "localhost"
l!l
00
MC/DC0%0/1
$opts{"host"} // "localhost"
$opts{"host"}
MC/DC0%0/1
$opts{"host"} // "localhost"
$opts{"host"}
Truth table0%0/2
A
A= $opts{"host"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"host"}
Aresult
11
00
302 0 ▶ my $port = $opts{port} // 9000;
Condition0%0/4
$opts{"port"} // 9000
l!l
00
$opts{"port"} // 9000
l!l
00
MC/DC0%0/1
$opts{"port"} // 9000
$opts{"port"}
MC/DC0%0/1
$opts{"port"} // 9000
$opts{"port"}
Truth table0%0/2
A
A= $opts{"port"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"port"}
Aresult
11
00
303 0 ▶ my $database = $opts{database} // 'default';
Condition0%0/4
$opts{"database"} // "default"
l!l
00
$opts{"database"} // "default"
l!l
00
MC/DC0%0/1
$opts{"database"} // "default"
$opts{"database"}
MC/DC0%0/1
$opts{"database"} // "default"
$opts{"database"}
Truth table0%0/2
A
A= $opts{"database"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"database"}
Aresult
11
00
304 0 ▶ my $user = $opts{user} // 'default';
Condition0%0/4
$opts{"user"} // "default"
l!l
00
$opts{"user"} // "default"
l!l
00
MC/DC0%0/1
$opts{"user"} // "default"
$opts{"user"}
MC/DC0%0/1
$opts{"user"} // "default"
$opts{"user"}
Truth table0%0/2
A
A= $opts{"user"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"user"}
Aresult
11
00
305 0 ▶ my $password = $opts{password} // '';
Condition0%0/4
$opts{"password"} // ""
l!l
00
$opts{"password"} // ""
l!l
00
MC/DC0%0/1
$opts{"password"} // ""
$opts{"password"}
MC/DC0%0/1
$opts{"password"} // ""
$opts{"password"}
Truth table0%0/2
A
A= $opts{"password"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"password"}
Aresult
11
00
306 0 ▶ my $client = $opts{client} // 'clickhouse-client';
Condition0%0/4
$opts{"client"} // "clickhouse-client"
l!l
00
$opts{"client"} // "clickhouse-client"
l!l
00
MC/DC0%0/1
$opts{"client"} // "clickhouse-client"
$opts{"client"}
MC/DC0%0/1
$opts{"client"} // "clickhouse-client"
$opts{"client"}
Truth table0%0/2
A
A= $opts{"client"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"client"}
Aresult
11
00
307  
308 0 my @cmd = (
309 $client,
310 '--host', $host,
311 '--port', $port,
312 '--database', $database,
313 '--user', $user,
314 '--query', "$describe_sql format tabseparated",
315 );
316  
317 # Pass password via env so it doesn't show up in `ps`. Always
318 # set $password (even empty) so an empty `password => ''` arg
319 # also overrides any inherited CLICKHOUSE_PASSWORD.
320 0 local $ENV{CLICKHOUSE_PASSWORD} = $password;
321  
322 0 ▶ open my $fh, '-|', @cmd
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "-|", @cmd or die "Failed to run clickhouse-client: $!" 0 0 0
open my $fh, "-|", @cmd or die "Failed to run clickhouse-client: $!" 0 0 0
323 or die "Failed to run clickhouse-client: $!";
324 0 local $/ = "\n";
325 0 @lines = <$fh>;
326 0 close $fh;
327 0 ▶ die "clickhouse-client failed: exit code " . ($? >> 8) if $?;
Branch: 2 branches
BranchTrueFalseTotal
if $? 0 0 0
if $? 0 0 0
328 }
329 else {
330 0 die "Unknown via='$via' (expected 'client' or 'http')";
331 }
332  
333 0 my @columns;
334 # ClickHouse's TabSeparated format escapes \\ \n \t \r \0 in field
335 # values. Multi-line type expressions (e.g. named Tuple, Nested) come
336 # out as a single TSV line with embedded \n and indentation; un-escape
337 # so the XS parser sees the canonical type string. The default branch
338 # ($1) handles \\ -> \ and any other backslash-escape forms transparently.
339 0 ▶ my $unesc = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
340 0 ▶ my $s = shift // '';
Subroutine __ANON__: not called CC=6 SCAR=37.4
Condition0%0/4
shift() // ""
l!l
00
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
Truth table0%0/2
A
A= shift()
Aresult
11
00
341 0 ▶ $s =~ s{\\(.)}{ $1 eq 'n' ? "\n"
Branch: 8 branches
BranchTrueFalseTotal
$1 eq 'n' ? : 0 0 0
$1 eq 't' ? : 0 0 0
$1 eq 'r' ? : 0 0 0
$1 eq '0' ? : 0 0 0
$1 eq 'n' ? : 0 0 0
$1 eq 't' ? : 0 0 0
$1 eq 'r' ? : 0 0 0
$1 eq '0' ? : 0 0 0
342 : $1 eq 't' ? "\t"
343 : $1 eq 'r' ? "\r"
344 : $1 eq '0' ? "\0"
345 : $1 }ge;
346 0 $s;
347 0 };
348 0 for my $line (@lines) {
349 0 chomp $line;
350 0 ▶ next if $line eq '';
Branch: 2 branches
BranchTrueFalseTotal
if $line eq "" 0 0 0
if $line eq "" 0 0 0
351 0 my ($name, $type, $kind) = split /\t/, $line;
352 0 ▶ next if defined $kind && $kind =~ /\A(?:materialized|alias|ephemeral)\z/i;
Branch: 2 branches
BranchTrueFalseTotal
if defined $kind and $kind =~ /\A(?:materialized|alias|ephemeral)\z/i 0 0 0
if defined $kind and $kind =~ /\A(?:materialized|alias|ephemeral)\z/i 0 0 0
Condition0%0/6
defined $kind and $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
!ll&&!rl&&r
000
defined $kind and $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $kind and $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
defined $kind $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
MC/DC0%0/2
defined $kind and $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
defined $kind $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
Truth table0%0/3
A and B
A= defined $kindB= $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $kindB= $kind =~ /\A(?:materialized|alias|ephemeral)\z/i
ABresult
0X0
100
111
353 0 push @columns, [$unesc->($name), $unesc->($type)];
354 }
355  
356 0 ▶ die "No columns found for: $describe_sql" unless @columns;
Branch: 2 branches
BranchTrueFalseTotal
unless @columns 0 0 0
unless @columns 0 0 0
357 0 return $class->new(columns => \@columns, _encoder_options(%opts));
358 }
359  
360 47 ▶sub _encoder_options {
Subroutine _encoder_options: called CC=2 SCAR=9.2
361 0 ▶ my (%opts) = @_;
Subroutine _encoder_options: not called CC=2 SCAR=9.2
362 0 ▶ return map { defined $opts{$_} ? ($_ => $opts{$_}) : () } qw(timezone empty_as_null empty_as_default defaults);
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{$_} ? : 12 176 188
defined $opts{$_} ? : 0 0 0
363 }
364  
365 71 ▶sub columns {
Subroutine columns: called CC=1 SCAR=1.2
Pod documented
366 0 ▶ my $self = shift;
Subroutine columns: not called CC=1 SCAR=1.2
Pod documented
367 0 return $self->_columns;
368 }
369  
370 8 ▶sub validate_rows {
Subroutine validate_rows: called CC=3 SCAR=14.2
Pod documented
371 0 ▶ my ($self, $rows) = @_;
Subroutine validate_rows: not called CC=3 SCAR=14.2
Pod documented
372 0 my @errors;
373 0 for my $i (0 .. $#$rows) {
374 0 local $@;
375 0 ▶ unless (eval { $self->encode([$rows->[$i]]); 1 }) {
Branch: 2 branches
BranchTrueFalseTotal
unless (eval { do { $self->encode([$rows->[$i]]); 1 } }) 4 20 24
unless (eval { do { $self->encode([$rows->[$i]]); 1 } }) 0 0 0
376 0 (my $msg = "$@") =~ s/\s*at .+ line \d+\.\s*\z//;
377 0 push @errors, { row => $i, error => $msg };
378 }
379 }
380 0 return \@errors;
381 }
382  
383 20 ▶sub compressed_writer {
Subroutine compressed_writer: called CC=5 SCAR=25.0
Pod documented
384 0 ▶ my ($class, $mode, $writer) = @_;
Subroutine compressed_writer: not called CC=5 SCAR=25.0
Pod documented
385 0 ▶ return $writer if !defined $mode || $mode eq 'raw';
Branch: 2 branches
BranchTrueFalseTotal
if not defined $mode or $mode eq "raw" 8 12 20
if not defined $mode or $mode eq "raw" 0 0 0
Condition50%3/6
not defined $mode or $mode eq "raw"
l!l&&r!l&&!r
4412
not defined $mode or $mode eq "raw"
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $mode or $mode eq "raw"
not defined $mode $mode eq "raw"
MC/DC0%0/2
not defined $mode or $mode eq "raw"
not defined $mode $mode eq "raw"
Truth table100%3/3
not(A) or B
A= not defined $modeB= $mode eq "raw"
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $modeB= $mode eq "raw"
ABresult
1X1
011
000
386 0 ▶ if ($mode eq 'zstd') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq "zstd") 0 12 12
if ($mode eq "zstd") 0 0 0
387 0 require Compress::Zstd;
388 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
389 0 ▶ my $out = Compress::Zstd::compress($_[0]);
Subroutine __ANON__: not called CC=2 SCAR=17.9
390 0 ▶ defined $out or die "zstd compression failed";
Branch: 2 branches
BranchTrueFalseTotal
defined $out or die "zstd compression failed" 0 0 0
defined $out or die "zstd compression failed" 0 0 0
391 0 $writer->($out);
392 0 };
393 }
394 0 ▶ if ($mode eq 'gzip') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq "gzip") 4 8 12
if ($mode eq "gzip") 0 0 0
395 0 require IO::Compress::Gzip;
396 4 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=10.5
397 0 ▶ my $out;
Subroutine __ANON__: not called CC=2 SCAR=10.5
398 0 ▶ IO::Compress::Gzip::gzip(\$_[0], \$out)
Branch: 2 branches
BranchTrueFalseTotal
IO::Compress::Gzip::gzip(\$_[0], \$out) or die "gzip failed: $IO::Compress::Gzip::GzipError" 4 0 4
IO::Compress::Gzip::gzip(\$_[0], \$out) or die "gzip failed: $IO::Compress::Gzip::GzipError" 0 0 0
399 or die "gzip failed: $IO::Compress::Gzip::GzipError";
400 0 $writer->($out);
401 0 };
402 }
403 0 die "Unknown compress mode '$mode' (expected 'zstd', 'gzip', or 'raw')";
404 }
405  
406 # Compressed-block framing used by ClickHouse's CompressedReadBuffer /
407 # CompressedWriteBuffer (native TCP protocol with compression=1 enabled,
408 # and Native-over-HTTP with Content-Encoding: clickhouse-lz4 etc).
409 # Layout:
410 # 16 bytes : checksum (CityHash128 of the next 9 + N bytes)
411 # 1 byte : method tag (0x82 LZ4, 0x90 ZSTD, 0x02 none)
412 # 4 bytes : compressed_size = 1 + 4 + 4 + N (LE UInt32)
413 # 4 bytes : uncompressed_size (LE UInt32)
414 # N bytes : compressed payload
415 #
416 # CH's checksum is CityHash128 from the "cityhash102" variant (Google
417 # CityHash v1.0.2 with ClickHouse's namespace fork). We bundle a port
418 # of that algorithm in cityhash.c, exposed as the `_cityhash128` XSUB,
419 # used as the default hasher below. Callers can plug in a different
420 # 16-byte hasher via the `hasher => \&h` option.
421 87 my %_COMPRESS_METHOD_TAG = (lz4 => 0x82, zstd => 0x90, none => 0x02);
422  
423 38 ▶sub compress_native_block {
Subroutine compress_native_block: called CC=11 SCAR=33.0
Pod documented
424 0 ▶ my ($class, $bytes, %opts) = @_;
Subroutine compress_native_block: not called CC=11 SCAR=33.0
Pod documented
425 0 ▶ my $mode = $opts{mode} // 'lz4';
Condition50%2/4
$opts{"mode"} // "lz4"
l!l
299
$opts{"mode"} // "lz4"
l!l
00
MC/DC100%1/1
$opts{"mode"} // "lz4"
$opts{"mode"}
MC/DC0%0/1
$opts{"mode"} // "lz4"
$opts{"mode"}
Truth table100%2/2
A
A= $opts{"mode"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"mode"}
Aresult
11
00
426 # Default to the bundled CityHash128 v1.0.2 (the "cityhash102"
427 # variant CH itself uses for the compressed-block checksum). The
428 # `hasher` opt is still honored for callers that want to plug in
429 # something else (Digest::CityHash, a vendored copy, etc.).
430 0 ▶ my $hasher = $opts{hasher} // \&_cityhash128;
Condition50%2/4
$opts{"hasher"} // \&ClickHouse::Encoder::_cityhash128
l!l
1028
$opts{"hasher"} // \&ClickHouse::Encoder::_cityhash128
l!l
00
MC/DC100%1/1
$opts{"hasher"} // \&ClickHouse::Encoder::_cityhash128
$opts{"hasher"}
MC/DC0%0/1
$opts{"hasher"} // \&ClickHouse::Encoder::_cityhash128
$opts{"hasher"}
Truth table100%2/2
A
A= $opts{"hasher"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"hasher"}
Aresult
11
00
431  
432 # mode=auto: try LZ4 first, fall back to method-tag 0x02 (uncompressed
433 # inside compressed framing - same shape CH's own CompressedWriteBuffer
434 # emits when the compressed result is >= the input). Saves CPU on
435 # already-incompressible payloads without giving up the checksum
436 # protection of the framing.
437 # The auto-mode probe doubles as the payload when LZ4 wins, so the
438 # input is compressed at most once regardless of which branch wins.
439 0 my $payload;
440 0 ▶ if ($mode eq 'auto') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq "auto") 3 35 38
if ($mode eq "auto") 0 0 0
441 0 require Compress::LZ4;
442 0 my $lz4 = Compress::LZ4::lz4_compress($bytes);
443 0 ▶ if (length($lz4) < length($bytes)) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $lz4 < length $bytes) { } 2 1 3
if (length $lz4 < length $bytes) { } 0 0 0
444 0 $mode = 'lz4';
445 0 $payload = $lz4;
446 } else {
447 0 $mode = 'none';
448 }
449 }
450  
451 0 ▶ my $tag = $_COMPRESS_METHOD_TAG{$mode}
Branch: 2 branches
BranchTrueFalseTotal
my $tag = $_COMPRESS_METHOD_TAG{$mode} or die "compress_native_block: unknown mode '${mode}' " . "(expected 'auto', 'lz4', 'zstd', or 'none')\n" 37 1 38
my $tag = $_COMPRESS_METHOD_TAG{$mode} or die "compress_native_block: unknown mode '${mode}' " . "(expected 'auto', 'lz4', 'zstd', or 'none')\n" 0 0 0
452 or die "compress_native_block: unknown mode '$mode' "
453 . "(expected 'auto', 'lz4', 'zstd', or 'none')\n";
454  
455 0 ▶ if (!defined $payload) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $payload) 35 2 37
unless (defined $payload) 0 0 0
456 0 ▶ if ($mode eq 'lz4') {
Branch: 4 branches
BranchTrueFalseTotal
if ($mode eq 'lz4') { } 17 18 35
elsif ($mode eq 'zstd') { } 3 15 18
if ($mode eq 'lz4') { } 0 0 0
elsif ($mode eq 'zstd') { } 0 0 0
457 0 require Compress::LZ4;
458 # Compress::LZ4::compress() prepends a 4-byte size header
459 # that ClickHouse doesn't want; lz4_compress is the raw-
460 # form variant emitting just the LZ4 byte stream.
461 0 $payload = Compress::LZ4::lz4_compress($bytes);
462 } elsif ($mode eq 'zstd') {
463 0 require Compress::Zstd;
464 0 my $z = Compress::Zstd::compress($bytes);
465 0 ▶ defined $z
Branch: 2 branches
BranchTrueFalseTotal
defined $z or die "compress_native_block: zstd compression failed\n" 3 0 3
defined $z or die "compress_native_block: zstd compression failed\n" 0 0 0
466 or die "compress_native_block: zstd compression failed\n";
467 0 $payload = $z;
468 } else { # none = uncompressed inside framing (method tag 0x02)
469 0 $payload = $bytes;
470 }
471 }
472  
473 0 my $compressed_size = 1 + 4 + 4 + length($payload); # tag + 2*UInt32 + N
474 0 my $hdr = pack('C V V', $tag, $compressed_size, length $bytes);
475 0 my $checksum = $hasher->($hdr . $payload);
476 0 ▶ die "compress_native_block: hasher returned "
Branch: 2 branches
BranchTrueFalseTotal
unless length $checksum == 16 1 36 37
unless length $checksum == 16 0 0 0
477 . length($checksum) . " bytes (expected 16)\n"
478 unless length($checksum) == 16;
479 0 return $checksum . $hdr . $payload;
480 }
481  
482 # Inverse of compress_native_block. Returns (uncompressed_bytes, bytes_consumed).
483 # Verifies the checksum if a hasher is supplied; if hasher is omitted the
484 # block is decompressed without verification (useful for inspecting captured
485 # data when the cityhash impl isn't available).
486 35 ▶sub decompress_native_block {
Subroutine decompress_native_block: called CC=17 SCAR=41.3
Pod documented
487 0 ▶ my ($class, $bytes, %opts) = @_;
Subroutine decompress_native_block: not called CC=17 SCAR=41.3
Pod documented
488 0 ▶ my $off = $opts{offset} // 0;
Condition50%2/4
$opts{"offset"} // 0
l!l
1025
$opts{"offset"} // 0
l!l
00
MC/DC100%1/1
$opts{"offset"} // 0
$opts{"offset"}
MC/DC0%0/1
$opts{"offset"} // 0
$opts{"offset"}
Truth table100%2/2
A
A= $opts{"offset"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"offset"}
Aresult
11
00
489 # Same default as compress_native_block. Callers can pass
490 # hasher => undef explicitly to skip checksum verification.
491 0 ▶ my $hasher = exists $opts{hasher} ? $opts{hasher} : \&_cityhash128;
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'hasher'} ? : 10 25 35
exists $opts{'hasher'} ? : 0 0 0
492 # Wire data the decompressor allocates up front, so a tiny block
493 # claiming 4 GiB asks for 4 GiB. Callers can raise the bound.
494 0 ▶ my $max_size = $opts{max_size} // 1024 * 1024 * 1024;
Condition25%1/4
$opts{"max_size"} // 1073741824
l!l
035
$opts{"max_size"} // 1073741824
l!l
00
MC/DC0%0/1
$opts{"max_size"} // 1073741824
$opts{"max_size"}
MC/DC0%0/1
$opts{"max_size"} // 1073741824
$opts{"max_size"}
Truth table50%1/2
A
A= $opts{"max_size"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"max_size"}
Aresult
11
00
495 0 my $total = length $bytes;
496 0 ▶ die "decompress_native_block: truncated header at offset $off "
Branch: 2 branches
BranchTrueFalseTotal
if $total - $off < 25 0 35 35
if $total - $off < 25 0 0 0
497 . "(need >= 25 bytes, have " . ($total - $off) . ")\n"
498 if $total - $off < 25;
499  
500 0 my $checksum = substr($bytes, $off, 16);
501 0 my $tag = ord substr($bytes, $off + 16, 1);
502 0 my $csize = unpack 'V', substr($bytes, $off + 17, 4); # incl. 9-byte header
503 0 my $usize = unpack 'V', substr($bytes, $off + 21, 4);
504  
505 0 ▶ die "decompress_native_block: compressed_size=$csize < 9 (corrupt)\n"
Branch: 2 branches
BranchTrueFalseTotal
if $csize < 9 0 35 35
if $csize < 9 0 0 0
506 if $csize < 9;
507 0 ▶ die "decompress_native_block: uncompressed_size=$usize exceeds "
Branch: 2 branches
BranchTrueFalseTotal
if $usize > $max_size 1 34 35
if $usize > $max_size 0 0 0
508 . "max_size=$max_size (pass max_size => N to raise)\n"
509 if $usize > $max_size;
510 0 my $payload_len = $csize - 9;
511 0 my $end = $off + 16 + $csize;
512 0 ▶ die "decompress_native_block: block extends past buffer end\n"
Branch: 2 branches
BranchTrueFalseTotal
if $end > $total 1 33 34
if $end > $total 0 0 0
513 if $end > $total;
514  
515 0 my $hdr = substr($bytes, $off + 16, 9);
516 0 my $payload = substr($bytes, $off + 25, $payload_len);
517  
518 0 ▶ if ($hasher) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hasher) 30 3 33
if ($hasher) 0 0 0
519 0 my $got = $hasher->($hdr . $payload);
520 0 ▶ die "decompress_native_block: checksum mismatch\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $got eq $checksum 1 29 30
unless $got eq $checksum 0 0 0
521 unless $got eq $checksum;
522 }
523  
524 0 my $out;
525 0 ▶ if ($tag == 0x02) { # uncompressed inside framing
Branch: 6 branches
BranchTrueFalseTotal
if ($tag == 2) { } 9 23 32
elsif ($tag == 130) { } 19 4 23
elsif ($tag == 144) { } 3 1 4
if ($tag == 2) { } 0 0 0
elsif ($tag == 130) { } 0 0 0
elsif ($tag == 144) { } 0 0 0
526 0 $out = $payload;
527 } elsif ($tag == 0x82) { # LZ4
528 0 require Compress::LZ4;
529 # lz4_decompress wants the expected uncompressed size as its
530 # second argument since the raw stream has no length prefix.
531 0 $out = Compress::LZ4::lz4_decompress($payload, $usize);
532 0 ▶ die "decompress_native_block: lz4 decompression failed\n"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $out 0 19 19
unless defined $out 0 0 0
533 unless defined $out;
534 } elsif ($tag == 0x90) { # ZSTD
535 0 require Compress::Zstd;
536 0 $out = Compress::Zstd::decompress($payload);
537 0 ▶ die "decompress_native_block: zstd decompression failed\n"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $out 0 3 3
unless defined $out 0 0 0
538 unless defined $out;
539 } else {
540 0 die sprintf("decompress_native_block: unknown method tag 0x%02x "
541 . "(expected 0x02 NONE, 0x82 LZ4, or 0x90 ZSTD)\n", $tag);
542 }
543 0 ▶ die "decompress_native_block: decompressed size mismatch "
Branch: 2 branches
BranchTrueFalseTotal
unless length $out == $usize 1 30 31
unless length $out == $usize 0 0 0
544 . "(header says $usize, got " . length($out) . ")\n"
545 unless length($out) == $usize;
546 0 ▶ return wantarray ? ($out, $end - $off) : $out;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 15 15 30
wantarray ? : 0 0 0
547 }
548  
549 # Expand any Nested(field T, ...) entries in a column list into the flat
550 # `name.field Array(T)` columns ClickHouse stores them as. Returns a new
551 # arrayref; non-Nested columns pass through unchanged. Pair with new():
552 # my $enc = ClickHouse::Encoder->new(
553 # columns => ClickHouse::Encoder->flatten_nested(\@user_columns));
554 15 ▶sub flatten_nested {
Subroutine flatten_nested: called CC=6 SCAR=23.5
Pod documented
555 0 ▶ my ($class, $cols) = @_;
Subroutine flatten_nested: not called CC=6 SCAR=23.5
Pod documented
556 0 my @out;
557 0 for my $c (@$cols) {
558 0 my ($name, $type) = @$c;
559 0 ▶ if ($type =~ /\ANested\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\ANested\((.+)\)\z/s) { } 11 8 19
if ($type =~ /\ANested\((.+)\)\z/s) { } 0 0 0
560 0 my @parts = _split_paren_list($1);
561 0 ▶ @parts or die "Nested($1) for column '$name' has no elements";
Branch: 2 branches
BranchTrueFalseTotal
@parts or die "Nested($1) for column '${name}' has no elements" 10 1 11
@parts or die "Nested($1) for column '${name}' has no elements" 0 0 0
562 0 for my $part (@parts) {
563 0 ▶ $part =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s+(.+?)\s*\z/s
Branch: 2 branches
BranchTrueFalseTotal
$part =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s+(.+?)\s*\z/s or die "Nested element '${part}' is not 'name Type'" 16 4 20
$part =~ /\A([A-Za-z_][A-Za-z0-9_]*)\s+(.+?)\s*\z/s or die "Nested element '${part}' is not 'name Type'" 0 0 0
564 or die "Nested element '$part' is not 'name Type'";
565 0 push @out, ["$name.$1", "Array($2)"];
566 }
567 } else {
568 0 push @out, [$name, $type];
569 }
570 }
571 0 return \@out;
572 }
573  
574 # Split a comma-separated list at depth-0 commas (so Tuple(Int32, String)
575 # inside a Nested element stays one entry).
576 43 ▶sub _split_paren_list {
Subroutine _split_paren_list: called CC=13 SCAR=36.5
577 0 ▶ my $body = shift;
Subroutine _split_paren_list: not called CC=13 SCAR=36.5
578 0 my @parts;
579 0 my ($start, $depth, $len) = (0, 0, length $body);
580 0 ▶ for (my $i = 0; $i <= $len; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $len) 1307 43 1350
if ($i <= $len) 0 0 0
581 0 ▶ my $c = $i < $len ? substr($body, $i, 1) : ',';
Branch: 2 branches
BranchTrueFalseTotal
$i < $len ? : 1264 43 1307
$i < $len ? : 0 0 0
582 0 ▶ if ($c eq "'" || $c eq '`' || $c eq '"') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "'" or $c eq "`" or $c eq "\"") 46 1261 1307
if ($c eq "'" or $c eq "`" or $c eq "\"") 0 0 0
Condition41%5/12
$c eq "'" or $c eq "`" or $c eq "\""
l!l&&r!l&&!r
4601261
$c eq "'" or $c eq "`"
l!l&&r!l&&!r
14321261
$c eq "'" or $c eq "`" or $c eq "\""
l!l&&r!l&&!r
000
$c eq "'" or $c eq "`"
l!l&&r!l&&!r
000
MC/DC66%2/3
$c eq "'" or $c eq "`" or $c eq "\""
$c eq "'" $c eq "`" $c eq "\""
MC/DC0%0/3
$c eq "'" or $c eq "`" or $c eq "\""
$c eq "'" $c eq "`" $c eq "\""
Truth table75%3/4
A or B or C
A= $c eq "'"B= $c eq "`"C= $c eq "\""
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B or C
A= $c eq "'"B= $c eq "`"C= $c eq "\""
ABCresult
1XX1
01X1
0011
0000
583 0 $i = _skip_sql_quoted($body, $i) - 1;
584 0 next;
585 }
586 0 ▶ if ($c eq '(') { $depth++ }
Branch: 6 branches
BranchTrueFalseTotal
if ($c eq '(') { } 45 1216 1261
elsif ($c eq ')') { } 45 1171 1216
elsif ($c eq ',' and not $depth) { } 81 2128 2209
if ($c eq '(') { } 0 0 0
elsif ($c eq ')') { } 0 0 0
elsif ($c eq ',' and not $depth) { } 0 0 0
587 0 ▶ elsif ($c eq ')') { die "unbalanced ')'\n" if --$depth < 0 }
Branch: 2 branches
BranchTrueFalseTotal
if --$depth < 0 0 45 45
if --$depth < 0 0 0 0
588 ▶ elsif ($c eq ',' && !$depth) {
Condition50%3/6
$c eq "," and not $depth
!ll&&!rl&&r
10385281
$c eq "," and not $depth
!ll&&!rl&&r
000
MC/DC100%2/2
$c eq "," and not $depth
$c eq "," not $depth
MC/DC0%0/2
$c eq "," and not $depth
$c eq "," not $depth
Truth table100%3/3
A and not(B)
A= $c eq ","B= not $depth
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $c eq ","B= not $depth
ABresult
0X0
100
111
589 0 my $p = substr($body, $start, $i - $start);
590 0 $p =~ s/\A\s+//; $p =~ s/\s+\z//;
591 0 ▶ push @parts, $p if length $p;
Branch: 2 branches
BranchTrueFalseTotal
if length $p 80 1 81
if length $p 0 0 0
592 0 $start = $i + 1;
593 }
594 }
595 0 ▶ die "unbalanced '('\n" if $depth;
Branch: 2 branches
BranchTrueFalseTotal
if $depth 0 43 43
if $depth 0 0 0
596 0 return @parts;
597 }
598  
599 # Row-oriented decode: { ncols, nrows, names, types, rows } where
600 # rows is an arrayref of arrayrefs. Calls the XS decode_block_rows,
601 # which distributes column values into per-row arrayrefs as each
602 # column is decoded and frees the column AV eagerly. Peak memory
603 # is one column's AV plus the row AVs (vs a Perl-side transpose
604 # that holds ALL column AVs alongside the half-built row AVs).
605 # Throughput is similar to the column-major path; the win is the
606 # tighter memory profile on wide blocks.
607 44 ▶sub decode_rows {
Subroutine decode_rows: called CC=2 SCAR=10.9
Pod documented
608 0 ▶ my ($class, $bytes, $offset) = @_;
Subroutine decode_rows: not called CC=2 SCAR=10.9
Pod documented
609 0 ▶ return $class->decode_block_rows($bytes, $offset // 0);
Condition25%1/4
$offset // 0
l!l
044
$offset // 0
l!l
00
MC/DC0%0/1
$offset // 0
$offset
MC/DC0%0/1
$offset // 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
Truth table0%0/2
A
A= $offset
Aresult
11
00
610 }
611  
612 # Decode a concatenated stream of Native blocks (the body of a
613 # `select ... format native` response). Returns an arrayref of the
614 # same hashref shape as decode_block. Stops cleanly when bytes are
615 # exhausted; partial trailing bytes raise an error from XS.
616 # Uses the 3-arg form of decode_block (with offset) to avoid O(N^2)
617 # substr copies on long streams.
618 8 ▶sub decode_blocks {
Subroutine decode_blocks: called CC=4 SCAR=17.9
Pod documented
619 0 ▶ my ($class, $bytes, $cb, %opts) = @_;
Subroutine decode_blocks: not called CC=4 SCAR=17.9
Pod documented
620 0 my $keep = $opts{keep};
621 0 my $off = 0;
622 0 my $len = length $bytes;
623 # Callback form: hand each block to $cb as it's decoded; never
624 # accumulate. Useful for streaming selects where the full block
625 # list would not fit comfortably in memory.
626 0 ▶ if ($cb) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cb) 2 6 8
if ($cb) 0 0 0
627 0 ▶ while ($off < $len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($off < $len) 5 2 7
if ($off < $len) 0 0 0
628 0 my $block = $class->decode_block($bytes, $off, $keep);
629 0 $off += $block->{consumed};
630 0 $cb->($block);
631 }
632 0 return;
633 }
634 0 my @blocks;
635 0 ▶ while ($off < $len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($off < $len) 10 5 15
if ($off < $len) 0 0 0
636 0 my $block = $class->decode_block($bytes, $off, $keep);
637 0 $off += $block->{consumed};
638 0 push @blocks, $block;
639 }
640 0 return \@blocks;
641 }
642  
643 # Return a coderef that yields one block per call (undef when done).
644 # Holds a reference to $bytes; the closure is the only thing that
645 # survives between calls.
646 3 ▶sub decode_blocks_iter {
Subroutine decode_blocks_iter: called CC=1 SCAR=1.2
Pod documented
647 0 ▶ my ($class, $bytes, %opts) = @_;
Subroutine decode_blocks_iter: not called CC=1 SCAR=1.2
Pod documented
648 0 my $keep = $opts{keep};
649 0 my $off = 0;
650 0 my $len = length $bytes;
651 9 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
652 0 ▶ return if $off >= $len;
Subroutine __ANON__: not called CC=2 SCAR=9.2
Branch: 2 branches
BranchTrueFalseTotal
if $off >= $len 4 5 9
if $off >= $len 0 0 0
653 0 my $block = $class->decode_block($bytes, $off, $keep);
654 0 $off += $block->{consumed};
655 0 return $block;
656 0 };
657 }
658  
659 # Pull-style decoder that reads incrementally from a filehandle (or
660 # any IO::Handle-ish object). For each complete block, invokes $cb
661 # with the block hashref. Keeps a sliding buffer; on truncated decode
662 # it reads more bytes and retries. Useful when the response body is
663 # too large to fit in memory.
664 21 ▶sub decode_stream {
Subroutine decode_stream: called CC=21 SCAR=44.3
Pod documented
665 0 ▶ my ($class, $fh, $cb, %opts) = @_;
Subroutine decode_stream: not called CC=21 SCAR=44.3
Pod documented
666 0 ▶ my $chunk_size = $opts{chunk_size} // 64 * 1024;
Condition50%2/4
$opts{"chunk_size"} // 65536
l!l
201
$opts{"chunk_size"} // 65536
l!l
00
MC/DC100%1/1
$opts{"chunk_size"} // 65536
$opts{"chunk_size"}
MC/DC0%0/1
$opts{"chunk_size"} // 65536
$opts{"chunk_size"}
Truth table100%2/2
A
A= $opts{"chunk_size"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"chunk_size"}
Aresult
11
00
667 0 my $keep = $opts{keep};
668 0 my $decompress = $opts{decompress};
669 0 my $buf = ''; # raw bytes from the filehandle
670 0 my $inner = ''; # decompressed Native bytes (== $buf when !decompress)
671 0 my $done = 0;
672 # A truncated decode re-parses the partial block from byte 0, so
673 # retrying on every chunk is quadratic in block size. The decoder
674 # says how many bytes it came up short, so wait for exactly that
675 # much before trying again: no wasted re-parse, and - unlike a blind
676 # doubling - never a complete block held back or an unbounded read.
677 0 my $need_at = 0;
678 0 my $at_eof = 0;
679 0 ▶ until ($done) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($done) 345 19 364
unless ($done) 0 0 0
680 # Phase 1: peel compressed-block frames out of $buf into $inner.
681 0 ▶ if ($decompress) {
Branch: 2 branches
BranchTrueFalseTotal
if ($decompress) { } 12 333 345
if ($decompress) { } 0 0 0
682 0 ▶ while (length($buf) >= 25) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $buf >= 25) 10 7 17
if (length $buf >= 25) 0 0 0
683 0 my $csize = unpack 'V', substr($buf, 17, 4);
684 0 ▶ last if length($buf) < 16 + $csize;
Branch: 2 branches
BranchTrueFalseTotal
if length $buf < 16 + $csize 5 5 10
if length $buf < 16 + $csize 0 0 0
685 0 my ($plain, $consumed) = eval {
686 0 $class->decompress_native_block($buf)
687 };
688 0 ▶ die $@ if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 0 5 5
if $@ 0 0 0
689 0 $inner .= $plain;
690 0 substr($buf, 0, $consumed, '');
691 }
692 } else {
693 0 $inner = $buf;
694 }
695 # Phase 2: decode complete Native blocks out of $inner. At EOF the
696 # threshold is ignored - the buffer gets a last chance before it
697 # is called trailing junk.
698 0 ▶ while (length($inner) > 0 && ($at_eof || length($inner) >= $need_at)) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $inner > 0 and $at_eof || length $inner >= $need_at) 327 62 389
if (length $inner > 0 and $at_eof || length $inner >= $need_at) 0 0 0
Condition50%6/12
length $inner > 0 and $at_eof || length $inner >= $need_at
!ll&&!rl&&r
4715327
$at_eof or length $inner >= $need_at
l!l&&r!l&&!r
132615
length $inner > 0 and $at_eof || length $inner >= $need_at
!ll&&!rl&&r
000
$at_eof or length $inner >= $need_at
l!l&&r!l&&!r
000
MC/DC100%3/3
length $inner > 0 and $at_eof || length $inner >= $need_at
length $inner > 0 $at_eof length $inner >= $need_at
MC/DC0%0/3
length $inner > 0 and $at_eof || length $inner >= $need_at
length $inner > 0 $at_eof length $inner >= $need_at
Truth table100%4/4
A and B or C
A= length $inner > 0B= $at_eofC= length $inner >= $need_at
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= length $inner > 0B= $at_eofC= length $inner >= $need_at
ABCresult
0XX0
1000
11X1
1011
699 0 my $block = eval { $class->decode_block($inner, 0, $keep) };
700 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 283 44 327
if ($@) 0 0 0
701 # Both shapes mean "not enough bytes yet". The prologue's
702 # "exceeds remaining buffer" guard compares a header count
703 # against what has arrived, so a block of N rows trips it
704 # whenever a chunk boundary leaves fewer than N bytes
705 # buffered - ordinary on real data. Reading more cannot
706 # loop: EOF ends the outer scan.
707 0 ▶ if ($@ =~ /buffer truncated \(need (\d+) more/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /buffer truncated \(need (\d+) more/i) 110 173 283
if ($@ =~ /buffer truncated \(need (\d+) more/i) 0 0 0
708 0 $need_at = length($inner) + $1;
709 0 last;
710 }
711 0 ▶ last if $@ =~ /buffer truncated|exceeds remaining buffer/i;
Branch: 2 branches
BranchTrueFalseTotal
if $@ =~ /buffer truncated|exceeds remaining buffer/i 173 0 173
if $@ =~ /buffer truncated|exceeds remaining buffer/i 0 0 0
712 0 die $@; # real error
713 }
714 0 $cb->($block);
715 0 substr($inner, 0, $block->{consumed}, '');
716 0 $need_at = 0;
717 }
718 # When not decompressing, $inner aliases $buf - carry the residual
719 # back so the next read sees the unconsumed tail.
720 0 ▶ $buf = $inner unless $decompress;
Branch: 2 branches
BranchTrueFalseTotal
unless $decompress 333 12 345
unless $decompress 0 0 0
721 # Read more bytes. read() returns 0 on EOF, undef on error.
722 0 my $more;
723 0 my $n = read $fh, $more, $chunk_size;
724 0 ▶ die "decode_stream: read error: $!" if !defined $n;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $n 0 345 345
unless defined $n 0 0 0
725 0 ▶ if ($n == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n == 0) { } 22 323 345
if ($n == 0) { } 0 0 0
726 # EOF. Re-run the scan ungated once, then anything still left
727 # in either buffer is a truncated final block; raise rather
728 # than swallow.
729 0 ▶ if (!$at_eof && length $inner) { $at_eof = 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if (not $at_eof and length $inner) 1 21 22
if (not $at_eof and length $inner) 0 0 0
Condition50%3/6
not $at_eof and length $inner
!ll&&!rl&&r
1201
not $at_eof and length $inner
!ll&&!rl&&r
000
MC/DC100%2/2
not $at_eof and length $inner
not $at_eof length $inner
MC/DC0%0/2
not $at_eof and length $inner
not $at_eof length $inner
Truth table100%3/3
not(A) and B
A= not $at_eofB= length $inner
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $at_eofB= length $inner
ABresult
0X0
100
111
730 0 ▶ die "decode_stream: " . length($buf) . " trailing bytes "
Branch: 2 branches
BranchTrueFalseTotal
if $decompress and length $buf 1 20 21
if $decompress and length $buf 0 0 0
Condition50%3/6
$decompress and length $buf
!ll&&!rl&&r
1821
$decompress and length $buf
!ll&&!rl&&r
000
MC/DC100%2/2
$decompress and length $buf
$decompress length $buf
MC/DC0%0/2
$decompress and length $buf
$decompress length $buf
Truth table100%3/3
A and B
A= $decompressB= length $buf
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $decompressB= length $buf
ABresult
0X0
100
111
731 . "after last complete compressed block"
732 if $decompress && length $buf;
733 0 ▶ die "decode_stream: " . length($inner) . " trailing bytes "
Branch: 2 branches
BranchTrueFalseTotal
if length $inner 1 19 20
if length $inner 0 0 0
734 . "after last complete block" if length $inner;
735 0 $done = 1;
736 } else {
737 0 $buf .= $more;
738 }
739 }
740 0 return;
741 }
742  
743 # Query the ClickHouse HTTP endpoint for its version. Returns a list
744 # of (major, minor, patch, build) integers and the raw string. Useful
745 # for capability gating in user code (e.g. only use JSON columns if
746 # the server is at least 24.8). HTTP-only; native TCP not supported.
747 6 ▶sub server_version {
Subroutine server_version: called CC=8 SCAR=29.9
Pod documented
748 0 ▶ my ($class, %opts) = @_;
Subroutine server_version: not called CC=8 SCAR=29.9
Pod documented
749 0 my ($url, $hdr) = _http_url_headers('select version()', %opts);
750 0 ▶ my $resp = _http_tiny(%opts, timeout => $opts{timeout} // 10)
Condition50%2/4
$opts{"timeout"} // 10
l!l
15
$opts{"timeout"} // 10
l!l
00
MC/DC100%1/1
$opts{"timeout"} // 10
$opts{"timeout"}
MC/DC0%0/1
$opts{"timeout"} // 10
$opts{"timeout"}
Truth table100%2/2
A
A= $opts{"timeout"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"timeout"}
Aresult
11
00
751 ->get($url, { headers => $hdr });
752 die "HTTP select version() failed (status $resp->{status}): "
753 . "$resp->{content}\n"
754 0 ▶ unless $resp->{success};
Branch: 2 branches
BranchTrueFalseTotal
unless $resp->{"success"} 2 4 6
unless $resp->{"success"} 0 0 0
755 0 (my $raw = $resp->{content}) =~ s/\s+\z//;
756 0 my @parts = ($raw =~ /(\d+)/g);
757 return wantarray
758 0 ▶ ? (@parts, $raw)
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 1 3 4
wantarray ? : 0 0 0
Condition31%5/16
$parts[0] // 0
l!l
30
$parts[1] // 0
l!l
30
$parts[2] // 0
l!l
30
$parts[3] // 0
l!l
21
$parts[0] // 0
l!l
00
$parts[1] // 0
l!l
00
$parts[2] // 0
l!l
00
$parts[3] // 0
l!l
00
MC/DC0%0/1
$parts[0] // 0
$parts[0]
MC/DC0%0/1
$parts[1] // 0
$parts[1]
MC/DC0%0/1
$parts[2] // 0
$parts[2]
MC/DC100%1/1
$parts[3] // 0
$parts[3]
MC/DC0%0/1
$parts[0] // 0
$parts[0]
MC/DC0%0/1
$parts[1] // 0
$parts[1]
MC/DC0%0/1
$parts[2] // 0
$parts[2]
MC/DC0%0/1
$parts[3] // 0
$parts[3]
Truth table50%1/2
A
A= $parts[0]
Aresult
11
00
Truth table50%1/2
A
A= $parts[1]
Aresult
11
00
Truth table50%1/2
A
A= $parts[2]
Aresult
11
00
Truth table100%2/2
A
A= $parts[3]
Aresult
11
00
Truth table0%0/2
A
A= $parts[0]
Aresult
11
00
Truth table0%0/2
A
A= $parts[1]
Aresult
11
00
Truth table0%0/2
A
A= $parts[2]
Aresult
11
00
Truth table0%0/2
A
A= $parts[3]
Aresult
11
00
759 : { major => $parts[0] // 0, minor => $parts[1] // 0,
760 patch => $parts[2] // 0, build => $parts[3] // 0,
761 raw => $raw };
762 }
763  
764 # Lightweight liveness check via CH's /ping endpoint. Returns 1 on
765 # success; croaks on HTTP failure (which includes connection refused,
766 # timeout, or non-2xx response). Use to gate on server availability in
767 # bootstrap scripts and bulk-load orchestration.
768 1 ▶sub ping {
Subroutine ping: called CC=3 SCAR=17.0
Pod documented
769 0 ▶ my ($class, %opts) = @_;
Subroutine ping: not called CC=3 SCAR=17.0
Pod documented
770 0 my ($scheme, $host, $port) = _check_endpoint(\%opts);
771 0 my $url = "$scheme://$host:$port/ping";
772 0 ▶ my $resp = _http_tiny(%opts, timeout => $opts{timeout} // 5)->get($url);
Condition25%1/4
$opts{"timeout"} // 5
l!l
10
$opts{"timeout"} // 5
l!l
00
MC/DC0%0/1
$opts{"timeout"} // 5
$opts{"timeout"}
MC/DC0%0/1
$opts{"timeout"} // 5
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"timeout"}
Aresult
11
00
773 die "ping: HTTP $resp->{status}: $resp->{content}\n"
774 0 ▶ unless $resp->{success};
Branch: 2 branches
BranchTrueFalseTotal
unless $resp->{"success"} 1 0 1
unless $resp->{"success"} 0 0 0
775 0 return 1;
776 }
777  
778 # Parse a Well-Known-Text (WKT) geometry string into the nested-arrayref
779 # representation that the Geo column encoders accept. Supported geometries:
780 # POINT, LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON. Coordinates
781 # are decimal numbers separated by whitespace; rings/parts are
782 # parenthesized. The Ring CH type has no WKT name (it is a single closed
783 # LINESTRING); accept LINESTRING for Ring as well. Returns the structure
784 # only; the caller chooses which Geo column to feed it into.
785 21 ▶sub parse_wkt {
Subroutine parse_wkt: called CC=14 SCAR=36.5
Pod documented
786 0 ▶ my ($class, $wkt) = @_;
Subroutine parse_wkt: not called CC=14 SCAR=36.5
Pod documented
787 0 ▶ die "parse_wkt: input required\n" unless defined $wkt;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $wkt 1 20 21
unless defined $wkt 0 0 0
788 0 $wkt =~ s/\A\s+//; $wkt =~ s/\s+\z//;
789 0 ▶ my ($kind, $rest) = $wkt =~ /\A([A-Za-z]+)\s*(.*)\z/s
Branch: 2 branches
BranchTrueFalseTotal
() = $wkt =~ /\A([A-Za-z]+)\s*(.*)\z/s or die "parse_wkt: not a WKT geometry: $wkt\n" 19 1 20
() = $wkt =~ /\A([A-Za-z]+)\s*(.*)\z/s or die "parse_wkt: not a WKT geometry: $wkt\n" 0 0 0
790 or die "parse_wkt: not a WKT geometry: $wkt\n";
791 0 $kind = uc $kind;
792 0 ▶ die "parse_wkt: $kind missing '('\n" unless $rest =~ /\A\s*\(/;
Branch: 2 branches
BranchTrueFalseTotal
unless $rest =~ /\A\s*\(/ 1 18 19
unless $rest =~ /\A\s*\(/ 0 0 0
793 0 ▶ die "parse_wkt: $kind unmatched parens\n" unless $rest =~ /\)\s*\z/;
Branch: 2 branches
BranchTrueFalseTotal
unless $rest =~ /\)\s*\z/ 1 17 18
unless $rest =~ /\)\s*\z/ 0 0 0
794 0 $rest = _wkt_group($rest);
795 0 ▶ die "parse_wkt: no parts parsed\n" if $kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ && $rest !~ /\S/;
Branch: 2 branches
BranchTrueFalseTotal
if $kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ and not $rest =~ /\S/ 1 16 17
if $kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ and not $rest =~ /\S/ 0 0 0
Condition50%3/6
$kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ and not $rest =~ /\S/
!ll&&!rl&&r
1331
$kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ and not $rest =~ /\S/
!ll&&!rl&&r
000
MC/DC100%2/2
$kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ and not $rest =~ /\S/
$kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ not $rest =~ /\S/
MC/DC0%0/2
$kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ and not $rest =~ /\S/
$kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/ not $rest =~ /\S/
Truth table100%3/3
A and not(B)
A= $kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/B= not $rest =~ /\S/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $kind =~ /\A(?:POLYGON|MULTILINESTRING)\z/B= not $rest =~ /\S/
ABresult
0X0
100
111
796 0 ▶ die "parse_wkt: no polygons parsed\n" if $kind eq 'MULTIPOLYGON' && $rest !~ /\S/;
Branch: 2 branches
BranchTrueFalseTotal
if $kind eq "MULTIPOLYGON" and not $rest =~ /\S/ 1 15 16
if $kind eq "MULTIPOLYGON" and not $rest =~ /\S/ 0 0 0
Condition50%3/6
$kind eq "MULTIPOLYGON" and not $rest =~ /\S/
!ll&&!rl&&r
1231
$kind eq "MULTIPOLYGON" and not $rest =~ /\S/
!ll&&!rl&&r
000
MC/DC100%2/2
$kind eq "MULTIPOLYGON" and not $rest =~ /\S/
$kind eq "MULTIPOLYGON" not $rest =~ /\S/
MC/DC0%0/2
$kind eq "MULTIPOLYGON" and not $rest =~ /\S/
$kind eq "MULTIPOLYGON" not $rest =~ /\S/
Truth table100%3/3
A and not(B)
A= $kind eq "MULTIPOLYGON"B= not $rest =~ /\S/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $kind eq "MULTIPOLYGON"B= not $rest =~ /\S/
ABresult
0X0
100
111
797  
798 0 ▶ return _wkt_point($rest) if $kind eq 'POINT';
Branch: 2 branches
BranchTrueFalseTotal
if $kind eq "POINT" 5 10 15
if $kind eq "POINT" 0 0 0
799 0 ▶ return [_wkt_points($rest)] if $kind eq 'LINESTRING';
Branch: 2 branches
BranchTrueFalseTotal
if $kind eq "LINESTRING" 3 7 10
if $kind eq "LINESTRING" 0 0 0
800 0 ▶ if ($kind eq 'POLYGON' || $kind eq 'MULTILINESTRING') {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq "POLYGON" or $kind eq "MULTILINESTRING") 3 4 7
if ($kind eq "POLYGON" or $kind eq "MULTILINESTRING") 0 0 0
Condition50%3/6
$kind eq "POLYGON" or $kind eq "MULTILINESTRING"
l!l&&r!l&&!r
214
$kind eq "POLYGON" or $kind eq "MULTILINESTRING"
l!l&&r!l&&!r
000
MC/DC100%2/2
$kind eq "POLYGON" or $kind eq "MULTILINESTRING"
$kind eq "POLYGON" $kind eq "MULTILINESTRING"
MC/DC0%0/2
$kind eq "POLYGON" or $kind eq "MULTILINESTRING"
$kind eq "POLYGON" $kind eq "MULTILINESTRING"
Truth table100%3/3
A or B
A= $kind eq "POLYGON"B= $kind eq "MULTILINESTRING"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $kind eq "POLYGON"B= $kind eq "MULTILINESTRING"
ABresult
1X1
011
000
801 0 return [map { [_wkt_points(_wkt_group($_))] } _split_paren_list($rest)];
802 }
803 0 ▶ if ($kind eq 'MULTIPOLYGON') {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq "MULTIPOLYGON") 3 1 4
if ($kind eq "MULTIPOLYGON") 0 0 0
804 return [map {
805 0 [map { [_wkt_points(_wkt_group($_))] }
806 _split_paren_list(_wkt_group($_))]
807 } _split_paren_list($rest)];
808 }
809 0 die "parse_wkt: unsupported geometry '$kind'\n";
810 }
811  
812 33 ▶sub _wkt_group {
Subroutine _wkt_group: called CC=2 SCAR=10.5
813 0 ▶ my $s = shift;
Subroutine _wkt_group: not called CC=2 SCAR=10.5
814 0 ▶ $s =~ /\A\s*\((.*)\)\s*\z/s or die "parse_wkt: unmatched parens\n";
Branch: 2 branches
BranchTrueFalseTotal
$s =~ /\A\s*\((.*)\)\s*\z/s or die "parse_wkt: unmatched parens\n" 33 0 33
$s =~ /\A\s*\((.*)\)\s*\z/s or die "parse_wkt: unmatched parens\n" 0 0 0
815 0 return $1;
816 }
817  
818 52 ▶sub _wkt_point {
Subroutine _wkt_point: called CC=3 SCAR=14.8
819 0 ▶ my $s = shift;
Subroutine _wkt_point: not called CC=3 SCAR=14.8
820 0 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
821 0 my @c = split /\s+/, $s;
822 die "parse_wkt: POINT needs 2 coords (numeric), got '$s'\n"
823 0 ▶ unless @c == 2 && !grep { !/\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/ } @c;
Branch: 2 branches
BranchTrueFalseTotal
unless @c == 2 and not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c) 2 50 52
unless @c == 2 and not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c) 0 0 0
Condition33%2/6
@c == 2 and not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
!ll&&!rl&&r
2050
@c == 2 and not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
!ll&&!rl&&r
000
MC/DC50%1/2
@c == 2 and not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
@c == 2 not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
MC/DC0%0/2
@c == 2 and not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
@c == 2 not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
Truth table66%2/3
A and not(B)
A= @c == 2B= not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= @c == 2B= not grep({not /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;} @c)
ABresult
0X0
100
111
824 0 return [map { $_ + 0 } @c];
825 }
826  
827 14 ▶sub _wkt_points {
Subroutine _wkt_points: called CC=2 SCAR=9.2
828 0 ▶ my ($s) = @_;
Subroutine _wkt_points: not called CC=2 SCAR=9.2
829 return map {
830 0 my $point = eval { _wkt_point($_) };
831 0 ▶ die "parse_wkt: point needs 2 coords (numeric)\n" if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 1 46 47
if $@ 0 0 0
832 0 $point;
833 } split /,/, $s;
834 }
835  
836 # Return the list of supported ClickHouse type names (parametric types
837 # are listed as their syntactic prefix). For runtime feature detection
838 # and tooling.
839 1 ▶sub types {
Subroutine types: called CC=1 SCAR=1.2
Pod documented
840 0 ▶ return (qw(
Subroutine types: not called CC=1 SCAR=1.2
Pod documented
841 Int8 Int16 Int32 Int64
842 UInt8 UInt16 UInt32 UInt64
843 Float32 Float64 BFloat16
844 String FixedString
845 Date Date32 DateTime DateTime64
846 Decimal Decimal32 Decimal64 Decimal128 Decimal256
847 Enum8 Enum16
848 Bool Boolean UUID IPv4 IPv6
849 Array Tuple Nullable Map LowCardinality Variant
850 Point Ring LineString MultiLineString Polygon MultiPolygon
851 SimpleAggregateFunction
852 JSON Object Dynamic
853 ));
854 }
855  
856 # Compare two column lists, return { added, removed, changed }. Each
857 # slot holds [name, type] pairs from $b (added/changed) or $a (removed).
858 # Useful for migration scripts and detecting drift between source and
859 # destination schemas in CH-to-CH pipelines.
860 13 ▶sub schema_diff {
Subroutine schema_diff: called CC=6 SCAR=23.5
Pod documented
861 0 ▶ my ($class, $a, $b) = @_;
Subroutine schema_diff: not called CC=6 SCAR=23.5
Pod documented
862 0 my %a = map { $_->[0] => $_->[1] } @$a;
863 0 my %b = map { $_->[0] => $_->[1] } @$b;
864 0 my (@added, @removed, @changed);
865 0 for my $name (sort keys %b) {
866 0 ▶ if (!exists $a{$name}) {
Branch: 4 branches
BranchTrueFalseTotal
if (not exists $a{$name}) { } 7 14 21
elsif ($a{$name} ne $b{$name}) { } 5 9 14
if (not exists $a{$name}) { } 0 0 0
elsif ($a{$name} ne $b{$name}) { } 0 0 0
867 0 push @added, [$name, $b{$name}];
868 } elsif ($a{$name} ne $b{$name}) {
869 0 push @changed, [$name, $a{$name}, $b{$name}];
870 }
871 }
872 0 for my $name (sort keys %a) {
873 0 ▶ push @removed, [$name, $a{$name}] unless exists $b{$name};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $b{$name} 5 14 19
unless exists $b{$name} 0 0 0
874 }
875 0 return { added => \@added, removed => \@removed, changed => \@changed };
876 }
877  
878 # Quote a CH identifier (table name, column name) with backticks,
879 # escaping any backtick within. CH's lexer processes C-style backslash
880 # escapes inside backtick identifiers, so a literal backslash must be
881 # escaped too (else `a\` would read as an escaped backtick and the
882 # identifier would never close). This mirrors ClickHouse's own
883 # backQuote(): backslash first, then backtick. CH also accepts
884 # double-quote quoting; we use backticks because show create table does.
885 52 ▶sub _quote_ident {
Subroutine _quote_ident: called CC=1 SCAR=1.2
886 0 ▶ my $name = shift;
Subroutine _quote_ident: not called CC=1 SCAR=1.2
887 0 $name =~ s/\\/\\\\/g;
888 0 $name =~ s/`/\\`/g;
889 0 return "`$name`";
890 }
891  
892 # Render one column definition for format_create_table. A column entry
893 # is [name, type] or [name, type, \%col] where %col may carry
894 # default/materialized/alias (mutually exclusive), codec, ttl, comment.
895 # Expression-valued keys are inserted verbatim - the caller owns their
896 # SQL-correctness; only the comment is quoted (it is the one string
897 # literal). Clause order matches CH's own `show create table` output;
898 # keywords are lowercased to match the rest of the generated SQL.
899 23 ▶sub _format_column {
Subroutine _format_column: called CC=8 SCAR=28.2
900 0 ▶ my ($col) = @_;
Subroutine _format_column: not called CC=8 SCAR=28.2
901 0 my ($name, $type, $extra) = @$col;
902 0 my $sql = _quote_ident($name) . ' ' . $type;
903 0 ▶ return $sql unless $extra && ref $extra eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless $extra and ref $extra eq "HASH" 15 8 23
unless $extra and ref $extra eq "HASH" 0 0 0
Condition33%2/6
$extra and ref $extra eq "HASH"
!ll&&!rl&&r
1508
$extra and ref $extra eq "HASH"
!ll&&!rl&&r
000
MC/DC50%1/2
$extra and ref $extra eq "HASH"
$extra ref $extra eq "HASH"
MC/DC0%0/2
$extra and ref $extra eq "HASH"
$extra ref $extra eq "HASH"
Truth table66%2/3
A and B
A= $extraB= ref $extra eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $extraB= ref $extra eq "HASH"
ABresult
0X0
100
111
904  
905 0 my @kind = grep { defined $extra->{$_} }
906 qw(default materialized alias);
907 0 ▶ die "format_create_table: column '$name' has more than one of "
Branch: 2 branches
BranchTrueFalseTotal
if @kind > 1 1 7 8
if @kind > 1 0 0 0
908 . "default/materialized/alias\n"
909 if @kind > 1;
910 0 ▶ if (@kind) {
Branch: 2 branches
BranchTrueFalseTotal
if (@kind) 3 4 7
if (@kind) 0 0 0
911 0 $sql .= " $kind[0] $extra->{$kind[0]}";
912 }
913 0 ▶ $sql .= " codec($extra->{codec})" if defined $extra->{codec};
Branch: 2 branches
BranchTrueFalseTotal
if defined $extra->{"codec"} 2 5 7
if defined $extra->{"codec"} 0 0 0
914 0 ▶ $sql .= " ttl $extra->{ttl}" if defined $extra->{ttl};
Branch: 2 branches
BranchTrueFalseTotal
if defined $extra->{"ttl"} 1 6 7
if defined $extra->{"ttl"} 0 0 0
915 0 ▶ if (defined $extra->{comment}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $extra->{"comment"}) 3 4 7
if (defined $extra->{"comment"}) 0 0 0
916 # CH string literals take C-style backslash escapes; escape the
917 # backslash before the quote so an embedded backslash survives.
918 0 (my $c = $extra->{comment}) =~ s/\\/\\\\/g;
919 0 $c =~ s/'/\\'/g;
920 0 $sql .= " comment '$c'";
921 }
922 0 return $sql;
923 }
924  
925 # Emit a create table statement for the given column list. Table-level
926 # opts (engine, partition_by, primary_key, order_by, sample_by, ttl,
927 # settings) are emitted in CH's canonical `show create table` order
928 # and inserted verbatim - the caller owns SQL correctness there; this
929 # helper validates only the column list. Per-column
930 # default/materialized/alias/codec/ttl/comment are supported by passing
931 # [name, type, \%col] entries. Returns the SQL string (no trailing
932 # semicolon).
933 14 ▶sub format_create_table {
Subroutine format_create_table: called CC=10 SCAR=32.1
Pod documented
934 0 ▶ my ($class, %opts) = @_;
Subroutine format_create_table: not called CC=10 SCAR=32.1
Pod documented
935 0 ▶ my $table = $opts{table} // die "format_create_table: 'table' required\n";
Condition25%1/4
$opts{"table"} // die("format_create_table: 'table' required\n")
l!l
140
$opts{"table"} // die("format_create_table: 'table' required\n")
l!l
00
MC/DC0%0/1
$opts{"table"} // die("format_create_table: 'table' required\n")
$opts{"table"}
MC/DC0%0/1
$opts{"table"} // die("format_create_table: 'table' required\n")
$opts{"table"}
Truth table50%1/2
A
A= $opts{"table"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"table"}
Aresult
11
00
936 0 ▶ my $columns = $opts{columns} // die "format_create_table: 'columns' arrayref required\n";
Condition25%1/4
$opts{"columns"} // die("format_create_table: 'columns' arrayref required\n")
l!l
140
$opts{"columns"} // die("format_create_table: 'columns' arrayref required\n")
l!l
00
MC/DC0%0/1
$opts{"columns"} // die("format_create_table: 'columns' arrayref required\n")
$opts{"columns"}
MC/DC0%0/1
$opts{"columns"} // die("format_create_table: 'columns' arrayref required\n")
$opts{"columns"}
Truth table50%1/2
A
A= $opts{"columns"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"columns"}
Aresult
11
00
937 0 ▶ my $engine = $opts{engine} // 'MergeTree';
Condition50%2/4
$opts{"engine"} // "MergeTree"
l!l
410
$opts{"engine"} // "MergeTree"
l!l
00
MC/DC100%1/1
$opts{"engine"} // "MergeTree"
$opts{"engine"}
MC/DC0%0/1
$opts{"engine"} // "MergeTree"
$opts{"engine"}
Truth table100%2/2
A
A= $opts{"engine"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"engine"}
Aresult
11
00
938 0 _validate_table_name($table);
939  
940 0 my $body = join ",\n ", map { _format_column($_) } @$columns;
941  
942 0 my $sql = "create table " . join(".", map { _quote_ident($_) } split /\./, $table) . " (\n $body\n)";
943 0 $sql .= "\nengine = $engine";
944 0 ▶ $sql .= "\npartition by $opts{partition_by}" if defined $opts{partition_by};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"partition_by"} 1 11 12
if defined $opts{"partition_by"} 0 0 0
945 0 ▶ $sql .= "\nprimary key $opts{primary_key}" if defined $opts{primary_key};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"primary_key"} 1 11 12
if defined $opts{"primary_key"} 0 0 0
946 0 ▶ $sql .= "\norder by $opts{order_by}" if defined $opts{order_by};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"order_by"} 4 8 12
if defined $opts{"order_by"} 0 0 0
947 0 ▶ $sql .= "\nsample by $opts{sample_by}" if defined $opts{sample_by};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"sample_by"} 1 11 12
if defined $opts{"sample_by"} 0 0 0
948 0 ▶ $sql .= "\nttl $opts{ttl}" if defined $opts{ttl};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"ttl"} 2 10 12
if defined $opts{"ttl"} 0 0 0
949 0 ▶ $sql .= "\nsettings $opts{settings}" if defined $opts{settings};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"settings"} 1 11 12
if defined $opts{"settings"} 0 0 0
950 0 return $sql;
951 }
952  
953 # Translate a schema_diff hashref into a list of alter table statements
954 # (one per column change). Returns an arrayref of SQL strings; the
955 # caller decides whether to apply them transactionally or one at a
956 # time. Conservative ordering: drops first, then modifies, then adds,
957 # so a column-rename modeled as drop+add ends up with the new column
958 # at the right position.
959 8 ▶sub apply_schema_diff {
Subroutine apply_schema_diff: called CC=8 SCAR=29.7
Pod documented
960 0 ▶ my ($class, $diff, %opts) = @_;
Subroutine apply_schema_diff: not called CC=8 SCAR=29.7
Pod documented
961 0 ▶ my $table = $opts{table} // die "apply_schema_diff: 'table' required\n";
Condition50%2/4
$opts{"table"} // die("apply_schema_diff: 'table' required\n")
l!l
71
$opts{"table"} // die("apply_schema_diff: 'table' required\n")
l!l
00
MC/DC100%1/1
$opts{"table"} // die("apply_schema_diff: 'table' required\n")
$opts{"table"}
MC/DC0%0/1
$opts{"table"} // die("apply_schema_diff: 'table' required\n")
$opts{"table"}
Truth table100%2/2
A
A= $opts{"table"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"table"}
Aresult
11
00
962 0 _validate_table_name($table);
963 0 my $qt = _quote_ident($table);
964 0 my @sql;
965 0 ▶ for my $row (@{ $diff->{removed} // [] }) {
Condition25%1/4
$diff->{"removed"} // []
l!l
60
$diff->{"removed"} // []
l!l
00
MC/DC0%0/1
$diff->{"removed"} // []
$diff->{"removed"}
MC/DC0%0/1
$diff->{"removed"} // []
$diff->{"removed"}
Truth table50%1/2
A
A= $diff->{"removed"}
Aresult
11
00
Truth table0%0/2
A
A= $diff->{"removed"}
Aresult
11
00
966 0 push @sql, "alter table $qt drop column "
967 . _quote_ident($row->[0]);
968 }
969 0 ▶ for my $row (@{ $diff->{changed} // [] }) {
Condition25%1/4
$diff->{"changed"} // []
l!l
60
$diff->{"changed"} // []
l!l
00
MC/DC0%0/1
$diff->{"changed"} // []
$diff->{"changed"}
MC/DC0%0/1
$diff->{"changed"} // []
$diff->{"changed"}
Truth table50%1/2
A
A= $diff->{"changed"}
Aresult
11
00
Truth table0%0/2
A
A= $diff->{"changed"}
Aresult
11
00
970 0 push @sql, "alter table $qt modify column "
971 . _quote_ident($row->[0]) . " $row->[2]";
972 }
973 0 ▶ for my $row (@{ $diff->{added} // [] }) {
Condition25%1/4
$diff->{"added"} // []
l!l
60
$diff->{"added"} // []
l!l
00
MC/DC0%0/1
$diff->{"added"} // []
$diff->{"added"}
MC/DC0%0/1
$diff->{"added"} // []
$diff->{"added"}
Truth table50%1/2
A
A= $diff->{"added"}
Aresult
11
00
Truth table0%0/2
A
A= $diff->{"added"}
Aresult
11
00
974 0 push @sql, "alter table $qt add column "
975 . _quote_ident($row->[0]) . " $row->[1]";
976 }
977 0 return \@sql;
978 }
979  
980 # Unquote a CH identifier: strip surrounding backticks and collapse both
981 # escape conventions CH's lexer accepts - C-style backslash escapes
982 # (\X -> X, what backQuote() and _quote_ident emit) and doubled
983 # backticks (`` -> `). Bare identifiers pass through untouched.
984 60 ▶sub _unquote_ident {
Subroutine _unquote_ident: called CC=3 SCAR=14.2
985 0 ▶ my $s = shift;
Subroutine _unquote_ident: not called CC=3 SCAR=14.2
986 0 ▶ if ($s =~ /\A`(.*)`\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s =~ /\A`(.*)`\z/s) 36 24 60
if ($s =~ /\A`(.*)`\z/s) 0 0 0
987 0 $s = $1;
988 0 ▶ $s =~ s/\\(.)|``/defined $1 ? $1 : '`'/ges;
Branch: 2 branches
BranchTrueFalseTotal
defined $1 ? : 1 1 2
defined $1 ? : 0 0 0
989 }
990 0 return $s;
991 }
992  
993 # Scan a quoted SQL value, honoring doubled quotes and backslash escapes.
994 106 ▶sub _skip_sql_quoted {
Subroutine _skip_sql_quoted: called CC=6 SCAR=24.7
995 0 ▶ my ($str, $i) = @_;
Subroutine _skip_sql_quoted: not called CC=6 SCAR=24.7
996 0 my $quote = substr($str, $i++, 1);
997 0 my $len = length $str;
998 0 ▶ while ($i < $len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $len) 442 0 442
if ($i < $len) 0 0 0
999 0 my $c = substr($str, $i++, 1);
1000 0 ▶ if ($c eq '\\') { $i++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "\\") 11 431 442
if ($c eq "\\") 0 0 0
1001 0 ▶ if ($c eq $quote) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq $quote) 114 317 431
if ($c eq $quote) 0 0 0
1002 0 ▶ return $i if $i >= $len || substr($str, $i, 1) ne $quote;
Branch: 2 branches
BranchTrueFalseTotal
if $i >= $len or substr($str, $i, 1) ne $quote 106 8 114
if $i >= $len or substr($str, $i, 1) ne $quote 0 0 0
Condition50%3/6
$i >= $len or substr($str, $i, 1) ne $quote
l!l&&r!l&&!r
41028
$i >= $len or substr($str, $i, 1) ne $quote
l!l&&r!l&&!r
000
MC/DC100%2/2
$i >= $len or substr($str, $i, 1) ne $quote
$i >= $len substr($str, $i, 1) ne $quote
MC/DC0%0/2
$i >= $len or substr($str, $i, 1) ne $quote
$i >= $len substr($str, $i, 1) ne $quote
Truth table100%3/3
A or B
A= $i >= $lenB= substr($str, $i, 1) ne $quote
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $i >= $lenB= substr($str, $i, 1) ne $quote
ABresult
1X1
011
000
1003 0 $i++;
1004 }
1005 }
1006 0 die "unterminated quoted SQL value\n";
1007 }
1008  
1009 # Split a (possibly backtick-quoted, possibly database-qualified) table
1010 # name into ($database, $table). The qualifying dot is the first one
1011 # that is not inside backticks; a name with no such dot has an undef
1012 # database.
1013 23 ▶sub _split_qname {
Subroutine _split_qname: called CC=6 SCAR=24.1
1014 0 ▶ my ($qname) = @_;
Subroutine _split_qname: not called CC=6 SCAR=24.1
1015 0 my $len = length $qname;
1016 0 my $dot = -1;
1017 0 ▶ for (my $i = 0; $i < $len; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $len) 54 18 72
if ($i < $len) 0 0 0
1018 0 my $c = substr($qname, $i, 1);
1019 0 ▶ if ($c eq '`' || $c eq "'") { $i = _skip_sql_quoted($qname, $i) - 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "`" or $c eq "'") 3 51 54
if ($c eq "`" or $c eq "'") 0 0 0
Condition33%2/6
$c eq "`" or $c eq "'"
l!l&&r!l&&!r
3051
$c eq "`" or $c eq "'"
l!l&&r!l&&!r
000
MC/DC50%1/2
$c eq "`" or $c eq "'"
$c eq "`" $c eq "'"
MC/DC0%0/2
$c eq "`" or $c eq "'"
$c eq "`" $c eq "'"
Truth table66%2/3
A or B
A= $c eq "`"B= $c eq "'"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $c eq "`"B= $c eq "'"
ABresult
1X1
011
000
1020 0 ▶ if ($c eq '.') { $dot = $i; last }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq ".") 5 46 51
if ($c eq ".") 0 0 0
1021 }
1022 0 ▶ return (undef, _unquote_ident($qname)) if $dot < 0;
Branch: 2 branches
BranchTrueFalseTotal
if $dot < 0 18 5 23
if $dot < 0 0 0 0
1023 0 return (_unquote_ident(substr($qname, 0, $dot)),
1024 _unquote_ident(substr($qname, $dot + 1)));
1025 }
1026  
1027 # Split column definitions using the shared quote-aware list scanner.
1028 21 ▶sub _split_column_defs {
Subroutine _split_column_defs: called CC=1 SCAR=1.2
1029 0 ▶ my ($body) = @_;
Subroutine _split_column_defs: not called CC=1 SCAR=1.2
1030 0 return _split_paren_list($body);
1031 }
1032  
1033 # Undo the TabSeparated escaping layer around show create output.
1034 1 ▶sub _tsv_unescape_ddl {
Subroutine _tsv_unescape_ddl: called CC=5 SCAR=28.6
1035 0 ▶ my $s = shift;
Subroutine _tsv_unescape_ddl: not called CC=5 SCAR=28.6
1036 0 ▶ $s =~ s{\\(.)}{ $1 eq 'n' ? "\n" : $1 eq 't' ? "\t"
Branch: 8 branches
BranchTrueFalseTotal
$1 eq 'n' ? : 7 0 7
$1 eq 't' ? : 0 0 0
$1 eq 'r' ? : 0 0 0
$1 eq '0' ? : 0 0 0
$1 eq 'n' ? : 0 0 0
$1 eq 't' ? : 0 0 0
$1 eq 'r' ? : 0 0 0
$1 eq '0' ? : 0 0 0
1037 : $1 eq 'r' ? "\r" : $1 eq '0' ? "\0" : $1 }ge;
1038 0 return $s;
1039 }
1040  
1041 25 ▶sub parse_create_table {
Subroutine parse_create_table: called CC=32 SCAR=52.2
Pod documented
1042 0 ▶ my ($class, $ddl) = @_;
Subroutine parse_create_table: not called CC=32 SCAR=52.2
Pod documented
1043 0 ▶ die "parse_create_table: input required\n" unless defined $ddl;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $ddl 1 24 25
unless defined $ddl 0 0 0
1044  
1045 # `show create table` over HTTP comes back TabSeparated, which escapes
1046 # the DDL's newlines into the two characters \ and n; left alone, the
1047 # column block is one unsplittable run. Key off the escaped column-block
1048 # opener the server always emits ("...name\n(\n") rather than on a bare
1049 # backslash. The trailing real newline is TSV's row terminator, hence
1050 # the trim. (_for_describe does the same for `describe table`.)
1051 0 (my $probe = $ddl) =~ s/\s+\z//;
1052 0 ▶ if ($probe !~ /\n/ && $probe =~ /\\n\s*\(/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $probe =~ /\n/ and $probe =~ /\\n\s*\(/) 1 23 24
if (not $probe =~ /\n/ and $probe =~ /\\n\s*\(/) 0 0 0
Condition50%3/6
not $probe =~ /\n/ and $probe =~ /\\n\s*\(/
!ll&&!rl&&r
5181
not $probe =~ /\n/ and $probe =~ /\\n\s*\(/
!ll&&!rl&&r
000
MC/DC100%2/2
not $probe =~ /\n/ and $probe =~ /\\n\s*\(/
not $probe =~ /\n/ $probe =~ /\\n\s*\(/
MC/DC0%0/2
not $probe =~ /\n/ and $probe =~ /\\n\s*\(/
not $probe =~ /\n/ $probe =~ /\\n\s*\(/
Truth table100%3/3
not(A) and B
A= not $probe =~ /\n/B= $probe =~ /\\n\s*\(/
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $probe =~ /\n/B= $probe =~ /\\n\s*\(/
ABresult
0X0
100
111
1053 0 $ddl = _tsv_unescape_ddl($ddl);
1054 }
1055  
1056 # A name part is a backtick-quoted identifier or a bare run with no
1057 # space / dot / paren; the table name is one part, optionally
1058 # database-qualified with a second. Inside backticks both escape
1059 # forms are accepted: \X (backslash) and `` (doubled).
1060 0 my $part = qr/(?:`(?:[^`\\]|\\.|``)+`|[^\s.(]+)/;
1061 0 ▶ $ddl =~ /\bcreate\s+(?:or\s+replace\s+)?(?:temporary\s+)?table\s+
Branch: 2 branches
BranchTrueFalseTotal
$ddl =~ /\bcreate\s+(?:or\s+replace\s+)?(?:temporary\s+)?table\s+ (?:if\s+not\s+exists\s+)? ($part (?:\.$part)?)/cgix or die "parse_create_table: no create table header found\n" 23 1 24
$ddl =~ /\bcreate\s+(?:or\s+replace\s+)?(?:temporary\s+)?table\s+ (?:if\s+not\s+exists\s+)? ($part (?:\.$part)?)/cgix or die "parse_create_table: no create table header found\n" 0 0 0
1062 (?:if\s+not\s+exists\s+)?
1063 ($part (?:\.$part)?)/xgci
1064 or die "parse_create_table: no create table header found\n";
1065 0 my $qname = $1;
1066 0 my $name_end = pos $ddl;
1067  
1068 0 my ($database, $table) = _split_qname($qname);
1069  
1070 # Locate the column block: the first balanced (...) after the name.
1071 0 my $open = index $ddl, '(', $name_end;
1072 0 ▶ die "parse_create_table: no column list found\n" if $open < 0;
Branch: 2 branches
BranchTrueFalseTotal
if $open < 0 1 22 23
if $open < 0 0 0 0
1073 0 my ($depth, $close, $len) = (0, -1, length $ddl);
1074 0 ▶ for (my $i = $open; $i < $len; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $len) 732 1 733
if ($i < $len) 0 0 0
1075 0 my $c = substr($ddl, $i, 1);
1076 0 ▶ if ($c eq '`' || $c eq "'") { $i = _skip_sql_quoted($ddl, $i) - 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "`" or $c eq "'") 45 687 732
if ($c eq "`" or $c eq "'") 0 0 0
Condition50%3/6
$c eq "`" or $c eq "'"
l!l&&r!l&&!r
3312687
$c eq "`" or $c eq "'"
l!l&&r!l&&!r
000
MC/DC100%2/2
$c eq "`" or $c eq "'"
$c eq "`" $c eq "'"
MC/DC0%0/2
$c eq "`" or $c eq "'"
$c eq "`" $c eq "'"
Truth table100%3/3
A or B
A= $c eq "`"B= $c eq "'"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $c eq "`"B= $c eq "'"
ABresult
1X1
011
000
1077 0 ▶ if ($c eq '(') { $depth++ }
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq '(') { } 39 648 687
elsif ($c eq ')') { } 38 610 648
if ($c eq '(') { } 0 0 0
elsif ($c eq ')') { } 0 0 0
1078 0 ▶ elsif ($c eq ')') { $depth--; if ($depth == 0) { $close = $i; last } }
Branch: 2 branches
BranchTrueFalseTotal
if ($depth == 0) 21 17 38
if ($depth == 0) 0 0 0
1079 }
1080 0 ▶ die "parse_create_table: unbalanced column list\n" if $close < 0;
Branch: 2 branches
BranchTrueFalseTotal
if $close < 0 1 21 22
if $close < 0 0 0 0
1081 0 my $block = substr $ddl, $open + 1, $close - $open - 1;
1082  
1083 0 my @columns;
1084 0 for my $def (_split_column_defs($block)) {
1085 # Skip table-level index / constraint / projection / primary key
1086 # entries that share the column block.
1087 0 ▶ next if $def =~ /\A(?:index|constraint|projection|primary\s+key)\b/i;
Branch: 2 branches
BranchTrueFalseTotal
if $def =~ /\A(?:index|constraint|projection|primary\s+key)\b/i 0 44 44
if $def =~ /\A(?:index|constraint|projection|primary\s+key)\b/i 0 0 0
1088 0 my ($cname, $rest);
1089 0 ▶ if ($def =~ /\A(`(?:[^`\\]|\\.|``)+`)\s+(.*)\z/s) {
Branch: 4 branches
BranchTrueFalseTotal
if ($def =~ /\A(`(?:[^`\\]|\\.|``)+`)\s+(.*)\z/s) { } 32 12 44
elsif ($def =~ /\A(\S+)\s+(.*)\z/s) { } 12 0 12
if ($def =~ /\A(`(?:[^`\\]|\\.|``)+`)\s+(.*)\z/s) { } 0 0 0
elsif ($def =~ /\A(\S+)\s+(.*)\z/s) { } 0 0 0
1090 0 ($cname, $rest) = (_unquote_ident($1), $2);
1091 } elsif ($def =~ /\A(\S+)\s+(.*)\z/s) {
1092 0 ($cname, $rest) = ($1, $2);
1093 } else {
1094 0 next;
1095 }
1096 # The type is the leading identifier plus its balanced (...) args;
1097 # default / codec / ttl / comment modifiers come after and are
1098 # not part of the type.
1099 0 my $type = _take_type(\$rest);
1100 0 ▶ push @columns, [$cname, $type] if length $type;
Branch: 2 branches
BranchTrueFalseTotal
if length $type 44 0 44
if length $type 0 0 0
1101 }
1102 0 ▶ die "parse_create_table: no columns parsed\n" unless @columns;
Branch: 2 branches
BranchTrueFalseTotal
unless @columns 0 21 21
unless @columns 0 0 0
1103  
1104 0 my $tail = substr $ddl, $close + 1;
1105 0 my %out = (
1106 database => $database,
1107 table => $table,
1108 columns => \@columns,
1109 );
1110 0 my %keys = ('engine' => 'engine', 'partition by' => 'partition_by',
1111 'primary key' => 'primary_key', 'order by' => 'order_by',
1112 'sample by' => 'sample_by', 'ttl' => 'ttl',
1113 'settings' => 'settings', 'comment' => 'comment');
1114 0 my @clauses;
1115 0 my $level = 0;
1116 0 ▶ for (my $i = 0; $i < length $tail; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < length $tail) 364 21 385
if ($i < length $tail) 0 0 0
1117 0 my $c = substr($tail, $i, 1);
1118 0 ▶ if ($c eq "'" || $c eq '`' || $c eq '"') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "'" or $c eq "`" or $c eq "\"") 2 362 364
if ($c eq "'" or $c eq "`" or $c eq "\"") 0 0 0
Condition33%4/12
$c eq "'" or $c eq "`" or $c eq "\""
l!l&&r!l&&!r
20362
$c eq "'" or $c eq "`"
l!l&&r!l&&!r
20362
$c eq "'" or $c eq "`" or $c eq "\""
l!l&&r!l&&!r
000
$c eq "'" or $c eq "`"
l!l&&r!l&&!r
000
MC/DC33%1/3
$c eq "'" or $c eq "`" or $c eq "\""
$c eq "'" $c eq "`" $c eq "\""
MC/DC0%0/3
$c eq "'" or $c eq "`" or $c eq "\""
$c eq "'" $c eq "`" $c eq "\""
Truth table50%2/4
A or B or C
A= $c eq "'"B= $c eq "`"C= $c eq "\""
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B or C
A= $c eq "'"B= $c eq "`"C= $c eq "\""
ABCresult
1XX1
01X1
0011
0000
1119 0 $i = _skip_sql_quoted($tail, $i) - 1; next;
1120 }
1121 0 ▶ if ($c eq '(') { $level++; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "(") 5 357 362
if ($c eq "(") 0 0 0
1122 0 ▶ if ($c eq ')') { $level--; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq ")") 5 352 357
if ($c eq ")") 0 0 0
1123 0 ▶ next if $level || ($i && substr($tail, $i - 1, 1) =~ /\w/);
Branch: 2 branches
BranchTrueFalseTotal
if $level or $i and substr($tail, $i - 1, 1) =~ /\w/ 238 114 352
if $level or $i and substr($tail, $i - 1, 1) =~ /\w/ 0 0 0
Condition50%6/12
$level or $i and substr($tail, $i - 1, 1) =~ /\w/
l!l&&r!l&&!r
20218114
$i and substr($tail, $i - 1, 1) =~ /\w/
!ll&&!rl&&r
2193218
$level or $i and substr($tail, $i - 1, 1) =~ /\w/
l!l&&r!l&&!r
000
$i and substr($tail, $i - 1, 1) =~ /\w/
!ll&&!rl&&r
000
MC/DC100%3/3
$level or $i and substr($tail, $i - 1, 1) =~ /\w/
$level $i substr($tail, $i - 1, 1) =~ /\w/
MC/DC0%0/3
$level or $i and substr($tail, $i - 1, 1) =~ /\w/
$level $i substr($tail, $i - 1, 1) =~ /\w/
Truth table100%4/4
A or B and C
A= $levelB= $iC= substr($tail, $i - 1, 1) =~ /\w/
ABCresult
1XX1
0111
00X0
0100
Truth table0%0/4
A or B and C
A= $levelB= $iC= substr($tail, $i - 1, 1) =~ /\w/
ABCresult
1XX1
0111
00X0
0100
1124 0 pos($tail) = $i;
1125 0 ▶ if ($tail =~ /\G(engine\s*=|partition\s+by\b|primary\s+key\b|order\s+by\b|sample\s+by\b|ttl\b|settings\b|comment\b)\s*/gci) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tail =~ /\G(engine\s*=|partition\s+by\b|primary\s+key\b|order\s+by\b|sample\s+by\b|ttl\b|settings\b|comment\b)\s*/cig) 37 77 114
if ($tail =~ /\G(engine\s*=|partition\s+by\b|primary\s+key\b|order\s+by\b|sample\s+by\b|ttl\b|settings\b|comment\b)\s*/cig) 0 0 0
1126 0 my $key = lc $1; $key =~ s/\s*=\z//; $key =~ s/\s+/ /g;
1127 0 push @clauses, [$i, pos($tail), $keys{$key}];
1128 0 $i = pos($tail) - 1;
1129 }
1130 }
1131 0 for my $i (0 .. $#clauses) {
1132 0 ▶ my $end = $i < $#clauses ? $clauses[$i + 1][0] : length $tail;
Branch: 2 branches
BranchTrueFalseTotal
$i < $#clauses ? : 16 21 37
$i < $#clauses ? : 0 0 0
1133 0 my $value = substr($tail, $clauses[$i][1], $end - $clauses[$i][1]);
1134 0 $value =~ s/\A\s+//; $value =~ s/[\s;]+\z//;
1135 0 ▶ $out{$clauses[$i][2]} = $value if length $value;
Branch: 2 branches
BranchTrueFalseTotal
if length $value 37 0 37
if length $value 0 0 0
1136 }
1137 0 return \%out;
1138 }
1139  
1140 # Consume one CH type expression from the front of $$rest (an identifier
1141 # optionally followed by a balanced parenthesised argument list), advance
1142 # $$rest past it, and return the type string. Used by parse_create_table
1143 # to separate the type from trailing default/codec/... modifiers.
1144 44 ▶sub _take_type {
Subroutine _take_type: called CC=9 SCAR=30.8
1145 0 ▶ my ($rest) = @_;
Subroutine _take_type: not called CC=9 SCAR=30.8
1146 0 $$rest =~ s/\A\s+//;
1147 0 ▶ return '' unless $$rest =~ /\A([A-Za-z_]\w*)/;
Branch: 2 branches
BranchTrueFalseTotal
unless $$rest =~ /\A([A-Za-z_]\w*)/ 0 44 44
unless $$rest =~ /\A([A-Za-z_]\w*)/ 0 0 0
1148 0 my $type = $1;
1149 0 my $pos = length $type;
1150 0 ▶ if (substr($$rest, $pos, 1) eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($$rest, $pos, 1) eq "(") 14 30 44
if (substr($$rest, $pos, 1) eq "(") 0 0 0
1151 0 my ($depth, $len) = (0, length $$rest);
1152 0 ▶ for (my $i = $pos; $i < $len; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $len) 168 0 168
if ($i < $len) 0 0 0
1153 0 my $c = substr($$rest, $i, 1);
1154 0 ▶ if ($c eq '`' || $c eq "'") { $i = _skip_sql_quoted($$rest, $i) - 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "`" or $c eq "'") 10 158 168
if ($c eq "`" or $c eq "'") 0 0 0
Condition33%2/6
$c eq "`" or $c eq "'"
l!l&&r!l&&!r
010158
$c eq "`" or $c eq "'"
l!l&&r!l&&!r
000
MC/DC50%1/2
$c eq "`" or $c eq "'"
$c eq "`" $c eq "'"
MC/DC0%0/2
$c eq "`" or $c eq "'"
$c eq "`" $c eq "'"
Truth table66%2/3
A or B
A= $c eq "`"B= $c eq "'"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $c eq "`"B= $c eq "'"
ABresult
1X1
011
000
1155 0 ▶ if ($c eq '(') { $depth++ }
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq '(') { } 16 142 158
elsif ($c eq ')') { } 16 126 142
if ($c eq '(') { } 0 0 0
elsif ($c eq ')') { } 0 0 0
1156 0 ▶ elsif ($c eq ')') { $depth--; if (!$depth) { $pos = $i + 1; last } }
Branch: 2 branches
BranchTrueFalseTotal
unless ($depth) 14 2 16
unless ($depth) 0 0 0
1157 }
1158 }
1159 0 $type = substr $$rest, 0, $pos;
1160 0 substr($$rest, 0, $pos) = '';
1161 0 return $type;
1162 }
1163  
1164 # Inspect a captured Native block and return its column shape as a
1165 # fresh encoder configured for that shape. Useful for diagnosing
1166 # captured payloads off-line (no server, no schema source) and for
1167 # round-tripping bytes through a transform/filter step. Zero-row
1168 # blocks work fine - the column headers are still on the wire.
1169 2 ▶sub for_native_bytes {
Subroutine for_native_bytes: called CC=2 SCAR=9.2
Pod documented
1170 0 ▶ my ($class, $bytes) = @_;
Subroutine for_native_bytes: not called CC=2 SCAR=9.2
Pod documented
1171 0 my $blk = $class->decode_block($bytes);
1172 0 my @cols;
1173 0 for my $col (@{ $blk->{columns} }) {
1174 0 push @cols, [ $col->{name}, $col->{type} ];
1175 }
1176 0 return $class->new(columns => \@cols);
1177 }
1178  
1179 # --- RowBinary -------------------------------------------------------
1180 #
1181 # RowBinary is ClickHouse's row-major binary format: each row is the
1182 # concatenation of its column values, with no headers. The per-value
1183 # byte encoding for scalar / String / FixedString / Nullable columns is
1184 # byte-identical to that column's data in a one-row Native block, so
1185 # encode_row_binary reuses the XS Native encoder and slices the value
1186 # region out; decode_row_binary wraps a value back into a one-row Native
1187 # block and runs the XS decoder. Array(T) is the one shape that differs
1188 # (RowBinary uses a varint element count where Native uses a UInt64
1189 # offset) and is handled by recursion. This keeps a single source of
1190 # truth for every type's wire bytes - no second per-type codec in Perl.
1191 #
1192 # Supported: all scalar types (Int*/UInt*/Float*/Bool/Date*/DateTime*/
1193 # Decimal*/UUID/IPv4/IPv6/Enum*), String, FixedString(N), Nullable of
1194 # any of those, Array(...) nesting, and LowCardinality(...) (encoded as
1195 # its inner type, which is how RowBinary represents it). Map, Tuple,
1196 # Variant, JSON, Dynamic, Geo and Nested croak - their Native and
1197 # RowBinary framings diverge in ways the slice trick cannot bridge.
1198  
1199 # Fixed on-wire byte width per scalar base type (parametric suffix
1200 # already stripped by the caller). Decimal/FixedString are sized
1201 # separately because their width depends on the type parameters.
1202 87 my %RB_FIXED_WIDTH = (
1203 Int8 => 1, UInt8 => 1, Bool => 1, Boolean => 1, Enum8 => 1,
1204 Int16 => 2, UInt16 => 2, Date => 2, Enum16 => 2,
1205 Int32 => 4, UInt32 => 4, Float32 => 4, BFloat16 => 2,
1206 Date32 => 4, DateTime => 4, IPv4 => 4, Decimal32 => 4,
1207 Int64 => 8, UInt64 => 8, Float64 => 8, DateTime64 => 8,
1208 Decimal64 => 8,
1209 Int128 => 16, UInt128 => 16, UUID => 16, IPv6 => 16, Decimal128 => 16,
1210 Int256 => 32, UInt256 => 32, Decimal256 => 32,
1211 );
1212  
1213 # Croak unless $type is a RowBinary-supported scalar (or Nullable of
1214 # one). Array / LowCardinality are peeled by the caller before this
1215 # runs, so anything parenthesised-and-recursive that reaches here is
1216 # unsupported.
1217 129 ▶sub _rb_assert_scalar {
Subroutine _rb_assert_scalar: called CC=3 SCAR=14.2
1218 0 ▶ my ($type) = @_;
Subroutine _rb_assert_scalar: not called CC=3 SCAR=14.2
1219 # ClickHouse does not nest Nullable, so a single peel is enough.
1220 0 my $t = $type;
1221 0 ▶ $t = $1 if $t =~ /\ANullable\((.+)\)\z/s;
Branch: 2 branches
BranchTrueFalseTotal
if $t =~ /\ANullable\((.+)\)\z/s 29 100 129
if $t =~ /\ANullable\((.+)\)\z/s 0 0 0
1222 0 ▶ die "row_binary: type '$type' is not supported (RowBinary covers "
Branch: 2 branches
BranchTrueFalseTotal
if $t =~ /\A(?:Map|Tuple|Variant|JSON|Object|Dynamic|Point|Ring |LineString|MultiLineString|Polygon|MultiPolygon |Nested|SimpleAggregateFunction|AggregateFunction |Array|LowCardinality)\b/x 4 125 129
if $t =~ /\A(?:Map|Tuple|Variant|JSON|Object|Dynamic|Point|Ring |LineString|MultiLineString|Polygon|MultiPolygon |Nested|SimpleAggregateFunction|AggregateFunction |Array|LowCardinality)\b/x 0 0 0
1223 . "scalar/String/FixedString columns, optionally Nullable, "
1224 . "Array, or LowCardinality)\n"
1225 if $t =~ /\A(?:Map|Tuple|Variant|JSON|Object|Dynamic|Point|Ring
1226 |LineString|MultiLineString|Polygon|MultiPolygon
1227 |Nested|SimpleAggregateFunction|AggregateFunction
1228 |Array|LowCardinality)\b/x;
1229 0 return;
1230 }
1231  
1232 # The varint / length-string codecs are XS, registered into the
1233 # ClickHouse::Encoder::TCP package (the protocol packers use them);
1234 # the single shared object installs them regardless of package, so
1235 # they are callable here once the main module's XS has booted. Alias
1236 # them in as glob aliases (not wrapper subs) so a call goes straight
1237 # to the XSUB with no extra frame and exact context propagation.
1238 ## no critic (ProhibitCallsToUnexportedSubs)
1239 87 *_rb_pack_varint = \&ClickHouse::Encoder::TCP::pack_varint;
1240 87 *_rb_unpack_varint = \&ClickHouse::Encoder::TCP::unpack_varint;
1241 87 *_rb_pack_string = \&ClickHouse::Encoder::TCP::pack_string;
1242 ## use critic
1243  
1244 # Encode one value of $type into RowBinary bytes. $cache holds the
1245 # parent encoder's options and memoises the single-column Native encoder
1246 # + value-region offset per scalar type.
1247 93 ▶sub _rb_encode_value {
Subroutine _rb_encode_value: called CC=8 SCAR=28.7
1248 0 ▶ my ($type, $val, $cache) = @_;
Subroutine _rb_encode_value: not called CC=8 SCAR=28.7
1249 0 ▶ if ($type =~ /\AArray\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\AArray\((.+)\)\z/s) 12 81 93
if ($type =~ /\AArray\((.+)\)\z/s) 0 0 0
1250 0 my $inner = $1;
1251 0 ▶ die "encode_row_binary: Array column needs an arrayref value\n"
Branch: 2 branches
BranchTrueFalseTotal
unless ref $val eq "ARRAY" 0 12 12
unless ref $val eq "ARRAY" 0 0 0
1252 unless ref $val eq 'ARRAY';
1253 0 my $s = _rb_pack_varint(scalar @$val);
1254 0 $s .= _rb_encode_value($inner, $_, $cache) for @$val;
1255 0 return $s;
1256 }
1257 0 ▶ if ($type =~ /\ALowCardinality\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\ALowCardinality\((.+)\)\z/s) 2 79 81
if ($type =~ /\ALowCardinality\((.+)\)\z/s) 0 0 0
1258 0 return _rb_encode_value($1, $val, $cache);
1259 }
1260 0 _rb_assert_scalar($type);
1261 0 ▶ my $slot = $cache->{types}{$type} ||= do {
Condition33%2/6
$cache->{"types"}{$type} ||= do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
l!l&&r!l&&!r
39360
$cache->{"types"}{$type} ||= do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
l!l&&r!l&&!r
000
MC/DC0%0/2
$cache->{"types"}{$type} ||= do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
$cache->{"types"}{$type} do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
MC/DC0%0/2
$cache->{"types"}{$type} ||= do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
$cache->{"types"}{$type} do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
Truth table66%2/3
A ||= B
A= $cache->{"types"}{$type}B= do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $cache->{"types"}{$type}B= do { my $enc = "ClickHouse::Encoder"->new("columns", [["c", $type]], @{$$cache{"options"};}); my $prefix = 2 + length(_rb_pack_string("c")) + length(_rb_pack_string($type)); [$enc, $prefix] }
ABresult
1X1
011
000
1262 my $enc = ClickHouse::Encoder->new(columns => [['c', $type]],
1263 0 @{ $cache->{options} });
1264 # One-row, one-column Native block prefix:
1265 # varint(ncols=1) varint(nrows=1) lenstr("c") lenstr(type)
1266 0 my $prefix = 2 + length(_rb_pack_string('c'))
1267 + length(_rb_pack_string($type));
1268 0 [$enc, $prefix];
1269 };
1270 0 my $bytes = substr($slot->[0]->encode([[ $val ]]), $slot->[1]);
1271 # RowBinary omits the value when the null flag is set; Native keeps a placeholder.
1272 0 ▶ return $type =~ /\ANullable\(/ && $bytes =~ /\A\x01/ ? "\x01" : $bytes;
Branch: 2 branches
BranchTrueFalseTotal
$type =~ /\ANullable\(/ && $bytes =~ /\A\x01/ ? : 13 62 75
$type =~ /\ANullable\(/ && $bytes =~ /\A\x01/ ? : 0 0 0
Condition50%3/6
$type =~ /\ANullable\(/ and $bytes =~ /\A\x01/
!ll&&!rl&&r
461613
$type =~ /\ANullable\(/ and $bytes =~ /\A\x01/
!ll&&!rl&&r
000
MC/DC100%2/2
$type =~ /\ANullable\(/ and $bytes =~ /\A\x01/
$type =~ /\ANullable\(/ $bytes =~ /\A\x01/
MC/DC0%0/2
$type =~ /\ANullable\(/ and $bytes =~ /\A\x01/
$type =~ /\ANullable\(/ $bytes =~ /\A\x01/
Truth table100%3/3
A and B
A= $type =~ /\ANullable\(/B= $bytes =~ /\A\x01/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $type =~ /\ANullable\(/B= $bytes =~ /\A\x01/
ABresult
0X0
100
111
1273 }
1274  
1275 # Encode an arrayref of rows into a RowBinary byte string. Call on an
1276 # encoder instance (its column types drive serialisation). The result
1277 # is the request body for `insert ... format RowBinary`.
1278 27 ▶sub encode_row_binary {
Subroutine encode_row_binary: called CC=6 SCAR=24.2
Pod documented
1279 0 ▶ my ($self, $rows) = @_;
Subroutine encode_row_binary: not called CC=6 SCAR=24.2
Pod documented
1280 0 ▶ die "encode_row_binary: rows must be an arrayref\n"
Branch: 2 branches
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 1 26 27
unless ref $rows eq "ARRAY" 0 0 0
1281 unless ref $rows eq 'ARRAY';
1282 0 my $cols = $self->columns;
1283 0 my %cache = (options => [$self->_options]);
1284 0 my $out = '';
1285 0 for my $ri (0 .. $#$rows) {
1286 0 my $row = $rows->[$ri];
1287 0 ▶ die "encode_row_binary: row $ri must be an arrayref\n"
Branch: 2 branches
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 0 34 34
unless ref $row eq "ARRAY" 0 0 0
1288 unless ref $row eq 'ARRAY';
1289 0 ▶ die "encode_row_binary: row $ri has " . scalar(@$row)
Branch: 2 branches
BranchTrueFalseTotal
unless @$row == @$cols 1 33 34
unless @$row == @$cols 0 0 0
1290 . " values, expected " . scalar(@$cols) . "\n"
1291 unless @$row == @$cols;
1292 0 for my $ci (0 .. $#$cols) {
1293 0 $out .= _rb_encode_value($cols->[$ci][1], $row->[$ci], \%cache);
1294 }
1295 }
1296 0 return $out;
1297 }
1298  
1299 # Byte length of one scalar/String/FixedString/Nullable value at
1300 # $$bufref position $pos (does not advance). Array is handled by the
1301 # caller's recursion, never reaching here.
1302 50 ▶sub _rb_value_len {
Subroutine _rb_value_len: called CC=14 SCAR=44.8
1303 0 ▶ my ($type, $bufref, $pos) = @_;
Subroutine _rb_value_len: not called CC=14 SCAR=44.8
1304 0 ▶ if ($type =~ /\ANullable\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\ANullable\((.+)\)\z/s) 0 50 50
if ($type =~ /\ANullable\((.+)\)\z/s) 0 0 0
1305 # A set null flag is the whole value; nothing follows it.
1306 0 ▶ return 1 if ord substr($$bufref, $pos, 1);
Branch: 2 branches
BranchTrueFalseTotal
if ord substr($$bufref, $pos, 1) 0 0 0
if ord substr($$bufref, $pos, 1) 0 0 0
1307 0 return 1 + _rb_value_len($1, $bufref, $pos + 1);
1308 }
1309 0 ▶ if ($type =~ /\ALowCardinality\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\ALowCardinality\((.+)\)\z/s) 0 50 50
if ($type =~ /\ALowCardinality\((.+)\)\z/s) 0 0 0
1310 0 return _rb_value_len($1, $bufref, $pos);
1311 }
1312 0 ▶ if ($type eq 'String') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq "String") 14 36 50
if ($type eq "String") 0 0 0
1313 0 my ($len, $after) = _rb_unpack_varint($$bufref, $pos);
1314 0 return ($after - $pos) + $len;
1315 }
1316 0 ▶ if ($type =~ /\AFixedString\(\s*(\d+)\s*\)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\AFixedString\(\s*(\d+)\s*\)\z/) 2 34 36
if ($type =~ /\AFixedString\(\s*(\d+)\s*\)\z/) 0 0 0
1317 0 return $1;
1318 }
1319 0 (my $base = $type) =~ s/\(.*//s;
1320 0 ▶ if ($base eq 'Decimal') {
Branch: 2 branches
BranchTrueFalseTotal
if ($base eq "Decimal") 0 34 34
if ($base eq "Decimal") 0 0 0
1321 # Decimal(P, S): storage width follows the precision P.
1322 0 my ($p) = $type =~ /\(\s*(\d+)/;
1323 0 ▶ return $p <= 9 ? 4 : $p <= 18 ? 8
Branch: 6 branches
BranchTrueFalseTotal
$p <= 9 ? : 0 0 0
$p <= 18 ? : 0 0 0
$p <= 38 ? : 0 0 0
$p <= 9 ? : 0 0 0
$p <= 18 ? : 0 0 0
$p <= 38 ? : 0 0 0
1324 : $p <= 38 ? 16 : 32;
1325 }
1326 0 ▶ return 8 if $base eq 'DateTime' && $type =~ /\(\s*(\d+)/ && $1 > 0;
Branch: 2 branches
BranchTrueFalseTotal
if $base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0 0 34 34
if $base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0 0 0 0
Condition25%3/12
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
!ll&&!rl&&r
3400
$base eq "DateTime" and $type =~ /\(\s*(\d+)/
!ll&&!rl&&r
3220
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
!ll&&!rl&&r
000
$base eq "DateTime" and $type =~ /\(\s*(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/3
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
$base eq "DateTime" $type =~ /\(\s*(\d+)/ $1 > 0
MC/DC0%0/3
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
$base eq "DateTime" $type =~ /\(\s*(\d+)/ $1 > 0
Truth table50%2/4
A and B and C
A= $base eq "DateTime"B= $type =~ /\(\s*(\d+)/C= $1 > 0
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $base eq "DateTime"B= $type =~ /\(\s*(\d+)/C= $1 > 0
ABCresult
0XX0
10X0
1100
1111
1327 0 my $w = $RB_FIXED_WIDTH{$base};
1328 0 ▶ die "decode_row_binary: cannot size unsupported type '$type'\n"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $w 0 34 34
unless defined $w 0 0 0
1329 unless defined $w;
1330 0 return $w;
1331 }
1332  
1333 # Decode one value of $type at $$posref, advancing $$posref past it.
1334 85 ▶sub _rb_decode_value {
Subroutine _rb_decode_value: called CC=9 SCAR=30.3
1335 0 ▶ my ($type, $bufref, $posref, $cache) = @_;
Subroutine _rb_decode_value: not called CC=9 SCAR=30.3
1336 0 ▶ if ($type =~ /\AArray\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\AArray\((.+)\)\z/s) 10 75 85
if ($type =~ /\AArray\((.+)\)\z/s) 0 0 0
1337 0 my $inner = $1;
1338 0 my ($n, $after) = _rb_unpack_varint($$bufref, $$posref);
1339 0 $$posref = $after;
1340 0 my @values;
1341 0 ▶ for (my $i = 0; $i < $n; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $n) 14 10 24
if ($i < $n) 0 0 0
1342 0 push @values, _rb_decode_value($inner, $bufref, $posref, $cache);
1343 }
1344 0 return \@values;
1345 }
1346 0 ▶ if ($type =~ /\ALowCardinality\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\ALowCardinality\((.+)\)\z/s) 2 73 75
if ($type =~ /\ALowCardinality\((.+)\)\z/s) 0 0 0
1347 0 return _rb_decode_value($1, $bufref, $posref, $cache);
1348 }
1349 # A set null flag is the entire value; see _rb_encode_value.
1350 0 ▶ if ($type =~ /\ANullable\((.+)\)\z/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\ANullable\((.+)\)\z/s) 23 50 73
if ($type =~ /\ANullable\((.+)\)\z/s) 0 0 0
1351 0 my $inner = $1;
1352 0 ▶ die "decode_row_binary: truncated null flag for '$type' "
Branch: 2 branches
BranchTrueFalseTotal
if $$posref >= length $$bufref 0 23 23
if $$posref >= length $$bufref 0 0 0
1353 . "at offset $$posref\n"
1354 if $$posref >= length $$bufref;
1355 0 my $is_null = ord substr($$bufref, $$posref, 1);
1356 0 $$posref += 1;
1357 # Explicit undef: the caller does `push @row, ...`, where a bare
1358 # return yields an empty list and would shift every later column.
1359 0 ▶ return undef if $is_null; ## no critic (ProhibitExplicitReturnUndef)
Branch: 2 branches
BranchTrueFalseTotal
if $is_null 10 13 23
if $is_null 0 0 0
1360 0 return _rb_decode_value($inner, $bufref, $posref, $cache);
1361 }
1362 0 _rb_assert_scalar($type);
1363 0 my $vlen = _rb_value_len($type, $bufref, $$posref);
1364 0 my $vbytes = substr($$bufref, $$posref, $vlen);
1365 0 ▶ die "decode_row_binary: truncated value for '$type' at offset $$posref\n"
Branch: 2 branches
BranchTrueFalseTotal
if length $vbytes != $vlen 0 50 50
if length $vbytes != $vlen 0 0 0
1366 if length($vbytes) != $vlen;
1367 0 $$posref += $vlen;
1368 0 ▶ my $prefix = $cache->{$type} ||=
Condition50%2/4
$cache->{$type} ||= "\cA\cA" . _rb_pack_string("c") . _rb_pack_string($type)
l!l
2426
$cache->{$type} ||= "\cA\cA" . _rb_pack_string("c") . _rb_pack_string($type)
l!l
00
MC/DC100%1/1
$cache->{$type} ||= "\cA\cA" . _rb_pack_string("c") . _rb_pack_string($type)
$cache->{$type}
MC/DC0%0/1
$cache->{$type} ||= "\cA\cA" . _rb_pack_string("c") . _rb_pack_string($type)
$cache->{$type}
Truth table100%2/2
A
A= $cache->{$type}
Aresult
11
00
Truth table0%0/2
A
A= $cache->{$type}
Aresult
11
00
1369 "\x01\x01" . _rb_pack_string('c') . _rb_pack_string($type);
1370 0 my $blk = ClickHouse::Encoder->decode_block($prefix . $vbytes);
1371 0 return $blk->{columns}[0]{values}[0];
1372 }
1373  
1374 # Decode a RowBinary byte string into an arrayref of row arrayrefs.
1375 # Call on an encoder instance whose column types match the producer.
1376 18 ▶sub decode_row_binary {
Subroutine decode_row_binary: called CC=7 SCAR=26.3
Pod documented
1377 0 ▶ my ($self, $bytes) = @_;
Subroutine decode_row_binary: not called CC=7 SCAR=26.3
Pod documented
1378 0 ▶ die "decode_row_binary: must be called on an encoder instance\n"
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self 1 17 18
unless ref $self 0 0 0
1379 unless ref $self;
1380 0 ▶ die "decode_row_binary: input must be defined\n" unless defined $bytes;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $bytes 0 17 17
unless defined $bytes 0 0 0
1381 0 my $cols = $self->columns;
1382 0 my $pos = 0;
1383 0 my $len = length $bytes;
1384 # With zero columns the inner per-column loop is a no-op, so a
1385 # non-empty buffer would never make $pos advance - guard explicitly
1386 # rather than spin forever.
1387 0 ▶ die "decode_row_binary: encoder has no columns but $len bytes given\n"
Branch: 2 branches
BranchTrueFalseTotal
if not @$cols and $len 1 16 17
if not @$cols and $len 0 0 0
Condition50%3/6
not @$cols and $len
!ll&&!rl&&r
1511
not @$cols and $len
!ll&&!rl&&r
000
MC/DC100%2/2
not @$cols and $len
not @$cols $len
MC/DC0%0/2
not @$cols and $len
not @$cols $len
Truth table100%3/3
not(A) and B
A= not @$colsB= $len
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not @$colsB= $len
ABresult
0X0
100
111
1388 if !@$cols && $len;
1389 0 my %cache;
1390 my @rows;
1391 0 ▶ while ($pos < $len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos < $len) 21 16 37
if ($pos < $len) 0 0 0
1392 0 my @row;
1393 0 for my $col (@$cols) {
1394 0 push @row, _rb_decode_value($col->[1], \$bytes, \$pos, \%cache);
1395 }
1396 0 push @rows, \@row;
1397 }
1398 0 return \@rows;
1399 }
1400  
1401 # Post-process a decoded block (or any one column's values arrayref):
1402 # rewrite Date / Date32 / DateTime / DateTime64 integer epochs into
1403 # ISO 8601 strings or Time::Moment instances. Modifies the block in
1404 # place AND returns it so the call can be chained. as => 'iso' (the
1405 # default) emits UTC strings with a 'Z' suffix; as => 'datetime'
1406 # returns Time::Moment objects (requires Time::Moment installed).
1407 # DateTime64 precision is read from the column's type string so each
1408 # tick converts to the correct number of fractional digits.
1409 10 ▶sub coerce_datetimes {
Subroutine coerce_datetimes: called CC=22 SCAR=45.0
Pod documented
1410 0 ▶ my ($class_or_self, $block, %opts) = @_;
Subroutine coerce_datetimes: not called CC=22 SCAR=45.0
Pod documented
1411 0 ▶ my $as = $opts{as} // 'iso';
Condition50%2/4
$opts{"as"} // "iso"
l!l
28
$opts{"as"} // "iso"
l!l
00
MC/DC100%1/1
$opts{"as"} // "iso"
$opts{"as"}
MC/DC0%0/1
$opts{"as"} // "iso"
$opts{"as"}
Truth table100%2/2
A
A= $opts{"as"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"as"}
Aresult
11
00
1412 0 ▶ die "coerce_datetimes: 'as' must be 'iso' or 'datetime' (got '$as')\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $as eq "iso" or $as eq "datetime" 2 8 10
unless $as eq "iso" or $as eq "datetime" 0 0 0
Condition33%2/6
$as eq "iso" or $as eq "datetime"
l!l&&r!l&&!r
802
$as eq "iso" or $as eq "datetime"
l!l&&r!l&&!r
000
MC/DC50%1/2
$as eq "iso" or $as eq "datetime"
$as eq "iso" $as eq "datetime"
MC/DC0%0/2
$as eq "iso" or $as eq "datetime"
$as eq "iso" $as eq "datetime"
Truth table66%2/3
A or B
A= $as eq "iso"B= $as eq "datetime"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $as eq "iso"B= $as eq "datetime"
ABresult
1X1
011
000
1413 unless $as eq 'iso' || $as eq 'datetime';
1414  
1415 0 ▶ if ($as eq 'datetime') {
Branch: 2 branches
BranchTrueFalseTotal
if ($as eq 'datetime') { } 0 8 8
if ($as eq 'datetime') { } 0 0 0
1416 0 require Time::Moment;
1417 } else {
1418 0 require POSIX;
1419 }
1420  
1421 0 for my $col (@{ $block->{columns} }) {
1422 0 ▶ next if $col->{skipped};
Branch: 2 branches
BranchTrueFalseTotal
if $col->{"skipped"} 1 19 20
if $col->{"skipped"} 0 0 0
1423 0 my $type = $col->{type};
1424 0 my $vals = $col->{values};
1425  
1426 # Strip Nullable() wrapping so the inner type matches below.
1427 # Nullable values come through as undef in the values array;
1428 # the loops already skip undef.
1429 0 ▶ $type = $1 while $type =~ /\A(?:Nullable|LowCardinality)\((.*)\)\z/s;
Branch: 2 branches
BranchTrueFalseTotal
$type =~ /\A(?:Nullable|LowCardinality)\((.*)\)\z/s and do { $type = $1 } 2 19 21
$type =~ /\A(?:Nullable|LowCardinality)\((.*)\)\z/s and do { $type = $1 } 0 0 0
1430  
1431 0 ▶ $type =~ s/\ADateTime\(/DateTime64(/
Branch: 2 branches
BranchTrueFalseTotal
if $type =~ /\ADateTime\(\s*(\d+)/ and $1 > 0 1 18 19
if $type =~ /\ADateTime\(\s*(\d+)/ and $1 > 0 0 0 0
Condition33%2/6
$type =~ /\ADateTime\(\s*(\d+)/ and $1 > 0
!ll&&!rl&&r
1801
$type =~ /\ADateTime\(\s*(\d+)/ and $1 > 0
!ll&&!rl&&r
000
MC/DC50%1/2
$type =~ /\ADateTime\(\s*(\d+)/ and $1 > 0
$type =~ /\ADateTime\(\s*(\d+)/ $1 > 0
MC/DC0%0/2
$type =~ /\ADateTime\(\s*(\d+)/ and $1 > 0
$type =~ /\ADateTime\(\s*(\d+)/ $1 > 0
Truth table66%2/3
A and B
A= $type =~ /\ADateTime\(\s*(\d+)/B= $1 > 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $type =~ /\ADateTime\(\s*(\d+)/B= $1 > 0
ABresult
0X0
100
111
1432 if $type =~ /\ADateTime\(\s*(\d+)/ && $1 > 0;
1433 0 ▶ if ($type eq 'Date' || $type eq 'Date32') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'Date' or $type eq 'Date32') { } 4 15 19
elsif ($type eq 'DateTime' or $type =~ /^DateTime\(/) { } 6 9 15
elsif ($type =~ /^DateTime(?:64)?\(\s*(\d+)/) { } 7 2 9
if ($type eq 'Date' or $type eq 'Date32') { } 0 0 0
elsif ($type eq 'DateTime' or $type =~ /^DateTime\(/) { } 0 0 0
elsif ($type =~ /^DateTime(?:64)?\(\s*(\d+)/) { } 0 0 0
Condition50%3/6
$type eq "Date" or $type eq "Date32"
l!l&&r!l&&!r
2215
$type eq "Date" or $type eq "Date32"
l!l&&r!l&&!r
000
MC/DC100%2/2
$type eq "Date" or $type eq "Date32"
$type eq "Date" $type eq "Date32"
MC/DC0%0/2
$type eq "Date" or $type eq "Date32"
$type eq "Date" $type eq "Date32"
Truth table100%3/3
A or B
A= $type eq "Date"B= $type eq "Date32"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $type eq "Date"B= $type eq "Date32"
ABresult
1X1
011
000
1434 0 for my $v (@$vals) {
1435 0 ▶ next unless defined $v;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $v 0 6 6
unless defined $v 0 0 0
1436 0 $v = _epoch_to_string($v * 86400, 0, $as, 'Y-m-d');
1437 }
1438 }
1439 ▶ elsif ($type eq 'DateTime' || $type =~ /^DateTime\(/) {
Condition50%3/6
$type eq "DateTime" or $type =~ /^DateTime\(/
l!l&&r!l&&!r
519
$type eq "DateTime" or $type =~ /^DateTime\(/
l!l&&r!l&&!r
000
MC/DC100%2/2
$type eq "DateTime" or $type =~ /^DateTime\(/
$type eq "DateTime" $type =~ /^DateTime\(/
MC/DC0%0/2
$type eq "DateTime" or $type =~ /^DateTime\(/
$type eq "DateTime" $type =~ /^DateTime\(/
Truth table100%3/3
A or B
A= $type eq "DateTime"B= $type =~ /^DateTime\(/
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $type eq "DateTime"B= $type =~ /^DateTime\(/
ABresult
1X1
011
000
1440 0 for my $v (@$vals) {
1441 0 ▶ next unless defined $v;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $v 2 8 10
unless defined $v 0 0 0
1442 0 $v = _epoch_to_string($v, 0, $as, 'iso');
1443 }
1444 }
1445 elsif ($type =~ /^DateTime(?:64)?\(\s*(\d+)/) {
1446 0 my $precision = $1;
1447 0 my $scale = 10 ** $precision;
1448 0 for my $v (@$vals) {
1449 0 ▶ next unless defined $v;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $v 0 8 8
unless defined $v 0 0 0
1450 # Decoded DateTime64 is an integer count of (10^precision)
1451 # ticks since the Unix epoch (a signed int64).
1452 87 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1453 0 my $secs = int($v / $scale);
1454 87 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1455 0 my $frac = $v - $secs * $scale;
1456 # Normalize negative fractional tail to a positive frac
1457 # below the integer epoch.
1458 0 ▶ if ($frac < 0) { $frac += $scale; $secs -= 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($frac < 0) 1 7 8
if ($frac < 0) 0 0 0
1459 0 $v = _epoch_to_string($secs, $frac, $as,
1460 'iso', $precision);
1461 }
1462 }
1463 # other columns (non-time) untouched
1464 }
1465 0 return $block;
1466 }
1467  
1468 # Format a (whole_seconds, fractional_ticks_at_$precision) pair under
1469 # either 'iso' or 'datetime' modes. Internal helper; the strftime is
1470 # UTC-only on purpose (CH itself stores UTC ticks; per-column timezone
1471 # is a display concern handled separately by the user if needed).
1472 22 ▶sub _epoch_to_string {
Subroutine _epoch_to_string: called CC=6 SCAR=29.3
1473 0 ▶ my ($secs, $frac_ticks, $as, $shape, $precision) = @_;
Subroutine _epoch_to_string: not called CC=6 SCAR=29.3
1474 0 ▶ if ($as eq 'datetime') {
Branch: 2 branches
BranchTrueFalseTotal
if ($as eq "datetime") 0 22 22
if ($as eq "datetime") 0 0 0
1475 0 ▶ if ($shape ne 'Y-m-d' && $precision) {
Branch: 2 branches
BranchTrueFalseTotal
if ($shape ne "Y-m-d" and $precision) 0 0 0
if ($shape ne "Y-m-d" and $precision) 0 0 0
Condition0%0/6
$shape ne "Y-m-d" and $precision
!ll&&!rl&&r
000
$shape ne "Y-m-d" and $precision
!ll&&!rl&&r
000
MC/DC0%0/2
$shape ne "Y-m-d" and $precision
$shape ne "Y-m-d" $precision
MC/DC0%0/2
$shape ne "Y-m-d" and $precision
$shape ne "Y-m-d" $precision
Truth table0%0/3
A and B
A= $shape ne "Y-m-d"B= $precision
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $shape ne "Y-m-d"B= $precision
ABresult
0X0
100
111
1476 # Time::Moment uses nanosecond precision; widen the ticks
1477 # accordingly. Precision > 9 isn't supported here.
1478 0 my $ns = $frac_ticks * (10 ** (9 - $precision));
1479 0 return Time::Moment->from_epoch($secs, $ns);
1480 }
1481 0 return Time::Moment->from_epoch($secs);
1482 }
1483 # 'iso' string form
1484 0 my @t = gmtime $secs;
1485 0 ▶ if ($shape eq 'Y-m-d') {
Branch: 2 branches
BranchTrueFalseTotal
if ($shape eq "Y-m-d") 6 16 22
if ($shape eq "Y-m-d") 0 0 0
1486 0 return POSIX::strftime('%Y-%m-%d', @t);
1487 }
1488 0 my $base = POSIX::strftime('%Y-%m-%dT%H:%M:%S', @t);
1489 0 ▶ if ($precision) {
Branch: 2 branches
BranchTrueFalseTotal
if ($precision) 7 9 16
if ($precision) 0 0 0
1490 0 $base .= sprintf('.%0*d', $precision, $frac_ticks);
1491 }
1492 0 return $base . 'Z';
1493 }
1494  
1495 # Static per-type byte budgets used by estimate_size. Returns the
1496 # bytes-per-row for fixed-width types; for variable types the second
1497 # return value is an "average string size" heuristic the caller can
1498 # use, and undef means "walk the actual values for accuracy". The
1499 # table is intentionally coarse - the goal is order-of-magnitude
1500 # sizing for batch-split decisions, not byte-exact accounting.
1501 87 my %FIXED_TYPE_BYTES = (
1502 Int8 => 1, UInt8 => 1, Bool => 1, Boolean => 1,
1503 Int16 => 2, UInt16 => 2, Date => 2,
1504 Int32 => 4, UInt32 => 4, Float32 => 4, BFloat16 => 2,
1505 Date32 => 4, DateTime => 4, IPv4 => 4, Decimal32 => 4,
1506 Enum8 => 1, Enum16 => 2,
1507 Int64 => 8, UInt64 => 8, Float64 => 8, DateTime64 => 8,
1508 Decimal64 => 8,
1509 UUID => 16, IPv6 => 16, Decimal128 => 16,
1510 Decimal256 => 32,
1511 # Geo aliases:
1512 Point => 16, Ring => 64, LineString => 64,
1513 MultiLineString => 256, Polygon => 256, MultiPolygon => 256,
1514 );
1515  
1516 25 ▶sub _type_byte_estimate {
Subroutine _type_byte_estimate: called CC=22 SCAR=53.4
1517 0 ▶ my ($type, $avg_str) = @_;
Subroutine _type_byte_estimate: not called CC=22 SCAR=53.4
1518 0 ▶ $avg_str //= 16;
Condition25%1/4
$avg_str //= 16
l!l
250
$avg_str //= 16
l!l
00
MC/DC0%0/1
$avg_str //= 16
$avg_str
MC/DC0%0/1
$avg_str //= 16
$avg_str
Truth table50%1/2
A
A= $avg_str
Aresult
11
00
Truth table0%0/2
A
A= $avg_str
Aresult
11
00
1519 # Strip outer parens/args for parametric prefix match.
1520 0 my $base = $type;
1521 0 $base =~ s/\(.*\z//s;
1522 0 ▶ return 8 if $base eq 'DateTime' && $type =~ /\(\s*(\d+)/ && $1 > 0;
Branch: 2 branches
BranchTrueFalseTotal
if $base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0 0 25 25
if $base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0 0 0 0
Condition25%3/12
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
!ll&&!rl&&r
2500
$base eq "DateTime" and $type =~ /\(\s*(\d+)/
!ll&&!rl&&r
2410
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
!ll&&!rl&&r
000
$base eq "DateTime" and $type =~ /\(\s*(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/3
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
$base eq "DateTime" $type =~ /\(\s*(\d+)/ $1 > 0
MC/DC0%0/3
$base eq "DateTime" and $type =~ /\(\s*(\d+)/ and $1 > 0
$base eq "DateTime" $type =~ /\(\s*(\d+)/ $1 > 0
Truth table50%2/4
A and B and C
A= $base eq "DateTime"B= $type =~ /\(\s*(\d+)/C= $1 > 0
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $base eq "DateTime"B= $type =~ /\(\s*(\d+)/C= $1 > 0
ABCresult
0XX0
10X0
1100
1111
1523 0 ▶ return $FIXED_TYPE_BYTES{$base} if exists $FIXED_TYPE_BYTES{$base};
Branch: 2 branches
BranchTrueFalseTotal
if exists $FIXED_TYPE_BYTES{$base} 14 11 25
if exists $FIXED_TYPE_BYTES{$base} 0 0 0
1524  
1525 0 ▶ if ($base eq 'String') {
Branch: 2 branches
BranchTrueFalseTotal
if ($base eq "String") 6 5 11
if ($base eq "String") 0 0 0
1526 0 return $avg_str + 1; # +1 for varint length prefix (small lens)
1527 }
1528 0 ▶ if ($base eq 'FixedString') {
Branch: 2 branches
BranchTrueFalseTotal
if ($base eq "FixedString") 1 4 5
if ($base eq "FixedString") 0 0 0
1529 0 my ($n) = $type =~ /^FixedString\((\d+)\)/;
1530 0 ▶ return $n // $avg_str;
Condition16%1/6
$n // $avg_str
l!l&&r!l&&!r
100
$n // $avg_str
l!l&&r!l&&!r
000
MC/DC0%0/2
$n // $avg_str
$n $avg_str
MC/DC0%0/2
$n // $avg_str
$n $avg_str
Truth table33%1/3
A // B
A= $nB= $avg_str
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $nB= $avg_str
ABresult
1X1
011
000
1531 }
1532 0 ▶ if ($base eq 'Nullable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($base eq "Nullable") 0 4 4
if ($base eq "Nullable") 0 0 0
1533 0 my ($inner) = $type =~ /^Nullable\((.+)\)$/;
1534 0 return 1 + _type_byte_estimate($inner, $avg_str);
1535 }
1536 0 ▶ if ($base eq 'Array' || $base eq 'Map') {
Branch: 2 branches
BranchTrueFalseTotal
if ($base eq "Array" or $base eq "Map") 1 3 4
if ($base eq "Array" or $base eq "Map") 0 0 0
Condition33%2/6
$base eq "Array" or $base eq "Map"
l!l&&r!l&&!r
103
$base eq "Array" or $base eq "Map"
l!l&&r!l&&!r
000
MC/DC50%1/2
$base eq "Array" or $base eq "Map"
$base eq "Array" $base eq "Map"
MC/DC0%0/2
$base eq "Array" or $base eq "Map"
$base eq "Array" $base eq "Map"
Truth table66%2/3
A or B
A= $base eq "Array"B= $base eq "Map"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $base eq "Array"B= $base eq "Map"
ABresult
1X1
011
000
1537 # 8-byte offset per row + N_avg(=4) inner elements.
1538 0 my ($inner) = $type =~ /^\w+\((.+)\)$/;
1539 0 return 8 + 4 * _type_byte_estimate($inner, $avg_str);
1540 }
1541 0 ▶ if ($base eq 'Tuple') {
Branch: 2 branches
BranchTrueFalseTotal
if ($base eq "Tuple") 0 3 3
if ($base eq "Tuple") 0 0 0
1542 0 my ($body) = $type =~ /^Tuple\((.+)\)$/;
1543 0 ▶ return 1 unless defined $body;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $body 0 0 0
unless defined $body 0 0 0
1544 0 my @parts = _split_paren_list($body);
1545 0 my $sum = 0;
1546 0 for my $p (@parts) {
1547 # Tuple elements may be named: "name Type"
1548 0 $p =~ s/^[A-Za-z_]\w*\s+//;
1549 0 $sum += _type_byte_estimate($p, $avg_str);
1550 }
1551 0 return $sum;
1552 }
1553 # LowCardinality: dict + per-row 1-byte index (typical low cardinality).
1554 # Variant: 1 disc byte + (heuristically) inner avg.
1555 0 ▶ return 1 + $avg_str if $base eq 'LowCardinality' || $base eq 'Variant';
Branch: 2 branches
BranchTrueFalseTotal
if $base eq "LowCardinality" or $base eq "Variant" 1 2 3
if $base eq "LowCardinality" or $base eq "Variant" 0 0 0
Condition33%2/6
$base eq "LowCardinality" or $base eq "Variant"
l!l&&r!l&&!r
102
$base eq "LowCardinality" or $base eq "Variant"
l!l&&r!l&&!r
000
MC/DC50%1/2
$base eq "LowCardinality" or $base eq "Variant"
$base eq "LowCardinality" $base eq "Variant"
MC/DC0%0/2
$base eq "LowCardinality" or $base eq "Variant"
$base eq "LowCardinality" $base eq "Variant"
Truth table66%2/3
A or B
A= $base eq "LowCardinality"B= $base eq "Variant"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $base eq "LowCardinality"B= $base eq "Variant"
ABresult
1X1
011
000
1556 # JSON/Object/Dynamic: shape-dependent. Heuristic: roughly two
1557 # avg_string_size payloads per row (one for path machinery + one
1558 # for value bytes), so the caller's avg_string_size override
1559 # actually moves the estimate.
1560 0 ▶ return 2 * $avg_str if $base eq 'JSON' || $base eq 'Object' || $base eq 'Dynamic';
Branch: 2 branches
BranchTrueFalseTotal
if $base eq "JSON" or $base eq "Object" or $base eq "Dynamic" 2 0 2
if $base eq "JSON" or $base eq "Object" or $base eq "Dynamic" 0 0 0
Condition16%2/12
$base eq "JSON" or $base eq "Object" or $base eq "Dynamic"
l!l&&r!l&&!r
200
$base eq "JSON" or $base eq "Object"
l!l&&r!l&&!r
200
$base eq "JSON" or $base eq "Object" or $base eq "Dynamic"
l!l&&r!l&&!r
000
$base eq "JSON" or $base eq "Object"
l!l&&r!l&&!r
000
MC/DC0%0/3
$base eq "JSON" or $base eq "Object" or $base eq "Dynamic"
$base eq "JSON" $base eq "Object" $base eq "Dynamic"
MC/DC0%0/3
$base eq "JSON" or $base eq "Object" or $base eq "Dynamic"
$base eq "JSON" $base eq "Object" $base eq "Dynamic"
Truth table25%1/4
A or B or C
A= $base eq "JSON"B= $base eq "Object"C= $base eq "Dynamic"
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B or C
A= $base eq "JSON"B= $base eq "Object"C= $base eq "Dynamic"
ABCresult
1XX1
01X1
0011
0000
1561 # SimpleAggregateFunction(func, T) -> inner T.
1562 0 ▶ if ($base eq 'SimpleAggregateFunction') {
Branch: 2 branches
BranchTrueFalseTotal
if ($base eq "SimpleAggregateFunction") 0 0 0
if ($base eq "SimpleAggregateFunction") 0 0 0
1563 0 my ($inner) = $type =~ /^SimpleAggregateFunction\([^,]+,\s*(.+)\)$/;
1564 0 ▶ return _type_byte_estimate($inner, $avg_str) if defined $inner;
Branch: 2 branches
BranchTrueFalseTotal
if defined $inner 0 0 0
if defined $inner 0 0 0
1565 }
1566 0 return $avg_str; # unknown -> conservative
1567 }
1568  
1569 # Coarse byte-size estimate for an encoded block, parameterized on
1570 # row count (an integer) or arrayref (counted). Uses per-type byte
1571 # budgets; variable-length types use a 16-byte average per value
1572 # (override via $avg_str). For batch-size decisions ("is this 1 MiB
1573 # or 100 MiB before I compress?"). not byte-exact: a String row with
1574 # a 10 KiB blob will be undercounted. Run encode() for the real size.
1575 19 ▶sub estimate_size {
Subroutine estimate_size: called CC=4 SCAR=17.9
Pod documented
1576 0 ▶ my ($self, $rows_or_n, %opts) = @_;
Subroutine estimate_size: not called CC=4 SCAR=17.9
Pod documented
1577 0 ▶ my $n = ref $rows_or_n eq 'ARRAY' ? scalar @$rows_or_n : $rows_or_n;
Branch: 2 branches
BranchTrueFalseTotal
ref $rows_or_n eq 'ARRAY' ? : 4 15 19
ref $rows_or_n eq 'ARRAY' ? : 0 0 0
1578 0 ▶ my $avg_str = $opts{avg_string_size} // 16;
Condition50%2/4
$opts{"avg_string_size"} // 16
l!l
613
$opts{"avg_string_size"} // 16
l!l
00
MC/DC100%1/1
$opts{"avg_string_size"} // 16
$opts{"avg_string_size"}
MC/DC0%0/1
$opts{"avg_string_size"} // 16
$opts{"avg_string_size"}
Truth table100%2/2
A
A= $opts{"avg_string_size"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"avg_string_size"}
Aresult
11
00
1579 0 my $cols = $self->columns;
1580 0 my $total = 4; # block header (ncols + nrows varints, ~tiny)
1581 0 for my $c (@$cols) {
1582 0 my ($name, $type) = @$c;
1583 0 $total += length($name) + length($type) + 2; # lenstr headers
1584 0 $total += $n * _type_byte_estimate($type, $avg_str);
1585 }
1586 0 return $total;
1587 }
1588  
1589 # Return a configured encoder for the column shape produced by an
1590 # arbitrary select. Runs `describe ($sql)` via the same `via=>...`
1591 # transport as for_table. Useful when the schema isn't a real table.
1592 0 ▶sub for_query {
Subroutine for_query: not called CC=1 SCAR=6.9
Pod documented
1593 0 ▶ my ($class, $sql, %opts) = @_;
Subroutine for_query: not called CC=1 SCAR=6.9
Pod documented
1594 # ClickHouse's describe accepts a subquery, but the SQL must not
1595 # contain unmatched parentheses; let the server reject malformed
1596 # queries rather than re-implementing SQL validation here.
1597 0 my $describe = "describe ($sql)";
1598 0 return $class->_for_describe($describe, %opts);
1599 }
1600  
1601 # Issue an insert ... format native over HTTP using HTTP::Tiny. Returns
1602 # the response hashref from HTTP::Tiny (->{success}, ->{status},
1603 # ->{content}). Compresses with zstd/gzip if `compress` is set; takes
1604 # whatever encoder produces (so `for_table` + rows is the typical
1605 # combination). Does not retry; the caller does HTTP-level error policy.
1606 # Set up URL + headers for an insert ... format native HTTP request.
1607 # Shared by insert_http and BulkInserter::new. Validates the table name
1608 # and stamps the Content-Type / Content-Encoding headers as needed.
1609 22 ▶sub _build_insert_endpoint {
Subroutine _build_insert_endpoint: called CC=6 SCAR=26.9
1610 0 ▶ my ($table, $compress, %args) = @_;
Subroutine _build_insert_endpoint: not called CC=6 SCAR=26.9
1611 0 _validate_table_name($table);
1612 0 ▶ die "unknown compress='$compress' "
Branch: 2 branches
BranchTrueFalseTotal
unless $compress eq "raw" or $compress eq "zstd" or $compress eq "gzip" 2 18 20
unless $compress eq "raw" or $compress eq "zstd" or $compress eq "gzip" 0 0 0
Condition33%4/12
$compress eq "raw" or $compress eq "zstd" or $compress eq "gzip"
l!l&&r!l&&!r
1802
$compress eq "raw" or $compress eq "zstd"
l!l&&r!l&&!r
1802
$compress eq "raw" or $compress eq "zstd" or $compress eq "gzip"
l!l&&r!l&&!r
000
$compress eq "raw" or $compress eq "zstd"
l!l&&r!l&&!r
000
MC/DC33%1/3
$compress eq "raw" or $compress eq "zstd" or $compress eq "gzip"
$compress eq "raw" $compress eq "zstd" $compress eq "gzip"
MC/DC0%0/3
$compress eq "raw" or $compress eq "zstd" or $compress eq "gzip"
$compress eq "raw" $compress eq "zstd" $compress eq "gzip"
Truth table50%2/4
A or B or C
A= $compress eq "raw"B= $compress eq "zstd"C= $compress eq "gzip"
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B or C
A= $compress eq "raw"B= $compress eq "zstd"C= $compress eq "gzip"
ABCresult
1XX1
01X1
0011
0000
1613 . "(expected 'raw', 'zstd', or 'gzip')\n"
1614 unless $compress eq 'raw' || $compress eq 'zstd'
1615 || $compress eq 'gzip';
1616 0 my ($url, $hdr) = _http_url_headers(
1617 "insert into $table format native", %args);
1618 0 $hdr->{'Content-Type'} = 'application/octet-stream';
1619 0 ▶ $hdr->{'Content-Encoding'} = $compress
Branch: 2 branches
BranchTrueFalseTotal
if $compress eq "zstd" or $compress eq "gzip" 0 18 18
if $compress eq "zstd" or $compress eq "gzip" 0 0 0
Condition16%1/6
$compress eq "zstd" or $compress eq "gzip"
l!l&&r!l&&!r
0018
$compress eq "zstd" or $compress eq "gzip"
l!l&&r!l&&!r
000
MC/DC0%0/2
$compress eq "zstd" or $compress eq "gzip"
$compress eq "zstd" $compress eq "gzip"
MC/DC0%0/2
$compress eq "zstd" or $compress eq "gzip"
$compress eq "zstd" $compress eq "gzip"
Truth table33%1/3
A or B
A= $compress eq "zstd"B= $compress eq "gzip"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $compress eq "zstd"B= $compress eq "gzip"
ABresult
1X1
011
000
1620 if $compress eq 'zstd' || $compress eq 'gzip';
1621 0 return ($url, $hdr);
1622 }
1623  
1624 # Apply zstd/gzip compression to $body in place (or pass through for 'raw').
1625 # $compress is validated upstream by _build_insert_endpoint; we trust it
1626 # here. $origin is the class used to resolve compressed_writer (so the
1627 # helper works for class-method and instance callers alike).
1628 23 ▶sub _apply_compression {
Subroutine _apply_compression: called CC=2 SCAR=14.4
1629 0 ▶ my ($origin, $compress, $body) = @_;
Subroutine _apply_compression: not called CC=2 SCAR=14.4
1630 0 ▶ return $body if $compress eq 'raw';
Branch: 2 branches
BranchTrueFalseTotal
if $compress eq "raw" 23 0 23
if $compress eq "raw" 0 0 0
1631 0 my $compressed;
1632 my $wrap = $origin->compressed_writer(
1633 0 ▶ $compress, sub { $compressed = $_[0] });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1634 0 $wrap->($body);
1635 0 return $compressed;
1636 }
1637  
1638 3 ▶sub insert_http {
Subroutine insert_http: called CC=8 SCAR=23.2
Pod documented
1639 3 my ($class_or_self, %args) = @_;
1640 3 ▶ my $enc = $args{encoder} // do {
Condition33%1/3
$args{"encoder"} // do { my $cols = $args{"columns"} or die "insert_http needs columns or encoder"; $class_or_self->new("columns", $cols, _encoder_options(%args)) }
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{"encoder"} // do { my $cols = $args{"columns"} or die "insert_http needs columns or encoder"; $class_or_self->new("columns", $cols, _encoder_options(%args)) }
$args{"encoder"} do { my $cols = $args{"columns"} or die "insert_http needs columns or encoder"; $class_or_self->new("columns", $cols, _encoder_options(%args)) }
Truth table33%1/3
A // B
A= $args{"encoder"}B= do { my $cols = $args{"columns"} or die "insert_http needs columns or encoder"; $class_or_self->new("columns", $cols, _encoder_options(%args)) }
ABresult
1X1
011
000
1641 3 ▶ my $cols = $args{columns} or die "insert_http needs columns or encoder";
Branch: 1 branch
BranchTrueFalseTotal
my $cols = $args{"columns"} or die "insert_http needs columns or encoder" 3 0 3
1642 3 $class_or_self->new(columns => $cols, _encoder_options(%args));
1643 };
1644 3 ▶ my $rows = $args{rows} or die "insert_http needs rows arrayref";
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $args{"rows"} or die "insert_http needs rows arrayref" 2 1 3
1645 2 ▶ my $table = $args{table} or die "insert_http needs table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $args{"table"} or die "insert_http needs table" 2 0 2
1646 2 ▶ my $timeout = $args{timeout} // 60;
Condition50%1/2
$args{"timeout"} // 60
l!l
02
MC/DC0%0/1
$args{"timeout"} // 60
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
1647 2 ▶ my $compress = $args{compress} // 'raw';
Condition100%2/2
$args{"compress"} // "raw"
l!l
11
MC/DC100%1/1
$args{"compress"} // "raw"
$args{"compress"}
Truth table100%2/2
A
A= $args{"compress"}
Aresult
11
00
1648 2 ▶ my $origin = ref($class_or_self) ? ref($class_or_self) : $class_or_self;
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_self ? : 0 2 2
1649  
1650 2 my ($url, $hdr) = _build_insert_endpoint($table, $compress, %args);
1651 0 my $body = _apply_compression($origin, $compress, $enc->encode($rows));
1652  
1653 0 my $resp = _http_tiny(%args, timeout => $timeout)
1654 ->post($url, { headers => $hdr, content => $body });
1655 0 return _decorate_response($resp);
1656 }
1657  
1658 # Stream a select response: POST the SQL with default_format=Native,
1659 # feed the response chunks into a sliding buffer, decode complete blocks
1660 # as they arrive, and pass each one to $opts{on_block}. Memory stays
1661 # bounded by chunk_size + one block, so this is the right entry point
1662 # for selects that won't fit in memory. The user's $sql must not include
1663 # a format clause - this helper always requests format Native.
1664 4 ▶sub _sql_without_literals {
Subroutine _sql_without_literals: called CC=9 SCAR=32.8
1665 4 my $s = shift;
1666 4 my $out = '';
1667 4 ▶ for (my $i = 0; $i < length $s; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $s) 77 4 81
1668 77 my $c = substr($s, $i, 1);
1669 77 ▶ if ($c eq "'" || $c eq '`' || $c eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "'" or $c eq "`" or $c eq "\"") 0 77 77
Condition33%2/6
$c eq "'" or $c eq "`" or $c eq "\""
l!l&&r!l&&!r
0077
$c eq "'" or $c eq "`"
l!l&&r!l&&!r
0077
MC/DC0%0/3
$c eq "'" or $c eq "`" or $c eq "\""
$c eq "'" $c eq "`" $c eq "\""
Truth table25%1/4
A or B or C
A= $c eq "'"B= $c eq "`"C= $c eq "\""
ABCresult
1XX1
01X1
0011
0000
1670 0 $i = _skip_sql_quoted($s, $i) - 1; $out .= ' '; next;
1671 }
1672 77 ▶ if (substr($s, $i, 2) eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 2) eq "--") 0 77 77
1673 0 my $end = index($s, "\n", $i + 2);
1674 0 ▶ last if $end < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 0 0 0
1675 0 $i = $end; $out .= ' '; next;
1676 }
1677 77 ▶ if (substr($s, $i, 2) eq '/*') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $i, 2) eq "/*") 0 77 77
1678 0 my $end = index($s, '*/', $i + 2);
1679 0 ▶ die "unterminated SQL comment\n" if $end < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 0 0 0
1680 0 $i = $end + 1; $out .= ' '; next;
1681 }
1682 77 $out .= $c;
1683 }
1684 4 return $out;
1685 }
1686  
1687 5 ▶sub select_blocks {
Subroutine select_blocks: called CC=10 SCAR=32.3
Pod documented
1688 5 my ($class, $sql, %opts) = @_;
1689 my $cb = $opts{on_block}
1690 5 ▶ or die "select_blocks: 'on_block' coderef required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $opts{"on_block"} or die "select_blocks: 'on_block' coderef required\n" 4 1 5
1691 4 ▶ die "select_blocks: \$sql should not include a format clause "
Branch: 1 branch
BranchTrueFalseTotal
if _sql_without_literals($sql) =~ /\bformat\s+\w+\s*;?\s*\z/i 3 1 4
1692 . "(select_blocks always requests format Native)\n"
1693 if _sql_without_literals($sql) =~ /\bformat\s+\w+\s*;?\s*\z/i;
1694 1 my $keep = $opts{keep};
1695 1 my $decompress = $opts{decompress};
1696  
1697 # Build URL+headers via the same helper insert_http uses, but with an
1698 # empty SQL placeholder; we POST the SQL as the request body. Adding
1699 # default_format=Native ensures the response is Native bytes even if
1700 # the user's SQL doesn't terminate with format.
1701 1 my %h_opts = %opts;
1702 # Drop keys this method consumes; also drop dedup_token, which is
1703 # meaningful only on insert (would be silently dead weight on the
1704 # select URL otherwise and could mask a typo by the caller).
1705 1 delete @h_opts{qw(on_block keep timeout decompress dedup_token)};
1706 1 my ($url, $hdr) = _http_url_headers('', %h_opts);
1707 1 $url .= '&default_format=Native';
1708 # When decompress=1 is requested the server wraps each Native block
1709 # in its compressed-block framing (X-ClickHouse-Compressed header).
1710 # Add ?compress=1 to the URL so CH knows to compress the response.
1711 1 ▶ $url .= '&compress=1' if $decompress;
Branch: 1 branch
BranchTrueFalseTotal
if $decompress 0 1 1
1712  
1713 1 my $buf = '';
1714 # Block walker: when decompress is set, walk through compressed-block-
1715 # framing entries and feed the decompressed bytes into a second
1716 # accumulator that decode_block reads. Otherwise feed buf directly.
1717 1 my $inner_buf = '';
1718 # As in decode_stream: after a truncation, wait for exactly the byte
1719 # count the decoder said it was short before re-parsing. The forced
1720 # final drain below still attempts the last partial block.
1721 1 my $need_at = 0;
1722 1 ▶ my $drain = sub {
Subroutine __ANON__: called CC=11 SCAR=42.9
1723 1 my ($force) = @_;
1724 # Phase 1: pull compressed-block frames out of $buf into $inner_buf
1725 1 ▶ if ($decompress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decompress) { } 0 1 1
1726 0 ▶ while (length($buf) >= 25) { # 16 hash + 9 header minimum
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf >= 25) 0 0 0
1727 0 my $csize = unpack 'V', substr($buf, 17, 4);
1728 0 ▶ last if length($buf) < 16 + $csize;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < 16 + $csize 0 0 0
1729 0 my ($plain, $consumed) =
1730 $class->decompress_native_block($buf);
1731 0 $inner_buf .= $plain;
1732 0 substr($buf, 0, $consumed, '');
1733 }
1734 } else {
1735 1 $inner_buf = $buf;
1736 }
1737 # Phase 2: decode whole Native blocks out of $inner_buf
1738 1 ▶ while (length($inner_buf) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $inner_buf > 0) 0 1 1
1739 0 ▶ last if !$force && length($inner_buf) < $need_at;
Branch: 1 branch
BranchTrueFalseTotal
if not $force and length $inner_buf < $need_at 0 0 0
Condition0%0/3
not $force and length $inner_buf < $need_at
!ll&&!rl&&r
000
MC/DC0%0/2
not $force and length $inner_buf < $need_at
not $force length $inner_buf < $need_at
Truth table0%0/3
not(A) and B
A= not $forceB= length $inner_buf < $need_at
ABresult
0X0
100
111
1740 0 my $block = eval { $class->decode_block($inner_buf, 0, $keep) };
1741 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1742 0 ▶ if ($@ =~ /buffer truncated \(need (\d+) more/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /buffer truncated \(need (\d+) more/i) 0 0 0
1743 0 $need_at = length($inner_buf) + $1;
1744 0 last;
1745 }
1746 # See decode_stream: a partial buffer trips the prologue's
1747 # header-count guard too, and it means the same thing here.
1748 0 ▶ last if $@ =~ /buffer truncated|exceeds remaining buffer/i;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /buffer truncated|exceeds remaining buffer/i 0 0 0
1749 0 die $@;
1750 }
1751 0 $cb->($block);
1752 0 substr($inner_buf, 0, $block->{consumed}, '');
1753 0 $need_at = 0;
1754 }
1755 # When not decompressing, inner_buf IS buf; carry the residual
1756 # back so the next data_callback append sees the unconsumed tail.
1757 1 ▶ if (!$decompress) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $decompress) 1 0 1
1758 1 $buf = $inner_buf;
1759 }
1760 1 };
1761  
1762 my $resp = _http_tiny(%opts, timeout => $opts{timeout} // 60)->post(
1763 $url,
1764 { content => utf8::is_utf8($sql) ? Encode::encode('UTF-8', $sql, 0) : $sql,
1765 headers => { %$hdr, 'Content-Type' => 'text/plain' },
1766 0 ▶ data_callback => sub { $buf .= $_[0]; $drain->() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1767 1 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($sql) ? : 0 1 1
Condition50%1/2
$opts{"timeout"} // 60
l!l
01
MC/DC0%0/1
$opts{"timeout"} // 60
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
Aresult
11
00
1768  
1769 die "select_blocks: HTTP $resp->{status}: $resp->{content}\n"
1770 1 ▶ unless $resp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->{"success"} 0 1 1
1771  
1772 1 $drain->(1); # forced: the response is complete, ignore $retry_at
1773 1 ▶ die "select_blocks: " . length($buf) . " trailing bytes "
Branch: 1 branch
BranchTrueFalseTotal
if $decompress and length $buf 0 1 1
Condition33%1/3
$decompress and length $buf
!ll&&!rl&&r
100
MC/DC0%0/2
$decompress and length $buf
$decompress length $buf
Truth table33%1/3
A and B
A= $decompressB= length $buf
ABresult
0X0
100
111
1774 . "after last complete compressed block\n"
1775 if $decompress && length $buf;
1776 1 ▶ die "select_blocks: " . length($inner_buf) . " trailing bytes "
Branch: 1 branch
BranchTrueFalseTotal
if length $inner_buf 0 1 1
1777 . "after last complete block\n"
1778 if length $inner_buf;
1779 1 return;
1780 }
1781  
1782 # Returns a bulk-inserter object: ->push($row), ->push_many(\@rows),
1783 # ->flush (idempotent), ->finish. Holds a single HTTP::Tiny instance
1784 # across batches and flushes at batch_size. HTTP 429, 5xx and network
1785 # failures are retried with exponential backoff; other errors die.
1786 21 ▶sub bulk_inserter {
Subroutine bulk_inserter: called CC=1 SCAR=0.0
Pod documented
1787 21 my ($class_or_self, %args) = @_;
1788 21 return ClickHouse::Encoder::BulkInserter->new(%args,
1789 _origin => $class_or_self);
1790 }
1791  
1792 package ClickHouse::Encoder::Streamer; ## no critic (ProhibitMultiplePackages)
1793  
1794 # Body is XS; declared here only to carry CLONE_SKIP (see main package).
1795 1 ▶sub CLONE_SKIP { return 1 }
Subroutine CLONE_SKIP: called CC=1 SCAR=0.0
1796  
1797 package ClickHouse::Encoder::BulkInserter; ## no critic (ProhibitMultiplePackages)
1798  
1799 # Holds an encoder, which does not survive a thread spawn (see the main
1800 # package). Skip cloning the whole inserter rather than handing the child
1801 # one whose {enc} has silently become undef.
1802 0 ▶sub CLONE_SKIP { return 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
1803  
1804 21 ▶sub new {
Subroutine new: called CC=16 SCAR=28.2
1805 21 my ($class, %args) = @_;
1806 21 my $origin_raw = delete $args{_origin};
1807 21 ▶ my $origin = (ref($origin_raw) ? ref($origin_raw) : $origin_raw)
Branch: 1 branch
BranchTrueFalseTotal
ref $origin_raw ? : 2 19 21
Condition50%1/2
(ref $origin_raw ? ref $origin_raw : $origin_raw) || 'ClickHouse::Encoder'
l!l
210
MC/DC0%0/1
(ref $origin_raw ? ref $origin_raw : $origin_raw) || 'ClickHouse::Encoder'
(ref $origin_raw ? ref $origin_raw : $origin_raw)
Truth table50%1/2
A
A= (ref $origin_raw ? ref $origin_raw : $origin_raw)
Aresult
11
00
1808 || 'ClickHouse::Encoder';
1809 21 ▶ my $enc = $args{encoder} // do {
Condition66%2/3
$args{"encoder"} // do { my $cols = $args{"columns"} or die "bulk_inserter needs columns or encoder"; $origin->new("columns", $cols, ClickHouse::Encoder::_encoder_options(%args)) }
l!l&&r!l&&!r
2190
MC/DC0%0/2
$args{"encoder"} // do { my $cols = $args{"columns"} or die "bulk_inserter needs columns or encoder"; $origin->new("columns", $cols, ClickHouse::Encoder::_encoder_options(%args)) }
$args{"encoder"} do { my $cols = $args{"columns"} or die "bulk_inserter needs columns or encoder"; $origin->new("columns", $cols, ClickHouse::Encoder::_encoder_options(%args)) }
Truth table66%2/3
A // B
A= $args{"encoder"}B= do { my $cols = $args{"columns"} or die "bulk_inserter needs columns or encoder"; $origin->new("columns", $cols, ClickHouse::Encoder::_encoder_options(%args)) }
ABresult
1X1
011
000
1810 19 ▶ my $cols = $args{columns} or die "bulk_inserter needs columns or encoder";
Branch: 1 branch
BranchTrueFalseTotal
my $cols = $args{"columns"} or die "bulk_inserter needs columns or encoder" 18 1 19
1811 18 $origin->new(columns => $cols, ClickHouse::Encoder::_encoder_options(%args));
1812 };
1813 20 ▶ my $table = $args{table} or die "bulk_inserter needs table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $args{"table"} or die "bulk_inserter needs table" 19 1 20
1814 19 ▶ my $compress = $args{compress} // 'raw';
Condition100%2/2
$args{"compress"} // "raw"
l!l
118
MC/DC100%1/1
$args{"compress"} // "raw"
$args{"compress"}
Truth table100%2/2
A
A= $args{"compress"}
Aresult
11
00
1815 19 ▶ my $timeout = $args{timeout} // 60;
Condition50%1/2
$args{"timeout"} // 60
l!l
019
MC/DC0%0/1
$args{"timeout"} // 60
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
1816 19 ▶ my $batch_size = $args{batch_size} // 10_000;
Condition100%2/2
$args{"batch_size"} // 10000
l!l
910
MC/DC100%1/1
$args{"batch_size"} // 10000
$args{"batch_size"}
Truth table100%2/2
A
A= $args{"batch_size"}
Aresult
11
00
1817 19 ▶ die "bulk_inserter: batch_size must be a positive integer\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $batch_size =~ /\A[1-9][0-9]*\z/ 0 19 19
1818 unless $batch_size =~ /\A[1-9][0-9]*\z/;
1819  
1820 19 my ($url, $hdr) = ClickHouse::Encoder::_build_insert_endpoint(
1821 $table, $compress, %args);
1822  
1823 17 my $token = $args{dedup_token};
1824 17 ▶ if (!defined $token && defined $args{settings}{insert_deduplication_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $token and defined $args{"settings"}{"insert_deduplication_token"}) 0 17 17
Condition66%2/3
not defined $token and defined $args{"settings"}{"insert_deduplication_token"}
!ll&&!rl&&r
4130
MC/DC0%0/2
not defined $token and defined $args{"settings"}{"insert_deduplication_token"}
not defined $token defined $args{"settings"}{"insert_deduplication_token"}
Truth table66%2/3
not(A) and B
A= not defined $tokenB= defined $args{"settings"}{"insert_deduplication_token"}
ABresult
0X0
100
111
1825 0 $token = $args{settings}{insert_deduplication_token};
1826 }
1827 17 ▶ $url =~ s/&insert_deduplication_token=[^&]*//g if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 4 13 17
1828  
1829 return bless {
1830 enc => $enc,
1831 url => $url,
1832 dedup_token => $token,
1833 hdr => $hdr,
1834 rows => [],
1835 batch_size => $batch_size,
1836 retries => $args{retries} // 3,
1837 retry_wait => $args{retry_wait} // 0.5,
1838 17 ▶ retry_max_wait => $args{retry_max_wait} // 30,
Condition100%6/6
$args{"retries"} // 3
l!l
116
$args{"retry_wait"} // "0.5"
l!l
89
$args{"retry_max_wait"} // 30
l!l
116
MC/DC100%1/1
$args{"retries"} // 3
$args{"retries"}
MC/DC100%1/1
$args{"retry_wait"} // "0.5"
$args{"retry_wait"}
MC/DC100%1/1
$args{"retry_max_wait"} // 30
$args{"retry_max_wait"}
Truth table100%2/2
A
A= $args{"retries"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"retry_wait"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"retry_max_wait"}
Aresult
11
00
1839 compress => $compress,
1840 http => ClickHouse::Encoder::_http_tiny(
1841 %args, timeout => $timeout, keep_alive => 1),
1842 origin => $origin,
1843 sent_rows => 0,
1844 sent_batches => 0,
1845 last_response => undef,
1846 summary => {},
1847 }, $class;
1848 }
1849  
1850 18 ▶sub push :method { ## no critic (ProhibitBuiltinHomonyms)
Subroutine push: called CC=2 SCAR=6.9
1851 18 my ($self, $row) = @_;
1852 18 CORE::push @{ $self->{rows} }, $row;
1853 18 ▶ $self->flush if @{ $self->{rows} } >= $self->{batch_size};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"rows"};} >= $self->{"batch_size"} 4 14 18
1854 18 return $self;
1855 }
1856  
1857 6 ▶sub push_many {
Subroutine push_many: called CC=4 SCAR=13.9
1858 6 my ($self, $rows) = @_;
1859 6 CORE::push @{ $self->{rows} }, @{$rows};
1860 # Restore the failed batch ahead of the untried rows on error.
1861 6 ▶ while (@{ $self->{rows} } > $self->{batch_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"rows"};} > $self->{"batch_size"}) 7 4 11
1862 7 my @batch = splice @{ $self->{rows} }, 0, $self->{batch_size};
1863 7 my $ok;
1864 7 { local $self->{rows} = \@batch; $ok = eval { $self->flush; 1 }; }
1865 7 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 5 7
1866 2 unshift @{ $self->{rows} }, @batch;
1867 2 die $@;
1868 }
1869 }
1870 4 ▶ $self->flush if @{ $self->{rows} } >= $self->{batch_size};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"rows"};} >= $self->{"batch_size"} 1 3 4
1871 4 return $self;
1872 }
1873  
1874 24 ▶sub flush {
Subroutine flush: called CC=15 SCAR=27.1
1875 24 my $self = shift;
1876 24 my $rows = $self->{rows};
1877 24 ▶ return $self if !@{$rows};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rows;} 1 23 24
1878 my $body = ClickHouse::Encoder::_apply_compression(
1879 23 $self->{origin}, $self->{compress}, $self->{enc}->encode($rows));
1880 23 my $url = $self->{url};
1881 23 ▶ if (defined $self->{dedup_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dedup_token"}) 8 15 23
1882 8 my $token = "$self->{dedup_token}-$self->{sent_batches}";
1883 8 require Encode;
1884 8 ▶ $token = Encode::encode('UTF-8', $token, 0) if utf8::is_utf8($token);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($token) 0 8 8
1885 8 $token =~ s/([^A-Za-z0-9\-_.~])/sprintf('%%%02X', ord($1))/ge;
1886 8 $url .= "&insert_deduplication_token=$token";
1887 }
1888 23 my $resp;
1889 my $last_err;
1890 23 for my $attempt (0 .. $self->{retries}) {
1891 $resp = $self->{http}->post($url,
1892 30 { headers => $self->{hdr}, content => $body });
1893 30 ▶ last if $resp->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"success"} 18 12 30
1894 die "bulk_inserter: HTTP $resp->{status}: $resp->{content}\n"
1895 12 ▶ if $resp->{status} < 500 && $resp->{status} != 429;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"status"} < 500 and $resp->{"status"} != 429 1 11 12
Condition100%3/3
$resp->{"status"} < 500 and $resp->{"status"} != 429
!ll&&!rl&&r
1011
MC/DC100%2/2
$resp->{"status"} < 500 and $resp->{"status"} != 429
$resp->{"status"} < 500 $resp->{"status"} != 429
Truth table100%3/3
A and B
A= $resp->{"status"} < 500B= $resp->{"status"} != 429
ABresult
0X0
100
111
1896 11 $last_err = "HTTP $resp->{status}: $resp->{content}";
1897 # 429, 5xx and network failures (599) are retryable. Exponential
1898 # backoff (retry_wait * 2^attempt, capped at retry_max_wait)
1899 # with equal jitter: sleep half the window deterministically
1900 # then a random half, so concurrent inserters retrying the
1901 # same failed server don't resynchronise into a thundering herd.
1902 11 ▶ if ($attempt < $self->{retries}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt < $self->{"retries"}) 7 4 11
1903 7 require Time::HiRes;
1904 7 my $window = $self->{retry_wait} * (2 ** $attempt);
1905 $window = $self->{retry_max_wait}
1906 7 ▶ if $window > $self->{retry_max_wait};
Branch: 1 branch
BranchTrueFalseTotal
if $window > $self->{"retry_max_wait"} 0 7 7
1907 7 ▶ Time::HiRes::sleep($window / 2 + rand($window / 2)) if $window > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $window > 0 0 7 7
1908 }
1909 }
1910 die "bulk_inserter: gave up after $self->{retries} retries; "
1911 . "last error: $last_err\n"
1912 22 ▶ unless $resp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->{"success"} 4 18 22
1913 18 ClickHouse::Encoder::_decorate_response($resp);
1914 18 $self->{last_response} = $resp;
1915 18 ▶ if (my $sum = $resp->{ch}{summary}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sum = $resp->{"ch"}{"summary"}) 2 16 18
1916 # Roll up CH summary fields across batches (read_rows,
1917 # written_rows, written_bytes, elapsed_ns, ...). Caller uses
1918 # ->summary to get the running totals; ->last_response to get
1919 # the most recent per-batch detail.
1920 $self->{summary}{$_} = ($self->{summary}{$_} // 0) + $sum->{$_}
1921 2 ▶ for grep { $sum->{$_} =~ /\A-?\d+\z/ } keys %$sum;
Condition100%2/2
$self->{"summary"}{$_} // 0
l!l
44
MC/DC100%1/1
$self->{"summary"}{$_} // 0
$self->{"summary"}{$_}
Truth table100%2/2
A
A= $self->{"summary"}{$_}
Aresult
11
00
1922 }
1923 18 $self->{rows} = [];
1924 18 $self->{sent_rows} += @{$rows};
1925 18 $self->{sent_batches}++;
1926 18 return $self;
1927 }
1928  
1929 2 ▶sub last_response { my $self = shift; return $self->{last_response} }
Subroutine last_response: called CC=1 SCAR=0.0
1930 2 ▶sub summary { my $self = shift; return $self->{summary} }
Subroutine summary: called CC=1 SCAR=0.0
1931  
1932 4 ▶sub finish {
Subroutine finish: called CC=1 SCAR=0.0
1933 4 my $self = shift;
1934 4 $self->flush;
1935 4 return { rows => $self->{sent_rows}, batches => $self->{sent_batches} };
1936 }
1937  
1938 5 ▶sub buffered_count { my $self = shift; return scalar @{ $self->{rows} } }
Subroutine buffered_count: called CC=1 SCAR=0.0
1939 5 ▶sub sent_rows { my $self = shift; return $self->{sent_rows} }
Subroutine sent_rows: called CC=1 SCAR=0.0
1940 1 ▶sub sent_batches { my $self = shift; return $self->{sent_batches} }
Subroutine sent_batches: called CC=1 SCAR=0.0
1941  
1942 package ClickHouse::Encoder; ## no critic (ProhibitMultiplePackages)
1943  
1944 # Decimal128 values returned by decode_block come as [lo_uint64,
1945 # hi_int64]; Decimal256 as a 4-limb arrayref. Use Math::BigInt to
1946 # stitch limbs into a scaled decimal string.
1947 8 ▶sub decimal128_str {
Subroutine decimal128_str: called CC=1 SCAR=0.0
Pod documented
1948 8 my ($class, $lo, $hi, $scale) = @_;
1949 8 require Math::BigInt;
1950 8 my $two64 = Math::BigInt->new(1)->blsft(64);
1951 8 my $v = Math::BigInt->new($hi)->bmul($two64)->badd($lo);
1952 8 return _scale_int_to_str($v, $scale);
1953 }
1954  
1955 5 ▶sub decimal256_str {
Subroutine decimal256_str: called CC=2 SCAR=6.9
Pod documented
1956 5 my ($class, $limbs, $scale) = @_;
1957 5 require Math::BigInt;
1958 5 my $two64 = Math::BigInt->new(1)->blsft(64);
1959 # Top limb (limbs[3]) is the sign-extended high quarter. The sign
1960 # check uses BigInt comparisons; native Perl `1 << 64` returns 0 on
1961 # a 64-bit Perl (shift past word width), which would silently turn
1962 # a negative value into a wrong positive one.
1963 5 my $top = Math::BigInt->new($limbs->[3]);
1964 5 my $two63 = Math::BigInt->new(1)->blsft(63);
1965 5 ▶ $top->bsub($two64) if $top >= $two63;
Branch: 1 branch
BranchTrueFalseTotal
if $top >= $two63 2 3 5
1966 5 my $v = $top;
1967 5 $v->bmul($two64)->badd(Math::BigInt->new($limbs->[2]));
1968 5 $v->bmul($two64)->badd(Math::BigInt->new($limbs->[1]));
1969 5 $v->bmul($two64)->badd(Math::BigInt->new($limbs->[0]));
1970 5 return _scale_int_to_str($v, $scale);
1971 }
1972  
1973 13 ▶sub _scale_int_to_str {
Subroutine _scale_int_to_str: called CC=5 SCAR=16.1
1974 13 my ($big, $scale) = @_;
1975 13 ▶ my $sign = $big->is_neg ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$big->is_neg ? : 5 8 13
1976 13 my $abs = $big->copy->babs->bstr;
1977 13 ▶ return "$sign$abs" if !$scale || $scale == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $scale or $scale == 0 8 5 13
Condition66%2/3
not $scale or $scale == 0
l!l&&r!l&&!r
805
MC/DC50%1/2
not $scale or $scale == 0
not $scale $scale == 0
Truth table66%2/3
not(A) or B
A= not $scaleB= $scale == 0
ABresult
1X1
011
000
1978 5 ▶ $abs = ('0' x ($scale - length($abs) + 1)) . $abs
Branch: 1 branch
BranchTrueFalseTotal
if length $abs <= $scale 1 4 5
1979 if length($abs) <= $scale;
1980 5 return $sign . substr($abs, 0, length($abs) - $scale)
1981 . '.'
1982 . substr($abs, length($abs) - $scale);
1983 }
1984  
1985 # Convenience: open `@cmd` as a write pipe, encode rows into it, and
1986 # close. Croaks on fork failure, exec failure, or non-zero child exit.
1987 # Used by examples piping into
1988 # `clickhouse-client insert ... format native`.
1989 13 ▶sub encode_to_command {
Subroutine encode_to_command: called CC=8 SCAR=21.8
Pod documented
1990 13 my ($self, $cmd, $rows) = @_;
1991 13 ▶ ref $cmd eq 'ARRAY' or die "encode_to_command: cmd must be arrayref";
Branch: 1 branch
BranchTrueFalseTotal
ref $cmd eq "ARRAY" or die "encode_to_command: cmd must be arrayref" 11 2 13
1992 11 ▶ @$cmd or die "encode_to_command: cmd must be non-empty arrayref";
Branch: 1 branch
BranchTrueFalseTotal
@$cmd or die "encode_to_command: cmd must be non-empty arrayref" 9 2 11
1993  
1994 # Without ignoring SIGPIPE, an early child exit (e.g. clickhouse-client
1995 # rejecting the schema) would kill the parent silently with status
1996 # 141 instead of producing a trappable diagnostic on close.
1997 9 local $SIG{PIPE} = 'IGNORE';
1998  
1999 ## no critic (InputOutput::RequireBriefOpen) -- $fh is closed below
2000 9 ▶ defined(my $pid = open my $fh, '|-') or die "fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = open(my $fh, "|-")) or die "fork: $!" 9 0 9
2001 9 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 3 6 9
2002 3 local $SIG{PIPE} = 'DEFAULT';
2003 # exec only returns on failure. Fold the failure path onto the
2004 # same statement (via `or do {...}`) so Perl doesn't flag the
2005 # post-exec code as unreachable at compile time. POSIX::_exit
2006 # (not die or plain exit) skips END blocks and DESTROY handlers
2007 # inherited from the parent; syswrite avoids running PerlIO
2008 # layers and __WARN__ handlers. Suppress Perl's default "Can't
2009 # exec" warning so the only diagnostic comes from our syswrite.
2010 87 ▶ no warnings 'exec'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
2011 3 ▶ exec { $cmd->[0] } @$cmd or do {
Branch: 1 branch
BranchTrueFalseTotal
exec {$cmd->[0];} @$cmd or do { my $err = $!; require POSIX; syswrite STDERR, "exec @${cmd}: $err\n"; POSIX::_exit(127) } 0 0 0
2012 0 my $err = $!;
2013 0 require POSIX;
2014 0 syswrite STDERR, "exec @$cmd: $err\n";
2015 0 POSIX::_exit(127);
2016 };
2017 }
2018 6 binmode $fh;
2019 6 $self->encode_to_handle($fh, $rows);
2020 5 ▶ close $fh
Branch: 2 branches
BranchTrueFalseTotal
close $fh or die "@$cmd " . ($! ? "close: $!" : 'exit ' . ($? >> 8)) 3 2 5
$! ? : 0 2 2
2021 or die "@$cmd " . ($! ? "close: $!" : "exit " . ($? >> 8));
2022 3 return;
2023 }
2024  
2025 87 1;
2026  
2027 __END__
↑ Summary blib/lib/ClickHouse/Encoder/TCP.pm »