From 0daadbe91616ff100446a3d245ef448c5239b8cc Mon Sep 17 00:00:00 2001
From: Denis Smirnov <sd@picodata.io>
Date: Wed, 11 Oct 2023 23:58:36 +0700
Subject: [PATCH] feat: pg_proto helpers for picodata

---
 sbroad-core/src/executor.rs | 21 +++++++++++++++++++--
 sbroad-core/src/helper.lua  | 14 +++++++-------
 sbroad-core/src/ir.rs       |  2 +-
 sbroad-core/src/otm.rs      | 16 ++++++++++++++++
 4 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/sbroad-core/src/executor.rs b/sbroad-core/src/executor.rs
index 963d645f11..a264cc0a0b 100644
--- a/sbroad-core/src/executor.rs
+++ b/sbroad-core/src/executor.rs
@@ -53,7 +53,10 @@ pub mod vtable;
 
 impl Plan {
     /// Apply optimization rules to the plan.
-    pub(crate) fn optimize(&mut self) -> Result<(), SbroadError> {
+    ///
+    /// # Errors
+    /// - Failed to optimize the plan.
+    pub fn optimize(&mut self) -> Result<(), SbroadError> {
         self.replace_in_operator()?;
         self.push_down_not()?;
         self.split_columns()?;
@@ -87,6 +90,20 @@ where
     C: Router + Vshard,
     &'a C: Vshard,
 {
+    pub fn from_parts(
+        is_explain: bool,
+        exec_plan: ExecutionPlan,
+        coordinator: &'a C,
+        bucket_map: HashMap<usize, Buckets>,
+    ) -> Self {
+        Self {
+            is_explain,
+            exec_plan,
+            coordinator,
+            bucket_map,
+        }
+    }
+
     /// Create a new query.
     ///
     /// # Errors
@@ -137,7 +154,7 @@ where
             plan.optimize()?;
         }
         let query = Query {
-            is_explain: plan.is_expain(),
+            is_explain: plan.is_explain(),
             exec_plan: ExecutionPlan::from(plan),
             coordinator,
             bucket_map: HashMap::new(),
diff --git a/sbroad-core/src/helper.lua b/sbroad-core/src/helper.lua
index 84712c7288..3e6c18bc8e 100644
--- a/sbroad-core/src/helper.lua
+++ b/sbroad-core/src/helper.lua
@@ -30,15 +30,15 @@ local function module_name()
 end
 
 local function format_result(result)
-    local formatted = {}
-    if result['metadata'] ~= nil then
-	formatted.metadata = setmetatable(result.metadata, { __serialize = nil })
+    local formatted = setmetatable(result, { __serialize = nil })
+    if formatted['available'] ~= nil then
+	formatted.available = setmetatable(formatted.available, { __serialize = nil })
     end
-    if result['rows'] ~= nil then
-	formatted.rows = setmetatable(result.rows, { __serialize = nil })
+    if formatted['metadata'] ~= nil then
+	formatted.metadata = setmetatable(formatted.metadata, { __serialize = nil })
     end
-    if result['metadata'] == nil and result['rows'] == nil then
-	formatted = setmetatable(result, { __serialize = nil })
+    if formatted['rows'] ~= nil then
+	formatted.rows = setmetatable(formatted.rows, { __serialize = nil })
     end
 
     return formatted
diff --git a/sbroad-core/src/ir.rs b/sbroad-core/src/ir.rs
index 6302f077aa..9e8101d9c4 100644
--- a/sbroad-core/src/ir.rs
+++ b/sbroad-core/src/ir.rs
@@ -765,7 +765,7 @@ impl Plan {
 
     /// Checks that plan is explain query
     #[must_use]
-    pub fn is_expain(&self) -> bool {
+    pub fn is_explain(&self) -> bool {
         self.is_explain
     }
 
diff --git a/sbroad-core/src/otm.rs b/sbroad-core/src/otm.rs
index 1aaa2fa26a..125809f502 100644
--- a/sbroad-core/src/otm.rs
+++ b/sbroad-core/src/otm.rs
@@ -276,6 +276,22 @@ where
     f()
 }
 
+#[inline]
+#[allow(unreachable_code)]
+#[allow(unused_variables)]
+pub fn stat_query_span<T, F>(name: &'static str, sql: &str, id: &str, f: F) -> T
+where
+    F: FnOnce() -> T,
+{
+    #[cfg(not(feature = "mock"))]
+    {
+        let tracer = QueryTracer::Statistics;
+        let ctx = Context::new();
+        return query_span(name, id, &tracer, &ctx, sql, f);
+    }
+    f()
+}
+
 #[inline]
 #[allow(unreachable_code)]
 #[allow(unused_variables)]
-- 
GitLab