ClickHouse/Introspect/Util.pm

filter: Statementstmt 92.4% 122 / 132 Branchbran 79.3% 73 / 92 Conditioncond 72.4% 21 / 29 MC/DCmcdc 55.5% 10 / 18 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 84.2% 241 / 286 CC 61 SCAR 23.4
CC
61
Cov
85%
_match_paren
15.9
split_top_level
15.7
_base_type
12.0
CRAP 10.4
« ClickHouse/Diff/View.pm ↑ Summary ClickHouse/Test.pm »
1 package DBIO::ClickHouse::Introspect::Util;
2 # ABSTRACT: ClickHouse type-string parser shared by the introspect submodules
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  
10  
11 # Portable spelling for each ClickHouse base type. Where the DDL type map is
12 # many-to-one (real/float -> Float32, double/double precision -> Float64,
13 # datetime/timestamp -> DateTime64(3)) this picks the representative; every
14 # entry here still renders back to the key it came from.
15 34 my %BASE_TYPE = (
16 Int8 => 'tinyint',
17 Int16 => 'smallint',
18 Int32 => 'integer',
19 Int64 => 'bigint',
20 Float32 => 'real',
21 Float64 => 'double precision',
22 String => 'text',
23 Date32 => 'date',
24 Bool => 'boolean',
25 UUID => 'uuid',
26 );
27  
28 34 my %UNSIGNED_BASE_TYPE = (
29 UInt8 => 'tinyint',
30 UInt16 => 'smallint',
31 UInt32 => 'integer',
32 UInt64 => 'bigint',
33 );
34  
35 # Decimal32/64/128/256(S) are fixed-precision aliases; system.columns normally
36 # reports the canonical Decimal(P, S), but a server that echoes the alias back
37 # must still land on the same [precision, scale].
38 34 my %DECIMAL_ALIAS_PRECISION = (
39 32 => 9,
40 64 => 18,
41 128 => 38,
42 256 => 76,
43 );
44  
45 143 ▶sub normalize_type {
Subroutine normalize_type: called CC=7 SCAR=19.5
Pod documented
46 143 my ($class, $raw) = @_;
47  
48 143 ▶ my $type = defined $raw ? $raw : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 143 0 143
49 143 $type =~ s/\A\s+//;
50 143 $type =~ s/\s+\z//;
51  
52 143 my %info = (native_type => $type, is_nullable => 0, extra => {});
53 143 ▶ return { %info, data_type => '' } unless length $type;
Branch: 1 branch
BranchTrueFalseTotal
unless length $type 0 143 143
54  
55 # LowCardinality is the outermost wrapper the DDL layer emits, and the only
56 # order ClickHouse accepts: LowCardinality(Nullable(T)).
57 143 ▶ if (defined(my $inner = _unwrap('LowCardinality', $type))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $inner = _unwrap("LowCardinality", $type))) 10 133 143
58 10 $info{extra}{low_cardinality} = 1;
59 10 $type = $inner;
60 }
61  
62 143 ▶ if (defined(my $inner = _unwrap('Nullable', $type))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $inner = _unwrap("Nullable", $type))) 14 129 143
63 14 $info{is_nullable} = 1;
64 14 $type = $inner;
65 }
66  
67 143 my $base = $class->_base_type($type);
68  
69 143 $info{data_type} = $base->{data_type};
70 143 ▶ $info{size} = $base->{size} if defined $base->{size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $base->{"size"} 37 106 143
71 143 ▶ $info{extra} = { %{ $info{extra} }, %{ $base->{extra} || {} } };
Condition100%2/2
$base->{'extra'} || {}
l!l
4697
MC/DC100%1/1
$base->{'extra'} || {}
$base->{'extra'}
Truth table100%2/2
A
A= $base->{'extra'}
Aresult
11
00
72  
73 143 return \%info;
74 }
75  
76 143 ▶sub _base_type {
Subroutine _base_type: called CC=12 SCAR=24.9
77 143 my ($class, $type) = @_;
78  
79 # Array(T): the portable DBIO/DBIC spelling is a trailing "[]".
80 143 ▶ if (defined(my $inner = _unwrap('Array', $type))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $inner = _unwrap("Array", $type))) 8 135 143
81 8 my $n = $class->normalize_type($inner);
82  
83 # The DDL layer refuses to wrap a composite type in Nullable /
84 # LowCardinality, so an element type carrying either cannot be rebuilt
85 # from "<T>[]" plus flags. Keep the whole type verbatim -- it round-trips
86 # through the DDL layer's pass-through for parameterized types.
87 return { data_type => $type }
88 8 ▶ if $n->{is_nullable} || $n->{extra}{low_cardinality};
Branch: 1 branch
BranchTrueFalseTotal
if $n->{"is_nullable"} or $n->{"extra"}{"low_cardinality"} 4 4 8
Condition66%2/3
$n->{"is_nullable"} or $n->{"extra"}{"low_cardinality"}
l!l&&r!l&&!r
404
MC/DC50%1/2
$n->{"is_nullable"} or $n->{"extra"}{"low_cardinality"}
$n->{"is_nullable"} $n->{"extra"}{"low_cardinality"}
Truth table66%2/3
A or B
A= $n->{"is_nullable"}B= $n->{"extra"}{"low_cardinality"}
ABresult
1X1
011
000
89  
90 return {
91 data_type => $n->{data_type} . '[]',
92 (defined $n->{size} ? (size => $n->{size}) : ()),
93 extra => $n->{extra},
94 4 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined $n->{'size'} ? : 0 4 4
95 }
96  
97 # FixedString(N) is opt-in on the DDL side and must stay opt-in here, or a
98 # regenerated schema would silently turn every char column into FixedString.
99 135 ▶ return { data_type => 'char', size => 0 + $1, extra => { fixed_string => 1 } }
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\AFixedString\(\s*([0-9]+)\s*\)\z/ 4 131 135
100 if $type =~ /\AFixedString\(\s*([0-9]+)\s*\)\z/;
101  
102 131 ▶ return { data_type => 'decimal', size => [ 0 + $1, 0 + $2 ] }
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\ADecimal\(\s*([0-9]+)\s*,\s*([0-9]+)\s*\)\z/ 8 123 131
103 if $type =~ /\ADecimal\(\s*([0-9]+)\s*,\s*([0-9]+)\s*\)\z/;
104  
105 return {
106 data_type => 'decimal',
107 123 ▶ size => [ $DECIMAL_ALIAS_PRECISION{$1}, 0 + $2 ],
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\ADecimal(32|64|128|256)\(\s*([0-9]+)\s*\)\z/ 1 122 123
108 } if $type =~ /\ADecimal(32|64|128|256)\(\s*([0-9]+)\s*\)\z/;
109  
110 122 ▶ return $class->_datetime64_type($type, 0 + $1, $2)
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\ADateTime64\(\s*([0-9]+)\s*(?:,\s*'([^']*)'\s*)?\)\z/ 24 98 122
111 if $type =~ /\ADateTime64\(\s*([0-9]+)\s*(?:,\s*'([^']*)'\s*)?\)\z/;
112  
113 98 ▶ return $class->_enum_type($type, $1)
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\AEnum(?:8|16)\((.*)\)\z/s 8 90 98
114 if $type =~ /\AEnum(?:8|16)\((.*)\)\z/s;
115  
116 return { data_type => $BASE_TYPE{$type} }
117 90 ▶ if exists $BASE_TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BASE_TYPE{$type} 66 24 90
118  
119 return { data_type => $UNSIGNED_BASE_TYPE{$type}, extra => { unsigned => 1 } }
120 24 ▶ if exists $UNSIGNED_BASE_TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $UNSIGNED_BASE_TYPE{$type} 6 18 24
121  
122 # Everything else (IPv6, Int128, Point, Map(...), Tuple(...), bare Date /
123 # DateTime) passes through verbatim, which is what the DDL layer does with a
124 # natively-spelled data_type in the other direction.
125 18 return { data_type => $type };
126 }
127  
128 # DateTime64 is the only temporal type the DDL layer parameterizes, and only at
129 # precision 3 (the millisecond resolution every other DBIO driver's `timestamp`
130 # gives you). Any other precision, or any timezone other than UTC, has no
131 # portable spelling and is kept verbatim -- with the precision still exposed as
132 # `size` so callers do not have to re-parse the type string.
133 24 ▶sub _datetime64_type {
Subroutine _datetime64_type: called CC=7 SCAR=19.5
134 24 my ($class, $type, $precision, $timezone) = @_;
135  
136 24 ▶ my %extra = defined $timezone ? (timezone => $timezone) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $timezone ? : 8 16 24
137  
138 24 ▶ return { data_type => 'datetime', size => $precision, extra => \%extra }
Branch: 1 branch
BranchTrueFalseTotal
if $precision == 3 and not defined $timezone 12 12 24
Condition100%3/3
$precision == 3 and not defined $timezone
!ll&&!rl&&r
4812
MC/DC100%2/2
$precision == 3 and not defined $timezone
$precision == 3 not defined $timezone
Truth table100%3/3
A and not(B)
A= $precision == 3B= not defined $timezone
ABresult
0X0
100
111
139 if $precision == 3 && !defined $timezone;
140  
141 12 ▶ return { data_type => 'timestamptz', size => $precision, extra => \%extra }
Branch: 1 branch
BranchTrueFalseTotal
if $precision == 3 and defined $timezone and $timezone eq "UTC" 4 8 12
Condition83%5/6
$precision == 3 and defined $timezone and $timezone eq "UTC"
!ll&&!rl&&r
444
$precision == 3 and defined $timezone
!ll&&!rl&&r
408
MC/DC66%2/3
$precision == 3 and defined $timezone and $timezone eq "UTC"
$precision == 3 defined $timezone $timezone eq "UTC"
Truth table75%3/4
A and B and C
A= $precision == 3B= defined $timezoneC= $timezone eq "UTC"
ABCresult
0XX0
10X0
1100
1111
142 if $precision == 3 && defined $timezone && $timezone eq 'UTC';
143  
144 8 return { data_type => $type, size => $precision, extra => \%extra };
145 }
146  
147 # Enum8('a' = 1, 'b' = 2). The DDL layer numbers members 1..N in declaration
148 # order, so only that exact numbering can be expressed as the portable
149 # `enum` + extra->{list}; anything else stays verbatim.
150 8 ▶sub _enum_type {
Subroutine _enum_type: called CC=7 SCAR=19.5
151 8 my ($class, $type, $body) = @_;
152  
153 8 my (@names, @values);
154 8 for my $member ($class->split_top_level($body)) {
155 16 ▶ return { data_type => $type }
Branch: 1 branch
BranchTrueFalseTotal
unless $member =~ /\A'((?:[^'\\]|\\.)*)'\s*=\s*(-?[0-9]+)\z/ 0 16 16
156 unless $member =~ /\A'((?:[^'\\]|\\.)*)'\s*=\s*(-?[0-9]+)\z/;
157 16 push @names, $class->unquote_string("'$1'");
158 16 push @values, 0 + $2;
159 }
160  
161 8 ▶ return { data_type => $type } unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 8 8
162  
163 8 my $sequential = 1;
164 8 for my $i (0 .. $#values) {
165 12 ▶ next if $values[$i] == $i + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $values[$i] == $i + 1 8 4 12
166 4 $sequential = 0;
167 4 last;
168 }
169  
170 8 ▶ return { data_type => 'enum', extra => { list => \@names } } if $sequential;
Branch: 1 branch
BranchTrueFalseTotal
if $sequential 4 4 8
171  
172 4 return { data_type => $type, extra => { list => \@names } };
173 }
174  
175  
176 2 ▶sub view_select {
Subroutine view_select: called CC=5 SCAR=17.8
Pod documented
177 2 my ($class, $create) = @_;
178 2 ▶ return undef unless defined $create && length $create;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $create and length $create 0 2 2
Condition33%1/3
defined $create and length $create
!ll&&!rl&&r
002
MC/DC0%0/2
defined $create and length $create
defined $create length $create
Truth table33%1/3
A and B
A= defined $createB= length $create
ABresult
0X0
100
111
179  
180 2 ▶ return undef unless $create =~ s/
Branch: 1 branch
BranchTrueFalseTotal
unless $create =~ s/ \A \s* CREATE \s+ (?: OR \s+ REPLACE \s+ )? (?: MATERIALIZED \s+ | LIVE \s+ | WINDOW \s+ )? VIEW \s+ .*? \s AS \s+ //six 0 2 2
181 \A \s* CREATE \s+ (?: OR \s+ REPLACE \s+ )?
182 (?: MATERIALIZED \s+ | LIVE \s+ | WINDOW \s+ )? VIEW \s+
183 .*? \s AS \s+
184 //xsi;
185  
186 2 $create =~ s/\s+\z//;
187 2 $create =~ s/\s*;\s*\z//;
188 2 ▶ return length($create) ? $create : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $create ? : 2 0 2
189 }
190  
191  
192 24 ▶sub split_top_level {
Subroutine split_top_level: called CC=15 SCAR=27.5
Pod documented
193 24 my ($class, $string) = @_;
194 24 ▶ return () unless defined $string && length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and length $string 7 17 24
Condition66%2/3
defined $string and length $string
!ll&&!rl&&r
0717
MC/DC50%1/2
defined $string and length $string
defined $string length $string
Truth table66%2/3
A and B
A= defined $stringB= length $string
ABresult
0X0
100
111
195  
196 17 my @entries;
197 17 my $current = '';
198 17 my $depth = 0;
199 17 my $quoted = 0;
200 17 my $escaped = 0;
201  
202 17 for my $char (split //, $string) {
203 0 ▶ if ($escaped) { $current .= $char; $escaped = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped) 0 236 236
204  
205 236 ▶ if ($quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted) 66 170 236
206 66 $current .= $char;
207 66 ▶ $escaped = 1 if $char eq "\\";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\\" 0 66 66
208 66 ▶ $quoted = 0 if $char eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "'" 18 48 66
209 66 next;
210 }
211  
212 170 ▶ if ($char eq "'") { $quoted = 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($char eq q[']) { } 18 152 170
elsif ($char eq '(' or $char eq '[') { } 3 149 152
elsif ($char eq ')' or $char eq ']') { } 3 146 149
elsif ($char eq ',' and $depth == 0) { } 13 266 279
213 3 ▶ elsif ($char eq '(' || $char eq '[') { $depth++ }
Condition66%2/3
$char eq "(" or $char eq "["
l!l&&r!l&&!r
30149
MC/DC50%1/2
$char eq "(" or $char eq "["
$char eq "(" $char eq "["
Truth table66%2/3
A or B
A= $char eq "("B= $char eq "["
ABresult
1X1
011
000
214 3 ▶ elsif ($char eq ')' || $char eq ']') { $depth-- }
Condition66%2/3
$char eq ")" or $char eq "]"
l!l&&r!l&&!r
30146
MC/DC50%1/2
$char eq ")" or $char eq "]"
$char eq ")" $char eq "]"
Truth table66%2/3
A or B
A= $char eq ")"B= $char eq "]"
ABresult
1X1
011
000
215 ▶ elsif ($char eq ',' && $depth == 0) {
Condition66%2/3
$char eq "," and $depth == 0
!ll&&!rl&&r
133013
MC/DC50%1/2
$char eq "," and $depth == 0
$char eq "," $depth == 0
Truth table66%2/3
A and B
A= $char eq ","B= $depth == 0
ABresult
0X0
100
111
216 13 push @entries, $current;
217 13 $current = '';
218 13 next;
219 }
220  
221 157 $current .= $char;
222 }
223 17 push @entries, $current;
224  
225 17 return grep { length } map { my $e = $_; $e =~ s/\A\s+//; $e =~ s/\s+\z//; $e } @entries;
226 }
227  
228  
229 31 ▶sub unquote_string {
Subroutine unquote_string: called CC=3 SCAR=11.0
Pod documented
230 31 my ($class, $string) = @_;
231 31 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 31 31
232 31 ▶ return undef unless $string =~ /\A'((?:[^'\\]|\\.)*)'\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\A'((?:[^'\\]|\\.)*)'\z/s 8 23 31
233 23 my $value = $1;
234 23 $value =~ s/\\(.)/$1/gs;
235 23 return $value;
236 }
237  
238 # Strip "Wrapper(...)" and return the inside, or undef when $type is not that
239 # wrapper applied to the whole string. The paren scan is quote-aware so that
240 # Enum8('a)b' = 1) does not confuse it.
241 429 ▶sub _unwrap {
Subroutine _unwrap: called CC=3 SCAR=11.0
242 429 my ($wrapper, $type) = @_;
243 429 ▶ return undef unless $type =~ /\A\Q$wrapper\E\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /\A\Q$wrapper\E\(/ 397 32 429
244  
245 32 my $open = length $wrapper;
246 32 my $close = _match_paren($type, $open);
247 32 ▶ return undef unless $close == length($type) - 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $close == length($type) - 1 0 32 32
248  
249 32 return substr $type, $open + 1, $close - $open - 1;
250 }
251  
252 # Index of the ')' matching the '(' at $open, or -1.
253 32 ▶sub _match_paren {
Subroutine _match_paren: called CC=10 SCAR=27.7
254 32 my ($string, $open) = @_;
255  
256 32 my @chars = split //, $string;
257 32 my $depth = 0;
258 32 my $quoted = 0;
259 32 my $escaped = 0;
260  
261 32 for my $i ($open .. $#chars) {
262 388 my $char = $chars[$i];
263 0 ▶ if ($escaped) { $escaped = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped) 0 388 388
264  
265 388 ▶ if ($quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted) 0 388 388
266 0 ▶ $escaped = 1 if $char eq "\\";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\\" 0 0 0
267 0 ▶ $quoted = 0 if $char eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "'" 0 0 0
268 0 next;
269 }
270  
271 0 ▶ if ($char eq "'") { $quoted = 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq q[']) { } 0 388 388
elsif ($char eq '(') { } 46 342 388
elsif ($char eq ')') { } 46 296 342
272 46 elsif ($char eq '(') { $depth++ }
273 46 ▶ elsif ($char eq ')') { $depth--; return $i unless $depth }
Branch: 1 branch
BranchTrueFalseTotal
unless $depth 32 14 46
274 }
275  
276 0 return -1;
277 }
278  
279  
280 34 1;
281  
282 __END__
« ClickHouse/Diff/View.pm ↑ Summary ClickHouse/Test.pm »