You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2021/05/03 23:21:00 UTC

[GitHub] [phoenix] gjacoby126 commented on a change in pull request #1217: PHOENIX-6454: Add feature to SchemaTool to get the DDL in specificati…

gjacoby126 commented on a change in pull request #1217:
URL: https://github.com/apache/phoenix/pull/1217#discussion_r625419782



##########
File path: phoenix-tools/src/main/java/org/apache/phoenix/schema/SchemaSynthesisProcessor.java
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.schema;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.hadoop.hbase.util.Pair;
+import org.apache.phoenix.parse.AddColumnStatement;
+import org.apache.phoenix.parse.BindableStatement;
+import org.apache.phoenix.parse.ColumnDef;
+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.SQLParser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SchemaSynthesisProcessor extends SchemaProcessor {
+    public static final String
+            ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH =
+            "Entity name in base and alter DDL don't match";
+    private final String baseDDLFile;
+    private final String alterDDLFile;
+
+    public SchemaSynthesisProcessor(String baseDDLFile, String alterDDLFile) {
+        this.baseDDLFile = baseDDLFile;
+        this.alterDDLFile = alterDDLFile;
+    }
+
+    @Override
+    public String process() throws Exception {
+        String baseDDL = getQueriesFromFile(baseDDLFile).get(0);
+        List <String> alterDDL = getQueriesFromFile(alterDDLFile);
+        for(String s : alterDDL) {
+            baseDDL = synthesize(baseDDL, s);
+        }
+        return baseDDL;
+    }
+
+    private String synthesize(String baseDDL, String alterDDL) throws Exception {
+        BindableStatement createStatement = new SQLParser(baseDDL).parseStatement();
+        BindableStatement alterStatement = new SQLParser(alterDDL).parseStatement();
+        if (createStatement instanceof CreateTableStatement) {
+            CreateTableStatement newCreateStmt = null;
+            CreateTableStatement createStmt = (CreateTableStatement) createStatement;
+            if (alterStatement instanceof AddColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((AddColumnStatement) alterStatement, createStmt);
+            } else if(alterStatement instanceof DropColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((DropColumnStatement) alterStatement, createStmt);
+            }
+            return getCreateTableSQL(newCreateStmt);
+        } else if (createStatement instanceof CreateIndexStatement) {
+            CreateIndexStatement newCreateIndexStmt =
+                    getCreateIndexStatement((CreateIndexStatement) createStatement, alterStatement);
+            return getCreateIndexSQL(newCreateIndexStmt);
+        }
+        return "";

Review comment:
       What about CreateSequenceStatement and CreateFunctionStatement? And if none of the above, shouldn't we get an error of some kind?

##########
File path: phoenix-tools/src/it/java/org/apache/phoenix/schema/SchemaToolSynthesisIT.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.schema;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.net.URL;
+
+import static org.apache.phoenix.schema.SchemaSynthesisProcessor.ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH;
+import static org.apache.phoenix.schema.SchemaToolExtractionIT.runSchemaTool;
+
+public class SchemaToolSynthesisIT {
+
+    private static final String SYNTHESIS_DIR = "synthesis/";
+    URL fileUrl = SchemaToolSynthesisIT.class.getClassLoader().getResource(
+            SYNTHESIS_DIR);
+    String filePath = new File(fileUrl.getFile()).getAbsolutePath();
+
+    @Test
+    public void testCreateTableStatement_addColumn() throws Exception {
+        String expected = "CREATE TABLE IF NOT EXISTS TEST.SAMPLE_TABLE"

Review comment:
       nit: each of these would be easier to read if there was a comment saying what the difference was, such as "Adds RELATED_COMMAND" in this test

##########
File path: phoenix-tools/src/main/java/org/apache/phoenix/schema/SchemaSynthesisProcessor.java
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.schema;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.hadoop.hbase.util.Pair;
+import org.apache.phoenix.parse.AddColumnStatement;
+import org.apache.phoenix.parse.BindableStatement;
+import org.apache.phoenix.parse.ColumnDef;
+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.SQLParser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SchemaSynthesisProcessor extends SchemaProcessor {
+    public static final String
+            ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH =
+            "Entity name in base and alter DDL don't match";
+    private final String baseDDLFile;
+    private final String alterDDLFile;
+
+    public SchemaSynthesisProcessor(String baseDDLFile, String alterDDLFile) {
+        this.baseDDLFile = baseDDLFile;
+        this.alterDDLFile = alterDDLFile;
+    }
+
+    @Override
+    public String process() throws Exception {
+        String baseDDL = getQueriesFromFile(baseDDLFile).get(0);
+        List <String> alterDDL = getQueriesFromFile(alterDDLFile);
+        for(String s : alterDDL) {
+            baseDDL = synthesize(baseDDL, s);
+        }
+        return baseDDL;
+    }
+
+    private String synthesize(String baseDDL, String alterDDL) throws Exception {
+        BindableStatement createStatement = new SQLParser(baseDDL).parseStatement();
+        BindableStatement alterStatement = new SQLParser(alterDDL).parseStatement();
+        if (createStatement instanceof CreateTableStatement) {
+            CreateTableStatement newCreateStmt = null;
+            CreateTableStatement createStmt = (CreateTableStatement) createStatement;
+            if (alterStatement instanceof AddColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((AddColumnStatement) alterStatement, createStmt);
+            } else if(alterStatement instanceof DropColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((DropColumnStatement) alterStatement, createStmt);
+            }
+            return getCreateTableSQL(newCreateStmt);
+        } else if (createStatement instanceof CreateIndexStatement) {
+            CreateIndexStatement newCreateIndexStmt =
+                    getCreateIndexStatement((CreateIndexStatement) createStatement, alterStatement);
+            return getCreateIndexSQL(newCreateIndexStmt);
+        }
+        return "";
+    }
+
+    private CreateIndexStatement getCreateIndexStatement(CreateIndexStatement createStatement,
+            BindableStatement alterStatement) throws Exception {
+        CreateIndexStatement newCreateIndexStmt = null;
+        String tableName = createStatement.getIndexTableName().toString();
+        String tableNameInAlter = ((AddColumnStatement)alterStatement).getTable().toString().trim();
+        sanityCheck(tableName, tableNameInAlter);
+        AddColumnStatement addStmt = (AddColumnStatement) alterStatement;
+        if (addStmt.getColumnDefs() == null) {
+            ListMultimap<String, Pair<String, Object>>
+                    finalProps =
+                    getEffectiveProperties(addStmt, createStatement.getProps());
+            newCreateIndexStmt = new CreateIndexStatement(createStatement, finalProps);
+        }
+        return newCreateIndexStmt;
+    }
+
+    private CreateTableStatement getCreateTableStatement(DropColumnStatement alterStatement,
+            CreateTableStatement createStmt) throws Exception {
+        CreateTableStatement newCreateStmt = null;
+        String tableName = createStmt.getTableName().toString();
+        String tableNameInAlter = alterStatement.getTable().toString().trim();
+        sanityCheck(tableName, tableNameInAlter);
+        List<ColumnDef> oldColumnDef = createStmt.getColumnDefs();
+        List<ColumnDef> newColumnDef = new ArrayList<>();
+        newColumnDef.addAll(oldColumnDef);
+        DropColumnStatement dropStmt = alterStatement;
+        for(ColumnName cName : dropStmt.getColumnRefs()) {
+            for(ColumnDef colDef : oldColumnDef) {
+                if(colDef.getColumnDefName().equals(cName)) {
+                    newColumnDef.remove(colDef);
+                    break;
+                }
+            }
+        }
+        newCreateStmt = new CreateTableStatement(createStmt, newColumnDef);
+        return newCreateStmt;
+    }
+
+    private CreateTableStatement getCreateTableStatement(AddColumnStatement alterStatement,
+            CreateTableStatement createStmt) throws Exception {
+        CreateTableStatement newCreateStmt = null;
+        String tableName = createStmt.getTableName().toString();
+        String tableNameInAlter = alterStatement.getTable().toString().trim();
+        sanityCheck(tableName, tableNameInAlter);
+        AddColumnStatement addStmt = alterStatement;
+        List<ColumnDef> oldColDef = createStmt.getColumnDefs();
+        List<ColumnDef> newColDef = new ArrayList<>();
+        if (addStmt.getColumnDefs() == null) {
+            ListMultimap<String, Pair<String, Object>>
+                    finalProps = getEffectiveProperties(addStmt, createStmt.getProps());
+            newCreateStmt = new CreateTableStatement(createStmt, finalProps, oldColDef);
+        } else {
+            newColDef.addAll(oldColDef);
+            newColDef.addAll(addStmt.getColumnDefs());
+            newCreateStmt = new CreateTableStatement(createStmt, newColDef);
+        }
+        return newCreateStmt;
+    }
+
+    private void sanityCheck(String tableName, String tableNameInAlter) throws Exception {
+        if (!tableName.equalsIgnoreCase(tableNameInAlter)) {
+            throw new Exception(ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH);
+        }
+    }
+
+    private ListMultimap<String, Pair<String, Object>> getEffectiveProperties(
+            AddColumnStatement addStmt, ListMultimap<String, Pair<String, Object>> oldProps) {
+        Map<String, Object> oldPropMap = new HashMap();
+        Map<String, Object> changePropMap = new HashMap();
+
+        for (Pair<String, Object> value : oldProps.values()) {
+            oldPropMap.put(value.getFirst(),value.getSecond());
+        }
+        for (Pair<String, Object> value : addStmt.getProps().values()) {
+            changePropMap.put(value.getFirst(),value.getSecond());
+        }
+
+        oldPropMap.putAll(changePropMap);
+        ListMultimap<String, Pair<String, Object>>
+                finalProps =
+                ArrayListMultimap.<String, Pair<String, Object>>create();
+        for (Map.Entry<String, Object> entry : oldPropMap.entrySet()) {
+            finalProps.put("", Pair.newPair(entry.getKey(), entry.getValue()));
+        }
+        return finalProps;
+    }
+
+    private String getCreateTableSQL(CreateTableStatement createStmt) {

Review comment:
       Good to extract these SQL-building helper functions into their own utility class that can be composed / injected. (For one thing, we're probably going to be adding the ability to output Statements to Avro schemas instead of SQL really soon now over at PHOENIX-6227)

##########
File path: phoenix-tools/src/main/java/org/apache/phoenix/schema/SchemaSynthesisProcessor.java
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.schema;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.hadoop.hbase.util.Pair;
+import org.apache.phoenix.parse.AddColumnStatement;
+import org.apache.phoenix.parse.BindableStatement;
+import org.apache.phoenix.parse.ColumnDef;
+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.SQLParser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SchemaSynthesisProcessor extends SchemaProcessor {
+    public static final String
+            ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH =
+            "Entity name in base and alter DDL don't match";
+    private final String baseDDLFile;
+    private final String alterDDLFile;
+
+    public SchemaSynthesisProcessor(String baseDDLFile, String alterDDLFile) {
+        this.baseDDLFile = baseDDLFile;
+        this.alterDDLFile = alterDDLFile;
+    }
+
+    @Override
+    public String process() throws Exception {
+        String baseDDL = getQueriesFromFile(baseDDLFile).get(0);
+        List <String> alterDDL = getQueriesFromFile(alterDDLFile);
+        for(String s : alterDDL) {
+            baseDDL = synthesize(baseDDL, s);
+        }
+        return baseDDL;
+    }
+
+    private String synthesize(String baseDDL, String alterDDL) throws Exception {
+        BindableStatement createStatement = new SQLParser(baseDDL).parseStatement();
+        BindableStatement alterStatement = new SQLParser(alterDDL).parseStatement();
+        if (createStatement instanceof CreateTableStatement) {
+            CreateTableStatement newCreateStmt = null;
+            CreateTableStatement createStmt = (CreateTableStatement) createStatement;
+            if (alterStatement instanceof AddColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((AddColumnStatement) alterStatement, createStmt);
+            } else if(alterStatement instanceof DropColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((DropColumnStatement) alterStatement, createStmt);
+            }
+            return getCreateTableSQL(newCreateStmt);
+        } else if (createStatement instanceof CreateIndexStatement) {
+            CreateIndexStatement newCreateIndexStmt =
+                    getCreateIndexStatement((CreateIndexStatement) createStatement, alterStatement);
+            return getCreateIndexSQL(newCreateIndexStmt);
+        }
+        return "";
+    }
+
+    private CreateIndexStatement getCreateIndexStatement(CreateIndexStatement createStatement,
+            BindableStatement alterStatement) throws Exception {
+        CreateIndexStatement newCreateIndexStmt = null;
+        String tableName = createStatement.getIndexTableName().toString();
+        String tableNameInAlter = ((AddColumnStatement)alterStatement).getTable().toString().trim();
+        sanityCheck(tableName, tableNameInAlter);
+        AddColumnStatement addStmt = (AddColumnStatement) alterStatement;
+        if (addStmt.getColumnDefs() == null) {
+            ListMultimap<String, Pair<String, Object>>
+                    finalProps =
+                    getEffectiveProperties(addStmt, createStatement.getProps());
+            newCreateIndexStmt = new CreateIndexStatement(createStatement, finalProps);
+        }
+        return newCreateIndexStmt;
+    }
+
+    private CreateTableStatement getCreateTableStatement(DropColumnStatement alterStatement,

Review comment:
       nit: up to now we've been doing parameters as Create, Alter, and this switches to Alter, Create. Good to be consistent. 

##########
File path: phoenix-tools/src/main/java/org/apache/phoenix/schema/SchemaSynthesisProcessor.java
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.schema;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.hadoop.hbase.util.Pair;
+import org.apache.phoenix.parse.AddColumnStatement;
+import org.apache.phoenix.parse.BindableStatement;
+import org.apache.phoenix.parse.ColumnDef;
+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.SQLParser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SchemaSynthesisProcessor extends SchemaProcessor {
+    public static final String
+            ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH =
+            "Entity name in base and alter DDL don't match";
+    private final String baseDDLFile;
+    private final String alterDDLFile;
+
+    public SchemaSynthesisProcessor(String baseDDLFile, String alterDDLFile) {
+        this.baseDDLFile = baseDDLFile;
+        this.alterDDLFile = alterDDLFile;
+    }
+
+    @Override
+    public String process() throws Exception {
+        String baseDDL = getQueriesFromFile(baseDDLFile).get(0);
+        List <String> alterDDL = getQueriesFromFile(alterDDLFile);
+        for(String s : alterDDL) {
+            baseDDL = synthesize(baseDDL, s);
+        }
+        return baseDDL;
+    }
+
+    private String synthesize(String baseDDL, String alterDDL) throws Exception {
+        BindableStatement createStatement = new SQLParser(baseDDL).parseStatement();
+        BindableStatement alterStatement = new SQLParser(alterDDL).parseStatement();
+        if (createStatement instanceof CreateTableStatement) {
+            CreateTableStatement newCreateStmt = null;
+            CreateTableStatement createStmt = (CreateTableStatement) createStatement;
+            if (alterStatement instanceof AddColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((AddColumnStatement) alterStatement, createStmt);
+            } else if(alterStatement instanceof DropColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((DropColumnStatement) alterStatement, createStmt);
+            }
+            return getCreateTableSQL(newCreateStmt);
+        } else if (createStatement instanceof CreateIndexStatement) {
+            CreateIndexStatement newCreateIndexStmt =
+                    getCreateIndexStatement((CreateIndexStatement) createStatement, alterStatement);
+            return getCreateIndexSQL(newCreateIndexStmt);
+        }
+        return "";
+    }
+
+    private CreateIndexStatement getCreateIndexStatement(CreateIndexStatement createStatement,
+            BindableStatement alterStatement) throws Exception {
+        CreateIndexStatement newCreateIndexStmt = null;
+        String tableName = createStatement.getIndexTableName().toString();
+        String tableNameInAlter = ((AddColumnStatement)alterStatement).getTable().toString().trim();

Review comment:
       What if alterStatement isn't an AddColumnStatement? (e.g a drop column). What if we're changing a property? 

##########
File path: phoenix-tools/src/main/java/org/apache/phoenix/schema/SchemaProcessor.java
##########
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.schema;
+
+public class SchemaProcessor {

Review comment:
       Why a class and not an interface?

##########
File path: phoenix-tools/src/main/java/org/apache/phoenix/schema/SchemaSynthesisProcessor.java
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.schema;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.hadoop.hbase.util.Pair;
+import org.apache.phoenix.parse.AddColumnStatement;
+import org.apache.phoenix.parse.BindableStatement;
+import org.apache.phoenix.parse.ColumnDef;
+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.SQLParser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SchemaSynthesisProcessor extends SchemaProcessor {
+    public static final String
+            ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH =
+            "Entity name in base and alter DDL don't match";
+    private final String baseDDLFile;
+    private final String alterDDLFile;
+
+    public SchemaSynthesisProcessor(String baseDDLFile, String alterDDLFile) {
+        this.baseDDLFile = baseDDLFile;
+        this.alterDDLFile = alterDDLFile;
+    }
+
+    @Override
+    public String process() throws Exception {
+        String baseDDL = getQueriesFromFile(baseDDLFile).get(0);
+        List <String> alterDDL = getQueriesFromFile(alterDDLFile);
+        for(String s : alterDDL) {
+            baseDDL = synthesize(baseDDL, s);
+        }
+        return baseDDL;
+    }
+
+    private String synthesize(String baseDDL, String alterDDL) throws Exception {
+        BindableStatement createStatement = new SQLParser(baseDDL).parseStatement();
+        BindableStatement alterStatement = new SQLParser(alterDDL).parseStatement();
+        if (createStatement instanceof CreateTableStatement) {
+            CreateTableStatement newCreateStmt = null;
+            CreateTableStatement createStmt = (CreateTableStatement) createStatement;
+            if (alterStatement instanceof AddColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((AddColumnStatement) alterStatement, createStmt);
+            } else if(alterStatement instanceof DropColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((DropColumnStatement) alterStatement, createStmt);
+            }
+            return getCreateTableSQL(newCreateStmt);
+        } else if (createStatement instanceof CreateIndexStatement) {
+            CreateIndexStatement newCreateIndexStmt =
+                    getCreateIndexStatement((CreateIndexStatement) createStatement, alterStatement);
+            return getCreateIndexSQL(newCreateIndexStmt);
+        }
+        return "";
+    }
+
+    private CreateIndexStatement getCreateIndexStatement(CreateIndexStatement createStatement,
+            BindableStatement alterStatement) throws Exception {
+        CreateIndexStatement newCreateIndexStmt = null;
+        String tableName = createStatement.getIndexTableName().toString();
+        String tableNameInAlter = ((AddColumnStatement)alterStatement).getTable().toString().trim();
+        sanityCheck(tableName, tableNameInAlter);
+        AddColumnStatement addStmt = (AddColumnStatement) alterStatement;
+        if (addStmt.getColumnDefs() == null) {
+            ListMultimap<String, Pair<String, Object>>
+                    finalProps =
+                    getEffectiveProperties(addStmt, createStatement.getProps());
+            newCreateIndexStmt = new CreateIndexStatement(createStatement, finalProps);
+        }
+        return newCreateIndexStmt;
+    }
+
+    private CreateTableStatement getCreateTableStatement(DropColumnStatement alterStatement,
+            CreateTableStatement createStmt) throws Exception {
+        CreateTableStatement newCreateStmt = null;
+        String tableName = createStmt.getTableName().toString();
+        String tableNameInAlter = alterStatement.getTable().toString().trim();
+        sanityCheck(tableName, tableNameInAlter);
+        List<ColumnDef> oldColumnDef = createStmt.getColumnDefs();
+        List<ColumnDef> newColumnDef = new ArrayList<>();
+        newColumnDef.addAll(oldColumnDef);
+        DropColumnStatement dropStmt = alterStatement;
+        for(ColumnName cName : dropStmt.getColumnRefs()) {
+            for(ColumnDef colDef : oldColumnDef) {
+                if(colDef.getColumnDefName().equals(cName)) {
+                    newColumnDef.remove(colDef);
+                    break;
+                }
+            }
+        }
+        newCreateStmt = new CreateTableStatement(createStmt, newColumnDef);
+        return newCreateStmt;
+    }
+
+    private CreateTableStatement getCreateTableStatement(AddColumnStatement alterStatement,
+            CreateTableStatement createStmt) throws Exception {
+        CreateTableStatement newCreateStmt = null;
+        String tableName = createStmt.getTableName().toString();
+        String tableNameInAlter = alterStatement.getTable().toString().trim();
+        sanityCheck(tableName, tableNameInAlter);
+        AddColumnStatement addStmt = alterStatement;
+        List<ColumnDef> oldColDef = createStmt.getColumnDefs();
+        List<ColumnDef> newColDef = new ArrayList<>();
+        if (addStmt.getColumnDefs() == null) {
+            ListMultimap<String, Pair<String, Object>>
+                    finalProps = getEffectiveProperties(addStmt, createStmt.getProps());
+            newCreateStmt = new CreateTableStatement(createStmt, finalProps, oldColDef);
+        } else {
+            newColDef.addAll(oldColDef);
+            newColDef.addAll(addStmt.getColumnDefs());
+            newCreateStmt = new CreateTableStatement(createStmt, newColDef);
+        }
+        return newCreateStmt;
+    }
+
+    private void sanityCheck(String tableName, String tableNameInAlter) throws Exception {
+        if (!tableName.equalsIgnoreCase(tableNameInAlter)) {
+            throw new Exception(ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH);
+        }
+    }
+
+    private ListMultimap<String, Pair<String, Object>> getEffectiveProperties(
+            AddColumnStatement addStmt, ListMultimap<String, Pair<String, Object>> oldProps) {
+        Map<String, Object> oldPropMap = new HashMap();
+        Map<String, Object> changePropMap = new HashMap();
+
+        for (Pair<String, Object> value : oldProps.values()) {
+            oldPropMap.put(value.getFirst(),value.getSecond());
+        }
+        for (Pair<String, Object> value : addStmt.getProps().values()) {
+            changePropMap.put(value.getFirst(),value.getSecond());
+        }
+
+        oldPropMap.putAll(changePropMap);
+        ListMultimap<String, Pair<String, Object>>
+                finalProps =
+                ArrayListMultimap.<String, Pair<String, Object>>create();
+        for (Map.Entry<String, Object> entry : oldPropMap.entrySet()) {
+            finalProps.put("", Pair.newPair(entry.getKey(), entry.getValue()));

Review comment:
       What's the purpose of a hard-coded ["" -> [key -> value]] instead of just [key -> value]? Can't be duplicates, because oldPropMap was already a multi-map, right?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org