Schema returns a schema for the metrics view's dimensions and measures.
(ctx context.Context)
| 271 | |
| 272 | // Schema returns a schema for the metrics view's dimensions and measures. |
| 273 | func (e *Executor) Schema(ctx context.Context) (*runtimev1.StructType, error) { |
| 274 | if !e.security.CanAccess() { |
| 275 | return nil, runtime.ErrForbidden |
| 276 | } |
| 277 | |
| 278 | // Build a query that selects all dimensions and measures. |
| 279 | // |
| 280 | // NOTE: We don't give special treatment to time dimensions here because: |
| 281 | // 1. if we use TimeFloor on a time idmension, it might its actual type (e.g. from DATE to TIMESTAMP or vice versa), |
| 282 | // 2. we may not know all the time dimensions yet (that inference happens later based on the result from this query). |
| 283 | qry := &metricsview.Query{} |
| 284 | |
| 285 | for _, d := range e.metricsView.Dimensions { |
| 286 | if e.security.CanAccessField(d.Name) { |
| 287 | qry.Dimensions = append(qry.Dimensions, metricsview.Dimension{Name: d.Name}) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | for _, m := range e.metricsView.Measures { |
| 292 | if e.security.CanAccessField(m.Name) { |
| 293 | qry.Measures = append(qry.Measures, metricsview.Measure{Name: m.Name}) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Setting both base and comparison time ranges in case there are time_comparison measures. |
| 298 | // Do not set it for BigQuery because it requires datatype to be already discovered to set time parameter. |
| 299 | if e.olap.Dialect().String() != drivers.DialectNameBigQuery && e.metricsView.TimeDimension != "" { |
| 300 | now := time.Now() |
| 301 | qry.TimeRange = &metricsview.TimeRange{ |
| 302 | Start: now.Add(-time.Second), |
| 303 | End: now, |
| 304 | } |
| 305 | qry.ComparisonTimeRange = &metricsview.TimeRange{ |
| 306 | Start: now.Add(-2 * time.Second), |
| 307 | End: now.Add(-time.Second), |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Importantly, limit to 0 rows |
| 312 | zero := int64(0) |
| 313 | qry.Limit = &zero |
| 314 | |
| 315 | // Execute the query to get the schema |
| 316 | ast, err := metricsview.NewAST(e.metricsView, e.security, qry, e.olap.Dialect()) |
| 317 | if err != nil { |
| 318 | return nil, err |
| 319 | } |
| 320 | |
| 321 | sql, args, err := ast.SQL() |
| 322 | if err != nil { |
| 323 | return nil, err |
| 324 | } |
| 325 | |
| 326 | schema, err := e.olap.QuerySchema(ctx, sql, args) |
| 327 | if err != nil { |
| 328 | return nil, err |
| 329 | } |
| 330 |
no test coverage detected