| 1 | package DBIO::ClickHouse::SQLMaker; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: ClickHouse-specific SQL generation for DBIO | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use base qw( DBIO::SQLMaker ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # ClickHouse has no SELECT ... FOR UPDATE (no row locks, no transactions). | ||||||||||||||||||||||||||||||||||||||||||
| 13 | sub _lock_select () { '' } | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 26 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 26 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # exists(), not truthiness: an explicit undef means "emit no SETTINGS clause" | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # and must survive, and 0 is a legal value. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 26 | ▶ | $opts{mutations_sync} = 1 unless exists $opts{mutations_sync}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | $self->throw_exception(sprintf | ||||||||||||||||||||||||||||||||||||||||||
| 25 | q{Invalid mutations_sync value '%s': ClickHouse accepts 0 (queue the } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | . 'mutation and return), 1 (wait for it on this server) or 2 (wait for it on ' | ||||||||||||||||||||||||||||||||||||||||||
| 27 | . 'all replicas). Pass undef to emit no SETTINGS clause and leave the setting ' | ||||||||||||||||||||||||||||||||||||||||||
| 28 | . 'to the session or the server profile', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | $opts{mutations_sync} | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 26 | ▶ | ) if defined $opts{mutations_sync} and $opts{mutations_sync} !~ /\A[012]\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opts{"mutations_sync"} and not $opts{"mutations_sync"} =~ /\A[012]\z/
MC/DC100%2/2
defined $opts{"mutations_sync"} and not $opts{"mutations_sync"} =~ /\A[012]\z/
defined $opts{"mutations_sync"}
not $opts{"mutations_sync"} =~ /\A[012]\z/
Truth table100%3/3
A and not(B)
A= defined $opts{"mutations_sync"}B= not $opts{"mutations_sync"} =~ /\A[012]\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # ClickHouse spells the assignment list without a SET keyword, which is what | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # SQL::Abstract's generic clause renderer would prepend. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 23 | ▶ | $opts{render_clause}{'update.set'} ||= '_render_update_clause_set'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"render_clause"}{"update.set"} ||= "_render_update_clause_set"
MC/DC0%0/1
$opts{"render_clause"}{"update.set"} ||= "_render_update_clause_set"
$opts{"render_clause"}{"update.set"}
Truth table50%1/2
A
A= $opts{"render_clause"}{"update.set"}
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 23 | return $self->next::method(\%opts); | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 21 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 21 | my ($self, $table, $set, $where, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 21 | $self->_assert_mutation_target(update => $table); | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 20 | ▶ | $self->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $set eq "HASH" and keys %$set
MC/DC100%2/2
ref $set eq "HASH" and keys %$set
ref $set eq "HASH"
keys %$set
Truth table100%3/3
A and B
A= ref $set eq "HASH"B= keys %$set
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 'An UPDATE on ClickHouse renders as ALTER TABLE ... UPDATE col = ..., ' | ||||||||||||||||||||||||||||||||||||||||||
| 47 | . 'which has no form without an assignment: update() needs a non-empty ' | ||||||||||||||||||||||||||||||||||||||||||
| 48 | . 'hashref of columns to set' | ||||||||||||||||||||||||||||||||||||||||||
| 49 | ) unless ref $set eq 'HASH' and keys %$set; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 18 | my ($sql, @bind) = $self->next::method( | |||||||||||||||||||||||||||||||||||||||||
| 52 | $table, $set, $self->_mutation_where($where), $options | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 18 | $sql .= $self->_mutations_sync_clause; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 18 | ▶ | return wantarray ? ($sql, @bind) : $sql; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 24 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 24 | my ($self, $table, $where, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 24 | $self->_assert_mutation_target(delete => $table); | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 21 | my ($sql, @bind) = $self->next::method( | |||||||||||||||||||||||||||||||||||||||||
| 66 | $table, $self->_mutation_where($where), $options | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 21 | $sql .= $self->_mutations_sync_clause; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 21 | ▶ | return wantarray ? ($sql, @bind) : $sql; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | # ALTER TABLE names exactly one real table. Everything else core can hand a | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # storage as an "ident" -- a from-list, a virtual view's literal SQL -- has no | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # ALTER form at all, and would otherwise reach the server as nonsense. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 45 | ▶ | sub _assert_mutation_target { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_mutation_target: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 45 | my ($self, $op, $table) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 45 | ▶ | return unless my $ref = ref $table; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | $self->throw_exception(sprintf | |||||||||||||||||||||||||||||||||||||||||
| 82 | 'ClickHouse renders %s as ALTER TABLE ... %s, which can only name a single ' | ||||||||||||||||||||||||||||||||||||||||||
| 83 | . 'real table -- but this one was given a %s target (a virtual view or other ' | ||||||||||||||||||||||||||||||||||||||||||
| 84 | . 'literal-SQL result source, or a join list). Run the %s against the ' | ||||||||||||||||||||||||||||||||||||||||||
| 85 | . 'underlying MergeTree table instead; if the rows are identified through ' | ||||||||||||||||||||||||||||||||||||||||||
| 86 | . 'another table, keep the target table as the result source and put the ' | ||||||||||||||||||||||||||||||||||||||||||
| 87 | . 'other one in the WHERE clause as a subquery, e.g. ' | ||||||||||||||||||||||||||||||||||||||||||
| 88 | . q{$rs->search({ id => { -in => $other_rs->get_column('id')->as_query } })}, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | uc($op), uc($op), $ref, $op | ||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # A ClickHouse mutation must carry a filter expression: 'ALTER TABLE t DELETE' | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # is a syntax error, not a delete-everything. WHERE 1 is how ClickHouse spells | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # what an unrestricted $rs->delete means everywhere else. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 39 | ▶ | sub _mutation_where { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mutation_where: called CC=13 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 39 | my ($self, $where) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 39 | ▶ | return \'1' unless defined $where; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 38 | my $ref = ref $where; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 38 | ▶ | return \'1' if $ref eq 'HASH' and ! keys %$where; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ref eq "HASH" and not keys %$where
MC/DC100%2/2
$ref eq "HASH" and not keys %$where
$ref eq "HASH"
not keys %$where
Truth table100%3/3
A and not(B)
A= $ref eq "HASH"B= not keys %$where
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 37 | ▶ | return \'1' if $ref eq 'ARRAY' and ! @$where; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ref eq "ARRAY" and not @$where
MC/DC50%1/2
$ref eq "ARRAY" and not @$where
$ref eq "ARRAY"
not @$where
Truth table66%2/3
A and not(B)
A= $ref eq "ARRAY"B= not @$where
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | # \'literal SQL' | ||||||||||||||||||||||||||||||||||||||||||
| 107 | return \'1' if $ref eq 'SCALAR' | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | and (! defined ${$where} or ${$where} !~ /\S/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$ref eq "SCALAR" and not defined ${$where;} && ${$where;} =~ /\S/
defined ${$where;} && ${$where;} =~ /\S/
MC/DC0%0/3
$ref eq "SCALAR" and not defined ${$where;} && ${$where;} =~ /\S/
$ref eq "SCALAR"
defined ${$where;}
${$where;} =~ /\S/
Truth table25%1/4
A and not(B && C)
A= $ref eq "SCALAR"B= defined ${$where;}C= ${$where;} =~ /\S/
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # \[ $sql, @bind ] -- the shape DBIO::ResultSet::_rs_update_delete builds, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # which is empty SQL for a resultset with no condition. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | return \'1' if $ref eq 'REF' | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 8 | ▶ | and ref ${$where} eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
MC/DC100%2/2
defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
defined ${$where;}->[0]
${$where;}->[0] =~ /\S/
Truth table100%3/3
A && B
A= defined ${$where;}->[0]B= ${$where;}->[0] =~ /\S/
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 36 | ▶ | and (! defined ${$where}->[0] or ${$where}->[0] !~ /\S/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$ref eq "REF" and ref ${$where;} eq "ARRAY" and not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
$ref eq "REF" and ref ${$where;} eq "ARRAY"
MC/DC66%2/3
$ref eq "REF" and ref ${$where;} eq "ARRAY" and not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
$ref eq "REF"
ref ${$where;} eq "ARRAY"
not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
Truth table75%3/4
A and B and not(C)
A= $ref eq "REF"B= ref ${$where;} eq "ARRAY"C= not defined ${$where;}->[0] && ${$where;}->[0] =~ /\S/
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 34 | return $where; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | # Setting *names* are case-sensitive in ClickHouse, so only the keyword goes | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # through _sqlcase. The value is 0/1/2, validated in new(). | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 39 | ▶ | sub _mutations_sync_clause { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mutations_sync_clause: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 39 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 39 | ▶ | my $sync = exists $self->{mutations_sync} ? $self->{mutations_sync} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 39 | ▶ | return '' unless defined $sync; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 36 | return $self->_sqlcase(' settings ') . "mutations_sync = $sync"; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 18 | ▶ | sub _render_update_clause_target { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_update_clause_target: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 18 | my ($self, undef, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 18 | return $self->join_query_parts(' ', | |||||||||||||||||||||||||||||||||||||||||
| 134 | { -keyword => 'alter_table' }, $target, { -keyword => 'update' } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 18 | ▶ | sub _render_update_clause_set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_update_clause_set: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 18 | my ($self, undef, $set) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 18 | return $self->render_aqt($set, 1); | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 21 | ▶ | sub _render_delete_clause_target { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_delete_clause_target: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 21 | my ($self, undef, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 21 | return $self->join_query_parts(' ', | |||||||||||||||||||||||||||||||||||||||||
| 148 | { -keyword => 'alter_table' }, $target, { -keyword => 'delete' } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 22 | ▶ | sub insert_multi { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_multi: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 22 | my ($self, $table, $cols, $rows) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 22 | ▶ | $self->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $cols eq "ARRAY" and @$cols
MC/DC50%1/2
ref $cols eq "ARRAY" and @$cols
ref $cols eq "ARRAY"
@$cols
Truth table66%2/3
A and B
A= ref $cols eq "ARRAY"B= @$cols
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 'insert_multi() needs a non-empty arrayref of column names' | ||||||||||||||||||||||||||||||||||||||||||
| 158 | ) unless ref $cols eq 'ARRAY' and @$cols; | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 21 | ▶ | $self->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $rows eq "ARRAY" and @$rows
MC/DC100%2/2
ref $rows eq "ARRAY" and @$rows
ref $rows eq "ARRAY"
@$rows
Truth table100%3/3
A and B
A= ref $rows eq "ARRAY"B= @$rows
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 'insert_multi() needs a non-empty arrayref of rows' | ||||||||||||||||||||||||||||||||||||||||||
| 162 | ) unless ref $rows eq 'ARRAY' and @$rows; | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 19 | my @tuples; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 19 | for my $idx (0 .. $#$rows) { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 34 | my $row = $rows->[$idx]; | |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 34 | ▶ | $self->throw_exception(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 'insert_multi() row %d is not an arrayref: rows are positional and ' | ||||||||||||||||||||||||||||||||||||||||||
| 170 | . 'must line up with the %d supplied column names (%s)', | ||||||||||||||||||||||||||||||||||||||||||
| 171 | $idx, scalar @$cols, join ', ', @$cols | ||||||||||||||||||||||||||||||||||||||||||
| 172 | ) unless ref $row eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 33 | ▶ | $self->throw_exception(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 'insert_multi() row %d has %d value(s) but there are %d columns (%s): ' | ||||||||||||||||||||||||||||||||||||||||||
| 176 | . 'a multi-row INSERT is a single statement with a single column list, so ' | ||||||||||||||||||||||||||||||||||||||||||
| 177 | . 'every row must fill all of them', | ||||||||||||||||||||||||||||||||||||||||||
| 178 | $idx, scalar @$row, scalar @$cols, join ', ', @$cols | ||||||||||||||||||||||||||||||||||||||||||
| 179 | ) unless @$row == @$cols; | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | push @tuples, { -row => [ | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 31 | map { $self->_insert_multi_value($cols->[$_], $row->[$_]) } 0 .. $#$cols | |||||||||||||||||||||||||||||||||||||||||
| 183 | ] }; | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 14 | return $self->render_statement({ -insert => { | |||||||||||||||||||||||||||||||||||||||||
| 187 | target => $table, | ||||||||||||||||||||||||||||||||||||||||||
| 188 | fields => [ @$cols ], | ||||||||||||||||||||||||||||||||||||||||||
| 189 | from => { -values => \@tuples } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | }}); | ||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # One VALUES cell as an SQL::Abstract AQT node. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 66 | ▶ | sub _insert_multi_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert_multi_value: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 66 | my ($self, $col, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 66 | my $ref = ref $value; | |||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | # \'literal SQL' | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 66 | ▶ | return { -literal => [ $$value ] } if $ref eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # \[ 'literal SQL with ?', @binds ] | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 65 | ▶ | return { -literal => [ @$$value ] } if $ref eq 'REF' and ref $$value eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ref eq "REF" and ref $$value eq "ARRAY"
MC/DC50%1/2
$ref eq "REF" and ref $$value eq "ARRAY"
$ref eq "REF"
ref $$value eq "ARRAY"
Truth table66%2/3
A and B
A= $ref eq "REF"B= ref $$value eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | # A bare arrayref is never a value. ClickHouse does have Array(T) columns, | ||||||||||||||||||||||||||||||||||||||||||
| 206 | # which makes this a likely mistake rather than an exotic one, and no | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # MySQL-wire DBD can bind a Perl list -- so say what to write instead. | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 63 | ▶ | $self->throw_exception(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | q{insert_multi(): column '%s' was given a plain arrayref. There is no bind } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | . 'type for a list: write an Array(T) value as literal SQL with binds, ' | ||||||||||||||||||||||||||||||||||||||||||
| 211 | . q{\[ '[?, ?]', $a, $b ], or hand in the ClickHouse array literal as a } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | . 'string' | ||||||||||||||||||||||||||||||||||||||||||
| 213 | , $col) if $ref eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 61 | return { -bind => [ $col, $value ] }; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 442 | ▶ | sub _render_bind { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_bind: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 442 | my ($self, $type, $bind) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 442 | my ($col, $value) = @$bind; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 442 | ▶ | if (defined $value and !ref $value and $value =~ /\x1A/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $value and not ref $value and $value =~ /\x1A/
defined $value and not ref $value
MC/DC66%2/3
defined $value and not ref $value and $value =~ /\x1A/
defined $value
not ref $value
$value =~ /\x1A/
Truth table75%3/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\x1A/
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 6 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 6 | ▶ | $n++ while index($value, "ch1a${n}x") >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 6 | my $marker = "ch1a${n}x"; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 6 | (my $marked = $value) =~ s/\x1A/$marker/g; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 6 | return [ "replaceAll(?, '$marker', char(26))", $self->_bindtype($col, $marked) ]; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 436 | return $self->next::method($type, $bind); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 59 | ▶ | sub select { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 59 | my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 59 | ▶ | my $ch = ref $rs_attrs eq 'HASH' ? $rs_attrs : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 59 | ▶ | local $self->{_clickhouse_final} = $ch->{final} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 59 | local $self->{_clickhouse_prewhere} = $ch->{prewhere}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 59 | return $self->next::method($table, $fields, $where, $rs_attrs, @rest); | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 53 | ▶ | sub _assemble_binds { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assemble_binds: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 53 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 53 | ▶ | return map { @{ (delete $self->{"${_}_bind"}) || [] } } ( | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $$self{"${_}_bind"} || []
MC/DC100%1/1
delete $$self{"${_}_bind"} || []
delete $$self{"${_}_bind"}
Truth table100%2/2
A
A= delete $$self{"${_}_bind"}
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | qw/pre_select select from clickhouse_prewhere where group having order clickhouse_limit_by limit/ | ||||||||||||||||||||||||||||||||||||||||||
| 250 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 59 | ▶ | sub _table { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 59 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 59 | my $sql = $self->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | # A from-list goes through _gen_from_blocks, which has already attached FINAL | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # to the primary block and consumed the flag; a bare table name does not, so | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # it is picked up here instead. | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 59 | $sql .= $self->_clickhouse_final_sql; | |||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 59 | my $prewhere = $self->{_clickhouse_prewhere}; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 59 | ▶ | if (defined $prewhere) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 4 | my ($frag, @bind) = $self->_recurse_where($prewhere); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 4 | ▶ | if (defined $frag and length $frag) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $frag and length $frag
MC/DC0%0/2
defined $frag and length $frag
defined $frag
length $frag
Truth table33%1/3
A and B
A= defined $fragB= length $frag
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 4 | push @{$self->{clickhouse_prewhere_bind}}, @bind; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 4 | $sql .= $self->_sqlcase(' prewhere ') . $frag; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 59 | return $sql; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 57 | ▶ | sub _gen_from_blocks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gen_from_blocks: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 57 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | # Consumed *before* recursing: _recurse_from re-enters this method for nested | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # join groups, and FINAL belongs to the primary table of the outermost list | ||||||||||||||||||||||||||||||||||||||||||
| 282 | # only. | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 57 | my $final = $self->_clickhouse_final_sql; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 57 | my @blocks = $self->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 57 | ▶ | $blocks[0] .= $final if @blocks and length $final; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@blocks and length $final
MC/DC50%1/2
@blocks and length $final
@blocks
length $final
Truth table66%2/3
A and B
A= @blocksB= length $final
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 57 | return @blocks; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # Returns ' FINAL' the first time it is asked within one statement, '' after | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # that -- the modifier applies to exactly one table reference. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 116 | ▶ | sub _clickhouse_final_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clickhouse_final_sql: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 116 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 116 | ▶ | return '' unless $self->{_clickhouse_final}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 5 | $self->{_clickhouse_final} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 5 | return $self->_sqlcase(' final'); | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 62 | ▶ | sub _parse_rs_attrs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_rs_attrs: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 62 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 62 | my ($rs_attrs) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 62 | my $sql = $self->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 62 | ▶ | if (ref $rs_attrs eq 'HASH' and defined $rs_attrs->{limit_by}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $rs_attrs eq "HASH" and defined $rs_attrs->{"limit_by"}
MC/DC50%1/2
ref $rs_attrs eq "HASH" and defined $rs_attrs->{"limit_by"}
ref $rs_attrs eq "HASH"
defined $rs_attrs->{"limit_by"}
Truth table66%2/3
A and B
A= ref $rs_attrs eq "HASH"B= defined $rs_attrs->{"limit_by"}
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 9 | my ($limit_by_sql, @limit_by_bind) = $self->_limit_by($rs_attrs->{limit_by}); | |||||||||||||||||||||||||||||||||||||||||
| 309 | 3 | push @{$self->{clickhouse_limit_by_bind}}, @limit_by_bind; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | $sql .= $limit_by_sql; | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 56 | return $sql; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 9 | ▶ | sub _limit_by { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_limit_by: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 9 | my ($self, $spec) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 9 | ▶ | $self->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | q{The 'limit_by' attribute must be a hashref of { rows => $n, by => $columns }} | ||||||||||||||||||||||||||||||||||||||||||
| 322 | ) unless ref $spec eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 8 | my $rows = $spec->{rows}; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 8 | ▶ | $self->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $rows or $rows =~ /\D/ or $rows <= 0
not defined $rows or $rows =~ /\D/
MC/DC100%3/3
not defined $rows or $rows =~ /\D/ or $rows <= 0
not defined $rows
$rows =~ /\D/
$rows <= 0
Truth table100%4/4
not(A) or B or C
A= not defined $rowsB= $rows =~ /\D/C= $rows <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | q{The 'limit_by' attribute needs a positive integer 'rows' value} | ||||||||||||||||||||||||||||||||||||||||||
| 327 | ) if ! defined $rows or $rows =~ /\D/ or $rows <= 0; | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | $self->throw_exception( | ||||||||||||||||||||||||||||||||||||||||||
| 330 | q{The 'limit_by' attribute needs a 'by' column specification} | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 5 | ▶ | ) unless defined $spec->{by}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 4 | my ($by_sql, @bind) = $self->_recurse_fields($spec->{by}); | |||||||||||||||||||||||||||||||||||||||||
| 334 | 4 | ▶ | $self->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $by_sql and length $by_sql
MC/DC50%1/2
defined $by_sql and length $by_sql
defined $by_sql
length $by_sql
Truth table66%2/3
A and B
A= defined $by_sqlB= length $by_sql
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | q{The 'limit_by' attribute needs a 'by' column specification} | ||||||||||||||||||||||||||||||||||||||||||
| 336 | ) unless defined $by_sql and length $by_sql; | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 3 | $self->_sqlcase(' limit ') . $rows . $self->_sqlcase(' by ') . $by_sql, | |||||||||||||||||||||||||||||||||||||||||
| 340 | @bind | ||||||||||||||||||||||||||||||||||||||||||
| 341 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | __END__ | ||||||||||||||||||||||||||||||||||||||||||