From d7e10846585379e4b64b2fd50e95292578354dc3 Mon Sep 17 00:00:00 2001
From: Arseniy Volynets <vol0ncar@yandex.ru>
Date: Tue, 12 Sep 2023 12:48:20 +0300
Subject: [PATCH] feat: allow create global table

---
 sbroad-core/src/frontend/sql.rs | 39 +++++++++++++++++++++------------
 sbroad-core/src/ir/ddl.rs       | 10 ++++-----
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/sbroad-core/src/frontend/sql.rs b/sbroad-core/src/frontend/sql.rs
index 23014c1ce7..546e09d41e 100644
--- a/sbroad-core/src/frontend/sql.rs
+++ b/sbroad-core/src/frontend/sql.rs
@@ -314,13 +314,7 @@ fn parse_create_table(ast: &AbstractSyntaxTree, node: &ParseNode) -> Result<Ddl,
                 ) {
                     let distribution_type_node = ast.nodes.get_node(*distribution_type_id)?;
                     match distribution_type_node.rule {
-                        Type::Global => {
-                            // TODO: support global spaces via SQL API.
-                            return Err(SbroadError::NotImplemented(
-                                Entity::Node,
-                                "global spaces are not supported yet".into(),
-                            ));
-                        }
+                        Type::Global => {}
                         Type::Sharding => {
                             let shard_node = ast.nodes.get_node(*distribution_type_id)?;
                             for shard_col_id in &shard_node.children {
@@ -379,13 +373,30 @@ fn parse_create_table(ast: &AbstractSyntaxTree, node: &ParseNode) -> Result<Ddl,
             }
         }
     }
-    let create_sharded_table = Ddl::CreateShardedTable {
-        name: table_name,
-        format: columns,
-        primary_key: pk_keys,
-        sharding_key: shard_key,
-        engine_type,
-        timeout,
+    let create_sharded_table = if shard_key.is_empty() {
+        if engine_type != SpaceEngineType::Memtx {
+            return Err(SbroadError::Unsupported(
+                Entity::Query,
+                Some("global spaces can use only memtx engine".into()),
+            ));
+        }
+        Ddl::CreateTable {
+            name: table_name,
+            format: columns,
+            primary_key: pk_keys,
+            sharding_key: None,
+            engine_type,
+            timeout,
+        }
+    } else {
+        Ddl::CreateTable {
+            name: table_name,
+            format: columns,
+            primary_key: pk_keys,
+            sharding_key: Some(shard_key),
+            engine_type,
+            timeout,
+        }
     };
     Ok(create_sharded_table)
 }
diff --git a/sbroad-core/src/ir/ddl.rs b/sbroad-core/src/ir/ddl.rs
index d421cc4dfb..1e4b6a7dca 100644
--- a/sbroad-core/src/ir/ddl.rs
+++ b/sbroad-core/src/ir/ddl.rs
@@ -15,11 +15,13 @@ pub struct ColumnDef {
 
 #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
 pub enum Ddl {
-    CreateShardedTable {
+    CreateTable {
         name: String,
         format: Vec<ColumnDef>,
         primary_key: Vec<String>,
-        sharding_key: Vec<String>,
+        /// If `None`, create global table.
+        sharding_key: Option<Vec<String>>,
+        /// Vinyl is supported only for sharded tables.
         engine_type: SpaceEngineType,
         timeout: Decimal,
     },
@@ -36,9 +38,7 @@ impl Ddl {
     /// - timeout parsing error
     pub fn timeout(&self) -> Result<f64, SbroadError> {
         match self {
-            Ddl::CreateShardedTable { ref timeout, .. } | Ddl::DropTable { ref timeout, .. } => {
-                timeout
-            }
+            Ddl::CreateTable { ref timeout, .. } | Ddl::DropTable { ref timeout, .. } => timeout,
         }
         .to_string()
         .parse()
-- 
GitLab