Skip to main content

query_builder.go

query_builder.go - Overview

  1. Overview This file provides functionality to build ClickHouse queries for fetching and aggregating metric data based on the provided v3 query builder model. It supports various aggregation operators, grouping, filtering, and ordering options.

  2. Detailed Documentation

aggregateOperatorToPercentile

var aggregateOperatorToPercentile = map[v3.AggregateOperator]float64{
v3.AggregateOperatorP05: 0.05,
v3.AggregateOperatorP10: 0.10,
v3.AggregateOperatorP20: 0.20,
v3.AggregateOperatorP25: 0.25,
v3.AggregateOperatorP50: 0.50,
v3.AggregateOperatorP75: 0.75,
v3.AggregateOperatorP90: 0.90,
v3.AggregateOperatorP95: 0.95,
v3.AggregateOperatorP99: 0.99,
v3.AggregateOperatorHistQuant50: 0.50,
v3.AggregateOperatorHistQuant75: 0.75,
v3.AggregateOperatorHistQuant90: 0.90,
v3.AggregateOperatorHistQuant95: 0.95,
v3.AggregateOperatorHistQuant99: 0.99,
}
  • Purpose: Maps aggregate operators to their corresponding percentile values (e.g., P95 to 0.95).

aggregateOperatorToSQLFunc

var aggregateOperatorToSQLFunc = map[v3.AggregateOperator]string{
v3.AggregateOperatorAvg: "avg",
v3.AggregateOperatorMax: "max",
v3.AggregateOperatorMin: "min",
v3.AggregateOperatorSum: "sum",
v3.AggregateOperatorRateSum: "sum",
v3.AggregateOperatorRateAvg: "avg",
v3.AggregateOperatorRateMax: "max",
v3.AggregateOperatorRateMin: "min",
v3.AggregateOperatorSumRate: "sum",
v3.AggregateOperatorAvgRate: "avg",
v3.AggregateOperatorMaxRate: "max",
v3.AggregateOperatorMinRate: "min",
}
  • Purpose: Maps aggregate operators to their corresponding SQL function names (e.g., AggregateOperatorAvg to "avg").

rateWithoutNegative

var rateWithoutNegative = `If((value - lagInFrame(value, 1, 0) OVER rate_window) < 0, nan, If((ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window) >= 86400, nan, (value - lagInFrame(value, 1, 0) OVER rate_window) / (ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window))) `
  • Purpose: Defines a ClickHouse expression for calculating the rate of change, handling negative rate values and large time gaps by returning NaN.

buildMetricQuery

func buildMetricQuery(start, end, step int64, mq *v3.BuilderQuery) (string, error) {
// ...
}
  • Purpose: Constructs a ClickHouse query to fetch and aggregate metric data based on the provided parameters.
    • Parameters:
      • start (int64): Start timestamp in milliseconds.
      • end (int64): End timestamp in milliseconds.
      • step (int64): Step interval in seconds.
      • mq (*v3.BuilderQuery): Query parameters.
    • Returns:
      • (string): The generated ClickHouse query.
      • (error): An error if any occurred during query construction.

groupingSets

func groupingSets(tags ...string) string {
// ...
}
  • Purpose: Creates a comma-separated string of tags for the GROUP BY clause, always including ts.
    • Parameters:
      • tags (...string): Variable number of tag names.
    • Returns:
      • (string): Comma-separated string of tags, including "ts".

groupBy

func groupBy(tags ...string) string {
// ...
}
  • Purpose: Creates a comma-separated string of tags for the GROUP BY clause, always including ts.
    • Parameters:
      • tags (...string): Variable number of tag names.
    • Returns:
      • (string): Comma-separated string of tags, including "ts".

groupSelect

func groupSelect(tags ...string) string {
// ...
}
  • Purpose: Creates a comma-separated string of tags for the SELECT clause.
    • Parameters:
      • tags (...string): Variable number of tag names.
    • Returns:
      • (string): Comma-separated string of tags, with a trailing comma if tags are present.

groupingSetsByAttributeKeyTags

func groupingSetsByAttributeKeyTags(tags ...v3.AttributeKey) string {
// ...
}
  • Purpose: Extracts keys from AttributeKey and uses them to call groupingSets.
    • Parameters:
      • tags (...v3.AttributeKey): Variable number of AttributeKey structs.
    • Returns:
      • (string): Comma-separated string of tags, including "ts".

groupByAttributeKeyTags

func groupByAttributeKeyTags(tags ...v3.AttributeKey) string {
// ...
}
  • Purpose: Extracts keys from AttributeKey and uses them to call groupBy.
    • Parameters:
      • tags (...v3.AttributeKey): Variable number of AttributeKey structs.
    • Returns:
      • (string): Comma-separated string of tags, including "ts".

groupSelectAttributeKeyTags

func groupSelectAttributeKeyTags(tags ...v3.AttributeKey) string {
// ...
}
  • Purpose: Extracts keys from AttributeKey and uses them to call groupSelect.
    • Parameters:
      • tags (...v3.AttributeKey): Variable number of AttributeKey structs.
    • Returns:
      • (string): Comma-separated string of tags, with a trailing comma if tags are present.

orderBy

func orderBy(items []v3.OrderBy, tags []string) string {
// ...
}
  • Purpose: Generates an ORDER BY clause string based on provided order configurations and tags.
    • Parameters:
      • items ([]v3.OrderBy): Slice of OrderBy structs specifying the order.
      • tags ([]string): Slice of tag names to order by.
    • Returns:
      • (string): ORDER BY clause string.

orderByAttributeKeyTags

func orderByAttributeKeyTags(items []v3.OrderBy, tags []v3.AttributeKey) string {
// ...
}
  • Purpose: Extracts keys from AttributeKey and uses them to call orderBy.
    • Parameters:
      • items ([]v3.OrderBy): Slice of OrderBy structs specifying the order.
      • tags ([]v3.AttributeKey): Slice of AttributeKey structs.
    • Returns:
      • (string): ORDER BY clause string.

having

func having(items []v3.Having) string {
// ...
}
  • Purpose: Generates a HAVING clause string based on provided having conditions.
    • Parameters:
      • items ([]v3.Having): Slice of Having structs specifying the conditions.
    • Returns:
      • (string): HAVING clause string.

reduceQuery

func reduceQuery(query string, reduceTo v3.ReduceToOperator, aggregateOperator v3.AggregateOperator) (string, error) {
// ...
}
  • Purpose: Modifies the input query to reduce the results to a single value based on the specified ReduceToOperator.
    • Parameters:
      • query (string): The original query.
      • reduceTo (v3.ReduceToOperator): The operator used to reduce the results (e.g., Last, Sum, Avg).
      • aggregateOperator (v3.AggregateOperator): The aggregate operator used in the original query.
    • Returns:
      • (string): The modified query with reduction logic.
      • (error): An error if an unsupported ReduceToOperator is provided.

PrepareMetricQuery

func PrepareMetricQuery(start, end int64, queryType v3.QueryType, panelType v3.PanelType, mq *v3.BuilderQuery, options Options) (string, error) {
// ...
}
  • Purpose: Prepares the ClickHouse query for fetching metrics, applying filters, aggregations, and other transformations.
    • Parameters:
      • start (int64): Start timestamp in milliseconds.
      • end (int64): End timestamp in milliseconds.
      • queryType (v3.QueryType): Type of the query.
      • panelType (v3.PanelType): Type of the panel.
      • mq (*v3.BuilderQuery): Query parameters.
      • options (Options): Additional options.
    • Returns:
      • (string): The final ClickHouse query.
      • (error): An error if any occurred during query preparation.

BuildPromQuery

func BuildPromQuery(promQuery *v3.PromQuery, step, start, end int64) *model.QueryRangeParams {
// ...
}
  • Purpose: Constructs a QueryRangeParams struct suitable for querying Prometheus.
    • Parameters:
      • promQuery (*v3.PromQuery): The Prometheus query details.
      • step (int64): Step interval in seconds.
      • start (int64): Start timestamp in milliseconds.
      • end (int64): End timestamp in milliseconds.
    • Returns:
      • (*model.QueryRangeParams): A QueryRangeParams struct containing the query and time range.
  1. Code Examples

No examples are necessary as the code is self-explanatory.

Include in Getting Started: NO