[Rt-commit] rt branch, 4.2/distinct-joins, created. rt-4.1.8-230-g2fb377c

Ruslan Zakirov ruz at bestpractical.com
Wed May 8 07:50:15 EDT 2013


The branch, 4.2/distinct-joins has been created
        at  2fb377c59c0e0a7918672f430f3caf58ba0f964e (commit)

- Log -----------------------------------------------------------------
commit 1a6d5882dcb823355882e07b65e86a497b8d36a3
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Wed May 8 13:58:48 2013 +0400

    replace NewAlias with Join calls
    
    Join can get us NewAlias and it makes next code
    easier to implment and cleaner.

diff --git a/lib/RT/Attachments.pm b/lib/RT/Attachments.pm
index 219dc78..4e2f195 100644
--- a/lib/RT/Attachments.pm
+++ b/lib/RT/Attachments.pm
@@ -111,14 +111,12 @@ sub TransactionAlias {
     return $self->{'_sql_transaction_alias'}
         if $self->{'_sql_transaction_alias'};
 
-    my $res = $self->NewAlias('Transactions');
-    $self->Limit(
-        ENTRYAGGREGATOR => 'AND',
-        FIELD           => 'TransactionId',
-        VALUE           => $res . '.id',
-        QUOTEVALUE      => 0,
+    return $self->{'_sql_transaction_alias'} = $self->Join(
+        ALIAS1 => 'main',
+        FIELD1 => 'TransactionId',
+        TABLE2 => 'Transactions',
+        FIELD2 => 'id',
     );
-    return $self->{'_sql_transaction_alias'} = $res;
 }
 
 =head2 ContentType (VALUE => 'text/plain', ENTRYAGGREGATOR => 'OR', OPERATOR => '=' ) 
@@ -202,13 +200,11 @@ sub LimitByTicket {
         VALUE           => 'RT::Ticket',
     );
 
-    my $tickets = $self->NewAlias('Tickets');
-    $self->Limit(
-        ENTRYAGGREGATOR => 'AND',
-        ALIAS           => $tickets,
-        FIELD           => 'id',
-        VALUE           => $transactions . '.ObjectId',
-        QUOTEVALUE      => 0,
+    my $tickets = $self->Join(
+        ALIAS1 => $transactions,
+        FIELD1 => 'ObjectId',
+        TABLE2 => 'Tickets',
+        FIELD2 => 'id',
     );
     $self->Limit(
         ENTRYAGGREGATOR => 'AND',
diff --git a/lib/RT/CachedGroupMembers.pm b/lib/RT/CachedGroupMembers.pm
index efa1273..3f8c39a 100644
--- a/lib/RT/CachedGroupMembers.pm
+++ b/lib/RT/CachedGroupMembers.pm
@@ -88,9 +88,10 @@ groups from users for display purposes
 sub LimitToUsers {
     my $self = shift;
 
-    my $principals = $self->NewAlias('Principals');
-    $self->Join( ALIAS1 => 'main', FIELD1 => 'MemberId',
-                 ALIAS2 => $principals, FIELD2 =>'id');
+    my $principals = $self->Join(
+        ALIAS1 => 'main', FIELD1 => 'MemberId',
+        TABLE2 => 'Principals', FIELD2 =>'id'
+    );
 
     $self->Limit(       ALIAS => $principals,
                          FIELD => 'PrincipalType',
@@ -113,9 +114,11 @@ groups from users for display purposes
 sub LimitToGroups {
     my $self = shift;
 
-    my $principals = $self->NewAlias('Principals');
-    $self->Join( ALIAS1 => 'main', FIELD1 => 'MemberId',
-                 ALIAS2 => $principals, FIELD2 =>'id');
+    my $principals = $self->Join(
+        ALIAS1 => 'main', FIELD1 => 'MemberId',
+        TABLE2 => 'Principals', FIELD2 =>'id'
+    );
+
 
     $self->Limit(       ALIAS => $principals,
                          FIELD => 'PrincipalType',
diff --git a/lib/RT/GroupMembers.pm b/lib/RT/GroupMembers.pm
index 0c0cf25..5a7aed2 100644
--- a/lib/RT/GroupMembers.pm
+++ b/lib/RT/GroupMembers.pm
@@ -88,9 +88,10 @@ groups from users for display purposes
 sub LimitToUsers {
     my $self = shift;
 
-    my $principals = $self->NewAlias('Principals');
-    $self->Join( ALIAS1 => 'main', FIELD1 => 'MemberId',
-                 ALIAS2 => $principals, FIELD2 =>'id');
+    my $principals = $self->Join(
+        ALIAS1 => 'main', FIELD1 => 'MemberId',
+        TABLE2 => 'Principals', FIELD2 =>'id'
+    );
 
     $self->Limit(       ALIAS => $principals,
                          FIELD => 'PrincipalType',
@@ -113,9 +114,10 @@ groups from users for display purposes
 sub LimitToGroups {
     my $self = shift;
 
-    my $principals = $self->NewAlias('Principals');
-    $self->Join( ALIAS1 => 'main', FIELD1 => 'MemberId',
-                 ALIAS2 => $principals, FIELD2 =>'id');
+    my $principals = $self->Join(
+        ALIAS1 => 'main', FIELD1 => 'MemberId',
+        TABLE2 => 'Principals', FIELD2 =>'id'
+    );
 
     $self->Limit(       ALIAS => $principals,
                          FIELD => 'PrincipalType',
diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index d88fe12..1d04df1 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -3524,12 +3524,11 @@ sub GetPrincipalsMap {
             );
 
             # Limit to UserEquiv groups
-            my $groups = $Users->NewAlias('Groups');
-            $Users->Join(
-                ALIAS1 => $groups,
-                FIELD1 => 'id',
-                ALIAS2 => $group_members,
-                FIELD2 => 'GroupId'
+            my $groups = $Users->Join(
+                ALIAS1 => $group_members,
+                FIELD1 => 'GroupId',
+                TABLE2 => 'Groups',
+                FIELD2 => 'id',
             );
             $Users->Limit( ALIAS => $groups, FIELD => 'Domain', VALUE => 'ACLEquivalence' );
             $Users->Limit( ALIAS => $groups, FIELD => 'Type', VALUE => 'UserEquiv' );
diff --git a/lib/RT/Transactions.pm b/lib/RT/Transactions.pm
index 54dd41e..f0e75ca 100644
--- a/lib/RT/Transactions.pm
+++ b/lib/RT/Transactions.pm
@@ -108,11 +108,10 @@ sub LimitToTicket {
     my $tid  = shift;
 
     unless ( $self->{'tickets_table'} ) {
-        $self->{'tickets_table'} ||= $self->NewAlias('Tickets');
-        $self->Join(
+        $self->{'tickets_table'} ||= $self->Join(
             ALIAS1 => 'main',
             FIELD1 => 'ObjectId',
-            ALIAS2 => $self->{'tickets_table'},
+            TABLE2 => 'Tickets',
             FIELD2 => 'id'
         );
         $self->Limit(

commit c69ec5d7d7f721c797d7d8ef4e131f93cbfc8737
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Wed May 8 14:01:21 2013 +0400

    put explicit DISTINCT argument in a few places

diff --git a/lib/RT/Groups.pm b/lib/RT/Groups.pm
index 8d30dde..434430f 100644
--- a/lib/RT/Groups.pm
+++ b/lib/RT/Groups.pm
@@ -255,15 +255,15 @@ sub WithMember {
     my %args = ( PrincipalId => undef,
                  Recursively => undef,
                  @_);
-    my $members;
-
-    if ($args{'Recursively'}) {
-        $members = $self->NewAlias('CachedGroupMembers');
-    } else {
-        $members = $self->NewAlias('GroupMembers');
-    }
-    $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
-                ALIAS2 => $members, FIELD2 => 'GroupId');
+    my $members = $self->Join(
+        ALIAS1 => 'main', FIELD1 => 'id',
+        $args{'Recursively'}
+            ? (TABLE2 => 'CachedGroupMembers')
+            # (GroupId, MemberId) is unique in GM table
+            : (TABLE2 => 'GroupMembers', DISTINCT => 1)
+        ,
+        FIELD2 => 'GroupId',
+    );
 
     $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
     $self->Limit(ALIAS => $members, FIELD => 'Disabled', VALUE => 0)
@@ -286,6 +286,7 @@ sub WithoutMember {
         FIELD1 => 'id',
         TABLE2 => $members,
         FIELD2 => 'GroupId',
+        DISTINCT => $members eq 'GroupMembers',
     );
     $self->Limit(
         LEFTJOIN => $members_alias,
diff --git a/lib/RT/SearchBuilder/Role/Roles.pm b/lib/RT/SearchBuilder/Role/Roles.pm
index 7167b13..b571599 100644
--- a/lib/RT/SearchBuilder/Role/Roles.pm
+++ b/lib/RT/SearchBuilder/Role/Roles.pm
@@ -119,6 +119,7 @@ sub _RoleGroupsJoin {
         TABLE2          => 'Groups',
         FIELD2          => 'Instance',
         ENTRYAGGREGATOR => 'AND',
+        DISTINCT        => !!$args{'Type'},
     );
     $self->Limit(
         LEFTJOIN        => $groups,

commit 9a7849af2e29b76282c4208bbb646cf7a3bc84c4
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Wed May 8 14:02:05 2013 +0400

    we can auto mark Join as distinct in some cases
    
    If we join to a table and in target table we join to id
    then join is distinct.

diff --git a/lib/RT/SearchBuilder.pm b/lib/RT/SearchBuilder.pm
index cd1c089..73b5446 100644
--- a/lib/RT/SearchBuilder.pm
+++ b/lib/RT/SearchBuilder.pm
@@ -91,6 +91,17 @@ sub CleanSlate {
     return $self->SUPER::CleanSlate(@_);
 }
 
+sub Join {
+    my $self = shift;
+    my %args = @_;
+
+    $args{'DISTINCT'} = 1 if
+        !exists $args{'DISTINCT'}
+        && $args{'TABLE2'} && lc($args{'FIELD2'}||'') eq 'id';
+
+    return $self->SUPER::Join( %args );
+}
+
 sub JoinTransactions {
     my $self = shift;
     my %args = ( New => 0, @_ );

commit 2fb377c59c0e0a7918672f430f3caf58ba0f964e
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Wed May 8 14:06:57 2013 +0400

    we don't need this code
    
    this is covered by auto marking joins as distinct

diff --git a/sbin/rt-fulltext-indexer.in b/sbin/rt-fulltext-indexer.in
index 624ee8f..f2a5264 100644
--- a/sbin/rt-fulltext-indexer.in
+++ b/sbin/rt-fulltext-indexer.in
@@ -210,11 +210,6 @@ sub attachments {
         VALUE => 'deleted'
     );
 
-    # On newer DBIx::SearchBuilder's, indicate that making the query DISTINCT
-    # is unnecessary because the joins won't produce duplicates.  This
-    # drastically improves performance when fetching attachments.
-    $res->{joins_are_distinct} = 1;
-
     return goto_specific(
         suffix => $type,
         error => "Don't know how to find $type attachments",

-----------------------------------------------------------------------


More information about the Rt-commit mailing list