From 2fcb09b23d262d7422600699554e3e027940fdb7 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Sun, 15 Jul 2018 16:22:23 +0200 Subject: [PATCH] filter datatype for groupby suggestions --- .../datasource/postgres/query_builder.ts | 17 +++++++++++------ .../plugins/datasource/postgres/query_ctrl.ts | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/public/app/plugins/datasource/postgres/query_builder.ts b/public/app/plugins/datasource/postgres/query_builder.ts index ffa8f6ba270..c57fc288f56 100644 --- a/public/app/plugins/datasource/postgres/query_builder.ts +++ b/public/app/plugins/datasource/postgres/query_builder.ts @@ -1,6 +1,11 @@ export class PostgresQueryBuilder { constructor(private target, private queryModel) {} + // quote identifier as literal to use in metadata queries + quoteIdentAsLiteral(value) { + return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value)); + } + buildSchemaQuery() { let query = 'SELECT quote_ident(schema_name) FROM information_schema.schemata WHERE'; query += " schema_name NOT LIKE 'pg_%' AND schema_name NOT LIKE '\\_%' AND schema_name <> 'information_schema';"; @@ -14,11 +19,6 @@ export class PostgresQueryBuilder { return query; } - // quote identifier as literal to use in metadata queries - quoteIdentAsLiteral(value) { - return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value)); - } - buildColumnQuery(type?: string) { let query = 'SELECT quote_ident(column_name) FROM information_schema.columns WHERE '; query += 'table_schema = ' + this.quoteIdentAsLiteral(this.target.schema); @@ -31,11 +31,16 @@ export class PostgresQueryBuilder { break; } case 'metric': { - query += " AND data_type IN ('text','char','varchar','integer','bigint')"; + query += " AND data_type IN ('text','char','varchar')"; break; } case 'value': { query += " AND data_type IN ('bigint','integer','double precision','real')"; + query += ' AND column_name <> ' + this.quoteIdentAsLiteral(this.target.timeColumn); + break; + } + case 'groupby': { + query += " AND data_type IN ('text','char','varchar')"; break; } } diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index fbd6907f767..0aa0c77a63d 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -442,7 +442,7 @@ export class PostgresQueryCtrl extends QueryCtrl { getGroupByOptions() { return this.datasource - .metricFindQuery(this.queryBuilder.buildColumnQuery()) + .metricFindQuery(this.queryBuilder.buildColumnQuery('groupby')) .then(tags => { var options = []; if (!this.queryModel.hasGroupByTime()) {