Skip to content
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

Adding support for defining filters on measures #3624

Merged
merged 26 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2f56587
Initial commit
AdityaHegde Dec 5, 2023
102ac29
Using enum for expression and adding to metricsview_aggregation
AdityaHegde Dec 6, 2023
ccfeb0e
Merge branch 'main' into adityahegde/filters-on-measures
AdityaHegde Dec 7, 2023
d439c54
Comparison in measure filters
AdityaHegde Dec 7, 2023
655ab2e
Adding to metricsview toplist
AdityaHegde Dec 7, 2023
4808486
Updated proto spec
AdityaHegde Dec 11, 2023
74bddbd
Add generic expression syntax
AdityaHegde Dec 12, 2023
3bc151a
Final proto draft
AdityaHegde Dec 13, 2023
d0d5228
Integrate and test where filter
AdityaHegde Dec 13, 2023
0f9c159
Integrate where clause everywhere
AdityaHegde Dec 13, 2023
171a7f2
Merge branch 'main' into adityahegde/filters-on-measures
AdityaHegde Dec 13, 2023
9d9b016
Adding some tests for having clauses
AdityaHegde Dec 13, 2023
aa1b379
Add backwards compatibility for filter
AdityaHegde Dec 14, 2023
e9ff93b
fix lint
AdityaHegde Dec 14, 2023
a828601
Adding alias support in metricsview_comparison_toplist
AdityaHegde Dec 14, 2023
5a7526a
Apply sum() only when having is enabled
AdityaHegde Dec 14, 2023
38db0ff
Fix lint
AdityaHegde Dec 14, 2023
0ddcba5
PR comments
AdityaHegde Dec 15, 2023
f02b422
Move filter helpers to pkg/expressionpb
AdityaHegde Dec 15, 2023
a4086af
refactor filter builder to not use map
AdityaHegde Dec 15, 2023
1e49865
Backwards compatibility for older sort types
AdityaHegde Dec 15, 2023
0d02580
PR comments
AdityaHegde Dec 15, 2023
025a62d
Testing on druid and fixes
AdityaHegde Dec 18, 2023
b5754b3
Merge branch 'main' into adityahegde/filters-on-measures
AdityaHegde Dec 18, 2023
d1186d1
Using outer query
AdityaHegde Dec 18, 2023
1832ebf
Removing unused testdata
AdityaHegde Dec 18, 2023
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
Prev Previous commit
Next Next commit
Comparison in measure filters
  • Loading branch information
AdityaHegde committed Dec 7, 2023
commit d439c54851fb411de2b6cb0ffb6efbe00a30f217
2,442 changes: 1,242 additions & 1,200 deletions proto/gen/rill/runtime/v1/queries.pb.go

Large diffs are not rendered by default.

370 changes: 276 additions & 94 deletions proto/gen/rill/runtime/v1/queries.pb.validate.go

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions proto/rill/runtime/v1/queries.proto
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,31 @@
}

message MeasureFilter {
MeasureFilterExpression expression = 1;
}

message MeasureFilterNode {
oneof entry {
MeasureFilterEntry MeasureFilterEntry = 1;
MeasureFilterMeasure MeasureFilterMeasure = 1;

Check failure on line 488 in proto/rill/runtime/v1/queries.proto

View workflow job for this annotation

GitHub Actions / check

Field name "MeasureFilterMeasure" should be lower_snake_case, such as "measure_filter_measure".
MeasureFilterExpression MeasureFilterExpression = 2;

Check failure on line 489 in proto/rill/runtime/v1/queries.proto

View workflow job for this annotation

GitHub Actions / check

Field name "MeasureFilterExpression" should be lower_snake_case, such as "measure_filter_expression".
google.protobuf.Value Value = 3;

Check failure on line 490 in proto/rill/runtime/v1/queries.proto

View workflow job for this annotation

GitHub Actions / check

Field name "Value" should be lower_snake_case, such as "value".
MeasureFilterNode MeasureFilterNode = 4;

Check failure on line 491 in proto/rill/runtime/v1/queries.proto

View workflow job for this annotation

GitHub Actions / check

Field name "MeasureFilterNode" should be lower_snake_case, such as "measure_filter_node".
}
}

