Skip to main content

query_builder.go

query_builder.go - Overview

This file provides functions for building ClickHouse queries for traces based on user-defined filters, aggregations, and grouping. It supports various query types and panel types for data visualization.

Detailed Documentation

AggregateOperatorToPercentile map[v3.AggregateOperator]float64

var AggregateOperatorToPercentile = map[v3.AggregateOperator]float64{ ... }
  • Purpose: Maps aggregate operators to percentile values (e.g., P95 to 0.95).

AggregateOperatorToSQLFunc map[v3.AggregateOperator]string

var AggregateOperatorToSQLFunc = map[v3.AggregateOperator]string{ ... }
  • Purpose: Maps aggregate operators to corresponding SQL functions (e.g., Avg to "avg").

tracesOperatorMappingV3 map[v3.FilterOperator]string

var tracesOperatorMappingV3 = map[v3.FilterOperator]string{ ... }
  • Purpose: Maps filter operators to their SQL equivalents (e.g., Equal to "=").

getColumnName(key v3.AttributeKey) string

func getColumnName(key v3.AttributeKey) string { ... }
  • Purpose: Constructs the column name for a given attribute key, considering whether it's a column or a tag.
    • Parameters:
      • key: The v3.AttributeKey to construct the column name for.
    • Returns: The constructed column name as a string.

getClickhouseTracesColumnDataTypeAndType(key v3.AttributeKey) (v3.AttributeKeyType, string)

func getClickhouseTracesColumnDataTypeAndType(key v3.AttributeKey) (v3.AttributeKeyType, string) { ... }
  • Purpose: Determines the ClickHouse data type and type for a given attribute key.
    • Parameters:
      • key: The v3.AttributeKey to determine the data type and type for.
    • Returns:
      • v3.AttributeKeyType: The attribute key type.
      • string: The ClickHouse data type.

enrichKeyWithMetadata(key v3.AttributeKey, keys map[string]v3.AttributeKey) v3.AttributeKey

func enrichKeyWithMetadata(key v3.AttributeKey, keys map[string]v3.AttributeKey) v3.AttributeKey { ... }
  • Purpose: Enriches an attribute key with metadata from a map of existing keys.
    • Parameters:
      • key: The v3.AttributeKey to enrich.
      • keys: A map of existing v3.AttributeKey to look up metadata from.
    • Returns: The enriched v3.AttributeKey.

getSelectLabels(aggregatorOperator v3.AggregateOperator, groupBy []v3.AttributeKey) string

func getSelectLabels(aggregatorOperator v3.AggregateOperator, groupBy []v3.AttributeKey) string { ... }
  • Purpose: Generates the SELECT labels based on the aggregate operator and GROUP BY attributes.
    • Parameters:
      • aggregatorOperator: The aggregate operator to use.
      • groupBy: A slice of v3.AttributeKey to group by.
    • Returns: A string containing the SELECT labels.

GetSelectKeys(aggregatorOperator v3.AggregateOperator, groupBy []v3.AttributeKey) string

func GetSelectKeys(aggregatorOperator v3.AggregateOperator, groupBy []v3.AttributeKey) string { ... }
  • Purpose: Retrieves the select keys for the query based on groupBy and aggregateOperator
    • Parameters:
      • aggregatorOperator: The aggregate operator.
      • groupBy: A slice of v3.AttributeKey to group by.
    • Returns: A comma-separated string of select keys.

getSelectColumns(sc []v3.AttributeKey) string

func getSelectColumns(sc []v3.AttributeKey) string { ... }
  • Purpose: Constructs the SELECT columns string for a given slice of attribute keys.
    • Parameters:
      • sc: A slice of v3.AttributeKey to select.
    • Returns: A comma-separated string of SELECT columns.

getZerosForEpochNano(epoch int64) int64

func getZerosForEpochNano(epoch int64) int64 { ... }
  • Purpose: Calculates the number of zeros to append to an epoch timestamp to convert it to nanoseconds.
    • Parameters:
      • epoch: The epoch timestamp.
    • Returns: The number of zeros to append.

buildTracesFilterQuery(fs *v3.FilterSet) (string, error)

func buildTracesFilterQuery(fs *v3.FilterSet) (string, error) { ... }
  • Purpose: Builds the filter query for traces based on a filter set.
    • Parameters:
      • fs: The v3.FilterSet to build the filter query from.
    • Returns:
      • string: The filter query string.
      • error: An error, if any.

ExistsSubQueryForFixedColumn(key v3.AttributeKey, op v3.FilterOperator) (string, error)

