Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqllab): rollback clean comments out #24009

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 1 addition & 67 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,72 +357,6 @@ export function fetchQueryResults(query, displayLimit) {
};
}

const quotes = '\'"`'.split('');
const quotedBlockHash = shortid.generate();
const quotedBlockMatch = new RegExp(`${quotedBlockHash}:\\d+:`, 'g');

function splitByQuotedBlock(str) {
const chunks = [];
let currentQuote = '';
let chunkStart = 0;

let i = 0;
while (i < str.length) {
const currentChar = str[i];
if (
currentQuote ? currentChar === currentQuote : quotes.includes(currentChar)
) {
let chunk;
if (currentQuote) {
chunk = str.substring(chunkStart, i + 1);
chunkStart = i + 1;
currentQuote = '';
} else {
chunk = str.substring(chunkStart, i);
chunkStart = i;
currentQuote = currentChar;
}
if (chunk) {
chunks.push(chunk);
}
}
i += 1;
}

if (chunkStart < str.length) {
const lastChunk = str.substring(chunkStart);
if (lastChunk) {
chunks.push(lastChunk);
}
}

return chunks;
}

export function cleanSqlComments(sql) {
if (!sql) return '';
// it sanitizes the following comment block groups
// group 1 -> /* */
// group 2 -> --
const chunks = splitByQuotedBlock(sql);
return (
chunks
// replace quoted blocks in a hash format
.map((chunk, index) =>
quotes.includes(chunk[0]) ? `${quotedBlockHash}:${index}:` : chunk,
)
.join('')
// Clean out the commented-out blocks
.replace(/(--.*?$|\/\*[\s\S]*?\*\/)\n?/gm, '\n')
.trim()
// restore quoted block to the original value
.replace(
quotedBlockMatch,
quotedBlock => chunks[quotedBlock.match(/:\d+/)[0].substring(1)],
)
);
}

export function runQuery(query) {
return function (dispatch) {
dispatch(startQuery(query));
Expand All @@ -432,7 +366,7 @@ export function runQuery(query) {
json: true,
runAsync: query.runAsync,
schema: query.schema,
sql: cleanSqlComments(query.sql),
sql: query.sql,
sql_editor_id: query.sqlEditorId,
tab: query.tab,
tmp_table_name: query.tempTable,
Expand Down
32 changes: 0 additions & 32 deletions superset-frontend/src/SqlLab/actions/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,38 +308,6 @@ describe('async actions', () => {
});
});

describe('runQuery with comments', () => {
const makeRequest = () => {
const request = actions.runQuery({
...query,
sql: `/*
SELECT * FROM
*/
SELECT 213--, {{ds}} "quote out"
/*
{{new_param1}}
{{new_param2}}*/

FROM table
WHERE value = '--"NULL"--' --{{test_param}}`,
});
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\nWHERE value = '--"NULL"--'`);
});
});
});

describe('reRunQuery', () => {
let stub;
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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;
Expand Down Expand Up @@ -106,7 +105,9 @@ const RunQueryActionButton = ({
: Button;

const sqlContent = selectedText || sql || '';
const isDisabled = cleanSqlComments(sqlContent).length === 0;
const isDisabled =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe outside the scope of this PR, but I wonder if we should remove the isDisable logic as well. Regex's scare me.

!sqlContent ||
!sqlContent.replace(/(\/\*[^*]*\*\/)|(\/\/[^*]*)|(--[^.].*)/gm, '').trim();

const stopButtonTooltipText = useMemo(
() =>
Expand Down