Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Fix Subtree tagging CTE implementation #5026

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,84 +136,42 @@ private function whenSubtreeWasUntagged(SubtreeWasUntagged $event): void

private function moveSubtreeTags(ContentStreamId $contentStreamId, NodeAggregateId $nodeAggregateId, NodeAggregateId $newParentNodeAggregateId, DimensionSpacePoint $coveredDimensionSpacePoint): void
{
$nodeTags = $this->nodeTagsForNode($nodeAggregateId, $contentStreamId, $coveredDimensionSpacePoint);
$newParentSubtreeTags = $this->nodeTagsForNode($newParentNodeAggregateId, $contentStreamId, $coveredDimensionSpacePoint);
$newSubtreeTags = [];
$newDescendantSubtreeTags = [];
foreach ($nodeTags->withoutInherited() as $tag) {
$newSubtreeTags[$tag->value] = true;
$newDescendantSubtreeTags[$tag->value] = null;
}
foreach ($newParentSubtreeTags as $tag) {
$newSubtreeTags[$tag->value] ??= null;
$newDescendantSubtreeTags[$tag->value] = null;
}
if ($newSubtreeTags === [] && $nodeTags->isEmpty()) {
return;
}
$this->getDatabaseConnection()->executeStatement('
UPDATE ' . $this->getTableNamePrefix() . '_hierarchyrelation h
SET h.subtreetags = JSON_MERGE_PATCH(:newParentTags, JSON_MERGE_PATCH(\'{}\', h.subtreetags))
WHERE h.contentstreamid = :contentStreamId
AND h.dimensionspacepointhash = :dimensionSpacePointHash
AND h.childnodeanchor IN (
WITH RECURSIVE cte (id) AS (
SELECT ch.childnodeanchor
FROM ' . $this->getTableNamePrefix() . '_hierarchyrelation ch
INNER JOIN ' . $this->getTableNamePrefix() . '_node n ON n.relationanchorpoint = ch.parentnodeanchor
WHERE
n.nodeaggregateid = :nodeAggregateId
AND ch.contentstreamid = :contentStreamId
AND ch.dimensionspacepointhash = :dimensionSpacePointHash
UNION ALL
UPDATE ' . $this->getTableNamePrefix() . '_hierarchyrelation h,
(
WITH RECURSIVE cte AS (
SELECT
dh.childnodeanchor
JSON_KEYS(th.subtreetags) subtreeTagsToInherit, th.childnodeanchor
FROM
' . $this->getTableNamePrefix() . '_hierarchyrelation th
INNER JOIN ' . $this->getTableNamePrefix() . '_node tn ON tn.relationanchorpoint = th.childnodeanchor
WHERE
tn.nodeaggregateid = :nodeAggregateId
AND th.contentstreamid = :contentStreamId
AND th.dimensionspacepointhash = :dimensionSpacePointHash
UNION
SELECT JSON_MERGE(cte.subtreetagsToInherit, JSON_KEYS(JSON_MERGE_PATCH(\'{}\', dh.subtreetags))) subtreeTagsToInherit, dh.childnodeanchor
FROM
cte
JOIN ' . $this->getTableNamePrefix() . '_hierarchyrelation dh ON dh.parentnodeanchor = cte.id
JOIN ' . $this->getTableNamePrefix() . '_hierarchyrelation dh ON dh.parentnodeanchor = cte.childnodeanchor
)
SELECT id FROM cte
SELECT * FROM cte
) AS r
SET h.subtreetags = (
SELECT
JSON_MERGE_PATCH(JSON_OBJECTAGG(htk.k, null), JSON_MERGE_PATCH(\'{}\', h.subtreetags))
FROM
JSON_TABLE(r.subtreeTagsToInherit, \'$[*]\' COLUMNS (k VARCHAR(36) PATH \'$\')) htk
)
', [
'contentStreamId' => $contentStreamId->value,
'nodeAggregateId' => $nodeAggregateId->value,
'dimensionSpacePointHash' => $coveredDimensionSpacePoint->hash,
'newParentTags' => json_encode($newDescendantSubtreeTags, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT),
]);
$this->getDatabaseConnection()->executeStatement('
UPDATE ' . $this->getTableNamePrefix() . '_hierarchyrelation h
INNER JOIN ' . $this->getTableNamePrefix() . '_node n ON n.relationanchorpoint = h.childnodeanchor
SET h.subtreetags = :newParentTags
WHERE
n.nodeaggregateid = :nodeAggregateId
h.childnodeanchor = r.childnodeanchor
AND h.contentstreamid = :contentStreamId
AND h.dimensionspacepointhash = :dimensionSpacePointHash
', [
'contentStreamId' => $contentStreamId->value,
'nodeAggregateId' => $nodeAggregateId->value,
'dimensionSpacePointHash' => $coveredDimensionSpacePoint->hash,
'newParentTags' => json_encode($newSubtreeTags, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT),
]);
}

private function nodeTagsForNode(NodeAggregateId $nodeAggregateId, ContentStreamId $contentStreamId, DimensionSpacePoint $dimensionSpacePoint): NodeTags
{
$subtreeTagsJson = $this->getDatabaseConnection()->fetchOne('
SELECT h.subtreetags FROM ' . $this->getTableNamePrefix() . '_hierarchyrelation h
INNER JOIN ' . $this->getTableNamePrefix() . '_node n ON n.relationanchorpoint = h.childnodeanchor
WHERE
n.nodeaggregateid = :nodeAggregateId
AND h.contentstreamid = :contentStreamId
AND h.dimensionspacepointhash = :dimensionSpacePointHash
', [
'nodeAggregateId' => $nodeAggregateId->value,
'contentStreamId' => $contentStreamId->value,
'dimensionSpacePointHash' => $dimensionSpacePoint->hash,
'nodeAggregateId' => $newParentNodeAggregateId->value,
'dimensionSpacePointHash' => $coveredDimensionSpacePoint->hash,
]);
if (!is_string($subtreeTagsJson)) {
throw new \RuntimeException(sprintf('Failed to fetch SubtreeTags for node "%s" in content subgraph "%s@%s"', $nodeAggregateId->value, $dimensionSpacePoint->toJson(), $contentStreamId->value), 1698838865);
}
return NodeFactory::extractNodeTagsFromJson($subtreeTagsJson);
}

private function subtreeTagsForHierarchyRelation(ContentStreamId $contentStreamId, NodeRelationAnchorPoint $parentNodeAnchorPoint, DimensionSpacePoint $dimensionSpacePoint): NodeTags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ Feature: Tag subtree without dimensions
"""
b (tag2*)
b1 (tag3*,tag2)
a1a (tag4*,tag3,tag2)
a1a1 (tag4*,tag1*,tag3,tag2)
a1a1a (tag4*,tag3,tag2)
a1a1b (tag4*,tag3,tag2)
a1a2 (tag4*,tag3,tag2)
a1a (tag4*,tag2,tag3)
a1a1 (tag1*,tag2,tag3,tag4)
a1a1a (tag1,tag2,tag3,tag4)
a1a1b (tag1,tag2,tag3,tag4)
a1a2 (tag2,tag3,tag4)
"""

When the command CreateNodeAggregateWithNode is executed with payload:
Expand All @@ -189,12 +189,12 @@ Feature: Tag subtree without dimensions
"""
b (tag2*)
b1 (tag3*,tag2)
a1a (tag4*,tag3,tag2)
a1a1 (tag4*,tag1*,tag3,tag2)
a1a1a (tag4*,tag3,tag2)
a1a1b (tag4*,tag3,tag2)
a1a2 (tag4*,tag3,tag2)
a1a3 (tag4,tag3,tag2)
a1a (tag4*,tag2,tag3)
a1a1 (tag1*,tag2,tag3,tag4)
a1a1a (tag1,tag2,tag3,tag4)
a1a1b (tag1,tag2,tag3,tag4)
a1a2 (tag2,tag3,tag4)
a1a3 (tag2,tag3,tag4)
"""

When the command UntagSubtree is executed with payload:
Expand All @@ -206,10 +206,10 @@ Feature: Tag subtree without dimensions
"""
b (tag2*)
b1 (tag3*,tag2)
a1a (tag3,tag2)
a1a1 (tag4*,tag1*,tag3,tag2)
a1a1a (tag4*,tag3,tag2)
a1a1b (tag4*,tag3,tag2)
a1a2 (tag4*,tag3,tag2)
a1a3 (tag3,tag2)
a1a (tag2,tag3)
a1a1 (tag1*,tag2,tag3)
a1a1a (tag1,tag2,tag3)
a1a1b (tag1,tag2,tag3)
a1a2 (tag2,tag3)
a1a3 (tag2,tag3)
"""
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ public function iExecuteTheFindSubtreeQueryIExpectTheFollowingTrees(string $entr
$subtree = array_shift($subtreeStack);
$tags = [];
if ($withTags !== null) {
$tags = [...array_map(static fn(string $tag) => $tag . '*', $subtree->node->tags->withoutInherited()->toStringArray()), ...$subtree->node->tags->onlyInherited()->toStringArray()];
$explicitTags = $subtree->node->tags->withoutInherited()->toStringArray();
sort($explicitTags);
$inheritedTags = $subtree->node->tags->onlyInherited()->toStringArray();
sort($inheritedTags);
$tags = [...array_map(static fn(string $tag) => $tag . '*', $explicitTags), ...$inheritedTags];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just sorts the tags to make the tests independent of the actual order

}
$result[] = str_repeat(' ', $subtree->level) . $subtree->node->nodeAggregateId->value . ($tags !== [] ? ' (' . implode(',', $tags) . ')' : '');
$subtreeStack = [...$subtree->children, ...$subtreeStack];
Expand Down
Loading