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

cherry-pick 2.0: schema-related fixes #23041

Merged
merged 7 commits into from
Feb 26, 2018

Conversation

knz
Copy link
Contributor

@knz knz commented Feb 24, 2018

Picks #22828.
Picks #22997.
Picks #22998.
Picks #22994.
Picks #23040.
Picks #23044.

knz added 4 commits February 24, 2018 12:55
Release note (sql change): the special identifier `current_catalog` is
supported as alias for `current_database()` for better compatibility
with PostgreSQL.
Release note (sql change): `current_role` is recognized as an alias
for `current_user` for better compatibility with PostgreSQL.
…standard

At least one client app (Zippix) uses `SET SCHEMA`, defined by the SQL
standard.  `SET SCHEMA` is used by an app after discovering the
available schemas in the `information_schema` tables. Postgres
implements `SET SCHEMA` as an alias for `SET search_path =`, since the
"current schema" in pg's dialect is defined exactly as "the first item
in `search_path`".

This patch makes CockroachDB do the same as pg.

Release note (sql change): CockroachDB now recognizes the special
syntax `SET SCHEMA <name>` as an alias for `SET search_path = <name>`
for better compatibility with PostgreSQL.
Now that SHOW TABLES only shows tables in the public schema by
default, and SET DATABASE only supports real databases and not
(virtual) schemas, there is no simple way for a user to realize that
there are multiple schemas in the first place, and which virtual
schemas exist / are recognized.

Release note (sql change): the new statement `SHOW SCHEMAS` reveals
which are the valid virtual schemas next to the physical schema
`public`.
@knz knz requested review from a team February 24, 2018 11:58
@cockroach-teamcity
Copy link
Member

This change is Reviewable

knz added 2 commits February 24, 2018 15:14
With the introduction of compatibility with schema specifications,
SHOW TABLES was modified to recognize the following two forms:

     SHOW TABLES FROM <db>
     SHOW TABLES FROM <db> . <schema>

So one could, for example, issue `SHOW TABLES FROM system.pg_catalog`
to see all tables in `system`'s `pg_catalog`.

However this change broke a use case, as users were previously able to
use `SHOW TABLES` directly on virtual schemas, e.g. `SHOW TABLES FROM
crdb_internal`.

This patch restores this functionality, by making `SHOW TABLES`
resolve its parameter as if it was a star prefix in GRANT.  That is,
`SHOW TABLES FROM crdb_internal` will first try to find a database
called `crdb_internal`, and if that fails will try the current
database's schema called `crdb_internal`.

Release note (bug fix): `SHOW TABLES` is now again able to inspect
virtual schemas.
Requested/suggested by @rytaft: "pattern" is a bit of a technical word
and is not necessary to convey the right meaning in the error message.

Release note: None
@knz knz requested review from a team February 24, 2018 16:19
@knz knz changed the title [WIP] cherry-pick 2.0: schema-related fixes cherry-pick 2.0: schema-related fixes Feb 25, 2018
@knz knz requested a review from rytaft February 25, 2018 00:17
@knz
Copy link
Contributor Author

knz commented Feb 25, 2018

cc @cockroachdb/release

Prior to this patch, an attempt to populate one of the virtual schemas
with a DDL statement would fail with the error "unsupported schema
specification". This is not extremely clear, also incorrect on its
face, since the virtual schemas are actually supported.

This patch clarifies the error by reporting "schema cannot be
modified" more explicitly.

Suggested/requested by @rytaft.

Release note (sql change): attempts to modify virtual schemas with DDL
statements now fail with a clearer error message.
@rytaft
Copy link
Collaborator

rytaft commented Feb 25, 2018

:lgtm:


Reviewed 9 of 12 files at r4, 8 of 13 files at r5, 5 of 5 files at r6, 4 of 4 files at r7.
Review status: all files reviewed at latest revision, 3 unresolved discussions, all commit checks successful.


pkg/sql/parser/sql.y, line 2874 at r7 (raw file):

    $$.val = &tree.ShowTables{TableNamePrefix:tree.TableNamePrefix{
        // Note: the schema name may be interpreted as database name,
	// see name_resolution.go.

[nit] remove tab character


pkg/sql/sem/tree/show.go, line 165 at r7 (raw file):

}

// ShowSchemas represents a SHOW TABLES statement.

SHOW TABLES -> SHOW SCHEMAS


pkg/sql/sem/tree/table_pattern.go, line 67 at r7 (raw file):

func (at *AllTablesSelector) Format(ctx *FmtCtx) {
	at.TableNamePrefix.Format(ctx)
	alwaysFormatPrefix := ctx.flags.HasFlags(FmtAlwaysQualifyTableNames) || ctx.tableNameFormatter != nil

Might be good to move this line and the following if statement to a helper function (e.g., func formatPrefix() bool) since it's repeated a few times.


Comments from Reviewable

@knz knz mentioned this pull request Feb 26, 2018
@knz
Copy link
Contributor Author

knz commented Feb 26, 2018

Becca I'll follow up on your requests in #23072. This PR here is merely a cherry-pick, I can't make changes here without first issuing a PR against master.

@knz knz merged commit 708b8cc into cockroachdb:release-2.0 Feb 26, 2018
@knz knz deleted the 20180224-schema-followups branch February 26, 2018 11:21
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.

3 participants