Skip to content

Commit

Permalink
Update Pinot query validator to support "like" in queries (#6188)
Browse files Browse the repository at this point in the history
* Update Pinot query validator to support like in queries for partial match in system fields

* Update pinotQueryValidator_test.go

* update with text_match

* Update es_visibility_store_test.go

* update with quotes
  • Loading branch information
sankari165 authored Jul 30, 2024
1 parent 9a7a8a4 commit edf4861
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion common/persistence/elasticsearch/es_visibility_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ func (s *ESVisibilitySuite) TestListWorkflowExecutions() {
s.True(strings.Contains(input.Query, `{"match_phrase":{"CloseStatus":{"query":"5"}}}`))
s.Equal(esIndexMaxResultWindow, input.MaxResultWindow)
return true
})).Return(testSearchResult, nil).Once()
})).Return(testSearchResult, nil).Twice()

request := &p.ListWorkflowExecutionsByQueryRequest{
DomainUUID: testDomainID,
Expand All @@ -861,6 +861,16 @@ func (s *ESVisibilitySuite) TestListWorkflowExecutions() {
_, err := s.visibilityStore.ListWorkflowExecutions(ctx, request)
s.NoError(err)

requestWithLike := &p.ListWorkflowExecutionsByQueryRequest{
DomainUUID: testDomainID,
Domain: testDomain,
PageSize: 10,
Query: `CloseStatus like '5'`,
}

_, err = s.visibilityStore.ListWorkflowExecutions(ctx, requestWithLike)
s.NoError(err)

s.mockESClient.On("SearchByQuery", mock.Anything, mock.Anything).Return(nil, errTestESSearch).Once()
_, err = s.visibilityStore.ListWorkflowExecutions(ctx, request)
s.Error(err)
Expand Down
9 changes: 9 additions & 0 deletions common/pinot/pinotQueryValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ func (qv *VisibilityQueryValidator) processSystemKey(expr sqlparser.Expr) (strin
}
colNameStr := colName.Name.String()

if comparisonExpr.Operator == sqlparser.LikeStr {
colVal, ok := comparisonExpr.Right.(*sqlparser.SQLVal)
if !ok {
return "", fmt.Errorf("right comparison is invalid: %v", comparisonExpr.Right)
}
colValStr := string(colVal.Val)
return fmt.Sprintf("TEXT_MATCH(%s, '%s')", colNameStr, colValStr), nil
}

if comparisonExpr.Operator != sqlparser.EqualStr && comparisonExpr.Operator != sqlparser.NotEqualStr {
if _, ok := timeSystemKeys[colNameStr]; ok {
sqlVal, ok := comparisonExpr.Right.(*sqlparser.SQLVal)
Expand Down
13 changes: 12 additions & 1 deletion common/pinot/pinotQueryValidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ func TestValidateQuery(t *testing.T) {
query: "",
validated: "",
},
"Case2: simple query": {
"Case2-1: simple query": {
query: "WorkflowID = 'wid'",
validated: "WorkflowID = 'wid'",
},
"Case2-2: simple query with partial match": {
query: "WorkflowID like 'wid'",
validated: "TEXT_MATCH(WorkflowID, 'wid')",
},
"Case2-3: invalid simple query with partial match": {
query: "WorkflowID like wid",
err: "right comparison is invalid: &{<nil> wid { }}"},
"Case3-1: query with custom field": {
query: "CustomStringField = 'custom'",
validated: "(JSON_MATCH(Attr, '\"$.CustomStringField\" is not null') AND REGEXP_LIKE(JSON_EXTRACT_SCALAR(Attr, '$.CustomStringField', 'string'), 'custom*'))",
Expand Down Expand Up @@ -78,6 +85,10 @@ func TestValidateQuery(t *testing.T) {
query: "WorkflowID = 'wid' and (CustomStringField = 'custom and custom2 or custom3 order by' or CustomIntField between 1 and 10)",
validated: "WorkflowID = 'wid' and ((JSON_MATCH(Attr, '\"$.CustomStringField\" is not null') AND REGEXP_LIKE(JSON_EXTRACT_SCALAR(Attr, '$.CustomStringField', 'string'), 'custom and custom2 or custom3 order by*')) or (JSON_MATCH(Attr, '\"$.CustomIntField\" is not null') AND CAST(JSON_EXTRACT_SCALAR(Attr, '$.CustomIntField') AS INT) >= 1 AND CAST(JSON_EXTRACT_SCALAR(Attr, '$.CustomIntField') AS INT) <= 10))",
},
"Case6-5: complex query with partial match": {
query: "RunID like '123' or WorkflowID like '123'",
validated: "(TEXT_MATCH(RunID, '123') or TEXT_MATCH(WorkflowID, '123'))",
},
"Case7: invalid sql query": {
query: "Invalid SQL",
err: "Invalid query: syntax error at position 38 near 'sql'",
Expand Down

0 comments on commit edf4861

Please sign in to comment.