Skip to content

Commit

Permalink
feat: add condition when the complex query result is time and refacto…
Browse files Browse the repository at this point in the history
…r codes
  • Loading branch information
Muhammad Luthfi Fahlevi committed Aug 21, 2024
1 parent f2ab1c3 commit a550154
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions pkg/queryexpr/query_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package queryexpr
import (
"fmt"
"strings"
"time"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
Expand Down Expand Up @@ -89,5 +90,9 @@ func GetQueryExprResult(fn string) (any, error) {
return nil, fmt.Errorf("failed to evaluate function '%s': %w", fn, err)
}

if t, ok := result.(time.Time); ok {
return t.Format(time.RFC3339), nil
}

return result, nil
}
23 changes: 16 additions & 7 deletions pkg/queryexpr/sql_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ func (s SQLExpr) convertToSQL(node ast.Node, stringBuilder *strings.Builder) err
return err
}
case *ast.BuiltinNode, *ast.ConditionalNode:
result, err := GetQueryExprResult(n.String())
if err != nil {
if err := s.getQueryExprResult(n.String(), stringBuilder); err != nil {
return err
}
fmt.Fprintf(stringBuilder, "%v", result)
default:
return s.unsupportedQueryError(n)
}
Expand All @@ -85,12 +83,10 @@ func (s SQLExpr) convertToSQL(node ast.Node, stringBuilder *strings.Builder) err

func (s SQLExpr) binaryNodeToSQLQuery(n *ast.BinaryNode, stringBuilder *strings.Builder) error {
operator := s.operatorToSQL(n)
if operator == "" { // most likely the node is operation
result, err := GetQueryExprResult(n.String())
if err != nil {
if operator == "" { // most likely the node is an operation
if err := s.getQueryExprResult(n.String(), stringBuilder); err != nil {
return err
}
fmt.Fprintf(stringBuilder, "%v", result)
} else {
stringBuilder.WriteString("(")
if err := s.convertToSQL(n.Left, stringBuilder); err != nil {
Expand All @@ -109,6 +105,19 @@ func (s SQLExpr) binaryNodeToSQLQuery(n *ast.BinaryNode, stringBuilder *strings.
return nil
}

func (SQLExpr) getQueryExprResult(fn string, stringBuilder *strings.Builder) error {
result, err := GetQueryExprResult(fn)
if err != nil {
return err
}
if str, ok := result.(string); ok {
result = fmt.Sprintf("'%s'", str)
}

fmt.Fprintf(stringBuilder, "%v", result)
return nil
}

func (s SQLExpr) arrayNodeToSQLQuery(n *ast.ArrayNode, stringBuilder *strings.Builder) error {
stringBuilder.WriteString("(")
for i := range n.Nodes {
Expand Down
8 changes: 8 additions & 0 deletions pkg/queryexpr/sql_expr_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package queryexpr_test

import (
"fmt"
"testing"
"time"

"github.com/goto/compass/pkg/queryexpr"
)
Expand Down Expand Up @@ -66,6 +68,12 @@ func TestSQLExpr_ToQuery(t *testing.T) {
want: `((bool_identifier = false) AND (name != 'John'))`,
wantErr: false,
},
{
name: "complex query expression that can directly produce a value regarding time",
expr: queryexpr.SQLExpr(`refreshed_at <= (now() - duration('1h'))`),
want: fmt.Sprintf("(refreshed_at <= '%s')", time.Now().Add(-1*time.Hour).Format(time.RFC3339)),
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 a550154

Please sign in to comment.