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
This commit is contained in:
Kjell Winblad 2023-06-16 11:56:01 +02:00
parent 3ae37d6d81
commit fba8a787f4
4 changed files with 39 additions and 2 deletions

View File

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

View File

@ -57,6 +57,7 @@ groups() ->
, t_sqlselect_00
, t_sqlselect_01
, t_sqlselect_02
, t_sqlselect_04
, t_sqlselect_1
, t_sqlselect_2
]},
@ -423,6 +424,39 @@ t_sqlselect_03(_Config) ->
emqtt:stop(Client),
emqx_rule_registry:remove_rule(TopicRule1).
t_sqlselect_04(_Config) ->
%% Verify that div and mod works as expected
ok = emqx_rule_engine:load_providers(),
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 \"t2\" "
"",
TopicRule1 = create_simple_repub_rule(
<<"t1">>,
SQL,
<<"{\"mod1\": ${mod1},\"mod2\": ${mod2},\"div1\": ${div1},\"div2\": ${div2}}">>
),
{ok, Client} = emqtt:start_link([{username, <<"emqx">>}]),
{ok, _} = emqtt:connect(Client),
{ok, _, _} = emqtt:subscribe(Client, <<"t1">>, 0),
emqtt:publish(Client, <<"t2">>, <<"">>, 0),
receive {publish, #{payload := Payload}} ->
?assertMatch(#{
<<"mod1">> := 0,
<<"mod2">> := 1,
<<"div1">> := 2,
<<"div2">> := 3
}, emqx_json:decode(Payload, [return_maps]))
after 1000 ->
ct:fail(wait_for_t1)
end,
emqtt:stop(Client),
emqx_rule_registry:remove_rule(TopicRule1).
t_sqlselect_1(_Config) ->
ok = emqx_rule_engine:load_providers(),
TopicRule = create_simple_repub_rule(

View File

@ -46,3 +46,5 @@
- Fixed an issue where the WebHook plugin failed to execute the `on_client_connack` hook [#10710](https://github.com/emqx/emqx/pull/10710).
See https://github.com/emqx/emqx/issues/10628 for more details.
- Addressed an inconsistency in the usage of 'div' and 'mod' operations within the rule engine. Previously, the 'div' operation was only usable as an infix operation and 'mod' could only be applied through a function call. With this change, both 'div' and 'mod' can be used via function call syntax and infix syntax.

View File

@ -57,7 +57,7 @@
, {replayq, {git, "https://github.com/emqx/replayq", {tag, "0.3.5"}}}
, {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {branch, "2.0.4"}}}
, {emqtt, {git, "https://github.com/emqx/emqtt", {tag, "1.2.3.2"}}}
, {rulesql, {git, "https://github.com/emqx/rulesql", {tag, "0.1.5"}}}
, {rulesql, {git, "https://github.com/emqx/rulesql", {tag, "0.1.7"}}}
, {recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}}
, {observer_cli, "1.6.1"} % NOTE: depends on recon 2.5.1
, {getopt, "1.0.1"}