Skip to content

Commit 521793d

Browse files
authored
fix: Ensure group-by works with sum metrics (#636)
Adds all available columns into the query so that we can properly apply the group by clause. Ref: HDX-1419
1 parent 8720ee5 commit 521793d

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

packages/common-utils/src/__tests__/renderChartConfig.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ describe('renderChartConfig', () => {
100100
const generatedSql = await renderChartConfig(config, mockMetadata);
101101
const actual = parameterizedQueryToSql(generatedSql);
102102
expect(actual).toBe(
103-
'WITH RawSum AS (SELECT MetricName,Value,TimeUnix,Attributes,\n' +
103+
'WITH RawSum AS (SELECT *,\n' +
104104
' any(Value) OVER (ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS PrevValue,\n' +
105105
' any(AttributesHash) OVER (ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS PrevAttributesHash,\n' +
106106
' IF(AggregationTemporality = 1,\n' +
107107
' Value,IF(Value - PrevValue < 0 AND AttributesHash = PrevAttributesHash, Value,\n' +
108108
' IF(AttributesHash != PrevAttributesHash, 0, Value - PrevValue))) as Rate\n' +
109109
' FROM (\n' +
110-
' SELECT mapConcat(ScopeAttributes, ResourceAttributes, Attributes) AS Attributes,\n' +
111-
' cityHash64(Attributes) AS AttributesHash, Value, MetricName, TimeUnix, AggregationTemporality\n' +
110+
' SELECT *, \n' +
111+
' cityHash64(mapConcat(ScopeAttributes, ResourceAttributes, Attributes)) AS AttributesHash\n' +
112112
' FROM default.otel_metrics_sum\n' +
113113
" WHERE MetricName = 'db.client.connections.usage'\n" +
114-
' ORDER BY Attributes, TimeUnix ASC\n' +
114+
' ORDER BY AttributesHash, TimeUnix ASC\n' +
115115
' ) )SELECT avg(\n' +
116116
' toFloat64OrNull(toString(Rate))\n' +
117117
' ),toStartOfInterval(toDateTime(TimeUnix), INTERVAL 5 minutes) AS `__hdx_time_bucket` ' +

packages/common-utils/src/renderChartConfig.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,18 +785,18 @@ function translateMetricChartConfig(
785785
with: [
786786
{
787787
name: 'RawSum',
788-
sql: chSql`SELECT MetricName,Value,TimeUnix,Attributes,
788+
sql: chSql`SELECT *,
789789
any(Value) OVER (ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS PrevValue,
790790
any(AttributesHash) OVER (ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS PrevAttributesHash,
791791
IF(AggregationTemporality = 1,
792792
Value,IF(Value - PrevValue < 0 AND AttributesHash = PrevAttributesHash, Value,
793793
IF(AttributesHash != PrevAttributesHash, 0, Value - PrevValue))) as Rate
794794
FROM (
795-
SELECT mapConcat(ScopeAttributes, ResourceAttributes, Attributes) AS Attributes,
796-
cityHash64(Attributes) AS AttributesHash, Value, MetricName, TimeUnix, AggregationTemporality
795+
SELECT *,
796+
cityHash64(mapConcat(ScopeAttributes, ResourceAttributes, Attributes)) AS AttributesHash
797797
FROM ${renderFrom({ from: { ...from, tableName: metricTables[MetricsDataType.Sum] } })}
798798
WHERE MetricName = '${metricName}'
799-
ORDER BY Attributes, TimeUnix ASC
799+
ORDER BY AttributesHash, TimeUnix ASC
800800
) `,
801801
},
802802
],

0 commit comments

Comments
 (0)