apache / apache/lucene

QueryNode#cloneTree produces a new tree where parents are not correctly set [LUCENE-7266]

Open
#8,321 0 comments 0 reactions 0 assignees View on GitHub
affects-version:5.4.1 legacy-jira-priority:Major module:queryparser type:bug
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

The following unit test performs a sanity check on the QueryNode tree, checking that each node has the parent set to the same node it was retrieved from. After calling cloneTree, this check fails on the returned node, as the parents in the cloned node still point back into the original tree.

```Java
import java.util.Arrays;
import java.util.List;

import org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode;
import org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode;
import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode;
import org.junit.Test;

public class TestCloneTree {
`@Test`
public void testCloneTree() throws Exception {
QueryNode original = new BooleanQueryNode(Arrays.asList(
new FieldQueryNode(null, "a", 0, 0),
new FieldQueryNode(null, "b", 0, 0)));

sanityCheckQueryTree(original);

QueryNode cloned = original.cloneTree();

sanityCheckQueryTree(cloned);
}

private void sanityCheckQueryTree(QueryNode node) {
List children = node.getChildren();
if (children != null) {
for (QueryNode child : children) {
// Matching what Lucene is using in QueryNodeImpl itself.
//noinspection ObjectEquality
if (child.getParent() != node) {
throw new IllegalStateException("Sanity check failed for child: " + child + '\n' +
" Parent is: " + child.getParent() + '\n' +
" But we got to it via: " + node);
}

sanityCheckQueryTree(child);
}
}
}
}
```

---
Migrated from [LUCENE-7266](https://issues.apache.org/jira/browse/LUCENE-7266) by Trejkaz

Contributor guide

Open the contributing guide

Research direction

Start by reading QueryNode#cloneTree and the parent handling in QueryNodeImpl, then reproduce the sanity check shown in the issue with BooleanQueryNode and FieldQueryNode. Done means every child returned from the cloned tree has its parent set to the corresponding cloned node rather than to a node in the original tree.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.