-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Quote column names for ADD
, RENAME
and DROP COLUMN
in JDBC connectors
#13878
Conversation
a4bd8fe
to
d34fa91
Compare
ADD
, RENAME
and DROP COLUMN
in JDBC connectors
2c992e2
to
16534ae
Compare
plugin/trino-druid/src/test/java/io/trino/plugin/druid/BaseDruidConnectorTest.java
Outdated
Show resolved
Hide resolved
plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/TestMySqlLegacyConnectorTest.java
Outdated
Show resolved
Hide resolved
plugin/trino-sqlserver/src/test/java/io/trino/plugin/sqlserver/BaseSqlServerConnectorTest.java
Outdated
Show resolved
Hide resolved
testing/trino-testing/src/main/java/io/trino/testing/BaseConnectorTest.java
Outdated
Show resolved
Hide resolved
} | ||
|
||
@Test(dataProvider = "testColumnNameDataProvider") | ||
public void testDropColumnName(String columnName) |
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.
Let's merge this with testAddColumnName
, since the setup isn't trivial (CREATE + exception handling)
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.
Do you mean testRenameColumnName
? The setup in testAddColumnName
creates a table without error handling.
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 meant to merge this with testAddColumnName
.
that would mean two lines more in that test
assertUpdate("ALTER TABLE " + tableName + " DROP COLUMN " + nameInSql);
assertTableColumnNames(tableName, "value");
ws the whole boilerplate of this method.
You can also rename the test method to testAddAndDropColumnName
16534ae
to
ddb4fb4
Compare
d3eef3e
to
3e7d20c
Compare
Addressed comments. |
|
||
private static String escape(String name) | ||
{ | ||
return name.replace("'", "'" + "'"); |
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.
"'" + "'"
-> "''"
} | ||
assertTableColumnNames(tableName, columnName.toLowerCase(ENGLISH)); | ||
|
||
String newNameInSql = toColumnNameInSql(columnName + "_new", delimited); |
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 don't test column name with trailing spaces.
- let's use
columnName
here without appending anything - let's use static
a;b$c
column name when creating the table in this test
} | ||
|
||
@Test(dataProvider = "testColumnNameDataProvider") | ||
public void testDropColumnName(String columnName) |
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 meant to merge this with testAddColumnName
.
that would mean two lines more in that test
assertUpdate("ALTER TABLE " + tableName + " DROP COLUMN " + nameInSql);
assertTableColumnNames(tableName, "value");
ws the whole boilerplate of this method.
You can also rename the test method to testAddAndDropColumnName
1980096
to
bc34280
Compare
bc34280
to
7306441
Compare
SQL Server sp_rename doesn't behave as you'd expect. Here's a test to showcase that: In Trino (with this PR applied): trino:dbo> create table dbo.test2 ("crazy; drop table customer" int, "[crazy; drop table customer]" int);
CREATE TABLE
trino:dbo> describe dbo.test2;
Column | Type | Extra | Comment
------------------------------+---------+-------+---------
crazy; drop table customer | integer | |
[crazy; drop table customer] | integer | |
(2 rows)
Query 20220910_104005_00030_ci9mm, FINISHED, 3 nodes
Splits: 17 total, 17 done (100.00%)
0.54 [2 rows, 152B] [3 rows/s, 280B/s]
trino:dbo> alter table dbo.test2 rename column "crazy; drop table customer" to "crazy; drop table nation";
RENAME COLUMN
trino:dbo> alter table dbo.test2 rename column "[crazy; drop table customer]" to "[crazy; drop table nation]";
Query 20220910_104038_00032_ci9mm failed: Unclosed quotation mark after the character string '[crazy; drop table nation], 'COLUMN''.
trino:dbo> describe dbo.test2;
Column | Type | Extra | Comment
------------------------------+---------+-------+---------
crazy; drop table nation | integer | |
[crazy; drop table customer] | integer | |
(2 rows)
Query 20220910_104042_00033_ci9mm, FINISHED, 3 nodes
Splits: 17 total, 17 done (100.00%)
0.46 [2 rows, 150B] [4 rows/s, 329B/s] In SQL ServerCREATE TABLE dbo.test ([crazy; drop table customer] int, "[crazy; drop table customer]" int);
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'test';
-- crazy; drop table customer
-- [crazy; drop table customer]
EXEC sp_rename 'dbo.test.crazy; drop table customer', 'crazy; drop table nation', 'COLUMN';
EXEC sp_rename 'dbo.test.[crazy; drop table customer]', '[crazy; drop table nation]', 'COLUMN';
-- Errors out with SQL Error [15248] [S0001]: Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
EXEC sp_rename 'dbo.test."[crazy; drop table customer]"', '[crazy; drop table nation]', 'COLUMN';
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'test';
-- crazy; drop table nation
-- [crazy; drop table nation] This suggests that the 2nd argument to |
See also this in SQL Server: CREATE TABLE dbo.test3 ([crazy" drop table customer] int, "[crazy drop table customer]" int);
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'test3';
-- crazy" drop table customer
-- [crazy drop table customer]
EXEC sp_rename 'dbo.test3.[crazy" drop table customer]', '[crazy" drop table nation]', 'COLUMN';
EXEC sp_rename 'dbo.test3."[crazy drop table customer]"', '[crazy" drop table customer]', 'COLUMN';
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'test3';
-- [crazy" drop table nation]
-- [crazy" drop table customer] |
i.e. sp_rename treats the 2nd argument always literally but the first argument follows the identifier naming rules and hence needs quoting. Also looks like we should delegate the work of doing the quoting to SQL Server itself using https://docs.microsoft.com/en-us/sql/t-sql/functions/quotename-transact-sql?redirectedfrom=MSDN&view=sql-server-ver16 Something like: SELECT QUOTENAME('crazy; drop table customer');
SELECT QUOTENAME('[crazy; drop table customer');
SELECT QUOTENAME('[crazy; drop table customer]');
SELECT QUOTENAME('[crazy;" " drop table customer]');
SELECT QUOTENAME('"[crazy;" " drop table customer]"');
SELECT QUOTENAME('["[crazy;" " drop table customer]"]'); |
I'll send a draft PR with the |
} | ||
|
||
@Test(dataProvider = "testColumnNameDataProvider") | ||
public void testRenameColumnName(String columnName) |
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.
Why do we need this new method testRenameColumnName
? Why cannot we use/update old one testRenameColumn
? What is the principal difference between them? How the name of these methods describe that difference?
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.
testRenameColumn
should execute some operations to verify RENAME COLUMN
works correctly (e.g. data didn't loss after renaming). testRenameColumnName
should focus on test for column names. I separated these tests because the test purpose is different.
How the name of these methods describe that difference?
Please feel free to send PR if you feel the name isn't clear,
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.
Thank you!
Description
Fixes #13839
Documentation
(x) No documentation is needed.
Release notes
(x) Release notes entries required with the following suggested text: