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: more Postgres compatibility measures for ActiveRecord #13429

Merged
merged 8 commits into from
Feb 10, 2017

Conversation

jordanlewis
Copy link
Member

@jordanlewis jordanlewis commented Feb 6, 2017

A few more compatibility features for ActiveRecord.

  • OID casts unwrap outer quotes from their inputs - '"table"'::regclass is equivalent to 'table'::regclass.
  • pg_type's entries for string types are now named the same as in Postgres: text and varchar.
  • pg_type's entry for decimal types are now named the same as in Postgres: numeric.
  • pg_namespace now has one entry per database.
  • Add the (empty) pg_extension virtual table.
  • Add (no-op) implementations of some advisory lock builtins.
  • Add the current_database builtin.
  • Add a new overload to obj_description.

Resolves #12153.
Resolves #12148.
Resolves #13436.
Resolves #13486.
Resolves #13485.
Resolves #13498.


This change is Reviewable

@cuongdo
Copy link
Contributor

cuongdo commented Feb 6, 2017

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


pkg/sql/parser/builtins.go, line 1406 at r3 (raw file):

				return NewDString(schemaForDb(ctx.Database)), nil
			},
			Info: "Returns the current database.",

This description is now outdated


pkg/sql/parser/builtins.go, line 1436 at r3 (raw file):

				return schemas, nil
			},
			Info: "Returns the current database; optionally include implicit schemas (e.g. " +

This is now outdated


pkg/sql/testdata/builtin_function, line 1147 at r2 (raw file):

SELECT CURRENT_SCHEMAS(true)
----
{pg_catalog,public}

These tests aren't part of this commit


pkg/sql/testdata/pgoidtype, line 12 at r1 (raw file):


query O
SELECT '"pg_constraint"'::REGCLASS

could you also test with some whitespace before and after pg_constraint?


Comments from Reviewable

@jordanlewis jordanlewis force-pushed the activerecord-fixes branch 2 times, most recently from 1a96562 to b852805 Compare February 6, 2017 19:39
@jordanlewis
Copy link
Member Author

TFTR!


Review status: 1 of 6 files reviewed at latest revision, 4 unresolved discussions.


pkg/sql/parser/builtins.go, line 1406 at r3 (raw file):

Previously, cuongdo (Cuong Do) wrote…

This description is now outdated

Done.


pkg/sql/parser/builtins.go, line 1436 at r3 (raw file):

Previously, cuongdo (Cuong Do) wrote…

This is now outdated

Done.


pkg/sql/testdata/builtin_function, line 1147 at r2 (raw file):

Previously, cuongdo (Cuong Do) wrote…

These tests aren't part of this commit

Done.


pkg/sql/testdata/pgoidtype, line 12 at r1 (raw file):

Previously, cuongdo (Cuong Do) wrote…

could you also test with some whitespace before and after pg_constraint?

Done.


Comments from Reviewable

@nvanbenschoten
Copy link
Member

Review status: 1 of 6 files reviewed at latest revision, 6 unresolved discussions, some commit checks pending.


pkg/sql/parser/builtins.go, line 1410 at r4 (raw file):

	},

	// For now, schemas are the same as databases. So, current_schemas returns the

This change should coordinate with #13404.


pkg/sql/testdata/pgoidtype, line 12 at r4 (raw file):


query OO
SELECT '"pg_constraint"'::REGCLASS, '  "pg_constraint" '::REGCLASS

Should something like ' pg_constraint '::REGCLASS (whitespace but no quotes) work?


Comments from Reviewable

@jordanlewis jordanlewis force-pushed the activerecord-fixes branch 3 times, most recently from 99b326f to 2520cb0 Compare February 6, 2017 23:10
@jordanlewis
Copy link
Member Author

Review status: 0 of 7 files reviewed at latest revision, 6 unresolved discussions.


pkg/sql/testdata/pgoidtype, line 12 at r4 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

Should something like ' pg_constraint '::REGCLASS (whitespace but no quotes) work?

Good point. Yes. That works now.


Comments from Reviewable

@nvanbenschoten
Copy link
Member

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


pkg/sql/parser/eval.go, line 2220 at r8 (raw file):

			// Trim whitespace and unwrap outer quotes if necessary.
			// This is required to mimic postgres.
			s = strings.Trim(s, " ")

It looks like newlines and tabs should also be stripped for whatever reason. strings.TrimSpaces is probably what we're looking for.


pkg/sql/parser/eval.go, line 2221 at r8 (raw file):

			// This is required to mimic postgres.
			s = strings.Trim(s, " ")
			s = unwrapQuotesRegexp.ReplaceAllString(s, "$1")

