diff --git a/changes/40117-fix-sql-table-alias-platform-detection b/changes/40117-fix-sql-table-alias-platform-detection new file mode 100644 index 00000000000..e40bfd7fdf9 --- /dev/null +++ b/changes/40117-fix-sql-table-alias-platform-detection @@ -0,0 +1 @@ +- Fixed a bug where SQL queries using table aliases (e.g., `FROM mounts m`) incorrectly reported no compatible platforms. diff --git a/frontend/utilities/sql_tools.tests.ts b/frontend/utilities/sql_tools.tests.ts index 9c97605441b..d7d83ef8335 100644 --- a/frontend/utilities/sql_tools.tests.ts +++ b/frontend/utilities/sql_tools.tests.ts @@ -60,13 +60,21 @@ WHERE triggering_extension IS NOT NULL AND username NOT LIKE '\\_%' ESCAPE '\\'; expect(tables).toEqual([ "file", "parse_json", - "chrome_preferences", "extension_safety_hub_menu_notifications", "extension_details", "problematic_extensions", ]); }); + // from https://github.com/fleetdm/fleet/issues/40117 + it("should not include table aliases from column references", () => { + const sql = + "SELECT * FROM mounts m, disk_encryption d WHERE m.device_alias = d.name"; + const { tables, error } = checkTable(sql); + expect(error).toBeNull(); + expect(tables).toEqual(["mounts", "disk_encryption"]); + }); + it("should return an error if SQL is invalid", () => { const result = checkTable("SELECTx * FROM users"); expect(result.error).not.toBeNull(); diff --git a/frontend/utilities/sql_tools.ts b/frontend/utilities/sql_tools.ts index 58ae505a972..40108b90be7 100644 --- a/frontend/utilities/sql_tools.ts +++ b/frontend/utilities/sql_tools.ts @@ -130,7 +130,7 @@ export const parseSqlTables = ( } // Plain ol' tables. - if (node.table) { + if (node.table && node.type !== "column_ref") { results.push(node.table as string); } }