Search executes the provided query against the metrics view.
(ctx context.Context, qry *metricsview.SearchQuery, executionTime *time.Time)
| 560 | |
| 561 | // Search executes the provided query against the metrics view. |
| 562 | func (e *Executor) Search(ctx context.Context, qry *metricsview.SearchQuery, executionTime *time.Time) ([]metricsview.SearchResult, error) { |
| 563 | if !e.security.CanAccess() { |
| 564 | return nil, runtime.ErrForbidden |
| 565 | } |
| 566 | |
| 567 | // Generate a metricsview.Query and build a AST |
| 568 | // This is a hacky implementation since both metricsview.Query and AST are designed for aggregate queries. |
| 569 | // TODO :: Refactor the code and extract common functionality from metricsview.Query and AST and write SearchQuery to underlying SQL/Native druid query directly. |
| 570 | |
| 571 | if e.olap.Dialect().String() == drivers.DialectNameDruid { |
| 572 | // native search |
| 573 | res, err := e.executeSearchInDruid(ctx, qry, executionTime) |
| 574 | if err == nil || !errors.Is(err, errDruidNativeSearchUnimplemented) { |
| 575 | return res, err |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | var ( |
| 580 | finalSQL strings.Builder |
| 581 | finalArgs []any |
| 582 | rowsCap int64 |
| 583 | ) |
| 584 | for i, d := range qry.Dimensions { |
| 585 | if i > 0 { |
| 586 | finalSQL.WriteString(" UNION ALL ") |
| 587 | } |
| 588 | q := &metricsview.Query{ |
| 589 | MetricsView: qry.MetricsView, |
| 590 | Dimensions: []metricsview.Dimension{{Name: d, Compute: nil}}, |
| 591 | Measures: nil, |
| 592 | PivotOn: nil, |
| 593 | Spine: nil, |
| 594 | Sort: nil, |
| 595 | TimeRange: qry.TimeRange, |
| 596 | ComparisonTimeRange: nil, |
| 597 | Where: nil, |
| 598 | Having: qry.Having, |
| 599 | Limit: qry.Limit, |
| 600 | Offset: nil, |
| 601 | TimeZone: "", |
| 602 | UseDisplayNames: false, |
| 603 | Rows: false, |
| 604 | QueryLimits: &metricsview.QueryLimits{ |
| 605 | RequireTimeRange: false, |
| 606 | MaxTimeRangeDays: 0, // not enforced |
| 607 | }, |
| 608 | UnusedFields: nil, |
| 609 | } //exhaustruct:enforce |
| 610 | q.Where = whereExprForSearch(qry.Where, d, qry.Search) |
| 611 | |
| 612 | err := e.RewriteQueryTimeRanges(ctx, q, executionTime) |
| 613 | if err != nil { |
| 614 | return nil, err |
| 615 | } |
| 616 | |
| 617 | rowsCap, err = e.rewriteQueryEnforceCaps(q) |
| 618 | if err != nil { |
| 619 | return nil, err |
no test coverage detected