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: various updates for sqlalchemy compatibility #38318

Merged
merged 5 commits into from
Jun 21, 2019

Conversation

jordanlewis
Copy link
Member

This PR contains several compatibility fixes for problems discovered by running
the SQLAlchemy test suite against CockroachDB. A followup PR will add that test
suite to our list of ORM test suites.

  • make pg_get_constraintdef output identical to Postgres for CHECK constraints
  • add length, scale, precision output to format_type() when passed a typmod argument
  • print foreign key constraint strings without an extra space, like Postgres does
  • add pg_type_is_visible builtin that always returns true if its input is a real type
  • support int2vector::string casts in the same way Postgres does

@jordanlewis jordanlewis requested review from knz and a team June 20, 2019 11:39
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits. Nice sleuthing to figure out how typmod works.

Reviewed 6 of 6 files at r1, 3 of 3 files at r2, 6 of 6 files at r3, 1 of 1 files at r4, 4 of 4 files at r5.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis)


pkg/sql/pgwire/types.go, line 179 at r5 (raw file):

	case *tree.DArray:
		// Arrays have custom formatting depending on their OID

nit: period at end of sentence.


pkg/sql/sem/builtins/pg_builtins.go, line 882 at r4 (raw file):

	),

	// pg_type returns true if the input oid corresponds to a type

have you checked the comment is still pertaining to this function? asking before the function name doesn't match.


pkg/sql/types/types.go, line 1008 at r2 (raw file):

		}
	case IntervalFamily:
		// TODO(jordan): intervals can have typmods, but we don't support them yet.

this could be best served with an error return with an unimplemented error.


pkg/sql/types/types.go, line 1049 at r2 (raw file):

			return "name"
		default:
			panic(errors.AssertionFailedf("unexpected OID: %d", t.Oid()))

ditto panic -> error return

@jordanlewis jordanlewis requested a review from a team as a code owner June 20, 2019 19:21
@jordanlewis jordanlewis requested a review from a team June 20, 2019 19:21
@jordanlewis jordanlewis requested a review from a team as a code owner June 20, 2019 19:21
Copy link
Member Author

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)


pkg/sql/sem/builtins/pg_builtins.go, line 882 at r4 (raw file):

Previously, knz (kena) wrote…

have you checked the comment is still pertaining to this function? asking before the function name doesn't match.

Oops. I did write this comment, but messed up the function name. Done.


pkg/sql/types/types.go, line 1008 at r2 (raw file):

Previously, knz (kena) wrote…

this could be best served with an error return with an unimplemented error.

Actually we want to make sure this always works, because clients will call format_type with interval and not expect an error.


pkg/sql/types/types.go, line 1049 at r2 (raw file):

Previously, knz (kena) wrote…

ditto panic -> error return

I'll leave this larger refactor for another time (lots of callsites to change).

@jordanlewis
Copy link
Member Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jun 20, 2019

Build failed (retrying...)

@craig
Copy link
Contributor

craig bot commented Jun 20, 2019

Build failed

Previously, we output CHECK constraints slightly differently than
Postgres, which confused SQLAlchemy. Specifically, Postgres always
outputs CHECK constraints with 2 (count 'em) sets of wrapping
parentheses. Now, we do that too!

Release note (sql change): pg_get_constraintdef now outputs CHECK
constraints exactly the same way that Postgres does.
Previously, our implementation of the pg builtin format_type ignored its
second argument, the type modifier, which indicates the length, scale,
or precision of the datatype. Now, it correctly heeds that argument and
produces a formatted type that includes length, scale, or precision,
when appropriate.

Release note (sql change): the format_type Postgres builtin now properly
respects its second argument.
Previously, foreign keys were printed slightly differently in SHOW
CREATE TABLE than Postgres prints them, which led to incompatibility
with SQLAlchemy.

Release note (sql change): SHOW CREATE TABLE now prints foreign keys
more like Postgres does.
Release note (sql change): the pg_type_is_visible builtin from Postgres
is now implemented.
Previously, casting an int2vector to a string would fail to use the
special int2vector formatting (space-separated elements instead of the
normal {}-wrapped and comma-separated elements format of arrays). This
was causing issues with SQLAlchemy.

This commit solves this problem and simplifies the code by removing the
DOidWrapper usages for int2vector and oidvector, in favor of a field on
DArray that contains a custom oid. This opens the door to teaching
DArray's format method about the special formatting, rather than having
it live only in pgwire, where it fails to be useful in the cast of e.g.
casts.

Release note (sql change): casting an int2vector to a string now
produces a Postgres-compatible result.
@jordanlewis
Copy link
Member Author

Fixed some test merge skew.

bors r+

craig bot pushed a commit that referenced this pull request Jun 21, 2019
38318: sql: various updates for sqlalchemy compatibility r=jordanlewis a=jordanlewis

This PR contains several compatibility fixes for problems discovered by running
the SQLAlchemy test suite against CockroachDB. A followup PR will add that test
suite to our list of ORM test suites.

- make pg_get_constraintdef output identical to Postgres for CHECK constraints
- add length, scale, precision output to format_type() when passed a typmod argument
- print foreign key constraint strings without an extra space, like Postgres does
- add pg_type_is_visible builtin that always returns true if its input is a real type
- support int2vector::string casts in the same way Postgres does

Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
@craig
Copy link
Contributor

craig bot commented Jun 21, 2019

Build succeeded

@craig craig bot merged commit 3f4d1a0 into cockroachdb:master Jun 21, 2019
@jordanlewis jordanlewis deleted the sqlalchemy-updates branch June 25, 2019 21:17
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