Parcourir la source

fix: rule engine different behavior for div and mod

Previously, the div operation could only be used as an infix operation
while mod could only be used as a function call. After this commit, one
can use both div and mod using function call syntax and infix syntax.

Fixes: https://emqx.atlassian.net/browse/EMQX-10216
Kjell Winblad il y a 2 ans
Parent
commit
cf31b65076

+ 2 - 1
apps/emqx_rule_engine/include/rule_engine.hrl

@@ -70,7 +70,8 @@
         Op =:= '-' orelse
         Op =:= '*' orelse
         Op =:= '/' orelse
-        Op =:= 'div')
+        Op =:= 'div' orelse
+        Op =:= 'mod')
 ).
 
 %% Compare operators

+ 31 - 0
apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl

@@ -66,6 +66,7 @@ groups() ->
             t_sqlselect_with_3rd_party_impl2,
             t_sqlselect_with_3rd_party_funcs_unknown,
             t_sqlselect_001,
+            t_sqlselect_002,
             t_sqlselect_inject_props,
             t_sqlselect_01,
             t_sqlselect_02,
@@ -1089,6 +1090,36 @@ t_sqlselect_001(_Config) ->
         )
     ).
 
+t_sqlselect_002(_Config) ->
+    %% Verify that the div and mod can be used both as infix operations and as
+    %% function calls
+    Sql =
+        ""
+        "select 2 mod 2   as mod1,\n"
+        "                  mod(3, 2) as mod2,\n"
+        "                  4 div 2   as div1,\n"
+        "                  div(7, 2) as div2\n"
+        "           from \"t/#\" "
+        "",
+    ?assertMatch(
+        {ok, #{
+            <<"mod1">> := 0,
+            <<"mod2">> := 1,
+            <<"div1">> := 2,
+            <<"div2">> := 3
+        }},
+        emqx_rule_sqltester:test(
+            #{
+                sql => Sql,
+                context =>
+                    #{
+                        payload => #{<<"what">> => 4},
+                        topic => <<"t/a">>
+                    }
+            }
+        )
+    ).
+
 t_sqlselect_inject_props(_Config) ->
     SQL =
         "SELECT json_decode(payload) as p, payload, "

+ 1 - 1
mix.exs

@@ -65,7 +65,7 @@ defmodule EMQXUmbrella.MixProject do
       # maybe forbid to fetch quicer
       {:emqtt,
        github: "emqx/emqtt", tag: "1.8.6", override: true, system_env: maybe_no_quic_env()},
-      {:rulesql, github: "emqx/rulesql", tag: "0.1.6"},
+      {:rulesql, github: "emqx/rulesql", tag: "0.1.7"},
       {:observer_cli, "1.7.1"},
       {:system_monitor, github: "ieQu1/system_monitor", tag: "3.0.3"},
       {:telemetry, "1.1.0"},

+ 1 - 1
rebar.config

@@ -70,7 +70,7 @@
     , {replayq, {git, "https://github.com/emqx/replayq.git", {tag, "0.3.7"}}}
     , {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}}
     , {emqtt, {git, "https://github.com/emqx/emqtt", {tag, "1.8.6"}}}
-    , {rulesql, {git, "https://github.com/emqx/rulesql", {tag, "0.1.6"}}}
+    , {rulesql, {git, "https://github.com/emqx/rulesql", {tag, "0.1.7"}}}
     , {observer_cli, "1.7.1"} % NOTE: depends on recon 2.5.x
     , {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.3"}}}
     , {getopt, "1.0.2"}