blib/lib/DBIO/DuckDB/Storage.pm

filter: Statementstmt 18.6% 69 / 370 Branchbran 1.3% 2 / 152 Conditioncond 5.7% 5 / 87 MC/DCmcdc 4.2% 2 / 47 Subroutinesub 26.0% 18 / 69 Podpod 93.7% 15 / 16 totaltotal 14.9% 111 / 741 CC 116 SCAR 37.3
CC
116
Cov
12%
__ANON__
182.0
__ANON__
110.0
_duckdb_release_statements
90.0
CRAP 41.8
« blib/lib/DBIO/DuckDB/Introspect/Tables.pm ↑ Summary blib/lib/DBIO/DuckDB/Introspect/Uniques.pm »
1 package DBIO::DuckDB::Storage;
2 # ABSTRACT: DuckDB storage driver for DBIO
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 qw/DBIO::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 34 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use FFI::Platypus::Buffer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use DBIO::Util qw(mask_connect_secrets);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 34 ▶use DBIO::DuckDB::Extension ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 34 ▶use DBIO::DuckDB::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 34 ▶use DBIO::Extension::Protocol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 34 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 34 __PACKAGE__->register_driver('DuckDB' => __PACKAGE__);
23  
24 0 ▶sub dbio_deploy_class { 'DBIO::DuckDB::Deploy' }
Subroutine dbio_deploy_class: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 34 __PACKAGE__->sql_maker_class('DBIO::DuckDB::SQLMaker');
27 34 __PACKAGE__->sql_quote_char ('"');
28  
29 # ADR 0037: core's base _exists_value_sql renders
30 # SELECT CASE WHEN EXISTS <inner> THEN 1 ELSE 0 END
31 # which DuckDB accepts unchanged -- so this driver declares the capability
32 # and overrides nothing. See the CAPABILITIES POD below for the three
33 # assumptions that form makes and where each is documented.
34 34 __PACKAGE__->_use_supports_exists_query(1);
35  
36 # F02 / F10 -- transactional DDL. Declared 1: DuckDB the engine has
37 # transactional DDL, comprehensively, and this storage now ends the
38 # transactions the DBI driver underneath cannot (ADR 0005).
39 #
40 # DuckDB the engine has transactional DDL, comprehensively. Measured on
41 # libduckdb v1.5.3 via DBD::DuckDB 0.16: CREATE TABLE, DROP TABLE, ALTER TABLE
42 # (ADD / DROP / ALTER COLUMN), CREATE INDEX, DROP INDEX, CREATE SEQUENCE and
43 # CREATE VIEW all roll back inside BEGIN ... ROLLBACK, and the whole install
44 # batch this driver emits -- CREATE SEQUENCE, CREATE TABLE with
45 # DEFAULT nextval('...'), CREATE INDEX -- commits as one transaction with the
46 # sequence live afterwards. DuckDB documents the ALTER TABLE half outright --
47 # "All the changes made by ALTER TABLE fully respect the transactional
48 # semantics ... and can be fully reverted through a rollback"
49 # (duckdb.org/docs/stable/sql/statements/alter_table) -- and documents no
50 # statement this driver emits as one that may not be in a transaction, unlike
51 # PostgreSQL's ALTER TYPE ... ADD VALUE. Nothing here needs core's
52 # _ddl_statement_is_transactional escape. Nor is the deploy path the reason:
53 # DBIO::DuckDB::Diff emits plain statements in tables-then-columns-then-indexes
54 # order, with no SQLite-style create/copy/drop/rename rebuild and no pragma
55 # brackets that a txn_do wrap could swallow. Bracketed, the real path works --
56 # install, diff, apply and a second empty diff all pass, and a batch whose
57 # last statement fails rolls the earlier ones back instead of leaving them
58 # applied.
59 #
60 # One engine-side caveat, forward-looking rather than a breakage today. DuckDB
61 # refuses to alter a column an index depends on, and documents the remedy as
62 # "drop the index, alter the table, and then recreate the index" (same page).
63 # That remedy only works UNBRACKETED: measured here, DROP INDEX followed by
64 # ALTER TABLE ... ALTER COLUMN ... TYPE in one transaction still dies with
65 # "Cannot change the type of this column: an index depends on it!", because the
66 # type-change check does not see the uncommitted DROP INDEX. It is specific to
67 # the type change -- DROP COLUMN and SET NOT NULL after a DROP INDEX both
68 # commit fine. The diff cannot emit that pair today (indexes are diffed after
69 # columns, so a type change on an indexed column already fails, wrapped or
70 # not), so nothing regresses; but a wrap does foreclose the documented fix for
71 # it, and that is worth knowing before anyone reorders the phases.
72 #
73 # What used to block the 1 is that DBD::DuckDB could not end such a
74 # transaction. Its rollback issues ROLLBACK and then immediately reopens one:
75 #
76 # $dbh->do('ROLLBACK');
77 # $dbh->do('BEGIN TRANSACTION');
78 #
79 # which is right for AutoCommit=0, and DBI restores AutoCommit=1 afterwards --
80 # but the AutoCommit STORE handler that would COMMIT the reopened transaction
81 # is unreachable. It guards the commit branch with
82 # C<my $never_set = !$old_value>, so the preceding branch, C<!$old_value &&
83 # $value && $never_set>, has the identical condition and always wins; the
84 # 0 -> 1 transition is a silent no-op. (DBD::DuckDB 0.16, the current CPAN
85 # release, sub STORE.)
86 #
87 # So every txn_do that ROLLS BACK used to leave DuckDB inside an open
88 # transaction while DBI believes AutoCommit is on, and the next txn_begin died
89 # -- "cannot start a transaction within a transaction", or "Current transaction
90 # is aborted (please ROLLBACK)" when the rollback followed a failed statement.
91 # On the :memory: DSN that is DuckDB's headline mode it was worse than an
92 # error: DBIO::Storage::DBI::_exec_txn_begin deliberately retries begin_work on
93 # reconnect, so the poisoned handle was silently replaced by a connection to a
94 # brand-new, EMPTY in-memory database -- the deployed schema simply gone, with
95 # nothing raised.
96 #
97 # _exec_txn_rollback below closes the stray transaction itself (ADR 0005), so
98 # the engine argument for a 1 stands on its own: a failed apply rolls back
99 # whole AND the connection survives, and core's documented recovery -- re-run
100 # the upgrade, the diff is recomputed against live state -- works. The upstream
101 # STORE-branch fix (giterlizzi/perl-DBD-DuckDB#54) is still worth landing; the
102 # override is a no-op in behaviour once it is.
103 34 __PACKAGE__->_use_transactional_ddl(1);
104  
105  
106 38 ▶sub sql_maker {
Subroutine sql_maker: called CC=3 SCAR=12.3
Pod documented
107 38 my $self = shift;
108 38 my $sm = $self->next::method(@_);
109 38 ▶ $sm->{quote_char} //= $self->sql_quote_char;
Condition33%1/3
$sm->{"quote_char"} //= $self->sql_quote_char
l!l&&r!l&&!r
3800
MC/DC0%0/2
$sm->{"quote_char"} //= $self->sql_quote_char
$sm->{"quote_char"} $self->sql_quote_char
Truth table33%1/3
A //= B
A= $sm->{"quote_char"}B= $self->sql_quote_char
ABresult
1X1
011
000
110 38 ▶ $sm->{name_sep} //= $self->sql_name_sep;
Condition33%1/3
$sm->{"name_sep"} //= $self->sql_name_sep
l!l&&r!l&&!r
3800
MC/DC0%0/2
$sm->{"name_sep"} //= $self->sql_name_sep
$sm->{"name_sep"} $self->sql_name_sep
Truth table33%1/3
A //= B
A= $sm->{"name_sep"}B= $self->sql_name_sep
ABresult
1X1
011
000
111 38 $sm;
112 }
113  
114 0 ▶sub _determine_supports_insert_returning { 1 }
Subroutine _determine_supports_insert_returning: not called CC=1 SCAR=6.9
115  
116 # DBD::DuckDB 0.16's rollback issues ROLLBACK and then immediately reopens
117 # a transaction (BEGIN TRANSACTION), and the AutoCommit STORE branch that
118 # would close it again is unreachable code -- so after any txn_do that
119 # rolls back, DuckDB is still inside a transaction DBI believes is closed,
120 # and the next txn_begin dies (karr k15, ADR 0005). Close the orphan
121 # ourselves. The "no transaction is active" error is the expected outcome
122 # once DBD::DuckDB's STORE branch is fixed upstream -- swallow exactly
123 # that, so the normal rollback path is not broken.
124 0 ▶sub _exec_txn_rollback {
Subroutine _exec_txn_rollback: not called CC=1 SCAR=6.9
125 0 my $self = shift;
126 0 $self->next::method(@_);
127 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 $self->_dbh->do('ROLLBACK');
129 }
130 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
131 0 ▶ die $_ unless $_ =~ /no transaction is active/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /no transaction is active/i 0 0 0
132 0 };
133 }
134  
135 0 ▶sub _ping {
Subroutine _ping: not called CC=3 SCAR=24.8
136 0 my $self = shift;
137 0 ▶ my $dbh = $self->_dbh or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return undef 0 0 0
138 0 ▶ return undef unless $dbh->FETCH('Active');
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->FETCH("Active") 0 0 0
139 0 ▶ return try { $dbh->do('SELECT 1'); 1 } catch { undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 }
141  
142 # A DBD::DuckDB 0.16 statement handle cannot be used a second time, and DBI's
143 # statement cache exists to hand it out a second time (karr k33, ADR 0007).
144 # Three defects in DBD::DuckDB::st, independent of one another, measured on
145 # libduckdb v1.5.3, see docs/upstream/:
146 #
147 # - finish deletes duckdb_stmt. The handle stays in CachedKids and
148 # prepare_cached returns it dead: the second auto-increment insert, find
149 # or single of one SQL shape dies in bind_param, and the second populate
150 # -- core finishes after execute_for_fetch -- is a segmentation fault.
151 # - execute never sets Active, so prepare_cached($sql, {}, 3) cannot see
152 # that a handle is being read, and two open cursors of one SQL shape share
153 # one handle.
154 # - execute leaves duckdb_chunk and duckdb_chunk_row of the previous result
155 # in place. A handle that was not read to its end goes on reading the OLD
156 # chunk after the next execute: ->first for one value, then ->first for
157 # another, answers the second row of the first search. No error.
158 #
159 # Core asks this accessor, in _prepare_sth, whether to prepare or to
160 # prepare_cached, so answering here covers every statement core prepares. It
161 # is a method and not a value set at construction for the sake of a schema
162 # that names no driver: its storage is configured as the generic
163 # DBIO::Storage::DBI and becomes this class on the first use of the handle,
164 # and connect_info resets every storage option on each call besides. A value
165 # would have to survive both; a method of the class has nothing to survive.
166 #
167 # Recognising dead handles as they come out of the cache (dbio-firebird's
168 # _sth_is_alive) would mend the first defect only. Unlike the statement
169 # release below this is not bound to a DBD::DuckDB version: not caching is
170 # correct against any release, and caching again is a decision to take once a
171 # release has been measured -- t/49-statement-reuse.t carries the sentinel.
172 31 ▶sub disable_sth_caching { 1 }
Subroutine disable_sth_caching: called CC=1 SCAR=0.0
Pod documented
173  
174 # karr k43 (core k298, k307) -- which lowercase keys of a broker's answer are
175 # attributes of the DBD and may go to DBI->connect. DBI's own registry, which
176 # core asks, has no prefix for DBD::DuckDB (DBI->driver_prefix("DuckDB") is
177 # empty), though the DBD takes duckdb_* attributes. $driver is the DBD name off
178 # the DSN: any other one is core's to answer.
179 7 ▶sub _dbd_attribute_prefixes {
Subroutine _dbd_attribute_prefixes: called CC=3 SCAR=11.0
180 7 my ($self, $driver) = @_;
181  
182 7 ▶ return 'duckdb_' if defined $driver && $driver eq 'DuckDB';
Branch: 1 branch
BranchTrueFalseTotal
if defined $driver and $driver eq "DuckDB" 1 6 7
Condition100%3/3
defined $driver and $driver eq "DuckDB"
!ll&&!rl&&r
151
MC/DC100%2/2
defined $driver and $driver eq "DuckDB"
defined $driver $driver eq "DuckDB"
Truth table100%3/3
A and B
A= defined $driverB= $driver eq "DuckDB"
ABresult
0X0
100
111
183  
184 6 return $self->next::method($driver);
185 }
186  
187 # DBD::DuckDB 0.16 never frees a prepared statement (karr k25). Its
188 # st::finish calls duckdb_destroy_prepare($stmt) with the handle BY VALUE,
189 # where the C function takes duckdb_prepared_statement* -- FFI::Platypus
190 # turns a non-reference argument for a pointer type into NULL, without a
191 # warning, and libduckdb does nothing on NULL. And there is no st::DESTROY,
192 # so a handle nobody called finish on -- every $dbh->do, every handle DBIO
193 # prepares for an update, a delete or a cursor -- is not even attempted. Each
194 # leaked statement keeps the database instance alive, so duckdb_close in
195 # disconnect cannot drop the last reference and the lock on the database file
196 # outlives the handle until the process exits. Measured on libduckdb v1.5.3,
197 # see docs/upstream/.
198 #
199 # So this storage frees them: a DBI per-handle callback on the statement
200 # handles of ITS OWN connection, before finish and before DESTROY. Nothing in
201 # the DBD::DuckDB namespace is touched and no other DuckDB handle in the
202 # process is affected.
203 #
204 # What keeps that from becoming a double free once upstream frees them too:
205 #
206 # - it takes the pointer out of the handle BEFORE freeing it. DBI runs a
207 # callback ahead of the method, so whatever DBD::DuckDB does afterwards
208 # finds undef, which arrives in C as NULL (by value) or as a pointer to
209 # NULL (by reference) -- libduckdb does nothing on either (measured).
210 # - it is bound to the releases whose finish has been read. A newer
211 # DBD::DuckDB is left alone until someone has looked at it;
212 # t/47-disconnect-releases-file-lock.t goes red if that release still
213 # leaks, and its TODO passes if it does not.
214 #
215 # It stands down in a forked child, as core does for the connection itself
216 # (_verify_pid): only the process that opened the database releases anything
217 # in it. One thing it cannot reach: with AutoCommit => 0, DBD::DuckDB runs
218 # BEGIN TRANSACTION inside DBI->connect, before there is a handle to install
219 # a callback on, and that one statement stays.
220 #
221 # The same finish has a second defect, and the same callback answers it (karr
222 # k38). It hands libduckdb the handle's result unconditionally --
223 #
224 # my $res = delete $sth->{duckdb_res};
225 # duckdb_destroy_result($res);
226 #
227 # -- and a handle has a result only once an execute has succeeded: execute
228 # stores it after the engine accepted the statement, finish deletes it. On a
229 # handle that was never executed, whose only executes were refused, or that
230 # was finished before, $res is undef, which reaches C as NULL, and
231 # duckdb_destroy_result(NULL) is a segmentation fault (measured). Core
232 # finishes the handle of a bulk insert whatever happened to it, so a populate
233 # in void context whose first row the engine refuses did not throw: it ended
234 # the process.
235 #
236 # So in front of finish, a handle without a result is given an empty one --
237 # a zeroed duckdb_result, which is the state duckdb_destroy_result itself
238 # leaves a result in and does nothing on (measured, any number of times).
239 # DBD::DuckDB's finish then runs as written. It is in the callback and not in
240 # an override of core's two bulk-insert methods because the defect is in
241 # finish and not in populate: this covers every finish on the connection,
242 # cleanup code after a refused execute in dbh_do included, where an override
243 # would cover the one caller and copy a hundred lines of core to do it.
244 #
245 # Not for DESTROY -- DBD::DuckDB 0.16 has none, a dropped handle touches no
246 # result -- and not under the pid check: an empty result frees nothing, and a
247 # forked child that finishes a handle it inherited would crash just the same.
248 # A release that guards its own finish gets no callback at all (the version
249 # bound above), and would find nothing wrong with an empty result if it did.
250 # t/51-finish-without-result.t carries the sentinel.
251 34 my $_STATEMENT_LEAK_READ_UP_TO = version->parse('0.16');
252  
253 0 ▶sub _run_connection_actions {
Subroutine _run_connection_actions: not called CC=1 SCAR=6.9
254 0 my $self = shift;
255 0 $self->_duckdb_release_statements($self->_dbh);
256 0 $self->_duckdb_text_callbacks($self->_dbh);
257 0 $self->_duckdb_exact_decimals;
258 0 $self->_duckdb_exact_hugeints;
259 0 return $self->next::method(@_);
260 }
261  
262 0 ▶sub _duckdb_release_statements {
Subroutine _duckdb_release_statements: not called CC=9 SCAR=45.0
263 0 my ($self, $dbh) = @_;
264  
265 0 ▶ return unless ($dbh->{Driver}{Name} // '') eq 'DuckDB';
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"Driver"}{"Name"} // "") eq "DuckDB" 0 0 0
Condition0%0/2
$dbh->{"Driver"}{"Name"} // ""
l!l
00
MC/DC0%0/1
$dbh->{"Driver"}{"Name"} // ""
$dbh->{"Driver"}{"Name"}
Truth table0%0/2
A
A= $dbh->{"Driver"}{"Name"}
Aresult
11
00
266 0 ▶ return if version->parse(DBD::DuckDB->VERSION) > $_STATEMENT_LEAK_READ_UP_TO;
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse("DBD::DuckDB"->VERSION) > $_STATEMENT_LEAK_READ_UP_TO 0 0 0
267 0 ▶ return if $dbh->{private_dbio_duckdb_statement_release};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"private_dbio_duckdb_statement_release"} 0 0 0
268  
269 0 my $pid = $$;
270 0 ▶ my $release = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
271 0 my $sth = shift;
272 0 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 0 0
273 0 ▶ my $stmt = $sth->{duckdb_stmt} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $sth->{"duckdb_stmt"} or return 0 0 0
274 0 $sth->{duckdb_stmt} = undef;
275 0 DBD::DuckDB::FFI::duckdb_destroy_prepare(\$stmt);
276 0 return;
277 0 };
278  
279 # finish only: something for it to destroy where the handle has no result.
280 0 ▶ my $give_result = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
281 0 my $sth = shift;
282 0 ▶ $sth->{duckdb_res} //= DBD::DuckDB::FFI::Result->new;
Condition0%0/3
$sth->{"duckdb_res"} //= "DBD::DuckDB::FFI::Result"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$sth->{"duckdb_res"} //= "DBD::DuckDB::FFI::Result"->new
$sth->{"duckdb_res"} "DBD::DuckDB::FFI::Result"->new
Truth table0%0/3
A //= B
A= $sth->{"duckdb_res"}B= "DBD::DuckDB::FFI::Result"->new
ABresult
1X1
011
000
283 0 return;
284 0 };
285  
286 my %before = (
287 0 ▶ finish => sub { $release->($_[0]); $give_result->($_[0]); return },
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 0 DESTROY => $release,
289 );
290  
291 # Chain in front of whatever the application installed, on a copy: the
292 # hash it handed to connect may be shared between connections.
293 0 ▶ my $callbacks = $dbh->{Callbacks} || {};
Condition0%0/2
$dbh->{'Callbacks'} || {}
l!l
00
MC/DC0%0/1
$dbh->{'Callbacks'} || {}
$dbh->{'Callbacks'}
Truth table0%0/2
A
A= $dbh->{'Callbacks'}
Aresult
11
00
294 0 ▶ my $child = $callbacks->{ChildCallbacks} || {};
Condition0%0/2
$callbacks->{'ChildCallbacks'} || {}
l!l
00
MC/DC0%0/1
$callbacks->{'ChildCallbacks'} || {}
$callbacks->{'ChildCallbacks'}
Truth table0%0/2
A
A= $callbacks->{'ChildCallbacks'}
Aresult
11
00
295 0 my %ours;
296 0 for my $method (qw/finish DESTROY/) {
297 0 my $mine = $before{$method};
298 0 my $theirs = $child->{$method};
299 $ours{$method} = $theirs
300 0 ▶ ? sub { $mine->($_[0]); goto &$theirs }
Subroutine __ANON__: not called CC=1 SCAR=6.9
301 0 ▶ : sub { $mine->($_[0]); return };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$theirs ? : 0 0 0
302 }
303  
304 0 $dbh->{Callbacks} = { %$callbacks, ChildCallbacks => { %$child, %ours } };
305 0 $dbh->{private_dbio_duckdb_statement_release} = 1;
306  
307 0 return;
308 }
309  
310 # DECIMAL, read back exact (karr k49, Bind conformance).
311 #
312 # DBD::DuckDB 0.16 reads a DECIMAL as its unscaled integer divided by 10**scale
313 # -- a perl double -- and prints that with sprintf("%.${scale}f"). A double holds
314 # 15 to 17 digits, so the 20-digit 1234567890123456.7890 of a numeric(20,4)
315 # comes back as 1234567890123456.7500: the stored value is right and the read
316 # invents the tail, with no error. A wide DECIMAL is stored as a HUGEINT; the
317 # replacement below reads those 16 bytes itself and does not call the DBD's
318 # HUGEINT reader, which misreads (_duckdb_exact_hugeints, below), so a
319 # numeric(20,4) above 2**64 unscaled units reads right either way.
320 #
321 # So the reader is replaced, once, by one that formats the unscaled integer as
322 # a string and puts the point in by hand; same output shape as before (scale
323 # digits, a sign only when negative). Only the readers of the 0.16 DBD that
324 # were read; a newer one is left alone. The replacement is of the DBD's
325 # private function, which is why it is bound to the version: 0.16 is the
326 # newest DBD::DuckDB there is, and the only one measured. t/58 holds the DBD's
327 # own misreading as a fact, in a child process that never loads this class, and
328 # goes red when a DBD reads these values right -- then this replacement goes.
329 34 my $_DECIMAL_READER_FIXED;
330  
331 0 ▶sub _duckdb_exact_decimals {
Subroutine _duckdb_exact_decimals: not called CC=5 SCAR=32.3
332 0 ▶ return if $_DECIMAL_READER_FIXED;
Branch: 1 branch
BranchTrueFalseTotal
if $_DECIMAL_READER_FIXED 0 0 0
333 0 ▶ return unless DBD::DuckDB->can('VERSION');
Branch: 1 branch
BranchTrueFalseTotal
unless "DBD::DuckDB"->can("VERSION") 0 0 0
334 0 ▶ return if version->parse(DBD::DuckDB->VERSION) > $_STATEMENT_LEAK_READ_UP_TO;
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse("DBD::DuckDB"->VERSION) > $_STATEMENT_LEAK_READ_UP_TO 0 0 0
335 0 ▶ return unless defined &DBD::DuckDB::st::_vector_decimal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &DBD::DuckDB::st::_vector_decimal 0 0 0
336  
337 0 my %width = (
338 DBD::DuckDB::Constants::DUCKDB_TYPE_SMALLINT() => [ 2, 's<' ],
339 DBD::DuckDB::Constants::DUCKDB_TYPE_INTEGER() => [ 4, 'l<' ],
340 DBD::DuckDB::Constants::DUCKDB_TYPE_BIGINT() => [ 8, 'q<' ],
341 );
342 0 my $hugeint = DBD::DuckDB::Constants::DUCKDB_TYPE_HUGEINT();
343  
344 0 my $original = \&DBD::DuckDB::st::_vector_decimal;
345 34 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
346 0 ▶ *DBD::DuckDB::st::_vector_decimal = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
347 0 my ($logical_type, $vector_data, $row_idx) = @_;
348  
349 0 my $scale = DBD::DuckDB::FFI::duckdb_decimal_scale($logical_type);
350 0 my $type = DBD::DuckDB::FFI::duckdb_decimal_internal_type($logical_type);
351  
352 0 my $unscaled;
353 0 ▶ if (my $w = $width{$type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $w = $width{$type}) { } 0 0 0
elsif ($type == $hugeint) { } 0 0 0
354 0 $unscaled = Math::BigInt->new(
355 unpack $w->[1], FFI::Platypus::Buffer::buffer_to_scalar(
356 $vector_data + $row_idx * $w->[0], $w->[0]
357 )
358 );
359 }
360 elsif ($type == $hugeint) {
361 0 my ($lower, $upper) = unpack 'Q< q<',
362 FFI::Platypus::Buffer::buffer_to_scalar($vector_data + $row_idx * 16, 16);
363 0 $unscaled = Math::BigInt->new($upper)->blsft(64)->badd($lower);
364 }
365 else {
366 0 goto &$original;
367 }
368  
369 0 my $negative = $unscaled->is_neg;
370 0 my $digits = $unscaled->copy->babs->bstr;
371 0 ▶ $digits = ('0' x ($scale + 1 - length $digits)) . $digits
Branch: 1 branch
BranchTrueFalseTotal
if length $digits <= $scale 0 0 0
372 if length $digits <= $scale;
373 0 ▶ substr($digits, length($digits) - $scale, 0, '.') if $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $scale 0 0 0
374 0 ▶ return ($negative ? '-' : '') . $digits;
Branch: 1 branch
BranchTrueFalseTotal
$negative ? : 0 0 0
375 0 };
376  
377 0 $_DECIMAL_READER_FIXED = 1;
378 0 return;
379 }
380  
381 # HUGEINT, read back exact.
382 #
383 # DBD::DuckDB 0.16 (DBD::DuckDB::st::_vector_hugeint) subtracts 2**128 from a
384 # value whose upper word tests true on `$upper & (1 << 64)`. Shifting by the
385 # width of the integer is undefined before perl 5.24, where `1 << 64` is 1, so
386 # on such a perl every value with an odd upper word comes back 2**128 too low:
387 # 170141183460469231731687303715884105727 reads as
388 # -170141183460469231731687303715884105729. The two's complement upper word is
389 # signed already (unpack 'q<'), so no subtraction is ever needed.
390 #
391 # So the reader is replaced, once, by one that adds the words with Math::BigInt
392 # and subtracts nothing. Bound to the DBD versions measured, as the DECIMAL
393 # reader is (0.16 and older); a newer one is left alone. The unsigned reader
394 # next to it, _vector_uhugeint, was checked and is right: it adds the words and
395 # has no subtraction, so it is not replaced. t/74 reads the boundary values
396 # through the storage; the Arrow path never went through this reader.
397 my $_HUGEINT_READER_FIXED;
398  
399 0 ▶sub _duckdb_exact_hugeints {
Subroutine _duckdb_exact_hugeints: not called CC=5 SCAR=30.3
400 0 ▶ return if $_HUGEINT_READER_FIXED;
Branch: 1 branch
BranchTrueFalseTotal
if $_HUGEINT_READER_FIXED 0 0 0
401 0 ▶ return unless DBD::DuckDB->can('VERSION');
Branch: 1 branch
BranchTrueFalseTotal
unless "DBD::DuckDB"->can("VERSION") 0 0 0
402 0 ▶ return if version->parse(DBD::DuckDB->VERSION) > $_STATEMENT_LEAK_READ_UP_TO;
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse("DBD::DuckDB"->VERSION) > $_STATEMENT_LEAK_READ_UP_TO 0 0 0
403 0 ▶ return unless defined &DBD::DuckDB::st::_vector_hugeint;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &DBD::DuckDB::st::_vector_hugeint 0 0 0
404  
405 34 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
406 0 ▶ *DBD::DuckDB::st::_vector_hugeint = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
407 0 my ($vector_data, $row_idx) = @_;
408  
409 0 my ($lower, $upper) = unpack 'Q< q<',
410 FFI::Platypus::Buffer::buffer_to_scalar($vector_data + $row_idx * 16, 16);
411 0 return Math::BigInt->new($upper)->blsft(64)->badd($lower)->bstr;
412 0 };
413  
414 0 $_HUGEINT_READER_FIXED = 1;
415 0 return;
416 }
417  
418 # Text in, text out; typed binds that stay typed (karr k49, Bind conformance).
419 #
420 # DBD::DuckDB 0.16 does three things to a string that lose it, none of them
421 # with an error:
422 #
423 # - it writes a perl string to libduckdb as it sits in memory, so a string
424 # with characters above 0x7f that perl holds as latin-1 ("\x{fc}" without
425 # the utf8 flag) is invalid UTF-8 and the bind dies, and one perl holds
426 # as UTF-8 only works by accident;
427 # - it hands every VARCHAR back as the bytes libduckdb stores, never as
428 # characters, so "\x{20ac}" comes back as three characters;
429 # - bind_param takes its type from an attribute only for that one call:
430 # execute(@bind), which is what execute_for_fetch -- a populate -- calls,
431 # binds again without it, so a bytes value meant for a BLOB is sniffed
432 # into a VARCHAR and refused.
433 #
434 # The callbacks below sit in front of bind_param and the fetch methods of every
435 # statement of this connection. bind_param remembers a bind type per parameter
436 # and applies it to a later bind without one, and encodes a text value as UTF-8
437 # before the DBD sees it (a copy: the application's variable is never touched);
438 # a fetch decodes the VARCHAR and ENUM columns of the row it returns, in place,
439 # so a bound column (bind_columns) sees characters as well. BLOB columns are
440 # left as bytes. Both run the DBD's own method with the callbacks standing down and
441 # replace the dispatch (undef $_), so nothing runs twice. Bound to DBD::DuckDB <= 0.16 like
442 # the statement release above.
443 0 ▶sub _duckdb_text_callbacks {
Subroutine _duckdb_text_callbacks: not called CC=9 SCAR=45.0
444 0 my ($self, $dbh) = @_;
445  
446 0 ▶ return unless ($dbh->{Driver}{Name} // '') eq 'DuckDB';
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"Driver"}{"Name"} // "") eq "DuckDB" 0 0 0
Condition0%0/2
$dbh->{"Driver"}{"Name"} // ""
l!l
00
MC/DC0%0/1
$dbh->{"Driver"}{"Name"} // ""
$dbh->{"Driver"}{"Name"}
Truth table0%0/2
A
A= $dbh->{"Driver"}{"Name"}
Aresult
11
00
447 0 ▶ return if version->parse(DBD::DuckDB->VERSION) > $_STATEMENT_LEAK_READ_UP_TO;
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse("DBD::DuckDB"->VERSION) > $_STATEMENT_LEAK_READ_UP_TO 0 0 0
448 0 ▶ return if $dbh->{private_dbio_duckdb_text_callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"private_dbio_duckdb_text_callbacks"} 0 0 0
449  
450 # the method itself, run again through dispatch with our callbacks standing
451 # down (the flag), and the dispatch of the original call replaced by its
452 # result
453 0 ▶ my $plain = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
454 0 my ($sth, $method, @args) = @_;
455 0 my ($ok, @result);
456 0 my $error;
457 0 $sth->{private_dbio_duckdb_in_callback} = 1;
458 {
459 0 local $@;
460 0 $ok = eval { @result = $sth->$method(@args); 1 };
461 0 ▶ $error = $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
462 }
463 0 $sth->{private_dbio_duckdb_in_callback} = 0;
464 0 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
465 0 undef $_;
466 0 return $result[0];
467 0 };
468  
469 0 ▶ my $bind_param = sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
470 0 my ($sth, $i, $value, $attr) = @_;
471 0 ▶ return if $sth->{private_dbio_duckdb_in_callback};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"private_dbio_duckdb_in_callback"} 0 0 0
472 0 ▶ my $types = $sth->{private_dbio_duckdb_bind_types} ||= {};
Condition0%0/2
$sth->{"private_dbio_duckdb_bind_types"} ||= {}
l!l
00
MC/DC0%0/1
$sth->{"private_dbio_duckdb_bind_types"} ||= {}
$sth->{"private_dbio_duckdb_bind_types"}
Truth table0%0/2
A
A= $sth->{"private_dbio_duckdb_bind_types"}
Aresult
11
00
473 0 ▶ if (defined $attr and not ref $attr) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $attr and not ref $attr) { } 0 0 0
elsif (not defined $attr and defined $types->{$i}) { } 0 0 0
Condition0%0/3
defined $attr and not ref $attr
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attr and not ref $attr
defined $attr not ref $attr
Truth table0%0/3
A and not(B)
A= defined $attrB= not ref $attr
ABresult
0X0
100
111
474 0 $types->{$i} = $attr;
475 }
476 ▶ elsif (not defined $attr and defined $types->{$i}) {
Condition0%0/3
not defined $attr and defined $types->{$i}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $attr and defined $types->{$i}
not defined $attr defined $types->{$i}
Truth table0%0/3
not(A) and B
A= not defined $attrB= defined $types->{$i}
ABresult
0X0
100
111
477 0 $attr = $types->{$i};
478 }
479 0 ▶ if (defined $value and not ref $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB() and $value =~ /[^\x00-\x7f]/) 0 0 0
Condition0%0/15
defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB() and $value =~ /[^\x00-\x7f]/
!ll&&!rl&&r
000
defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB()
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
not defined $attr or ref $attr or $attr != &DBI::SQL_BLOB()
l!l&&r!l&&!r
000
not defined $attr or ref $attr
l!l&&r!l&&!r
000
MC/DC0%0/6
defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB() and $value =~ /[^\x00-\x7f]/
defined $value not ref $value not defined $attr ref $attr $attr != &DBI::SQL_BLOB() $value =~ /[^\x00-\x7f]/
Truth table0%0/9
A and not(B) and not(C) or D or E and F
A= defined $valueB= not ref $valueC= not defined $attrD= ref $attrE= $attr != &DBI::SQL_BLOB()F= $value =~ /[^\x00-\x7f]/
ABCDEFresult
0XXXXX0
10XXXX0
11000X0
111XX00
1101X00
1100100
111XX11
1101X11
1100111
480 and (not defined $attr or ref $attr or $attr != DBI::SQL_BLOB())
481 and $value =~ /[^\x00-\x7f]/) {
482 0 utf8::encode($value);
483 }
484 0 return $plain->($sth, 'bind_param', $i, $value, $attr);
485 0 };
486  
487 0 ▶ my $fetch = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
488 0 my $sth = $_[0];
489 0 ▶ return if $sth->{private_dbio_duckdb_in_callback};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"private_dbio_duckdb_in_callback"} 0 0 0
490 0 my $row = $plain->($sth, 'fetchrow_arrayref');
491 0 ▶ return $row unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
492  
493 0 ▶ my $logical = $sth->{duckdb_col_logical_types} or return $row;
Branch: 1 branch
BranchTrueFalseTotal
my $logical = $sth->{"duckdb_col_logical_types"} or return $row 0 0 0
494 0 my $cache = $sth->{private_dbio_duckdb_text_columns};
495 0 ▶ unless ($cache and $cache->[0] == $logical) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache and $cache->[0] == $logical) 0 0 0
Condition0%0/3
$cache and $cache->[0] == $logical
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and $cache->[0] == $logical
$cache $cache->[0] == $logical
Truth table0%0/3
A and B
A= $cacheB= $cache->[0] == $logical
ABresult
0X0
100
111
496 my @text = grep {
497 0 my $id = DBD::DuckDB::FFI::duckdb_get_type_id($logical->[$_]);
498 0 ▶ $id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR()
Condition0%0/3
$id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR() || $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
l!l&&r!l&&!r
000
MC/DC0%0/2
$id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR() || $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
$id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR() $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
Truth table0%0/3
A || B
A= $id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR()B= $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
ABresult
1X1
011
000
499 or $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
500 } 0 .. $#$logical;
501 0 $cache = $sth->{private_dbio_duckdb_text_columns} = [ $logical, \@text ];
502 }
503 0 for my $c (@{ $cache->[1] }) {
504 0 ▶ utf8::decode($row->[$c])
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->[$c] and not utf8::is_utf8($row->[$c]) 0 0 0
Condition0%0/3
defined $row->[$c] and not utf8::is_utf8($row->[$c])
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row->[$c] and not utf8::is_utf8($row->[$c])
defined $row->[$c] not utf8::is_utf8($row->[$c])
Truth table0%0/3
A and not(B)
A= defined $row->[$c]B= not utf8::is_utf8($row->[$c])
ABresult
0X0
100
111
505 if defined $row->[$c] and not utf8::is_utf8($row->[$c]);
506 }
507 0 return $row;
508 0 };
509  
510 0 my %ours = (
511 bind_param => $bind_param,
512 fetch => $fetch,
513 fetchrow_arrayref => $fetch,
514 );
515  
516 0 ▶ my $callbacks = $dbh->{Callbacks} || {};
Condition0%0/2
$dbh->{'Callbacks'} || {}
l!l
00
MC/DC0%0/1
$dbh->{'Callbacks'} || {}
$dbh->{'Callbacks'}
Truth table0%0/2
A
A= $dbh->{'Callbacks'}
Aresult
11
00
517 0 ▶ my $child = $callbacks->{ChildCallbacks} || {};
Condition0%0/2
$callbacks->{'ChildCallbacks'} || {}
l!l
00
MC/DC0%0/1
$callbacks->{'ChildCallbacks'} || {}
$callbacks->{'ChildCallbacks'}
Truth table0%0/2
A
A= $callbacks->{'ChildCallbacks'}
Aresult
11
00
518 0 my %chained;
519 0 for my $method (keys %ours) {
520 0 my $mine = $ours{$method};
521 0 my $theirs = $child->{$method};
522 $chained{$method} = $theirs
523 0 ▶ ? sub { my $r = $theirs->(@_); defined $_ ? $mine->(@_) : $r }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
524 0 ▶ : $mine;
Branch: 1 branch
BranchTrueFalseTotal
$theirs ? : 0 0 0
525 }
526  
527 0 $dbh->{Callbacks} = { %$callbacks, ChildCallbacks => { %$child, %chained } };
528 0 $dbh->{private_dbio_duckdb_text_callbacks} = 1;
529  
530 0 return;
531 }
532  
533  
534 0 ▶sub _mask_error_values {
Subroutine _mask_error_values: not called CC=8 SCAR=42.8
535 0 my ($self, $text, $values, $statement) = @_;
536 0 ▶ return $text unless defined $text and !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 0 0 0
Condition0%0/3
defined $text and not ref $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and not ref $text
defined $text not ref $text
Truth table0%0/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
537  
538 # a non-ASCII value reaches the engine as UTF-8 bytes and comes back in its
539 # text in that form, which core's net (the value as written) does not look for
540 $values = [ map {
541 0 my $v = $_;
542 (defined $v && !ref $v && $v =~ /[^\x00-\x7f]/ && utf8::is_utf8($v))
543 0 ▶ ? ($v, do { my $b = $v; utf8::encode($b); $b })
Branch: 1 branch
BranchTrueFalseTotal
defined $v && !ref($v) && $v =~ /[^\x00-\x7f]/ && utf8::is_utf8($v) ? : 0 0 0
Condition0%0/9
defined $v and not ref $v and $v =~ /[^\x00-\x7f]/ and utf8::is_utf8($v)
!ll&&!rl&&r
000
defined $v and not ref $v and $v =~ /[^\x00-\x7f]/
!ll&&!rl&&r
000
defined $v and not ref $v
!ll&&!rl&&r
000
MC/DC0%0/4
defined $v and not ref $v and $v =~ /[^\x00-\x7f]/ and utf8::is_utf8($v)
defined $v not ref $v $v =~ /[^\x00-\x7f]/ utf8::is_utf8($v)
Truth table0%0/5
A and not(B) and C and D
A= defined $vB= not ref $vC= $v =~ /[^\x00-\x7f]/D= utf8::is_utf8($v)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
544 : $v;
545 0 ▶ } @{ $values // [] } ];
Condition0%0/2
$values // []
l!l
00
MC/DC0%0/1
$values // []
$values
Truth table0%0/2
A
A= $values
Aresult
11
00
546  
547 0 my ($head, $tail) = DBIO::DuckDB::Util::_split_excerpt($text, $statement);
548 0 $head = DBIO::DuckDB::Util::mask_error_details($head, $values);
549 0 return $self->next::method($head, $values, $statement) . $tail;
550 }
551  
552  
553 0 ▶sub duckdb_appender {
Subroutine duckdb_appender: not called CC=2 SCAR=17.9
Pod documented
554 0 my ($self, $table, $schema) = @_;
555 0 ▶ $self->throw_exception('Usage: $storage->duckdb_appender($table [, $schema])') unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 0 0
556 0 return $self->_get_dbh->x_duckdb_appender($table, $schema);
557 }
558  
559  
560 0 ▶sub duckdb_arrow_fetch {
Subroutine duckdb_arrow_fetch: not called CC=3 SCAR=24.8
Pod documented
561 0 my ($self, $sql, $binds) = @_;
562 0 ▶ $self->throw_exception('Usage: $storage->duckdb_arrow_fetch($sql [, \@binds])')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql and length $sql 0 0 0
Condition0%0/3
defined $sql and length $sql
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sql and length $sql
defined $sql length $sql
Truth table0%0/3
A and B
A= defined $sqlB= length $sql
ABresult
0X0
100
111
563 unless defined $sql and length $sql;
564 0 my $dbh = $self->_get_dbh;
565 0 ▶ return try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
566 # loaded here, not at the top: the module binds libduckdb's Arrow
567 # functions when it loads, and a missing library is an exception of this
568 # method, not a storage class that does not load
569 0 require DBIO::DuckDB::Arrow;
570 DBIO::DuckDB::Arrow->fetch($dbh, $sql, $binds // [],
571 0 ▶ sub { $self->_mask_error_values($_[0], $binds // [], $sql) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/4
$binds // []
l!l
00
$binds // []
l!l
00
MC/DC0%0/1
$binds // []
$binds
MC/DC0%0/1
$binds // []
$binds
Truth table0%0/2
A
A= $binds
Aresult
11
00
Truth table0%0/2
A
A= $binds
Aresult
11
00
572 }
573 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
574 0 $self->throw_exception("$_");
575 0 };
576 }
577  
578  
579 0 ▶sub duckdb_read_csv {
Subroutine duckdb_read_csv: not called CC=4 SCAR=30.0
Pod documented
580 0 my ($self, $path, $opts) = @_;
581 0 ▶ $self->throw_exception('Usage: $storage->duckdb_read_csv($path [, \%opts])') unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
582 0 ▶ my $sth = $self->_get_dbh->x_duckdb_read_csv($path, $opts // {});
Condition0%0/2
$opts // {}
l!l
00
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
583 0 my @rows;
584 0 ▶ while (my $row = $sth->fetchrow_hashref) { push @rows, { %$row } }
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
585 0 return \@rows;
586 }
587  
588  
589 0 ▶sub duckdb_read_parquet {
Subroutine duckdb_read_parquet: not called CC=2 SCAR=17.9
Pod documented
590 0 my ($self, $path) = @_;
591 0 ▶ $self->throw_exception('Usage: $storage->duckdb_read_parquet($path)') unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
592 0 return $self->_get_dbh->selectall_arrayref(
593 'SELECT * FROM read_parquet(?)',
594 { Slice => {} },
595 $path,
596 );
597 }
598  
599  
600 0 ▶sub duckdb_read_json {
Subroutine duckdb_read_json: not called CC=4 SCAR=30.0
Pod documented
601 0 my ($self, $path, $opts) = @_;
602 0 ▶ $self->throw_exception('Usage: $storage->duckdb_read_json($path [, \%opts])') unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
603 0 ▶ my $sth = $self->_get_dbh->x_duckdb_read_json($path, $opts // {});
Condition0%0/2
$opts // {}
l!l
00
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
604 0 my @rows;
605 0 ▶ while (my $row = $sth->fetchrow_hashref) { push @rows, { %$row } }
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
606 0 return \@rows;
607 }
608  
609  
610 0 ▶sub duckdb_version {
Subroutine duckdb_version: not called CC=1 SCAR=6.9
Pod documented
611 0 my $self = shift;
612 0 return $self->_get_dbh->x_duckdb_version;
613 }
614  
615  
616 0 ▶sub duckdb_install_extension {
Subroutine duckdb_install_extension: not called CC=4 SCAR=30.0
Pod documented
617 0 my ($self, $name, %opts) = @_;
618 0 ▶ $self->throw_exception('extension name required') unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
619 0 ▶ $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("invalid extension name: $name") 0 0 0
620 or $self->throw_exception("invalid extension name: $name");
621 0 my $dbh = $self->_get_dbh;
622 0 $dbh->do("INSTALL $name");
623 0 ▶ $dbh->do("LOAD $name") if $opts{load};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"load"} 0 0 0
624 0 return 1;
625 }
626  
627  
628 0 ▶sub _extension_activation {
Subroutine _extension_activation: not called CC=2 SCAR=17.9
629 0 my ($self, $name) = @_;
630 0 my $none = { load => [], search_path => [] };
631  
632 0 ▶ my $list = DBIO::DuckDB::Extension->resolve_list($self->schema)
Branch: 1 branch
BranchTrueFalseTotal
my $list = "DBIO::DuckDB::Extension"->resolve_list($self->schema) or return $none 0 0 0
633 or return $none;
634  
635 0 return DBIO::Extension::Protocol->new(
636 entries => [ $list->entries ],
637 driver => {},
638 connection_schema => $name,
639 restricted => 0,
640 )->activation;
641 }
642  
643  
644 0 ▶sub _activate_extensions {
Subroutine _activate_extensions: not called CC=2 SCAR=17.9
645 0 my ($self, $activation) = @_;
646 0 $self->_do_query($_) for $self->_extension_activation_sql($activation);
647 0 return $self;
648 }
649  
650  
651 0 ▶sub _extension_activation_sql {
Subroutine _extension_activation_sql: not called CC=6 SCAR=37.4
652 0 my ($self, $activation) = @_;
653  
654 $self->throw_exception('DuckDB extensions have no search_path; an extension asked for '
655 0 . join(', ', @{ $activation->{search_path} }))
656 0 ▶ if @{ $activation->{search_path} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$activation->{"search_path"} or [];} 0 0 0
Condition0%0/2
$activation->{'search_path'} || []
l!l
00
MC/DC0%0/1
$activation->{'search_path'} || []
$activation->{'search_path'}
Truth table0%0/2
A
A= $activation->{'search_path'}
Aresult
11
00
657  
658 0 my @sql;
659 0 ▶ for my $name (@{ $activation->{load} || [] }) {
Condition0%0/2
$activation->{'load'} || []
l!l
00
MC/DC0%0/1
$activation->{'load'} || []
$activation->{'load'}
Truth table0%0/2
A
A= $activation->{'load'}
Aresult
11
00
660 0 ▶ $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("invalid extension name: $name") 0 0 0
661 or $self->throw_exception("invalid extension name: $name");
662 0 push @sql, "INSTALL $name", "LOAD $name";
663 }
664 0 return @sql;
665 }
666  
667  
668 0 ▶sub duckdb_checkpoint {
Subroutine duckdb_checkpoint: not called CC=1 SCAR=6.9
Pod documented
669 0 my $self = shift;
670 0 $self->_get_dbh->do('CHECKPOINT');
671 0 return 1;
672 }
673  
674  
675 0 ▶sub quack_serve {
Subroutine quack_serve: not called CC=3 SCAR=24.8
Pod documented
676 0 my ($self, $addr, %opts) = @_;
677 0 ▶ $addr //= 'quack:localhost';
Condition0%0/2
$addr //= "quack:localhost"
l!l
00
MC/DC0%0/1
$addr //= "quack:localhost"
$addr
Truth table0%0/2
A
A= $addr
Aresult
11
00
678 0 _validate_quack_addr($self, $addr);
679  
680 0 $self->duckdb_install_extension('quack', load => 1);
681  
682 0 my $dbh = $self->_get_dbh;
683  
684 0 ▶ if (defined $opts{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'token'}) { } 0 0 0
685 0 _validate_quack_token($self, $opts{token});
686 0 my $token = $opts{token};
687 0 my $tok_lit = $opts{token};
688 0 $tok_lit =~ s/'/''/g;
689 0 my $addr_lit = $addr;
690 0 $addr_lit =~ s/'/''/g;
691 0 $dbh->do("CALL quack_serve('$addr_lit', token => '$tok_lit')");
692 }
693 else {
694 0 my $addr_lit = $addr;
695 0 $addr_lit =~ s/'/''/g;
696 0 $dbh->do("CALL quack_serve('$addr_lit')");
697 }
698  
699 0 return 1;
700 }
701  
702  
703 0 ▶sub quack_attach {
Subroutine quack_attach: not called CC=6 SCAR=37.4
Pod documented
704 0 my ($self, $addr, %opts) = @_;
705 0 ▶ $self->throw_exception('Usage: $storage->quack_attach($addr, as => $name, ...)') unless $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 0 0 0
706 0 _validate_quack_addr($self, $addr);
707  
708 my $as = $opts{as}
709 0 ▶ or $self->throw_exception('quack_attach: "as" option (catalog alias) is required');
Branch: 1 branch
BranchTrueFalseTotal
my $as = $opts{"as"} or $self->throw_exception("quack_attach: \"as\" option (catalog alias) is required") 0 0 0
710 0 ▶ $as =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$as =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("quack_attach: invalid catalog alias '${as}'") 0 0 0
711 or $self->throw_exception("quack_attach: invalid catalog alias '$as'");
712  
713 0 $self->duckdb_install_extension('quack', load => 1);
714  
715 0 my $addr_lit = $addr;
716 0 $addr_lit =~ s/'/''/g;
717 0 my $attach_sql = "ATTACH '$addr_lit' AS $as";
718 0 ▶ $attach_sql .= ' (READ_ONLY)' if $opts{read_only};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"read_only"} 0 0 0
719  
720 0 ▶ if (defined $opts{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'token'}) { } 0 0 0
721 0 _validate_quack_token($self, $opts{token});
722 0 my $tok_lit = $opts{token};
723 0 $tok_lit =~ s/'/''/g;
724 0 my $secret = _quack_secret_name($self, $as);
725  
726 # One transaction, because DuckDB's secret store is transactional and a
727 # leftover is not harmless: of two secrets with the same scope the name
728 # that sorts first wins, so the secret of a failed attach would shadow
729 # the token of a later alias for the same address. The rollback also
730 # puts back a secret this CREATE OR REPLACE overwrote (karr k32).
731 #
732 # DuckDB quotes the failed statement in its error text, and the CREATE
733 # SECRET one holds the token (a NUL in it fails the parse, and the parser
734 # repeats it): the token goes through mask_connect_secrets as the
735 # password of the connect info (karr k42).
736 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
737 0 ▶ $self->txn_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
738 0 my $dbh = $self->_get_dbh;
739 0 $dbh->do("CREATE OR REPLACE SECRET $secret (TYPE quack, TOKEN '$tok_lit', SCOPE '$addr_lit')");
740 0 $dbh->do($attach_sql);
741 0 });
742 }
743 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
744 0 my $msg = mask_connect_secrets("$_", [ undef, undef, $opts{token}, {} ]);
745 0 ▶ $msg .= "\n" unless $msg =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n\z/ 0 0 0
746 0 die $msg;
747 0 };
748 }
749 else {
750 0 $self->_get_dbh->do($attach_sql);
751 }
752  
753 0 return 1;
754 }
755  
756  
757 0 ▶sub quack_detach {
Subroutine quack_detach: not called CC=5 SCAR=34.0
Pod documented
758 0 my ($self, $name) = @_;
759 0 ▶ $self->throw_exception('Usage: $storage->quack_detach($name)') unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
760 0 ▶ $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("quack_detach: invalid catalog name '${name}'") 0 0 0
761 or $self->throw_exception("quack_detach: invalid catalog name '$name'");
762 0 my $dbh = $self->_get_dbh;
763 # Cleaning up wins over the DETACH result: the DROP runs even when the
764 # DETACH died, and the DETACH error is the one reported, also when the
765 # DROP dies too (karr k36).
766 0 my $detached = eval { $dbh->do("DETACH $name"); 1 };
767 0 my $detach_error = $@;
768 0 my $dropped = eval {
769 0 $dbh->do('DROP SECRET IF EXISTS ' . _quack_secret_name($self, $name));
770 0 1;
771 };
772 0 my $drop_error = $@;
773 0 ▶ die $detach_error unless $detached;
Branch: 1 branch
BranchTrueFalseTotal
unless $detached 0 0 0
774 0 ▶ die $drop_error unless $dropped;
Branch: 1 branch
BranchTrueFalseTotal
unless $dropped 0 0 0
775 0 return 1;
776 }
777  
778  
779 0 ▶sub connect_call_quack_attach {
Subroutine connect_call_quack_attach: not called CC=3 SCAR=24.8
Pod documented
780 0 my ($self, $args) = @_;
781 0 ▶ $self->throw_exception('connect_call_quack_attach: hashref argument required')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 0 0
782 unless ref $args eq 'HASH';
783 my $addr = $args->{addr}
784 0 ▶ or $self->throw_exception('connect_call_quack_attach: "addr" required in args hashref');
Branch: 1 branch
BranchTrueFalseTotal
my $addr = $args->{"addr"} or $self->throw_exception("connect_call_quack_attach: \"addr\" required in args hashref") 0 0 0
785 0 return $self->quack_attach($addr, %{ $args }{ grep { $_ ne 'addr' } keys %$args });
786 }
787  
788 # ---- private helpers ----
789  
790 0 ▶sub _validate_quack_addr {
Subroutine _validate_quack_addr: not called CC=4 SCAR=30.0
791 0 my ($self, $addr) = @_;
792 0 ▶ $addr =~ /^quack:/
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /^quack:/ or $self->throw_exception(q[quack address must start with 'quack:' (got: ] . mask_connect_secrets($addr) . ')') 0 0 0
793 or $self->throw_exception(
794 "quack address must start with 'quack:' (got: " . mask_connect_secrets($addr) . ')'
795 );
796 0 ▶ $addr =~ /'/
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /'/ and $self->throw_exception("quack address must not contain single quotes") 0 0 0
797 and $self->throw_exception("quack address must not contain single quotes");
798 0 ▶ $addr =~ /\n/
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /\n/ and $self->throw_exception("quack address must not contain newlines") 0 0 0
799 and $self->throw_exception("quack address must not contain newlines");
800 0 return 1;
801 }
802  
803 0 ▶sub _validate_quack_token {
Subroutine _validate_quack_token: not called CC=2 SCAR=17.9
804 0 my ($self, $tok) = @_;
805 0 ▶ $tok =~ /\n/
Branch: 1 branch
BranchTrueFalseTotal
$tok =~ /\n/ and $self->throw_exception("quack token must not contain newlines") 0 0 0
806 and $self->throw_exception("quack token must not contain newlines");
807 0 return 1;
808 }
809  
810 # Name of the secret quack_attach creates for an alias and quack_detach
811 # drops again. Both validate the alias as an identifier before asking.
812 0 ▶sub _quack_secret_name {
Subroutine _quack_secret_name: not called CC=1 SCAR=6.9
813 0 my ($self, $alias) = @_;
814 0 return "dbio_quack_$alias";
815 }
816  
817  
818 # Types a double cannot represent exactly, keyed by the lower-cased data_type
819 # with any size/precision suffix removed (so 'decimal(38,10)' arrives here as
820 # 'decimal'). Spellings cover both what DBIO::DuckDB::DDL accepts and what
821 # DuckDB itself reports back through introspection.
822 34 my %_BIND_AS_STRING = map { $_ => 1 } qw(
823 bigint bigserial int8 int64 long
824 ubigint uint64
825 hugeint uhugeint int128 uint128
826 varint bignum
827 decimal numeric dec money
828 );
829  
830 # Text and binary columns: DBD::DuckDB sniffs a value that looks like a number
831 # or a boolean ('01234', '5.0', '1') and binds it as one, so a text column hands
832 # back 1234.0 for '01234' and a comparison fails with a conversion error; a
833 # value with a NUL or a byte string fails to bind as text. The column's own
834 # type decides instead (karr k49, Bind conformance).
835 34 my %_BIND_AS_TEXT = map { $_ => 1 } qw(
836 varchar char bpchar text string nvarchar nchar clob json
837 );
838 34 my %_BIND_AS_BLOB = map { $_ => 1 } qw(
839 blob bytea binary varbinary longblob mediumblob tinyblob
840 );
841  
842 0 ▶sub bind_attribute_by_data_type {
Subroutine bind_attribute_by_data_type: not called CC=5 SCAR=34.0
Pod documented
843 0 my ($self, $data_type) = @_;
844  
845 0 ▶ return undef unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 0 0 0
846  
847 0 (my $type = lc $data_type) =~ s/\(.*//s;
848 0 $type =~ s/\A\s+//;
849 0 $type =~ s/\s+\z//;
850  
851 0 ▶ return DBI::SQL_BIGINT() if $_BIND_AS_STRING{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $_BIND_AS_STRING{$type} 0 0 0
852 0 ▶ return DBI::SQL_VARCHAR() if $_BIND_AS_TEXT{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $_BIND_AS_TEXT{$type} 0 0 0
853 0 ▶ return DBI::SQL_BLOB() if $_BIND_AS_BLOB{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $_BIND_AS_BLOB{$type} 0 0 0
854 0 return undef;
855 }
856  
857  
858 0 ▶sub _get_server_version {
Subroutine _get_server_version: not called CC=2 SCAR=17.9
859 0 my $self = shift;
860  
861 0 my $version = $self->next::method(@_);
862  
863 0 ▶ $version =~ s/^v(?=[0-9])// if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 0 0
864  
865 0 return $version;
866 }
867  
868  
869 34 1;
870  
871 __END__
« blib/lib/DBIO/DuckDB/Introspect/Tables.pm ↑ Summary blib/lib/DBIO/DuckDB/Introspect/Uniques.pm »