Skip to content

Commit 21cabb2

Browse files
Fix decimal comparisons in metrics SQL filters (#9720)
1 parent 2c1972b commit 21cabb2

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

runtime/metricsview/metricssql/filter_parser.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package metricssql
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67
"time"
78

89
"github.com/itlightning/dateparse"
910
"github.com/pingcap/tidb/pkg/parser"
1011
"github.com/pingcap/tidb/pkg/parser/ast"
1112
"github.com/pingcap/tidb/pkg/parser/mysql"
13+
"github.com/pingcap/tidb/pkg/parser/test_driver"
1214
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
1315
"github.com/rilldata/rill/runtime/metricsview"
1416
)
@@ -434,6 +436,13 @@ func parseValueExpr(in ast.Node) (any, error) {
434436
val = int(actual) // Cast to plain int
435437
case uint64:
436438
val = int(actual) // Cast to plain int
439+
case *test_driver.MyDecimal:
440+
// Decimal literals arrive as an opaque struct that would serialize to an empty value; convert to float64
441+
f, err := strconv.ParseFloat(actual.String(), 64)
442+
if err != nil {
443+
return nil, fmt.Errorf("metrics sql: failed to parse decimal literal %q: %w", actual.String(), err)
444+
}
445+
val = f
437446
}
438447

439448
return val, nil

runtime/metricsview/metricssql/filter_parser_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,42 @@ func TestParseFilter(t *testing.T) {
6969
},
7070
false,
7171
},
72+
{
73+
"greater than decimal expression",
74+
"measure > 10.5",
75+
&metricsview.Expression{
76+
Condition: &metricsview.Condition{
77+
Operator: metricsview.OperatorGt,
78+
Expressions: []*metricsview.Expression{
79+
{
80+
Name: "measure",
81+
},
82+
{
83+
Value: 10.5,
84+
},
85+
},
86+
},
87+
},
88+
false,
89+
},
90+
{
91+
"in decimal expression",
92+
"measure IN (1.5, 2.5)",
93+
&metricsview.Expression{
94+
Condition: &metricsview.Condition{
95+
Operator: metricsview.OperatorIn,
96+
Expressions: []*metricsview.Expression{
97+
{
98+
Name: "measure",
99+
},
100+
{
101+
Value: []any{1.5, 2.5},
102+
},
103+
},
104+
},
105+
},
106+
false,
107+
},
72108
{
73109
"subquery expression with having",
74110
"dim IN (SELECT dim FROM mv HAVING count > 10)",

0 commit comments

Comments
 (0)