From a1bf8486becf2e91e34eb8adc89c55308ed15ba6 Mon Sep 17 00:00:00 2001 From: Muhammad Luthfi Fahlevi Date: Thu, 22 Aug 2024 14:06:46 +0700 Subject: [PATCH] feat: add case for MemberNode which handle nested query --- pkg/queryexpr/es_expr.go | 2 ++ pkg/queryexpr/es_expr_test.go | 6 ++++++ pkg/queryexpr/sql_expr.go | 8 ++++++++ pkg/queryexpr/sql_expr_test.go | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/pkg/queryexpr/es_expr.go b/pkg/queryexpr/es_expr.go index c177cf77..40b743ef 100644 --- a/pkg/queryexpr/es_expr.go +++ b/pkg/queryexpr/es_expr.go @@ -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) } diff --git a/pkg/queryexpr/es_expr_test.go b/pkg/queryexpr/es_expr_test.go index b8a7ad37..c66b98c9 100644 --- a/pkg/queryexpr/es_expr_test.go +++ b/pkg/queryexpr/es_expr_test.go @@ -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")`), diff --git a/pkg/queryexpr/sql_expr.go b/pkg/queryexpr/sql_expr.go index 23cf40b8..388ce2f4 100644 --- a/pkg/queryexpr/sql_expr.go +++ b/pkg/queryexpr/sql_expr.go @@ -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) } diff --git a/pkg/queryexpr/sql_expr_test.go b/pkg/queryexpr/sql_expr_test.go index 2ca2e7a9..d289d43b 100644 --- a/pkg/queryexpr/sql_expr_test.go +++ b/pkg/queryexpr/sql_expr_test.go @@ -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")`),