Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Revert "fix(sqllab): clean comments within quotes (apache#23908)"
Browse files Browse the repository at this point in the history
This reverts commit 63d8015.
  • Loading branch information
justinpark committed May 10, 2023
1 parent c0cdc22 commit fc7bb70
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 70 deletions.
60 changes: 1 addition & 59 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,70 +358,12 @@ 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)],
)
);
return sql.replace(/(--.*?$|\/\*[\s\S]*?\*\/)\n?/gm, '\n').trim();
}

export function runQuery(query) {
Expand Down
13 changes: 2 additions & 11 deletions superset-frontend/src/SqlLab/actions/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,7 @@ describe('async actions', () => {
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}}`,
sql: '/*\nSELECT * FROM\n */\nSELECT 213--, {{ds}}\n/*\n{{new_param1}}\n{{new_param2}}*/\n\nFROM table',
});
return request(dispatch, () => initialState);
};
Expand All @@ -335,7 +326,7 @@ WHERE value = '--"NULL"--' --{{test_param}}`,
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"--'`);
).toEqual('SELECT 213\n\n\nFROM table');
});
});
});
Expand Down

0 comments on commit fc7bb70

Please sign in to comment.