2025-05-27 20:54:48 +05:30
|
|
|
package querier
|
|
|
|
|
|
|
|
|
|
import (
|
2025-06-23 14:00:50 +05:30
|
|
|
"bytes"
|
2025-05-27 20:54:48 +05:30
|
|
|
"context"
|
2025-06-23 14:00:50 +05:30
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
|
|
|
|
"text/template"
|
2025-05-27 20:54:48 +05:30
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/ClickHouse/clickhouse-go/v2"
|
2025-06-23 14:00:50 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/errors"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/querybuilder"
|
2025-05-27 20:54:48 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/telemetrystore"
|
|
|
|
|
qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type chSQLQuery struct {
|
2025-06-23 14:00:50 +05:30
|
|
|
logger *slog.Logger
|
2025-05-27 20:54:48 +05:30
|
|
|
telemetryStore telemetrystore.TelemetryStore
|
|
|
|
|
|
|
|
|
|
query qbtypes.ClickHouseQuery
|
|
|
|
|
args []any
|
|
|
|
|
fromMS uint64
|
|
|
|
|
toMS uint64
|
|
|
|
|
kind qbtypes.RequestType
|
2025-06-23 14:00:50 +05:30
|
|
|
vars map[string]qbtypes.VariableItem
|
2025-05-27 20:54:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ qbtypes.Query = (*chSQLQuery)(nil)
|
|
|
|
|
|
|
|
|
|
func newchSQLQuery(
|
2025-06-23 14:00:50 +05:30
|
|
|
logger *slog.Logger,
|
2025-05-27 20:54:48 +05:30
|
|
|
telemetryStore telemetrystore.TelemetryStore,
|
|
|
|
|
query qbtypes.ClickHouseQuery,
|
|
|
|
|
args []any,
|
|
|
|
|
tr qbtypes.TimeRange,
|
|
|
|
|
kind qbtypes.RequestType,
|
2025-06-23 14:00:50 +05:30
|
|
|
variables map[string]qbtypes.VariableItem,
|
2025-05-27 20:54:48 +05:30
|
|
|
) *chSQLQuery {
|
|
|
|
|
return &chSQLQuery{
|
2025-06-23 14:00:50 +05:30
|
|
|
logger: logger,
|
2025-05-27 20:54:48 +05:30
|
|
|
telemetryStore: telemetryStore,
|
|
|
|
|
query: query,
|
|
|
|
|
args: args,
|
|
|
|
|
fromMS: tr.From,
|
|
|
|
|
toMS: tr.To,
|
|
|
|
|
kind: kind,
|
2025-06-23 14:00:50 +05:30
|
|
|
vars: variables,
|
2025-05-27 20:54:48 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 18:26:28 +05:30
|
|
|
func (q *chSQLQuery) Fingerprint() string {
|
|
|
|
|
// No caching for CH queries for now
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 20:54:48 +05:30
|
|
|
func (q *chSQLQuery) Window() (uint64, uint64) { return q.fromMS, q.toMS }
|
|
|
|
|
|
2025-06-23 14:00:50 +05:30
|
|
|
// TODO(srikanthccv): cleanup the templating logic
|
|
|
|
|
func (q *chSQLQuery) renderVars(query string, vars map[string]qbtypes.VariableItem, start, end uint64) (string, error) {
|
|
|
|
|
varsData := map[string]any{}
|
|
|
|
|
for k, v := range vars {
|
2025-07-12 16:47:59 +05:30
|
|
|
varsData[k] = formatValueForCH(v.Value)
|
2025-06-23 14:00:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
querybuilder.AssignReservedVars(varsData, start, end)
|
|
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(varsData))
|
|
|
|
|
for k := range varsData {
|
|
|
|
|
keys = append(keys, k)
|
|
|
|
|
}
|
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
|
|
|
return len(keys[i]) > len(keys[j])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
|
query = strings.Replace(query, fmt.Sprintf("{{%s}}", k), fmt.Sprint(varsData[k]), -1)
|
|
|
|
|
query = strings.Replace(query, fmt.Sprintf("[[%s]]", k), fmt.Sprint(varsData[k]), -1)
|
|
|
|
|
query = strings.Replace(query, fmt.Sprintf("$%s", k), fmt.Sprint(varsData[k]), -1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmpl := template.New("clickhouse-query")
|
|
|
|
|
tmpl, err := tmpl.Parse(query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.WrapInternalf(err, errors.CodeInternal, "error while replacing template variables")
|
|
|
|
|
}
|
|
|
|
|
var newQuery bytes.Buffer
|
|
|
|
|
|
|
|
|
|
// replace go template variables
|
|
|
|
|
err = tmpl.Execute(&newQuery, varsData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.WrapInternalf(err, errors.CodeInternal, "error while replacing template variables")
|
|
|
|
|
}
|
|
|
|
|
return newQuery.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 20:54:48 +05:30
|
|
|
func (q *chSQLQuery) Execute(ctx context.Context) (*qbtypes.Result, error) {
|
|
|
|
|
|
|
|
|
|
totalRows := uint64(0)
|
|
|
|
|
totalBytes := uint64(0)
|
|
|
|
|
elapsed := time.Duration(0)
|
|
|
|
|
|
|
|
|
|
ctx = clickhouse.Context(ctx, clickhouse.WithProgress(func(p *clickhouse.Progress) {
|
|
|
|
|
totalRows += p.Rows
|
|
|
|
|
totalBytes += p.Bytes
|
|
|
|
|
elapsed += p.Elapsed
|
|
|
|
|
}))
|
|
|
|
|
|
2025-06-23 14:00:50 +05:30
|
|
|
query, err := q.renderVars(q.query.Query, q.vars, q.fromMS, q.toMS)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rows, err := q.telemetryStore.ClickhouseDB().Query(ctx, query, q.args...)
|
2025-05-27 20:54:48 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
// TODO: map the errors from ClickHouse to our error types
|
2025-06-10 18:26:28 +05:30
|
|
|
payload, err := consume(rows, q.kind, nil, qbtypes.Step{}, q.query.Name)
|
2025-05-27 20:54:48 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &qbtypes.Result{
|
|
|
|
|
Type: q.kind,
|
|
|
|
|
Value: payload,
|
|
|
|
|
Stats: qbtypes.ExecStats{
|
|
|
|
|
RowsScanned: totalRows,
|
|
|
|
|
BytesScanned: totalBytes,
|
|
|
|
|
DurationMS: uint64(elapsed.Milliseconds()),
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|