From 24f61caca9147c3646b3c10c80dd11cf9cdce02a Mon Sep 17 00:00:00 2001 From: bowen xiao Date: Tue, 24 Sep 2024 14:20:12 -0700 Subject: [PATCH] Pinot handle customer keyword type empty val (#6302) * add query to handle empty keyword val --- common/pinot/pinotQueryValidator.go | 16 ++++++++++++---- common/pinot/pinotQueryValidator_test.go | 10 +++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/common/pinot/pinotQueryValidator.go b/common/pinot/pinotQueryValidator.go index e5316708b1b..ec6024126f3 100644 --- a/common/pinot/pinotQueryValidator.go +++ b/common/pinot/pinotQueryValidator.go @@ -445,12 +445,20 @@ func processEqual(colNameStr string, colValStr string) string { func processCustomKeyword(operator string, colNameStr string, colValStr string) string { // edge case if operator == "!=" { - return fmt.Sprintf("JSON_MATCH(Attr, '\"$.%s\"%s''%s''') and JSON_MATCH(Attr, '\"$.%s[*]\"%s''%s''')", - colNameStr, operator, colValStr, colNameStr, operator, colValStr) + return createKeywordQuery(operator, colNameStr, colValStr, "and", "NOT ") } - return fmt.Sprintf("(JSON_MATCH(Attr, '\"$.%s\"%s''%s''') or JSON_MATCH(Attr, '\"$.%s[*]\"%s''%s'''))", - colNameStr, operator, colValStr, colNameStr, operator, colValStr) + return createKeywordQuery(operator, colNameStr, colValStr, "or", "") +} + +func createKeywordQuery(operator string, colNameStr string, colValStr string, connector string, notEqual string) string { + if colValStr == "" { + // partial match for an empty string (still it will only match empty string) + // so it equals to exact match for an empty string + return createCustomStringQuery(colNameStr, colValStr, notEqual) + } + return fmt.Sprintf("(JSON_MATCH(Attr, '\"$.%s\"%s''%s''') %s JSON_MATCH(Attr, '\"$.%s[*]\"%s''%s'''))", + colNameStr, operator, colValStr, connector, colNameStr, operator, colValStr) } func processCustomString(operator string, colNameStr string, colValStr string) string { diff --git a/common/pinot/pinotQueryValidator_test.go b/common/pinot/pinotQueryValidator_test.go index ebf51d4fc0a..f6d0501c74b 100644 --- a/common/pinot/pinotQueryValidator_test.go +++ b/common/pinot/pinotQueryValidator_test.go @@ -115,7 +115,7 @@ func TestValidateQuery(t *testing.T) { }, "Case8-4: query with custom keyword field not equal": { query: "CustomKeywordField != 0", - validated: "JSON_MATCH(Attr, '\"$.CustomKeywordField\"!=''0''') and JSON_MATCH(Attr, '\"$.CustomKeywordField[*]\"!=''0''')", + validated: "(JSON_MATCH(Attr, '\"$.CustomKeywordField\"!=''0''') and JSON_MATCH(Attr, '\"$.CustomKeywordField[*]\"!=''0'''))", }, "Case9: invalid where expression": { query: "InvalidWhereExpr", @@ -359,6 +359,14 @@ func TestValidateQuery(t *testing.T) { query: "CustomStringField = 'abc' OR CustomStringField = 'def'", validated: "(JSON_MATCH(Attr, '\"$.CustomStringField\" is not null') AND JSON_MATCH(Attr, 'REGEXP_LIKE(\"$.CustomStringField\", ''.*abc.*'')') or JSON_MATCH(Attr, '\"$.CustomStringField\" is not null') AND JSON_MATCH(Attr, 'REGEXP_LIKE(\"$.CustomStringField\", ''.*def.*'')'))", }, + "case23-1: custom keyword field is empty case": { + query: "CustomKeywordField = ''", + validated: "JSON_MATCH(Attr, '\"$.CustomKeywordField\" is not null') AND JSON_MATCH(Attr, 'REGEXP_LIKE(\"$.CustomKeywordField\", ''^$'')')", + }, + "case23-2: custom keyword field is not empty case": { + query: "CustomKeywordField != ''", + validated: "JSON_MATCH(Attr, '\"$.CustomKeywordField\" is not null') AND NOT JSON_MATCH(Attr, 'REGEXP_LIKE(\"$.CustomKeywordField\", ''^$'')')", + }, } for name, test := range tests {