Commit e07513bd by Sven Klemm

Don't use unnest in queries for redshift compatibility

parent fe9d86c0
...@@ -25,7 +25,7 @@ export class PostgresMetaQuery { ...@@ -25,7 +25,7 @@ export class PostgresMetaQuery {
findMetricTable() { findMetricTable() {
// query that returns first table found that has a timestamp(tz) column and a float column // query that returns first table found that has a timestamp(tz) column and a float column
const query = ` let query = `
SELECT SELECT
quote_ident(table_name) as table_name, quote_ident(table_name) as table_name,
( SELECT ( SELECT
...@@ -47,11 +47,9 @@ SELECT ...@@ -47,11 +47,9 @@ SELECT
ORDER BY ordinal_position LIMIT 1 ORDER BY ordinal_position LIMIT 1
) AS value_column ) AS value_column
FROM information_schema.tables t FROM information_schema.tables t
WHERE WHERE `;
table_schema IN ( query += this.buildSchemaConstraint();
SELECT CASE WHEN trim(unnest) = '"$user"' THEN user ELSE trim(unnest) END query += ` AND
FROM unnest(string_to_array(current_setting('search_path'),','))
) AND
EXISTS EXISTS
( SELECT 1 ( SELECT 1
FROM information_schema.columns c FROM information_schema.columns c
...@@ -76,8 +74,14 @@ LIMIT 1 ...@@ -76,8 +74,14 @@ LIMIT 1
buildSchemaConstraint() { buildSchemaConstraint() {
const query = ` const query = `
table_schema IN ( table_schema IN (
SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END SELECT
FROM unnest(string_to_array(current_setting(\'search_path\'),\',\')) CASE WHEN trim(s[i]) = '"$user"' THEN user ELSE trim(s[i]) END
FROM
generate_series(
array_lower(string_to_array(current_setting('search_path'),','),1),
array_upper(string_to_array(current_setting('search_path'),','),1)
) as i,
string_to_array(current_setting('search_path'),',') s
)`; )`;
return query; return query;
} }
...@@ -92,11 +96,7 @@ table_schema IN ( ...@@ -92,11 +96,7 @@ table_schema IN (
query += ' AND table_name = ' + this.quoteIdentAsLiteral(parts[1]); query += ' AND table_name = ' + this.quoteIdentAsLiteral(parts[1]);
return query; return query;
} else { } else {
query = ` query = this.buildSchemaConstraint();
table_schema IN (
SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END
FROM unnest(string_to_array(current_setting(\'search_path\'),\',\'))
)`;
query += ' AND table_name = ' + this.quoteIdentAsLiteral(table); query += ' AND table_name = ' + this.quoteIdentAsLiteral(table);
return query; return query;
...@@ -149,18 +149,8 @@ table_schema IN ( ...@@ -149,18 +149,8 @@ table_schema IN (
} }
buildDatatypeQuery(column: string) { buildDatatypeQuery(column: string) {
let query = ` let query = 'SELECT udt_name FROM information_schema.columns WHERE ';
SELECT udt_name query += this.buildSchemaConstraint();
FROM information_schema.columns
WHERE
table_schema IN (
SELECT schema FROM (
SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END as schema
FROM unnest(string_to_array(current_setting(\'search_path\'),\',\'))
) s
WHERE EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = s.schema)
)
`;
query += ' AND table_name = ' + this.quoteIdentAsLiteral(this.target.table); query += ' AND table_name = ' + this.quoteIdentAsLiteral(this.target.table);
query += ' AND column_name = ' + this.quoteIdentAsLiteral(column); query += ' AND column_name = ' + this.quoteIdentAsLiteral(column);
return query; return query;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment