Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -60,6 +60,21 @@ public CreateTableStatement(CreateTableStatement createTable, List<ColumnDef> co
this.immutableRows = createTable.immutableRows;
}

public CreateTableStatement(CreateTableStatement createTable, PrimaryKeyConstraint pkConstraint,
List<ColumnDef> columns) {
this.tableName = createTable.tableName;
this.tableType = createTable.tableType;
this.columns = ImmutableList.copyOf(columns);
this.pkConstraint = pkConstraint;
this.splitNodes = createTable.splitNodes;
this.bindCount = createTable.bindCount;
this.props = createTable.props;
this.ifNotExists = createTable.ifNotExists;
this.baseTableName = createTable.baseTableName;
this.whereClause = createTable.whereClause;
this.immutableRows = createTable.immutableRows;
}

public CreateTableStatement(CreateTableStatement createTable, ListMultimap<String,Pair<String,Object>> props, List<ColumnDef> columns) {
this.tableName = createTable.tableName;
this.tableType = createTable.tableType;
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,7 +36,7 @@ public class PrimaryKeyConstraint extends NamedNode {
private final Map<ColumnName, Pair<ColumnName, Boolean>> columnNameToRowTimestamp;
private final int numColumnsWithRowTimestamp;

PrimaryKeyConstraint(String name, List<ColumnDefInPkConstraint> columnDefs) {
public PrimaryKeyConstraint(String name, List<ColumnDefInPkConstraint> columnDefs) {
super(name);
if (columnDefs == null) {
this.columns = Collections.<Pair<ColumnName, SortOrder>>emptyList();
Expand DownExpand Up@@ -95,13 +95,13 @@ public boolean equals(Object obj) {
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for(Map.Entry<ColumnName, Pair<ColumnName, SortOrder>> entry : columnNameToSortOrder.entrySet()) {
for (Pair<ColumnName, SortOrder> entry : columns) {
if(sb.length()!=0) {
sb.append(", ");
}
sb.append(entry.getKey());
if(entry.getValue().getSecond() != SortOrder.getDefault()) {
sb.append(" "+entry.getValue().getSecond());
sb.append(entry.getFirst());
if(entry.getSecond() != SortOrder.getDefault()) {
sb.append(" "+entry.getSecond());
}
}
return sb.toString();
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,11 +39,11 @@ public class SchemaToolSynthesisIT {
// Adding new column RELATED_COMMAND
public void testCreateTableStatement_addColumn() throws Exception {
String expected = "CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n" + "SECOND_ID BIGINT NOT NULL,\n"
+ "TYPE VARCHAR,\n" + "STATUS VARCHAR,\n" + "START_TIMESTAMP BIGINT,\n"
+ "END_TIMESTAMP BIGINT,\n" + "PARAMS VARCHAR,\n" + "RESULT VARCHAR,\n"
+ "RELATED_COMMAND BIGINT\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID))\n"
+ "RELATED_COMMAND BIGINT DEFAULT 100\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID))\n"
+ "VERSIONS=1,MULTI_TENANT=false,REPLICATION_SCOPE=1,TTL=31536000";
String baseDDL = filePath+"/alter_table_add.sql";
runAndVerify(expected, baseDDL);
Expand All@@ -53,10 +53,10 @@ public void testCreateTableStatement_addColumn() throws Exception {
// Dropping TYPE column
public void testCreateTableStatement_dropColumn() throws Exception {
String expected = "CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n" + "SECOND_ID BIGINT NOT NULL,\n"
+ "STATUS VARCHAR,\n" + "START_TIMESTAMP BIGINT,\n" + "END_TIMESTAMP BIGINT,\n"
+ "PARAMS VARCHAR,\n" + "RESULT VARCHAR\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID))\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID))\n"
+ "VERSIONS=1,MULTI_TENANT=false,REPLICATION_SCOPE=1,TTL=31536000";
String baseDDL = filePath+"/alter_table_drop.sql";
runAndVerify(expected, baseDDL);
Expand All@@ -66,10 +66,10 @@ public void testCreateTableStatement_dropColumn() throws Exception {
// Changing REPLICATION SCOPE from 1 to 0
public void testCreateTableStatement_changeProperty() throws Exception {
String expected = "CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n" + "SECOND_ID BIGINT NOT NULL,\n"
+ "TYPE VARCHAR,\n" + "STATUS VARCHAR,\n" + "START_TIMESTAMP BIGINT,\n"
+ "END_TIMESTAMP BIGINT,\n" + "PARAMS VARCHAR,\n" + "RESULT VARCHAR\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID))\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID))\n"
+ "MULTI_TENANT=false,REPLICATION_SCOPE=0,TTL=31536000,VERSIONS=1";
String baseDDL = filePath+"/alter_change_property.sql";
runAndVerify(expected, baseDDL);
Expand All@@ -79,10 +79,10 @@ public void testCreateTableStatement_changeProperty() throws Exception {
// Adding DISABLE_MIGRATION property
public void testCreateTableStatement_addProperty() throws Exception {
String expected = "CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n" + "SECOND_ID BIGINT NOT NULL,\n"
+ "TYPE VARCHAR,\n" + "STATUS VARCHAR,\n" + "START_TIMESTAMP BIGINT,\n"
+ "END_TIMESTAMP BIGINT,\n" + "PARAMS VARCHAR,\n" + "RESULT VARCHAR\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID))\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID))\n"
+ "DISABLE_MIGRATION=true,MULTI_TENANT=false,REPLICATION_SCOPE=1,TTL=31536000,VERSIONS=1";
String baseDDL = filePath+"/alter_add_property.sql";
runAndVerify(expected, baseDDL);
Expand All@@ -96,7 +96,7 @@ public void testCreateViewStatement_addColumn() throws Exception {
+ "SOME_ID CHAR(15) NOT NULL,\n" + "DOUBLE1 DECIMAL(12,3),\n"
+ "IS_BOOLEAN BOOLEAN,\n" + "RELATE CHAR(15),\n" + "TEXT1 VARCHAR,\n"
+ "TEXT_READ_ONLY VARCHAR,\n" + "NEW_COLUMN VARCHAR(20)\n"
+ "CONSTRAINT PKVIEW PRIMARY KEY (DATE_TIME1 DESC, SOME_ID, INT1))\n"
+ "CONSTRAINT PKVIEW PRIMARY KEY (DATE_TIME1 DESC, INT1, SOME_ID))\n"
+ "AS SELECT * FROM TEST.SAMPLE_TABLE_VIEW WHERE FILTER_PREFIX = 'abc'";
String baseDDL = filePath+"/alter_view_add.sql";
runAndVerify(expected, baseDDL);
Expand All@@ -109,7 +109,7 @@ public void testCreateViewStatement_dropColumn() throws Exception {
+ "(DATE_TIME1 DATE NOT NULL,\n" + "INT1 BIGINT NOT NULL,\n"
+ "SOME_ID CHAR(15) NOT NULL,\n" + "IS_BOOLEAN BOOLEAN,\n" + "RELATE CHAR(15),\n"
+ "TEXT1 VARCHAR,\n" + "TEXT_READ_ONLY VARCHAR\n"
+ "CONSTRAINT PKVIEW PRIMARY KEY (DATE_TIME1 DESC, SOME_ID, INT1))\n"
+ "CONSTRAINT PKVIEW PRIMARY KEY (DATE_TIME1 DESC, INT1, SOME_ID))\n"
+ "AS SELECT * FROM TEST.SAMPLE_TABLE_VIEW WHERE FILTER_PREFIX = 'abc'";
String baseDDL = filePath+"/alter_view_drop.sql";
runAndVerify(expected, baseDDL);
Expand DownExpand Up@@ -143,6 +143,28 @@ public void testCreateTableStatement_dropTable() throws Exception {
runAndVerify(expected, baseDDL);
}

@Test
// drop table
public void testCreateTableStatement_dropRecreateTable() throws Exception {
String expected = "CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (\n"
+ " ORG_ID CHAR(15) NOT NULL,\n" + " SOME_ANOTHER_ID BIGINT NOT NULL,\n"
+ " TYPE VARCHAR,\n" + " STATUS VARCHAR,\n" + " START_TIMESTAMP BIGINT,\n"
+ " END_TIMESTAMP BIGINT,\n" + " PARAMS VARCHAR, RESULT VARCHAR\n"
+ " CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)\n"
+ ") VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000";
String baseDDL = filePath+"/drop_create_table.sql";
runAndVerify(expected, baseDDL);
}

@Test
// drop table
public void testCreateTableStatement_add_pk() throws Exception {
String expected = "CREATE TABLE IF NOT EXISTS TEST.TABLE_1\n" + "(STATE CHAR(1) NOT NULL,\n"
+ "SOME_ID VARCHAR\n" + "CONSTRAINT PK PRIMARY KEY (STATE, SOME_ID))";
String baseDDL = filePath+"/alter_table_add_pk.sql";
runAndVerify(expected, baseDDL);
}

@Test
// drop table
public void testCreateIndexStatement_dropIndex() throws Exception {
Expand All@@ -161,11 +183,11 @@ private void runAndVerify(String expected, String baseDDL) throws Exception {
// Alter DDL file can have multiple alter statements
public void testMultipleAlterDDL() throws Exception {
String expected = "CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n"
+ "(ORG_ID CHAR(15) NOT NULL,\n" + "SOME_ANOTHER_ID BIGINT NOT NULL,\n" + "SECOND_ID BIGINT NOT NULL,\n"
+ "TYPE VARCHAR,\n" + "STATUS VARCHAR,\n" + "START_TIMESTAMP BIGINT,\n"
+ "END_TIMESTAMP BIGINT,\n" + "PARAMS VARCHAR,\n" + "RESULT VARCHAR,\n"
+ "SOME_NEW_COLUMN BIGINT\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID))\n"
+ "CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID))\n"
+ "MULTI_TENANT=false,REPLICATION_SCOPE=1,TTL=2000,VERSIONS=1";
String baseDDL = filePath+"/alter_table_multiple.sql";
runAndVerify(expected, baseDDL);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,12 +18,13 @@
CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
SECOND_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;

ALTER TABLE TEST.SAMPLE_TABLE SET DISABLE_MIGRATION=true;
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,12 +18,13 @@
CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
SECOND_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;

ALTER TABLE TEST.SAMPLE_TABLE SET REPLICATION_SCOPE=0;
5 changes: 3 additions & 2 deletions phoenix-tools/src/it/resources/synthesis/alter_table_add.sql
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,12 +18,13 @@
CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
SECOND_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;

ALTER TABLE TEST.SAMPLE_TABLE ADD IF NOT EXISTS RELATED_COMMAND BIGINT NULL;
ALTER TABLE TEST.SAMPLE_TABLE ADD IF NOT EXISTS RELATED_COMMAND BIGINT DEFAULT 100;
26 changes: 26 additions & 0 deletions phoenix-tools/src/it/resources/synthesis/alter_table_add_pk.sql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
CREATE TABLE IF NOT EXISTS TEST.TABLE_1 (
STATE CHAR(1) NOT NULL,
CONSTRAINT PK PRIMARY KEY
(
STATE
)
);

ALTER TABLE TEST.TABLE_1 ADD IF NOT EXISTS SOME_ID VARCHAR NULL PRIMARY KEY;
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,12 +18,13 @@
CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
SECOND_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;

ALTER TABLE TEST.SAMPLE_TABLE DROP COLUMN TYPE;
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,12 +18,13 @@
CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
SECOND_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;

ALTER TABLE TEST.SAMPLE_TABLE SET TTL=2000;
Expand Down
42 changes: 42 additions & 0 deletions phoenix-tools/src/it/resources/synthesis/drop_create_table.sql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;

ALTER TABLE TEST.SAMPLE_TABLE ADD IF NOT EXISTS RELATED_COMMAND BIGINT NULL;

DROP TABLE TEST.SAMPLE_TABLE CASCADE;

CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,12 +18,13 @@
CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE (
ORG_ID CHAR(15) NOT NULL,
SOME_ANOTHER_ID BIGINT NOT NULL,
SECOND_ID BIGINT NOT NULL,
TYPE VARCHAR,
STATUS VARCHAR,
START_TIMESTAMP BIGINT,
END_TIMESTAMP BIGINT,
PARAMS VARCHAR, RESULT VARCHAR
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID)
CONSTRAINT PK PRIMARY KEY (ORG_ID, SOME_ANOTHER_ID, SECOND_ID)
) VERSIONS=1,MULTI_TENANT=FALSE,REPLICATION_SCOPE=1,TTL=31536000;

ALTER VIEW TEST.SAMPLE_VIEW ADD NEW_COLUMN VARCHAR(20);
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,7 +67,7 @@ protected static String getCreateIndexSQL(CreateIndexStatement createStmt) {
sb.append(createStmt.getIndexTableName().getTableName()).append("\n")
.append("ON "+createStmt.getTable().getName())
.append("("+createStmt.getIndexConstraint().toString()).append(")");
if (createStmt.getIncludeColumns()!=null) {
if (createStmt.getIncludeColumns()!=null && !createStmt.getIncludeColumns().isEmpty()) {
sb.append("\nINCLUDE ");
sb.append(getColumnListToString(createStmt.getIncludeColumns()));
}
Expand DownExpand Up@@ -137,6 +137,10 @@ private static String getColumnInfoString(ColumnDef cDef) {
buf.append(' ');
buf.append("NOT NULL");
}
if(cDef.getExpression()!=null) {
buf.append(" DEFAULT ");
buf.append(cDef.getExpression());
}

return buf.toString();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,12 +23,14 @@
import org.apache.phoenix.parse.AddColumnStatement;
import org.apache.phoenix.parse.BindableStatement;
import org.apache.phoenix.parse.ColumnDef;
import org.apache.phoenix.parse.ColumnDefInPkConstraint;
import org.apache.phoenix.parse.ColumnName;
import org.apache.phoenix.parse.CreateIndexStatement;
import org.apache.phoenix.parse.CreateTableStatement;
import org.apache.phoenix.parse.DropColumnStatement;
import org.apache.phoenix.parse.DropIndexStatement;
import org.apache.phoenix.parse.DropTableStatement;
import org.apache.phoenix.parse.PrimaryKeyConstraint;
import org.apache.phoenix.parse.SQLParser;

import java.io.BufferedReader;
Expand DownExpand Up@@ -154,7 +156,22 @@ private CreateTableStatement getCreateTableStatement(AddColumnStatement alterSta
} else {
newColDef.addAll(oldColDef);
newColDef.addAll(addStmt.getColumnDefs());
newCreateStmt = new CreateTableStatement(createStmt, newColDef);
PrimaryKeyConstraint oldPKConstraint = createStmt.getPrimaryKeyConstraint();
List<ColumnDefInPkConstraint> pkList = new ArrayList<>();
for(Pair<ColumnName, SortOrder> entry : oldPKConstraint.getColumnNames()) {
ColumnDefInPkConstraint cd = new
ColumnDefInPkConstraint(entry.getFirst(), entry.getSecond(), oldPKConstraint.isColumnRowTimestamp(entry
.getFirst()));
pkList.add(cd);
}
for(ColumnDef cd : addStmt.getColumnDefs()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What if the ALTER tries to add an existing PK column?

@swaroopakswaroopakJun 10, 2021

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

That's a valid point, I am thinking to create a new Jira which will validate if the statements are valid on the mini-cluster (as parsing won't catch this and similar problem) and then only proceed for synthesis. Does that sound okay?

if(cd.isPK()) {
ColumnDefInPkConstraint cdpk = new ColumnDefInPkConstraint(cd.getColumnDefName(), cd.getSortOrder(), cd.isRowTimestamp());
pkList.add(cdpk);
}
}
PrimaryKeyConstraint pkConstraint = new PrimaryKeyConstraint(oldPKConstraint.getName(), pkList);
newCreateStmt = new CreateTableStatement(createStmt, pkConstraint, newColDef);
}
return newCreateStmt;
}
Expand Down