Skip to content

Commit

Permalink
feat: add case for MemberNode which handle nested query
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Luthfi Fahlevi committed Aug 22, 2024
1 parent 0355599 commit a1bf848
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/queryexpr/es_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (e ESExpr) translateToEsQuery(node ast.Node) (interface{}, error) {
return nil, err
}
return result, nil
case *ast.MemberNode:
return n.String(), nil
default:
return nil, e.unsupportedQueryError(n)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/queryexpr/es_expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func TestESExpr_ToQuery(t *testing.T) {
time.Date(2024, 8, 21, 0, 0, 0, 0, time.UTC).Format(time.RFC3339)),
wantErr: false,
},
{
name: "nested column query",
expr: queryexpr.ESExpr(`foo.bar.abc.def == 'example'`),
want: `{"query":{"term":{"foo.bar.abc.def":"example"}}}`,
wantErr: false,
},
{
name: "complex query expression that can NOT directly produce a value",
expr: queryexpr.ESExpr(`service in filter(assets, .Service startsWith "T")`),
Expand Down
8 changes: 8 additions & 0 deletions pkg/queryexpr/sql_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ func (s SQLExpr) convertToSQL(node ast.Node, stringBuilder *strings.Builder) err
if err := s.getQueryExprResultForSQL(n.String(), stringBuilder); err != nil {
return err
}
case *ast.MemberNode:
memberIdentifiers := strings.Split(n.String(), ".")
identifier := memberIdentifiers[0]
for i := 1; i <= len(memberIdentifiers)-2; i++ {
identifier += fmt.Sprintf("->'%s'", memberIdentifiers[i])
}
identifier += fmt.Sprintf("->>'%s'", memberIdentifiers[len(memberIdentifiers)-1])
stringBuilder.WriteString(identifier)
default:
return s.unsupportedQueryError(n)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/queryexpr/sql_expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func TestSQLExpr_ToQuery(t *testing.T) {
want: fmt.Sprintf("(refreshed_at <= '%s')", time.Date(2024, 8, 21, 0, 0, 0, 0, time.UTC).Format(time.RFC3339)),
wantErr: false,
},
{
name: "nested column query",
expr: queryexpr.SQLExpr(`foo.bar.abc.def == 'example'`),
want: `(foo->'bar'->'abc'->>'def' = 'example')`,
wantErr: false,
},
{
name: "complex query expression that can NOT directly produce a value",
expr: queryexpr.SQLExpr(`service in filter(assets, .Service startsWith "T")`),
Expand Down

0 comments on commit a1bf848

Please sign in to comment.