From fba8a787f448ffb8c3528b2285bae12b12e91972 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Fri, 16 Jun 2023 11:56:01 +0200 Subject: [PATCH] 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 --- apps/emqx_rule_engine/include/rule_engine.hrl | 3 +- .../test/emqx_rulesql_SUITE.erl | 34 +++++++++++++++++++ changes/v4.4.19-en.md | 2 ++ rebar.config | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/apps/emqx_rule_engine/include/rule_engine.hrl b/apps/emqx_rule_engine/include/rule_engine.hrl index fe2cc1880..1507839c1 100644 --- a/apps/emqx_rule_engine/include/rule_engine.hrl +++ b/apps/emqx_rule_engine/include/rule_engine.hrl @@ -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 diff --git a/apps/emqx_rule_engine/test/emqx_rulesql_SUITE.erl b/apps/emqx_rule_engine/test/emqx_rulesql_SUITE.erl index 51849a35f..5e48420ec 100644 --- a/apps/emqx_rule_engine/test/emqx_rulesql_SUITE.erl +++ b/apps/emqx_rule_engine/test/emqx_rulesql_SUITE.erl @@ -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( diff --git a/changes/v4.4.19-en.md b/changes/v4.4.19-en.md index f7525a253..6b9452aa5 100644 --- a/changes/v4.4.19-en.md +++ b/changes/v4.4.19-en.md @@ -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. diff --git a/rebar.config b/rebar.config index 31b06bb0f..83ab8fbcf 100644 --- a/rebar.config +++ b/rebar.config @@ -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"}