We don't really need a regexp anymore then:

if len(s) > 1 && s[0] == `"` && s[len(s)-1] == `"` {
  s = s[1:len(s)-1]
}

Comments from Reviewable

@jordanlewis
Copy link
Member Author

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


pkg/sql/parser/eval.go, line 2220 at r8 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

It looks like newlines and tabs should also be stripped for whatever reason. strings.TrimSpaces is probably what we're looking for.

Done.


pkg/sql/parser/eval.go, line 2221 at r8 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

We don't really need a regexp anymore then:

if len(s) > 1 && s[0] == `"` && s[len(s)-1] == `"` {
  s = s[1:len(s)-1]
}

Done.


Comments from Reviewable

@jordanlewis jordanlewis force-pushed the activerecord-fixes branch 2 times, most recently from 1cb8586 to f39d118 Compare February 7, 2017 15:31
@tamird
Copy link
Contributor

tamird commented Feb 7, 2017

Reviewed 1 of 2 files at r5, 6 of 6 files at r9, 3 of 3 files at r10, 3 of 3 files at r11, 3 of 3 files at r12.
Review status: all files reviewed at latest revision, 11 unresolved discussions, some commit checks pending.


pkg/sql/parser/builtins.go, line 1441 at r11 (raw file):

			},
			Info: "Returns the schema of the current current database; optionally " +
				"include implicit schemas (e.g. `pg_catalog`). CockroachDB " +

includes?


pkg/sql/parser/builtins.go, line 1664 at r11 (raw file):

