Skip to content

Commit

Permalink
Apply sum() only when having is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Dec 14, 2023
1 parent a828601 commit b0b4a36
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
32 changes: 21 additions & 11 deletions runtime/queries/metricsview_comparison_toplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"fmt"
"io"
"strings"
// Load IANA time zone data
_ "time/tzdata"

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/pkg/pbutil"
"google.golang.org/protobuf/types/known/structpb"

// Load IANA time zone data
_ "time/tzdata"
)

type MetricsViewComparison struct {
Expand All @@ -32,7 +33,7 @@ type MetricsViewComparison struct {
ResolvedMVSecurity *runtime.ResolvedMetricsViewSecurity `json:"security"`
Exact bool `json:"exact"`

// TODO: backwards compatibility
// backwards compatibility
Filter *runtimev1.MetricsViewFilter `json:"filter"`

Result *runtimev1.MetricsViewComparisonResponse `json:"-"`
Expand Down Expand Up @@ -461,14 +462,23 @@ func (q *MetricsViewComparison) buildMetricsComparisonTopListSQL(mv *runtimev1.M
var columnsTuple string
var labelTuple string
if dialect != drivers.DialectDruid {
// TODO: only add `sum()` only if having is enabled
columnsTuple = fmt.Sprintf(
"sum(base.%[1]s) as %[1]s, sum(comparison.%[1]s) AS %[2]s, sum(base.%[1]s - comparison.%[1]s) AS %[3]s, sum((base.%[1]s - comparison.%[1]s)/comparison.%[1]s::DOUBLE) AS %[4]s",
safeName(m.Name),
safeName(m.Name+"__previous"),
safeName(m.Name+"__delta_abs"),
safeName(m.Name+"__delta_rel"),
)
if q.Having != nil {
columnsTuple = fmt.Sprintf(
"sum(base.%[1]s) as %[1]s, sum(comparison.%[1]s) AS %[2]s, sum(base.%[1]s - comparison.%[1]s) AS %[3]s, sum((base.%[1]s - comparison.%[1]s)/comparison.%[1]s::DOUBLE) AS %[4]s",
safeName(m.Name),
safeName(m.Name+"__previous"),
safeName(m.Name+"__delta_abs"),
safeName(m.Name+"__delta_rel"),
)
} else {
columnsTuple = fmt.Sprintf(
"base.%[1]s, comparison.%[1]s AS %[2]s, base.%[1]s - comparison.%[1]s AS %[3]s, (base.%[1]s - comparison.%[1]s)/comparison.%[1]s::DOUBLE AS %[4]s",
safeName(m.Name),
safeName(m.Name+"__previous"),
safeName(m.Name+"__delta_abs"),
safeName(m.Name+"__delta_rel"),
)
}
labelTuple = fmt.Sprintf(
"base.%[1]s AS %[5]s, comparison.%[1]s AS %[2]s, base.%[1]s - comparison.%[1]s AS %[3]s, (base.%[1]s - comparison.%[1]s)/comparison.%[1]s::DOUBLE AS %[4]s",
safeName(m.Name),
Expand Down
2 changes: 1 addition & 1 deletion runtime/queries/metricsview_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type MetricsViewRows struct {
MetricsView *runtimev1.MetricsViewSpec `json:"-"`
ResolvedMVSecurity *runtime.ResolvedMetricsViewSecurity `json:"security"`

// TODO: backwards compatibility
// backwards compatibility
Filter *runtimev1.MetricsViewFilter `json:"filter,omitempty"`

Result *runtimev1.MetricsViewRowsResponse `json:"-"`
Expand Down
2 changes: 1 addition & 1 deletion runtime/queries/metricsview_timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type MetricsViewTimeSeries struct {
MetricsView *runtimev1.MetricsViewSpec `json:"-"`
ResolvedMVSecurity *runtime.ResolvedMetricsViewSecurity `json:"security"`

// TODO: backwards compatibility
// backwards compatibility
Filter *runtimev1.MetricsViewFilter `json:"filter,omitempty"`

Result *runtimev1.MetricsViewTimeSeriesResponse `json:"-"`
Expand Down
2 changes: 1 addition & 1 deletion runtime/queries/metricsview_toplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type MetricsViewToplist struct {
MetricsView *runtimev1.MetricsViewSpec `json:"-"`
ResolvedMVSecurity *runtime.ResolvedMetricsViewSecurity `json:"security"`

// TODO: backwards compatibility
// backwards compatibility
Filter *runtimev1.MetricsViewFilter `json:"filter,omitempty"`

Result *runtimev1.MetricsViewToplistResponse `json:"-"`
Expand Down
2 changes: 1 addition & 1 deletion runtime/queries/metricsview_totals.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type MetricsViewTotals struct {
MetricsView *runtimev1.MetricsViewSpec `json:"-"`
ResolvedMVSecurity *runtime.ResolvedMetricsViewSecurity `json:"security"`

// TODO: backwards compatibility
// backwards compatibility
Filter *runtimev1.MetricsViewFilter `json:"filter,omitempty"`

Result *runtimev1.MetricsViewTotalsResponse `json:"-"`
Expand Down

0 comments on commit b0b4a36

Please sign in to comment.