ResolveDimension returns a dimension spec for the given dimension query. If the dimension query specifies a computed dimension, it constructs a dimension spec to match it.
(qd Dimension, visible bool)
| 363 | // ResolveDimension returns a dimension spec for the given dimension query. |
| 364 | // If the dimension query specifies a computed dimension, it constructs a dimension spec to match it. |
| 365 | func (a *AST) ResolveDimension(qd Dimension, visible bool) (*runtimev1.MetricsViewSpec_Dimension, error) { |
| 366 | // Handle regular dimension |
| 367 | if qd.Compute == nil { |
| 368 | return a.LookupDimension(qd.Name, visible) |
| 369 | } |
| 370 | |
| 371 | // Handle computed dimension. This means "compute.time_floor" must be configured. |
| 372 | |
| 373 | if qd.Compute.TimeFloor == nil { |
| 374 | return nil, errors.New(`unsupported "compute"`) |
| 375 | } |
| 376 | |
| 377 | if !qd.Compute.TimeFloor.Grain.Valid() { |
| 378 | return nil, errors.New(`invalid "grain"`) |
| 379 | } |
| 380 | if qd.Compute.TimeFloor.Grain == TimeGrainUnspecified { |
| 381 | return nil, errors.New(`"grain" must be specified for time floor`) |
| 382 | } |
| 383 | |
| 384 | dim, err := a.LookupDimension(qd.Compute.TimeFloor.Dimension, visible) |
| 385 | if err != nil { |
| 386 | return nil, err |
| 387 | } |
| 388 | |
| 389 | if qd.Name != qd.Compute.TimeFloor.Dimension { |
| 390 | err := a.checkNameForComputedField(qd.Name) |
| 391 | if err != nil { |
| 392 | return nil, err |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | grain := qd.Compute.TimeFloor.Grain.ToProto() |
| 397 | expr, err := a.Dialect.DateTruncExpr(dim, grain, a.Query.TimeZone, int(a.MetricsView.FirstDayOfWeek), int(a.MetricsView.FirstMonthOfYear)) |
| 398 | if err != nil { |
| 399 | return nil, fmt.Errorf(`failed to compute time floor: %w`, err) |
| 400 | } |
| 401 | |
| 402 | displayName := dim.DisplayName |
| 403 | if displayName == "" { |
| 404 | displayName = qd.Name |
| 405 | } |
| 406 | displayName = fmt.Sprintf("%s (%s)", displayName, qd.Compute.TimeFloor.Grain) |
| 407 | |
| 408 | return &runtimev1.MetricsViewSpec_Dimension{ |
| 409 | Name: qd.Name, |
| 410 | Expression: expr, |
| 411 | DisplayName: displayName, |
| 412 | Unnest: dim.Unnest, |
| 413 | }, nil |
| 414 | } |
| 415 | |
| 416 | // ResolveMeasure returns a measure spec for the given measure query. |
| 417 | // If the measure query specifies a computed measure, it constructs a measure spec to match it. |
no test coverage detected