From 366124c4bd89cc490a8ec1d2bc7b45ce0902814d Mon Sep 17 00:00:00 2001
From: Denis Smirnov <sd@picodata.io>
Date: Fri, 3 Mar 2023 21:38:23 +0700
Subject: [PATCH] perf: improve cast grammar to speed up parsing

---
 .../integration/arbitrary_expressions_test.lua   | 16 +++++++---------
 sbroad-core/src/frontend/sql/query.pest          |  5 +++--
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/sbroad-cartridge/test_app/test/integration/arbitrary_expressions_test.lua b/sbroad-cartridge/test_app/test/integration/arbitrary_expressions_test.lua
index 17b79959fd..a468edf2ac 100644
--- a/sbroad-cartridge/test_app/test/integration/arbitrary_expressions_test.lua
+++ b/sbroad-cartridge/test_app/test/integration/arbitrary_expressions_test.lua
@@ -60,6 +60,12 @@ arbitrary_projection.test_arbitrary_invalid = function()
         select "a" + "b" and true from "arithmetic_space"
     ]], {} })
     t.assert_str_contains(tostring(err), "Type mismatch: can not convert integer(3) to boolean")
+
+    -- projection consisted of arithmetic, bool and cast (function)
+    local _, err = api:call("sbroad.execute", { [[
+        select cast("id" * 2 > 0 as boolean), cast("id" * 2 > 0 as boolean) as "cast" from "arithmetic_space"
+    ]], {} })
+    t.assert_str_contains(tostring(err), "rule parsing error")
 end
 
 arbitrary_projection.test_arbitrary_valid = function()
@@ -145,12 +151,4 @@ arbitrary_projection.test_arbitrary_valid = function()
     ]], {} })
     t.assert_equals(err, nil)
     t.assert_equals(r.rows, all_true)
-
-    -- projection consisted of arithmetic, bool and cast (function)
-    local r, err = api:call("sbroad.execute", { [[
-        select cast("id" * 2 > 0 as boolean), cast("id" * 2 > 0 as boolean) as "cast" from "arithmetic_space"
-    ]], {} })
-    t.assert_equals(err, nil)
-    t.assert_equals(r.rows, all_true)
-
-end
\ No newline at end of file
+end
diff --git a/sbroad-core/src/frontend/sql/query.pest b/sbroad-core/src/frontend/sql/query.pest
index 56e4419283..06ae2cd0d2 100644
--- a/sbroad-core/src/frontend/sql/query.pest
+++ b/sbroad-core/src/frontend/sql/query.pest
@@ -93,7 +93,8 @@ Function = { FunctionName ~ ("(" ~ FunctionArgs ~ ")") }
     FunctionArgs = _{ (FunctionExpr ~ ("," ~ FunctionExpr)*)? }
     FunctionExpr = _{ Parentheses | Primary }
 
-Cast = { ^"cast" ~ "(" ~ Expr ~ ^"as" ~ Type ~ ")" }
+Cast = { ^"cast" ~ "(" ~ CastExpr ~ ^"as" ~ Type ~ ")" }
+    CastExpr = _{ FunctionExpr | Cast }
     Type = _{ TypeAny | TypeBool | TypeDecimal | TypeDouble
              | TypeInt | TypeNumber | TypeScalar | TypeString
              | TypeText | TypeUnsigned | TypeVarchar }
@@ -111,7 +112,7 @@ Cast = { ^"cast" ~ "(" ~ Expr ~ ^"as" ~ Type ~ ")" }
         Length = @{ Unsigned }
 
 Concat = { ConcatLeft ~ ^"||" ~ ConcatRight }
-    ConcatLeft = _{ SingleQuotedString | Cast | Function | Reference }
+    ConcatLeft = _{ Cast | Function | Reference | SingleQuotedString }
     ConcatRight = _{ Concat | ConcatLeft }
 
 GroupingElement = _{ Concat | Cast | Function | Reference }
-- 
GitLab