blib/lib/ClickHouse/Encoder.pm

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