From babd86ecc420c70b6367c94ee7a5f7b4107ce69c Mon Sep 17 00:00:00 2001
From: Kurdakov Alexander <kusancho12@gmail.com>
Date: Thu, 24 Aug 2023 19:02:53 +0300
Subject: [PATCH] feat: set interactive sql executor to pico.sql

---
 CHANGELOG.md         |  4 +++-
 src/lib.rs           | 12 ++++++++++++
 tarantool-sys        |  2 +-
 test/int/test_sql.py | 46 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e96957d203..72ef771742 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ with the `YY.0M.MICRO` scheme.
 
 ### Features
 
+- _Clusterwide SQL_ now availiable via `\set language sql` in interactive console.
+
 - Allow specifying `picodata connect [user@][host][:port]` format. It
   overrides the `--user` option.
 
@@ -46,7 +48,7 @@ with the `YY.0M.MICRO` scheme.
 
 - Update `pico.LUA_API_VERSION`: `1.0.0` -> `2.2.0`
 - New semantics of `pico.create_space()`. It's idempotent now.
-- `pico.create_space()` has new optional parameter: `engine`. 
+- `pico.create_space()` has new optional parameter: `engine`.
   Note: global spaces can only have memtx engine.
 - Add `pico.drop_space()`
 - Add `pico.create_user()`, `pico.drop_user()`
diff --git a/src/lib.rs b/src/lib.rs
index b543ad89ba..95b33208f2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -243,6 +243,17 @@ fn set_console_prompt() {
     .expect("setting prompt should never fail")
 }
 
+fn redirect_interactive_sql() {
+    tarantool::exec(
+        r#"
+        local console = require('console')
+        assert(pico.sql)
+        console.set_sql_executor(pico.sql)
+        "#,
+    )
+    .expect("overriding sql executor shouldn't fail")
+}
+
 #[allow(clippy::enum_variant_names)]
 #[derive(Debug, Serialize, Deserialize)]
 pub enum Entrypoint {
@@ -285,6 +296,7 @@ fn init_common(args: &args::Run, cfg: &tarantool::Cfg) -> (Clusterwide, RaftSpac
     init_sbroad();
 
     set_console_prompt();
+    redirect_interactive_sql();
     init_handlers();
     traft::event::init();
 
diff --git a/tarantool-sys b/tarantool-sys
index d148e51958..23fb9d4fe5 160000
--- a/tarantool-sys
+++ b/tarantool-sys
@@ -1 +1 @@
-Subproject commit d148e51958d2fb07daeeb9f1431e7756a84303e0
+Subproject commit 23fb9d4fe53631b360a94abca0d875b51aeea5a7
diff --git a/test/int/test_sql.py b/test/int/test_sql.py
index a6917acd32..3ccbb15ceb 100644
--- a/test/int/test_sql.py
+++ b/test/int/test_sql.py
@@ -333,3 +333,49 @@ def test_sql_limits(cluster: Cluster):
         select * from "t" option(vtable_max_rows=1, sql_vdbe_max_steps=50)
     """
         )
+
+
+def test_distributed_sql_via_set_language(cluster: Cluster):
+    cluster.deploy(instance_count=2)
+    i1, i2 = cluster.instances
+
+    prelude = """
+        local console = require('console')
+        console.eval([[\\ set language sql]])
+        console.eval([[\\ set delimiter ;]])
+    """
+
+    i1.eval(
+        f"""
+        {prelude}
+        return console.eval('create table t \
+            (a integer not null, b int not null, primary key (a)) \
+                using memtx distributed by (b) option (timeout = 3);')
+    """
+    )
+
+    i1.eval(
+        f"""
+        {prelude}
+        return console.eval('insert into t values (22, 8);')
+    """
+    )
+
+    select_from_second_instance = i2.eval(
+        f"""
+        {prelude}
+        return console.eval('select * from t where a = 22;')
+    """
+    )
+
+    assert (
+        select_from_second_instance
+        == """---
+- metadata:
+  - {'name': 'A', 'type': 'integer'}
+  - {'name': 'B', 'type': 'integer'}
+  rows:
+  - [22, 8]
+...
+"""
+    )
-- 
GitLab