Skip to content

Commit

Permalink
Fixes regression in parse_schema_tables
Browse files Browse the repository at this point in the history
  • Loading branch information
loren committed Jan 16, 2025
1 parent 74db111 commit 95f3663
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sinker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions tests/test_parse_schema_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 95f3663

Please sign in to comment.