diff --git a/changes/issue-3590-query-compatibilty b/changes/issue-3590-query-compatibilty new file mode 100644 index 000000000..3d90ccb2f --- /dev/null +++ b/changes/issue-3590-query-compatibilty @@ -0,0 +1 @@ +* Improve accuracy of query platform compatibility check when `WITH` expressions used \ No newline at end of file diff --git a/frontend/utilities/sql_tools.js b/frontend/utilities/sql_tools.js index e7b896c9d..ef3331766 100644 --- a/frontend/utilities/sql_tools.js +++ b/frontend/utilities/sql_tools.js @@ -44,17 +44,22 @@ export const listCompatiblePlatforms = (tablesList) => { return compatiblePlatforms.length ? compatiblePlatforms : ["None"]; }; -export const parseSqlTables = (sqlString) => { - const tablesList = []; +export const parseSqlTables = (sqlString, inludeCteTables = false) => { + let results = []; + + // Tables defined via common table expression will be excluded from results by default + const cteTables = []; const _callback = (node) => { if (node) { - if (node.variant === "recursive") { - throw new Error( - "Invalid usage: `recursive` is not supported by `parseSqlTables`" - ); + if ( + (node.variant === "common" || node.variant === "recursive") && + node.format === "table" && + node.type === "expression" + ) { + cteTables.push(node.target?.name); } 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); _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) { // console.log(`Invalid query syntax: ${err.message}\n\n${sqlString}`);