From d1947f735485986364f0930c66f68bc6c3292383 Mon Sep 17 00:00:00 2001 From: "JUST.in DO IT" Date: Thu, 23 Mar 2023 16:06:22 -0700 Subject: [PATCH] fix(sqllab): throw errors of commented out query (#23378) --- .../src/SqlLab/actions/sqlLab.js | 10 +++++++- .../src/SqlLab/actions/sqlLab.test.js | 23 +++++++++++++++++++ .../components/RunQueryActionButton/index.tsx | 5 ++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/superset-frontend/src/SqlLab/actions/sqlLab.js b/superset-frontend/src/SqlLab/actions/sqlLab.js index 50a6346524..88c500022b 100644 --- a/superset-frontend/src/SqlLab/actions/sqlLab.js +++ b/superset-frontend/src/SqlLab/actions/sqlLab.js @@ -353,6 +353,14 @@ export function fetchQueryResults(query, displayLimit) { }; } +export function cleanSqlComments(sql) { + if (!sql) return ''; + // it sanitizes the following comment block groups + // group 1 -> /* */ + // group 2 -> -- + return sql.replace(/(--.*?$|\/\*[\s\S]*?\*\/)\n?/gm, '\n').trim(); +} + export function runQuery(query) { return function (dispatch) { dispatch(startQuery(query)); @@ -362,7 +370,7 @@ export function runQuery(query) { json: true, runAsync: query.runAsync, schema: query.schema, - sql: query.sql, + sql: cleanSqlComments(query.sql), sql_editor_id: query.sqlEditorId, tab: query.tab, tmp_table_name: query.tempTable, diff --git a/superset-frontend/src/SqlLab/actions/sqlLab.test.js b/superset-frontend/src/SqlLab/actions/sqlLab.test.js index 01886d6f77..1e6dcf78a4 100644 --- a/superset-frontend/src/SqlLab/actions/sqlLab.test.js +++ b/superset-frontend/src/SqlLab/actions/sqlLab.test.js @@ -308,6 +308,29 @@ describe('async actions', () => { }); }); + describe('runQuery with comments', () => { + const makeRequest = () => { + const request = actions.runQuery({ + ...query, + sql: '/*\nSELECT * FROM\n */\nSELECT 213--, {{ds}}\n/*\n{{new_param1}}\n{{new_param2}}*/\n\nFROM table', + }); + return request(dispatch, () => initialState); + }; + + it('makes the fetch request without comments', async () => { + const runQueryEndpoint = 'glob:*/api/v1/sqllab/execute/'; + fetchMock.post(runQueryEndpoint, '{}', { + overwriteRoutes: true, + }); + await makeRequest().then(() => { + expect(fetchMock.calls(runQueryEndpoint)).toHaveLength(1); + expect( + JSON.parse(fetchMock.calls(runQueryEndpoint)[0][1].body).sql, + ).toEqual('SELECT 213\n\n\nFROM table'); + }); + }); + }); + describe('reRunQuery', () => { let stub; beforeEach(() => { diff --git a/superset-frontend/src/SqlLab/components/RunQueryActionButton/index.tsx b/superset-frontend/src/SqlLab/components/RunQueryActionButton/index.tsx index 9a94fb81c3..0a2aa99264 100644 --- a/superset-frontend/src/SqlLab/components/RunQueryActionButton/index.tsx +++ b/superset-frontend/src/SqlLab/components/RunQueryActionButton/index.tsx @@ -26,6 +26,7 @@ import { DropdownButton } from 'src/components/DropdownButton'; import { detectOS } from 'src/utils/common'; import { QueryButtonProps } from 'src/SqlLab/types'; import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor'; +import { cleanSqlComments } from 'src/SqlLab/actions/sqlLab'; export interface RunQueryActionButtonProps { queryEditorId: string; @@ -105,9 +106,7 @@ const RunQueryActionButton = ({ : Button; const sqlContent = selectedText || sql || ''; - const isDisabled = - !sqlContent || - !sqlContent.replace(/(\/\*[^*]*\*\/)|(\/\/[^*]*)|(--[^.].*)/gm, '').trim(); + const isDisabled = cleanSqlComments(sqlContent).length === 0; const stopButtonTooltipText = useMemo( () =>