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

sql: make database, table, view and columns lookups properly case-sensitive #16884

Merged
merged 2 commits into from
Jul 17, 2017

Conversation

knz
Copy link
Contributor

@knz knz commented Jul 6, 2017

sql: sanitize the production of error message for tables/views

  • use the same word "relation" for tables and views for permission
    errors and "descriptor does not exist" errors, for consistency with
    the existing "already exists" message that already uses the word
    "relation".

  • ensure that names containing special characters are not
    doubly quoted in the error message.

sql: make table and view lookups properly case-sensitive

An earlier change introduced pre-normalization of descriptor names
upon descriptor creation, thereby aiming for two goals:

  • normalize descriptor names upon creation of the descriptor, so as to
    avoid the time overhead of re-normalizing the name on every access;

  • creating a distinction, like one exists in postgres, between
    descriptors created with the syntax "A" and the syntax A - the
    latter is normalized, the former is not. This makes case sensitivity
    opt-in for client applications.

Unfortunately, prior to this patch, most SQL statements also used a
case-insensitive lookup of database/table/view/column names from
storage, preventing the aforementioned benefits.

This patch completes the earlier change by making name lookups
case-sensitive. An exhaustive set of tests is modified/introduced to confirm
that the change is invisible to most common use cases -- i.e. as long
as client apps were not double quoting their identifiers.

Fixes #8862.
Fixes #16858.

@knz knz requested a review from jordanlewis July 6, 2017 15:23
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@knz knz force-pushed the 20170706-table-view-msgs branch 2 times, most recently from 4f9bd2d to 1ba4ad7 Compare July 6, 2017 15:54
@knz knz changed the title sql: sanitize the production of error message for tables/views sql: make table and view lookups properly case-sensitive Jul 6, 2017
@knz knz requested a review from dt July 6, 2017 15:56
@knz knz force-pushed the 20170706-table-view-msgs branch from 1ba4ad7 to 5f50cb9 Compare July 6, 2017 22:46
@knz knz changed the title sql: make table and view lookups properly case-sensitive sql: make database, table, view and columns lookups properly case-sensitive Jul 6, 2017
@knz knz requested a review from eisenstatdavid July 6, 2017 22:47
@knz
Copy link
Contributor Author

knz commented Jul 6, 2017

Added the part for column names too.

@knz knz force-pushed the 20170706-table-view-msgs branch 7 times, most recently from 8cda01e to 87da655 Compare July 7, 2017 13:24
@eisenstatdavid
Copy link

Thanks so much for your hard work on this! Aside from some nits (noted below), my main concern is that there are no new/altered tests concerned with Unicode normalization.

Also, has management approved this behavior change?


Reviewed 38 of 48 files at r1, 56 of 56 files at r2.
Review status: all files reviewed at latest revision, 7 unresolved discussions, all commit checks successful.


pkg/ccl/sqlccl/targets.go, line 35 at r2 (raw file):

	starByDatabase := make(map[string]validity, len(targets.Databases))
	for _, d := range targets.Databases {
		starByDatabase[string(d)] = maybeValid

Can you change the type of the map instead of converting everywhere?


pkg/sql/alter_table.go, line 89 at r2 (raw file):

				return err
			}
			_, dropped, err := n.tableDesc.FindColumnByName(string(d.Name))

Could we change the signature of FindColumnByName instead?


pkg/sql/data_source.go, line 677 at r2 (raw file):

		if !ok {
			return nil, nil, fmt.Errorf("relation %q not found",
				parser.AsStringWithFlags(&tableName, parser.FmtBareIdentifiers))

Is this a change that should be made more systematically?


pkg/sql/drop.go, line 433 at r2 (raw file):

			}
			// View does not exist, but we want it to: error out.
			return nil, sqlbase.NewUndefinedTableError(tn)

Perhaps rename to NewUndefinedRelationError?


