-
Notifications
You must be signed in to change notification settings - Fork 894
Set column name explicitely when using current_database()
in queries
#3526
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
Set column name explicitely when using current_database()
in queries
#3526
Conversation
I think I'd rather change all of the code referring to current_database to not use SQL at all. The connection knows the database. I don't thin it is necessary to use this function |
@davecramer are you thinking about reverting #3390 ? |
Probably not reverting it but changing the way it is done. |
Frankly, I see no issues leaving it as It would make query easier to understand and test. It would naturally avoid "concatenate string sql on every execution" as well. |
void getColumnsForSchema() throws Exception { | ||
DatabaseMetaData dbmd = conn.getMetaData(); | ||
|
||
ResultSet rs = dbmd.getColumns(null, "%", "decimaltest", "%"); | ||
assertTrue(rs.next()); | ||
assertEquals("a", rs.getString("COLUMN_NAME")); | ||
assertEquals(0, rs.getInt("DECIMAL_DIGITS")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a verification for the current_database
column name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added it to the test. Notice that getColumns
reports the current database in the TABLE_CAT
column: https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java#L1671
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought current_database
was returned via client=facing APIs, however, now I see it is used only in the internals.
However, it looks like the specific usage could be completely removed from SQL and we could use something like
byte[] catalogName = /* rs.getBytes("current_database") */; // replace with a known literal
...
tuple[0] = catalogName;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I had this in mind as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latest commit is removing current_database()
where possible and use either the argument or use the connection's catalog name.
If #3528 fixes all of the performance regressions then we can leave this one as is. |
Thank you for taken the time to review my PR. I have expanded the test as outlined in #3526 (review) |
It looks like the startup parameters do not return the database name. At the same time, there's So it looks like we should use those functions rather than |
Interesting, somehow I though it was.
Only if it creates a performance regression. |
Ok, PostgreSQL has a regression test that ensures At the same time, it looks like So it is fine to keep using |
@@ -57,6 +57,13 @@ public PgDatabaseMetaData(PgConnection conn) { | |||
private int nameDataLength; // length for name datatype | |||
private int indexMaxKeys; // maximum number of keys in an index. | |||
|
|||
private byte[] getCatalogName(@Nullable String catalog) throws SQLException { | |||
if (catalog == null) { | |||
return connection.getCatalog().getBytes(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use connection's encoding here. Otherwise there's a risk the bytes will be decoded differently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point - I have added the encoded
Assuming no performance regression |
@davecramer @vlsi Do you need any changes before you can merge my PR? When merging, you should probably merge #3528 too so the performance is improved. |
2fc2e79
to
ab8e2fb
Compare
ab8e2fb
to
de7a693
Compare
798d192
to
4766e04
Compare
88373e1
to
ceeca4d
Compare
@davecramer @vlsi I have rebased my branch on |
} | ||
return catalog.getBytes(Charset.defaultCharset()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I can tell we check to see if the catalog is null before calling this function.
Moot point: If we get a SQLException we should just throw it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many checks like the following in the code:
if (catalog != null) {
sql += " AND current_database() = " + escapeQuotes(catalog);
}
They will not exclude null
. Likewise, the additional checks in #3588 don't exclude catalog
to be null
afaik.
Removing the try
/catch
makes sense as errors can bubble up to the application.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In retrospect I should have removed all of those in #3588. By the time we get to the query we know that if the catalog is not null then it is the same as current_database()
If you want to make those changes I'd appreciate it. Otherwise I'll make them which might make your's harder to rebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look at it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davecramer @vlsi I gave it a shot, and I took the liberty to modify the condition as suggested in #3588 (comment)
There's still a considerable amount of whitespace changes in this PR. Mostly around ( and beginning lines. |
7764e57
to
8e6f12c
Compare
@davecramer says
I have restored the white spaces |
40414fe
to
5f5eb0f
Compare
Yes, checkstyle seems a little specific at times, but one more fix and we should be good to go |
... and done 😄 |
Thank you for merging |
Thank you for persisting through this process |
All Submissions:
Postgres compatible databases like https://github.com/crate/crate can use PG JDBC as driver. Often the PG JDBC driver is used by a tool or integration (JetBrains DataGrip to name one). As CrateDB isn't fully PG compatible, this patch will make life a bit easier for our users. The issue has been reported as crate/crate#17393.