Improve accuracy of query platform compatibility check when WITH expressions used (#3731)

This commit is contained in:
gillespi314 2022-01-17 20:01:29 -06:00 committed by GitHub
parent 371c533bfc
commit dea23356de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 8 deletions

View File

@ -0,0 +1 @@
* Improve accuracy of query platform compatibility check when `WITH` expressions used

View File

@ -44,17 +44,22 @@ export const listCompatiblePlatforms = (tablesList) => {
return compatiblePlatforms.length ? compatiblePlatforms : ["None"]; return compatiblePlatforms.length ? compatiblePlatforms : ["None"];
}; };
export const parseSqlTables = (sqlString) => { export const parseSqlTables = (sqlString, inludeCteTables = false) => {
const tablesList = []; let results = [];
// Tables defined via common table expression will be excluded from results by default
const cteTables = [];
const _callback = (node) => { const _callback = (node) => {
if (node) { if (node) {
if (node.variant === "recursive") { if (
throw new Error( (node.variant === "common" || node.variant === "recursive") &&
"Invalid usage: `recursive` is not supported by `parseSqlTables`" node.format === "table" &&
); node.type === "expression"
) {
cteTables.push(node.target?.name);
} else if (node.variant === "table") { } else if (node.variant === "table") {
tablesList.push(node.name); results.push(node.name);
} }
} }
}; };
@ -63,7 +68,11 @@ export const parseSqlTables = (sqlString) => {
const sqlTree = sqliteParser(sqlString); const sqlTree = sqliteParser(sqlString);
_visit(sqlTree, _callback); _visit(sqlTree, _callback);
return tablesList.length ? tablesList : ["No tables in query AST"]; if (cteTables.length) {
results = results.filter((t) => !cteTables.includes(t));
}
return results.length ? results : ["No tables in query AST"];
} catch (err) { } catch (err) {
// console.log(`Invalid query syntax: ${err.message}\n\n${sqlString}`); // console.log(`Invalid query syntax: ${err.message}\n\n${sqlString}`);