From cf31b650764d7d779423fda0bc6f70126e4932e0 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Mon, 12 Jun 2023 17:20:52 +0200 Subject: [PATCH 1/2] 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_rule_engine_SUITE.erl | 31 +++++++++++++++++++ mix.exs | 2 +- rebar.config | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/apps/emqx_rule_engine/include/rule_engine.hrl b/apps/emqx_rule_engine/include/rule_engine.hrl index e3fef7e62..b2a6a549e 100644 --- a/apps/emqx_rule_engine/include/rule_engine.hrl +++ b/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 diff --git a/apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl b/apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl index 2ec32173f..c9feda601 100644 --- a/apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl +++ b/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, " diff --git a/mix.exs b/mix.exs index e53f0c224..3fc807c78 100644 --- a/mix.exs +++ b/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"}, diff --git a/rebar.config b/rebar.config index f6830f83b..713cbce40 100644 --- a/rebar.config +++ b/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"} From 256adeb580d219cf899b6ab4b8bc2a43f6d6ece6 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Mon, 12 Jun 2023 17:33:54 +0200 Subject: [PATCH 2/2] docs: add changelog entry --- changes/ce/fix-11026.en.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/ce/fix-11026.en.md diff --git a/changes/ce/fix-11026.en.md b/changes/ce/fix-11026.en.md new file mode 100644 index 000000000..d07157b5f --- /dev/null +++ b/changes/ce/fix-11026.en.md @@ -0,0 +1 @@ +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.