func ExistsSubQueryForFixedColumn(key v3.AttributeKey, op v3.FilterOperator) (string, error) { ... }
  • Purpose: Generates subquery for checking existence of values in fixed columns.
    • Parameters:
      • key: The v3.AttributeKey representing the column.
      • op: The v3.FilterOperator (Exists or NotExists).
    • Returns:
      • string: The generated subquery.
      • error: An error if the operation is unsupported.

handleEmptyValuesInGroupBy(groupBy []v3.AttributeKey) (string, error)

func handleEmptyValuesInGroupBy(groupBy []v3.AttributeKey) (string, error) { ... }
  • Purpose: Handles empty values in group by attributes by adding filters.
    • Parameters:
      • groupBy: A slice of v3.AttributeKey representing the group by attributes.
    • Returns:
      • string: The filter query string.
      • error: An error, if any.

buildTracesQuery(start, end, step int64, mq *v3.BuilderQuery, _ string, panelType v3.PanelType, options v3.QBOptions) (string, error)

func buildTracesQuery(start, end, step int64, mq *v3.BuilderQuery, _ string, panelType v3.PanelType, options v3.QBOptions) (string, error) { ... }
  • Purpose: Builds the complete traces query based on the provided parameters.
    • Parameters:
      • start: Start timestamp (epoch millisecond).
      • end: End timestamp (epoch millisecond).
      • step: Step interval in seconds.
      • mq: The v3.BuilderQuery containing query parameters.
      • _: Unused parameter.
      • panelType: The v3.PanelType for which the query is being built.
      • options: v3.QBOptions for query building.
    • Returns:
      • string: The complete traces query.
      • error: An error, if any.

enrichOrderBy(items []v3.OrderBy, keys map[string]v3.AttributeKey) []v3.OrderBy

func enrichOrderBy(items []v3.OrderBy, keys map[string]v3.AttributeKey) []v3.OrderBy { ... }
  • Purpose: Enriches order by items with metadata from a map of existing keys.
    • Parameters:
      • items: A slice of v3.OrderBy to enrich.
      • keys: A map of existing v3.AttributeKey to look up metadata from.
    • Returns: A slice of enriched v3.OrderBy.

groupBy(panelType v3.PanelType, graphLimitQtype string, tags ...string) string

func groupBy(panelType v3.PanelType, graphLimitQtype string, tags ...string) string { ... }
  • Purpose: Constructs the GROUP BY clause for a query.
    • Parameters:
      • panelType: The v3.PanelType for which the query is being built.
      • graphLimitQtype: Graph limit query type.
      • tags: A variable number of strings representing the tags to group by.
    • Returns: The GROUP BY clause as a string.

GroupByAttributeKeyTags(panelType v3.PanelType, graphLimitQtype string, tags ...v3.AttributeKey) string

func GroupByAttributeKeyTags(panelType v3.PanelType, graphLimitQtype string, tags ...v3.AttributeKey) string { ... }
  • Purpose: Constructs the GROUP BY clause for a query using AttributeKey.
    • Parameters:
      • panelType: The v3.PanelType for which the query is being built.
      • graphLimitQtype: Graph limit query type.
      • tags: A variable number of v3.AttributeKey representing the tags to group by.
    • Returns: The GROUP BY clause as a string.

orderBy(panelType v3.PanelType, items []v3.OrderBy, tagLookup map[string]struct{}) []string

func orderBy(panelType v3.PanelType, items []v3.OrderBy, tagLookup map[string]struct{}) []string { ... }
  • Purpose: Constructs the ORDER BY clause for a query.
    • Parameters:
      • panelType: The v3.PanelType for which the query is being built.
      • items: A slice of v3.OrderBy specifying the order.
      • tagLookup: A map to look up tag names.
    • Returns: A slice of strings representing the ORDER BY clause.

orderByAttributeKeyTags(panelType v3.PanelType, items []v3.OrderBy, tags []v3.AttributeKey) string

func orderByAttributeKeyTags(panelType v3.PanelType, items []v3.OrderBy, tags []v3.AttributeKey) string { ... }
  • Purpose: Constructs the ORDER BY clause using AttributeKey for a query.
    • Parameters:
      • panelType: The v3.PanelType for which the query is being built.
      • items: A slice of v3.OrderBy specifying the order.
      • tags: A slice of v3.AttributeKey representing the tags to order by.
    • Returns: The ORDER BY clause as a string.

Having(items []v3.Having) string

func Having(items []v3.Having) string { ... }
  • Purpose