ClickHouse/DDL.pm

filter: Statementstmt 97.3% 261 / 268 Branchbran 82.9% 131 / 158 Conditioncond 69.9% 100 / 143 MC/DCmcdc 51.8% 42 / 81 Subroutinesub 100% 26 / 26 Podpod 100% 6 / 6 totaltotal 82.9% 566 / 682 CC 148 SCAR 28.8
CC
148
Cov
86%
_refuse_unbuildable
33.7
_skipping_index_defs
30.9
_clickhouse_base_type
22.1
CRAP 17.8
« ClickHouse/Test/Conformance.pm ↑ Summary ClickHouse/Result.pm »
1 package DBIO::ClickHouse::DDL;
2 # ABSTRACT: Generate ClickHouse DDL from DBIO Result classes
3  
4 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 53 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 53 our $VERSION = '0.900007';
8  
9 53 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 53 ▶use DBIO::ClickHouse::DDL::Emit qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 quote_ident quote_string column_def index_def create_table create_view
12 engine_clause partition_by_clause primary_key_clause order_by_clause
13 settings_clause split_table_name
14 53 );
15  
16  
17  
18 51 ▶sub install_ddl {
Subroutine install_ddl: called CC=18 SCAR=28.9
Pod documented
19 51 my ($class, $schema, $opts) = @_;
20 51 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
348
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
21  
22 51 $class->_refuse_unbuildable($schema);
23  
24 38 my @stmts;
25 my %seen_table;
26  
27 38 for my $source_name (sort $schema->sources) {
28 80 my $source = $schema->source($source_name);
29 80 my $result_class = $source->result_class;
30  
31 # Views are emitted after all tables, below.
32 80 ▶ next if $source->isa('DBIO::ResultSource::View');
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIO::ResultSource::View") 26 54 80
33  
34 54 my $table_name = _resolve_table_name($source->name);
35  
36 # Virtual source whose name is inline SQL: nothing to create.
37 54 ▶ next unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 54 54
38  
39 # A table of another database is made there, and only by the install of
40 # the schema itself: the scratch database of a diff leaves it out.
41 54 my ($table_database, $table_short) = split_table_name($table_name);
42 54 ▶ next if defined $table_database && $opts->{local_only};
Branch: 1 branch
BranchTrueFalseTotal
if defined $table_database and $opts->{"local_only"} 2 52 54
Condition100%3/3
defined $table_database and $opts->{"local_only"}
!ll&&!rl&&r
5112
MC/DC100%2/2
defined $table_database and $opts->{"local_only"}
defined $table_database $opts->{"local_only"}
Truth table100%3/3
A and B
A= defined $table_databaseB= $opts->{"local_only"}
ABresult
0X0
100
111
43  
44 # Multiple Result classes may share one physical table; emit it once.
45 52 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 0 52 52
46  
47 52 my @col_defs;
48 52 for my $col_name ($source->columns) {
49 163 my $info = $source->column_info($col_name);
50 163 push @col_defs, column_def(
51 name => $col_name,
52 type => _clickhouse_column_type($info),
53 default => _column_default($info)
54 );
55 }
56  
57 52 push @col_defs, _skipping_index_defs($source);
58  
59 40 my $opts = _table_options($result_class);
60  
61 # The sorting key defaults to the DBIO primary key. A source with neither
62 # an explicit order_by nor a primary key gets ORDER BY tuple().
63 my @order_by = defined $opts->{order_by}
64 40 ▶ ? @{ $opts->{order_by} }
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'order_by'} ? : 4 36 40
65 : $source->primary_columns;
66  
67 40 my @clauses = (engine_clause($opts->{engine}));
68 push @clauses, partition_by_clause($opts->{partition_by})
69 40 ▶ if defined $opts->{partition_by};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"partition_by"} 3 37 40
70 1 push @clauses, primary_key_clause(@{ $opts->{primary_key} })
71 40 ▶ if defined $opts->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"primary_key"} 1 39 40
72 40 push @clauses, order_by_clause(@order_by);
73 40 my $settings = settings_clause($opts->{settings});
74 40 ▶ push @clauses, $settings if defined $settings;
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings 2 38 40
75  
76 40 ▶ push @stmts, create_table(
Branch: 1 branch
BranchTrueFalseTotal
defined $table_database ? : 1 39 40
77 (defined $table_database ? (database => $table_database) : ()),
78 name => $table_short,
79 columns => \@col_defs,
80 clauses => \@clauses
81 );
82 }
83  
84 # A view of another database is made there too, and only by the install of
85 # the schema itself, like such a table.
86 26 my @view_stmts;
87 26 for my $pair ($class->_in_view_order($class->_declared_views($schema))) {
88 18 my ($view_database, $view_short) = split_table_name($pair->[0]);
89 18 ▶ next if defined $view_database && $opts->{local_only};
Branch: 1 branch
BranchTrueFalseTotal
if defined $view_database and $opts->{"local_only"} 1 17 18
Condition100%3/3
defined $view_database and $opts->{"local_only"}
!ll&&!rl&&r
1611
MC/DC100%2/2
defined $view_database and $opts->{"local_only"}
defined $view_database $opts->{"local_only"}
Truth table100%3/3
A and B
A= defined $view_databaseB= $opts->{"local_only"}
ABresult
0X0
100
111
90 17 ▶ push @view_stmts, create_view(
Branch: 1 branch
BranchTrueFalseTotal
defined $view_database ? : 1 16 17
91 (defined $view_database ? (database => $view_database) : ()),
92 name => $view_short,
93 definition => $pair->[1],
94 );
95 }
96  
97 24 return join "\n\n", @stmts, @view_stmts;
98 }
99  
100  
101 51 ▶sub _refuse_unbuildable {
Subroutine _refuse_unbuildable: called CC=28 SCAR=35.2
102 51 my ($class, $schema) = @_;
103  
104 51 my (@problems, %seen);
105 51 for my $source_name (sort $schema->sources) {
106 97 my $source = $schema->source($source_name);
107 97 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 29 68 97
108 29 my $view = _resolve_table_name($source->name);
109 push @problems, sprintf
110 "view '%s' is refused: a view name is view or database.view, with "
111 . "both halves given and one dot", $view
112 29 ▶ if defined $view && !$source->is_virtual && !$seen{"view $view"}++
Branch: 1 branch
BranchTrueFalseTotal
if defined $view and not $source->is_virtual and not $seen{"view $view"}++ and $view =~ /\./ and not $view =~ /\A[^.]+\.[^.]+\z/ 3 26 29
Condition58%7/12
defined $view and not $source->is_virtual and not $seen{"view $view"}++ and $view =~ /\./ and not $view =~ /\A[^.]+\.[^.]+\z/
!ll&&!rl&&r
2423
defined $view and not $source->is_virtual and not $seen{"view $view"}++ and $view =~ /\./
!ll&&!rl&&r
0245
defined $view and not $source->is_virtual and not $seen{"view $view"}++
!ll&&!rl&&r
0029
defined $view and not $source->is_virtual
!ll&&!rl&&r
0029
MC/DC40%2/5
defined $view and not $source->is_virtual and not $seen{"view $view"}++ and $view =~ /\./ and not $view =~ /\A[^.]+\.[^.]+\z/
defined $view not $source->is_virtual not $seen{"view $view"}++ $view =~ /\./ not $view =~ /\A[^.]+\.[^.]+\z/
Truth table50%3/6
A and not(B) and not(C) and D and not(E)
A= defined $viewB= not $source->is_virtualC= not $seen{"view $view"}++D= $view =~ /\./E= not $view =~ /\A[^.]+\.[^.]+\z/
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
113 && $view =~ /\./ && $view !~ /\A[^.]+\.[^.]+\z/;
114 29 next;
115 }
116 68 my $table = _resolve_table_name($source->name);
117 68 ▶ next unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 68 68
118 68 ▶ next if $seen{$table}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$table}++ 0 68 68
119  
120 68 ▶ push @problems, sprintf
Branch: 1 branch
BranchTrueFalseTotal
if $table =~ /\./ and not $table =~ /\A[^.]+\.[^.]+\z/ 3 65 68
Condition100%3/3
$table =~ /\./ and not $table =~ /\A[^.]+\.[^.]+\z/
!ll&&!rl&&r
6233
MC/DC100%2/2
$table =~ /\./ and not $table =~ /\A[^.]+\.[^.]+\z/
$table =~ /\./ not $table =~ /\A[^.]+\.[^.]+\z/
Truth table100%3/3
A and not(B)
A= $table =~ /\./B= not $table =~ /\A[^.]+\.[^.]+\z/
ABresult
0X0
100
111
121 "table '%s' is refused: a table name is table or database.table, with "
122 . "both halves given and one dot", $table
123 if $table =~ /\./ && $table !~ /\A[^.]+\.[^.]+\z/;
124  
125 # A refusal names the table's columns with it: the text is then the one
126 # place that shows which of them the refusal sits on, and every problem of
127 # the table stands in the same exception.
128 68 my $columns = join ', ', $source->columns;
129  
130 68 for my $col ($source->columns) {
131 190 my $info = $source->column_info($col);
132  
133 push @problems, sprintf
134 "table '%s' (columns %s): column '%s' is auto-increment, which ClickHouse cannot "
135 . "build -- it has no auto-increment and no identity column "
136 . "(https://clickhouse.com/docs/sql-reference/statements/create/table); "
137 . "supply the key yourself or declare it data_type => 'uuid', "
138 . "is_auto_increment => 1, which gets DEFAULT generateUUIDv4()",
139 $table, $columns, $col
140 190 ▶ if $info->{is_auto_increment} && !_is_uuid_type($info);
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_auto_increment"} and not _is_uuid_type($info) 2 188 190
Condition100%3/3
$info->{"is_auto_increment"} and not _is_uuid_type($info)
!ll&&!rl&&r
168202
MC/DC100%2/2
$info->{"is_auto_increment"} and not _is_uuid_type($info)
$info->{"is_auto_increment"} not _is_uuid_type($info)
Truth table100%3/3
A and not(B)
A= $info->{"is_auto_increment"}B= not _is_uuid_type($info)
ABresult
0X0
100
111
141  
142 push @problems, sprintf
143 "table '%s' (columns %s): column '%s': size '%s' on type 'bit' is refused: "
144 . "ClickHouse has no BIT type, so the width would be lost "
145 . "(https://clickhouse.com/docs/sql-reference/data-types); "
146 . "use UInt8/UInt16/UInt32/UInt64 or Bool, or a native type spelled out",
147 0 $table, $columns, $col, ref $info->{size} ? join(',', @{ $info->{size} }) : $info->{size}
148 190 ▶ if lc($info->{data_type} // '') eq 'bit' && defined $info->{size};
Branch: 2 branches
BranchTrueFalseTotal
if lc($info->{"data_type"} // "") eq "bit" and defined $info->{"size"} 1 189 190
ref $info->{'size'} ? : 0 1 1
Condition60%3/5
lc($info->{"data_type"} // "") eq "bit" and defined $info->{"size"}
!ll&&!rl&&r
18901
$info->{"data_type"} // ""
l!l
1900
MC/DC0%0/2
lc($info->{"data_type"} // "") eq "bit" and defined $info->{"size"}
$info->{"data_type"} defined $info->{"size"}
Truth table33%1/3
A and B
A= $info->{"data_type"}B= defined $info->{"size"}
ABresult
0X0
100
111
149 }
150  
151 68 ▶ my %unique = $source->can('unique_constraints') ? $source->unique_constraints : ();
Branch: 1 branch
BranchTrueFalseTotal
$source->can('unique_constraints') ? : 68 0 68
152 68 for my $name (sort grep { $_ ne 'primary' } keys %unique) {
153 push @problems, sprintf
154 "table '%s' (columns %s): unique constraint '%s' on (%s) cannot be enforced -- "
155 . "ClickHouse has no unique constraint and does not check a key before "
156 . "inserting (https://clickhouse.com/docs/guides/developer/deduplication); "
157 . "deduplicate with ReplacingMergeTree through clickhouse_table instead",
158 2 $table, $columns, $name, join ', ', @{ $unique{$name} };
159 }
160  
161 68 for my $rel (sort $source->relationships) {
162 10 my $info = $source->relationship_info($rel);
163 10 ▶ next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"} 7 3 10
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
073
$info and $info->{"attrs"}
!ll&&!rl&&r
0010
MC/DC33%1/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"} $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table50%2/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
ABCresult
0XX0
10X0
1100
1111
164 3 my $cond = $info->{cond};
165 my @cols = ref $cond eq 'HASH'
166 3 ▶ ? sort map { s/^self\.//r } values %$cond
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'HASH' ? : 3 0 3
167 : ();
168 3 ▶ push @problems, sprintf
Condition50%1/2
join(', ', @cols) || '?'
l!l
30
MC/DC0%0/1
join(', ', @cols) || '?'
join(', ', @cols)
Truth table50%1/2
A
A= join(', ', @cols)
Aresult
11
00
169 "table '%s' (columns %s): relationship '%s' (column %s) is a foreign key constraint, "
170 . "which ClickHouse cannot create -- its CREATE TABLE has no foreign key "
171 . "clause (https://clickhouse.com/docs/sql-reference/statements/create/table); "
172 . "declare the relationship with is_foreign_key_constraint => 0",
173 $table, $columns, $rel, join(', ', @cols) || '?';
174 }
175 }
176 51 ▶ return unless @problems;
Branch: 1 branch
BranchTrueFalseTotal
unless @problems 38 13 51
177  
178 DBIO::Exception->throw(
179 "DBIO::ClickHouse::DDL refuses to install, no statement was executed:\n"
180 13 . join("\n", map { " - $_" } @problems)
181 );
182 }
183  
184  
185 5 ▶sub qualified_tables {
Subroutine qualified_tables: called CC=9 SCAR=22.0
Pod documented
186 5 my ($class, $schema) = @_;
187 5 my (@out, %seen);
188 5 for my $source_name (sort $schema->sources) {
189 16 my $source = $schema->source($source_name);
190 16 ▶ next if $source->isa('DBIO::ResultSource::View');
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIO::ResultSource::View") 4 12 16
191 12 my $name = _resolve_table_name($source->name);
192 12 ▶ next unless defined $name && $name =~ /\A[^.]+\.[^.]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ /\A[^.]+\.[^.]+\z/ 8 4 12
Condition66%2/3
defined $name and $name =~ /\A[^.]+\.[^.]+\z/
!ll&&!rl&&r
084
MC/DC50%1/2
defined $name and $name =~ /\A[^.]+\.[^.]+\z/
defined $name $name =~ /\A[^.]+\.[^.]+\z/
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /\A[^.]+\.[^.]+\z/
ABresult
0X0
100
111
193 4 my ($database) = split_table_name($name);
194 4 ▶ push @out, [ $name, $database ] unless $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$name}++ 3 1 4
195 }
196 5 for my $pair ($class->_declared_views($schema)) {
197 3 my $name = $pair->[0];
198 3 ▶ next unless $name =~ /\A[^.]+\.[^.]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[^.]+\.[^.]+\z/ 1 2 3
199 2 my ($database) = split_table_name($name);
200 2 ▶ push @out, [ $name, $database ] unless $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$name}++ 2 0 2
201 }
202 5 return @out;
203 }
204  
205  
206 58 ▶sub view_order {
Subroutine view_order: called CC=4 SCAR=13.9
Pod documented
207 58 my ($class, $defs) = @_;
208 58 my @names = sort keys %$defs;
209 58 my %after;
210 58 for my $name (@names) {
211 49 my $text = _strip_sql_noise($defs->{$name});
212 49 ▶ $after{$name} = [ grep { $_ ne $name && _reads_view($text, $_) } @names ];
Condition100%3/3
$_ ne $name && _reads_view($text, $_)
!ll&&!rl&&r
498119
MC/DC100%2/2
$_ ne $name && _reads_view($text, $_)
$_ ne $name _reads_view($text, $_)
Truth table100%3/3
A && B
A= $_ ne $nameB= _reads_view($text, $_)
ABresult
0X0
100
111
213 }
214 58 my (@out, %state);
215 58 ▶ my $visit; $visit = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
216 61 my ($name, @path) = @_;
217 61 ▶ return if ($state{$name} // 0) == 2;
Branch: 1 branch
BranchTrueFalseTotal
if ($state{$name} // 0) == 2 11 50 61
Condition100%2/2
$state{$name} // 0
l!l
1447
MC/DC100%1/1
$state{$name} // 0
$state{$name}
Truth table100%2/2
A
A= $state{$name}
Aresult
11
00
218 50 ▶ if (($state{$name} // 0) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($state{$name} // 0) == 1) 3 47 50
Condition100%2/2
$state{$name} // 0
l!l
347
MC/DC100%1/1
$state{$name} // 0
$state{$name}
Truth table100%2/2
A
A= $state{$name}
Aresult
11
00
219 3 my ($at) = grep { $path[$_] eq $name } 0 .. $#path;
220 3 my @cycle = @path[$at .. $#path];
221 DBIO::Exception->throw(sprintf
222 'DBIO::ClickHouse refuses the views: they read each other in a cycle (%s), so no order '
223 . 'exists to create them in; no statement was executed',
224 3 join ', ', map { "`$_`" } sort @cycle);
225 }
226 47 $state{$name} = 1;
227 47 $visit->($_, @path, $name) for @{ $after{$name} };
228 39 $state{$name} = 2;
229 39 push @out, $name;
230 58 };
231 58 $visit->($_) for @names;
232 55 return @out;
233 }
234  
235 # The text without string literals and comments, which hold no read.
236 49 ▶sub _strip_sql_noise {
Subroutine _strip_sql_noise: called CC=1 SCAR=0.0
237 49 my ($sql) = @_;
238 49 $sql =~ s{/\*.*?\*/}{ }gs;
239 49 $sql =~ s{--[^\n]*}{ }g;
240 49 $sql =~ s{'(?:[^'\\]|\\.|'')*'}{''}gs;
241 49 return $sql;
242 }
243  
244 # Does the text read the view (FROM or JOIN, then the name)?
245 100 ▶sub _reads_view {
Subroutine _reads_view: called CC=3 SCAR=11.0
246 100 my ($text, $name) = @_;
247 100 ▶ my $id = sub { my ($i) = @_; return '(?:`' . quotemeta($i) . '`|"' . quotemeta($i) . '"|(?<![\w`"])' . quotemeta($i) . '(?![\w`"]))' };
Subroutine __ANON__: called CC=1 SCAR=0.0
248 100 my ($database, $short) = split_table_name($name);
249 100 ▶ my $ref = defined $database
Branch: 1 branch
BranchTrueFalseTotal
defined $database ? : 2 98 100
250 ? $id->($database) . '\s*\.\s*' . $id->($short)
251 : '(?:' . '(?:`[^`]+`|"[^"]+"|\w+)' . '\s*\.\s*)?' . $id->($short);
252 100 ▶ return $text =~ /\b(?:FROM|JOIN)\s+(?:\(\s*)*$ref(?![\w`".(])/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\b(?:FROM|JOIN)\s+(?:\(\s*)*$ref(?![\w`".(])/i ? : 19 81 100
253 }
254  
255 # [name, definition] pairs put in view_order.
256 26 ▶sub _in_view_order {
Subroutine _in_view_order: called CC=1 SCAR=0.0
257 26 my ($class, @pairs) = @_;
258 26 my %def = map { @$_ } @pairs;
259 26 return map { [ $_, $def{$_} ] } $class->view_order(\%def);
260 }
261  
262  
263 3 ▶sub declared_views {
Subroutine declared_views: called CC=3 SCAR=11.0
Pod documented
264 3 my ($class, $schema, $opts) = @_;
265 3 ▶ return { map { @$_ } grep { !($opts && $opts->{local_only}) || $_->[0] !~ /\./ }
Condition83%5/6
($opts && $opts->{'local_only'}) && $_->[0] =~ /\./
!ll&&!rl&&r
211
$opts and $opts->{"local_only"}
!ll&&!rl&&r
202
MC/DC66%2/3
($opts && $opts->{'local_only'}) && $_->[0] =~ /\./
$opts $opts->{"local_only"} $_->[0] =~ /\./
Truth table75%3/4
A and B && C
A= $optsB= $opts->{"local_only"}C= $_->[0] =~ /\./
ABCresult
0XX0
10X0
1100
1111
266 $class->_declared_views($schema) };
267 }
268  
269 # [name, definition] pairs in source-name order, one per physical view.
270 34 ▶sub _declared_views {
Subroutine _declared_views: called CC=10 SCAR=24.6
271 34 my ($class, $schema) = @_;
272 34 my (@views, %seen);
273 34 for my $source_name (sort $schema->sources) {
274 93 my $source = $schema->source($source_name);
275 93 ▶ next unless $source->isa('DBIO::ResultSource::View');
Branch: 1 branch
BranchTrueFalseTotal
unless $source->isa("DBIO::ResultSource::View") 59 34 93
276 34 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 0 34 34
Condition33%1/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
0340
MC/DC0%0/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table33%1/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
277 34 my $vname = _resolve_table_name($source->name);
278 34 ▶ next if !defined $vname || $seen{$vname}++;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $vname or $seen{$vname}++ 1 33 34
Condition66%2/3
not defined $vname or $seen{$vname}++
l!l&&r!l&&!r
0133
MC/DC50%1/2
not defined $vname or $seen{$vname}++
not defined $vname $seen{$vname}++
Truth table66%2/3
not(A) or B
A= not defined $vnameB= $seen{$vname}++
ABresult
1X1
011
000
279 33 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 33 0 33
280 33 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 0 33 33
Condition33%1/3
defined $def and length $def
!ll&&!rl&&r
0033
MC/DC0%0/2
defined $def and length $def
defined $def length $def
Truth table33%1/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
281 33 push @views, [ $vname, $def ];
282 }
283 34 return @views;
284 }
285  
286  
287 2 ▶sub declared_settings {
Subroutine declared_settings: called CC=7 SCAR=20.2
Pod documented
288 2 my ($class, $schema) = @_;
289 2 my %settings;
290 2 for my $source_name (sort $schema->sources) {
291 5 my $source = $schema->source($source_name);
292 5 ▶ next if $source->isa('DBIO::ResultSource::View');
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIO::ResultSource::View") 0 5 5
293 5 my $table_name = _resolve_table_name($source->name);
294 5 ▶ next unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 5 5
295 5 my $declared = _table_options($source->result_class)->{settings};
296 5 ▶ $settings{$table_name} //= $declared
Branch: 1 branch
BranchTrueFalseTotal
if ref $declared eq "HASH" and keys %$declared 1 4 5
Condition50%3/6
ref $declared eq "HASH" and keys %$declared
!ll&&!rl&&r
401
$settings{$table_name} //= $declared
l!l&&r!l&&!r
010
MC/DC50%1/2
ref $declared eq "HASH" and keys %$declared
ref $declared eq "HASH" keys %$declared
MC/DC0%0/2
$settings{$table_name} //= $declared
$settings{$table_name} $declared
Truth table66%2/3
A and B
A= ref $declared eq "HASH"B= keys %$declared
ABresult
0X0
100
111
Truth table33%1/3
A //= B
A= $settings{$table_name}B= $declared
ABresult
1X1
011
000
297 if ref $declared eq 'HASH' && keys %$declared;
298 }
299 2 return \%settings;
300 }
301  
302 # Canonical index declarations as rendered `INDEX ...` body lines. A skipping
303 # index needs a TYPE the engine will not default, so everything that cannot be
304 # said exactly is refused rather than degraded.
305 52 ▶sub _skipping_index_defs {
Subroutine _skipping_index_defs: called CC=25 SCAR=34.3
306 52 my ($source) = @_;
307  
308 52 ▶ my $label = $source->source_name // $source->name;
Condition33%1/3
$source->source_name // $source->name
l!l&&r!l&&!r
5200
MC/DC0%0/2
$source->source_name // $source->name
$source->source_name $source->name
Truth table33%1/3
A // B
A= $source->source_nameB= $source->name
ABresult
1X1
011
000
309 52 ▶ $label = 'source' if ref $label;
Branch: 1 branch
BranchTrueFalseTotal
if ref $label 0 52 52
310  
311 10 ▶ my $refuse = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
312 10 my ($name, $why) = @_;
313 10 $source->throw_exception("$label: index '$name': $why");
314 52 };
315  
316 52 my @defs;
317 52 for my $decl (sort { $a->{name} cmp $b->{name} }
318 $source->index_declarations_for('clickhouse', [qw(type granularity expression)])) {
319 14 my $name = $decl->{name};
320  
321 $refuse->($name, 'ClickHouse has no unique index (a data-skipping index '
322 . 'enforces nothing)')
323 14 ▶ if $decl->{unique};
Branch: 1 branch
BranchTrueFalseTotal
if $decl->{"unique"} 2 12 14
324 $refuse->($name, "index type '$decl->{type}' is not expressible; a "
325 . 'ClickHouse index is clickhouse => { type => ..., granularity => ... }')
326 12 ▶ if defined $decl->{type} && length $decl->{type} && lc $decl->{type} ne 'unique';
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{"type"} and length $decl->{"type"} and lc $decl->{"type"} ne "unique" 0 12 12
Condition33%2/6
defined $decl->{"type"} and length $decl->{"type"} and lc $decl->{"type"} ne "unique"
!ll&&!rl&&r
1200
defined $decl->{"type"} and length $decl->{"type"}
!ll&&!rl&&r
1200
MC/DC0%0/3
defined $decl->{"type"} and length $decl->{"type"} and lc $decl->{"type"} ne "unique"
defined $decl->{"type"} length $decl->{"type"} lc $decl->{"type"} ne "unique"
Truth table25%1/4
A and B and C
A= defined $decl->{"type"}B= length $decl->{"type"}C= lc $decl->{"type"} ne "unique"
ABCresult
0XX0
10X0
1100
1111
327 $refuse->($name, 'the generic options are not expressible in ClickHouse; '
328 . 'use clickhouse => { ... }')
329 if ref $decl->{options} eq 'HASH'
330 1 ▶ ? scalar keys %{ $decl->{options} }
Condition33%1/3
defined $decl->{"options"} and length $decl->{"options"}
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $decl->{"options"} and length $decl->{"options"}
defined $decl->{"options"} length $decl->{"options"}
Truth table33%1/3
A and B
A= defined $decl->{"options"}B= length $decl->{"options"}
ABresult
0X0
100
111
331 12 ▶ : (defined $decl->{options} && length $decl->{options});
Branch: 2 branches
BranchTrueFalseTotal
if ref $decl->{"options"} eq "HASH" ? scalar keys %{$$decl{"options"};} : defined $decl->{"options"} && length $decl->{"options"} 1 11 12
ref $decl->{'options'} eq 'HASH' ? : 1 11 12
332  
333 11 ▶ my $own = ($decl->{driver} || {})->{clickhouse};
Condition50%1/2
$decl->{'driver'} || {}
l!l
110
MC/DC0%0/1
$decl->{'driver'} || {}
$decl->{'driver'}
Truth table50%1/2
A
A= $decl->{'driver'}
Aresult
11
00
334 $refuse->($name, 'has no clickhouse => { type => ... } block; ClickHouse '
335 . 'has no plain index and the TYPE cannot be invented')
336 11 ▶ unless $own && defined $own->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $own and defined $own->{"type"} 5 6 11
Condition100%3/3
$own and defined $own->{"type"}
!ll&&!rl&&r
236
MC/DC100%2/2
$own and defined $own->{"type"}
$own defined $own->{"type"}
Truth table100%3/3
A and B
A= $ownB= defined $own->{"type"}
ABresult
0X0
100
111
337  
338 6 my $type = $own->{type};
339 6 ▶ $refuse->($name, "invalid type '$type'")
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $type and $type =~ /\A\w+(?:\(.+\))?\z/s 0 6 6
Condition33%1/3
not ref $type and $type =~ /\A\w+(?:\(.+\))?\z/s
!ll&&!rl&&r
006
MC/DC0%0/2
not ref $type and $type =~ /\A\w+(?:\(.+\))?\z/s
not ref $type $type =~ /\A\w+(?:\(.+\))?\z/s
Truth table33%1/3
not(A) and B
A= not ref $typeB= $type =~ /\A\w+(?:\(.+\))?\z/s
ABresult
0X0
100
111
340 unless !ref $type && $type =~ /\A\w+(?:\(.+\))?\z/s;
341  
342 6 my $gran = $own->{granularity};
343 6 ▶ $refuse->($name, "granularity must be a positive integer, got '$gran'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $gran and ref $gran || !($gran =~ /\A[1-9][0-9]*\z/) 1 5 6
Condition83%5/6
defined $gran and ref $gran || !($gran =~ /\A[1-9][0-9]*\z/)
!ll&&!rl&&r
231
ref $gran or not $gran =~ /\A[1-9][0-9]*\z/
l!l&&r!l&&!r
013
MC/DC66%2/3
defined $gran and ref $gran || !($gran =~ /\A[1-9][0-9]*\z/)
defined $gran ref $gran not $gran =~ /\A[1-9][0-9]*\z/
Truth table75%3/4
A and B or not(C)
A= defined $granB= ref $granC= not $gran =~ /\A[1-9][0-9]*\z/
ABCresult
0XX0
1000
11X1
1011
344 if defined $gran && (ref $gran || $gran !~ /\A[1-9][0-9]*\z/);
345  
346 5 my $expr;
347 5 ▶ if (defined $own->{expression}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $own->{'expression'}) { } 1 4 5
348 1 $expr = $own->{expression};
349 1 ▶ $refuse->($name, 'expression must be a non-empty string')
Branch: 1 branch
BranchTrueFalseTotal
if ref $expr or not length $expr 0 1 1
Condition33%1/3
ref $expr or not length $expr
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $expr or not length $expr
ref $expr not length $expr
Truth table33%1/3
A or not(B)
A= ref $exprB= not length $expr
ABresult
1X1
011
000
350 if ref $expr || !length $expr;
351 }
352 else {
353 4 my @cols = @{ $decl->{columns} };
354 4 for my $col (@cols) {
355 5 ▶ $refuse->($name, "column '$col' is not a column of the source")
Branch: 1 branch
BranchTrueFalseTotal
unless $source->has_column($col) 1 4 5
356 unless $source->has_column($col);
357 }
358 $expr = @cols == 1
359 ? quote_ident($cols[0])
360 3 ▶ : '(' . join(', ', map { quote_ident($_) } @cols) . ')';
Branch: 1 branch
BranchTrueFalseTotal
@cols == 1 ? : 2 1 3
361 }
362  
363 4 push @defs, index_def({
364 index_name => $name,
365 expr => $expr,
366 type_full => $type,
367 granularity => $gran,
368 });
369 }
370  
371 40 return @defs;
372 }
373  
374  
375 165 ▶sub column_type {
Subroutine column_type: called CC=1 SCAR=0.0
Pod documented
376 165 my ($class, $info) = @_;
377 165 return _clickhouse_column_type($info);
378 }
379  
380 # Table options declared via DBIO::ClickHouse::Result's clickhouse_table,
381 # merged over the engine default. Harmless when the Result component was never
382 # loaded: the accessor simply is not there and every table gets plain
383 # MergeTree + ORDER BY <primary key>.
384 45 ▶sub _table_options {
Subroutine _table_options: called CC=7 SCAR=19.7
385 45 my ($result_class) = @_;
386  
387 45 my %opts = (engine => 'MergeTree');
388 45 ▶ return \%opts unless $result_class && $result_class->can('clickhouse_table');
Branch: 1 branch
BranchTrueFalseTotal
unless $result_class and $result_class->can("clickhouse_table") 40 5 45
Condition66%2/3
$result_class and $result_class->can("clickhouse_table")
!ll&&!rl&&r
0405
MC/DC50%1/2
$result_class and $result_class->can("clickhouse_table")
$result_class $result_class->can("clickhouse_table")
Truth table66%2/3
A and B
A= $result_classB= $result_class->can("clickhouse_table")
ABresult
0X0
100
111
389  
390 5 my $declared = $result_class->clickhouse_table;
391 5 ▶ return \%opts unless ref $declared eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $declared eq "HASH" 0 5 5
392  
393 5 %opts = (%opts, %$declared);
394  
395 # The Result component normalizes these to arrayrefs; a result class that
396 # rolls its own clickhouse_table might not.
397 5 for my $key (qw(order_by primary_key)) {
398 10 ▶ next unless defined $opts{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$key} 4 6 10
399 6 ▶ $opts{$key} = [$opts{$key}] unless ref $opts{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{$key} eq "ARRAY" 0 6 6
400 }
401  
402 5 return \%opts;
403 }
404  
405 202 ▶sub _resolve_table_name {
Subroutine _resolve_table_name: called CC=5 SCAR=28.8
406 202 my ($name) = @_;
407 # Plain string: real table name.
408 202 ▶ return $name unless ref $name;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $name 202 0 202
409 0 ▶ return undef unless ref $name eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $name eq "SCALAR" 0 0 0
410 # SCALAR ref: either a pointer to a plain identifier or inline SQL for a
411 # virtual view. Word-only content is a real identifier; anything else
412 # (whitespace, SELECT, parens) is virtual and is skipped.
413 0 my $v = $$name;
414 0 ▶ return undef unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
415 0 ▶ return $v if $v =~ /\A\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\A\w+\z/ 0 0 0
416 0 return undef;
417 }
418  
419 # DEFAULT fragment for a column, or undef for none.
420 163 ▶sub _column_default {
Subroutine _column_default: called CC=6 SCAR=18.0
421 163 my ($info) = @_;
422  
423 163 ▶ if ($info->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_auto_increment"}) 19 144 163
424 # ClickHouse has no sequences and no identity columns. The one surrogate
425 # identity it does offer natively is a generated UUID; any other
426 # auto-increment column is refused by _refuse_unbuildable before this runs.
427 19 ▶ return 'generateUUIDv4()' if _is_uuid_type($info);
Branch: 1 branch
BranchTrueFalseTotal
if _is_uuid_type($info) 19 0 19
428 0 return undef;
429 }
430  
431 144 ▶ return undef unless defined $info->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"default_value"} 139 5 144
432  
433 5 my $dv = $info->{default_value};
434 5 ▶ return quote_string($dv) unless ref $dv eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dv eq "SCALAR" 3 2 5
435  
436 # The portable spelling of "now". ClickHouse accepts it in a SELECT but
437 # rejects the bare word as a DEFAULT (UNKNOWN_IDENTIFIER), so it is written
438 # as the function; any other literal is the author's own SQL and stays.
439 2 ▶ return 'now()' if $$dv =~ /\A\s*CURRENT_TIMESTAMP\s*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $$dv =~ /\A\s*CURRENT_TIMESTAMP\s*\z/i 1 1 2
440 1 return $$dv;
441 }
442  
443 41 ▶sub _is_uuid_type {
Subroutine _is_uuid_type: called CC=3 SCAR=11.1
444 41 my ($info) = @_;
445 41 ▶ 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 ? : 39 2 41
Condition50%1/2
$info->{"data_type"} // ""
l!l
410
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
446 }
447  
448 # Base type name (no Nullable / LowCardinality wrapping) for one column_info.
449 53 my %TYPE_MAP = (
450 # signed integers -- widths follow the source dialect's width, not a
451 # one-size-fits-all Int64
452 tinyint => 'Int8',
453 smallint => 'Int16',
454 mediumint => 'Int32',
455 int => 'Int32',
456 integer => 'Int32',
457 bigint => 'Int64',
458 serial => 'Int32',
459 bigserial => 'Int64',
460  
461 # floats -- ClickHouse has no unsized FLOAT
462 real => 'Float32',
463 float => 'Float32',
464 double => 'Float64',
465 'double precision' => 'Float64',
466  
467 # strings: ClickHouse's String is an arbitrary-length byte string, so the
468 # whole varchar/text/blob family collapses onto it
469 text => 'String',
470 string => 'String',
471 varchar => 'String',
472 varchar2 => 'String',
473 nvarchar => 'String',
474 char => 'String',
475 character => 'String',
476 nchar => 'String',
477 clob => 'String',
478 tinytext => 'String',
479 mediumtext => 'String',
480 longtext => 'String',
481 ntext => 'String',
482 blob => 'String',
483 bytea => 'String',
484 binary => 'String',
485 varbinary => 'String',
486 tinyblob => 'String',
487 mediumblob => 'String',
488 longblob => 'String',
489 json => 'String',
490 jsonb => 'String',
491 # an enum whose members were not declared has nothing to render as Enum8/16
492 enum => 'String',
493  
494 # temporal
495 date => 'Date32',
496 datetime => 'DateTime64(3)',
497 timestamp => 'DateTime64(3)',
498 timestamptz => "DateTime64(3, 'UTC')",
499 'timestamp with time zone' => "DateTime64(3, 'UTC')",
500 time => 'String',
501  
502 # misc
503 boolean => 'Bool',
504 bool => 'Bool',
505 uuid => 'UUID',
506 guid => 'UUID',
507 uniqueidentifier => 'UUID',
508 money => 'Decimal(19, 4)'
509 );
510  
511 # Native ClickHouse type names that the lowercased %TYPE_MAP lookup would
512 # otherwise swallow. A data_type spelled exactly the way ClickHouse spells the
513 # type itself is native and is never translated -- the same escape hatch the
514 # parameterized types (DateTime64(6), Map(String, UInt64)) already get,
515 # extended to the parameterless ones.
516 #
517 # Only names that COLLIDE with a %TYPE_MAP key belong here; every other native
518 # name (Int128, IPv6, Point) is unmapped and already falls through the lookup
519 # untouched. String / Bool / UUID render identically either way and are listed
520 # so the rule holds without exceptions; Date, DateTime, Time and JSON are the
521 # four where the map and the native name disagree and the native name wins.
522 #
523 # Add an entry whenever a new %TYPE_MAP key collides with a native ClickHouse
524 # type name that it does not itself render to.
525 53 my %NATIVE_TYPE = map { $_ => 1 } qw(String Bool UUID Date DateTime Time JSON);
526  
527 53 my %UNSIGNED_INT = (
528 Int8 => 'UInt8',
529 Int16 => 'UInt16',
530 Int32 => 'UInt32',
531 Int64 => 'UInt64'
532 );
533  
534 # A sizeless decimal cannot be rendered: ClickHouse has no bare Decimal, every
535 # Decimal carries (precision, scale). 38 is the widest precision that still
536 # fits Decimal128; 9 digits of scale is enough for currency and ratios alike.
537 53 my $DEFAULT_DECIMAL = 'Decimal(38, 9)';
538  
539 456 ▶sub _clickhouse_column_type {
Subroutine _clickhouse_column_type: called CC=6 SCAR=17.9
540 456 my ($info) = @_;
541  
542 456 my $type = _clickhouse_base_type($info);
543 456 ▶ my $extra = $info->{extra} // {};
Condition100%2/2
$info->{"extra"} // {}
l!l
93363
MC/DC100%1/1
$info->{"extra"} // {}
$info->{"extra"}
Truth table100%2/2
A
A= $info->{"extra"}
Aresult
11
00
544  
545 # Nullable(T): ClickHouse columns are NOT NULL by default and nullability is
546 # part of the type, not a column constraint. Composite types cannot be
547 # wrapped -- Nullable(Array(T)) is rejected by the server -- so an array
548 # column stays as-is regardless of is_nullable.
549 456 ▶ if ($info->{is_nullable}
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_nullable"} and not $type =~ /\A(?:Nullable|LowCardinality|Array|Tuple|Map|Nested)\(/) 67 389 456
Condition100%3/3
$info->{"is_nullable"} and not $type =~ /\A(?:Nullable|LowCardinality|Array|Tuple|Map|Nested)\(/
!ll&&!rl&&r
386367
MC/DC100%2/2
$info->{"is_nullable"} and not $type =~ /\A(?:Nullable|LowCardinality|Array|Tuple|Map|Nested)\(/
$info->{"is_nullable"} not $type =~ /\A(?:Nullable|LowCardinality|Array|Tuple|Map|Nested)\(/
Truth table100%3/3
A and not(B)
A= $info->{"is_nullable"}B= not $type =~ /\A(?:Nullable|LowCardinality|Array|Tuple|Map|Nested)\(/
ABresult
0X0
100
111
550 && $type !~ /\A(?:Nullable|LowCardinality|Array|Tuple|Map|Nested)\(/) {
551 67 $type = "Nullable($type)";
552 }
553  
554 # LowCardinality(T) is opt-in: it is a dictionary encoding that pays off for
555 # low-distinct-count columns and hurts for high-cardinality ones, so it can
556 # never be inferred from the portable metadata. Note the nesting order --
557 # LowCardinality(Nullable(String)), not the other way round.
558 456 ▶ if ($extra->{low_cardinality} && $type !~ /\ALowCardinality\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra->{"low_cardinality"} and not $type =~ /\ALowCardinality\(/) 53 403 456
Condition66%2/3
$extra->{"low_cardinality"} and not $type =~ /\ALowCardinality\(/
!ll&&!rl&&r
403053
MC/DC50%1/2
$extra->{"low_cardinality"} and not $type =~ /\ALowCardinality\(/
$extra->{"low_cardinality"} not $type =~ /\ALowCardinality\(/
Truth table66%2/3
A and not(B)
A= $extra->{"low_cardinality"}B= not $type =~ /\ALowCardinality\(/
ABresult
0X0
100
111
559 53 $type = "LowCardinality($type)";
560 }
561  
562 456 return $type;
563 }
564  
565 469 ▶sub _clickhouse_base_type {
Subroutine _clickhouse_base_type: called CC=22 SCAR=31.0
566 469 my ($info) = @_;
567  
568 469 ▶ my $type = $info->{data_type} // 'String';
Condition100%2/2
$info->{"data_type"} // "String"
l!l
4681
MC/DC100%1/1
$info->{"data_type"} // "String"
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
Aresult
11
00
569 469 ▶ my $extra = $info->{extra} // {};
Condition100%2/2
$info->{"extra"} // {}
l!l
93376
MC/DC100%1/1
$info->{"extra"} // {}
$info->{"extra"}
Truth table100%2/2
A
A= $info->{"extra"}
Aresult
11
00
570  
571 # Array(T): a trailing "[]" is the portable DBIO/DBIC spelling for an array
572 # column (text[], integer[]).
573 469 ▶ if ($type =~ /\A(.+?)\[\]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /\A(.+?)\[\]\z/) 13 456 469
574 13 return sprintf 'Array(%s)',
575 _clickhouse_base_type({ %$info, data_type => $1 });
576 }
577  
578 # Escape hatch: a data_type that already carries parameters is a native
579 # ClickHouse type spelled out by hand -- DateTime64(6), Decimal(20, 4),
580 # Enum8('a' = 1) -- and is passed through untouched.
581 456 ▶ return $type if $type =~ /\(.+\)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\(.+\)\z/ 12 444 456
582  
583 # Same escape hatch for the parameterless native names, case-sensitively and
584 # before any lowercasing or mapping: 'Date' and 'DateTime' would otherwise
585 # hit the portable date => Date32 / datetime => DateTime64(3) entries and the
586 # narrow types would be unreachable. The check sits ahead of the enum /
587 # decimal / fixed_string branches too, so that "a native spelling is passed
588 # through" holds without exceptions -- those branches key off portable
589 # spellings that no native name shares.
590 444 ▶ return $type if $NATIVE_TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $NATIVE_TYPE{$type} 21 423 444
591  
592 423 my $lc = lc $type;
593  
594 # enum: DBIC's portable spelling carries the members in extra->{list}.
595 423 ▶ if ($lc eq 'enum' && ref $extra->{list} eq 'ARRAY' && @{ $extra->{list} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc eq "enum" and ref $extra->{"list"} eq "ARRAY" and @{$extra->{"list"};}) 7 416 423
Condition83%5/6
$lc eq "enum" and ref $extra->{"list"} eq "ARRAY" and @{$extra->{"list"};}
!ll&&!rl&&r
41607
$lc eq "enum" and ref $extra->{"list"} eq "ARRAY"
!ll&&!rl&&r
41517
MC/DC66%2/3
$lc eq "enum" and ref $extra->{"list"} eq "ARRAY" and @{$extra->{"list"};}
$lc eq "enum" ref $extra->{"list"} eq "ARRAY" @{$extra->{"list"};}
Truth table75%3/4
A and B and C
A= $lc eq "enum"B= ref $extra->{"list"} eq "ARRAY"C= @{$extra->{"list"};}
ABCresult
0XX0
10X0
1100
1111
596 7 return _enum_type(@{ $extra->{list} });
597 }
598  
599 # decimal/numeric: size is [precision, scale], or a bare precision.
600 416 ▶ if ($lc eq 'decimal' || $lc eq 'numeric') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc eq "decimal" or $lc eq "numeric") 9 407 416
Condition100%3/3
$lc eq "decimal" or $lc eq "numeric"
l!l&&r!l&&!r
72407
MC/DC100%2/2
$lc eq "decimal" or $lc eq "numeric"
$lc eq "decimal" $lc eq "numeric"
Truth table100%3/3
A or B
A= $lc eq "decimal"B= $lc eq "numeric"
ABresult
1X1
011
000
601 9 my $size = $info->{size};
602 9 ▶ return sprintf 'Decimal(%d, %d)', @{$size}[0, 1]
Branch: 1 branch
BranchTrueFalseTotal
if ref $size eq "ARRAY" and @$size >= 2 7 2 9
Condition66%2/3
ref $size eq "ARRAY" and @$size >= 2
!ll&&!rl&&r
207
MC/DC50%1/2
ref $size eq "ARRAY" and @$size >= 2
ref $size eq "ARRAY" @$size >= 2
Truth table66%2/3
A and B
A= ref $size eq "ARRAY"B= @$size >= 2
ABresult
0X0
100
111
603 if ref $size eq 'ARRAY' && @$size >= 2;
604 2 ▶ return sprintf 'Decimal(%d, 0)', $size
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and not ref $size and $size =~ /\A[0-9]+\z/ 1 1 2
Condition66%4/6
defined $size and not ref $size and $size =~ /\A[0-9]+\z/
!ll&&!rl&&r
101
defined $size and not ref $size
!ll&&!rl&&r
101
MC/DC33%1/3
defined $size and not ref $size and $size =~ /\A[0-9]+\z/
defined $size not ref $size $size =~ /\A[0-9]+\z/
Truth table50%2/4
A and not(B) and C
A= defined $sizeB= not ref $sizeC= $size =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
605 if defined $size && !ref $size && $size =~ /\A[0-9]+\z/;
606 1 return $DEFAULT_DECIMAL;
607 }
608  
609 # FixedString(N) is opt-in for char/binary columns, never inferred: it pads
610 # with zero BYTES (not spaces) and returns the padding to the client, which
611 # is not what a CHAR(N) column means anywhere else.
612 407 ▶ if ($extra->{fixed_string} && defined $info->{size} && !ref $info->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra->{"fixed_string"} and defined $info->{"size"} and not ref $info->{"size"}) 3 404 407
Condition83%5/6
$extra->{"fixed_string"} and defined $info->{"size"} and not ref $info->{"size"}
!ll&&!rl&&r
40403
$extra->{"fixed_string"} and defined $info->{"size"}
!ll&&!rl&&r
40313
MC/DC66%2/3
$extra->{"fixed_string"} and defined $info->{"size"} and not ref $info->{"size"}
$extra->{"fixed_string"} defined $info->{"size"} not ref $info->{"size"}
Truth table75%3/4
A and B and not(C)
A= $extra->{"fixed_string"}B= defined $info->{"size"}C= not ref $info->{"size"}
ABCresult
0XX0
10X0
1100
1111
613 3 return sprintf 'FixedString(%d)', $info->{size};
614 }
615  
616 404 my $mapped = $TYPE_MAP{$lc};
617  
618 # Unknown types pass through verbatim so that native ClickHouse type names
619 # (IPv6, Point, AggregateFunction(...)) can be used directly in data_type.
620 404 ▶ return $type unless defined $mapped;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mapped 97 307 404
621  
622 307 ▶ return $UNSIGNED_INT{$mapped} if $extra->{unsigned} && $UNSIGNED_INT{$mapped};
Branch: 1 branch
BranchTrueFalseTotal
if $extra->{"unsigned"} and $UNSIGNED_INT{$mapped} 21 286 307
Condition100%3/3
$extra->{"unsigned"} and $UNSIGNED_INT{$mapped}
!ll&&!rl&&r
285121
MC/DC100%2/2
$extra->{"unsigned"} and $UNSIGNED_INT{$mapped}
$extra->{"unsigned"} $UNSIGNED_INT{$mapped}
Truth table100%3/3
A and B
A= $extra->{"unsigned"}B= $UNSIGNED_INT{$mapped}
ABresult
0X0
100
111
623  
624 286 return $mapped;
625 }
626  
627 # Enum8 holds Int8 values, Enum16 Int16 ones. Members are numbered 1..N in
628 # declaration order: deterministic, and stable when new members are appended
629 # (renumbering an existing member is a destructive ALTER in ClickHouse).
630 7 ▶sub _enum_type {
Subroutine _enum_type: called CC=2 SCAR=6.9
631 7 my (@members) = @_;
632 7 ▶ my $kind = @members <= 127 ? 'Enum8' : 'Enum16';
Branch: 1 branch
BranchTrueFalseTotal
@members <= 127 ? : 6 1 7
633 return sprintf '%s(%s)', $kind, join ', ',
634 7 map { sprintf '%s = %d', quote_string($members[$_]), $_ + 1 } 0 .. $#members;
635 }
636  
637  
638 53 1;
639  
640 __END__
« ClickHouse/Test/Conformance.pm ↑ Summary ClickHouse/Result.pm »