diff --git a/runtime/queries/metricsview.go b/runtime/queries/metricsview.go index 70058a72a71..03631b036b4 100644 --- a/runtime/queries/metricsview.go +++ b/runtime/queries/metricsview.go @@ -164,7 +164,7 @@ func buildFilterClauseForMetricsViewFilter(mv *runtimev1.MetricsViewSpec, filter } if policy != nil && policy.RowFilter != "" { - clauses = append(clauses, "AND "+policy.RowFilter) + clauses = append(clauses, fmt.Sprintf("AND (%s)", policy.RowFilter)) } return strings.Join(clauses, " "), args, nil diff --git a/runtime/queries/metricsview_aggregation.go b/runtime/queries/metricsview_aggregation.go index 5185ca561d0..e4d65f573d7 100644 --- a/runtime/queries/metricsview_aggregation.go +++ b/runtime/queries/metricsview_aggregation.go @@ -210,14 +210,16 @@ func (q *MetricsViewAggregation) buildMetricsAggregationSQL(mv *runtimev1.Metric } whereClause += clause } - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) - if err != nil { - return "", nil, err - } - whereClause += " " + clause - args = append(args, clauseArgs...) + + filterClause, filterClauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) + if err != nil { + return "", nil, err + } + if filterClause != "" { + whereClause += " " + filterClause + args = append(args, filterClauseArgs...) } + if len(whereClause) > 0 { whereClause = "WHERE 1=1" + whereClause } diff --git a/runtime/queries/metricsview_comparison_toplist.go b/runtime/queries/metricsview_comparison_toplist.go index 29d365aadb3..c308c434d5a 100644 --- a/runtime/queries/metricsview_comparison_toplist.go +++ b/runtime/queries/metricsview_comparison_toplist.go @@ -291,14 +291,13 @@ func (q *MetricsViewComparison) buildMetricsTopListSQL(mv *runtimev1.MetricsView } baseWhereClause += trc - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) - if err != nil { - return "", nil, err - } - baseWhereClause += " " + clause - - args = append(args, clauseArgs...) + filterClause, filterClauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) + if err != nil { + return "", nil, err + } + if filterClause != "" { + baseWhereClause += " " + filterClause + args = append(args, filterClauseArgs...) } var orderClauses []string @@ -484,20 +483,20 @@ func (q *MetricsViewComparison) buildMetricsComparisonTopListSQL(mv *runtimev1.M td := safeName(mv.TimeDimension) + filterClause, filterClauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) + if err != nil { + return "", nil, err + } + trc, err := timeRangeClause(q.TimeRange, mv, dialect, td, &args) if err != nil { return "", nil, err } baseWhereClause += trc - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) - if err != nil { - return "", nil, err - } - baseWhereClause += " " + clause - - args = append(args, clauseArgs...) + if filterClause != "" { + baseWhereClause += " " + filterClause + args = append(args, filterClauseArgs...) } trc, err = timeRangeClause(q.ComparisonTimeRange, mv, dialect, td, &args) @@ -506,14 +505,9 @@ func (q *MetricsViewComparison) buildMetricsComparisonTopListSQL(mv *runtimev1.M } comparisonWhereClause += trc - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) - if err != nil { - return "", nil, err - } - comparisonWhereClause += " " + clause - - args = append(args, clauseArgs...) + if filterClause != "" { + comparisonWhereClause += " " + filterClause + args = append(args, filterClauseArgs...) } err = validateSort(q.Sort) diff --git a/runtime/queries/metricsview_rows.go b/runtime/queries/metricsview_rows.go index b38e8702b33..46678847fee 100644 --- a/runtime/queries/metricsview_rows.go +++ b/runtime/queries/metricsview_rows.go @@ -228,13 +228,13 @@ func (q *MetricsViewRows) buildMetricsRowsSQL(mv *runtimev1.MetricsViewSpec, dia } } - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) - if err != nil { - return "", nil, err - } - whereClause += " " + clause - args = append(args, clauseArgs...) + filterClause, filterClauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) + if err != nil { + return "", nil, err + } + if filterClause != "" { + whereClause += " " + filterClause + args = append(args, filterClauseArgs...) } sortingCriteria := make([]string, 0, len(q.Sort)) diff --git a/runtime/queries/metricsview_timeseries.go b/runtime/queries/metricsview_timeseries.go index ea2a2c8a617..3a5b6debcfa 100644 --- a/runtime/queries/metricsview_timeseries.go +++ b/runtime/queries/metricsview_timeseries.go @@ -277,13 +277,13 @@ func (q *MetricsViewTimeSeries) buildMetricsTimeseriesSQL(olap drivers.OLAPStore args = append(args, q.TimeEnd.AsTime()) } - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, olap.Dialect(), policy) - if err != nil { - return "", "", nil, err - } - whereClause += " " + clause - args = append(args, clauseArgs...) + filterClause, filterClauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, olap.Dialect(), policy) + if err != nil { + return "", "", nil, err + } + if filterClause != "" { + whereClause += " " + filterClause + args = append(args, filterClauseArgs...) } tsAlias := tempName("_ts_") diff --git a/runtime/queries/metricsview_toplist.go b/runtime/queries/metricsview_toplist.go index cbac2e637a3..844ac50e6e9 100644 --- a/runtime/queries/metricsview_toplist.go +++ b/runtime/queries/metricsview_toplist.go @@ -214,13 +214,13 @@ func (q *MetricsViewToplist) buildMetricsTopListSQL(mv *runtimev1.MetricsViewSpe } } - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) - if err != nil { - return "", nil, err - } - whereClause += " " + clause - args = append(args, clauseArgs...) + filterClause, filterClauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) + if err != nil { + return "", nil, err + } + if filterClause != "" { + whereClause += " " + filterClause + args = append(args, filterClauseArgs...) } sortingCriteria := make([]string, 0, len(q.Sort)) diff --git a/runtime/queries/metricsview_totals.go b/runtime/queries/metricsview_totals.go index f3279536ae9..e679cc88717 100644 --- a/runtime/queries/metricsview_totals.go +++ b/runtime/queries/metricsview_totals.go @@ -124,13 +124,13 @@ func (q *MetricsViewTotals) buildMetricsTotalsSQL(mv *runtimev1.MetricsViewSpec, } } - if q.Filter != nil { - clause, clauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) - if err != nil { - return "", nil, err - } - whereClause += " " + clause - args = append(args, clauseArgs...) + filterClause, filterClauseArgs, err := buildFilterClauseForMetricsViewFilter(mv, q.Filter, dialect, policy) + if err != nil { + return "", nil, err + } + if filterClause != "" { + whereClause += " " + filterClause + args = append(args, filterClauseArgs...) } sql := fmt.Sprintf(