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



##########
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:
       Property multimap is formed with String and Pair as a key-value. Key for 
all properties pair is same "" but the value(pair of property key, value) is 
different. I had to stick to this to create a new CreateTableStatement. Not 
sure if I misunderstood your suggestion. LMK.




-- 
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:
[email protected]


Reply via email to