| 1 | package DBIO::ClickHouse::Introspect::Tables; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect ClickHouse tables and views via system.tables | ||||||||||||||||||||||||||||
| 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 DBIO::ClickHouse::Introspect::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 2 | my ($class, $dbh, $database) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | my $sql = q{ | |||||||||||||||||||||||||||
| 17 | SELECT | ||||||||||||||||||||||||||||
| 18 | name, | ||||||||||||||||||||||||||||
| 19 | database, | ||||||||||||||||||||||||||||
| 20 | engine, | ||||||||||||||||||||||||||||
| 21 | engine_full, | ||||||||||||||||||||||||||||
| 22 | sorting_key, | ||||||||||||||||||||||||||||
| 23 | primary_key, | ||||||||||||||||||||||||||||
| 24 | partition_key, | ||||||||||||||||||||||||||||
| 25 | sampling_key, | ||||||||||||||||||||||||||||
| 26 | comment, | ||||||||||||||||||||||||||||
| 27 | create_table_query | ||||||||||||||||||||||||||||
| 28 | FROM system.tables | ||||||||||||||||||||||||||||
| 29 | WHERE database = %s | ||||||||||||||||||||||||||||
| 30 | ORDER BY name | ||||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||
| 32 | 2 | my @bind; | |||||||||||||||||||||||||||
| 33 | 2 | ▶ | if (defined $database) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | $sql = sprintf $sql, '?'; | |||||||||||||||||||||||||||
| 35 | 0 | push @bind, $database; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | else { | ||||||||||||||||||||||||||||
| 38 | 2 | $sql = sprintf $sql, 'currentDatabase()'; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 42 | 2 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | my %tables; | |||||||||||||||||||||||||||
| 45 | 2 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 4 | my $name = $row->{name}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | my @sorting = _entries($row->{sorting_key}); | |||||||||||||||||||||||||||
| 49 | 4 | my @primary = _entries($row->{primary_key}); | |||||||||||||||||||||||||||
| 50 | 4 | my @partition = _entries($row->{partition_key}); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # ClickHouse leaves primary_key empty for engines that have no key at all | ||||||||||||||||||||||||||||
| 53 | # (Log, Memory, View); for MergeTree it mirrors the sorting key unless a | ||||||||||||||||||||||||||||
| 54 | # narrower PRIMARY KEY prefix was declared. | ||||||||||||||||||||||||||||
| 55 | 4 | ▶ | @primary = @sorting unless @primary; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | $tables{$name} = { | ||||||||||||||||||||||||||||
| 58 | table_name => $name, | ||||||||||||||||||||||||||||
| 59 | database => $row->{database}, | ||||||||||||||||||||||||||||
| 60 | kind => _kind($row->{engine}), | ||||||||||||||||||||||||||||
| 61 | engine => $row->{engine}, | ||||||||||||||||||||||||||||
| 62 | engine_full => $row->{engine_full}, | ||||||||||||||||||||||||||||
| 63 | engine_expression => _engine_expression($row->{engine}, $row->{engine_full}), | ||||||||||||||||||||||||||||
| 64 | sorting_key => $row->{sorting_key}, | ||||||||||||||||||||||||||||
| 65 | sorting_key_entries => \@sorting, | ||||||||||||||||||||||||||||
| 66 | primary_key => $row->{primary_key}, | ||||||||||||||||||||||||||||
| 67 | primary_key_entries => \@primary, | ||||||||||||||||||||||||||||
| 68 | 2 | primary_key_columns => [ grep { /\A\w+\z/ } @primary ], | |||||||||||||||||||||||||||
| 69 | partition_key => $row->{partition_key}, | ||||||||||||||||||||||||||||
| 70 | partition_key_entries => \@partition, | ||||||||||||||||||||||||||||
| 71 | sampling_key => $row->{sampling_key}, | ||||||||||||||||||||||||||||
| 72 | comment => $row->{comment}, | ||||||||||||||||||||||||||||
| 73 | 4 | }; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # What a view selects, as the server holds it: the diff compares it | ||||||||||||||||||||||||||||
| 76 | # (DBIO::ClickHouse::Diff::View). Only views carry one. | ||||||||||||||||||||||||||||
| 77 | 4 | ▶ | if ($tables{$name}{kind} eq 'view') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | $tables{$name}{definition} = DBIO::ClickHouse::Introspect::Util->view_select( | ||||||||||||||||||||||||||||
| 79 | 2 | $row->{create_table_query}); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | return \%tables; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 12 | ▶ | sub _entries { | ||||||||||||||||||||||||||
|
Subroutine
_entries: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 12 | my ($expression) = @_; | |||||||||||||||||||||||||||
| 88 | 12 | return DBIO::ClickHouse::Introspect::Util->split_top_level($expression); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # ClickHouse has no table_type column: the engine is the discriminator. | ||||||||||||||||||||||||||||
| 92 | # View, MaterializedView, LiveView and WindowView are all views. | ||||||||||||||||||||||||||||
| 93 | 7 | ▶ | sub _kind { | ||||||||||||||||||||||||||
|
Subroutine
_kind: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 94 | 7 | my ($engine) = @_; | |||||||||||||||||||||||||||
| 95 | 7 | ▶ | return (($engine // '') =~ /View\z/) ? 'view' : 'table'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$engine // ""
MC/DC0%0/1
$engine // ""
$engine
Truth table50%1/2
A
A= $engine
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # engine_full is "<Engine>[(args)] [PARTITION BY ...] [ORDER BY ...] [SETTINGS ...]". | ||||||||||||||||||||||||||||
| 99 | # Take the leading engine term with its arguments, if any. | ||||||||||||||||||||||||||||
| 100 | 7 | ▶ | sub _engine_expression { | ||||||||||||||||||||||||||
|
Subroutine
_engine_expression: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 101 | 7 | my ($engine, $engine_full) = @_; | |||||||||||||||||||||||||||
| 102 | 7 | ▶ | return $engine unless defined $engine_full && length $engine_full; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $engine_full and length $engine_full
MC/DC50%1/2
defined $engine_full and length $engine_full
defined $engine_full
length $engine_full
Truth table66%2/3
A and B
A= defined $engine_fullB= length $engine_full
| |||||||||||||||||||||||||||||
| 103 | 6 | ▶ | return $1 if $engine_full =~ /\A(\w+\((?:[^()]|\([^()]*\))*\))/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 3 | ▶ | return $1 if $engine_full =~ /\A(\w+)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | return $engine; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 34 | 1; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||