From 24596a0bbf04a7a37b578cbc8e8325febf5d3cb8 Mon Sep 17 00:00:00 2001
From: Denis Smirnov <sd@picodata.io>
Date: Fri, 17 Dec 2021 18:29:32 +0700
Subject: [PATCH] refactoring: make "add_sub_query" Plan method.

---
 src/ir/operator.rs       | 51 +++++++++++++++++++---------------------
 src/ir/operator/tests.rs |  7 +++---
 2 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/src/ir/operator.rs b/src/ir/operator.rs
index 11d3c54ff8..33ed546f66 100644
--- a/src/ir/operator.rs
+++ b/src/ir/operator.rs
@@ -190,33 +190,6 @@ impl Relational {
             Relational::ScanRelation { .. } => None,
         }
     }
-
-    /// New `ScanSubQuery` constructor.
-    ///
-    /// # Errors
-    /// Returns `QueryPlannerError`:
-    /// - child node is not relational
-    /// - child node output is not a correct tuple
-    /// - `SubQuery` name is empty
-    pub fn new_sub_query(
-        plan: &mut Plan,
-        child: usize,
-        alias: &str,
-    ) -> Result<Self, QueryPlannerError> {
-        if alias.is_empty() {
-            return Err(QueryPlannerError::InvalidName);
-        }
-        let id = plan.nodes.next_id();
-        let children: Vec<usize> = vec![child];
-        let output = plan.add_output_row(id, &children, &[0], &[])?;
-
-        Ok(Relational::ScanSubQuery {
-            alias: String::from(alias),
-            children,
-            id,
-            output,
-        })
-    }
 }
 
 impl Plan {
@@ -307,6 +280,30 @@ impl Plan {
         Ok(self.nodes.push(Node::Relational(select)))
     }
 
+    /// Add sub query scan node.
+    ///
+    /// # Errors
+    /// - child node is not relational
+    /// - child node output is not a correct tuple
+    /// - sub query name is empty
+    pub fn add_sub_query(&mut self, child: usize, alias: &str) -> Result<usize, QueryPlannerError> {
+        if alias.is_empty() {
+            return Err(QueryPlannerError::InvalidName);
+        }
+        let id = self.nodes.next_id();
+        let children: Vec<usize> = vec![child];
+        let output = self.add_output_row(id, &children, &[0], &[])?;
+
+        let sq = Relational::ScanSubQuery {
+            alias: String::from(alias),
+            children,
+            id,
+            output,
+        };
+
+        Ok(self.nodes.push(Node::Relational(sq)))
+    }
+
     /// Add union all node.
     ///
     /// # Errors
diff --git a/src/ir/operator/tests.rs b/src/ir/operator/tests.rs
index 1f6c1966bf..8df5bc57a4 100644
--- a/src/ir/operator/tests.rs
+++ b/src/ir/operator/tests.rs
@@ -241,20 +241,19 @@ fn sub_query() {
     plan.add_rel(t);
 
     let scan_id = plan.add_scan("t").unwrap();
-
-    Relational::new_sub_query(&mut plan, scan_id, "sq").unwrap();
+    plan.add_sub_query(scan_id, "sq").unwrap();
 
     // Non-relational child node
     let a = 1;
     assert_eq!(
         QueryPlannerError::InvalidNode,
-        Relational::new_sub_query(&mut plan, a, "sq").unwrap_err()
+        plan.add_sub_query(a, "sq").unwrap_err()
     );
 
     // Invalid name
     assert_eq!(
         QueryPlannerError::InvalidName,
-        Relational::new_sub_query(&mut plan, scan_id, "").unwrap_err()
+        plan.add_sub_query(scan_id, "").unwrap_err()
     );
 }
 
-- 
GitLab