ClickHouse/Introspect.pm

filter: Statementstmt 90.4% 95 / 105 Branchbran 83.3% 30 / 36 Conditioncond 56.7% 21 / 37 MC/DCmcdc 20.0% 4 / 20 Subroutinesub 100% 19 / 19 Podpod 100% 8 / 8 totaltotal 78.6% 177 / 225 CC 38 SCAR 18.7
CC
38
Cov
82%
result_class_extra_statements
12.4
_normalize_default
9.1
view_definition
7.6
CRAP 6.5
« ClickHouse/Diff/Table.pm ↑ Summary ClickHouse/SQLMaker.pm »
1 package DBIO::ClickHouse::Introspect;
2 # ABSTRACT: Introspect a ClickHouse database via the system.* catalog
3  
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 our $VERSION = '0.900007';
8  
9 34 ▶use base 'DBIO::Introspect::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 34 ▶use DBIO::ClickHouse::Introspect::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use DBIO::ClickHouse::Introspect::Columns;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use DBIO::ClickHouse::Introspect::Indexes;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use DBIO::ClickHouse::Introspect::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17  
18 2 ▶sub database { $_[0]->{database} }
Subroutine database: called CC=1 SCAR=0.0
Pod documented
19  
20 2 ▶sub _build_model {
Subroutine _build_model: called CC=1 SCAR=0.0
21 2 my ($self) = @_;
22 2 my $dbh = $self->dbh;
23 2 my $database = $self->database;
24  
25 2 my $tables = DBIO::ClickHouse::Introspect::Tables->fetch($dbh, $database);
26  
27 return {
28 2 tables => $tables,
29 columns => DBIO::ClickHouse::Introspect::Columns->fetch($dbh, $tables, $database),
30 data_skipping_indexes =>
31 DBIO::ClickHouse::Introspect::Indexes->fetch($dbh, $tables, $database),
32 indexes => {},
33 foreign_keys => {},
34 };
35 }
36  
37  
38 9 ▶sub table_columns_info {
Subroutine table_columns_info: called CC=6 SCAR=18.0
Pod documented
39 9 my ($self, $key) = @_;
40  
41 9 my %info;
42 9 ▶ for my $col (@{ $self->model->{columns}{$key} || [] }) {
Condition50%1/2
$self->model->{'columns'}{$key} || []
l!l
90
MC/DC0%0/1
$self->model->{'columns'}{$key} || []
$self->model->{'columns'}{$key}
Truth table50%1/2
A
A= $self->model->{'columns'}{$key}
Aresult
11
00
43 97 my $type = DBIO::ClickHouse::Introspect::Util->normalize_type($col->{data_type});
44  
45 my %column = (
46 data_type => $type->{data_type},
47 # Deliberately NOT the base's `($col->{not_null} || $col->{is_pk}) ? 0 : 1`
48 # (core k126). That fold repairs models where the engine reports the
49 # NOT NULL attribute and the key constraint separately, so a key column
50 # arrives with not_null = 0 -- SQLite's PRAGMA does exactly that. There is
51 # no such gap here: nullability is part of the ClickHouse type
52 # (Nullable(T)) and is read straight off it, which is also the only thing
53 # DBIO::ClickHouse::Introspect::Columns derives not_null from. The fold
54 # could therefore only ever change an answer for a key column the server
55 # really did declare Nullable (MergeTree `SETTINGS allow_nullable_key = 1`),
56 # and there it would claim NOT NULL for a column that holds NULLs -- which
57 # breaks the DDL <-> Introspect round-trip this class exists to keep, since
58 # the redeploy would silently drop the Nullable wrapper.
59 is_nullable => $type->{is_nullable},
60 97 );
61 97 ▶ $column{size} = $type->{size} if defined $type->{size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $type->{"size"} 26 71 97
62  
63 97 ▶ my %extra = %{ $type->{extra} || {} };
Condition50%1/2
$type->{'extra'} || {}
l!l
970
MC/DC0%0/1
$type->{'extra'} || {}
$type->{'extra'}
Truth table50%1/2
A
A= $type->{'extra'}
Aresult
11
00
64 97 $self->_normalize_default(\%column, \%extra, $col);
65 97 ▶ $column{extra} = \%extra if keys %extra;
Branch: 1 branch
BranchTrueFalseTotal
if keys %extra 29 68 97
66  
67 97 $info{ $col->{column_name} } = \%column;
68 }
69  
70 9 return \%info;
71 }
72  
73 # The inverse of DBIO::ClickHouse::DDL::_column_default: a plain scalar
74 # default_value renders as a quoted string literal and a SCALAR ref renders
75 # verbatim, so a quoted literal comes back as a plain scalar and everything
76 # else as a SCALAR ref. This is deliberately stricter than the DuckDB
77 # introspector, which turns bare numbers into plain scalars too -- here that
78 # would re-render `DEFAULT 0` as `DEFAULT '0'` and change the emitted DDL.
79 97 ▶sub _normalize_default {
Subroutine _normalize_default: called CC=9 SCAR=22.1
80 97 my ($self, $column, $extra, $col) = @_;
81  
82 97 my $expression = $col->{default_value};
83 97 ▶ return unless defined $expression && length $expression;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expression and length $expression 79 18 97
Condition66%2/3
defined $expression and length $expression
!ll&&!rl&&r
79018
MC/DC50%1/2
defined $expression and length $expression
defined $expression length $expression
Truth table66%2/3
A and B
A= defined $expressionB= length $expression
ABresult
0X0
100
111
84  
85 # MATERIALIZED / ALIAS / EPHEMERAL are not DEFAULT: the value is computed or
86 # not stored at all. Emitting them as default_value would turn them into a
87 # plain DEFAULT on the next deploy and change the column's semantics, so they
88 # are recorded in extra and left out of default_value.
89 18 ▶ my $kind = uc($col->{default_kind} // 'DEFAULT');
Condition50%1/2
$col->{"default_kind"} // "DEFAULT"
l!l
180
MC/DC0%0/1
$col->{"default_kind"} // "DEFAULT"
$col->{"default_kind"}
Truth table50%1/2
A
A= $col->{"default_kind"}
Aresult
11
00
90 18 ▶ if ($kind ne 'DEFAULT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind ne "DEFAULT") 3 15 18
91 3 $extra->{default_kind} = $kind;
92 3 $extra->{default_expression} = $expression;
93 3 return;
94 }
95  
96 # generateUUIDv4() on a UUID column is this driver's only surrogate identity
97 # (see DBIO::ClickHouse::DDL: there are no sequences and no identity
98 # columns), so it maps back to is_auto_increment and no default_value --
99 # exactly what the DDL layer consumes. retrieve_on_insert is deliberately
100 # NOT set: ClickHouse has no RETURNING and no last_insert_id, so DBIO could
101 # not fetch the generated value back.
102 15 ▶ if (($column->{data_type} // '') eq 'uuid'
Branch: 1 branch
BranchTrueFalseTotal
if (($column->{"data_type"} // "") eq "uuid" and $expression =~ /\Agenerate(?:UUIDv4|UUIDv7)\(\s*\)\z/) 3 12 15
Condition60%3/5
($column->{"data_type"} // "") eq "uuid" and $expression =~ /\Agenerate(?:UUIDv4|UUIDv7)\(\s*\)\z/
!ll&&!rl&&r
1203
$column->{"data_type"} // ""
l!l
150
MC/DC0%0/2
($column->{"data_type"} // "") eq "uuid" and $expression =~ /\Agenerate(?:UUIDv4|UUIDv7)\(\s*\)\z/
$column->{"data_type"} $expression =~ /\Agenerate(?:UUIDv4|UUIDv7)\(\s*\)\z/
Truth table33%1/3
A and B
A= $column->{"data_type"}B= $expression =~ /\Agenerate(?:UUIDv4|UUIDv7)\(\s*\)\z/
ABresult
0X0
100
111
103 && $expression =~ /\Agenerate(?:UUIDv4|UUIDv7)\(\s*\)\z/) {
104 3 $column->{is_auto_increment} = 1;
105 3 return;
106 }
107  
108 12 my $literal = DBIO::ClickHouse::Introspect::Util->unquote_string($expression);
109 12 ▶ if (defined $literal) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $literal) 6 6 12
110 6 $column->{default_value} = $literal;
111 6 return;
112 }
113  
114 6 my $raw = $expression;
115 6 $column->{default_value} = \$raw;
116 }
117  
118  
119 1 ▶sub table_uniq_info { return [] }
Subroutine table_uniq_info: called CC=1 SCAR=0.0
Pod documented
120  
121  
122 1 ▶sub table_fk_info { return [] }
Subroutine table_fk_info: called CC=1 SCAR=0.0
Pod documented
123  
124  
125 3 ▶sub table_comment {
Subroutine table_comment: called CC=4 SCAR=13.9
Pod documented
126 3 my ($self, $key) = @_;
127 3 ▶ my $comment = ($self->model->{tables}{$key} || {})->{comment};
Condition50%1/2
$self->model->{'tables'}{$key} || {}
l!l
30
MC/DC0%0/1
$self->model->{'tables'}{$key} || {}
$self->model->{'tables'}{$key}
Truth table50%1/2
A
A= $self->model->{'tables'}{$key}
Aresult
11
00
128 3 ▶ return (defined $comment && length $comment) ? $comment : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $comment && length $comment ? : 1 2 3
Condition100%3/3
defined $comment and length $comment
!ll&&!rl&&r
111
MC/DC100%2/2
defined $comment and length $comment
defined $comment length $comment
Truth table100%3/3
A and B
A= defined $commentB= length $comment
ABresult
0X0
100
111
129 }
130  
131  
132 3 ▶sub column_comment {
Subroutine column_comment: called CC=4 SCAR=14.2
Pod documented
133 3 my ($self, $key, $column_name) = @_;
134 3 ▶ for my $col (@{ $self->model->{columns}{$key} || [] }) {
Condition50%1/2
$self->model->{'columns'}{$key} || []
l!l
30
MC/DC0%0/1
$self->model->{'columns'}{$key} || []
$self->model->{'columns'}{$key}
Truth table50%1/2
A
A= $self->model->{'columns'}{$key}
Aresult
11
00
135 8 ▶ next unless $col->{column_name} eq $column_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $col->{"column_name"} eq $column_name 5 3 8
136 3 return $col->{comment};
137 }
138 0 return undef;
139 }
140  
141  
142 1 ▶sub view_definition {
Subroutine view_definition: called CC=3 SCAR=20.3
Pod documented
143 1 my ($self, $key) = @_;
144 1 ▶ return undef unless $self->table_is_view($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->table_is_view($key) 1 0 1
145  
146 0 my $sql = q{
147 SELECT create_table_query
148 FROM system.tables
149 WHERE database = %s AND name = ?
150 };
151 0 my @bind;
152 0 ▶ if (defined $self->database) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->database) { } 0 0 0
153 0 $sql = sprintf $sql, '?';
154 0 push @bind, $self->database;
155 }
156 else {
157 0 $sql = sprintf $sql, 'currentDatabase()';
158 }
159 0 push @bind, $key;
160  
161 0 my ($create) = $self->dbh->selectrow_array($sql, undef, @bind);
162 0 return DBIO::ClickHouse::Introspect::Util->view_select($create);
163 }
164  
165  
166 4 ▶sub result_class_extra_statements {
Subroutine result_class_extra_statements: called CC=11 SCAR=25.2
Pod documented
167 4 my ($self, $key) = @_;
168  
169 4 ▶ my $table = $self->model->{tables}{$key} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->model->{"tables"}{$key} or return () 4 0 4
170 4 ▶ return () if ($table->{kind} // '') eq 'view';
Branch: 1 branch
BranchTrueFalseTotal
if ($table->{"kind"} // "") eq "view" 1 3 4
Condition50%1/2
$table->{"kind"} // ""
l!l
40
MC/DC0%0/1
$table->{"kind"} // ""
$table->{"kind"}
Truth table50%1/2
A
A= $table->{"kind"}
Aresult
11
00
171  
172 3 my %options;
173 $options{engine} = $table->{engine_expression}
174 3 ▶ if defined $table->{engine_expression} && length $table->{engine_expression};
Branch: 1 branch
BranchTrueFalseTotal
if defined $table->{"engine_expression"} and length $table->{"engine_expression"} 3 0 3
Condition33%1/3
defined $table->{"engine_expression"} and length $table->{"engine_expression"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $table->{"engine_expression"} and length $table->{"engine_expression"}
defined $table->{"engine_expression"} length $table->{"engine_expression"}
Truth table33%1/3
A and B
A= defined $table->{"engine_expression"}B= length $table->{"engine_expression"}
ABresult
0X0
100
111
175  
176 3 ▶ $options{order_by} = [ map { _key_entry($_) } @{ $table->{sorting_key_entries} || [] } ];
Condition50%1/2
$table->{'sorting_key_entries'} || []
l!l
30
MC/DC0%0/1
$table->{'sorting_key_entries'} || []
$table->{'sorting_key_entries'}
Truth table50%1/2
A
A= $table->{'sorting_key_entries'}
Aresult
11
00
177  
178 $options{partition_by} = $table->{partition_key}
179 3 ▶ if defined $table->{partition_key} && length $table->{partition_key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $table->{"partition_key"} and length $table->{"partition_key"} 1 2 3
Condition66%2/3
defined $table->{"partition_key"} and length $table->{"partition_key"}
!ll&&!rl&&r
021
MC/DC50%1/2
defined $table->{"partition_key"} and length $table->{"partition_key"}
defined $table->{"partition_key"} length $table->{"partition_key"}
Truth table66%2/3
A and B
A= defined $table->{"partition_key"}B= length $table->{"partition_key"}
ABresult
0X0
100
111
180  
181 1 ▶ $options{primary_key} = [ map { _key_entry($_) } @{ $table->{primary_key_entries} || [] } ]
Condition50%1/2
$table->{'primary_key_entries'} || []
l!l
10
MC/DC0%0/1
$table->{'primary_key_entries'} || []
$table->{'primary_key_entries'}
Truth table50%1/2
A
A= $table->{'primary_key_entries'}
Aresult
11
00
182 3 ▶ if _key_differs($table->{primary_key_entries}, $table->{sorting_key_entries});
Branch: 1 branch
BranchTrueFalseTotal
if _key_differs($table->{"primary_key_entries"}, $table->{"sorting_key_entries"}) 1 2 3
183  
184 3 return ([ clickhouse_table => \%options ]);
185 }
186  
187 # A bare column name stays a string; anything else is raw SQL and has to be a
188 # SCALAR ref, or clickhouse_table would quote cityHash64(id) as an identifier.
189 5 ▶sub _key_entry {
Subroutine _key_entry: called CC=2 SCAR=6.9
190 5 my ($entry) = @_;
191 5 ▶ return $entry if $entry =~ /\A\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /\A\w+\z/ 4 1 5
192 1 my $expression = $entry;
193 1 return \$expression;
194 }
195  
196 3 ▶sub _key_differs {
Subroutine _key_differs: called CC=6 SCAR=18.4
197 3 my ($left, $right) = @_;
198 3 ▶ $left ||= [];
Condition50%1/2
$left ||= []
l!l
30
MC/DC0%0/1
$left ||= []
$left
Truth table50%1/2
A
A= $left
Aresult
11
00
199 3 ▶ $right ||= [];
Condition50%1/2
$right ||= []
l!l
30
MC/DC0%0/1
$right ||= []
$right
Truth table50%1/2
A
A= $right
Aresult
11
00
200 3 ▶ return 1 if @$left != @$right;
Branch: 1 branch
BranchTrueFalseTotal
if @$left != @$right 1 2 3
201 2 for my $i (0 .. $#$left) {
202 2 ▶ return 1 if $left->[$i] ne $right->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $left->[$i] ne $right->[$i] 0 2 2
203 }
204 2 return 0;
205 }
206  
207  
208 34 1;
209  
210 __END__
« ClickHouse/Diff/Table.pm ↑ Summary ClickHouse/SQLMaker.pm »