From 64cdb80cadfd20cb657fbd7845170c50fadafe9d Mon Sep 17 00:00:00 2001
From: Mergen Imeev <imeevma@tarantool.org>
Date: Mon, 4 Jul 2022 16:57:39 +0300
Subject: [PATCH] sql: fix error in ORDER BY ephemeral space format

This patch fixes a bug where the ANY field type was replaced by the
SCALAR field type in the ephemeral space used in ORDER BY.

Closes #7345

NO_DOC=bugfix
---
 .../gh-7345-any-in-order-by-ephem.md          |  3 +++
 src/box/sql/select.c                          |  2 --
 .../gh_7345_any_in_order_by_ephem_test.lua    | 26 +++++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 changelogs/unreleased/gh-7345-any-in-order-by-ephem.md
 create mode 100644 test/sql-luatest/gh_7345_any_in_order_by_ephem_test.lua

diff --git a/changelogs/unreleased/gh-7345-any-in-order-by-ephem.md b/changelogs/unreleased/gh-7345-any-in-order-by-ephem.md
new file mode 100644
index 0000000000..c5651fe832
--- /dev/null
+++ b/changelogs/unreleased/gh-7345-any-in-order-by-ephem.md
@@ -0,0 +1,3 @@
+## bugfix/sql
+
+* Fixed bug with ANY type in ephemeral space format in ORDER BY (gh-7043).
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index e002e749cb..89905bfba8 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -229,8 +229,6 @@ sql_space_info_new_for_sorting(struct Parse *parser, struct ExprList *order_by,
 		bool b;
 		struct Expr *expr = list->a[i].pExpr;
 		enum field_type type = sql_expr_type(expr);
-		if (type == FIELD_TYPE_ANY)
-			type = FIELD_TYPE_SCALAR;
 		uint32_t id;
 		struct coll *coll;
 		if (sql_expr_coll(parser, expr, &b, &id, &coll) != 0)
diff --git a/test/sql-luatest/gh_7345_any_in_order_by_ephem_test.lua b/test/sql-luatest/gh_7345_any_in_order_by_ephem_test.lua
new file mode 100644
index 0000000000..fb66cf4bad
--- /dev/null
+++ b/test/sql-luatest/gh_7345_any_in_order_by_ephem_test.lua
@@ -0,0 +1,26 @@
+local server = require('test.luatest_helpers.server')
+local t = require('luatest')
+local g = t.group()
+
+g.before_all(function()
+    g.server = server:new({alias = 'test_any_in_order_by_ephem'})
+    g.server:start()
+end)
+
+g.after_all(function()
+    g.server:stop()
+end)
+
+g.test_any_in_order_by_ephem = function()
+    g.server:exec(function()
+        local t = require('luatest')
+        box.execute([[CREATE TABLE t(i INT PRIMARY KEY, a ANY, b INTEGER);]])
+        box.execute([[INSERT INTO t VALUES(1, [1, 2], 2);]])
+        box.execute([[INSERT INTO t VALUES(2, {'a': 1, 'b': 2}, 1);]])
+        local sql = [[SELECT a FROM t ORDER BY b LIMIT 1;]]
+        t.assert_equals(box.execute(sql).rows, {{{a = 1, b = 2}}})
+        local sql = [[SELECT a FROM t ORDER BY b LIMIT 1 OFFSET 1;]]
+        t.assert_equals(box.execute(sql).rows, {{{1, 2}}})
+        box.execute([[DROP TABLE t;]])
+    end)
+end
-- 
GitLab