blib/lib/DBIO/DuckDB/Util.pm

filter: Statementstmt 98.5% 67 / 68 Branchbran 88.8% 16 / 18 Conditioncond 76.4% 26 / 34 MC/DCmcdc 63.1% 12 / 19 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 87.7% 129 / 147 CC 25 SCAR 21.4
CC
25
Cov
91%
_mask_key
11.3
_split_excerpt
9.6
mask_error_details
7.0
CRAP 8.5
« blib/lib/DBIO/DuckDB.pm ↑ Summary blib/lib/DBIO/DuckDB/Test.pm »
1 package DBIO::DuckDB::Util;
2 # ABSTRACT: Shared DuckDB-specific helper functions for DBIO
3  
4 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '0.900007';
8  
9 35 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 35 our @EXPORT_OK = qw(mask_error_details);
12  
13 # What stands in for a value in an error text.
14 35 my $MASK = '***';
15  
16 # The shortest bound value that is looked for verbatim in an error text. A
17 # shorter one is a letter or a number, which is part of any text; the engine's
18 # sentences that quote a value are known below and catch those.
19 35 my $MIN_BOUND_LENGTH = 4;
20  
21  
22 # "key "a: 1, b: x"": the columns and the values of a key. Read as
23 # column: value pairs where that is the only reading, else all of it is masked.
24 # The text does not escape anything, so a value holding ", " or ": " could be
25 # read as one more column (and show part of the value as its name): such a
26 # text is masked whole, also when a bound value has either in it.
27 23 ▶sub _mask_key {
Subroutine _mask_key: called CC=11 SCAR=24.2
28 23 my ($key, $binds) = @_;
29  
30 23 ▶ for my $bound (@{ $binds // [] }) {
Condition100%2/2
$binds // []
l!l
716
MC/DC100%1/1
$binds // []
$binds
Truth table100%2/2
A
A= $binds
Aresult
11
00
31 12 ▶ return $MASK if defined $bound && !ref $bound && $bound =~ /, |: /;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bound and not ref $bound and $bound =~ /, |: / 4 8 12
Condition50%3/6
defined $bound and not ref $bound and $bound =~ /, |: /
!ll&&!rl&&r
084
defined $bound and not ref $bound
!ll&&!rl&&r
0012
MC/DC33%1/3
defined $bound and not ref $bound and $bound =~ /, |: /
defined $bound not ref $bound $bound =~ /, |: /
Truth table50%2/4
A and not(B) and C
A= defined $boundB= not ref $boundC= $bound =~ /, |: /
ABCresult
0XX0
10X0
1100
1111
32 }
33  
34 19 my (@names, %seen);
35 19 for my $pair (split /, /, $key, -1) {
36 27 ▶ my ($name, $value) = $pair =~ /\A([^:,"'\s][^:,"']*): (.*)\z/s
Branch: 1 branch
BranchTrueFalseTotal
() = $pair =~ /\A([^:,"'\s][^:,"']*): (.*)\z/s or return $MASK 26 1 27
37 or return $MASK;
38 26 ▶ return $MASK if $value =~ /: / or $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /: / or $seen{$name}++ 2 24 26
Condition100%3/3
$value =~ /: / or $seen{$name}++
l!l&&r!l&&!r
1124
MC/DC100%2/2
$value =~ /: / or $seen{$name}++
$value =~ /: / $seen{$name}++
Truth table100%3/3
A or B
A= $value =~ /: /B= $seen{$name}++
ABresult
1X1
011
000
39 24 push @names, $name;
40 }
41 16 ▶ return $MASK unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 16 16
42  
43 16 return join ', ', map { "$_: $MASK" } @names;
44 }
45  
46 # The sentences of the engine that hold a value, one rule each. A rule keeps
47 # the words around the value -- the type, the column, the constraint -- and
48 # replaces the value, wherever the engine puts quotes, so a rule never leaves
49 # a part of it. Every rule gives the same text again when it is applied to its
50 # own result.
51 86 ▶sub _mask_sentences {
Subroutine _mask_sentences: called CC=1 SCAR=0.0
52 86 my ($text, $binds) = @_;
53  
54 # Could not convert string 'x' to INT32 / string "x" to DECIMAL(3,1)
55 86 $text =~ s/(Could not convert string )(['"]).*\2( to \S+)/$1$2$MASK$2$3/s;
56  
57 # Type DOUBLE with value 3000000000.0 can't be cast ...
58 86 $text =~ s/(\bwith value ).*( can't be cast)/$1$MASK$2/s;
59  
60 # Duplicate key "id: 1" violates ... / because key "id: 1" does not exist ...
61 86 $text =~ s{(\bkey )"(.*)"(?= (?:violates|does not exist|is still referenced))}
62 23 {$1 . '"' . _mask_key($2, $binds) . '"'}se;
63  
64 # Malformed JSON at byte 1 of input: unexpected character. Input: "{x"
65 86 $text =~ s/(Malformed JSON at byte \d+ of input: .*?\bInput: ).*\z/$1"$MASK"/s;
66  
67 # date field value out of range: "x" / invalid timestamp field format: "x", expected ...
68 86 $text =~ s/(\bfield [^:\n]*: )"[^\n]*"((?:, expected format is [^"\n]*)?)$/$1"$MASK"$2/mg;
69  
70 # Could not cast value 99999.900000 to DECIMAL(4,1)
71 86 $text =~ s/(Could not cast value )[^\n]*( to \S+)$/$1$MASK$2/mg;
72  
73 # ... in string -> blob conversion of string "\xZZ": \xZZ
74 86 $text =~ s/(blob conversion of string )"[^\n]*"[^\n]*$/$1"$MASK": $MASK/mg;
75  
76 # Could not parse string "x" according to format specifier "%Y"\nx\n^
77 86 $text =~ s/(Could not parse string )"[^\n]*"( according to format specifier )/$1"$MASK"$2/g;
78 86 $text =~ s/(according to format specifier [^\n]*\n)[^\n]*(\n\^)/$1$MASK$2/g;
79  
80 # Overflow in multiplication of INT32 (5000 * 1000000)!
81 86 $text =~ s/(\bOverflow in \w+ of \w+ )\([^\n]*\)!/$1($MASK)!/g;
82  
83 # Unknown TimeZone 'x'! / extract specifier "x" not recognized / JSON path error near 'x'
84 86 $text =~ s/(Unknown TimeZone )'[^\n]*'!/$1'$MASK'!/g;
85 86 $text =~ s/(extract specifier )"[^\n]*"( not recognized)/$1"$MASK"$2/g;
86 86 $text =~ s/(JSON path error near )'[^\n]*'/$1'$MASK'/g;
87 86 $text =~ s/(Cannot create a string of size: )'[^'\n]*'/$1'$MASK'/g;
88  
89 # Invalid character encountered in string -> bit conversion: 'g'
90 86 $text =~ s/(\bbit conversion: )'[^\n]*'/$1'$MASK'/g;
91  
92 # Invalid Input Error: <what>: <the fragment of the input> (regex, hex, ...)
93 86 $text =~ s/(Invalid Input Error: [^:\n]*: )[^\n]*$/$1$MASK/mg;
94  
95 86 return $text;
96 }
97  
98  
99 # The excerpt DuckDB appends to the message of a statement it refuses, if the
100 # text ends with one: "\n\nLINE n: <a piece of line n of the statement>\n<spaces>^".
101 # The piece is cut with ... at the start and the end where the line is long.
102 # Returns ($message, $excerpt); the excerpt is empty unless it is DuckDB's.
103 86 ▶sub _split_excerpt {
Subroutine _split_excerpt: called CC=9 SCAR=22.6
104 86 my ($message, $statement) = @_;
105  
106 86 ▶ return ($message, '') unless defined $statement && !ref $statement && length $statement;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $statement and not ref $statement and length $statement 73 13 86
Condition66%4/6
defined $statement and not ref $statement and length $statement
!ll&&!rl&&r
73013
defined $statement and not ref $statement
!ll&&!rl&&r
73013
MC/DC33%1/3
defined $statement and not ref $statement and length $statement
defined $statement not ref $statement length $statement
Truth table50%2/4
A and not(B) and C
A= defined $statementB= not ref $statementC= length $statement
ABCresult
0XX0
10X0
1100
1111
107 13 ▶ $message =~ /\A(.*)(\n\nLINE (\d+): ([^\n]*)\n[ \t]*\^)\z/s
Branch: 1 branch
BranchTrueFalseTotal
$message =~ /\A(.*)(\n\nLINE (\d+): ([^\n]*)\n[ \t]*\^)\z/s or return $message, "" 8 5 13
108 or return ($message, '');
109 8 my ($head, $excerpt, $line, $piece) = ($1, $2, $3, $4);
110  
111 8 my @lines = split /\r?\n/, $statement, -1;
112 8 ▶ return ($message, '') unless $line >= 1 && $line <= @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless $line >= 1 and $line <= @lines 0 8 8
Condition33%1/3
$line >= 1 and $line <= @lines
!ll&&!rl&&r
008
MC/DC0%0/2
$line >= 1 and $line <= @lines
$line >= 1 $line <= @lines
Truth table33%1/3
A and B
A= $line >= 1B= $line <= @lines
ABresult
0X0
100
111
113  
114 8 $piece =~ s/\r\z//;
115 8 $piece =~ s/\A\.\.\.//;
116 8 $piece =~ s/\.\.\.\z//;
117 8 ▶ return ($message, '') unless length $piece && index($lines[$line - 1], $piece) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $piece and index($lines[$line - 1], $piece) != -1 2 6 8
Condition66%2/3
length $piece and index($lines[$line - 1], $piece) != -1
!ll&&!rl&&r
026
MC/DC50%1/2
length $piece and index($lines[$line - 1], $piece) != -1
length $piece index($lines[$line - 1], $piece) != -1
Truth table66%2/3
A and B
A= length $pieceB= index($lines[$line - 1], $piece) != -1
ABresult
0X0
100
111
118  
119 6 return ($head, $excerpt);
120 }
121  
122 88 ▶sub mask_error_details {
Subroutine mask_error_details: called CC=7 SCAR=19.5
Pod documented
123 88 my ($message, $binds, $statement) = @_;
124 88 ▶ return $message unless defined $message && length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message 2 86 88
Condition100%3/3
defined $message and length $message
!ll&&!rl&&r
1186
MC/DC100%2/2
defined $message and length $message
defined $message length $message
Truth table100%3/3
A and B
A= defined $messageB= length $message
ABresult
0X0
100
111
125  
126 86 my $tail;
127 86 ($message, $tail) = _split_excerpt($message, $statement);
128  
129 86 $message = _mask_sentences($message, $binds);
130  
131 0 my @bound = sort { length $b <=> length $a }
132 86 ▶ grep { defined && !ref && length >= $MIN_BOUND_LENGTH } @{ $binds // [] };
Condition100%8/8
defined $_ && !ref($_) && length $_ >= $MIN_BOUND_LENGTH
!ll&&!rl&&r
2810
defined $_ and not ref $_
!ll&&!rl&&r
1118
$binds // []
l!l
1274
MC/DC100%3/3
defined $_ && !ref($_) && length $_ >= $MIN_BOUND_LENGTH
defined $_ not ref $_ length $_ >= $MIN_BOUND_LENGTH
MC/DC100%1/1
$binds // []
$binds
Truth table100%4/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_ >= $MIN_BOUND_LENGTH
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $binds
Aresult
11
00
133 86 for my $bound (@bound) {
134 10 $message =~ s/\Q$bound\E/$MASK/g;
135 }
136  
137 86 return $message . $tail;
138 }
139  
140  
141 35 1;
142  
143 __END__
« blib/lib/DBIO/DuckDB.pm ↑ Summary blib/lib/DBIO/DuckDB/Test.pm »