Skip to content

Commit

Permalink
Runtime : Parse metrics_sql to metricsview.query (#5346)
Browse files Browse the repository at this point in the history
* parse metrics_sql to metricsview.query

* fixing unit tests

* fixing unit tests

* lint fix

* is null fix

* add back the test case

* review comments
  • Loading branch information
k-anshul authored Jul 31, 2024
1 parent 7700971 commit bf67f80
Show file tree
Hide file tree
Showing 13 changed files with 749 additions and 717 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.5
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/golang-lru v0.6.0
github.com/itlightning/dateparse v0.2.0
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgtype v1.14.3
github.com/jackc/pgx/v4 v4.18.2
Expand Down Expand Up @@ -292,7 +293,7 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/shirou/gopsutil/v3 v3.23.11 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,8 @@ github.com/intel/goresctrl v0.2.0/go.mod h1:+CZdzouYFn5EsxgqAQTEzMfwKwuc0fVdMrT9
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
github.com/itlightning/dateparse v0.2.0 h1:eOYLGZORnHweKdTZGOVjDXHhOwMQTNdP4g6+ErgPyeg=
github.com/itlightning/dateparse v0.2.0/go.mod h1:W2PH6/Sq+PuJJ6JUgx2nau+ew1KLGXwoGP1A240x204=
github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA=
github.com/j-keck/arping v1.0.2/go.mod h1:aJbELhR92bSk7tp79AWM/ftfc90EfEi2bQJrbBFOsPw=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
Expand Down Expand Up @@ -1970,8 +1972,8 @@ github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTK
github.com/rilldata/arrow/go/v14 v14.0.0-20240624035703-e234e04219ff h1:Tt67B9BQVkymWsosWgz7vyz8MXnlYzc8xbqtxYuPU1s=
github.com/rilldata/arrow/go/v14 v14.0.0-20240624035703-e234e04219ff/go.mod h1:u3fgh3EdgN/YQ8cVQRguVW3R+seMybFg8QBQ5LU+eBY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a/go.mod h1:4r5QyqhjIWCcK8DO4KMclc5Iknq5qVBAlbYYzAbUScQ=
Expand Down
15 changes: 11 additions & 4 deletions runtime/metricsview/astexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,17 @@ func (b *sqlExprBuilder) writeBinaryConditionInner(left, right *Expression, left
return err
}
}
// Special case: "dim = NULL" should be written as "dim IS NULL"
if op == OperatorEq && hasNilValue(right) {
b.writeString(" IS NULL")
return nil
// Special cases:
// "dim = NULL" should be written as "dim IS NULL"
// dim != NULL" should be written as "dim IS NOT NULL"
if hasNilValue(right) {
if op == OperatorEq {
b.writeString(" IS NULL")
return nil
} else if op == OperatorNeq {
b.writeString(" IS NOT NULL")
return nil
}
}
b.writeString(joiner)
err := b.writeExpression(right)
Expand Down
7 changes: 6 additions & 1 deletion runtime/metricsview/query_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,18 @@ func (b exprStrBuilder) writeBinaryCondition(exprs []*Expression, op Operator) e

switch op {
case OperatorEq:
/// Special case: "dim = NULL" should be written as "dim IS NULL"
// Special case: "dim = NULL" should be written as "dim IS NULL"
if hasNilValue(right) {
b.writeString(" IS NULL")
return nil
}
b.writeString("=")
case OperatorNeq:
// Special case: "dim != NULL" should be written as "dim IS NOT NULL"
if hasNilValue(right) {
b.writeString(" IS NOT NULL")
return nil
}
b.writeString("!=")
case OperatorLt:
b.writeString("<")
Expand Down
30 changes: 30 additions & 0 deletions runtime/metricsview/query_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ func TestExpressionToString(t *testing.T) {
},
want: "foo OR (bar IS NULL) OR (baz=42)",
},
{
name: "or is not null expression",
e: &Expression{
Condition: &Condition{
Operator: OperatorOr,
Expressions: []*Expression{
{Name: "foo"},
{
Condition: &Condition{
Operator: OperatorNeq,
Expressions: []*Expression{
{Name: "bar"},
{Value: nil},
},
},
},
{
Condition: &Condition{
Operator: OperatorEq,
Expressions: []*Expression{
{Name: "baz"},
{Value: 42},
},
},
},
},
},
},
want: "foo OR (bar IS NOT NULL) OR (baz=42)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit bf67f80

Please sign in to comment.