diff --git a/compose/repository/record_report_builder.go b/compose/repository/record_report_builder.go index 84d547c59..101f31aa1 100644 --- a/compose/repository/record_report_builder.go +++ b/compose/repository/record_report_builder.go @@ -150,7 +150,11 @@ func (b *recordReportBuilder) Build(metrics, dimensions, filters string) (sql st return } - b.report = b.report.Where(filter) + // We need to wrap this one level deeper, since additional filters should + // be evaluated as a whole. + // For example A AND B OR C =should be> (A AND B OR C) + // so the output becomes BASE AND (ADDITIONAL) + b.report = b.report.Where(ql.ASTNodes{filter}) } return b.report.ToSql() diff --git a/compose/repository/record_report_builder_test.go b/compose/repository/record_report_builder_test.go index 876305d2c..65eabd531 100644 --- a/compose/repository/record_report_builder_test.go +++ b/compose/repository/record_report_builder_test.go @@ -24,7 +24,7 @@ func TestRecordReportBuilder2(t *testing.T) { "FROM compose_record AS r " + "LEFT JOIN compose_record_value AS rv_single1 ON (rv_single1.record_id = r.id AND rv_single1.name = ? AND rv_single1.deleted_at IS NULL) " + "LEFT JOIN compose_record_value AS rv_ref1 ON (rv_ref1.record_id = r.id AND rv_ref1.name = ? AND rv_ref1.deleted_at IS NULL) " + - "WHERE r.deleted_at IS NULL AND r.module_id = ? AND rv_ref1.value = 2 " + + "WHERE r.deleted_at IS NULL AND r.module_id = ? AND (rv_ref1.value = 2) " + "GROUP BY dimension_0 " + "ORDER BY dimension_0"