-
Notifications
You must be signed in to change notification settings - Fork 201
Escape aliases in metrics view search fallback query (value is reserved in Druid SQL)
#9891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bbc28a8
Escape aliases in metrics view search fallback query (`value` is rese…
nishantmonu51 c21aa4f
exclude time dim from search, fix search on non string types
pjain1 6ba3c20
fix numeric search not working
pjain1 c554721
fix search not visible on cold load of explore
pjain1 4c18df3
test
pjain1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
runtime/metricsview/executor/executor_search_druid_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package executor | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1" | ||
| "github.com/rilldata/rill/runtime" | ||
| "github.com/rilldata/rill/runtime/drivers" | ||
| "github.com/rilldata/rill/runtime/drivers/druid" | ||
| "github.com/rilldata/rill/runtime/metricsview" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // errSQLCaptured is returned by sqlCaptureOLAP.Query so that a test can stop the executor right after the SQL is generated. | ||
| var errSQLCaptured = errors.New("sql captured") | ||
|
|
||
| // sqlCaptureOLAP is a stub OLAP store that reports the Druid dialect and records the statement passed to Query instead of executing it. | ||
| type sqlCaptureOLAP struct { | ||
| drivers.OLAPStore | ||
| stmt *drivers.Statement | ||
| } | ||
|
|
||
| func (o *sqlCaptureOLAP) Dialect() drivers.Dialect { return druid.DialectDruid } | ||
|
|
||
| func (o *sqlCaptureOLAP) Query(_ context.Context, stmt *drivers.Statement) (*drivers.Result, error) { | ||
| o.stmt = stmt | ||
| return nil, errSQLCaptured | ||
| } | ||
|
|
||
| // TestSearchDruidFallbackSQL checks the full UNION ALL query that Search issues to Druid when native search is unavailable (here because the query has no time range). | ||
| // The outer aliases must be quoted since "value" is a reserved keyword in Druid (Calcite) SQL. | ||
| // The per-dimension inner queries are covered separately by TestSearchDruidSQLCastsNonStringDimensions. | ||
| func TestSearchDruidFallbackSQL(t *testing.T) { | ||
| olap := &sqlCaptureOLAP{} | ||
| e := &Executor{ | ||
| metricsView: &runtimev1.MetricsViewSpec{ | ||
| Table: "events", | ||
| Dimensions: []*runtimev1.MetricsViewSpec_Dimension{ | ||
| {Name: "publisher", Column: "publisher"}, | ||
| {Name: "domain", Column: "domain"}, | ||
| }, | ||
| }, | ||
| security: runtime.ResolvedSecurityOpen, | ||
| olap: olap, | ||
| } | ||
|
|
||
| _, err := e.Search(context.Background(), &metricsview.SearchQuery{ | ||
| MetricsView: "mv", | ||
| Dimensions: []string{"publisher", "domain"}, | ||
| Search: "oo", | ||
| }, nil) | ||
| require.ErrorIs(t, err, errSQLCaptured) | ||
| require.NotNil(t, olap.stmt) | ||
| require.Equal(t, `SELECT 'publisher' AS "dimension", "publisher" AS "value" FROM (SELECT ("publisher") AS "publisher" FROM "events" WHERE (REGEXP_LIKE(("publisher"), ?)) GROUP BY 1) UNION ALL SELECT 'domain' AS "dimension", "domain" AS "value" FROM (SELECT ("domain") AS "domain" FROM "events" WHERE (REGEXP_LIKE(("domain"), ?)) GROUP BY 1)`, olap.stmt.Query) | ||
| require.Equal(t, []any{"^(?i).*oo.*$", "^(?i).*oo.*$"}, olap.stmt.Args) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package executor_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1" | ||
| "github.com/rilldata/rill/runtime" | ||
| "github.com/rilldata/rill/runtime/drivers/druid" | ||
| "github.com/rilldata/rill/runtime/metricsview" | ||
| "github.com/rilldata/rill/runtime/metricsview/executor" | ||
| "github.com/rilldata/rill/runtime/testruntime" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| _ "github.com/rilldata/rill/runtime/resolvers" | ||
| ) | ||
|
|
||
| func TestSearch(t *testing.T) { | ||
| rt, instanceID := testruntime.NewInstanceWithOptions(t, testruntime.InstanceOptions{ | ||
| Files: map[string]string{ | ||
| "rill.yaml": "", | ||
| "models/events.sql": ` | ||
| SELECT * FROM (VALUES | ||
| (TIMESTAMP '2024-01-01 00:00:00', 'Google', 'news.com'), | ||
| (TIMESTAMP '2024-01-02 00:00:00', 'Facebook', 'sports.com'), | ||
| (TIMESTAMP '2024-01-03 00:00:00', 'Microsoft', 'foo.com'), | ||
| (TIMESTAMP '2024-02-01 00:00:00', 'Yahoo', 'news.com') | ||
| ) t(timestamp, publisher, domain) | ||
| `, | ||
| "metrics_views/mv.yaml": ` | ||
| type: metrics_view | ||
| version: 1 | ||
| model: events | ||
| timeseries: timestamp | ||
| dimensions: | ||
| - name: publisher | ||
| column: publisher | ||
| - name: domain | ||
| column: domain | ||
| measures: | ||
| - name: count | ||
| expression: count(*) | ||
| `, | ||
| }, | ||
| }) | ||
| testruntime.RequireReconcileState(t, rt, instanceID, 3, 0, 0) | ||
|
|
||
| r := testruntime.GetResource(t, rt, instanceID, runtime.ResourceKindMetricsView, "mv") | ||
| mv := r.GetMetricsView().State.ValidSpec | ||
| require.NotNil(t, mv) | ||
|
|
||
| e, err := executor.New(context.Background(), rt, instanceID, mv, false, runtime.ResolvedSecurityOpen, 0, nil) | ||
| require.NoError(t, err) | ||
| defer e.Close() | ||
|
|
||
| t.Run("multiple dimensions", func(t *testing.T) { | ||
| res, err := e.Search(context.Background(), &metricsview.SearchQuery{ | ||
| MetricsView: "mv", | ||
| Dimensions: []string{"publisher", "domain"}, | ||
| Search: "oo", | ||
| }, nil) | ||
| require.NoError(t, err) | ||
| require.ElementsMatch(t, []metricsview.SearchResult{ | ||
| {Dimension: "publisher", Value: "Google"}, | ||
| {Dimension: "publisher", Value: "Facebook"}, | ||
| {Dimension: "publisher", Value: "Yahoo"}, | ||
| {Dimension: "domain", Value: "foo.com"}, | ||
| }, res) | ||
| }) | ||
|
|
||
| t.Run("with where and time range", func(t *testing.T) { | ||
| res, err := e.Search(context.Background(), &metricsview.SearchQuery{ | ||
| MetricsView: "mv", | ||
| Dimensions: []string{"publisher", "domain"}, | ||
| Search: "oo", | ||
| Where: &metricsview.Expression{Condition: &metricsview.Condition{ | ||
| Operator: metricsview.OperatorNeq, | ||
| Expressions: []*metricsview.Expression{{Name: "domain"}, {Value: "sports.com"}}, | ||
| }}, | ||
| TimeRange: &metricsview.TimeRange{ | ||
| Start: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), | ||
| End: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC), | ||
| }, | ||
| }, nil) | ||
| require.NoError(t, err) | ||
| require.ElementsMatch(t, []metricsview.SearchResult{ | ||
| {Dimension: "publisher", Value: "Google"}, | ||
| {Dimension: "domain", Value: "foo.com"}, | ||
| }, res) | ||
| }) | ||
| } | ||
|
|
||
| // TestSearchDruidSQLCastsNonStringDimensions checks the per-dimension SQL that the search fallback (UNION ALL) query generates for Druid. | ||
| // Druid has no ILIKE, so the search is compiled to REGEXP_LIKE, which only accepts string operands: | ||
| // dimensions with a known non-string type (including the time dimension) must be cast to VARCHAR, while string dimensions and dimensions of unknown type are matched directly. | ||
| func TestSearchDruidSQLCastsNonStringDimensions(t *testing.T) { | ||
| mv := &runtimev1.MetricsViewSpec{ | ||
| Table: "events", | ||
| TimeDimension: "__time", | ||
| Dimensions: []*runtimev1.MetricsViewSpec_Dimension{ | ||
| {Name: "__time", Column: "__time", DataType: &runtimev1.Type{Code: runtimev1.Type_CODE_TIMESTAMP}}, | ||
| {Name: "account_id", Column: "account_id", DataType: &runtimev1.Type{Code: runtimev1.Type_CODE_INT64}}, | ||
| {Name: "account_name", Column: "account_name", DataType: &runtimev1.Type{Code: runtimev1.Type_CODE_STRING}}, | ||
| {Name: "domain", Column: "domain"}, | ||
| }, | ||
| } | ||
|
|
||
| cases := map[string]string{ | ||
| "__time": `SELECT ("__time") AS "__time" FROM "events" WHERE (REGEXP_LIKE(CAST(("__time") AS VARCHAR), ?)) GROUP BY 1`, | ||
| "account_id": `SELECT ("account_id") AS "account_id" FROM "events" WHERE (REGEXP_LIKE(CAST(("account_id") AS VARCHAR), ?)) GROUP BY 1`, | ||
| "account_name": `SELECT ("account_name") AS "account_name" FROM "events" WHERE (REGEXP_LIKE(("account_name"), ?)) GROUP BY 1`, | ||
| "domain": `SELECT ("domain") AS "domain" FROM "events" WHERE (REGEXP_LIKE(("domain"), ?)) GROUP BY 1`, | ||
| } | ||
| for dim, want := range cases { | ||
| t.Run(dim, func(t *testing.T) { | ||
| qry := &metricsview.Query{ | ||
| MetricsView: "mv", | ||
| Dimensions: []metricsview.Dimension{{Name: dim}}, | ||
| Where: &metricsview.Expression{Condition: &metricsview.Condition{ | ||
| Operator: metricsview.OperatorIlike, | ||
| Expressions: []*metricsview.Expression{{Name: dim}, {Value: "%tvc%"}}, | ||
| }}, | ||
| } | ||
| ast, err := metricsview.NewAST(mv, runtime.ResolvedSecurityOpen, qry, druid.DialectDruid) | ||
| require.NoError(t, err) | ||
|
|
||
| sql, args, err := ast.SQL() | ||
| require.NoError(t, err) | ||
| require.Equal(t, want, sql) | ||
| require.Equal(t, []any{"^(?i).*tvc.*$"}, args) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.