Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix: mark numeric columns as aggregation in scalar query response (#11593)

Tushar Vats080aae9567ed
diff --git a/pkg/querier/consume.go b/pkg/querier/consume.go
index eaada79864..dc340a8bd2 100644
--- a/pkg/querier/consume.go
+++ b/pkg/querier/consume.go
@@ -62,7 +62,7 @@ func readAsTimeSeries(rows driver.Rows, queryWindow *qbtypes.TimeRange, step qbt
numericColsCount := 0
for i, ct := range colTypes {
slots[i] = reflect.New(ct.ScanType()).Interface()
- if numericKind(ct.ScanType().Kind()) {
+ if isNumericKind(ct.ScanType()) {
numericColsCount++
}
}
@@ -270,8 +270,14 @@ func readAsTimeSeries(rows driver.Rows, queryWindow *qbtypes.TimeRange, step qbt
}, nil
}
-func numericKind(k reflect.Kind) bool {
- switch k {
+func isNumericKind(t reflect.Type) bool {
+ if t == nil {
+ return false
+ }
+ for t.Kind() == reflect.Ptr || t.Kind() == reflect.UnsafePointer {
+ t = t.Elem()
+ }
+ switch t.Kind() {
case reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
@@ -290,7 +296,13 @@ func readAsScalar(rows driver.Rows, queryName string) (*qbtypes.ScalarData, erro
var aggIndex int64
for i, name := range colNames {
colType := qbtypes.ColumnTypeGroup
- if aggRe.MatchString(name) {
+ // Builder queries aliases aggregation columns as __result_N (always numeric) and wraps group-by keys with toString (always string);
+ // Raw ClickHouse queries may use any aliases.
+ // Handling Builder queries, If name like __result_N -> aggregation, otherwise group-by column
+ // Handling Raw ClickHouse queries, If type is numeric -> aggregation, otherwise group-by column
+ // NOTE: For clickhouse queries, its wrong to assume that numeric columns are always aggregations, user might be grouping by on integer status_code.
+ // However, we are fine with this for now. If need arises, simplest way would be to solve this on the frontend side by asking user a mapping of column names to column types.
+ if aggRe.MatchString(name) || isNumericKind(colTypes[i].ScanType()) {
colType = qbtypes.ColumnTypeAggregation
}
cd[i] = &qbtypes.ColumnDescriptor{
diff --git a/tests/integration/tests/querier/17_clickhouse_queries.py b/tests/integration/tests/querier/17_clickhouse_queries.py
new file mode 100644
index 0000000000..8237225f06
--- /dev/null
+++ b/tests/integration/tests/querier/17_clickhouse_queries.py
@@ -0,0 +1,74 @@
+"""
+Integration tests for raw ClickHouse SQL queries in the querier.
+"""
+
+from collections.abc import Callable
+from datetime import UTC, datetime, timedelta
+from http import HTTPStatus
+
+from fixtures import querier, types
+from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
+
+
+def test_clickhouse_scalar_numeric_result_alias_classified_as_aggregation(
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+) -> None:
+ """A numeric column aliased ``__result_0`` is classified as an aggregation."""
+ now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+
+ response = querier.make_query_request(
+ signoz,
+ token,
+ int((now - timedelta(hours=1)).timestamp() * 1000),
+ int(now.timestamp() * 1000),
+ [
+ {
+ "type": "clickhouse_sql",
+ "spec": {
+ "name": "A",
+ "query": "SELECT toFloat64(1.5) AS `__result_0`",
+ "disabled": False,
+ },
+ }
+ ],
+ request_type=querier.RequestType.SCALAR,
+ )
+
+ assert response.status_code == HTTPStatus.OK
+ assert response.json()["status"] == "success"
+
+ columns = querier.get_scalar_columns(response.json())
+ assert len(columns) == 1
+ assert columns[0]["name"] == "__result_0"
+ assert columns[0]["columnType"] == "aggregation"
+ assert columns[0]["aggregationIndex"] == 0
+
+ response = querier.make_query_request(
+ signoz,
+ token,
+ int((now - timedelta(hours=1)).timestamp() * 1000),
+ int(now.timestamp() * 1000),
+ [
+ {
+ "type": "clickhouse_sql",
+ "spec": {
+ "name": "A",
+ "query": "SELECT toNullable(toFloat64(1.5)) AS value",
+ "disabled": False,
+ },
+ }
+ ],
+ request_type=querier.RequestType.SCALAR,
+ )
+
+ assert response.status_code == HTTPStatus.OK
+ assert response.json()["status"] == "success"
+
+ columns = querier.get_scalar_columns(response.json())
+ assert len(columns) == 1
+ assert columns[0]["name"] == "value"
+ assert columns[0]["columnType"] == "aggregation"
+ assert columns[0]["aggregationIndex"] == 0