Repository: modin-project/modin

Issue:
BUG: HDK: Query execution fails because the query contains not supported self-join pattern
### Modin version checks

- [X] I have checked that this issue has not already been reported.

- [X] I have confirmed this bug exists on the latest released version of Modin.

- [X] I have confirmed this bug exists on the main branch of Modin. (In order to do this you can follow [this guide](https://modin.readthedocs.io/en/stable/getting_started/installation.html#installing-from-the-github-master-branch).)


### Reproducible Example

```python
import modin.pandas as pd
df = pd.DataFrame({"a": [1]})
print(pd.merge(df, df))
```


### Issue Description

This code fails with the following error:

Query execution fails because the query contains not supported self-join pattern. We suspect the query requires multiple left-deep join tree due to the join condition of the self-join and is not supported for now. Please consider rewriting table order in FROM clause.

For more details see https://github.com/intel-ai/hdk/issues/255.

### Expected Behavior

```
   a
0  1
```

### Error Logs

<details>

```python-traceback

Replace this line with the error backtrace (if applicable).

```

</details>


### Installed Versions

<details>

Replace this line with the output of pd.show_versions()

</details>



Discussion:

Resolving patch:
diff --git a/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py b/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py
--- a/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py
+++ b/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py
@@ -867,6 +867,20 @@ def join(
             right_on
         ), "'left_on' and 'right_on' lengths don't match"
 
+        if other is self:
+            # To avoid the self-join failure - #5891
+            if isinstance(self._op, FrameNode):
+                other = self.copy()
+            else:
+                exprs = OrderedDict((c, self.ref(c)) for c in self._table_cols)
+                other = self.__constructor__(
+                    columns=self.columns,
+                    dtypes=self._dtypes_for_exprs(exprs),
+                    op=TransformNode(self, exprs),
+                    index_cols=self._index_cols,
+                    force_execution_mode=self._force_execution_mode,
+                )
+
         orig_left_on = left_on
         orig_right_on = right_on
         left, left_on = check_cols_to_join("left_on", self, left_on)


Test patch:
diff --git a/modin/experimental/core/execution/native/implementations/hdk_on_native/test/test_dataframe.py b/modin/experimental/core/execution/native/implementations/hdk_on_native/test/test_dataframe.py
--- a/modin/experimental/core/execution/native/implementations/hdk_on_native/test/test_dataframe.py
+++ b/modin/experimental/core/execution/native/implementations/hdk_on_native/test/test_dataframe.py
@@ -1512,6 +1512,19 @@ def merge(df1, df2, how, left_on, right_on, **kwargs):
             right_on=left_on,
         )
 
+    def test_self_merge(self):
+        def merge(df, lib, iterations, **kwargs):
+            for _ in range(iterations):
+                df = lib.merge(df, df)
+            return df
+
+        for i in range(1, 3):
+            run_and_compare(
+                merge,
+                data={"a": [1]},
+                iterations=i,
+            )
+
 
 class TestBinaryOp:
     data = {