From f49ced91438ab11f4d1449f137ce7182bfecbf1b Mon Sep 17 00:00:00 2001 From: tianzhou Date: Thu, 23 Oct 2025 11:34:05 +0800 Subject: [PATCH] chore: view subquery test case --- testdata/diff/create_view/add_view/diff.sql | 30 +++++++++++++++ testdata/diff/create_view/add_view/new.sql | 39 ++++++++++++++++++++ testdata/diff/create_view/add_view/plan.json | 6 +++ testdata/diff/create_view/add_view/plan.sql | 30 +++++++++++++++ testdata/diff/create_view/add_view/plan.txt | 35 +++++++++++++++++- 5 files changed, 138 insertions(+), 2 deletions(-) diff --git a/testdata/diff/create_view/add_view/diff.sql b/testdata/diff/create_view/add_view/diff.sql index 1e96a59b..d207f857 100644 --- a/testdata/diff/create_view/add_view/diff.sql +++ b/testdata/diff/create_view/add_view/diff.sql @@ -66,3 +66,33 @@ CREATE OR REPLACE VIEW text_search_view AS to_tsvector('english'::regconfig, (((COALESCE(first_name, ''::character varying)::text || ' '::text) || COALESCE(last_name, ''::character varying)::text) || ' '::text) || COALESCE(bio, ''::text)) AS search_vector FROM employees WHERE status::text = 'active'::text; + +CREATE OR REPLACE VIEW union_subquery_view AS + SELECT id, + name, + source_type, + CASE + WHEN source_type = 'employee'::text THEN 'active'::text + WHEN source_type = 'department'::text THEN 'organizational'::text + ELSE 'unknown'::text + END AS category + FROM (( + SELECT employees.id, + employees.name, + 'employee'::text AS source_type + FROM employees + WHERE employees.status::text = 'active'::text + UNION + SELECT departments.id, + departments.name, + 'department'::text AS source_type + FROM departments + WHERE departments.manager_id IS NOT NULL + ) UNION ALL + SELECT employees.id, + COALESCE((employees.first_name::text || ' '::text) || employees.last_name::text, employees.name::text) AS name, + 'employee_full'::text AS source_type + FROM employees + WHERE employees.priority > 10) combined_data + WHERE id IS NOT NULL + ORDER BY source_type, id; diff --git a/testdata/diff/create_view/add_view/new.sql b/testdata/diff/create_view/add_view/new.sql index 79f1ef9d..8434246d 100644 --- a/testdata/diff/create_view/add_view/new.sql +++ b/testdata/diff/create_view/add_view/new.sql @@ -102,3 +102,42 @@ FROM monthly_stats ms CROSS JOIN departments d LEFT JOIN employee_summary es ON d.id = es.department_id ORDER BY ms.month_start DESC, d.name; + +-- View testing UNION and UNION ALL in subqueries (regression test for issue #104) +CREATE VIEW public.union_subquery_view AS +SELECT + id, + name, + source_type, + -- Additional columns from the union result + CASE + WHEN source_type = 'employee' THEN 'active' + WHEN source_type = 'department' THEN 'organizational' + ELSE 'unknown' + END AS category +FROM ( + -- Simple UNION combining employees and departments (main issue from #104) + SELECT + id, + name, + 'employee' AS source_type + FROM employees + WHERE status = 'active' + UNION + SELECT + id, + name, + 'department' AS source_type + FROM departments + WHERE manager_id IS NOT NULL + -- UNION ALL variant (keep duplicates) + UNION ALL + SELECT + id, + COALESCE(first_name || ' ' || last_name, name) AS name, + 'employee_full' AS source_type + FROM employees + WHERE priority > 10 +) AS combined_data +WHERE id IS NOT NULL +ORDER BY source_type, id; diff --git a/testdata/diff/create_view/add_view/plan.json b/testdata/diff/create_view/add_view/plan.json index af248387..a32c9210 100644 --- a/testdata/diff/create_view/add_view/plan.json +++ b/testdata/diff/create_view/add_view/plan.json @@ -31,6 +31,12 @@ "type": "view", "operation": "create", "path": "public.text_search_view" + }, + { + "sql": "CREATE OR REPLACE VIEW union_subquery_view AS\n SELECT id,\n name,\n source_type,\n CASE\n WHEN source_type = 'employee'::text THEN 'active'::text\n WHEN source_type = 'department'::text THEN 'organizational'::text\n ELSE 'unknown'::text\n END AS category\n FROM ((\n SELECT employees.id,\n employees.name,\n 'employee'::text AS source_type\n FROM employees\n WHERE employees.status::text = 'active'::text\n UNION\n SELECT departments.id,\n departments.name,\n 'department'::text AS source_type\n FROM departments\n WHERE departments.manager_id IS NOT NULL\n ) UNION ALL\n SELECT employees.id,\n COALESCE((employees.first_name::text || ' '::text) || employees.last_name::text, employees.name::text) AS name,\n 'employee_full'::text AS source_type\n FROM employees\n WHERE employees.priority > 10) combined_data\n WHERE id IS NOT NULL\n ORDER BY source_type, id;", + "type": "view", + "operation": "create", + "path": "public.union_subquery_view" } ] } diff --git a/testdata/diff/create_view/add_view/plan.sql b/testdata/diff/create_view/add_view/plan.sql index 1e96a59b..d207f857 100644 --- a/testdata/diff/create_view/add_view/plan.sql +++ b/testdata/diff/create_view/add_view/plan.sql @@ -66,3 +66,33 @@ CREATE OR REPLACE VIEW text_search_view AS to_tsvector('english'::regconfig, (((COALESCE(first_name, ''::character varying)::text || ' '::text) || COALESCE(last_name, ''::character varying)::text) || ' '::text) || COALESCE(bio, ''::text)) AS search_vector FROM employees WHERE status::text = 'active'::text; + +CREATE OR REPLACE VIEW union_subquery_view AS + SELECT id, + name, + source_type, + CASE + WHEN source_type = 'employee'::text THEN 'active'::text + WHEN source_type = 'department'::text THEN 'organizational'::text + ELSE 'unknown'::text + END AS category + FROM (( + SELECT employees.id, + employees.name, + 'employee'::text AS source_type + FROM employees + WHERE employees.status::text = 'active'::text + UNION + SELECT departments.id, + departments.name, + 'department'::text AS source_type + FROM departments + WHERE departments.manager_id IS NOT NULL + ) UNION ALL + SELECT employees.id, + COALESCE((employees.first_name::text || ' '::text) || employees.last_name::text, employees.name::text) AS name, + 'employee_full'::text AS source_type + FROM employees + WHERE employees.priority > 10) combined_data + WHERE id IS NOT NULL + ORDER BY source_type, id; diff --git a/testdata/diff/create_view/add_view/plan.txt b/testdata/diff/create_view/add_view/plan.txt index 4e1b6171..fe2b1cca 100644 --- a/testdata/diff/create_view/add_view/plan.txt +++ b/testdata/diff/create_view/add_view/plan.txt @@ -1,13 +1,14 @@ -Plan: 4 to add. +Plan: 5 to add. Summary by type: - views: 4 to add + views: 5 to add Views: + array_operators_view + cte_with_case_view + nullif_functions_view + text_search_view + + union_subquery_view DDL to be executed: -------------------------------------------------- @@ -80,3 +81,33 @@ CREATE OR REPLACE VIEW text_search_view AS to_tsvector('english'::regconfig, (((COALESCE(first_name, ''::character varying)::text || ' '::text) || COALESCE(last_name, ''::character varying)::text) || ' '::text) || COALESCE(bio, ''::text)) AS search_vector FROM employees WHERE status::text = 'active'::text; + +CREATE OR REPLACE VIEW union_subquery_view AS + SELECT id, + name, + source_type, + CASE + WHEN source_type = 'employee'::text THEN 'active'::text + WHEN source_type = 'department'::text THEN 'organizational'::text + ELSE 'unknown'::text + END AS category + FROM (( + SELECT employees.id, + employees.name, + 'employee'::text AS source_type + FROM employees + WHERE employees.status::text = 'active'::text + UNION + SELECT departments.id, + departments.name, + 'department'::text AS source_type + FROM departments + WHERE departments.manager_id IS NOT NULL + ) UNION ALL + SELECT employees.id, + COALESCE((employees.first_name::text || ' '::text) || employees.last_name::text, employees.name::text) AS name, + 'employee_full'::text AS source_type + FROM employees + WHERE employees.priority > 10) combined_data + WHERE id IS NOT NULL + ORDER BY source_type, id;