Use a COALESCE/NULLIF nested structure instead of CASE (one branch, easy peasy) for...
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 11 Aug 2010 20:18:26 +0000 (20:18 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 11 Aug 2010 20:18:26 +0000 (20:18 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@17174 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm

index cbff570..4717380 100644 (file)
@@ -570,17 +570,13 @@ sub rel_bump {
     return '' if (!@$only_atoms);
 
     if ($bump eq 'first_word') {
-        return "/* first_word */ CASE WHEN naco_normalize(".$node->table_alias.".value) ".
-                    "LIKE naco_normalize(".$self->QueryParser->quote_value($only_atoms->[0]->content).") \|\| '\%' ".
-                    "THEN $multiplier ELSE 1 END";
+        return " /* first_word */ COALESCE(NULLIF( (naco_normalize(".$node->table_alias.".value) ~ ('^'||naco_normalize(".$self->QueryParser->quote_value($only_atoms->[0]->content).")))::BOOL::INT, 0 ) * $multiplier, 1)";
     } elsif ($bump eq 'full_match') {
-        return "/* full_match */ CASE WHEN naco_normalize(".$node->table_alias.".value) ".
-                    "LIKE". join( '||\'%\'||', map { " naco_normalize(".$self->QueryParser->quote_value($_->content).") " } @$only_atoms ) .
-                    "THEN $multiplier ELSE 1 END";
+        return " /* full_match */ COALESCE(NULLIF( (naco_normalize(".$node->table_alias.".value) ~ ('^'||".
+                    join( "||' '||", map { "naco_normalize(".$self->QueryParser->quote_value($_->content).")" } @$only_atoms )."||'\$'))::BOOL::INT, 0 ) * $multiplier, 1)";
     } elsif ($bump eq 'word_order') {
-        return "/* word_order */ CASE WHEN naco_normalize(".$node->table_alias.".value) ".
-                    "LIKE '\%'||". join( '||\'%\'||', map { " naco_normalize(".$self->QueryParser->quote_value($_->content).") " } @$only_atoms ) . '||\'%\' '.
-                    "THEN $multiplier ELSE 1 END";
+        return " /* word_order */ COALESCE(NULLIF( (naco_normalize(".$node->table_alias.".value) ~ (".
+                    join( "||'.*'||", map { "naco_normalize(".$self->QueryParser->quote_value($_->content).")" } @$only_atoms )."))::BOOL::INT, 0 ) * $multiplier, 1)";
     }
 
     return '';
@@ -634,6 +630,8 @@ sub flatten {
                         next if ($used_bumps{$b});
                         $used_bumps{$b} = 1;
 
+                        next if ($$bumps{$b}{multiplier} == 1); # optimization to remove unneeded bumps
+
                         my $bump_case = $self->rel_bump( $node, $b, $$bumps{$b}{multiplier} );
                         $node_rank .= "\n\t\t\t\t * " . $bump_case if ($bump_case);
                     }