File Coverage

blib/lib/Pg/Explain/FromYAML.pm
Criterion Covered Total %
statement 103 103 100.0
branch 23 28 82.1
condition 2 3 66.6
subroutine 23 23 100.0
pod 1 1 100.0
total 152 158 96.2


line stmt bran cond sub pod time code
1             package Pg::Explain::FromYAML;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 73     73   950 use v5.18;
  73         223  
5 73     73   509 use strict;
  73         139  
  73         1328  
6 73     73   459 use warnings;
  73         122  
  73         1900  
7 73     73   507 use warnings qw( FATAL utf8 );
  73         122  
  73         2071  
8 73     73   463 use utf8;
  73         130  
  73         387  
9 73     73   1847 use open qw( :std :utf8 );
  73         134  
  73         359  
10 73     73   8884 use Unicode::Normalize qw( NFC );
  73         170  
  73         3834  
11 73     73   628 use Unicode::Collate;
  73         142  
  73         2083  
12 73     73   540 use Encode qw( decode );
  73         162  
  73         4165  
13              
14             if ( grep /\P{ASCII}/ => @ARGV ) {
15             @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;
16             }
17              
18             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
19              
20 73     73   14377 use base qw( Pg::Explain::From );
  73         140  
  73         30148  
21 73     73   29081 use YAML::XS;
  73         179280  
  73         3851  
22 73     73   616 use Carp;
  73         146  
  73         3098  
23 73     73   569 use Pg::Explain::JIT;
  73         137  
  73         1281  
24 73     73   521 use Pg::Explain::Buffers;
  73         124  
  73         45669  
25              
26             =head1 NAME
27              
28             Pg::Explain::FromYAML - Parser for explains in YAML format
29              
30             =head1 VERSION
31              
32             Version 2.2
33              
34             =cut
35              
36             our $VERSION = '2.2';
37              
38             =head1 SYNOPSIS
39              
40             It's internal class to wrap some work. It should be used by Pg::Explain, and not directly.
41              
42             =head1 FUNCTIONS
43              
44             =head2 parse_source
45              
46             Function which parses actual plan, and constructs Pg::Explain::Node objects
47             which represent it.
48              
49             Returns Top node of query plan.
50              
51             =cut
52              
53             sub parse_source {
54 71     71 1 367 my $self = shift;
55 71         130 my $source = shift;
56              
57             # If this is plan from auto-explain
58 71 100       767 if ( $source =~ s{ \A (\s*) Query \s+ Text : \s+ " ( [^\n]* ) " \s* \n }{}xms ) {
59 5         173 my ( $prefix, $query ) = ( $1, $2 );
60              
61             # Change prefix to two spaces in all lines
62 5         89 $source =~ s{^$prefix}{ }gm;
63              
64             # Add - to first line (should be Plan:)
65 5         23 $source =~ s{ \A \s \s }{- }xms;
66              
67 5         175 $query =~ s/\\n/\n/g;
68 5         15 $query =~ s/\\r/\r/g;
69 5         9 $query =~ s/\\t/\t/g;
70 5         177 $query =~ s/\\(.)/$1/g;
71 5         19 $self->explain->query( $query );
72              
73             }
74              
75 71 50       1115 unless ( $source =~ s{\A .*? ^ (\s*) ( - \s+ Plan: \s*\n )}{$1$2}xms ) {
76 1         164 carp( 'Source does not match first s///' );
77 1         6 return;
78             }
79              
80 71         1673 $source =~ s{ ^ ( \s* | [^\s-] [^\n] * ) \n .* \z }{}xms;
81              
82             # Make sure boolean variables will be properly marked if/for exporting to JSON
83 71         330 $YAML::XS::Boolean = "JSON::PP";
84 71     19   2000 my $struct = Load( $source )->[ 0 ];
  18     13   13822  
  18     6   220541  
  18     5   3742  
  12     4   93  
  12     3   29  
  12     3   1922  
  5     2   37  
  5         10  
  5         750  
  4         29  
  4         10  
  4         568  
  3         17  
  3         5  
  3         331  
  2         11  
  2         4  
  2         294  
  2         13  
  2         4  
  2         248  
  2         11  
  2         5  
  2         306  
85              
86 71         832 my $top_node = $self->make_node_from( $struct->{ 'Plan' } );
87              
88 71 100       428 if ( $struct->{ 'Planning' } ) {
    100          
89 5         21 $self->explain->planning_time( $struct->{ 'Planning' }->{ 'Planning Time' } );
90 5         19 my $buffers = Pg::Explain::Buffers->new( $struct->{ 'Planning' } );
91 5 100       185 $self->explain->planning_buffers( $buffers ) if $buffers;
92             }
93             elsif ( $struct->{ 'Planning Time' } ) {
94 50         135 $self->explain->planning_time( $struct->{ 'Planning Time' } );
95             }
96 71 100       231 $self->explain->execution_time( $struct->{ 'Execution Time' } ) if $struct->{ 'Execution Time' };
97 71 100       403 $self->explain->total_runtime( $struct->{ 'Total Runtime' } ) if $struct->{ 'Total Runtime' };
98 70 100       158 if ( $struct->{ 'Triggers' } ) {
99 1         3 for my $t ( @{ $struct->{ 'Triggers' } } ) {
  1         2  
100 2         3 my $ts = {};
101 2 50       7 $ts->{ 'calls' } = $t->{ 'Calls' } if defined $t->{ 'Calls' };
102 2 50       6 $ts->{ 'time' } = $t->{ 'Time' } if defined $t->{ 'Time' };
103 2 50       5 $ts->{ 'relation' } = $t->{ 'Relation' } if defined $t->{ 'Relation' };
104 2 50       6 $ts->{ 'name' } = $t->{ 'Trigger Name' } if defined $t->{ 'Trigger Name' };
105 2         5 $self->explain->add_trigger_time( $ts );
106             }
107             }
108 70 100       165 $self->explain->jit( Pg::Explain::JIT->new( 'struct' => $struct->{ 'JIT' } ) ) if $struct->{ 'JIT' };
109              
110 70 100 66     190 $self->explain->settings( $struct->{ 'Settings' } ) if ( $struct->{ 'Settings' } ) && ( 0 < scalar keys %{ $struct->{ 'Settings' } } );
  2         12  
111              
112 70         682 return $top_node;
113             }
114              
115             =head1 AUTHOR
116              
117             hubert depesz lubaczewski, C<< >>
118              
119             =head1 BUGS
120              
121             Please report any bugs or feature requests to C.
122              
123             =head1 SUPPORT
124              
125             You can find documentation for this module with the perldoc command.
126              
127             perldoc Pg::Explain
128              
129             =head1 COPYRIGHT & LICENSE
130              
131             Copyright 2008-2021 hubert depesz lubaczewski, all rights reserved.
132              
133             This program is free software; you can redistribute it and/or modify it
134             under the same terms as Perl itself.
135              
136             =cut
137              
138             1; # End of Pg::Explain::FromYAML