Skip to content

sql: add pg_catalog.pg_get_viewdef#18618

Merged
jordanlewis merged 1 commit intocockroachdb:masterfrom
bsnyder788:support-pg_get_viewdef
Sep 21, 2017
Merged

sql: add pg_catalog.pg_get_viewdef#18618
jordanlewis merged 1 commit intocockroachdb:masterfrom
bsnyder788:support-pg_get_viewdef

Conversation

@bsnyder788
Copy link
Copy Markdown
Contributor

@jordanlewis I wasn't able to re-use the SHOW CREATE VIEW code as it actually returns a different format than PostgreSQL pg_get_viewdef. Thus, I had to extract it from the pg_catalog.pg_views table. Let me know if the SHOW CREATE VIEW format is indeed okay for this purpose in Cockroach and I can easily modify it to leverage that. Also I couldn't tell the difference on the overloaded function with the boolean when setting up some fake data in PostgreSQL 9.6. If you have a good example where true and false would yield different outputs I can update that portion of the PR as well. Thanks!

@bsnyder788 bsnyder788 requested review from a team September 20, 2017 14:29
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@jordanlewis
Copy link
Copy Markdown
Member

Thanks for the contribution! A few comments enclosed.

The difference between the true and false argument is pretty minor I think and I only discovered it by looking at the Postgres source code. It controls whether the output is formatted using disambiguating parentheses.

For example:

tpcc=# create view a as select 1+2 from customer;
CREATE VIEW
tpcc=# select pg_get_viewdef('a'::regclass, false);
  pg_get_viewdef
-------------------
  SELECT (1 + 2)  +
    FROM customer;
(1 row)

tpcc=# select pg_get_viewdef('a'::regclass, true);
  pg_get_viewdef
-------------------
  SELECT 1 + 2    +
    FROM customer;
(1 row)

I'm fine with us ignoring this difference entirely.


Review status: 0 of 2 files reviewed at latest revision, 3 unresolved discussions, all commit checks successful.


pkg/sql/logictest/testdata/logic_test/builtin_function, line 1605 at r1 (raw file):


query T
SELECT pg_catalog.pg_get_viewdef((SELECT oid FROM pg_class WHERE relname='pg_viewdef_view'))

There's a shortcut for this, so you don't have to do this subselect: 'pg_viewdef_view'::regclass'.


pkg/sql/parser/pg_builtins.go, line 231 at r1 (raw file):

				}
				if len(r) == 0 {
					return nil, pgerror.NewErrorf(pgerror.CodeInvalidParameterValueError, "unknown view (OID=%s)", args[0])

This should return NULL like it does in Postgres, not an error.


pkg/sql/parser/pg_builtins.go, line 237 at r1 (raw file):

			Info: notUsableInfo,
		},
		Builtin{

It's fine that we have no special behavior in this overload, but it seems unnecessary to me to repeat the code in both overloads. Could you DRY this definition?


Comments from Reviewable

@knz
Copy link
Copy Markdown
Contributor

knz commented Sep 20, 2017

Reviewed 2 of 2 files at r1.
Review status: all files reviewed at latest revision, 3 unresolved discussions, all commit checks successful.


Comments from Reviewable

@bsnyder788
Copy link
Copy Markdown
Contributor Author

sounds good!


Comments from Reviewable

@bsnyder788
Copy link
Copy Markdown
Contributor Author

pkg/sql/logictest/testdata/logic_test/builtin_function, line 1605 at r1 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

There's a shortcut for this, so you don't have to do this subselect: 'pg_viewdef_view'::regclass'.

I will update with this, thanks for the pointer.


Comments from Reviewable

@bsnyder788
Copy link
Copy Markdown
Contributor Author

pkg/sql/parser/pg_builtins.go, line 231 at r1 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

This should return NULL like it does in Postgres, not an error.

Agreed.


Comments from Reviewable

@bsnyder788
Copy link
Copy Markdown
Contributor Author

pkg/sql/parser/pg_builtins.go, line 237 at r1 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

It's fine that we have no special behavior in this overload, but it seems unnecessary to me to repeat the code in both overloads. Could you DRY this definition?

Sounds good, I will refactor it.


Comments from Reviewable

@bsnyder788 bsnyder788 force-pushed the support-pg_get_viewdef branch from b9d2ed9 to 2c619d7 Compare September 20, 2017 18:57
@bsnyder788
Copy link
Copy Markdown
Contributor Author

pkg/sql/logictest/testdata/logic_test/builtin_function, line 1605 at r1 (raw file):

Previously, bsnyder788 (Brett Snyder) wrote…

I will update with this, thanks for the pointer.

Done.


Comments from Reviewable

@bsnyder788
Copy link
Copy Markdown
Contributor Author

pkg/sql/parser/pg_builtins.go, line 231 at r1 (raw file):

Previously, bsnyder788 (Brett Snyder) wrote…

Agreed.

Done.


Comments from Reviewable

@bsnyder788
Copy link
Copy Markdown
Contributor Author

pkg/sql/parser/pg_builtins.go, line 237 at r1 (raw file):

Previously, bsnyder788 (Brett Snyder) wrote…

Sounds good, I will refactor it.

Done.


Comments from Reviewable

@jordanlewis
Copy link
Copy Markdown
Member

:lgtm: thanks again!


Reviewed 2 of 2 files at r1, 2 of 2 files at r2.
Review status: :shipit: all files reviewed at latest revision, all discussions resolved, all commit checks successful.


Comments from Reviewable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants