Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1k
PHOENIX-6343 : Phoenix allows duplicate column names when one of them is a primary key#1118
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -52,6 +52,7 @@ | ||
| import org.apache.phoenix.query.KeyRange; | ||
| import org.apache.phoenix.query.QueryServices; | ||
| import org.apache.phoenix.query.QueryServicesOptions; | ||
| import org.apache.phoenix.schema.ColumnAlreadyExistsException; | ||
| import org.apache.phoenix.schema.PTable; | ||
| import org.apache.phoenix.schema.PTable.ImmutableStorageScheme; | ||
| import org.apache.phoenix.schema.PTable.QualifierEncodingScheme; | ||
| @@ -90,6 +91,136 @@ public void testStartKeyStopKey() throws SQLException { | ||
| assertTrue(splits.size() > 0); | ||
| } | ||
| @Test | ||
| public void testCreateAlterTableWithDuplicateColumn() throws Exception { | ||
| Properties props = new Properties(); | ||
| int failureCount = 0; | ||
| int expectedExecCount = 0; | ||
| String tableName = generateUniqueName(); | ||
| String viewName = generateUniqueName(); | ||
| try (Connection conn = DriverManager.getConnection(getUrl(), props)) { | ||
| try { | ||
| // Case 1 - Adding a column as "Default_CF.Column" in | ||
| // CREATE TABLE query where "Column" is same as single PK Column | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Would be good if before each case there was a brief comment explaining it -- e.g "Same column name, different column type" for the third one. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, on it. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks @gjacoby126 | ||
| + " (name VARCHAR NOT NULL PRIMARY KEY, city VARCHAR," | ||
| + " name1 VARCHAR, name VARCHAR)", tableName)); | ||
| fail("Should have failed with ColumnAlreadyExistsException"); | ||
| } catch (ColumnAlreadyExistsException e) { | ||
| // expected | ||
| failureCount++; | ||
| } | ||
| try { | ||
| // Case 2 - Adding a column as "Default_CF.Column" in CREATE | ||
| // TABLE query where "Default_CF.Column" already exists as non-Pk column | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (name VARCHAR NOT NULL PRIMARY KEY, city VARCHAR," | ||
| + " name1 VARCHAR, name1 VARCHAR)", tableName)); | ||
| fail("Should have failed with ColumnAlreadyExistsException"); | ||
| } catch (ColumnAlreadyExistsException e) { | ||
| // expected | ||
| failureCount++; | ||
| } | ||
| try { | ||
| // Case 3 - Adding a column as "Default_CF.Column" in CREATE | ||
| // TABLE query where "Column" is same as single PK Column. | ||
| // The only diff from Case 1 is that both PK Column and | ||
| // "Default_CF.Column" are of different DataType | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (key1 VARCHAR NOT NULL PRIMARY KEY, city VARCHAR," | ||
| + " name1 VARCHAR, key1 INTEGER)", tableName)); | ||
| fail("Should have failed with ColumnAlreadyExistsException"); | ||
| } catch (ColumnAlreadyExistsException e) { | ||
| // expected | ||
| failureCount++; | ||
| } | ||
| try { | ||
| // Case 4 - Adding a column as "Default_CF.Column" in | ||
| // CREATE TABLE query where "Column" is same as one of the | ||
| // PK Column from Composite PK Columns | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (name VARCHAR NOT NULL, name1 VARCHAR NOT NULL," | ||
| + " name2 VARCHAR, name VARCHAR" | ||
| + " CONSTRAINT PK PRIMARY KEY(name, name1))", tableName)); | ||
| fail("Should have failed with ColumnAlreadyExistsException"); | ||
| } catch (ColumnAlreadyExistsException e) { | ||
| // expected | ||
| failureCount++; | ||
| } | ||
| try { | ||
| // Case 5 - Adding a column as "Default_CF.Column" in | ||
| // CREATE VIEW query where "Column" is same as one of the | ||
| // PK Column from Composite PK Columns derived from parent table | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (name VARCHAR NOT NULL, name1 VARCHAR NOT NULL," | ||
| + " name2 VARCHAR, name3 VARCHAR" | ||
| + " CONSTRAINT PK PRIMARY KEY(name, name1))", tableName)); | ||
| expectedExecCount++; | ||
| conn.createStatement().execute(String.format("CREATE VIEW %s" | ||
| + " (name1 CHAR(5)) AS SELECT * FROM %s", viewName, tableName)); | ||
| fail("Should have failed with ColumnAlreadyExistsException"); | ||
| } catch (ColumnAlreadyExistsException e) { | ||
| // expected | ||
| failureCount++; | ||
| } | ||
| try { | ||
| // Case 6 - Adding a column as "Default_CF.Column" in | ||
| // ALTER TABLE query where "Column" is same as one of the | ||
| // PK Column from Composite PK Columns | ||
| conn.createStatement().execute( | ||
| String.format("DROP TABLE %s", tableName)); | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (name VARCHAR NOT NULL, name1 VARCHAR NOT NULL," | ||
| + " name2 VARCHAR, name3 VARCHAR" | ||
| + " CONSTRAINT PK PRIMARY KEY(name, name1))", tableName)); | ||
| expectedExecCount++; | ||
| conn.createStatement().execute( | ||
| String.format("ALTER TABLE %s ADD name1 INTEGER", tableName)); | ||
| fail("Should have failed with ColumnAlreadyExistsException"); | ||
| } catch (ColumnAlreadyExistsException e) { | ||
| // expected | ||
| failureCount++; | ||
| } | ||
| try { | ||
| // Case 7 - Adding a column as "Default_CF.Column" in | ||
| // ALTER TABLE query where "Column" is same as single PK Column | ||
| conn.createStatement().execute( | ||
| String.format("DROP TABLE %s", tableName)); | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (name VARCHAR NOT NULL PRIMARY KEY, city VARCHAR," | ||
| + " name1 VARCHAR, name2 VARCHAR)", tableName)); | ||
| expectedExecCount++; | ||
| conn.createStatement().execute( | ||
| String.format("ALTER TABLE %s ADD name VARCHAR", tableName)); | ||
| fail("Should have failed with ColumnAlreadyExistsException"); | ||
| } catch (ColumnAlreadyExistsException e) { | ||
| // expected | ||
| failureCount++; | ||
| } | ||
| assertEquals(7, failureCount); | ||
| assertEquals(3, expectedExecCount); | ||
| conn.createStatement().execute( | ||
| String.format("DROP TABLE %s", tableName)); | ||
| // Case 8 - Adding a column as "Non_Default_CF.Column" in | ||
| // CREATE TABLE query where "Column" is same as single PK Column | ||
| // Hence, we allow creating such column | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (name VARCHAR NOT NULL PRIMARY KEY, city VARCHAR," | ||
| + " name1 VARCHAR, a.name VARCHAR)", tableName)); | ||
| conn.createStatement().execute( | ||
| String.format("DROP TABLE %s", tableName)); | ||
| // Case 9 - Adding a column as "Non_Default_CF.Column" in | ||
| // ALTER TABLE query where "Column" is same as single PK Column | ||
| // Hence, we allow creating such column | ||
| conn.createStatement().execute(String.format("CREATE TABLE %s" | ||
| + " (name VARCHAR NOT NULL PRIMARY KEY, city VARCHAR," | ||
| + " name1 VARCHAR, name2 VARCHAR)", tableName)); | ||
| conn.createStatement().execute( | ||
| String.format("ALTER TABLE %s ADD a.name VARCHAR", tableName)); | ||
| } | ||
| } | ||
| @Test | ||
| public void testCreateTable() throws Exception { | ||
| String schemaName = "TEST"; | ||
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 you add a test for adding the identical column with an alter table statement ?
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.
Done in the same test