pkg/sql/table.go, line 388 at r2 (raw file):

			return nil, sqlbase.NewUndefinedTableError(
				&parser.TableName{
					TableName:               parser.Name(fmt.Sprintf("<id=%d>", tableID)),

I vaguely remember that the parser accepts a different syntax for specifying tables by ID? Perhaps I'm confused.


pkg/sql/logictest/testdata/logic_test/case_sensitive_names, line 36 at r2 (raw file):

SHOW CREATE TABLE "A"

statement error relation "test.A" does not exist

Is there a good reason that this one is not like the others?


pkg/sql/logictest/testdata/logic_test/select, line 173 at r2 (raw file):


query T
SELECT "foo"."v" FROM kv AS foo WHERE foo.k = 'a'

Are the quotes still needed?


Comments from Reviewable

@knz knz force-pushed the 20170706-table-view-msgs branch 2 times, most recently from 4384026 to 5c193f4 Compare July 14, 2017 21:36
@knz
Copy link
Contributor Author

knz commented Jul 14, 2017

@bdarnell said "ok as long as it brings us closer to pg compatibility".


Review status: 69 of 99 files reviewed at latest revision, 7 unresolved discussions.


pkg/ccl/sqlccl/targets.go, line 35 at r2 (raw file):

Previously, eisenstatdavid (David Eisenstat) wrote…

Can you change the type of the map instead of converting everywhere?

Not comfortable about that: there are more uses below which do not convert.


pkg/sql/alter_table.go, line 89 at r2 (raw file):

Previously, eisenstatdavid (David Eisenstat) wrote…

Could we change the signature of FindColumnByName instead?

Done.


pkg/sql/data_source.go, line 677 at r2 (raw file):

Previously, eisenstatdavid (David Eisenstat) wrote…

Is this a change that should be made more systematically?

Done.


pkg/sql/drop.go, line 433 at r2 (raw file):

Previously, eisenstatdavid (David Eisenstat) wrote…

Perhaps rename to NewUndefinedRelationError?

Done.


pkg/sql/table.go, line 388 at r2 (raw file):

Previously, eisenstatdavid (David Eisenstat) wrote…

I vaguely remember that the parser accepts a different syntax for specifying tables by ID? Perhaps I'm confused.

Not yet. This feature will only come when #15388 is merged (or the underlying commit which adds the syntax).


pkg/sql/logictest/testdata/logic_test/case_sensitive_names, line 36 at r2 (raw file):

Previously, eisenstatdavid (David Eisenstat) wrote…

Is there a good reason that this one is not like the others?

Fixed.


pkg/sql/logictest/testdata/logic_test/select, line 173 at r2 (raw file):

Previously, eisenstatdavid (David Eisenstat) wrote…

Are the quotes still needed?

they are not needed, it's only a smoke test to check that the double quote syntax is allowed also in expression context.


Comments from Reviewable

@knz knz force-pushed the 20170706-table-view-msgs branch from 5c193f4 to 4009728 Compare July 14, 2017 21:50
@knz knz force-pushed the 20170706-table-view-msgs branch 2 times, most recently from a2dcf44 to 4240905 Compare July 15, 2017 16:24
@knz
Copy link
Contributor Author

knz commented Jul 15, 2017

Review status: 69 of 100 files reviewed at latest revision, 7 unresolved discussions.


pkg/sql/table.go, line 388 at r2 (raw file):

Previously, knz (kena) wrote…

Not yet. This feature will only come when #15388 is merged (or the underlying commit which adds the syntax).

Scratch that, actually we supported the feature already. Changed.


Comments from Reviewable

@knz knz force-pushed the 20170706-table-view-msgs branch from 4240905 to d9092ed Compare July 17, 2017 18:17
@knz
Copy link
Contributor Author

knz commented Jul 17, 2017

Added a unicode normalization test as requested.

@eisenstatdavid
Copy link

:lgtm_strong: Thanks again!


Reviewed 31 of 31 files at r3.
Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


Comments from Reviewable

@knz knz force-pushed the 20170706-table-view-msgs branch from d9092ed to 4cfcde8 Compare July 17, 2017 19:08
knz added 2 commits July 17, 2017 19:22
- use the same word "relation" for tables and views for permission
  errors and "descriptor does not exist" errors, for consistency with
  the existing "already exists" message that already uses the word
  "relation".

- ensure that names containing special characters are not
  double-quoted in the error message.

- use pg error codes more often.
An earlier change introduced pre-normalization of descriptor names
upon descriptor creation, thereby aiming for two goals:

- normalize descriptor names upon creation of the descriptor, so as to
  avoid the time overhead of re-normalizing the name on every access;

- creating a distinction, like one exists in postgres, between
  descriptors created with the syntax `"A"` and the syntax `A` - the
  latter is normalized, the former is not. This makes case sensitivity
  opt-in for client applications.

Unfortunately, prior to this patch, most SQL statements also used a
case-insensitive *lookup* of database/table/view/column names from
storage, preventing the aforementioned benefits.

This patch completes the earlier change by making name lookups
case-sensitive. An exhaustive test is modified/introduced to confirm
that the change is invisible to most common use cases -- i.e. as long
as client apps were not double quoting their identifiers.

Fixes cockroachdb#8862.
Fixes cockroachdb#16858.
@knz knz force-pushed the 20170706-table-view-msgs branch from 4cfcde8 to 9926f25 Compare July 17, 2017 19:22
@knz
Copy link
Contributor Author

knz commented Jul 17, 2017

TFYRs!

@knz knz merged commit 00ad837 into cockroachdb:master Jul 17, 2017
@knz knz deleted the 20170706-table-view-msgs branch July 17, 2017 19:52
knz added a commit to knz/cockroach that referenced this pull request Feb 4, 2018
There was a time in the past where CockroachDB was (or attempted to
be) case-insensitive about identifiers, and this was a source of
incompatibility with PostgreSQL.

This incompatibility was corrected for object
(table/view/database/index) names and column names in cockroachdb#16884, but
somehow I forgot about function names in that patch. As a result,
CockroachDB continued to consider `upper()` and `"UPPER"()` the same
thing, whereas most definitely only the former is valid.

This patch corrects the issue and simplifies the code accordingly.

Release note (sql change): CockroachDB now properly rejects
incorrectly cased SQL function names with an error.
knz added a commit that referenced this pull request Feb 5, 2018
There was a time in the past where CockroachDB was (or attempted to
be) case-insensitive about identifiers, and this was a source of
incompatibility with PostgreSQL.

This incompatibility was corrected for object
(table/view/database/index) names and column names in #16884, but
somehow I forgot about function names in that patch. As a result,
CockroachDB continued to consider `upper()` and `"UPPER"()` the same
thing, whereas most definitely only the former is valid.

This patch corrects the issue and simplifies the code accordingly.

Release note (sql change): CockroachDB now properly rejects
incorrectly cased SQL function names with an error.
knz added a commit to knz/cockroach that referenced this pull request Feb 6, 2018
There was a time in the past where CockroachDB was (or attempted to
be) case-insensitive about identifiers, and this was a source of
incompatibility with PostgreSQL.

This incompatibility was corrected for object
(table/view/database/index) names and column names in cockroachdb#16884, but
somehow I forgot about function names in that patch. As a result,
CockroachDB continued to consider `upper()` and `"UPPER"()` the same
thing, whereas most definitely only the former is valid.

This patch corrects the issue and simplifies the code accordingly.

Release note (sql change): CockroachDB now properly rejects
incorrectly cased SQL function names with an error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants