From 95f36632e980a7adbc609015f8e1893b33871761 Mon Sep 17 00:00:00 2001 From: Loren Siebert Date: Thu, 16 Jan 2025 11:38:56 -0800 Subject: [PATCH] Fixes regression in parse_schema_tables --- src/sinker/utils.py | 2 +- tests/test_parse_schema_tables.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/sinker/utils.py b/src/sinker/utils.py index f8fde3b..c84dbe3 100644 --- a/src/sinker/utils.py +++ b/src/sinker/utils.py @@ -14,7 +14,7 @@ def parse_schema_tables(view_select_query: str) -> Tuple[str, Set[str]]: parent_table = parsed.find(Table) if parent_table is None: raise ValueError("No table found in the query") - tables = {table.name for table in parsed.find_all(Table)} + tables = {table.name for table in parsed.find_all(Table) if table.name} ctes = {cte.alias for cte in parsed.find_all(CTE)} schema_tables = tables - ctes return parent_table.name, schema_tables diff --git a/tests/test_parse_schema_tables.py b/tests/test_parse_schema_tables.py index a47e75f..559ce06 100644 --- a/tests/test_parse_schema_tables.py +++ b/tests/test_parse_schema_tables.py @@ -62,6 +62,32 @@ def test_parse_schema_tables_with_cte(): assert schema_tables == {"EmailAddress", "HostedEvent", "HostedEventAttendance", "Person"} +def test_parse_schema_tables_handles_blank_table_name(): + view_select_query = """ + select id, + json_build_object( + 'summary', "summary", + 'startTime', "start_time", + 'organizerEmail', "organizerEmail", + 'attendees', (select json_agg(json_build_object('email', key, 'eventResponse', value)) + as formatted_attendees + from (select id, key, value + from "googleEvents", + jsonb_each_text(attendees) as kv(key, value)) as subquery + where id = "googleEvents".id), + 'organizationIds', (select array_agg("_NotesToOrganization"."B") + from "_NotesToOrganization" + left join public."Notes" N on "_NotesToOrganization"."A" = N.id + where "googleEventId" = "googleEvents".id) + ) as "google_events" + from "googleEvents"; + """ + parent_table, schema_tables = parse_schema_tables(view_select_query) + assert parent_table == "googleEvents" + # parsed.find_all(Table) returns a Table object with a blank '' name + assert schema_tables == {"googleEvents", "_NotesToOrganization", "Notes"} + + def test_error_handling_on_query_with_no_table(): view_select_query = """select 1""" try: