ClickHouse/DDL/Emit.pm

filter: Statementstmt 100% 93 / 93 Branchbran 96.0% 48 / 50 Conditioncond 72.2% 26 / 36 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 100% 30 / 30 Podpod 100% 22 / 22 totaltotal 91.6% 229 / 250 CC 41 SCAR 12.9
CC
41
Cov
93%
split_table_name
7.1
create_table
7.0
create_view
5.5
CRAP 3.6
« ClickHouse/Deploy.pm ↑ Summary ClickHouse/Storage/TxnScopeGuard.pm »
1 package DBIO::ClickHouse::DDL::Emit;
2 # ABSTRACT: Single source of ClickHouse DDL statement shape
3  
4 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 54 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 54 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 our $VERSION = '0.900007';
11  
12 54 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 54 our @EXPORT_OK = qw(
15 quote_ident quote_string column_def index_def
16 engine_clause partition_by_clause primary_key_clause order_by_clause
17 settings_clause create_table create_view drop_view drop_table
18 split_table_name
19 alter_table_add_column alter_table_modify_column alter_table_drop_column
20 alter_table_remove_default
21 alter_table_add_index alter_table_drop_index
22 create_database drop_database
23 );
24  
25  
26  
27 549 ▶sub quote_ident {
Subroutine quote_ident: called CC=1 SCAR=0.0
Pod documented
28 549 my ($name) = @_;
29 549 $name =~ s/([\\`])/\\$1/g;
30 549 return "`$name`";
31 }
32  
33  
34 271 ▶sub quote_string {
Subroutine quote_string: called CC=1 SCAR=0.0
Pod documented
35 271 my ($value) = @_;
36 271 $value =~ s/([\\'])/\\$1/g;
37 271 return "'$value'";
38 }
39  
40  
41 231 ▶sub column_def {
Subroutine column_def: called CC=3 SCAR=11.0
Pod documented
42 231 my (%a) = @_;
43 231 ▶ my $def = sprintf '%s %s', quote_ident($a{name}), ($a{type} // 'String');
Condition100%2/2
$a{"type"} // "String"
l!l
2301
MC/DC100%1/1
$a{"type"} // "String"
$a{"type"}
Truth table100%2/2
A
A= $a{"type"}
Aresult
11
00
44 231 ▶ $def .= " DEFAULT $a{default}" if defined $a{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a{"default"} 36 195 231
45 231 return $def;
46 }
47  
48  
49 21 ▶sub index_def {
Subroutine index_def: called CC=5 SCAR=16.7
Pod documented
50 21 my ($info) = @_;
51  
52 my $def = sprintf 'INDEX %s %s TYPE %s',
53 quote_ident($info->{index_name}),
54 ($info->{expr} // ''),
55 21 ▶ ($info->{type_full} // $info->{type} // 'minmax');
Condition57%4/7
$info->{"expr"} // ""
l!l
210
($info->{"type_full"} // $info->{"type"}) // "minmax"
l!l
210
$info->{"type_full"} // $info->{"type"}
l!l&&r!l&&!r
2010
MC/DC0%0/1
$info->{"expr"} // ""
$info->{"expr"}
MC/DC0%0/2
($info->{"type_full"} // $info->{"type"}) // "minmax"
$info->{"type_full"} $info->{"type"}
Truth table50%1/2
A
A= $info->{"expr"}
Aresult
11
00
Truth table66%2/3
A // B
A= $info->{"type_full"}B= $info->{"type"}
ABresult
1X1
011
000
56  
57 21 ▶ $def .= " GRANULARITY $info->{granularity}" if defined $info->{granularity};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"granularity"} 17 4 21
58  
59 21 return $def;
60 }
61  
62  
63 62 ▶sub engine_clause {
Subroutine engine_clause: called CC=2 SCAR=6.9
Pod documented
64 62 my ($engine) = @_;
65 62 ▶ return sprintf 'ENGINE = %s', $engine // 'MergeTree';
Condition100%2/2
$engine // "MergeTree"
l!l
611
MC/DC100%1/1
$engine // "MergeTree"
$engine
Truth table100%2/2
A
A= $engine
Aresult
11
00
66 }
67  
68  
69 7 ▶sub partition_by_clause {
Subroutine partition_by_clause: called CC=1 SCAR=0.0
Pod documented
70 7 my ($expr) = @_;
71 7 return "PARTITION BY $expr";
72 }
73  
74  
75 5 ▶sub primary_key_clause {
Subroutine primary_key_clause: called CC=1 SCAR=0.0
Pod documented
76 5 my (@key) = @_;
77 5 return sprintf 'PRIMARY KEY (%s)', _key_list(@key);
78 }
79  
80  
81 63 ▶sub order_by_clause {
Subroutine order_by_clause: called CC=2 SCAR=6.9
Pod documented
82 63 my (@key) = @_;
83 63 ▶ return 'ORDER BY tuple()' unless @key;
Branch: 1 branch
BranchTrueFalseTotal
unless @key 2 61 63
84 61 return sprintf 'ORDER BY (%s)', _key_list(@key);
85 }
86  
87 66 ▶sub _key_list {
Subroutine _key_list: called CC=2 SCAR=6.9
88 66 ▶ return join ', ', map { ref $_ eq 'SCALAR' ? $$_ : quote_ident($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'SCALAR' ? : 1 74 75
89 }
90  
91  
92 62 ▶sub settings_clause {
Subroutine settings_clause: called CC=3 SCAR=11.0
Pod documented
93 62 my ($settings) = @_;
94 62 ▶ return undef unless ref $settings eq 'HASH' && keys %$settings;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" and keys %$settings 55 7 62
Condition100%3/3
ref $settings eq "HASH" and keys %$settings
!ll&&!rl&&r
5417
MC/DC100%2/2
ref $settings eq "HASH" and keys %$settings
ref $settings eq "HASH" keys %$settings
Truth table100%3/3
A and B
A= ref $settings eq "HASH"B= keys %$settings
ABresult
0X0
100
111
95 return sprintf 'SETTINGS %s', join ', ',
96 7 map { sprintf '%s = %s', $_, _setting_value($settings->{$_}) }
97 sort keys %$settings;
98 }
99  
100 9 ▶sub _setting_value {
Subroutine _setting_value: called CC=4 SCAR=13.9
101 9 my ($value) = @_;
102 9 ▶ return $$value if ref $value eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "SCALAR" 1 8 9
103 8 ▶ return 'NULL' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
104 8 ▶ return $value if $value =~ /\A-?(?:[0-9]+(?:\.[0-9]+)?)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A-?(?:[0-9]+(?:\.[0-9]+)?)\z/ 7 1 8
105 1 return quote_string($value);
106 }
107  
108  
109 60 ▶sub create_table {
Subroutine create_table: called CC=7 SCAR=19.5
Pod documented
110 60 my (%a) = @_;
111 my $sql = sprintf "CREATE TABLE%s %s (\n%s\n)",
112 ($a{if_not_exists} ? ' IF NOT EXISTS' : ''),
113 (defined $a{database} ? quote_ident($a{database}) . '.' : '') . quote_ident($a{name}),
114 60 ▶ join ",\n", map { " $_" } @{ $a{columns} // [] };
Branch: 2 branches
BranchTrueFalseTotal
$a{'if_not_exists'} ? : 7 53 60
defined $a{'database'} ? : 2 58 60
Condition50%1/2
$a{"columns"} // []
l!l
600
MC/DC0%0/1
$a{"columns"} // []
$a{"columns"}
Truth table50%1/2
A
A= $a{"columns"}
Aresult
11
00
115 60 ▶ my @clauses = grep { defined && length } @{ $a{clauses} // [] };
Condition80%4/5
defined $_ && length $_
!ll&&!rl&&r
160131
$a{"clauses"} // []
l!l
591
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
MC/DC100%1/1
$a{"clauses"} // []
$a{"clauses"}
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table100%2/2
A
A= $a{"clauses"}
Aresult
11
00
116 60 ▶ $sql .= "\n" . join "\n", @clauses if @clauses;
Branch: 1 branch
BranchTrueFalseTotal
if @clauses 59 1 60
117 60 return "$sql;";
118 }
119  
120  
121 31 ▶sub create_view {
Subroutine create_view: called CC=5 SCAR=17.1
Pod documented
122 31 my (%a) = @_;
123 return sprintf 'CREATE %sVIEW%s %s AS %s;',
124 ($a{or_replace} ? 'OR REPLACE ' : ''),
125 ($a{if_not_exists} && !$a{or_replace} ? ' IF NOT EXISTS' : ''),
126 (defined $a{database} ? quote_ident($a{database}) . '.' : '') . quote_ident($a{name}),
127 31 ▶ $a{definition};
Branch: 3 branches
BranchTrueFalseTotal
$a{'or_replace'} ? : 7 24 31
$a{'if_not_exists'} && !$a{'or_replace'} ? : 0 31 31
defined $a{'database'} ? : 3 28 31
Condition33%1/3
$a{"if_not_exists"} and not $a{"or_replace"}
!ll&&!rl&&r
3100
MC/DC0%0/2
$a{"if_not_exists"} and not $a{"or_replace"}
$a{"if_not_exists"} not $a{"or_replace"}
Truth table33%1/3
A and not(B)
A= $a{"if_not_exists"}B= not $a{"or_replace"}
ABresult
0X0
100
111
128 }
129  
130  
131 8 ▶sub drop_view {
Subroutine drop_view: called CC=3 SCAR=11.0
Pod documented
132 8 my (%a) = @_;
133 return sprintf 'DROP VIEW%s %s;',
134 ($a{if_exists} ? ' IF EXISTS' : ''),
135 8 ▶ (defined $a{database} ? quote_ident($a{database}) . '.' : '') . quote_ident($a{name});
Branch: 2 branches
BranchTrueFalseTotal
$a{'if_exists'} ? : 2 6 8
defined $a{'database'} ? : 1 7 8
136 }
137  
138  
139 183 ▶sub split_table_name {
Subroutine split_table_name: called CC=7 SCAR=19.7
Pod documented
140 183 my ($name) = @_;
141 183 ▶ return (undef, $name) unless defined $name && !ref $name && $name =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and $name =~ /\./ 166 17 183
Condition50%3/6
defined $name and not ref $name and $name =~ /\./
!ll&&!rl&&r
016617
defined $name and not ref $name
!ll&&!rl&&r
00183
MC/DC33%1/3
defined $name and not ref $name and $name =~ /\./
defined $name not ref $name $name =~ /\./
Truth table50%2/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $name =~ /\./
ABCresult
0XX0
10X0
1100
1111
142 17 my @parts = split /\./, $name, -1;
143 17 ▶ DBIO::Exception->throw("DBIO::ClickHouse: table '$name' is refused: a table "
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 2 and length $parts[0] and length $parts[1] 3 14 17
Condition100%6/6
@parts == 2 and length $parts[0] and length $parts[1]
!ll&&!rl&&r
2114
@parts == 2 and length $parts[0]
!ll&&!rl&&r
1115
MC/DC100%3/3
@parts == 2 and length $parts[0] and length $parts[1]
@parts == 2 length $parts[0] length $parts[1]
Truth table100%4/4
A and B and C
A= @parts == 2B= length $parts[0]C= length $parts[1]
ABCresult
0XX0
10X0
1100
1111
144 . 'or view name is name or database.name, with both halves given and one dot')
145 unless @parts == 2 && length $parts[0] && length $parts[1];
146 14 return @parts;
147 }
148  
149  
150 14 ▶sub drop_table {
Subroutine drop_table: called CC=2 SCAR=6.9
Pod documented
151 14 my (%a) = @_;
152 return sprintf 'DROP TABLE%s %s;',
153 ($a{if_exists} ? ' IF EXISTS' : ''),
154 14 ▶ quote_ident($a{name});
Branch: 1 branch
BranchTrueFalseTotal
$a{'if_exists'} ? : 6 8 14
155 }
156  
157  
158 9 ▶sub alter_table_add_column {
Subroutine alter_table_add_column: called CC=2 SCAR=6.9
Pod documented
159 9 my (%a) = @_;
160 return _alter_table($a{table},
161 9 ▶ "ADD COLUMN" . ($a{if_not_exists} ? ' IF NOT EXISTS' : '') . " $a{definition}");
Branch: 1 branch
BranchTrueFalseTotal
$a{'if_not_exists'} ? : 2 7 9
162 }
163  
164  
165 16 ▶sub alter_table_modify_column {
Subroutine alter_table_modify_column: called CC=3 SCAR=11.0
Pod documented
166 16 my (%a) = @_;
167 16 for my $forbidden (qw(if_exists if_not_exists)) {
168 Carp::croak("alter_table_modify_column does not accept $forbidden - "
169 . 'ClickHouse has no MODIFY COLUMN IF [NOT] EXISTS guard; '
170 . 'see t/38-ddl-if-exists-live.t for the negative-space verification')
171 31 ▶ if exists $a{$forbidden};
Branch: 1 branch
BranchTrueFalseTotal
if exists $a{$forbidden} 1 30 31
172 }
173 15 return _alter_table($a{table}, "MODIFY COLUMN $a{definition}");
174 }
175  
176  
177 2 ▶sub alter_table_remove_default {
Subroutine alter_table_remove_default: called CC=1 SCAR=0.0
Pod documented
178 2 my (%a) = @_;
179 2 return _alter_table($a{table}, 'MODIFY COLUMN ' . quote_ident($a{name}) . ' REMOVE DEFAULT');
180 }
181  
182  
183 6 ▶sub alter_table_drop_column {
Subroutine alter_table_drop_column: called CC=2 SCAR=6.9
Pod documented
184 6 my (%a) = @_;
185 return _alter_table($a{table},
186 6 ▶ 'DROP COLUMN' . ($a{if_exists} ? ' IF EXISTS' : '') . ' ' . quote_ident($a{name}));
Branch: 1 branch
BranchTrueFalseTotal
$a{'if_exists'} ? : 2 4 6
187 }
188  
189  
190 9 ▶sub alter_table_add_index {
Subroutine alter_table_add_index: called CC=2 SCAR=6.9
Pod documented
191 9 my (%a) = @_;
192 # $a{definition} comes from index_def() and begins with "INDEX `name` ..."
193 # so the guard has to slot in after the INDEX keyword. Strip the leading
194 # INDEX here once rather than duplicate it under either condition.
195 9 my ($rest) = $a{definition} =~ /\AINDEX\s+(.*)\z/s;
196 return _alter_table($a{table},
197 9 ▶ 'ADD INDEX' . ($a{if_not_exists} ? ' IF NOT EXISTS' : '') . " $rest");
Branch: 1 branch
BranchTrueFalseTotal
$a{'if_not_exists'} ? : 3 6 9
198 }
199  
200  
201 9 ▶sub alter_table_drop_index {
Subroutine alter_table_drop_index: called CC=2 SCAR=6.9
Pod documented
202 9 my (%a) = @_;
203 return _alter_table($a{table},
204 9 ▶ 'DROP INDEX' . ($a{if_exists} ? ' IF EXISTS' : '') . ' ' . quote_ident($a{name}));
Branch: 1 branch
BranchTrueFalseTotal
$a{'if_exists'} ? : 2 7 9
205 }
206  
207 50 ▶sub _alter_table {
Subroutine _alter_table: called CC=1 SCAR=0.0
208 50 my ($table, $action) = @_;
209 50 return sprintf 'ALTER TABLE %s %s;', quote_ident($table), $action;
210 }
211  
212  
213 1 ▶sub create_database {
Subroutine create_database: called CC=1 SCAR=0.0
Pod documented
214 1 my (%a) = @_;
215 1 return sprintf 'CREATE DATABASE %s', quote_ident($a{name});
216 }
217  
218  
219 2 ▶sub drop_database {
Subroutine drop_database: called CC=2 SCAR=6.9
Pod documented
220 2 my (%a) = @_;
221 return sprintf 'DROP DATABASE %s%s',
222 2 ▶ ($a{if_exists} ? 'IF EXISTS ' : ''), quote_ident($a{name});
Branch: 1 branch
BranchTrueFalseTotal
$a{'if_exists'} ? : 1 1 2
223 }
224  
225  
226 54 1;
227  
228 __END__
« ClickHouse/Deploy.pm ↑ Summary ClickHouse/Storage/TxnScopeGuard.pm »