From ed809474d62911bccff90f241a581acf6b551fb6 Mon Sep 17 00:00:00 2001 From: Nityananda Gohain Date: Wed, 13 Sep 2023 21:00:40 +0530 Subject: [PATCH] feat: json filter bool support (#3544) * feat: json filter bool support * fix: update json filter for bool --- pkg/query-service/app/logs/v3/json_filter.go | 7 ++- .../app/logs/v3/json_filter_test.go | 46 +++++++++++++++++++ pkg/query-service/model/v3/v3.go | 3 +- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/query-service/app/logs/v3/json_filter.go b/pkg/query-service/app/logs/v3/json_filter.go index f15b101a8c..345da5a013 100644 --- a/pkg/query-service/app/logs/v3/json_filter.go +++ b/pkg/query-service/app/logs/v3/json_filter.go @@ -12,24 +12,29 @@ const ( STRING = "String" INT64 = "Int64" FLOAT64 = "Float64" + BOOL = "Bool" ARRAY_STRING = "Array(String)" ARRAY_INT64 = "Array(Int64)" ARRAY_FLOAT64 = "Array(Float64)" + ARRAY_BOOL = "Array(Bool)" ) var dataTypeMapping = map[string]string{ "string": STRING, "int64": INT64, "float64": FLOAT64, + "bool": BOOL, "array(string)": ARRAY_STRING, "array(int64)": ARRAY_INT64, "array(float64)": ARRAY_FLOAT64, + "array(bool)": ARRAY_BOOL, } var arrayValueTypeMapping = map[string]string{ "array(string)": "string", "array(int64)": "int64", "array(float64)": "float64", + "array(bool)": "bool", } var jsonLogOperators = map[v3.FilterOperator]string{ @@ -74,7 +79,7 @@ func getJSONFilterKey(key v3.AttributeKey, isArray bool) (string, error) { // for non array keyname := fmt.Sprintf("JSON_VALUE(%s, '$.%s')", keyArr[0], strings.Join(keyArr[1:], ".")) - if dataType != "String" { + if dataType != STRING { keyname = fmt.Sprintf("JSONExtract(%s, '%s')", keyname, dataType) } diff --git a/pkg/query-service/app/logs/v3/json_filter_test.go b/pkg/query-service/app/logs/v3/json_filter_test.go index 6509bf5d16..455d705b1d 100644 --- a/pkg/query-service/app/logs/v3/json_filter_test.go +++ b/pkg/query-service/app/logs/v3/json_filter_test.go @@ -74,6 +74,16 @@ var testGetJSONFilterKeyData = []struct { IsArray: true, ClickhouseKey: "JSONExtract(JSON_QUERY(body, '$.nested_num[*].float_nums[*]'), '" + ARRAY_FLOAT64 + "')", }, + { + Name: "Array Bool", + Key: v3.AttributeKey{ + Key: "body.boolarray[*]", + DataType: "array(bool)", + IsJSON: true, + }, + IsArray: true, + ClickhouseKey: "JSONExtract(JSON_QUERY(body, '$.boolarray[*]'), '" + ARRAY_BOOL + "')", + }, { Name: "String", Key: v3.AttributeKey{ @@ -104,6 +114,16 @@ var testGetJSONFilterKeyData = []struct { IsArray: false, ClickhouseKey: "JSONExtract(JSON_VALUE(body, '$.fraction'), '" + FLOAT64 + "')", }, + { + Name: "Bool", + Key: v3.AttributeKey{ + Key: "body.boolkey", + DataType: "bool", + IsJSON: true, + }, + IsArray: false, + ClickhouseKey: "JSONExtract(JSON_VALUE(body, '$.boolkey'), '" + BOOL + "')", + }, } func TestGetJSONFilterKey(t *testing.T) { @@ -165,6 +185,19 @@ var testGetJSONFilterData = []struct { }, Filter: "NOT has(JSONExtract(JSON_QUERY(body, '$.nested_num[*].float_nums[*]'), '" + ARRAY_FLOAT64 + "'), 2.200000)", }, + { + Name: "Array membership bool", + FilterItem: v3.FilterItem{ + Key: v3.AttributeKey{ + Key: "body.bool[*]", + DataType: "array(bool)", + IsJSON: true, + }, + Operator: "has", + Value: true, + }, + Filter: "has(JSONExtract(JSON_QUERY(body, '$.bool[*]'), '" + ARRAY_BOOL + "'), true)", + }, { Name: "eq operator", FilterItem: v3.FilterItem{ @@ -204,6 +237,19 @@ var testGetJSONFilterData = []struct { }, Filter: "JSONExtract(JSON_VALUE(body, '$.status'), '" + FLOAT64 + "') = 1.100000", }, + { + Name: "eq operator bool", + FilterItem: v3.FilterItem{ + Key: v3.AttributeKey{ + Key: "body.boolkey", + DataType: "bool", + IsJSON: true, + }, + Operator: "=", + Value: true, + }, + Filter: "JSONExtract(JSON_VALUE(body, '$.boolkey'), '" + BOOL + "') = true", + }, { Name: "greater than operator", FilterItem: v3.FilterItem{ diff --git a/pkg/query-service/model/v3/v3.go b/pkg/query-service/model/v3/v3.go index a8c03066b8..039ea1c672 100644 --- a/pkg/query-service/model/v3/v3.go +++ b/pkg/query-service/model/v3/v3.go @@ -242,6 +242,7 @@ const ( AttributeKeyDataTypeArrayString AttributeKeyDataType = "array(string)" AttributeKeyDataTypeArrayInt64 AttributeKeyDataType = "array(int64)" AttributeKeyDataTypeArrayFloat64 AttributeKeyDataType = "array(float64)" + AttributeKeyDataTypeArrayBool AttributeKeyDataType = "array(bool)" ) func (q AttributeKeyDataType) Validate() error { @@ -297,7 +298,7 @@ func (a AttributeKey) CacheKey() string { func (a AttributeKey) Validate() error { switch a.DataType { - case AttributeKeyDataTypeBool, AttributeKeyDataTypeInt64, AttributeKeyDataTypeFloat64, AttributeKeyDataTypeString, AttributeKeyDataTypeArrayFloat64, AttributeKeyDataTypeArrayString, AttributeKeyDataTypeArrayInt64, AttributeKeyDataTypeUnspecified: + case AttributeKeyDataTypeBool, AttributeKeyDataTypeInt64, AttributeKeyDataTypeFloat64, AttributeKeyDataTypeString, AttributeKeyDataTypeArrayFloat64, AttributeKeyDataTypeArrayString, AttributeKeyDataTypeArrayInt64, AttributeKeyDataTypeArrayBool, AttributeKeyDataTypeUnspecified: break default: return fmt.Errorf("invalid attribute dataType: %s", a.DataType)