ClickHouse/Storage.pm

filter: Statementstmt 88.1% 164 / 186 Branchbran 76.9% 60 / 78 Conditioncond 54.4% 37 / 68 MC/DCmcdc 28.5% 12 / 42 Subroutinesub 84.3% 43 / 51 Podpod 100% 10 / 10 totaltotal 74.9% 326 / 435 CC 66 SCAR 21.3
CC
66
Cov
79%
_refuse_non_numeric_binds
26.7
_integer_type
17.4
_ping
12.0
CRAP 8.4
« ClickHouse/Introspect/Columns.pm ↑ Summary ClickHouse/Native.pm »
1 package DBIO::ClickHouse::Storage;
2 # ABSTRACT: ClickHouse storage driver for DBIO (MySQL wire protocol)
3  
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 our $VERSION = '0.900007';
8  
9 16 ▶use base qw/DBIO::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 16 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use DBIO::Storage::BlockRunner;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use DBIO::ClickHouse::Storage::TxnScopeGuard;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Crypt::URandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use DBIO::ClickHouse::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use DBIO::ClickHouse::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 16 __PACKAGE__->register_driver('ClickHouse' => __PACKAGE__);
24  
25 16 __PACKAGE__->sql_maker_class('DBIO::ClickHouse::SQLMaker');
26 16 __PACKAGE__->sql_quote_char('`');
27  
28 # F02 / F10: ClickHouse has no transactions at all (ADR 0003), so DDL cannot
29 # be transactional either. Core's default for an undeclared capability is
30 # already 0, but leaving it undeclared would make the deploy path depend on
31 # that default rather than on a statement by this driver: DBIO::Deploy::Base
32 # ::_execute_ddl brackets its DDL loop in $storage->txn_do only when this
33 # reports true, and our txn_do neither brackets nor rolls back (L</txn_do>,
34 # L</txn_rollback>). A truthy value here would therefore buy a promise of
35 # atomicity we cannot keep. Declared, and pinned by t/17-capabilities.t.
36 16 __PACKAGE__->_use_transactional_ddl(0);
37  
38 # F12: ClickHouse parses IF EXISTS / IF NOT EXISTS on the object kinds this
39 # driver emits DDL for -- CREATE/DROP DATABASE, CREATE/DROP TABLE,
40 # ALTER TABLE ADD/DROP COLUMN and ADD/DROP INDEX -- so a re-applied diff can
41 # no-op instead of erroring after a partial failure; probed against a live
42 # server by t/38-ddl-if-exists-live.t. Core's
43 # DBIO::Diff::Op::should_emit_if_exists reads this declaration, which
44 # t/17-capabilities.t pins. Declaring it still changes no rendered statement:
45 # the diff ops here never call that helper, and DBIO::ClickHouse::DDL::Emit's
46 # drop_table / alter_table_* forms take no if_exists. This is the engine fact,
47 # not a claim about what the diff currently emits.
48 16 __PACKAGE__->_use_supports_if_exists(1);
49  
50 # ADR 0037: ClickHouse accepts core's base rendering of _exists_value_sql
51 #
52 # SELECT CASE WHEN EXISTS (<subquery>) THEN 1 ELSE 0 END
53 #
54 # unchanged, so this driver declares the capability and overrides nothing.
55 # Checked against the documentation rather than assumed -- the capability
56 # defaults to 0 exactly so an unchecked engine is never handed that SQL, and
57 # ADR 0037 listed ClickHouse among the unchecked. All three assumptions the
58 # fragment stacks hold:
59 #
60 # 1. EXISTS used as a *value*, not only as a predicate. ClickHouse
61 # documents it as returning 0 or 1 ("If it is empty, then the operator
62 # returns 0. Otherwise, it returns 1") and its own example selects two
63 # of them straight into a select list -- sql-reference/operators/exists.
64 # Available since 21.11 ("Support EXISTS(subquery)", CH #29731).
65 # 2. A SELECT with no FROM: that same documented example is literally
66 # "SELECT EXISTS(...), EXISTS(...)" with no FROM clause.
67 # 3. CASE WHEN ... THEN ... ELSE ... END: documented under Conditional
68 # Functions / "CASE statement", lowered internally to multiIf().
69 #
70 # The one thing left to check was the space core leaves between EXISTS and
71 # the subquery -- the documentation only ever writes EXISTS(subquery), and
72 # ClickHouse parses it through its *function* layer (ExistsLayer, in
73 # src/Parsers/ExpressionListParsers.cpp), not as a prefix operator. It parses
74 # either way: whitespace never reaches the parser (src/Parsers/TokenIterator.h
75 # -- "All whitespace and comment tokens are transparently skipped"). So no
76 # override, and none for ADR 0037's third assumption either: "AS" before a
77 # derived-table alias is unreachable since that ADR dropped the wrapper.
78 #
79 # Not covered by a live test (the karr k15 suite run passed, but no live file
80 # asserts an EXISTS subquery) and one documented limit is deliberately NOT
81 # hidden by this declaration: the subquery may not reference the outer query
82 # (L</EXISTS QUERIES>).
83 16 __PACKAGE__->_use_supports_exists_query(1);
84  
85  
86 266 ▶sub sql_maker {
Subroutine sql_maker: called CC=3 SCAR=12.3
Pod documented
87 266 my $self = shift;
88 266 my $sm = $self->next::method(@_);
89 266 ▶ $sm->{quote_char} //= $self->sql_quote_char;
Condition33%1/3
$sm->{"quote_char"} //= $self->sql_quote_char
l!l&&r!l&&!r
26600
MC/DC0%0/2
$sm->{"quote_char"} //= $self->sql_quote_char
$sm->{"quote_char"} $self->sql_quote_char
Truth table33%1/3
A //= B
A= $sm->{"quote_char"}B= $self->sql_quote_char
ABresult
1X1
011
000
90 266 ▶ $sm->{name_sep} //= $self->sql_name_sep;
Condition33%1/3
$sm->{"name_sep"} //= $self->sql_name_sep
l!l&&r!l&&!r
26600
MC/DC0%0/2
$sm->{"name_sep"} //= $self->sql_name_sep
$sm->{"name_sep"} $self->sql_name_sep
Truth table33%1/3
A //= B
A= $sm->{"name_sep"}B= $self->sql_name_sep
ABresult
1X1
011
000
91 266 $sm;
92 }
93  
94  
95 16 __PACKAGE__->register_access_path_dbd(qw( mysql MariaDB ));
96  
97  
98 1 ▶sub _determine_supports_insert_returning { 0 }
Subroutine _determine_supports_insert_returning: called CC=1 SCAR=0.0
99  
100  
101 98 ▶sub _prefetch_autovalues {
Subroutine _prefetch_autovalues: called CC=5 SCAR=16.1
102 98 my ($self, $source, $colinfo, $to_insert) = @_;
103  
104 98 my $values = $self->next::method($source, $colinfo, $to_insert);
105  
106 98 for my $col (keys %$colinfo) {
107 1129 ▶ next unless $colinfo->{$col}{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
unless $colinfo->{$col}{"is_auto_increment"} 1117 12 1129
108 12 ▶ next unless _is_uuid_type($colinfo->{$col});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_uuid_type($colinfo->{$col}) 1 11 12
109  
110 # An explicitly supplied value always wins, literal SQL included. undef
111 # counts as "not supplied", the same reading core applies in insert().
112 11 ▶ next if defined $to_insert->{$col};
Branch: 1 branch
BranchTrueFalseTotal
if defined $to_insert->{$col} 2 9 11
113  
114 9 $values->{$col} = $self->_generate_uuid;
115 }
116  
117 98 return $values;
118 }
119  
120 # Must stay in lockstep with DBIO::ClickHouse::DDL::_is_uuid_type: the DDL
121 # layer emits DEFAULT generateUUIDv4() for exactly the columns this driver
122 # generates a UUID for. t/13-insert.t asserts the two agree.
123 12 ▶sub _is_uuid_type {
Subroutine _is_uuid_type: called CC=3 SCAR=11.1
124 12 my ($info) = @_;
125 12 ▶ return (($info->{data_type} // '') =~ /\A(?:uuid|guid|uniqueidentifier)\z/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($info->{'data_type'} // '') =~ /\A(?:uuid|guid|uniqueidentifier)\z/i ? : 11 1 12
Condition50%1/2
$info->{"data_type"} // ""
l!l
120
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
126 }
127  
128  
129 9 ▶sub _generate_uuid {
Subroutine _generate_uuid: called CC=1 SCAR=0.0
130 9 my $bytes = Crypt::URandom::urandom(16);
131  
132 # RFC 4122 section 4.4: version nibble in byte 6, variant bits in byte 8.
133 9 vec($bytes, 6, 8) = (vec($bytes, 6, 8) & 0x0f) | 0x40;
134 9 vec($bytes, 8, 8) = (vec($bytes, 8, 8) & 0x3f) | 0x80;
135  
136 9 return join '-', unpack 'H8H4H4H4H12', $bytes;
137 }
138  
139  
140 3 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=5 SCAR=18.6
Pod documented
141 3 my ($self, $source, @cols) = @_;
142  
143 3 ▶ my $where = ( ref $source and $source->can('source_name') )
Branch: 1 branch
BranchTrueFalseTotal
ref $source && $source->can('source_name') ? : 3 0 3
Condition33%1/3
ref $source and $source->can("source_name")
!ll&&!rl&&r
003
MC/DC0%0/2
ref $source and $source->can("source_name")
ref $source $source->can("source_name")
Truth table33%1/3
A and B
A= ref $sourceB= $source->can("source_name")
ABresult
0X0
100
111
144 ? $source->source_name
145 : 'this result source';
146  
147 $self->throw_exception(sprintf
148 'ClickHouse has no auto-increment and no last_insert_id(), so the primary '
149 . 'key of the row just inserted into %s (column%s %s) cannot be read back -- '
150 . 'the row itself was written. Supply the key as a plain value, or, for a '
151 . q{surrogate key, declare the column as data_type => 'uuid', }
152 . 'is_auto_increment => 1: this driver then generates a UUID client-side and '
153 . 'sends it, so the stored row and the value you get back are the same. '
154 . 'is_auto_increment on any other type is refused by the DDL layer -- '
155 . 'ClickHouse has no server-side generator for it. A key left to a DEFAULT or written as '
156 . q{literal SQL (\'generateUUIDv4()') is computed by the server, which is }
157 . 'the same unreadable case: use populate(), which never needs the key back',
158 $where,
159 (@cols == 1 ? '' : 's'),
160 3 ▶ (@cols ? join(', ', map { "'$_'" } @cols) : '(none named)')
Branch: 2 branches
BranchTrueFalseTotal
@cols == 1 ? : 3 0 3
@cols ? : 3 0 3
161 );
162 }
163  
164  
165 7 ▶sub _insert_bulk {
Subroutine _insert_bulk: called CC=3 SCAR=11.0
166 7 my ($self, $source, $cols, $data) = @_;
167  
168 $self->throw_exception(
169 'Calling _insert_bulk without a dataset to process makes no sense'
170 7 ▶ ) unless @{ $data || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$data or [];} 1 6 7
Condition50%1/2
$data || []
l!l
70
MC/DC0%0/1
$data || []
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
171  
172 6 return $self->_execute('insert_multi', $source, $cols, $data);
173 }
174  
175  
176 131 ▶sub _prep_for_execute {
Subroutine _prep_for_execute: called CC=4 SCAR=13.9
177 131 my ($self, $op, $ident, $args) = @_;
178  
179 131 my ($sql, $bind) = $self->next::method($op, $ident, $args);
180  
181 127 ▶ $self->_refuse_non_numeric_binds($ident, $bind)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "insert" or $op eq "insert_multi" or $op eq "update" 114 13 127
Condition100%6/6
$op eq "insert" or $op eq "insert_multi" or $op eq "update"
l!l&&r!l&&!r
1041013
$op eq "insert" or $op eq "insert_multi"
l!l&&r!l&&!r
98623
MC/DC100%3/3
$op eq "insert" or $op eq "insert_multi" or $op eq "update"
$op eq "insert" $op eq "insert_multi" $op eq "update"
Truth table100%4/4
A or B or C
A= $op eq "insert"B= $op eq "insert_multi"C= $op eq "update"
ABCresult
1XX1
01X1
0011
0000
182 if $op eq 'insert' or $op eq 'insert_multi' or $op eq 'update';
183  
184 84 return ($sql, $bind);
185 }
186  
187 16 my $NUMBER = {
188 integer => qr/\A[+-]?\d+\z/,
189 float => qr/\A(?:[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?|[+-]?(?:inf|nan))\z/i,
190 decimal => qr/\A[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?\z/,
191 };
192  
193 # The numeric family of a column's data_type, or undef: portable names
194 # (integer, bigint, double, numeric ...) and the native ones (Int32, UInt8,
195 # Float64, Decimal(10, 2), Nullable(Int64) ...).
196 286 ▶sub _numeric_kind {
Subroutine _numeric_kind: called CC=6 SCAR=17.9
197 286 my ($data_type) = @_;
198 286 ▶ return undef unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 0 286 286
199 286 my $t = lc $data_type;
200 286 ▶ 1 while $t =~ s/\A\s*(?:nullable|lowcardinality)\s*\((.*)\)\s*\z/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$t =~ s/\A\s*(?:nullable|lowcardinality)\s*\((.*)\)\s*\z/$1/ and do { '???' } 6 286 292
201 286 $t =~ s/\s+unsigned\z//;
202 286 ▶ return 'integer' if $t =~ /\A(?:u?int(?:8|16|32|64|128|256)?|tinyint|smallint|mediumint|integer|bigint|serial|bigserial)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /\A(?:u?int(?:8|16|32|64|128|256)?|tinyint|smallint|mediumint|integer|bigint|serial|bigserial)\z/ 139 147 286
203 147 ▶ return 'float' if $t =~ /\A(?:float(?:32|64)?|real|double(?:\s+precision)?)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /\A(?:float(?:32|64)?|real|double(?:\s+precision)?)\z/ 12 135 147
204 135 ▶ return 'decimal' if $t =~ /\A(?:decimal(?:32|64|128|256)?|numeric|number|money)\s*(?:\(.*\))?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /\A(?:decimal(?:32|64|128|256)?|numeric|number|money)\s*(?:\(.*\))?\z/ 7 128 135
205 128 return undef;
206 }
207  
208 # The native integer type of the bind's column (Int8 .. UInt256), or undef:
209 # the column's own info when the statement's source has it (so extra =>
210 # { unsigned => 1 } counts), else the bind's data type -- through the one type
211 # map the DDL deploys with, Nullable and LowCardinality looked through.
212 126 ▶sub _integer_type {
Subroutine _integer_type: called CC=10 SCAR=28.6
213 126 my ($ident, $attrs) = @_;
214  
215 126 my $col = $attrs->{dbic_colname};
216 126 ▶ my $info = Scalar::Util::blessed($ident) && $ident->can('has_column')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ident) && $ident->can('has_column') && $ident->has_column($col) ? : 126 0 126
Condition33%2/6
builtin::blessed($ident) and $ident->can("has_column") and $ident->has_column($col)
!ll&&!rl&&r
00126
builtin::blessed($ident) and $ident->can("has_column")
!ll&&!rl&&r
00126
MC/DC0%0/3
builtin::blessed($ident) and $ident->can("has_column") and $ident->has_column($col)
builtin::blessed($ident) $ident->can("has_column") $ident->has_column($col)
Truth table25%1/4
A and B and C
A= builtin::blessed($ident)B= $ident->can("has_column")C= $ident->has_column($col)
ABCresult
0XX0
10X0
1100
1111
217 && $ident->has_column($col) ? $ident->column_info($col) : undef;
218 126 ▶ $info = { data_type => $attrs->{sqlt_datatype} } unless $info and defined $info->{data_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and defined $info->{"data_type"} 0 126 126
Condition33%1/3
$info and defined $info->{"data_type"}
!ll&&!rl&&r
00126
MC/DC0%0/2
$info and defined $info->{"data_type"}
$info defined $info->{"data_type"}
Truth table33%1/3
A and B
A= $infoB= defined $info->{"data_type"}
ABresult
0X0
100
111
219  
220 126 my %info = %$info;
221 0 ▶ $info{extra} = { %{ $info{extra} || {} }, unsigned => 1 }
Condition0%0/2
$info{'extra'} || {}
l!l
00
MC/DC0%0/1
$info{'extra'} || {}
$info{'extra'}
Truth table0%0/2
A
A= $info{'extra'}
Aresult
11
00
222 126 ▶ if $info{data_type} =~ s/\s+unsigned\z//i;
Branch: 1 branch
BranchTrueFalseTotal
if $info{"data_type"} =~ s/\s+unsigned\z//i 0 126 126
223  
224 126 my $type = DBIO::ClickHouse::DDL->column_type(\%info);
225 126 ▶ 1 while $type =~ s/\A(?:Nullable|LowCardinality)\((.*)\)\z/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$type =~ s/\A(?:Nullable|LowCardinality)\((.*)\)\z/$1/ and do { '???' } 86 126 212
226 126 ▶ return $type =~ /\AU?Int(?:8|16|32|64|128|256)\z/ ? $type : undef;
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /\AU?Int(?:8|16|32|64|128|256)\z/ ? : 126 0 126
227 }
228  
229 16 my %RANGE;
230  
231 # True when the integer string $value does not fit the native type.
232 126 ▶sub _outside_range {
Subroutine _outside_range: called CC=5 SCAR=16.1
233 126 my ($value, $type) = @_;
234  
235 126 ▶ my ($min, $max) = @{ $RANGE{$type} ||= do {
Condition66%2/3
$RANGE{$type} ||= do { () = $type =~ /\A(U?)Int(\d+)\z/; $u ? ["Math::BigInt"->bzero, "Math::BigInt"->new(2)->bpow($bits)->bdec] : ["Math::BigInt"->new(2)->bpow($bits - 1)->bneg, "Math::BigInt"->new(2)->bpow($bits - 1)->bdec] }
l!l&&r!l&&!r
11790
MC/DC0%0/2
$RANGE{$type} ||= do { () = $type =~ /\A(U?)Int(\d+)\z/; $u ? ["Math::BigInt"->bzero, "Math::BigInt"->new(2)->bpow($bits)->bdec] : ["Math::BigInt"->new(2)->bpow($bits - 1)->bneg, "Math::BigInt"->new(2)->bpow($bits - 1)->bdec] }
$RANGE{$type} do { () = $type =~ /\A(U?)Int(\d+)\z/; $u ? ["Math::BigInt"->bzero, "Math::BigInt"->new(2)->bpow($bits)->bdec] : ["Math::BigInt"->new(2)->bpow($bits - 1)->bneg, "Math::BigInt"->new(2)->bpow($bits - 1)->bdec] }
Truth table66%2/3
A ||= B
A= $RANGE{$type}B= do { () = $type =~ /\A(U?)Int(\d+)\z/; $u ? ["Math::BigInt"->bzero, "Math::BigInt"->new(2)->bpow($bits)->bdec] : ["Math::BigInt"->new(2)->bpow($bits - 1)->bneg, "Math::BigInt"->new(2)->bpow($bits - 1)->bdec] }
ABresult
1X1
011
000
236 9 my ($u, $bits) = $type =~ /\A(U?)Int(\d+)\z/;
237 9 ▶ $u ? [ Math::BigInt->bzero, Math::BigInt->new(2)->bpow($bits)->bdec ]
Branch: 1 branch
BranchTrueFalseTotal
$u ? : 3 6 9
238 : [ Math::BigInt->new(2)->bpow($bits - 1)->bneg,
239 Math::BigInt->new(2)->bpow($bits - 1)->bdec ];
240 } };
241  
242 126 my $n = Math::BigInt->new($value =~ s/\A\+//r);
243 126 ▶ return ($n < $min or $n > $max) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n < $min || $n > $max ? : 24 102 126
Condition100%3/3
$n < $min or $n > $max
l!l&&r!l&&!r
816102
MC/DC100%2/2
$n < $min or $n > $max
$n < $min $n > $max
Truth table100%3/3
A or B
A= $n < $minB= $n > $max
ABresult
1X1
011
000
244 }
245  
246 114 ▶sub _refuse_non_numeric_binds {
Subroutine _refuse_non_numeric_binds: called CC=19 SCAR=32.9
247 114 my ($self, $ident, $bind) = @_;
248  
249 114 ▶ for my $item (@{ $bind || [] }) {
Condition50%1/2
$bind || []
l!l
1140
MC/DC0%0/1
$bind || []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
250 289 my ($attrs, $value) = @$item;
251 289 ▶ next unless defined $value and not ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 3 286 289
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
30286
MC/DC50%1/2
defined $value and not ref $value
defined $value not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
252 286 ▶ next unless ref $attrs eq 'HASH' and defined $attrs->{dbic_colname};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs eq "HASH" and defined $attrs->{"dbic_colname"} 0 286 286
Condition33%1/3
ref $attrs eq "HASH" and defined $attrs->{"dbic_colname"}
!ll&&!rl&&r
00286
MC/DC0%0/2
ref $attrs eq "HASH" and defined $attrs->{"dbic_colname"}
ref $attrs eq "HASH" defined $attrs->{"dbic_colname"}
Truth table33%1/3
A and B
A= ref $attrs eq "HASH"B= defined $attrs->{"dbic_colname"}
ABresult
0X0
100
111
253  
254 286 ▶ my $kind = _numeric_kind($attrs->{sqlt_datatype}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $kind = _numeric_kind($attrs->{"sqlt_datatype"}) or next 158 128 286
255 158 ▶ my $table = Scalar::Util::blessed($ident) && $ident->can('name') ? $ident->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ident) && $ident->can('name') ? : 158 0 158
Condition33%1/3
builtin::blessed($ident) and $ident->can("name")
!ll&&!rl&&r
00158
MC/DC0%0/2
builtin::blessed($ident) and $ident->can("name")
builtin::blessed($ident) $ident->can("name")
Truth table33%1/3
A and B
A= builtin::blessed($ident)B= $ident->can("name")
ABresult
0X0
100
111
256  
257 158 ▶ if ($value =~ $NUMBER->{$kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$NUMBER->{$kind}/) 139 19 158
258 139 ▶ next unless $kind eq 'integer';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "integer" 13 126 139
259  
260 126 my $native = _integer_type($ident, $attrs);
261 126 ▶ next unless defined $native and _outside_range($value, $native);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $native and _outside_range($value, $native) 102 24 126
Condition66%2/3
defined $native and _outside_range($value, $native)
!ll&&!rl&&r
010224
MC/DC50%1/2
defined $native and _outside_range($value, $native)
defined $native _outside_range($value, $native)
Truth table66%2/3
A and B
A= defined $nativeB= _outside_range($value, $native)
ABresult
0X0
100
111
262  
263 $self->throw_exception(sprintf
264 'DBIO::ClickHouse: table %s, column %s: the value is refused, it is '
265 . 'outside the range of %s. ClickHouse accepts such a number and stores '
266 . 'NULL (a Nullable column) or a wrapped-around number (any other) '
267 . 'without an error. Nothing was sent',
268 (defined $table and not ref $table) ? $table : '(statement)',
269 $attrs->{dbic_colname},
270 24 ▶ $native,
Branch: 1 branch
BranchTrueFalseTotal
defined $table && !ref($table) ? : 24 0 24
Condition33%1/3
defined $table and not ref $table
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $table and not ref $table
defined $table not ref $table
Truth table33%1/3
A and not(B)
A= defined $tableB= not ref $table
ABresult
0X0
100
111
271 );
272 }
273  
274 $self->throw_exception(sprintf
275 'DBIO::ClickHouse: table %s, column %s: the value is refused, it is not '
276 . 'a%s number. ClickHouse accepts such a string for a %s column and stores '
277 . 'NULL without an error. Nothing was sent',
278 (defined $table and not ref $table) ? $table : '(statement)',
279 $attrs->{dbic_colname},
280 $kind eq 'integer' ? 'n integer' : '',
281 $attrs->{sqlt_datatype},
282 19 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
defined $table && !ref($table) ? : 19 0 19
$kind eq 'integer' ? : 13 6 19
Condition33%1/3
defined $table and not ref $table
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $table and not ref $table
defined $table not ref $table
Truth table33%1/3
A and not(B)
A= defined $tableB= not ref $table
ABresult
0X0
100
111
283 }
284 }
285  
286  
287 12 ▶sub update {
Subroutine update: called CC=1 SCAR=0.0
Pod documented
288 12 my $self = shift;
289 12 $self->next::method(@_);
290 6 return -1;
291 }
292  
293 12 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
294 12 my $self = shift;
295 12 $self->next::method(@_);
296 8 return -1;
297 }
298  
299  
300 42 ▶sub _dbio_sql_maker_option_names {
Subroutine _dbio_sql_maker_option_names: called CC=1 SCAR=0.0
301 42 my $self = shift;
302 42 return ($self->next::method(@_), 'mutations_sync');
303 }
304  
305  
306 0 ▶sub _adjust_select_args_for_complex_prefetch {
Subroutine _adjust_select_args_for_complex_prefetch: not called CC=1 SCAR=6.9
307 0 my $self = shift;
308 0 my $outer_attrs = $self->next::method(@_);
309 0 delete @{$outer_attrs}{qw( final prewhere limit_by )};
310 0 return $outer_attrs;
311 }
312  
313  
314 # ADR 0039. This is the case ADR 0037 section 3 predicted and ADR 0039
315 # recorded as never having turned up -- a second engine that *spells* a set
316 # operation differently rather than lacking it. It is not one operation but
317 # three, and the reason is sharper than Oracle's MINUS: ClickHouse does not
318 # disagree about the word for an operation, it disagrees about what the word
319 # without a modifier means. A bare keyword there is resolved from a session
320 # setting -- union_default_mode ships empty (a bare UNION throws, changed to
321 # throw in 21.1), intersect_default_mode and except_default_mode ship 'ALL'
322 # (a bare INTERSECT/EXCEPT keeps duplicates). So core's ANSI spelling is a
323 # hard error for one operation and a silently different result set for two.
324 # Spelling all three explicitly is also what makes the statement immune to
325 # those settings.
326 16 my %SET_OPERATION_KEYWORD = (
327 union => 'UNION DISTINCT',
328 intersect => 'INTERSECT DISTINCT',
329 except => 'EXCEPT DISTINCT',
330 );
331  
332 23 ▶sub _set_operation_keyword {
Subroutine _set_operation_keyword: called CC=3 SCAR=11.0
333 23 my ($self, $op) = @_;
334  
335 return $SET_OPERATION_KEYWORD{$op}
336 23 ▶ if defined $op and exists $SET_OPERATION_KEYWORD{$op};
Branch: 1 branch
BranchTrueFalseTotal
if defined $op and exists $SET_OPERATION_KEYWORD{$op} 12 11 23
Condition100%3/3
defined $op and exists $SET_OPERATION_KEYWORD{$op}
!ll&&!rl&&r
11012
MC/DC100%2/2
defined $op and exists $SET_OPERATION_KEYWORD{$op}
defined $op exists $SET_OPERATION_KEYWORD{$op}
Truth table100%3/3
A and B
A= defined $opB= exists $SET_OPERATION_KEYWORD{$op}
ABresult
0X0
100
111
337  
338 # Everything this driver has no opinion about -- including an unknown $op,
339 # which is a core programming error and belongs to core's throw.
340 11 return $self->next::method($op);
341 }
342  
343  
344 # The value this returns is never seen by anyone: core drops the string into
345 # an order_by literal and nowhere else, so only the induced ORDER is
346 # observable and a uniform UInt32 orders as uniformly as randCanonical()'s
347 # Float64 would -- while working on every ClickHouse back to 1.1 instead of
348 # 22.11. The trap the choice has to avoid is randConstant(), documented as one
349 # value for the whole query: ORDER BY that is a non-shuffle with no error at
350 # all. rand() is a regular function and random numbers are the documented
351 # exception to constant folding, i.e. per row. See L</RANDOM ORDERING>.
352 6 ▶sub _random_function { 'rand()' }
Subroutine _random_function: called CC=1 SCAR=0.0
353  
354 # _explain_query is deliberately NOT overridden (ADR 0040). ClickHouse's bare
355 # EXPLAIN *is* EXPLAIN PLAN -- one statement, executed with the query's own
356 # binds, answering with the plan as rows -- which is precisely core's base
357 # implementation, so an override could only re-spell what is already right.
358 # The non-override is pinned by t/43-random-and-explain.t so it cannot decay
359 # into a half-considered EXPLAIN AST / PIPELINE / ESTIMATE later. See
360 # L</QUERY PLANS>.
361  
362  
363 17 ▶sub _exec_txn_begin { shift->_warn_no_transactions }
Subroutine _exec_txn_begin: called CC=1 SCAR=0.0
364  
365 5 ▶sub _exec_txn_commit { shift->_warn_no_transactions }
Subroutine _exec_txn_commit: called CC=1 SCAR=0.0
366  
367 22 ▶sub _warn_no_transactions {
Subroutine _warn_no_transactions: called CC=1 SCAR=0.0
368 22 carp_once(
369 'ClickHouse has no transactions: txn_begin() and txn_commit() are no-ops. '
370 . 'Nothing this storage writes is atomic, and nothing can be rolled back'
371 );
372 }
373  
374  
375 6 ▶sub txn_do {
Subroutine txn_do: called CC=2 SCAR=6.9
Pod documented
376 6 my $self = shift;
377  
378 6 carp_once(
379 'ClickHouse has no transactions: txn_do() runs its coderef without '
380 . 'BEGIN/COMMIT. The block is not atomic -- a failure part-way through '
381 . 'leaves every completed statement in place'
382 );
383  
384 # take a ref instead of a copy, to preserve @_ aliasing semantics within the
385 # coderef (same trick as core's dbh_do)
386 6 ▶ my $args = @_ ? \@_ : [];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 0 6
387  
388 my $runner = DBIO::Storage::BlockRunner->new(
389 storage => $self,
390 wrap_txn => 0,
391 3 ▶ retry_handler => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
392 6 );
393  
394 6 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
395 6 $runner->run(@$args);
396 }
397 3 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
398 3 my $err = $_;
399 3 carp(
400 'txn_do() failed on ClickHouse and NOTHING WAS ROLLED BACK: every '
401 . 'statement the block completed before the failure is permanent. '
402 . 'Check your data for consistency'
403 );
404 3 die $err;
405 6 };
406 }
407  
408  
409 3 ▶sub txn_rollback {
Subroutine txn_rollback: called CC=2 SCAR=6.9
Pod documented
410 3 my $self = shift;
411  
412 # Unwind core's depth bookkeeping before refusing. Nothing was begun on the
413 # server, but a stuck transaction_depth would make every later statement
414 # believe it runs inside a transaction. The rest of core's rollback state is
415 # dead here: savepoints always throw, and this is the only rollback path, so
416 # the deferred-rollback machinery is never armed.
417 3 my $depth = $self->transaction_depth;
418 3 ▶ $self->transaction_depth($depth - 1) if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 2 1 3
419  
420 3 $self->throw_exception(
421 'ClickHouse has no transactions: txn_rollback() cannot undo anything -- '
422 . 'every statement issued so far is already permanent. Remove the rollback '
423 . 'or handle the partial write explicitly'
424 );
425 }
426  
427  
428 24 ▶sub txn_scope_guard {
Subroutine txn_scope_guard: called CC=1 SCAR=0.0
Pod documented
429 24 my $self = shift;
430 24 return DBIO::ClickHouse::Storage::TxnScopeGuard->new($self);
431 }
432  
433  
434 0 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
435 0 my ($self, $name) = @_;
436 0 $self->throw_exception('ClickHouse does not support savepoints');
437 }
438  
439 0 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
440 0 my ($self, $name) = @_;
441 0 $self->throw_exception('ClickHouse does not support savepoints');
442 }
443  
444 0 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
445 0 my ($self, $name) = @_;
446 0 $self->throw_exception('ClickHouse does not support savepoints');
447 }
448  
449  
450 0 ▶sub _ping {
Subroutine _ping: not called CC=3 SCAR=24.8
451 0 my $self = shift;
452 0 ▶ my $dbh = $self->_dbh or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return undef 0 0 0
453 0 ▶ return undef unless $dbh->FETCH('Active');
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->FETCH("Active") 0 0 0
454 0 ▶ return try { $dbh->do('SELECT 1'); 1 } catch { undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
455 }
456  
457  
458 0 ▶sub clickhouse_version {
Subroutine clickhouse_version: not called CC=1 SCAR=6.9
Pod documented
459 0 my $self = shift;
460 0 my ($v) = $self->_get_dbh->selectrow_array('SELECT version()');
461 0 return $v;
462 }
463  
464  
465 10 ▶sub _mask_error_values {
Subroutine _mask_error_values: called CC=3 SCAR=11.0
466 10 my ($self, $text, $values, $statement) = @_;
467  
468 10 ▶ return $text unless defined $text and !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 2 8 10
Condition100%3/3
defined $text and not ref $text
!ll&&!rl&&r
118
MC/DC100%2/2
defined $text and not ref $text
defined $text not ref $text
Truth table100%3/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
469  
470 8 $text = DBIO::ClickHouse::Util::mask_error_details($text);
471  
472 8 return $self->next::method($text, $values, $statement);
473 }
474  
475  
476 3 ▶sub dbio_deploy_class { 'DBIO::ClickHouse::Deploy' }
Subroutine dbio_deploy_class: called CC=1 SCAR=0.0
Pod documented
477  
478  
479 2 ▶sub deployment_statements {
Subroutine deployment_statements: called CC=5 SCAR=17.5
Pod documented
480 2 my ($self, $schema) = @_;
481  
482 2 ▶ return $schema->clickhouse_install_ddl
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema and $schema->can("clickhouse_install_ddl") 1 1 2
Condition66%2/3
defined $schema and $schema->can("clickhouse_install_ddl")
!ll&&!rl&&r
011
MC/DC50%1/2
defined $schema and $schema->can("clickhouse_install_ddl")
defined $schema $schema->can("clickhouse_install_ddl")
Truth table66%2/3
A and B
A= defined $schemaB= $schema->can("clickhouse_install_ddl")
ABresult
0X0
100
111
483 if defined $schema && $schema->can('clickhouse_install_ddl');
484  
485 1 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 1 0 1
Condition33%1/3
ref $schema or $schema
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $schema or $schema
ref $schema $schema
Truth table33%1/3
A or B
A= ref $schemaB= $schema
ABresult
1X1
011
000
486 'Cannot generate ClickHouse DDL: '
487 . (defined $schema ? ref($schema) || $schema : 'the schema')
488 . ' has no clickhouse_install_ddl(). Load the ClickHouse component into your '
489 . "schema class (__PACKAGE__->load_components('ClickHouse')) or declare it "
490 . 'with the -ch shortcut (use DBIO Schema => -ch). There is no generic '
491 . 'fallback here: a ClickHouse connection reports its DBD name '
492 . '(MariaDB/mysql) as sqlt_type, so the generic path would deploy MySQL DDL '
493 . '-- InnoDB, FOREIGN KEY, AUTO_INCREMENT -- which ClickHouse cannot execute'
494 );
495 }
496  
497  
498 16 1;
499  
500 __END__
« ClickHouse/Introspect/Columns.pm ↑ Summary ClickHouse/Native.pm »