Skip to content

Commit

Permalink
Item2247: query optimisation (constant folding) reduces size of query…
Browse files Browse the repository at this point in the history
… expressions.

git-svn-id: http://svn.foswiki.org/trunk@7131 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Apr 9, 2010
1 parent 0dd4431 commit 87fe8d0
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 203 deletions.
373 changes: 193 additions & 180 deletions UnitTestContrib/test/unit/QueryTests.pm

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions core/lib/Foswiki/Contrib/core/MANIFEST
Expand Up @@ -688,6 +688,7 @@ lib/Foswiki/Prefs/Web.pm 0444
lib/Foswiki/Query/BinaryOP.pm 0444
lib/Foswiki/Query/HoistREs.pm 0444
lib/Foswiki/Query/Node.pm 0444
lib/Foswiki/Query/OP.pm 0444
lib/Foswiki/Query/OP_and.pm 0444
lib/Foswiki/Query/OP_d2n.pm 0444
lib/Foswiki/Query/OP_dot.pm 0444
Expand Down
3 changes: 2 additions & 1 deletion core/lib/Foswiki/Func.pm
Expand Up @@ -24,7 +24,7 @@ API version $Date$ (revision $Rev$)
*Since* _date_ indicates where functions or parameters have been added since
the baseline of the API (Foswiki 1.0.0). The _date_ indicates the
earliest date of a Foswiki release that will support that function or
parameter.
parameter. See Foswiki:Download.ReleaseDates for version release dates.
*Deprecated* _date_ indicates where a function or parameters has been
[[http://en.wikipedia.org/wiki/Deprecation][deprecated]]. Deprecated
Expand Down Expand Up @@ -3279,6 +3279,7 @@ Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://F
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Expand Down
28 changes: 28 additions & 0 deletions core/lib/Foswiki/Query/BinaryOP.pm
Expand Up @@ -57,4 +57,32 @@ sub evalTest {
}
}

sub evaluatesToConstant {
my $this = shift;
my $node = shift;
return 0 unless $node->{params}[0]->evaluatesToConstant(@_);
return $node->{params}[1]->evaluatesToConstant(@_);
}

1;
__END__
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://Foswiki.org/
Copyright (C) 2009 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
77 changes: 66 additions & 11 deletions core/lib/Foswiki/Query/Node.pm
Expand Up @@ -127,22 +127,77 @@ sub evaluate {
return $result;
}

=begin TML
---++ evaluatesToConstant(%opts)
Determine if this node evaluates to a constant or not. "Constant" is defined
as "anything that doesn't involve actually looking in searched topics".
This function takes the same parameters (%domain) as evaluate(). Note that
no reference to the tom or data web or topic will be made, so you can
simply pass an arbitrary Foswiki::Meta.
=cut

sub evaluatesToConstant {
my $this = shift;
if (!ref($this->{op})
&& ($this->{op} == $Foswiki::Infix::Node::NUMBER
|| $this->{op} == $Foswiki::Infix::Node::STRING)) {
return 1;
}
elsif (ref($this->{op})) {
return $this->{op}->evaluatesToConstant($this, @_);
}
return 0;
}

=begin TML
---++ simplify(%opts)
Simplify the query by spotting constant expressions and evaluating them,
replacing the constant expression with an atomic value in the expression tree.
This function takes the same parameters (%domain) as evaluate(). Note that
no reference to the tom or data web or topic will be made, so you can
simply pass an arbitrary Foswiki::Meta.
=cut

sub simplify {
my $this = shift;

if ($this->evaluatesToConstant(@_)) {
my $c = $this->evaluate(@_) || 0;
if ($c =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/) {
$this->{op} = $Foswiki::Infix::Node::NUMBER;
} else {
$this->{op} = $Foswiki::Infix::Node::STRING;
}
@{$this->{params}} = ( $c );
} else {
for my $f (@{$this->{params}}) {
if (UNIVERSAL::can($f, 'simplify')) {
$f->simplify(@_);
}
}
}
}

1;
__DATA__
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://Foswiki.org/
# Copyright (C) 2008-2009 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.
#
# Additional copyrights apply to some or all of the code in this
# file as follows:
#
# Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
# TWiki Contributors are listed in the AUTHORS file in the root
# of this distribution. NOTE: Please extend that file, not this notice.
#
Copyright (C) 2008-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.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
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
Expand Down
6 changes: 6 additions & 0 deletions core/lib/Foswiki/Query/OP.pm
Expand Up @@ -8,6 +8,12 @@ sub new {
return bless(\%opts, $class);
}

# Does this operator evaluate to a constant?
# See Foswiki::Query::Node::evaluatesToConstant
sub evaluatesToConstant {
return 0;
}

1;
__END__
Expand Down
11 changes: 11 additions & 0 deletions core/lib/Foswiki/Query/OP_and.pm
Expand Up @@ -27,6 +27,17 @@ sub evaluate {
return $b->evaluate(@_) ? 1 : 0;
}

sub evaluatesToConstant {
my $this = shift;
my $node = shift;
my $ac = $node->{params}[0]->evaluatesToConstant(@_);
my $bc = $node->{params}[1]->evaluatesToConstant(@_);
return 1 if ($ac && $bc);
return 1 if $ac && ! $node->{params}[0]->evaluate(@_);
return 1 if $bc && ! $node->{params}[1]->evaluate(@_);
return 0;
}

1;

__DATA__
Expand Down
11 changes: 11 additions & 0 deletions core/lib/Foswiki/Query/OP_or.pm
Expand Up @@ -27,6 +27,17 @@ sub evaluate {
return $b->evaluate(@_) ? 1 : 0;
}

sub evaluatesToConstant {
my $this = shift;
my $node = shift;
my $ac = $node->{params}[0]->evaluatesToConstant(@_);
my $bc = $node->{params}[1]->evaluatesToConstant(@_);
return 1 if ($ac && $bc);
return 1 if $ac && $node->{params}[0]->evaluate(@_);
return 1 if $bc && $node->{params}[1]->evaluate(@_);
return 0;
}

1;

__DATA__
Expand Down
29 changes: 18 additions & 11 deletions core/lib/Foswiki/Query/OP_ref.pm
Expand Up @@ -65,23 +65,30 @@ sub evaluate {
return \@result;
}

sub evaluatesToConstant {
my $this = shift;
my $node = shift;
return 1 if $node->{params}[0]->evaluatesToConstant(@_);
# param[1] may contain non-constant terms, but that's OK because
# they are evaluated relative to the (constant) param[0]
return 0;
}

1;

__DATA__
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://Foswiki.org/
# Copyright (C) 2008-2009 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.
#
# Additional copyrights apply to some or all of the code in this
# file as follows:
#
# Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
# TWiki Contributors are listed in the AUTHORS file in the root
# of this distribution. NOTE: Please extend that file, not this notice.
#
Copyright (C) 2008-2009 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.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
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
Expand Down
6 changes: 6 additions & 0 deletions core/lib/Foswiki/Query/UnaryOP.pm
Expand Up @@ -26,6 +26,12 @@ sub evalUnary {
}
}

sub evaluatesToConstant {
my $this = shift;
my $node = shift;
return $node->{params}[0]->evaluatesToConstant(@_);
}

1;
__END__
Expand Down
4 changes: 4 additions & 0 deletions core/lib/Foswiki/Store/QueryAlgorithms/BruteForce.pm
Expand Up @@ -37,6 +37,10 @@ sub query {
# return new Foswiki::Search::InfoCache($session, '');
#}

# Eliminate static expressions
my $context = Foswiki::Meta->new( $session, $session->{webName} );
$query->simplify(tom => $context, data => $context );

my $webNames = $options->{web} || '';
my $recurse = $options->{'recurse'} || '';
my $isAdmin = $session->{users}->isAdmin( $session->{user} );
Expand Down

0 comments on commit 87fe8d0

Please sign in to comment.