/
HoistSQL.pm
331 lines (280 loc) · 10.1 KB
/
HoistSQL.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# See bottom of file for copyright and license details
=begin TML
---+ package Foswiki::Contrib::DBIStoreContrib::HoistSQL
Static functions to extract SQL expressions from queries. The SQL can
be used to pre-filter topics cached in an SQL DB for more efficient
query matching.
=cut
package Foswiki::Contrib::DBIStoreContrib::HoistSQL;
use strict;
use Foswiki::Infix::Node ();
use Foswiki::Query::Node ();
# Try to optimise a query by hoisting SQL searches
# out of the query.
#
# patterns we need to look for:
#
# top level is defined by a sequence of AND and OR conjunctions
# second level, operator that can be mapped to SQL
# second level LHS is a field access
# second level RHS is a static string or number
# So, say I have:
# number=99 AND string='String' AND (moved.by='AlbertCamus'
# OR moved.by ~ '*bert*')
# This can be fully hoisted, to
# SELECT topic.name FROM topic,FIELD,MOVED
# WHERE
# EXISTS(
# SELECT tid FROM FIELD
# WHERE FIELD.tid=topic.tid AND FIELD.name='number' AND FIELD.value=99)
# AND
# EXISTS(
# SELECT tid FROM FIELD
# WHERE FIELD.tid=topic.tid AND FIELD.name='string'
# AND FIELD.string='String')
# AND (
# EXISTS(
# SELECT tid FROM FIELD
# WHERE MOVED.tid=topic.tid AND MOVED.by='AlbertCamus')
# OR
# EXISTS(
# SELECT tid FROM FIELD
# WHERE MOVED.tid=topic.tid AND MOVED.by LIKE '%bert%')
# )
use constant MONITOR => 0;
=begin TML
---++ ObjectMethod hoist($query) -> $sql_statement
Hoisting consists of assembly of a WHERE clause. There may be a
point where the expression can't be converted to SQL, because some operator
(for example, a date operator) can't be done in SQL. But in most cases
the hoisting allows us to extract a set of criteria that can be AND and
ORed together in an SQL statement sufficient to narrow down and isolate
that subset of topics that might match the query.
The result is a string SQL query, and the $query is modified to replace
the hoisted expressions with constants.
=cut
sub hoist {
my ($node, $indent) = @_;
return undef unless ref( $node->{op} );
$indent ||= '';
if ( $node->{op}->{name} eq '(' ) {
return hoist( $node->{params}[0], "$indent(" );
}
print STDERR "${indent}hoist ", $node->stringify(), "\n" if MONITOR;
if ( $node->{op}->{name} eq 'and' ) {
my $lhs = hoist( $node->{params}[0], "${indent}l" );
my $rhs = _hoistB( $node->{params}[1], "${indent}r" );
if ( $lhs && $rhs ) {
$node->makeConstant(Foswiki::Infix::Node::NUMBER, 1);
print STDERR "${indent}L&R\n" if MONITOR;
return "($lhs) AND ($rhs)";
}
elsif ( $lhs ) {
$node->{params}[0]->makeConstant(Foswiki::Infix::Node::NUMBER, 1);
print STDERR "${indent}L\n" if MONITOR;
return $lhs;
}
elsif ($rhs) {
$node->{params}[1]->makeConstant(Foswiki::Infix::Node::NUMBER, 1);
print STDERR "${indent}R\n" if MONITOR;
return $rhs;
}
}
else {
my $or = _hoistB($node, "${indent}|");
if ($or) {
$node->makeConstant(Foswiki::Infix::Node::NUMBER, 1);
return $or;
}
}
print STDERR "\tFAILED\n" if MONITOR;
return undef;
}
sub _hoistB {
my ($node, $indent) = @_;
return unless ref( $node->{op} );
if ( $node->{op}->{name} eq '(' ) {
return _hoistB( $node->{params}[0], "${indent}(" );
}
print STDERR "${indent}OR ", $node->stringify(), "\n" if MONITOR;
if ( $node->{op}->{name} eq 'or' ) {
my $lhs = _hoistB( $node->{params}[0], "${indent}l" );
my $rhs = _hoistC( $node->{params}[1], "${indent}r", 0 );
if ( $lhs && $rhs ) {
print STDERR "${indent}L&R\n" if MONITOR;
return "(($lhs) OR ($rhs))";
}
}
else {
return _hoistC($node, "${indent}|", 0);
}
return undef;
}
sub _hoistC {
my ($node, $indent, $negated) = @_;
return undef unless ref( $node->{op} );
my $op = $node->{op}->{name};
if ( $op eq '(' ) {
return _hoistC( $node->{params}[0], "${indent}(", $negated );
}
print STDERR "${indent}EQ ", $node->stringify(), "\n" if MONITOR;
my ($lhs, $rhs, $table, $test);
if ( $op eq 'not' ) {
return _hoistC( $node->{params}[0], "${indent}(", !$negated );
}
elsif ( $op eq '=' || $op eq '!=' ) {
($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}l" );
$rhs = _hoistConstant( $node->{params}[1] );
if ( !$lhs || !$rhs ) {
# = and != are symmetric, so try the other order
($lhs, $table) = _hoistValue( $node->{params}[1], "${indent}r" );
$rhs = _hoistConstant( $node->{params}[0] );
}
if ( $lhs && $rhs ) {
print STDERR "${indent}R=L\n" if MONITOR;
$test = "$lhs$op'$rhs'";
$test = "NOT($test)" if $negated;
}
}
elsif ( $op eq '~' ) {
($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}l" );
$rhs = _hoistConstant( $node->{params}[1] );
if ( $lhs && $rhs ) {
my $escape = '';
$rhs = quotemeta($rhs);
if ($rhs =~ /'/) {
$rhs =~ s/([s'])/s$1/g;
$escape = " ESCAPE 's'";
}
$rhs =~ s/\\\?/./g;
$rhs =~ s/\\\*/.*/g;
print STDERR "${indent}L~R\n" if MONITOR;
$test = "$lhs REGEXP '^(?s:$rhs)\$'$escape";
$test = "NOT($test)" if $negated;
}
}
elsif ( $op eq '=~' ) {
($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}l" );
$rhs = _hoistConstant( $node->{params}[1] );
if ( $lhs && $rhs ) {
my $escape = '';
if ($rhs =~ /'/) {
$rhs =~ s/([s'])/s$1/g;
$escape = " ESCAPE 's'";
}
print STDERR "${indent}L=~R\n" if MONITOR;
$test = "$lhs REGEXP '$rhs'$escape";
$test = "NOT($test)" if $negated;
}
}
if ($table && $test) {
if ($table ne 'topic') {
# Have to use an EXISTS if the sub-test refers to another table
return <<SQL;
EXISTS(SELECT * FROM $table WHERE $table.tid=topic.tid AND $test)
SQL
} else {
return $test;
}
}
return undef;
}
# Expecting a (root level) field access expression. This must be of the form
# <name>
# or
# <rootfield>.<name>
# <rootfield> may be aliased
# Returns a partial SQL statement that can be followed by a condition for
# testing the value.
# A limited set of functions - UPPER, LOWER,
sub _hoistValue {
my ($node, $indent) = @_;
my $op = ref( $node->{op}) ? $node->{op}->{name} : '';
print STDERR "${indent}V ", $node->stringify(), "\n" if MONITOR;
if ( $op eq '(' ) {
return _hoistValue( $node->{params}[0] );
}
if ( $op eq 'lc' ) {
my ($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ("LOWER($lhs)", $table) if $lhs;
} elsif ( $op eq 'uc' ) {
my ($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ("UPPER($lhs)", $table) if $lhs;
} elsif ( $op eq 'length' ) {
# This is slightly risky, because 'length' also works on array
# values, but SQL LEN only works on text values.
my ($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ("LENGTH($lhs)", $table) if $lhs;
} elsif ( $op eq '.' ) {
my $lhs = $node->{params}[0];
my $rhs = $node->{params}[1];
if ( !ref( $lhs->{op} )
&& !ref( $rhs->{op} )
&& $lhs->{op} eq Foswiki::Infix::Node::NAME
&& $rhs->{op} eq Foswiki::Infix::Node::NAME )
{
$lhs = $lhs->{params}[0];
$rhs = $rhs->{params}[0];
if ( $Foswiki::Query::Node::aliases{$lhs} ) {
$lhs = $Foswiki::Query::Node::aliases{$lhs};
}
if ( $lhs =~ /^META:(\w+)/ ) {
return ("$1.$rhs", $1);
}
if ( $rhs eq 'text' ) {
# Special case for the text body
return ('topic.text', 'topic');
}
if ( $rhs eq 'raw' ) {
# Special case for the text body
return ('topic.raw', 'topic');
}
# Otherwise assume the term before the dot is the form name
return ("EXISTS(SELECT * FROM FORM WHERE FORM.tid=topic.tid AND FORM.name='$lhs') AND FIELD.name='$rhs' AND FIELD.value",
"FIELD")
}
}
elsif ( !ref( $node->{op} )
&& $node->{op} eq Foswiki::Infix::Node::NAME ) {
# A simple name
if ( $node->{params}[0] =~ /^(name|web|text|raw)$/ ) {
# Special case for the topic name, web or text body
return ("topic.$1", 'topic');
}
else {
return ("FIELD.name='$node->{params}[0]' AND FIELD.value",
'FIELD');
}
}
print STDERR "\tFAILED\n" if MONITOR;
return (undef, undef);
}
# Expecting a constant
sub _hoistConstant {
my $node = shift;
if (
!ref( $node->{op} )
&& ( $node->{op} eq Foswiki::Infix::Node::STRING
|| $node->{op} eq Foswiki::Infix::Node::NUMBER )
)
{
return $node->{params}[0];
}
return;
}
1;
__DATA__
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://Foswiki.org/
Copyright (C) 2010 Foswiki Contributors. All Rights Reserved.
Foswiki Contributors are listed in the AUTHORS file in the root
of this distribution. NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.
Author: Crawford Currie http://c-dot.co.uk