From 3a2a4d9aa413c3b67927d270d0b44edd9fa6f118 Mon Sep 17 00:00:00 2001 From: bowen xiao Date: Wed, 27 Mar 2024 12:28:52 -0700 Subject: [PATCH] check time type in custom search attribute (#5822) * check time type in custom search attribute * clean up * add test * refactor * typo --- common/pinot/pinotQueryValidator.go | 9 +++++++-- common/pinot/pinotQueryValidator_test.go | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/common/pinot/pinotQueryValidator.go b/common/pinot/pinotQueryValidator.go index 0bae73e8e8..3386c2bcb0 100644 --- a/common/pinot/pinotQueryValidator.go +++ b/common/pinot/pinotQueryValidator.go @@ -334,12 +334,11 @@ func (qv *VisibilityQueryValidator) processCustomKey(expr sqlparser.Expr) (strin if !ok { return "", errors.New("invalid comparison expression, right") } - colValStr := string(colVal.Val) // get the value type indexValType := common.ConvertIndexedValueTypeToInternalType(valType, log.NewNoop()) - operator := comparisonExpr.Operator + colValStr := string(colVal.Val) switch indexValType { case types.IndexedValueTypeString: @@ -347,6 +346,12 @@ func (qv *VisibilityQueryValidator) processCustomKey(expr sqlparser.Expr) (strin case types.IndexedValueTypeKeyword: return processCustomKeyword(operator, colNameStr, colValStr), nil case types.IndexedValueTypeDatetime: + var err error + colVal, err = trimTimeFieldValueFromNanoToMilliSeconds(colVal) + if err != nil { + return "", fmt.Errorf("trim time field %s got error: %w", colNameStr, err) + } + colValStr := string(colVal.Val) return processCustomNum(operator, colNameStr, colValStr, "BIGINT"), nil case types.IndexedValueTypeDouble: return processCustomNum(operator, colNameStr, colValStr, "DOUBLE"), nil diff --git a/common/pinot/pinotQueryValidator_test.go b/common/pinot/pinotQueryValidator_test.go index fee9690bb8..fd4a99e07c 100644 --- a/common/pinot/pinotQueryValidator_test.go +++ b/common/pinot/pinotQueryValidator_test.go @@ -194,6 +194,19 @@ func TestValidateQuery(t *testing.T) { query: "CloseTime != missing and StartTime >= 1707662555754408145", validated: "CloseTime != -1 and StartTime >= 1707662555754", }, + "Case15-17: CustomDatetimeField with big int type case": { + query: "CustomDatetimeField = 1707319950000", + validated: "JSON_MATCH(Attr, '\"$.CustomDatetimeField\"=''1707319950000''')", + }, + "Case15-18: CustomDatetimeField with time.Time() type case": { + query: "CustomDatetimeField = '2024-02-07T15:32:30Z'", + validated: "JSON_MATCH(Attr, '\"$.CustomDatetimeField\"=''1707319950000''')", + }, + "Case15-19: CustomDatetimeField with error case": { + query: "CustomDatetimeField = 'test'", + validated: "", + err: "trim time field CustomDatetimeField got error: error: failed to parse int from SQLVal test", + }, "Case16-1: custom int attribute greater than or equal to": { query: "CustomIntField >= 0", validated: "(JSON_MATCH(Attr, '\"$.CustomIntField\" is not null') AND CAST(JSON_EXTRACT_SCALAR(Attr, '$.CustomIntField') AS INT) >= 0)",