From e06996a2e4d8497f75e8e59c8cf1113bc6d9763e Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 3 Feb 2021 15:58:54 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B03660=20-=20Fix=20JOIN=20without=20condit?= =?UTF-8?q?ion=20on=20child=20joined=20table=20is=20ignored=20(check=20don?= =?UTF-8?q?e=20in=20optimizer)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/oqlclassnode.class.inc.php | 8 ++++++++ core/oqlclasstreebuilder.class.inc.php | 7 ------- core/oqlclasstreeoptimizer.class.inc.php | 11 +++++++++-- test/core/OQLTest.php | 4 ++-- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/core/oqlclassnode.class.inc.php b/core/oqlclassnode.class.inc.php index da157f588..3c078ab14 100644 --- a/core/oqlclassnode.class.inc.php +++ b/core/oqlclassnode.class.inc.php @@ -331,4 +331,12 @@ class OQLJoin return $this->sRightField; } + /** + * @return string + */ + public function GetLeftField() + { + return $this->sLeftField; + } + } diff --git a/core/oqlclasstreebuilder.class.inc.php b/core/oqlclasstreebuilder.class.inc.php index 2e25fb1dd..a397ab3db 100644 --- a/core/oqlclasstreebuilder.class.inc.php +++ b/core/oqlclasstreebuilder.class.inc.php @@ -294,13 +294,6 @@ class OQLClassTreeBuilder $oClassExpr = new FieldExpression($sClassAttCode, $this->sClassAlias); $oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr); $oQBContextExpressions->AddCondition($oClassRestriction); - } elseif (($oKeyAttDef instanceof AttributeExternalKey) && $sKeyClass != $oKeyAttDef->GetTargetClass()) { - $sClassAttCode = 'finalclass'; - $oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses($sKeyClass, - ENUM_CHILD_CLASSES_ALL)); - $oClassExpr = new FieldExpression($sClassAttCode, $sKeyClassAlias); - $oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr); - $oQBContextExpressions->AddCondition($oClassRestriction); } // Translate prior to recursing diff --git a/core/oqlclasstreeoptimizer.class.inc.php b/core/oqlclasstreeoptimizer.class.inc.php index 0472863e7..1e12384e9 100644 --- a/core/oqlclasstreeoptimizer.class.inc.php +++ b/core/oqlclasstreeoptimizer.class.inc.php @@ -50,8 +50,15 @@ class OQLClassTreeOptimizer { if ($oJoin->IsOutbound()) { - // The join is not used, remove from tree - $oCurrentClassNode->RemoveJoin($sLeftKey, $index); + // If joined class in not the same class than the external key target class + // then the join cannot be removed because it is used to filter the request + $sJoinedClass = $oJoin->GetOOQLClassNode()->GetNodeClass(); + $sExtKeyAttCode = $oJoin->GetLeftField(); + $oExtKeyAttDef = MetaModel::GetAttributeDef($oCurrentClassNode->GetNodeClass(), $sExtKeyAttCode); + if ($sJoinedClass == $oExtKeyAttDef->GetTargetClass()) { + // The join is not used, remove from tree + $oCurrentClassNode->RemoveJoin($sLeftKey, $index); + } } else { diff --git a/test/core/OQLTest.php b/test/core/OQLTest.php index d419f46b3..54b0293d7 100644 --- a/test/core/OQLTest.php +++ b/test/core/OQLTest.php @@ -441,8 +441,8 @@ class OQLTest extends ItopDataTestCase "SELECT `U` FROM `UserRequest` AS `U` INNER JOIN `lnkContactToTicket` AS `l` ON `U`.`id` = `l`.`ticket_id` - INNER JOIN `Contact` AS `T_Contact` - ON `l`.`contact_id` = `T_Contact`.`id`", + INNER JOIN `Team` AS `T` + ON `l`.`contact_id` = `T`.`id`", ], 'Bug 3660 2' => [ "SELECT UserRequest AS U