From ec657ef583bb146450eaae14c25d70296ffd6335 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Fri, 5 Jan 2024 18:52:34 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20support=20dict.inRange=20for=20numb?= =?UTF-8?q?ers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After #2952 Signed-off-by: Dominik Richter --- llx/builtin.go | 1 + llx/builtin_map.go | 17 ++++++++++++++++- mqlc/builtin.go | 2 ++ providers/os/resources/mql_test.go | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/llx/builtin.go b/llx/builtin.go index 1b5ac846c6..ed033078ae 100644 --- a/llx/builtin.go +++ b/llx/builtin.go @@ -513,6 +513,7 @@ func init() { "last": {f: dictGetLastIndexV2}, "{}": {f: dictBlockCallV2}, "length": {f: dictLengthV2}, + "inRange": {f: dictInRange}, "camelcase": {f: dictCamelcaseV2, Label: "camelcase"}, "downcase": {f: dictDowncaseV2, Label: "downcase"}, "upcase": {f: dictUpcaseV2, Label: "upcase"}, diff --git a/llx/builtin_map.go b/llx/builtin_map.go index b9c95c9df2..5929c66524 100644 --- a/llx/builtin_map.go +++ b/llx/builtin_map.go @@ -397,7 +397,22 @@ func dictLengthV2(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*R case map[string]interface{}: return IntData(int64(len(x))), 0, nil default: - return nil, 0, errors.New("dict value does not support field `length`") + return nil, 0, errors.New("dict value does not support `length`") + } +} + +func dictInRange(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*RawData, uint64, error) { + if bind.Value == nil { + return BoolFalse, 0, nil + } + + switch x := bind.Value.(type) { + case int64: + return int64InRange(e, x, chunk, ref) + case float64: + return float64InRange(e, x, chunk, ref) + default: + return nil, 0, errors.New("dict value does not support `inRange`") } } diff --git a/mqlc/builtin.go b/mqlc/builtin.go index a8599019b9..fceadfb9fa 100644 --- a/mqlc/builtin.go +++ b/mqlc/builtin.go @@ -61,6 +61,8 @@ func init() { types.Dict: { "[]": {typ: dictType, signature: FunctionSignature{Required: 1, Args: []types.Type{types.Any}}}, "{}": {typ: blockType, signature: FunctionSignature{Required: 1, Args: []types.Type{types.FunctionLike}}}, + // number-ish + "inRange": {typ: boolType, compile: compileInRange}, // string-ish "find": {typ: stringArrayType, signature: FunctionSignature{Required: 1, Args: []types.Type{types.Regex}}}, "length": {typ: intType, signature: FunctionSignature{}}, diff --git a/providers/os/resources/mql_test.go b/providers/os/resources/mql_test.go index bff4aca328..ee085a51d6 100644 --- a/providers/os/resources/mql_test.go +++ b/providers/os/resources/mql_test.go @@ -254,6 +254,24 @@ func TestResource_duplicateFields(t *testing.T) { }) } +func TestDict_Methods_InRange(t *testing.T) { + p := "parse.json('/dummy.json')." + + x := testutils.InitTester(testutils.LinuxMock()) + x.TestSimple(t, []testutils.SimpleTest{ + { + Code: p + "params['1'].inRange(1,3)", + ResultIndex: 1, + Expectation: true, + }, + { + Code: p + "params['1'].inRange(3,4)", + ResultIndex: 1, + Expectation: false, + }, + }) +} + func TestDict_Methods_Contains(t *testing.T) { p := "parse.json('/dummy.json')."