Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View definition falls in rule.sql for specific cases #770

Open
shubham-yb opened this issue Feb 7, 2023 · 0 comments
Open

View definition falls in rule.sql for specific cases #770

shubham-yb opened this issue Feb 7, 2023 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@shubham-yb
Copy link
Contributor

Boiled down to group by behaviours when dealing with views.

Case 1: All columns specified in the view must be a part of the group by statement. Or the DDL creation will fail.
ex:

create table foo(n1 int, n2 int);
create view v1 as select n1,n2 from foo group by n1;

ERROR:  column "foo.n2" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: create view v1 as select n1,n2 from foo group by n1;

Case 2: However there’s an exception to this. If all the columns which are a part of the primary key of the tables used for the view are present in the group by clause, DDL creation will go through.
ex:

alter table foo add constraint foo_pk primary key(n1);
create view v1 as select n1,n2 from foo group by n1;
CREATE VIEW

If the view is created using this exception, the correct DDL will fall under rule.sql and the incorrect DDL will fall under view.sql
rule.sql

CREATE OR REPLACE VIEW public.v1 AS
 SELECT foo.n1,
    foo.n2
   FROM public.foo
  GROUP BY foo.n1;

view.sql

CREATE VIEW public.v1 AS
SELECT
    NULL::integer AS n1,
    NULL::integer AS n2;

Case 3: If the group by clause has all the columns mentioned in the view, the DDLs will fall under expected files.

create view v2 as select n1,n2 from foo group by n1,n2;
CREATE VIEW

view.sql

CREATE VIEW public.v2 AS
 SELECT foo.n1,
    foo.n2
   FROM public.foo
  GROUP BY foo.n1, foo.n2;
@shubham-yb shubham-yb self-assigned this Feb 7, 2023
@github-actions github-actions bot added the triage Needs to be triaged label Feb 7, 2023
@shubham-yb shubham-yb removed their assignment Feb 14, 2023
@shubham-yb shubham-yb added documentation Improvements or additions to documentation and removed triage Needs to be triaged labels Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant