From 7af9cd7dfc722eda93e6e2449ce29feca05b27f0 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Sun, 22 Jul 2018 15:06:59 +0200 Subject: [PATCH] set explicit order for rate and increase --- .../plugins/datasource/postgres/postgres_query.ts | 12 ++++++++++-- .../datasource/postgres/specs/postgres_query.jest.ts | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index 2222e512e32..a16a9e5b458 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -136,16 +136,24 @@ export default class PostgresQuery { query = columnName.params[0]; let aggregate = _.find(column, (g: any) => g.type === 'aggregate'); + let special = _.find(column, (g: any) => g.type === 'special'); + if (aggregate) { - query = aggregate.params[0] + '(' + query + ')'; + if (special) { + query = aggregate.params[0] + '(' + query + ' ORDER BY ' + this.target.timeColumn + ')'; + } else { + query = aggregate.params[0] + '(' + query + ')'; + } } - let special = _.find(column, (g: any) => g.type === 'special'); if (special) { let over = ''; if (this.hasMetricColumn()) { over = 'PARTITION BY ' + this.target.metricColumn; } + if (!aggregate) { + over += 'ORDER BY ' + this.target.timeColumn; + } switch (special.params[0]) { case 'increase': query = query + ' - lag(' + query + ') OVER (' + over + ')'; diff --git a/public/app/plugins/datasource/postgres/specs/postgres_query.jest.ts b/public/app/plugins/datasource/postgres/specs/postgres_query.jest.ts index d9b3f46b4bf..659ca94496c 100644 --- a/public/app/plugins/datasource/postgres/specs/postgres_query.jest.ts +++ b/public/app/plugins/datasource/postgres/specs/postgres_query.jest.ts @@ -63,7 +63,7 @@ describe('PostgresQuery', function() { { type: 'alias', params: ['a'] }, { type: 'special', params: ['increase'] }, ]; - expect(query.buildValueColumn(column)).toBe('v - lag(v) OVER () AS "a"'); + expect(query.buildValueColumn(column)).toBe('v - lag(v) OVER (ORDER BY time) AS "a"'); }); describe('When generating WHERE clause', function() {