Skip to content

Commit 16e61b4

Browse files
fix: support in operator for json search (#6689)
* fix: support in in json search * fix: remove else condition and update test cases --------- Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent fdcdbf0 commit 16e61b4

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

pkg/query-service/app/logs/v3/enrich_query.go

+16
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ func jsonFilterEnrich(filter v3.FilterItem) v3.FilterItem {
167167
// check if the value is a int, float, string, bool
168168
valueType := ""
169169
switch filter.Value.(type) {
170+
// even the filter value is an array the actual type of the value is string.
171+
case []interface{}:
172+
// check first value type in array and use that
173+
if len(filter.Value.([]interface{})) > 0 {
174+
firstVal := filter.Value.([]interface{})[0]
175+
switch firstVal.(type) {
176+
case uint8, uint16, uint32, uint64, int, int8, int16, int32, int64:
177+
valueType = "int64"
178+
case float32, float64:
179+
valueType = "float64"
180+
case bool:
181+
valueType = "bool"
182+
default:
183+
valueType = "string"
184+
}
185+
}
170186
case uint8, uint16, uint32, uint64, int, int8, int16, int32, int64:
171187
valueType = "int64"
172188
case float32, float64:

pkg/query-service/app/logs/v3/enrich_query_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,50 @@ var testJSONFilterEnrichData = []struct {
563563
Value: 10.0,
564564
},
565565
},
566+
{
567+
Name: "check IN",
568+
Filter: v3.FilterItem{
569+
Key: v3.AttributeKey{
570+
Key: "body.attr",
571+
DataType: v3.AttributeKeyDataTypeUnspecified,
572+
Type: v3.AttributeKeyTypeUnspecified,
573+
},
574+
Operator: "IN",
575+
Value: []interface{}{"hello", "world"},
576+
},
577+
Result: v3.FilterItem{
578+
Key: v3.AttributeKey{
579+
Key: "body.attr",
580+
DataType: v3.AttributeKeyDataTypeString,
581+
Type: v3.AttributeKeyTypeUnspecified,
582+
IsJSON: true,
583+
},
584+
Operator: "IN",
585+
Value: []interface{}{"hello", "world"},
586+
},
587+
},
588+
{
589+
Name: "check NOT_IN",
590+
Filter: v3.FilterItem{
591+
Key: v3.AttributeKey{
592+
Key: "body.attr",
593+
DataType: v3.AttributeKeyDataTypeUnspecified,
594+
Type: v3.AttributeKeyTypeUnspecified,
595+
},
596+
Operator: "NOT_IN",
597+
Value: []interface{}{10, 20},
598+
},
599+
Result: v3.FilterItem{
600+
Key: v3.AttributeKey{
601+
Key: "body.attr",
602+
DataType: v3.AttributeKeyDataTypeInt64,
603+
Type: v3.AttributeKeyTypeUnspecified,
604+
IsJSON: true,
605+
},
606+
Operator: "NOT_IN",
607+
Value: []interface{}{10, 20},
608+
},
609+
},
566610
}
567611

568612
func TestJsonEnrich(t *testing.T) {

pkg/query-service/app/logs/v4/json_filter_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,71 @@ var testGetJSONFilterData = []struct {
183183
},
184184
Filter: "lower(body) like lower('%message%') AND JSON_EXISTS(body, '$.\"message\"')",
185185
},
186+
{
187+
Name: "test json in array string",
188+
FilterItem: v3.FilterItem{
189+
Key: v3.AttributeKey{
190+
Key: "body.name",
191+
DataType: "string",
192+
IsJSON: true,
193+
},
194+
Operator: "in",
195+
Value: []interface{}{"hello", "world"},
196+
},
197+
Filter: "lower(body) like lower('%name%') AND JSON_EXISTS(body, '$.\"name\"') AND JSON_VALUE(body, '$.\"name\"') IN ['hello','world']",
198+
},
199+
{
200+
Name: "test json in array number",
201+
FilterItem: v3.FilterItem{
202+
Key: v3.AttributeKey{
203+
Key: "body.value",
204+
DataType: "int64",
205+
IsJSON: true,
206+
},
207+
Operator: "in",
208+
Value: []interface{}{10, 11},
209+
},
210+
Filter: "lower(body) like lower('%value%') AND JSON_EXISTS(body, '$.\"value\"') AND JSONExtract(JSON_VALUE(body, '$.\"value\"'), 'Int64') IN [10,11]",
211+
},
212+
{
213+
Name: "test json in array mixed data- allow",
214+
FilterItem: v3.FilterItem{
215+
Key: v3.AttributeKey{
216+
Key: "body.value",
217+
DataType: "int64",
218+
IsJSON: true,
219+
},
220+
Operator: "in",
221+
Value: []interface{}{11, "11"},
222+
},
223+
Filter: "lower(body) like lower('%value%') AND JSON_EXISTS(body, '$.\"value\"') AND JSONExtract(JSON_VALUE(body, '$.\"value\"'), 'Int64') IN [11,11]",
224+
},
225+
{
226+
Name: "test json in array mixed data- fail",
227+
FilterItem: v3.FilterItem{
228+
Key: v3.AttributeKey{
229+
Key: "body.value",
230+
DataType: "int64",
231+
IsJSON: true,
232+
},
233+
Operator: "in",
234+
Value: []interface{}{11, "11", "hello"},
235+
},
236+
Error: true,
237+
},
238+
{
239+
Name: "test json in array mixed data- allow",
240+
FilterItem: v3.FilterItem{
241+
Key: v3.AttributeKey{
242+
Key: "body.value",
243+
DataType: "string",
244+
IsJSON: true,
245+
},
246+
Operator: "in",
247+
Value: []interface{}{"hello", 11},
248+
},
249+
Filter: "lower(body) like lower('%value%') AND JSON_EXISTS(body, '$.\"value\"') AND JSON_VALUE(body, '$.\"value\"') IN ['hello','11']",
250+
},
186251
}
187252

188253
func TestGetJSONFilter(t *testing.T) {

0 commit comments

Comments
 (0)