InfluxDB: Fix query builder produces invalid SQL query when using wildcard column name (#89032)
* influx: changes to not wrap * with double quotes for column name Signed-off-by: Syed Nihal <syed.nihal@nokia.com> * added unit test for https://github.com/grafana/grafana/issues/88008 Signed-off-by: Syed Nihal <syed.nihal@nokia.com> --------- Signed-off-by: Syed Nihal <syed.nihal@nokia.com>
This commit is contained in:
1 parent
51c858f32e
commit
a9736cf7dc
2 files changed
+39
-1
No files matched your search
@@ -78,4 +78,29 @@ describe('toRawSql', () => {
|
||||
const result = toRawSql(testQuery);
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should not wrap * with quote', () => {
|
||||
const expected = 'SELECT * FROM "TestValue" WHERE "time" >= $__timeFrom AND "time" <= $__timeTo LIMIT 50';
|
||||
const testQuery: SQLQuery = {
|
||||
refId: 'A',
|
||||
sql: {
|
||||
limit: 50,
|
||||
columns: [
|
||||
{
|
||||
parameters: [
|
||||
{
|
||||
name: '*',
|
||||
type: QueryEditorExpressionType.FunctionParameter,
|
||||
},
|
||||
],
|
||||
type: QueryEditorExpressionType.Function,
|
||||
},
|
||||
],
|
||||
},
|
||||
dataset: 'iox',
|
||||
table: 'TestValue',
|
||||
};
|
||||
const result = toRawSql(testQuery);
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -26,7 +26,10 @@ export function toRawSql({ sql, table }: SQLQuery): string {
|
||||
}
|
||||
|
||||
// wrapping the column name with quotes
|
||||
const sc = sql.columns.map((c) => ({ ...c, parameters: c.parameters?.map((p) => ({ ...p, name: `"${p.name}"` })) }));
|
||||
const sc = sql.columns.map((c) => ({
|
||||
...c,
|
||||
parameters: c.parameters?.map((p) => ({ ...p, name: formatTableName(p.name) })),
|
||||
}));
|
||||
rawQuery += createSelectClause(sc);
|
||||
|
||||
if (table) {
|
||||
@@ -66,6 +69,16 @@ export function toRawSql({ sql, table }: SQLQuery): string {
|
||||
return rawQuery;
|
||||
}
|
||||
|
||||
// When the column name is *, do not wrap the column name in double-quotes.
|
||||
// See: https://github.com/grafana/grafana/issues/88008
|
||||
function formatTableName(parameter: string | undefined): string {
|
||||
if (parameter === '*') {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
return `"${parameter}"`;
|
||||
}
|
||||
|
||||
const isLimit = (limit: number | undefined): boolean => limit !== undefined && limit >= 0;
|
||||
|
||||
// Puts double quotes (") around the identifier if it is necessary.
|
||||
|
||||
Reference in new issue
Block a user