Skip to main content

query_builder.go

query_builder.go - Overview

  1. Overview This file defines functions and data structures for building ClickHouse queries for logs based on user-defined filters, aggregations, and grouping. It handles the translation of high-level query parameters into executable SQL queries against ClickHouse.

  2. Detailed Documentation

AggregateOperatorToPercentile

  • Purpose: Maps aggregate operators (like P05, P90, P99) to their corresponding percentile values (0.05, 0.90, 0.99).
  • Type: map[v3.AggregateOperator]float64

AggregateOperatorToSQLFunc

  • Purpose: Maps aggregate operators (like Avg, Max, Min, Sum) to their corresponding SQL function names.
  • Type: map[v3.AggregateOperator]string

logOperators

  • Purpose: Maps filter operators (like Equal, NotEqual, LessThan) to their corresponding SQL operators.
  • Type: map[v3.FilterOperator]string

BODY

  • Purpose: Constant string representing the log body column name.
  • Type: const string
  • Value: "body"

GetClickhouseLogsColumnType

  • Purpose: Determines the ClickHouse column type (attributes, resources, scope) based on the attribute key type.
  • Parameters:
    • columnType (v3.AttributeKeyType): The attribute key type.
  • Returns:
    • string: The ClickHouse column type string.

getClickhouseLogsColumnDataType

  • Purpose: Determines the ClickHouse column data type (float64, int64, bool, string) based on the attribute key data type.
  • Parameters:
    • columnDataType (v3.AttributeKeyDataType): The attribute key data type.
  • Returns:
    • string: The ClickHouse data type string.

getClickhouseColumnName

  • Purpose: Constructs the ClickHouse column name based on the attribute key.
  • Parameters:
    • key (v3.AttributeKey): The attribute key.
  • Returns:
    • string: The ClickHouse column name.

getSelectLabels

  • Purpose: Constructs the select labels for the query based on the group by keys.
  • Parameters:
    • aggregatorOperator (v3.AggregateOperator): The aggregate operator.
    • groupBy ([]v3.AttributeKey): The group by attribute keys.
  • Returns:
    • string: Comma-separated select labels.

GetSelectKeys

  • Purpose: Extracts the keys from attribute keys and returns a comma-separated string.
  • Parameters:
    • aggregatorOperator (v3.AggregateOperator): The aggregate operator.
    • groupBy ([]v3.AttributeKey): The group by attribute keys.
  • Returns:
    • string: Comma-separated list of keys enclosed in backticks.

GetExistsNexistsFilter

  • Purpose: Constructs the filter for "exists" and "not exists" operators.
  • Parameters:
    • op (v3.FilterOperator): The filter operator (Exists or NotExists).
    • item (v3.FilterItem): The filter item.
  • Returns:
    • string: The filter string.

buildLogsTimeSeriesFilterQuery

  • Purpose: Builds the filter part of the query based on the filter set.
  • Parameters:
    • fs (*v3.FilterSet): The filter set.
    • groupBy ([]v3.AttributeKey): The group by attribute keys.
    • aggregateAttribute (v3.AttributeKey): The aggregate attribute key.
  • Returns:
    • string: The filter query string.
    • error: An error if any.

buildLogsQuery

  • Purpose: Builds the complete logs query based on the provided parameters.
  • Parameters:
    • panelType (v3.PanelType): The panel type.
    • start (int64): The start timestamp (epoch millisecond).
    • end (int64): The end timestamp (epoch millisecond).
    • step (int64): The step interval in seconds.
    • mq (*v3.BuilderQuery): The builder query object.
    • graphLimitQtype (string): The graph limit query type.
  • Returns:
    • string: The complete logs query.
    • error: An error if any.

buildLogsLiveTailQuery

  • Purpose: Builds the query for live tailing of logs.
  • Parameters:
    • mq (*v3.BuilderQuery): The builder query object.
  • Returns:
    • string: The live tail query.
    • error: An error if any.

groupBy

  • Purpose: Creates a comma-separated string of tags for the GROUP BY clause.
  • Parameters:
    • panelType (v3.PanelType): The panel type.
    • graphLimitQtype (string): The graph limit query type.
    • tags (...string): The tags to group by.
  • Returns:
    • string: The GROUP BY clause string.

GroupByAttributeKeyTags

  • Purpose: Transforms v3.AttributeKey tags into strings and calls the groupBy function.
  • Parameters:
    • panelType (v3.PanelType): The panel type.
    • graphLimitQtype (string): The graph limit query type.
    • tags (...v3.AttributeKey): The attribute keys to group by.
  • Returns:
    • string: The GROUP BY clause string.

orderBy

  • Purpose: Constructs the ORDER BY clause based on provided items and tag lookup.
  • Parameters:
    • panelType (v3.PanelType): The panel type.
    • items ([]v3.OrderBy): Order by items.
    • tagLookup (map[string]struct{}): A lookup for tags.
  • Returns:
    • []string: An array of order by strings.

orderByAttributeKeyTags

  • Purpose: Creates the ORDER BY clause string from attribute keys.
  • Parameters:
    • panelType (v3.PanelType): The panel type.
    • items ([]v3.OrderBy): Order by items.
    • tags ([]v3.AttributeKey): Attribute keys for tags.
  • Returns:
    • string: The ORDER BY clause string.

Having

  • Purpose: Builds the HAVING clause for the query.
  • Parameters:
    • items ([]v3.Having): The having items.
  • Returns:
    • string: The HAVING clause string.

ReduceQuery

  • Purpose: Adds a reduction operation to the query.
  • Parameters:
    • query (string): The base query.
    • reduceTo (v3.ReduceToOperator): The reduction operator (e.g., Last, Sum, Avg).
    • aggregateOperator (v3.AggregateOperator): The aggregate operator.
  • Returns:
    • string: The modified query with the reduction applied.
    • error: An error if the reduce operator is unsupported.

AddLimitToQuery

  • Purpose: Adds a LIMIT clause to the query.
  • Parameters:
    • query (string): The base query.
    • limit (uint64): The limit value.
  • Returns:
    • string: The modified query with the LIMIT clause.

AddOffsetToQuery

  • Purpose: Adds an OFFSET clause to the query.
  • Parameters:
    • query (string): The base query.
    • offset (uint64): The offset value.
  • Returns:
    • string: The modified query with the OFFSET clause.

IsOrderByTs

  • Purpose: Checks if the order by is on timestamp.
  • Parameters:
    • orderBy ([]v3.OrderBy): Array of order by clauses
  • Returns:
    • bool: true if ordering by timestamp, false otherwise

PrepareLogsQuery

  • Purpose: Orchestrates the building of the log query based on the provided options and parameters.
  • Parameters:
    • start (int64): The start timestamp (epoch millisecond).
    • end (int64): The end timestamp (epoch millisecond).
    • queryType (v3.QueryType): The query type.
    • panelType (v3.PanelType): The panel type.
    • mq (*v3.BuilderQuery): The builder query object.
    • options (v3.QBOptions): Query builder options.
  • Returns:
    • string: The final log query.
    • error: An error if any.
  1. Code Examples

Not applicable as code consists of query construction logic.

  1. Clarity and Accuracy

The documentation aims to accurately reflect the purpose and functionality of each function and data structure based on the provided code.

  1. Markdown & MDX Perfection

The documentation is formatted in Markdown.

  1. Edge Cases To Avoid Breaking MDX:

All characters are escaped correctly.

  1. Getting Started Relevance Include in Getting Started: NO