const (
	// InformationSchemaName is the name of the information_schema table.

s/table/database/ on all three constants


pkg/sql/parser/builtins.go, line 1674 at r11 (raw file):

// schemaForDb maps a DatabaseDescriptor to its corresponding pgNamespace.
// See the comment above pgNamespace for more details.
func schemaForDb(db string) string {

how come not 'DB'?


pkg/sql/parser/builtins.go, line 1675 at r11 (raw file):

// See the comment above pgNamespace for more details.
func schemaForDb(db string) string {
	if db == InformationSchemaName || db == PgCatalogName || db == SystemName {

might read better as a switch-case


Comments from Reviewable

@jordanlewis
Copy link
Member Author

Review status: all files reviewed at latest revision, 11 unresolved discussions, some commit checks failed.


pkg/sql/parser/builtins.go, line 1441 at r11 (raw file):

Previously, tamird (Tamir Duberstein) wrote…

includes?

Done.


pkg/sql/parser/builtins.go, line 1664 at r11 (raw file):

Previously, tamird (Tamir Duberstein) wrote…

s/table/database/ on all three constants

Done.


pkg/sql/parser/builtins.go, line 1674 at r11 (raw file):

Previously, tamird (Tamir Duberstein) wrote…

how come not 'DB'?

Done.


pkg/sql/parser/builtins.go, line 1675 at r11 (raw file):

Previously, tamird (Tamir Duberstein) wrote…

might read better as a switch-case

Done.


Comments from Reviewable

@tamird
Copy link
Contributor

tamird commented Feb 7, 2017

Reviewed 1 of 4 files at r13, 3 of 3 files at r14.
Review status: all files reviewed at latest revision, 8 unresolved discussions, all commit checks successful.


pkg/sql/parser/builtins.go, line 1676 at r14 (raw file):

func schemaForDB(db string) string {
	switch db {
	case InformationSchemaName:

you can make this one single case

switch db {
case InformationSchemaName, PgCatalogName, SystemName:
  return db
default:
  return "public"
}

Comments from Reviewable

@jordanlewis
Copy link
Member Author

Review status: 3 of 7 files reviewed at latest revision, 8 unresolved discussions.


pkg/sql/parser/builtins.go, line 1676 at r14 (raw file):

Previously, tamird (Tamir Duberstein) wrote…

you can make this one single case

switch db {
case InformationSchemaName, PgCatalogName, SystemName:
  return db
default:
  return "public"
}

Done.


Comments from Reviewable

@tamird
Copy link
Contributor

tamird commented Feb 7, 2017

Reviewed 1 of 4 files at r15, 3 of 3 files at r16.
Review status: all files reviewed at latest revision, 7 unresolved discussions, all commit checks successful.


Comments from Reviewable

@jordanlewis
Copy link
Member Author

Reworked the current_schema commit after discussing with @knz about compatibility with his upcoming namespaces change. Now the current_schema behavior is left unchanged - but pg_namespace now reports a namespace per database instead of a single public namespace for all user-created databases.

Also added a few more commits with some new fixes for active record and updated the PR description to match.

@jordanlewis jordanlewis force-pushed the activerecord-fixes branch 3 times, most recently from 553e530 to 83bd928 Compare February 9, 2017 22:49
@knz
Copy link
Contributor

knz commented Feb 9, 2017

Awesome, thanks. One request about a builtin description, though. Looks otherwise fine to me.


Reviewed 1 of 3 files at r16, 1 of 4 files at r18, 3 of 3 files at r21, 1 of 1 files at r22.
Review status: 7 of 8 files reviewed at latest revision, 9 unresolved discussions, all commit checks successful.


pkg/sql/parser/builtins.go, line 1395 at r22 (raw file):

	},

	"current_database": {

Side note: The existence of both current_database and current_schema is bound to introduce confusion.
All the more reason to do something about #13319.


pkg/sql/parser/builtins.go, line 1421 at r22 (raw file):

				return NewDString(ctx.Database), nil
			},
			Info: "Returns the current database.",

Once current_database becomes available, please change the Info field of current_schema to say "This function is provided for compatibility with PostgreSQL. For a new CockroachDB application, consider using current_database() instead."


Comments from Reviewable

@jordanlewis
Copy link
Member Author

Review status: 3 of 8 files reviewed at latest revision, 8 unresolved discussions.


pkg/sql/parser/builtins.go, line 1421 at r22 (raw file):

Previously, knz (kena) wrote…

Once current_database becomes available, please change the Info field of current_schema to say "This function is provided for compatibility with PostgreSQL. For a new CockroachDB application, consider using current_database() instead."

Done.


Comments from Reviewable

Postgres unwraps inner quotes when doing oid casts, and some ORMs rely
on this behavior. For example, `'table'::REGCLASS` and
`'"table"'::REGCLASS` are treated the same.
For postgres compatibility. Strings are called either text or varchar
depending on their OID, despite their identical disk and syntax
representation in CockroachDB.
CockroachDB doesn't support extensions, so this is an empty table. It's
needed for ORM support.
pg_namespace now contains a single namespace for every database,
including system ones, named as the database is. This corrects the
broken link between what `current_schema` reports and the `pg_namespace`
virtual table - this link is required for ORM compatibility.

Previously, we implemented pg_namespace by exposing a single `public`
namespace to which every user-created database was linked. This is
inconsistent with Postgres as well as our future plans for
schemas/namespaces. We no longer expose any namespaces with the name
`public`.
These are required for ActiveRecord ORM support. We implement them by
returning true no matter what.
@jordanlewis
Copy link
Member Author

RFAL - is this good to go?

@knz
Copy link
Contributor

knz commented Feb 10, 2017 via email

@jordanlewis
Copy link
Member Author

I mainly didn't set you as a primary reviewer because you have been awfully busy - but I'm more than happy to take your LGTM if you're offering 😄

@jordanlewis jordanlewis merged commit 71e7475 into cockroachdb:master Feb 10, 2017
@jordanlewis jordanlewis deleted the activerecord-fixes branch February 10, 2017 20:33
@bdarnell
Copy link
Contributor

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


pkg/sql/parser/builtins.go, line 1680 at r30 (raw file):

			ReturnType: fixedReturnType(TypeBool),
			fn: func(_ *EvalContext, _ Datums) (Datum, error) {
				return DBoolTrue, nil

I'm a little late to this PR, but it seems bad to lie about locking like this. Even if ActiveRecord is (currently) OK with us stubbing out this lock, implementing this function in a silently broken way could cause problems for other frameworks in the future.


Comments from Reviewable

@jordanlewis
Copy link
Member Author

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


pkg/sql/parser/builtins.go, line 1680 at r30 (raw file):

Previously, bdarnell (Ben Darnell) wrote…

I'm a little late to this PR, but it seems bad to lie about locking like this. Even if ActiveRecord is (currently) OK with us stubbing out this lock, implementing this function in a silently broken way could cause problems for other frameworks in the future.

Good point. Opened #13546.


Comments from Reviewable

@sandstrom
Copy link

You should mention in your docs somewhere, that there are known limitations with your ActiveRecord compatibility and explain that these things are (mentioning this one).

/~https://github.com/rails/rails/blob/8de10f9bb6edaf10bde21cea73db3da2aac6f6de/activerecord/lib/active_record/migration.rb#L151

@brandondrew
Copy link

@jordanlewis
Copy link
Member Author

It stands for "thanks for the review"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants