From dea23356de4d274f868af44135b2184fde2ff336 Mon Sep 17 00:00:00 2001 From: gillespi314 <73313222+gillespi314@users.noreply.github.com> Date: Mon, 17 Jan 2022 20:01:29 -0600 Subject: [PATCH] Improve accuracy of query platform compatibility check when `WITH` expressions used (#3731) --- changes/issue-3590-query-compatibilty | 1 + frontend/utilities/sql_tools.js | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 changes/issue-3590-query-compatibilty 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}`);