| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use JSON; |
| 4 |
use File::Spec::Functions qw/ canonpath /; |
| 5 |
my $usage = "This is from WL#5257 \"first API for optimizer trace\". |
| 6 |
|
| 7 |
Usage: |
| 8 |
%s [-q] <a_file> <another_file> <etc> |
| 9 |
|
| 10 |
-q quiet mode: only display errors and warnings. |
| 11 |
|
| 12 |
It will verify that all optimizer traces of files (usually a_file |
| 13 |
is a .result or .reject file which contains |
| 14 |
SELECT * FROM OPTIMIZER_TRACE; ) are JSON-compliant, and that |
| 15 |
they contain no duplicates keys. |
| 16 |
Exit code is 0 if all ok."; |
| 17 |
my $retcode = 0; |
| 18 |
my @ignored; |
| 19 |
my @input = @ARGV; |
| 20 |
|
| 21 |
# Filter out "-q" options |
| 22 |
@input = grep {!/-q/} @input; |
| 23 |
|
| 24 |
if (!@input) |
| 25 |
{ |
| 26 |
print "$usage\n"; |
| 27 |
exit 1; |
| 28 |
} |
| 29 |
|
| 30 |
# If command line contains at least one "-q" option, it is quiet mode |
| 31 |
my $quiet= scalar(@input) <= scalar(@ARGV) -1; |
| 32 |
# On Windows, command line arguments specified using wildcards need to be evaluated. |
| 33 |
# On Unix too if the arguments are passed with single quotes. |
| 34 |
my $need_parse = grep(/\*/,@input); |
| 35 |
if ($need_parse) |
| 36 |
{ |
| 37 |
my $platform_independent_dir; |
| 38 |
$platform_independent_dir= canonpath "@input"; |
| 39 |
@input= glob "$platform_independent_dir"; |
| 40 |
} |
| 41 |
|
| 42 |
foreach my $input_file (@input) |
| 43 |
{ |
| 44 |
handle_one_file($input_file); |
| 45 |
print "\n"; |
| 46 |
} |
| 47 |
|
| 48 |
if ( @ignored ) |
| 49 |
{ |
| 50 |
print STDERR "These files have been ignored:\n"; |
| 51 |
foreach my $ig ( @ignored ) |
| 52 |
{ |
| 53 |
print "$ig\n"; |
| 54 |
} |
| 55 |
print "\n"; |
| 56 |
} |
| 57 |
if ( $retcode ) |
| 58 |
{ |
| 59 |
print STDERR "There are errors\n"; |
| 60 |
} |
| 61 |
|
| 62 |
else |
| 63 |
{ |
| 64 |
print "\n"; |
| 65 |
print "ALL OK\n"; |
| 66 |
} |
| 67 |
|
| 68 |
exit $retcode; |
| 69 |
|
| 70 |
sub handle_one_file { |
| 71 |
|
| 72 |
my ( $input_file ) = @_; |
| 73 |
if ( $input_file =~ /^.*(ctype_.*|mysqldump)\.result/ ) |
| 74 |
{ |
| 75 |
push @ignored ,$input_file; |
| 76 |
return; |
| 77 |
} |
| 78 |
print "FILE $input_file\n"; |
| 79 |
print "\n"; |
| 80 |
open(DATA,"<$input_file") or die "Can't open file"; |
| 81 |
my @lines = <DATA>; |
| 82 |
close(DATA); |
| 83 |
my $first_trace_line = 0; |
| 84 |
my $trace_line = 0; |
| 85 |
my @trace = undef; |
| 86 |
label_to: foreach my $i ( @lines ) |
| 87 |
{ |
| 88 |
$trace_line = $trace_line + 1; |
| 89 |
if (( grep(/^.*(\t)?{\n/,$i) ) and ( $first_trace_line == 0 )) |
| 90 |
{ |
| 91 |
@trace = undef; |
| 92 |
$first_trace_line = $trace_line; |
| 93 |
push @trace, "{\n"; |
| 94 |
next label_to; |
| 95 |
} |
| 96 |
if (( $i =~ /^}/ ) and ( $first_trace_line != 0)) |
| 97 |
{ |
| 98 |
push @trace, "}"; |
| 99 |
check($first_trace_line,@trace); |
| 100 |
$first_trace_line = 0; |
| 101 |
} |
| 102 |
if ( $first_trace_line != 0 ) |
| 103 |
{ |
| 104 |
# Eliminate /* */ from end_marker=on (not valid JSON) |
| 105 |
$i =~ s/\/\*.*\*\// /g; |
| 106 |
push @trace, $i; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
sub check { |
| 114 |
|
| 115 |
my ( $first_trace_line, @trace ) = @_; |
| 116 |
my $string = join("", @trace); |
| 117 |
my $parsed; |
| 118 |
eval { $parsed = decode_json($string); }; |
| 119 |
unless ( $parsed ) |
| 120 |
{ |
| 121 |
print "Parse error at line: $first_trace_line\n"; |
| 122 |
my $error = $@; |
| 123 |
print "Error: $@\n"; |
| 124 |
# If there is a character position specified, put a mark ('&') in front of this character |
| 125 |
if ($error =~ /invalid character.*at character offset (\d+)/) |
| 126 |
{ |
| 127 |
substr($string,$1,0) = "&"; |
| 128 |
print "$string\n"; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
print "$string\n"; |
| 133 |
} |
| 134 |
$retcode = 1; |
| 135 |
print "\n"; |
| 136 |
return; |
| 137 |
} |
| 138 |
# Detect non-unique keys in one object, by counting |
| 139 |
# number of quote symbols ("): the json module outputs only |
| 140 |
# one of the non-unique keys, making the number of " |
| 141 |
# smaller compared to the input string. |
| 142 |
|
| 143 |
my $before = $string =~ tr/'"'//; |
| 144 |
my $re_json; |
| 145 |
$re_json= to_json($parsed); |
| 146 |
my $after = $re_json =~ tr/'"'//; |
| 147 |
if ( $before != $after ) |
| 148 |
{ |
| 149 |
print "Non-unique keys at line $first_trace_line ( $before vs $after )\n"; |
| 150 |
print "$string\n"; |
| 151 |
$retcode = 1; |
| 152 |
print "\n"; |
| 153 |
return; |
| 154 |
} |
| 155 |
if ( !$quiet ) |
| 156 |
{ |
| 157 |
print "OK at line $first_trace_line\n"; |
| 158 |
} |
| 159 |
} |