ClickHouse/SQLMaker.pm

filter: Statementstmt 97.6% 124 / 127 Branchbran 91.1% 62 / 68 Conditioncond 75.0% 48 / 64 MC/DCmcdc 63.1% 24 / 38 Subroutinesub 100% 22 / 22 Podpod 100% 5 / 5 totaltotal 87.9% 285 / 324 CC 58 SCAR 18.5
CC
58
Cov
90%
_mutation_where
15.0
insert_multi
8.0
_limit_by
8.0
CRAP 6.3
« ClickHouse/Introspect.pm ↑ Summary ClickHouse/Introspect/Indexes.pm »
1 package DBIO::ClickHouse::SQLMaker;
2 # ABSTRACT: ClickHouse-specific SQL generation for DBIO
3  
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 our $VERSION = '0.900007';
8  
9 12 ▶use base qw( DBIO::SQLMaker );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 # ClickHouse has no SELECT ... FOR UPDATE (no row locks, no transactions).
13 sub _lock_select () { '' }
14  
15  
16 26 ▶sub new {
Subroutine new: called CC=6 SCAR=18.2
Pod documented
17 26 my $self = shift;
18 0 ▶ my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 26 26
19  
20 # exists(), not truthiness: an explicit undef means "emit no SETTINGS clause"
21 # and must survive, and 0 is a legal value.
22 26 ▶ $opts{mutations_sync} = 1 unless exists $opts{mutations_sync};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"mutations_sync"} 18 8 26
23  
24 $self->throw_exception(sprintf
25 q{Invalid mutations_sync value '%s': ClickHouse accepts 0 (queue the }
26 . 'mutation and return), 1 (wait for it on this server) or 2 (wait for it on '
27 . 'all replicas). Pass undef to emit no SETTINGS clause and leave the setting '
28 . 'to the session or the server profile',
29 $opts{mutations_sync}
30 26 ▶ ) if defined $opts{mutations_sync} and $opts{mutations_sync} !~ /\A[012]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"mutations_sync"} and not $opts{"mutations_sync"} =~ /\A[012]\z/ 3 23 26
Condition100%3/3
defined $opts{"mutations_sync"} and not $opts{"mutations_sync"} =~ /\A[012]\z/
!ll&&!rl&&r
2213
MC/DC100%2/2
defined $opts{"mutations_sync"} and not $opts{"mutations_sync"} =~ /\A[012]\z/
defined $opts{"mutations_sync"} not $opts{"mutations_sync"} =~ /\A[012]\z/
Truth table100%3/3
A and not(B)
A= defined $opts{"mutations_sync"}B= not $opts{"mutations_sync"} =~ /\A[012]\z/
ABresult
0X0
100
111
31  
32 # ClickHouse spells the assignment list without a SET keyword, which is what
33 # SQL::Abstract's generic clause renderer would prepend.
34 23 ▶ $opts{render_clause}{'update.set'} ||= '_render_update_clause_set';
Condition50%1/2
$opts{"render_clause"}{"update.set"} ||= "_render_update_clause_set"
l!l
023
MC/DC0%0/1
$opts{"render_clause"}{"update.set"} ||= "_render_update_clause_set"
$opts{"render_clause"}{"update.set"}
Truth table50%1/2
A
A= $opts{"render_clause"}{"update.set"}
Aresult
11
00
35  
36 23 return $self->next::method(\%opts);
37 }
38  
39  
40 21 ▶sub update {
Subroutine update: called CC=4 SCAR=13.9
Pod documented
41 21 my ($self, $table, $set, $where, $options) = @_;
42  
43 21 $self->_assert_mutation_target(update => $table);
44  
45 20 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set eq "HASH" and keys %$set 2 18 20
Condition100%3/3
ref $set eq "HASH" and keys %$set
!ll&&!rl&&r
1118
MC/DC100%2/2
ref $set eq "HASH" and keys %$set
ref $set eq "HASH" keys %$set
Truth table100%3/3
A and B
A= ref $set eq "HASH"B= keys %$set
ABresult
0X0
100
111
46 'An UPDATE on ClickHouse renders as ALTER TABLE ... UPDATE col = ..., '
47 . 'which has no form without an assignment: update() needs a non-empty '
48 . 'hashref of columns to set'
49 ) unless ref $set eq 'HASH' and keys %$set;
50  
51 18 my ($sql, @bind) = $self->next::method(
52 $table, $set, $self->_mutation_where($where), $options
53 );
54 18 $sql .= $self->_mutations_sync_clause;
55  
56 18 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 0 18
57 }
58  
59  
60 24 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.0
Pod documented
61 24 my ($self, $table, $where, $options) = @_;
62  
63 24 $self->_assert_mutation_target(delete => $table);
64  
65 21 my ($sql, @bind) = $self->next::method(
66 $table, $self->_mutation_where($where), $options
67 );
68 21 $sql .= $self->_mutations_sync_clause;
69  
70 21 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 21 0 21
71 }
72  
73 # ALTER TABLE names exactly one real table. Everything else core can hand a
74 # storage as an "ident" -- a from-list, a virtual view's literal SQL -- has no
75 # ALTER form at all, and would otherwise reach the server as nonsense.
76 45 ▶sub _assert_mutation_target {
Subroutine _assert_mutation_target: called CC=2 SCAR=6.9
77 45 my ($self, $op, $table) = @_;
78  
79 45 ▶ return unless my $ref = ref $table;
Branch: 1 branch
BranchTrueFalseTotal
unless my $ref = ref $table 41 4 45
80  
81 4 $self->throw_exception(sprintf
82 'ClickHouse renders %s as ALTER TABLE ... %s, which can only name a single '
83 . 'real table -- but this one was given a %s target (a virtual view or other '
84 . 'literal-SQL result source, or a join list). Run the %s against the '
85 . 'underlying MergeTree table instead; if the rows are identified through '
86 . 'another table, keep the target table as the result source and put the '
87 . 'other one in the WHERE clause as a subquery, e.g. '
88 . q{$rs->search({ id => { -in => $other_rs->get_column('id')->as_query } })},
89 uc($op), uc($op), $ref, $op
90 );
91 }
92  
93 # A ClickHouse mutation must carry a filter expression: 'ALTER TABLE t DELETE'
94 # is a syntax error, not a delete-everything. WHERE 1 is how ClickHouse spells
95 # what an unrestricted $rs->delete means everywhere else.
96 39 ▶sub _mutation_where {
Subroutine _mutation_where: called CC=13 SCAR=27.1
97 39 my ($self, $where) = @_;
98  
99 39 ▶ return \'1' unless defined $where;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where 1 38 39
100  
101 38 my $ref = ref $where;
102  
103 38 ▶ return \'1' if $ref eq 'HASH' and ! keys %$where;
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" and not keys %$where 1 37 38
Condition100%3/3
$ref eq "HASH" and not keys %$where
!ll&&!rl&&r
9281
MC/DC100%2/2
$ref eq "HASH" and not keys %$where
$ref eq "HASH" not keys %$where
Truth table100%3/3
A and not(B)
A= $ref eq "HASH"B= not keys %$where
ABresult
0X0
100
111
104 37 ▶ return \'1' if $ref eq 'ARRAY' and ! @$where;
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" and not @$where 1 36 37
Condition66%2/3
$ref eq "ARRAY" and not @$where
!ll&&!rl&&r
3601
MC/DC50%1/2
$ref eq "ARRAY" and not @$where
$ref eq "ARRAY" not @$where
Truth table66%2/3
A and not(B)
A= $ref eq "ARRAY"B= not @$where
ABresult
0X0
100
111
105  
106 # \'literal SQL'
107 return \'1' if $ref eq 'SCALAR'
108 0 ▶ and (! defined ${$where} or ${$where} !~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "SCALAR" and not defined ${$where;} && ${$where;} =~ /\S/ 0 36 36
Condition16%1/6
$ref eq "SCALAR" and not defined ${$where;} && ${$where;} =~ /\S/
!ll&&!rl&&r
3600
defined ${$where;} && ${$where;} =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/3
$ref eq "SCALAR" and not defined ${$where;} && ${$where;} =~ /\S/
$ref eq "SCALAR" defined ${$where;} ${$where;} =~ /\S/
Truth table25%1/4
A and not(B && C)
A= $ref eq "SCALAR"B= defined ${$where;}C= ${$where;} =~ /\S/
ABCresult
0XX0
1110
10X1
1101
109  
110 # \[ $sql, @bind ] -- the shape DBIO::ResultSet::_rs_update_delete builds,
111 # which is empty SQL for a resultset with no condition.
112 return \'1' if $ref eq 'REF'
113 8 ▶ and ref ${$where} eq 'ARRAY'
Condition100%3/3
defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
!ll&&!rl&&r
116
MC/DC100%2/2
defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
defined ${$where;}->[0] ${$where;}->[0] =~ /\S/
Truth table100%3/3
A && B
A= defined ${$where;}->[0]B= ${$where;}->[0] =~ /\S/
ABresult
0X0
100
111
114 36 ▶ and (! defined ${$where}->[0] or ${$where}->[0] !~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "REF" and ref ${$where;} eq "ARRAY" and not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/ 2 34 36
Condition83%5/6
$ref eq "REF" and ref ${$where;} eq "ARRAY" and not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
!ll&&!rl&&r
2862
$ref eq "REF" and ref ${$where;} eq "ARRAY"
!ll&&!rl&&r
2808
MC/DC66%2/3
$ref eq "REF" and ref ${$where;} eq "ARRAY" and not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
$ref eq "REF" ref ${$where;} eq "ARRAY" not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
Truth table75%3/4
A and B and not(C)
A= $ref eq "REF"B= ref ${$where;} eq "ARRAY"C= not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
ABCresult
0XX0
10X0
1100
1111
115  
116 34 return $where;
117 }
118  
119 # Setting *names* are case-sensitive in ClickHouse, so only the keyword goes
120 # through _sqlcase. The value is 0/1/2, validated in new().
121 39 ▶sub _mutations_sync_clause {
Subroutine _mutations_sync_clause: called CC=3 SCAR=11.0
122 39 my $self = shift;
123  
124 39 ▶ my $sync = exists $self->{mutations_sync} ? $self->{mutations_sync} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'mutations_sync'} ? : 39 0 39
125 39 ▶ return '' unless defined $sync;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sync 3 36 39
126  
127 36 return $self->_sqlcase(' settings ') . "mutations_sync = $sync";
128 }
129  
130  
131 18 ▶sub _render_update_clause_target {
Subroutine _render_update_clause_target: called CC=1 SCAR=0.0
132 18 my ($self, undef, $target) = @_;
133 18 return $self->join_query_parts(' ',
134 { -keyword => 'alter_table' }, $target, { -keyword => 'update' }
135 );
136 }
137  
138  
139 18 ▶sub _render_update_clause_set {
Subroutine _render_update_clause_set: called CC=1 SCAR=0.0
140 18 my ($self, undef, $set) = @_;
141 18 return $self->render_aqt($set, 1);
142 }
143  
144  
145 21 ▶sub _render_delete_clause_target {
Subroutine _render_delete_clause_target: called CC=1 SCAR=0.0
146 21 my ($self, undef, $target) = @_;
147 21 return $self->join_query_parts(' ',
148 { -keyword => 'alter_table' }, $target, { -keyword => 'delete' }
149 );
150 }
151  
152  
153 22 ▶sub insert_multi {
Subroutine insert_multi: called CC=8 SCAR=20.8
Pod documented
154 22 my ($self, $table, $cols, $rows) = @_;
155  
156 22 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cols eq "ARRAY" and @$cols 1 21 22
Condition66%2/3
ref $cols eq "ARRAY" and @$cols
!ll&&!rl&&r
0121
MC/DC50%1/2
ref $cols eq "ARRAY" and @$cols
ref $cols eq "ARRAY" @$cols
Truth table66%2/3
A and B
A= ref $cols eq "ARRAY"B= @$cols
ABresult
0X0
100
111
157 'insert_multi() needs a non-empty arrayref of column names'
158 ) unless ref $cols eq 'ARRAY' and @$cols;
159  
160 21 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" and @$rows 2 19 21
Condition100%3/3
ref $rows eq "ARRAY" and @$rows
!ll&&!rl&&r
1119
MC/DC100%2/2
ref $rows eq "ARRAY" and @$rows
ref $rows eq "ARRAY" @$rows
Truth table100%3/3
A and B
A= ref $rows eq "ARRAY"B= @$rows
ABresult
0X0
100
111
161 'insert_multi() needs a non-empty arrayref of rows'
162 ) unless ref $rows eq 'ARRAY' and @$rows;
163  
164 19 my @tuples;
165 19 for my $idx (0 .. $#$rows) {
166 34 my $row = $rows->[$idx];
167  
168 34 ▶ $self->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 1 33 34
169 'insert_multi() row %d is not an arrayref: rows are positional and '
170 . 'must line up with the %d supplied column names (%s)',
171 $idx, scalar @$cols, join ', ', @$cols
172 ) unless ref $row eq 'ARRAY';
173  
174 33 ▶ $self->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless @$row == @$cols 2 31 33
175 'insert_multi() row %d has %d value(s) but there are %d columns (%s): '
176 . 'a multi-row INSERT is a single statement with a single column list, so '
177 . 'every row must fill all of them',
178 $idx, scalar @$row, scalar @$cols, join ', ', @$cols
179 ) unless @$row == @$cols;
180  
181 push @tuples, { -row => [
182 31 map { $self->_insert_multi_value($cols->[$_], $row->[$_]) } 0 .. $#$cols
183 ] };
184 }
185  
186 14 return $self->render_statement({ -insert => {
187 target => $table,
188 fields => [ @$cols ],
189 from => { -values => \@tuples }
190 }});
191 }
192  
193 # One VALUES cell as an SQL::Abstract AQT node.
194 66 ▶sub _insert_multi_value {
Subroutine _insert_multi_value: called CC=5 SCAR=16.1
195 66 my ($self, $col, $value) = @_;
196  
197 66 my $ref = ref $value;
198  
199 # \'literal SQL'
200 66 ▶ return { -literal => [ $$value ] } if $ref eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "SCALAR" 1 65 66
201  
202 # \[ 'literal SQL with ?', @binds ]
203 65 ▶ return { -literal => [ @$$value ] } if $ref eq 'REF' and ref $$value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "REF" and ref $$value eq "ARRAY" 2 63 65
Condition66%2/3
$ref eq "REF" and ref $$value eq "ARRAY"
!ll&&!rl&&r
6302
MC/DC50%1/2
$ref eq "REF" and ref $$value eq "ARRAY"
$ref eq "REF" ref $$value eq "ARRAY"
Truth table66%2/3
A and B
A= $ref eq "REF"B= ref $$value eq "ARRAY"
ABresult
0X0
100
111
204  
205 # A bare arrayref is never a value. ClickHouse does have Array(T) columns,
206 # which makes this a likely mistake rather than an exotic one, and no
207 # MySQL-wire DBD can bind a Perl list -- so say what to write instead.
208 63 ▶ $self->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 2 61 63
209 q{insert_multi(): column '%s' was given a plain arrayref. There is no bind }
210 . 'type for a list: write an Array(T) value as literal SQL with binds, '
211 . q{\[ '[?, ?]', $a, $b ], or hand in the ClickHouse array literal as a }
212 . 'string'
213 , $col) if $ref eq 'ARRAY';
214  
215 61 return { -bind => [ $col, $value ] };
216 }
217  
218  
219 442 ▶sub _render_bind {
Subroutine _render_bind: called CC=5 SCAR=16.1
220 442 my ($self, $type, $bind) = @_;
221 442 my ($col, $value) = @$bind;
222  
223 442 ▶ if (defined $value and !ref $value and $value =~ /\x1A/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and $value =~ /\x1A/) 6 436 442
Condition83%5/6
defined $value and not ref $value and $value =~ /\x1A/
!ll&&!rl&&r
44326
defined $value and not ref $value
!ll&&!rl&&r
40438
MC/DC66%2/3
defined $value and not ref $value and $value =~ /\x1A/
defined $value not ref $value $value =~ /\x1A/
Truth table75%3/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\x1A/
ABCresult
0XX0
10X0
1100
1111
224 6 my $n = 0;
225 6 ▶ $n++ while index($value, "ch1a${n}x") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
index($value, "ch1a${n}x") != -1 and do { ++$n } 1 6 7
226 6 my $marker = "ch1a${n}x";
227 6 (my $marked = $value) =~ s/\x1A/$marker/g;
228 6 return [ "replaceAll(?, '$marker', char(26))", $self->_bindtype($col, $marked) ];
229 }
230  
231 436 return $self->next::method($type, $bind);
232 }
233  
234  
235 59 ▶sub select {
Subroutine select: called CC=3 SCAR=11.0
Pod documented
236 59 my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_;
237  
238 59 ▶ my $ch = ref $rs_attrs eq 'HASH' ? $rs_attrs : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $rs_attrs eq 'HASH' ? : 58 1 59
239 59 ▶ local $self->{_clickhouse_final} = $ch->{final} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ch->{'final'} ? : 5 54 59
240 59 local $self->{_clickhouse_prewhere} = $ch->{prewhere};
241  
242 59 return $self->next::method($table, $fields, $where, $rs_attrs, @rest);
243 }
244  
245  
246 53 ▶sub _assemble_binds {
Subroutine _assemble_binds: called CC=2 SCAR=6.9
247 53 my $self = shift;
248 53 ▶ return map { @{ (delete $self->{"${_}_bind"}) || [] } } (
Condition100%2/2
delete $$self{"${_}_bind"} || []
l!l
133397
MC/DC100%1/1
delete $$self{"${_}_bind"} || []
delete $$self{"${_}_bind"}
Truth table100%2/2
A
A= delete $$self{"${_}_bind"}
Aresult
11
00
249 qw/pre_select select from clickhouse_prewhere where group having order clickhouse_limit_by limit/
250 );
251 }
252  
253  
254 59 ▶sub _table {
Subroutine _table: called CC=4 SCAR=14.0
255 59 my $self = shift;
256  
257 59 my $sql = $self->next::method(@_);
258  
259 # A from-list goes through _gen_from_blocks, which has already attached FINAL
260 # to the primary block and consumed the flag; a bare table name does not, so
261 # it is picked up here instead.
262 59 $sql .= $self->_clickhouse_final_sql;
263  
264 59 my $prewhere = $self->{_clickhouse_prewhere};
265 59 ▶ if (defined $prewhere) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prewhere) 4 55 59
266 4 my ($frag, @bind) = $self->_recurse_where($prewhere);
267 4 ▶ if (defined $frag and length $frag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $frag and length $frag) 4 0 4
Condition33%1/3
defined $frag and length $frag
!ll&&!rl&&r
004
MC/DC0%0/2
defined $frag and length $frag
defined $frag length $frag
Truth table33%1/3
A and B
A= defined $fragB= length $frag
ABresult
0X0
100
111
268 4 push @{$self->{clickhouse_prewhere_bind}}, @bind;
269 4 $sql .= $self->_sqlcase(' prewhere ') . $frag;
270 }
271 }
272  
273 59 return $sql;
274 }
275  
276  
277 57 ▶sub _gen_from_blocks {
Subroutine _gen_from_blocks: called CC=3 SCAR=11.0
278 57 my $self = shift;
279  
280 # Consumed *before* recursing: _recurse_from re-enters this method for nested
281 # join groups, and FINAL belongs to the primary table of the outermost list
282 # only.
283 57 my $final = $self->_clickhouse_final_sql;
284  
285 57 my @blocks = $self->next::method(@_);
286 57 ▶ $blocks[0] .= $final if @blocks and length $final;
Branch: 1 branch
BranchTrueFalseTotal
if @blocks and length $final 5 52 57
Condition66%2/3
@blocks and length $final
!ll&&!rl&&r
0525
MC/DC50%1/2
@blocks and length $final
@blocks length $final
Truth table66%2/3
A and B
A= @blocksB= length $final
ABresult
0X0
100
111
287  
288 57 return @blocks;
289 }
290  
291 # Returns ' FINAL' the first time it is asked within one statement, '' after
292 # that -- the modifier applies to exactly one table reference.
293 116 ▶sub _clickhouse_final_sql {
Subroutine _clickhouse_final_sql: called CC=2 SCAR=6.9
294 116 my $self = shift;
295 116 ▶ return '' unless $self->{_clickhouse_final};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_clickhouse_final"} 111 5 116
296 5 $self->{_clickhouse_final} = 0;
297 5 return $self->_sqlcase(' final');
298 }
299  
300  
301 62 ▶sub _parse_rs_attrs {
Subroutine _parse_rs_attrs: called CC=3 SCAR=11.0
302 62 my $self = shift;
303 62 my ($rs_attrs) = @_;
304  
305 62 my $sql = $self->next::method(@_);
306  
307 62 ▶ if (ref $rs_attrs eq 'HASH' and defined $rs_attrs->{limit_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rs_attrs eq "HASH" and defined $rs_attrs->{"limit_by"}) 9 53 62
Condition66%2/3
ref $rs_attrs eq "HASH" and defined $rs_attrs->{"limit_by"}
!ll&&!rl&&r
0539
MC/DC50%1/2
ref $rs_attrs eq "HASH" and defined $rs_attrs->{"limit_by"}
ref $rs_attrs eq "HASH" defined $rs_attrs->{"limit_by"}
Truth table66%2/3
A and B
A= ref $rs_attrs eq "HASH"B= defined $rs_attrs->{"limit_by"}
ABresult
0X0
100
111
308 9 my ($limit_by_sql, @limit_by_bind) = $self->_limit_by($rs_attrs->{limit_by});
309 3 push @{$self->{clickhouse_limit_by_bind}}, @limit_by_bind;
310 3 $sql .= $limit_by_sql;
311 }
312  
313 56 return $sql;
314 }
315  
316  
317 9 ▶sub _limit_by {
Subroutine _limit_by: called CC=8 SCAR=20.8
318 9 my ($self, $spec) = @_;
319  
320 9 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 1 8 9
321 q{The 'limit_by' attribute must be a hashref of { rows => $n, by => $columns }}
322 ) unless ref $spec eq 'HASH';
323  
324 8 my $rows = $spec->{rows};
325 8 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rows or $rows =~ /\D/ or $rows <= 0 3 5 8
Condition100%6/6
not defined $rows or $rows =~ /\D/ or $rows <= 0
l!l&&r!l&&!r
215
not defined $rows or $rows =~ /\D/
l!l&&r!l&&!r
116
MC/DC100%3/3
not defined $rows or $rows =~ /\D/ or $rows <= 0
not defined $rows $rows =~ /\D/ $rows <= 0
Truth table100%4/4
not(A) or B or C
A= not defined $rowsB= $rows =~ /\D/C= $rows <= 0
ABCresult
1XX1
01X1
0011
0000
326 q{The 'limit_by' attribute needs a positive integer 'rows' value}
327 ) if ! defined $rows or $rows =~ /\D/ or $rows <= 0;
328  
329 $self->throw_exception(
330 q{The 'limit_by' attribute needs a 'by' column specification}
331 5 ▶ ) unless defined $spec->{by};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"by"} 1 4 5
332  
333 4 my ($by_sql, @bind) = $self->_recurse_fields($spec->{by});
334 4 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $by_sql and length $by_sql 1 3 4
Condition66%2/3
defined $by_sql and length $by_sql
!ll&&!rl&&r
013
MC/DC50%1/2
defined $by_sql and length $by_sql
defined $by_sql length $by_sql
Truth table66%2/3
A and B
A= defined $by_sqlB= length $by_sql
ABresult
0X0
100
111
335 q{The 'limit_by' attribute needs a 'by' column specification}
336 ) unless defined $by_sql and length $by_sql;
337  
338 return (
339 3 $self->_sqlcase(' limit ') . $rows . $self->_sqlcase(' by ') . $by_sql,
340 @bind
341 );
342 }
343  
344  
345 12 1;
346  
347 __END__
« ClickHouse/Introspect.pm ↑ Summary ClickHouse/Introspect/Indexes.pm »