MCPcopy Create free account
hub / github.com/rilldata/rill / Query

Method Query

runtime/query.go:70–157  ·  view source on GitHub ↗
(ctx context.Context, instanceID string, query Query, priority int)

Source from the content-addressed store, hash-verified

68}
69
70func (r *Runtime) Query(ctx context.Context, instanceID string, query Query, priority int) error {
71 qk := query.Key()
72 // If key is empty, skip caching
73 if qk == "" {
74 return query.Resolve(ctx, r, instanceID, priority)
75 }
76
77 // Get dependency cache keys and optionally the underlying OLAP connector
78 ctrl, err := r.Controller(ctx, instanceID)
79 if err != nil {
80 return err
81 }
82 deps := query.Deps()
83 depKeys := make([]string, 0, len(deps))
84 for _, dep := range deps {
85 // Get the dependency resource
86 res, err := ctrl.Get(ctx, dep, false)
87 if err != nil {
88 // Deps are approximate, not exact (see docstring for Deps()), so they may not all exist
89 continue
90 }
91
92 // Add to cache key.
93 // Using StateUpdatedOn instead of StateVersion because the state version is reset when the resource is deleted and recreated.
94 key := fmt.Sprintf("%s:%s:%d:%d", res.Meta.Name.Kind, res.Meta.Name.Name, res.Meta.StateUpdatedOn.Seconds, res.Meta.StateUpdatedOn.Nanos/int32(time.Millisecond))
95 if mv := res.GetMetricsView(); mv != nil {
96 cacheKey, ok, err := r.metricsViewCacheKey(ctx, instanceID, res.Meta.Name.Name, priority)
97 if err != nil {
98 return err
99 }
100 if !ok {
101 // skip caching
102 return query.Resolve(ctx, r, instanceID, priority)
103 }
104 key = key + ":" + string(cacheKey)
105 }
106 depKeys = append(depKeys, key)
107 }
108
109 // If there were no known dependencies, skip caching
110 if len(depKeys) == 0 {
111 return query.Resolve(ctx, r, instanceID, priority)
112 }
113
114 // Build cache key
115 depKey := strings.Join(depKeys, ";")
116 key := queryCacheKey{
117 instanceID: instanceID,
118 queryKey: qk,
119 dependencyKey: depKey,
120 }.String()
121
122 // Try to get from cache
123 if val, ok := r.queryCache.cache.Get(key); ok {
124 observability.AddRequestAttributes(ctx, attribute.Bool("query.cache_hit", true))
125 return query.UnmarshalResult(val)
126 }
127 observability.AddRequestAttributes(ctx, attribute.Bool("query.cache_hit", false))

Callers

nothing calls this directly

Calls 15

ControllerMethod · 0.95
metricsViewCacheKeyMethod · 0.95
AddRequestAttributesFunction · 0.92
queryNameFunction · 0.85
DoMethod · 0.80
KeyMethod · 0.65
ResolveMethod · 0.65
DepsMethod · 0.65
GetMethod · 0.65
StringMethod · 0.65
UnmarshalResultMethod · 0.65
MarshalResultMethod · 0.65

Tested by

no test coverage detected