Skip to content

Commit 0b666f1

Browse files
add structs to mv query cache key (#6818)
* add structs to mv query cache key * test refactor * better test * comment * time decode hook * Update runtime/metricsview/query.go Co-authored-by: Benjamin Egelund-Müller <[email protected]> * use inbuilt string to time decode hook --------- Co-authored-by: Benjamin Egelund-Müller <[email protected]>
1 parent d4db62e commit 0b666f1

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

runtime/metricsview/query.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package metricsview
22

33
import (
44
"fmt"
5+
"reflect"
56
"time"
67

8+
"github.com/mitchellh/mapstructure"
79
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
810
"github.com/rilldata/rill/runtime/pkg/timeutil"
911
)
@@ -54,6 +56,33 @@ type MeasureCompute struct {
5456
URI *MeasureComputeURI `mapstructure:"uri"`
5557
}
5658

59+
func (q *Query) AsMap() (map[string]any, error) {
60+
queryMap := make(map[string]any)
61+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
62+
Result: &queryMap,
63+
DecodeHook: func(from reflect.Type, to reflect.Type, data any) (any, error) {
64+
if from == reflect.TypeOf(&time.Time{}) {
65+
t, ok := data.(*time.Time)
66+
if !ok {
67+
return nil, fmt.Errorf("expected *time.Time, got %T", data)
68+
}
69+
return map[string]any{
70+
"t": t.Format(time.RFC3339Nano),
71+
}, nil
72+
}
73+
return data, nil
74+
},
75+
})
76+
if err != nil {
77+
return nil, err
78+
}
79+
err = decoder.Decode(q)
80+
if err != nil {
81+
return nil, err
82+
}
83+
return queryMap, nil
84+
}
85+
5786
func (m *MeasureCompute) Validate() error {
5887
n := 0
5988
if m.Count {
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mapstructureutil
22

33
import (
4-
"reflect"
54
"time"
65

76
"github.com/mitchellh/mapstructure"
@@ -10,7 +9,7 @@ import (
109
// WeakDecode is similar to mapstructure.WeakDecode, but it also supports decoding RFC3339Nano-formatted timestamp strings to time.Time.
1110
func WeakDecode(input, output any) error {
1211
config := &mapstructure.DecoderConfig{
13-
DecodeHook: stringToTimeHook,
12+
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339Nano),
1413
Metadata: nil,
1514
Result: output,
1615
WeaklyTypedInput: true,
@@ -23,11 +22,3 @@ func WeakDecode(input, output any) error {
2322

2423
return decoder.Decode(input)
2524
}
26-
27-
func stringToTimeHook(from, to reflect.Type, data any) (any, error) {
28-
if to == reflect.TypeOf(time.Time{}) && from == reflect.TypeOf("") {
29-
return time.Parse(time.RFC3339Nano, data.(string))
30-
}
31-
32-
return data, nil
33-
}

runtime/resolvers/metrics.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"time"
1010

11-
"github.com/mitchellh/mapstructure"
1211
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
1312
"github.com/rilldata/rill/runtime"
1413
"github.com/rilldata/rill/runtime/metricsview"
@@ -100,15 +99,15 @@ func (r *metricsResolver) CacheKey(ctx context.Context) ([]byte, bool, error) {
10099
return nil, false, nil
101100
}
102101

103-
queryMap := make(map[string]any)
104-
err = mapstructure.Decode(r.query, &queryMap)
102+
queryMap, err := r.query.AsMap()
105103
if err != nil {
106104
return nil, false, err
107105
}
108106

109107
queryMap["mv_cache_key"] = key
110-
bytes, err := json.Marshal(queryMap)
111-
return bytes, true, err
108+
109+
b, err := json.Marshal(queryMap)
110+
return b, true, err
112111
}
113112

114113
func (r *metricsResolver) Refs() []*runtimev1.ResourceName {

runtime/resolvers/testdata/metrics_comparisons.yaml

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ tests:
147147
- sum: 4
148148
sum_prev: 2
149149
time__day: "2024-01-04T00:00:00Z"
150+
- name: time_as_dimension_check_cache_duckdb
151+
resolver: metrics
152+
properties:
153+
metrics_view: duckdb_metrics
154+
dimensions:
155+
- name: time__day
156+
compute:
157+
time_floor:
158+
dimension: time
159+
grain: day
160+
measures:
161+
- name: sum
162+
- name: sum_prev
163+
compute:
164+
comparison_value:
165+
measure: sum
166+
time_range:
167+
end: 2024-01-04T00:00:00Z
168+
start: 2024-01-03T00:00:00Z
169+
comparison_time_range:
170+
end: 2024-01-02T00:00:00Z
171+
start: 2024-01-01T00:00:00Z
172+
sort:
173+
- name: time__day
174+
result:
175+
- sum: 3
176+
sum_prev: 1
177+
time__day: "2024-01-03T00:00:00Z"
150178
- name: iso_ranges_duckdb
151179
resolver: metrics
152180
properties:
@@ -167,9 +195,9 @@ tests:
167195
sort:
168196
- name: sum
169197
result:
170-
- sum: 7
198+
- country: US
199+
sum: 7
171200
sum_prev: 2
172-
country: "US"
173201
- name: iso_ranges_clickhouse
174202
resolver: metrics
175203
properties:
@@ -190,9 +218,9 @@ tests:
190218
sort:
191219
- name: sum
192220
result:
193-
- sum: 7
221+
- country: US
222+
sum: 7
194223
sum_prev: 2
195-
country: "US"
196224
- name: rill_time_ranges_duckdb
197225
resolver: metrics
198226
properties:
@@ -212,12 +240,12 @@ tests:
212240
sort:
213241
- name: sum
214242
result:
215-
- sum: 5
243+
- country: DK
244+
sum: 5
216245
sum_prev: 1
217-
country: "DK"
218-
- sum: 7
246+
- country: US
247+
sum: 7
219248
sum_prev: 2
220-
country: "US"
221249
- name: rill_time_ranges_clickhouse
222250
resolver: metrics
223251
properties:
@@ -237,9 +265,9 @@ tests:
237265
sort:
238266
- name: sum
239267
result:
240-
- sum: 5
268+
- country: DK
269+
sum: 5
241270
sum_prev: 1
242-
country: "DK"
243-
- sum: 7
271+
- country: US
272+
sum: 7
244273
sum_prev: 2
245-
country: "US"

0 commit comments

Comments
 (0)