ClickHouse/Util.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 90.6% 29 / 32 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 96.9% 125 / 129 CC 20 SCAR 15.9
CC
20
Cov
96%
_mask_column_values
8.1
_mask_sentences
6.0
mask_error_details
4.0
CRAP 4.9
« ClickHouse/Introspect/Indexes.pm ↑ Summary ClickHouse/Introspect/Tables.pm »
1 package DBIO::ClickHouse::Util;
2 # ABSTRACT: Shared ClickHouse-specific helper functions for DBIO
3  
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 our $VERSION = '0.900007';
8  
9 17 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 our @EXPORT_OK = qw(mask_error_details);
12  
13 # What stands in for a value in an error text.
14 17 my $MASK = '***';
15  
16 # A single-quoted text of the engine. It escapes a quote inside as \' where it
17 # writes the text itself, but not everywhere (Cannot convert string 'a'b' to
18 # type ...): the escape-aware reading is tried first, the greedy one is the
19 # fallback that runs to the last quote the sentence allows.
20 17 my $QUOTED = qr/(?s:'(?:[^'\\]|\\.)*'|'.*')/;
21  
22 # Where a text of the engine that is not quoted ends: the next of its own
23 # sentences, or the error code that closes the message.
24 17 my $UNQUOTED_END = qr/(?=: Cannot parse \w+ from String|: [Ii]n scope |: [Ww]hile |\. \([A-Z][A-Z0-9_]*\)|\z)/;
25  
26 # The last error code of the message: ". (CANNOT_PARSE_TEXT)"; the engine
27 # ends every message with it, then its version.
28 17 my $CODE = qr/\. \([A-Z][A-Z0-9_]*\)/;
29  
30  
31 # Every literal of a piece of SQL -- a string, a number -- is replaced.
32 # Identifiers, keywords and function names stay: a bare word, a `quoted` or
33 # "quoted" name. A string that is cut off (the engine shortens a long excerpt)
34 # is masked up to the end of the piece.
35 55 ▶sub _mask_literals {
Subroutine _mask_literals: called CC=2 SCAR=6.9
36 55 my ($sql) = @_;
37  
38 55 $sql =~ s{
39 (?<keep>
40 `(?:[^`\\]|\\.|``)*`
41 | "(?:[^"\\]|\\.|"")*"
42 | [A-Za-z_]\w*
43 )
44 |
45 (?<lit>
46 '(?:[^'\\]|\\.|'')*'
47 | '.*\z
48 | 0[xXbB][0-9A-Fa-f]+
49 | \d+(?:\.\d*)?(?:[eE][+-]?\d+)?
50 | \.\d+(?:[eE][+-]?\d+)?
51 )
52 220 ▶ }{ defined $+{keep} ? $+{keep} : $MASK }gesx;
Branch: 1 branch
BranchTrueFalseTotal
defined $+{'keep'} ? : 169 51 220
53  
54 55 return $sql;
55 }
56  
57 # "a = 1, b = 'x'": the values of the row that broke a constraint. Read as
58 # name = value pairs where that is the only reading, else all of it is masked.
59 8 ▶sub _mask_column_values {
Subroutine _mask_column_values: called CC=8 SCAR=20.9
60 8 my ($values) = @_;
61  
62 8 my (@names, %seen);
63 8 while (1) {
64 16 ▶ $values =~ /\G(`(?:[^`\\]|\\.)+`|[A-Za-z_][\w.]*) = /gc or return $MASK;
Branch: 1 branch
BranchTrueFalseTotal
$values =~ /\G(`(?:[^`\\]|\\.)+`|[A-Za-z_][\w.]*) = /cg or return $MASK 15 1 16
65 15 ▶ return $MASK if $seen{$1}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$1}++ 0 15 15
66 15 push @names, $1;
67  
68 15 ▶ if ($values =~ /\G'(?:[^'\\]|\\.)*'/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($values =~ /\G'(?:[^'\\]|\\.)*'/cg) { } 5 10 15
elsif ($values =~ /\G[\[\(\{]/cg) { } 1 9 10
69 # a string, escaped the way the engine writes it
70 }
71 elsif ($values =~ /\G[\[\(\{]/gc) {
72 1 return $MASK; # a compound value holds commas and names of its own
73 }
74 else {
75 9 ▶ $values =~ /\G.+?(?=, (?:`[^`]+`|[A-Za-z_][\w.]*) = |\z)/gcs or return $MASK;
Branch: 1 branch
BranchTrueFalseTotal
$values =~ /\G.+?(?=, (?:`[^`]+`|[A-Za-z_][\w.]*) = |\z)/cgs or return $MASK 9 0 9
76 }
77  
78 14 ▶ last if pos($values) == length $values;
Branch: 1 branch
BranchTrueFalseTotal
if pos $values == length $values 6 8 14
79 8 ▶ $values =~ /\G, /gc or return $MASK;
Branch: 1 branch
BranchTrueFalseTotal
$values =~ /\G, /cg or return $MASK 8 0 8
80 }
81  
82 6 return join ', ', map { "$_ = $MASK" } @names;
83 }
84  
85 # "Syntax error: failed at position 1 (UPDATE): UPDATE t SET ... . Expected
86 # one of: ...": the text after the position is the statement as sent, values
87 # included. The token the parser stopped at stays only where it cannot be a
88 # value: a word or punctuation, no literal.
89 13 ▶sub _mask_syntax_error {
Subroutine _mask_syntax_error: called CC=3 SCAR=11.0
90 13 my ($text) = @_;
91  
92 13 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
93 13 my ($head, $token, $rest) = @_;
94 13 ▶ $token = $MASK
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /\A(?:[A-Za-z_]\w*(?: \w+)*|[^\w\s'"`\$]{1,3}|`[^`]*`)\z/ 3 10 13
95 unless $token =~ /\A(?:[A-Za-z_]\w*(?: \w+)*|[^\w\s'"`\$]{1,3}|`[^`]*`)\z/;
96 13 return $head . $token . '): ' . _mask_literals($rest);
97 13 };
98  
99 13 my $head = qr/(?s:(Syntax error[^\n:]*: failed at position \d+ \()(.*?)\): )/;
100  
101 13 ▶ $text =~ s/$head(.*)(?=\. Expected )/$sub->($1, $2, $3)/se
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/$head(.*)(?=\. Expected )/&$sub($1, $2, $3);/es or $text =~ s/$head(.*)(?=$CODE)/&$sub($1, $2, $3);/es or $text =~ s/$head(.*)\z/&$sub($1, $2, $3);/es 12 1 13
Condition100%3/3
$text =~ s/$head(.*)(?=\. Expected )/&$sub($1, $2, $3);/es or $text =~ s/$head(.*)(?=$CODE)/&$sub($1, $2, $3);/es
l!l&&r!l&&!r
1021
MC/DC100%2/2
$text =~ s/$head(.*)(?=\. Expected )/&$sub($1, $2, $3);/es or $text =~ s/$head(.*)(?=$CODE)/&$sub($1, $2, $3);/es
$text =~ s/$head(.*)(?=\. Expected )/&$sub($1, $2, $3);/es $text =~ s/$head(.*)(?=$CODE)/&$sub($1, $2, $3);/es
Truth table100%3/3
A or B
A= $text =~ s/$head(.*)(?=\. Expected )/&$sub($1, $2, $3);/esB= $text =~ s/$head(.*)(?=$CODE)/&$sub($1, $2, $3);/es
ABresult
1X1
011
000
102 2 or $text =~ s/$head(.*)(?=$CODE)/$sub->($1, $2, $3)/se
103 1 or $text =~ s/$head(.*)\z/$sub->($1, $2, $3)/se;
104  
105 13 return $text;
106 }
107  
108 # The sentences of the engine that hold a value, one rule each. A rule keeps
109 # the words around the value -- the type, the column, the constraint -- and
110 # replaces the value, wherever the engine puts quotes, so a rule never leaves
111 # a part of it. Every rule gives the same text again when it is applied to its
112 # own result.
113 116 ▶sub _mask_sentences {
Subroutine _mask_sentences: called CC=6 SCAR=17.9
114 116 my ($text) = @_;
115  
116 # Cannot parse string 'x' as Int32: ... (parsed just 'x')
117 116 $text =~ s/(Cannot parse string )$QUOTED( as )/$1'$MASK'$2/;
118 116 $text =~ s/(\(parsed just )'.*?'(\)(?:\. |: |\z))/$1'$MASK'$2/gs;
119  
120 # Cannot convert string 'x' to type Int32 / Unknown element 'x' for enum
121 116 $text =~ s/(Cannot convert string )$QUOTED( to type )/$1'$MASK'$2/;
122 116 $text =~ s/(Unknown element )$QUOTED( for enum)/$1'$MASK'$2/;
123  
124 # Cannot parse boolean value here: 'x', should be 'true' or 'false' ...
125 116 $text =~ s/(Cannot parse boolean value here: )$QUOTED(, should be )/$1'$MASK'$2/;
126  
127 # Cannot read array from text, expected comma or end of array, found 'g'
128 116 $text =~ s/(Cannot read array from text[^\n]*?, found )'.*?'(?=: [Ii]n scope |: [Ww]hile |\. \(|\z)/$1'$MASK'/gs;
129  
130 # Cannot parse date here: x: Cannot parse Date from String
131 # Cannot parse uuid x: Cannot parse UUID from String / IPv4 / IPv6
132 116 $text =~ s/(Cannot parse date here: ).*(: Cannot parse \w+ from String)/$1$MASK$2/s;
133 116 $text =~ s/(Cannot parse (?:uuid|IPv4|IPv6) ).*(: Cannot parse \w+ from String)/$1$MASK$2/s;
134  
135 # Cannot parse JSON object here: {x / Cannot parse expression of type Int32 here: 'x')
136 116 $text =~ s/(Cannot parse (?!boolean )(?:[\w ]+?) here: ).*?$UNQUOTED_END/$1$MASK/gs;
137  
138 # Cannot parse input: expected '{' before: 'x': ... / while converting 'x' to Map(...)
139 116 $text =~ s/(\bbefore: )'.*?'(?=: [Ii]n scope |: [Ww]hile |: ?\(at row|:\s+at row|\. \([A-Z]|\s*\z)/$1'$MASK'/gs;
140 116 $text =~ s/(\bwhile converting )'.*?'(?= to )/$1'$MASK'/gs;
141  
142 # while executing 'FUNCTION equals(id : 0, 'x' : 1) -> equals(id, 'x') UInt8 : 2'
143 # The plan holds the constants of the statement; nothing in it names a cause.
144 116 $text =~ s/(\bwhile executing )'.*'(?=: While executing |$CODE|\s*\z)/$1'$MASK'/s;
145  
146 # While processing _CAST(if(id = 1, _CAST('x', 'Date'), d), 'Date'). (CODE)
147 116 ▶ $text =~ s/(\bWhile processing )(.*)(?=$CODE)/$1 . _mask_literals($2)/se
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/(\bWhile processing )(.*)(?=$CODE)/$1 . _mask_literals($2);/es or $text =~ s/(\bWhile processing )(.*)\z/$1 . _mask_literals($2);/es 2 114 116
148 0 or $text =~ s/(\bWhile processing )(.*)\z/$1 . _mask_literals($2)/se;
149  
150 # ... In scope SELECT CAST('x', 'Int32') FROM t. (CODE) / Unknown ... in scope ...
151 116 ▶ $text =~ s/(\b[Ii]n scope )(.*)(?=$CODE)/$1 . _mask_literals($2)/se
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/(\b[Ii]n scope )(.*)(?=$CODE)/$1 . _mask_literals($2);/es or $text =~ s/(\b[Ii]n scope )(.*)\z/$1 . _mask_literals($2);/es 38 78 116
152 2 or $text =~ s/(\b[Ii]n scope )(.*)\z/$1 . _mask_literals($2)/se;
153  
154 # Syntax error: failed at position N (TOKEN): <statement>. Expected one of: ...
155 116 ▶ $text = _mask_syntax_error($text) if $text =~ /Syntax error[^\n:]*: failed at position \d+ \(/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /Syntax error[^\n:]*: failed at position \d+ \(/ 13 103 116
156  
157 # Constraint `c` for table d.t is violated at row 1. Expression: (a > 0). Column values: a = 1, b = 'x'. (CODE)
158 116 ▶ $text =~ s/(\bColumn values: )(.*)(?=$CODE)/$1 . _mask_column_values($2)/se
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/(\bColumn values: )(.*)(?=$CODE)/$1 . _mask_column_values($2);/es or $text =~ s/(\bColumn values: )(.*)\z/$1 . _mask_column_values($2);/es 7 109 116
159 1 or $text =~ s/(\bColumn values: )(.*)\z/$1 . _mask_column_values($2)/se;
160  
161 # Value 60913 for year must be in the range [1970, 2106]
162 116 $text =~ s/(\bValue )\S+( (?:for [\w ]+? )?(?:must be|cannot be|is out of|out of)\b)/$1$MASK$2/g;
163  
164 # OptimizedRegularExpression: cannot compile re2: x, error: missing ): x. Look at ...
165 116 ▶ $text =~ s/(cannot compile re2: )(.*?)(, error: .*?: )\2(\. Look at )/$1$MASK$3$MASK$4/s
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/(cannot compile re2: )(.*?)(, error: .*?: )\2(\. Look at )/$1$MASK$3$MASK$4/s or $text =~ s/(cannot compile re2: ).*?(\. Look at )/$1$MASK$2/s 2 114 116
166 or $text =~ s/(cannot compile re2: ).*?(\. Look at )/$1$MASK$2/s;
167  
168 116 return $text;
169 }
170  
171  
172 119 ▶sub mask_error_details {
Subroutine mask_error_details: called CC=4 SCAR=13.9
Pod documented
173 119 my ($message) = @_;
174 119 ▶ return $message unless defined $message && !ref $message && length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and not ref $message and length $message 3 116 119
Condition100%6/6
defined $message and not ref $message and length $message
!ll&&!rl&&r
21116
defined $message and not ref $message
!ll&&!rl&&r
11117
MC/DC100%3/3
defined $message and not ref $message and length $message
defined $message not ref $message length $message
Truth table100%4/4
A and not(B) and C
A= defined $messageB= not ref $messageC= length $message
ABCresult
0XX0
10X0
1100
1111
175  
176 116 return _mask_sentences($message);
177 }
178  
179  
180 17 1;
181  
182 __END__
« ClickHouse/Introspect/Indexes.pm ↑ Summary ClickHouse/Introspect/Tables.pm »