feat: json filter bool support (#3544)

* feat: json filter bool support

* fix: update json filter for bool
This commit is contained in:
Nityananda Gohain 2023-09-13 21:00:40 +05:30 committed by GitHub
parent a8c4a91001
commit ed809474d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 2 deletions

View File

@ -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)
}

View File

@ -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{

View File

@ -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)