Repository: Azure/pykusto

Issue:
query.__add__ only handles short queries

Discussion:

Patch:
diff --git a/pykusto/query.py b/pykusto/query.py
index 42caba4..272bbaf 100644
--- a/pykusto/query.py
+++ b/pykusto/query.py
@@ -53,7 +53,12 @@ class Query:
         self._table = head if isinstance(head, Table) else None
 
     def __add__(self, other: 'Query'):
-        other._head = self
+        other_base = other
+        while other_base._head is not None:
+            if other_base._head._head is None:
+                break
+            other_base = other_base._head
+        other_base._head = self
         return other
 
     def where(self, predicate: BooleanType) -> 'WhereQuery':


Test patch:
diff --git a/test/test_query.py b/test/test_query.py
index a228e03..034feec 100644
--- a/test/test_query.py
+++ b/test/test_query.py
@@ -15,9 +15,21 @@ class TestQuery(TestBase):
         )
 
     def test_add_queries(self):
-        query = Query().where(col.foo > 4) + Query().take(5) + Query().sort_by(col.bar, Order.ASC, Nulls.LAST)
+        query = (Query().where(col.foo > 4) +
+                 Query().take(5) +
+                 Query().where(col.foo > 1).sort_by(col.bar, Order.ASC, Nulls.LAST))
         self.assertEqual(
-            " | where foo > 4 | take 5 | sort by bar asc nulls last",
+            " | where foo > 4 | take 5 | where foo > 1 | sort by bar asc nulls last",
+            query.render(),
+        )
+
+    def test_add_queries_with_table(self):
+        mock_kusto_client = MockKustoClient()
+        table = PyKustoClient(mock_kusto_client)['test_db']['test_table']
+        b = Query().take(5).take(2).sort_by(col.bar, Order.ASC, Nulls.LAST)
+        query = Query(table).where(col.foo > 4) + b
+        self.assertEqual(
+            "test_table | where foo > 4 | take 5 | take 2 | sort by bar asc nulls last",
             query.render(),
         )