| 1 | package DBIO::ClickHouse::Diff::Table; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for ClickHouse tables, and the no-silent-rebuild policy | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 38 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 38 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 38 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 38 | ▶ | use DBIO::Diff::Op (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 38 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 38 | ▶ | use DBIO::ClickHouse::DDL::Emit qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | quote_ident column_def index_def create_table drop_table | ||||||||||||||||||||||||||||||||||||||||||
| 15 | engine_clause partition_by_clause primary_key_clause order_by_clause | ||||||||||||||||||||||||||||||||||||||||||
| 16 | settings_clause | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 38 | ); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 38 | __PACKAGE__->mk_diff_accessors(qw/table_name table_info columns skipping_indexes storage settings/); | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | sub throw_exception { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
throw_exception: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | my ($self, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | DBIO::Exception->throw($message); | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # The four immutable properties. 'list' entries are compared order-preservingly | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # and rendered comma-joined; 'scalar' entries are compared as strings. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # The order is which one to REPORT, not the CREATE TABLE clause order, and the | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # sorting key deliberately comes before the primary key: ClickHouse fills the | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # primary key in from ORDER BY when none was declared separately, so changing | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # the sorting key changes both. Reporting the primary key there would name the | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # mirror instead of the cause and send the operator looking for a PRIMARY KEY | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # clause their schema never had. | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 38 | my @IMMUTABLE = ( | |||||||||||||||||||||||||||||||||||||||||
| 38 | { field => 'engine_expression', kind => 'scalar', label => 'engine' }, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | { field => 'partition_key_entries', kind => 'list', label => 'partition key (PARTITION BY)' }, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | { field => 'sorting_key_entries', kind => 'list', label => 'sorting key (ORDER BY)' }, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | { field => 'primary_key_entries', kind => 'list', label => 'primary key (PRIMARY KEY)' } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 99 | ▶ | sub check_rebuild_required { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_rebuild_required: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 99 | my ($class, $source, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 99 | ▶ | $source ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source ||= {}
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 99 | ▶ | $target ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target ||= {}
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 99 | for my $name (sort keys %$target) { | |||||||||||||||||||||||||||||||||||||||||
| 51 | 96 | ▶ | my $old = $source->{$name} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 82 | my $new = $target->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 82 | for my $property (@IMMUTABLE) { | |||||||||||||||||||||||||||||||||||||||||
| 55 | 317 | my $field = $property->{field}; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Desired-state contract: not prescribed by the target -> not compared. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 317 | ▶ | next unless defined $new->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | my ($old_value, $new_value) = $property->{kind} eq 'list' | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ? (_render_key($old->{$field}), _render_key($new->{$field})) | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 292 | ▶ | : (defined $old->{$field} ? $old->{$field} : '', $new->{$field}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 292 | ▶ | next if $old_value eq $new_value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | $class->throw_exception( | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 9 | _rebuild_message($name, $property->{label}, $old_value, $new_value)); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 90 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 422 | ▶ | sub _render_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render_key: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 422 | my ($entries) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 422 | ▶ | return '' unless ref $entries eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 422 | return join ', ', @$entries; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 9 | ▶ | sub _rebuild_message { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rebuild_message: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 9 | my ($table, $label, $old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 9 | my $live = quote_ident($table); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 9 | my $new_table = quote_ident($table . '_new'); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 9 | ▶ | $old = '(none)' if $old eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 9 | ▶ | $new = '(none)' if $new eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 9 | return <<"END_MESSAGE"; | |||||||||||||||||||||||||||||||||||||||||
| 89 | Table $live cannot be upgraded in place: its $label changed from | ||||||||||||||||||||||||||||||||||||||||||
| 90 | $old | ||||||||||||||||||||||||||||||||||||||||||
| 91 | to | ||||||||||||||||||||||||||||||||||||||||||
| 92 | $new | ||||||||||||||||||||||||||||||||||||||||||
| 93 | and ClickHouse fixes that property at CREATE TABLE time -- there is no ALTER | ||||||||||||||||||||||||||||||||||||||||||
| 94 | for it. DBIO::ClickHouse::Diff refuses to drop and recreate a data table on | ||||||||||||||||||||||||||||||||||||||||||
| 95 | your behalf, because that deletes every row in it and there is no transaction | ||||||||||||||||||||||||||||||||||||||||||
| 96 | to undo it with. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | Apply the change by hand, in this order: | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 1. CREATE TABLE $new_table (...) with the new definition. Take the exact | ||||||||||||||||||||||||||||||||||||||||||
| 101 | statement from DBIO::ClickHouse::DDL->install_ddl(\$schema) and rename the | ||||||||||||||||||||||||||||||||||||||||||
| 102 | table in it. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 2. INSERT INTO $new_table SELECT * FROM $live; | ||||||||||||||||||||||||||||||||||||||||||
| 104 | Then compare the row counts -- this copy is NOT atomic and NOT | ||||||||||||||||||||||||||||||||||||||||||
| 105 | transactional; concurrent inserts land in the old table only. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 3. EXCHANGE TABLES $new_table AND $live; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | Atomic on an Atomic database (the default). On an Ordinary database use | ||||||||||||||||||||||||||||||||||||||||||
| 108 | RENAME TABLE twice instead, and accept the gap between them. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 4. DROP TABLE $new_table; | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | Re-run the upgrade afterwards; this check will pass and the remaining column | ||||||||||||||||||||||||||||||||||||||||||
| 112 | and index changes will be applied normally. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | END_MESSAGE | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 98 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 98 | my ($class, $source, $target, $target_columns, $target_indexes, $storage, | |||||||||||||||||||||||||||||||||||||||||
| 119 | $declared_settings) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 98 | ▶ | $declared_settings //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$declared_settings //= {}
MC/DC100%1/1
$declared_settings //= {}
$declared_settings
Truth table100%2/2
A
A= $declared_settings
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 98 | ▶ | $source ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source ||= {}
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 98 | ▶ | $target ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target ||= {}
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 98 | ▶ | $target_columns //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_columns //= {}
MC/DC0%0/1
$target_columns //= {}
$target_columns
Truth table50%1/2
A
A= $target_columns
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 98 | ▶ | $target_indexes //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_indexes //= {}
MC/DC0%0/1
$target_indexes //= {}
$target_indexes
Truth table50%1/2
A
A= $target_indexes
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 98 | $class->check_rebuild_required($source, $target); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | return $class->diff_toplevel($source, $target, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 14 | ▶ | create => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 14 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 131 | $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 132 | action => 'create', | ||||||||||||||||||||||||||||||||||||||||||
| 133 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 134 | table_info => $target->{$name}, | ||||||||||||||||||||||||||||||||||||||||||
| 135 | columns => $target_columns->{$name} // [], | ||||||||||||||||||||||||||||||||||||||||||
| 136 | skipping_indexes => $target_indexes->{$name} // {}, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | storage => $storage, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 14 | ▶ | settings => $declared_settings->{$name}, | ||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$target_columns->{$name} // []
$target_indexes->{$name} // {}
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_indexes->{$name} // {}
$target_indexes->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Truth table100%2/2
A
A= $target_indexes->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 10 | ▶ | drop => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 10 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 143 | $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 144 | action => 'drop', | ||||||||||||||||||||||||||||||||||||||||||
| 145 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 10 | table_info => $source->{$name}, | |||||||||||||||||||||||||||||||||||||||||
| 147 | storage => $storage, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 89 | ); | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 27 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=12 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 27 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 27 | ▶ | my $info = $self->table_info // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->table_info // {}
MC/DC0%0/1
$self->table_info // {}
$self->table_info
Truth table50%1/2
A
A= $self->table_info
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 27 | ▶ | my $if_guard = DBIO::Diff::Op::should_emit_if_exists($self->storage) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 27 | ▶ | return drop_table(name => $self->table_name, if_exists => $if_guard) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 16 | my @body = map { _column_def($_) } @{ $self->columns }; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | # Skipping indexes are part of the table body, not a separate statement. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 16 | ▶ | my $indexes = $self->skipping_indexes // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->skipping_indexes // {}
MC/DC0%0/1
$self->skipping_indexes // {}
$self->skipping_indexes
Truth table50%1/2
A
A= $self->skipping_indexes
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 16 | push @body, index_def($indexes->{$_}) for sort keys %$indexes; | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 16 | ▶ | my @sorting = @{ $info->{sorting_key_entries} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"sorting_key_entries"} // []
MC/DC0%0/1
$info->{"sorting_key_entries"} // []
$info->{"sorting_key_entries"}
Truth table50%1/2
A
A= $info->{"sorting_key_entries"}
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 16 | ▶ | my @primary = @{ $info->{primary_key_entries} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"primary_key_entries"} // []
MC/DC0%0/1
$info->{"primary_key_entries"} // []
$info->{"primary_key_entries"}
Truth table50%1/2
A
A= $info->{"primary_key_entries"}
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 16 | ▶ | my @clauses = (engine_clause($info->{engine_expression} // $info->{engine})); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$info->{"engine_expression"} // $info->{"engine"}
MC/DC0%0/2
$info->{"engine_expression"} // $info->{"engine"}
$info->{"engine_expression"}
$info->{"engine"}
Truth table33%1/3
A // B
A= $info->{"engine_expression"}B= $info->{"engine"}
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | push @clauses, partition_by_clause($info->{partition_key}) | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 16 | ▶ | if defined $info->{partition_key} && length $info->{partition_key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"partition_key"} and length $info->{"partition_key"}
MC/DC50%1/2
defined $info->{"partition_key"} and length $info->{"partition_key"}
defined $info->{"partition_key"}
length $info->{"partition_key"}
Truth table66%2/3
A and B
A= defined $info->{"partition_key"}B= length $info->{"partition_key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 16 | ▶ | push @clauses, primary_key_clause(map { _key_entry($_) } @primary) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | if _key_differs(\@primary, \@sorting); | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 16 | push @clauses, order_by_clause(map { _key_entry($_) } @sorting); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 16 | push @clauses, settings_clause($self->settings); | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 16 | return create_table( | |||||||||||||||||||||||||||||||||||||||||
| 181 | name => $self->table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 182 | columns => \@body, | ||||||||||||||||||||||||||||||||||||||||||
| 183 | clauses => \@clauses, | ||||||||||||||||||||||||||||||||||||||||||
| 184 | if_not_exists => $if_guard, | ||||||||||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | # A model column carries the RAW ClickHouse default expression plus the kind | ||||||||||||||||||||||||||||||||||||||||||
| 189 | # it was declared with. DEFAULT goes through Emit's column_def; MATERIALIZED / | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # ALIAS / EPHEMERAL are a different clause with different semantics (computed, | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # or not stored at all), so rendering them as DEFAULT would quietly change what | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # the column means -- they are appended as themselves instead. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 33 | ▶ | sub _column_def { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_column_def: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 33 | my ($col) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 33 | ▶ | my $kind = uc($col->{default_kind} // 'DEFAULT'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$col->{"default_kind"} // "DEFAULT"
MC/DC100%1/1
$col->{"default_kind"} // "DEFAULT"
$col->{"default_kind"}
Truth table100%2/2
A
A= $col->{"default_kind"}
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 33 | my $expression = $col->{default_value}; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 33 | my $is_default = $kind eq 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | my $def = column_def( | ||||||||||||||||||||||||||||||||||||||||||
| 201 | name => $col->{column_name}, | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 33 | ▶ | type => ($col->{data_type} // 'String'), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$col->{"data_type"} // "String"
MC/DC0%0/1
$col->{"data_type"} // "String"
$col->{"data_type"}
Truth table50%1/2
A
A= $col->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | default => ($is_default ? $expression : undef) | ||||||||||||||||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 33 | ▶ | $def .= " $kind $expression" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not $is_default and defined $expression and length $expression
not $is_default and defined $expression
MC/DC33%1/3
not $is_default and defined $expression and length $expression
not $is_default
defined $expression
length $expression
Truth table50%2/4
not(A) and B and C
A= not $is_defaultB= defined $expressionC= length $expression
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | if !$is_default && defined $expression && length $expression; | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 33 | return $def; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | # A bare column name is an identifier and gets quoted; anything else is a key | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # expression (toYYYYMM(ts), cityHash64(id)) and has to reach Emit as a SCALAR | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # ref, or it would come out backtick-quoted as one long identifier. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 22 | ▶ | sub _key_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_key_entry: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 22 | my ($entry) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 22 | ▶ | return $entry if $entry =~ /\A\w+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $expression = $entry; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | return \$expression; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 16 | ▶ | sub _key_differs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_key_differs: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 16 | my ($left, $right) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 16 | ▶ | return 1 if @$left != @$right; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 13 | for my $i (0 .. $#$left) { | |||||||||||||||||||||||||||||||||||||||||
| 226 | 13 | ▶ | return 1 if $left->[$i] ne $right->[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 13 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | return sprintf '%s table: %s', $self->summary_prefix, $self->table_name; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 38 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | __END__ | ||||||||||||||||||||||||||||||||||||||||||