| 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 // []
MC/DC100%1/1
$binds // []
$binds
Truth table100%2/2
A
A= $binds
| |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | ▶ | return $MASK if defined $bound && !ref $bound && $bound =~ /, |: /; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $bound and not ref $bound and $bound =~ /, |: /
defined $bound and not ref $bound
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 =~ /, |: /
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 37 | or return $MASK; | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | 26 | ▶ | return $MASK if $value =~ /: / or $seen{$name}++; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value =~ /: / or $seen{$name}++
MC/DC100%2/2
$value =~ /: / or $seen{$name}++
$value =~ /: /
$seen{$name}++
Truth table100%3/3
A or B
A= $value =~ /: /B= $seen{$name}++
| |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 24 | push @names, $name; | |||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 41 | 16 | ▶ | return $MASK unless @names; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
defined $statement and not ref $statement and length $statement
defined $statement and not ref $statement
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
| |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 13 | ▶ | $message =~ /\A(.*)(\n\nLINE (\d+): ([^\n]*)\n[ \t]*\^)\z/s | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$line >= 1 and $line <= @lines
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
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
length $piece and index($lines[$line - 1], $piece) != -1
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
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $message and length $message
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
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
defined $_ and not ref $_
$binds // []
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
Truth table100%2/2
A
A= $binds
| |||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||