Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docker/grafana/dashboards/adhoc_in_list_issue_941.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"uid": "issue-941-in-list",
"title": "issue 941 adhoc with IN list",
"editable": true,
"schemaVersion": 39,
"time": {"from": "now-6h", "to": "now"},
"templating": {
"list": [
{
"type": "adhoc",
"name": "adhoc",
"datasource": {"type": "vertamedia-clickhouse-datasource", "uid": "P7E099F39B84EA795"},
"filters": [{"key": "default.test_grafana.country", "operator": "=", "value": "US", "condition": ""}]
}
]
},
"panels": [
{
"id": 1,
"type": "table",
"title": "IN list with adhoc filter",
"datasource": {"type": "vertamedia-clickhouse-datasource", "uid": "P7E099F39B84EA795"},
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 0},
"targets": [
{
"refId": "A",
"datasource": {"type": "vertamedia-clickhouse-datasource", "uid": "P7E099F39B84EA795"},
"format": "table",
"editorMode": "sql",
"query": "SELECT service_name, count() AS c FROM default.test_grafana WHERE service_name IN ('mysql','postgresql') GROUP BY service_name ORDER BY service_name"
}
]
}
]
}
6 changes: 3 additions & 3 deletions pkg/eval/eval_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1726,11 +1726,11 @@ func (s *EvalQueryScanner) ToAST() (*EvalAST, error) {
}
if subAST.HasOwnProperty("root") && len(subAST.Obj["root"].(*EvalAST).Arr) > 0 {
var subArr = subAST.Obj["root"].(*EvalAST)
argument += " ("
items := make([]string, 0, len(subArr.Arr))
for _, item := range subArr.Arr {
argument += item.(string)
items = append(items, item.(string))
}
argument = argument + ")"
argument += " (" + strings.Join(items, ", ") + ")"
} else {
argument += " (" + newLine + PrintAST(subAST, tabSize) + ")"
if s.RootToken != "select" {
Expand Down
38 changes: 38 additions & 0 deletions pkg/eval/eval_query_regression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eval

import (
"strings"
"testing"
)

func TestScannerPreservesInListSeparators(t *testing.T) {
tests := []struct {
name string
query string
want string
}{
{
name: "string values",
query: "SELECT dt FROM default.messages WHERE severity IN ('EMERGENCY','ALERT','CRITICAL')",
want: "severity IN ('EMERGENCY', 'ALERT', 'CRITICAL')",
},
{
name: "numeric values",
query: "SELECT id FROM default.messages WHERE id IN (1,2,3)",
want: "id IN (1, 2, 3)",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scanner := NewScanner(tt.query)
formatted, err := scanner.Format()
if err != nil {
t.Fatalf("format query: %v", err)
}
if !strings.Contains(formatted, tt.want) {
t.Fatalf("formatted query %q does not contain %q", formatted, tt.want)
}
})
}
}
9 changes: 9 additions & 0 deletions pkg/resource_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ func TestHandleApplyAdhocFilters(t *testing.T) {
map[string]interface{}{"query": "SELECT x FROM default.test", "adhocFilters": filters, "target": target},
http.StatusOK, []string{"WHERE", "b = '2'"}, "",
},
{
"preserve IN list when injecting filters",
map[string]interface{}{
"query": "SELECT dt FROM default.messages WHERE severity IN ('EMERGENCY','ALERT','CRITICAL')",
"adhocFilters": []map[string]interface{}{{"key": "environment", "operator": "=", "value": "test"}},
"target": target,
},
http.StatusOK, []string{"severity IN ('EMERGENCY', 'ALERT', 'CRITICAL')", "(environment = 'test')"}, "",
},
{
"string value quoted",
map[string]interface{}{"query": "SELECT x FROM default.test", "adhocFilters": []map[string]interface{}{{"key": "b", "operator": "=", "value": "web"}}, "target": target},
Expand Down
10 changes: 10 additions & 0 deletions tests/testflows/tests/automated/adhoc_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ def adhoc_from_template_variable_issue_805(self):
)


@TestScenario
def adhoc_in_list_issue_941(self):
adhoc_dashboard_check_outline(
dashboard_name="issue 941 adhoc with IN list",
panels=[
("IN list with adhoc filter", ["service_name IN ('mysql', 'postgresql')", "country = 'US'"]),
],
)


@TestScenario
def adhoc_hide_table_names_issue_456(self):
"""Check that adhoc dashboard 'AdHoc with Hiding table name' shows correct SQL and data."""
Expand Down
Loading