fix: cannot access columns exported by FOREACH in DO clause

This commit is contained in:
firest 2023-05-17 10:55:47 +08:00
parent 2a1814691e
commit 18043150be
2 changed files with 12 additions and 4 deletions

View File

@ -144,14 +144,14 @@ do_apply_rule(
) )
of of
true -> true ->
Collection2 = filter_collection(Columns, InCase, DoEach, Collection), Collection2 = filter_collection(ColumnsAndSelected, InCase, DoEach, Collection),
case Collection2 of case Collection2 of
[] -> [] ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'failed.no_result'); ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'failed.no_result');
_ -> _ ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'passed') ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'passed')
end, end,
NewEnvs = maps:merge(Columns, Envs), NewEnvs = maps:merge(ColumnsAndSelected, Envs),
{ok, [handle_action_list(RuleId, Actions, Coll, NewEnvs) || Coll <- Collection2]}; {ok, [handle_action_list(RuleId, Actions, Coll, NewEnvs) || Coll <- Collection2]};
false -> false ->
ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'failed.no_result'), ok = emqx_metrics_worker:inc(rule_metrics, RuleId, 'failed.no_result'),

View File

@ -1735,11 +1735,12 @@ t_sqlparse_foreach_7(_Config) ->
) )
). ).
-define(COLL, #{<<"info">> := [<<"haha">>, #{<<"name">> := <<"cmd1">>, <<"cmd">> := <<"1">>}]}).
t_sqlparse_foreach_8(_Config) -> t_sqlparse_foreach_8(_Config) ->
%% Verify foreach-do-incase and cascaded AS %% Verify foreach-do-incase and cascaded AS
Sql = Sql =
"foreach json_decode(payload) as p, p.sensors as s, s.collection as c, c.info as info " "foreach json_decode(payload) as p, p.sensors as s, s.collection as c, c.info as info "
"do info.cmd as msg_type, info.name as name " "do info.cmd as msg_type, info.name as name, s, c "
"incase is_map(info) " "incase is_map(info) "
"from \"t/#\" " "from \"t/#\" "
"where s.page = '2' ", "where s.page = '2' ",
@ -1748,7 +1749,14 @@ t_sqlparse_foreach_8(_Config) ->
"{\"info\":[\"haha\", {\"name\":\"cmd1\", \"cmd\":\"1\"}]} } }" "{\"info\":[\"haha\", {\"name\":\"cmd1\", \"cmd\":\"1\"}]} } }"
>>, >>,
?assertMatch( ?assertMatch(
{ok, [#{<<"name">> := <<"cmd1">>, <<"msg_type">> := <<"1">>}]}, {ok, [
#{
<<"name">> := <<"cmd1">>,
<<"msg_type">> := <<"1">>,
<<"s">> := #{<<"page">> := 2, <<"collection">> := ?COLL},
<<"c">> := ?COLL
}
]},
emqx_rule_sqltester:test( emqx_rule_sqltester:test(
#{ #{
sql => Sql, sql => Sql,