message MeasureFilterEntry {
message MeasureFilterMeasure {
enum ColumnType {
COLUMN_TYPE_UNSPECIFIED = 0;
COLUMN_TYPE_PREVIOUS = 1;
COLUMN_TYPE_DELTA_ABSOLUTE = 2;
COLUMN_TYPE_DELTA_RELATIVE = 3;
}

MetricsViewAggregationMeasure measure = 1;
ColumnType column_type = 2;
}

message MeasureFilterExpression {
enum OperationType {
OPERATION_TYPE_UNSPECIFIED = 0;
OPERATION_TYPE_EQUALS = 1;
Expand All @@ -501,23 +513,13 @@
OPERATION_TYPE_LESSER_OR_EQUALS = 4;
OPERATION_TYPE_GREATER = 5;
OPERATION_TYPE_GREATER_OR_EQUALS = 6;
OPERATION_TYPE_OR = 7;
OPERATION_TYPE_AND = 8;
OPERATION_TYPE_BETWEEN = 9;
}

MetricsViewAggregationMeasure measure = 1;
repeated MeasureFilterNode entries = 1;
OperationType operation_type = 2;
google.protobuf.Value value = 3;
ColumnType column_type = 4;
}

message MeasureFilterExpression {
enum Joiner {
JOINER_UNSPECIFIED = 0;
JOINER_OR = 2;
JOINER_AND = 3;
}

repeated MeasureFilter entries = 1;
Joiner joiner = 2;
}

// **********
Expand Down
142 changes: 99 additions & 43 deletions runtime/queries/metricsview.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,69 +292,125 @@
return fmt.Sprintf("AND (%s) ", condsClause), args, nil
}

func buildHavingClause(filter *runtimev1.MeasureFilter, mv *runtimev1.MetricsViewSpec) (string, []any, error) {
func buildHavingClause(filter *runtimev1.MeasureFilter, mv *runtimev1.MetricsViewSpec, hasComparison bool) (string, []any, error) {
if filter.Expression == nil {
return "", []any{}, nil
}
return buildHavingClauseFromExpression(filter.Expression, mv, hasComparison)
}

func buildHavingClauseFromNode(node *runtimev1.MeasureFilterNode, mv *runtimev1.MetricsViewSpec, hasComparison bool) (string, []any, error) {
sql := ""
args := make([]any, 0)
switch e := filter.Entry.(type) {
case *runtimev1.MeasureFilter_MeasureFilterEntry:
switch e.MeasureFilterEntry.Measure.BuiltinMeasure {
case runtimev1.BuiltinMeasure_BUILTIN_MEASURE_UNSPECIFIED:
expr, err := metricsViewMeasureExpression(mv, e.MeasureFilterEntry.Measure.Name)
if err != nil {
return "", args, err
}
sql = fmt.Sprintf(`%s %s ?`, expr, measureFilterClauseOperation(e.MeasureFilterEntry))
arg, err := pbutil.FromValue(e.MeasureFilterEntry.Value)
if err != nil {
return "", args, err
}
args = append(args, arg)
// TODO: comparison
switch e := node.Entry.(type) {
case *runtimev1.MeasureFilterNode_MeasureFilterMeasure:
expr, subArgs, err := buildHavingClauseFromMeasure(e.MeasureFilterMeasure, mv, hasComparison)
if err != nil {
return "", args, err
}
args = append(args, subArgs...)
sql += expr

case runtimev1.BuiltinMeasure_BUILTIN_MEASURE_COUNT:
//TODO: impl
case runtimev1.BuiltinMeasure_BUILTIN_MEASURE_COUNT_DISTINCT:
//TODO: impl
case *runtimev1.MeasureFilterNode_MeasureFilterExpression:
expr, subArgs, err := buildHavingClauseFromExpression(e.MeasureFilterExpression, mv, hasComparison)
if err != nil {
return "", args, err
}
args = append(args, subArgs...)
sql += expr

case *runtimev1.MeasureFilter_MeasureFilterExpression:
exprs := make([]string, len(e.MeasureFilterExpression.Entries))
for i, e := range e.MeasureFilterExpression.Entries {
expr, subArgs, err := buildHavingClause(e, mv)
if err != nil {
return "", args, err
case *runtimev1.MeasureFilterNode_Value:
arg, err := pbutil.FromValue(e.Value)
if err != nil {
return "", args, err
}
args = append(args, arg)
sql += "?"
}

return sql, args, nil
}

func buildHavingClauseFromMeasure(measure *runtimev1.MeasureFilterMeasure, mv *runtimev1.MetricsViewSpec, hasComparison bool) (string, []any, error) {
expr := ""
args := make([]any, 0)
switch measure.Measure.BuiltinMeasure {
case runtimev1.BuiltinMeasure_BUILTIN_MEASURE_UNSPECIFIED:
if measure.ColumnType != runtimev1.MeasureFilterMeasure_COLUMN_TYPE_UNSPECIFIED && !hasComparison {
return "", args, fmt.Errorf("comparison filter cannot be applied when it is not enabled/supported")
}

var ms *runtimev1.MetricsViewSpec_MeasureV2
for _, m := range mv.Measures {
if strings.EqualFold(m.Name, measure.Measure.Name) {
ms = m
break
}
args = append(args, subArgs...)
exprs[i] = expr
}
joiner := ""
switch e.MeasureFilterExpression.Joiner {
case runtimev1.MeasureFilterExpression_JOINER_UNSPECIFIED:
case runtimev1.MeasureFilterExpression_JOINER_OR:
joiner = " OR "
case runtimev1.MeasureFilterExpression_JOINER_AND:
joiner = " AND "
if ms == nil {
return "", args, fmt.Errorf("measure %s not found", measure.Measure.Name)
}

switch measure.ColumnType {
case runtimev1.MeasureFilterMeasure_COLUMN_TYPE_UNSPECIFIED:
expr += ms.Name
case runtimev1.MeasureFilterMeasure_COLUMN_TYPE_PREVIOUS:
expr += ms.Name + "__previous"
case runtimev1.MeasureFilterMeasure_COLUMN_TYPE_DELTA_ABSOLUTE:
expr += ms.Name + "__delta_abs"
case runtimev1.MeasureFilterMeasure_COLUMN_TYPE_DELTA_RELATIVE:
expr += ms.Name + "__delta_rel"
}
sql = strings.Join(exprs, joiner)

case runtimev1.BuiltinMeasure_BUILTIN_MEASURE_COUNT:
//TODO: impl

Check failure on line 366 in runtime/queries/metricsview.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
case runtimev1.BuiltinMeasure_BUILTIN_MEASURE_COUNT_DISTINCT:
//TODO: impl

Check failure on line 368 in runtime/queries/metricsview.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}
return expr, args, nil
}

func buildHavingClauseFromExpression(expr *runtimev1.MeasureFilterExpression, mv *runtimev1.MetricsViewSpec, hasComparison bool) (string, []any, error) {
args := make([]any, 0)
if len(expr.Entries) < 2 {
return "", args, fmt.Errorf("exactly 2 entries should be provided")
}

leftExpr, subArgs, err := buildHavingClauseFromNode(expr.Entries[0], mv, hasComparison)
if err != nil {
return "", args, err
}
args = append(args, subArgs...)
rightExpr, subArgs, err := buildHavingClauseFromNode(expr.Entries[1], mv, hasComparison)
if err != nil {
return "", args, err
}
args = append(args, subArgs...)

sql := fmt.Sprintf("(%s) %s (%s)", leftExpr, measureFilterClauseOperation(expr), rightExpr)
return sql, args, nil
}

func measureFilterClauseOperation(e *runtimev1.MeasureFilterEntry) string {
func measureFilterClauseOperation(e *runtimev1.MeasureFilterExpression) string {
switch e.OperationType {
case runtimev1.MeasureFilterEntry_OPERATION_TYPE_EQUALS:
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_EQUALS:
return "="
case runtimev1.MeasureFilterEntry_OPERATION_TYPE_NOT_EQUALS:
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_NOT_EQUALS:
return "!="
case runtimev1.MeasureFilterEntry_OPERATION_TYPE_LESSER:
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_LESSER:
return "<"
case runtimev1.MeasureFilterEntry_OPERATION_TYPE_LESSER_OR_EQUALS:
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_LESSER_OR_EQUALS:
return "<="
case runtimev1.MeasureFilterEntry_OPERATION_TYPE_GREATER:
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_GREATER:
return ">"
case runtimev1.MeasureFilterEntry_OPERATION_TYPE_GREATER_OR_EQUALS:
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_GREATER_OR_EQUALS:
return ">="
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_OR:
return "OR"
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_AND:
return "AND"
case runtimev1.MeasureFilterExpression_OPERATION_TYPE_BETWEEN:
return "BETWEEN"
}
return "=" // TODO: handle unknown operation type
AdityaHegde marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/queries/metricsview_aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (q *MetricsViewAggregation) buildMetricsAggregationSQL(mv *runtimev1.Metric
if q.MeasureFilter != nil {
var havingClauseArgs []any
var err error
havingClause, havingClauseArgs, err = buildHavingClause(q.MeasureFilter, mv)
havingClause, havingClauseArgs, err = buildHavingClause(q.MeasureFilter, mv, false)
if err != nil {
return "", nil, err
}
Expand Down
20 changes: 15 additions & 5 deletions runtime/queries/metricsview_aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ func TestMetricsViewAggregation_measure_filters(t *testing.T) {
},
Limit: &lmt,
MeasureFilter: &runtimev1.MeasureFilter{
Entry: &runtimev1.MeasureFilter_MeasureFilterEntry{
MeasureFilterEntry: &runtimev1.MeasureFilterEntry{
Measure: &runtimev1.MetricsViewAggregationMeasure{Name: "measure_1"},
OperationType: runtimev1.MeasureFilterEntry_OPERATION_TYPE_GREATER,
Value: structpb.NewNumberValue(3.25),
Expression: &runtimev1.MeasureFilterExpression{
Entries: []*runtimev1.MeasureFilterNode{
{
Entry: &runtimev1.MeasureFilterNode_MeasureFilterMeasure{
MeasureFilterMeasure: &runtimev1.MeasureFilterMeasure{
Measure: &runtimev1.MetricsViewAggregationMeasure{Name: "measure_1"},
},
},
},
{
Entry: &runtimev1.MeasureFilterNode_Value{
Value: structpb.NewNumberValue(3.25),
},
},
},
OperationType: runtimev1.MeasureFilterExpression_OPERATION_TYPE_GREATER,
},
},
}
Expand Down
18 changes: 16 additions & 2 deletions runtime/queries/metricsview_comparison_toplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"fmt"
"io"
"strings"
// Load IANA time zone data

Check failure on line 9 in runtime/queries/metricsview_comparison_toplist.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot --custom-order (gci)
_ "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"

Check failure on line 16 in runtime/queries/metricsview_comparison_toplist.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot --custom-order (gci)
)

type MetricsViewComparison struct {
Expand Down Expand Up @@ -304,7 +304,7 @@
havingClause := ""
if q.MeasureFilter != nil {
var havingClauseArgs []any
havingClause, havingClauseArgs, err = buildHavingClause(q.MeasureFilter, mv)
havingClause, havingClauseArgs, err = buildHavingClause(q.MeasureFilter, mv, false)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -440,7 +440,7 @@
var labelTuple string
if dialect != drivers.DialectDruid {
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",
"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"),
Expand Down Expand Up @@ -524,6 +524,17 @@
args = append(args, clauseArgs...)
}

havingClause := ""
if q.MeasureFilter != nil {
var havingClauseArgs []any
havingClause, havingClauseArgs, err = buildHavingClause(q.MeasureFilter, mv, true)
if err != nil {
return "", nil, err
}
havingClause = "HAVING " + havingClause
args = append(args, havingClauseArgs...)
}

err = validateSort(q.Sort)
if err != nil {
return "", nil, err
Expand Down Expand Up @@ -655,6 +666,8 @@
) comparison
ON
base.%[2]s = comparison.%[2]s OR (base.%[2]s is null and comparison.%[2]s is null)
GROUP BY 1
%[16]s
ORDER BY
%[6]s
%[7]s
Expand All @@ -676,6 +689,7 @@
comparisonLimitClause, // 13
unnestClause, // 14
groupByCol, // 15
havingClause, // 16
)
} else {
/*
Expand Down
Loading
Loading